blob: 91137ad3511693985eb3f325f9d6c62629fdd779 [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;
938
H. Peter Anvine2c80182005-01-15 22:15:51 +0000939 while (*line) {
940 p = line;
941 if (*p == '%') {
942 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300943 if (*p == '+' && !nasm_isdigit(p[1])) {
944 p++;
945 type = TOK_PASTE;
946 } else if (nasm_isdigit(*p) ||
947 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000948 do {
949 p++;
950 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700951 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000952 type = TOK_PREPROC_ID;
953 } else if (*p == '{') {
954 p++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500955 while (*p && *p != '}') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000956 p[-1] = *p;
957 p++;
958 }
959 p[-1] = '\0';
960 if (*p)
961 p++;
962 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300963 } else if (*p == '[') {
964 int lvl = 1;
965 line += 2; /* Skip the leading %[ */
966 p++;
967 while (lvl && (c = *p++)) {
968 switch (c) {
969 case ']':
970 lvl--;
971 break;
972 case '%':
973 if (*p == '[')
974 lvl++;
975 break;
976 case '\'':
977 case '\"':
978 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400979 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300980 break;
981 default:
982 break;
983 }
984 }
985 p--;
986 if (*p)
987 *p++ = '\0';
Keith Kaniosba935f22010-12-18 10:54:39 -0600988 if (lvl && !defining)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300989 error(ERR_NONFATAL, "unterminated %[ construct");
990 type = TOK_INDIRECT;
991 } else if (*p == '?') {
992 type = TOK_PREPROC_Q; /* %? */
993 p++;
994 if (*p == '?') {
995 type = TOK_PREPROC_QQ; /* %?? */
996 p++;
997 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +0400998 } else if (*p == '!') {
999 type = TOK_PREPROC_ID;
1000 p++;
1001 if (isidchar(*p)) {
1002 do {
1003 p++;
1004 } while (isidchar(*p));
1005 } else if (*p == '\'' || *p == '\"' || *p == '`') {
1006 p = nasm_skip_string(p);
1007 if (*p)
1008 p++;
Keith Kaniosba935f22010-12-18 10:54:39 -06001009 else if(!defining)
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001010 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
1011 } else {
1012 /* %! without string or identifier */
1013 type = TOK_OTHER; /* Legacy behavior... */
1014 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001015 } else if (isidchar(*p) ||
1016 ((*p == '!' || *p == '%' || *p == '$') &&
1017 isidchar(p[1]))) {
1018 do {
1019 p++;
1020 }
1021 while (isidchar(*p));
1022 type = TOK_PREPROC_ID;
1023 } else {
1024 type = TOK_OTHER;
1025 if (*p == '%')
1026 p++;
1027 }
1028 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1029 type = TOK_ID;
1030 p++;
1031 while (*p && isidchar(*p))
1032 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001033 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001034 /*
1035 * A string token.
1036 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001037 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001038 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001039
H. Peter Anvine2c80182005-01-15 22:15:51 +00001040 if (*p) {
1041 p++;
Keith Kaniosba935f22010-12-18 10:54:39 -06001042 } else if(!defining) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001043 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001044 /* Handling unterminated strings by UNV */
1045 /* type = -1; */
1046 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001047 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001048 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001049 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001050 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001051 bool is_hex = false;
1052 bool is_float = false;
1053 bool has_e = false;
1054 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001055
H. Peter Anvine2c80182005-01-15 22:15:51 +00001056 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001057 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001058 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001059
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001060 if (*p == '$') {
1061 p++;
1062 is_hex = true;
1063 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001064
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001065 for (;;) {
1066 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001067
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001068 if (!is_hex && (c == 'e' || c == 'E')) {
1069 has_e = true;
1070 if (*p == '+' || *p == '-') {
1071 /*
1072 * e can only be followed by +/- if it is either a
1073 * prefixed hex number or a floating-point number
1074 */
1075 p++;
1076 is_float = true;
1077 }
1078 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1079 is_hex = true;
1080 } else if (c == 'P' || c == 'p') {
1081 is_float = true;
1082 if (*p == '+' || *p == '-')
1083 p++;
1084 } else if (isnumchar(c) || c == '_')
1085 ; /* just advance */
1086 else if (c == '.') {
1087 /*
1088 * we need to deal with consequences of the legacy
1089 * parser, like "1.nolist" being two tokens
1090 * (TOK_NUMBER, TOK_ID) here; at least give it
1091 * a shot for now. In the future, we probably need
1092 * a flex-based scanner with proper pattern matching
1093 * to do it as well as it can be done. Nothing in
1094 * the world is going to help the person who wants
1095 * 0x123.p16 interpreted as two tokens, though.
1096 */
1097 r = p;
1098 while (*r == '_')
1099 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001100
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001101 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1102 (!is_hex && (*r == 'e' || *r == 'E')) ||
1103 (*r == 'p' || *r == 'P')) {
1104 p = r;
1105 is_float = true;
1106 } else
1107 break; /* Terminate the token */
1108 } else
1109 break;
1110 }
1111 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001112
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001113 if (p == line+1 && *line == '$') {
1114 type = TOK_OTHER; /* TOKEN_HERE */
1115 } else {
1116 if (has_e && !is_hex) {
1117 /* 1e13 is floating-point, but 1e13h is not */
1118 is_float = true;
1119 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001120
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001121 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1122 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001123 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001125 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001126 /*
1127 * Whitespace just before end-of-line is discarded by
1128 * pretending it's a comment; whitespace just before a
1129 * comment gets lumped into the comment.
1130 */
1131 if (!*p || *p == ';') {
1132 type = TOK_COMMENT;
1133 while (*p)
1134 p++;
1135 }
1136 } else if (*p == ';') {
1137 type = TOK_COMMENT;
1138 while (*p)
1139 p++;
1140 } else {
1141 /*
1142 * Anything else is an operator of some kind. We check
1143 * for all the double-character operators (>>, <<, //,
1144 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001145 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001146 */
1147 type = TOK_OTHER;
1148 if ((p[0] == '>' && p[1] == '>') ||
1149 (p[0] == '<' && p[1] == '<') ||
1150 (p[0] == '/' && p[1] == '/') ||
1151 (p[0] == '<' && p[1] == '=') ||
1152 (p[0] == '>' && p[1] == '=') ||
1153 (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++;
1160 }
1161 p++;
1162 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001163
H. Peter Anvine2c80182005-01-15 22:15:51 +00001164 /* Handling unterminated string by UNV */
1165 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001166 {
1167 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1168 t->text[p-line] = *line;
1169 tail = &t->next;
1170 }
1171 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001172 if (type != TOK_COMMENT) {
1173 *tail = t = new_Token(NULL, type, line, p - line);
1174 tail = &t->next;
1175 }
1176 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001177 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001178 return list;
1179}
1180
H. Peter Anvince616072002-04-30 21:02:23 +00001181/*
1182 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001183 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001184 * deleted only all at once by the delete_Blocks function.
1185 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001187{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001188 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001189
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001190 /* first, get to the end of the linked list */
1191 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001192 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001193 /* now allocate the requested chunk */
1194 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001195
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001196 /* now allocate a new block for the next request */
1197 b->next = nasm_malloc(sizeof(Blocks));
1198 /* and initialize the contents of the new block */
1199 b->next->next = NULL;
1200 b->next->chunk = NULL;
1201 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001202}
1203
1204/*
1205 * this function deletes all managed blocks of memory
1206 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001207static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001208{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001210
H. Peter Anvin70653092007-10-19 14:42:29 -07001211 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001212 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001213 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001214 * free it.
1215 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001216 while (b) {
1217 if (b->chunk)
1218 nasm_free(b->chunk);
1219 a = b;
1220 b = b->next;
1221 if (a != &blocks)
1222 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001223 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001224}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001225
1226/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001227 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001228 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001229 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001230 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001231static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001232 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001233{
1234 Token *t;
1235 int i;
1236
H. Peter Anvin89cee572009-07-15 09:16:54 -04001237 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001238 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1239 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1240 freeTokens[i].next = &freeTokens[i + 1];
1241 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001242 }
1243 t = freeTokens;
1244 freeTokens = t->next;
1245 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001246 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001247 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001248 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001249 t->text = NULL;
1250 } else {
1251 if (txtlen == 0)
1252 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001253 t->text = nasm_malloc(txtlen+1);
1254 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001255 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001256 }
1257 return t;
1258}
1259
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001260static Token *copy_Token(Token * tline)
1261{
1262 Token *t, *tt, *first = NULL, *prev = NULL;
1263 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001264 for (tt = tline; tt != NULL; tt = tt->next) {
1265 if (!freeTokens) {
1266 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1267 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1268 freeTokens[i].next = &freeTokens[i + 1];
1269 freeTokens[i].next = NULL;
1270 }
1271 t = freeTokens;
1272 freeTokens = t->next;
1273 t->next = NULL;
1274 t->text = tt->text ? nasm_strdup(tt->text) : NULL;
1275 t->a.mac = tt->a.mac;
1276 t->a.len = tt->a.len;
1277 t->type = tt->type;
1278 if (prev != NULL) {
1279 prev->next = t;
1280 } else {
1281 first = t;
1282 }
1283 prev = t;
1284 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001285 return first;
1286}
1287
H. Peter Anvine2c80182005-01-15 22:15:51 +00001288static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001289{
1290 Token *next = t->next;
1291 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001292 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001293 freeTokens = t;
1294 return next;
1295}
1296
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001297/*
1298 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001299 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1300 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001301 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001302static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001303{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001304 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001305 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001306 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001307 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001308
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001309 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001311 char *v;
1312 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001313
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001314 v = t->text + 2;
1315 if (*v == '\'' || *v == '\"' || *v == '`') {
1316 size_t len = nasm_unquote(v, NULL);
1317 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001318
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001319 if (len != clen) {
1320 error(ERR_NONFATAL | ERR_PASS1,
1321 "NUL character in %! string");
1322 v = NULL;
1323 }
1324 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001325
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001326 if (v) {
1327 char *p = getenv(v);
1328 if (!p) {
1329 error(ERR_NONFATAL | ERR_PASS1,
1330 "nonexistent environment variable `%s'", v);
1331 p = "";
1332 }
1333 t->text = nasm_strdup(p);
1334 }
1335 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001337
H. Peter Anvine2c80182005-01-15 22:15:51 +00001338 /* Expand local macros here and not during preprocessing */
1339 if (expand_locals &&
1340 t->type == TOK_PREPROC_ID && t->text &&
1341 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001342 const char *q;
1343 char *p;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001344 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001345 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001346 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001347 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001348 p = nasm_strcat(buffer, q);
1349 nasm_free(t->text);
1350 t->text = p;
1351 }
1352 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001353
1354 /* Expand %? and %?? directives */
Keith Kanios3136d482010-11-13 09:34:34 -06001355 if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001356 ((t->type == TOK_PREPROC_Q) ||
1357 (t->type == TOK_PREPROC_QQ))) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001358 ExpInv *ei;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001359 for (ei = istk->expansion; ei != NULL; ei = ei->prev){
1360 if (ei->type == EXP_MMACRO) {
1361 nasm_free(t->text);
1362 if (t->type == TOK_PREPROC_Q) {
1363 t->text = nasm_strdup(ei->name);
1364 } else {
1365 t->text = nasm_strdup(ei->def->name);
1366 }
1367 break;
1368 }
1369 }
1370 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001371
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001372 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001373 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001374 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001375 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001376 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001377
H. Peter Anvin734b1882002-04-30 21:01:08 +00001378 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001379
1380 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001381 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001382 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001383 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001384 q = t->text;
1385 while (*q)
1386 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001387 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001388 }
1389 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001390
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001391 return line;
1392}
1393
1394/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001395 * Initialize a new Line
1396 */
Cyrill Gorcunov29cb0bb2010-11-11 11:19:43 +03001397static inline Line *new_Line(void)
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001398{
Cyrill Gorcunoveb7bf982010-11-11 23:08:14 +03001399 Line *l = nasm_malloc(sizeof(Line));
1400 l->next = NULL;
1401 l->first = NULL;
1402 return l;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001403}
1404
1405
1406/*
1407 * Initialize a new Expansion Definition
1408 */
1409static ExpDef *new_ExpDef(int exp_type)
1410{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001411 ExpDef *ed = (ExpDef*)nasm_zalloc(sizeof(ExpDef));
1412 ed->type = exp_type;
1413 ed->casesense = true;
1414 ed->state = COND_NEVER;
1415
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001416 return ed;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001417}
1418
1419
1420/*
1421 * Initialize a new Expansion Instance
1422 */
1423static ExpInv *new_ExpInv(int exp_type, ExpDef *ed)
1424{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001425 ExpInv *ei = (ExpInv*)nasm_zalloc(sizeof(ExpInv));
1426 ei->type = exp_type;
1427 ei->def = ed;
1428 ei->unique = ++unique;
1429
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001430 if ((istk->mmac_depth < 1) &&
1431 (istk->expansion == NULL) &&
1432 (ed != NULL) &&
1433 (ed->type != EXP_MMACRO) &&
1434 (ed->type != EXP_REP) &&
1435 (ed->type != EXP_WHILE)) {
1436 ei->linnum = src_get_linnum();
1437 src_set_linnum(ei->linnum - ed->linecount - 1);
1438 } else {
1439 ei->linnum = -1;
1440 }
1441 if ((istk->expansion == NULL) ||
1442 (ei->type == EXP_MMACRO)) {
1443 ei->relno = 0;
1444 } else {
1445 ei->relno = istk->expansion->lineno;
1446 if (ed != NULL) {
1447 ei->relno -= (ed->linecount + 1);
1448 }
1449 }
1450 return ei;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001451}
1452
1453/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001454 * A scanner, suitable for use by the expression evaluator, which
1455 * operates on a line of Tokens. Expects a pointer to a pointer to
1456 * the first token in the line to be passed in as its private_data
1457 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001458 *
1459 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001460 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001461static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001462{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001463 Token **tlineptr = private_data;
1464 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001465 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001466
H. Peter Anvine2c80182005-01-15 22:15:51 +00001467 do {
1468 tline = *tlineptr;
1469 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001470 } while (tline && (tline->type == TOK_WHITESPACE ||
1471 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001472
1473 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001474 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001475
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001476 tokval->t_charptr = tline->text;
1477
H. Peter Anvin76690a12002-04-30 20:52:49 +00001478 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001479 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001480 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001481 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001482
H. Peter Anvine2c80182005-01-15 22:15:51 +00001483 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001484 p = tokval->t_charptr = tline->text;
1485 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001486 tokval->t_charptr++;
1487 return tokval->t_type = TOKEN_ID;
1488 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001489
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001490 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001491 if (r >= p+MAX_KEYWORD)
1492 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001493 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001494 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001495 *s = '\0';
1496 /* right, so we have an identifier sitting in temp storage. now,
1497 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001498 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001499 }
1500
H. Peter Anvine2c80182005-01-15 22:15:51 +00001501 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001502 bool rn_error;
1503 tokval->t_integer = readnum(tline->text, &rn_error);
1504 tokval->t_charptr = tline->text;
1505 if (rn_error)
1506 return tokval->t_type = TOKEN_ERRNUM;
1507 else
1508 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001509 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001510
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001511 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001512 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001513 }
1514
H. Peter Anvine2c80182005-01-15 22:15:51 +00001515 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001516 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001517
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001518 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001519 tokval->t_charptr = tline->text;
1520 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001521
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001522 if (ep[0] != bq || ep[1] != '\0')
1523 return tokval->t_type = TOKEN_ERRSTR;
1524 else
1525 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001526 }
1527
H. Peter Anvine2c80182005-01-15 22:15:51 +00001528 if (tline->type == TOK_OTHER) {
1529 if (!strcmp(tline->text, "<<"))
1530 return tokval->t_type = TOKEN_SHL;
1531 if (!strcmp(tline->text, ">>"))
1532 return tokval->t_type = TOKEN_SHR;
1533 if (!strcmp(tline->text, "//"))
1534 return tokval->t_type = TOKEN_SDIV;
1535 if (!strcmp(tline->text, "%%"))
1536 return tokval->t_type = TOKEN_SMOD;
1537 if (!strcmp(tline->text, "=="))
1538 return tokval->t_type = TOKEN_EQ;
1539 if (!strcmp(tline->text, "<>"))
1540 return tokval->t_type = TOKEN_NE;
1541 if (!strcmp(tline->text, "!="))
1542 return tokval->t_type = TOKEN_NE;
1543 if (!strcmp(tline->text, "<="))
1544 return tokval->t_type = TOKEN_LE;
1545 if (!strcmp(tline->text, ">="))
1546 return tokval->t_type = TOKEN_GE;
1547 if (!strcmp(tline->text, "&&"))
1548 return tokval->t_type = TOKEN_DBL_AND;
1549 if (!strcmp(tline->text, "^^"))
1550 return tokval->t_type = TOKEN_DBL_XOR;
1551 if (!strcmp(tline->text, "||"))
1552 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001553 }
1554
1555 /*
1556 * We have no other options: just return the first character of
1557 * the token text.
1558 */
1559 return tokval->t_type = tline->text[0];
1560}
1561
1562/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001563 * Compare a string to the name of an existing macro; this is a
1564 * simple wrapper which calls either strcmp or nasm_stricmp
1565 * depending on the value of the `casesense' parameter.
1566 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001567static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001568{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001569 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001570}
1571
1572/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001573 * Compare a string to the name of an existing macro; this is a
1574 * simple wrapper which calls either strcmp or nasm_stricmp
1575 * depending on the value of the `casesense' parameter.
1576 */
1577static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1578{
1579 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1580}
1581
1582/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001583 * Return the Context structure associated with a %$ token. Return
1584 * NULL, having _already_ reported an error condition, if the
1585 * context stack isn't deep enough for the supplied number of $
1586 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001587 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001588 * also scanned for such smacro, until it is found; if not -
1589 * only the context that directly results from the number of $'s
1590 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001591 *
1592 * If "namep" is non-NULL, set it to the pointer to the macro name
1593 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001594 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001595static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001596 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001597{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001598 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001599 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001600 int i;
1601
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001602 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001603 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001604
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001605 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001606 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001607
H. Peter Anvine2c80182005-01-15 22:15:51 +00001608 if (!cstk) {
1609 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1610 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001611 }
1612
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001613 name += 2;
1614 ctx = cstk;
1615 i = 0;
1616 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001617 name++;
1618 i++;
1619 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001620 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001621 if (!ctx) {
1622 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001623 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001624 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001625 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001626
1627 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001628 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001629
Keith Kanios404589e2010-08-10 20:12:57 -05001630 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001631 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001632
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001633 do {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001634 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001635 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001636 while (m) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001637 if (!mstrcmp(m->name, name, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001638 return ctx;
1639 m = m->next;
1640 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001641 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001642 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001643 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001644 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001645}
1646
1647/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001648 * Check to see if a file is already in a string list
1649 */
1650static bool in_list(const StrList *list, const char *str)
1651{
1652 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001653 if (!strcmp(list->str, str))
1654 return true;
1655 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001656 }
1657 return false;
1658}
1659
1660/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001661 * Open an include file. This routine must always return a valid
1662 * file pointer if it returns - it's responsible for throwing an
1663 * ERR_FATAL and bombing out completely if not. It should also try
1664 * the include path one by one until it finds the file or reaches
1665 * the end of the path.
1666 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001667static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001668 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001669{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001670 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001671 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001672 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001673 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001674 size_t prefix_len = 0;
1675 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001676
H. Peter Anvine2c80182005-01-15 22:15:51 +00001677 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001678 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunov6f38fe62010-11-11 11:32:16 +03001679 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001680 memcpy(sl->str, prefix, prefix_len);
1681 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001682 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001683 if (fp && dhead && !in_list(*dhead, sl->str)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001684 **dtail = sl;
1685 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001686 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001687 nasm_free(sl);
1688 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001689 if (fp)
1690 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001691 if (!ip) {
1692 if (!missing_ok)
1693 break;
1694 prefix = NULL;
1695 } else {
1696 prefix = ip->path;
1697 ip = ip->next;
1698 }
1699 if (prefix) {
1700 prefix_len = strlen(prefix);
1701 } else {
1702 /* -MG given and file not found */
1703 if (dhead && !in_list(*dhead, file)) {
1704 sl = nasm_malloc(len+1+sizeof sl->next);
1705 sl->next = NULL;
1706 strcpy(sl->str, file);
1707 **dtail = sl;
1708 *dtail = &sl->next;
1709 }
1710 return NULL;
1711 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001712 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001713
H. Peter Anvin734b1882002-04-30 21:01:08 +00001714 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001715 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001716}
1717
1718/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001719 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001720 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001721 * return true if _any_ single-line macro of that name is defined.
1722 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001723 * `nparam' or no parameters is defined.
1724 *
1725 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001726 * defined, or nparam is -1, the address of the definition structure
1727 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001728 * is NULL, no action will be taken regarding its contents, and no
1729 * error will occur.
1730 *
1731 * Note that this is also called with nparam zero to resolve
1732 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001733 *
1734 * If you already know which context macro belongs to, you can pass
1735 * the context pointer as first parameter; if you won't but name begins
1736 * with %$ the context will be automatically computed. If all_contexts
1737 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001738 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001739static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001740smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001741 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001742{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001743 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001744 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001745
H. Peter Anvin97a23472007-09-16 17:57:25 -07001746 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001747 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001748 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001749 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001750 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001752 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001753 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001754 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001755 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001756 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001757 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001758
H. Peter Anvine2c80182005-01-15 22:15:51 +00001759 while (m) {
1760 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001761 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001762 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001763 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001764 *defn = m;
1765 else
1766 *defn = NULL;
1767 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001768 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001769 }
1770 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001771 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001772
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001773 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001774}
1775
1776/*
1777 * Count and mark off the parameters in a multi-line macro call.
1778 * This is called both from within the multi-line macro expansion
1779 * code, and also to mark off the default parameters when provided
1780 * in a %macro definition line.
1781 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001783{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001784 int paramsize, brace;
1785
1786 *nparam = paramsize = 0;
1787 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001788 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001789 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001790 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001791 paramsize += PARAM_DELTA;
1792 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1793 }
1794 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001795 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001796 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001797 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001798 (*params)[(*nparam)++] = t;
1799 while (tok_isnt_(t, brace ? "}" : ","))
1800 t = t->next;
1801 if (t) { /* got a comma/brace */
1802 t = t->next;
1803 if (brace) {
1804 /*
1805 * Now we've found the closing brace, look further
1806 * for the comma.
1807 */
1808 skip_white_(t);
1809 if (tok_isnt_(t, ",")) {
1810 error(ERR_NONFATAL,
1811 "braces do not enclose all of macro parameter");
1812 while (tok_isnt_(t, ","))
1813 t = t->next;
1814 }
1815 if (t)
1816 t = t->next; /* eat the comma */
1817 }
1818 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001819 }
1820}
1821
1822/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001823 * Determine whether one of the various `if' conditions is true or
1824 * not.
1825 *
1826 * We must free the tline we get passed.
1827 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001828static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001829{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001830 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001831 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001832 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001833 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001834 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001835 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001836 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001837
1838 origline = tline;
1839
H. Peter Anvine2c80182005-01-15 22:15:51 +00001840 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001841 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001842 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001843 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001844 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001845 if (!tline)
1846 break;
1847 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001848 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001849 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001850 free_tlist(origline);
1851 return -1;
1852 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001853 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001854 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001855 tline = tline->next;
1856 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001857 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001858
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001859 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001860 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001861 while (tline) {
1862 skip_white_(tline);
1863 if (!tline || (tline->type != TOK_ID &&
1864 (tline->type != TOK_PREPROC_ID ||
1865 tline->text[1] != '$'))) {
1866 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001867 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001868 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001869 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001870 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001871 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001872 tline = tline->next;
1873 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001874 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001875
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001876 case PPC_IFENV:
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001877 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001878 j = false; /* have we matched yet? */
1879 while (tline) {
1880 skip_white_(tline);
1881 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001882 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001883 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001884 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001885 error(ERR_NONFATAL,
1886 "`%s' expects environment variable names",
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001887 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001888 goto fail;
1889 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001890 p = tline->text;
1891 if (tline->type == TOK_PREPROC_ID)
1892 p += 2; /* Skip leading %! */
1893 if (*p == '\'' || *p == '\"' || *p == '`')
1894 nasm_unquote_cstr(p, ct);
1895 if (getenv(p))
1896 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001897 tline = tline->next;
1898 }
1899 break;
1900
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001901 case PPC_IFIDN:
1902 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001903 tline = expand_smacro(tline);
1904 t = tt = tline;
1905 while (tok_isnt_(tt, ","))
1906 tt = tt->next;
1907 if (!tt) {
1908 error(ERR_NONFATAL,
1909 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001910 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001911 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001912 }
1913 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001914 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001915 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1916 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1917 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001918 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001919 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001920 }
1921 if (t->type == TOK_WHITESPACE) {
1922 t = t->next;
1923 continue;
1924 }
1925 if (tt->type == TOK_WHITESPACE) {
1926 tt = tt->next;
1927 continue;
1928 }
1929 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001930 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001931 break;
1932 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001933 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001934 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001935 size_t l1 = nasm_unquote(t->text, NULL);
1936 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001937
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001938 if (l1 != l2) {
1939 j = false;
1940 break;
1941 }
1942 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1943 j = false;
1944 break;
1945 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001946 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001947 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001948 break;
1949 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001950
H. Peter Anvine2c80182005-01-15 22:15:51 +00001951 t = t->next;
1952 tt = tt->next;
1953 }
1954 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001955 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001956 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001957
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001958 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001959 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001960 bool found = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001961 ExpDef searching, *ed;
H. Peter Anvin65747262002-05-07 00:10:05 +00001962
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001963 skip_white_(tline);
1964 tline = expand_id(tline);
1965 if (!tok_type_(tline, TOK_ID)) {
1966 error(ERR_NONFATAL,
1967 "`%s' expects a macro name", pp_directives[ct]);
1968 goto fail;
1969 }
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03001970 memset(&searching, 0, sizeof(searching));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001971 searching.name = nasm_strdup(tline->text);
1972 searching.casesense = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001973 searching.nparam_max = INT_MAX;
1974 tline = expand_smacro(tline->next);
1975 skip_white_(tline);
1976 if (!tline) {
1977 } else if (!tok_type_(tline, TOK_NUMBER)) {
1978 error(ERR_NONFATAL,
1979 "`%s' expects a parameter count or nothing",
1980 pp_directives[ct]);
1981 } else {
1982 searching.nparam_min = searching.nparam_max =
1983 readnum(tline->text, &j);
1984 if (j)
1985 error(ERR_NONFATAL,
1986 "unable to parse parameter count `%s'",
1987 tline->text);
1988 }
1989 if (tline && tok_is_(tline->next, "-")) {
1990 tline = tline->next->next;
1991 if (tok_is_(tline, "*"))
1992 searching.nparam_max = INT_MAX;
1993 else if (!tok_type_(tline, TOK_NUMBER))
1994 error(ERR_NONFATAL,
1995 "`%s' expects a parameter count after `-'",
1996 pp_directives[ct]);
1997 else {
1998 searching.nparam_max = readnum(tline->text, &j);
1999 if (j)
2000 error(ERR_NONFATAL,
2001 "unable to parse parameter count `%s'",
2002 tline->text);
2003 if (searching.nparam_min > searching.nparam_max)
2004 error(ERR_NONFATAL,
2005 "minimum parameter count exceeds maximum");
2006 }
2007 }
2008 if (tline && tok_is_(tline->next, "+")) {
2009 tline = tline->next;
2010 searching.plus = true;
2011 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002012 ed = (ExpDef *) hash_findix(&expdefs, searching.name);
2013 while (ed != NULL) {
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002014 if (!strcmp(ed->name, searching.name) &&
2015 (ed->nparam_min <= searching.nparam_max || searching.plus) &&
2016 (searching.nparam_min <= ed->nparam_max || ed->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002017 found = true;
2018 break;
2019 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002020 ed = ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002021 }
2022 if (tline && tline->next)
2023 error(ERR_WARNING|ERR_PASS1,
2024 "trailing garbage after %%ifmacro ignored");
2025 nasm_free(searching.name);
2026 j = found;
2027 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002028 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002029
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002030 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002031 needtype = TOK_ID;
2032 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002033 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002034 needtype = TOK_NUMBER;
2035 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002036 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002037 needtype = TOK_STRING;
2038 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002039
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002040iftype:
2041 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002042
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002043 while (tok_type_(t, TOK_WHITESPACE) ||
2044 (needtype == TOK_NUMBER &&
2045 tok_type_(t, TOK_OTHER) &&
2046 (t->text[0] == '-' || t->text[0] == '+') &&
2047 !t->text[1]))
2048 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002049
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002050 j = tok_type_(t, needtype);
2051 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002052
2053 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002054 t = tline = expand_smacro(tline);
2055 while (tok_type_(t, TOK_WHITESPACE))
2056 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002057
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002058 j = false;
2059 if (t) {
2060 t = t->next; /* Skip the actual token */
2061 while (tok_type_(t, TOK_WHITESPACE))
2062 t = t->next;
2063 j = !t; /* Should be nothing left */
2064 }
2065 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002066
H. Peter Anvin134b9462008-02-16 17:01:40 -08002067 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002068 t = tline = expand_smacro(tline);
2069 while (tok_type_(t, TOK_WHITESPACE))
2070 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002071
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002072 j = !t; /* Should be empty */
2073 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002074
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002075 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002076 t = tline = expand_smacro(tline);
2077 tptr = &t;
2078 tokval.t_type = TOKEN_INVALID;
2079 evalresult = evaluate(ppscan, tptr, &tokval,
2080 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002081 if (!evalresult)
2082 return -1;
2083 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002084 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002085 "trailing garbage after expression ignored");
2086 if (!is_simple(evalresult)) {
2087 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002088 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002089 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002090 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002091 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002092 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002093
H. Peter Anvine2c80182005-01-15 22:15:51 +00002094 default:
2095 error(ERR_FATAL,
2096 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002097 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002098 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002099 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002100
2101 free_tlist(origline);
2102 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002103
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002104fail:
2105 free_tlist(origline);
2106 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002107}
2108
2109/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002110 * Common code for defining an smacro
2111 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002112static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002113 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002114{
2115 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002116 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002117
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002118 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002119 if (!smac) {
2120 error(ERR_WARNING|ERR_PASS1,
2121 "single-line macro `%s' defined both with and"
2122 " without parameters", mname);
2123 /*
2124 * Some instances of the old code considered this a failure,
2125 * some others didn't. What is the right thing to do here?
2126 */
2127 free_tlist(expansion);
2128 return false; /* Failure */
2129 } else {
2130 /*
2131 * We're redefining, so we have to take over an
2132 * existing SMacro structure. This means freeing
2133 * what was already in it.
2134 */
2135 nasm_free(smac->name);
2136 free_tlist(smac->expansion);
2137 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002138 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002139 smtbl = ctx ? &ctx->localmac : &smacros;
2140 smhead = (SMacro **) hash_findi_add(smtbl, mname);
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002141 smac = nasm_zalloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002142 smac->next = *smhead;
2143 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002144 }
2145 smac->name = nasm_strdup(mname);
2146 smac->casesense = casesense;
2147 smac->nparam = nparam;
2148 smac->expansion = expansion;
2149 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002150 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002151}
2152
2153/*
2154 * Undefine an smacro
2155 */
2156static void undef_smacro(Context *ctx, const char *mname)
2157{
2158 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002159 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002160
H. Peter Anvin166c2472008-05-28 12:28:58 -07002161 smtbl = ctx ? &ctx->localmac : &smacros;
2162 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002163
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002164 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002165 /*
2166 * We now have a macro name... go hunt for it.
2167 */
2168 sp = smhead;
2169 while ((s = *sp) != NULL) {
2170 if (!mstrcmp(s->name, mname, s->casesense)) {
2171 *sp = s->next;
2172 nasm_free(s->name);
2173 free_tlist(s->expansion);
2174 nasm_free(s);
2175 } else {
2176 sp = &s->next;
2177 }
2178 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002179 }
2180}
2181
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002182/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002183 * Parse a mmacro specification.
2184 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002185static bool parse_mmacro_spec(Token *tline, ExpDef *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002186{
2187 bool err;
2188
2189 tline = tline->next;
2190 skip_white_(tline);
2191 tline = expand_id(tline);
2192 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002193 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2194 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002195 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002196
H. Peter Anvina26433d2008-07-16 14:40:01 -07002197 def->name = nasm_strdup(tline->text);
2198 def->plus = false;
2199 def->nolist = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002200// def->in_progress = 0;
2201// def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002202 def->nparam_min = 0;
2203 def->nparam_max = 0;
2204
H. Peter Anvina26433d2008-07-16 14:40:01 -07002205 tline = expand_smacro(tline->next);
2206 skip_white_(tline);
2207 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002208 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002209 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002210 def->nparam_min = def->nparam_max =
2211 readnum(tline->text, &err);
2212 if (err)
2213 error(ERR_NONFATAL,
2214 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002215 }
2216 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002217 tline = tline->next->next;
2218 if (tok_is_(tline, "*")) {
2219 def->nparam_max = INT_MAX;
2220 } else if (!tok_type_(tline, TOK_NUMBER)) {
2221 error(ERR_NONFATAL,
2222 "`%s' expects a parameter count after `-'", directive);
2223 } else {
2224 def->nparam_max = readnum(tline->text, &err);
2225 if (err) {
2226 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2227 tline->text);
2228 }
2229 if (def->nparam_min > def->nparam_max) {
2230 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2231 }
2232 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002233 }
2234 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002235 tline = tline->next;
2236 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002237 }
2238 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002239 !nasm_stricmp(tline->next->text, ".nolist")) {
2240 tline = tline->next;
2241 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002242 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002243
H. Peter Anvina26433d2008-07-16 14:40:01 -07002244 /*
2245 * Handle default parameters.
2246 */
2247 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002248 def->dlist = tline->next;
2249 tline->next = NULL;
2250 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002251 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002252 def->dlist = NULL;
2253 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002254 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002255 def->line = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002256
H. Peter Anvin89cee572009-07-15 09:16:54 -04002257 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002258 !def->plus)
2259 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2260 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002261
H. Peter Anvina26433d2008-07-16 14:40:01 -07002262 return true;
2263}
2264
2265
2266/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002267 * Decode a size directive
2268 */
2269static int parse_size(const char *str) {
2270 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002271 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002272 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002273 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002274
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002275 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002276}
2277
Ed Beroset3ab3f412002-06-11 03:31:49 +00002278/**
2279 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002280 * Find out if a line contains a preprocessor directive, and deal
2281 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002282 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002283 * If a directive _is_ found, it is the responsibility of this routine
2284 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002285 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002286 * @param tline a pointer to the current tokeninzed line linked list
2287 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002288 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002289 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002290static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002291{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002292 enum preproc_token i;
2293 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002294 bool err;
2295 int nparam;
2296 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002297 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002298 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002299 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002300 char *p, *pp;
2301 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002302 Include *inc;
2303 Context *ctx;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002304 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002305 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002306 struct tokenval tokval;
2307 expr *evalresult;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002308 ExpDef *ed, *eed, **edhead;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002309 ExpInv *ei, *eei;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002310 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002311 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002312 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002313
2314 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002315
H. Peter Anvineba20a72002-04-30 20:53:55 +00002316 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002317 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002318 (tline->text[1] == '%' || tline->text[1] == '$'
2319 || tline->text[1] == '!'))
2320 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002321
H. Peter Anvin4169a472007-09-12 01:29:43 +00002322 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002323
H. Peter Anvin4169a472007-09-12 01:29:43 +00002324 switch (i) {
2325 case PP_INVALID:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002326 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002327 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2328 tline->text);
2329 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002330
H. Peter Anvine2c80182005-01-15 22:15:51 +00002331 case PP_STACKSIZE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002332 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002333 /* Directive to tell NASM what the default stack size is. The
2334 * default is for a 16-bit stack, and this can be overriden with
2335 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002336 */
2337 tline = tline->next;
2338 if (tline && tline->type == TOK_WHITESPACE)
2339 tline = tline->next;
2340 if (!tline || tline->type != TOK_ID) {
2341 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2342 free_tlist(origline);
2343 return DIRECTIVE_FOUND;
2344 }
2345 if (nasm_stricmp(tline->text, "flat") == 0) {
2346 /* All subsequent ARG directives are for a 32-bit stack */
2347 StackSize = 4;
2348 StackPointer = "ebp";
2349 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002350 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002351 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2352 /* All subsequent ARG directives are for a 64-bit stack */
2353 StackSize = 8;
2354 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002355 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002356 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002357 } else if (nasm_stricmp(tline->text, "large") == 0) {
2358 /* All subsequent ARG directives are for a 16-bit stack,
2359 * far function call.
2360 */
2361 StackSize = 2;
2362 StackPointer = "bp";
2363 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002364 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002365 } else if (nasm_stricmp(tline->text, "small") == 0) {
2366 /* All subsequent ARG directives are for a 16-bit stack,
2367 * far function call. We don't support near functions.
2368 */
2369 StackSize = 2;
2370 StackPointer = "bp";
2371 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002372 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002373 } else {
2374 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2375 free_tlist(origline);
2376 return DIRECTIVE_FOUND;
2377 }
2378 free_tlist(origline);
2379 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002380
H. Peter Anvine2c80182005-01-15 22:15:51 +00002381 case PP_ARG:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002382 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002383 /* TASM like ARG directive to define arguments to functions, in
2384 * the following form:
2385 *
2386 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2387 */
2388 offset = ArgOffset;
2389 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002390 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002391 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002392
H. Peter Anvine2c80182005-01-15 22:15:51 +00002393 /* Find the argument name */
2394 tline = tline->next;
2395 if (tline && tline->type == TOK_WHITESPACE)
2396 tline = tline->next;
2397 if (!tline || tline->type != TOK_ID) {
2398 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2399 free_tlist(origline);
2400 return DIRECTIVE_FOUND;
2401 }
2402 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002403
H. Peter Anvine2c80182005-01-15 22:15:51 +00002404 /* Find the argument size type */
2405 tline = tline->next;
2406 if (!tline || tline->type != TOK_OTHER
2407 || tline->text[0] != ':') {
2408 error(ERR_NONFATAL,
2409 "Syntax error processing `%%arg' directive");
2410 free_tlist(origline);
2411 return DIRECTIVE_FOUND;
2412 }
2413 tline = tline->next;
2414 if (!tline || tline->type != TOK_ID) {
2415 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2416 free_tlist(origline);
2417 return DIRECTIVE_FOUND;
2418 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002419
H. Peter Anvine2c80182005-01-15 22:15:51 +00002420 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002421 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002422 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002423 size = parse_size(tt->text);
2424 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002425 error(ERR_NONFATAL,
2426 "Invalid size type for `%%arg' missing directive");
2427 free_tlist(tt);
2428 free_tlist(origline);
2429 return DIRECTIVE_FOUND;
2430 }
2431 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002432
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002433 /* Round up to even stack slots */
2434 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002435
H. Peter Anvine2c80182005-01-15 22:15:51 +00002436 /* Now define the macro for the argument */
2437 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2438 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002439 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002440 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002441
H. Peter Anvine2c80182005-01-15 22:15:51 +00002442 /* Move to the next argument in the list */
2443 tline = tline->next;
2444 if (tline && tline->type == TOK_WHITESPACE)
2445 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002446 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002447 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002448 free_tlist(origline);
2449 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002450
H. Peter Anvine2c80182005-01-15 22:15:51 +00002451 case PP_LOCAL:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002452 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002453 /* TASM like LOCAL directive to define local variables for a
2454 * function, in the following form:
2455 *
2456 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2457 *
2458 * The '= LocalSize' at the end is ignored by NASM, but is
2459 * required by TASM to define the local parameter size (and used
2460 * by the TASM macro package).
2461 */
2462 offset = LocalOffset;
2463 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002464 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002465 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002466
H. Peter Anvine2c80182005-01-15 22:15:51 +00002467 /* Find the argument name */
2468 tline = tline->next;
2469 if (tline && tline->type == TOK_WHITESPACE)
2470 tline = tline->next;
2471 if (!tline || tline->type != TOK_ID) {
2472 error(ERR_NONFATAL,
2473 "`%%local' missing argument parameter");
2474 free_tlist(origline);
2475 return DIRECTIVE_FOUND;
2476 }
2477 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002478
H. Peter Anvine2c80182005-01-15 22:15:51 +00002479 /* Find the argument size type */
2480 tline = tline->next;
2481 if (!tline || tline->type != TOK_OTHER
2482 || tline->text[0] != ':') {
2483 error(ERR_NONFATAL,
2484 "Syntax error processing `%%local' directive");
2485 free_tlist(origline);
2486 return DIRECTIVE_FOUND;
2487 }
2488 tline = tline->next;
2489 if (!tline || tline->type != TOK_ID) {
2490 error(ERR_NONFATAL,
2491 "`%%local' missing size type parameter");
2492 free_tlist(origline);
2493 return DIRECTIVE_FOUND;
2494 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002495
H. Peter Anvine2c80182005-01-15 22:15:51 +00002496 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002497 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002498 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002499 size = parse_size(tt->text);
2500 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002501 error(ERR_NONFATAL,
2502 "Invalid size type for `%%local' missing directive");
2503 free_tlist(tt);
2504 free_tlist(origline);
2505 return DIRECTIVE_FOUND;
2506 }
2507 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002508
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002509 /* Round up to even stack slots */
2510 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002511
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002512 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002513
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002514 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002515 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2516 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002517 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002518
H. Peter Anvine2c80182005-01-15 22:15:51 +00002519 /* Now define the assign to setup the enter_c macro correctly */
2520 snprintf(directive, sizeof(directive),
2521 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002522 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002523
H. Peter Anvine2c80182005-01-15 22:15:51 +00002524 /* Move to the next argument in the list */
2525 tline = tline->next;
2526 if (tline && tline->type == TOK_WHITESPACE)
2527 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002528 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002529 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002530 free_tlist(origline);
2531 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002532
H. Peter Anvine2c80182005-01-15 22:15:51 +00002533 case PP_CLEAR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002534 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002535 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002536 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002537 "trailing garbage after `%%clear' ignored");
2538 free_macros();
2539 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002540 free_tlist(origline);
2541 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002542
H. Peter Anvin418ca702008-05-30 10:42:30 -07002543 case PP_DEPEND:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002544 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002545 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002546 skip_white_(t);
2547 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002548 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002549 error(ERR_NONFATAL, "`%%depend' expects a file name");
2550 free_tlist(origline);
2551 return DIRECTIVE_FOUND; /* but we did _something_ */
2552 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002553 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002554 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002555 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002556 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002557 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002558 nasm_unquote_cstr(p, i);
2559 if (dephead && !in_list(*dephead, p)) {
2560 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2561 sl->next = NULL;
2562 strcpy(sl->str, p);
2563 *deptail = sl;
2564 deptail = &sl->next;
2565 }
2566 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002567 return DIRECTIVE_FOUND;
2568
2569 case PP_INCLUDE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002570 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002571 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002572 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002573
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002574 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002575 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002576 error(ERR_NONFATAL, "`%%include' expects a file name");
2577 free_tlist(origline);
2578 return DIRECTIVE_FOUND; /* but we did _something_ */
2579 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002580 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002581 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002582 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002583 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002584 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002585 nasm_unquote_cstr(p, i);
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03002586 inc = nasm_zalloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002587 inc->next = istk;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002588 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002589 if (!inc->fp) {
2590 /* -MG given but file not found */
2591 nasm_free(inc);
2592 } else {
2593 inc->fname = src_set_fname(nasm_strdup(p));
2594 inc->lineno = src_set_linnum(0);
2595 inc->lineinc = 1;
2596 inc->expansion = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002597 istk = inc;
2598 list->uplevel(LIST_INCLUDE);
2599 }
2600 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002601 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002602
H. Peter Anvind2456592008-06-19 15:04:18 -07002603 case PP_USE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002604 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002605 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002606 static macros_t *use_pkg;
2607 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002608
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002609 tline = tline->next;
2610 skip_white_(tline);
2611 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002612
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002613 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002614 tline->type != TOK_INTERNAL_STRING &&
2615 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002616 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002617 free_tlist(origline);
2618 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002619 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002620 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002621 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002622 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002623 if (tline->type == TOK_STRING)
2624 nasm_unquote_cstr(tline->text, i);
2625 use_pkg = nasm_stdmac_find_package(tline->text);
2626 if (!use_pkg)
2627 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2628 else
2629 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002630 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002631 /* Not already included, go ahead and include it */
2632 stdmacpos = use_pkg;
2633 }
2634 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002635 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002636 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002637 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002638 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002639 case PP_POP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002640 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002641 tline = tline->next;
2642 skip_white_(tline);
2643 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002644 if (tline) {
2645 if (!tok_type_(tline, TOK_ID)) {
2646 error(ERR_NONFATAL, "`%s' expects a context identifier",
2647 pp_directives[i]);
2648 free_tlist(origline);
2649 return DIRECTIVE_FOUND; /* but we did _something_ */
2650 }
2651 if (tline->next)
2652 error(ERR_WARNING|ERR_PASS1,
2653 "trailing garbage after `%s' ignored",
2654 pp_directives[i]);
2655 p = nasm_strdup(tline->text);
2656 } else {
2657 p = NULL; /* Anonymous */
2658 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002659
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002660 if (i == PP_PUSH) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002661 ctx = nasm_zalloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002662 ctx->next = cstk;
2663 hash_init(&ctx->localmac, HASH_SMALL);
2664 ctx->name = p;
2665 ctx->number = unique++;
2666 cstk = ctx;
2667 } else {
2668 /* %pop or %repl */
2669 if (!cstk) {
2670 error(ERR_NONFATAL, "`%s': context stack is empty",
2671 pp_directives[i]);
2672 } else if (i == PP_POP) {
2673 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2674 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2675 "expected %s",
2676 cstk->name ? cstk->name : "anonymous", p);
2677 else
2678 ctx_pop();
2679 } else {
2680 /* i == PP_REPL */
2681 nasm_free(cstk->name);
2682 cstk->name = p;
2683 p = NULL;
2684 }
2685 nasm_free(p);
2686 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002687 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002688 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002689 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002690 severity = ERR_FATAL;
2691 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002692 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002693 severity = ERR_NONFATAL;
2694 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002695 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002696 severity = ERR_WARNING|ERR_WARN_USER;
2697 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002698
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002699issue_error:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002700 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002701 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002702 /* Only error out if this is the final pass */
2703 if (pass != 2 && i != PP_FATAL)
2704 return DIRECTIVE_FOUND;
2705
2706 tline->next = expand_smacro(tline->next);
2707 tline = tline->next;
2708 skip_white_(tline);
2709 t = tline ? tline->next : NULL;
2710 skip_white_(t);
2711 if (tok_type_(tline, TOK_STRING) && !t) {
2712 /* The line contains only a quoted string */
2713 p = tline->text;
2714 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2715 error(severity, "%s", p);
2716 } else {
2717 /* Not a quoted string, or more than a quoted string */
2718 p = detoken(tline, false);
2719 error(severity, "%s", p);
2720 nasm_free(p);
2721 }
2722 free_tlist(origline);
2723 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002724 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002725
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002726 CASE_PP_IF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002727 if (defining != NULL) {
2728 if (defining->type == EXP_IF) {
2729 defining->def_depth ++;
2730 }
2731 return NO_DIRECTIVE_FOUND;
2732 }
2733 if ((istk->expansion != NULL) &&
2734 (istk->expansion->emitting == false)) {
2735 j = COND_NEVER;
2736 } else {
2737 j = if_condition(tline->next, i);
2738 tline->next = NULL; /* it got freed */
2739 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
2740 }
2741 ed = new_ExpDef(EXP_IF);
2742 ed->state = j;
2743 ed->nolist = NULL;
2744 ed->def_depth = 0;
2745 ed->cur_depth = 0;
2746 ed->max_depth = 0;
2747 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
2748 ed->prev = defining;
2749 defining = ed;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002750 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002751 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002752
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002753 CASE_PP_ELIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002754 if (defining != NULL) {
2755 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2756 return NO_DIRECTIVE_FOUND;
2757 }
2758 }
2759 if ((defining == NULL) || (defining->type != EXP_IF)) {
2760 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2761 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002762 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002763 case COND_IF_TRUE:
2764 defining->state = COND_DONE;
2765 defining->ignoring = true;
2766 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002767
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002768 case COND_DONE:
2769 case COND_NEVER:
2770 defining->ignoring = true;
2771 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002772
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002773 case COND_ELSE_TRUE:
2774 case COND_ELSE_FALSE:
2775 error_precond(ERR_WARNING|ERR_PASS1,
2776 "`%%elif' after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002777 defining->state = COND_NEVER;
2778 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002779 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002780
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002781 case COND_IF_FALSE:
2782 /*
2783 * IMPORTANT: In the case of %if, we will already have
2784 * called expand_mmac_params(); however, if we're
2785 * processing an %elif we must have been in a
2786 * non-emitting mode, which would have inhibited
2787 * the normal invocation of expand_mmac_params().
2788 * Therefore, we have to do it explicitly here.
2789 */
2790 j = if_condition(expand_mmac_params(tline->next), i);
2791 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002792 defining->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002793 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002794 defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002795 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002796 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002797 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002798 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002799
H. Peter Anvine2c80182005-01-15 22:15:51 +00002800 case PP_ELSE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002801 if (defining != NULL) {
2802 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2803 return NO_DIRECTIVE_FOUND;
2804 }
2805 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002806 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002807 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002808 "trailing garbage after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002809 if ((defining == NULL) || (defining->type != EXP_IF)) {
2810 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2811 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002812 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002813 case COND_IF_TRUE:
2814 case COND_DONE:
2815 defining->state = COND_ELSE_FALSE;
2816 defining->ignoring = true;
2817 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002818
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002819 case COND_NEVER:
2820 defining->ignoring = true;
2821 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002822
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002823 case COND_IF_FALSE:
2824 defining->state = COND_ELSE_TRUE;
2825 defining->ignoring = false;
2826 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002827
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002828 case COND_ELSE_TRUE:
2829 case COND_ELSE_FALSE:
2830 error_precond(ERR_WARNING|ERR_PASS1,
2831 "`%%else' after `%%else' ignored.");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002832 defining->state = COND_NEVER;
2833 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002834 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002835 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002836 free_tlist(origline);
2837 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002838
H. Peter Anvine2c80182005-01-15 22:15:51 +00002839 case PP_ENDIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002840 if (defining != NULL) {
2841 if (defining->type == EXP_IF) {
2842 if (defining->def_depth > 0) {
2843 defining->def_depth --;
2844 return NO_DIRECTIVE_FOUND;
2845 }
2846 } else {
2847 return NO_DIRECTIVE_FOUND;
2848 }
2849 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002850 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002851 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002852 "trailing garbage after `%%endif' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002853 if ((defining == NULL) || (defining->type != EXP_IF)) {
2854 error(ERR_NONFATAL, "`%%endif': no matching `%%if'");
2855 return DIRECTIVE_FOUND;
2856 }
2857 ed = defining;
2858 defining = ed->prev;
2859 ed->prev = expansions;
2860 expansions = ed;
2861 ei = new_ExpInv(EXP_IF, ed);
2862 ei->current = ed->line;
2863 ei->emitting = true;
2864 ei->prev = istk->expansion;
2865 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002866 free_tlist(origline);
2867 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002868
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002869 case PP_RMACRO:
2870 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002871 case PP_MACRO:
2872 case PP_IMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002873 if (defining != NULL) {
2874 if (defining->type == EXP_MMACRO) {
2875 defining->def_depth ++;
2876 }
2877 return NO_DIRECTIVE_FOUND;
2878 }
2879 ed = new_ExpDef(EXP_MMACRO);
2880 ed->max_depth =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002881 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002882 ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002883 if (!parse_mmacro_spec(tline, ed, pp_directives[i])) {
2884 nasm_free(ed);
2885 ed = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002886 return DIRECTIVE_FOUND;
2887 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002888 ed->def_depth = 0;
2889 ed->cur_depth = 0;
2890 ed->max_depth = (ed->max_depth + 1);
2891 ed->ignoring = false;
2892 ed->prev = defining;
2893 defining = ed;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002894
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002895 eed = (ExpDef *) hash_findix(&expdefs, ed->name);
2896 while (eed) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002897 if (!strcmp(eed->name, ed->name) &&
2898 (eed->nparam_min <= ed->nparam_max || ed->plus) &&
2899 (ed->nparam_min <= eed->nparam_max || eed->plus)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002900 error(ERR_WARNING|ERR_PASS1,
2901 "redefining multi-line macro `%s'", ed->name);
2902 return DIRECTIVE_FOUND;
2903 }
2904 eed = eed->next;
2905 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002906 free_tlist(origline);
2907 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002908
H. Peter Anvine2c80182005-01-15 22:15:51 +00002909 case PP_ENDM:
2910 case PP_ENDMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002911 if (defining != NULL) {
2912 if (defining->type == EXP_MMACRO) {
2913 if (defining->def_depth > 0) {
2914 defining->def_depth --;
2915 return NO_DIRECTIVE_FOUND;
2916 }
2917 } else {
2918 return NO_DIRECTIVE_FOUND;
2919 }
2920 }
2921 if (!(defining) || (defining->type != EXP_MMACRO)) {
2922 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2923 return DIRECTIVE_FOUND;
2924 }
2925 edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name);
2926 defining->next = *edhead;
2927 *edhead = defining;
2928 ed = defining;
2929 defining = ed->prev;
2930 ed->prev = expansions;
2931 expansions = ed;
2932 ed = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002933 free_tlist(origline);
2934 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002935
H. Peter Anvin89cee572009-07-15 09:16:54 -04002936 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002937 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2938 /*
2939 * We must search along istk->expansion until we hit a
2940 * macro invocation. Then we disable the emitting state(s)
2941 * between exitmacro and endmacro.
2942 */
2943 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
2944 if(ei->type == EXP_MMACRO) {
2945 break;
2946 }
2947 }
2948
2949 if (ei != NULL) {
2950 /*
2951 * Set all invocations leading back to the macro
2952 * invocation to a non-emitting state.
2953 */
2954 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
2955 eei->emitting = false;
2956 }
2957 eei->emitting = false;
2958 } else {
2959 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2960 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002961 free_tlist(origline);
2962 return DIRECTIVE_FOUND;
2963
H. Peter Anvina26433d2008-07-16 14:40:01 -07002964 case PP_UNMACRO:
2965 case PP_UNIMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002966 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002967 {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002968 ExpDef **ed_p;
2969 ExpDef spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002970
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002971 spec.casesense = (i == PP_UNMACRO);
2972 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2973 return DIRECTIVE_FOUND;
2974 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002975 ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL);
2976 while (ed_p && *ed_p) {
2977 ed = *ed_p;
2978 if (ed->casesense == spec.casesense &&
2979 !mstrcmp(ed->name, spec.name, spec.casesense) &&
2980 ed->nparam_min == spec.nparam_min &&
2981 ed->nparam_max == spec.nparam_max &&
2982 ed->plus == spec.plus) {
2983 *ed_p = ed->next;
2984 free_expdef(ed);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002985 } else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002986 ed_p = &ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002987 }
2988 }
2989 free_tlist(origline);
2990 free_tlist(spec.dlist);
2991 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002992 }
2993
H. Peter Anvine2c80182005-01-15 22:15:51 +00002994 case PP_ROTATE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002995 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002996 if (tline->next && tline->next->type == TOK_WHITESPACE)
2997 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002998 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002999 free_tlist(origline);
3000 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
3001 return DIRECTIVE_FOUND;
3002 }
3003 t = expand_smacro(tline->next);
3004 tline->next = NULL;
3005 free_tlist(origline);
3006 tline = t;
3007 tptr = &t;
3008 tokval.t_type = TOKEN_INVALID;
3009 evalresult =
3010 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3011 free_tlist(tline);
3012 if (!evalresult)
3013 return DIRECTIVE_FOUND;
3014 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003015 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003016 "trailing garbage after expression ignored");
3017 if (!is_simple(evalresult)) {
3018 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
3019 return DIRECTIVE_FOUND;
3020 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003021 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3022 if (ei->type == EXP_MMACRO) {
3023 break;
3024 }
3025 }
3026 if (ei == NULL) {
3027 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
3028 } else if (ei->nparam == 0) {
3029 error(ERR_NONFATAL,
3030 "`%%rotate' invoked within macro without parameters");
3031 } else {
3032 int rotate = ei->rotate + reloc_value(evalresult);
3033
3034 rotate %= (int)ei->nparam;
3035 if (rotate < 0)
3036 rotate += ei->nparam;
3037 ei->rotate = rotate;
3038 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003039 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003040
H. Peter Anvine2c80182005-01-15 22:15:51 +00003041 case PP_REP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003042 if (defining != NULL) {
3043 if (defining->type == EXP_REP) {
3044 defining->def_depth ++;
3045 }
3046 return NO_DIRECTIVE_FOUND;
3047 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003048 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003049 do {
3050 tline = tline->next;
3051 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003052
H. Peter Anvine2c80182005-01-15 22:15:51 +00003053 if (tok_type_(tline, TOK_ID) &&
3054 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003055 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003056 do {
3057 tline = tline->next;
3058 } while (tok_type_(tline, TOK_WHITESPACE));
3059 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003060
H. Peter Anvine2c80182005-01-15 22:15:51 +00003061 if (tline) {
3062 t = expand_smacro(tline);
3063 tptr = &t;
3064 tokval.t_type = TOKEN_INVALID;
3065 evalresult =
3066 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3067 if (!evalresult) {
3068 free_tlist(origline);
3069 return DIRECTIVE_FOUND;
3070 }
3071 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003072 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003073 "trailing garbage after expression ignored");
3074 if (!is_simple(evalresult)) {
3075 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
3076 return DIRECTIVE_FOUND;
3077 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003078 count = reloc_value(evalresult);
3079 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04003080 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003081 count = 0;
3082 } else
3083 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003084 } else {
3085 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003086 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003087 }
3088 free_tlist(origline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003089 ed = new_ExpDef(EXP_REP);
3090 ed->nolist = nolist;
3091 ed->def_depth = 0;
3092 ed->cur_depth = 1;
3093 ed->max_depth = (count - 1);
3094 ed->ignoring = false;
3095 ed->prev = defining;
3096 defining = ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003097 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003098
H. Peter Anvine2c80182005-01-15 22:15:51 +00003099 case PP_ENDREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003100 if (defining != NULL) {
3101 if (defining->type == EXP_REP) {
3102 if (defining->def_depth > 0) {
3103 defining->def_depth --;
3104 return NO_DIRECTIVE_FOUND;
3105 }
3106 } else {
3107 return NO_DIRECTIVE_FOUND;
3108 }
3109 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003110 if ((defining == NULL) || (defining->type != EXP_REP)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003111 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
3112 return DIRECTIVE_FOUND;
3113 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003114
H. Peter Anvine2c80182005-01-15 22:15:51 +00003115 /*
3116 * Now we have a "macro" defined - although it has no name
3117 * and we won't be entering it in the hash tables - we must
3118 * push a macro-end marker for it on to istk->expansion.
3119 * After that, it will take care of propagating itself (a
3120 * macro-end marker line for a macro which is really a %rep
3121 * block will cause the macro to be re-expanded, complete
3122 * with another macro-end marker to ensure the process
3123 * continues) until the whole expansion is forcibly removed
3124 * from istk->expansion by a %exitrep.
3125 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003126 ed = defining;
3127 defining = ed->prev;
3128 ed->prev = expansions;
3129 expansions = ed;
3130 ei = new_ExpInv(EXP_REP, ed);
3131 ei->current = ed->line;
3132 ei->emitting = ((ed->max_depth > 0) ? true : false);
3133 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3134 ei->prev = istk->expansion;
3135 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003136 free_tlist(origline);
3137 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003138
H. Peter Anvine2c80182005-01-15 22:15:51 +00003139 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003140 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3141 /*
3142 * We must search along istk->expansion until we hit a
3143 * rep invocation. Then we disable the emitting state(s)
3144 * between exitrep and endrep.
3145 */
3146 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3147 if (ei->type == EXP_REP) {
3148 break;
3149 }
3150 }
3151
3152 if (ei != NULL) {
3153 /*
3154 * Set all invocations leading back to the rep
3155 * invocation to a non-emitting state.
3156 */
3157 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3158 eei->emitting = false;
3159 }
3160 eei->emitting = false;
3161 eei->current = NULL;
3162 eei->def->cur_depth = eei->def->max_depth;
3163 } else {
3164 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3165 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003166 free_tlist(origline);
3167 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003168
H. Peter Anvine2c80182005-01-15 22:15:51 +00003169 case PP_XDEFINE:
3170 case PP_IXDEFINE:
3171 case PP_DEFINE:
3172 case PP_IDEFINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003173 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003174 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003175
H. Peter Anvine2c80182005-01-15 22:15:51 +00003176 tline = tline->next;
3177 skip_white_(tline);
3178 tline = expand_id(tline);
3179 if (!tline || (tline->type != TOK_ID &&
3180 (tline->type != TOK_PREPROC_ID ||
3181 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003182 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003183 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003184 free_tlist(origline);
3185 return DIRECTIVE_FOUND;
3186 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003187
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003188 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003189 last = tline;
3190 param_start = tline = tline->next;
3191 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003192
H. Peter Anvine2c80182005-01-15 22:15:51 +00003193 /* Expand the macro definition now for %xdefine and %ixdefine */
3194 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3195 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003196
H. Peter Anvine2c80182005-01-15 22:15:51 +00003197 if (tok_is_(tline, "(")) {
3198 /*
3199 * This macro has parameters.
3200 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003201
H. Peter Anvine2c80182005-01-15 22:15:51 +00003202 tline = tline->next;
3203 while (1) {
3204 skip_white_(tline);
3205 if (!tline) {
3206 error(ERR_NONFATAL, "parameter identifier expected");
3207 free_tlist(origline);
3208 return DIRECTIVE_FOUND;
3209 }
3210 if (tline->type != TOK_ID) {
3211 error(ERR_NONFATAL,
3212 "`%s': parameter identifier expected",
3213 tline->text);
3214 free_tlist(origline);
3215 return DIRECTIVE_FOUND;
3216 }
3217 tline->type = TOK_SMAC_PARAM + nparam++;
3218 tline = tline->next;
3219 skip_white_(tline);
3220 if (tok_is_(tline, ",")) {
3221 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003222 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003223 if (!tok_is_(tline, ")")) {
3224 error(ERR_NONFATAL,
3225 "`)' expected to terminate macro template");
3226 free_tlist(origline);
3227 return DIRECTIVE_FOUND;
3228 }
3229 break;
3230 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003231 }
3232 last = tline;
3233 tline = tline->next;
3234 }
3235 if (tok_type_(tline, TOK_WHITESPACE))
3236 last = tline, tline = tline->next;
3237 macro_start = NULL;
3238 last->next = NULL;
3239 t = tline;
3240 while (t) {
3241 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003242 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003243 if (tt->type >= TOK_SMAC_PARAM &&
3244 !strcmp(tt->text, t->text))
3245 t->type = tt->type;
3246 }
3247 tt = t->next;
3248 t->next = macro_start;
3249 macro_start = t;
3250 t = tt;
3251 }
3252 /*
3253 * Good. We now have a macro name, a parameter count, and a
3254 * token list (in reverse order) for an expansion. We ought
3255 * to be OK just to create an SMacro, store it, and let
3256 * free_tlist have the rest of the line (which we have
3257 * carefully re-terminated after chopping off the expansion
3258 * from the end).
3259 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003260 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003261 free_tlist(origline);
3262 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003263
H. Peter Anvine2c80182005-01-15 22:15:51 +00003264 case PP_UNDEF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003265 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003266 tline = tline->next;
3267 skip_white_(tline);
3268 tline = expand_id(tline);
3269 if (!tline || (tline->type != TOK_ID &&
3270 (tline->type != TOK_PREPROC_ID ||
3271 tline->text[1] != '$'))) {
3272 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3273 free_tlist(origline);
3274 return DIRECTIVE_FOUND;
3275 }
3276 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003277 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003278 "trailing garbage after macro name ignored");
3279 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003280
H. Peter Anvine2c80182005-01-15 22:15:51 +00003281 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003282 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003283 undef_smacro(ctx, mname);
3284 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003285 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003286
H. Peter Anvin9e200162008-06-04 17:23:14 -07003287 case PP_DEFSTR:
3288 case PP_IDEFSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003289 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003290 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003291
3292 tline = tline->next;
3293 skip_white_(tline);
3294 tline = expand_id(tline);
3295 if (!tline || (tline->type != TOK_ID &&
3296 (tline->type != TOK_PREPROC_ID ||
3297 tline->text[1] != '$'))) {
3298 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003299 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003300 free_tlist(origline);
3301 return DIRECTIVE_FOUND;
3302 }
3303
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003304 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003305 last = tline;
3306 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003307 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003308
3309 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003310 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003311
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003312 p = detoken(tline, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003313 macro_start = nasm_malloc(sizeof(*macro_start));
3314 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003315 macro_start->text = nasm_quote(p, strlen(p));
3316 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003317 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003318 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003319
3320 /*
3321 * We now have a macro name, an implicit parameter count of
3322 * zero, and a string token to use as an expansion. Create
3323 * and store an SMacro.
3324 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003325 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003326 free_tlist(origline);
3327 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003328
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003329 case PP_DEFTOK:
3330 case PP_IDEFTOK:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003331 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003332 casesense = (i == PP_DEFTOK);
3333
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003334 tline = tline->next;
3335 skip_white_(tline);
3336 tline = expand_id(tline);
3337 if (!tline || (tline->type != TOK_ID &&
3338 (tline->type != TOK_PREPROC_ID ||
3339 tline->text[1] != '$'))) {
3340 error(ERR_NONFATAL,
3341 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003342 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003343 free_tlist(origline);
3344 return DIRECTIVE_FOUND;
3345 }
3346 ctx = get_ctx(tline->text, &mname, false);
3347 last = tline;
3348 tline = expand_smacro(tline->next);
3349 last->next = NULL;
3350
3351 t = tline;
3352 while (tok_type_(t, TOK_WHITESPACE))
3353 t = t->next;
3354 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003355 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003356 error(ERR_NONFATAL,
3357 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003358 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003359 free_tlist(tline);
3360 free_tlist(origline);
3361 return DIRECTIVE_FOUND;
3362 }
3363
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003364 /*
3365 * Convert the string to a token stream. Note that smacros
3366 * are stored with the token stream reversed, so we have to
3367 * reverse the output of tokenize().
3368 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003369 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003370 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003371
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003372 /*
3373 * We now have a macro name, an implicit parameter count of
3374 * zero, and a numeric token to use as an expansion. Create
3375 * and store an SMacro.
3376 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003377 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003378 free_tlist(tline);
3379 free_tlist(origline);
3380 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003381
H. Peter Anvin418ca702008-05-30 10:42:30 -07003382 case PP_PATHSEARCH:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003383 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003384 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003385 FILE *fp;
3386 StrList *xsl = NULL;
3387 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003388
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003389 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003390
3391 tline = tline->next;
3392 skip_white_(tline);
3393 tline = expand_id(tline);
3394 if (!tline || (tline->type != TOK_ID &&
3395 (tline->type != TOK_PREPROC_ID ||
3396 tline->text[1] != '$'))) {
3397 error(ERR_NONFATAL,
3398 "`%%pathsearch' expects a macro identifier as first parameter");
3399 free_tlist(origline);
3400 return DIRECTIVE_FOUND;
3401 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003402 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003403 last = tline;
3404 tline = expand_smacro(tline->next);
3405 last->next = NULL;
3406
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003407 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003408 while (tok_type_(t, TOK_WHITESPACE))
3409 t = t->next;
3410
3411 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003412 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003413 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003414 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003415 free_tlist(origline);
3416 return DIRECTIVE_FOUND; /* but we did _something_ */
3417 }
3418 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003419 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003420 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003421 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003422 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003423 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003424
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003425 fp = inc_fopen(p, &xsl, &xst, true);
3426 if (fp) {
3427 p = xsl->str;
3428 fclose(fp); /* Don't actually care about the file */
3429 }
H. Peter Anvin418ca702008-05-30 10:42:30 -07003430 macro_start = nasm_malloc(sizeof(*macro_start));
3431 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003432 macro_start->text = nasm_quote(p, strlen(p));
3433 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003434 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003435 if (xsl)
3436 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003437
3438 /*
3439 * We now have a macro name, an implicit parameter count of
3440 * zero, and a string token to use as an expansion. Create
3441 * and store an SMacro.
3442 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003443 define_smacro(ctx, mname, casesense, 0, macro_start);
3444 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003445 free_tlist(origline);
3446 return DIRECTIVE_FOUND;
3447 }
3448
H. Peter Anvine2c80182005-01-15 22:15:51 +00003449 case PP_STRLEN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003450 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003451 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003452
H. Peter Anvine2c80182005-01-15 22:15:51 +00003453 tline = tline->next;
3454 skip_white_(tline);
3455 tline = expand_id(tline);
3456 if (!tline || (tline->type != TOK_ID &&
3457 (tline->type != TOK_PREPROC_ID ||
3458 tline->text[1] != '$'))) {
3459 error(ERR_NONFATAL,
3460 "`%%strlen' expects a macro identifier as first parameter");
3461 free_tlist(origline);
3462 return DIRECTIVE_FOUND;
3463 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003464 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003465 last = tline;
3466 tline = expand_smacro(tline->next);
3467 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003468
H. Peter Anvine2c80182005-01-15 22:15:51 +00003469 t = tline;
3470 while (tok_type_(t, TOK_WHITESPACE))
3471 t = t->next;
3472 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003473 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003474 error(ERR_NONFATAL,
3475 "`%%strlen` requires string as second parameter");
3476 free_tlist(tline);
3477 free_tlist(origline);
3478 return DIRECTIVE_FOUND;
3479 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003480
H. Peter Anvine2c80182005-01-15 22:15:51 +00003481 macro_start = nasm_malloc(sizeof(*macro_start));
3482 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003483 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003484 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003485
H. Peter Anvine2c80182005-01-15 22:15:51 +00003486 /*
3487 * We now have a macro name, an implicit parameter count of
3488 * zero, and a numeric token to use as an expansion. Create
3489 * and store an SMacro.
3490 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003491 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003492 free_tlist(tline);
3493 free_tlist(origline);
3494 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003495
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003496 case PP_STRCAT:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003497 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003498 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003499
3500 tline = tline->next;
3501 skip_white_(tline);
3502 tline = expand_id(tline);
3503 if (!tline || (tline->type != TOK_ID &&
3504 (tline->type != TOK_PREPROC_ID ||
3505 tline->text[1] != '$'))) {
3506 error(ERR_NONFATAL,
3507 "`%%strcat' expects a macro identifier as first parameter");
3508 free_tlist(origline);
3509 return DIRECTIVE_FOUND;
3510 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003511 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003512 last = tline;
3513 tline = expand_smacro(tline->next);
3514 last->next = NULL;
3515
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003516 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003517 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003518 switch (t->type) {
3519 case TOK_WHITESPACE:
3520 break;
3521 case TOK_STRING:
3522 len += t->a.len = nasm_unquote(t->text, NULL);
3523 break;
3524 case TOK_OTHER:
3525 if (!strcmp(t->text, ",")) /* permit comma separators */
3526 break;
3527 /* else fall through */
3528 default:
3529 error(ERR_NONFATAL,
3530 "non-string passed to `%%strcat' (%d)", t->type);
3531 free_tlist(tline);
3532 free_tlist(origline);
3533 return DIRECTIVE_FOUND;
3534 }
3535 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003536
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003537 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003538 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003539 if (t->type == TOK_STRING) {
3540 memcpy(p, t->text, t->a.len);
3541 p += t->a.len;
3542 }
3543 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003544
3545 /*
3546 * We now have a macro name, an implicit parameter count of
3547 * zero, and a numeric token to use as an expansion. Create
3548 * and store an SMacro.
3549 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003550 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3551 macro_start->text = nasm_quote(pp, len);
3552 nasm_free(pp);
3553 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003554 free_tlist(tline);
3555 free_tlist(origline);
3556 return DIRECTIVE_FOUND;
3557
H. Peter Anvine2c80182005-01-15 22:15:51 +00003558 case PP_SUBSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003559 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003560 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003561 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003562 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003563
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003564 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003565
H. Peter Anvine2c80182005-01-15 22:15:51 +00003566 tline = tline->next;
3567 skip_white_(tline);
3568 tline = expand_id(tline);
3569 if (!tline || (tline->type != TOK_ID &&
3570 (tline->type != TOK_PREPROC_ID ||
3571 tline->text[1] != '$'))) {
3572 error(ERR_NONFATAL,
3573 "`%%substr' expects a macro identifier as first parameter");
3574 free_tlist(origline);
3575 return DIRECTIVE_FOUND;
3576 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003577 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003578 last = tline;
3579 tline = expand_smacro(tline->next);
3580 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003581
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003582 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003583 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003584 while (tok_type_(t, TOK_WHITESPACE))
3585 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003586
H. Peter Anvine2c80182005-01-15 22:15:51 +00003587 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003588 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003589 error(ERR_NONFATAL,
3590 "`%%substr` requires string as second parameter");
3591 free_tlist(tline);
3592 free_tlist(origline);
3593 return DIRECTIVE_FOUND;
3594 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003595
H. Peter Anvine2c80182005-01-15 22:15:51 +00003596 tt = t->next;
3597 tptr = &tt;
3598 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003599 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003600 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003601 if (!evalresult) {
3602 free_tlist(tline);
3603 free_tlist(origline);
3604 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003605 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003606 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3607 free_tlist(tline);
3608 free_tlist(origline);
3609 return DIRECTIVE_FOUND;
3610 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003611 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003612
3613 while (tok_type_(tt, TOK_WHITESPACE))
3614 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003615 if (!tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003616 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003617 } else {
3618 tokval.t_type = TOKEN_INVALID;
3619 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3620 pass, error, NULL);
3621 if (!evalresult) {
3622 free_tlist(tline);
3623 free_tlist(origline);
3624 return DIRECTIVE_FOUND;
3625 } else if (!is_simple(evalresult)) {
3626 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3627 free_tlist(tline);
3628 free_tlist(origline);
3629 return DIRECTIVE_FOUND;
3630 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003631 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003632 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003633
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003634 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003635 /* make start and count being in range */
3636 if (start < 0)
3637 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003638 if (count < 0)
3639 count = len + count + 1 - start;
3640 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003641 count = len - start;
3642 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003643 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003644
H. Peter Anvine2c80182005-01-15 22:15:51 +00003645 macro_start = nasm_malloc(sizeof(*macro_start));
3646 macro_start->next = NULL;
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 Anvinf26e0972008-07-01 21:26:27 -07003649 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003650
H. Peter Anvine2c80182005-01-15 22:15:51 +00003651 /*
3652 * We now have a macro name, an implicit parameter count of
3653 * zero, and a numeric token to use as an expansion. Create
3654 * and store an SMacro.
3655 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003656 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003657 free_tlist(tline);
3658 free_tlist(origline);
3659 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003660 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003661
H. Peter Anvine2c80182005-01-15 22:15:51 +00003662 case PP_ASSIGN:
3663 case PP_IASSIGN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003664 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003665 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003666
H. Peter Anvine2c80182005-01-15 22:15:51 +00003667 tline = tline->next;
3668 skip_white_(tline);
3669 tline = expand_id(tline);
3670 if (!tline || (tline->type != TOK_ID &&
3671 (tline->type != TOK_PREPROC_ID ||
3672 tline->text[1] != '$'))) {
3673 error(ERR_NONFATAL,
3674 "`%%%sassign' expects a macro identifier",
3675 (i == PP_IASSIGN ? "i" : ""));
3676 free_tlist(origline);
3677 return DIRECTIVE_FOUND;
3678 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003679 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003680 last = tline;
3681 tline = expand_smacro(tline->next);
3682 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003683
H. Peter Anvine2c80182005-01-15 22:15:51 +00003684 t = tline;
3685 tptr = &t;
3686 tokval.t_type = TOKEN_INVALID;
3687 evalresult =
3688 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3689 free_tlist(tline);
3690 if (!evalresult) {
3691 free_tlist(origline);
3692 return DIRECTIVE_FOUND;
3693 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003694
H. Peter Anvine2c80182005-01-15 22:15:51 +00003695 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003696 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003697 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003698
H. Peter Anvine2c80182005-01-15 22:15:51 +00003699 if (!is_simple(evalresult)) {
3700 error(ERR_NONFATAL,
3701 "non-constant value given to `%%%sassign'",
3702 (i == PP_IASSIGN ? "i" : ""));
3703 free_tlist(origline);
3704 return DIRECTIVE_FOUND;
3705 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003706
H. Peter Anvine2c80182005-01-15 22:15:51 +00003707 macro_start = nasm_malloc(sizeof(*macro_start));
3708 macro_start->next = NULL;
3709 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003710 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003711
H. Peter Anvine2c80182005-01-15 22:15:51 +00003712 /*
3713 * We now have a macro name, an implicit parameter count of
3714 * zero, and a numeric token to use as an expansion. Create
3715 * and store an SMacro.
3716 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003717 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003718 free_tlist(origline);
3719 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003720
H. Peter Anvine2c80182005-01-15 22:15:51 +00003721 case PP_LINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003722 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003723 /*
3724 * Syntax is `%line nnn[+mmm] [filename]'
3725 */
3726 tline = tline->next;
3727 skip_white_(tline);
3728 if (!tok_type_(tline, TOK_NUMBER)) {
3729 error(ERR_NONFATAL, "`%%line' expects line number");
3730 free_tlist(origline);
3731 return DIRECTIVE_FOUND;
3732 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003733 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003734 m = 1;
3735 tline = tline->next;
3736 if (tok_is_(tline, "+")) {
3737 tline = tline->next;
3738 if (!tok_type_(tline, TOK_NUMBER)) {
3739 error(ERR_NONFATAL, "`%%line' expects line increment");
3740 free_tlist(origline);
3741 return DIRECTIVE_FOUND;
3742 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003743 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003744 tline = tline->next;
3745 }
3746 skip_white_(tline);
3747 src_set_linnum(k);
3748 istk->lineinc = m;
3749 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003750 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003751 }
3752 free_tlist(origline);
3753 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003754
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003755 case PP_WHILE:
3756 if (defining != NULL) {
3757 if (defining->type == EXP_WHILE) {
3758 defining->def_depth ++;
3759 }
3760 return NO_DIRECTIVE_FOUND;
3761 }
3762 l = NULL;
3763 if ((istk->expansion != NULL) &&
3764 (istk->expansion->emitting == false)) {
3765 j = COND_NEVER;
3766 } else {
3767 l = new_Line();
3768 l->first = copy_Token(tline->next);
3769 j = if_condition(tline->next, i);
3770 tline->next = NULL; /* it got freed */
3771 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
3772 }
3773 ed = new_ExpDef(EXP_WHILE);
3774 ed->state = j;
3775 ed->cur_depth = 1;
3776 ed->max_depth = DEADMAN_LIMIT;
3777 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
3778 if (ed->ignoring == false) {
3779 ed->line = l;
3780 ed->last = l;
3781 } else if (l != NULL) {
3782 delete_Token(l->first);
3783 nasm_free(l);
3784 l = NULL;
3785 }
3786 ed->prev = defining;
3787 defining = ed;
3788 free_tlist(origline);
3789 return DIRECTIVE_FOUND;
3790
3791 case PP_ENDWHILE:
3792 if (defining != NULL) {
3793 if (defining->type == EXP_WHILE) {
3794 if (defining->def_depth > 0) {
3795 defining->def_depth --;
3796 return NO_DIRECTIVE_FOUND;
3797 }
3798 } else {
3799 return NO_DIRECTIVE_FOUND;
3800 }
3801 }
3802 if (tline->next != NULL) {
3803 error_precond(ERR_WARNING|ERR_PASS1,
3804 "trailing garbage after `%%endwhile' ignored");
3805 }
3806 if ((defining == NULL) || (defining->type != EXP_WHILE)) {
3807 error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'");
3808 return DIRECTIVE_FOUND;
3809 }
3810 ed = defining;
3811 defining = ed->prev;
3812 if (ed->ignoring == false) {
3813 ed->prev = expansions;
3814 expansions = ed;
3815 ei = new_ExpInv(EXP_WHILE, ed);
3816 ei->current = ed->line->next;
3817 ei->emitting = true;
3818 ei->prev = istk->expansion;
3819 istk->expansion = ei;
3820 } else {
3821 nasm_free(ed);
3822 }
3823 free_tlist(origline);
3824 return DIRECTIVE_FOUND;
3825
3826 case PP_EXITWHILE:
3827 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3828 /*
3829 * We must search along istk->expansion until we hit a
3830 * while invocation. Then we disable the emitting state(s)
3831 * between exitwhile and endwhile.
3832 */
3833 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3834 if (ei->type == EXP_WHILE) {
3835 break;
3836 }
3837 }
3838
3839 if (ei != NULL) {
3840 /*
3841 * Set all invocations leading back to the while
3842 * invocation to a non-emitting state.
3843 */
3844 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3845 eei->emitting = false;
3846 }
3847 eei->emitting = false;
3848 eei->current = NULL;
3849 eei->def->cur_depth = eei->def->max_depth;
3850 } else {
3851 error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block");
3852 }
3853 free_tlist(origline);
3854 return DIRECTIVE_FOUND;
3855
3856 case PP_COMMENT:
3857 if (defining != NULL) {
3858 if (defining->type == EXP_COMMENT) {
3859 defining->def_depth ++;
3860 }
3861 return NO_DIRECTIVE_FOUND;
3862 }
3863 ed = new_ExpDef(EXP_COMMENT);
3864 ed->ignoring = true;
3865 ed->prev = defining;
3866 defining = ed;
3867 free_tlist(origline);
3868 return DIRECTIVE_FOUND;
3869
3870 case PP_ENDCOMMENT:
3871 if (defining != NULL) {
3872 if (defining->type == EXP_COMMENT) {
3873 if (defining->def_depth > 0) {
3874 defining->def_depth --;
3875 return NO_DIRECTIVE_FOUND;
3876 }
3877 } else {
3878 return NO_DIRECTIVE_FOUND;
3879 }
3880 }
3881 if ((defining == NULL) || (defining->type != EXP_COMMENT)) {
3882 error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'");
3883 return DIRECTIVE_FOUND;
3884 }
3885 ed = defining;
3886 defining = ed->prev;
3887 nasm_free(ed);
3888 free_tlist(origline);
3889 return DIRECTIVE_FOUND;
3890
3891 case PP_FINAL:
3892 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3893 if (in_final != false) {
3894 error(ERR_FATAL, "`%%final' cannot be used recursively");
3895 }
3896 tline = tline->next;
3897 skip_white_(tline);
3898 if (tline == NULL) {
3899 error(ERR_NONFATAL, "`%%final' expects at least one parameter");
3900 } else {
3901 l = new_Line();
3902 l->first = copy_Token(tline);
3903 l->next = finals;
3904 finals = l;
3905 }
3906 free_tlist(origline);
3907 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003908
H. Peter Anvine2c80182005-01-15 22:15:51 +00003909 default:
3910 error(ERR_FATAL,
3911 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003912 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003913 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003914 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003915}
3916
3917/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003918 * Ensure that a macro parameter contains a condition code and
3919 * nothing else. Return the condition code index if so, or -1
3920 * otherwise.
3921 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003922static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003923{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003924 Token *tt;
3925 int i, j, k, m;
3926
H. Peter Anvin25a99342007-09-22 17:45:45 -07003927 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003928 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003929
H. Peter Anvineba20a72002-04-30 20:53:55 +00003930 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003931 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003932 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003933 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003934 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003935 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003936 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003937
3938 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003939 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003940 while (j - i > 1) {
3941 k = (j + i) / 2;
3942 m = nasm_stricmp(t->text, conditions[k]);
3943 if (m == 0) {
3944 i = k;
3945 j = -2;
3946 break;
3947 } else if (m < 0) {
3948 j = k;
3949 } else
3950 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003951 }
3952 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003953 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003954 return i;
3955}
3956
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003957static bool paste_tokens(Token **head, const struct tokseq_match *m,
3958 int mnum, bool handle_paste_tokens)
H. Peter Anvind784a082009-04-20 14:01:18 -07003959{
3960 Token **tail, *t, *tt;
3961 Token **paste_head;
3962 bool did_paste = false;
3963 char *tmp;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003964 int i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003965
3966 /* Now handle token pasting... */
3967 paste_head = NULL;
3968 tail = head;
3969 while ((t = *tail) && (tt = t->next)) {
3970 switch (t->type) {
3971 case TOK_WHITESPACE:
3972 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003973 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003974 t->next = delete_Token(tt);
3975 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003976 /* Do not advance paste_head here */
3977 tail = &t->next;
3978 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003979 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003980 case TOK_PASTE: /* %+ */
3981 if (handle_paste_tokens) {
3982 /* Zap %+ and whitespace tokens to the right */
3983 while (t && (t->type == TOK_WHITESPACE ||
3984 t->type == TOK_PASTE))
3985 t = *tail = delete_Token(t);
3986 if (!paste_head || !t)
3987 break; /* Nothing to paste with */
3988 tail = paste_head;
3989 t = *tail;
3990 tt = t->next;
3991 while (tok_type_(tt, TOK_WHITESPACE))
3992 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003993 if (tt) {
3994 tmp = nasm_strcat(t->text, tt->text);
3995 delete_Token(t);
3996 tt = delete_Token(tt);
3997 t = *tail = tokenize(tmp);
3998 nasm_free(tmp);
3999 while (t->next) {
4000 tail = &t->next;
4001 t = t->next;
4002 }
4003 t->next = tt; /* Attach the remaining token chain */
4004 did_paste = true;
4005 }
4006 paste_head = tail;
4007 tail = &t->next;
4008 break;
4009 }
4010 /* else fall through */
4011 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004012 /*
4013 * Concatenation of tokens might look nontrivial
4014 * but in real it's pretty simple -- the caller
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004015 * prepares the masks of token types to be concatenated
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004016 * and we simply find matched sequences and slip
4017 * them together
4018 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004019 for (i = 0; i < mnum; i++) {
4020 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
4021 size_t len = 0;
4022 char *tmp, *p;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004023
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004024 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004025 len += strlen(tt->text);
4026 tt = tt->next;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004027 }
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004028
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004029 /*
4030 * Now tt points to the first token after
4031 * the potential paste area...
4032 */
4033 if (tt != t->next) {
4034 /* We have at least two tokens... */
4035 len += strlen(t->text);
4036 p = tmp = nasm_malloc(len+1);
4037 while (t != tt) {
4038 strcpy(p, t->text);
4039 p = strchr(p, '\0');
4040 t = delete_Token(t);
4041 }
4042 t = *tail = tokenize(tmp);
4043 nasm_free(tmp);
4044 while (t->next) {
4045 tail = &t->next;
4046 t = t->next;
4047 }
4048 t->next = tt; /* Attach the remaining token chain */
4049 did_paste = true;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004050 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004051 paste_head = tail;
4052 tail = &t->next;
4053 break;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004054 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004055 }
4056 if (i >= mnum) { /* no match */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004057 tail = &t->next;
4058 if (!tok_type_(t->next, TOK_WHITESPACE))
4059 paste_head = tail;
4060 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004061 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004062 }
4063 }
4064 return did_paste;
4065}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004066
4067/*
4068 * expands to a list of tokens from %{x:y}
4069 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004070static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004071{
4072 Token *t = tline, **tt, *tm, *head;
4073 char *pos;
4074 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004075
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004076 pos = strchr(tline->text, ':');
4077 nasm_assert(pos);
4078
4079 lst = atoi(pos + 1);
4080 fst = atoi(tline->text + 1);
4081
4082 /*
4083 * only macros params are accounted so
4084 * if someone passes %0 -- we reject such
4085 * value(s)
4086 */
4087 if (lst == 0 || fst == 0)
4088 goto err;
4089
4090 /* the values should be sane */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004091 if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) ||
4092 (lst > (int)ei->nparam || lst < (-(int)ei->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004093 goto err;
4094
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004095 fst = fst < 0 ? fst + (int)ei->nparam + 1: fst;
4096 lst = lst < 0 ? lst + (int)ei->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004097
4098 /* counted from zero */
4099 fst--, lst--;
4100
4101 /*
4102 * it will be at least one token
4103 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004104 tm = ei->params[(fst + ei->rotate) % ei->nparam];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004105 t = new_Token(NULL, tm->type, tm->text, 0);
4106 head = t, tt = &t->next;
4107 if (fst < lst) {
4108 for (i = fst + 1; i <= lst; i++) {
4109 t = new_Token(NULL, TOK_OTHER, ",", 0);
4110 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004111 j = (i + ei->rotate) % ei->nparam;
4112 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004113 t = new_Token(NULL, tm->type, tm->text, 0);
4114 *tt = t, tt = &t->next;
4115 }
4116 } else {
4117 for (i = fst - 1; i >= lst; i--) {
4118 t = new_Token(NULL, TOK_OTHER, ",", 0);
4119 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004120 j = (i + ei->rotate) % ei->nparam;
4121 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004122 t = new_Token(NULL, tm->type, tm->text, 0);
4123 *tt = t, tt = &t->next;
4124 }
4125 }
4126
4127 *last = tt;
4128 return head;
4129
4130err:
4131 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
4132 &tline->text[1]);
4133 return tline;
4134}
4135
H. Peter Anvin76690a12002-04-30 20:52:49 +00004136/*
4137 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004138 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004139 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004140 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004141static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004142{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004143 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004144 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004145 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004146
4147 tail = &thead;
4148 thead = NULL;
4149
H. Peter Anvine2c80182005-01-15 22:15:51 +00004150 while (tline) {
4151 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004152 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4153 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4154 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004155 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004156 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004157 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004158 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004159 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004160 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004161
H. Peter Anvine2c80182005-01-15 22:15:51 +00004162 t = tline;
4163 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004164
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004165 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
4166 if (ei->type == EXP_MMACRO) {
4167 break;
4168 }
4169 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004170 if (ei == NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004171 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004172 } else {
4173 pos = strchr(t->text, ':');
4174 if (!pos) {
4175 switch (t->text[1]) {
4176 /*
4177 * We have to make a substitution of one of the
4178 * forms %1, %-1, %+1, %%foo, %0.
4179 */
4180 case '0':
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004181 if ((strlen(t->text) > 2) && (t->text[2] == '0')) {
4182 type = TOK_ID;
4183 text = nasm_strdup(ei->label_text);
4184 } else {
4185 type = TOK_NUMBER;
4186 snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam);
4187 text = nasm_strdup(tmpbuf);
4188 }
4189 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004190 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004191 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004192 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004193 ei->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004194 text = nasm_strcat(tmpbuf, t->text + 2);
4195 break;
4196 case '-':
4197 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004198 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004199 tt = NULL;
4200 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004201 if (ei->nparam > 1)
4202 n = (n + ei->rotate) % ei->nparam;
4203 tt = ei->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004204 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004205 cc = find_cc(tt);
4206 if (cc == -1) {
4207 error(ERR_NONFATAL,
4208 "macro parameter %d is not a condition code",
4209 n + 1);
4210 text = NULL;
4211 } else {
4212 type = TOK_ID;
4213 if (inverse_ccs[cc] == -1) {
4214 error(ERR_NONFATAL,
4215 "condition code `%s' is not invertible",
4216 conditions[cc]);
4217 text = NULL;
4218 } else
4219 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4220 }
4221 break;
4222 case '+':
4223 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004224 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004225 tt = NULL;
4226 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004227 if (ei->nparam > 1)
4228 n = (n + ei->rotate) % ei->nparam;
4229 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004230 }
4231 cc = find_cc(tt);
4232 if (cc == -1) {
4233 error(ERR_NONFATAL,
4234 "macro parameter %d is not a condition code",
4235 n + 1);
4236 text = NULL;
4237 } else {
4238 type = TOK_ID;
4239 text = nasm_strdup(conditions[cc]);
4240 }
4241 break;
4242 default:
4243 n = atoi(t->text + 1) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004244 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004245 tt = NULL;
4246 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004247 if (ei->nparam > 1)
4248 n = (n + ei->rotate) % ei->nparam;
4249 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004250 }
4251 if (tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004252 for (i = 0; i < ei->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004253 *tail = new_Token(NULL, tt->type, tt->text, 0);
4254 tail = &(*tail)->next;
4255 tt = tt->next;
4256 }
4257 }
4258 text = NULL; /* we've done it here */
4259 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004260 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004261 } else {
4262 /*
4263 * seems we have a parameters range here
4264 */
4265 Token *head, **last;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004266 head = expand_mmac_params_range(ei, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004267 if (head != t) {
4268 *tail = head;
4269 *last = tline;
4270 tline = head;
4271 text = NULL;
4272 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004273 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004274 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004275 if (!text) {
4276 delete_Token(t);
4277 } else {
4278 *tail = t;
4279 tail = &t->next;
4280 t->type = type;
4281 nasm_free(t->text);
4282 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004283 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004284 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004285 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004286 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004287 } else if (tline->type == TOK_INDIRECT) {
4288 t = tline;
4289 tline = tline->next;
4290 tt = tokenize(t->text);
4291 tt = expand_mmac_params(tt);
4292 tt = expand_smacro(tt);
4293 *tail = tt;
4294 while (tt) {
4295 tt->a.mac = NULL; /* Necessary? */
4296 tail = &tt->next;
4297 tt = tt->next;
4298 }
4299 delete_Token(t);
4300 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004301 } else {
4302 t = *tail = tline;
4303 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004304 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004305 tail = &t->next;
4306 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004307 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004308 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004309
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004310 if (changed) {
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004311 const struct tokseq_match t[] = {
4312 {
4313 PP_CONCAT_MASK(TOK_ID) |
4314 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4315 PP_CONCAT_MASK(TOK_ID) |
4316 PP_CONCAT_MASK(TOK_NUMBER) |
4317 PP_CONCAT_MASK(TOK_FLOAT) |
4318 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4319 },
4320 {
4321 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4322 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4323 }
4324 };
4325 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004326 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004327
H. Peter Anvin76690a12002-04-30 20:52:49 +00004328 return thead;
4329}
4330
4331/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004332 * Expand all single-line macro calls made in the given line.
4333 * Return the expanded version of the line. The original is deemed
4334 * to be destroyed in the process. (In reality we'll just move
4335 * Tokens from input to output a lot of the time, rather than
4336 * actually bothering to destroy and replicate.)
4337 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004338
H. Peter Anvine2c80182005-01-15 22:15:51 +00004339static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004340{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004341 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004342 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004343 Token **params;
4344 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004345 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004346 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004347 Token *org_tline = tline;
4348 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004349 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004350 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004351 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004352
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004353 /*
4354 * Trick: we should avoid changing the start token pointer since it can
4355 * be contained in "next" field of other token. Because of this
4356 * we allocate a copy of first token and work with it; at the end of
4357 * routine we copy it back
4358 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004359 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004360 tline = new_Token(org_tline->next, org_tline->type,
4361 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004362 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004363 nasm_free(org_tline->text);
4364 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004365 }
4366
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004367 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004368
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004369again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004370 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004371 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004372
H. Peter Anvine2c80182005-01-15 22:15:51 +00004373 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004374 if (!--deadman) {
4375 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004376 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004377 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004378
H. Peter Anvine2c80182005-01-15 22:15:51 +00004379 if ((mname = tline->text)) {
4380 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004381 if (tline->type == TOK_ID) {
4382 head = (SMacro *)hash_findix(&smacros, mname);
4383 } else if (tline->type == TOK_PREPROC_ID) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004384 ctx = get_ctx(mname, &mname, false);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004385 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4386 } else
4387 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004388
H. Peter Anvine2c80182005-01-15 22:15:51 +00004389 /*
4390 * We've hit an identifier. As in is_mmacro below, we first
4391 * check whether the identifier is a single-line macro at
4392 * all, then think about checking for parameters if
4393 * necessary.
4394 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004395 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004396 if (!mstrcmp(m->name, mname, m->casesense))
4397 break;
4398 if (m) {
4399 mstart = tline;
4400 params = NULL;
4401 paramsize = NULL;
4402 if (m->nparam == 0) {
4403 /*
4404 * Simple case: the macro is parameterless. Discard the
4405 * one token that the macro call took, and push the
4406 * expansion back on the to-do stack.
4407 */
4408 if (!m->expansion) {
4409 if (!strcmp("__FILE__", m->name)) {
4410 int32_t num = 0;
4411 char *file = NULL;
4412 src_get(&num, &file);
4413 tline->text = nasm_quote(file, strlen(file));
4414 tline->type = TOK_STRING;
4415 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004416 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004417 }
4418 if (!strcmp("__LINE__", m->name)) {
4419 nasm_free(tline->text);
4420 make_tok_num(tline, src_get_linnum());
4421 continue;
4422 }
4423 if (!strcmp("__BITS__", m->name)) {
4424 nasm_free(tline->text);
4425 make_tok_num(tline, globalbits);
4426 continue;
4427 }
4428 tline = delete_Token(tline);
4429 continue;
4430 }
4431 } else {
4432 /*
4433 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004434 * exists and takes parameters. We must find the
4435 * parameters in the call, count them, find the SMacro
4436 * that corresponds to that form of the macro call, and
4437 * substitute for the parameters when we expand. What a
4438 * pain.
4439 */
4440 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004441 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004442 do {
4443 t = tline->next;
4444 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004445 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004446 t->text = NULL;
4447 t = tline->next = delete_Token(t);
4448 }
4449 tline = t;
4450 } while (tok_type_(tline, TOK_WHITESPACE));
4451 if (!tok_is_(tline, "(")) {
4452 /*
4453 * This macro wasn't called with parameters: ignore
4454 * the call. (Behaviour borrowed from gnu cpp.)
4455 */
4456 tline = mstart;
4457 m = NULL;
4458 } else {
4459 int paren = 0;
4460 int white = 0;
4461 brackets = 0;
4462 nparam = 0;
4463 sparam = PARAM_DELTA;
4464 params = nasm_malloc(sparam * sizeof(Token *));
4465 params[0] = tline->next;
4466 paramsize = nasm_malloc(sparam * sizeof(int));
4467 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004468 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004469 /*
4470 * For some unusual expansions
4471 * which concatenates function call
4472 */
4473 t = tline->next;
4474 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004475 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004476 t->text = NULL;
4477 t = tline->next = delete_Token(t);
4478 }
4479 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004480
H. Peter Anvine2c80182005-01-15 22:15:51 +00004481 if (!tline) {
4482 error(ERR_NONFATAL,
4483 "macro call expects terminating `)'");
4484 break;
4485 }
4486 if (tline->type == TOK_WHITESPACE
4487 && brackets <= 0) {
4488 if (paramsize[nparam])
4489 white++;
4490 else
4491 params[nparam] = tline->next;
4492 continue; /* parameter loop */
4493 }
4494 if (tline->type == TOK_OTHER
4495 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004496 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004497 if (ch == ',' && !paren && brackets <= 0) {
4498 if (++nparam >= sparam) {
4499 sparam += PARAM_DELTA;
4500 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004501 sparam * sizeof(Token *));
4502 paramsize = nasm_realloc(paramsize,
4503 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004504 }
4505 params[nparam] = tline->next;
4506 paramsize[nparam] = 0;
4507 white = 0;
4508 continue; /* parameter loop */
4509 }
4510 if (ch == '{' &&
4511 (brackets > 0 || (brackets == 0 &&
4512 !paramsize[nparam])))
4513 {
4514 if (!(brackets++)) {
4515 params[nparam] = tline->next;
4516 continue; /* parameter loop */
4517 }
4518 }
4519 if (ch == '}' && brackets > 0)
4520 if (--brackets == 0) {
4521 brackets = -1;
4522 continue; /* parameter loop */
4523 }
4524 if (ch == '(' && !brackets)
4525 paren++;
4526 if (ch == ')' && brackets <= 0)
4527 if (--paren < 0)
4528 break;
4529 }
4530 if (brackets < 0) {
4531 brackets = 0;
4532 error(ERR_NONFATAL, "braces do not "
4533 "enclose all of macro parameter");
4534 }
4535 paramsize[nparam] += white + 1;
4536 white = 0;
4537 } /* parameter loop */
4538 nparam++;
4539 while (m && (m->nparam != nparam ||
4540 mstrcmp(m->name, mname,
4541 m->casesense)))
4542 m = m->next;
4543 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004544 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004545 "macro `%s' exists, "
4546 "but not taking %d parameters",
4547 mstart->text, nparam);
4548 }
4549 }
4550 if (m && m->in_progress)
4551 m = NULL;
4552 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004553 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004554 * Design question: should we handle !tline, which
4555 * indicates missing ')' here, or expand those
4556 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004557 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004558 */
4559 nasm_free(params);
4560 nasm_free(paramsize);
4561 tline = mstart;
4562 } else {
4563 /*
4564 * Expand the macro: we are placed on the last token of the
4565 * call, so that we can easily split the call from the
4566 * following tokens. We also start by pushing an SMAC_END
4567 * token for the cycle removal.
4568 */
4569 t = tline;
4570 if (t) {
4571 tline = t->next;
4572 t->next = NULL;
4573 }
4574 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004575 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004576 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004577 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004578 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004579 if (t->type >= TOK_SMAC_PARAM) {
4580 Token *pcopy = tline, **ptail = &pcopy;
4581 Token *ttt, *pt;
4582 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004583
H. Peter Anvine2c80182005-01-15 22:15:51 +00004584 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004585 i = paramsize[t->type - TOK_SMAC_PARAM];
4586 while (--i >= 0) {
4587 pt = *ptail = new_Token(tline, ttt->type,
4588 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004589 ptail = &pt->next;
4590 ttt = ttt->next;
4591 }
4592 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004593 } else if (t->type == TOK_PREPROC_Q) {
4594 tt = new_Token(tline, TOK_ID, mname, 0);
4595 tline = tt;
4596 } else if (t->type == TOK_PREPROC_QQ) {
4597 tt = new_Token(tline, TOK_ID, m->name, 0);
4598 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004599 } else {
4600 tt = new_Token(tline, t->type, t->text, 0);
4601 tline = tt;
4602 }
4603 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004604
H. Peter Anvine2c80182005-01-15 22:15:51 +00004605 /*
4606 * Having done that, get rid of the macro call, and clean
4607 * up the parameters.
4608 */
4609 nasm_free(params);
4610 nasm_free(paramsize);
4611 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004612 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004613 continue; /* main token loop */
4614 }
4615 }
4616 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004617
H. Peter Anvine2c80182005-01-15 22:15:51 +00004618 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004619 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004620 tline = delete_Token(tline);
4621 } else {
4622 t = *tail = tline;
4623 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004624 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004625 t->next = NULL;
4626 tail = &t->next;
4627 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004628 }
4629
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004630 /*
4631 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004632 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004633 * TOK_IDs should be concatenated.
4634 * Also we look for %+ tokens and concatenate the tokens before and after
4635 * them (without white spaces in between).
4636 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004637 if (expanded) {
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004638 const struct tokseq_match t[] = {
4639 {
4640 PP_CONCAT_MASK(TOK_ID) |
4641 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4642 PP_CONCAT_MASK(TOK_ID) |
4643 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4644 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4645 }
4646 };
4647 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004648 /*
4649 * If we concatenated something, *and* we had previously expanded
4650 * an actual macro, scan the lines again for macros...
4651 */
4652 tline = thead;
4653 expanded = false;
4654 goto again;
4655 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004656 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004657
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004658err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004659 if (org_tline) {
4660 if (thead) {
4661 *org_tline = *thead;
4662 /* since we just gave text to org_line, don't free it */
4663 thead->text = NULL;
4664 delete_Token(thead);
4665 } else {
4666 /* the expression expanded to empty line;
4667 we can't return NULL for some reasons
4668 we just set the line to a single WHITESPACE token. */
4669 memset(org_tline, 0, sizeof(*org_tline));
4670 org_tline->text = NULL;
4671 org_tline->type = TOK_WHITESPACE;
4672 }
4673 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004674 }
4675
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004676 return thead;
4677}
4678
4679/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004680 * Similar to expand_smacro but used exclusively with macro identifiers
4681 * right before they are fetched in. The reason is that there can be
4682 * identifiers consisting of several subparts. We consider that if there
4683 * are more than one element forming the name, user wants a expansion,
4684 * otherwise it will be left as-is. Example:
4685 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004686 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004687 *
4688 * the identifier %$abc will be left as-is so that the handler for %define
4689 * will suck it and define the corresponding value. Other case:
4690 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004691 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004692 *
4693 * In this case user wants name to be expanded *before* %define starts
4694 * working, so we'll expand %$abc into something (if it has a value;
4695 * otherwise it will be left as-is) then concatenate all successive
4696 * PP_IDs into one.
4697 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004698static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004699{
4700 Token *cur, *oldnext = NULL;
4701
H. Peter Anvin734b1882002-04-30 21:01:08 +00004702 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004703 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004704
4705 cur = tline;
4706 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004707 (cur->next->type == TOK_ID ||
4708 cur->next->type == TOK_PREPROC_ID
4709 || cur->next->type == TOK_NUMBER))
4710 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004711
4712 /* If identifier consists of just one token, don't expand */
4713 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004714 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004715
H. Peter Anvine2c80182005-01-15 22:15:51 +00004716 if (cur) {
4717 oldnext = cur->next; /* Detach the tail past identifier */
4718 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004719 }
4720
H. Peter Anvin734b1882002-04-30 21:01:08 +00004721 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004722
H. Peter Anvine2c80182005-01-15 22:15:51 +00004723 if (cur) {
4724 /* expand_smacro possibly changhed tline; re-scan for EOL */
4725 cur = tline;
4726 while (cur && cur->next)
4727 cur = cur->next;
4728 if (cur)
4729 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004730 }
4731
4732 return tline;
4733}
4734
4735/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004736 * Determine whether the given line constitutes a multi-line macro
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004737 * call, and return the ExpDef structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004738 * to check for an initial label - that's taken care of in
4739 * expand_mmacro - but must check numbers of parameters. Guaranteed
4740 * to be called with tline->type == TOK_ID, so the putative macro
4741 * name is easy to find.
4742 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004743static ExpDef *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004744{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004745 ExpDef *head, *ed;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004746 Token **params;
4747 int nparam;
4748
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004749 head = (ExpDef *) hash_findix(&expdefs, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004750
4751 /*
4752 * Efficiency: first we see if any macro exists with the given
4753 * name. If not, we can return NULL immediately. _Then_ we
4754 * count the parameters, and then we look further along the
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004755 * list if necessary to find the proper ExpDef.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004756 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004757 list_for_each(ed, head)
4758 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004759 break;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004760 if (!ed)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004761 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004762
4763 /*
4764 * OK, we have a potential macro. Count and demarcate the
4765 * parameters.
4766 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004767 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004768
4769 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004770 * So we know how many parameters we've got. Find the ExpDef
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004771 * structure that handles this number.
4772 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004773 while (ed) {
4774 if (ed->nparam_min <= nparam
4775 && (ed->plus || nparam <= ed->nparam_max)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004776 /*
4777 * It's right, and we can use it. Add its default
4778 * parameters to the end of our list if necessary.
4779 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004780 if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004781 params =
4782 nasm_realloc(params,
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004783 ((ed->nparam_min + ed->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004784 1) * sizeof(*params)));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004785 while (nparam < ed->nparam_min + ed->ndefs) {
4786 params[nparam] = ed->defaults[nparam - ed->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004787 nparam++;
4788 }
4789 }
4790 /*
4791 * If we've gone over the maximum parameter count (and
4792 * we're in Plus mode), ignore parameters beyond
4793 * nparam_max.
4794 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004795 if (ed->plus && nparam > ed->nparam_max)
4796 nparam = ed->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004797 /*
4798 * Then terminate the parameter list, and leave.
4799 */
4800 if (!params) { /* need this special case */
4801 params = nasm_malloc(sizeof(*params));
4802 nparam = 0;
4803 }
4804 params[nparam] = NULL;
4805 *params_array = params;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004806 return ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004807 }
4808 /*
4809 * This one wasn't right: look for the next one with the
4810 * same name.
4811 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004812 list_for_each(ed, ed->next)
4813 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004814 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004815 }
4816
4817 /*
4818 * After all that, we didn't find one with the right number of
4819 * parameters. Issue a warning, and fail to expand the macro.
4820 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004821 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004822 "macro `%s' exists, but not taking %d parameters",
4823 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004824 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004825 return NULL;
4826}
4827
4828/*
4829 * Expand the multi-line macro call made by the given line, if
4830 * there is one to be expanded. If there is, push the expansion on
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004831 * istk->expansion and return true. Otherwise return false.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004832 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004833static bool expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004834{
H. Peter Anvineba20a72002-04-30 20:53:55 +00004835 Token *label = NULL;
4836 int dont_prepend = 0;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004837 Token **params, *t, *mtok;
4838 Line *l = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004839 ExpDef *ed;
4840 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004841 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004842 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004843
4844 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004845 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004846 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004847 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004848 return false;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004849 mtok = t;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004850 ed = is_mmacro(t, &params);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004851 if (ed != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004852 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004853 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004854 Token *last;
4855 /*
4856 * We have an id which isn't a macro call. We'll assume
4857 * it might be a label; we'll also check to see if a
4858 * colon follows it. Then, if there's another id after
4859 * that lot, we'll check it again for macro-hood.
4860 */
4861 label = last = t;
4862 t = t->next;
4863 if (tok_type_(t, TOK_WHITESPACE))
4864 last = t, t = t->next;
4865 if (tok_is_(t, ":")) {
4866 dont_prepend = 1;
4867 last = t, t = t->next;
4868 if (tok_type_(t, TOK_WHITESPACE))
4869 last = t, t = t->next;
4870 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004871 if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, &params)))
4872 return false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004873 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004874 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004875 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004876 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004877
4878 /*
4879 * Fix up the parameters: this involves stripping leading and
4880 * trailing whitespace, then stripping braces if they are
4881 * present.
4882 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004883 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004884 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004885
H. Peter Anvine2c80182005-01-15 22:15:51 +00004886 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004887 int brace = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004888 int comma = (!ed->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004889
H. Peter Anvine2c80182005-01-15 22:15:51 +00004890 t = params[i];
4891 skip_white_(t);
4892 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004893 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004894 params[i] = t;
4895 paramlen[i] = 0;
4896 while (t) {
4897 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4898 break; /* ... because we have hit a comma */
4899 if (comma && t->type == TOK_WHITESPACE
4900 && tok_is_(t->next, ","))
4901 break; /* ... or a space then a comma */
4902 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4903 break; /* ... or a brace */
4904 t = t->next;
4905 paramlen[i]++;
4906 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004907 }
4908
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004909 if (ed->cur_depth >= ed->max_depth) {
4910 if (ed->max_depth > 1) {
4911 error(ERR_WARNING,
4912 "reached maximum macro recursion depth of %i for %s",
4913 ed->max_depth,ed->name);
4914 }
4915 return false;
4916 } else {
4917 ed->cur_depth ++;
4918 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004919
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004920 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004921 * OK, we have found a ExpDef structure representing a
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004922 * previously defined mmacro. Create an expansion invocation
4923 * and point it back to the expansion definition. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004924 * parameter tokens and macro-local tokens doesn't get done
4925 * until the single-line macro substitution process; this is
4926 * because delaying them allows us to change the semantics
4927 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004928 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004929 ei = new_ExpInv(EXP_MMACRO, ed);
4930 ei->name = nasm_strdup(mname);
4931 //ei->label = label;
4932 //ei->label_text = detoken(label, false);
4933 ei->current = ed->line;
4934 ei->emitting = true;
4935 //ei->iline = tline;
4936 ei->params = params;
4937 ei->nparam = nparam;
4938 ei->rotate = 0;
4939 ei->paramlen = paramlen;
4940 ei->lineno = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004941
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004942 ei->prev = istk->expansion;
4943 istk->expansion = ei;
4944
4945 /*
4946 * Special case: detect %00 on first invocation; if found,
4947 * avoid emitting any labels that precede the mmacro call.
4948 * ed->prepend is set to -1 when %00 is detected, else 1.
4949 */
4950 if (ed->prepend == 0) {
4951 for (l = ed->line; l != NULL; l = l->next) {
4952 for (t = l->first; t != NULL; t = t->next) {
4953 if ((t->type == TOK_PREPROC_ID) &&
4954 (strlen(t->text) == 3) &&
4955 (t->text[1] == '0') && (t->text[2] == '0')) {
4956 dont_prepend = -1;
4957 break;
4958 }
4959 }
4960 if (dont_prepend < 0) {
4961 break;
4962 }
4963 }
4964 ed->prepend = ((dont_prepend < 0) ? -1 : 1);
4965 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004966
4967 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004968 * If we had a label, push it on as the first line of
4969 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004970 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004971 if (label != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004972 if (ed->prepend < 0) {
4973 ei->label_text = detoken(label, false);
4974 } else {
4975 if (dont_prepend == 0) {
4976 t = label;
4977 while (t->next != NULL) {
4978 t = t->next;
4979 }
4980 t->next = new_Token(NULL, TOK_OTHER, ":", 0);
4981 }
4982 l = new_Line();
4983 l->first = copy_Token(label);
4984 l->next = ei->current;
4985 ei->current = l;
4986 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004987 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004988
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004989 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004990
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004991 istk->mmac_depth++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004992 return true;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004993}
4994
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004995/* The function that actually does the error reporting */
4996static void verror(int severity, const char *fmt, va_list arg)
4997{
4998 char buff[1024];
4999
5000 vsnprintf(buff, sizeof(buff), fmt, arg);
5001
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005002 if (istk && istk->mmac_depth > 0) {
5003 ExpInv *ei = istk->expansion;
5004 int lineno = ei->lineno;
5005 while (ei) {
5006 if (ei->type == EXP_MMACRO)
5007 break;
5008 lineno += ei->relno;
5009 ei = ei->prev;
5010 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005011 nasm_error(severity, "(%s:%d) %s", ei->def->name,
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005012 lineno, buff);
5013 } else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005014 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005015}
5016
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005017/*
5018 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07005019 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005020 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005021static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005022{
5023 va_list arg;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005024 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005025 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00005026 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005027}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005028
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005029/*
5030 * Because %else etc are evaluated in the state context
5031 * of the previous branch, errors might get lost with error():
5032 * %if 0 ... %else trailing garbage ... %endif
5033 * So %else etc should report errors with this function.
5034 */
5035static void error_precond(int severity, const char *fmt, ...)
5036{
5037 va_list arg;
5038
5039 /* Only ignore the error if it's really in a dead branch */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005040 if ((istk != NULL) &&
5041 (istk->expansion != NULL) &&
5042 (istk->expansion->type == EXP_IF) &&
5043 (istk->expansion->def->state == COND_NEVER))
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005044 return;
5045
5046 va_start(arg, fmt);
5047 verror(severity, fmt, arg);
5048 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005049}
5050
H. Peter Anvin734b1882002-04-30 21:01:08 +00005051static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005052pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005053{
H. Peter Anvin7383b402008-09-24 10:20:40 -07005054 Token *t;
5055
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005056 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005057 istk = nasm_malloc(sizeof(Include));
5058 istk->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005059 istk->expansion = NULL;
5060 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00005061 istk->fname = NULL;
5062 src_set_fname(nasm_strdup(file));
5063 src_set_linnum(0);
5064 istk->lineinc = 1;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005065 istk->mmac_depth = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005066 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07005067 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00005068 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005069 defining = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005070 finals = NULL;
5071 in_final = false;
Charles Crayned4200be2008-07-12 16:42:33 -07005072 nested_mac_count = 0;
5073 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005074 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005075 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005076 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005077 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005078 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005079 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005080 }
H. Peter Anvind2456592008-06-19 15:04:18 -07005081 any_extrastdmac = extrastdmac && *extrastdmac;
5082 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005083 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005084
5085 /*
5086 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5087 * The caller, however, will also pass in 3 for preprocess-only so
5088 * we can set __PASS__ accordingly.
5089 */
5090 pass = apass > 2 ? 2 : apass;
5091
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005092 dephead = deptail = deplist;
5093 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005094 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
5095 sl->next = NULL;
5096 strcpy(sl->str, file);
5097 *deptail = sl;
5098 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005099 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07005100
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005101 /*
5102 * Define the __PASS__ macro. This is defined here unlike
5103 * all the other builtins, because it is special -- it varies between
5104 * passes.
5105 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07005106 t = nasm_malloc(sizeof(*t));
5107 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005108 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07005109 t->a.mac = NULL;
5110 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005111}
5112
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005113static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005114{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005115 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005116 Token *tline;
Keith Kanios9858cec2010-11-08 00:58:02 -06005117 ExpDef *ed;
5118 ExpInv *ei;
5119 Line *l;
5120 int j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005121
H. Peter Anvine2c80182005-01-15 22:15:51 +00005122 while (1) {
5123 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005124 * Fetch a tokenized line, either from the expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005125 * buffer or from the input file.
5126 */
5127 tline = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005128
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005129 while (1) { /* until we get a line we can use */
5130 /*
5131 * Fetch a tokenized line from the expansion buffer
5132 */
5133 if (istk->expansion != NULL) {
5134 ei = istk->expansion;
5135 if (ei->current != NULL) {
5136 if (ei->emitting == false) {
5137 ei->current = NULL;
5138 continue;
5139 }
5140 l = ei->current;
5141 ei->current = l->next;
5142 ei->lineno++;
5143 tline = copy_Token(l->first);
5144 if (((ei->type == EXP_REP) ||
5145 (ei->type == EXP_MMACRO) ||
5146 (ei->type == EXP_WHILE))
5147 && (ei->def->nolist == false)) {
5148 char *p = detoken(tline, false);
5149 list->line(LIST_MACRO, p);
5150 nasm_free(p);
5151 }
5152 if (ei->linnum > -1) {
5153 src_set_linnum(src_get_linnum() + 1);
5154 }
5155 break;
5156 } else if ((ei->type == EXP_REP) &&
5157 (ei->def->cur_depth < ei->def->max_depth)) {
5158 ei->def->cur_depth ++;
5159 ei->current = ei->def->line;
5160 ei->lineno = 0;
5161 continue;
5162 } else if ((ei->type == EXP_WHILE) &&
5163 (ei->def->cur_depth < ei->def->max_depth)) {
5164 ei->current = ei->def->line;
5165 ei->lineno = 0;
5166 tline = copy_Token(ei->current->first);
5167 j = if_condition(tline, PP_WHILE);
5168 tline = NULL;
5169 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
5170 if (j == COND_IF_TRUE) {
5171 ei->current = ei->current->next;
5172 ei->def->cur_depth ++;
5173 } else {
5174 ei->emitting = false;
5175 ei->current = NULL;
5176 ei->def->cur_depth = ei->def->max_depth;
5177 }
5178 continue;
5179 } else {
5180 istk->expansion = ei->prev;
5181 ed = ei->def;
5182 if (ed != NULL) {
5183 if ((ei->emitting == true) &&
5184 (ed->max_depth == DEADMAN_LIMIT) &&
5185 (ed->cur_depth == DEADMAN_LIMIT)
5186 ) {
5187 error(ERR_FATAL, "runaway expansion detected, aborting");
5188 }
5189 if (ed->cur_depth > 0) {
5190 ed->cur_depth --;
5191 } else if ((ed->type != EXP_MMACRO) && (ed->type != EXP_IF)) {
5192 /***** should this really be right here??? *****/
5193 /*
5194 Line *l = NULL, *ll = NULL;
5195 for (l = ed->line; l != NULL;) {
5196 if (l->first != NULL) {
5197 free_tlist(l->first);
5198 l->first = NULL;
5199 }
5200 ll = l;
5201 l = l->next;
5202 nasm_free(ll);
5203 }
5204 expansions = ed->prev;
5205 nasm_free(ed);
5206 */
5207 }
5208 if ((ei->type == EXP_REP) ||
5209 (ei->type == EXP_MMACRO) ||
5210 (ei->type == EXP_WHILE)) {
5211 list->downlevel(LIST_MACRO);
5212 if (ei->type == EXP_MMACRO) {
5213 istk->mmac_depth--;
5214 }
5215 }
5216 }
5217 if (ei->linnum > -1) {
5218 src_set_linnum(ei->linnum);
5219 }
5220 free_expinv(ei);
5221 continue;
5222 }
5223 }
5224
5225 /*
5226 * Read in line from input and tokenize
5227 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005228 line = read_line();
5229 if (line) { /* from the current input file */
5230 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005231 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005232 nasm_free(line);
5233 break;
5234 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005235
H. Peter Anvine2c80182005-01-15 22:15:51 +00005236 /*
5237 * The current file has ended; work down the istk
5238 */
5239 {
5240 Include *i = istk;
5241 fclose(i->fp);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005242 if (i->expansion != NULL) {
5243 error(ERR_FATAL,
5244 "end of file while still in an expansion");
5245 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005246 /* only set line and file name if there's a next node */
5247 if (i->next) {
5248 src_set_linnum(i->lineno);
5249 nasm_free(src_set_fname(i->fname));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005250 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005251 if ((i->next == NULL) && (finals != NULL)) {
5252 in_final = true;
5253 ei = new_ExpInv(EXP_FINAL, NULL);
5254 ei->emitting = true;
5255 ei->current = finals;
5256 istk->expansion = ei;
5257 finals = NULL;
5258 continue;
5259 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005260 istk = i->next;
5261 list->downlevel(LIST_INCLUDE);
5262 nasm_free(i);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005263 if (istk == NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005264 if (finals != NULL) {
5265 in_final = true;
5266 } else {
5267 return NULL;
5268 }
5269 }
5270 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005271 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005272 }
5273
5274 if (defining == NULL) {
5275 tline = expand_mmac_params(tline);
5276 }
5277
H. Peter Anvine2c80182005-01-15 22:15:51 +00005278 /*
5279 * Check the line to see if it's a preprocessor directive.
5280 */
5281 if (do_directive(tline) == DIRECTIVE_FOUND) {
5282 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005283 } else if (defining != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005284 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005285 * We're defining an expansion. We emit nothing at all,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005286 * and just shove the tokenized line on to the definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005287 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005288 if (defining->ignoring == false) {
5289 Line *l = new_Line();
5290 l->first = tline;
5291 if (defining->line == NULL) {
5292 defining->line = l;
5293 defining->last = l;
5294 } else {
5295 defining->last->next = l;
5296 defining->last = l;
5297 }
5298 } else {
5299 //free_tlist(tline); /***** sanity check: is this supposed to be here? *****/
5300 }
5301 defining->linecount++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005302 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005303 } else if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005304 (istk->expansion->emitting != true)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005305 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005306 * We're in a non-emitting branch of an expansion.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005307 * Emit nothing at all, not even a blank line: when we
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005308 * emerge from the expansion we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005309 * directive so we keep our place correctly.
5310 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005311 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005312 continue;
5313 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005314 tline = expand_smacro(tline);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005315 if (expand_mmacro(tline) != true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005316 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005317 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005318 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005319 line = detoken(tline, true);
5320 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005321 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005322 } else {
5323 continue;
5324 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005325 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005326 }
5327 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005328}
5329
H. Peter Anvine2c80182005-01-15 22:15:51 +00005330static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005331{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005332 if (defining != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005333 error(ERR_NONFATAL, "end of file while still defining an expansion");
5334 while (defining != NULL) {
5335 ExpDef *ed = defining;
5336 defining = ed->prev;
5337 free_expdef(ed);
5338 }
5339 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005340 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005341 while (cstk != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005342 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005343 free_macros();
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005344 while (istk != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005345 Include *i = istk;
5346 istk = istk->next;
5347 fclose(i->fp);
5348 nasm_free(i->fname);
5349 nasm_free(i);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005350 while (i->expansion != NULL) {
5351 ExpInv *ei = i->expansion;
5352 i->expansion = ei->prev;
5353 free_expinv(ei);
5354 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005355 }
5356 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005357 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005358 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005359 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005360 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005361 free_llist(predef);
5362 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005363 while ((i = ipath)) {
5364 ipath = i->next;
5365 if (i->path)
5366 nasm_free(i->path);
5367 nasm_free(i);
5368 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005369 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005370}
5371
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005372void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005373{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005374 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005375
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005376 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005377 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005378 i->next = NULL;
5379
H. Peter Anvin89cee572009-07-15 09:16:54 -04005380 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005381 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005382 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005383 j = j->next;
5384 j->next = i;
5385 } else {
5386 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005387 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005388}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005389
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005390void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005391{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005392 Token *inc, *space, *name;
5393 Line *l;
5394
H. Peter Anvin734b1882002-04-30 21:01:08 +00005395 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5396 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5397 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005398
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005399 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005400 l->next = predef;
5401 l->first = inc;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005402 predef = l;
5403}
5404
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005405void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005406{
5407 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005408 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005409 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005410
5411 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005412 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5413 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005414 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005415 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005416 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005417 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005418 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005419
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005420 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005421 l->next = predef;
5422 l->first = def;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005423 predef = l;
5424}
5425
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005426void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005427{
5428 Token *def, *space;
5429 Line *l;
5430
H. Peter Anvin734b1882002-04-30 21:01:08 +00005431 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5432 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005433 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005434
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005435 l = new_Line();
H. Peter Anvin620515a2002-04-30 20:57:38 +00005436 l->next = predef;
5437 l->first = def;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005438 predef = l;
5439}
5440
Keith Kaniosb7a89542007-04-12 02:40:54 +00005441/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005442 * This function is used to assist with "runtime" preprocessor
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005443 * directives, e.g. pp_runtime("%define __BITS__ 64");
Keith Kaniosb7a89542007-04-12 02:40:54 +00005444 *
5445 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5446 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5447 */
5448
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005449void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005450{
5451 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005452
Keith Kaniosb7a89542007-04-12 02:40:54 +00005453 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005454 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005455 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005456
Keith Kaniosb7a89542007-04-12 02:40:54 +00005457}
5458
H. Peter Anvina70547f2008-07-19 21:44:26 -07005459void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005460{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005461 extrastdmac = macros;
5462}
5463
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005464static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005465{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005466 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005467 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005468 tok->text = nasm_strdup(numbuf);
5469 tok->type = TOK_NUMBER;
5470}
5471
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005472Preproc nasmpp = {
5473 pp_reset,
5474 pp_getline,
5475 pp_cleanup
5476};