blob: 0614400eb8a9c7378c6c706d1e407aaac90348b3 [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
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400504#define stringify(x) #x
505
506#define nasm_trace(msg, ...) printf("(%s:%d): " msg "\n", __func__, __LINE__, __VA_ARGS__)
507#define nasm_dump_token(t) nasm_raw_dump_token(t, __FILE__, __LINE__, __func__);
508
509/* FIXME: we really need some compound type here instead of inplace code */
510static const char *nasm_get_tok_type_str(enum pp_token_type type)
511{
512#define SWITCH_TOK_NAME(type) \
513 case (type): \
514 return stringify(type)
515
516 switch (type) {
517 SWITCH_TOK_NAME(TOK_NONE);
518 SWITCH_TOK_NAME(TOK_WHITESPACE);
519 SWITCH_TOK_NAME(TOK_COMMENT);
520 SWITCH_TOK_NAME(TOK_ID);
521 SWITCH_TOK_NAME(TOK_PREPROC_ID);
522 SWITCH_TOK_NAME(TOK_STRING);
523 SWITCH_TOK_NAME(TOK_NUMBER);
524 SWITCH_TOK_NAME(TOK_FLOAT);
525 SWITCH_TOK_NAME(TOK_SMAC_END);
526 SWITCH_TOK_NAME(TOK_OTHER);
527 SWITCH_TOK_NAME(TOK_INTERNAL_STRING);
528 SWITCH_TOK_NAME(TOK_PREPROC_Q);
529 SWITCH_TOK_NAME(TOK_PREPROC_QQ);
530 SWITCH_TOK_NAME(TOK_PASTE);
531 SWITCH_TOK_NAME(TOK_INDIRECT);
532 SWITCH_TOK_NAME(TOK_SMAC_PARAM);
533 SWITCH_TOK_NAME(TOK_MAX);
534 }
535
536 return NULL;
537}
538
539static void nasm_raw_dump_token(Token *token, const char *file, int line, const char *func)
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300540{
541 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
542 if (token) {
543 Token *t;
544 list_for_each(t, token) {
545 if (t->text)
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400546 printf("'%s'(%s) ", t->text,
547 nasm_get_tok_type_str(t->type));
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300548 }
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400549 printf("\n\n");
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300550 }
551}
552
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400553#else
554#define nasm_trace(msg, ...)
555#define nasm_dump_token(t)
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300556#endif
557
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300558/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700559 * nasm_unquote with error if the string contains NUL characters.
560 * If the string contains NUL characters, issue an error and return
561 * the C len, i.e. truncate at the NUL.
562 */
563static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
564{
565 size_t len = nasm_unquote(qstr, NULL);
566 size_t clen = strlen(qstr);
567
568 if (len != clen)
569 error(ERR_NONFATAL, "NUL character in `%s' directive",
570 pp_directives[directive]);
571
572 return clen;
573}
574
575/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700576 * In-place reverse a list of tokens.
577 */
578static Token *reverse_tokens(Token *t)
579{
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400580 Token *prev, *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700581
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400582 list_reverse(t, prev, next);
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700583
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400584 return t;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700585}
586
587/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300588 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000589 * front of them. We do it here because I could not find any other
590 * place to do it for the moment, and it is a hack (ideally it would
591 * be nice to be able to use the NASM pre-processor to do it).
592 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000593static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000594{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000595 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400596 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000597
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400598 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000599
600 /* Binary search for the directive name */
601 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400602 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400603 q = nasm_skip_word(p);
604 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000605 if (len) {
606 oldchar = p[len];
607 p[len] = 0;
608 while (j - i > 1) {
609 k = (j + i) / 2;
610 m = nasm_stricmp(p, tasm_directives[k]);
611 if (m == 0) {
612 /* We have found a directive, so jam a % in front of it
613 * so that NASM will then recognise it as one if it's own.
614 */
615 p[len] = oldchar;
616 len = strlen(p);
617 oldline = line;
618 line = nasm_malloc(len + 2);
619 line[0] = '%';
620 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700621 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300622 * NASM does not recognise IFDIFI, so we convert
623 * it to %if 0. This is not used in NASM
624 * compatible code, but does need to parse for the
625 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000626 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700627 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000628 } else {
629 memcpy(line + 1, p, len + 1);
630 }
631 nasm_free(oldline);
632 return line;
633 } else if (m < 0) {
634 j = k;
635 } else
636 i = k;
637 }
638 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000639 }
640 return line;
641}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000642
H. Peter Anvin76690a12002-04-30 20:52:49 +0000643/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000644 * The pre-preprocessing stage... This function translates line
645 * number indications as they emerge from GNU cpp (`# lineno "file"
646 * flags') into NASM preprocessor line number indications (`%line
647 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000648 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000649static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000650{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000651 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000652 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000653
H. Peter Anvine2c80182005-01-15 22:15:51 +0000654 if (line[0] == '#' && line[1] == ' ') {
655 oldline = line;
656 fname = oldline + 2;
657 lineno = atoi(fname);
658 fname += strspn(fname, "0123456789 ");
659 if (*fname == '"')
660 fname++;
661 fnlen = strcspn(fname, "\"");
662 line = nasm_malloc(20 + fnlen);
663 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
664 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000665 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000666 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000667 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000668 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000669}
670
671/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000672 * Free a linked list of tokens.
673 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000674static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000675{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400676 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000677 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000678}
679
680/*
681 * Free a linked list of lines.
682 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000683static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000684{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400685 Line *l, *tmp;
686 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000687 free_tlist(l->first);
688 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000689 }
690}
691
692/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500693 * Free an ExpDef
H. Peter Anvineba20a72002-04-30 20:53:55 +0000694 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500695static void free_expdef(ExpDef * ed)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000696{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500697 nasm_free(ed->name);
698 free_tlist(ed->dlist);
699 nasm_free(ed->defaults);
700 free_llist(ed->line);
701 nasm_free(ed);
702}
703
704/*
705 * Free an ExpInv
706 */
707static void free_expinv(ExpInv * ei)
708{
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300709 if (ei->name != NULL)
710 nasm_free(ei->name);
711 if (ei->label_text != NULL)
712 nasm_free(ei->label_text);
713 nasm_free(ei);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000714}
715
716/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700717 * Free all currently defined macros, and free the hash tables
718 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700719static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700720{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400721 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700722 const char *key;
723 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700724
H. Peter Anvin072771e2008-05-22 13:17:51 -0700725 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300726 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400727 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300728 nasm_free(s->name);
729 free_tlist(s->expansion);
730 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300731 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700732 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700733 hash_free(smt);
734}
735
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500736static void free_expdef_table(struct hash_table *edt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700737{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500738 ExpDef *ed, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700739 const char *key;
740 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700741
742 it = NULL;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500743 while ((ed = hash_iterate(edt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300744 nasm_free((void *)key);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500745 list_for_each_safe(ed ,tmp, ed)
746 free_expdef(ed);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700747 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500748 hash_free(edt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700749}
750
751static void free_macros(void)
752{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700753 free_smacro_table(&smacros);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500754 free_expdef_table(&expdefs);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700755}
756
757/*
758 * Initialize the hash tables
759 */
760static void init_macros(void)
761{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700762 hash_init(&smacros, HASH_LARGE);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500763 hash_init(&expdefs, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700764}
765
766/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000767 * Pop the context stack.
768 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000769static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000770{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000771 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000772
773 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700774 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000775 nasm_free(c->name);
776 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000777}
778
H. Peter Anvin072771e2008-05-22 13:17:51 -0700779/*
780 * Search for a key in the hash index; adding it if necessary
781 * (in which case we initialize the data pointer to NULL.)
782 */
783static void **
784hash_findi_add(struct hash_table *hash, const char *str)
785{
786 struct hash_insert hi;
787 void **r;
788 char *strx;
789
790 r = hash_findi(hash, str, &hi);
791 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300792 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700793
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300794 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700795 return hash_add(&hi, strx, NULL);
796}
797
798/*
799 * Like hash_findi, but returns the data element rather than a pointer
800 * to it. Used only when not adding a new element, hence no third
801 * argument.
802 */
803static void *
804hash_findix(struct hash_table *hash, const char *str)
805{
806 void **p;
807
808 p = hash_findi(hash, str, NULL);
809 return p ? *p : NULL;
810}
811
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400812/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500813 * read line from standard macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400814 * if there no more left -- return NULL
815 */
816static char *line_from_stdmac(void)
817{
818 unsigned char c;
819 const unsigned char *p = stdmacpos;
820 char *line, *q;
821 size_t len = 0;
822
823 if (!stdmacpos)
824 return NULL;
825
826 while ((c = *p++)) {
827 if (c >= 0x80)
828 len += pp_directives_len[c - 0x80] + 1;
829 else
830 len++;
831 }
832
833 line = nasm_malloc(len + 1);
834 q = line;
835 while ((c = *stdmacpos++)) {
836 if (c >= 0x80) {
837 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
838 q += pp_directives_len[c - 0x80];
839 *q++ = ' ';
840 } else {
841 *q++ = c;
842 }
843 }
844 stdmacpos = p;
845 *q = '\0';
846
847 if (!*stdmacpos) {
848 /* This was the last of the standard macro chain... */
849 stdmacpos = NULL;
850 if (any_extrastdmac) {
851 stdmacpos = extrastdmac;
852 any_extrastdmac = false;
853 } else if (do_predef) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300854 ExpInv *ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400855 Line *pd, *l;
856 Token *head, **tail, *t;
857
858 /*
859 * Nasty hack: here we push the contents of
860 * `predef' on to the top-level expansion stack,
861 * since this is the most convenient way to
862 * implement the pre-include and pre-define
863 * features.
864 */
865 list_for_each(pd, predef) {
866 head = NULL;
867 tail = &head;
868 list_for_each(t, pd->first) {
869 *tail = new_Token(NULL, t->type, t->text, 0);
870 tail = &(*tail)->next;
871 }
872
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500873 l = new_Line();
874 l->first = head;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300875 ei = new_ExpInv(EXP_PREDEF, NULL);
876 ei->current = l;
877 ei->emitting = true;
878 ei->prev = istk->expansion;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500879 istk->expansion = ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400880 }
881 do_predef = false;
882 }
883 }
884
885 return line;
886}
887
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000888#define BUF_DELTA 512
889/*
890 * Read a line from the top file in istk, handling multiple CR/LFs
891 * at the end of the line read, and handling spurious ^Zs. Will
892 * return lines from the standard macro set if this has not already
893 * been done.
894 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000895static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000896{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000897 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000898 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000899
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400900 /*
901 * standart macros set (predefined) goes first
902 */
903 p = line_from_stdmac();
904 if (p)
905 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700906
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400907 /*
908 * regular read from a file
909 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000910 bufsize = BUF_DELTA;
911 buffer = nasm_malloc(BUF_DELTA);
912 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000913 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000914 while (1) {
915 q = fgets(p, bufsize - (p - buffer), istk->fp);
916 if (!q)
917 break;
918 p += strlen(p);
919 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300920 /*
921 * Convert backslash-CRLF line continuation sequences into
922 * nothing at all (for DOS and Windows)
923 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000924 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
925 p -= 3;
926 *p = 0;
927 continued_count++;
928 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300929 /*
930 * Also convert backslash-LF line continuation sequences into
931 * nothing at all (for Unix)
932 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000933 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
934 p -= 2;
935 *p = 0;
936 continued_count++;
937 } else {
938 break;
939 }
940 }
941 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000942 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000943 bufsize += BUF_DELTA;
944 buffer = nasm_realloc(buffer, bufsize);
945 p = buffer + offset; /* prevent stale-pointer problems */
946 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000947 }
948
H. Peter Anvine2c80182005-01-15 22:15:51 +0000949 if (!q && p == buffer) {
950 nasm_free(buffer);
951 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000952 }
953
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300954 src_set_linnum(src_get_linnum() + istk->lineinc +
955 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000956
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000957 /*
958 * Play safe: remove CRs as well as LFs, if any of either are
959 * present at the end of the line.
960 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000961 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000962 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000963
964 /*
965 * Handle spurious ^Z, which may be inserted into source files
966 * by some file transfer utilities.
967 */
968 buffer[strcspn(buffer, "\032")] = '\0';
969
H. Peter Anvin734b1882002-04-30 21:01:08 +0000970 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000971
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000972 return buffer;
973}
974
975/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000976 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000977 * don't need to parse the value out of e.g. numeric tokens: we
978 * simply split one string into many.
979 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000980static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000981{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700982 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000983 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000984 Token *list = NULL;
985 Token *t, **tail = &list;
Keith Kanios6faad4e2010-12-18 14:08:02 -0600986 bool verbose = true;
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +0400987
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400988 nasm_trace("Tokenize for '%s'", line);
989
Keith Kanios6faad4e2010-12-18 14:08:02 -0600990 if ((defining != NULL) && (defining->ignoring == true)) {
991 verbose = false;
992 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000993
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 while (*line) {
995 p = line;
996 if (*p == '%') {
997 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300998 if (*p == '+' && !nasm_isdigit(p[1])) {
999 p++;
1000 type = TOK_PASTE;
1001 } else if (nasm_isdigit(*p) ||
1002 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 do {
1004 p++;
1005 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001006 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 type = TOK_PREPROC_ID;
1008 } else if (*p == '{') {
1009 p++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001010 while (*p && *p != '}') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 p[-1] = *p;
1012 p++;
1013 }
1014 p[-1] = '\0';
1015 if (*p)
1016 p++;
1017 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001018 } else if (*p == '[') {
1019 int lvl = 1;
1020 line += 2; /* Skip the leading %[ */
1021 p++;
1022 while (lvl && (c = *p++)) {
1023 switch (c) {
1024 case ']':
1025 lvl--;
1026 break;
1027 case '%':
1028 if (*p == '[')
1029 lvl++;
1030 break;
1031 case '\'':
1032 case '\"':
1033 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +04001034 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001035 break;
1036 default:
1037 break;
1038 }
1039 }
1040 p--;
1041 if (*p)
1042 *p++ = '\0';
Keith Kanios6faad4e2010-12-18 14:08:02 -06001043 if (lvl && verbose)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001044 error(ERR_NONFATAL, "unterminated %[ construct");
1045 type = TOK_INDIRECT;
1046 } else if (*p == '?') {
1047 type = TOK_PREPROC_Q; /* %? */
1048 p++;
1049 if (*p == '?') {
1050 type = TOK_PREPROC_QQ; /* %?? */
1051 p++;
1052 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001053 } else if (*p == '!') {
1054 type = TOK_PREPROC_ID;
1055 p++;
1056 if (isidchar(*p)) {
1057 do {
1058 p++;
1059 } while (isidchar(*p));
1060 } else if (*p == '\'' || *p == '\"' || *p == '`') {
1061 p = nasm_skip_string(p);
1062 if (*p)
1063 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001064 else if(verbose)
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001065 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
1066 } else {
1067 /* %! without string or identifier */
1068 type = TOK_OTHER; /* Legacy behavior... */
1069 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001070 } else if (isidchar(*p) ||
1071 ((*p == '!' || *p == '%' || *p == '$') &&
1072 isidchar(p[1]))) {
1073 do {
1074 p++;
1075 }
1076 while (isidchar(*p));
1077 type = TOK_PREPROC_ID;
1078 } else {
1079 type = TOK_OTHER;
1080 if (*p == '%')
1081 p++;
1082 }
1083 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1084 type = TOK_ID;
1085 p++;
1086 while (*p && isidchar(*p))
1087 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001088 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001089 /*
1090 * A string token.
1091 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001093 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001094
H. Peter Anvine2c80182005-01-15 22:15:51 +00001095 if (*p) {
1096 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001097 } else if(verbose) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001098 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 /* Handling unterminated strings by UNV */
1100 /* type = -1; */
1101 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001102 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001103 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001104 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001105 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001106 bool is_hex = false;
1107 bool is_float = false;
1108 bool has_e = false;
1109 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001110
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001112 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001113 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001114
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001115 if (*p == '$') {
1116 p++;
1117 is_hex = true;
1118 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001119
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001120 for (;;) {
1121 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001122
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001123 if (!is_hex && (c == 'e' || c == 'E')) {
1124 has_e = true;
1125 if (*p == '+' || *p == '-') {
1126 /*
1127 * e can only be followed by +/- if it is either a
1128 * prefixed hex number or a floating-point number
1129 */
1130 p++;
1131 is_float = true;
1132 }
1133 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1134 is_hex = true;
1135 } else if (c == 'P' || c == 'p') {
1136 is_float = true;
1137 if (*p == '+' || *p == '-')
1138 p++;
1139 } else if (isnumchar(c) || c == '_')
1140 ; /* just advance */
1141 else if (c == '.') {
1142 /*
1143 * we need to deal with consequences of the legacy
1144 * parser, like "1.nolist" being two tokens
1145 * (TOK_NUMBER, TOK_ID) here; at least give it
1146 * a shot for now. In the future, we probably need
1147 * a flex-based scanner with proper pattern matching
1148 * to do it as well as it can be done. Nothing in
1149 * the world is going to help the person who wants
1150 * 0x123.p16 interpreted as two tokens, though.
1151 */
1152 r = p;
1153 while (*r == '_')
1154 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001155
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001156 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1157 (!is_hex && (*r == 'e' || *r == 'E')) ||
1158 (*r == 'p' || *r == 'P')) {
1159 p = r;
1160 is_float = true;
1161 } else
1162 break; /* Terminate the token */
1163 } else
1164 break;
1165 }
1166 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001167
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001168 if (p == line+1 && *line == '$') {
1169 type = TOK_OTHER; /* TOKEN_HERE */
1170 } else {
1171 if (has_e && !is_hex) {
1172 /* 1e13 is floating-point, but 1e13h is not */
1173 is_float = true;
1174 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001175
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001176 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1177 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001178 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001179 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001180 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001181 /*
1182 * Whitespace just before end-of-line is discarded by
1183 * pretending it's a comment; whitespace just before a
1184 * comment gets lumped into the comment.
1185 */
1186 if (!*p || *p == ';') {
1187 type = TOK_COMMENT;
1188 while (*p)
1189 p++;
1190 }
1191 } else if (*p == ';') {
1192 type = TOK_COMMENT;
1193 while (*p)
1194 p++;
1195 } else {
1196 /*
1197 * Anything else is an operator of some kind. We check
1198 * for all the double-character operators (>>, <<, //,
1199 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001200 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001201 */
1202 type = TOK_OTHER;
1203 if ((p[0] == '>' && p[1] == '>') ||
1204 (p[0] == '<' && p[1] == '<') ||
1205 (p[0] == '/' && p[1] == '/') ||
1206 (p[0] == '<' && p[1] == '=') ||
1207 (p[0] == '>' && p[1] == '=') ||
1208 (p[0] == '=' && p[1] == '=') ||
1209 (p[0] == '!' && p[1] == '=') ||
1210 (p[0] == '<' && p[1] == '>') ||
1211 (p[0] == '&' && p[1] == '&') ||
1212 (p[0] == '|' && p[1] == '|') ||
1213 (p[0] == '^' && p[1] == '^')) {
1214 p++;
1215 }
1216 p++;
1217 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001218
H. Peter Anvine2c80182005-01-15 22:15:51 +00001219 /* Handling unterminated string by UNV */
1220 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001221 {
1222 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1223 t->text[p-line] = *line;
1224 tail = &t->next;
1225 }
1226 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001227 if (type != TOK_COMMENT) {
1228 *tail = t = new_Token(NULL, type, line, p - line);
1229 tail = &t->next;
1230 }
1231 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001232 }
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +04001233
1234 nasm_dump_token(list);
1235
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001236 return list;
1237}
1238
H. Peter Anvince616072002-04-30 21:02:23 +00001239/*
1240 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001241 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001242 * deleted only all at once by the delete_Blocks function.
1243 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001244static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001245{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001246 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001247
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001248 /* first, get to the end of the linked list */
1249 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001250 b = b->next;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001251
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001252 /* now allocate the requested chunk */
1253 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001254
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001255 /* now allocate a new block for the next request */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001256 b->next = nasm_zalloc(sizeof(Blocks));
1257
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001258 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001259}
1260
1261/*
1262 * this function deletes all managed blocks of memory
1263 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001264static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001265{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001266 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001267
H. Peter Anvin70653092007-10-19 14:42:29 -07001268 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001269 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001270 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001271 * free it.
1272 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001273 while (b) {
1274 if (b->chunk)
1275 nasm_free(b->chunk);
1276 a = b;
1277 b = b->next;
1278 if (a != &blocks)
1279 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001280 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001281}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001282
1283/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001284 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001285 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001286 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001287 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001288static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001289 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001290{
1291 Token *t;
1292 int i;
1293
H. Peter Anvin89cee572009-07-15 09:16:54 -04001294 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001295 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1296 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1297 freeTokens[i].next = &freeTokens[i + 1];
1298 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001299 }
1300 t = freeTokens;
1301 freeTokens = t->next;
1302 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001303 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001304 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001305 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001306 t->text = NULL;
1307 } else {
1308 if (txtlen == 0)
1309 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001310 t->text = nasm_malloc(txtlen+1);
1311 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001312 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001313 }
1314 return t;
1315}
1316
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001317static Token *copy_Token(Token * tline)
1318{
1319 Token *t, *tt, *first = NULL, *prev = NULL;
1320 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001321 for (tt = tline; tt != NULL; tt = tt->next) {
1322 if (!freeTokens) {
1323 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1324 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1325 freeTokens[i].next = &freeTokens[i + 1];
1326 freeTokens[i].next = NULL;
1327 }
1328 t = freeTokens;
1329 freeTokens = t->next;
1330 t->next = NULL;
1331 t->text = tt->text ? nasm_strdup(tt->text) : NULL;
1332 t->a.mac = tt->a.mac;
1333 t->a.len = tt->a.len;
1334 t->type = tt->type;
1335 if (prev != NULL) {
1336 prev->next = t;
1337 } else {
1338 first = t;
1339 }
1340 prev = t;
1341 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001342 return first;
1343}
1344
H. Peter Anvine2c80182005-01-15 22:15:51 +00001345static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001346{
1347 Token *next = t->next;
1348 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001349 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001350 freeTokens = t;
1351 return next;
1352}
1353
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001354/*
1355 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001356 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1357 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001358 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001359static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001360{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001361 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001362 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001363 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001364 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001365
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001366 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001367 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001368 char *v;
1369 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001370
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001371 v = t->text + 2;
1372 if (*v == '\'' || *v == '\"' || *v == '`') {
1373 size_t len = nasm_unquote(v, NULL);
1374 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001375
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001376 if (len != clen) {
1377 error(ERR_NONFATAL | ERR_PASS1,
1378 "NUL character in %! string");
1379 v = NULL;
1380 }
1381 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001382
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001383 if (v) {
1384 char *p = getenv(v);
1385 if (!p) {
1386 error(ERR_NONFATAL | ERR_PASS1,
1387 "nonexistent environment variable `%s'", v);
1388 p = "";
1389 }
1390 t->text = nasm_strdup(p);
1391 }
1392 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001393 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001394
H. Peter Anvine2c80182005-01-15 22:15:51 +00001395 /* Expand local macros here and not during preprocessing */
1396 if (expand_locals &&
1397 t->type == TOK_PREPROC_ID && t->text &&
1398 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001399 const char *q;
1400 char *p;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001401 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001402 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001403 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001404 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001405 p = nasm_strcat(buffer, q);
1406 nasm_free(t->text);
1407 t->text = p;
1408 }
1409 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001410
1411 /* Expand %? and %?? directives */
Keith Kanios3136d482010-11-13 09:34:34 -06001412 if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001413 ((t->type == TOK_PREPROC_Q) ||
1414 (t->type == TOK_PREPROC_QQ))) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001415 ExpInv *ei;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001416 for (ei = istk->expansion; ei != NULL; ei = ei->prev){
1417 if (ei->type == EXP_MMACRO) {
1418 nasm_free(t->text);
1419 if (t->type == TOK_PREPROC_Q) {
1420 t->text = nasm_strdup(ei->name);
1421 } else {
1422 t->text = nasm_strdup(ei->def->name);
1423 }
1424 break;
1425 }
1426 }
1427 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001428
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001429 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001431 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001432 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001433 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001434
H. Peter Anvin734b1882002-04-30 21:01:08 +00001435 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001436
1437 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001438 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001439 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001440 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001441 q = t->text;
1442 while (*q)
1443 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001444 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001445 }
1446 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001447
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001448 return line;
1449}
1450
1451/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001452 * Initialize a new Line
1453 */
Cyrill Gorcunov29cb0bb2010-11-11 11:19:43 +03001454static inline Line *new_Line(void)
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001455{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001456 return (Line *)nasm_zalloc(sizeof(Line));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001457}
1458
1459
1460/*
1461 * Initialize a new Expansion Definition
1462 */
1463static ExpDef *new_ExpDef(int exp_type)
1464{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001465 ExpDef *ed = (ExpDef*)nasm_zalloc(sizeof(ExpDef));
1466 ed->type = exp_type;
1467 ed->casesense = true;
1468 ed->state = COND_NEVER;
1469
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001470 return ed;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001471}
1472
1473
1474/*
1475 * Initialize a new Expansion Instance
1476 */
1477static ExpInv *new_ExpInv(int exp_type, ExpDef *ed)
1478{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001479 ExpInv *ei = (ExpInv*)nasm_zalloc(sizeof(ExpInv));
1480 ei->type = exp_type;
1481 ei->def = ed;
1482 ei->unique = ++unique;
1483
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001484 if ((istk->mmac_depth < 1) &&
1485 (istk->expansion == NULL) &&
1486 (ed != NULL) &&
1487 (ed->type != EXP_MMACRO) &&
1488 (ed->type != EXP_REP) &&
1489 (ed->type != EXP_WHILE)) {
1490 ei->linnum = src_get_linnum();
1491 src_set_linnum(ei->linnum - ed->linecount - 1);
1492 } else {
1493 ei->linnum = -1;
1494 }
1495 if ((istk->expansion == NULL) ||
1496 (ei->type == EXP_MMACRO)) {
1497 ei->relno = 0;
1498 } else {
1499 ei->relno = istk->expansion->lineno;
1500 if (ed != NULL) {
1501 ei->relno -= (ed->linecount + 1);
1502 }
1503 }
1504 return ei;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001505}
1506
1507/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001508 * A scanner, suitable for use by the expression evaluator, which
1509 * operates on a line of Tokens. Expects a pointer to a pointer to
1510 * the first token in the line to be passed in as its private_data
1511 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001512 *
1513 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001514 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001515static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001516{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001517 Token **tlineptr = private_data;
1518 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001519 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001520
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 do {
1522 tline = *tlineptr;
1523 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001524 } while (tline && (tline->type == TOK_WHITESPACE ||
1525 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001526
1527 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001528 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001529
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001530 tokval->t_charptr = tline->text;
1531
H. Peter Anvin76690a12002-04-30 20:52:49 +00001532 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001533 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001534 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001535 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001536
H. Peter Anvine2c80182005-01-15 22:15:51 +00001537 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001538 p = tokval->t_charptr = tline->text;
1539 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001540 tokval->t_charptr++;
1541 return tokval->t_type = TOKEN_ID;
1542 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001543
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001544 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001545 if (r >= p+MAX_KEYWORD)
1546 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001547 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001548 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001549 *s = '\0';
1550 /* right, so we have an identifier sitting in temp storage. now,
1551 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001552 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001553 }
1554
H. Peter Anvine2c80182005-01-15 22:15:51 +00001555 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001556 bool rn_error;
1557 tokval->t_integer = readnum(tline->text, &rn_error);
1558 tokval->t_charptr = tline->text;
1559 if (rn_error)
1560 return tokval->t_type = TOKEN_ERRNUM;
1561 else
1562 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001563 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001564
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001565 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001566 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001567 }
1568
H. Peter Anvine2c80182005-01-15 22:15:51 +00001569 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001570 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001571
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001572 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001573 tokval->t_charptr = tline->text;
1574 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001575
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001576 if (ep[0] != bq || ep[1] != '\0')
1577 return tokval->t_type = TOKEN_ERRSTR;
1578 else
1579 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001580 }
1581
H. Peter Anvine2c80182005-01-15 22:15:51 +00001582 if (tline->type == TOK_OTHER) {
1583 if (!strcmp(tline->text, "<<"))
1584 return tokval->t_type = TOKEN_SHL;
1585 if (!strcmp(tline->text, ">>"))
1586 return tokval->t_type = TOKEN_SHR;
1587 if (!strcmp(tline->text, "//"))
1588 return tokval->t_type = TOKEN_SDIV;
1589 if (!strcmp(tline->text, "%%"))
1590 return tokval->t_type = TOKEN_SMOD;
1591 if (!strcmp(tline->text, "=="))
1592 return tokval->t_type = TOKEN_EQ;
1593 if (!strcmp(tline->text, "<>"))
1594 return tokval->t_type = TOKEN_NE;
1595 if (!strcmp(tline->text, "!="))
1596 return tokval->t_type = TOKEN_NE;
1597 if (!strcmp(tline->text, "<="))
1598 return tokval->t_type = TOKEN_LE;
1599 if (!strcmp(tline->text, ">="))
1600 return tokval->t_type = TOKEN_GE;
1601 if (!strcmp(tline->text, "&&"))
1602 return tokval->t_type = TOKEN_DBL_AND;
1603 if (!strcmp(tline->text, "^^"))
1604 return tokval->t_type = TOKEN_DBL_XOR;
1605 if (!strcmp(tline->text, "||"))
1606 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001607 }
1608
1609 /*
1610 * We have no other options: just return the first character of
1611 * the token text.
1612 */
1613 return tokval->t_type = tline->text[0];
1614}
1615
1616/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001617 * Compare a string to the name of an existing macro; this is a
1618 * simple wrapper which calls either strcmp or nasm_stricmp
1619 * depending on the value of the `casesense' parameter.
1620 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001621static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001622{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001623 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001624}
1625
1626/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001627 * Compare a string to the name of an existing macro; this is a
1628 * simple wrapper which calls either strcmp or nasm_stricmp
1629 * depending on the value of the `casesense' parameter.
1630 */
1631static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1632{
1633 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1634}
1635
1636/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001637 * Return the Context structure associated with a %$ token. Return
1638 * NULL, having _already_ reported an error condition, if the
1639 * context stack isn't deep enough for the supplied number of $
1640 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001641 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001642 * also scanned for such smacro, until it is found; if not -
1643 * only the context that directly results from the number of $'s
1644 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001645 *
1646 * If "namep" is non-NULL, set it to the pointer to the macro name
1647 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001648 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001649static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001650 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001651{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001652 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001653 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001654 int i;
1655
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001656 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001657 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001658
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001659 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001660 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001661
H. Peter Anvine2c80182005-01-15 22:15:51 +00001662 if (!cstk) {
1663 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1664 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001665 }
1666
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001667 name += 2;
1668 ctx = cstk;
1669 i = 0;
1670 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001671 name++;
1672 i++;
1673 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001674 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001675 if (!ctx) {
1676 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001677 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001678 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001679 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001680
1681 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001682 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001683
Keith Kanios404589e2010-08-10 20:12:57 -05001684 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001686
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001687 do {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001688 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001689 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 while (m) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001691 if (!mstrcmp(m->name, name, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001692 return ctx;
1693 m = m->next;
1694 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001695 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001696 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001697 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001698 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001699}
1700
1701/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001702 * Check to see if a file is already in a string list
1703 */
1704static bool in_list(const StrList *list, const char *str)
1705{
1706 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001707 if (!strcmp(list->str, str))
1708 return true;
1709 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001710 }
1711 return false;
1712}
1713
1714/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001715 * Open an include file. This routine must always return a valid
1716 * file pointer if it returns - it's responsible for throwing an
1717 * ERR_FATAL and bombing out completely if not. It should also try
1718 * the include path one by one until it finds the file or reaches
1719 * the end of the path.
1720 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001721static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001722 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001723{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001724 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001725 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001726 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001727 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001728 size_t prefix_len = 0;
1729 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001730
H. Peter Anvine2c80182005-01-15 22:15:51 +00001731 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001732 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunov6f38fe62010-11-11 11:32:16 +03001733 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001734 memcpy(sl->str, prefix, prefix_len);
1735 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001736 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001737 if (fp && dhead && !in_list(*dhead, sl->str)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001738 **dtail = sl;
1739 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001740 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001741 nasm_free(sl);
1742 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001743 if (fp)
1744 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001745 if (!ip) {
1746 if (!missing_ok)
1747 break;
1748 prefix = NULL;
1749 } else {
1750 prefix = ip->path;
1751 ip = ip->next;
1752 }
1753 if (prefix) {
1754 prefix_len = strlen(prefix);
1755 } else {
1756 /* -MG given and file not found */
1757 if (dhead && !in_list(*dhead, file)) {
1758 sl = nasm_malloc(len+1+sizeof sl->next);
1759 sl->next = NULL;
1760 strcpy(sl->str, file);
1761 **dtail = sl;
1762 *dtail = &sl->next;
1763 }
1764 return NULL;
1765 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001766 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001767
H. Peter Anvin734b1882002-04-30 21:01:08 +00001768 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001769 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001770}
1771
1772/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001773 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001774 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001775 * return true if _any_ single-line macro of that name is defined.
1776 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001777 * `nparam' or no parameters is defined.
1778 *
1779 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001780 * defined, or nparam is -1, the address of the definition structure
1781 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001782 * is NULL, no action will be taken regarding its contents, and no
1783 * error will occur.
1784 *
1785 * Note that this is also called with nparam zero to resolve
1786 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001787 *
1788 * If you already know which context macro belongs to, you can pass
1789 * the context pointer as first parameter; if you won't but name begins
1790 * with %$ the context will be automatically computed. If all_contexts
1791 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001792 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001793static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001794smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001795 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001796{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001797 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001798 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001799
H. Peter Anvin97a23472007-09-16 17:57:25 -07001800 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001801 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001802 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001803 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001804 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001805 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001806 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001807 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001808 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001809 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001810 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001811 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001812
H. Peter Anvine2c80182005-01-15 22:15:51 +00001813 while (m) {
1814 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001815 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001816 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001817 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001818 *defn = m;
1819 else
1820 *defn = NULL;
1821 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001822 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823 }
1824 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001825 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001826
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001827 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001828}
1829
1830/*
1831 * Count and mark off the parameters in a multi-line macro call.
1832 * This is called both from within the multi-line macro expansion
1833 * code, and also to mark off the default parameters when provided
1834 * in a %macro definition line.
1835 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001836static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001837{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001838 int paramsize, brace;
1839
1840 *nparam = paramsize = 0;
1841 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001842 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001843 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001844 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001845 paramsize += PARAM_DELTA;
1846 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1847 }
1848 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001849 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001850 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001851 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001852 (*params)[(*nparam)++] = t;
1853 while (tok_isnt_(t, brace ? "}" : ","))
1854 t = t->next;
1855 if (t) { /* got a comma/brace */
1856 t = t->next;
1857 if (brace) {
1858 /*
1859 * Now we've found the closing brace, look further
1860 * for the comma.
1861 */
1862 skip_white_(t);
1863 if (tok_isnt_(t, ",")) {
1864 error(ERR_NONFATAL,
1865 "braces do not enclose all of macro parameter");
1866 while (tok_isnt_(t, ","))
1867 t = t->next;
1868 }
1869 if (t)
1870 t = t->next; /* eat the comma */
1871 }
1872 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001873 }
1874}
1875
1876/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001877 * Determine whether one of the various `if' conditions is true or
1878 * not.
1879 *
1880 * We must free the tline we get passed.
1881 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001882static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001883{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001884 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001885 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001886 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001887 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001888 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001889 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001890 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001891
1892 origline = tline;
1893
H. Peter Anvine2c80182005-01-15 22:15:51 +00001894 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001895 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001896 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001897 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001898 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001899 if (!tline)
1900 break;
1901 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001902 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001903 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001904 free_tlist(origline);
1905 return -1;
1906 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001907 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001908 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001909 tline = tline->next;
1910 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001911 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001912
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001913 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001914 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001915 while (tline) {
1916 skip_white_(tline);
1917 if (!tline || (tline->type != TOK_ID &&
1918 (tline->type != TOK_PREPROC_ID ||
1919 tline->text[1] != '$'))) {
1920 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001921 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001922 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001923 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001924 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001925 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001926 tline = tline->next;
1927 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001928 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001929
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001930 case PPC_IFENV:
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001931 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001932 j = false; /* have we matched yet? */
1933 while (tline) {
1934 skip_white_(tline);
1935 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001936 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001937 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001938 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001939 error(ERR_NONFATAL,
1940 "`%s' expects environment variable names",
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001941 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001942 goto fail;
1943 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001944 p = tline->text;
1945 if (tline->type == TOK_PREPROC_ID)
1946 p += 2; /* Skip leading %! */
1947 if (*p == '\'' || *p == '\"' || *p == '`')
1948 nasm_unquote_cstr(p, ct);
1949 if (getenv(p))
1950 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001951 tline = tline->next;
1952 }
1953 break;
1954
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001955 case PPC_IFIDN:
1956 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001957 tline = expand_smacro(tline);
1958 t = tt = tline;
1959 while (tok_isnt_(tt, ","))
1960 tt = tt->next;
1961 if (!tt) {
1962 error(ERR_NONFATAL,
1963 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001964 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001965 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001966 }
1967 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001968 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001969 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1970 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1971 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001972 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001973 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001974 }
1975 if (t->type == TOK_WHITESPACE) {
1976 t = t->next;
1977 continue;
1978 }
1979 if (tt->type == TOK_WHITESPACE) {
1980 tt = tt->next;
1981 continue;
1982 }
1983 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001984 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001985 break;
1986 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001987 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001988 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001989 size_t l1 = nasm_unquote(t->text, NULL);
1990 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001991
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001992 if (l1 != l2) {
1993 j = false;
1994 break;
1995 }
1996 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1997 j = false;
1998 break;
1999 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002000 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002001 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002002 break;
2003 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002004
H. Peter Anvine2c80182005-01-15 22:15:51 +00002005 t = t->next;
2006 tt = tt->next;
2007 }
2008 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002009 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002010 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002011
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002012 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002013 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002014 bool found = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002015 ExpDef searching, *ed;
H. Peter Anvin65747262002-05-07 00:10:05 +00002016
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002017 skip_white_(tline);
2018 tline = expand_id(tline);
2019 if (!tok_type_(tline, TOK_ID)) {
2020 error(ERR_NONFATAL,
2021 "`%s' expects a macro name", pp_directives[ct]);
2022 goto fail;
2023 }
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002024 memset(&searching, 0, sizeof(searching));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002025 searching.name = nasm_strdup(tline->text);
2026 searching.casesense = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002027 searching.nparam_max = INT_MAX;
2028 tline = expand_smacro(tline->next);
2029 skip_white_(tline);
2030 if (!tline) {
2031 } else if (!tok_type_(tline, TOK_NUMBER)) {
2032 error(ERR_NONFATAL,
2033 "`%s' expects a parameter count or nothing",
2034 pp_directives[ct]);
2035 } else {
2036 searching.nparam_min = searching.nparam_max =
2037 readnum(tline->text, &j);
2038 if (j)
2039 error(ERR_NONFATAL,
2040 "unable to parse parameter count `%s'",
2041 tline->text);
2042 }
2043 if (tline && tok_is_(tline->next, "-")) {
2044 tline = tline->next->next;
2045 if (tok_is_(tline, "*"))
2046 searching.nparam_max = INT_MAX;
2047 else if (!tok_type_(tline, TOK_NUMBER))
2048 error(ERR_NONFATAL,
2049 "`%s' expects a parameter count after `-'",
2050 pp_directives[ct]);
2051 else {
2052 searching.nparam_max = readnum(tline->text, &j);
2053 if (j)
2054 error(ERR_NONFATAL,
2055 "unable to parse parameter count `%s'",
2056 tline->text);
2057 if (searching.nparam_min > searching.nparam_max)
2058 error(ERR_NONFATAL,
2059 "minimum parameter count exceeds maximum");
2060 }
2061 }
2062 if (tline && tok_is_(tline->next, "+")) {
2063 tline = tline->next;
2064 searching.plus = true;
2065 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002066 ed = (ExpDef *) hash_findix(&expdefs, searching.name);
2067 while (ed != NULL) {
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002068 if (!strcmp(ed->name, searching.name) &&
2069 (ed->nparam_min <= searching.nparam_max || searching.plus) &&
2070 (searching.nparam_min <= ed->nparam_max || ed->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002071 found = true;
2072 break;
2073 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002074 ed = ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002075 }
2076 if (tline && tline->next)
2077 error(ERR_WARNING|ERR_PASS1,
2078 "trailing garbage after %%ifmacro ignored");
2079 nasm_free(searching.name);
2080 j = found;
2081 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002082 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002083
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002084 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002085 needtype = TOK_ID;
2086 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002087 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002088 needtype = TOK_NUMBER;
2089 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002090 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002091 needtype = TOK_STRING;
2092 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002093
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002094iftype:
2095 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002096
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002097 while (tok_type_(t, TOK_WHITESPACE) ||
2098 (needtype == TOK_NUMBER &&
2099 tok_type_(t, TOK_OTHER) &&
2100 (t->text[0] == '-' || t->text[0] == '+') &&
2101 !t->text[1]))
2102 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002103
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002104 j = tok_type_(t, needtype);
2105 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002106
2107 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002108 t = tline = expand_smacro(tline);
2109 while (tok_type_(t, TOK_WHITESPACE))
2110 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002111
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002112 j = false;
2113 if (t) {
2114 t = t->next; /* Skip the actual token */
2115 while (tok_type_(t, TOK_WHITESPACE))
2116 t = t->next;
2117 j = !t; /* Should be nothing left */
2118 }
2119 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002120
H. Peter Anvin134b9462008-02-16 17:01:40 -08002121 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002122 t = tline = expand_smacro(tline);
2123 while (tok_type_(t, TOK_WHITESPACE))
2124 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002125
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002126 j = !t; /* Should be empty */
2127 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002128
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002129 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002130 t = tline = expand_smacro(tline);
2131 tptr = &t;
2132 tokval.t_type = TOKEN_INVALID;
2133 evalresult = evaluate(ppscan, tptr, &tokval,
2134 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002135 if (!evalresult)
2136 return -1;
2137 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002138 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002139 "trailing garbage after expression ignored");
2140 if (!is_simple(evalresult)) {
2141 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002142 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002143 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002144 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002145 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002146 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002147
H. Peter Anvine2c80182005-01-15 22:15:51 +00002148 default:
2149 error(ERR_FATAL,
2150 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002151 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002152 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002153 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002154
2155 free_tlist(origline);
2156 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002157
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002158fail:
2159 free_tlist(origline);
2160 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002161}
2162
2163/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002164 * Common code for defining an smacro
2165 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002166static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002167 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002168{
2169 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002170 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002171
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002172 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002173 if (!smac) {
2174 error(ERR_WARNING|ERR_PASS1,
2175 "single-line macro `%s' defined both with and"
2176 " without parameters", mname);
2177 /*
2178 * Some instances of the old code considered this a failure,
2179 * some others didn't. What is the right thing to do here?
2180 */
2181 free_tlist(expansion);
2182 return false; /* Failure */
2183 } else {
2184 /*
2185 * We're redefining, so we have to take over an
2186 * existing SMacro structure. This means freeing
2187 * what was already in it.
2188 */
2189 nasm_free(smac->name);
2190 free_tlist(smac->expansion);
2191 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002192 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002193 smtbl = ctx ? &ctx->localmac : &smacros;
2194 smhead = (SMacro **) hash_findi_add(smtbl, mname);
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002195 smac = nasm_zalloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002196 smac->next = *smhead;
2197 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002198 }
2199 smac->name = nasm_strdup(mname);
2200 smac->casesense = casesense;
2201 smac->nparam = nparam;
2202 smac->expansion = expansion;
2203 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002204 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002205}
2206
2207/*
2208 * Undefine an smacro
2209 */
2210static void undef_smacro(Context *ctx, const char *mname)
2211{
2212 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002213 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002214
H. Peter Anvin166c2472008-05-28 12:28:58 -07002215 smtbl = ctx ? &ctx->localmac : &smacros;
2216 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002217
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002218 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002219 /*
2220 * We now have a macro name... go hunt for it.
2221 */
2222 sp = smhead;
2223 while ((s = *sp) != NULL) {
2224 if (!mstrcmp(s->name, mname, s->casesense)) {
2225 *sp = s->next;
2226 nasm_free(s->name);
2227 free_tlist(s->expansion);
2228 nasm_free(s);
2229 } else {
2230 sp = &s->next;
2231 }
2232 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002233 }
2234}
2235
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002236/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002237 * Parse a mmacro specification.
2238 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002239static bool parse_mmacro_spec(Token *tline, ExpDef *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002240{
2241 bool err;
2242
2243 tline = tline->next;
2244 skip_white_(tline);
2245 tline = expand_id(tline);
2246 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002247 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2248 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002249 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002250
H. Peter Anvina26433d2008-07-16 14:40:01 -07002251 def->name = nasm_strdup(tline->text);
2252 def->plus = false;
2253 def->nolist = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002254// def->in_progress = 0;
2255// def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002256 def->nparam_min = 0;
2257 def->nparam_max = 0;
2258
H. Peter Anvina26433d2008-07-16 14:40:01 -07002259 tline = expand_smacro(tline->next);
2260 skip_white_(tline);
2261 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002262 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002263 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002264 def->nparam_min = def->nparam_max =
2265 readnum(tline->text, &err);
2266 if (err)
2267 error(ERR_NONFATAL,
2268 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002269 }
2270 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002271 tline = tline->next->next;
2272 if (tok_is_(tline, "*")) {
2273 def->nparam_max = INT_MAX;
2274 } else if (!tok_type_(tline, TOK_NUMBER)) {
2275 error(ERR_NONFATAL,
2276 "`%s' expects a parameter count after `-'", directive);
2277 } else {
2278 def->nparam_max = readnum(tline->text, &err);
2279 if (err) {
2280 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2281 tline->text);
2282 }
2283 if (def->nparam_min > def->nparam_max) {
2284 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2285 }
2286 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002287 }
2288 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002289 tline = tline->next;
2290 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002291 }
2292 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002293 !nasm_stricmp(tline->next->text, ".nolist")) {
2294 tline = tline->next;
2295 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002296 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002297
H. Peter Anvina26433d2008-07-16 14:40:01 -07002298 /*
2299 * Handle default parameters.
2300 */
2301 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002302 def->dlist = tline->next;
2303 tline->next = NULL;
2304 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002305 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002306 def->dlist = NULL;
2307 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002308 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002309 def->line = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002310
H. Peter Anvin89cee572009-07-15 09:16:54 -04002311 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002312 !def->plus)
2313 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2314 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002315
H. Peter Anvina26433d2008-07-16 14:40:01 -07002316 return true;
2317}
2318
2319
2320/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002321 * Decode a size directive
2322 */
2323static int parse_size(const char *str) {
2324 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002325 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002326 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002327 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002328
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002329 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002330}
2331
Ed Beroset3ab3f412002-06-11 03:31:49 +00002332/**
2333 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002334 * Find out if a line contains a preprocessor directive, and deal
2335 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002336 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002337 * If a directive _is_ found, it is the responsibility of this routine
2338 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002339 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002340 * @param tline a pointer to the current tokeninzed line linked list
2341 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002342 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002343 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002344static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002345{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002346 enum preproc_token i;
2347 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002348 bool err;
2349 int nparam;
2350 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002351 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002352 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002353 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002354 char *p, *pp;
2355 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002356 Include *inc;
2357 Context *ctx;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002358 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002359 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002360 struct tokenval tokval;
2361 expr *evalresult;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002362 ExpDef *ed, *eed, **edhead;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002363 ExpInv *ei, *eei;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002364 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002365 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002366 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002367
2368 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002369
H. Peter Anvineba20a72002-04-30 20:53:55 +00002370 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002371 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002372 (tline->text[1] == '%' || tline->text[1] == '$'
2373 || tline->text[1] == '!'))
2374 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002375
H. Peter Anvin4169a472007-09-12 01:29:43 +00002376 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002377
H. Peter Anvin4169a472007-09-12 01:29:43 +00002378 switch (i) {
2379 case PP_INVALID:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002380 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002381 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2382 tline->text);
2383 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002384
H. Peter Anvine2c80182005-01-15 22:15:51 +00002385 case PP_STACKSIZE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002386 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002387 /* Directive to tell NASM what the default stack size is. The
2388 * default is for a 16-bit stack, and this can be overriden with
2389 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002390 */
2391 tline = tline->next;
2392 if (tline && tline->type == TOK_WHITESPACE)
2393 tline = tline->next;
2394 if (!tline || tline->type != TOK_ID) {
2395 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2396 free_tlist(origline);
2397 return DIRECTIVE_FOUND;
2398 }
2399 if (nasm_stricmp(tline->text, "flat") == 0) {
2400 /* All subsequent ARG directives are for a 32-bit stack */
2401 StackSize = 4;
2402 StackPointer = "ebp";
2403 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002404 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002405 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2406 /* All subsequent ARG directives are for a 64-bit stack */
2407 StackSize = 8;
2408 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002409 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002410 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002411 } else if (nasm_stricmp(tline->text, "large") == 0) {
2412 /* All subsequent ARG directives are for a 16-bit stack,
2413 * far function call.
2414 */
2415 StackSize = 2;
2416 StackPointer = "bp";
2417 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002418 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002419 } else if (nasm_stricmp(tline->text, "small") == 0) {
2420 /* All subsequent ARG directives are for a 16-bit stack,
2421 * far function call. We don't support near functions.
2422 */
2423 StackSize = 2;
2424 StackPointer = "bp";
2425 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002426 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002427 } else {
2428 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2429 free_tlist(origline);
2430 return DIRECTIVE_FOUND;
2431 }
2432 free_tlist(origline);
2433 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002434
H. Peter Anvine2c80182005-01-15 22:15:51 +00002435 case PP_ARG:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002436 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002437 /* TASM like ARG directive to define arguments to functions, in
2438 * the following form:
2439 *
2440 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2441 */
2442 offset = ArgOffset;
2443 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002444 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002445 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002446
H. Peter Anvine2c80182005-01-15 22:15:51 +00002447 /* Find the argument name */
2448 tline = tline->next;
2449 if (tline && tline->type == TOK_WHITESPACE)
2450 tline = tline->next;
2451 if (!tline || tline->type != TOK_ID) {
2452 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2453 free_tlist(origline);
2454 return DIRECTIVE_FOUND;
2455 }
2456 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002457
H. Peter Anvine2c80182005-01-15 22:15:51 +00002458 /* Find the argument size type */
2459 tline = tline->next;
2460 if (!tline || tline->type != TOK_OTHER
2461 || tline->text[0] != ':') {
2462 error(ERR_NONFATAL,
2463 "Syntax error processing `%%arg' directive");
2464 free_tlist(origline);
2465 return DIRECTIVE_FOUND;
2466 }
2467 tline = tline->next;
2468 if (!tline || tline->type != TOK_ID) {
2469 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2470 free_tlist(origline);
2471 return DIRECTIVE_FOUND;
2472 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002473
H. Peter Anvine2c80182005-01-15 22:15:51 +00002474 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002475 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002476 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002477 size = parse_size(tt->text);
2478 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002479 error(ERR_NONFATAL,
2480 "Invalid size type for `%%arg' missing directive");
2481 free_tlist(tt);
2482 free_tlist(origline);
2483 return DIRECTIVE_FOUND;
2484 }
2485 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002486
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002487 /* Round up to even stack slots */
2488 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002489
H. Peter Anvine2c80182005-01-15 22:15:51 +00002490 /* Now define the macro for the argument */
2491 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2492 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002493 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002494 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002495
H. Peter Anvine2c80182005-01-15 22:15:51 +00002496 /* Move to the next argument in the list */
2497 tline = tline->next;
2498 if (tline && tline->type == TOK_WHITESPACE)
2499 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002500 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002501 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002502 free_tlist(origline);
2503 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002504
H. Peter Anvine2c80182005-01-15 22:15:51 +00002505 case PP_LOCAL:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002506 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002507 /* TASM like LOCAL directive to define local variables for a
2508 * function, in the following form:
2509 *
2510 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2511 *
2512 * The '= LocalSize' at the end is ignored by NASM, but is
2513 * required by TASM to define the local parameter size (and used
2514 * by the TASM macro package).
2515 */
2516 offset = LocalOffset;
2517 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002518 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002519 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002520
H. Peter Anvine2c80182005-01-15 22:15:51 +00002521 /* Find the argument name */
2522 tline = tline->next;
2523 if (tline && tline->type == TOK_WHITESPACE)
2524 tline = tline->next;
2525 if (!tline || tline->type != TOK_ID) {
2526 error(ERR_NONFATAL,
2527 "`%%local' missing argument parameter");
2528 free_tlist(origline);
2529 return DIRECTIVE_FOUND;
2530 }
2531 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002532
H. Peter Anvine2c80182005-01-15 22:15:51 +00002533 /* Find the argument size type */
2534 tline = tline->next;
2535 if (!tline || tline->type != TOK_OTHER
2536 || tline->text[0] != ':') {
2537 error(ERR_NONFATAL,
2538 "Syntax error processing `%%local' directive");
2539 free_tlist(origline);
2540 return DIRECTIVE_FOUND;
2541 }
2542 tline = tline->next;
2543 if (!tline || tline->type != TOK_ID) {
2544 error(ERR_NONFATAL,
2545 "`%%local' missing size type parameter");
2546 free_tlist(origline);
2547 return DIRECTIVE_FOUND;
2548 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002549
H. Peter Anvine2c80182005-01-15 22:15:51 +00002550 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002551 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002552 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002553 size = parse_size(tt->text);
2554 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002555 error(ERR_NONFATAL,
2556 "Invalid size type for `%%local' missing directive");
2557 free_tlist(tt);
2558 free_tlist(origline);
2559 return DIRECTIVE_FOUND;
2560 }
2561 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002562
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002563 /* Round up to even stack slots */
2564 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002565
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002566 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002567
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002568 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002569 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2570 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002571 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002572
H. Peter Anvine2c80182005-01-15 22:15:51 +00002573 /* Now define the assign to setup the enter_c macro correctly */
2574 snprintf(directive, sizeof(directive),
2575 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002576 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002577
H. Peter Anvine2c80182005-01-15 22:15:51 +00002578 /* Move to the next argument in the list */
2579 tline = tline->next;
2580 if (tline && tline->type == TOK_WHITESPACE)
2581 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002582 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002583 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002584 free_tlist(origline);
2585 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002586
H. Peter Anvine2c80182005-01-15 22:15:51 +00002587 case PP_CLEAR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002588 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002589 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002590 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002591 "trailing garbage after `%%clear' ignored");
2592 free_macros();
2593 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002594 free_tlist(origline);
2595 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002596
H. Peter Anvin418ca702008-05-30 10:42:30 -07002597 case PP_DEPEND:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002598 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002599 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002600 skip_white_(t);
2601 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002602 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002603 error(ERR_NONFATAL, "`%%depend' expects a file name");
2604 free_tlist(origline);
2605 return DIRECTIVE_FOUND; /* but we did _something_ */
2606 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002607 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002608 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002609 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002610 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002611 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002612 nasm_unquote_cstr(p, i);
2613 if (dephead && !in_list(*dephead, p)) {
2614 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2615 sl->next = NULL;
2616 strcpy(sl->str, p);
2617 *deptail = sl;
2618 deptail = &sl->next;
2619 }
2620 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002621 return DIRECTIVE_FOUND;
2622
2623 case PP_INCLUDE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002624 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002625 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002626 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002627
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002628 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002629 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002630 error(ERR_NONFATAL, "`%%include' expects a file name");
2631 free_tlist(origline);
2632 return DIRECTIVE_FOUND; /* but we did _something_ */
2633 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002634 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002635 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002636 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002637 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002638 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002639 nasm_unquote_cstr(p, i);
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03002640 inc = nasm_zalloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002641 inc->next = istk;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002642 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002643 if (!inc->fp) {
2644 /* -MG given but file not found */
2645 nasm_free(inc);
2646 } else {
2647 inc->fname = src_set_fname(nasm_strdup(p));
2648 inc->lineno = src_set_linnum(0);
2649 inc->lineinc = 1;
2650 inc->expansion = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002651 istk = inc;
2652 list->uplevel(LIST_INCLUDE);
2653 }
2654 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002655 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002656
H. Peter Anvind2456592008-06-19 15:04:18 -07002657 case PP_USE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002658 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002659 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002660 static macros_t *use_pkg;
2661 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002662
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002663 tline = tline->next;
2664 skip_white_(tline);
2665 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002666
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002667 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002668 tline->type != TOK_INTERNAL_STRING &&
2669 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002670 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002671 free_tlist(origline);
2672 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002673 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002674 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002675 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002676 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002677 if (tline->type == TOK_STRING)
2678 nasm_unquote_cstr(tline->text, i);
2679 use_pkg = nasm_stdmac_find_package(tline->text);
2680 if (!use_pkg)
2681 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2682 else
2683 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002684 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002685 /* Not already included, go ahead and include it */
2686 stdmacpos = use_pkg;
2687 }
2688 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002689 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002690 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002691 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002692 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002693 case PP_POP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002694 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002695 tline = tline->next;
2696 skip_white_(tline);
2697 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002698 if (tline) {
2699 if (!tok_type_(tline, TOK_ID)) {
2700 error(ERR_NONFATAL, "`%s' expects a context identifier",
2701 pp_directives[i]);
2702 free_tlist(origline);
2703 return DIRECTIVE_FOUND; /* but we did _something_ */
2704 }
2705 if (tline->next)
2706 error(ERR_WARNING|ERR_PASS1,
2707 "trailing garbage after `%s' ignored",
2708 pp_directives[i]);
2709 p = nasm_strdup(tline->text);
2710 } else {
2711 p = NULL; /* Anonymous */
2712 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002713
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002714 if (i == PP_PUSH) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002715 ctx = nasm_zalloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002716 ctx->next = cstk;
2717 hash_init(&ctx->localmac, HASH_SMALL);
2718 ctx->name = p;
2719 ctx->number = unique++;
2720 cstk = ctx;
2721 } else {
2722 /* %pop or %repl */
2723 if (!cstk) {
2724 error(ERR_NONFATAL, "`%s': context stack is empty",
2725 pp_directives[i]);
2726 } else if (i == PP_POP) {
2727 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2728 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2729 "expected %s",
2730 cstk->name ? cstk->name : "anonymous", p);
2731 else
2732 ctx_pop();
2733 } else {
2734 /* i == PP_REPL */
2735 nasm_free(cstk->name);
2736 cstk->name = p;
2737 p = NULL;
2738 }
2739 nasm_free(p);
2740 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002741 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002742 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002743 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002744 severity = ERR_FATAL;
2745 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002746 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002747 severity = ERR_NONFATAL;
2748 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002749 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002750 severity = ERR_WARNING|ERR_WARN_USER;
2751 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002752
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002753issue_error:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002754 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002755 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002756 /* Only error out if this is the final pass */
2757 if (pass != 2 && i != PP_FATAL)
2758 return DIRECTIVE_FOUND;
2759
2760 tline->next = expand_smacro(tline->next);
2761 tline = tline->next;
2762 skip_white_(tline);
2763 t = tline ? tline->next : NULL;
2764 skip_white_(t);
2765 if (tok_type_(tline, TOK_STRING) && !t) {
2766 /* The line contains only a quoted string */
2767 p = tline->text;
2768 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2769 error(severity, "%s", p);
2770 } else {
2771 /* Not a quoted string, or more than a quoted string */
2772 p = detoken(tline, false);
2773 error(severity, "%s", p);
2774 nasm_free(p);
2775 }
2776 free_tlist(origline);
2777 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002778 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002779
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002780 CASE_PP_IF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002781 if (defining != NULL) {
2782 if (defining->type == EXP_IF) {
2783 defining->def_depth ++;
2784 }
2785 return NO_DIRECTIVE_FOUND;
2786 }
2787 if ((istk->expansion != NULL) &&
2788 (istk->expansion->emitting == false)) {
2789 j = COND_NEVER;
2790 } else {
2791 j = if_condition(tline->next, i);
2792 tline->next = NULL; /* it got freed */
2793 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
2794 }
2795 ed = new_ExpDef(EXP_IF);
2796 ed->state = j;
2797 ed->nolist = NULL;
2798 ed->def_depth = 0;
2799 ed->cur_depth = 0;
2800 ed->max_depth = 0;
2801 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
2802 ed->prev = defining;
2803 defining = ed;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002804 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002805 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002806
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002807 CASE_PP_ELIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002808 if (defining != NULL) {
2809 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2810 return NO_DIRECTIVE_FOUND;
2811 }
2812 }
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +04002813 if ((defining == NULL) || (defining->type != EXP_IF)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002814 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2815 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002816 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002817 case COND_IF_TRUE:
2818 defining->state = COND_DONE;
2819 defining->ignoring = true;
2820 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002821
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002822 case COND_DONE:
2823 case COND_NEVER:
2824 defining->ignoring = true;
2825 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002826
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002827 case COND_ELSE_TRUE:
2828 case COND_ELSE_FALSE:
2829 error_precond(ERR_WARNING|ERR_PASS1,
2830 "`%%elif' after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002831 defining->state = COND_NEVER;
2832 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002833 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002834
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002835 case COND_IF_FALSE:
2836 /*
2837 * IMPORTANT: In the case of %if, we will already have
2838 * called expand_mmac_params(); however, if we're
2839 * processing an %elif we must have been in a
2840 * non-emitting mode, which would have inhibited
2841 * the normal invocation of expand_mmac_params().
2842 * Therefore, we have to do it explicitly here.
2843 */
2844 j = if_condition(expand_mmac_params(tline->next), i);
2845 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002846 defining->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002847 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002848 defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002849 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002850 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002851 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002852 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002853
H. Peter Anvine2c80182005-01-15 22:15:51 +00002854 case PP_ELSE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002855 if (defining != NULL) {
2856 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2857 return NO_DIRECTIVE_FOUND;
2858 }
2859 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002860 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002861 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002862 "trailing garbage after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002863 if ((defining == NULL) || (defining->type != EXP_IF)) {
2864 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2865 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002866 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002867 case COND_IF_TRUE:
2868 case COND_DONE:
2869 defining->state = COND_ELSE_FALSE;
2870 defining->ignoring = true;
2871 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002872
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002873 case COND_NEVER:
2874 defining->ignoring = true;
2875 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002876
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002877 case COND_IF_FALSE:
2878 defining->state = COND_ELSE_TRUE;
2879 defining->ignoring = false;
2880 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002881
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002882 case COND_ELSE_TRUE:
2883 case COND_ELSE_FALSE:
2884 error_precond(ERR_WARNING|ERR_PASS1,
2885 "`%%else' after `%%else' ignored.");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002886 defining->state = COND_NEVER;
2887 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002888 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002889 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002890 free_tlist(origline);
2891 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002892
H. Peter Anvine2c80182005-01-15 22:15:51 +00002893 case PP_ENDIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002894 if (defining != NULL) {
2895 if (defining->type == EXP_IF) {
2896 if (defining->def_depth > 0) {
2897 defining->def_depth --;
2898 return NO_DIRECTIVE_FOUND;
2899 }
2900 } else {
2901 return NO_DIRECTIVE_FOUND;
2902 }
2903 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002904 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002905 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002906 "trailing garbage after `%%endif' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002907 if ((defining == NULL) || (defining->type != EXP_IF)) {
2908 error(ERR_NONFATAL, "`%%endif': no matching `%%if'");
2909 return DIRECTIVE_FOUND;
2910 }
2911 ed = defining;
2912 defining = ed->prev;
2913 ed->prev = expansions;
2914 expansions = ed;
2915 ei = new_ExpInv(EXP_IF, ed);
2916 ei->current = ed->line;
2917 ei->emitting = true;
2918 ei->prev = istk->expansion;
2919 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002920 free_tlist(origline);
2921 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002922
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002923 case PP_RMACRO:
2924 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002925 case PP_MACRO:
2926 case PP_IMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002927 if (defining != NULL) {
2928 if (defining->type == EXP_MMACRO) {
2929 defining->def_depth ++;
2930 }
2931 return NO_DIRECTIVE_FOUND;
2932 }
2933 ed = new_ExpDef(EXP_MMACRO);
2934 ed->max_depth =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002935 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002936 ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002937 if (!parse_mmacro_spec(tline, ed, pp_directives[i])) {
2938 nasm_free(ed);
2939 ed = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002940 return DIRECTIVE_FOUND;
2941 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002942 ed->def_depth = 0;
2943 ed->cur_depth = 0;
2944 ed->max_depth = (ed->max_depth + 1);
2945 ed->ignoring = false;
2946 ed->prev = defining;
2947 defining = ed;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002948
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002949 eed = (ExpDef *) hash_findix(&expdefs, ed->name);
2950 while (eed) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002951 if (!strcmp(eed->name, ed->name) &&
2952 (eed->nparam_min <= ed->nparam_max || ed->plus) &&
2953 (ed->nparam_min <= eed->nparam_max || eed->plus)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002954 error(ERR_WARNING|ERR_PASS1,
2955 "redefining multi-line macro `%s'", ed->name);
2956 return DIRECTIVE_FOUND;
2957 }
2958 eed = eed->next;
2959 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002960 free_tlist(origline);
2961 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002962
H. Peter Anvine2c80182005-01-15 22:15:51 +00002963 case PP_ENDM:
2964 case PP_ENDMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002965 if (defining != NULL) {
2966 if (defining->type == EXP_MMACRO) {
2967 if (defining->def_depth > 0) {
2968 defining->def_depth --;
2969 return NO_DIRECTIVE_FOUND;
2970 }
2971 } else {
2972 return NO_DIRECTIVE_FOUND;
2973 }
2974 }
2975 if (!(defining) || (defining->type != EXP_MMACRO)) {
2976 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2977 return DIRECTIVE_FOUND;
2978 }
2979 edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name);
2980 defining->next = *edhead;
2981 *edhead = defining;
2982 ed = defining;
2983 defining = ed->prev;
2984 ed->prev = expansions;
2985 expansions = ed;
2986 ed = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002987 free_tlist(origline);
2988 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002989
H. Peter Anvin89cee572009-07-15 09:16:54 -04002990 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002991 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2992 /*
2993 * We must search along istk->expansion until we hit a
2994 * macro invocation. Then we disable the emitting state(s)
2995 * between exitmacro and endmacro.
2996 */
2997 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
2998 if(ei->type == EXP_MMACRO) {
2999 break;
3000 }
3001 }
3002
3003 if (ei != NULL) {
3004 /*
3005 * Set all invocations leading back to the macro
3006 * invocation to a non-emitting state.
3007 */
3008 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3009 eei->emitting = false;
3010 }
3011 eei->emitting = false;
3012 } else {
3013 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
3014 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05003015 free_tlist(origline);
3016 return DIRECTIVE_FOUND;
3017
H. Peter Anvina26433d2008-07-16 14:40:01 -07003018 case PP_UNMACRO:
3019 case PP_UNIMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003020 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003021 {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003022 ExpDef **ed_p;
3023 ExpDef spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003024
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003025 spec.casesense = (i == PP_UNMACRO);
3026 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
3027 return DIRECTIVE_FOUND;
3028 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003029 ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL);
3030 while (ed_p && *ed_p) {
3031 ed = *ed_p;
3032 if (ed->casesense == spec.casesense &&
3033 !mstrcmp(ed->name, spec.name, spec.casesense) &&
3034 ed->nparam_min == spec.nparam_min &&
3035 ed->nparam_max == spec.nparam_max &&
3036 ed->plus == spec.plus) {
Keith Kaniosc98a5b42010-12-18 12:17:31 -06003037 if (ed->cur_depth > 0) {
Keith Kanios21d885b2010-12-18 12:22:21 -06003038 error(ERR_NONFATAL, "`%s' ignored on active macro",
Keith Kaniosc98a5b42010-12-18 12:17:31 -06003039 pp_directives[i]);
3040 break;
3041 } else {
3042 *ed_p = ed->next;
3043 free_expdef(ed);
3044 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003045 } else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003046 ed_p = &ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003047 }
3048 }
3049 free_tlist(origline);
3050 free_tlist(spec.dlist);
3051 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003052 }
3053
H. Peter Anvine2c80182005-01-15 22:15:51 +00003054 case PP_ROTATE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003055 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003056 if (tline->next && tline->next->type == TOK_WHITESPACE)
3057 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003058 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003059 free_tlist(origline);
3060 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
3061 return DIRECTIVE_FOUND;
3062 }
3063 t = expand_smacro(tline->next);
3064 tline->next = NULL;
3065 free_tlist(origline);
3066 tline = t;
3067 tptr = &t;
3068 tokval.t_type = TOKEN_INVALID;
3069 evalresult =
3070 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3071 free_tlist(tline);
3072 if (!evalresult)
3073 return DIRECTIVE_FOUND;
3074 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003075 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003076 "trailing garbage after expression ignored");
3077 if (!is_simple(evalresult)) {
3078 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
3079 return DIRECTIVE_FOUND;
3080 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003081 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3082 if (ei->type == EXP_MMACRO) {
3083 break;
3084 }
3085 }
3086 if (ei == NULL) {
3087 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
3088 } else if (ei->nparam == 0) {
3089 error(ERR_NONFATAL,
3090 "`%%rotate' invoked within macro without parameters");
3091 } else {
3092 int rotate = ei->rotate + reloc_value(evalresult);
3093
3094 rotate %= (int)ei->nparam;
3095 if (rotate < 0)
3096 rotate += ei->nparam;
3097 ei->rotate = rotate;
3098 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003099 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003100
H. Peter Anvine2c80182005-01-15 22:15:51 +00003101 case PP_REP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003102 if (defining != NULL) {
3103 if (defining->type == EXP_REP) {
3104 defining->def_depth ++;
3105 }
3106 return NO_DIRECTIVE_FOUND;
3107 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003108 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003109 do {
3110 tline = tline->next;
3111 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003112
H. Peter Anvine2c80182005-01-15 22:15:51 +00003113 if (tok_type_(tline, TOK_ID) &&
3114 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003115 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003116 do {
3117 tline = tline->next;
3118 } while (tok_type_(tline, TOK_WHITESPACE));
3119 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003120
H. Peter Anvine2c80182005-01-15 22:15:51 +00003121 if (tline) {
3122 t = expand_smacro(tline);
3123 tptr = &t;
3124 tokval.t_type = TOKEN_INVALID;
3125 evalresult =
3126 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3127 if (!evalresult) {
3128 free_tlist(origline);
3129 return DIRECTIVE_FOUND;
3130 }
3131 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003132 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003133 "trailing garbage after expression ignored");
3134 if (!is_simple(evalresult)) {
3135 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
3136 return DIRECTIVE_FOUND;
3137 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003138 count = reloc_value(evalresult);
3139 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04003140 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003141 count = 0;
3142 } else
3143 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003144 } else {
3145 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003146 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003147 }
3148 free_tlist(origline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003149 ed = new_ExpDef(EXP_REP);
3150 ed->nolist = nolist;
3151 ed->def_depth = 0;
3152 ed->cur_depth = 1;
3153 ed->max_depth = (count - 1);
3154 ed->ignoring = false;
3155 ed->prev = defining;
3156 defining = ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003157 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003158
H. Peter Anvine2c80182005-01-15 22:15:51 +00003159 case PP_ENDREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003160 if (defining != NULL) {
3161 if (defining->type == EXP_REP) {
3162 if (defining->def_depth > 0) {
3163 defining->def_depth --;
3164 return NO_DIRECTIVE_FOUND;
3165 }
3166 } else {
3167 return NO_DIRECTIVE_FOUND;
3168 }
3169 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003170 if ((defining == NULL) || (defining->type != EXP_REP)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003171 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
3172 return DIRECTIVE_FOUND;
3173 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003174
H. Peter Anvine2c80182005-01-15 22:15:51 +00003175 /*
3176 * Now we have a "macro" defined - although it has no name
3177 * and we won't be entering it in the hash tables - we must
3178 * push a macro-end marker for it on to istk->expansion.
3179 * After that, it will take care of propagating itself (a
3180 * macro-end marker line for a macro which is really a %rep
3181 * block will cause the macro to be re-expanded, complete
3182 * with another macro-end marker to ensure the process
3183 * continues) until the whole expansion is forcibly removed
3184 * from istk->expansion by a %exitrep.
3185 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003186 ed = defining;
3187 defining = ed->prev;
3188 ed->prev = expansions;
3189 expansions = ed;
3190 ei = new_ExpInv(EXP_REP, ed);
3191 ei->current = ed->line;
3192 ei->emitting = ((ed->max_depth > 0) ? true : false);
3193 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3194 ei->prev = istk->expansion;
3195 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003196 free_tlist(origline);
3197 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003198
H. Peter Anvine2c80182005-01-15 22:15:51 +00003199 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003200 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3201 /*
3202 * We must search along istk->expansion until we hit a
3203 * rep invocation. Then we disable the emitting state(s)
3204 * between exitrep and endrep.
3205 */
3206 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3207 if (ei->type == EXP_REP) {
3208 break;
3209 }
3210 }
3211
3212 if (ei != NULL) {
3213 /*
3214 * Set all invocations leading back to the rep
3215 * invocation to a non-emitting state.
3216 */
3217 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3218 eei->emitting = false;
3219 }
3220 eei->emitting = false;
3221 eei->current = NULL;
3222 eei->def->cur_depth = eei->def->max_depth;
3223 } else {
3224 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3225 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003226 free_tlist(origline);
3227 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003228
H. Peter Anvine2c80182005-01-15 22:15:51 +00003229 case PP_XDEFINE:
3230 case PP_IXDEFINE:
3231 case PP_DEFINE:
3232 case PP_IDEFINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003233 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003234 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003235
H. Peter Anvine2c80182005-01-15 22:15:51 +00003236 tline = tline->next;
3237 skip_white_(tline);
3238 tline = expand_id(tline);
3239 if (!tline || (tline->type != TOK_ID &&
3240 (tline->type != TOK_PREPROC_ID ||
3241 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003242 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003243 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003244 free_tlist(origline);
3245 return DIRECTIVE_FOUND;
3246 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003247
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003248 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003249 last = tline;
3250 param_start = tline = tline->next;
3251 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003252
H. Peter Anvine2c80182005-01-15 22:15:51 +00003253 /* Expand the macro definition now for %xdefine and %ixdefine */
3254 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3255 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003256
H. Peter Anvine2c80182005-01-15 22:15:51 +00003257 if (tok_is_(tline, "(")) {
3258 /*
3259 * This macro has parameters.
3260 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003261
H. Peter Anvine2c80182005-01-15 22:15:51 +00003262 tline = tline->next;
3263 while (1) {
3264 skip_white_(tline);
3265 if (!tline) {
3266 error(ERR_NONFATAL, "parameter identifier expected");
3267 free_tlist(origline);
3268 return DIRECTIVE_FOUND;
3269 }
3270 if (tline->type != TOK_ID) {
3271 error(ERR_NONFATAL,
3272 "`%s': parameter identifier expected",
3273 tline->text);
3274 free_tlist(origline);
3275 return DIRECTIVE_FOUND;
3276 }
3277 tline->type = TOK_SMAC_PARAM + nparam++;
3278 tline = tline->next;
3279 skip_white_(tline);
3280 if (tok_is_(tline, ",")) {
3281 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003282 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003283 if (!tok_is_(tline, ")")) {
3284 error(ERR_NONFATAL,
3285 "`)' expected to terminate macro template");
3286 free_tlist(origline);
3287 return DIRECTIVE_FOUND;
3288 }
3289 break;
3290 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003291 }
3292 last = tline;
3293 tline = tline->next;
3294 }
3295 if (tok_type_(tline, TOK_WHITESPACE))
3296 last = tline, tline = tline->next;
3297 macro_start = NULL;
3298 last->next = NULL;
3299 t = tline;
3300 while (t) {
3301 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003302 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003303 if (tt->type >= TOK_SMAC_PARAM &&
3304 !strcmp(tt->text, t->text))
3305 t->type = tt->type;
3306 }
3307 tt = t->next;
3308 t->next = macro_start;
3309 macro_start = t;
3310 t = tt;
3311 }
3312 /*
3313 * Good. We now have a macro name, a parameter count, and a
3314 * token list (in reverse order) for an expansion. We ought
3315 * to be OK just to create an SMacro, store it, and let
3316 * free_tlist have the rest of the line (which we have
3317 * carefully re-terminated after chopping off the expansion
3318 * from the end).
3319 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003320 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003321 free_tlist(origline);
3322 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003323
H. Peter Anvine2c80182005-01-15 22:15:51 +00003324 case PP_UNDEF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003325 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003326 tline = tline->next;
3327 skip_white_(tline);
3328 tline = expand_id(tline);
3329 if (!tline || (tline->type != TOK_ID &&
3330 (tline->type != TOK_PREPROC_ID ||
3331 tline->text[1] != '$'))) {
3332 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3333 free_tlist(origline);
3334 return DIRECTIVE_FOUND;
3335 }
3336 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003337 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003338 "trailing garbage after macro name ignored");
3339 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003340
H. Peter Anvine2c80182005-01-15 22:15:51 +00003341 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003342 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003343 undef_smacro(ctx, mname);
3344 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003345 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003346
H. Peter Anvin9e200162008-06-04 17:23:14 -07003347 case PP_DEFSTR:
3348 case PP_IDEFSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003349 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003350 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003351
3352 tline = tline->next;
3353 skip_white_(tline);
3354 tline = expand_id(tline);
3355 if (!tline || (tline->type != TOK_ID &&
3356 (tline->type != TOK_PREPROC_ID ||
3357 tline->text[1] != '$'))) {
3358 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003359 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003360 free_tlist(origline);
3361 return DIRECTIVE_FOUND;
3362 }
3363
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003364 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003365 last = tline;
3366 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003367 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003368
3369 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003370 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003371
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003372 p = detoken(tline, false);
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003373 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003374 macro_start->text = nasm_quote(p, strlen(p));
3375 macro_start->type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003376 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003377
3378 /*
3379 * We now have a macro name, an implicit parameter count of
3380 * zero, and a string token to use as an expansion. Create
3381 * and store an SMacro.
3382 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003383 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003384 free_tlist(origline);
3385 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003386
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003387 case PP_DEFTOK:
3388 case PP_IDEFTOK:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003389 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003390 casesense = (i == PP_DEFTOK);
3391
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003392 tline = tline->next;
3393 skip_white_(tline);
3394 tline = expand_id(tline);
3395 if (!tline || (tline->type != TOK_ID &&
3396 (tline->type != TOK_PREPROC_ID ||
3397 tline->text[1] != '$'))) {
3398 error(ERR_NONFATAL,
3399 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003400 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003401 free_tlist(origline);
3402 return DIRECTIVE_FOUND;
3403 }
3404 ctx = get_ctx(tline->text, &mname, false);
3405 last = tline;
3406 tline = expand_smacro(tline->next);
3407 last->next = NULL;
3408
3409 t = tline;
3410 while (tok_type_(t, TOK_WHITESPACE))
3411 t = t->next;
3412 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003413 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003414 error(ERR_NONFATAL,
3415 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003416 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003417 free_tlist(tline);
3418 free_tlist(origline);
3419 return DIRECTIVE_FOUND;
3420 }
3421
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003422 /*
3423 * Convert the string to a token stream. Note that smacros
3424 * are stored with the token stream reversed, so we have to
3425 * reverse the output of tokenize().
3426 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003427 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003428 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003429
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003430 /*
3431 * We now have a macro name, an implicit parameter count of
3432 * zero, and a numeric token to use as an expansion. Create
3433 * and store an SMacro.
3434 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003435 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003436 free_tlist(tline);
3437 free_tlist(origline);
3438 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003439
H. Peter Anvin418ca702008-05-30 10:42:30 -07003440 case PP_PATHSEARCH:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003441 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003442 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003443 FILE *fp;
3444 StrList *xsl = NULL;
3445 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003446
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003447 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003448
3449 tline = tline->next;
3450 skip_white_(tline);
3451 tline = expand_id(tline);
3452 if (!tline || (tline->type != TOK_ID &&
3453 (tline->type != TOK_PREPROC_ID ||
3454 tline->text[1] != '$'))) {
3455 error(ERR_NONFATAL,
3456 "`%%pathsearch' expects a macro identifier as first parameter");
3457 free_tlist(origline);
3458 return DIRECTIVE_FOUND;
3459 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003460 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003461 last = tline;
3462 tline = expand_smacro(tline->next);
3463 last->next = NULL;
3464
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003465 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003466 while (tok_type_(t, TOK_WHITESPACE))
3467 t = t->next;
3468
3469 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003470 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003471 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003472 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003473 free_tlist(origline);
3474 return DIRECTIVE_FOUND; /* but we did _something_ */
3475 }
3476 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003477 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003478 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003479 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003480 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003481 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003482
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003483 fp = inc_fopen(p, &xsl, &xst, true);
3484 if (fp) {
3485 p = xsl->str;
3486 fclose(fp); /* Don't actually care about the file */
3487 }
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003488 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003489 macro_start->text = nasm_quote(p, strlen(p));
3490 macro_start->type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003491 if (xsl)
3492 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003493
3494 /*
3495 * We now have a macro name, an implicit parameter count of
3496 * zero, and a string token to use as an expansion. Create
3497 * and store an SMacro.
3498 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003499 define_smacro(ctx, mname, casesense, 0, macro_start);
3500 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003501 free_tlist(origline);
3502 return DIRECTIVE_FOUND;
3503 }
3504
H. Peter Anvine2c80182005-01-15 22:15:51 +00003505 case PP_STRLEN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003506 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003507 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003508
H. Peter Anvine2c80182005-01-15 22:15:51 +00003509 tline = tline->next;
3510 skip_white_(tline);
3511 tline = expand_id(tline);
3512 if (!tline || (tline->type != TOK_ID &&
3513 (tline->type != TOK_PREPROC_ID ||
3514 tline->text[1] != '$'))) {
3515 error(ERR_NONFATAL,
3516 "`%%strlen' expects a macro identifier as first parameter");
3517 free_tlist(origline);
3518 return DIRECTIVE_FOUND;
3519 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003520 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003521 last = tline;
3522 tline = expand_smacro(tline->next);
3523 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003524
H. Peter Anvine2c80182005-01-15 22:15:51 +00003525 t = tline;
3526 while (tok_type_(t, TOK_WHITESPACE))
3527 t = t->next;
3528 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003529 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003530 error(ERR_NONFATAL,
3531 "`%%strlen` requires string as second parameter");
3532 free_tlist(tline);
3533 free_tlist(origline);
3534 return DIRECTIVE_FOUND;
3535 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003536
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003537 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003538 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003539
H. Peter Anvine2c80182005-01-15 22:15:51 +00003540 /*
3541 * We now have a macro name, an implicit parameter count of
3542 * zero, and a numeric token to use as an expansion. Create
3543 * and store an SMacro.
3544 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003545 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003546 free_tlist(tline);
3547 free_tlist(origline);
3548 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003549
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003550 case PP_STRCAT:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003551 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003552 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003553
3554 tline = tline->next;
3555 skip_white_(tline);
3556 tline = expand_id(tline);
3557 if (!tline || (tline->type != TOK_ID &&
3558 (tline->type != TOK_PREPROC_ID ||
3559 tline->text[1] != '$'))) {
3560 error(ERR_NONFATAL,
3561 "`%%strcat' expects a macro identifier as first parameter");
3562 free_tlist(origline);
3563 return DIRECTIVE_FOUND;
3564 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003565 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003566 last = tline;
3567 tline = expand_smacro(tline->next);
3568 last->next = NULL;
3569
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003570 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003571 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003572 switch (t->type) {
3573 case TOK_WHITESPACE:
3574 break;
3575 case TOK_STRING:
3576 len += t->a.len = nasm_unquote(t->text, NULL);
3577 break;
3578 case TOK_OTHER:
3579 if (!strcmp(t->text, ",")) /* permit comma separators */
3580 break;
3581 /* else fall through */
3582 default:
3583 error(ERR_NONFATAL,
3584 "non-string passed to `%%strcat' (%d)", t->type);
3585 free_tlist(tline);
3586 free_tlist(origline);
3587 return DIRECTIVE_FOUND;
3588 }
3589 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003590
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003591 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003592 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003593 if (t->type == TOK_STRING) {
3594 memcpy(p, t->text, t->a.len);
3595 p += t->a.len;
3596 }
3597 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003598
3599 /*
3600 * We now have a macro name, an implicit parameter count of
3601 * zero, and a numeric token to use as an expansion. Create
3602 * and store an SMacro.
3603 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003604 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3605 macro_start->text = nasm_quote(pp, len);
3606 nasm_free(pp);
3607 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003608 free_tlist(tline);
3609 free_tlist(origline);
3610 return DIRECTIVE_FOUND;
3611
H. Peter Anvine2c80182005-01-15 22:15:51 +00003612 case PP_SUBSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003613 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003614 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003615 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003616 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003617
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003618 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003619
H. Peter Anvine2c80182005-01-15 22:15:51 +00003620 tline = tline->next;
3621 skip_white_(tline);
3622 tline = expand_id(tline);
3623 if (!tline || (tline->type != TOK_ID &&
3624 (tline->type != TOK_PREPROC_ID ||
3625 tline->text[1] != '$'))) {
3626 error(ERR_NONFATAL,
3627 "`%%substr' expects a macro identifier as first parameter");
3628 free_tlist(origline);
3629 return DIRECTIVE_FOUND;
3630 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003631 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003632 last = tline;
3633 tline = expand_smacro(tline->next);
3634 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003635
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003636 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003637 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003638 while (tok_type_(t, TOK_WHITESPACE))
3639 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003640
H. Peter Anvine2c80182005-01-15 22:15:51 +00003641 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003642 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003643 error(ERR_NONFATAL,
3644 "`%%substr` requires string as second parameter");
3645 free_tlist(tline);
3646 free_tlist(origline);
3647 return DIRECTIVE_FOUND;
3648 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003649
H. Peter Anvine2c80182005-01-15 22:15:51 +00003650 tt = t->next;
3651 tptr = &tt;
3652 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003653 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003654 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003655 if (!evalresult) {
3656 free_tlist(tline);
3657 free_tlist(origline);
3658 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003659 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003660 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3661 free_tlist(tline);
3662 free_tlist(origline);
3663 return DIRECTIVE_FOUND;
3664 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003665 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003666
3667 while (tok_type_(tt, TOK_WHITESPACE))
3668 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003669 if (!tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003670 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003671 } else {
3672 tokval.t_type = TOKEN_INVALID;
3673 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3674 pass, error, NULL);
3675 if (!evalresult) {
3676 free_tlist(tline);
3677 free_tlist(origline);
3678 return DIRECTIVE_FOUND;
3679 } else if (!is_simple(evalresult)) {
3680 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3681 free_tlist(tline);
3682 free_tlist(origline);
3683 return DIRECTIVE_FOUND;
3684 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003685 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003686 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003687
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003688 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003689 /* make start and count being in range */
3690 if (start < 0)
3691 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003692 if (count < 0)
3693 count = len + count + 1 - start;
3694 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003695 count = len - start;
3696 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003697 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003698
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003699 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003700 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003701 macro_start->type = TOK_STRING;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003702
H. Peter Anvine2c80182005-01-15 22:15:51 +00003703 /*
3704 * We now have a macro name, an implicit parameter count of
3705 * zero, and a numeric token to use as an expansion. Create
3706 * and store an SMacro.
3707 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003708 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003709 free_tlist(tline);
3710 free_tlist(origline);
3711 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003712 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003713
H. Peter Anvine2c80182005-01-15 22:15:51 +00003714 case PP_ASSIGN:
3715 case PP_IASSIGN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003716 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003717 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003718
H. Peter Anvine2c80182005-01-15 22:15:51 +00003719 tline = tline->next;
3720 skip_white_(tline);
3721 tline = expand_id(tline);
3722 if (!tline || (tline->type != TOK_ID &&
3723 (tline->type != TOK_PREPROC_ID ||
3724 tline->text[1] != '$'))) {
3725 error(ERR_NONFATAL,
3726 "`%%%sassign' expects a macro identifier",
3727 (i == PP_IASSIGN ? "i" : ""));
3728 free_tlist(origline);
3729 return DIRECTIVE_FOUND;
3730 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003731 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003732 last = tline;
3733 tline = expand_smacro(tline->next);
3734 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003735
H. Peter Anvine2c80182005-01-15 22:15:51 +00003736 t = tline;
3737 tptr = &t;
3738 tokval.t_type = TOKEN_INVALID;
3739 evalresult =
3740 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3741 free_tlist(tline);
3742 if (!evalresult) {
3743 free_tlist(origline);
3744 return DIRECTIVE_FOUND;
3745 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003746
H. Peter Anvine2c80182005-01-15 22:15:51 +00003747 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003748 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003749 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003750
H. Peter Anvine2c80182005-01-15 22:15:51 +00003751 if (!is_simple(evalresult)) {
3752 error(ERR_NONFATAL,
3753 "non-constant value given to `%%%sassign'",
3754 (i == PP_IASSIGN ? "i" : ""));
3755 free_tlist(origline);
3756 return DIRECTIVE_FOUND;
3757 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003758
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003759 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003760 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003761
H. Peter Anvine2c80182005-01-15 22:15:51 +00003762 /*
3763 * We now have a macro name, an implicit parameter count of
3764 * zero, and a numeric token to use as an expansion. Create
3765 * and store an SMacro.
3766 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003767 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003768 free_tlist(origline);
3769 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003770
H. Peter Anvine2c80182005-01-15 22:15:51 +00003771 case PP_LINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003772 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003773 /*
3774 * Syntax is `%line nnn[+mmm] [filename]'
3775 */
3776 tline = tline->next;
3777 skip_white_(tline);
3778 if (!tok_type_(tline, TOK_NUMBER)) {
3779 error(ERR_NONFATAL, "`%%line' expects line number");
3780 free_tlist(origline);
3781 return DIRECTIVE_FOUND;
3782 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003783 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003784 m = 1;
3785 tline = tline->next;
3786 if (tok_is_(tline, "+")) {
3787 tline = tline->next;
3788 if (!tok_type_(tline, TOK_NUMBER)) {
3789 error(ERR_NONFATAL, "`%%line' expects line increment");
3790 free_tlist(origline);
3791 return DIRECTIVE_FOUND;
3792 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003793 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003794 tline = tline->next;
3795 }
3796 skip_white_(tline);
3797 src_set_linnum(k);
3798 istk->lineinc = m;
3799 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003800 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003801 }
3802 free_tlist(origline);
3803 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003804
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003805 case PP_WHILE:
3806 if (defining != NULL) {
3807 if (defining->type == EXP_WHILE) {
3808 defining->def_depth ++;
3809 }
3810 return NO_DIRECTIVE_FOUND;
3811 }
3812 l = NULL;
3813 if ((istk->expansion != NULL) &&
3814 (istk->expansion->emitting == false)) {
3815 j = COND_NEVER;
3816 } else {
3817 l = new_Line();
3818 l->first = copy_Token(tline->next);
3819 j = if_condition(tline->next, i);
3820 tline->next = NULL; /* it got freed */
3821 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
3822 }
3823 ed = new_ExpDef(EXP_WHILE);
3824 ed->state = j;
3825 ed->cur_depth = 1;
3826 ed->max_depth = DEADMAN_LIMIT;
3827 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
3828 if (ed->ignoring == false) {
3829 ed->line = l;
3830 ed->last = l;
3831 } else if (l != NULL) {
3832 delete_Token(l->first);
3833 nasm_free(l);
3834 l = NULL;
3835 }
3836 ed->prev = defining;
3837 defining = ed;
3838 free_tlist(origline);
3839 return DIRECTIVE_FOUND;
3840
3841 case PP_ENDWHILE:
3842 if (defining != NULL) {
3843 if (defining->type == EXP_WHILE) {
3844 if (defining->def_depth > 0) {
3845 defining->def_depth --;
3846 return NO_DIRECTIVE_FOUND;
3847 }
3848 } else {
3849 return NO_DIRECTIVE_FOUND;
3850 }
3851 }
3852 if (tline->next != NULL) {
3853 error_precond(ERR_WARNING|ERR_PASS1,
3854 "trailing garbage after `%%endwhile' ignored");
3855 }
3856 if ((defining == NULL) || (defining->type != EXP_WHILE)) {
3857 error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'");
3858 return DIRECTIVE_FOUND;
3859 }
3860 ed = defining;
3861 defining = ed->prev;
3862 if (ed->ignoring == false) {
3863 ed->prev = expansions;
3864 expansions = ed;
3865 ei = new_ExpInv(EXP_WHILE, ed);
3866 ei->current = ed->line->next;
3867 ei->emitting = true;
3868 ei->prev = istk->expansion;
3869 istk->expansion = ei;
3870 } else {
3871 nasm_free(ed);
3872 }
3873 free_tlist(origline);
3874 return DIRECTIVE_FOUND;
3875
3876 case PP_EXITWHILE:
3877 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3878 /*
3879 * We must search along istk->expansion until we hit a
3880 * while invocation. Then we disable the emitting state(s)
3881 * between exitwhile and endwhile.
3882 */
3883 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3884 if (ei->type == EXP_WHILE) {
3885 break;
3886 }
3887 }
3888
3889 if (ei != NULL) {
3890 /*
3891 * Set all invocations leading back to the while
3892 * invocation to a non-emitting state.
3893 */
3894 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3895 eei->emitting = false;
3896 }
3897 eei->emitting = false;
3898 eei->current = NULL;
3899 eei->def->cur_depth = eei->def->max_depth;
3900 } else {
3901 error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block");
3902 }
3903 free_tlist(origline);
3904 return DIRECTIVE_FOUND;
3905
3906 case PP_COMMENT:
3907 if (defining != NULL) {
3908 if (defining->type == EXP_COMMENT) {
3909 defining->def_depth ++;
3910 }
3911 return NO_DIRECTIVE_FOUND;
3912 }
3913 ed = new_ExpDef(EXP_COMMENT);
3914 ed->ignoring = true;
3915 ed->prev = defining;
3916 defining = ed;
3917 free_tlist(origline);
3918 return DIRECTIVE_FOUND;
3919
3920 case PP_ENDCOMMENT:
3921 if (defining != NULL) {
3922 if (defining->type == EXP_COMMENT) {
3923 if (defining->def_depth > 0) {
3924 defining->def_depth --;
3925 return NO_DIRECTIVE_FOUND;
3926 }
3927 } else {
3928 return NO_DIRECTIVE_FOUND;
3929 }
3930 }
3931 if ((defining == NULL) || (defining->type != EXP_COMMENT)) {
3932 error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'");
3933 return DIRECTIVE_FOUND;
3934 }
3935 ed = defining;
3936 defining = ed->prev;
3937 nasm_free(ed);
3938 free_tlist(origline);
3939 return DIRECTIVE_FOUND;
3940
3941 case PP_FINAL:
3942 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3943 if (in_final != false) {
3944 error(ERR_FATAL, "`%%final' cannot be used recursively");
3945 }
3946 tline = tline->next;
3947 skip_white_(tline);
3948 if (tline == NULL) {
3949 error(ERR_NONFATAL, "`%%final' expects at least one parameter");
3950 } else {
3951 l = new_Line();
3952 l->first = copy_Token(tline);
3953 l->next = finals;
3954 finals = l;
3955 }
3956 free_tlist(origline);
3957 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003958
H. Peter Anvine2c80182005-01-15 22:15:51 +00003959 default:
3960 error(ERR_FATAL,
3961 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003962 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003963 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003964 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003965}
3966
3967/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003968 * Ensure that a macro parameter contains a condition code and
3969 * nothing else. Return the condition code index if so, or -1
3970 * otherwise.
3971 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003972static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003973{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003974 Token *tt;
3975 int i, j, k, m;
3976
H. Peter Anvin25a99342007-09-22 17:45:45 -07003977 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003978 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003979
H. Peter Anvineba20a72002-04-30 20:53:55 +00003980 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003981 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003982 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003983 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003984 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003985 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003986 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003987
3988 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003989 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003990 while (j - i > 1) {
3991 k = (j + i) / 2;
3992 m = nasm_stricmp(t->text, conditions[k]);
3993 if (m == 0) {
3994 i = k;
3995 j = -2;
3996 break;
3997 } else if (m < 0) {
3998 j = k;
3999 } else
4000 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004001 }
4002 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004003 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004004 return i;
4005}
4006
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004007static bool paste_tokens(Token **head, const struct tokseq_match *m,
4008 int mnum, bool handle_paste_tokens)
H. Peter Anvind784a082009-04-20 14:01:18 -07004009{
4010 Token **tail, *t, *tt;
4011 Token **paste_head;
4012 bool did_paste = false;
4013 char *tmp;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004014 int i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004015
4016 /* Now handle token pasting... */
4017 paste_head = NULL;
4018 tail = head;
4019 while ((t = *tail) && (tt = t->next)) {
4020 switch (t->type) {
4021 case TOK_WHITESPACE:
4022 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004023 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07004024 t->next = delete_Token(tt);
4025 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004026 /* Do not advance paste_head here */
4027 tail = &t->next;
4028 }
H. Peter Anvind784a082009-04-20 14:01:18 -07004029 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004030 case TOK_PASTE: /* %+ */
4031 if (handle_paste_tokens) {
4032 /* Zap %+ and whitespace tokens to the right */
4033 while (t && (t->type == TOK_WHITESPACE ||
4034 t->type == TOK_PASTE))
4035 t = *tail = delete_Token(t);
4036 if (!paste_head || !t)
4037 break; /* Nothing to paste with */
4038 tail = paste_head;
4039 t = *tail;
4040 tt = t->next;
4041 while (tok_type_(tt, TOK_WHITESPACE))
4042 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004043 if (tt) {
4044 tmp = nasm_strcat(t->text, tt->text);
4045 delete_Token(t);
4046 tt = delete_Token(tt);
4047 t = *tail = tokenize(tmp);
4048 nasm_free(tmp);
4049 while (t->next) {
4050 tail = &t->next;
4051 t = t->next;
4052 }
4053 t->next = tt; /* Attach the remaining token chain */
4054 did_paste = true;
4055 }
4056 paste_head = tail;
4057 tail = &t->next;
4058 break;
4059 }
4060 /* else fall through */
4061 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004062 /*
4063 * Concatenation of tokens might look nontrivial
4064 * but in real it's pretty simple -- the caller
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004065 * prepares the masks of token types to be concatenated
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004066 * and we simply find matched sequences and slip
4067 * them together
4068 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004069 for (i = 0; i < mnum; i++) {
4070 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
4071 size_t len = 0;
4072 char *tmp, *p;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004073
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004074 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004075 len += strlen(tt->text);
4076 tt = tt->next;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004077 }
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004078
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004079 /*
4080 * Now tt points to the first token after
4081 * the potential paste area...
4082 */
4083 if (tt != t->next) {
4084 /* We have at least two tokens... */
4085 len += strlen(t->text);
4086 p = tmp = nasm_malloc(len+1);
4087 while (t != tt) {
4088 strcpy(p, t->text);
4089 p = strchr(p, '\0');
4090 t = delete_Token(t);
4091 }
4092 t = *tail = tokenize(tmp);
4093 nasm_free(tmp);
4094 while (t->next) {
4095 tail = &t->next;
4096 t = t->next;
4097 }
4098 t->next = tt; /* Attach the remaining token chain */
4099 did_paste = true;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004100 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004101 paste_head = tail;
4102 tail = &t->next;
4103 break;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004104 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004105 }
4106 if (i >= mnum) { /* no match */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004107 tail = &t->next;
4108 if (!tok_type_(t->next, TOK_WHITESPACE))
4109 paste_head = tail;
4110 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004111 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004112 }
4113 }
4114 return did_paste;
4115}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004116
4117/*
4118 * expands to a list of tokens from %{x:y}
4119 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004120static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004121{
4122 Token *t = tline, **tt, *tm, *head;
4123 char *pos;
4124 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004125
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004126 pos = strchr(tline->text, ':');
4127 nasm_assert(pos);
4128
4129 lst = atoi(pos + 1);
4130 fst = atoi(tline->text + 1);
4131
4132 /*
4133 * only macros params are accounted so
4134 * if someone passes %0 -- we reject such
4135 * value(s)
4136 */
4137 if (lst == 0 || fst == 0)
4138 goto err;
4139
4140 /* the values should be sane */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004141 if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) ||
4142 (lst > (int)ei->nparam || lst < (-(int)ei->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004143 goto err;
4144
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004145 fst = fst < 0 ? fst + (int)ei->nparam + 1: fst;
4146 lst = lst < 0 ? lst + (int)ei->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004147
4148 /* counted from zero */
4149 fst--, lst--;
4150
4151 /*
4152 * it will be at least one token
4153 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004154 tm = ei->params[(fst + ei->rotate) % ei->nparam];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004155 t = new_Token(NULL, tm->type, tm->text, 0);
4156 head = t, tt = &t->next;
4157 if (fst < lst) {
4158 for (i = fst + 1; i <= lst; i++) {
4159 t = new_Token(NULL, TOK_OTHER, ",", 0);
4160 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004161 j = (i + ei->rotate) % ei->nparam;
4162 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004163 t = new_Token(NULL, tm->type, tm->text, 0);
4164 *tt = t, tt = &t->next;
4165 }
4166 } else {
4167 for (i = fst - 1; i >= lst; i--) {
4168 t = new_Token(NULL, TOK_OTHER, ",", 0);
4169 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004170 j = (i + ei->rotate) % ei->nparam;
4171 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004172 t = new_Token(NULL, tm->type, tm->text, 0);
4173 *tt = t, tt = &t->next;
4174 }
4175 }
4176
4177 *last = tt;
4178 return head;
4179
4180err:
4181 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
4182 &tline->text[1]);
4183 return tline;
4184}
4185
H. Peter Anvin76690a12002-04-30 20:52:49 +00004186/*
4187 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004188 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004189 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004190 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004191static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004192{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004193 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004194 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004195 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004196
4197 tail = &thead;
4198 thead = NULL;
4199
H. Peter Anvine2c80182005-01-15 22:15:51 +00004200 while (tline) {
4201 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004202 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4203 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4204 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004205 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004206 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004207 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004208 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004209 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004210 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004211
H. Peter Anvine2c80182005-01-15 22:15:51 +00004212 t = tline;
4213 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004214
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004215 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
4216 if (ei->type == EXP_MMACRO) {
4217 break;
4218 }
4219 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004220 if (ei == NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004221 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004222 } else {
4223 pos = strchr(t->text, ':');
4224 if (!pos) {
4225 switch (t->text[1]) {
4226 /*
4227 * We have to make a substitution of one of the
4228 * forms %1, %-1, %+1, %%foo, %0.
4229 */
4230 case '0':
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004231 if ((strlen(t->text) > 2) && (t->text[2] == '0')) {
4232 type = TOK_ID;
4233 text = nasm_strdup(ei->label_text);
4234 } else {
4235 type = TOK_NUMBER;
4236 snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam);
4237 text = nasm_strdup(tmpbuf);
4238 }
4239 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004240 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004241 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004242 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004243 ei->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004244 text = nasm_strcat(tmpbuf, t->text + 2);
4245 break;
4246 case '-':
4247 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004248 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004249 tt = NULL;
4250 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004251 if (ei->nparam > 1)
4252 n = (n + ei->rotate) % ei->nparam;
4253 tt = ei->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004254 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004255 cc = find_cc(tt);
4256 if (cc == -1) {
4257 error(ERR_NONFATAL,
4258 "macro parameter %d is not a condition code",
4259 n + 1);
4260 text = NULL;
4261 } else {
4262 type = TOK_ID;
4263 if (inverse_ccs[cc] == -1) {
4264 error(ERR_NONFATAL,
4265 "condition code `%s' is not invertible",
4266 conditions[cc]);
4267 text = NULL;
4268 } else
4269 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4270 }
4271 break;
4272 case '+':
4273 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004274 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004275 tt = NULL;
4276 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004277 if (ei->nparam > 1)
4278 n = (n + ei->rotate) % ei->nparam;
4279 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004280 }
4281 cc = find_cc(tt);
4282 if (cc == -1) {
4283 error(ERR_NONFATAL,
4284 "macro parameter %d is not a condition code",
4285 n + 1);
4286 text = NULL;
4287 } else {
4288 type = TOK_ID;
4289 text = nasm_strdup(conditions[cc]);
4290 }
4291 break;
4292 default:
4293 n = atoi(t->text + 1) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004294 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004295 tt = NULL;
4296 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004297 if (ei->nparam > 1)
4298 n = (n + ei->rotate) % ei->nparam;
4299 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004300 }
4301 if (tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004302 for (i = 0; i < ei->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004303 *tail = new_Token(NULL, tt->type, tt->text, 0);
4304 tail = &(*tail)->next;
4305 tt = tt->next;
4306 }
4307 }
4308 text = NULL; /* we've done it here */
4309 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004310 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004311 } else {
4312 /*
4313 * seems we have a parameters range here
4314 */
4315 Token *head, **last;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004316 head = expand_mmac_params_range(ei, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004317 if (head != t) {
4318 *tail = head;
4319 *last = tline;
4320 tline = head;
4321 text = NULL;
4322 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004323 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004324 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004325 if (!text) {
4326 delete_Token(t);
4327 } else {
4328 *tail = t;
4329 tail = &t->next;
4330 t->type = type;
4331 nasm_free(t->text);
4332 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004333 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004334 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004335 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004336 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004337 } else if (tline->type == TOK_INDIRECT) {
4338 t = tline;
4339 tline = tline->next;
4340 tt = tokenize(t->text);
4341 tt = expand_mmac_params(tt);
4342 tt = expand_smacro(tt);
4343 *tail = tt;
4344 while (tt) {
4345 tt->a.mac = NULL; /* Necessary? */
4346 tail = &tt->next;
4347 tt = tt->next;
4348 }
4349 delete_Token(t);
4350 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004351 } else {
4352 t = *tail = tline;
4353 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004354 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004355 tail = &t->next;
4356 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004357 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004358 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004359
Cyrill Gorcunovcb00cd12011-06-13 21:25:10 +04004360 if (changed)
4361 paste_tokens(&thead, pp_concat_match,
4362 ARRAY_SIZE(pp_concat_match),
4363 false);
H. Peter Anvin6125b622009-04-08 14:02:25 -07004364
H. Peter Anvin76690a12002-04-30 20:52:49 +00004365 return thead;
4366}
4367
4368/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004369 * Expand all single-line macro calls made in the given line.
4370 * Return the expanded version of the line. The original is deemed
4371 * to be destroyed in the process. (In reality we'll just move
4372 * Tokens from input to output a lot of the time, rather than
4373 * actually bothering to destroy and replicate.)
4374 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004375
H. Peter Anvine2c80182005-01-15 22:15:51 +00004376static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004377{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004378 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004379 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004380 Token **params;
4381 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004382 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004383 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004384 Token *org_tline = tline;
4385 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004386 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004387 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004388 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004389
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004390 /*
4391 * Trick: we should avoid changing the start token pointer since it can
4392 * be contained in "next" field of other token. Because of this
4393 * we allocate a copy of first token and work with it; at the end of
4394 * routine we copy it back
4395 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004396 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004397 tline = new_Token(org_tline->next, org_tline->type,
4398 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004399 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004400 nasm_free(org_tline->text);
4401 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004402 }
4403
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004404 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004405
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004406again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004407 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004408 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004409
H. Peter Anvine2c80182005-01-15 22:15:51 +00004410 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004411 if (!--deadman) {
4412 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004413 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004414 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004415
H. Peter Anvine2c80182005-01-15 22:15:51 +00004416 if ((mname = tline->text)) {
4417 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004418 if (tline->type == TOK_ID) {
4419 head = (SMacro *)hash_findix(&smacros, mname);
4420 } else if (tline->type == TOK_PREPROC_ID) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004421 ctx = get_ctx(mname, &mname, false);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004422 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4423 } else
4424 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004425
H. Peter Anvine2c80182005-01-15 22:15:51 +00004426 /*
4427 * We've hit an identifier. As in is_mmacro below, we first
4428 * check whether the identifier is a single-line macro at
4429 * all, then think about checking for parameters if
4430 * necessary.
4431 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004432 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004433 if (!mstrcmp(m->name, mname, m->casesense))
4434 break;
4435 if (m) {
4436 mstart = tline;
4437 params = NULL;
4438 paramsize = NULL;
4439 if (m->nparam == 0) {
4440 /*
4441 * Simple case: the macro is parameterless. Discard the
4442 * one token that the macro call took, and push the
4443 * expansion back on the to-do stack.
4444 */
4445 if (!m->expansion) {
4446 if (!strcmp("__FILE__", m->name)) {
4447 int32_t num = 0;
4448 char *file = NULL;
4449 src_get(&num, &file);
4450 tline->text = nasm_quote(file, strlen(file));
4451 tline->type = TOK_STRING;
4452 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004453 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004454 }
4455 if (!strcmp("__LINE__", m->name)) {
4456 nasm_free(tline->text);
4457 make_tok_num(tline, src_get_linnum());
4458 continue;
4459 }
4460 if (!strcmp("__BITS__", m->name)) {
4461 nasm_free(tline->text);
4462 make_tok_num(tline, globalbits);
4463 continue;
4464 }
4465 tline = delete_Token(tline);
4466 continue;
4467 }
4468 } else {
4469 /*
4470 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004471 * exists and takes parameters. We must find the
4472 * parameters in the call, count them, find the SMacro
4473 * that corresponds to that form of the macro call, and
4474 * substitute for the parameters when we expand. What a
4475 * pain.
4476 */
4477 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004478 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004479 do {
4480 t = tline->next;
4481 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004482 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004483 t->text = NULL;
4484 t = tline->next = delete_Token(t);
4485 }
4486 tline = t;
4487 } while (tok_type_(tline, TOK_WHITESPACE));
4488 if (!tok_is_(tline, "(")) {
4489 /*
4490 * This macro wasn't called with parameters: ignore
4491 * the call. (Behaviour borrowed from gnu cpp.)
4492 */
4493 tline = mstart;
4494 m = NULL;
4495 } else {
4496 int paren = 0;
4497 int white = 0;
4498 brackets = 0;
4499 nparam = 0;
4500 sparam = PARAM_DELTA;
4501 params = nasm_malloc(sparam * sizeof(Token *));
4502 params[0] = tline->next;
4503 paramsize = nasm_malloc(sparam * sizeof(int));
4504 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004505 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004506 /*
4507 * For some unusual expansions
4508 * which concatenates function call
4509 */
4510 t = tline->next;
4511 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004512 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004513 t->text = NULL;
4514 t = tline->next = delete_Token(t);
4515 }
4516 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004517
H. Peter Anvine2c80182005-01-15 22:15:51 +00004518 if (!tline) {
4519 error(ERR_NONFATAL,
4520 "macro call expects terminating `)'");
4521 break;
4522 }
4523 if (tline->type == TOK_WHITESPACE
4524 && brackets <= 0) {
4525 if (paramsize[nparam])
4526 white++;
4527 else
4528 params[nparam] = tline->next;
4529 continue; /* parameter loop */
4530 }
4531 if (tline->type == TOK_OTHER
4532 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004533 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004534 if (ch == ',' && !paren && brackets <= 0) {
4535 if (++nparam >= sparam) {
4536 sparam += PARAM_DELTA;
4537 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004538 sparam * sizeof(Token *));
4539 paramsize = nasm_realloc(paramsize,
4540 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004541 }
4542 params[nparam] = tline->next;
4543 paramsize[nparam] = 0;
4544 white = 0;
4545 continue; /* parameter loop */
4546 }
4547 if (ch == '{' &&
4548 (brackets > 0 || (brackets == 0 &&
4549 !paramsize[nparam])))
4550 {
4551 if (!(brackets++)) {
4552 params[nparam] = tline->next;
4553 continue; /* parameter loop */
4554 }
4555 }
4556 if (ch == '}' && brackets > 0)
4557 if (--brackets == 0) {
4558 brackets = -1;
4559 continue; /* parameter loop */
4560 }
4561 if (ch == '(' && !brackets)
4562 paren++;
4563 if (ch == ')' && brackets <= 0)
4564 if (--paren < 0)
4565 break;
4566 }
4567 if (brackets < 0) {
4568 brackets = 0;
4569 error(ERR_NONFATAL, "braces do not "
4570 "enclose all of macro parameter");
4571 }
4572 paramsize[nparam] += white + 1;
4573 white = 0;
4574 } /* parameter loop */
4575 nparam++;
4576 while (m && (m->nparam != nparam ||
4577 mstrcmp(m->name, mname,
4578 m->casesense)))
4579 m = m->next;
4580 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004581 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004582 "macro `%s' exists, "
4583 "but not taking %d parameters",
4584 mstart->text, nparam);
4585 }
4586 }
4587 if (m && m->in_progress)
4588 m = NULL;
4589 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004590 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004591 * Design question: should we handle !tline, which
4592 * indicates missing ')' here, or expand those
4593 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004594 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004595 */
4596 nasm_free(params);
4597 nasm_free(paramsize);
4598 tline = mstart;
4599 } else {
4600 /*
4601 * Expand the macro: we are placed on the last token of the
4602 * call, so that we can easily split the call from the
4603 * following tokens. We also start by pushing an SMAC_END
4604 * token for the cycle removal.
4605 */
4606 t = tline;
4607 if (t) {
4608 tline = t->next;
4609 t->next = NULL;
4610 }
4611 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004612 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004613 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004614 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004615 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004616 if (t->type >= TOK_SMAC_PARAM) {
4617 Token *pcopy = tline, **ptail = &pcopy;
4618 Token *ttt, *pt;
4619 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004620
H. Peter Anvine2c80182005-01-15 22:15:51 +00004621 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004622 i = paramsize[t->type - TOK_SMAC_PARAM];
4623 while (--i >= 0) {
4624 pt = *ptail = new_Token(tline, ttt->type,
4625 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004626 ptail = &pt->next;
4627 ttt = ttt->next;
4628 }
4629 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004630 } else if (t->type == TOK_PREPROC_Q) {
4631 tt = new_Token(tline, TOK_ID, mname, 0);
4632 tline = tt;
4633 } else if (t->type == TOK_PREPROC_QQ) {
4634 tt = new_Token(tline, TOK_ID, m->name, 0);
4635 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004636 } else {
4637 tt = new_Token(tline, t->type, t->text, 0);
4638 tline = tt;
4639 }
4640 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004641
H. Peter Anvine2c80182005-01-15 22:15:51 +00004642 /*
4643 * Having done that, get rid of the macro call, and clean
4644 * up the parameters.
4645 */
4646 nasm_free(params);
4647 nasm_free(paramsize);
4648 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004649 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004650 continue; /* main token loop */
4651 }
4652 }
4653 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004654
H. Peter Anvine2c80182005-01-15 22:15:51 +00004655 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004656 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004657 tline = delete_Token(tline);
4658 } else {
4659 t = *tail = tline;
4660 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004661 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004662 t->next = NULL;
4663 tail = &t->next;
4664 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004665 }
4666
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004667 /*
4668 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004669 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004670 * TOK_IDs should be concatenated.
4671 * Also we look for %+ tokens and concatenate the tokens before and after
4672 * them (without white spaces in between).
4673 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004674 if (expanded) {
Cyrill Gorcunovcb00cd12011-06-13 21:25:10 +04004675 if (paste_tokens(&thead, pp_concat_match,
4676 ARRAY_SIZE(pp_concat_match),
4677 true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004678 /*
4679 * If we concatenated something, *and* we had previously expanded
4680 * an actual macro, scan the lines again for macros...
4681 */
4682 tline = thead;
4683 expanded = false;
4684 goto again;
4685 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004686 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004687
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004688err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004689 if (org_tline) {
4690 if (thead) {
4691 *org_tline = *thead;
4692 /* since we just gave text to org_line, don't free it */
4693 thead->text = NULL;
4694 delete_Token(thead);
4695 } else {
4696 /* the expression expanded to empty line;
4697 we can't return NULL for some reasons
4698 we just set the line to a single WHITESPACE token. */
4699 memset(org_tline, 0, sizeof(*org_tline));
4700 org_tline->text = NULL;
4701 org_tline->type = TOK_WHITESPACE;
4702 }
4703 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004704 }
4705
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004706 return thead;
4707}
4708
4709/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004710 * Similar to expand_smacro but used exclusively with macro identifiers
4711 * right before they are fetched in. The reason is that there can be
4712 * identifiers consisting of several subparts. We consider that if there
4713 * are more than one element forming the name, user wants a expansion,
4714 * otherwise it will be left as-is. Example:
4715 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004716 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004717 *
4718 * the identifier %$abc will be left as-is so that the handler for %define
4719 * will suck it and define the corresponding value. Other case:
4720 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004721 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004722 *
4723 * In this case user wants name to be expanded *before* %define starts
4724 * working, so we'll expand %$abc into something (if it has a value;
4725 * otherwise it will be left as-is) then concatenate all successive
4726 * PP_IDs into one.
4727 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004728static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004729{
4730 Token *cur, *oldnext = NULL;
4731
H. Peter Anvin734b1882002-04-30 21:01:08 +00004732 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004733 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004734
4735 cur = tline;
4736 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004737 (cur->next->type == TOK_ID ||
4738 cur->next->type == TOK_PREPROC_ID
4739 || cur->next->type == TOK_NUMBER))
4740 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004741
4742 /* If identifier consists of just one token, don't expand */
4743 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004744 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004745
H. Peter Anvine2c80182005-01-15 22:15:51 +00004746 if (cur) {
4747 oldnext = cur->next; /* Detach the tail past identifier */
4748 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004749 }
4750
H. Peter Anvin734b1882002-04-30 21:01:08 +00004751 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004752
H. Peter Anvine2c80182005-01-15 22:15:51 +00004753 if (cur) {
4754 /* expand_smacro possibly changhed tline; re-scan for EOL */
4755 cur = tline;
4756 while (cur && cur->next)
4757 cur = cur->next;
4758 if (cur)
4759 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004760 }
4761
4762 return tline;
4763}
4764
4765/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004766 * Determine whether the given line constitutes a multi-line macro
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004767 * call, and return the ExpDef structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004768 * to check for an initial label - that's taken care of in
4769 * expand_mmacro - but must check numbers of parameters. Guaranteed
4770 * to be called with tline->type == TOK_ID, so the putative macro
4771 * name is easy to find.
4772 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004773static ExpDef *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004774{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004775 ExpDef *head, *ed;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004776 Token **params;
4777 int nparam;
4778
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004779 head = (ExpDef *) hash_findix(&expdefs, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004780
4781 /*
4782 * Efficiency: first we see if any macro exists with the given
4783 * name. If not, we can return NULL immediately. _Then_ we
4784 * count the parameters, and then we look further along the
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004785 * list if necessary to find the proper ExpDef.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004786 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004787 list_for_each(ed, head)
4788 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004789 break;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004790 if (!ed)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004791 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004792
4793 /*
4794 * OK, we have a potential macro. Count and demarcate the
4795 * parameters.
4796 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004797 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004798
4799 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004800 * So we know how many parameters we've got. Find the ExpDef
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004801 * structure that handles this number.
4802 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004803 while (ed) {
4804 if (ed->nparam_min <= nparam
4805 && (ed->plus || nparam <= ed->nparam_max)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004806 /*
4807 * It's right, and we can use it. Add its default
4808 * parameters to the end of our list if necessary.
4809 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004810 if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004811 params =
4812 nasm_realloc(params,
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004813 ((ed->nparam_min + ed->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004814 1) * sizeof(*params)));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004815 while (nparam < ed->nparam_min + ed->ndefs) {
4816 params[nparam] = ed->defaults[nparam - ed->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004817 nparam++;
4818 }
4819 }
4820 /*
4821 * If we've gone over the maximum parameter count (and
4822 * we're in Plus mode), ignore parameters beyond
4823 * nparam_max.
4824 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004825 if (ed->plus && nparam > ed->nparam_max)
4826 nparam = ed->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004827 /*
4828 * Then terminate the parameter list, and leave.
4829 */
4830 if (!params) { /* need this special case */
4831 params = nasm_malloc(sizeof(*params));
4832 nparam = 0;
4833 }
4834 params[nparam] = NULL;
4835 *params_array = params;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004836 return ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004837 }
4838 /*
4839 * This one wasn't right: look for the next one with the
4840 * same name.
4841 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004842 list_for_each(ed, ed->next)
4843 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004844 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004845 }
4846
4847 /*
4848 * After all that, we didn't find one with the right number of
4849 * parameters. Issue a warning, and fail to expand the macro.
4850 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004851 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004852 "macro `%s' exists, but not taking %d parameters",
4853 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004854 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004855 return NULL;
4856}
4857
4858/*
4859 * Expand the multi-line macro call made by the given line, if
4860 * there is one to be expanded. If there is, push the expansion on
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004861 * istk->expansion and return true. Otherwise return false.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004862 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004863static bool expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004864{
H. Peter Anvineba20a72002-04-30 20:53:55 +00004865 Token *label = NULL;
4866 int dont_prepend = 0;
Cyrill Gorcunovfb27fc22011-06-25 12:08:30 +04004867 Token **params, *t;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004868 Line *l = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004869 ExpDef *ed;
4870 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004871 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004872 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004873
4874 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004875 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004876 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004877 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004878 return false;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004879 ed = is_mmacro(t, &params);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004880 if (ed != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004881 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004882 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004883 Token *last;
4884 /*
4885 * We have an id which isn't a macro call. We'll assume
4886 * it might be a label; we'll also check to see if a
4887 * colon follows it. Then, if there's another id after
4888 * that lot, we'll check it again for macro-hood.
4889 */
4890 label = last = t;
4891 t = t->next;
4892 if (tok_type_(t, TOK_WHITESPACE))
4893 last = t, t = t->next;
4894 if (tok_is_(t, ":")) {
4895 dont_prepend = 1;
4896 last = t, t = t->next;
4897 if (tok_type_(t, TOK_WHITESPACE))
4898 last = t, t = t->next;
4899 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004900 if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, &params)))
4901 return false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004902 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004903 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004904 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004905 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004906
4907 /*
4908 * Fix up the parameters: this involves stripping leading and
4909 * trailing whitespace, then stripping braces if they are
4910 * present.
4911 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004912 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004913 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004914
H. Peter Anvine2c80182005-01-15 22:15:51 +00004915 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004916 int brace = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004917 int comma = (!ed->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004918
H. Peter Anvine2c80182005-01-15 22:15:51 +00004919 t = params[i];
4920 skip_white_(t);
4921 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004922 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004923 params[i] = t;
4924 paramlen[i] = 0;
4925 while (t) {
4926 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4927 break; /* ... because we have hit a comma */
4928 if (comma && t->type == TOK_WHITESPACE
4929 && tok_is_(t->next, ","))
4930 break; /* ... or a space then a comma */
4931 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4932 break; /* ... or a brace */
4933 t = t->next;
4934 paramlen[i]++;
4935 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004936 }
4937
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004938 if (ed->cur_depth >= ed->max_depth) {
4939 if (ed->max_depth > 1) {
4940 error(ERR_WARNING,
4941 "reached maximum macro recursion depth of %i for %s",
4942 ed->max_depth,ed->name);
4943 }
4944 return false;
4945 } else {
4946 ed->cur_depth ++;
4947 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004948
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004949 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004950 * OK, we have found a ExpDef structure representing a
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004951 * previously defined mmacro. Create an expansion invocation
4952 * and point it back to the expansion definition. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004953 * parameter tokens and macro-local tokens doesn't get done
4954 * until the single-line macro substitution process; this is
4955 * because delaying them allows us to change the semantics
4956 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004957 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004958 ei = new_ExpInv(EXP_MMACRO, ed);
4959 ei->name = nasm_strdup(mname);
4960 //ei->label = label;
4961 //ei->label_text = detoken(label, false);
4962 ei->current = ed->line;
4963 ei->emitting = true;
4964 //ei->iline = tline;
4965 ei->params = params;
4966 ei->nparam = nparam;
4967 ei->rotate = 0;
4968 ei->paramlen = paramlen;
4969 ei->lineno = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004970
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004971 ei->prev = istk->expansion;
4972 istk->expansion = ei;
4973
4974 /*
4975 * Special case: detect %00 on first invocation; if found,
4976 * avoid emitting any labels that precede the mmacro call.
4977 * ed->prepend is set to -1 when %00 is detected, else 1.
4978 */
4979 if (ed->prepend == 0) {
4980 for (l = ed->line; l != NULL; l = l->next) {
4981 for (t = l->first; t != NULL; t = t->next) {
4982 if ((t->type == TOK_PREPROC_ID) &&
4983 (strlen(t->text) == 3) &&
4984 (t->text[1] == '0') && (t->text[2] == '0')) {
4985 dont_prepend = -1;
4986 break;
4987 }
4988 }
4989 if (dont_prepend < 0) {
4990 break;
4991 }
4992 }
4993 ed->prepend = ((dont_prepend < 0) ? -1 : 1);
4994 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004995
4996 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004997 * If we had a label, push it on as the first line of
4998 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004999 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005000 if (label != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005001 if (ed->prepend < 0) {
5002 ei->label_text = detoken(label, false);
5003 } else {
5004 if (dont_prepend == 0) {
5005 t = label;
5006 while (t->next != NULL) {
5007 t = t->next;
5008 }
5009 t->next = new_Token(NULL, TOK_OTHER, ":", 0);
5010 }
5011 l = new_Line();
5012 l->first = copy_Token(label);
5013 l->next = ei->current;
5014 ei->current = l;
5015 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005016 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005017
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005018 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005019
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005020 istk->mmac_depth++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005021 return true;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005022}
5023
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005024/* The function that actually does the error reporting */
5025static void verror(int severity, const char *fmt, va_list arg)
5026{
5027 char buff[1024];
5028
5029 vsnprintf(buff, sizeof(buff), fmt, arg);
5030
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005031 if (istk && istk->mmac_depth > 0) {
5032 ExpInv *ei = istk->expansion;
5033 int lineno = ei->lineno;
5034 while (ei) {
5035 if (ei->type == EXP_MMACRO)
5036 break;
5037 lineno += ei->relno;
5038 ei = ei->prev;
5039 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005040 nasm_error(severity, "(%s:%d) %s", ei->def->name,
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005041 lineno, buff);
5042 } else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005043 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005044}
5045
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005046/*
5047 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07005048 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005049 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005050static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005051{
5052 va_list arg;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005053 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005054 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00005055 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005056}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005057
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005058/*
5059 * Because %else etc are evaluated in the state context
5060 * of the previous branch, errors might get lost with error():
5061 * %if 0 ... %else trailing garbage ... %endif
5062 * So %else etc should report errors with this function.
5063 */
5064static void error_precond(int severity, const char *fmt, ...)
5065{
5066 va_list arg;
5067
5068 /* Only ignore the error if it's really in a dead branch */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005069 if ((istk != NULL) &&
5070 (istk->expansion != NULL) &&
5071 (istk->expansion->type == EXP_IF) &&
5072 (istk->expansion->def->state == COND_NEVER))
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005073 return;
5074
5075 va_start(arg, fmt);
5076 verror(severity, fmt, arg);
5077 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005078}
5079
H. Peter Anvin734b1882002-04-30 21:01:08 +00005080static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005081pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005082{
H. Peter Anvin7383b402008-09-24 10:20:40 -07005083 Token *t;
5084
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005085 cstk = NULL;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005086 istk = nasm_zalloc(sizeof(Include));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005087 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00005088 src_set_fname(nasm_strdup(file));
5089 src_set_linnum(0);
5090 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005091 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07005092 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00005093 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005094 defining = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005095 finals = NULL;
5096 in_final = false;
Charles Crayned4200be2008-07-12 16:42:33 -07005097 nested_mac_count = 0;
5098 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005099 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005100 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005101 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005102 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005103 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005104 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005105 }
H. Peter Anvind2456592008-06-19 15:04:18 -07005106 any_extrastdmac = extrastdmac && *extrastdmac;
5107 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005108 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005109
5110 /*
5111 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5112 * The caller, however, will also pass in 3 for preprocess-only so
5113 * we can set __PASS__ accordingly.
5114 */
5115 pass = apass > 2 ? 2 : apass;
5116
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005117 dephead = deptail = deplist;
5118 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005119 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
5120 sl->next = NULL;
5121 strcpy(sl->str, file);
5122 *deptail = sl;
5123 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005124 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07005125
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005126 /*
5127 * Define the __PASS__ macro. This is defined here unlike
5128 * all the other builtins, because it is special -- it varies between
5129 * passes.
5130 */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005131 t = nasm_zalloc(sizeof(*t));
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005132 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07005133 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005134}
5135
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005136static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005137{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005138 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005139 Token *tline;
Keith Kanios9858cec2010-11-08 00:58:02 -06005140 ExpDef *ed;
5141 ExpInv *ei;
5142 Line *l;
5143 int j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005144
H. Peter Anvine2c80182005-01-15 22:15:51 +00005145 while (1) {
5146 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005147 * Fetch a tokenized line, either from the expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005148 * buffer or from the input file.
5149 */
5150 tline = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005151
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005152 while (1) { /* until we get a line we can use */
5153 /*
5154 * Fetch a tokenized line from the expansion buffer
5155 */
5156 if (istk->expansion != NULL) {
5157 ei = istk->expansion;
5158 if (ei->current != NULL) {
5159 if (ei->emitting == false) {
5160 ei->current = NULL;
5161 continue;
5162 }
5163 l = ei->current;
5164 ei->current = l->next;
5165 ei->lineno++;
5166 tline = copy_Token(l->first);
5167 if (((ei->type == EXP_REP) ||
5168 (ei->type == EXP_MMACRO) ||
5169 (ei->type == EXP_WHILE))
5170 && (ei->def->nolist == false)) {
5171 char *p = detoken(tline, false);
5172 list->line(LIST_MACRO, p);
5173 nasm_free(p);
5174 }
5175 if (ei->linnum > -1) {
5176 src_set_linnum(src_get_linnum() + 1);
5177 }
5178 break;
5179 } else if ((ei->type == EXP_REP) &&
5180 (ei->def->cur_depth < ei->def->max_depth)) {
5181 ei->def->cur_depth ++;
5182 ei->current = ei->def->line;
5183 ei->lineno = 0;
5184 continue;
5185 } else if ((ei->type == EXP_WHILE) &&
5186 (ei->def->cur_depth < ei->def->max_depth)) {
5187 ei->current = ei->def->line;
5188 ei->lineno = 0;
5189 tline = copy_Token(ei->current->first);
5190 j = if_condition(tline, PP_WHILE);
5191 tline = NULL;
5192 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
5193 if (j == COND_IF_TRUE) {
5194 ei->current = ei->current->next;
5195 ei->def->cur_depth ++;
5196 } else {
5197 ei->emitting = false;
5198 ei->current = NULL;
5199 ei->def->cur_depth = ei->def->max_depth;
5200 }
5201 continue;
5202 } else {
5203 istk->expansion = ei->prev;
5204 ed = ei->def;
5205 if (ed != NULL) {
5206 if ((ei->emitting == true) &&
5207 (ed->max_depth == DEADMAN_LIMIT) &&
5208 (ed->cur_depth == DEADMAN_LIMIT)
5209 ) {
5210 error(ERR_FATAL, "runaway expansion detected, aborting");
5211 }
5212 if (ed->cur_depth > 0) {
5213 ed->cur_depth --;
Keith Kanios94124652010-12-18 11:47:28 -06005214 } else if (ed->type != EXP_MMACRO) {
Keith Kanios6a7c3e92010-12-18 11:49:53 -06005215 expansions = ed->prev;
5216 free_expdef(ed);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005217 }
5218 if ((ei->type == EXP_REP) ||
5219 (ei->type == EXP_MMACRO) ||
5220 (ei->type == EXP_WHILE)) {
5221 list->downlevel(LIST_MACRO);
5222 if (ei->type == EXP_MMACRO) {
5223 istk->mmac_depth--;
5224 }
5225 }
5226 }
5227 if (ei->linnum > -1) {
5228 src_set_linnum(ei->linnum);
5229 }
5230 free_expinv(ei);
5231 continue;
5232 }
5233 }
5234
5235 /*
5236 * Read in line from input and tokenize
5237 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005238 line = read_line();
5239 if (line) { /* from the current input file */
5240 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005241 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005242 nasm_free(line);
5243 break;
5244 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005245
H. Peter Anvine2c80182005-01-15 22:15:51 +00005246 /*
5247 * The current file has ended; work down the istk
5248 */
5249 {
5250 Include *i = istk;
5251 fclose(i->fp);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005252 if (i->expansion != NULL) {
5253 error(ERR_FATAL,
5254 "end of file while still in an expansion");
5255 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005256 /* only set line and file name if there's a next node */
5257 if (i->next) {
5258 src_set_linnum(i->lineno);
Cyrill Gorcunovd34a1082011-03-07 11:23:08 +03005259 nasm_free(src_set_fname(nasm_strdup(i->fname)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005260 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005261 if ((i->next == NULL) && (finals != NULL)) {
5262 in_final = true;
5263 ei = new_ExpInv(EXP_FINAL, NULL);
5264 ei->emitting = true;
5265 ei->current = finals;
5266 istk->expansion = ei;
5267 finals = NULL;
5268 continue;
5269 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005270 istk = i->next;
5271 list->downlevel(LIST_INCLUDE);
5272 nasm_free(i);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005273 if (istk == NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005274 if (finals != NULL) {
5275 in_final = true;
5276 } else {
5277 return NULL;
5278 }
5279 }
5280 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005281 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005282 }
5283
5284 if (defining == NULL) {
5285 tline = expand_mmac_params(tline);
5286 }
5287
H. Peter Anvine2c80182005-01-15 22:15:51 +00005288 /*
5289 * Check the line to see if it's a preprocessor directive.
5290 */
5291 if (do_directive(tline) == DIRECTIVE_FOUND) {
5292 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005293 } else if (defining != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005294 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005295 * We're defining an expansion. We emit nothing at all,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005296 * and just shove the tokenized line on to the definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005297 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005298 if (defining->ignoring == false) {
5299 Line *l = new_Line();
5300 l->first = tline;
5301 if (defining->line == NULL) {
5302 defining->line = l;
5303 defining->last = l;
5304 } else {
5305 defining->last->next = l;
5306 defining->last = l;
5307 }
5308 } else {
Keith Kanios104803d2010-12-18 11:05:46 -06005309 free_tlist(tline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005310 }
5311 defining->linecount++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005312 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005313 } else if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005314 (istk->expansion->emitting != true)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005315 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005316 * We're in a non-emitting branch of an expansion.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005317 * Emit nothing at all, not even a blank line: when we
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005318 * emerge from the expansion we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005319 * directive so we keep our place correctly.
5320 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005321 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005322 continue;
5323 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005324 tline = expand_smacro(tline);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005325 if (expand_mmacro(tline) != true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005326 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005327 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005328 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005329 line = detoken(tline, true);
5330 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005331 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005332 } else {
5333 continue;
5334 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005335 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005336 }
5337 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005338}
5339
H. Peter Anvine2c80182005-01-15 22:15:51 +00005340static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005341{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005342 if (defining != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005343 error(ERR_NONFATAL, "end of file while still defining an expansion");
5344 while (defining != NULL) {
5345 ExpDef *ed = defining;
5346 defining = ed->prev;
5347 free_expdef(ed);
5348 }
5349 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005350 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005351 while (cstk != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005352 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005353 free_macros();
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005354 while (istk != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005355 Include *i = istk;
5356 istk = istk->next;
5357 fclose(i->fp);
5358 nasm_free(i->fname);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005359 while (i->expansion != NULL) {
5360 ExpInv *ei = i->expansion;
5361 i->expansion = ei->prev;
5362 free_expinv(ei);
5363 }
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005364 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005365 }
5366 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005367 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005368 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005369 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005370 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005371 free_llist(predef);
5372 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005373 while ((i = ipath)) {
5374 ipath = i->next;
5375 if (i->path)
5376 nasm_free(i->path);
5377 nasm_free(i);
5378 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005379 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005380}
5381
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005382void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005383{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005384 IncPath *i = nasm_zalloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005385
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005386 if (path)
5387 i->path = nasm_strdup(path);
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005388
H. Peter Anvin89cee572009-07-15 09:16:54 -04005389 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005390 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005391 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005392 j = j->next;
5393 j->next = i;
5394 } else {
5395 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005396 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005397}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005398
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005399void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005400{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005401 Token *inc, *space, *name;
5402 Line *l;
5403
H. Peter Anvin734b1882002-04-30 21:01:08 +00005404 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5405 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5406 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005407
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005408 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005409 l->next = predef;
5410 l->first = inc;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005411 predef = l;
5412}
5413
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005414void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005415{
5416 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005417 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005418 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005419
5420 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005421 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5422 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005423 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005424 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005425 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005426 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005427 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005428
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005429 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005430 l->next = predef;
5431 l->first = def;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005432 predef = l;
5433}
5434
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005435void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005436{
5437 Token *def, *space;
5438 Line *l;
5439
H. Peter Anvin734b1882002-04-30 21:01:08 +00005440 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5441 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005442 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005443
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005444 l = new_Line();
H. Peter Anvin620515a2002-04-30 20:57:38 +00005445 l->next = predef;
5446 l->first = def;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005447 predef = l;
5448}
5449
Keith Kaniosb7a89542007-04-12 02:40:54 +00005450/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005451 * This function is used to assist with "runtime" preprocessor
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005452 * directives, e.g. pp_runtime("%define __BITS__ 64");
Keith Kaniosb7a89542007-04-12 02:40:54 +00005453 *
5454 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5455 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5456 */
5457
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005458void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005459{
5460 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005461
Keith Kaniosb7a89542007-04-12 02:40:54 +00005462 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005463 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005464 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005465
Keith Kaniosb7a89542007-04-12 02:40:54 +00005466}
5467
H. Peter Anvina70547f2008-07-19 21:44:26 -07005468void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005469{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005470 extrastdmac = macros;
5471}
5472
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005473static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005474{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005475 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005476 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005477 tok->text = nasm_strdup(numbuf);
5478 tok->type = TOK_NUMBER;
5479}
5480
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005481Preproc nasmpp = {
5482 pp_reset,
5483 pp_getline,
5484 pp_cleanup
5485};