blob: 42c248d1dc40a4e1ce7ae587f7417a5fb9e7715a [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +04003 * Copyright 1996-2011 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000065#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000066#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000067#include <stdlib.h>
68#include <stddef.h>
69#include <string.h>
70#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000071#include <limits.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000072#include <inttypes.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000073
74#include "nasm.h"
75#include "nasmlib.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000076#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070077#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070078#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070079#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070080#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070081#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070082#include "tables.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000083
84typedef struct SMacro SMacro;
Keith Kaniosb307a4f2010-11-06 17:41:51 -050085typedef struct ExpDef ExpDef;
86typedef struct ExpInv ExpInv;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000087typedef struct Context Context;
88typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000089typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000090typedef struct Line Line;
91typedef struct Include Include;
92typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000093typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000094
95/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070096 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
102 */
103
104/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 * Store the definition of a single-line macro.
106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107struct SMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000108 SMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000109 char *name;
H. Peter Anvin70055962007-10-11 00:05:31 -0700110 bool casesense;
H. Peter Anvin16ed4382007-10-11 10:06:19 -0700111 bool in_progress;
H. Peter Anvin70055962007-10-11 00:05:31 -0700112 unsigned int nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000113 Token *expansion;
114};
115
116/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000117 * The context stack is composed of a linked list of these.
118 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000119struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000120 Context *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000121 char *name;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700122 struct hash_table localmac;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000123 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000124};
125
126/*
127 * This is the internal form which we break input lines up into.
128 * Typically stored in linked lists.
129 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000130 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
131 * necessarily used as-is, but is intended to denote the number of
132 * the substituted parameter. So in the definition
133 *
134 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700135 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000136 * the token representing `x' will have its type changed to
137 * TOK_SMAC_PARAM, but the one representing `y' will be
138 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000139 *
140 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
141 * which doesn't need quotes around it. Used in the pre-include
142 * mechanism as an alternative to trying to find a sensible type of
143 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000144 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000145enum pp_token_type {
146 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
147 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700148 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
149 TOK_INTERNAL_STRING,
150 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300151 TOK_PASTE, /* %+ */
152 TOK_INDIRECT, /* %[...] */
153 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
154 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000155};
156
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400157#define PP_CONCAT_MASK(x) (1 << (x))
158
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400159struct tokseq_match {
160 int mask_head;
161 int mask_tail;
162};
163
H. Peter Anvine2c80182005-01-15 22:15:51 +0000164struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000165 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000166 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700167 union {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300168 SMacro *mac; /* associated macro for TOK_SMAC_END */
169 size_t len; /* scratch length field */
170 } a; /* Auxiliary data */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000171 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000172};
173
174/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500175 * Expansion definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000176 * these, which is essentially a container to allow several linked
177 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700178 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000179 * Note that in this module, linked lists are treated as stacks
180 * wherever possible. For this reason, Lines are _pushed_ on to the
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500181 * `last' field in ExpDef structures, so that the linked list,
182 * if walked, would emit the expansion lines in the proper order.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000183 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000184struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000185 Line *next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000186 Token *first;
187};
188
189/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500190 * Expansion Types
191 */
192enum pp_exp_type {
193 EXP_NONE = 0, EXP_PREDEF,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300194 EXP_MMACRO, EXP_REP,
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500195 EXP_IF, EXP_WHILE,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300196 EXP_COMMENT, EXP_FINAL,
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500197 EXP_MAX = INT_MAX /* Keep compiler from reducing the range */
198};
199
200/*
201 * Store the definition of an expansion, in which is any
202 * preprocessor directive that has an ending pair.
203 *
204 * This design allows for arbitrary expansion/recursion depth,
205 * upto the DEADMAN_LIMIT.
206 *
207 * The `next' field is used for storing ExpDef in hash tables; the
208 * `prev' field is for the global `expansions` linked-list.
209 */
210struct ExpDef {
211 ExpDef *prev; /* previous definition */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300212 ExpDef *next; /* next in hash table */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500213 enum pp_exp_type type; /* expansion type */
214 char *name; /* definition name */
215 int nparam_min, nparam_max;
216 bool casesense;
217 bool plus; /* is the last parameter greedy? */
218 bool nolist; /* is this expansion listing-inhibited? */
219 Token *dlist; /* all defaults as one list */
220 Token **defaults; /* parameter default pointers */
221 int ndefs; /* number of default parameters */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300222
223 int prepend; /* label prepend state */
224 Line *label;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500225 Line *line;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300226 Line *last;
227 int linecount; /* number of lines within expansion */
228
229 int64_t def_depth; /* current number of definition pairs deep */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500230 int64_t cur_depth; /* current number of expansions */
231 int64_t max_depth; /* maximum number of expansions allowed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300232
233 int state; /* condition state */
234 bool ignoring; /* ignoring definition lines */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500235};
236
237/*
238 * Store the invocation of an expansion.
239 *
240 * The `prev' field is for the `istk->expansion` linked-list.
241 *
242 * When an expansion is being expanded, `params', `iline', `nparam',
243 * `paramlen', `rotate' and `unique' are local to the invocation.
244 */
245struct ExpInv {
246 ExpInv *prev; /* previous invocation */
247 enum pp_exp_type type; /* expansion type */
248 ExpDef *def; /* pointer to expansion definition */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300249 char *name; /* invocation name */
250 Line *label; /* pointer to label */
251 char *label_text; /* pointer to label text */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500252 Line *current; /* pointer to current line in invocation */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300253
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500254 Token **params; /* actual parameters */
255 Token *iline; /* invocation line */
256 unsigned int nparam, rotate;
257 int *paramlen;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300258
259 uint64_t unique;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500260 bool emitting;
261 int lineno; /* current line number in expansion */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300262 int linnum; /* line number at invocation */
263 int relno; /* relative line number at invocation */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500264};
265
266/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000267 * To handle an arbitrary level of file inclusion, we maintain a
268 * stack (ie linked list) of these things.
269 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000270struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000271 Include *next;
272 FILE *fp;
273 Cond *conds;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500274 ExpInv *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000275 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000276 int lineno, lineinc;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300277 int mmac_depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000278};
279
280/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000281 * Include search path. This is simply a list of strings which get
282 * prepended, in turn, to the name of an include file, in an
283 * attempt to find the file if it's not in the current directory.
284 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000285struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000286 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000287 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000288};
289
290/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000291 * Conditional assembly: we maintain a separate stack of these for
292 * each level of file inclusion. (The only reason we keep the
293 * stacks separate is to ensure that a stray `%endif' in a file
294 * included from within the true branch of a `%if' won't terminate
295 * it and cause confusion: instead, rightly, it'll cause an error.)
296 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000297enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000298 /*
299 * These states are for use just after %if or %elif: IF_TRUE
300 * means the condition has evaluated to truth so we are
301 * currently emitting, whereas IF_FALSE means we are not
302 * currently emitting but will start doing so if a %else comes
303 * up. In these states, all directives are admissible: %elif,
304 * %else and %endif. (And of course %if.)
305 */
306 COND_IF_TRUE, COND_IF_FALSE,
307 /*
308 * These states come up after a %else: ELSE_TRUE means we're
309 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
310 * any %elif or %else will cause an error.
311 */
312 COND_ELSE_TRUE, COND_ELSE_FALSE,
313 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200314 * These states mean that we're not emitting now, and also that
315 * nothing until %endif will be emitted at all. COND_DONE is
316 * used when we've had our moment of emission
317 * and have now started seeing %elifs. COND_NEVER is used when
318 * the condition construct in question is contained within a
319 * non-emitting branch of a larger condition construct,
320 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000321 */
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200322 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000323};
324#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
325
H. Peter Anvin70653092007-10-19 14:42:29 -0700326/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000327 * These defines are used as the possible return values for do_directive
328 */
329#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300330#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000331
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000332/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500333 * This define sets the upper limit for smacro and expansions
Keith Kanios852f1ee2009-07-12 00:19:55 -0500334 */
335#define DEADMAN_LIMIT (1 << 20)
336
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400337/* max reps */
338#define REP_LIMIT ((INT64_C(1) << 62))
339
Cyrill Gorcunovcb00cd12011-06-13 21:25:10 +0400340const struct tokseq_match pp_concat_match[] = {
341 {
342 PP_CONCAT_MASK(TOK_ID) |
343 PP_CONCAT_MASK(TOK_PREPROC_ID) |
344 PP_CONCAT_MASK(TOK_NUMBER) |
345 PP_CONCAT_MASK(TOK_FLOAT) |
346 PP_CONCAT_MASK(TOK_OTHER),
347
348 PP_CONCAT_MASK(TOK_ID) |
349 PP_CONCAT_MASK(TOK_PREPROC_ID) |
350 PP_CONCAT_MASK(TOK_NUMBER) |
351 PP_CONCAT_MASK(TOK_FLOAT) |
352 PP_CONCAT_MASK(TOK_OTHER)
353 }
354};
355
Keith Kanios852f1ee2009-07-12 00:19:55 -0500356/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000357 * Condition codes. Note that we use c_ prefix not C_ because C_ is
358 * used in nasm.h for the "real" condition codes. At _this_ level,
359 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
360 * ones, so we need a different enum...
361 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700362static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000363 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
364 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000365 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000366};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700367enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000368 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
369 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 -0700370 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
371 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000372};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700373static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000374 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
375 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 +0000376 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000377};
378
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000379/* For TASM compatibility we need to be able to recognise TASM compatible
380 * conditional compilation directives. Using the NASM pre-processor does
381 * not work, so we look for them specifically from the following list and
382 * then jam in the equivalent NASM directive into the input stream.
383 */
384
H. Peter Anvine2c80182005-01-15 22:15:51 +0000385enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000386 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
387 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
388};
389
H. Peter Anvin476d2862007-10-02 22:04:15 -0700390static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000391 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
392 "ifndef", "include", "local"
393};
394
395static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000396static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000397static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800398static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000399
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000400static Context *cstk;
401static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000402static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000403
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300404static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700405static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000406
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300407static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000408
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000409static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700410static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000411
412static ListGen *list;
413
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000414/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500415 * The current set of expansion definitions we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000416 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500417static struct hash_table expdefs;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000418
419/*
420 * The current set of single-line macros we have defined.
421 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700422static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000423
424/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500425 * Linked List of all active expansion definitions
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000426 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500427struct ExpDef *expansions = NULL;
428
429/*
430 * The expansion we are currently defining
431 */
432static ExpDef *defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000433
Charles Crayned4200be2008-07-12 16:42:33 -0700434static uint64_t nested_mac_count;
435static uint64_t nested_rep_count;
436
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000437/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500438 * Linked-list of lines to preprocess, prior to cleanup
439 */
440static Line *finals = NULL;
441static bool in_final = false;
442
443/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000444 * The number of macro parameters to allocate space for at a time.
445 */
446#define PARAM_DELTA 16
447
448/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700449 * The standard macro set: defined in macros.c in the array nasm_stdmac.
450 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000451 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700452static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000453
454/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000455 * The extra standard macros that come from the object format, if
456 * any.
457 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700458static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700459static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000460
461/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000462 * Tokens are allocated in blocks to improve speed
463 */
464#define TOKEN_BLOCKSIZE 4096
465static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000466struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000467 Blocks *next;
468 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000469};
470
471static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000472
473/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000474 * Forward declarations.
475 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000476static Token *expand_mmac_params(Token * tline);
477static Token *expand_smacro(Token * tline);
478static Token *expand_id(Token * tline);
H. Peter Anvinf8ad5322009-02-21 17:55:08 -0800479static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300480 bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700481static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000482static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200483static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000484static void *new_Block(size_t size);
485static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700486static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300487 const char *text, int txtlen);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500488static Token *copy_Token(Token * tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000489static Token *delete_Token(Token * t);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500490static Line *new_Line(void);
491static ExpDef *new_ExpDef(int exp_type);
492static ExpInv *new_ExpInv(int exp_type, ExpDef *ed);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000493
494/*
495 * Macros for safe checking of token pointers, avoid *(NULL)
496 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300497#define tok_type_(x,t) ((x) && (x)->type == (t))
498#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
499#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
500#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000501
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300502#ifdef NASM_TRACE
503
504#define dump_token(t) raw_dump_token(t, __FILE__, __LINE__, __func__);
505static void raw_dump_token(Token *token, const char *file, int line, const char *func)
506{
507 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
508 if (token) {
509 Token *t;
510 list_for_each(t, token) {
511 if (t->text)
512 printf("'%s' ", t->text);
513 }
514 printf("\n");
515 }
516}
517
518#endif
519
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300520/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700521 * nasm_unquote with error if the string contains NUL characters.
522 * If the string contains NUL characters, issue an error and return
523 * the C len, i.e. truncate at the NUL.
524 */
525static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
526{
527 size_t len = nasm_unquote(qstr, NULL);
528 size_t clen = strlen(qstr);
529
530 if (len != clen)
531 error(ERR_NONFATAL, "NUL character in `%s' directive",
532 pp_directives[directive]);
533
534 return clen;
535}
536
537/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700538 * In-place reverse a list of tokens.
539 */
540static Token *reverse_tokens(Token *t)
541{
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400542 Token *prev, *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700543
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400544 list_reverse(t, prev, next);
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700545
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400546 return t;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700547}
548
549/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300550 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000551 * front of them. We do it here because I could not find any other
552 * place to do it for the moment, and it is a hack (ideally it would
553 * be nice to be able to use the NASM pre-processor to do it).
554 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000555static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000556{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000557 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400558 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000559
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400560 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000561
562 /* Binary search for the directive name */
563 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400564 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400565 q = nasm_skip_word(p);
566 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000567 if (len) {
568 oldchar = p[len];
569 p[len] = 0;
570 while (j - i > 1) {
571 k = (j + i) / 2;
572 m = nasm_stricmp(p, tasm_directives[k]);
573 if (m == 0) {
574 /* We have found a directive, so jam a % in front of it
575 * so that NASM will then recognise it as one if it's own.
576 */
577 p[len] = oldchar;
578 len = strlen(p);
579 oldline = line;
580 line = nasm_malloc(len + 2);
581 line[0] = '%';
582 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700583 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300584 * NASM does not recognise IFDIFI, so we convert
585 * it to %if 0. This is not used in NASM
586 * compatible code, but does need to parse for the
587 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000588 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700589 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000590 } else {
591 memcpy(line + 1, p, len + 1);
592 }
593 nasm_free(oldline);
594 return line;
595 } else if (m < 0) {
596 j = k;
597 } else
598 i = k;
599 }
600 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000601 }
602 return line;
603}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000604
H. Peter Anvin76690a12002-04-30 20:52:49 +0000605/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000606 * The pre-preprocessing stage... This function translates line
607 * number indications as they emerge from GNU cpp (`# lineno "file"
608 * flags') into NASM preprocessor line number indications (`%line
609 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000610 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000611static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000612{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000613 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000614 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000615
H. Peter Anvine2c80182005-01-15 22:15:51 +0000616 if (line[0] == '#' && line[1] == ' ') {
617 oldline = line;
618 fname = oldline + 2;
619 lineno = atoi(fname);
620 fname += strspn(fname, "0123456789 ");
621 if (*fname == '"')
622 fname++;
623 fnlen = strcspn(fname, "\"");
624 line = nasm_malloc(20 + fnlen);
625 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
626 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000627 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000628 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000629 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000630 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000631}
632
633/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000634 * Free a linked list of tokens.
635 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000636static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000637{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400638 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000639 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000640}
641
642/*
643 * Free a linked list of lines.
644 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000645static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000646{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400647 Line *l, *tmp;
648 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000649 free_tlist(l->first);
650 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000651 }
652}
653
654/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500655 * Free an ExpDef
H. Peter Anvineba20a72002-04-30 20:53:55 +0000656 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500657static void free_expdef(ExpDef * ed)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000658{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500659 nasm_free(ed->name);
660 free_tlist(ed->dlist);
661 nasm_free(ed->defaults);
662 free_llist(ed->line);
663 nasm_free(ed);
664}
665
666/*
667 * Free an ExpInv
668 */
669static void free_expinv(ExpInv * ei)
670{
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300671 if (ei->name != NULL)
672 nasm_free(ei->name);
673 if (ei->label_text != NULL)
674 nasm_free(ei->label_text);
675 nasm_free(ei);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000676}
677
678/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700679 * Free all currently defined macros, and free the hash tables
680 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700681static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700682{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400683 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700684 const char *key;
685 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700686
H. Peter Anvin072771e2008-05-22 13:17:51 -0700687 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300688 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400689 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300690 nasm_free(s->name);
691 free_tlist(s->expansion);
692 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300693 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700694 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700695 hash_free(smt);
696}
697
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500698static void free_expdef_table(struct hash_table *edt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700699{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500700 ExpDef *ed, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700701 const char *key;
702 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700703
704 it = NULL;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500705 while ((ed = hash_iterate(edt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300706 nasm_free((void *)key);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500707 list_for_each_safe(ed ,tmp, ed)
708 free_expdef(ed);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700709 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500710 hash_free(edt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700711}
712
713static void free_macros(void)
714{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700715 free_smacro_table(&smacros);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500716 free_expdef_table(&expdefs);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700717}
718
719/*
720 * Initialize the hash tables
721 */
722static void init_macros(void)
723{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700724 hash_init(&smacros, HASH_LARGE);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500725 hash_init(&expdefs, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700726}
727
728/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000729 * Pop the context stack.
730 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000731static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000732{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000733 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000734
735 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700736 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000737 nasm_free(c->name);
738 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000739}
740
H. Peter Anvin072771e2008-05-22 13:17:51 -0700741/*
742 * Search for a key in the hash index; adding it if necessary
743 * (in which case we initialize the data pointer to NULL.)
744 */
745static void **
746hash_findi_add(struct hash_table *hash, const char *str)
747{
748 struct hash_insert hi;
749 void **r;
750 char *strx;
751
752 r = hash_findi(hash, str, &hi);
753 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300754 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700755
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300756 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700757 return hash_add(&hi, strx, NULL);
758}
759
760/*
761 * Like hash_findi, but returns the data element rather than a pointer
762 * to it. Used only when not adding a new element, hence no third
763 * argument.
764 */
765static void *
766hash_findix(struct hash_table *hash, const char *str)
767{
768 void **p;
769
770 p = hash_findi(hash, str, NULL);
771 return p ? *p : NULL;
772}
773
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400774/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500775 * read line from standard macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400776 * if there no more left -- return NULL
777 */
778static char *line_from_stdmac(void)
779{
780 unsigned char c;
781 const unsigned char *p = stdmacpos;
782 char *line, *q;
783 size_t len = 0;
784
785 if (!stdmacpos)
786 return NULL;
787
788 while ((c = *p++)) {
789 if (c >= 0x80)
790 len += pp_directives_len[c - 0x80] + 1;
791 else
792 len++;
793 }
794
795 line = nasm_malloc(len + 1);
796 q = line;
797 while ((c = *stdmacpos++)) {
798 if (c >= 0x80) {
799 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
800 q += pp_directives_len[c - 0x80];
801 *q++ = ' ';
802 } else {
803 *q++ = c;
804 }
805 }
806 stdmacpos = p;
807 *q = '\0';
808
809 if (!*stdmacpos) {
810 /* This was the last of the standard macro chain... */
811 stdmacpos = NULL;
812 if (any_extrastdmac) {
813 stdmacpos = extrastdmac;
814 any_extrastdmac = false;
815 } else if (do_predef) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300816 ExpInv *ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400817 Line *pd, *l;
818 Token *head, **tail, *t;
819
820 /*
821 * Nasty hack: here we push the contents of
822 * `predef' on to the top-level expansion stack,
823 * since this is the most convenient way to
824 * implement the pre-include and pre-define
825 * features.
826 */
827 list_for_each(pd, predef) {
828 head = NULL;
829 tail = &head;
830 list_for_each(t, pd->first) {
831 *tail = new_Token(NULL, t->type, t->text, 0);
832 tail = &(*tail)->next;
833 }
834
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500835 l = new_Line();
836 l->first = head;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300837 ei = new_ExpInv(EXP_PREDEF, NULL);
838 ei->current = l;
839 ei->emitting = true;
840 ei->prev = istk->expansion;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500841 istk->expansion = ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400842 }
843 do_predef = false;
844 }
845 }
846
847 return line;
848}
849
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000850#define BUF_DELTA 512
851/*
852 * Read a line from the top file in istk, handling multiple CR/LFs
853 * at the end of the line read, and handling spurious ^Zs. Will
854 * return lines from the standard macro set if this has not already
855 * been done.
856 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000857static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000858{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000859 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000860 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000861
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400862 /*
863 * standart macros set (predefined) goes first
864 */
865 p = line_from_stdmac();
866 if (p)
867 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700868
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400869 /*
870 * regular read from a file
871 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000872 bufsize = BUF_DELTA;
873 buffer = nasm_malloc(BUF_DELTA);
874 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000875 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000876 while (1) {
877 q = fgets(p, bufsize - (p - buffer), istk->fp);
878 if (!q)
879 break;
880 p += strlen(p);
881 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300882 /*
883 * Convert backslash-CRLF line continuation sequences into
884 * nothing at all (for DOS and Windows)
885 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000886 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
887 p -= 3;
888 *p = 0;
889 continued_count++;
890 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300891 /*
892 * Also convert backslash-LF line continuation sequences into
893 * nothing at all (for Unix)
894 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000895 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
896 p -= 2;
897 *p = 0;
898 continued_count++;
899 } else {
900 break;
901 }
902 }
903 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000904 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000905 bufsize += BUF_DELTA;
906 buffer = nasm_realloc(buffer, bufsize);
907 p = buffer + offset; /* prevent stale-pointer problems */
908 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000909 }
910
H. Peter Anvine2c80182005-01-15 22:15:51 +0000911 if (!q && p == buffer) {
912 nasm_free(buffer);
913 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000914 }
915
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300916 src_set_linnum(src_get_linnum() + istk->lineinc +
917 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000918
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000919 /*
920 * Play safe: remove CRs as well as LFs, if any of either are
921 * present at the end of the line.
922 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000923 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000924 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000925
926 /*
927 * Handle spurious ^Z, which may be inserted into source files
928 * by some file transfer utilities.
929 */
930 buffer[strcspn(buffer, "\032")] = '\0';
931
H. Peter Anvin734b1882002-04-30 21:01:08 +0000932 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000933
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000934 return buffer;
935}
936
937/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000938 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000939 * don't need to parse the value out of e.g. numeric tokens: we
940 * simply split one string into many.
941 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000942static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000943{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700944 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000945 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000946 Token *list = NULL;
947 Token *t, **tail = &list;
Keith Kanios6faad4e2010-12-18 14:08:02 -0600948 bool verbose = true;
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +0400949
Keith Kanios6faad4e2010-12-18 14:08:02 -0600950 if ((defining != NULL) && (defining->ignoring == true)) {
951 verbose = false;
952 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000953
H. Peter Anvine2c80182005-01-15 22:15:51 +0000954 while (*line) {
955 p = line;
956 if (*p == '%') {
957 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300958 if (*p == '+' && !nasm_isdigit(p[1])) {
959 p++;
960 type = TOK_PASTE;
961 } else if (nasm_isdigit(*p) ||
962 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000963 do {
964 p++;
965 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700966 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967 type = TOK_PREPROC_ID;
968 } else if (*p == '{') {
969 p++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500970 while (*p && *p != '}') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971 p[-1] = *p;
972 p++;
973 }
974 p[-1] = '\0';
975 if (*p)
976 p++;
977 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300978 } else if (*p == '[') {
979 int lvl = 1;
980 line += 2; /* Skip the leading %[ */
981 p++;
982 while (lvl && (c = *p++)) {
983 switch (c) {
984 case ']':
985 lvl--;
986 break;
987 case '%':
988 if (*p == '[')
989 lvl++;
990 break;
991 case '\'':
992 case '\"':
993 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400994 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300995 break;
996 default:
997 break;
998 }
999 }
1000 p--;
1001 if (*p)
1002 *p++ = '\0';
Keith Kanios6faad4e2010-12-18 14:08:02 -06001003 if (lvl && verbose)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001004 error(ERR_NONFATAL, "unterminated %[ construct");
1005 type = TOK_INDIRECT;
1006 } else if (*p == '?') {
1007 type = TOK_PREPROC_Q; /* %? */
1008 p++;
1009 if (*p == '?') {
1010 type = TOK_PREPROC_QQ; /* %?? */
1011 p++;
1012 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001013 } else if (*p == '!') {
1014 type = TOK_PREPROC_ID;
1015 p++;
1016 if (isidchar(*p)) {
1017 do {
1018 p++;
1019 } while (isidchar(*p));
1020 } else if (*p == '\'' || *p == '\"' || *p == '`') {
1021 p = nasm_skip_string(p);
1022 if (*p)
1023 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001024 else if(verbose)
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001025 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
1026 } else {
1027 /* %! without string or identifier */
1028 type = TOK_OTHER; /* Legacy behavior... */
1029 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001030 } else if (isidchar(*p) ||
1031 ((*p == '!' || *p == '%' || *p == '$') &&
1032 isidchar(p[1]))) {
1033 do {
1034 p++;
1035 }
1036 while (isidchar(*p));
1037 type = TOK_PREPROC_ID;
1038 } else {
1039 type = TOK_OTHER;
1040 if (*p == '%')
1041 p++;
1042 }
1043 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1044 type = TOK_ID;
1045 p++;
1046 while (*p && isidchar(*p))
1047 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001048 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001049 /*
1050 * A string token.
1051 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001052 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001053 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001054
H. Peter Anvine2c80182005-01-15 22:15:51 +00001055 if (*p) {
1056 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001057 } else if(verbose) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001058 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 /* Handling unterminated strings by UNV */
1060 /* type = -1; */
1061 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001062 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001063 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001064 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001065 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001066 bool is_hex = false;
1067 bool is_float = false;
1068 bool has_e = false;
1069 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001070
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001072 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001073 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001074
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001075 if (*p == '$') {
1076 p++;
1077 is_hex = true;
1078 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001079
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001080 for (;;) {
1081 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001082
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001083 if (!is_hex && (c == 'e' || c == 'E')) {
1084 has_e = true;
1085 if (*p == '+' || *p == '-') {
1086 /*
1087 * e can only be followed by +/- if it is either a
1088 * prefixed hex number or a floating-point number
1089 */
1090 p++;
1091 is_float = true;
1092 }
1093 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1094 is_hex = true;
1095 } else if (c == 'P' || c == 'p') {
1096 is_float = true;
1097 if (*p == '+' || *p == '-')
1098 p++;
1099 } else if (isnumchar(c) || c == '_')
1100 ; /* just advance */
1101 else if (c == '.') {
1102 /*
1103 * we need to deal with consequences of the legacy
1104 * parser, like "1.nolist" being two tokens
1105 * (TOK_NUMBER, TOK_ID) here; at least give it
1106 * a shot for now. In the future, we probably need
1107 * a flex-based scanner with proper pattern matching
1108 * to do it as well as it can be done. Nothing in
1109 * the world is going to help the person who wants
1110 * 0x123.p16 interpreted as two tokens, though.
1111 */
1112 r = p;
1113 while (*r == '_')
1114 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001115
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001116 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1117 (!is_hex && (*r == 'e' || *r == 'E')) ||
1118 (*r == 'p' || *r == 'P')) {
1119 p = r;
1120 is_float = true;
1121 } else
1122 break; /* Terminate the token */
1123 } else
1124 break;
1125 }
1126 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001127
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001128 if (p == line+1 && *line == '$') {
1129 type = TOK_OTHER; /* TOKEN_HERE */
1130 } else {
1131 if (has_e && !is_hex) {
1132 /* 1e13 is floating-point, but 1e13h is not */
1133 is_float = true;
1134 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001135
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001136 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1137 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001138 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001140 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001141 /*
1142 * Whitespace just before end-of-line is discarded by
1143 * pretending it's a comment; whitespace just before a
1144 * comment gets lumped into the comment.
1145 */
1146 if (!*p || *p == ';') {
1147 type = TOK_COMMENT;
1148 while (*p)
1149 p++;
1150 }
1151 } else if (*p == ';') {
1152 type = TOK_COMMENT;
1153 while (*p)
1154 p++;
1155 } else {
1156 /*
1157 * Anything else is an operator of some kind. We check
1158 * for all the double-character operators (>>, <<, //,
1159 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001160 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161 */
1162 type = TOK_OTHER;
1163 if ((p[0] == '>' && p[1] == '>') ||
1164 (p[0] == '<' && p[1] == '<') ||
1165 (p[0] == '/' && p[1] == '/') ||
1166 (p[0] == '<' && p[1] == '=') ||
1167 (p[0] == '>' && p[1] == '=') ||
1168 (p[0] == '=' && p[1] == '=') ||
1169 (p[0] == '!' && p[1] == '=') ||
1170 (p[0] == '<' && p[1] == '>') ||
1171 (p[0] == '&' && p[1] == '&') ||
1172 (p[0] == '|' && p[1] == '|') ||
1173 (p[0] == '^' && p[1] == '^')) {
1174 p++;
1175 }
1176 p++;
1177 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001178
H. Peter Anvine2c80182005-01-15 22:15:51 +00001179 /* Handling unterminated string by UNV */
1180 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001181 {
1182 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1183 t->text[p-line] = *line;
1184 tail = &t->next;
1185 }
1186 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001187 if (type != TOK_COMMENT) {
1188 *tail = t = new_Token(NULL, type, line, p - line);
1189 tail = &t->next;
1190 }
1191 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001192 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001193 return list;
1194}
1195
H. Peter Anvince616072002-04-30 21:02:23 +00001196/*
1197 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001198 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001199 * deleted only all at once by the delete_Blocks function.
1200 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001201static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001202{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001203 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001204
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001205 /* first, get to the end of the linked list */
1206 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001207 b = b->next;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001208
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001209 /* now allocate the requested chunk */
1210 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001211
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001212 /* now allocate a new block for the next request */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001213 b->next = nasm_zalloc(sizeof(Blocks));
1214
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001215 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001216}
1217
1218/*
1219 * this function deletes all managed blocks of memory
1220 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001221static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001222{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001223 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001224
H. Peter Anvin70653092007-10-19 14:42:29 -07001225 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001226 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001227 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001228 * free it.
1229 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001230 while (b) {
1231 if (b->chunk)
1232 nasm_free(b->chunk);
1233 a = b;
1234 b = b->next;
1235 if (a != &blocks)
1236 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001237 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001238}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001239
1240/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001241 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001242 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001243 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001244 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001245static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001246 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001247{
1248 Token *t;
1249 int i;
1250
H. Peter Anvin89cee572009-07-15 09:16:54 -04001251 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001252 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1253 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1254 freeTokens[i].next = &freeTokens[i + 1];
1255 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001256 }
1257 t = freeTokens;
1258 freeTokens = t->next;
1259 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001260 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001261 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001262 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 t->text = NULL;
1264 } else {
1265 if (txtlen == 0)
1266 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001267 t->text = nasm_malloc(txtlen+1);
1268 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001269 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001270 }
1271 return t;
1272}
1273
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001274static Token *copy_Token(Token * tline)
1275{
1276 Token *t, *tt, *first = NULL, *prev = NULL;
1277 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001278 for (tt = tline; tt != NULL; tt = tt->next) {
1279 if (!freeTokens) {
1280 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1281 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1282 freeTokens[i].next = &freeTokens[i + 1];
1283 freeTokens[i].next = NULL;
1284 }
1285 t = freeTokens;
1286 freeTokens = t->next;
1287 t->next = NULL;
1288 t->text = tt->text ? nasm_strdup(tt->text) : NULL;
1289 t->a.mac = tt->a.mac;
1290 t->a.len = tt->a.len;
1291 t->type = tt->type;
1292 if (prev != NULL) {
1293 prev->next = t;
1294 } else {
1295 first = t;
1296 }
1297 prev = t;
1298 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001299 return first;
1300}
1301
H. Peter Anvine2c80182005-01-15 22:15:51 +00001302static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001303{
1304 Token *next = t->next;
1305 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001306 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001307 freeTokens = t;
1308 return next;
1309}
1310
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001311/*
1312 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001313 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1314 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001315 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001316static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001317{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001318 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001319 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001320 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001321 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001322
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001323 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001324 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001325 char *v;
1326 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001327
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001328 v = t->text + 2;
1329 if (*v == '\'' || *v == '\"' || *v == '`') {
1330 size_t len = nasm_unquote(v, NULL);
1331 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001332
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001333 if (len != clen) {
1334 error(ERR_NONFATAL | ERR_PASS1,
1335 "NUL character in %! string");
1336 v = NULL;
1337 }
1338 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001339
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001340 if (v) {
1341 char *p = getenv(v);
1342 if (!p) {
1343 error(ERR_NONFATAL | ERR_PASS1,
1344 "nonexistent environment variable `%s'", v);
1345 p = "";
1346 }
1347 t->text = nasm_strdup(p);
1348 }
1349 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001351
H. Peter Anvine2c80182005-01-15 22:15:51 +00001352 /* Expand local macros here and not during preprocessing */
1353 if (expand_locals &&
1354 t->type == TOK_PREPROC_ID && t->text &&
1355 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001356 const char *q;
1357 char *p;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001358 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001360 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001361 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001362 p = nasm_strcat(buffer, q);
1363 nasm_free(t->text);
1364 t->text = p;
1365 }
1366 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001367
1368 /* Expand %? and %?? directives */
Keith Kanios3136d482010-11-13 09:34:34 -06001369 if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001370 ((t->type == TOK_PREPROC_Q) ||
1371 (t->type == TOK_PREPROC_QQ))) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001372 ExpInv *ei;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001373 for (ei = istk->expansion; ei != NULL; ei = ei->prev){
1374 if (ei->type == EXP_MMACRO) {
1375 nasm_free(t->text);
1376 if (t->type == TOK_PREPROC_Q) {
1377 t->text = nasm_strdup(ei->name);
1378 } else {
1379 t->text = nasm_strdup(ei->def->name);
1380 }
1381 break;
1382 }
1383 }
1384 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001385
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001386 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001387 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001388 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001389 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001390 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001391
H. Peter Anvin734b1882002-04-30 21:01:08 +00001392 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001393
1394 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001395 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001396 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001397 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001398 q = t->text;
1399 while (*q)
1400 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001401 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001402 }
1403 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001404
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001405 return line;
1406}
1407
1408/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001409 * Initialize a new Line
1410 */
Cyrill Gorcunov29cb0bb2010-11-11 11:19:43 +03001411static inline Line *new_Line(void)
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001412{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001413 return (Line *)nasm_zalloc(sizeof(Line));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001414}
1415
1416
1417/*
1418 * Initialize a new Expansion Definition
1419 */
1420static ExpDef *new_ExpDef(int exp_type)
1421{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001422 ExpDef *ed = (ExpDef*)nasm_zalloc(sizeof(ExpDef));
1423 ed->type = exp_type;
1424 ed->casesense = true;
1425 ed->state = COND_NEVER;
1426
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001427 return ed;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001428}
1429
1430
1431/*
1432 * Initialize a new Expansion Instance
1433 */
1434static ExpInv *new_ExpInv(int exp_type, ExpDef *ed)
1435{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001436 ExpInv *ei = (ExpInv*)nasm_zalloc(sizeof(ExpInv));
1437 ei->type = exp_type;
1438 ei->def = ed;
1439 ei->unique = ++unique;
1440
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001441 if ((istk->mmac_depth < 1) &&
1442 (istk->expansion == NULL) &&
1443 (ed != NULL) &&
1444 (ed->type != EXP_MMACRO) &&
1445 (ed->type != EXP_REP) &&
1446 (ed->type != EXP_WHILE)) {
1447 ei->linnum = src_get_linnum();
1448 src_set_linnum(ei->linnum - ed->linecount - 1);
1449 } else {
1450 ei->linnum = -1;
1451 }
1452 if ((istk->expansion == NULL) ||
1453 (ei->type == EXP_MMACRO)) {
1454 ei->relno = 0;
1455 } else {
1456 ei->relno = istk->expansion->lineno;
1457 if (ed != NULL) {
1458 ei->relno -= (ed->linecount + 1);
1459 }
1460 }
1461 return ei;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001462}
1463
1464/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001465 * A scanner, suitable for use by the expression evaluator, which
1466 * operates on a line of Tokens. Expects a pointer to a pointer to
1467 * the first token in the line to be passed in as its private_data
1468 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001469 *
1470 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001471 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001472static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001473{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001474 Token **tlineptr = private_data;
1475 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001476 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001477
H. Peter Anvine2c80182005-01-15 22:15:51 +00001478 do {
1479 tline = *tlineptr;
1480 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001481 } while (tline && (tline->type == TOK_WHITESPACE ||
1482 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001483
1484 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001485 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001486
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001487 tokval->t_charptr = tline->text;
1488
H. Peter Anvin76690a12002-04-30 20:52:49 +00001489 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001490 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001491 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001492 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001493
H. Peter Anvine2c80182005-01-15 22:15:51 +00001494 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001495 p = tokval->t_charptr = tline->text;
1496 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001497 tokval->t_charptr++;
1498 return tokval->t_type = TOKEN_ID;
1499 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001500
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001501 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001502 if (r >= p+MAX_KEYWORD)
1503 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001504 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001505 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001506 *s = '\0';
1507 /* right, so we have an identifier sitting in temp storage. now,
1508 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001509 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001510 }
1511
H. Peter Anvine2c80182005-01-15 22:15:51 +00001512 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001513 bool rn_error;
1514 tokval->t_integer = readnum(tline->text, &rn_error);
1515 tokval->t_charptr = tline->text;
1516 if (rn_error)
1517 return tokval->t_type = TOKEN_ERRNUM;
1518 else
1519 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001520 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001521
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001522 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001523 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001524 }
1525
H. Peter Anvine2c80182005-01-15 22:15:51 +00001526 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001527 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001528
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001529 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001530 tokval->t_charptr = tline->text;
1531 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001532
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001533 if (ep[0] != bq || ep[1] != '\0')
1534 return tokval->t_type = TOKEN_ERRSTR;
1535 else
1536 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001537 }
1538
H. Peter Anvine2c80182005-01-15 22:15:51 +00001539 if (tline->type == TOK_OTHER) {
1540 if (!strcmp(tline->text, "<<"))
1541 return tokval->t_type = TOKEN_SHL;
1542 if (!strcmp(tline->text, ">>"))
1543 return tokval->t_type = TOKEN_SHR;
1544 if (!strcmp(tline->text, "//"))
1545 return tokval->t_type = TOKEN_SDIV;
1546 if (!strcmp(tline->text, "%%"))
1547 return tokval->t_type = TOKEN_SMOD;
1548 if (!strcmp(tline->text, "=="))
1549 return tokval->t_type = TOKEN_EQ;
1550 if (!strcmp(tline->text, "<>"))
1551 return tokval->t_type = TOKEN_NE;
1552 if (!strcmp(tline->text, "!="))
1553 return tokval->t_type = TOKEN_NE;
1554 if (!strcmp(tline->text, "<="))
1555 return tokval->t_type = TOKEN_LE;
1556 if (!strcmp(tline->text, ">="))
1557 return tokval->t_type = TOKEN_GE;
1558 if (!strcmp(tline->text, "&&"))
1559 return tokval->t_type = TOKEN_DBL_AND;
1560 if (!strcmp(tline->text, "^^"))
1561 return tokval->t_type = TOKEN_DBL_XOR;
1562 if (!strcmp(tline->text, "||"))
1563 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001564 }
1565
1566 /*
1567 * We have no other options: just return the first character of
1568 * the token text.
1569 */
1570 return tokval->t_type = tline->text[0];
1571}
1572
1573/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001574 * Compare a string to the name of an existing macro; this is a
1575 * simple wrapper which calls either strcmp or nasm_stricmp
1576 * depending on the value of the `casesense' parameter.
1577 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001578static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001579{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001580 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001581}
1582
1583/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001584 * Compare a string to the name of an existing macro; this is a
1585 * simple wrapper which calls either strcmp or nasm_stricmp
1586 * depending on the value of the `casesense' parameter.
1587 */
1588static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1589{
1590 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1591}
1592
1593/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001594 * Return the Context structure associated with a %$ token. Return
1595 * NULL, having _already_ reported an error condition, if the
1596 * context stack isn't deep enough for the supplied number of $
1597 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001598 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001599 * also scanned for such smacro, until it is found; if not -
1600 * only the context that directly results from the number of $'s
1601 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001602 *
1603 * If "namep" is non-NULL, set it to the pointer to the macro name
1604 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001605 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001606static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001607 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001608{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001609 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001610 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001611 int i;
1612
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001613 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001614 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001615
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001616 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001618
H. Peter Anvine2c80182005-01-15 22:15:51 +00001619 if (!cstk) {
1620 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1621 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001622 }
1623
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001624 name += 2;
1625 ctx = cstk;
1626 i = 0;
1627 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001628 name++;
1629 i++;
1630 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001631 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001632 if (!ctx) {
1633 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001634 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001636 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001637
1638 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001639 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001640
Keith Kanios404589e2010-08-10 20:12:57 -05001641 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001642 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001643
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001644 do {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001646 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001647 while (m) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001648 if (!mstrcmp(m->name, name, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 return ctx;
1650 m = m->next;
1651 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001652 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001653 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001654 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001655 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001656}
1657
1658/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001659 * Check to see if a file is already in a string list
1660 */
1661static bool in_list(const StrList *list, const char *str)
1662{
1663 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001664 if (!strcmp(list->str, str))
1665 return true;
1666 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001667 }
1668 return false;
1669}
1670
1671/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001672 * Open an include file. This routine must always return a valid
1673 * file pointer if it returns - it's responsible for throwing an
1674 * ERR_FATAL and bombing out completely if not. It should also try
1675 * the include path one by one until it finds the file or reaches
1676 * the end of the path.
1677 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001678static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001679 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001680{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001681 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001682 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001683 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001684 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001685 size_t prefix_len = 0;
1686 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001687
H. Peter Anvine2c80182005-01-15 22:15:51 +00001688 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001689 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunov6f38fe62010-11-11 11:32:16 +03001690 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001691 memcpy(sl->str, prefix, prefix_len);
1692 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001693 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001694 if (fp && dhead && !in_list(*dhead, sl->str)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001695 **dtail = sl;
1696 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001697 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001698 nasm_free(sl);
1699 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001700 if (fp)
1701 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001702 if (!ip) {
1703 if (!missing_ok)
1704 break;
1705 prefix = NULL;
1706 } else {
1707 prefix = ip->path;
1708 ip = ip->next;
1709 }
1710 if (prefix) {
1711 prefix_len = strlen(prefix);
1712 } else {
1713 /* -MG given and file not found */
1714 if (dhead && !in_list(*dhead, file)) {
1715 sl = nasm_malloc(len+1+sizeof sl->next);
1716 sl->next = NULL;
1717 strcpy(sl->str, file);
1718 **dtail = sl;
1719 *dtail = &sl->next;
1720 }
1721 return NULL;
1722 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001723 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001724
H. Peter Anvin734b1882002-04-30 21:01:08 +00001725 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001726 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001727}
1728
1729/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001730 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001731 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001732 * return true if _any_ single-line macro of that name is defined.
1733 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001734 * `nparam' or no parameters is defined.
1735 *
1736 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001737 * defined, or nparam is -1, the address of the definition structure
1738 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001739 * is NULL, no action will be taken regarding its contents, and no
1740 * error will occur.
1741 *
1742 * Note that this is also called with nparam zero to resolve
1743 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001744 *
1745 * If you already know which context macro belongs to, you can pass
1746 * the context pointer as first parameter; if you won't but name begins
1747 * with %$ the context will be automatically computed. If all_contexts
1748 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001749 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001750static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001751smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001752 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001753{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001754 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001755 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001756
H. Peter Anvin97a23472007-09-16 17:57:25 -07001757 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001758 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001759 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001760 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001761 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001762 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001763 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001764 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001765 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001766 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001767 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001768 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001769
H. Peter Anvine2c80182005-01-15 22:15:51 +00001770 while (m) {
1771 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001772 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001773 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001774 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001775 *defn = m;
1776 else
1777 *defn = NULL;
1778 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001779 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001780 }
1781 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001782 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001783
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001784 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001785}
1786
1787/*
1788 * Count and mark off the parameters in a multi-line macro call.
1789 * This is called both from within the multi-line macro expansion
1790 * code, and also to mark off the default parameters when provided
1791 * in a %macro definition line.
1792 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001793static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001794{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001795 int paramsize, brace;
1796
1797 *nparam = paramsize = 0;
1798 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001799 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001800 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001801 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 paramsize += PARAM_DELTA;
1803 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1804 }
1805 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001806 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001807 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001808 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809 (*params)[(*nparam)++] = t;
1810 while (tok_isnt_(t, brace ? "}" : ","))
1811 t = t->next;
1812 if (t) { /* got a comma/brace */
1813 t = t->next;
1814 if (brace) {
1815 /*
1816 * Now we've found the closing brace, look further
1817 * for the comma.
1818 */
1819 skip_white_(t);
1820 if (tok_isnt_(t, ",")) {
1821 error(ERR_NONFATAL,
1822 "braces do not enclose all of macro parameter");
1823 while (tok_isnt_(t, ","))
1824 t = t->next;
1825 }
1826 if (t)
1827 t = t->next; /* eat the comma */
1828 }
1829 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001830 }
1831}
1832
1833/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001834 * Determine whether one of the various `if' conditions is true or
1835 * not.
1836 *
1837 * We must free the tline we get passed.
1838 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001839static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001840{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001841 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001842 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001843 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001844 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001845 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001846 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001847 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001848
1849 origline = tline;
1850
H. Peter Anvine2c80182005-01-15 22:15:51 +00001851 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001852 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001853 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001854 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001855 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001856 if (!tline)
1857 break;
1858 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001859 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001860 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001861 free_tlist(origline);
1862 return -1;
1863 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001864 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001865 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001866 tline = tline->next;
1867 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001868 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001869
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001870 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001871 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001872 while (tline) {
1873 skip_white_(tline);
1874 if (!tline || (tline->type != TOK_ID &&
1875 (tline->type != TOK_PREPROC_ID ||
1876 tline->text[1] != '$'))) {
1877 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001878 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001879 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001880 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001881 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001882 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001883 tline = tline->next;
1884 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001885 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001886
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001887 case PPC_IFENV:
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001888 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001889 j = false; /* have we matched yet? */
1890 while (tline) {
1891 skip_white_(tline);
1892 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001893 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001894 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001895 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001896 error(ERR_NONFATAL,
1897 "`%s' expects environment variable names",
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001898 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001899 goto fail;
1900 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001901 p = tline->text;
1902 if (tline->type == TOK_PREPROC_ID)
1903 p += 2; /* Skip leading %! */
1904 if (*p == '\'' || *p == '\"' || *p == '`')
1905 nasm_unquote_cstr(p, ct);
1906 if (getenv(p))
1907 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001908 tline = tline->next;
1909 }
1910 break;
1911
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001912 case PPC_IFIDN:
1913 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001914 tline = expand_smacro(tline);
1915 t = tt = tline;
1916 while (tok_isnt_(tt, ","))
1917 tt = tt->next;
1918 if (!tt) {
1919 error(ERR_NONFATAL,
1920 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001921 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001922 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001923 }
1924 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001925 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001926 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1927 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1928 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001929 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001930 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001931 }
1932 if (t->type == TOK_WHITESPACE) {
1933 t = t->next;
1934 continue;
1935 }
1936 if (tt->type == TOK_WHITESPACE) {
1937 tt = tt->next;
1938 continue;
1939 }
1940 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001941 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001942 break;
1943 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001944 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001945 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001946 size_t l1 = nasm_unquote(t->text, NULL);
1947 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001948
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001949 if (l1 != l2) {
1950 j = false;
1951 break;
1952 }
1953 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1954 j = false;
1955 break;
1956 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001957 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001958 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001959 break;
1960 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001961
H. Peter Anvine2c80182005-01-15 22:15:51 +00001962 t = t->next;
1963 tt = tt->next;
1964 }
1965 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001966 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001967 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001968
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001969 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001970 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001971 bool found = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001972 ExpDef searching, *ed;
H. Peter Anvin65747262002-05-07 00:10:05 +00001973
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001974 skip_white_(tline);
1975 tline = expand_id(tline);
1976 if (!tok_type_(tline, TOK_ID)) {
1977 error(ERR_NONFATAL,
1978 "`%s' expects a macro name", pp_directives[ct]);
1979 goto fail;
1980 }
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03001981 memset(&searching, 0, sizeof(searching));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001982 searching.name = nasm_strdup(tline->text);
1983 searching.casesense = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001984 searching.nparam_max = INT_MAX;
1985 tline = expand_smacro(tline->next);
1986 skip_white_(tline);
1987 if (!tline) {
1988 } else if (!tok_type_(tline, TOK_NUMBER)) {
1989 error(ERR_NONFATAL,
1990 "`%s' expects a parameter count or nothing",
1991 pp_directives[ct]);
1992 } else {
1993 searching.nparam_min = searching.nparam_max =
1994 readnum(tline->text, &j);
1995 if (j)
1996 error(ERR_NONFATAL,
1997 "unable to parse parameter count `%s'",
1998 tline->text);
1999 }
2000 if (tline && tok_is_(tline->next, "-")) {
2001 tline = tline->next->next;
2002 if (tok_is_(tline, "*"))
2003 searching.nparam_max = INT_MAX;
2004 else if (!tok_type_(tline, TOK_NUMBER))
2005 error(ERR_NONFATAL,
2006 "`%s' expects a parameter count after `-'",
2007 pp_directives[ct]);
2008 else {
2009 searching.nparam_max = readnum(tline->text, &j);
2010 if (j)
2011 error(ERR_NONFATAL,
2012 "unable to parse parameter count `%s'",
2013 tline->text);
2014 if (searching.nparam_min > searching.nparam_max)
2015 error(ERR_NONFATAL,
2016 "minimum parameter count exceeds maximum");
2017 }
2018 }
2019 if (tline && tok_is_(tline->next, "+")) {
2020 tline = tline->next;
2021 searching.plus = true;
2022 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002023 ed = (ExpDef *) hash_findix(&expdefs, searching.name);
2024 while (ed != NULL) {
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002025 if (!strcmp(ed->name, searching.name) &&
2026 (ed->nparam_min <= searching.nparam_max || searching.plus) &&
2027 (searching.nparam_min <= ed->nparam_max || ed->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002028 found = true;
2029 break;
2030 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002031 ed = ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002032 }
2033 if (tline && tline->next)
2034 error(ERR_WARNING|ERR_PASS1,
2035 "trailing garbage after %%ifmacro ignored");
2036 nasm_free(searching.name);
2037 j = found;
2038 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002039 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002040
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002041 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002042 needtype = TOK_ID;
2043 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002044 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002045 needtype = TOK_NUMBER;
2046 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002047 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002048 needtype = TOK_STRING;
2049 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002050
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002051iftype:
2052 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002053
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002054 while (tok_type_(t, TOK_WHITESPACE) ||
2055 (needtype == TOK_NUMBER &&
2056 tok_type_(t, TOK_OTHER) &&
2057 (t->text[0] == '-' || t->text[0] == '+') &&
2058 !t->text[1]))
2059 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002060
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002061 j = tok_type_(t, needtype);
2062 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002063
2064 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002065 t = tline = expand_smacro(tline);
2066 while (tok_type_(t, TOK_WHITESPACE))
2067 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002068
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002069 j = false;
2070 if (t) {
2071 t = t->next; /* Skip the actual token */
2072 while (tok_type_(t, TOK_WHITESPACE))
2073 t = t->next;
2074 j = !t; /* Should be nothing left */
2075 }
2076 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002077
H. Peter Anvin134b9462008-02-16 17:01:40 -08002078 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002079 t = tline = expand_smacro(tline);
2080 while (tok_type_(t, TOK_WHITESPACE))
2081 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002082
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002083 j = !t; /* Should be empty */
2084 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002085
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002086 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002087 t = tline = expand_smacro(tline);
2088 tptr = &t;
2089 tokval.t_type = TOKEN_INVALID;
2090 evalresult = evaluate(ppscan, tptr, &tokval,
2091 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002092 if (!evalresult)
2093 return -1;
2094 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002095 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002096 "trailing garbage after expression ignored");
2097 if (!is_simple(evalresult)) {
2098 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002099 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002100 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002101 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002102 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002103 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002104
H. Peter Anvine2c80182005-01-15 22:15:51 +00002105 default:
2106 error(ERR_FATAL,
2107 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002108 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002109 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002110 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002111
2112 free_tlist(origline);
2113 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002114
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002115fail:
2116 free_tlist(origline);
2117 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002118}
2119
2120/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002121 * Common code for defining an smacro
2122 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002123static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002124 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002125{
2126 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002127 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002128
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002129 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002130 if (!smac) {
2131 error(ERR_WARNING|ERR_PASS1,
2132 "single-line macro `%s' defined both with and"
2133 " without parameters", mname);
2134 /*
2135 * Some instances of the old code considered this a failure,
2136 * some others didn't. What is the right thing to do here?
2137 */
2138 free_tlist(expansion);
2139 return false; /* Failure */
2140 } else {
2141 /*
2142 * We're redefining, so we have to take over an
2143 * existing SMacro structure. This means freeing
2144 * what was already in it.
2145 */
2146 nasm_free(smac->name);
2147 free_tlist(smac->expansion);
2148 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002149 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002150 smtbl = ctx ? &ctx->localmac : &smacros;
2151 smhead = (SMacro **) hash_findi_add(smtbl, mname);
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002152 smac = nasm_zalloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002153 smac->next = *smhead;
2154 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002155 }
2156 smac->name = nasm_strdup(mname);
2157 smac->casesense = casesense;
2158 smac->nparam = nparam;
2159 smac->expansion = expansion;
2160 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002161 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002162}
2163
2164/*
2165 * Undefine an smacro
2166 */
2167static void undef_smacro(Context *ctx, const char *mname)
2168{
2169 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002170 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002171
H. Peter Anvin166c2472008-05-28 12:28:58 -07002172 smtbl = ctx ? &ctx->localmac : &smacros;
2173 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002174
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002175 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002176 /*
2177 * We now have a macro name... go hunt for it.
2178 */
2179 sp = smhead;
2180 while ((s = *sp) != NULL) {
2181 if (!mstrcmp(s->name, mname, s->casesense)) {
2182 *sp = s->next;
2183 nasm_free(s->name);
2184 free_tlist(s->expansion);
2185 nasm_free(s);
2186 } else {
2187 sp = &s->next;
2188 }
2189 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002190 }
2191}
2192
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002193/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002194 * Parse a mmacro specification.
2195 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002196static bool parse_mmacro_spec(Token *tline, ExpDef *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002197{
2198 bool err;
2199
2200 tline = tline->next;
2201 skip_white_(tline);
2202 tline = expand_id(tline);
2203 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002204 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2205 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002206 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002207
H. Peter Anvina26433d2008-07-16 14:40:01 -07002208 def->name = nasm_strdup(tline->text);
2209 def->plus = false;
2210 def->nolist = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002211// def->in_progress = 0;
2212// def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002213 def->nparam_min = 0;
2214 def->nparam_max = 0;
2215
H. Peter Anvina26433d2008-07-16 14:40:01 -07002216 tline = expand_smacro(tline->next);
2217 skip_white_(tline);
2218 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002219 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002220 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002221 def->nparam_min = def->nparam_max =
2222 readnum(tline->text, &err);
2223 if (err)
2224 error(ERR_NONFATAL,
2225 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002226 }
2227 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002228 tline = tline->next->next;
2229 if (tok_is_(tline, "*")) {
2230 def->nparam_max = INT_MAX;
2231 } else if (!tok_type_(tline, TOK_NUMBER)) {
2232 error(ERR_NONFATAL,
2233 "`%s' expects a parameter count after `-'", directive);
2234 } else {
2235 def->nparam_max = readnum(tline->text, &err);
2236 if (err) {
2237 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2238 tline->text);
2239 }
2240 if (def->nparam_min > def->nparam_max) {
2241 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2242 }
2243 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002244 }
2245 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002246 tline = tline->next;
2247 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002248 }
2249 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002250 !nasm_stricmp(tline->next->text, ".nolist")) {
2251 tline = tline->next;
2252 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002253 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002254
H. Peter Anvina26433d2008-07-16 14:40:01 -07002255 /*
2256 * Handle default parameters.
2257 */
2258 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002259 def->dlist = tline->next;
2260 tline->next = NULL;
2261 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002262 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002263 def->dlist = NULL;
2264 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002265 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002266 def->line = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002267
H. Peter Anvin89cee572009-07-15 09:16:54 -04002268 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002269 !def->plus)
2270 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2271 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002272
H. Peter Anvina26433d2008-07-16 14:40:01 -07002273 return true;
2274}
2275
2276
2277/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002278 * Decode a size directive
2279 */
2280static int parse_size(const char *str) {
2281 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002282 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002283 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002284 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002285
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002286 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002287}
2288
Ed Beroset3ab3f412002-06-11 03:31:49 +00002289/**
2290 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002291 * Find out if a line contains a preprocessor directive, and deal
2292 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002293 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002294 * If a directive _is_ found, it is the responsibility of this routine
2295 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002296 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002297 * @param tline a pointer to the current tokeninzed line linked list
2298 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002299 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002300 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002301static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002302{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002303 enum preproc_token i;
2304 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002305 bool err;
2306 int nparam;
2307 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002308 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002309 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002310 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002311 char *p, *pp;
2312 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002313 Include *inc;
2314 Context *ctx;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002315 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002316 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002317 struct tokenval tokval;
2318 expr *evalresult;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002319 ExpDef *ed, *eed, **edhead;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002320 ExpInv *ei, *eei;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002321 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002322 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002323 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002324
2325 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002326
H. Peter Anvineba20a72002-04-30 20:53:55 +00002327 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002328 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002329 (tline->text[1] == '%' || tline->text[1] == '$'
2330 || tline->text[1] == '!'))
2331 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002332
H. Peter Anvin4169a472007-09-12 01:29:43 +00002333 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002334
H. Peter Anvin4169a472007-09-12 01:29:43 +00002335 switch (i) {
2336 case PP_INVALID:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002337 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002338 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2339 tline->text);
2340 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002341
H. Peter Anvine2c80182005-01-15 22:15:51 +00002342 case PP_STACKSIZE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002343 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002344 /* Directive to tell NASM what the default stack size is. The
2345 * default is for a 16-bit stack, and this can be overriden with
2346 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002347 */
2348 tline = tline->next;
2349 if (tline && tline->type == TOK_WHITESPACE)
2350 tline = tline->next;
2351 if (!tline || tline->type != TOK_ID) {
2352 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2353 free_tlist(origline);
2354 return DIRECTIVE_FOUND;
2355 }
2356 if (nasm_stricmp(tline->text, "flat") == 0) {
2357 /* All subsequent ARG directives are for a 32-bit stack */
2358 StackSize = 4;
2359 StackPointer = "ebp";
2360 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002361 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002362 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2363 /* All subsequent ARG directives are for a 64-bit stack */
2364 StackSize = 8;
2365 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002366 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002367 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002368 } else if (nasm_stricmp(tline->text, "large") == 0) {
2369 /* All subsequent ARG directives are for a 16-bit stack,
2370 * far function call.
2371 */
2372 StackSize = 2;
2373 StackPointer = "bp";
2374 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002375 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002376 } else if (nasm_stricmp(tline->text, "small") == 0) {
2377 /* All subsequent ARG directives are for a 16-bit stack,
2378 * far function call. We don't support near functions.
2379 */
2380 StackSize = 2;
2381 StackPointer = "bp";
2382 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002383 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 } else {
2385 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2386 free_tlist(origline);
2387 return DIRECTIVE_FOUND;
2388 }
2389 free_tlist(origline);
2390 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002391
H. Peter Anvine2c80182005-01-15 22:15:51 +00002392 case PP_ARG:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002393 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002394 /* TASM like ARG directive to define arguments to functions, in
2395 * the following form:
2396 *
2397 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2398 */
2399 offset = ArgOffset;
2400 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002401 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002402 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002403
H. Peter Anvine2c80182005-01-15 22:15:51 +00002404 /* Find the argument name */
2405 tline = tline->next;
2406 if (tline && tline->type == TOK_WHITESPACE)
2407 tline = tline->next;
2408 if (!tline || tline->type != TOK_ID) {
2409 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2410 free_tlist(origline);
2411 return DIRECTIVE_FOUND;
2412 }
2413 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002414
H. Peter Anvine2c80182005-01-15 22:15:51 +00002415 /* Find the argument size type */
2416 tline = tline->next;
2417 if (!tline || tline->type != TOK_OTHER
2418 || tline->text[0] != ':') {
2419 error(ERR_NONFATAL,
2420 "Syntax error processing `%%arg' directive");
2421 free_tlist(origline);
2422 return DIRECTIVE_FOUND;
2423 }
2424 tline = tline->next;
2425 if (!tline || tline->type != TOK_ID) {
2426 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2427 free_tlist(origline);
2428 return DIRECTIVE_FOUND;
2429 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002430
H. Peter Anvine2c80182005-01-15 22:15:51 +00002431 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002432 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002433 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002434 size = parse_size(tt->text);
2435 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002436 error(ERR_NONFATAL,
2437 "Invalid size type for `%%arg' missing directive");
2438 free_tlist(tt);
2439 free_tlist(origline);
2440 return DIRECTIVE_FOUND;
2441 }
2442 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002443
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002444 /* Round up to even stack slots */
2445 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002446
H. Peter Anvine2c80182005-01-15 22:15:51 +00002447 /* Now define the macro for the argument */
2448 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2449 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002450 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002451 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002452
H. Peter Anvine2c80182005-01-15 22:15:51 +00002453 /* Move to the next argument in the list */
2454 tline = tline->next;
2455 if (tline && tline->type == TOK_WHITESPACE)
2456 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002457 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002458 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002459 free_tlist(origline);
2460 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002461
H. Peter Anvine2c80182005-01-15 22:15:51 +00002462 case PP_LOCAL:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002463 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002464 /* TASM like LOCAL directive to define local variables for a
2465 * function, in the following form:
2466 *
2467 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2468 *
2469 * The '= LocalSize' at the end is ignored by NASM, but is
2470 * required by TASM to define the local parameter size (and used
2471 * by the TASM macro package).
2472 */
2473 offset = LocalOffset;
2474 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002475 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002476 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002477
H. Peter Anvine2c80182005-01-15 22:15:51 +00002478 /* Find the argument name */
2479 tline = tline->next;
2480 if (tline && tline->type == TOK_WHITESPACE)
2481 tline = tline->next;
2482 if (!tline || tline->type != TOK_ID) {
2483 error(ERR_NONFATAL,
2484 "`%%local' missing argument parameter");
2485 free_tlist(origline);
2486 return DIRECTIVE_FOUND;
2487 }
2488 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002489
H. Peter Anvine2c80182005-01-15 22:15:51 +00002490 /* Find the argument size type */
2491 tline = tline->next;
2492 if (!tline || tline->type != TOK_OTHER
2493 || tline->text[0] != ':') {
2494 error(ERR_NONFATAL,
2495 "Syntax error processing `%%local' directive");
2496 free_tlist(origline);
2497 return DIRECTIVE_FOUND;
2498 }
2499 tline = tline->next;
2500 if (!tline || tline->type != TOK_ID) {
2501 error(ERR_NONFATAL,
2502 "`%%local' missing size type parameter");
2503 free_tlist(origline);
2504 return DIRECTIVE_FOUND;
2505 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002506
H. Peter Anvine2c80182005-01-15 22:15:51 +00002507 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002508 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002509 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002510 size = parse_size(tt->text);
2511 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002512 error(ERR_NONFATAL,
2513 "Invalid size type for `%%local' missing directive");
2514 free_tlist(tt);
2515 free_tlist(origline);
2516 return DIRECTIVE_FOUND;
2517 }
2518 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002519
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002520 /* Round up to even stack slots */
2521 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002522
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002523 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002524
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002525 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002526 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2527 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002528 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002529
H. Peter Anvine2c80182005-01-15 22:15:51 +00002530 /* Now define the assign to setup the enter_c macro correctly */
2531 snprintf(directive, sizeof(directive),
2532 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002533 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002534
H. Peter Anvine2c80182005-01-15 22:15:51 +00002535 /* Move to the next argument in the list */
2536 tline = tline->next;
2537 if (tline && tline->type == TOK_WHITESPACE)
2538 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002539 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002540 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002541 free_tlist(origline);
2542 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002543
H. Peter Anvine2c80182005-01-15 22:15:51 +00002544 case PP_CLEAR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002545 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002546 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002547 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002548 "trailing garbage after `%%clear' ignored");
2549 free_macros();
2550 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002551 free_tlist(origline);
2552 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002553
H. Peter Anvin418ca702008-05-30 10:42:30 -07002554 case PP_DEPEND:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002555 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002556 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002557 skip_white_(t);
2558 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002559 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002560 error(ERR_NONFATAL, "`%%depend' expects a file name");
2561 free_tlist(origline);
2562 return DIRECTIVE_FOUND; /* but we did _something_ */
2563 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002564 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002565 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002566 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002567 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002568 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002569 nasm_unquote_cstr(p, i);
2570 if (dephead && !in_list(*dephead, p)) {
2571 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2572 sl->next = NULL;
2573 strcpy(sl->str, p);
2574 *deptail = sl;
2575 deptail = &sl->next;
2576 }
2577 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002578 return DIRECTIVE_FOUND;
2579
2580 case PP_INCLUDE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002581 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002582 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002583 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002584
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002585 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002586 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002587 error(ERR_NONFATAL, "`%%include' expects a file name");
2588 free_tlist(origline);
2589 return DIRECTIVE_FOUND; /* but we did _something_ */
2590 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002591 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002592 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002593 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002594 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002595 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002596 nasm_unquote_cstr(p, i);
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03002597 inc = nasm_zalloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002598 inc->next = istk;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002599 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002600 if (!inc->fp) {
2601 /* -MG given but file not found */
2602 nasm_free(inc);
2603 } else {
2604 inc->fname = src_set_fname(nasm_strdup(p));
2605 inc->lineno = src_set_linnum(0);
2606 inc->lineinc = 1;
2607 inc->expansion = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002608 istk = inc;
2609 list->uplevel(LIST_INCLUDE);
2610 }
2611 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002612 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002613
H. Peter Anvind2456592008-06-19 15:04:18 -07002614 case PP_USE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002615 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002616 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002617 static macros_t *use_pkg;
2618 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002619
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002620 tline = tline->next;
2621 skip_white_(tline);
2622 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002623
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002624 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002625 tline->type != TOK_INTERNAL_STRING &&
2626 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002627 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002628 free_tlist(origline);
2629 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002630 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002631 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002632 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002633 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002634 if (tline->type == TOK_STRING)
2635 nasm_unquote_cstr(tline->text, i);
2636 use_pkg = nasm_stdmac_find_package(tline->text);
2637 if (!use_pkg)
2638 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2639 else
2640 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002641 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002642 /* Not already included, go ahead and include it */
2643 stdmacpos = use_pkg;
2644 }
2645 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002646 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002647 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002648 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002649 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002650 case PP_POP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002651 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002652 tline = tline->next;
2653 skip_white_(tline);
2654 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002655 if (tline) {
2656 if (!tok_type_(tline, TOK_ID)) {
2657 error(ERR_NONFATAL, "`%s' expects a context identifier",
2658 pp_directives[i]);
2659 free_tlist(origline);
2660 return DIRECTIVE_FOUND; /* but we did _something_ */
2661 }
2662 if (tline->next)
2663 error(ERR_WARNING|ERR_PASS1,
2664 "trailing garbage after `%s' ignored",
2665 pp_directives[i]);
2666 p = nasm_strdup(tline->text);
2667 } else {
2668 p = NULL; /* Anonymous */
2669 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002670
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002671 if (i == PP_PUSH) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002672 ctx = nasm_zalloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002673 ctx->next = cstk;
2674 hash_init(&ctx->localmac, HASH_SMALL);
2675 ctx->name = p;
2676 ctx->number = unique++;
2677 cstk = ctx;
2678 } else {
2679 /* %pop or %repl */
2680 if (!cstk) {
2681 error(ERR_NONFATAL, "`%s': context stack is empty",
2682 pp_directives[i]);
2683 } else if (i == PP_POP) {
2684 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2685 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2686 "expected %s",
2687 cstk->name ? cstk->name : "anonymous", p);
2688 else
2689 ctx_pop();
2690 } else {
2691 /* i == PP_REPL */
2692 nasm_free(cstk->name);
2693 cstk->name = p;
2694 p = NULL;
2695 }
2696 nasm_free(p);
2697 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002698 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002699 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002700 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002701 severity = ERR_FATAL;
2702 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002703 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002704 severity = ERR_NONFATAL;
2705 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002706 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002707 severity = ERR_WARNING|ERR_WARN_USER;
2708 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002709
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002710issue_error:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002711 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002712 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002713 /* Only error out if this is the final pass */
2714 if (pass != 2 && i != PP_FATAL)
2715 return DIRECTIVE_FOUND;
2716
2717 tline->next = expand_smacro(tline->next);
2718 tline = tline->next;
2719 skip_white_(tline);
2720 t = tline ? tline->next : NULL;
2721 skip_white_(t);
2722 if (tok_type_(tline, TOK_STRING) && !t) {
2723 /* The line contains only a quoted string */
2724 p = tline->text;
2725 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2726 error(severity, "%s", p);
2727 } else {
2728 /* Not a quoted string, or more than a quoted string */
2729 p = detoken(tline, false);
2730 error(severity, "%s", p);
2731 nasm_free(p);
2732 }
2733 free_tlist(origline);
2734 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002735 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002736
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002737 CASE_PP_IF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002738 if (defining != NULL) {
2739 if (defining->type == EXP_IF) {
2740 defining->def_depth ++;
2741 }
2742 return NO_DIRECTIVE_FOUND;
2743 }
2744 if ((istk->expansion != NULL) &&
2745 (istk->expansion->emitting == false)) {
2746 j = COND_NEVER;
2747 } else {
2748 j = if_condition(tline->next, i);
2749 tline->next = NULL; /* it got freed */
2750 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
2751 }
2752 ed = new_ExpDef(EXP_IF);
2753 ed->state = j;
2754 ed->nolist = NULL;
2755 ed->def_depth = 0;
2756 ed->cur_depth = 0;
2757 ed->max_depth = 0;
2758 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
2759 ed->prev = defining;
2760 defining = ed;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002761 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002762 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002763
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002764 CASE_PP_ELIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002765 if (defining != NULL) {
2766 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2767 return NO_DIRECTIVE_FOUND;
2768 }
2769 }
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +04002770 if ((defining == NULL) || (defining->type != EXP_IF)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002771 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2772 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002773 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002774 case COND_IF_TRUE:
2775 defining->state = COND_DONE;
2776 defining->ignoring = true;
2777 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002778
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002779 case COND_DONE:
2780 case COND_NEVER:
2781 defining->ignoring = true;
2782 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002783
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002784 case COND_ELSE_TRUE:
2785 case COND_ELSE_FALSE:
2786 error_precond(ERR_WARNING|ERR_PASS1,
2787 "`%%elif' after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002788 defining->state = COND_NEVER;
2789 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002790 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002791
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002792 case COND_IF_FALSE:
2793 /*
2794 * IMPORTANT: In the case of %if, we will already have
2795 * called expand_mmac_params(); however, if we're
2796 * processing an %elif we must have been in a
2797 * non-emitting mode, which would have inhibited
2798 * the normal invocation of expand_mmac_params().
2799 * Therefore, we have to do it explicitly here.
2800 */
2801 j = if_condition(expand_mmac_params(tline->next), i);
2802 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002803 defining->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002804 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002805 defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002806 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002807 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002808 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002809 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002810
H. Peter Anvine2c80182005-01-15 22:15:51 +00002811 case PP_ELSE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002812 if (defining != NULL) {
2813 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2814 return NO_DIRECTIVE_FOUND;
2815 }
2816 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002817 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002818 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002819 "trailing garbage after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002820 if ((defining == NULL) || (defining->type != EXP_IF)) {
2821 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2822 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002823 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002824 case COND_IF_TRUE:
2825 case COND_DONE:
2826 defining->state = COND_ELSE_FALSE;
2827 defining->ignoring = true;
2828 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002829
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002830 case COND_NEVER:
2831 defining->ignoring = true;
2832 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002833
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002834 case COND_IF_FALSE:
2835 defining->state = COND_ELSE_TRUE;
2836 defining->ignoring = false;
2837 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002838
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002839 case COND_ELSE_TRUE:
2840 case COND_ELSE_FALSE:
2841 error_precond(ERR_WARNING|ERR_PASS1,
2842 "`%%else' after `%%else' ignored.");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002843 defining->state = COND_NEVER;
2844 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002845 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002846 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002847 free_tlist(origline);
2848 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002849
H. Peter Anvine2c80182005-01-15 22:15:51 +00002850 case PP_ENDIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002851 if (defining != NULL) {
2852 if (defining->type == EXP_IF) {
2853 if (defining->def_depth > 0) {
2854 defining->def_depth --;
2855 return NO_DIRECTIVE_FOUND;
2856 }
2857 } else {
2858 return NO_DIRECTIVE_FOUND;
2859 }
2860 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002861 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002862 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002863 "trailing garbage after `%%endif' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002864 if ((defining == NULL) || (defining->type != EXP_IF)) {
2865 error(ERR_NONFATAL, "`%%endif': no matching `%%if'");
2866 return DIRECTIVE_FOUND;
2867 }
2868 ed = defining;
2869 defining = ed->prev;
2870 ed->prev = expansions;
2871 expansions = ed;
2872 ei = new_ExpInv(EXP_IF, ed);
2873 ei->current = ed->line;
2874 ei->emitting = true;
2875 ei->prev = istk->expansion;
2876 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002877 free_tlist(origline);
2878 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002879
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002880 case PP_RMACRO:
2881 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002882 case PP_MACRO:
2883 case PP_IMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002884 if (defining != NULL) {
2885 if (defining->type == EXP_MMACRO) {
2886 defining->def_depth ++;
2887 }
2888 return NO_DIRECTIVE_FOUND;
2889 }
2890 ed = new_ExpDef(EXP_MMACRO);
2891 ed->max_depth =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002892 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002893 ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002894 if (!parse_mmacro_spec(tline, ed, pp_directives[i])) {
2895 nasm_free(ed);
2896 ed = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002897 return DIRECTIVE_FOUND;
2898 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002899 ed->def_depth = 0;
2900 ed->cur_depth = 0;
2901 ed->max_depth = (ed->max_depth + 1);
2902 ed->ignoring = false;
2903 ed->prev = defining;
2904 defining = ed;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002905
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002906 eed = (ExpDef *) hash_findix(&expdefs, ed->name);
2907 while (eed) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002908 if (!strcmp(eed->name, ed->name) &&
2909 (eed->nparam_min <= ed->nparam_max || ed->plus) &&
2910 (ed->nparam_min <= eed->nparam_max || eed->plus)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002911 error(ERR_WARNING|ERR_PASS1,
2912 "redefining multi-line macro `%s'", ed->name);
2913 return DIRECTIVE_FOUND;
2914 }
2915 eed = eed->next;
2916 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002917 free_tlist(origline);
2918 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002919
H. Peter Anvine2c80182005-01-15 22:15:51 +00002920 case PP_ENDM:
2921 case PP_ENDMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002922 if (defining != NULL) {
2923 if (defining->type == EXP_MMACRO) {
2924 if (defining->def_depth > 0) {
2925 defining->def_depth --;
2926 return NO_DIRECTIVE_FOUND;
2927 }
2928 } else {
2929 return NO_DIRECTIVE_FOUND;
2930 }
2931 }
2932 if (!(defining) || (defining->type != EXP_MMACRO)) {
2933 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2934 return DIRECTIVE_FOUND;
2935 }
2936 edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name);
2937 defining->next = *edhead;
2938 *edhead = defining;
2939 ed = defining;
2940 defining = ed->prev;
2941 ed->prev = expansions;
2942 expansions = ed;
2943 ed = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002944 free_tlist(origline);
2945 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002946
H. Peter Anvin89cee572009-07-15 09:16:54 -04002947 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002948 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2949 /*
2950 * We must search along istk->expansion until we hit a
2951 * macro invocation. Then we disable the emitting state(s)
2952 * between exitmacro and endmacro.
2953 */
2954 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
2955 if(ei->type == EXP_MMACRO) {
2956 break;
2957 }
2958 }
2959
2960 if (ei != NULL) {
2961 /*
2962 * Set all invocations leading back to the macro
2963 * invocation to a non-emitting state.
2964 */
2965 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
2966 eei->emitting = false;
2967 }
2968 eei->emitting = false;
2969 } else {
2970 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2971 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002972 free_tlist(origline);
2973 return DIRECTIVE_FOUND;
2974
H. Peter Anvina26433d2008-07-16 14:40:01 -07002975 case PP_UNMACRO:
2976 case PP_UNIMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002977 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002978 {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002979 ExpDef **ed_p;
2980 ExpDef spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002981
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002982 spec.casesense = (i == PP_UNMACRO);
2983 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2984 return DIRECTIVE_FOUND;
2985 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002986 ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL);
2987 while (ed_p && *ed_p) {
2988 ed = *ed_p;
2989 if (ed->casesense == spec.casesense &&
2990 !mstrcmp(ed->name, spec.name, spec.casesense) &&
2991 ed->nparam_min == spec.nparam_min &&
2992 ed->nparam_max == spec.nparam_max &&
2993 ed->plus == spec.plus) {
Keith Kaniosc98a5b42010-12-18 12:17:31 -06002994 if (ed->cur_depth > 0) {
Keith Kanios21d885b2010-12-18 12:22:21 -06002995 error(ERR_NONFATAL, "`%s' ignored on active macro",
Keith Kaniosc98a5b42010-12-18 12:17:31 -06002996 pp_directives[i]);
2997 break;
2998 } else {
2999 *ed_p = ed->next;
3000 free_expdef(ed);
3001 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003002 } else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003003 ed_p = &ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003004 }
3005 }
3006 free_tlist(origline);
3007 free_tlist(spec.dlist);
3008 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003009 }
3010
H. Peter Anvine2c80182005-01-15 22:15:51 +00003011 case PP_ROTATE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003012 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003013 if (tline->next && tline->next->type == TOK_WHITESPACE)
3014 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003015 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003016 free_tlist(origline);
3017 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
3018 return DIRECTIVE_FOUND;
3019 }
3020 t = expand_smacro(tline->next);
3021 tline->next = NULL;
3022 free_tlist(origline);
3023 tline = t;
3024 tptr = &t;
3025 tokval.t_type = TOKEN_INVALID;
3026 evalresult =
3027 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3028 free_tlist(tline);
3029 if (!evalresult)
3030 return DIRECTIVE_FOUND;
3031 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003032 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003033 "trailing garbage after expression ignored");
3034 if (!is_simple(evalresult)) {
3035 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
3036 return DIRECTIVE_FOUND;
3037 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003038 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3039 if (ei->type == EXP_MMACRO) {
3040 break;
3041 }
3042 }
3043 if (ei == NULL) {
3044 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
3045 } else if (ei->nparam == 0) {
3046 error(ERR_NONFATAL,
3047 "`%%rotate' invoked within macro without parameters");
3048 } else {
3049 int rotate = ei->rotate + reloc_value(evalresult);
3050
3051 rotate %= (int)ei->nparam;
3052 if (rotate < 0)
3053 rotate += ei->nparam;
3054 ei->rotate = rotate;
3055 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003056 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003057
H. Peter Anvine2c80182005-01-15 22:15:51 +00003058 case PP_REP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003059 if (defining != NULL) {
3060 if (defining->type == EXP_REP) {
3061 defining->def_depth ++;
3062 }
3063 return NO_DIRECTIVE_FOUND;
3064 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003065 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003066 do {
3067 tline = tline->next;
3068 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003069
H. Peter Anvine2c80182005-01-15 22:15:51 +00003070 if (tok_type_(tline, TOK_ID) &&
3071 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003072 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003073 do {
3074 tline = tline->next;
3075 } while (tok_type_(tline, TOK_WHITESPACE));
3076 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003077
H. Peter Anvine2c80182005-01-15 22:15:51 +00003078 if (tline) {
3079 t = expand_smacro(tline);
3080 tptr = &t;
3081 tokval.t_type = TOKEN_INVALID;
3082 evalresult =
3083 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3084 if (!evalresult) {
3085 free_tlist(origline);
3086 return DIRECTIVE_FOUND;
3087 }
3088 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003089 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003090 "trailing garbage after expression ignored");
3091 if (!is_simple(evalresult)) {
3092 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
3093 return DIRECTIVE_FOUND;
3094 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003095 count = reloc_value(evalresult);
3096 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04003097 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003098 count = 0;
3099 } else
3100 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003101 } else {
3102 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003103 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003104 }
3105 free_tlist(origline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003106 ed = new_ExpDef(EXP_REP);
3107 ed->nolist = nolist;
3108 ed->def_depth = 0;
3109 ed->cur_depth = 1;
3110 ed->max_depth = (count - 1);
3111 ed->ignoring = false;
3112 ed->prev = defining;
3113 defining = ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003114 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003115
H. Peter Anvine2c80182005-01-15 22:15:51 +00003116 case PP_ENDREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003117 if (defining != NULL) {
3118 if (defining->type == EXP_REP) {
3119 if (defining->def_depth > 0) {
3120 defining->def_depth --;
3121 return NO_DIRECTIVE_FOUND;
3122 }
3123 } else {
3124 return NO_DIRECTIVE_FOUND;
3125 }
3126 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003127 if ((defining == NULL) || (defining->type != EXP_REP)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003128 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
3129 return DIRECTIVE_FOUND;
3130 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003131
H. Peter Anvine2c80182005-01-15 22:15:51 +00003132 /*
3133 * Now we have a "macro" defined - although it has no name
3134 * and we won't be entering it in the hash tables - we must
3135 * push a macro-end marker for it on to istk->expansion.
3136 * After that, it will take care of propagating itself (a
3137 * macro-end marker line for a macro which is really a %rep
3138 * block will cause the macro to be re-expanded, complete
3139 * with another macro-end marker to ensure the process
3140 * continues) until the whole expansion is forcibly removed
3141 * from istk->expansion by a %exitrep.
3142 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003143 ed = defining;
3144 defining = ed->prev;
3145 ed->prev = expansions;
3146 expansions = ed;
3147 ei = new_ExpInv(EXP_REP, ed);
3148 ei->current = ed->line;
3149 ei->emitting = ((ed->max_depth > 0) ? true : false);
3150 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3151 ei->prev = istk->expansion;
3152 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003153 free_tlist(origline);
3154 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003155
H. Peter Anvine2c80182005-01-15 22:15:51 +00003156 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003157 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3158 /*
3159 * We must search along istk->expansion until we hit a
3160 * rep invocation. Then we disable the emitting state(s)
3161 * between exitrep and endrep.
3162 */
3163 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3164 if (ei->type == EXP_REP) {
3165 break;
3166 }
3167 }
3168
3169 if (ei != NULL) {
3170 /*
3171 * Set all invocations leading back to the rep
3172 * invocation to a non-emitting state.
3173 */
3174 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3175 eei->emitting = false;
3176 }
3177 eei->emitting = false;
3178 eei->current = NULL;
3179 eei->def->cur_depth = eei->def->max_depth;
3180 } else {
3181 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3182 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003183 free_tlist(origline);
3184 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003185
H. Peter Anvine2c80182005-01-15 22:15:51 +00003186 case PP_XDEFINE:
3187 case PP_IXDEFINE:
3188 case PP_DEFINE:
3189 case PP_IDEFINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003190 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003191 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003192
H. Peter Anvine2c80182005-01-15 22:15:51 +00003193 tline = tline->next;
3194 skip_white_(tline);
3195 tline = expand_id(tline);
3196 if (!tline || (tline->type != TOK_ID &&
3197 (tline->type != TOK_PREPROC_ID ||
3198 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003199 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003200 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003201 free_tlist(origline);
3202 return DIRECTIVE_FOUND;
3203 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003204
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003205 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003206 last = tline;
3207 param_start = tline = tline->next;
3208 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003209
H. Peter Anvine2c80182005-01-15 22:15:51 +00003210 /* Expand the macro definition now for %xdefine and %ixdefine */
3211 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3212 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003213
H. Peter Anvine2c80182005-01-15 22:15:51 +00003214 if (tok_is_(tline, "(")) {
3215 /*
3216 * This macro has parameters.
3217 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003218
H. Peter Anvine2c80182005-01-15 22:15:51 +00003219 tline = tline->next;
3220 while (1) {
3221 skip_white_(tline);
3222 if (!tline) {
3223 error(ERR_NONFATAL, "parameter identifier expected");
3224 free_tlist(origline);
3225 return DIRECTIVE_FOUND;
3226 }
3227 if (tline->type != TOK_ID) {
3228 error(ERR_NONFATAL,
3229 "`%s': parameter identifier expected",
3230 tline->text);
3231 free_tlist(origline);
3232 return DIRECTIVE_FOUND;
3233 }
3234 tline->type = TOK_SMAC_PARAM + nparam++;
3235 tline = tline->next;
3236 skip_white_(tline);
3237 if (tok_is_(tline, ",")) {
3238 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003239 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003240 if (!tok_is_(tline, ")")) {
3241 error(ERR_NONFATAL,
3242 "`)' expected to terminate macro template");
3243 free_tlist(origline);
3244 return DIRECTIVE_FOUND;
3245 }
3246 break;
3247 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003248 }
3249 last = tline;
3250 tline = tline->next;
3251 }
3252 if (tok_type_(tline, TOK_WHITESPACE))
3253 last = tline, tline = tline->next;
3254 macro_start = NULL;
3255 last->next = NULL;
3256 t = tline;
3257 while (t) {
3258 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003259 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003260 if (tt->type >= TOK_SMAC_PARAM &&
3261 !strcmp(tt->text, t->text))
3262 t->type = tt->type;
3263 }
3264 tt = t->next;
3265 t->next = macro_start;
3266 macro_start = t;
3267 t = tt;
3268 }
3269 /*
3270 * Good. We now have a macro name, a parameter count, and a
3271 * token list (in reverse order) for an expansion. We ought
3272 * to be OK just to create an SMacro, store it, and let
3273 * free_tlist have the rest of the line (which we have
3274 * carefully re-terminated after chopping off the expansion
3275 * from the end).
3276 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003277 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003278 free_tlist(origline);
3279 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003280
H. Peter Anvine2c80182005-01-15 22:15:51 +00003281 case PP_UNDEF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003282 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003283 tline = tline->next;
3284 skip_white_(tline);
3285 tline = expand_id(tline);
3286 if (!tline || (tline->type != TOK_ID &&
3287 (tline->type != TOK_PREPROC_ID ||
3288 tline->text[1] != '$'))) {
3289 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3290 free_tlist(origline);
3291 return DIRECTIVE_FOUND;
3292 }
3293 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003294 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003295 "trailing garbage after macro name ignored");
3296 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003297
H. Peter Anvine2c80182005-01-15 22:15:51 +00003298 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003299 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003300 undef_smacro(ctx, mname);
3301 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003302 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003303
H. Peter Anvin9e200162008-06-04 17:23:14 -07003304 case PP_DEFSTR:
3305 case PP_IDEFSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003306 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003307 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003308
3309 tline = tline->next;
3310 skip_white_(tline);
3311 tline = expand_id(tline);
3312 if (!tline || (tline->type != TOK_ID &&
3313 (tline->type != TOK_PREPROC_ID ||
3314 tline->text[1] != '$'))) {
3315 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003316 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003317 free_tlist(origline);
3318 return DIRECTIVE_FOUND;
3319 }
3320
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003321 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003322 last = tline;
3323 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003324 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003325
3326 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003327 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003328
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003329 p = detoken(tline, false);
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003330 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003331 macro_start->text = nasm_quote(p, strlen(p));
3332 macro_start->type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003333 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003334
3335 /*
3336 * We now have a macro name, an implicit parameter count of
3337 * zero, and a string token to use as an expansion. Create
3338 * and store an SMacro.
3339 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003340 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003341 free_tlist(origline);
3342 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003343
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003344 case PP_DEFTOK:
3345 case PP_IDEFTOK:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003346 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003347 casesense = (i == PP_DEFTOK);
3348
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003349 tline = tline->next;
3350 skip_white_(tline);
3351 tline = expand_id(tline);
3352 if (!tline || (tline->type != TOK_ID &&
3353 (tline->type != TOK_PREPROC_ID ||
3354 tline->text[1] != '$'))) {
3355 error(ERR_NONFATAL,
3356 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003357 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003358 free_tlist(origline);
3359 return DIRECTIVE_FOUND;
3360 }
3361 ctx = get_ctx(tline->text, &mname, false);
3362 last = tline;
3363 tline = expand_smacro(tline->next);
3364 last->next = NULL;
3365
3366 t = tline;
3367 while (tok_type_(t, TOK_WHITESPACE))
3368 t = t->next;
3369 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003370 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003371 error(ERR_NONFATAL,
3372 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003373 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003374 free_tlist(tline);
3375 free_tlist(origline);
3376 return DIRECTIVE_FOUND;
3377 }
3378
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003379 /*
3380 * Convert the string to a token stream. Note that smacros
3381 * are stored with the token stream reversed, so we have to
3382 * reverse the output of tokenize().
3383 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003384 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003385 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003386
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003387 /*
3388 * We now have a macro name, an implicit parameter count of
3389 * zero, and a numeric token to use as an expansion. Create
3390 * and store an SMacro.
3391 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003392 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003393 free_tlist(tline);
3394 free_tlist(origline);
3395 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003396
H. Peter Anvin418ca702008-05-30 10:42:30 -07003397 case PP_PATHSEARCH:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003398 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003399 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003400 FILE *fp;
3401 StrList *xsl = NULL;
3402 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003403
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003404 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003405
3406 tline = tline->next;
3407 skip_white_(tline);
3408 tline = expand_id(tline);
3409 if (!tline || (tline->type != TOK_ID &&
3410 (tline->type != TOK_PREPROC_ID ||
3411 tline->text[1] != '$'))) {
3412 error(ERR_NONFATAL,
3413 "`%%pathsearch' expects a macro identifier as first parameter");
3414 free_tlist(origline);
3415 return DIRECTIVE_FOUND;
3416 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003417 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003418 last = tline;
3419 tline = expand_smacro(tline->next);
3420 last->next = NULL;
3421
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003422 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003423 while (tok_type_(t, TOK_WHITESPACE))
3424 t = t->next;
3425
3426 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003427 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003428 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003429 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003430 free_tlist(origline);
3431 return DIRECTIVE_FOUND; /* but we did _something_ */
3432 }
3433 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003434 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003435 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003436 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003437 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003438 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003439
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003440 fp = inc_fopen(p, &xsl, &xst, true);
3441 if (fp) {
3442 p = xsl->str;
3443 fclose(fp); /* Don't actually care about the file */
3444 }
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003445 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003446 macro_start->text = nasm_quote(p, strlen(p));
3447 macro_start->type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003448 if (xsl)
3449 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003450
3451 /*
3452 * We now have a macro name, an implicit parameter count of
3453 * zero, and a string token to use as an expansion. Create
3454 * and store an SMacro.
3455 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003456 define_smacro(ctx, mname, casesense, 0, macro_start);
3457 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003458 free_tlist(origline);
3459 return DIRECTIVE_FOUND;
3460 }
3461
H. Peter Anvine2c80182005-01-15 22:15:51 +00003462 case PP_STRLEN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003463 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003464 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003465
H. Peter Anvine2c80182005-01-15 22:15:51 +00003466 tline = tline->next;
3467 skip_white_(tline);
3468 tline = expand_id(tline);
3469 if (!tline || (tline->type != TOK_ID &&
3470 (tline->type != TOK_PREPROC_ID ||
3471 tline->text[1] != '$'))) {
3472 error(ERR_NONFATAL,
3473 "`%%strlen' expects a macro identifier as first parameter");
3474 free_tlist(origline);
3475 return DIRECTIVE_FOUND;
3476 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003477 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003478 last = tline;
3479 tline = expand_smacro(tline->next);
3480 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003481
H. Peter Anvine2c80182005-01-15 22:15:51 +00003482 t = tline;
3483 while (tok_type_(t, TOK_WHITESPACE))
3484 t = t->next;
3485 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003486 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003487 error(ERR_NONFATAL,
3488 "`%%strlen` requires string as second parameter");
3489 free_tlist(tline);
3490 free_tlist(origline);
3491 return DIRECTIVE_FOUND;
3492 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003493
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003494 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003495 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003496
H. Peter Anvine2c80182005-01-15 22:15:51 +00003497 /*
3498 * We now have a macro name, an implicit parameter count of
3499 * zero, and a numeric token to use as an expansion. Create
3500 * and store an SMacro.
3501 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003502 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003503 free_tlist(tline);
3504 free_tlist(origline);
3505 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003506
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003507 case PP_STRCAT:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003508 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003509 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003510
3511 tline = tline->next;
3512 skip_white_(tline);
3513 tline = expand_id(tline);
3514 if (!tline || (tline->type != TOK_ID &&
3515 (tline->type != TOK_PREPROC_ID ||
3516 tline->text[1] != '$'))) {
3517 error(ERR_NONFATAL,
3518 "`%%strcat' expects a macro identifier as first parameter");
3519 free_tlist(origline);
3520 return DIRECTIVE_FOUND;
3521 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003522 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003523 last = tline;
3524 tline = expand_smacro(tline->next);
3525 last->next = NULL;
3526
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003527 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003528 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003529 switch (t->type) {
3530 case TOK_WHITESPACE:
3531 break;
3532 case TOK_STRING:
3533 len += t->a.len = nasm_unquote(t->text, NULL);
3534 break;
3535 case TOK_OTHER:
3536 if (!strcmp(t->text, ",")) /* permit comma separators */
3537 break;
3538 /* else fall through */
3539 default:
3540 error(ERR_NONFATAL,
3541 "non-string passed to `%%strcat' (%d)", t->type);
3542 free_tlist(tline);
3543 free_tlist(origline);
3544 return DIRECTIVE_FOUND;
3545 }
3546 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003547
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003548 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003549 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003550 if (t->type == TOK_STRING) {
3551 memcpy(p, t->text, t->a.len);
3552 p += t->a.len;
3553 }
3554 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003555
3556 /*
3557 * We now have a macro name, an implicit parameter count of
3558 * zero, and a numeric token to use as an expansion. Create
3559 * and store an SMacro.
3560 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003561 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3562 macro_start->text = nasm_quote(pp, len);
3563 nasm_free(pp);
3564 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003565 free_tlist(tline);
3566 free_tlist(origline);
3567 return DIRECTIVE_FOUND;
3568
H. Peter Anvine2c80182005-01-15 22:15:51 +00003569 case PP_SUBSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003570 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003571 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003572 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003573 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003574
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003575 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003576
H. Peter Anvine2c80182005-01-15 22:15:51 +00003577 tline = tline->next;
3578 skip_white_(tline);
3579 tline = expand_id(tline);
3580 if (!tline || (tline->type != TOK_ID &&
3581 (tline->type != TOK_PREPROC_ID ||
3582 tline->text[1] != '$'))) {
3583 error(ERR_NONFATAL,
3584 "`%%substr' expects a macro identifier as first parameter");
3585 free_tlist(origline);
3586 return DIRECTIVE_FOUND;
3587 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003588 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003589 last = tline;
3590 tline = expand_smacro(tline->next);
3591 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003592
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003593 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003594 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003595 while (tok_type_(t, TOK_WHITESPACE))
3596 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003597
H. Peter Anvine2c80182005-01-15 22:15:51 +00003598 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003599 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003600 error(ERR_NONFATAL,
3601 "`%%substr` requires string as second parameter");
3602 free_tlist(tline);
3603 free_tlist(origline);
3604 return DIRECTIVE_FOUND;
3605 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003606
H. Peter Anvine2c80182005-01-15 22:15:51 +00003607 tt = t->next;
3608 tptr = &tt;
3609 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003610 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003611 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003612 if (!evalresult) {
3613 free_tlist(tline);
3614 free_tlist(origline);
3615 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003616 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003617 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3618 free_tlist(tline);
3619 free_tlist(origline);
3620 return DIRECTIVE_FOUND;
3621 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003622 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003623
3624 while (tok_type_(tt, TOK_WHITESPACE))
3625 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003626 if (!tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003627 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003628 } else {
3629 tokval.t_type = TOKEN_INVALID;
3630 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3631 pass, error, NULL);
3632 if (!evalresult) {
3633 free_tlist(tline);
3634 free_tlist(origline);
3635 return DIRECTIVE_FOUND;
3636 } else if (!is_simple(evalresult)) {
3637 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3638 free_tlist(tline);
3639 free_tlist(origline);
3640 return DIRECTIVE_FOUND;
3641 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003642 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003643 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003644
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003645 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003646 /* make start and count being in range */
3647 if (start < 0)
3648 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003649 if (count < 0)
3650 count = len + count + 1 - start;
3651 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003652 count = len - start;
3653 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003654 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003655
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003656 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003657 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003658 macro_start->type = TOK_STRING;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003659
H. Peter Anvine2c80182005-01-15 22:15:51 +00003660 /*
3661 * We now have a macro name, an implicit parameter count of
3662 * zero, and a numeric token to use as an expansion. Create
3663 * and store an SMacro.
3664 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003665 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003666 free_tlist(tline);
3667 free_tlist(origline);
3668 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003669 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003670
H. Peter Anvine2c80182005-01-15 22:15:51 +00003671 case PP_ASSIGN:
3672 case PP_IASSIGN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003673 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003674 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003675
H. Peter Anvine2c80182005-01-15 22:15:51 +00003676 tline = tline->next;
3677 skip_white_(tline);
3678 tline = expand_id(tline);
3679 if (!tline || (tline->type != TOK_ID &&
3680 (tline->type != TOK_PREPROC_ID ||
3681 tline->text[1] != '$'))) {
3682 error(ERR_NONFATAL,
3683 "`%%%sassign' expects a macro identifier",
3684 (i == PP_IASSIGN ? "i" : ""));
3685 free_tlist(origline);
3686 return DIRECTIVE_FOUND;
3687 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003688 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003689 last = tline;
3690 tline = expand_smacro(tline->next);
3691 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003692
H. Peter Anvine2c80182005-01-15 22:15:51 +00003693 t = tline;
3694 tptr = &t;
3695 tokval.t_type = TOKEN_INVALID;
3696 evalresult =
3697 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3698 free_tlist(tline);
3699 if (!evalresult) {
3700 free_tlist(origline);
3701 return DIRECTIVE_FOUND;
3702 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003703
H. Peter Anvine2c80182005-01-15 22:15:51 +00003704 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003705 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003706 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003707
H. Peter Anvine2c80182005-01-15 22:15:51 +00003708 if (!is_simple(evalresult)) {
3709 error(ERR_NONFATAL,
3710 "non-constant value given to `%%%sassign'",
3711 (i == PP_IASSIGN ? "i" : ""));
3712 free_tlist(origline);
3713 return DIRECTIVE_FOUND;
3714 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003715
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003716 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003717 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003718
H. Peter Anvine2c80182005-01-15 22:15:51 +00003719 /*
3720 * We now have a macro name, an implicit parameter count of
3721 * zero, and a numeric token to use as an expansion. Create
3722 * and store an SMacro.
3723 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003724 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003725 free_tlist(origline);
3726 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003727
H. Peter Anvine2c80182005-01-15 22:15:51 +00003728 case PP_LINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003729 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003730 /*
3731 * Syntax is `%line nnn[+mmm] [filename]'
3732 */
3733 tline = tline->next;
3734 skip_white_(tline);
3735 if (!tok_type_(tline, TOK_NUMBER)) {
3736 error(ERR_NONFATAL, "`%%line' expects line number");
3737 free_tlist(origline);
3738 return DIRECTIVE_FOUND;
3739 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003740 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003741 m = 1;
3742 tline = tline->next;
3743 if (tok_is_(tline, "+")) {
3744 tline = tline->next;
3745 if (!tok_type_(tline, TOK_NUMBER)) {
3746 error(ERR_NONFATAL, "`%%line' expects line increment");
3747 free_tlist(origline);
3748 return DIRECTIVE_FOUND;
3749 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003750 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003751 tline = tline->next;
3752 }
3753 skip_white_(tline);
3754 src_set_linnum(k);
3755 istk->lineinc = m;
3756 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003757 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003758 }
3759 free_tlist(origline);
3760 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003761
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003762 case PP_WHILE:
3763 if (defining != NULL) {
3764 if (defining->type == EXP_WHILE) {
3765 defining->def_depth ++;
3766 }
3767 return NO_DIRECTIVE_FOUND;
3768 }
3769 l = NULL;
3770 if ((istk->expansion != NULL) &&
3771 (istk->expansion->emitting == false)) {
3772 j = COND_NEVER;
3773 } else {
3774 l = new_Line();
3775 l->first = copy_Token(tline->next);
3776 j = if_condition(tline->next, i);
3777 tline->next = NULL; /* it got freed */
3778 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
3779 }
3780 ed = new_ExpDef(EXP_WHILE);
3781 ed->state = j;
3782 ed->cur_depth = 1;
3783 ed->max_depth = DEADMAN_LIMIT;
3784 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
3785 if (ed->ignoring == false) {
3786 ed->line = l;
3787 ed->last = l;
3788 } else if (l != NULL) {
3789 delete_Token(l->first);
3790 nasm_free(l);
3791 l = NULL;
3792 }
3793 ed->prev = defining;
3794 defining = ed;
3795 free_tlist(origline);
3796 return DIRECTIVE_FOUND;
3797
3798 case PP_ENDWHILE:
3799 if (defining != NULL) {
3800 if (defining->type == EXP_WHILE) {
3801 if (defining->def_depth > 0) {
3802 defining->def_depth --;
3803 return NO_DIRECTIVE_FOUND;
3804 }
3805 } else {
3806 return NO_DIRECTIVE_FOUND;
3807 }
3808 }
3809 if (tline->next != NULL) {
3810 error_precond(ERR_WARNING|ERR_PASS1,
3811 "trailing garbage after `%%endwhile' ignored");
3812 }
3813 if ((defining == NULL) || (defining->type != EXP_WHILE)) {
3814 error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'");
3815 return DIRECTIVE_FOUND;
3816 }
3817 ed = defining;
3818 defining = ed->prev;
3819 if (ed->ignoring == false) {
3820 ed->prev = expansions;
3821 expansions = ed;
3822 ei = new_ExpInv(EXP_WHILE, ed);
3823 ei->current = ed->line->next;
3824 ei->emitting = true;
3825 ei->prev = istk->expansion;
3826 istk->expansion = ei;
3827 } else {
3828 nasm_free(ed);
3829 }
3830 free_tlist(origline);
3831 return DIRECTIVE_FOUND;
3832
3833 case PP_EXITWHILE:
3834 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3835 /*
3836 * We must search along istk->expansion until we hit a
3837 * while invocation. Then we disable the emitting state(s)
3838 * between exitwhile and endwhile.
3839 */
3840 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3841 if (ei->type == EXP_WHILE) {
3842 break;
3843 }
3844 }
3845
3846 if (ei != NULL) {
3847 /*
3848 * Set all invocations leading back to the while
3849 * invocation to a non-emitting state.
3850 */
3851 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3852 eei->emitting = false;
3853 }
3854 eei->emitting = false;
3855 eei->current = NULL;
3856 eei->def->cur_depth = eei->def->max_depth;
3857 } else {
3858 error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block");
3859 }
3860 free_tlist(origline);
3861 return DIRECTIVE_FOUND;
3862
3863 case PP_COMMENT:
3864 if (defining != NULL) {
3865 if (defining->type == EXP_COMMENT) {
3866 defining->def_depth ++;
3867 }
3868 return NO_DIRECTIVE_FOUND;
3869 }
3870 ed = new_ExpDef(EXP_COMMENT);
3871 ed->ignoring = true;
3872 ed->prev = defining;
3873 defining = ed;
3874 free_tlist(origline);
3875 return DIRECTIVE_FOUND;
3876
3877 case PP_ENDCOMMENT:
3878 if (defining != NULL) {
3879 if (defining->type == EXP_COMMENT) {
3880 if (defining->def_depth > 0) {
3881 defining->def_depth --;
3882 return NO_DIRECTIVE_FOUND;
3883 }
3884 } else {
3885 return NO_DIRECTIVE_FOUND;
3886 }
3887 }
3888 if ((defining == NULL) || (defining->type != EXP_COMMENT)) {
3889 error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'");
3890 return DIRECTIVE_FOUND;
3891 }
3892 ed = defining;
3893 defining = ed->prev;
3894 nasm_free(ed);
3895 free_tlist(origline);
3896 return DIRECTIVE_FOUND;
3897
3898 case PP_FINAL:
3899 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3900 if (in_final != false) {
3901 error(ERR_FATAL, "`%%final' cannot be used recursively");
3902 }
3903 tline = tline->next;
3904 skip_white_(tline);
3905 if (tline == NULL) {
3906 error(ERR_NONFATAL, "`%%final' expects at least one parameter");
3907 } else {
3908 l = new_Line();
3909 l->first = copy_Token(tline);
3910 l->next = finals;
3911 finals = l;
3912 }
3913 free_tlist(origline);
3914 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003915
H. Peter Anvine2c80182005-01-15 22:15:51 +00003916 default:
3917 error(ERR_FATAL,
3918 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003919 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003920 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003921 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003922}
3923
3924/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003925 * Ensure that a macro parameter contains a condition code and
3926 * nothing else. Return the condition code index if so, or -1
3927 * otherwise.
3928 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003929static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003930{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003931 Token *tt;
3932 int i, j, k, m;
3933
H. Peter Anvin25a99342007-09-22 17:45:45 -07003934 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003935 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003936
H. Peter Anvineba20a72002-04-30 20:53:55 +00003937 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003938 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003939 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003940 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003941 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003942 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003943 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003944
3945 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003946 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003947 while (j - i > 1) {
3948 k = (j + i) / 2;
3949 m = nasm_stricmp(t->text, conditions[k]);
3950 if (m == 0) {
3951 i = k;
3952 j = -2;
3953 break;
3954 } else if (m < 0) {
3955 j = k;
3956 } else
3957 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003958 }
3959 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003960 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003961 return i;
3962}
3963
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003964static bool paste_tokens(Token **head, const struct tokseq_match *m,
3965 int mnum, bool handle_paste_tokens)
H. Peter Anvind784a082009-04-20 14:01:18 -07003966{
3967 Token **tail, *t, *tt;
3968 Token **paste_head;
3969 bool did_paste = false;
3970 char *tmp;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003971 int i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003972
3973 /* Now handle token pasting... */
3974 paste_head = NULL;
3975 tail = head;
3976 while ((t = *tail) && (tt = t->next)) {
3977 switch (t->type) {
3978 case TOK_WHITESPACE:
3979 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003980 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003981 t->next = delete_Token(tt);
3982 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003983 /* Do not advance paste_head here */
3984 tail = &t->next;
3985 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003986 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003987 case TOK_PASTE: /* %+ */
3988 if (handle_paste_tokens) {
3989 /* Zap %+ and whitespace tokens to the right */
3990 while (t && (t->type == TOK_WHITESPACE ||
3991 t->type == TOK_PASTE))
3992 t = *tail = delete_Token(t);
3993 if (!paste_head || !t)
3994 break; /* Nothing to paste with */
3995 tail = paste_head;
3996 t = *tail;
3997 tt = t->next;
3998 while (tok_type_(tt, TOK_WHITESPACE))
3999 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004000 if (tt) {
4001 tmp = nasm_strcat(t->text, tt->text);
4002 delete_Token(t);
4003 tt = delete_Token(tt);
4004 t = *tail = tokenize(tmp);
4005 nasm_free(tmp);
4006 while (t->next) {
4007 tail = &t->next;
4008 t = t->next;
4009 }
4010 t->next = tt; /* Attach the remaining token chain */
4011 did_paste = true;
4012 }
4013 paste_head = tail;
4014 tail = &t->next;
4015 break;
4016 }
4017 /* else fall through */
4018 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004019 /*
4020 * Concatenation of tokens might look nontrivial
4021 * but in real it's pretty simple -- the caller
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004022 * prepares the masks of token types to be concatenated
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004023 * and we simply find matched sequences and slip
4024 * them together
4025 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004026 for (i = 0; i < mnum; i++) {
4027 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
4028 size_t len = 0;
4029 char *tmp, *p;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004030
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004031 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004032 len += strlen(tt->text);
4033 tt = tt->next;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004034 }
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004035
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004036 /*
4037 * Now tt points to the first token after
4038 * the potential paste area...
4039 */
4040 if (tt != t->next) {
4041 /* We have at least two tokens... */
4042 len += strlen(t->text);
4043 p = tmp = nasm_malloc(len+1);
4044 while (t != tt) {
4045 strcpy(p, t->text);
4046 p = strchr(p, '\0');
4047 t = delete_Token(t);
4048 }
4049 t = *tail = tokenize(tmp);
4050 nasm_free(tmp);
4051 while (t->next) {
4052 tail = &t->next;
4053 t = t->next;
4054 }
4055 t->next = tt; /* Attach the remaining token chain */
4056 did_paste = true;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004057 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004058 paste_head = tail;
4059 tail = &t->next;
4060 break;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004061 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004062 }
4063 if (i >= mnum) { /* no match */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004064 tail = &t->next;
4065 if (!tok_type_(t->next, TOK_WHITESPACE))
4066 paste_head = tail;
4067 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004068 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004069 }
4070 }
4071 return did_paste;
4072}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004073
4074/*
4075 * expands to a list of tokens from %{x:y}
4076 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004077static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004078{
4079 Token *t = tline, **tt, *tm, *head;
4080 char *pos;
4081 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004082
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004083 pos = strchr(tline->text, ':');
4084 nasm_assert(pos);
4085
4086 lst = atoi(pos + 1);
4087 fst = atoi(tline->text + 1);
4088
4089 /*
4090 * only macros params are accounted so
4091 * if someone passes %0 -- we reject such
4092 * value(s)
4093 */
4094 if (lst == 0 || fst == 0)
4095 goto err;
4096
4097 /* the values should be sane */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004098 if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) ||
4099 (lst > (int)ei->nparam || lst < (-(int)ei->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004100 goto err;
4101
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004102 fst = fst < 0 ? fst + (int)ei->nparam + 1: fst;
4103 lst = lst < 0 ? lst + (int)ei->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004104
4105 /* counted from zero */
4106 fst--, lst--;
4107
4108 /*
4109 * it will be at least one token
4110 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004111 tm = ei->params[(fst + ei->rotate) % ei->nparam];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004112 t = new_Token(NULL, tm->type, tm->text, 0);
4113 head = t, tt = &t->next;
4114 if (fst < lst) {
4115 for (i = fst + 1; i <= lst; i++) {
4116 t = new_Token(NULL, TOK_OTHER, ",", 0);
4117 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004118 j = (i + ei->rotate) % ei->nparam;
4119 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004120 t = new_Token(NULL, tm->type, tm->text, 0);
4121 *tt = t, tt = &t->next;
4122 }
4123 } else {
4124 for (i = fst - 1; i >= lst; i--) {
4125 t = new_Token(NULL, TOK_OTHER, ",", 0);
4126 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004127 j = (i + ei->rotate) % ei->nparam;
4128 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004129 t = new_Token(NULL, tm->type, tm->text, 0);
4130 *tt = t, tt = &t->next;
4131 }
4132 }
4133
4134 *last = tt;
4135 return head;
4136
4137err:
4138 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
4139 &tline->text[1]);
4140 return tline;
4141}
4142
H. Peter Anvin76690a12002-04-30 20:52:49 +00004143/*
4144 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004145 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004146 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004147 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004148static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004149{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004150 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004151 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004152 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004153
4154 tail = &thead;
4155 thead = NULL;
4156
H. Peter Anvine2c80182005-01-15 22:15:51 +00004157 while (tline) {
4158 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004159 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4160 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4161 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004162 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004163 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004164 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004165 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004166 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004167 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004168
H. Peter Anvine2c80182005-01-15 22:15:51 +00004169 t = tline;
4170 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004171
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004172 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
4173 if (ei->type == EXP_MMACRO) {
4174 break;
4175 }
4176 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004177 if (ei == NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004178 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004179 } else {
4180 pos = strchr(t->text, ':');
4181 if (!pos) {
4182 switch (t->text[1]) {
4183 /*
4184 * We have to make a substitution of one of the
4185 * forms %1, %-1, %+1, %%foo, %0.
4186 */
4187 case '0':
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004188 if ((strlen(t->text) > 2) && (t->text[2] == '0')) {
4189 type = TOK_ID;
4190 text = nasm_strdup(ei->label_text);
4191 } else {
4192 type = TOK_NUMBER;
4193 snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam);
4194 text = nasm_strdup(tmpbuf);
4195 }
4196 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004197 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004198 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004199 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004200 ei->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004201 text = nasm_strcat(tmpbuf, t->text + 2);
4202 break;
4203 case '-':
4204 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004205 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004206 tt = NULL;
4207 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004208 if (ei->nparam > 1)
4209 n = (n + ei->rotate) % ei->nparam;
4210 tt = ei->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004211 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004212 cc = find_cc(tt);
4213 if (cc == -1) {
4214 error(ERR_NONFATAL,
4215 "macro parameter %d is not a condition code",
4216 n + 1);
4217 text = NULL;
4218 } else {
4219 type = TOK_ID;
4220 if (inverse_ccs[cc] == -1) {
4221 error(ERR_NONFATAL,
4222 "condition code `%s' is not invertible",
4223 conditions[cc]);
4224 text = NULL;
4225 } else
4226 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4227 }
4228 break;
4229 case '+':
4230 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004231 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004232 tt = NULL;
4233 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004234 if (ei->nparam > 1)
4235 n = (n + ei->rotate) % ei->nparam;
4236 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004237 }
4238 cc = find_cc(tt);
4239 if (cc == -1) {
4240 error(ERR_NONFATAL,
4241 "macro parameter %d is not a condition code",
4242 n + 1);
4243 text = NULL;
4244 } else {
4245 type = TOK_ID;
4246 text = nasm_strdup(conditions[cc]);
4247 }
4248 break;
4249 default:
4250 n = atoi(t->text + 1) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004251 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004252 tt = NULL;
4253 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004254 if (ei->nparam > 1)
4255 n = (n + ei->rotate) % ei->nparam;
4256 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004257 }
4258 if (tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004259 for (i = 0; i < ei->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004260 *tail = new_Token(NULL, tt->type, tt->text, 0);
4261 tail = &(*tail)->next;
4262 tt = tt->next;
4263 }
4264 }
4265 text = NULL; /* we've done it here */
4266 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004267 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004268 } else {
4269 /*
4270 * seems we have a parameters range here
4271 */
4272 Token *head, **last;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004273 head = expand_mmac_params_range(ei, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004274 if (head != t) {
4275 *tail = head;
4276 *last = tline;
4277 tline = head;
4278 text = NULL;
4279 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004280 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004281 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004282 if (!text) {
4283 delete_Token(t);
4284 } else {
4285 *tail = t;
4286 tail = &t->next;
4287 t->type = type;
4288 nasm_free(t->text);
4289 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004290 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004291 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004292 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004293 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004294 } else if (tline->type == TOK_INDIRECT) {
4295 t = tline;
4296 tline = tline->next;
4297 tt = tokenize(t->text);
4298 tt = expand_mmac_params(tt);
4299 tt = expand_smacro(tt);
4300 *tail = tt;
4301 while (tt) {
4302 tt->a.mac = NULL; /* Necessary? */
4303 tail = &tt->next;
4304 tt = tt->next;
4305 }
4306 delete_Token(t);
4307 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004308 } else {
4309 t = *tail = tline;
4310 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004311 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004312 tail = &t->next;
4313 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004314 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004315 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004316
Cyrill Gorcunovcb00cd12011-06-13 21:25:10 +04004317 if (changed)
4318 paste_tokens(&thead, pp_concat_match,
4319 ARRAY_SIZE(pp_concat_match),
4320 false);
H. Peter Anvin6125b622009-04-08 14:02:25 -07004321
H. Peter Anvin76690a12002-04-30 20:52:49 +00004322 return thead;
4323}
4324
4325/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004326 * Expand all single-line macro calls made in the given line.
4327 * Return the expanded version of the line. The original is deemed
4328 * to be destroyed in the process. (In reality we'll just move
4329 * Tokens from input to output a lot of the time, rather than
4330 * actually bothering to destroy and replicate.)
4331 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004332
H. Peter Anvine2c80182005-01-15 22:15:51 +00004333static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004334{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004335 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004336 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004337 Token **params;
4338 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004339 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004340 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004341 Token *org_tline = tline;
4342 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004343 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004344 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004345 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004346
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004347 /*
4348 * Trick: we should avoid changing the start token pointer since it can
4349 * be contained in "next" field of other token. Because of this
4350 * we allocate a copy of first token and work with it; at the end of
4351 * routine we copy it back
4352 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004353 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004354 tline = new_Token(org_tline->next, org_tline->type,
4355 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004356 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004357 nasm_free(org_tline->text);
4358 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004359 }
4360
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004361 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004362
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004363again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004364 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004365 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004366
H. Peter Anvine2c80182005-01-15 22:15:51 +00004367 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004368 if (!--deadman) {
4369 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004370 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004371 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004372
H. Peter Anvine2c80182005-01-15 22:15:51 +00004373 if ((mname = tline->text)) {
4374 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004375 if (tline->type == TOK_ID) {
4376 head = (SMacro *)hash_findix(&smacros, mname);
4377 } else if (tline->type == TOK_PREPROC_ID) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004378 ctx = get_ctx(mname, &mname, false);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004379 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4380 } else
4381 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004382
H. Peter Anvine2c80182005-01-15 22:15:51 +00004383 /*
4384 * We've hit an identifier. As in is_mmacro below, we first
4385 * check whether the identifier is a single-line macro at
4386 * all, then think about checking for parameters if
4387 * necessary.
4388 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004389 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004390 if (!mstrcmp(m->name, mname, m->casesense))
4391 break;
4392 if (m) {
4393 mstart = tline;
4394 params = NULL;
4395 paramsize = NULL;
4396 if (m->nparam == 0) {
4397 /*
4398 * Simple case: the macro is parameterless. Discard the
4399 * one token that the macro call took, and push the
4400 * expansion back on the to-do stack.
4401 */
4402 if (!m->expansion) {
4403 if (!strcmp("__FILE__", m->name)) {
4404 int32_t num = 0;
4405 char *file = NULL;
4406 src_get(&num, &file);
4407 tline->text = nasm_quote(file, strlen(file));
4408 tline->type = TOK_STRING;
4409 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004410 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004411 }
4412 if (!strcmp("__LINE__", m->name)) {
4413 nasm_free(tline->text);
4414 make_tok_num(tline, src_get_linnum());
4415 continue;
4416 }
4417 if (!strcmp("__BITS__", m->name)) {
4418 nasm_free(tline->text);
4419 make_tok_num(tline, globalbits);
4420 continue;
4421 }
4422 tline = delete_Token(tline);
4423 continue;
4424 }
4425 } else {
4426 /*
4427 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004428 * exists and takes parameters. We must find the
4429 * parameters in the call, count them, find the SMacro
4430 * that corresponds to that form of the macro call, and
4431 * substitute for the parameters when we expand. What a
4432 * pain.
4433 */
4434 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004435 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004436 do {
4437 t = tline->next;
4438 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004439 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004440 t->text = NULL;
4441 t = tline->next = delete_Token(t);
4442 }
4443 tline = t;
4444 } while (tok_type_(tline, TOK_WHITESPACE));
4445 if (!tok_is_(tline, "(")) {
4446 /*
4447 * This macro wasn't called with parameters: ignore
4448 * the call. (Behaviour borrowed from gnu cpp.)
4449 */
4450 tline = mstart;
4451 m = NULL;
4452 } else {
4453 int paren = 0;
4454 int white = 0;
4455 brackets = 0;
4456 nparam = 0;
4457 sparam = PARAM_DELTA;
4458 params = nasm_malloc(sparam * sizeof(Token *));
4459 params[0] = tline->next;
4460 paramsize = nasm_malloc(sparam * sizeof(int));
4461 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004462 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004463 /*
4464 * For some unusual expansions
4465 * which concatenates function call
4466 */
4467 t = tline->next;
4468 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004469 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004470 t->text = NULL;
4471 t = tline->next = delete_Token(t);
4472 }
4473 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004474
H. Peter Anvine2c80182005-01-15 22:15:51 +00004475 if (!tline) {
4476 error(ERR_NONFATAL,
4477 "macro call expects terminating `)'");
4478 break;
4479 }
4480 if (tline->type == TOK_WHITESPACE
4481 && brackets <= 0) {
4482 if (paramsize[nparam])
4483 white++;
4484 else
4485 params[nparam] = tline->next;
4486 continue; /* parameter loop */
4487 }
4488 if (tline->type == TOK_OTHER
4489 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004490 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004491 if (ch == ',' && !paren && brackets <= 0) {
4492 if (++nparam >= sparam) {
4493 sparam += PARAM_DELTA;
4494 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004495 sparam * sizeof(Token *));
4496 paramsize = nasm_realloc(paramsize,
4497 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004498 }
4499 params[nparam] = tline->next;
4500 paramsize[nparam] = 0;
4501 white = 0;
4502 continue; /* parameter loop */
4503 }
4504 if (ch == '{' &&
4505 (brackets > 0 || (brackets == 0 &&
4506 !paramsize[nparam])))
4507 {
4508 if (!(brackets++)) {
4509 params[nparam] = tline->next;
4510 continue; /* parameter loop */
4511 }
4512 }
4513 if (ch == '}' && brackets > 0)
4514 if (--brackets == 0) {
4515 brackets = -1;
4516 continue; /* parameter loop */
4517 }
4518 if (ch == '(' && !brackets)
4519 paren++;
4520 if (ch == ')' && brackets <= 0)
4521 if (--paren < 0)
4522 break;
4523 }
4524 if (brackets < 0) {
4525 brackets = 0;
4526 error(ERR_NONFATAL, "braces do not "
4527 "enclose all of macro parameter");
4528 }
4529 paramsize[nparam] += white + 1;
4530 white = 0;
4531 } /* parameter loop */
4532 nparam++;
4533 while (m && (m->nparam != nparam ||
4534 mstrcmp(m->name, mname,
4535 m->casesense)))
4536 m = m->next;
4537 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004538 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004539 "macro `%s' exists, "
4540 "but not taking %d parameters",
4541 mstart->text, nparam);
4542 }
4543 }
4544 if (m && m->in_progress)
4545 m = NULL;
4546 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004547 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004548 * Design question: should we handle !tline, which
4549 * indicates missing ')' here, or expand those
4550 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004551 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004552 */
4553 nasm_free(params);
4554 nasm_free(paramsize);
4555 tline = mstart;
4556 } else {
4557 /*
4558 * Expand the macro: we are placed on the last token of the
4559 * call, so that we can easily split the call from the
4560 * following tokens. We also start by pushing an SMAC_END
4561 * token for the cycle removal.
4562 */
4563 t = tline;
4564 if (t) {
4565 tline = t->next;
4566 t->next = NULL;
4567 }
4568 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004569 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004570 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004571 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004572 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004573 if (t->type >= TOK_SMAC_PARAM) {
4574 Token *pcopy = tline, **ptail = &pcopy;
4575 Token *ttt, *pt;
4576 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004577
H. Peter Anvine2c80182005-01-15 22:15:51 +00004578 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004579 i = paramsize[t->type - TOK_SMAC_PARAM];
4580 while (--i >= 0) {
4581 pt = *ptail = new_Token(tline, ttt->type,
4582 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004583 ptail = &pt->next;
4584 ttt = ttt->next;
4585 }
4586 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004587 } else if (t->type == TOK_PREPROC_Q) {
4588 tt = new_Token(tline, TOK_ID, mname, 0);
4589 tline = tt;
4590 } else if (t->type == TOK_PREPROC_QQ) {
4591 tt = new_Token(tline, TOK_ID, m->name, 0);
4592 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004593 } else {
4594 tt = new_Token(tline, t->type, t->text, 0);
4595 tline = tt;
4596 }
4597 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004598
H. Peter Anvine2c80182005-01-15 22:15:51 +00004599 /*
4600 * Having done that, get rid of the macro call, and clean
4601 * up the parameters.
4602 */
4603 nasm_free(params);
4604 nasm_free(paramsize);
4605 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004606 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004607 continue; /* main token loop */
4608 }
4609 }
4610 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004611
H. Peter Anvine2c80182005-01-15 22:15:51 +00004612 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004613 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004614 tline = delete_Token(tline);
4615 } else {
4616 t = *tail = tline;
4617 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004618 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004619 t->next = NULL;
4620 tail = &t->next;
4621 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004622 }
4623
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004624 /*
4625 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004626 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004627 * TOK_IDs should be concatenated.
4628 * Also we look for %+ tokens and concatenate the tokens before and after
4629 * them (without white spaces in between).
4630 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004631 if (expanded) {
Cyrill Gorcunovcb00cd12011-06-13 21:25:10 +04004632 if (paste_tokens(&thead, pp_concat_match,
4633 ARRAY_SIZE(pp_concat_match),
4634 true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004635 /*
4636 * If we concatenated something, *and* we had previously expanded
4637 * an actual macro, scan the lines again for macros...
4638 */
4639 tline = thead;
4640 expanded = false;
4641 goto again;
4642 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004643 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004644
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004645err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004646 if (org_tline) {
4647 if (thead) {
4648 *org_tline = *thead;
4649 /* since we just gave text to org_line, don't free it */
4650 thead->text = NULL;
4651 delete_Token(thead);
4652 } else {
4653 /* the expression expanded to empty line;
4654 we can't return NULL for some reasons
4655 we just set the line to a single WHITESPACE token. */
4656 memset(org_tline, 0, sizeof(*org_tline));
4657 org_tline->text = NULL;
4658 org_tline->type = TOK_WHITESPACE;
4659 }
4660 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004661 }
4662
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004663 return thead;
4664}
4665
4666/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004667 * Similar to expand_smacro but used exclusively with macro identifiers
4668 * right before they are fetched in. The reason is that there can be
4669 * identifiers consisting of several subparts. We consider that if there
4670 * are more than one element forming the name, user wants a expansion,
4671 * otherwise it will be left as-is. Example:
4672 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004673 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004674 *
4675 * the identifier %$abc will be left as-is so that the handler for %define
4676 * will suck it and define the corresponding value. Other case:
4677 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004678 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004679 *
4680 * In this case user wants name to be expanded *before* %define starts
4681 * working, so we'll expand %$abc into something (if it has a value;
4682 * otherwise it will be left as-is) then concatenate all successive
4683 * PP_IDs into one.
4684 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004685static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004686{
4687 Token *cur, *oldnext = NULL;
4688
H. Peter Anvin734b1882002-04-30 21:01:08 +00004689 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004690 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004691
4692 cur = tline;
4693 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004694 (cur->next->type == TOK_ID ||
4695 cur->next->type == TOK_PREPROC_ID
4696 || cur->next->type == TOK_NUMBER))
4697 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004698
4699 /* If identifier consists of just one token, don't expand */
4700 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004701 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004702
H. Peter Anvine2c80182005-01-15 22:15:51 +00004703 if (cur) {
4704 oldnext = cur->next; /* Detach the tail past identifier */
4705 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004706 }
4707
H. Peter Anvin734b1882002-04-30 21:01:08 +00004708 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004709
H. Peter Anvine2c80182005-01-15 22:15:51 +00004710 if (cur) {
4711 /* expand_smacro possibly changhed tline; re-scan for EOL */
4712 cur = tline;
4713 while (cur && cur->next)
4714 cur = cur->next;
4715 if (cur)
4716 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004717 }
4718
4719 return tline;
4720}
4721
4722/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004723 * Determine whether the given line constitutes a multi-line macro
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004724 * call, and return the ExpDef structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004725 * to check for an initial label - that's taken care of in
4726 * expand_mmacro - but must check numbers of parameters. Guaranteed
4727 * to be called with tline->type == TOK_ID, so the putative macro
4728 * name is easy to find.
4729 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004730static ExpDef *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004731{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004732 ExpDef *head, *ed;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004733 Token **params;
4734 int nparam;
4735
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004736 head = (ExpDef *) hash_findix(&expdefs, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004737
4738 /*
4739 * Efficiency: first we see if any macro exists with the given
4740 * name. If not, we can return NULL immediately. _Then_ we
4741 * count the parameters, and then we look further along the
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004742 * list if necessary to find the proper ExpDef.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004743 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004744 list_for_each(ed, head)
4745 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004746 break;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004747 if (!ed)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004748 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004749
4750 /*
4751 * OK, we have a potential macro. Count and demarcate the
4752 * parameters.
4753 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004754 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004755
4756 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004757 * So we know how many parameters we've got. Find the ExpDef
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004758 * structure that handles this number.
4759 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004760 while (ed) {
4761 if (ed->nparam_min <= nparam
4762 && (ed->plus || nparam <= ed->nparam_max)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004763 /*
4764 * It's right, and we can use it. Add its default
4765 * parameters to the end of our list if necessary.
4766 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004767 if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004768 params =
4769 nasm_realloc(params,
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004770 ((ed->nparam_min + ed->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004771 1) * sizeof(*params)));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004772 while (nparam < ed->nparam_min + ed->ndefs) {
4773 params[nparam] = ed->defaults[nparam - ed->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004774 nparam++;
4775 }
4776 }
4777 /*
4778 * If we've gone over the maximum parameter count (and
4779 * we're in Plus mode), ignore parameters beyond
4780 * nparam_max.
4781 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004782 if (ed->plus && nparam > ed->nparam_max)
4783 nparam = ed->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004784 /*
4785 * Then terminate the parameter list, and leave.
4786 */
4787 if (!params) { /* need this special case */
4788 params = nasm_malloc(sizeof(*params));
4789 nparam = 0;
4790 }
4791 params[nparam] = NULL;
4792 *params_array = params;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004793 return ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004794 }
4795 /*
4796 * This one wasn't right: look for the next one with the
4797 * same name.
4798 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004799 list_for_each(ed, ed->next)
4800 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004801 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004802 }
4803
4804 /*
4805 * After all that, we didn't find one with the right number of
4806 * parameters. Issue a warning, and fail to expand the macro.
4807 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004808 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004809 "macro `%s' exists, but not taking %d parameters",
4810 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004811 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004812 return NULL;
4813}
4814
4815/*
4816 * Expand the multi-line macro call made by the given line, if
4817 * there is one to be expanded. If there is, push the expansion on
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004818 * istk->expansion and return true. Otherwise return false.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004819 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004820static bool expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004821{
H. Peter Anvineba20a72002-04-30 20:53:55 +00004822 Token *label = NULL;
4823 int dont_prepend = 0;
Cyrill Gorcunovfb27fc22011-06-25 12:08:30 +04004824 Token **params, *t;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004825 Line *l = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004826 ExpDef *ed;
4827 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004828 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004829 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004830
4831 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004832 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004833 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004834 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004835 return false;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004836 ed = is_mmacro(t, &params);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004837 if (ed != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004838 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004839 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004840 Token *last;
4841 /*
4842 * We have an id which isn't a macro call. We'll assume
4843 * it might be a label; we'll also check to see if a
4844 * colon follows it. Then, if there's another id after
4845 * that lot, we'll check it again for macro-hood.
4846 */
4847 label = last = t;
4848 t = t->next;
4849 if (tok_type_(t, TOK_WHITESPACE))
4850 last = t, t = t->next;
4851 if (tok_is_(t, ":")) {
4852 dont_prepend = 1;
4853 last = t, t = t->next;
4854 if (tok_type_(t, TOK_WHITESPACE))
4855 last = t, t = t->next;
4856 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004857 if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, &params)))
4858 return false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004859 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004860 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004861 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004862 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004863
4864 /*
4865 * Fix up the parameters: this involves stripping leading and
4866 * trailing whitespace, then stripping braces if they are
4867 * present.
4868 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004869 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004870 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004871
H. Peter Anvine2c80182005-01-15 22:15:51 +00004872 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004873 int brace = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004874 int comma = (!ed->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004875
H. Peter Anvine2c80182005-01-15 22:15:51 +00004876 t = params[i];
4877 skip_white_(t);
4878 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004879 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004880 params[i] = t;
4881 paramlen[i] = 0;
4882 while (t) {
4883 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4884 break; /* ... because we have hit a comma */
4885 if (comma && t->type == TOK_WHITESPACE
4886 && tok_is_(t->next, ","))
4887 break; /* ... or a space then a comma */
4888 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4889 break; /* ... or a brace */
4890 t = t->next;
4891 paramlen[i]++;
4892 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004893 }
4894
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004895 if (ed->cur_depth >= ed->max_depth) {
4896 if (ed->max_depth > 1) {
4897 error(ERR_WARNING,
4898 "reached maximum macro recursion depth of %i for %s",
4899 ed->max_depth,ed->name);
4900 }
4901 return false;
4902 } else {
4903 ed->cur_depth ++;
4904 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004905
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004906 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004907 * OK, we have found a ExpDef structure representing a
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004908 * previously defined mmacro. Create an expansion invocation
4909 * and point it back to the expansion definition. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004910 * parameter tokens and macro-local tokens doesn't get done
4911 * until the single-line macro substitution process; this is
4912 * because delaying them allows us to change the semantics
4913 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004914 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004915 ei = new_ExpInv(EXP_MMACRO, ed);
4916 ei->name = nasm_strdup(mname);
4917 //ei->label = label;
4918 //ei->label_text = detoken(label, false);
4919 ei->current = ed->line;
4920 ei->emitting = true;
4921 //ei->iline = tline;
4922 ei->params = params;
4923 ei->nparam = nparam;
4924 ei->rotate = 0;
4925 ei->paramlen = paramlen;
4926 ei->lineno = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004927
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004928 ei->prev = istk->expansion;
4929 istk->expansion = ei;
4930
4931 /*
4932 * Special case: detect %00 on first invocation; if found,
4933 * avoid emitting any labels that precede the mmacro call.
4934 * ed->prepend is set to -1 when %00 is detected, else 1.
4935 */
4936 if (ed->prepend == 0) {
4937 for (l = ed->line; l != NULL; l = l->next) {
4938 for (t = l->first; t != NULL; t = t->next) {
4939 if ((t->type == TOK_PREPROC_ID) &&
4940 (strlen(t->text) == 3) &&
4941 (t->text[1] == '0') && (t->text[2] == '0')) {
4942 dont_prepend = -1;
4943 break;
4944 }
4945 }
4946 if (dont_prepend < 0) {
4947 break;
4948 }
4949 }
4950 ed->prepend = ((dont_prepend < 0) ? -1 : 1);
4951 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004952
4953 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004954 * If we had a label, push it on as the first line of
4955 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004956 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004957 if (label != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004958 if (ed->prepend < 0) {
4959 ei->label_text = detoken(label, false);
4960 } else {
4961 if (dont_prepend == 0) {
4962 t = label;
4963 while (t->next != NULL) {
4964 t = t->next;
4965 }
4966 t->next = new_Token(NULL, TOK_OTHER, ":", 0);
4967 }
4968 l = new_Line();
4969 l->first = copy_Token(label);
4970 l->next = ei->current;
4971 ei->current = l;
4972 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004973 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004974
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004975 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004976
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004977 istk->mmac_depth++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004978 return true;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004979}
4980
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004981/* The function that actually does the error reporting */
4982static void verror(int severity, const char *fmt, va_list arg)
4983{
4984 char buff[1024];
4985
4986 vsnprintf(buff, sizeof(buff), fmt, arg);
4987
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03004988 if (istk && istk->mmac_depth > 0) {
4989 ExpInv *ei = istk->expansion;
4990 int lineno = ei->lineno;
4991 while (ei) {
4992 if (ei->type == EXP_MMACRO)
4993 break;
4994 lineno += ei->relno;
4995 ei = ei->prev;
4996 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004997 nasm_error(severity, "(%s:%d) %s", ei->def->name,
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03004998 lineno, buff);
4999 } else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005000 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005001}
5002
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005003/*
5004 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07005005 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005006 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005007static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005008{
5009 va_list arg;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005010 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005011 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00005012 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005013}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005014
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005015/*
5016 * Because %else etc are evaluated in the state context
5017 * of the previous branch, errors might get lost with error():
5018 * %if 0 ... %else trailing garbage ... %endif
5019 * So %else etc should report errors with this function.
5020 */
5021static void error_precond(int severity, const char *fmt, ...)
5022{
5023 va_list arg;
5024
5025 /* Only ignore the error if it's really in a dead branch */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005026 if ((istk != NULL) &&
5027 (istk->expansion != NULL) &&
5028 (istk->expansion->type == EXP_IF) &&
5029 (istk->expansion->def->state == COND_NEVER))
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005030 return;
5031
5032 va_start(arg, fmt);
5033 verror(severity, fmt, arg);
5034 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005035}
5036
H. Peter Anvin734b1882002-04-30 21:01:08 +00005037static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005038pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005039{
H. Peter Anvin7383b402008-09-24 10:20:40 -07005040 Token *t;
5041
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005042 cstk = NULL;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005043 istk = nasm_zalloc(sizeof(Include));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005044 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00005045 src_set_fname(nasm_strdup(file));
5046 src_set_linnum(0);
5047 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005048 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07005049 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00005050 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005051 defining = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005052 finals = NULL;
5053 in_final = false;
Charles Crayned4200be2008-07-12 16:42:33 -07005054 nested_mac_count = 0;
5055 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005056 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005057 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005058 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005059 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005060 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005061 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005062 }
H. Peter Anvind2456592008-06-19 15:04:18 -07005063 any_extrastdmac = extrastdmac && *extrastdmac;
5064 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005065 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005066
5067 /*
5068 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5069 * The caller, however, will also pass in 3 for preprocess-only so
5070 * we can set __PASS__ accordingly.
5071 */
5072 pass = apass > 2 ? 2 : apass;
5073
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005074 dephead = deptail = deplist;
5075 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005076 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
5077 sl->next = NULL;
5078 strcpy(sl->str, file);
5079 *deptail = sl;
5080 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005081 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07005082
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005083 /*
5084 * Define the __PASS__ macro. This is defined here unlike
5085 * all the other builtins, because it is special -- it varies between
5086 * passes.
5087 */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005088 t = nasm_zalloc(sizeof(*t));
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005089 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07005090 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005091}
5092
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005093static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005094{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005095 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005096 Token *tline;
Keith Kanios9858cec2010-11-08 00:58:02 -06005097 ExpDef *ed;
5098 ExpInv *ei;
5099 Line *l;
5100 int j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005101
H. Peter Anvine2c80182005-01-15 22:15:51 +00005102 while (1) {
5103 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005104 * Fetch a tokenized line, either from the expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005105 * buffer or from the input file.
5106 */
5107 tline = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005108
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005109 while (1) { /* until we get a line we can use */
5110 /*
5111 * Fetch a tokenized line from the expansion buffer
5112 */
5113 if (istk->expansion != NULL) {
5114 ei = istk->expansion;
5115 if (ei->current != NULL) {
5116 if (ei->emitting == false) {
5117 ei->current = NULL;
5118 continue;
5119 }
5120 l = ei->current;
5121 ei->current = l->next;
5122 ei->lineno++;
5123 tline = copy_Token(l->first);
5124 if (((ei->type == EXP_REP) ||
5125 (ei->type == EXP_MMACRO) ||
5126 (ei->type == EXP_WHILE))
5127 && (ei->def->nolist == false)) {
5128 char *p = detoken(tline, false);
5129 list->line(LIST_MACRO, p);
5130 nasm_free(p);
5131 }
5132 if (ei->linnum > -1) {
5133 src_set_linnum(src_get_linnum() + 1);
5134 }
5135 break;
5136 } else if ((ei->type == EXP_REP) &&
5137 (ei->def->cur_depth < ei->def->max_depth)) {
5138 ei->def->cur_depth ++;
5139 ei->current = ei->def->line;
5140 ei->lineno = 0;
5141 continue;
5142 } else if ((ei->type == EXP_WHILE) &&
5143 (ei->def->cur_depth < ei->def->max_depth)) {
5144 ei->current = ei->def->line;
5145 ei->lineno = 0;
5146 tline = copy_Token(ei->current->first);
5147 j = if_condition(tline, PP_WHILE);
5148 tline = NULL;
5149 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
5150 if (j == COND_IF_TRUE) {
5151 ei->current = ei->current->next;
5152 ei->def->cur_depth ++;
5153 } else {
5154 ei->emitting = false;
5155 ei->current = NULL;
5156 ei->def->cur_depth = ei->def->max_depth;
5157 }
5158 continue;
5159 } else {
5160 istk->expansion = ei->prev;
5161 ed = ei->def;
5162 if (ed != NULL) {
5163 if ((ei->emitting == true) &&
5164 (ed->max_depth == DEADMAN_LIMIT) &&
5165 (ed->cur_depth == DEADMAN_LIMIT)
5166 ) {
5167 error(ERR_FATAL, "runaway expansion detected, aborting");
5168 }
5169 if (ed->cur_depth > 0) {
5170 ed->cur_depth --;
Keith Kanios94124652010-12-18 11:47:28 -06005171 } else if (ed->type != EXP_MMACRO) {
Keith Kanios6a7c3e92010-12-18 11:49:53 -06005172 expansions = ed->prev;
5173 free_expdef(ed);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005174 }
5175 if ((ei->type == EXP_REP) ||
5176 (ei->type == EXP_MMACRO) ||
5177 (ei->type == EXP_WHILE)) {
5178 list->downlevel(LIST_MACRO);
5179 if (ei->type == EXP_MMACRO) {
5180 istk->mmac_depth--;
5181 }
5182 }
5183 }
5184 if (ei->linnum > -1) {
5185 src_set_linnum(ei->linnum);
5186 }
5187 free_expinv(ei);
5188 continue;
5189 }
5190 }
5191
5192 /*
5193 * Read in line from input and tokenize
5194 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005195 line = read_line();
5196 if (line) { /* from the current input file */
5197 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005198 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005199 nasm_free(line);
5200 break;
5201 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005202
H. Peter Anvine2c80182005-01-15 22:15:51 +00005203 /*
5204 * The current file has ended; work down the istk
5205 */
5206 {
5207 Include *i = istk;
5208 fclose(i->fp);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005209 if (i->expansion != NULL) {
5210 error(ERR_FATAL,
5211 "end of file while still in an expansion");
5212 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005213 /* only set line and file name if there's a next node */
5214 if (i->next) {
5215 src_set_linnum(i->lineno);
Cyrill Gorcunovd34a1082011-03-07 11:23:08 +03005216 nasm_free(src_set_fname(nasm_strdup(i->fname)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005217 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005218 if ((i->next == NULL) && (finals != NULL)) {
5219 in_final = true;
5220 ei = new_ExpInv(EXP_FINAL, NULL);
5221 ei->emitting = true;
5222 ei->current = finals;
5223 istk->expansion = ei;
5224 finals = NULL;
5225 continue;
5226 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005227 istk = i->next;
5228 list->downlevel(LIST_INCLUDE);
5229 nasm_free(i);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005230 if (istk == NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005231 if (finals != NULL) {
5232 in_final = true;
5233 } else {
5234 return NULL;
5235 }
5236 }
5237 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005238 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005239 }
5240
5241 if (defining == NULL) {
5242 tline = expand_mmac_params(tline);
5243 }
5244
H. Peter Anvine2c80182005-01-15 22:15:51 +00005245 /*
5246 * Check the line to see if it's a preprocessor directive.
5247 */
5248 if (do_directive(tline) == DIRECTIVE_FOUND) {
5249 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005250 } else if (defining != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005251 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005252 * We're defining an expansion. We emit nothing at all,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005253 * and just shove the tokenized line on to the definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005254 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005255 if (defining->ignoring == false) {
5256 Line *l = new_Line();
5257 l->first = tline;
5258 if (defining->line == NULL) {
5259 defining->line = l;
5260 defining->last = l;
5261 } else {
5262 defining->last->next = l;
5263 defining->last = l;
5264 }
5265 } else {
Keith Kanios104803d2010-12-18 11:05:46 -06005266 free_tlist(tline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005267 }
5268 defining->linecount++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005269 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005270 } else if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005271 (istk->expansion->emitting != true)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005272 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005273 * We're in a non-emitting branch of an expansion.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005274 * Emit nothing at all, not even a blank line: when we
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005275 * emerge from the expansion we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005276 * directive so we keep our place correctly.
5277 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005278 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005279 continue;
5280 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005281 tline = expand_smacro(tline);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005282 if (expand_mmacro(tline) != true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005283 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005284 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005285 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005286 line = detoken(tline, true);
5287 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005288 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005289 } else {
5290 continue;
5291 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005292 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005293 }
5294 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005295}
5296
H. Peter Anvine2c80182005-01-15 22:15:51 +00005297static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005298{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005299 if (defining != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005300 error(ERR_NONFATAL, "end of file while still defining an expansion");
5301 while (defining != NULL) {
5302 ExpDef *ed = defining;
5303 defining = ed->prev;
5304 free_expdef(ed);
5305 }
5306 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005307 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005308 while (cstk != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005309 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005310 free_macros();
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005311 while (istk != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005312 Include *i = istk;
5313 istk = istk->next;
5314 fclose(i->fp);
5315 nasm_free(i->fname);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005316 while (i->expansion != NULL) {
5317 ExpInv *ei = i->expansion;
5318 i->expansion = ei->prev;
5319 free_expinv(ei);
5320 }
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005321 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005322 }
5323 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005324 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005325 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005326 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005327 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005328 free_llist(predef);
5329 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005330 while ((i = ipath)) {
5331 ipath = i->next;
5332 if (i->path)
5333 nasm_free(i->path);
5334 nasm_free(i);
5335 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005336 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005337}
5338
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005339void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005340{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005341 IncPath *i = nasm_zalloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005342
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005343 if (path)
5344 i->path = nasm_strdup(path);
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005345
H. Peter Anvin89cee572009-07-15 09:16:54 -04005346 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005347 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005348 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005349 j = j->next;
5350 j->next = i;
5351 } else {
5352 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005353 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005354}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005355
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005356void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005357{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005358 Token *inc, *space, *name;
5359 Line *l;
5360
H. Peter Anvin734b1882002-04-30 21:01:08 +00005361 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5362 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5363 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005364
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005365 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005366 l->next = predef;
5367 l->first = inc;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005368 predef = l;
5369}
5370
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005371void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005372{
5373 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005374 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005375 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005376
5377 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005378 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5379 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005380 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005381 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005382 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005383 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005384 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005385
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005386 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005387 l->next = predef;
5388 l->first = def;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005389 predef = l;
5390}
5391
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005392void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005393{
5394 Token *def, *space;
5395 Line *l;
5396
H. Peter Anvin734b1882002-04-30 21:01:08 +00005397 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5398 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005399 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005400
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005401 l = new_Line();
H. Peter Anvin620515a2002-04-30 20:57:38 +00005402 l->next = predef;
5403 l->first = def;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005404 predef = l;
5405}
5406
Keith Kaniosb7a89542007-04-12 02:40:54 +00005407/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005408 * This function is used to assist with "runtime" preprocessor
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005409 * directives, e.g. pp_runtime("%define __BITS__ 64");
Keith Kaniosb7a89542007-04-12 02:40:54 +00005410 *
5411 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5412 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5413 */
5414
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005415void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005416{
5417 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005418
Keith Kaniosb7a89542007-04-12 02:40:54 +00005419 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005420 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005421 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005422
Keith Kaniosb7a89542007-04-12 02:40:54 +00005423}
5424
H. Peter Anvina70547f2008-07-19 21:44:26 -07005425void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005426{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005427 extrastdmac = macros;
5428}
5429
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005430static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005431{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005432 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005433 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005434 tok->text = nasm_strdup(numbuf);
5435 tok->type = TOK_NUMBER;
5436}
5437
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005438Preproc nasmpp = {
5439 pp_reset,
5440 pp_getline,
5441 pp_cleanup
5442};