blob: 2f3dda4a107387aa8c453d79d3b797bad1338abf [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 Gorcunov49e8f692010-11-11 15:06:12 +0300486#ifdef NASM_TRACE
487
488#define dump_token(t) raw_dump_token(t, __FILE__, __LINE__, __func__);
489static void raw_dump_token(Token *token, const char *file, int line, const char *func)
490{
491 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
492 if (token) {
493 Token *t;
494 list_for_each(t, token) {
495 if (t->text)
496 printf("'%s' ", t->text);
497 }
498 printf("\n");
499 }
500}
501
502#endif
503
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300504/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700505 * nasm_unquote with error if the string contains NUL characters.
506 * If the string contains NUL characters, issue an error and return
507 * the C len, i.e. truncate at the NUL.
508 */
509static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
510{
511 size_t len = nasm_unquote(qstr, NULL);
512 size_t clen = strlen(qstr);
513
514 if (len != clen)
515 error(ERR_NONFATAL, "NUL character in `%s' directive",
516 pp_directives[directive]);
517
518 return clen;
519}
520
521/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700522 * In-place reverse a list of tokens.
523 */
524static Token *reverse_tokens(Token *t)
525{
526 Token *prev = NULL;
527 Token *next;
528
529 while (t) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500530 next = t->next;
531 t->next = prev;
532 prev = t;
533 t = next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300534 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700535
536 return prev;
537}
538
539/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300540 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000541 * front of them. We do it here because I could not find any other
542 * place to do it for the moment, and it is a hack (ideally it would
543 * be nice to be able to use the NASM pre-processor to do it).
544 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000545static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000546{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000547 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400548 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000549
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400550 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000551
552 /* Binary search for the directive name */
553 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400554 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400555 q = nasm_skip_word(p);
556 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000557 if (len) {
558 oldchar = p[len];
559 p[len] = 0;
560 while (j - i > 1) {
561 k = (j + i) / 2;
562 m = nasm_stricmp(p, tasm_directives[k]);
563 if (m == 0) {
564 /* We have found a directive, so jam a % in front of it
565 * so that NASM will then recognise it as one if it's own.
566 */
567 p[len] = oldchar;
568 len = strlen(p);
569 oldline = line;
570 line = nasm_malloc(len + 2);
571 line[0] = '%';
572 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700573 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300574 * NASM does not recognise IFDIFI, so we convert
575 * it to %if 0. This is not used in NASM
576 * compatible code, but does need to parse for the
577 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000578 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700579 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000580 } else {
581 memcpy(line + 1, p, len + 1);
582 }
583 nasm_free(oldline);
584 return line;
585 } else if (m < 0) {
586 j = k;
587 } else
588 i = k;
589 }
590 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000591 }
592 return line;
593}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000594
H. Peter Anvin76690a12002-04-30 20:52:49 +0000595/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000596 * The pre-preprocessing stage... This function translates line
597 * number indications as they emerge from GNU cpp (`# lineno "file"
598 * flags') into NASM preprocessor line number indications (`%line
599 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000600 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000601static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000602{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000603 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000604 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000605
H. Peter Anvine2c80182005-01-15 22:15:51 +0000606 if (line[0] == '#' && line[1] == ' ') {
607 oldline = line;
608 fname = oldline + 2;
609 lineno = atoi(fname);
610 fname += strspn(fname, "0123456789 ");
611 if (*fname == '"')
612 fname++;
613 fnlen = strcspn(fname, "\"");
614 line = nasm_malloc(20 + fnlen);
615 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
616 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000617 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000618 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000619 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000620 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000621}
622
623/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000624 * Free a linked list of tokens.
625 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000626static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000627{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400628 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000629 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000630}
631
632/*
633 * Free a linked list of lines.
634 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000635static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000636{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400637 Line *l, *tmp;
638 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000639 free_tlist(l->first);
640 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000641 }
642}
643
644/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500645 * Free an ExpDef
H. Peter Anvineba20a72002-04-30 20:53:55 +0000646 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500647static void free_expdef(ExpDef * ed)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000648{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500649 nasm_free(ed->name);
650 free_tlist(ed->dlist);
651 nasm_free(ed->defaults);
652 free_llist(ed->line);
653 nasm_free(ed);
654}
655
656/*
657 * Free an ExpInv
658 */
659static void free_expinv(ExpInv * ei)
660{
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300661 if (ei->name != NULL)
662 nasm_free(ei->name);
663 if (ei->label_text != NULL)
664 nasm_free(ei->label_text);
665 nasm_free(ei);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000666}
667
668/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700669 * Free all currently defined macros, and free the hash tables
670 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700671static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700672{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400673 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700674 const char *key;
675 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700676
H. Peter Anvin072771e2008-05-22 13:17:51 -0700677 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300678 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400679 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300680 nasm_free(s->name);
681 free_tlist(s->expansion);
682 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300683 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700684 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700685 hash_free(smt);
686}
687
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500688static void free_expdef_table(struct hash_table *edt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700689{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500690 ExpDef *ed, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700691 const char *key;
692 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700693
694 it = NULL;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500695 while ((ed = hash_iterate(edt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300696 nasm_free((void *)key);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500697 list_for_each_safe(ed ,tmp, ed)
698 free_expdef(ed);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700699 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500700 hash_free(edt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700701}
702
703static void free_macros(void)
704{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700705 free_smacro_table(&smacros);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500706 free_expdef_table(&expdefs);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700707}
708
709/*
710 * Initialize the hash tables
711 */
712static void init_macros(void)
713{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700714 hash_init(&smacros, HASH_LARGE);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500715 hash_init(&expdefs, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700716}
717
718/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000719 * Pop the context stack.
720 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000721static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000722{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000723 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000724
725 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700726 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000727 nasm_free(c->name);
728 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000729}
730
H. Peter Anvin072771e2008-05-22 13:17:51 -0700731/*
732 * Search for a key in the hash index; adding it if necessary
733 * (in which case we initialize the data pointer to NULL.)
734 */
735static void **
736hash_findi_add(struct hash_table *hash, const char *str)
737{
738 struct hash_insert hi;
739 void **r;
740 char *strx;
741
742 r = hash_findi(hash, str, &hi);
743 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300744 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700745
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300746 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700747 return hash_add(&hi, strx, NULL);
748}
749
750/*
751 * Like hash_findi, but returns the data element rather than a pointer
752 * to it. Used only when not adding a new element, hence no third
753 * argument.
754 */
755static void *
756hash_findix(struct hash_table *hash, const char *str)
757{
758 void **p;
759
760 p = hash_findi(hash, str, NULL);
761 return p ? *p : NULL;
762}
763
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400764/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500765 * read line from standard macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400766 * if there no more left -- return NULL
767 */
768static char *line_from_stdmac(void)
769{
770 unsigned char c;
771 const unsigned char *p = stdmacpos;
772 char *line, *q;
773 size_t len = 0;
774
775 if (!stdmacpos)
776 return NULL;
777
778 while ((c = *p++)) {
779 if (c >= 0x80)
780 len += pp_directives_len[c - 0x80] + 1;
781 else
782 len++;
783 }
784
785 line = nasm_malloc(len + 1);
786 q = line;
787 while ((c = *stdmacpos++)) {
788 if (c >= 0x80) {
789 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
790 q += pp_directives_len[c - 0x80];
791 *q++ = ' ';
792 } else {
793 *q++ = c;
794 }
795 }
796 stdmacpos = p;
797 *q = '\0';
798
799 if (!*stdmacpos) {
800 /* This was the last of the standard macro chain... */
801 stdmacpos = NULL;
802 if (any_extrastdmac) {
803 stdmacpos = extrastdmac;
804 any_extrastdmac = false;
805 } else if (do_predef) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300806 ExpInv *ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400807 Line *pd, *l;
808 Token *head, **tail, *t;
809
810 /*
811 * Nasty hack: here we push the contents of
812 * `predef' on to the top-level expansion stack,
813 * since this is the most convenient way to
814 * implement the pre-include and pre-define
815 * features.
816 */
817 list_for_each(pd, predef) {
818 head = NULL;
819 tail = &head;
820 list_for_each(t, pd->first) {
821 *tail = new_Token(NULL, t->type, t->text, 0);
822 tail = &(*tail)->next;
823 }
824
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500825 l = new_Line();
826 l->first = head;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300827 ei = new_ExpInv(EXP_PREDEF, NULL);
828 ei->current = l;
829 ei->emitting = true;
830 ei->prev = istk->expansion;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500831 istk->expansion = ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400832 }
833 do_predef = false;
834 }
835 }
836
837 return line;
838}
839
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000840#define BUF_DELTA 512
841/*
842 * Read a line from the top file in istk, handling multiple CR/LFs
843 * at the end of the line read, and handling spurious ^Zs. Will
844 * return lines from the standard macro set if this has not already
845 * been done.
846 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000847static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000848{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000849 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000850 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000851
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400852 /*
853 * standart macros set (predefined) goes first
854 */
855 p = line_from_stdmac();
856 if (p)
857 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700858
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400859 /*
860 * regular read from a file
861 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000862 bufsize = BUF_DELTA;
863 buffer = nasm_malloc(BUF_DELTA);
864 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000865 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000866 while (1) {
867 q = fgets(p, bufsize - (p - buffer), istk->fp);
868 if (!q)
869 break;
870 p += strlen(p);
871 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300872 /*
873 * Convert backslash-CRLF line continuation sequences into
874 * nothing at all (for DOS and Windows)
875 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000876 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
877 p -= 3;
878 *p = 0;
879 continued_count++;
880 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300881 /*
882 * Also convert backslash-LF line continuation sequences into
883 * nothing at all (for Unix)
884 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000885 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
886 p -= 2;
887 *p = 0;
888 continued_count++;
889 } else {
890 break;
891 }
892 }
893 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000894 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000895 bufsize += BUF_DELTA;
896 buffer = nasm_realloc(buffer, bufsize);
897 p = buffer + offset; /* prevent stale-pointer problems */
898 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000899 }
900
H. Peter Anvine2c80182005-01-15 22:15:51 +0000901 if (!q && p == buffer) {
902 nasm_free(buffer);
903 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000904 }
905
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300906 src_set_linnum(src_get_linnum() + istk->lineinc +
907 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000908
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000909 /*
910 * Play safe: remove CRs as well as LFs, if any of either are
911 * present at the end of the line.
912 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000913 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000914 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000915
916 /*
917 * Handle spurious ^Z, which may be inserted into source files
918 * by some file transfer utilities.
919 */
920 buffer[strcspn(buffer, "\032")] = '\0';
921
H. Peter Anvin734b1882002-04-30 21:01:08 +0000922 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000923
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000924 return buffer;
925}
926
927/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000928 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000929 * don't need to parse the value out of e.g. numeric tokens: we
930 * simply split one string into many.
931 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000932static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000933{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700934 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000935 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000936 Token *list = NULL;
937 Token *t, **tail = &list;
Keith Kanios6faad4e2010-12-18 14:08:02 -0600938 bool verbose = true;
939
940 if ((defining != NULL) && (defining->ignoring == true)) {
941 verbose = false;
942 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000943
H. Peter Anvine2c80182005-01-15 22:15:51 +0000944 while (*line) {
945 p = line;
946 if (*p == '%') {
947 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300948 if (*p == '+' && !nasm_isdigit(p[1])) {
949 p++;
950 type = TOK_PASTE;
951 } else if (nasm_isdigit(*p) ||
952 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953 do {
954 p++;
955 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700956 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000957 type = TOK_PREPROC_ID;
958 } else if (*p == '{') {
959 p++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500960 while (*p && *p != '}') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000961 p[-1] = *p;
962 p++;
963 }
964 p[-1] = '\0';
965 if (*p)
966 p++;
967 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300968 } else if (*p == '[') {
969 int lvl = 1;
970 line += 2; /* Skip the leading %[ */
971 p++;
972 while (lvl && (c = *p++)) {
973 switch (c) {
974 case ']':
975 lvl--;
976 break;
977 case '%':
978 if (*p == '[')
979 lvl++;
980 break;
981 case '\'':
982 case '\"':
983 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400984 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300985 break;
986 default:
987 break;
988 }
989 }
990 p--;
991 if (*p)
992 *p++ = '\0';
Keith Kanios6faad4e2010-12-18 14:08:02 -0600993 if (lvl && verbose)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300994 error(ERR_NONFATAL, "unterminated %[ construct");
995 type = TOK_INDIRECT;
996 } else if (*p == '?') {
997 type = TOK_PREPROC_Q; /* %? */
998 p++;
999 if (*p == '?') {
1000 type = TOK_PREPROC_QQ; /* %?? */
1001 p++;
1002 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001003 } else if (*p == '!') {
1004 type = TOK_PREPROC_ID;
1005 p++;
1006 if (isidchar(*p)) {
1007 do {
1008 p++;
1009 } while (isidchar(*p));
1010 } else if (*p == '\'' || *p == '\"' || *p == '`') {
1011 p = nasm_skip_string(p);
1012 if (*p)
1013 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001014 else if(verbose)
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001015 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
1016 } else {
1017 /* %! without string or identifier */
1018 type = TOK_OTHER; /* Legacy behavior... */
1019 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001020 } else if (isidchar(*p) ||
1021 ((*p == '!' || *p == '%' || *p == '$') &&
1022 isidchar(p[1]))) {
1023 do {
1024 p++;
1025 }
1026 while (isidchar(*p));
1027 type = TOK_PREPROC_ID;
1028 } else {
1029 type = TOK_OTHER;
1030 if (*p == '%')
1031 p++;
1032 }
1033 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1034 type = TOK_ID;
1035 p++;
1036 while (*p && isidchar(*p))
1037 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001038 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001039 /*
1040 * A string token.
1041 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001042 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001043 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001044
H. Peter Anvine2c80182005-01-15 22:15:51 +00001045 if (*p) {
1046 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001047 } else if(verbose) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001048 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001049 /* Handling unterminated strings by UNV */
1050 /* type = -1; */
1051 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001052 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001053 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001054 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001055 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001056 bool is_hex = false;
1057 bool is_float = false;
1058 bool has_e = false;
1059 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001060
H. Peter Anvine2c80182005-01-15 22:15:51 +00001061 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001062 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001063 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001064
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001065 if (*p == '$') {
1066 p++;
1067 is_hex = true;
1068 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001069
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001070 for (;;) {
1071 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001072
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001073 if (!is_hex && (c == 'e' || c == 'E')) {
1074 has_e = true;
1075 if (*p == '+' || *p == '-') {
1076 /*
1077 * e can only be followed by +/- if it is either a
1078 * prefixed hex number or a floating-point number
1079 */
1080 p++;
1081 is_float = true;
1082 }
1083 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1084 is_hex = true;
1085 } else if (c == 'P' || c == 'p') {
1086 is_float = true;
1087 if (*p == '+' || *p == '-')
1088 p++;
1089 } else if (isnumchar(c) || c == '_')
1090 ; /* just advance */
1091 else if (c == '.') {
1092 /*
1093 * we need to deal with consequences of the legacy
1094 * parser, like "1.nolist" being two tokens
1095 * (TOK_NUMBER, TOK_ID) here; at least give it
1096 * a shot for now. In the future, we probably need
1097 * a flex-based scanner with proper pattern matching
1098 * to do it as well as it can be done. Nothing in
1099 * the world is going to help the person who wants
1100 * 0x123.p16 interpreted as two tokens, though.
1101 */
1102 r = p;
1103 while (*r == '_')
1104 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001105
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001106 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1107 (!is_hex && (*r == 'e' || *r == 'E')) ||
1108 (*r == 'p' || *r == 'P')) {
1109 p = r;
1110 is_float = true;
1111 } else
1112 break; /* Terminate the token */
1113 } else
1114 break;
1115 }
1116 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001117
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001118 if (p == line+1 && *line == '$') {
1119 type = TOK_OTHER; /* TOKEN_HERE */
1120 } else {
1121 if (has_e && !is_hex) {
1122 /* 1e13 is floating-point, but 1e13h is not */
1123 is_float = true;
1124 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001125
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001126 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1127 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001128 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001130 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001131 /*
1132 * Whitespace just before end-of-line is discarded by
1133 * pretending it's a comment; whitespace just before a
1134 * comment gets lumped into the comment.
1135 */
1136 if (!*p || *p == ';') {
1137 type = TOK_COMMENT;
1138 while (*p)
1139 p++;
1140 }
1141 } else if (*p == ';') {
1142 type = TOK_COMMENT;
1143 while (*p)
1144 p++;
1145 } else {
1146 /*
1147 * Anything else is an operator of some kind. We check
1148 * for all the double-character operators (>>, <<, //,
1149 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001150 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151 */
1152 type = TOK_OTHER;
1153 if ((p[0] == '>' && p[1] == '>') ||
1154 (p[0] == '<' && p[1] == '<') ||
1155 (p[0] == '/' && p[1] == '/') ||
1156 (p[0] == '<' && p[1] == '=') ||
1157 (p[0] == '>' && p[1] == '=') ||
1158 (p[0] == '=' && p[1] == '=') ||
1159 (p[0] == '!' && p[1] == '=') ||
1160 (p[0] == '<' && p[1] == '>') ||
1161 (p[0] == '&' && p[1] == '&') ||
1162 (p[0] == '|' && p[1] == '|') ||
1163 (p[0] == '^' && p[1] == '^')) {
1164 p++;
1165 }
1166 p++;
1167 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001168
H. Peter Anvine2c80182005-01-15 22:15:51 +00001169 /* Handling unterminated string by UNV */
1170 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001171 {
1172 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1173 t->text[p-line] = *line;
1174 tail = &t->next;
1175 }
1176 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177 if (type != TOK_COMMENT) {
1178 *tail = t = new_Token(NULL, type, line, p - line);
1179 tail = &t->next;
1180 }
1181 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001182 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001183 return list;
1184}
1185
H. Peter Anvince616072002-04-30 21:02:23 +00001186/*
1187 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001188 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001189 * deleted only all at once by the delete_Blocks function.
1190 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001191static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001192{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001193 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001194
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001195 /* first, get to the end of the linked list */
1196 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001197 b = b->next;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001198
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001199 /* now allocate the requested chunk */
1200 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001201
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001202 /* now allocate a new block for the next request */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001203 b->next = nasm_zalloc(sizeof(Blocks));
1204
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001205 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001206}
1207
1208/*
1209 * this function deletes all managed blocks of memory
1210 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001211static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001212{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001213 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001214
H. Peter Anvin70653092007-10-19 14:42:29 -07001215 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001216 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001217 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001218 * free it.
1219 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001220 while (b) {
1221 if (b->chunk)
1222 nasm_free(b->chunk);
1223 a = b;
1224 b = b->next;
1225 if (a != &blocks)
1226 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001227 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001228}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001229
1230/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001231 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001232 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001233 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001234 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001235static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001236 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001237{
1238 Token *t;
1239 int i;
1240
H. Peter Anvin89cee572009-07-15 09:16:54 -04001241 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001242 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1243 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1244 freeTokens[i].next = &freeTokens[i + 1];
1245 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001246 }
1247 t = freeTokens;
1248 freeTokens = t->next;
1249 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001250 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001251 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001252 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001253 t->text = NULL;
1254 } else {
1255 if (txtlen == 0)
1256 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001257 t->text = nasm_malloc(txtlen+1);
1258 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001259 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001260 }
1261 return t;
1262}
1263
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001264static Token *copy_Token(Token * tline)
1265{
1266 Token *t, *tt, *first = NULL, *prev = NULL;
1267 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001268 for (tt = tline; tt != NULL; tt = tt->next) {
1269 if (!freeTokens) {
1270 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1271 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1272 freeTokens[i].next = &freeTokens[i + 1];
1273 freeTokens[i].next = NULL;
1274 }
1275 t = freeTokens;
1276 freeTokens = t->next;
1277 t->next = NULL;
1278 t->text = tt->text ? nasm_strdup(tt->text) : NULL;
1279 t->a.mac = tt->a.mac;
1280 t->a.len = tt->a.len;
1281 t->type = tt->type;
1282 if (prev != NULL) {
1283 prev->next = t;
1284 } else {
1285 first = t;
1286 }
1287 prev = t;
1288 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001289 return first;
1290}
1291
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001293{
1294 Token *next = t->next;
1295 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001296 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001297 freeTokens = t;
1298 return next;
1299}
1300
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001301/*
1302 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001303 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1304 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001305 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001306static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001307{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001308 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001309 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001310 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001311 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001312
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001313 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001314 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001315 char *v;
1316 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001317
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001318 v = t->text + 2;
1319 if (*v == '\'' || *v == '\"' || *v == '`') {
1320 size_t len = nasm_unquote(v, NULL);
1321 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001322
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001323 if (len != clen) {
1324 error(ERR_NONFATAL | ERR_PASS1,
1325 "NUL character in %! string");
1326 v = NULL;
1327 }
1328 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001329
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001330 if (v) {
1331 char *p = getenv(v);
1332 if (!p) {
1333 error(ERR_NONFATAL | ERR_PASS1,
1334 "nonexistent environment variable `%s'", v);
1335 p = "";
1336 }
1337 t->text = nasm_strdup(p);
1338 }
1339 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001341
H. Peter Anvine2c80182005-01-15 22:15:51 +00001342 /* Expand local macros here and not during preprocessing */
1343 if (expand_locals &&
1344 t->type == TOK_PREPROC_ID && t->text &&
1345 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001346 const char *q;
1347 char *p;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001348 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001349 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001350 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001351 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001352 p = nasm_strcat(buffer, q);
1353 nasm_free(t->text);
1354 t->text = p;
1355 }
1356 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001357
1358 /* Expand %? and %?? directives */
Keith Kanios3136d482010-11-13 09:34:34 -06001359 if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001360 ((t->type == TOK_PREPROC_Q) ||
1361 (t->type == TOK_PREPROC_QQ))) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001362 ExpInv *ei;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001363 for (ei = istk->expansion; ei != NULL; ei = ei->prev){
1364 if (ei->type == EXP_MMACRO) {
1365 nasm_free(t->text);
1366 if (t->type == TOK_PREPROC_Q) {
1367 t->text = nasm_strdup(ei->name);
1368 } else {
1369 t->text = nasm_strdup(ei->def->name);
1370 }
1371 break;
1372 }
1373 }
1374 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001375
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001376 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001377 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001378 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001379 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001380 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001381
H. Peter Anvin734b1882002-04-30 21:01:08 +00001382 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001383
1384 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001385 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001386 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001387 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001388 q = t->text;
1389 while (*q)
1390 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001391 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001392 }
1393 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001394
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001395 return line;
1396}
1397
1398/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001399 * Initialize a new Line
1400 */
Cyrill Gorcunov29cb0bb2010-11-11 11:19:43 +03001401static inline Line *new_Line(void)
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001402{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001403 return (Line *)nasm_zalloc(sizeof(Line));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001404}
1405
1406
1407/*
1408 * Initialize a new Expansion Definition
1409 */
1410static ExpDef *new_ExpDef(int exp_type)
1411{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001412 ExpDef *ed = (ExpDef*)nasm_zalloc(sizeof(ExpDef));
1413 ed->type = exp_type;
1414 ed->casesense = true;
1415 ed->state = COND_NEVER;
1416
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001417 return ed;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001418}
1419
1420
1421/*
1422 * Initialize a new Expansion Instance
1423 */
1424static ExpInv *new_ExpInv(int exp_type, ExpDef *ed)
1425{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001426 ExpInv *ei = (ExpInv*)nasm_zalloc(sizeof(ExpInv));
1427 ei->type = exp_type;
1428 ei->def = ed;
1429 ei->unique = ++unique;
1430
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001431 if ((istk->mmac_depth < 1) &&
1432 (istk->expansion == NULL) &&
1433 (ed != NULL) &&
1434 (ed->type != EXP_MMACRO) &&
1435 (ed->type != EXP_REP) &&
1436 (ed->type != EXP_WHILE)) {
1437 ei->linnum = src_get_linnum();
1438 src_set_linnum(ei->linnum - ed->linecount - 1);
1439 } else {
1440 ei->linnum = -1;
1441 }
1442 if ((istk->expansion == NULL) ||
1443 (ei->type == EXP_MMACRO)) {
1444 ei->relno = 0;
1445 } else {
1446 ei->relno = istk->expansion->lineno;
1447 if (ed != NULL) {
1448 ei->relno -= (ed->linecount + 1);
1449 }
1450 }
1451 return ei;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001452}
1453
1454/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001455 * A scanner, suitable for use by the expression evaluator, which
1456 * operates on a line of Tokens. Expects a pointer to a pointer to
1457 * the first token in the line to be passed in as its private_data
1458 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001459 *
1460 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001461 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001463{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001464 Token **tlineptr = private_data;
1465 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001466 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001467
H. Peter Anvine2c80182005-01-15 22:15:51 +00001468 do {
1469 tline = *tlineptr;
1470 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001471 } while (tline && (tline->type == TOK_WHITESPACE ||
1472 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001473
1474 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001475 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001476
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001477 tokval->t_charptr = tline->text;
1478
H. Peter Anvin76690a12002-04-30 20:52:49 +00001479 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001480 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001481 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001482 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001483
H. Peter Anvine2c80182005-01-15 22:15:51 +00001484 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001485 p = tokval->t_charptr = tline->text;
1486 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001487 tokval->t_charptr++;
1488 return tokval->t_type = TOKEN_ID;
1489 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001490
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001491 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001492 if (r >= p+MAX_KEYWORD)
1493 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001494 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001495 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001496 *s = '\0';
1497 /* right, so we have an identifier sitting in temp storage. now,
1498 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001499 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001500 }
1501
H. Peter Anvine2c80182005-01-15 22:15:51 +00001502 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001503 bool rn_error;
1504 tokval->t_integer = readnum(tline->text, &rn_error);
1505 tokval->t_charptr = tline->text;
1506 if (rn_error)
1507 return tokval->t_type = TOKEN_ERRNUM;
1508 else
1509 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001510 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001511
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001512 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001513 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001514 }
1515
H. Peter Anvine2c80182005-01-15 22:15:51 +00001516 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001517 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001518
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001519 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001520 tokval->t_charptr = tline->text;
1521 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001522
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001523 if (ep[0] != bq || ep[1] != '\0')
1524 return tokval->t_type = TOKEN_ERRSTR;
1525 else
1526 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001527 }
1528
H. Peter Anvine2c80182005-01-15 22:15:51 +00001529 if (tline->type == TOK_OTHER) {
1530 if (!strcmp(tline->text, "<<"))
1531 return tokval->t_type = TOKEN_SHL;
1532 if (!strcmp(tline->text, ">>"))
1533 return tokval->t_type = TOKEN_SHR;
1534 if (!strcmp(tline->text, "//"))
1535 return tokval->t_type = TOKEN_SDIV;
1536 if (!strcmp(tline->text, "%%"))
1537 return tokval->t_type = TOKEN_SMOD;
1538 if (!strcmp(tline->text, "=="))
1539 return tokval->t_type = TOKEN_EQ;
1540 if (!strcmp(tline->text, "<>"))
1541 return tokval->t_type = TOKEN_NE;
1542 if (!strcmp(tline->text, "!="))
1543 return tokval->t_type = TOKEN_NE;
1544 if (!strcmp(tline->text, "<="))
1545 return tokval->t_type = TOKEN_LE;
1546 if (!strcmp(tline->text, ">="))
1547 return tokval->t_type = TOKEN_GE;
1548 if (!strcmp(tline->text, "&&"))
1549 return tokval->t_type = TOKEN_DBL_AND;
1550 if (!strcmp(tline->text, "^^"))
1551 return tokval->t_type = TOKEN_DBL_XOR;
1552 if (!strcmp(tline->text, "||"))
1553 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001554 }
1555
1556 /*
1557 * We have no other options: just return the first character of
1558 * the token text.
1559 */
1560 return tokval->t_type = tline->text[0];
1561}
1562
1563/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001564 * Compare a string to the name of an existing macro; this is a
1565 * simple wrapper which calls either strcmp or nasm_stricmp
1566 * depending on the value of the `casesense' parameter.
1567 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001568static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001569{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001570 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001571}
1572
1573/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001574 * 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 */
1578static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1579{
1580 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1581}
1582
1583/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001584 * Return the Context structure associated with a %$ token. Return
1585 * NULL, having _already_ reported an error condition, if the
1586 * context stack isn't deep enough for the supplied number of $
1587 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001588 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001589 * also scanned for such smacro, until it is found; if not -
1590 * only the context that directly results from the number of $'s
1591 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001592 *
1593 * If "namep" is non-NULL, set it to the pointer to the macro name
1594 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001595 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001596static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001597 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001598{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001599 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001600 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001601 int i;
1602
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001603 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001604 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001605
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001606 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001607 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001608
H. Peter Anvine2c80182005-01-15 22:15:51 +00001609 if (!cstk) {
1610 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1611 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001612 }
1613
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001614 name += 2;
1615 ctx = cstk;
1616 i = 0;
1617 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001618 name++;
1619 i++;
1620 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001621 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001622 if (!ctx) {
1623 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001624 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001625 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001626 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001627
1628 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001629 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001630
Keith Kanios404589e2010-08-10 20:12:57 -05001631 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001632 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001633
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001634 do {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001636 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001637 while (m) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001638 if (!mstrcmp(m->name, name, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 return ctx;
1640 m = m->next;
1641 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001642 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001643 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001644 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001645 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001646}
1647
1648/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001649 * Check to see if a file is already in a string list
1650 */
1651static bool in_list(const StrList *list, const char *str)
1652{
1653 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001654 if (!strcmp(list->str, str))
1655 return true;
1656 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001657 }
1658 return false;
1659}
1660
1661/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001662 * Open an include file. This routine must always return a valid
1663 * file pointer if it returns - it's responsible for throwing an
1664 * ERR_FATAL and bombing out completely if not. It should also try
1665 * the include path one by one until it finds the file or reaches
1666 * the end of the path.
1667 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001668static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001669 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001670{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001671 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001672 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001673 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001674 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001675 size_t prefix_len = 0;
1676 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001677
H. Peter Anvine2c80182005-01-15 22:15:51 +00001678 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001679 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunov6f38fe62010-11-11 11:32:16 +03001680 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001681 memcpy(sl->str, prefix, prefix_len);
1682 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001683 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001684 if (fp && dhead && !in_list(*dhead, sl->str)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001685 **dtail = sl;
1686 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001687 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001688 nasm_free(sl);
1689 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 if (fp)
1691 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001692 if (!ip) {
1693 if (!missing_ok)
1694 break;
1695 prefix = NULL;
1696 } else {
1697 prefix = ip->path;
1698 ip = ip->next;
1699 }
1700 if (prefix) {
1701 prefix_len = strlen(prefix);
1702 } else {
1703 /* -MG given and file not found */
1704 if (dhead && !in_list(*dhead, file)) {
1705 sl = nasm_malloc(len+1+sizeof sl->next);
1706 sl->next = NULL;
1707 strcpy(sl->str, file);
1708 **dtail = sl;
1709 *dtail = &sl->next;
1710 }
1711 return NULL;
1712 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001713 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001714
H. Peter Anvin734b1882002-04-30 21:01:08 +00001715 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001716 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001717}
1718
1719/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001720 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001721 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001722 * return true if _any_ single-line macro of that name is defined.
1723 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001724 * `nparam' or no parameters is defined.
1725 *
1726 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001727 * defined, or nparam is -1, the address of the definition structure
1728 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001729 * is NULL, no action will be taken regarding its contents, and no
1730 * error will occur.
1731 *
1732 * Note that this is also called with nparam zero to resolve
1733 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001734 *
1735 * If you already know which context macro belongs to, you can pass
1736 * the context pointer as first parameter; if you won't but name begins
1737 * with %$ the context will be automatically computed. If all_contexts
1738 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001739 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001740static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001741smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001742 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001743{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001744 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001745 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001746
H. Peter Anvin97a23472007-09-16 17:57:25 -07001747 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001748 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001749 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001750 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001751 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001752 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001753 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001754 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001755 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001756 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001757 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001758 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001759
H. Peter Anvine2c80182005-01-15 22:15:51 +00001760 while (m) {
1761 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001762 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001763 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001764 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001765 *defn = m;
1766 else
1767 *defn = NULL;
1768 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001769 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001770 }
1771 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001772 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001773
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001774 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001775}
1776
1777/*
1778 * Count and mark off the parameters in a multi-line macro call.
1779 * This is called both from within the multi-line macro expansion
1780 * code, and also to mark off the default parameters when provided
1781 * in a %macro definition line.
1782 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001783static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001784{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001785 int paramsize, brace;
1786
1787 *nparam = paramsize = 0;
1788 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001789 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001790 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001791 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001792 paramsize += PARAM_DELTA;
1793 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1794 }
1795 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001796 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001797 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001798 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001799 (*params)[(*nparam)++] = t;
1800 while (tok_isnt_(t, brace ? "}" : ","))
1801 t = t->next;
1802 if (t) { /* got a comma/brace */
1803 t = t->next;
1804 if (brace) {
1805 /*
1806 * Now we've found the closing brace, look further
1807 * for the comma.
1808 */
1809 skip_white_(t);
1810 if (tok_isnt_(t, ",")) {
1811 error(ERR_NONFATAL,
1812 "braces do not enclose all of macro parameter");
1813 while (tok_isnt_(t, ","))
1814 t = t->next;
1815 }
1816 if (t)
1817 t = t->next; /* eat the comma */
1818 }
1819 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001820 }
1821}
1822
1823/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001824 * Determine whether one of the various `if' conditions is true or
1825 * not.
1826 *
1827 * We must free the tline we get passed.
1828 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001829static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001830{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001831 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001832 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001833 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001834 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001835 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001836 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001837 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001838
1839 origline = tline;
1840
H. Peter Anvine2c80182005-01-15 22:15:51 +00001841 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001842 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001843 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001844 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001845 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001846 if (!tline)
1847 break;
1848 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001849 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001850 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001851 free_tlist(origline);
1852 return -1;
1853 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001854 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001855 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001856 tline = tline->next;
1857 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001858 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001859
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001860 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001861 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001862 while (tline) {
1863 skip_white_(tline);
1864 if (!tline || (tline->type != TOK_ID &&
1865 (tline->type != TOK_PREPROC_ID ||
1866 tline->text[1] != '$'))) {
1867 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001868 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001869 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001870 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001871 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001872 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001873 tline = tline->next;
1874 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001875 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001876
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001877 case PPC_IFENV:
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001878 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001879 j = false; /* have we matched yet? */
1880 while (tline) {
1881 skip_white_(tline);
1882 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001883 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001884 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001885 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001886 error(ERR_NONFATAL,
1887 "`%s' expects environment variable names",
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001888 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001889 goto fail;
1890 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001891 p = tline->text;
1892 if (tline->type == TOK_PREPROC_ID)
1893 p += 2; /* Skip leading %! */
1894 if (*p == '\'' || *p == '\"' || *p == '`')
1895 nasm_unquote_cstr(p, ct);
1896 if (getenv(p))
1897 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001898 tline = tline->next;
1899 }
1900 break;
1901
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001902 case PPC_IFIDN:
1903 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001904 tline = expand_smacro(tline);
1905 t = tt = tline;
1906 while (tok_isnt_(tt, ","))
1907 tt = tt->next;
1908 if (!tt) {
1909 error(ERR_NONFATAL,
1910 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001911 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001912 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001913 }
1914 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001915 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001916 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1917 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1918 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001919 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001920 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001921 }
1922 if (t->type == TOK_WHITESPACE) {
1923 t = t->next;
1924 continue;
1925 }
1926 if (tt->type == TOK_WHITESPACE) {
1927 tt = tt->next;
1928 continue;
1929 }
1930 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001931 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001932 break;
1933 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001934 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001935 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001936 size_t l1 = nasm_unquote(t->text, NULL);
1937 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001938
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001939 if (l1 != l2) {
1940 j = false;
1941 break;
1942 }
1943 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1944 j = false;
1945 break;
1946 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001947 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001948 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001949 break;
1950 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001951
H. Peter Anvine2c80182005-01-15 22:15:51 +00001952 t = t->next;
1953 tt = tt->next;
1954 }
1955 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001956 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001957 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001958
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001959 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001960 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001961 bool found = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001962 ExpDef searching, *ed;
H. Peter Anvin65747262002-05-07 00:10:05 +00001963
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001964 skip_white_(tline);
1965 tline = expand_id(tline);
1966 if (!tok_type_(tline, TOK_ID)) {
1967 error(ERR_NONFATAL,
1968 "`%s' expects a macro name", pp_directives[ct]);
1969 goto fail;
1970 }
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03001971 memset(&searching, 0, sizeof(searching));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001972 searching.name = nasm_strdup(tline->text);
1973 searching.casesense = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001974 searching.nparam_max = INT_MAX;
1975 tline = expand_smacro(tline->next);
1976 skip_white_(tline);
1977 if (!tline) {
1978 } else if (!tok_type_(tline, TOK_NUMBER)) {
1979 error(ERR_NONFATAL,
1980 "`%s' expects a parameter count or nothing",
1981 pp_directives[ct]);
1982 } else {
1983 searching.nparam_min = searching.nparam_max =
1984 readnum(tline->text, &j);
1985 if (j)
1986 error(ERR_NONFATAL,
1987 "unable to parse parameter count `%s'",
1988 tline->text);
1989 }
1990 if (tline && tok_is_(tline->next, "-")) {
1991 tline = tline->next->next;
1992 if (tok_is_(tline, "*"))
1993 searching.nparam_max = INT_MAX;
1994 else if (!tok_type_(tline, TOK_NUMBER))
1995 error(ERR_NONFATAL,
1996 "`%s' expects a parameter count after `-'",
1997 pp_directives[ct]);
1998 else {
1999 searching.nparam_max = readnum(tline->text, &j);
2000 if (j)
2001 error(ERR_NONFATAL,
2002 "unable to parse parameter count `%s'",
2003 tline->text);
2004 if (searching.nparam_min > searching.nparam_max)
2005 error(ERR_NONFATAL,
2006 "minimum parameter count exceeds maximum");
2007 }
2008 }
2009 if (tline && tok_is_(tline->next, "+")) {
2010 tline = tline->next;
2011 searching.plus = true;
2012 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002013 ed = (ExpDef *) hash_findix(&expdefs, searching.name);
2014 while (ed != NULL) {
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002015 if (!strcmp(ed->name, searching.name) &&
2016 (ed->nparam_min <= searching.nparam_max || searching.plus) &&
2017 (searching.nparam_min <= ed->nparam_max || ed->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002018 found = true;
2019 break;
2020 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002021 ed = ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002022 }
2023 if (tline && tline->next)
2024 error(ERR_WARNING|ERR_PASS1,
2025 "trailing garbage after %%ifmacro ignored");
2026 nasm_free(searching.name);
2027 j = found;
2028 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002029 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002030
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002031 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002032 needtype = TOK_ID;
2033 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002034 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002035 needtype = TOK_NUMBER;
2036 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002037 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002038 needtype = TOK_STRING;
2039 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002040
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002041iftype:
2042 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002043
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002044 while (tok_type_(t, TOK_WHITESPACE) ||
2045 (needtype == TOK_NUMBER &&
2046 tok_type_(t, TOK_OTHER) &&
2047 (t->text[0] == '-' || t->text[0] == '+') &&
2048 !t->text[1]))
2049 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002050
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002051 j = tok_type_(t, needtype);
2052 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002053
2054 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002055 t = tline = expand_smacro(tline);
2056 while (tok_type_(t, TOK_WHITESPACE))
2057 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002058
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002059 j = false;
2060 if (t) {
2061 t = t->next; /* Skip the actual token */
2062 while (tok_type_(t, TOK_WHITESPACE))
2063 t = t->next;
2064 j = !t; /* Should be nothing left */
2065 }
2066 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002067
H. Peter Anvin134b9462008-02-16 17:01:40 -08002068 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002069 t = tline = expand_smacro(tline);
2070 while (tok_type_(t, TOK_WHITESPACE))
2071 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002072
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002073 j = !t; /* Should be empty */
2074 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002075
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002076 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002077 t = tline = expand_smacro(tline);
2078 tptr = &t;
2079 tokval.t_type = TOKEN_INVALID;
2080 evalresult = evaluate(ppscan, tptr, &tokval,
2081 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002082 if (!evalresult)
2083 return -1;
2084 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002085 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002086 "trailing garbage after expression ignored");
2087 if (!is_simple(evalresult)) {
2088 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002089 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002090 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002091 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002092 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002093 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002094
H. Peter Anvine2c80182005-01-15 22:15:51 +00002095 default:
2096 error(ERR_FATAL,
2097 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002098 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002099 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002100 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002101
2102 free_tlist(origline);
2103 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002104
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002105fail:
2106 free_tlist(origline);
2107 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002108}
2109
2110/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002111 * Common code for defining an smacro
2112 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002113static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002114 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002115{
2116 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002117 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002118
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002119 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002120 if (!smac) {
2121 error(ERR_WARNING|ERR_PASS1,
2122 "single-line macro `%s' defined both with and"
2123 " without parameters", mname);
2124 /*
2125 * Some instances of the old code considered this a failure,
2126 * some others didn't. What is the right thing to do here?
2127 */
2128 free_tlist(expansion);
2129 return false; /* Failure */
2130 } else {
2131 /*
2132 * We're redefining, so we have to take over an
2133 * existing SMacro structure. This means freeing
2134 * what was already in it.
2135 */
2136 nasm_free(smac->name);
2137 free_tlist(smac->expansion);
2138 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002139 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002140 smtbl = ctx ? &ctx->localmac : &smacros;
2141 smhead = (SMacro **) hash_findi_add(smtbl, mname);
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002142 smac = nasm_zalloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002143 smac->next = *smhead;
2144 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002145 }
2146 smac->name = nasm_strdup(mname);
2147 smac->casesense = casesense;
2148 smac->nparam = nparam;
2149 smac->expansion = expansion;
2150 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002151 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002152}
2153
2154/*
2155 * Undefine an smacro
2156 */
2157static void undef_smacro(Context *ctx, const char *mname)
2158{
2159 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002160 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002161
H. Peter Anvin166c2472008-05-28 12:28:58 -07002162 smtbl = ctx ? &ctx->localmac : &smacros;
2163 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002164
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002165 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002166 /*
2167 * We now have a macro name... go hunt for it.
2168 */
2169 sp = smhead;
2170 while ((s = *sp) != NULL) {
2171 if (!mstrcmp(s->name, mname, s->casesense)) {
2172 *sp = s->next;
2173 nasm_free(s->name);
2174 free_tlist(s->expansion);
2175 nasm_free(s);
2176 } else {
2177 sp = &s->next;
2178 }
2179 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002180 }
2181}
2182
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002183/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002184 * Parse a mmacro specification.
2185 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002186static bool parse_mmacro_spec(Token *tline, ExpDef *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002187{
2188 bool err;
2189
2190 tline = tline->next;
2191 skip_white_(tline);
2192 tline = expand_id(tline);
2193 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002194 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2195 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002196 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002197
H. Peter Anvina26433d2008-07-16 14:40:01 -07002198 def->name = nasm_strdup(tline->text);
2199 def->plus = false;
2200 def->nolist = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002201// def->in_progress = 0;
2202// def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002203 def->nparam_min = 0;
2204 def->nparam_max = 0;
2205
H. Peter Anvina26433d2008-07-16 14:40:01 -07002206 tline = expand_smacro(tline->next);
2207 skip_white_(tline);
2208 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002209 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002210 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002211 def->nparam_min = def->nparam_max =
2212 readnum(tline->text, &err);
2213 if (err)
2214 error(ERR_NONFATAL,
2215 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002216 }
2217 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002218 tline = tline->next->next;
2219 if (tok_is_(tline, "*")) {
2220 def->nparam_max = INT_MAX;
2221 } else if (!tok_type_(tline, TOK_NUMBER)) {
2222 error(ERR_NONFATAL,
2223 "`%s' expects a parameter count after `-'", directive);
2224 } else {
2225 def->nparam_max = readnum(tline->text, &err);
2226 if (err) {
2227 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2228 tline->text);
2229 }
2230 if (def->nparam_min > def->nparam_max) {
2231 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2232 }
2233 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002234 }
2235 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002236 tline = tline->next;
2237 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002238 }
2239 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002240 !nasm_stricmp(tline->next->text, ".nolist")) {
2241 tline = tline->next;
2242 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002243 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002244
H. Peter Anvina26433d2008-07-16 14:40:01 -07002245 /*
2246 * Handle default parameters.
2247 */
2248 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002249 def->dlist = tline->next;
2250 tline->next = NULL;
2251 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002252 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002253 def->dlist = NULL;
2254 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002255 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002256 def->line = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002257
H. Peter Anvin89cee572009-07-15 09:16:54 -04002258 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002259 !def->plus)
2260 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2261 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002262
H. Peter Anvina26433d2008-07-16 14:40:01 -07002263 return true;
2264}
2265
2266
2267/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002268 * Decode a size directive
2269 */
2270static int parse_size(const char *str) {
2271 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002272 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002273 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002274 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002275
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002276 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002277}
2278
Ed Beroset3ab3f412002-06-11 03:31:49 +00002279/**
2280 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002281 * Find out if a line contains a preprocessor directive, and deal
2282 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002283 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002284 * If a directive _is_ found, it is the responsibility of this routine
2285 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002286 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002287 * @param tline a pointer to the current tokeninzed line linked list
2288 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002289 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002290 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002291static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002292{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002293 enum preproc_token i;
2294 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002295 bool err;
2296 int nparam;
2297 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002298 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002299 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002300 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002301 char *p, *pp;
2302 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002303 Include *inc;
2304 Context *ctx;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002305 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002306 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002307 struct tokenval tokval;
2308 expr *evalresult;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002309 ExpDef *ed, *eed, **edhead;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002310 ExpInv *ei, *eei;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002311 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002312 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002313 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002314
2315 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002316
H. Peter Anvineba20a72002-04-30 20:53:55 +00002317 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002318 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002319 (tline->text[1] == '%' || tline->text[1] == '$'
2320 || tline->text[1] == '!'))
2321 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002322
H. Peter Anvin4169a472007-09-12 01:29:43 +00002323 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002324
H. Peter Anvin4169a472007-09-12 01:29:43 +00002325 switch (i) {
2326 case PP_INVALID:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002327 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002328 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2329 tline->text);
2330 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002331
H. Peter Anvine2c80182005-01-15 22:15:51 +00002332 case PP_STACKSIZE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002333 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002334 /* Directive to tell NASM what the default stack size is. The
2335 * default is for a 16-bit stack, and this can be overriden with
2336 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002337 */
2338 tline = tline->next;
2339 if (tline && tline->type == TOK_WHITESPACE)
2340 tline = tline->next;
2341 if (!tline || tline->type != TOK_ID) {
2342 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2343 free_tlist(origline);
2344 return DIRECTIVE_FOUND;
2345 }
2346 if (nasm_stricmp(tline->text, "flat") == 0) {
2347 /* All subsequent ARG directives are for a 32-bit stack */
2348 StackSize = 4;
2349 StackPointer = "ebp";
2350 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002351 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002352 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2353 /* All subsequent ARG directives are for a 64-bit stack */
2354 StackSize = 8;
2355 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002356 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002357 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002358 } else if (nasm_stricmp(tline->text, "large") == 0) {
2359 /* All subsequent ARG directives are for a 16-bit stack,
2360 * far function call.
2361 */
2362 StackSize = 2;
2363 StackPointer = "bp";
2364 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002365 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002366 } else if (nasm_stricmp(tline->text, "small") == 0) {
2367 /* All subsequent ARG directives are for a 16-bit stack,
2368 * far function call. We don't support near functions.
2369 */
2370 StackSize = 2;
2371 StackPointer = "bp";
2372 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002373 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002374 } else {
2375 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2376 free_tlist(origline);
2377 return DIRECTIVE_FOUND;
2378 }
2379 free_tlist(origline);
2380 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002381
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 case PP_ARG:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002383 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 /* TASM like ARG directive to define arguments to functions, in
2385 * the following form:
2386 *
2387 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2388 */
2389 offset = ArgOffset;
2390 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002391 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002392 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002393
H. Peter Anvine2c80182005-01-15 22:15:51 +00002394 /* Find the argument name */
2395 tline = tline->next;
2396 if (tline && tline->type == TOK_WHITESPACE)
2397 tline = tline->next;
2398 if (!tline || tline->type != TOK_ID) {
2399 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2400 free_tlist(origline);
2401 return DIRECTIVE_FOUND;
2402 }
2403 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002404
H. Peter Anvine2c80182005-01-15 22:15:51 +00002405 /* Find the argument size type */
2406 tline = tline->next;
2407 if (!tline || tline->type != TOK_OTHER
2408 || tline->text[0] != ':') {
2409 error(ERR_NONFATAL,
2410 "Syntax error processing `%%arg' directive");
2411 free_tlist(origline);
2412 return DIRECTIVE_FOUND;
2413 }
2414 tline = tline->next;
2415 if (!tline || tline->type != TOK_ID) {
2416 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2417 free_tlist(origline);
2418 return DIRECTIVE_FOUND;
2419 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002420
H. Peter Anvine2c80182005-01-15 22:15:51 +00002421 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002422 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002423 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002424 size = parse_size(tt->text);
2425 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002426 error(ERR_NONFATAL,
2427 "Invalid size type for `%%arg' missing directive");
2428 free_tlist(tt);
2429 free_tlist(origline);
2430 return DIRECTIVE_FOUND;
2431 }
2432 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002433
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002434 /* Round up to even stack slots */
2435 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002436
H. Peter Anvine2c80182005-01-15 22:15:51 +00002437 /* Now define the macro for the argument */
2438 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2439 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002440 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002441 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002442
H. Peter Anvine2c80182005-01-15 22:15:51 +00002443 /* Move to the next argument in the list */
2444 tline = tline->next;
2445 if (tline && tline->type == TOK_WHITESPACE)
2446 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002447 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002448 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002449 free_tlist(origline);
2450 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002451
H. Peter Anvine2c80182005-01-15 22:15:51 +00002452 case PP_LOCAL:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002453 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002454 /* TASM like LOCAL directive to define local variables for a
2455 * function, in the following form:
2456 *
2457 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2458 *
2459 * The '= LocalSize' at the end is ignored by NASM, but is
2460 * required by TASM to define the local parameter size (and used
2461 * by the TASM macro package).
2462 */
2463 offset = LocalOffset;
2464 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002465 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002466 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002467
H. Peter Anvine2c80182005-01-15 22:15:51 +00002468 /* Find the argument name */
2469 tline = tline->next;
2470 if (tline && tline->type == TOK_WHITESPACE)
2471 tline = tline->next;
2472 if (!tline || tline->type != TOK_ID) {
2473 error(ERR_NONFATAL,
2474 "`%%local' missing argument parameter");
2475 free_tlist(origline);
2476 return DIRECTIVE_FOUND;
2477 }
2478 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002479
H. Peter Anvine2c80182005-01-15 22:15:51 +00002480 /* Find the argument size type */
2481 tline = tline->next;
2482 if (!tline || tline->type != TOK_OTHER
2483 || tline->text[0] != ':') {
2484 error(ERR_NONFATAL,
2485 "Syntax error processing `%%local' directive");
2486 free_tlist(origline);
2487 return DIRECTIVE_FOUND;
2488 }
2489 tline = tline->next;
2490 if (!tline || tline->type != TOK_ID) {
2491 error(ERR_NONFATAL,
2492 "`%%local' missing size type parameter");
2493 free_tlist(origline);
2494 return DIRECTIVE_FOUND;
2495 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002496
H. Peter Anvine2c80182005-01-15 22:15:51 +00002497 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002498 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002499 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002500 size = parse_size(tt->text);
2501 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002502 error(ERR_NONFATAL,
2503 "Invalid size type for `%%local' missing directive");
2504 free_tlist(tt);
2505 free_tlist(origline);
2506 return DIRECTIVE_FOUND;
2507 }
2508 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002509
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002510 /* Round up to even stack slots */
2511 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002512
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002513 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002514
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002515 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002516 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2517 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002518 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002519
H. Peter Anvine2c80182005-01-15 22:15:51 +00002520 /* Now define the assign to setup the enter_c macro correctly */
2521 snprintf(directive, sizeof(directive),
2522 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002523 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002524
H. Peter Anvine2c80182005-01-15 22:15:51 +00002525 /* Move to the next argument in the list */
2526 tline = tline->next;
2527 if (tline && tline->type == TOK_WHITESPACE)
2528 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002529 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002530 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002531 free_tlist(origline);
2532 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002533
H. Peter Anvine2c80182005-01-15 22:15:51 +00002534 case PP_CLEAR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002535 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002536 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002537 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002538 "trailing garbage after `%%clear' ignored");
2539 free_macros();
2540 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002541 free_tlist(origline);
2542 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002543
H. Peter Anvin418ca702008-05-30 10:42:30 -07002544 case PP_DEPEND:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002545 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002546 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002547 skip_white_(t);
2548 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002549 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002550 error(ERR_NONFATAL, "`%%depend' expects a file name");
2551 free_tlist(origline);
2552 return DIRECTIVE_FOUND; /* but we did _something_ */
2553 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002554 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002555 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002556 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002557 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002558 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002559 nasm_unquote_cstr(p, i);
2560 if (dephead && !in_list(*dephead, p)) {
2561 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2562 sl->next = NULL;
2563 strcpy(sl->str, p);
2564 *deptail = sl;
2565 deptail = &sl->next;
2566 }
2567 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002568 return DIRECTIVE_FOUND;
2569
2570 case PP_INCLUDE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002571 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002572 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002573 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002574
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002575 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002576 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002577 error(ERR_NONFATAL, "`%%include' expects a file name");
2578 free_tlist(origline);
2579 return DIRECTIVE_FOUND; /* but we did _something_ */
2580 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002581 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002582 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002583 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002584 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002585 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002586 nasm_unquote_cstr(p, i);
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03002587 inc = nasm_zalloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002588 inc->next = istk;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002589 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002590 if (!inc->fp) {
2591 /* -MG given but file not found */
2592 nasm_free(inc);
2593 } else {
2594 inc->fname = src_set_fname(nasm_strdup(p));
2595 inc->lineno = src_set_linnum(0);
2596 inc->lineinc = 1;
2597 inc->expansion = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002598 istk = inc;
2599 list->uplevel(LIST_INCLUDE);
2600 }
2601 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002602 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002603
H. Peter Anvind2456592008-06-19 15:04:18 -07002604 case PP_USE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002605 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002606 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002607 static macros_t *use_pkg;
2608 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002609
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002610 tline = tline->next;
2611 skip_white_(tline);
2612 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002613
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002614 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002615 tline->type != TOK_INTERNAL_STRING &&
2616 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002617 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002618 free_tlist(origline);
2619 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002620 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002621 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002622 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002623 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002624 if (tline->type == TOK_STRING)
2625 nasm_unquote_cstr(tline->text, i);
2626 use_pkg = nasm_stdmac_find_package(tline->text);
2627 if (!use_pkg)
2628 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2629 else
2630 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002631 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002632 /* Not already included, go ahead and include it */
2633 stdmacpos = use_pkg;
2634 }
2635 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002636 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002637 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002638 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002639 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002640 case PP_POP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002641 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002642 tline = tline->next;
2643 skip_white_(tline);
2644 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002645 if (tline) {
2646 if (!tok_type_(tline, TOK_ID)) {
2647 error(ERR_NONFATAL, "`%s' expects a context identifier",
2648 pp_directives[i]);
2649 free_tlist(origline);
2650 return DIRECTIVE_FOUND; /* but we did _something_ */
2651 }
2652 if (tline->next)
2653 error(ERR_WARNING|ERR_PASS1,
2654 "trailing garbage after `%s' ignored",
2655 pp_directives[i]);
2656 p = nasm_strdup(tline->text);
2657 } else {
2658 p = NULL; /* Anonymous */
2659 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002660
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002661 if (i == PP_PUSH) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002662 ctx = nasm_zalloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002663 ctx->next = cstk;
2664 hash_init(&ctx->localmac, HASH_SMALL);
2665 ctx->name = p;
2666 ctx->number = unique++;
2667 cstk = ctx;
2668 } else {
2669 /* %pop or %repl */
2670 if (!cstk) {
2671 error(ERR_NONFATAL, "`%s': context stack is empty",
2672 pp_directives[i]);
2673 } else if (i == PP_POP) {
2674 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2675 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2676 "expected %s",
2677 cstk->name ? cstk->name : "anonymous", p);
2678 else
2679 ctx_pop();
2680 } else {
2681 /* i == PP_REPL */
2682 nasm_free(cstk->name);
2683 cstk->name = p;
2684 p = NULL;
2685 }
2686 nasm_free(p);
2687 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002688 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002689 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002690 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002691 severity = ERR_FATAL;
2692 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002693 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002694 severity = ERR_NONFATAL;
2695 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002696 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002697 severity = ERR_WARNING|ERR_WARN_USER;
2698 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002699
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002700issue_error:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002701 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002702 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002703 /* Only error out if this is the final pass */
2704 if (pass != 2 && i != PP_FATAL)
2705 return DIRECTIVE_FOUND;
2706
2707 tline->next = expand_smacro(tline->next);
2708 tline = tline->next;
2709 skip_white_(tline);
2710 t = tline ? tline->next : NULL;
2711 skip_white_(t);
2712 if (tok_type_(tline, TOK_STRING) && !t) {
2713 /* The line contains only a quoted string */
2714 p = tline->text;
2715 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2716 error(severity, "%s", p);
2717 } else {
2718 /* Not a quoted string, or more than a quoted string */
2719 p = detoken(tline, false);
2720 error(severity, "%s", p);
2721 nasm_free(p);
2722 }
2723 free_tlist(origline);
2724 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002725 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002726
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002727 CASE_PP_IF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002728 if (defining != NULL) {
2729 if (defining->type == EXP_IF) {
2730 defining->def_depth ++;
2731 }
2732 return NO_DIRECTIVE_FOUND;
2733 }
2734 if ((istk->expansion != NULL) &&
2735 (istk->expansion->emitting == false)) {
2736 j = COND_NEVER;
2737 } else {
2738 j = if_condition(tline->next, i);
2739 tline->next = NULL; /* it got freed */
2740 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
2741 }
2742 ed = new_ExpDef(EXP_IF);
2743 ed->state = j;
2744 ed->nolist = NULL;
2745 ed->def_depth = 0;
2746 ed->cur_depth = 0;
2747 ed->max_depth = 0;
2748 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
2749 ed->prev = defining;
2750 defining = ed;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002751 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002752 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002753
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002754 CASE_PP_ELIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002755 if (defining != NULL) {
2756 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2757 return NO_DIRECTIVE_FOUND;
2758 }
2759 }
2760 if ((defining == NULL) || (defining->type != EXP_IF)) {
2761 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2762 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002763 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002764 case COND_IF_TRUE:
2765 defining->state = COND_DONE;
2766 defining->ignoring = true;
2767 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002768
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002769 case COND_DONE:
2770 case COND_NEVER:
2771 defining->ignoring = true;
2772 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002773
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002774 case COND_ELSE_TRUE:
2775 case COND_ELSE_FALSE:
2776 error_precond(ERR_WARNING|ERR_PASS1,
2777 "`%%elif' after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002778 defining->state = COND_NEVER;
2779 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002780 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002781
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002782 case COND_IF_FALSE:
2783 /*
2784 * IMPORTANT: In the case of %if, we will already have
2785 * called expand_mmac_params(); however, if we're
2786 * processing an %elif we must have been in a
2787 * non-emitting mode, which would have inhibited
2788 * the normal invocation of expand_mmac_params().
2789 * Therefore, we have to do it explicitly here.
2790 */
2791 j = if_condition(expand_mmac_params(tline->next), i);
2792 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002793 defining->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002794 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002795 defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002796 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002797 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002798 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002799 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002800
H. Peter Anvine2c80182005-01-15 22:15:51 +00002801 case PP_ELSE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002802 if (defining != NULL) {
2803 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2804 return NO_DIRECTIVE_FOUND;
2805 }
2806 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002807 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002808 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002809 "trailing garbage after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002810 if ((defining == NULL) || (defining->type != EXP_IF)) {
2811 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2812 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002813 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002814 case COND_IF_TRUE:
2815 case COND_DONE:
2816 defining->state = COND_ELSE_FALSE;
2817 defining->ignoring = true;
2818 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002819
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002820 case COND_NEVER:
2821 defining->ignoring = true;
2822 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002823
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002824 case COND_IF_FALSE:
2825 defining->state = COND_ELSE_TRUE;
2826 defining->ignoring = false;
2827 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002828
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002829 case COND_ELSE_TRUE:
2830 case COND_ELSE_FALSE:
2831 error_precond(ERR_WARNING|ERR_PASS1,
2832 "`%%else' after `%%else' ignored.");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002833 defining->state = COND_NEVER;
2834 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002835 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002836 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002837 free_tlist(origline);
2838 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002839
H. Peter Anvine2c80182005-01-15 22:15:51 +00002840 case PP_ENDIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002841 if (defining != NULL) {
2842 if (defining->type == EXP_IF) {
2843 if (defining->def_depth > 0) {
2844 defining->def_depth --;
2845 return NO_DIRECTIVE_FOUND;
2846 }
2847 } else {
2848 return NO_DIRECTIVE_FOUND;
2849 }
2850 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002851 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002852 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002853 "trailing garbage after `%%endif' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002854 if ((defining == NULL) || (defining->type != EXP_IF)) {
2855 error(ERR_NONFATAL, "`%%endif': no matching `%%if'");
2856 return DIRECTIVE_FOUND;
2857 }
2858 ed = defining;
2859 defining = ed->prev;
2860 ed->prev = expansions;
2861 expansions = ed;
2862 ei = new_ExpInv(EXP_IF, ed);
2863 ei->current = ed->line;
2864 ei->emitting = true;
2865 ei->prev = istk->expansion;
2866 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002867 free_tlist(origline);
2868 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002869
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002870 case PP_RMACRO:
2871 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002872 case PP_MACRO:
2873 case PP_IMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002874 if (defining != NULL) {
2875 if (defining->type == EXP_MMACRO) {
2876 defining->def_depth ++;
2877 }
2878 return NO_DIRECTIVE_FOUND;
2879 }
2880 ed = new_ExpDef(EXP_MMACRO);
2881 ed->max_depth =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002882 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002883 ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002884 if (!parse_mmacro_spec(tline, ed, pp_directives[i])) {
2885 nasm_free(ed);
2886 ed = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002887 return DIRECTIVE_FOUND;
2888 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002889 ed->def_depth = 0;
2890 ed->cur_depth = 0;
2891 ed->max_depth = (ed->max_depth + 1);
2892 ed->ignoring = false;
2893 ed->prev = defining;
2894 defining = ed;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002895
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002896 eed = (ExpDef *) hash_findix(&expdefs, ed->name);
2897 while (eed) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002898 if (!strcmp(eed->name, ed->name) &&
2899 (eed->nparam_min <= ed->nparam_max || ed->plus) &&
2900 (ed->nparam_min <= eed->nparam_max || eed->plus)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002901 error(ERR_WARNING|ERR_PASS1,
2902 "redefining multi-line macro `%s'", ed->name);
2903 return DIRECTIVE_FOUND;
2904 }
2905 eed = eed->next;
2906 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002907 free_tlist(origline);
2908 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002909
H. Peter Anvine2c80182005-01-15 22:15:51 +00002910 case PP_ENDM:
2911 case PP_ENDMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002912 if (defining != NULL) {
2913 if (defining->type == EXP_MMACRO) {
2914 if (defining->def_depth > 0) {
2915 defining->def_depth --;
2916 return NO_DIRECTIVE_FOUND;
2917 }
2918 } else {
2919 return NO_DIRECTIVE_FOUND;
2920 }
2921 }
2922 if (!(defining) || (defining->type != EXP_MMACRO)) {
2923 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2924 return DIRECTIVE_FOUND;
2925 }
2926 edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name);
2927 defining->next = *edhead;
2928 *edhead = defining;
2929 ed = defining;
2930 defining = ed->prev;
2931 ed->prev = expansions;
2932 expansions = ed;
2933 ed = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002934 free_tlist(origline);
2935 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002936
H. Peter Anvin89cee572009-07-15 09:16:54 -04002937 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002938 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2939 /*
2940 * We must search along istk->expansion until we hit a
2941 * macro invocation. Then we disable the emitting state(s)
2942 * between exitmacro and endmacro.
2943 */
2944 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
2945 if(ei->type == EXP_MMACRO) {
2946 break;
2947 }
2948 }
2949
2950 if (ei != NULL) {
2951 /*
2952 * Set all invocations leading back to the macro
2953 * invocation to a non-emitting state.
2954 */
2955 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
2956 eei->emitting = false;
2957 }
2958 eei->emitting = false;
2959 } else {
2960 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2961 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002962 free_tlist(origline);
2963 return DIRECTIVE_FOUND;
2964
H. Peter Anvina26433d2008-07-16 14:40:01 -07002965 case PP_UNMACRO:
2966 case PP_UNIMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002967 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002968 {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002969 ExpDef **ed_p;
2970 ExpDef spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002971
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002972 spec.casesense = (i == PP_UNMACRO);
2973 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2974 return DIRECTIVE_FOUND;
2975 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002976 ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL);
2977 while (ed_p && *ed_p) {
2978 ed = *ed_p;
2979 if (ed->casesense == spec.casesense &&
2980 !mstrcmp(ed->name, spec.name, spec.casesense) &&
2981 ed->nparam_min == spec.nparam_min &&
2982 ed->nparam_max == spec.nparam_max &&
2983 ed->plus == spec.plus) {
Keith Kaniosc98a5b42010-12-18 12:17:31 -06002984 if (ed->cur_depth > 0) {
Keith Kanios21d885b2010-12-18 12:22:21 -06002985 error(ERR_NONFATAL, "`%s' ignored on active macro",
Keith Kaniosc98a5b42010-12-18 12:17:31 -06002986 pp_directives[i]);
2987 break;
2988 } else {
2989 *ed_p = ed->next;
2990 free_expdef(ed);
2991 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002992 } else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002993 ed_p = &ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002994 }
2995 }
2996 free_tlist(origline);
2997 free_tlist(spec.dlist);
2998 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002999 }
3000
H. Peter Anvine2c80182005-01-15 22:15:51 +00003001 case PP_ROTATE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003002 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003003 if (tline->next && tline->next->type == TOK_WHITESPACE)
3004 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003005 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003006 free_tlist(origline);
3007 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
3008 return DIRECTIVE_FOUND;
3009 }
3010 t = expand_smacro(tline->next);
3011 tline->next = NULL;
3012 free_tlist(origline);
3013 tline = t;
3014 tptr = &t;
3015 tokval.t_type = TOKEN_INVALID;
3016 evalresult =
3017 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3018 free_tlist(tline);
3019 if (!evalresult)
3020 return DIRECTIVE_FOUND;
3021 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003022 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003023 "trailing garbage after expression ignored");
3024 if (!is_simple(evalresult)) {
3025 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
3026 return DIRECTIVE_FOUND;
3027 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003028 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3029 if (ei->type == EXP_MMACRO) {
3030 break;
3031 }
3032 }
3033 if (ei == NULL) {
3034 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
3035 } else if (ei->nparam == 0) {
3036 error(ERR_NONFATAL,
3037 "`%%rotate' invoked within macro without parameters");
3038 } else {
3039 int rotate = ei->rotate + reloc_value(evalresult);
3040
3041 rotate %= (int)ei->nparam;
3042 if (rotate < 0)
3043 rotate += ei->nparam;
3044 ei->rotate = rotate;
3045 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003046 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003047
H. Peter Anvine2c80182005-01-15 22:15:51 +00003048 case PP_REP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003049 if (defining != NULL) {
3050 if (defining->type == EXP_REP) {
3051 defining->def_depth ++;
3052 }
3053 return NO_DIRECTIVE_FOUND;
3054 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003055 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003056 do {
3057 tline = tline->next;
3058 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003059
H. Peter Anvine2c80182005-01-15 22:15:51 +00003060 if (tok_type_(tline, TOK_ID) &&
3061 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003062 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003063 do {
3064 tline = tline->next;
3065 } while (tok_type_(tline, TOK_WHITESPACE));
3066 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003067
H. Peter Anvine2c80182005-01-15 22:15:51 +00003068 if (tline) {
3069 t = expand_smacro(tline);
3070 tptr = &t;
3071 tokval.t_type = TOKEN_INVALID;
3072 evalresult =
3073 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3074 if (!evalresult) {
3075 free_tlist(origline);
3076 return DIRECTIVE_FOUND;
3077 }
3078 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003079 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003080 "trailing garbage after expression ignored");
3081 if (!is_simple(evalresult)) {
3082 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
3083 return DIRECTIVE_FOUND;
3084 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003085 count = reloc_value(evalresult);
3086 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04003087 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003088 count = 0;
3089 } else
3090 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003091 } else {
3092 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003093 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003094 }
3095 free_tlist(origline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003096 ed = new_ExpDef(EXP_REP);
3097 ed->nolist = nolist;
3098 ed->def_depth = 0;
3099 ed->cur_depth = 1;
3100 ed->max_depth = (count - 1);
3101 ed->ignoring = false;
3102 ed->prev = defining;
3103 defining = ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003104 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003105
H. Peter Anvine2c80182005-01-15 22:15:51 +00003106 case PP_ENDREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003107 if (defining != NULL) {
3108 if (defining->type == EXP_REP) {
3109 if (defining->def_depth > 0) {
3110 defining->def_depth --;
3111 return NO_DIRECTIVE_FOUND;
3112 }
3113 } else {
3114 return NO_DIRECTIVE_FOUND;
3115 }
3116 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003117 if ((defining == NULL) || (defining->type != EXP_REP)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003118 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
3119 return DIRECTIVE_FOUND;
3120 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003121
H. Peter Anvine2c80182005-01-15 22:15:51 +00003122 /*
3123 * Now we have a "macro" defined - although it has no name
3124 * and we won't be entering it in the hash tables - we must
3125 * push a macro-end marker for it on to istk->expansion.
3126 * After that, it will take care of propagating itself (a
3127 * macro-end marker line for a macro which is really a %rep
3128 * block will cause the macro to be re-expanded, complete
3129 * with another macro-end marker to ensure the process
3130 * continues) until the whole expansion is forcibly removed
3131 * from istk->expansion by a %exitrep.
3132 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003133 ed = defining;
3134 defining = ed->prev;
3135 ed->prev = expansions;
3136 expansions = ed;
3137 ei = new_ExpInv(EXP_REP, ed);
3138 ei->current = ed->line;
3139 ei->emitting = ((ed->max_depth > 0) ? true : false);
3140 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3141 ei->prev = istk->expansion;
3142 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003143 free_tlist(origline);
3144 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003145
H. Peter Anvine2c80182005-01-15 22:15:51 +00003146 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003147 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3148 /*
3149 * We must search along istk->expansion until we hit a
3150 * rep invocation. Then we disable the emitting state(s)
3151 * between exitrep and endrep.
3152 */
3153 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3154 if (ei->type == EXP_REP) {
3155 break;
3156 }
3157 }
3158
3159 if (ei != NULL) {
3160 /*
3161 * Set all invocations leading back to the rep
3162 * invocation to a non-emitting state.
3163 */
3164 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3165 eei->emitting = false;
3166 }
3167 eei->emitting = false;
3168 eei->current = NULL;
3169 eei->def->cur_depth = eei->def->max_depth;
3170 } else {
3171 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3172 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003173 free_tlist(origline);
3174 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003175
H. Peter Anvine2c80182005-01-15 22:15:51 +00003176 case PP_XDEFINE:
3177 case PP_IXDEFINE:
3178 case PP_DEFINE:
3179 case PP_IDEFINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003180 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003181 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003182
H. Peter Anvine2c80182005-01-15 22:15:51 +00003183 tline = tline->next;
3184 skip_white_(tline);
3185 tline = expand_id(tline);
3186 if (!tline || (tline->type != TOK_ID &&
3187 (tline->type != TOK_PREPROC_ID ||
3188 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003189 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003190 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003191 free_tlist(origline);
3192 return DIRECTIVE_FOUND;
3193 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003194
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003195 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003196 last = tline;
3197 param_start = tline = tline->next;
3198 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003199
H. Peter Anvine2c80182005-01-15 22:15:51 +00003200 /* Expand the macro definition now for %xdefine and %ixdefine */
3201 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3202 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003203
H. Peter Anvine2c80182005-01-15 22:15:51 +00003204 if (tok_is_(tline, "(")) {
3205 /*
3206 * This macro has parameters.
3207 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003208
H. Peter Anvine2c80182005-01-15 22:15:51 +00003209 tline = tline->next;
3210 while (1) {
3211 skip_white_(tline);
3212 if (!tline) {
3213 error(ERR_NONFATAL, "parameter identifier expected");
3214 free_tlist(origline);
3215 return DIRECTIVE_FOUND;
3216 }
3217 if (tline->type != TOK_ID) {
3218 error(ERR_NONFATAL,
3219 "`%s': parameter identifier expected",
3220 tline->text);
3221 free_tlist(origline);
3222 return DIRECTIVE_FOUND;
3223 }
3224 tline->type = TOK_SMAC_PARAM + nparam++;
3225 tline = tline->next;
3226 skip_white_(tline);
3227 if (tok_is_(tline, ",")) {
3228 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003229 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003230 if (!tok_is_(tline, ")")) {
3231 error(ERR_NONFATAL,
3232 "`)' expected to terminate macro template");
3233 free_tlist(origline);
3234 return DIRECTIVE_FOUND;
3235 }
3236 break;
3237 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003238 }
3239 last = tline;
3240 tline = tline->next;
3241 }
3242 if (tok_type_(tline, TOK_WHITESPACE))
3243 last = tline, tline = tline->next;
3244 macro_start = NULL;
3245 last->next = NULL;
3246 t = tline;
3247 while (t) {
3248 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003249 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003250 if (tt->type >= TOK_SMAC_PARAM &&
3251 !strcmp(tt->text, t->text))
3252 t->type = tt->type;
3253 }
3254 tt = t->next;
3255 t->next = macro_start;
3256 macro_start = t;
3257 t = tt;
3258 }
3259 /*
3260 * Good. We now have a macro name, a parameter count, and a
3261 * token list (in reverse order) for an expansion. We ought
3262 * to be OK just to create an SMacro, store it, and let
3263 * free_tlist have the rest of the line (which we have
3264 * carefully re-terminated after chopping off the expansion
3265 * from the end).
3266 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003267 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003268 free_tlist(origline);
3269 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003270
H. Peter Anvine2c80182005-01-15 22:15:51 +00003271 case PP_UNDEF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003272 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003273 tline = tline->next;
3274 skip_white_(tline);
3275 tline = expand_id(tline);
3276 if (!tline || (tline->type != TOK_ID &&
3277 (tline->type != TOK_PREPROC_ID ||
3278 tline->text[1] != '$'))) {
3279 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3280 free_tlist(origline);
3281 return DIRECTIVE_FOUND;
3282 }
3283 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003284 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003285 "trailing garbage after macro name ignored");
3286 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003287
H. Peter Anvine2c80182005-01-15 22:15:51 +00003288 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003289 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003290 undef_smacro(ctx, mname);
3291 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003292 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003293
H. Peter Anvin9e200162008-06-04 17:23:14 -07003294 case PP_DEFSTR:
3295 case PP_IDEFSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003296 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003297 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003298
3299 tline = tline->next;
3300 skip_white_(tline);
3301 tline = expand_id(tline);
3302 if (!tline || (tline->type != TOK_ID &&
3303 (tline->type != TOK_PREPROC_ID ||
3304 tline->text[1] != '$'))) {
3305 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003306 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003307 free_tlist(origline);
3308 return DIRECTIVE_FOUND;
3309 }
3310
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003311 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003312 last = tline;
3313 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003314 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003315
3316 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003317 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003318
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003319 p = detoken(tline, false);
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003320 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003321 macro_start->text = nasm_quote(p, strlen(p));
3322 macro_start->type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003323 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003324
3325 /*
3326 * We now have a macro name, an implicit parameter count of
3327 * zero, and a string token to use as an expansion. Create
3328 * and store an SMacro.
3329 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003330 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003331 free_tlist(origline);
3332 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003333
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003334 case PP_DEFTOK:
3335 case PP_IDEFTOK:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003336 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003337 casesense = (i == PP_DEFTOK);
3338
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003339 tline = tline->next;
3340 skip_white_(tline);
3341 tline = expand_id(tline);
3342 if (!tline || (tline->type != TOK_ID &&
3343 (tline->type != TOK_PREPROC_ID ||
3344 tline->text[1] != '$'))) {
3345 error(ERR_NONFATAL,
3346 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003347 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003348 free_tlist(origline);
3349 return DIRECTIVE_FOUND;
3350 }
3351 ctx = get_ctx(tline->text, &mname, false);
3352 last = tline;
3353 tline = expand_smacro(tline->next);
3354 last->next = NULL;
3355
3356 t = tline;
3357 while (tok_type_(t, TOK_WHITESPACE))
3358 t = t->next;
3359 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003360 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003361 error(ERR_NONFATAL,
3362 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003363 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003364 free_tlist(tline);
3365 free_tlist(origline);
3366 return DIRECTIVE_FOUND;
3367 }
3368
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003369 /*
3370 * Convert the string to a token stream. Note that smacros
3371 * are stored with the token stream reversed, so we have to
3372 * reverse the output of tokenize().
3373 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003374 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003375 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003376
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003377 /*
3378 * We now have a macro name, an implicit parameter count of
3379 * zero, and a numeric token to use as an expansion. Create
3380 * and store an SMacro.
3381 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003382 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003383 free_tlist(tline);
3384 free_tlist(origline);
3385 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003386
H. Peter Anvin418ca702008-05-30 10:42:30 -07003387 case PP_PATHSEARCH:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003388 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003389 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003390 FILE *fp;
3391 StrList *xsl = NULL;
3392 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003393
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003394 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003395
3396 tline = tline->next;
3397 skip_white_(tline);
3398 tline = expand_id(tline);
3399 if (!tline || (tline->type != TOK_ID &&
3400 (tline->type != TOK_PREPROC_ID ||
3401 tline->text[1] != '$'))) {
3402 error(ERR_NONFATAL,
3403 "`%%pathsearch' expects a macro identifier as first parameter");
3404 free_tlist(origline);
3405 return DIRECTIVE_FOUND;
3406 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003407 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003408 last = tline;
3409 tline = expand_smacro(tline->next);
3410 last->next = NULL;
3411
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003412 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003413 while (tok_type_(t, TOK_WHITESPACE))
3414 t = t->next;
3415
3416 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003417 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003418 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003419 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003420 free_tlist(origline);
3421 return DIRECTIVE_FOUND; /* but we did _something_ */
3422 }
3423 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003424 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003425 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003426 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003427 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003428 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003429
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003430 fp = inc_fopen(p, &xsl, &xst, true);
3431 if (fp) {
3432 p = xsl->str;
3433 fclose(fp); /* Don't actually care about the file */
3434 }
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003435 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003436 macro_start->text = nasm_quote(p, strlen(p));
3437 macro_start->type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003438 if (xsl)
3439 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003440
3441 /*
3442 * We now have a macro name, an implicit parameter count of
3443 * zero, and a string token to use as an expansion. Create
3444 * and store an SMacro.
3445 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003446 define_smacro(ctx, mname, casesense, 0, macro_start);
3447 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003448 free_tlist(origline);
3449 return DIRECTIVE_FOUND;
3450 }
3451
H. Peter Anvine2c80182005-01-15 22:15:51 +00003452 case PP_STRLEN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003453 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003454 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003455
H. Peter Anvine2c80182005-01-15 22:15:51 +00003456 tline = tline->next;
3457 skip_white_(tline);
3458 tline = expand_id(tline);
3459 if (!tline || (tline->type != TOK_ID &&
3460 (tline->type != TOK_PREPROC_ID ||
3461 tline->text[1] != '$'))) {
3462 error(ERR_NONFATAL,
3463 "`%%strlen' expects a macro identifier as first parameter");
3464 free_tlist(origline);
3465 return DIRECTIVE_FOUND;
3466 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003467 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003468 last = tline;
3469 tline = expand_smacro(tline->next);
3470 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003471
H. Peter Anvine2c80182005-01-15 22:15:51 +00003472 t = tline;
3473 while (tok_type_(t, TOK_WHITESPACE))
3474 t = t->next;
3475 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003476 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003477 error(ERR_NONFATAL,
3478 "`%%strlen` requires string as second parameter");
3479 free_tlist(tline);
3480 free_tlist(origline);
3481 return DIRECTIVE_FOUND;
3482 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003483
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003484 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003485 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003486
H. Peter Anvine2c80182005-01-15 22:15:51 +00003487 /*
3488 * We now have a macro name, an implicit parameter count of
3489 * zero, and a numeric token to use as an expansion. Create
3490 * and store an SMacro.
3491 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003492 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003493 free_tlist(tline);
3494 free_tlist(origline);
3495 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003496
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003497 case PP_STRCAT:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003498 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003499 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003500
3501 tline = tline->next;
3502 skip_white_(tline);
3503 tline = expand_id(tline);
3504 if (!tline || (tline->type != TOK_ID &&
3505 (tline->type != TOK_PREPROC_ID ||
3506 tline->text[1] != '$'))) {
3507 error(ERR_NONFATAL,
3508 "`%%strcat' expects a macro identifier as first parameter");
3509 free_tlist(origline);
3510 return DIRECTIVE_FOUND;
3511 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003512 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003513 last = tline;
3514 tline = expand_smacro(tline->next);
3515 last->next = NULL;
3516
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003517 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003518 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003519 switch (t->type) {
3520 case TOK_WHITESPACE:
3521 break;
3522 case TOK_STRING:
3523 len += t->a.len = nasm_unquote(t->text, NULL);
3524 break;
3525 case TOK_OTHER:
3526 if (!strcmp(t->text, ",")) /* permit comma separators */
3527 break;
3528 /* else fall through */
3529 default:
3530 error(ERR_NONFATAL,
3531 "non-string passed to `%%strcat' (%d)", t->type);
3532 free_tlist(tline);
3533 free_tlist(origline);
3534 return DIRECTIVE_FOUND;
3535 }
3536 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003537
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003538 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003539 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003540 if (t->type == TOK_STRING) {
3541 memcpy(p, t->text, t->a.len);
3542 p += t->a.len;
3543 }
3544 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003545
3546 /*
3547 * We now have a macro name, an implicit parameter count of
3548 * zero, and a numeric token to use as an expansion. Create
3549 * and store an SMacro.
3550 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003551 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3552 macro_start->text = nasm_quote(pp, len);
3553 nasm_free(pp);
3554 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003555 free_tlist(tline);
3556 free_tlist(origline);
3557 return DIRECTIVE_FOUND;
3558
H. Peter Anvine2c80182005-01-15 22:15:51 +00003559 case PP_SUBSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003560 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003561 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003562 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003563 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003564
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003565 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003566
H. Peter Anvine2c80182005-01-15 22:15:51 +00003567 tline = tline->next;
3568 skip_white_(tline);
3569 tline = expand_id(tline);
3570 if (!tline || (tline->type != TOK_ID &&
3571 (tline->type != TOK_PREPROC_ID ||
3572 tline->text[1] != '$'))) {
3573 error(ERR_NONFATAL,
3574 "`%%substr' expects a macro identifier as first parameter");
3575 free_tlist(origline);
3576 return DIRECTIVE_FOUND;
3577 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003578 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003579 last = tline;
3580 tline = expand_smacro(tline->next);
3581 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003582
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003583 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003584 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003585 while (tok_type_(t, TOK_WHITESPACE))
3586 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003587
H. Peter Anvine2c80182005-01-15 22:15:51 +00003588 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003589 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003590 error(ERR_NONFATAL,
3591 "`%%substr` requires string as second parameter");
3592 free_tlist(tline);
3593 free_tlist(origline);
3594 return DIRECTIVE_FOUND;
3595 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003596
H. Peter Anvine2c80182005-01-15 22:15:51 +00003597 tt = t->next;
3598 tptr = &tt;
3599 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003600 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003601 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003602 if (!evalresult) {
3603 free_tlist(tline);
3604 free_tlist(origline);
3605 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003606 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003607 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3608 free_tlist(tline);
3609 free_tlist(origline);
3610 return DIRECTIVE_FOUND;
3611 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003612 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003613
3614 while (tok_type_(tt, TOK_WHITESPACE))
3615 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003616 if (!tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003617 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003618 } else {
3619 tokval.t_type = TOKEN_INVALID;
3620 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3621 pass, error, NULL);
3622 if (!evalresult) {
3623 free_tlist(tline);
3624 free_tlist(origline);
3625 return DIRECTIVE_FOUND;
3626 } else if (!is_simple(evalresult)) {
3627 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3628 free_tlist(tline);
3629 free_tlist(origline);
3630 return DIRECTIVE_FOUND;
3631 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003632 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003633 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003634
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003635 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003636 /* make start and count being in range */
3637 if (start < 0)
3638 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003639 if (count < 0)
3640 count = len + count + 1 - start;
3641 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003642 count = len - start;
3643 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003644 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003645
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003646 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003647 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003648 macro_start->type = TOK_STRING;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003649
H. Peter Anvine2c80182005-01-15 22:15:51 +00003650 /*
3651 * We now have a macro name, an implicit parameter count of
3652 * zero, and a numeric token to use as an expansion. Create
3653 * and store an SMacro.
3654 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003655 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003656 free_tlist(tline);
3657 free_tlist(origline);
3658 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003659 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003660
H. Peter Anvine2c80182005-01-15 22:15:51 +00003661 case PP_ASSIGN:
3662 case PP_IASSIGN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003663 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003664 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003665
H. Peter Anvine2c80182005-01-15 22:15:51 +00003666 tline = tline->next;
3667 skip_white_(tline);
3668 tline = expand_id(tline);
3669 if (!tline || (tline->type != TOK_ID &&
3670 (tline->type != TOK_PREPROC_ID ||
3671 tline->text[1] != '$'))) {
3672 error(ERR_NONFATAL,
3673 "`%%%sassign' expects a macro identifier",
3674 (i == PP_IASSIGN ? "i" : ""));
3675 free_tlist(origline);
3676 return DIRECTIVE_FOUND;
3677 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003678 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003679 last = tline;
3680 tline = expand_smacro(tline->next);
3681 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003682
H. Peter Anvine2c80182005-01-15 22:15:51 +00003683 t = tline;
3684 tptr = &t;
3685 tokval.t_type = TOKEN_INVALID;
3686 evalresult =
3687 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3688 free_tlist(tline);
3689 if (!evalresult) {
3690 free_tlist(origline);
3691 return DIRECTIVE_FOUND;
3692 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003693
H. Peter Anvine2c80182005-01-15 22:15:51 +00003694 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003695 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003696 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003697
H. Peter Anvine2c80182005-01-15 22:15:51 +00003698 if (!is_simple(evalresult)) {
3699 error(ERR_NONFATAL,
3700 "non-constant value given to `%%%sassign'",
3701 (i == PP_IASSIGN ? "i" : ""));
3702 free_tlist(origline);
3703 return DIRECTIVE_FOUND;
3704 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003705
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003706 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003707 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003708
H. Peter Anvine2c80182005-01-15 22:15:51 +00003709 /*
3710 * We now have a macro name, an implicit parameter count of
3711 * zero, and a numeric token to use as an expansion. Create
3712 * and store an SMacro.
3713 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003714 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003715 free_tlist(origline);
3716 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003717
H. Peter Anvine2c80182005-01-15 22:15:51 +00003718 case PP_LINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003719 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003720 /*
3721 * Syntax is `%line nnn[+mmm] [filename]'
3722 */
3723 tline = tline->next;
3724 skip_white_(tline);
3725 if (!tok_type_(tline, TOK_NUMBER)) {
3726 error(ERR_NONFATAL, "`%%line' expects line number");
3727 free_tlist(origline);
3728 return DIRECTIVE_FOUND;
3729 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003730 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003731 m = 1;
3732 tline = tline->next;
3733 if (tok_is_(tline, "+")) {
3734 tline = tline->next;
3735 if (!tok_type_(tline, TOK_NUMBER)) {
3736 error(ERR_NONFATAL, "`%%line' expects line increment");
3737 free_tlist(origline);
3738 return DIRECTIVE_FOUND;
3739 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003740 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003741 tline = tline->next;
3742 }
3743 skip_white_(tline);
3744 src_set_linnum(k);
3745 istk->lineinc = m;
3746 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003747 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003748 }
3749 free_tlist(origline);
3750 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003751
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003752 case PP_WHILE:
3753 if (defining != NULL) {
3754 if (defining->type == EXP_WHILE) {
3755 defining->def_depth ++;
3756 }
3757 return NO_DIRECTIVE_FOUND;
3758 }
3759 l = NULL;
3760 if ((istk->expansion != NULL) &&
3761 (istk->expansion->emitting == false)) {
3762 j = COND_NEVER;
3763 } else {
3764 l = new_Line();
3765 l->first = copy_Token(tline->next);
3766 j = if_condition(tline->next, i);
3767 tline->next = NULL; /* it got freed */
3768 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
3769 }
3770 ed = new_ExpDef(EXP_WHILE);
3771 ed->state = j;
3772 ed->cur_depth = 1;
3773 ed->max_depth = DEADMAN_LIMIT;
3774 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
3775 if (ed->ignoring == false) {
3776 ed->line = l;
3777 ed->last = l;
3778 } else if (l != NULL) {
3779 delete_Token(l->first);
3780 nasm_free(l);
3781 l = NULL;
3782 }
3783 ed->prev = defining;
3784 defining = ed;
3785 free_tlist(origline);
3786 return DIRECTIVE_FOUND;
3787
3788 case PP_ENDWHILE:
3789 if (defining != NULL) {
3790 if (defining->type == EXP_WHILE) {
3791 if (defining->def_depth > 0) {
3792 defining->def_depth --;
3793 return NO_DIRECTIVE_FOUND;
3794 }
3795 } else {
3796 return NO_DIRECTIVE_FOUND;
3797 }
3798 }
3799 if (tline->next != NULL) {
3800 error_precond(ERR_WARNING|ERR_PASS1,
3801 "trailing garbage after `%%endwhile' ignored");
3802 }
3803 if ((defining == NULL) || (defining->type != EXP_WHILE)) {
3804 error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'");
3805 return DIRECTIVE_FOUND;
3806 }
3807 ed = defining;
3808 defining = ed->prev;
3809 if (ed->ignoring == false) {
3810 ed->prev = expansions;
3811 expansions = ed;
3812 ei = new_ExpInv(EXP_WHILE, ed);
3813 ei->current = ed->line->next;
3814 ei->emitting = true;
3815 ei->prev = istk->expansion;
3816 istk->expansion = ei;
3817 } else {
3818 nasm_free(ed);
3819 }
3820 free_tlist(origline);
3821 return DIRECTIVE_FOUND;
3822
3823 case PP_EXITWHILE:
3824 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3825 /*
3826 * We must search along istk->expansion until we hit a
3827 * while invocation. Then we disable the emitting state(s)
3828 * between exitwhile and endwhile.
3829 */
3830 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3831 if (ei->type == EXP_WHILE) {
3832 break;
3833 }
3834 }
3835
3836 if (ei != NULL) {
3837 /*
3838 * Set all invocations leading back to the while
3839 * invocation to a non-emitting state.
3840 */
3841 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3842 eei->emitting = false;
3843 }
3844 eei->emitting = false;
3845 eei->current = NULL;
3846 eei->def->cur_depth = eei->def->max_depth;
3847 } else {
3848 error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block");
3849 }
3850 free_tlist(origline);
3851 return DIRECTIVE_FOUND;
3852
3853 case PP_COMMENT:
3854 if (defining != NULL) {
3855 if (defining->type == EXP_COMMENT) {
3856 defining->def_depth ++;
3857 }
3858 return NO_DIRECTIVE_FOUND;
3859 }
3860 ed = new_ExpDef(EXP_COMMENT);
3861 ed->ignoring = true;
3862 ed->prev = defining;
3863 defining = ed;
3864 free_tlist(origline);
3865 return DIRECTIVE_FOUND;
3866
3867 case PP_ENDCOMMENT:
3868 if (defining != NULL) {
3869 if (defining->type == EXP_COMMENT) {
3870 if (defining->def_depth > 0) {
3871 defining->def_depth --;
3872 return NO_DIRECTIVE_FOUND;
3873 }
3874 } else {
3875 return NO_DIRECTIVE_FOUND;
3876 }
3877 }
3878 if ((defining == NULL) || (defining->type != EXP_COMMENT)) {
3879 error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'");
3880 return DIRECTIVE_FOUND;
3881 }
3882 ed = defining;
3883 defining = ed->prev;
3884 nasm_free(ed);
3885 free_tlist(origline);
3886 return DIRECTIVE_FOUND;
3887
3888 case PP_FINAL:
3889 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3890 if (in_final != false) {
3891 error(ERR_FATAL, "`%%final' cannot be used recursively");
3892 }
3893 tline = tline->next;
3894 skip_white_(tline);
3895 if (tline == NULL) {
3896 error(ERR_NONFATAL, "`%%final' expects at least one parameter");
3897 } else {
3898 l = new_Line();
3899 l->first = copy_Token(tline);
3900 l->next = finals;
3901 finals = l;
3902 }
3903 free_tlist(origline);
3904 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003905
H. Peter Anvine2c80182005-01-15 22:15:51 +00003906 default:
3907 error(ERR_FATAL,
3908 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003909 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003910 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003911 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003912}
3913
3914/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003915 * Ensure that a macro parameter contains a condition code and
3916 * nothing else. Return the condition code index if so, or -1
3917 * otherwise.
3918 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003919static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003920{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003921 Token *tt;
3922 int i, j, k, m;
3923
H. Peter Anvin25a99342007-09-22 17:45:45 -07003924 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003925 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003926
H. Peter Anvineba20a72002-04-30 20:53:55 +00003927 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003928 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003929 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003930 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003931 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003932 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003933 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003934
3935 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003936 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003937 while (j - i > 1) {
3938 k = (j + i) / 2;
3939 m = nasm_stricmp(t->text, conditions[k]);
3940 if (m == 0) {
3941 i = k;
3942 j = -2;
3943 break;
3944 } else if (m < 0) {
3945 j = k;
3946 } else
3947 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003948 }
3949 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003950 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003951 return i;
3952}
3953
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003954static bool paste_tokens(Token **head, const struct tokseq_match *m,
3955 int mnum, bool handle_paste_tokens)
H. Peter Anvind784a082009-04-20 14:01:18 -07003956{
3957 Token **tail, *t, *tt;
3958 Token **paste_head;
3959 bool did_paste = false;
3960 char *tmp;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003961 int i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003962
3963 /* Now handle token pasting... */
3964 paste_head = NULL;
3965 tail = head;
3966 while ((t = *tail) && (tt = t->next)) {
3967 switch (t->type) {
3968 case TOK_WHITESPACE:
3969 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003970 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003971 t->next = delete_Token(tt);
3972 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003973 /* Do not advance paste_head here */
3974 tail = &t->next;
3975 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003976 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003977 case TOK_PASTE: /* %+ */
3978 if (handle_paste_tokens) {
3979 /* Zap %+ and whitespace tokens to the right */
3980 while (t && (t->type == TOK_WHITESPACE ||
3981 t->type == TOK_PASTE))
3982 t = *tail = delete_Token(t);
3983 if (!paste_head || !t)
3984 break; /* Nothing to paste with */
3985 tail = paste_head;
3986 t = *tail;
3987 tt = t->next;
3988 while (tok_type_(tt, TOK_WHITESPACE))
3989 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003990 if (tt) {
3991 tmp = nasm_strcat(t->text, tt->text);
3992 delete_Token(t);
3993 tt = delete_Token(tt);
3994 t = *tail = tokenize(tmp);
3995 nasm_free(tmp);
3996 while (t->next) {
3997 tail = &t->next;
3998 t = t->next;
3999 }
4000 t->next = tt; /* Attach the remaining token chain */
4001 did_paste = true;
4002 }
4003 paste_head = tail;
4004 tail = &t->next;
4005 break;
4006 }
4007 /* else fall through */
4008 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004009 /*
4010 * Concatenation of tokens might look nontrivial
4011 * but in real it's pretty simple -- the caller
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004012 * prepares the masks of token types to be concatenated
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004013 * and we simply find matched sequences and slip
4014 * them together
4015 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004016 for (i = 0; i < mnum; i++) {
4017 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
4018 size_t len = 0;
4019 char *tmp, *p;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004020
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004021 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004022 len += strlen(tt->text);
4023 tt = tt->next;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004024 }
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004025
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004026 /*
4027 * Now tt points to the first token after
4028 * the potential paste area...
4029 */
4030 if (tt != t->next) {
4031 /* We have at least two tokens... */
4032 len += strlen(t->text);
4033 p = tmp = nasm_malloc(len+1);
4034 while (t != tt) {
4035 strcpy(p, t->text);
4036 p = strchr(p, '\0');
4037 t = delete_Token(t);
4038 }
4039 t = *tail = tokenize(tmp);
4040 nasm_free(tmp);
4041 while (t->next) {
4042 tail = &t->next;
4043 t = t->next;
4044 }
4045 t->next = tt; /* Attach the remaining token chain */
4046 did_paste = true;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004047 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004048 paste_head = tail;
4049 tail = &t->next;
4050 break;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004051 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004052 }
4053 if (i >= mnum) { /* no match */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004054 tail = &t->next;
4055 if (!tok_type_(t->next, TOK_WHITESPACE))
4056 paste_head = tail;
4057 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004058 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004059 }
4060 }
4061 return did_paste;
4062}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004063
4064/*
4065 * expands to a list of tokens from %{x:y}
4066 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004067static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004068{
4069 Token *t = tline, **tt, *tm, *head;
4070 char *pos;
4071 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004072
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004073 pos = strchr(tline->text, ':');
4074 nasm_assert(pos);
4075
4076 lst = atoi(pos + 1);
4077 fst = atoi(tline->text + 1);
4078
4079 /*
4080 * only macros params are accounted so
4081 * if someone passes %0 -- we reject such
4082 * value(s)
4083 */
4084 if (lst == 0 || fst == 0)
4085 goto err;
4086
4087 /* the values should be sane */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004088 if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) ||
4089 (lst > (int)ei->nparam || lst < (-(int)ei->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004090 goto err;
4091
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004092 fst = fst < 0 ? fst + (int)ei->nparam + 1: fst;
4093 lst = lst < 0 ? lst + (int)ei->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004094
4095 /* counted from zero */
4096 fst--, lst--;
4097
4098 /*
4099 * it will be at least one token
4100 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004101 tm = ei->params[(fst + ei->rotate) % ei->nparam];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004102 t = new_Token(NULL, tm->type, tm->text, 0);
4103 head = t, tt = &t->next;
4104 if (fst < lst) {
4105 for (i = fst + 1; i <= lst; i++) {
4106 t = new_Token(NULL, TOK_OTHER, ",", 0);
4107 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004108 j = (i + ei->rotate) % ei->nparam;
4109 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004110 t = new_Token(NULL, tm->type, tm->text, 0);
4111 *tt = t, tt = &t->next;
4112 }
4113 } else {
4114 for (i = fst - 1; i >= lst; i--) {
4115 t = new_Token(NULL, TOK_OTHER, ",", 0);
4116 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004117 j = (i + ei->rotate) % ei->nparam;
4118 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004119 t = new_Token(NULL, tm->type, tm->text, 0);
4120 *tt = t, tt = &t->next;
4121 }
4122 }
4123
4124 *last = tt;
4125 return head;
4126
4127err:
4128 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
4129 &tline->text[1]);
4130 return tline;
4131}
4132
H. Peter Anvin76690a12002-04-30 20:52:49 +00004133/*
4134 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004135 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004136 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004137 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004138static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004139{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004140 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004141 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004142 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004143
4144 tail = &thead;
4145 thead = NULL;
4146
H. Peter Anvine2c80182005-01-15 22:15:51 +00004147 while (tline) {
4148 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004149 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4150 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4151 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004152 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004153 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004154 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004155 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004156 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004157 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004158
H. Peter Anvine2c80182005-01-15 22:15:51 +00004159 t = tline;
4160 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004161
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004162 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
4163 if (ei->type == EXP_MMACRO) {
4164 break;
4165 }
4166 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004167 if (ei == NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004168 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004169 } else {
4170 pos = strchr(t->text, ':');
4171 if (!pos) {
4172 switch (t->text[1]) {
4173 /*
4174 * We have to make a substitution of one of the
4175 * forms %1, %-1, %+1, %%foo, %0.
4176 */
4177 case '0':
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004178 if ((strlen(t->text) > 2) && (t->text[2] == '0')) {
4179 type = TOK_ID;
4180 text = nasm_strdup(ei->label_text);
4181 } else {
4182 type = TOK_NUMBER;
4183 snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam);
4184 text = nasm_strdup(tmpbuf);
4185 }
4186 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004187 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004188 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004189 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004190 ei->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004191 text = nasm_strcat(tmpbuf, t->text + 2);
4192 break;
4193 case '-':
4194 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004195 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004196 tt = NULL;
4197 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004198 if (ei->nparam > 1)
4199 n = (n + ei->rotate) % ei->nparam;
4200 tt = ei->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004201 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004202 cc = find_cc(tt);
4203 if (cc == -1) {
4204 error(ERR_NONFATAL,
4205 "macro parameter %d is not a condition code",
4206 n + 1);
4207 text = NULL;
4208 } else {
4209 type = TOK_ID;
4210 if (inverse_ccs[cc] == -1) {
4211 error(ERR_NONFATAL,
4212 "condition code `%s' is not invertible",
4213 conditions[cc]);
4214 text = NULL;
4215 } else
4216 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4217 }
4218 break;
4219 case '+':
4220 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004221 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004222 tt = NULL;
4223 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004224 if (ei->nparam > 1)
4225 n = (n + ei->rotate) % ei->nparam;
4226 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004227 }
4228 cc = find_cc(tt);
4229 if (cc == -1) {
4230 error(ERR_NONFATAL,
4231 "macro parameter %d is not a condition code",
4232 n + 1);
4233 text = NULL;
4234 } else {
4235 type = TOK_ID;
4236 text = nasm_strdup(conditions[cc]);
4237 }
4238 break;
4239 default:
4240 n = atoi(t->text + 1) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004241 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004242 tt = NULL;
4243 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004244 if (ei->nparam > 1)
4245 n = (n + ei->rotate) % ei->nparam;
4246 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004247 }
4248 if (tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004249 for (i = 0; i < ei->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004250 *tail = new_Token(NULL, tt->type, tt->text, 0);
4251 tail = &(*tail)->next;
4252 tt = tt->next;
4253 }
4254 }
4255 text = NULL; /* we've done it here */
4256 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004257 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004258 } else {
4259 /*
4260 * seems we have a parameters range here
4261 */
4262 Token *head, **last;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004263 head = expand_mmac_params_range(ei, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004264 if (head != t) {
4265 *tail = head;
4266 *last = tline;
4267 tline = head;
4268 text = NULL;
4269 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004270 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004271 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004272 if (!text) {
4273 delete_Token(t);
4274 } else {
4275 *tail = t;
4276 tail = &t->next;
4277 t->type = type;
4278 nasm_free(t->text);
4279 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004280 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004281 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004282 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004283 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004284 } else if (tline->type == TOK_INDIRECT) {
4285 t = tline;
4286 tline = tline->next;
4287 tt = tokenize(t->text);
4288 tt = expand_mmac_params(tt);
4289 tt = expand_smacro(tt);
4290 *tail = tt;
4291 while (tt) {
4292 tt->a.mac = NULL; /* Necessary? */
4293 tail = &tt->next;
4294 tt = tt->next;
4295 }
4296 delete_Token(t);
4297 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004298 } else {
4299 t = *tail = tline;
4300 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004301 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004302 tail = &t->next;
4303 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004304 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004305 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004306
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004307 if (changed) {
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004308 const struct tokseq_match t[] = {
4309 {
4310 PP_CONCAT_MASK(TOK_ID) |
4311 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4312 PP_CONCAT_MASK(TOK_ID) |
4313 PP_CONCAT_MASK(TOK_NUMBER) |
4314 PP_CONCAT_MASK(TOK_FLOAT) |
4315 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4316 },
4317 {
4318 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4319 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4320 }
4321 };
4322 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004323 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004324
H. Peter Anvin76690a12002-04-30 20:52:49 +00004325 return thead;
4326}
4327
4328/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004329 * Expand all single-line macro calls made in the given line.
4330 * Return the expanded version of the line. The original is deemed
4331 * to be destroyed in the process. (In reality we'll just move
4332 * Tokens from input to output a lot of the time, rather than
4333 * actually bothering to destroy and replicate.)
4334 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004335
H. Peter Anvine2c80182005-01-15 22:15:51 +00004336static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004337{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004338 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004339 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004340 Token **params;
4341 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004342 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004343 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004344 Token *org_tline = tline;
4345 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004346 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004347 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004348 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004349
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004350 /*
4351 * Trick: we should avoid changing the start token pointer since it can
4352 * be contained in "next" field of other token. Because of this
4353 * we allocate a copy of first token and work with it; at the end of
4354 * routine we copy it back
4355 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004356 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004357 tline = new_Token(org_tline->next, org_tline->type,
4358 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004359 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004360 nasm_free(org_tline->text);
4361 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004362 }
4363
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004364 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004365
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004366again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004367 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004368 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004369
H. Peter Anvine2c80182005-01-15 22:15:51 +00004370 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004371 if (!--deadman) {
4372 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004373 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004374 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004375
H. Peter Anvine2c80182005-01-15 22:15:51 +00004376 if ((mname = tline->text)) {
4377 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004378 if (tline->type == TOK_ID) {
4379 head = (SMacro *)hash_findix(&smacros, mname);
4380 } else if (tline->type == TOK_PREPROC_ID) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004381 ctx = get_ctx(mname, &mname, false);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004382 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4383 } else
4384 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004385
H. Peter Anvine2c80182005-01-15 22:15:51 +00004386 /*
4387 * We've hit an identifier. As in is_mmacro below, we first
4388 * check whether the identifier is a single-line macro at
4389 * all, then think about checking for parameters if
4390 * necessary.
4391 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004392 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004393 if (!mstrcmp(m->name, mname, m->casesense))
4394 break;
4395 if (m) {
4396 mstart = tline;
4397 params = NULL;
4398 paramsize = NULL;
4399 if (m->nparam == 0) {
4400 /*
4401 * Simple case: the macro is parameterless. Discard the
4402 * one token that the macro call took, and push the
4403 * expansion back on the to-do stack.
4404 */
4405 if (!m->expansion) {
4406 if (!strcmp("__FILE__", m->name)) {
4407 int32_t num = 0;
4408 char *file = NULL;
4409 src_get(&num, &file);
4410 tline->text = nasm_quote(file, strlen(file));
4411 tline->type = TOK_STRING;
4412 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004413 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004414 }
4415 if (!strcmp("__LINE__", m->name)) {
4416 nasm_free(tline->text);
4417 make_tok_num(tline, src_get_linnum());
4418 continue;
4419 }
4420 if (!strcmp("__BITS__", m->name)) {
4421 nasm_free(tline->text);
4422 make_tok_num(tline, globalbits);
4423 continue;
4424 }
4425 tline = delete_Token(tline);
4426 continue;
4427 }
4428 } else {
4429 /*
4430 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004431 * exists and takes parameters. We must find the
4432 * parameters in the call, count them, find the SMacro
4433 * that corresponds to that form of the macro call, and
4434 * substitute for the parameters when we expand. What a
4435 * pain.
4436 */
4437 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004438 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004439 do {
4440 t = tline->next;
4441 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004442 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004443 t->text = NULL;
4444 t = tline->next = delete_Token(t);
4445 }
4446 tline = t;
4447 } while (tok_type_(tline, TOK_WHITESPACE));
4448 if (!tok_is_(tline, "(")) {
4449 /*
4450 * This macro wasn't called with parameters: ignore
4451 * the call. (Behaviour borrowed from gnu cpp.)
4452 */
4453 tline = mstart;
4454 m = NULL;
4455 } else {
4456 int paren = 0;
4457 int white = 0;
4458 brackets = 0;
4459 nparam = 0;
4460 sparam = PARAM_DELTA;
4461 params = nasm_malloc(sparam * sizeof(Token *));
4462 params[0] = tline->next;
4463 paramsize = nasm_malloc(sparam * sizeof(int));
4464 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004465 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004466 /*
4467 * For some unusual expansions
4468 * which concatenates function call
4469 */
4470 t = tline->next;
4471 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004472 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004473 t->text = NULL;
4474 t = tline->next = delete_Token(t);
4475 }
4476 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004477
H. Peter Anvine2c80182005-01-15 22:15:51 +00004478 if (!tline) {
4479 error(ERR_NONFATAL,
4480 "macro call expects terminating `)'");
4481 break;
4482 }
4483 if (tline->type == TOK_WHITESPACE
4484 && brackets <= 0) {
4485 if (paramsize[nparam])
4486 white++;
4487 else
4488 params[nparam] = tline->next;
4489 continue; /* parameter loop */
4490 }
4491 if (tline->type == TOK_OTHER
4492 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004493 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004494 if (ch == ',' && !paren && brackets <= 0) {
4495 if (++nparam >= sparam) {
4496 sparam += PARAM_DELTA;
4497 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004498 sparam * sizeof(Token *));
4499 paramsize = nasm_realloc(paramsize,
4500 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004501 }
4502 params[nparam] = tline->next;
4503 paramsize[nparam] = 0;
4504 white = 0;
4505 continue; /* parameter loop */
4506 }
4507 if (ch == '{' &&
4508 (brackets > 0 || (brackets == 0 &&
4509 !paramsize[nparam])))
4510 {
4511 if (!(brackets++)) {
4512 params[nparam] = tline->next;
4513 continue; /* parameter loop */
4514 }
4515 }
4516 if (ch == '}' && brackets > 0)
4517 if (--brackets == 0) {
4518 brackets = -1;
4519 continue; /* parameter loop */
4520 }
4521 if (ch == '(' && !brackets)
4522 paren++;
4523 if (ch == ')' && brackets <= 0)
4524 if (--paren < 0)
4525 break;
4526 }
4527 if (brackets < 0) {
4528 brackets = 0;
4529 error(ERR_NONFATAL, "braces do not "
4530 "enclose all of macro parameter");
4531 }
4532 paramsize[nparam] += white + 1;
4533 white = 0;
4534 } /* parameter loop */
4535 nparam++;
4536 while (m && (m->nparam != nparam ||
4537 mstrcmp(m->name, mname,
4538 m->casesense)))
4539 m = m->next;
4540 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004541 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004542 "macro `%s' exists, "
4543 "but not taking %d parameters",
4544 mstart->text, nparam);
4545 }
4546 }
4547 if (m && m->in_progress)
4548 m = NULL;
4549 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004550 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004551 * Design question: should we handle !tline, which
4552 * indicates missing ')' here, or expand those
4553 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004554 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004555 */
4556 nasm_free(params);
4557 nasm_free(paramsize);
4558 tline = mstart;
4559 } else {
4560 /*
4561 * Expand the macro: we are placed on the last token of the
4562 * call, so that we can easily split the call from the
4563 * following tokens. We also start by pushing an SMAC_END
4564 * token for the cycle removal.
4565 */
4566 t = tline;
4567 if (t) {
4568 tline = t->next;
4569 t->next = NULL;
4570 }
4571 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004572 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004573 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004574 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004575 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004576 if (t->type >= TOK_SMAC_PARAM) {
4577 Token *pcopy = tline, **ptail = &pcopy;
4578 Token *ttt, *pt;
4579 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004580
H. Peter Anvine2c80182005-01-15 22:15:51 +00004581 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004582 i = paramsize[t->type - TOK_SMAC_PARAM];
4583 while (--i >= 0) {
4584 pt = *ptail = new_Token(tline, ttt->type,
4585 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004586 ptail = &pt->next;
4587 ttt = ttt->next;
4588 }
4589 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004590 } else if (t->type == TOK_PREPROC_Q) {
4591 tt = new_Token(tline, TOK_ID, mname, 0);
4592 tline = tt;
4593 } else if (t->type == TOK_PREPROC_QQ) {
4594 tt = new_Token(tline, TOK_ID, m->name, 0);
4595 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004596 } else {
4597 tt = new_Token(tline, t->type, t->text, 0);
4598 tline = tt;
4599 }
4600 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004601
H. Peter Anvine2c80182005-01-15 22:15:51 +00004602 /*
4603 * Having done that, get rid of the macro call, and clean
4604 * up the parameters.
4605 */
4606 nasm_free(params);
4607 nasm_free(paramsize);
4608 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004609 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004610 continue; /* main token loop */
4611 }
4612 }
4613 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004614
H. Peter Anvine2c80182005-01-15 22:15:51 +00004615 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004616 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004617 tline = delete_Token(tline);
4618 } else {
4619 t = *tail = tline;
4620 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004621 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004622 t->next = NULL;
4623 tail = &t->next;
4624 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004625 }
4626
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004627 /*
4628 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004629 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004630 * TOK_IDs should be concatenated.
4631 * Also we look for %+ tokens and concatenate the tokens before and after
4632 * them (without white spaces in between).
4633 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004634 if (expanded) {
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004635 const struct tokseq_match t[] = {
4636 {
4637 PP_CONCAT_MASK(TOK_ID) |
4638 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4639 PP_CONCAT_MASK(TOK_ID) |
4640 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4641 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4642 }
4643 };
4644 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004645 /*
4646 * If we concatenated something, *and* we had previously expanded
4647 * an actual macro, scan the lines again for macros...
4648 */
4649 tline = thead;
4650 expanded = false;
4651 goto again;
4652 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004653 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004654
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004655err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004656 if (org_tline) {
4657 if (thead) {
4658 *org_tline = *thead;
4659 /* since we just gave text to org_line, don't free it */
4660 thead->text = NULL;
4661 delete_Token(thead);
4662 } else {
4663 /* the expression expanded to empty line;
4664 we can't return NULL for some reasons
4665 we just set the line to a single WHITESPACE token. */
4666 memset(org_tline, 0, sizeof(*org_tline));
4667 org_tline->text = NULL;
4668 org_tline->type = TOK_WHITESPACE;
4669 }
4670 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004671 }
4672
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004673 return thead;
4674}
4675
4676/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004677 * Similar to expand_smacro but used exclusively with macro identifiers
4678 * right before they are fetched in. The reason is that there can be
4679 * identifiers consisting of several subparts. We consider that if there
4680 * are more than one element forming the name, user wants a expansion,
4681 * otherwise it will be left as-is. Example:
4682 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004683 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004684 *
4685 * the identifier %$abc will be left as-is so that the handler for %define
4686 * will suck it and define the corresponding value. Other case:
4687 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004688 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004689 *
4690 * In this case user wants name to be expanded *before* %define starts
4691 * working, so we'll expand %$abc into something (if it has a value;
4692 * otherwise it will be left as-is) then concatenate all successive
4693 * PP_IDs into one.
4694 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004695static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004696{
4697 Token *cur, *oldnext = NULL;
4698
H. Peter Anvin734b1882002-04-30 21:01:08 +00004699 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004700 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004701
4702 cur = tline;
4703 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004704 (cur->next->type == TOK_ID ||
4705 cur->next->type == TOK_PREPROC_ID
4706 || cur->next->type == TOK_NUMBER))
4707 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004708
4709 /* If identifier consists of just one token, don't expand */
4710 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004711 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004712
H. Peter Anvine2c80182005-01-15 22:15:51 +00004713 if (cur) {
4714 oldnext = cur->next; /* Detach the tail past identifier */
4715 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004716 }
4717
H. Peter Anvin734b1882002-04-30 21:01:08 +00004718 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004719
H. Peter Anvine2c80182005-01-15 22:15:51 +00004720 if (cur) {
4721 /* expand_smacro possibly changhed tline; re-scan for EOL */
4722 cur = tline;
4723 while (cur && cur->next)
4724 cur = cur->next;
4725 if (cur)
4726 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004727 }
4728
4729 return tline;
4730}
4731
4732/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004733 * Determine whether the given line constitutes a multi-line macro
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004734 * call, and return the ExpDef structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004735 * to check for an initial label - that's taken care of in
4736 * expand_mmacro - but must check numbers of parameters. Guaranteed
4737 * to be called with tline->type == TOK_ID, so the putative macro
4738 * name is easy to find.
4739 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004740static ExpDef *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004741{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004742 ExpDef *head, *ed;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004743 Token **params;
4744 int nparam;
4745
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004746 head = (ExpDef *) hash_findix(&expdefs, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004747
4748 /*
4749 * Efficiency: first we see if any macro exists with the given
4750 * name. If not, we can return NULL immediately. _Then_ we
4751 * count the parameters, and then we look further along the
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004752 * list if necessary to find the proper ExpDef.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004753 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004754 list_for_each(ed, head)
4755 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004756 break;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004757 if (!ed)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004758 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004759
4760 /*
4761 * OK, we have a potential macro. Count and demarcate the
4762 * parameters.
4763 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004764 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004765
4766 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004767 * So we know how many parameters we've got. Find the ExpDef
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004768 * structure that handles this number.
4769 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004770 while (ed) {
4771 if (ed->nparam_min <= nparam
4772 && (ed->plus || nparam <= ed->nparam_max)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004773 /*
4774 * It's right, and we can use it. Add its default
4775 * parameters to the end of our list if necessary.
4776 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004777 if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004778 params =
4779 nasm_realloc(params,
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004780 ((ed->nparam_min + ed->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004781 1) * sizeof(*params)));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004782 while (nparam < ed->nparam_min + ed->ndefs) {
4783 params[nparam] = ed->defaults[nparam - ed->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004784 nparam++;
4785 }
4786 }
4787 /*
4788 * If we've gone over the maximum parameter count (and
4789 * we're in Plus mode), ignore parameters beyond
4790 * nparam_max.
4791 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004792 if (ed->plus && nparam > ed->nparam_max)
4793 nparam = ed->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004794 /*
4795 * Then terminate the parameter list, and leave.
4796 */
4797 if (!params) { /* need this special case */
4798 params = nasm_malloc(sizeof(*params));
4799 nparam = 0;
4800 }
4801 params[nparam] = NULL;
4802 *params_array = params;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004803 return ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004804 }
4805 /*
4806 * This one wasn't right: look for the next one with the
4807 * same name.
4808 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004809 list_for_each(ed, ed->next)
4810 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004811 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004812 }
4813
4814 /*
4815 * After all that, we didn't find one with the right number of
4816 * parameters. Issue a warning, and fail to expand the macro.
4817 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004818 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004819 "macro `%s' exists, but not taking %d parameters",
4820 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004821 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004822 return NULL;
4823}
4824
4825/*
4826 * Expand the multi-line macro call made by the given line, if
4827 * there is one to be expanded. If there is, push the expansion on
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004828 * istk->expansion and return true. Otherwise return false.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004829 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004830static bool expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004831{
H. Peter Anvineba20a72002-04-30 20:53:55 +00004832 Token *label = NULL;
4833 int dont_prepend = 0;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004834 Token **params, *t, *mtok;
4835 Line *l = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004836 ExpDef *ed;
4837 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004838 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004839 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004840
4841 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004842 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004843 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004844 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004845 return false;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004846 mtok = t;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004847 ed = is_mmacro(t, &params);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004848 if (ed != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004849 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004850 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004851 Token *last;
4852 /*
4853 * We have an id which isn't a macro call. We'll assume
4854 * it might be a label; we'll also check to see if a
4855 * colon follows it. Then, if there's another id after
4856 * that lot, we'll check it again for macro-hood.
4857 */
4858 label = last = t;
4859 t = t->next;
4860 if (tok_type_(t, TOK_WHITESPACE))
4861 last = t, t = t->next;
4862 if (tok_is_(t, ":")) {
4863 dont_prepend = 1;
4864 last = t, t = t->next;
4865 if (tok_type_(t, TOK_WHITESPACE))
4866 last = t, t = t->next;
4867 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004868 if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, &params)))
4869 return false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004870 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004871 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004872 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004873 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004874
4875 /*
4876 * Fix up the parameters: this involves stripping leading and
4877 * trailing whitespace, then stripping braces if they are
4878 * present.
4879 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004880 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004881 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004882
H. Peter Anvine2c80182005-01-15 22:15:51 +00004883 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004884 int brace = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004885 int comma = (!ed->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004886
H. Peter Anvine2c80182005-01-15 22:15:51 +00004887 t = params[i];
4888 skip_white_(t);
4889 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004890 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004891 params[i] = t;
4892 paramlen[i] = 0;
4893 while (t) {
4894 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4895 break; /* ... because we have hit a comma */
4896 if (comma && t->type == TOK_WHITESPACE
4897 && tok_is_(t->next, ","))
4898 break; /* ... or a space then a comma */
4899 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4900 break; /* ... or a brace */
4901 t = t->next;
4902 paramlen[i]++;
4903 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004904 }
4905
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004906 if (ed->cur_depth >= ed->max_depth) {
4907 if (ed->max_depth > 1) {
4908 error(ERR_WARNING,
4909 "reached maximum macro recursion depth of %i for %s",
4910 ed->max_depth,ed->name);
4911 }
4912 return false;
4913 } else {
4914 ed->cur_depth ++;
4915 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004916
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004917 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004918 * OK, we have found a ExpDef structure representing a
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004919 * previously defined mmacro. Create an expansion invocation
4920 * and point it back to the expansion definition. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004921 * parameter tokens and macro-local tokens doesn't get done
4922 * until the single-line macro substitution process; this is
4923 * because delaying them allows us to change the semantics
4924 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004925 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004926 ei = new_ExpInv(EXP_MMACRO, ed);
4927 ei->name = nasm_strdup(mname);
4928 //ei->label = label;
4929 //ei->label_text = detoken(label, false);
4930 ei->current = ed->line;
4931 ei->emitting = true;
4932 //ei->iline = tline;
4933 ei->params = params;
4934 ei->nparam = nparam;
4935 ei->rotate = 0;
4936 ei->paramlen = paramlen;
4937 ei->lineno = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004938
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004939 ei->prev = istk->expansion;
4940 istk->expansion = ei;
4941
4942 /*
4943 * Special case: detect %00 on first invocation; if found,
4944 * avoid emitting any labels that precede the mmacro call.
4945 * ed->prepend is set to -1 when %00 is detected, else 1.
4946 */
4947 if (ed->prepend == 0) {
4948 for (l = ed->line; l != NULL; l = l->next) {
4949 for (t = l->first; t != NULL; t = t->next) {
4950 if ((t->type == TOK_PREPROC_ID) &&
4951 (strlen(t->text) == 3) &&
4952 (t->text[1] == '0') && (t->text[2] == '0')) {
4953 dont_prepend = -1;
4954 break;
4955 }
4956 }
4957 if (dont_prepend < 0) {
4958 break;
4959 }
4960 }
4961 ed->prepend = ((dont_prepend < 0) ? -1 : 1);
4962 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004963
4964 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004965 * If we had a label, push it on as the first line of
4966 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004967 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004968 if (label != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004969 if (ed->prepend < 0) {
4970 ei->label_text = detoken(label, false);
4971 } else {
4972 if (dont_prepend == 0) {
4973 t = label;
4974 while (t->next != NULL) {
4975 t = t->next;
4976 }
4977 t->next = new_Token(NULL, TOK_OTHER, ":", 0);
4978 }
4979 l = new_Line();
4980 l->first = copy_Token(label);
4981 l->next = ei->current;
4982 ei->current = l;
4983 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004984 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004985
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004986 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004987
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004988 istk->mmac_depth++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004989 return true;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004990}
4991
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004992/* The function that actually does the error reporting */
4993static void verror(int severity, const char *fmt, va_list arg)
4994{
4995 char buff[1024];
4996
4997 vsnprintf(buff, sizeof(buff), fmt, arg);
4998
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03004999 if (istk && istk->mmac_depth > 0) {
5000 ExpInv *ei = istk->expansion;
5001 int lineno = ei->lineno;
5002 while (ei) {
5003 if (ei->type == EXP_MMACRO)
5004 break;
5005 lineno += ei->relno;
5006 ei = ei->prev;
5007 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005008 nasm_error(severity, "(%s:%d) %s", ei->def->name,
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005009 lineno, buff);
5010 } else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005011 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005012}
5013
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005014/*
5015 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07005016 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005017 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005018static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005019{
5020 va_list arg;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005021 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005022 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00005023 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005024}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005025
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005026/*
5027 * Because %else etc are evaluated in the state context
5028 * of the previous branch, errors might get lost with error():
5029 * %if 0 ... %else trailing garbage ... %endif
5030 * So %else etc should report errors with this function.
5031 */
5032static void error_precond(int severity, const char *fmt, ...)
5033{
5034 va_list arg;
5035
5036 /* Only ignore the error if it's really in a dead branch */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005037 if ((istk != NULL) &&
5038 (istk->expansion != NULL) &&
5039 (istk->expansion->type == EXP_IF) &&
5040 (istk->expansion->def->state == COND_NEVER))
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005041 return;
5042
5043 va_start(arg, fmt);
5044 verror(severity, fmt, arg);
5045 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005046}
5047
H. Peter Anvin734b1882002-04-30 21:01:08 +00005048static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005049pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005050{
H. Peter Anvin7383b402008-09-24 10:20:40 -07005051 Token *t;
5052
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005053 cstk = NULL;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005054 istk = nasm_zalloc(sizeof(Include));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005055 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00005056 src_set_fname(nasm_strdup(file));
5057 src_set_linnum(0);
5058 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005059 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07005060 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00005061 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005062 defining = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005063 finals = NULL;
5064 in_final = false;
Charles Crayned4200be2008-07-12 16:42:33 -07005065 nested_mac_count = 0;
5066 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005067 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005068 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005069 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005070 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005071 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005072 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005073 }
H. Peter Anvind2456592008-06-19 15:04:18 -07005074 any_extrastdmac = extrastdmac && *extrastdmac;
5075 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005076 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005077
5078 /*
5079 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5080 * The caller, however, will also pass in 3 for preprocess-only so
5081 * we can set __PASS__ accordingly.
5082 */
5083 pass = apass > 2 ? 2 : apass;
5084
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005085 dephead = deptail = deplist;
5086 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005087 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
5088 sl->next = NULL;
5089 strcpy(sl->str, file);
5090 *deptail = sl;
5091 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005092 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07005093
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005094 /*
5095 * Define the __PASS__ macro. This is defined here unlike
5096 * all the other builtins, because it is special -- it varies between
5097 * passes.
5098 */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005099 t = nasm_zalloc(sizeof(*t));
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005100 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07005101 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005102}
5103
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005104static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005105{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005106 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005107 Token *tline;
Keith Kanios9858cec2010-11-08 00:58:02 -06005108 ExpDef *ed;
5109 ExpInv *ei;
5110 Line *l;
5111 int j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005112
H. Peter Anvine2c80182005-01-15 22:15:51 +00005113 while (1) {
5114 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005115 * Fetch a tokenized line, either from the expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005116 * buffer or from the input file.
5117 */
5118 tline = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005119
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005120 while (1) { /* until we get a line we can use */
5121 /*
5122 * Fetch a tokenized line from the expansion buffer
5123 */
5124 if (istk->expansion != NULL) {
5125 ei = istk->expansion;
5126 if (ei->current != NULL) {
5127 if (ei->emitting == false) {
5128 ei->current = NULL;
5129 continue;
5130 }
5131 l = ei->current;
5132 ei->current = l->next;
5133 ei->lineno++;
5134 tline = copy_Token(l->first);
5135 if (((ei->type == EXP_REP) ||
5136 (ei->type == EXP_MMACRO) ||
5137 (ei->type == EXP_WHILE))
5138 && (ei->def->nolist == false)) {
5139 char *p = detoken(tline, false);
5140 list->line(LIST_MACRO, p);
5141 nasm_free(p);
5142 }
5143 if (ei->linnum > -1) {
5144 src_set_linnum(src_get_linnum() + 1);
5145 }
5146 break;
5147 } else if ((ei->type == EXP_REP) &&
5148 (ei->def->cur_depth < ei->def->max_depth)) {
5149 ei->def->cur_depth ++;
5150 ei->current = ei->def->line;
5151 ei->lineno = 0;
5152 continue;
5153 } else if ((ei->type == EXP_WHILE) &&
5154 (ei->def->cur_depth < ei->def->max_depth)) {
5155 ei->current = ei->def->line;
5156 ei->lineno = 0;
5157 tline = copy_Token(ei->current->first);
5158 j = if_condition(tline, PP_WHILE);
5159 tline = NULL;
5160 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
5161 if (j == COND_IF_TRUE) {
5162 ei->current = ei->current->next;
5163 ei->def->cur_depth ++;
5164 } else {
5165 ei->emitting = false;
5166 ei->current = NULL;
5167 ei->def->cur_depth = ei->def->max_depth;
5168 }
5169 continue;
5170 } else {
5171 istk->expansion = ei->prev;
5172 ed = ei->def;
5173 if (ed != NULL) {
5174 if ((ei->emitting == true) &&
5175 (ed->max_depth == DEADMAN_LIMIT) &&
5176 (ed->cur_depth == DEADMAN_LIMIT)
5177 ) {
5178 error(ERR_FATAL, "runaway expansion detected, aborting");
5179 }
5180 if (ed->cur_depth > 0) {
5181 ed->cur_depth --;
Keith Kanios94124652010-12-18 11:47:28 -06005182 } else if (ed->type != EXP_MMACRO) {
Keith Kanios6a7c3e92010-12-18 11:49:53 -06005183 expansions = ed->prev;
5184 free_expdef(ed);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005185 }
5186 if ((ei->type == EXP_REP) ||
5187 (ei->type == EXP_MMACRO) ||
5188 (ei->type == EXP_WHILE)) {
5189 list->downlevel(LIST_MACRO);
5190 if (ei->type == EXP_MMACRO) {
5191 istk->mmac_depth--;
5192 }
5193 }
5194 }
5195 if (ei->linnum > -1) {
5196 src_set_linnum(ei->linnum);
5197 }
5198 free_expinv(ei);
5199 continue;
5200 }
5201 }
5202
5203 /*
5204 * Read in line from input and tokenize
5205 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005206 line = read_line();
5207 if (line) { /* from the current input file */
5208 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005209 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005210 nasm_free(line);
5211 break;
5212 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005213
H. Peter Anvine2c80182005-01-15 22:15:51 +00005214 /*
5215 * The current file has ended; work down the istk
5216 */
5217 {
5218 Include *i = istk;
5219 fclose(i->fp);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005220 if (i->expansion != NULL) {
5221 error(ERR_FATAL,
5222 "end of file while still in an expansion");
5223 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005224 /* only set line and file name if there's a next node */
5225 if (i->next) {
5226 src_set_linnum(i->lineno);
5227 nasm_free(src_set_fname(i->fname));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005228 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005229 if ((i->next == NULL) && (finals != NULL)) {
5230 in_final = true;
5231 ei = new_ExpInv(EXP_FINAL, NULL);
5232 ei->emitting = true;
5233 ei->current = finals;
5234 istk->expansion = ei;
5235 finals = NULL;
5236 continue;
5237 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005238 istk = i->next;
5239 list->downlevel(LIST_INCLUDE);
5240 nasm_free(i);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005241 if (istk == NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005242 if (finals != NULL) {
5243 in_final = true;
5244 } else {
5245 return NULL;
5246 }
5247 }
5248 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005249 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005250 }
5251
5252 if (defining == NULL) {
5253 tline = expand_mmac_params(tline);
5254 }
5255
H. Peter Anvine2c80182005-01-15 22:15:51 +00005256 /*
5257 * Check the line to see if it's a preprocessor directive.
5258 */
5259 if (do_directive(tline) == DIRECTIVE_FOUND) {
5260 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005261 } else if (defining != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005262 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005263 * We're defining an expansion. We emit nothing at all,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005264 * and just shove the tokenized line on to the definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005265 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005266 if (defining->ignoring == false) {
5267 Line *l = new_Line();
5268 l->first = tline;
5269 if (defining->line == NULL) {
5270 defining->line = l;
5271 defining->last = l;
5272 } else {
5273 defining->last->next = l;
5274 defining->last = l;
5275 }
5276 } else {
Keith Kanios104803d2010-12-18 11:05:46 -06005277 free_tlist(tline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005278 }
5279 defining->linecount++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005280 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005281 } else if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005282 (istk->expansion->emitting != true)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005283 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005284 * We're in a non-emitting branch of an expansion.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005285 * Emit nothing at all, not even a blank line: when we
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005286 * emerge from the expansion we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005287 * directive so we keep our place correctly.
5288 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005289 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005290 continue;
5291 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005292 tline = expand_smacro(tline);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005293 if (expand_mmacro(tline) != true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005294 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005295 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005296 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005297 line = detoken(tline, true);
5298 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005299 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005300 } else {
5301 continue;
5302 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005303 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005304 }
5305 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005306}
5307
H. Peter Anvine2c80182005-01-15 22:15:51 +00005308static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005309{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005310 if (defining != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005311 error(ERR_NONFATAL, "end of file while still defining an expansion");
5312 while (defining != NULL) {
5313 ExpDef *ed = defining;
5314 defining = ed->prev;
5315 free_expdef(ed);
5316 }
5317 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005318 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005319 while (cstk != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005320 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005321 free_macros();
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005322 while (istk != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005323 Include *i = istk;
5324 istk = istk->next;
5325 fclose(i->fp);
5326 nasm_free(i->fname);
5327 nasm_free(i);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005328 while (i->expansion != NULL) {
5329 ExpInv *ei = i->expansion;
5330 i->expansion = ei->prev;
5331 free_expinv(ei);
5332 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005333 }
5334 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005335 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005336 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005337 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005338 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005339 free_llist(predef);
5340 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005341 while ((i = ipath)) {
5342 ipath = i->next;
5343 if (i->path)
5344 nasm_free(i->path);
5345 nasm_free(i);
5346 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005347 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005348}
5349
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005350void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005351{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005352 IncPath *i = nasm_zalloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005353
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005354 if (path)
5355 i->path = nasm_strdup(path);
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005356
H. Peter Anvin89cee572009-07-15 09:16:54 -04005357 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005358 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005359 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005360 j = j->next;
5361 j->next = i;
5362 } else {
5363 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005364 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005365}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005366
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005367void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005368{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005369 Token *inc, *space, *name;
5370 Line *l;
5371
H. Peter Anvin734b1882002-04-30 21:01:08 +00005372 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5373 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5374 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005375
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005376 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005377 l->next = predef;
5378 l->first = inc;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005379 predef = l;
5380}
5381
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005382void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005383{
5384 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005385 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005386 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005387
5388 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005389 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5390 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005391 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005392 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005393 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005394 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005395 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005396
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005397 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005398 l->next = predef;
5399 l->first = def;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005400 predef = l;
5401}
5402
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005403void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005404{
5405 Token *def, *space;
5406 Line *l;
5407
H. Peter Anvin734b1882002-04-30 21:01:08 +00005408 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5409 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005410 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005411
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005412 l = new_Line();
H. Peter Anvin620515a2002-04-30 20:57:38 +00005413 l->next = predef;
5414 l->first = def;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005415 predef = l;
5416}
5417
Keith Kaniosb7a89542007-04-12 02:40:54 +00005418/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005419 * This function is used to assist with "runtime" preprocessor
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005420 * directives, e.g. pp_runtime("%define __BITS__ 64");
Keith Kaniosb7a89542007-04-12 02:40:54 +00005421 *
5422 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5423 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5424 */
5425
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005426void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005427{
5428 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005429
Keith Kaniosb7a89542007-04-12 02:40:54 +00005430 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005431 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005432 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005433
Keith Kaniosb7a89542007-04-12 02:40:54 +00005434}
5435
H. Peter Anvina70547f2008-07-19 21:44:26 -07005436void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005437{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005438 extrastdmac = macros;
5439}
5440
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005441static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005442{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005443 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005444 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005445 tok->text = nasm_strdup(numbuf);
5446 tok->type = TOK_NUMBER;
5447}
5448
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005449Preproc nasmpp = {
5450 pp_reset,
5451 pp_getline,
5452 pp_cleanup
5453};