blob: 3f4f900aa1a81d5628ea02d83fbfb713775dad60 [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
Cyrill Gorcunov9d1141a2011-06-26 23:07:35 +0400506#define nasm_trace(msg, ...) printf("(%s:%d): " msg "\n", __func__, __LINE__, ##__VA_ARGS__)
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400507#define nasm_dump_token(t) nasm_raw_dump_token(t, __FILE__, __LINE__, __func__);
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400508#define nasm_dump_stream(t) nasm_raw_dump_stream(t, __FILE__, __LINE__, __func__);
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400509
510/* FIXME: we really need some compound type here instead of inplace code */
511static const char *nasm_get_tok_type_str(enum pp_token_type type)
512{
513#define SWITCH_TOK_NAME(type) \
514 case (type): \
515 return stringify(type)
516
517 switch (type) {
518 SWITCH_TOK_NAME(TOK_NONE);
519 SWITCH_TOK_NAME(TOK_WHITESPACE);
520 SWITCH_TOK_NAME(TOK_COMMENT);
521 SWITCH_TOK_NAME(TOK_ID);
522 SWITCH_TOK_NAME(TOK_PREPROC_ID);
523 SWITCH_TOK_NAME(TOK_STRING);
524 SWITCH_TOK_NAME(TOK_NUMBER);
525 SWITCH_TOK_NAME(TOK_FLOAT);
526 SWITCH_TOK_NAME(TOK_SMAC_END);
527 SWITCH_TOK_NAME(TOK_OTHER);
528 SWITCH_TOK_NAME(TOK_INTERNAL_STRING);
529 SWITCH_TOK_NAME(TOK_PREPROC_Q);
530 SWITCH_TOK_NAME(TOK_PREPROC_QQ);
531 SWITCH_TOK_NAME(TOK_PASTE);
532 SWITCH_TOK_NAME(TOK_INDIRECT);
533 SWITCH_TOK_NAME(TOK_SMAC_PARAM);
534 SWITCH_TOK_NAME(TOK_MAX);
535 }
536
537 return NULL;
538}
539
540static void nasm_raw_dump_token(Token *token, const char *file, int line, const char *func)
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300541{
542 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
543 if (token) {
544 Token *t;
545 list_for_each(t, token) {
546 if (t->text)
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400547 printf("'%s'(%s) ", t->text,
548 nasm_get_tok_type_str(t->type));
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300549 }
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400550 printf("\n\n");
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300551 }
552}
553
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400554static void nasm_raw_dump_stream(Token *token, const char *file, int line, const char *func)
555{
556 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
557 if (token) {
558 Token *t;
559 list_for_each(t, token)
560 printf("%s", t->text ? t->text : " ");
561 printf("\n\n");
562 }
563}
564
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400565#else
566#define nasm_trace(msg, ...)
567#define nasm_dump_token(t)
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400568#define nasm_dump_stream(t)
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300569#endif
570
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300571/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700572 * nasm_unquote with error if the string contains NUL characters.
573 * If the string contains NUL characters, issue an error and return
574 * the C len, i.e. truncate at the NUL.
575 */
576static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
577{
578 size_t len = nasm_unquote(qstr, NULL);
579 size_t clen = strlen(qstr);
580
581 if (len != clen)
582 error(ERR_NONFATAL, "NUL character in `%s' directive",
583 pp_directives[directive]);
584
585 return clen;
586}
587
588/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700589 * In-place reverse a list of tokens.
590 */
591static Token *reverse_tokens(Token *t)
592{
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400593 Token *prev, *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700594
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400595 list_reverse(t, prev, next);
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700596
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400597 return t;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700598}
599
600/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300601 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000602 * front of them. We do it here because I could not find any other
603 * place to do it for the moment, and it is a hack (ideally it would
604 * be nice to be able to use the NASM pre-processor to do it).
605 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000606static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000607{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000608 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400609 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000610
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400611 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000612
613 /* Binary search for the directive name */
614 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400615 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400616 q = nasm_skip_word(p);
617 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000618 if (len) {
619 oldchar = p[len];
620 p[len] = 0;
621 while (j - i > 1) {
622 k = (j + i) / 2;
623 m = nasm_stricmp(p, tasm_directives[k]);
624 if (m == 0) {
625 /* We have found a directive, so jam a % in front of it
626 * so that NASM will then recognise it as one if it's own.
627 */
628 p[len] = oldchar;
629 len = strlen(p);
630 oldline = line;
631 line = nasm_malloc(len + 2);
632 line[0] = '%';
633 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700634 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300635 * NASM does not recognise IFDIFI, so we convert
636 * it to %if 0. This is not used in NASM
637 * compatible code, but does need to parse for the
638 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000639 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700640 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000641 } else {
642 memcpy(line + 1, p, len + 1);
643 }
644 nasm_free(oldline);
645 return line;
646 } else if (m < 0) {
647 j = k;
648 } else
649 i = k;
650 }
651 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000652 }
653 return line;
654}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000655
H. Peter Anvin76690a12002-04-30 20:52:49 +0000656/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000657 * The pre-preprocessing stage... This function translates line
658 * number indications as they emerge from GNU cpp (`# lineno "file"
659 * flags') into NASM preprocessor line number indications (`%line
660 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000661 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000662static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000663{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000664 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000665 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000666
H. Peter Anvine2c80182005-01-15 22:15:51 +0000667 if (line[0] == '#' && line[1] == ' ') {
668 oldline = line;
669 fname = oldline + 2;
670 lineno = atoi(fname);
671 fname += strspn(fname, "0123456789 ");
672 if (*fname == '"')
673 fname++;
674 fnlen = strcspn(fname, "\"");
675 line = nasm_malloc(20 + fnlen);
676 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
677 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000678 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000679 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000680 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000681 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000682}
683
684/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000685 * Free a linked list of tokens.
686 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000687static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000688{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400689 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000690 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000691}
692
693/*
694 * Free a linked list of lines.
695 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000696static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000697{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400698 Line *l, *tmp;
699 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000700 free_tlist(l->first);
701 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000702 }
703}
704
705/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500706 * Free an ExpDef
H. Peter Anvineba20a72002-04-30 20:53:55 +0000707 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500708static void free_expdef(ExpDef * ed)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000709{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500710 nasm_free(ed->name);
711 free_tlist(ed->dlist);
712 nasm_free(ed->defaults);
713 free_llist(ed->line);
714 nasm_free(ed);
715}
716
717/*
718 * Free an ExpInv
719 */
720static void free_expinv(ExpInv * ei)
721{
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300722 if (ei->name != NULL)
723 nasm_free(ei->name);
724 if (ei->label_text != NULL)
725 nasm_free(ei->label_text);
726 nasm_free(ei);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000727}
728
729/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700730 * Free all currently defined macros, and free the hash tables
731 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700732static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700733{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400734 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700735 const char *key;
736 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700737
H. Peter Anvin072771e2008-05-22 13:17:51 -0700738 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300739 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400740 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300741 nasm_free(s->name);
742 free_tlist(s->expansion);
743 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300744 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700745 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700746 hash_free(smt);
747}
748
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500749static void free_expdef_table(struct hash_table *edt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700750{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500751 ExpDef *ed, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700752 const char *key;
753 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700754
755 it = NULL;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500756 while ((ed = hash_iterate(edt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300757 nasm_free((void *)key);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500758 list_for_each_safe(ed ,tmp, ed)
759 free_expdef(ed);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700760 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500761 hash_free(edt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700762}
763
764static void free_macros(void)
765{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700766 free_smacro_table(&smacros);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500767 free_expdef_table(&expdefs);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700768}
769
770/*
771 * Initialize the hash tables
772 */
773static void init_macros(void)
774{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700775 hash_init(&smacros, HASH_LARGE);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500776 hash_init(&expdefs, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700777}
778
779/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000780 * Pop the context stack.
781 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000782static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000783{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000784 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000785
786 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700787 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000788 nasm_free(c->name);
789 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000790}
791
H. Peter Anvin072771e2008-05-22 13:17:51 -0700792/*
793 * Search for a key in the hash index; adding it if necessary
794 * (in which case we initialize the data pointer to NULL.)
795 */
796static void **
797hash_findi_add(struct hash_table *hash, const char *str)
798{
799 struct hash_insert hi;
800 void **r;
801 char *strx;
802
803 r = hash_findi(hash, str, &hi);
804 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300805 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700806
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300807 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700808 return hash_add(&hi, strx, NULL);
809}
810
811/*
812 * Like hash_findi, but returns the data element rather than a pointer
813 * to it. Used only when not adding a new element, hence no third
814 * argument.
815 */
816static void *
817hash_findix(struct hash_table *hash, const char *str)
818{
819 void **p;
820
821 p = hash_findi(hash, str, NULL);
822 return p ? *p : NULL;
823}
824
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400825/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500826 * read line from standard macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400827 * if there no more left -- return NULL
828 */
829static char *line_from_stdmac(void)
830{
831 unsigned char c;
832 const unsigned char *p = stdmacpos;
833 char *line, *q;
834 size_t len = 0;
835
836 if (!stdmacpos)
837 return NULL;
838
839 while ((c = *p++)) {
840 if (c >= 0x80)
841 len += pp_directives_len[c - 0x80] + 1;
842 else
843 len++;
844 }
845
846 line = nasm_malloc(len + 1);
847 q = line;
848 while ((c = *stdmacpos++)) {
849 if (c >= 0x80) {
850 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
851 q += pp_directives_len[c - 0x80];
852 *q++ = ' ';
853 } else {
854 *q++ = c;
855 }
856 }
857 stdmacpos = p;
858 *q = '\0';
859
860 if (!*stdmacpos) {
861 /* This was the last of the standard macro chain... */
862 stdmacpos = NULL;
863 if (any_extrastdmac) {
864 stdmacpos = extrastdmac;
865 any_extrastdmac = false;
866 } else if (do_predef) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300867 ExpInv *ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400868 Line *pd, *l;
869 Token *head, **tail, *t;
870
871 /*
872 * Nasty hack: here we push the contents of
873 * `predef' on to the top-level expansion stack,
874 * since this is the most convenient way to
875 * implement the pre-include and pre-define
876 * features.
877 */
878 list_for_each(pd, predef) {
879 head = NULL;
880 tail = &head;
881 list_for_each(t, pd->first) {
882 *tail = new_Token(NULL, t->type, t->text, 0);
883 tail = &(*tail)->next;
884 }
885
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500886 l = new_Line();
887 l->first = head;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300888 ei = new_ExpInv(EXP_PREDEF, NULL);
889 ei->current = l;
890 ei->emitting = true;
891 ei->prev = istk->expansion;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500892 istk->expansion = ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400893 }
894 do_predef = false;
895 }
896 }
897
898 return line;
899}
900
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000901#define BUF_DELTA 512
902/*
903 * Read a line from the top file in istk, handling multiple CR/LFs
904 * at the end of the line read, and handling spurious ^Zs. Will
905 * return lines from the standard macro set if this has not already
906 * been done.
907 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000908static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000909{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000910 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000911 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000912
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400913 /*
914 * standart macros set (predefined) goes first
915 */
916 p = line_from_stdmac();
917 if (p)
918 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700919
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400920 /*
921 * regular read from a file
922 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000923 bufsize = BUF_DELTA;
924 buffer = nasm_malloc(BUF_DELTA);
925 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000926 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000927 while (1) {
928 q = fgets(p, bufsize - (p - buffer), istk->fp);
929 if (!q)
930 break;
931 p += strlen(p);
932 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300933 /*
934 * Convert backslash-CRLF line continuation sequences into
935 * nothing at all (for DOS and Windows)
936 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000937 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
938 p -= 3;
939 *p = 0;
940 continued_count++;
941 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300942 /*
943 * Also convert backslash-LF line continuation sequences into
944 * nothing at all (for Unix)
945 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
947 p -= 2;
948 *p = 0;
949 continued_count++;
950 } else {
951 break;
952 }
953 }
954 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000955 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000956 bufsize += BUF_DELTA;
957 buffer = nasm_realloc(buffer, bufsize);
958 p = buffer + offset; /* prevent stale-pointer problems */
959 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000960 }
961
H. Peter Anvine2c80182005-01-15 22:15:51 +0000962 if (!q && p == buffer) {
963 nasm_free(buffer);
964 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000965 }
966
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300967 src_set_linnum(src_get_linnum() + istk->lineinc +
968 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000969
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000970 /*
971 * Play safe: remove CRs as well as LFs, if any of either are
972 * present at the end of the line.
973 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000974 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000976
977 /*
978 * Handle spurious ^Z, which may be inserted into source files
979 * by some file transfer utilities.
980 */
981 buffer[strcspn(buffer, "\032")] = '\0';
982
H. Peter Anvin734b1882002-04-30 21:01:08 +0000983 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000984
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000985 return buffer;
986}
987
988/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000989 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000990 * don't need to parse the value out of e.g. numeric tokens: we
991 * simply split one string into many.
992 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000993static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000994{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700995 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000996 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000997 Token *list = NULL;
998 Token *t, **tail = &list;
Keith Kanios6faad4e2010-12-18 14:08:02 -0600999 bool verbose = true;
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +04001000
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +04001001 nasm_trace("Tokenize for '%s'", line);
1002
Keith Kanios6faad4e2010-12-18 14:08:02 -06001003 if ((defining != NULL) && (defining->ignoring == true)) {
1004 verbose = false;
1005 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001006
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 while (*line) {
1008 p = line;
1009 if (*p == '%') {
1010 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001011 if (*p == '+' && !nasm_isdigit(p[1])) {
1012 p++;
1013 type = TOK_PASTE;
1014 } else if (nasm_isdigit(*p) ||
1015 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 do {
1017 p++;
1018 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001019 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001020 type = TOK_PREPROC_ID;
1021 } else if (*p == '{') {
1022 p++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001023 while (*p && *p != '}') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001024 p[-1] = *p;
1025 p++;
1026 }
1027 p[-1] = '\0';
1028 if (*p)
1029 p++;
1030 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001031 } else if (*p == '[') {
1032 int lvl = 1;
1033 line += 2; /* Skip the leading %[ */
1034 p++;
1035 while (lvl && (c = *p++)) {
1036 switch (c) {
1037 case ']':
1038 lvl--;
1039 break;
1040 case '%':
1041 if (*p == '[')
1042 lvl++;
1043 break;
1044 case '\'':
1045 case '\"':
1046 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +04001047 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001048 break;
1049 default:
1050 break;
1051 }
1052 }
1053 p--;
1054 if (*p)
1055 *p++ = '\0';
Keith Kanios6faad4e2010-12-18 14:08:02 -06001056 if (lvl && verbose)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001057 error(ERR_NONFATAL, "unterminated %[ construct");
1058 type = TOK_INDIRECT;
1059 } else if (*p == '?') {
1060 type = TOK_PREPROC_Q; /* %? */
1061 p++;
1062 if (*p == '?') {
1063 type = TOK_PREPROC_QQ; /* %?? */
1064 p++;
1065 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001066 } else if (*p == '!') {
1067 type = TOK_PREPROC_ID;
1068 p++;
1069 if (isidchar(*p)) {
1070 do {
1071 p++;
1072 } while (isidchar(*p));
1073 } else if (*p == '\'' || *p == '\"' || *p == '`') {
1074 p = nasm_skip_string(p);
1075 if (*p)
1076 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001077 else if(verbose)
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001078 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
1079 } else {
1080 /* %! without string or identifier */
1081 type = TOK_OTHER; /* Legacy behavior... */
1082 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001083 } else if (isidchar(*p) ||
1084 ((*p == '!' || *p == '%' || *p == '$') &&
1085 isidchar(p[1]))) {
1086 do {
1087 p++;
1088 }
1089 while (isidchar(*p));
1090 type = TOK_PREPROC_ID;
1091 } else {
1092 type = TOK_OTHER;
1093 if (*p == '%')
1094 p++;
1095 }
1096 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1097 type = TOK_ID;
1098 p++;
1099 while (*p && isidchar(*p))
1100 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001101 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001102 /*
1103 * A string token.
1104 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001105 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001106 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001107
H. Peter Anvine2c80182005-01-15 22:15:51 +00001108 if (*p) {
1109 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001110 } else if(verbose) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001111 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001112 /* Handling unterminated strings by UNV */
1113 /* type = -1; */
1114 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001115 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001116 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001117 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001118 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001119 bool is_hex = false;
1120 bool is_float = false;
1121 bool has_e = false;
1122 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001123
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001125 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001126 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001127
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001128 if (*p == '$') {
1129 p++;
1130 is_hex = true;
1131 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001132
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001133 for (;;) {
1134 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001135
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001136 if (!is_hex && (c == 'e' || c == 'E')) {
1137 has_e = true;
1138 if (*p == '+' || *p == '-') {
1139 /*
1140 * e can only be followed by +/- if it is either a
1141 * prefixed hex number or a floating-point number
1142 */
1143 p++;
1144 is_float = true;
1145 }
1146 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1147 is_hex = true;
1148 } else if (c == 'P' || c == 'p') {
1149 is_float = true;
1150 if (*p == '+' || *p == '-')
1151 p++;
1152 } else if (isnumchar(c) || c == '_')
1153 ; /* just advance */
1154 else if (c == '.') {
1155 /*
1156 * we need to deal with consequences of the legacy
1157 * parser, like "1.nolist" being two tokens
1158 * (TOK_NUMBER, TOK_ID) here; at least give it
1159 * a shot for now. In the future, we probably need
1160 * a flex-based scanner with proper pattern matching
1161 * to do it as well as it can be done. Nothing in
1162 * the world is going to help the person who wants
1163 * 0x123.p16 interpreted as two tokens, though.
1164 */
1165 r = p;
1166 while (*r == '_')
1167 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001168
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001169 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1170 (!is_hex && (*r == 'e' || *r == 'E')) ||
1171 (*r == 'p' || *r == 'P')) {
1172 p = r;
1173 is_float = true;
1174 } else
1175 break; /* Terminate the token */
1176 } else
1177 break;
1178 }
1179 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001180
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001181 if (p == line+1 && *line == '$') {
1182 type = TOK_OTHER; /* TOKEN_HERE */
1183 } else {
1184 if (has_e && !is_hex) {
1185 /* 1e13 is floating-point, but 1e13h is not */
1186 is_float = true;
1187 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001188
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001189 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1190 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001191 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001192 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001193 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001194 /*
1195 * Whitespace just before end-of-line is discarded by
1196 * pretending it's a comment; whitespace just before a
1197 * comment gets lumped into the comment.
1198 */
1199 if (!*p || *p == ';') {
1200 type = TOK_COMMENT;
1201 while (*p)
1202 p++;
1203 }
1204 } else if (*p == ';') {
1205 type = TOK_COMMENT;
1206 while (*p)
1207 p++;
1208 } else {
1209 /*
1210 * Anything else is an operator of some kind. We check
1211 * for all the double-character operators (>>, <<, //,
1212 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001213 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001214 */
1215 type = TOK_OTHER;
1216 if ((p[0] == '>' && p[1] == '>') ||
1217 (p[0] == '<' && p[1] == '<') ||
1218 (p[0] == '/' && p[1] == '/') ||
1219 (p[0] == '<' && p[1] == '=') ||
1220 (p[0] == '>' && p[1] == '=') ||
1221 (p[0] == '=' && p[1] == '=') ||
1222 (p[0] == '!' && p[1] == '=') ||
1223 (p[0] == '<' && p[1] == '>') ||
1224 (p[0] == '&' && p[1] == '&') ||
1225 (p[0] == '|' && p[1] == '|') ||
1226 (p[0] == '^' && p[1] == '^')) {
1227 p++;
1228 }
1229 p++;
1230 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001231
H. Peter Anvine2c80182005-01-15 22:15:51 +00001232 /* Handling unterminated string by UNV */
1233 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001234 {
1235 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1236 t->text[p-line] = *line;
1237 tail = &t->next;
1238 }
1239 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001240 if (type != TOK_COMMENT) {
1241 *tail = t = new_Token(NULL, type, line, p - line);
1242 tail = &t->next;
1243 }
1244 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001245 }
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +04001246
1247 nasm_dump_token(list);
1248
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001249 return list;
1250}
1251
H. Peter Anvince616072002-04-30 21:02:23 +00001252/*
1253 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001254 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001255 * deleted only all at once by the delete_Blocks function.
1256 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001257static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001258{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001259 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001260
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001261 /* first, get to the end of the linked list */
1262 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 b = b->next;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001264
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001265 /* now allocate the requested chunk */
1266 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001267
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001268 /* now allocate a new block for the next request */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001269 b->next = nasm_zalloc(sizeof(Blocks));
1270
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001271 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001272}
1273
1274/*
1275 * this function deletes all managed blocks of memory
1276 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001277static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001278{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001279 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001280
H. Peter Anvin70653092007-10-19 14:42:29 -07001281 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001282 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001283 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001284 * free it.
1285 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001286 while (b) {
1287 if (b->chunk)
1288 nasm_free(b->chunk);
1289 a = b;
1290 b = b->next;
1291 if (a != &blocks)
1292 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001293 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001294}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001295
1296/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001297 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001298 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001299 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001300 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001301static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001302 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001303{
1304 Token *t;
1305 int i;
1306
H. Peter Anvin89cee572009-07-15 09:16:54 -04001307 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001308 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1309 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1310 freeTokens[i].next = &freeTokens[i + 1];
1311 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001312 }
1313 t = freeTokens;
1314 freeTokens = t->next;
1315 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001316 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001317 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001318 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319 t->text = NULL;
1320 } else {
1321 if (txtlen == 0)
1322 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001323 t->text = nasm_malloc(txtlen+1);
1324 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001326 }
1327 return t;
1328}
1329
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001330static Token *copy_Token(Token * tline)
1331{
1332 Token *t, *tt, *first = NULL, *prev = NULL;
1333 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001334 for (tt = tline; tt != NULL; tt = tt->next) {
1335 if (!freeTokens) {
1336 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1337 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1338 freeTokens[i].next = &freeTokens[i + 1];
1339 freeTokens[i].next = NULL;
1340 }
1341 t = freeTokens;
1342 freeTokens = t->next;
1343 t->next = NULL;
1344 t->text = tt->text ? nasm_strdup(tt->text) : NULL;
1345 t->a.mac = tt->a.mac;
1346 t->a.len = tt->a.len;
1347 t->type = tt->type;
1348 if (prev != NULL) {
1349 prev->next = t;
1350 } else {
1351 first = t;
1352 }
1353 prev = t;
1354 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001355 return first;
1356}
1357
H. Peter Anvine2c80182005-01-15 22:15:51 +00001358static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001359{
1360 Token *next = t->next;
1361 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001362 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001363 freeTokens = t;
1364 return next;
1365}
1366
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001367/*
1368 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001369 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1370 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001371 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001372static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001373{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001374 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001375 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001376 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001377 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001378
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001379 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001381 char *v;
1382 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001383
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001384 v = t->text + 2;
1385 if (*v == '\'' || *v == '\"' || *v == '`') {
1386 size_t len = nasm_unquote(v, NULL);
1387 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001388
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001389 if (len != clen) {
1390 error(ERR_NONFATAL | ERR_PASS1,
1391 "NUL character in %! string");
1392 v = NULL;
1393 }
1394 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001395
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001396 if (v) {
1397 char *p = getenv(v);
1398 if (!p) {
1399 error(ERR_NONFATAL | ERR_PASS1,
1400 "nonexistent environment variable `%s'", v);
1401 p = "";
1402 }
1403 t->text = nasm_strdup(p);
1404 }
1405 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001406 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001407
H. Peter Anvine2c80182005-01-15 22:15:51 +00001408 /* Expand local macros here and not during preprocessing */
1409 if (expand_locals &&
1410 t->type == TOK_PREPROC_ID && t->text &&
1411 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001412 const char *q;
1413 char *p;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001414 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001415 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001416 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001417 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001418 p = nasm_strcat(buffer, q);
1419 nasm_free(t->text);
1420 t->text = p;
1421 }
1422 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001423
1424 /* Expand %? and %?? directives */
Keith Kanios3136d482010-11-13 09:34:34 -06001425 if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001426 ((t->type == TOK_PREPROC_Q) ||
1427 (t->type == TOK_PREPROC_QQ))) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001428 ExpInv *ei;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001429 for (ei = istk->expansion; ei != NULL; ei = ei->prev){
1430 if (ei->type == EXP_MMACRO) {
1431 nasm_free(t->text);
1432 if (t->type == TOK_PREPROC_Q) {
1433 t->text = nasm_strdup(ei->name);
1434 } else {
1435 t->text = nasm_strdup(ei->def->name);
1436 }
1437 break;
1438 }
1439 }
1440 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001441
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001442 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001443 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001444 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001445 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001446 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001447
H. Peter Anvin734b1882002-04-30 21:01:08 +00001448 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001449
1450 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001451 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001452 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001453 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001454 q = t->text;
1455 while (*q)
1456 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001457 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001458 }
1459 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001460
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001461 return line;
1462}
1463
1464/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001465 * Initialize a new Line
1466 */
Cyrill Gorcunov29cb0bb2010-11-11 11:19:43 +03001467static inline Line *new_Line(void)
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001468{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001469 return (Line *)nasm_zalloc(sizeof(Line));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001470}
1471
1472
1473/*
1474 * Initialize a new Expansion Definition
1475 */
1476static ExpDef *new_ExpDef(int exp_type)
1477{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001478 ExpDef *ed = (ExpDef*)nasm_zalloc(sizeof(ExpDef));
1479 ed->type = exp_type;
1480 ed->casesense = true;
1481 ed->state = COND_NEVER;
1482
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001483 return ed;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001484}
1485
1486
1487/*
1488 * Initialize a new Expansion Instance
1489 */
1490static ExpInv *new_ExpInv(int exp_type, ExpDef *ed)
1491{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001492 ExpInv *ei = (ExpInv*)nasm_zalloc(sizeof(ExpInv));
1493 ei->type = exp_type;
1494 ei->def = ed;
1495 ei->unique = ++unique;
1496
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001497 if ((istk->mmac_depth < 1) &&
1498 (istk->expansion == NULL) &&
1499 (ed != NULL) &&
1500 (ed->type != EXP_MMACRO) &&
1501 (ed->type != EXP_REP) &&
1502 (ed->type != EXP_WHILE)) {
1503 ei->linnum = src_get_linnum();
1504 src_set_linnum(ei->linnum - ed->linecount - 1);
1505 } else {
1506 ei->linnum = -1;
1507 }
1508 if ((istk->expansion == NULL) ||
1509 (ei->type == EXP_MMACRO)) {
1510 ei->relno = 0;
1511 } else {
1512 ei->relno = istk->expansion->lineno;
1513 if (ed != NULL) {
1514 ei->relno -= (ed->linecount + 1);
1515 }
1516 }
1517 return ei;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001518}
1519
1520/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001521 * A scanner, suitable for use by the expression evaluator, which
1522 * operates on a line of Tokens. Expects a pointer to a pointer to
1523 * the first token in the line to be passed in as its private_data
1524 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001525 *
1526 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001527 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001528static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001529{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001530 Token **tlineptr = private_data;
1531 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001532 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001533
H. Peter Anvine2c80182005-01-15 22:15:51 +00001534 do {
1535 tline = *tlineptr;
1536 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001537 } while (tline && (tline->type == TOK_WHITESPACE ||
1538 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001539
1540 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001541 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001542
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001543 tokval->t_charptr = tline->text;
1544
H. Peter Anvin76690a12002-04-30 20:52:49 +00001545 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001546 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001547 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001548 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001549
H. Peter Anvine2c80182005-01-15 22:15:51 +00001550 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001551 p = tokval->t_charptr = tline->text;
1552 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001553 tokval->t_charptr++;
1554 return tokval->t_type = TOKEN_ID;
1555 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001556
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001557 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001558 if (r >= p+MAX_KEYWORD)
1559 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001560 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001561 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001562 *s = '\0';
1563 /* right, so we have an identifier sitting in temp storage. now,
1564 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001565 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001566 }
1567
H. Peter Anvine2c80182005-01-15 22:15:51 +00001568 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001569 bool rn_error;
1570 tokval->t_integer = readnum(tline->text, &rn_error);
1571 tokval->t_charptr = tline->text;
1572 if (rn_error)
1573 return tokval->t_type = TOKEN_ERRNUM;
1574 else
1575 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001576 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001577
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001578 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001579 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001580 }
1581
H. Peter Anvine2c80182005-01-15 22:15:51 +00001582 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001583 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001584
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001585 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001586 tokval->t_charptr = tline->text;
1587 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001588
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001589 if (ep[0] != bq || ep[1] != '\0')
1590 return tokval->t_type = TOKEN_ERRSTR;
1591 else
1592 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001593 }
1594
H. Peter Anvine2c80182005-01-15 22:15:51 +00001595 if (tline->type == TOK_OTHER) {
1596 if (!strcmp(tline->text, "<<"))
1597 return tokval->t_type = TOKEN_SHL;
1598 if (!strcmp(tline->text, ">>"))
1599 return tokval->t_type = TOKEN_SHR;
1600 if (!strcmp(tline->text, "//"))
1601 return tokval->t_type = TOKEN_SDIV;
1602 if (!strcmp(tline->text, "%%"))
1603 return tokval->t_type = TOKEN_SMOD;
1604 if (!strcmp(tline->text, "=="))
1605 return tokval->t_type = TOKEN_EQ;
1606 if (!strcmp(tline->text, "<>"))
1607 return tokval->t_type = TOKEN_NE;
1608 if (!strcmp(tline->text, "!="))
1609 return tokval->t_type = TOKEN_NE;
1610 if (!strcmp(tline->text, "<="))
1611 return tokval->t_type = TOKEN_LE;
1612 if (!strcmp(tline->text, ">="))
1613 return tokval->t_type = TOKEN_GE;
1614 if (!strcmp(tline->text, "&&"))
1615 return tokval->t_type = TOKEN_DBL_AND;
1616 if (!strcmp(tline->text, "^^"))
1617 return tokval->t_type = TOKEN_DBL_XOR;
1618 if (!strcmp(tline->text, "||"))
1619 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001620 }
1621
1622 /*
1623 * We have no other options: just return the first character of
1624 * the token text.
1625 */
1626 return tokval->t_type = tline->text[0];
1627}
1628
1629/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001630 * Compare a string to the name of an existing macro; this is a
1631 * simple wrapper which calls either strcmp or nasm_stricmp
1632 * depending on the value of the `casesense' parameter.
1633 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001634static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001635{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001636 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001637}
1638
1639/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001640 * Compare a string to the name of an existing macro; this is a
1641 * simple wrapper which calls either strcmp or nasm_stricmp
1642 * depending on the value of the `casesense' parameter.
1643 */
1644static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1645{
1646 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1647}
1648
1649/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001650 * Return the Context structure associated with a %$ token. Return
1651 * NULL, having _already_ reported an error condition, if the
1652 * context stack isn't deep enough for the supplied number of $
1653 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001654 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001655 * also scanned for such smacro, until it is found; if not -
1656 * only the context that directly results from the number of $'s
1657 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001658 *
1659 * If "namep" is non-NULL, set it to the pointer to the macro name
1660 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001661 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001662static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001663 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001664{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001665 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001666 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001667 int i;
1668
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001669 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001670 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001671
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001672 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001673 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001674
H. Peter Anvine2c80182005-01-15 22:15:51 +00001675 if (!cstk) {
1676 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1677 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001678 }
1679
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001680 name += 2;
1681 ctx = cstk;
1682 i = 0;
1683 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001684 name++;
1685 i++;
1686 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001687 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001688 if (!ctx) {
1689 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001690 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001691 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001692 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001693
1694 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001695 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001696
Keith Kanios404589e2010-08-10 20:12:57 -05001697 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001698 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001699
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001700 do {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001701 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001702 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001703 while (m) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001704 if (!mstrcmp(m->name, name, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001705 return ctx;
1706 m = m->next;
1707 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001708 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001709 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001710 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001711 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001712}
1713
1714/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001715 * Check to see if a file is already in a string list
1716 */
1717static bool in_list(const StrList *list, const char *str)
1718{
1719 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001720 if (!strcmp(list->str, str))
1721 return true;
1722 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001723 }
1724 return false;
1725}
1726
1727/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001728 * Open an include file. This routine must always return a valid
1729 * file pointer if it returns - it's responsible for throwing an
1730 * ERR_FATAL and bombing out completely if not. It should also try
1731 * the include path one by one until it finds the file or reaches
1732 * the end of the path.
1733 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001734static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001735 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001736{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001737 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001738 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001739 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001740 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001741 size_t prefix_len = 0;
1742 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001743
H. Peter Anvine2c80182005-01-15 22:15:51 +00001744 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001745 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunov6f38fe62010-11-11 11:32:16 +03001746 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001747 memcpy(sl->str, prefix, prefix_len);
1748 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001749 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001750 if (fp && dhead && !in_list(*dhead, sl->str)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001751 **dtail = sl;
1752 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001753 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001754 nasm_free(sl);
1755 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001756 if (fp)
1757 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001758 if (!ip) {
1759 if (!missing_ok)
1760 break;
1761 prefix = NULL;
1762 } else {
1763 prefix = ip->path;
1764 ip = ip->next;
1765 }
1766 if (prefix) {
1767 prefix_len = strlen(prefix);
1768 } else {
1769 /* -MG given and file not found */
1770 if (dhead && !in_list(*dhead, file)) {
1771 sl = nasm_malloc(len+1+sizeof sl->next);
1772 sl->next = NULL;
1773 strcpy(sl->str, file);
1774 **dtail = sl;
1775 *dtail = &sl->next;
1776 }
1777 return NULL;
1778 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001779 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001780
H. Peter Anvin734b1882002-04-30 21:01:08 +00001781 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001782 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001783}
1784
1785/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001786 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001787 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001788 * return true if _any_ single-line macro of that name is defined.
1789 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001790 * `nparam' or no parameters is defined.
1791 *
1792 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001793 * defined, or nparam is -1, the address of the definition structure
1794 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001795 * is NULL, no action will be taken regarding its contents, and no
1796 * error will occur.
1797 *
1798 * Note that this is also called with nparam zero to resolve
1799 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001800 *
1801 * If you already know which context macro belongs to, you can pass
1802 * the context pointer as first parameter; if you won't but name begins
1803 * with %$ the context will be automatically computed. If all_contexts
1804 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001805 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001806static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001807smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001808 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001809{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001810 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001811 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001812
H. Peter Anvin97a23472007-09-16 17:57:25 -07001813 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001814 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001815 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001816 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001817 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001818 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001819 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001820 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001821 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001822 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001823 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001824 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001825
H. Peter Anvine2c80182005-01-15 22:15:51 +00001826 while (m) {
1827 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001828 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001829 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001830 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831 *defn = m;
1832 else
1833 *defn = NULL;
1834 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001835 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001836 }
1837 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001838 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001839
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001840 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001841}
1842
1843/*
1844 * Count and mark off the parameters in a multi-line macro call.
1845 * This is called both from within the multi-line macro expansion
1846 * code, and also to mark off the default parameters when provided
1847 * in a %macro definition line.
1848 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001849static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001850{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001851 int paramsize, brace;
1852
1853 *nparam = paramsize = 0;
1854 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001855 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001856 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001857 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001858 paramsize += PARAM_DELTA;
1859 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1860 }
1861 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001862 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001863 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001864 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001865 (*params)[(*nparam)++] = t;
1866 while (tok_isnt_(t, brace ? "}" : ","))
1867 t = t->next;
1868 if (t) { /* got a comma/brace */
1869 t = t->next;
1870 if (brace) {
1871 /*
1872 * Now we've found the closing brace, look further
1873 * for the comma.
1874 */
1875 skip_white_(t);
1876 if (tok_isnt_(t, ",")) {
1877 error(ERR_NONFATAL,
1878 "braces do not enclose all of macro parameter");
1879 while (tok_isnt_(t, ","))
1880 t = t->next;
1881 }
1882 if (t)
1883 t = t->next; /* eat the comma */
1884 }
1885 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001886 }
1887}
1888
1889/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001890 * Determine whether one of the various `if' conditions is true or
1891 * not.
1892 *
1893 * We must free the tline we get passed.
1894 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001895static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001896{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001897 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001898 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001899 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001900 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001901 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001902 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001903 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001904
1905 origline = tline;
1906
H. Peter Anvine2c80182005-01-15 22:15:51 +00001907 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001908 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001909 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001910 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001911 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001912 if (!tline)
1913 break;
1914 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001915 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001916 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001917 free_tlist(origline);
1918 return -1;
1919 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001920 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001921 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001922 tline = tline->next;
1923 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001924 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001925
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001926 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001927 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001928 while (tline) {
1929 skip_white_(tline);
1930 if (!tline || (tline->type != TOK_ID &&
1931 (tline->type != TOK_PREPROC_ID ||
1932 tline->text[1] != '$'))) {
1933 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001934 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001935 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001936 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001937 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001938 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001939 tline = tline->next;
1940 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001941 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001942
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001943 case PPC_IFENV:
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001944 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001945 j = false; /* have we matched yet? */
1946 while (tline) {
1947 skip_white_(tline);
1948 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001949 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001950 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001951 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001952 error(ERR_NONFATAL,
1953 "`%s' expects environment variable names",
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001954 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001955 goto fail;
1956 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001957 p = tline->text;
1958 if (tline->type == TOK_PREPROC_ID)
1959 p += 2; /* Skip leading %! */
1960 if (*p == '\'' || *p == '\"' || *p == '`')
1961 nasm_unquote_cstr(p, ct);
1962 if (getenv(p))
1963 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001964 tline = tline->next;
1965 }
1966 break;
1967
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001968 case PPC_IFIDN:
1969 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001970 tline = expand_smacro(tline);
1971 t = tt = tline;
1972 while (tok_isnt_(tt, ","))
1973 tt = tt->next;
1974 if (!tt) {
1975 error(ERR_NONFATAL,
1976 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001977 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001978 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001979 }
1980 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001981 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001982 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1983 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1984 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001985 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001986 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001987 }
1988 if (t->type == TOK_WHITESPACE) {
1989 t = t->next;
1990 continue;
1991 }
1992 if (tt->type == TOK_WHITESPACE) {
1993 tt = tt->next;
1994 continue;
1995 }
1996 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001997 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001998 break;
1999 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002000 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002001 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002002 size_t l1 = nasm_unquote(t->text, NULL);
2003 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07002004
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002005 if (l1 != l2) {
2006 j = false;
2007 break;
2008 }
2009 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
2010 j = false;
2011 break;
2012 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002013 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002014 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002015 break;
2016 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002017
H. Peter Anvine2c80182005-01-15 22:15:51 +00002018 t = t->next;
2019 tt = tt->next;
2020 }
2021 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002022 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002023 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002024
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002025 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002026 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002027 bool found = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002028 ExpDef searching, *ed;
H. Peter Anvin65747262002-05-07 00:10:05 +00002029
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002030 skip_white_(tline);
2031 tline = expand_id(tline);
2032 if (!tok_type_(tline, TOK_ID)) {
2033 error(ERR_NONFATAL,
2034 "`%s' expects a macro name", pp_directives[ct]);
2035 goto fail;
2036 }
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002037 memset(&searching, 0, sizeof(searching));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002038 searching.name = nasm_strdup(tline->text);
2039 searching.casesense = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002040 searching.nparam_max = INT_MAX;
2041 tline = expand_smacro(tline->next);
2042 skip_white_(tline);
2043 if (!tline) {
2044 } else if (!tok_type_(tline, TOK_NUMBER)) {
2045 error(ERR_NONFATAL,
2046 "`%s' expects a parameter count or nothing",
2047 pp_directives[ct]);
2048 } else {
2049 searching.nparam_min = searching.nparam_max =
2050 readnum(tline->text, &j);
2051 if (j)
2052 error(ERR_NONFATAL,
2053 "unable to parse parameter count `%s'",
2054 tline->text);
2055 }
2056 if (tline && tok_is_(tline->next, "-")) {
2057 tline = tline->next->next;
2058 if (tok_is_(tline, "*"))
2059 searching.nparam_max = INT_MAX;
2060 else if (!tok_type_(tline, TOK_NUMBER))
2061 error(ERR_NONFATAL,
2062 "`%s' expects a parameter count after `-'",
2063 pp_directives[ct]);
2064 else {
2065 searching.nparam_max = readnum(tline->text, &j);
2066 if (j)
2067 error(ERR_NONFATAL,
2068 "unable to parse parameter count `%s'",
2069 tline->text);
2070 if (searching.nparam_min > searching.nparam_max)
2071 error(ERR_NONFATAL,
2072 "minimum parameter count exceeds maximum");
2073 }
2074 }
2075 if (tline && tok_is_(tline->next, "+")) {
2076 tline = tline->next;
2077 searching.plus = true;
2078 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002079 ed = (ExpDef *) hash_findix(&expdefs, searching.name);
2080 while (ed != NULL) {
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002081 if (!strcmp(ed->name, searching.name) &&
2082 (ed->nparam_min <= searching.nparam_max || searching.plus) &&
2083 (searching.nparam_min <= ed->nparam_max || ed->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002084 found = true;
2085 break;
2086 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002087 ed = ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002088 }
2089 if (tline && tline->next)
2090 error(ERR_WARNING|ERR_PASS1,
2091 "trailing garbage after %%ifmacro ignored");
2092 nasm_free(searching.name);
2093 j = found;
2094 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002095 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002096
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002097 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002098 needtype = TOK_ID;
2099 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002100 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002101 needtype = TOK_NUMBER;
2102 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002103 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002104 needtype = TOK_STRING;
2105 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002106
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002107iftype:
2108 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002109
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002110 while (tok_type_(t, TOK_WHITESPACE) ||
2111 (needtype == TOK_NUMBER &&
2112 tok_type_(t, TOK_OTHER) &&
2113 (t->text[0] == '-' || t->text[0] == '+') &&
2114 !t->text[1]))
2115 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002116
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002117 j = tok_type_(t, needtype);
2118 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002119
2120 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002121 t = tline = expand_smacro(tline);
2122 while (tok_type_(t, TOK_WHITESPACE))
2123 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002124
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002125 j = false;
2126 if (t) {
2127 t = t->next; /* Skip the actual token */
2128 while (tok_type_(t, TOK_WHITESPACE))
2129 t = t->next;
2130 j = !t; /* Should be nothing left */
2131 }
2132 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002133
H. Peter Anvin134b9462008-02-16 17:01:40 -08002134 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002135 t = tline = expand_smacro(tline);
2136 while (tok_type_(t, TOK_WHITESPACE))
2137 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002138
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002139 j = !t; /* Should be empty */
2140 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002141
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002142 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002143 t = tline = expand_smacro(tline);
2144 tptr = &t;
2145 tokval.t_type = TOKEN_INVALID;
2146 evalresult = evaluate(ppscan, tptr, &tokval,
2147 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002148 if (!evalresult)
2149 return -1;
2150 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002151 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002152 "trailing garbage after expression ignored");
2153 if (!is_simple(evalresult)) {
2154 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002155 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002156 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002157 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002158 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002159 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002160
H. Peter Anvine2c80182005-01-15 22:15:51 +00002161 default:
2162 error(ERR_FATAL,
2163 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002164 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002165 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002166 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002167
2168 free_tlist(origline);
2169 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002170
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002171fail:
2172 free_tlist(origline);
2173 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002174}
2175
2176/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002177 * Common code for defining an smacro
2178 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002179static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002180 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002181{
2182 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002183 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002184
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002185 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002186 if (!smac) {
2187 error(ERR_WARNING|ERR_PASS1,
2188 "single-line macro `%s' defined both with and"
2189 " without parameters", mname);
2190 /*
2191 * Some instances of the old code considered this a failure,
2192 * some others didn't. What is the right thing to do here?
2193 */
2194 free_tlist(expansion);
2195 return false; /* Failure */
2196 } else {
2197 /*
2198 * We're redefining, so we have to take over an
2199 * existing SMacro structure. This means freeing
2200 * what was already in it.
2201 */
2202 nasm_free(smac->name);
2203 free_tlist(smac->expansion);
2204 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002205 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002206 smtbl = ctx ? &ctx->localmac : &smacros;
2207 smhead = (SMacro **) hash_findi_add(smtbl, mname);
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002208 smac = nasm_zalloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002209 smac->next = *smhead;
2210 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002211 }
2212 smac->name = nasm_strdup(mname);
2213 smac->casesense = casesense;
2214 smac->nparam = nparam;
2215 smac->expansion = expansion;
2216 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002217 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002218}
2219
2220/*
2221 * Undefine an smacro
2222 */
2223static void undef_smacro(Context *ctx, const char *mname)
2224{
2225 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002226 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002227
H. Peter Anvin166c2472008-05-28 12:28:58 -07002228 smtbl = ctx ? &ctx->localmac : &smacros;
2229 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002230
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002231 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002232 /*
2233 * We now have a macro name... go hunt for it.
2234 */
2235 sp = smhead;
2236 while ((s = *sp) != NULL) {
2237 if (!mstrcmp(s->name, mname, s->casesense)) {
2238 *sp = s->next;
2239 nasm_free(s->name);
2240 free_tlist(s->expansion);
2241 nasm_free(s);
2242 } else {
2243 sp = &s->next;
2244 }
2245 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002246 }
2247}
2248
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002249/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002250 * Parse a mmacro specification.
2251 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002252static bool parse_mmacro_spec(Token *tline, ExpDef *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002253{
2254 bool err;
2255
2256 tline = tline->next;
2257 skip_white_(tline);
2258 tline = expand_id(tline);
2259 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002260 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2261 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002262 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002263
H. Peter Anvina26433d2008-07-16 14:40:01 -07002264 def->name = nasm_strdup(tline->text);
2265 def->plus = false;
2266 def->nolist = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002267// def->in_progress = 0;
2268// def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002269 def->nparam_min = 0;
2270 def->nparam_max = 0;
2271
H. Peter Anvina26433d2008-07-16 14:40:01 -07002272 tline = expand_smacro(tline->next);
2273 skip_white_(tline);
2274 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002275 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002276 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002277 def->nparam_min = def->nparam_max =
2278 readnum(tline->text, &err);
2279 if (err)
2280 error(ERR_NONFATAL,
2281 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002282 }
2283 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002284 tline = tline->next->next;
2285 if (tok_is_(tline, "*")) {
2286 def->nparam_max = INT_MAX;
2287 } else if (!tok_type_(tline, TOK_NUMBER)) {
2288 error(ERR_NONFATAL,
2289 "`%s' expects a parameter count after `-'", directive);
2290 } else {
2291 def->nparam_max = readnum(tline->text, &err);
2292 if (err) {
2293 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2294 tline->text);
2295 }
2296 if (def->nparam_min > def->nparam_max) {
2297 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2298 }
2299 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002300 }
2301 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002302 tline = tline->next;
2303 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002304 }
2305 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002306 !nasm_stricmp(tline->next->text, ".nolist")) {
2307 tline = tline->next;
2308 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002309 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002310
H. Peter Anvina26433d2008-07-16 14:40:01 -07002311 /*
2312 * Handle default parameters.
2313 */
2314 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002315 def->dlist = tline->next;
2316 tline->next = NULL;
2317 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002318 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002319 def->dlist = NULL;
2320 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002321 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002322 def->line = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002323
H. Peter Anvin89cee572009-07-15 09:16:54 -04002324 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002325 !def->plus)
2326 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2327 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002328
H. Peter Anvina26433d2008-07-16 14:40:01 -07002329 return true;
2330}
2331
2332
2333/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002334 * Decode a size directive
2335 */
2336static int parse_size(const char *str) {
2337 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002338 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002339 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002340 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002341
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002342 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002343}
2344
Ed Beroset3ab3f412002-06-11 03:31:49 +00002345/**
2346 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002347 * Find out if a line contains a preprocessor directive, and deal
2348 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002349 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002350 * If a directive _is_ found, it is the responsibility of this routine
2351 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002352 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002353 * @param tline a pointer to the current tokeninzed line linked list
2354 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002355 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002356 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002357static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002358{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002359 enum preproc_token i;
2360 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002361 bool err;
2362 int nparam;
2363 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002364 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002365 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002366 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002367 char *p, *pp;
2368 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002369 Include *inc;
2370 Context *ctx;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002371 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002372 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002373 struct tokenval tokval;
2374 expr *evalresult;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002375 ExpDef *ed, *eed, **edhead;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002376 ExpInv *ei, *eei;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002377 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002378 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002379 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002380
2381 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002382
H. Peter Anvineba20a72002-04-30 20:53:55 +00002383 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002384 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002385 (tline->text[1] == '%' || tline->text[1] == '$'
2386 || tline->text[1] == '!'))
2387 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002388
H. Peter Anvin4169a472007-09-12 01:29:43 +00002389 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002390
H. Peter Anvin4169a472007-09-12 01:29:43 +00002391 switch (i) {
2392 case PP_INVALID:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002393 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002394 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2395 tline->text);
2396 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002397
H. Peter Anvine2c80182005-01-15 22:15:51 +00002398 case PP_STACKSIZE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002399 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002400 /* Directive to tell NASM what the default stack size is. The
2401 * default is for a 16-bit stack, and this can be overriden with
2402 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002403 */
2404 tline = tline->next;
2405 if (tline && tline->type == TOK_WHITESPACE)
2406 tline = tline->next;
2407 if (!tline || tline->type != TOK_ID) {
2408 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2409 free_tlist(origline);
2410 return DIRECTIVE_FOUND;
2411 }
2412 if (nasm_stricmp(tline->text, "flat") == 0) {
2413 /* All subsequent ARG directives are for a 32-bit stack */
2414 StackSize = 4;
2415 StackPointer = "ebp";
2416 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002417 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002418 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2419 /* All subsequent ARG directives are for a 64-bit stack */
2420 StackSize = 8;
2421 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002422 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002423 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002424 } else if (nasm_stricmp(tline->text, "large") == 0) {
2425 /* All subsequent ARG directives are for a 16-bit stack,
2426 * far function call.
2427 */
2428 StackSize = 2;
2429 StackPointer = "bp";
2430 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002431 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002432 } else if (nasm_stricmp(tline->text, "small") == 0) {
2433 /* All subsequent ARG directives are for a 16-bit stack,
2434 * far function call. We don't support near functions.
2435 */
2436 StackSize = 2;
2437 StackPointer = "bp";
2438 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002439 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002440 } else {
2441 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2442 free_tlist(origline);
2443 return DIRECTIVE_FOUND;
2444 }
2445 free_tlist(origline);
2446 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002447
H. Peter Anvine2c80182005-01-15 22:15:51 +00002448 case PP_ARG:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002449 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002450 /* TASM like ARG directive to define arguments to functions, in
2451 * the following form:
2452 *
2453 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2454 */
2455 offset = ArgOffset;
2456 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002457 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002458 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002459
H. Peter Anvine2c80182005-01-15 22:15:51 +00002460 /* Find the argument name */
2461 tline = tline->next;
2462 if (tline && tline->type == TOK_WHITESPACE)
2463 tline = tline->next;
2464 if (!tline || tline->type != TOK_ID) {
2465 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2466 free_tlist(origline);
2467 return DIRECTIVE_FOUND;
2468 }
2469 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002470
H. Peter Anvine2c80182005-01-15 22:15:51 +00002471 /* Find the argument size type */
2472 tline = tline->next;
2473 if (!tline || tline->type != TOK_OTHER
2474 || tline->text[0] != ':') {
2475 error(ERR_NONFATAL,
2476 "Syntax error processing `%%arg' directive");
2477 free_tlist(origline);
2478 return DIRECTIVE_FOUND;
2479 }
2480 tline = tline->next;
2481 if (!tline || tline->type != TOK_ID) {
2482 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2483 free_tlist(origline);
2484 return DIRECTIVE_FOUND;
2485 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002486
H. Peter Anvine2c80182005-01-15 22:15:51 +00002487 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002488 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002489 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002490 size = parse_size(tt->text);
2491 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002492 error(ERR_NONFATAL,
2493 "Invalid size type for `%%arg' missing directive");
2494 free_tlist(tt);
2495 free_tlist(origline);
2496 return DIRECTIVE_FOUND;
2497 }
2498 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002499
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002500 /* Round up to even stack slots */
2501 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002502
H. Peter Anvine2c80182005-01-15 22:15:51 +00002503 /* Now define the macro for the argument */
2504 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2505 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002506 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002507 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002508
H. Peter Anvine2c80182005-01-15 22:15:51 +00002509 /* Move to the next argument in the list */
2510 tline = tline->next;
2511 if (tline && tline->type == TOK_WHITESPACE)
2512 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002513 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002514 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002515 free_tlist(origline);
2516 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002517
H. Peter Anvine2c80182005-01-15 22:15:51 +00002518 case PP_LOCAL:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002519 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002520 /* TASM like LOCAL directive to define local variables for a
2521 * function, in the following form:
2522 *
2523 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2524 *
2525 * The '= LocalSize' at the end is ignored by NASM, but is
2526 * required by TASM to define the local parameter size (and used
2527 * by the TASM macro package).
2528 */
2529 offset = LocalOffset;
2530 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002531 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002532 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002533
H. Peter Anvine2c80182005-01-15 22:15:51 +00002534 /* Find the argument name */
2535 tline = tline->next;
2536 if (tline && tline->type == TOK_WHITESPACE)
2537 tline = tline->next;
2538 if (!tline || tline->type != TOK_ID) {
2539 error(ERR_NONFATAL,
2540 "`%%local' missing argument parameter");
2541 free_tlist(origline);
2542 return DIRECTIVE_FOUND;
2543 }
2544 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002545
H. Peter Anvine2c80182005-01-15 22:15:51 +00002546 /* Find the argument size type */
2547 tline = tline->next;
2548 if (!tline || tline->type != TOK_OTHER
2549 || tline->text[0] != ':') {
2550 error(ERR_NONFATAL,
2551 "Syntax error processing `%%local' directive");
2552 free_tlist(origline);
2553 return DIRECTIVE_FOUND;
2554 }
2555 tline = tline->next;
2556 if (!tline || tline->type != TOK_ID) {
2557 error(ERR_NONFATAL,
2558 "`%%local' missing size type parameter");
2559 free_tlist(origline);
2560 return DIRECTIVE_FOUND;
2561 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002562
H. Peter Anvine2c80182005-01-15 22:15:51 +00002563 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002564 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002565 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002566 size = parse_size(tt->text);
2567 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002568 error(ERR_NONFATAL,
2569 "Invalid size type for `%%local' missing directive");
2570 free_tlist(tt);
2571 free_tlist(origline);
2572 return DIRECTIVE_FOUND;
2573 }
2574 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002575
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002576 /* Round up to even stack slots */
2577 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002578
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002579 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002580
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002581 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002582 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2583 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002584 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002585
H. Peter Anvine2c80182005-01-15 22:15:51 +00002586 /* Now define the assign to setup the enter_c macro correctly */
2587 snprintf(directive, sizeof(directive),
2588 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002589 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002590
H. Peter Anvine2c80182005-01-15 22:15:51 +00002591 /* Move to the next argument in the list */
2592 tline = tline->next;
2593 if (tline && tline->type == TOK_WHITESPACE)
2594 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002595 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002596 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002597 free_tlist(origline);
2598 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002599
H. Peter Anvine2c80182005-01-15 22:15:51 +00002600 case PP_CLEAR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002601 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002602 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002603 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002604 "trailing garbage after `%%clear' ignored");
2605 free_macros();
2606 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002607 free_tlist(origline);
2608 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002609
H. Peter Anvin418ca702008-05-30 10:42:30 -07002610 case PP_DEPEND:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002611 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002612 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002613 skip_white_(t);
2614 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002615 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002616 error(ERR_NONFATAL, "`%%depend' expects a file name");
2617 free_tlist(origline);
2618 return DIRECTIVE_FOUND; /* but we did _something_ */
2619 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002620 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002621 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002622 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002623 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002624 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002625 nasm_unquote_cstr(p, i);
2626 if (dephead && !in_list(*dephead, p)) {
2627 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2628 sl->next = NULL;
2629 strcpy(sl->str, p);
2630 *deptail = sl;
2631 deptail = &sl->next;
2632 }
2633 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002634 return DIRECTIVE_FOUND;
2635
2636 case PP_INCLUDE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002637 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002638 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002639 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002640
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002641 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002642 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002643 error(ERR_NONFATAL, "`%%include' expects a file name");
2644 free_tlist(origline);
2645 return DIRECTIVE_FOUND; /* but we did _something_ */
2646 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002647 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002648 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002649 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002650 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002651 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002652 nasm_unquote_cstr(p, i);
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03002653 inc = nasm_zalloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002654 inc->next = istk;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002655 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002656 if (!inc->fp) {
2657 /* -MG given but file not found */
2658 nasm_free(inc);
2659 } else {
2660 inc->fname = src_set_fname(nasm_strdup(p));
2661 inc->lineno = src_set_linnum(0);
2662 inc->lineinc = 1;
2663 inc->expansion = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002664 istk = inc;
2665 list->uplevel(LIST_INCLUDE);
2666 }
2667 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002668 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002669
H. Peter Anvind2456592008-06-19 15:04:18 -07002670 case PP_USE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002671 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002672 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002673 static macros_t *use_pkg;
2674 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002675
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002676 tline = tline->next;
2677 skip_white_(tline);
2678 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002679
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002680 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002681 tline->type != TOK_INTERNAL_STRING &&
2682 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002683 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002684 free_tlist(origline);
2685 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002686 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002687 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002688 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002689 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002690 if (tline->type == TOK_STRING)
2691 nasm_unquote_cstr(tline->text, i);
2692 use_pkg = nasm_stdmac_find_package(tline->text);
2693 if (!use_pkg)
2694 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2695 else
2696 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002697 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002698 /* Not already included, go ahead and include it */
2699 stdmacpos = use_pkg;
2700 }
2701 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002702 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002703 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002704 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002705 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002706 case PP_POP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002707 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002708 tline = tline->next;
2709 skip_white_(tline);
2710 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002711 if (tline) {
2712 if (!tok_type_(tline, TOK_ID)) {
2713 error(ERR_NONFATAL, "`%s' expects a context identifier",
2714 pp_directives[i]);
2715 free_tlist(origline);
2716 return DIRECTIVE_FOUND; /* but we did _something_ */
2717 }
2718 if (tline->next)
2719 error(ERR_WARNING|ERR_PASS1,
2720 "trailing garbage after `%s' ignored",
2721 pp_directives[i]);
2722 p = nasm_strdup(tline->text);
2723 } else {
2724 p = NULL; /* Anonymous */
2725 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002726
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002727 if (i == PP_PUSH) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002728 ctx = nasm_zalloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002729 ctx->next = cstk;
2730 hash_init(&ctx->localmac, HASH_SMALL);
2731 ctx->name = p;
2732 ctx->number = unique++;
2733 cstk = ctx;
2734 } else {
2735 /* %pop or %repl */
2736 if (!cstk) {
2737 error(ERR_NONFATAL, "`%s': context stack is empty",
2738 pp_directives[i]);
2739 } else if (i == PP_POP) {
2740 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2741 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2742 "expected %s",
2743 cstk->name ? cstk->name : "anonymous", p);
2744 else
2745 ctx_pop();
2746 } else {
2747 /* i == PP_REPL */
2748 nasm_free(cstk->name);
2749 cstk->name = p;
2750 p = NULL;
2751 }
2752 nasm_free(p);
2753 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002754 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002755 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002756 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002757 severity = ERR_FATAL;
2758 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002759 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002760 severity = ERR_NONFATAL;
2761 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002762 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002763 severity = ERR_WARNING|ERR_WARN_USER;
2764 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002765
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002766issue_error:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002767 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002768 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002769 /* Only error out if this is the final pass */
2770 if (pass != 2 && i != PP_FATAL)
2771 return DIRECTIVE_FOUND;
2772
2773 tline->next = expand_smacro(tline->next);
2774 tline = tline->next;
2775 skip_white_(tline);
2776 t = tline ? tline->next : NULL;
2777 skip_white_(t);
2778 if (tok_type_(tline, TOK_STRING) && !t) {
2779 /* The line contains only a quoted string */
2780 p = tline->text;
2781 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2782 error(severity, "%s", p);
2783 } else {
2784 /* Not a quoted string, or more than a quoted string */
2785 p = detoken(tline, false);
2786 error(severity, "%s", p);
2787 nasm_free(p);
2788 }
2789 free_tlist(origline);
2790 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002791 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002792
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002793 CASE_PP_IF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002794 if (defining != NULL) {
2795 if (defining->type == EXP_IF) {
2796 defining->def_depth ++;
2797 }
2798 return NO_DIRECTIVE_FOUND;
2799 }
2800 if ((istk->expansion != NULL) &&
2801 (istk->expansion->emitting == false)) {
2802 j = COND_NEVER;
2803 } else {
2804 j = if_condition(tline->next, i);
2805 tline->next = NULL; /* it got freed */
2806 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
2807 }
2808 ed = new_ExpDef(EXP_IF);
2809 ed->state = j;
2810 ed->nolist = NULL;
2811 ed->def_depth = 0;
2812 ed->cur_depth = 0;
2813 ed->max_depth = 0;
2814 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
2815 ed->prev = defining;
2816 defining = ed;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002817 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002818 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002819
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002820 CASE_PP_ELIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002821 if (defining != NULL) {
2822 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2823 return NO_DIRECTIVE_FOUND;
2824 }
2825 }
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +04002826 if ((defining == NULL) || (defining->type != EXP_IF)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002827 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2828 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002829 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002830 case COND_IF_TRUE:
2831 defining->state = COND_DONE;
2832 defining->ignoring = true;
2833 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002834
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002835 case COND_DONE:
2836 case COND_NEVER:
2837 defining->ignoring = true;
2838 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002839
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002840 case COND_ELSE_TRUE:
2841 case COND_ELSE_FALSE:
2842 error_precond(ERR_WARNING|ERR_PASS1,
2843 "`%%elif' after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002844 defining->state = COND_NEVER;
2845 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002846 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002847
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002848 case COND_IF_FALSE:
2849 /*
2850 * IMPORTANT: In the case of %if, we will already have
2851 * called expand_mmac_params(); however, if we're
2852 * processing an %elif we must have been in a
2853 * non-emitting mode, which would have inhibited
2854 * the normal invocation of expand_mmac_params().
2855 * Therefore, we have to do it explicitly here.
2856 */
2857 j = if_condition(expand_mmac_params(tline->next), i);
2858 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002859 defining->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002860 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002861 defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002862 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002863 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002864 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002865 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002866
H. Peter Anvine2c80182005-01-15 22:15:51 +00002867 case PP_ELSE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002868 if (defining != NULL) {
2869 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2870 return NO_DIRECTIVE_FOUND;
2871 }
2872 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002873 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002874 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002875 "trailing garbage after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002876 if ((defining == NULL) || (defining->type != EXP_IF)) {
2877 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2878 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002879 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002880 case COND_IF_TRUE:
2881 case COND_DONE:
2882 defining->state = COND_ELSE_FALSE;
2883 defining->ignoring = true;
2884 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002885
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002886 case COND_NEVER:
2887 defining->ignoring = true;
2888 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002889
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002890 case COND_IF_FALSE:
2891 defining->state = COND_ELSE_TRUE;
2892 defining->ignoring = false;
2893 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002894
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002895 case COND_ELSE_TRUE:
2896 case COND_ELSE_FALSE:
2897 error_precond(ERR_WARNING|ERR_PASS1,
2898 "`%%else' after `%%else' ignored.");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002899 defining->state = COND_NEVER;
2900 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002901 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002902 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002903 free_tlist(origline);
2904 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002905
H. Peter Anvine2c80182005-01-15 22:15:51 +00002906 case PP_ENDIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002907 if (defining != NULL) {
2908 if (defining->type == EXP_IF) {
2909 if (defining->def_depth > 0) {
2910 defining->def_depth --;
2911 return NO_DIRECTIVE_FOUND;
2912 }
2913 } else {
2914 return NO_DIRECTIVE_FOUND;
2915 }
2916 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002917 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002918 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002919 "trailing garbage after `%%endif' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002920 if ((defining == NULL) || (defining->type != EXP_IF)) {
2921 error(ERR_NONFATAL, "`%%endif': no matching `%%if'");
2922 return DIRECTIVE_FOUND;
2923 }
2924 ed = defining;
2925 defining = ed->prev;
2926 ed->prev = expansions;
2927 expansions = ed;
2928 ei = new_ExpInv(EXP_IF, ed);
2929 ei->current = ed->line;
2930 ei->emitting = true;
2931 ei->prev = istk->expansion;
2932 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002933 free_tlist(origline);
2934 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002935
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002936 case PP_RMACRO:
2937 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002938 case PP_MACRO:
2939 case PP_IMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002940 if (defining != NULL) {
2941 if (defining->type == EXP_MMACRO) {
2942 defining->def_depth ++;
2943 }
2944 return NO_DIRECTIVE_FOUND;
2945 }
2946 ed = new_ExpDef(EXP_MMACRO);
2947 ed->max_depth =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002948 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002949 ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002950 if (!parse_mmacro_spec(tline, ed, pp_directives[i])) {
2951 nasm_free(ed);
2952 ed = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002953 return DIRECTIVE_FOUND;
2954 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002955 ed->def_depth = 0;
2956 ed->cur_depth = 0;
2957 ed->max_depth = (ed->max_depth + 1);
2958 ed->ignoring = false;
2959 ed->prev = defining;
2960 defining = ed;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002961
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002962 eed = (ExpDef *) hash_findix(&expdefs, ed->name);
2963 while (eed) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002964 if (!strcmp(eed->name, ed->name) &&
2965 (eed->nparam_min <= ed->nparam_max || ed->plus) &&
2966 (ed->nparam_min <= eed->nparam_max || eed->plus)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002967 error(ERR_WARNING|ERR_PASS1,
2968 "redefining multi-line macro `%s'", ed->name);
2969 return DIRECTIVE_FOUND;
2970 }
2971 eed = eed->next;
2972 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002973 free_tlist(origline);
2974 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002975
H. Peter Anvine2c80182005-01-15 22:15:51 +00002976 case PP_ENDM:
2977 case PP_ENDMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002978 if (defining != NULL) {
2979 if (defining->type == EXP_MMACRO) {
2980 if (defining->def_depth > 0) {
2981 defining->def_depth --;
2982 return NO_DIRECTIVE_FOUND;
2983 }
2984 } else {
2985 return NO_DIRECTIVE_FOUND;
2986 }
2987 }
2988 if (!(defining) || (defining->type != EXP_MMACRO)) {
2989 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2990 return DIRECTIVE_FOUND;
2991 }
2992 edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name);
2993 defining->next = *edhead;
2994 *edhead = defining;
2995 ed = defining;
2996 defining = ed->prev;
2997 ed->prev = expansions;
2998 expansions = ed;
2999 ed = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003000 free_tlist(origline);
3001 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003002
H. Peter Anvin89cee572009-07-15 09:16:54 -04003003 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003004 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3005 /*
3006 * We must search along istk->expansion until we hit a
3007 * macro invocation. Then we disable the emitting state(s)
3008 * between exitmacro and endmacro.
3009 */
3010 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3011 if(ei->type == EXP_MMACRO) {
3012 break;
3013 }
3014 }
3015
3016 if (ei != NULL) {
3017 /*
3018 * Set all invocations leading back to the macro
3019 * invocation to a non-emitting state.
3020 */
3021 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3022 eei->emitting = false;
3023 }
3024 eei->emitting = false;
3025 } else {
3026 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
3027 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05003028 free_tlist(origline);
3029 return DIRECTIVE_FOUND;
3030
H. Peter Anvina26433d2008-07-16 14:40:01 -07003031 case PP_UNMACRO:
3032 case PP_UNIMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003033 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003034 {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003035 ExpDef **ed_p;
3036 ExpDef spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003037
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003038 spec.casesense = (i == PP_UNMACRO);
3039 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
3040 return DIRECTIVE_FOUND;
3041 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003042 ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL);
3043 while (ed_p && *ed_p) {
3044 ed = *ed_p;
3045 if (ed->casesense == spec.casesense &&
3046 !mstrcmp(ed->name, spec.name, spec.casesense) &&
3047 ed->nparam_min == spec.nparam_min &&
3048 ed->nparam_max == spec.nparam_max &&
3049 ed->plus == spec.plus) {
Keith Kaniosc98a5b42010-12-18 12:17:31 -06003050 if (ed->cur_depth > 0) {
Keith Kanios21d885b2010-12-18 12:22:21 -06003051 error(ERR_NONFATAL, "`%s' ignored on active macro",
Keith Kaniosc98a5b42010-12-18 12:17:31 -06003052 pp_directives[i]);
3053 break;
3054 } else {
3055 *ed_p = ed->next;
3056 free_expdef(ed);
3057 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003058 } else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003059 ed_p = &ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003060 }
3061 }
3062 free_tlist(origline);
3063 free_tlist(spec.dlist);
3064 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003065 }
3066
H. Peter Anvine2c80182005-01-15 22:15:51 +00003067 case PP_ROTATE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003068 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003069 if (tline->next && tline->next->type == TOK_WHITESPACE)
3070 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003071 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003072 free_tlist(origline);
3073 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
3074 return DIRECTIVE_FOUND;
3075 }
3076 t = expand_smacro(tline->next);
3077 tline->next = NULL;
3078 free_tlist(origline);
3079 tline = t;
3080 tptr = &t;
3081 tokval.t_type = TOKEN_INVALID;
3082 evalresult =
3083 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3084 free_tlist(tline);
3085 if (!evalresult)
3086 return DIRECTIVE_FOUND;
3087 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003088 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003089 "trailing garbage after expression ignored");
3090 if (!is_simple(evalresult)) {
3091 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
3092 return DIRECTIVE_FOUND;
3093 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003094 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3095 if (ei->type == EXP_MMACRO) {
3096 break;
3097 }
3098 }
3099 if (ei == NULL) {
3100 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
3101 } else if (ei->nparam == 0) {
3102 error(ERR_NONFATAL,
3103 "`%%rotate' invoked within macro without parameters");
3104 } else {
3105 int rotate = ei->rotate + reloc_value(evalresult);
3106
3107 rotate %= (int)ei->nparam;
3108 if (rotate < 0)
3109 rotate += ei->nparam;
3110 ei->rotate = rotate;
3111 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003112 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003113
H. Peter Anvine2c80182005-01-15 22:15:51 +00003114 case PP_REP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003115 if (defining != NULL) {
3116 if (defining->type == EXP_REP) {
3117 defining->def_depth ++;
3118 }
3119 return NO_DIRECTIVE_FOUND;
3120 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003121 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003122 do {
3123 tline = tline->next;
3124 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003125
H. Peter Anvine2c80182005-01-15 22:15:51 +00003126 if (tok_type_(tline, TOK_ID) &&
3127 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003128 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003129 do {
3130 tline = tline->next;
3131 } while (tok_type_(tline, TOK_WHITESPACE));
3132 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003133
H. Peter Anvine2c80182005-01-15 22:15:51 +00003134 if (tline) {
3135 t = expand_smacro(tline);
3136 tptr = &t;
3137 tokval.t_type = TOKEN_INVALID;
3138 evalresult =
3139 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3140 if (!evalresult) {
3141 free_tlist(origline);
3142 return DIRECTIVE_FOUND;
3143 }
3144 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003145 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003146 "trailing garbage after expression ignored");
3147 if (!is_simple(evalresult)) {
3148 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
3149 return DIRECTIVE_FOUND;
3150 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003151 count = reloc_value(evalresult);
3152 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04003153 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003154 count = 0;
3155 } else
3156 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003157 } else {
3158 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003159 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003160 }
3161 free_tlist(origline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003162 ed = new_ExpDef(EXP_REP);
3163 ed->nolist = nolist;
3164 ed->def_depth = 0;
3165 ed->cur_depth = 1;
3166 ed->max_depth = (count - 1);
3167 ed->ignoring = false;
3168 ed->prev = defining;
3169 defining = ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003170 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003171
H. Peter Anvine2c80182005-01-15 22:15:51 +00003172 case PP_ENDREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003173 if (defining != NULL) {
3174 if (defining->type == EXP_REP) {
3175 if (defining->def_depth > 0) {
3176 defining->def_depth --;
3177 return NO_DIRECTIVE_FOUND;
3178 }
3179 } else {
3180 return NO_DIRECTIVE_FOUND;
3181 }
3182 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003183 if ((defining == NULL) || (defining->type != EXP_REP)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003184 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
3185 return DIRECTIVE_FOUND;
3186 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003187
H. Peter Anvine2c80182005-01-15 22:15:51 +00003188 /*
3189 * Now we have a "macro" defined - although it has no name
3190 * and we won't be entering it in the hash tables - we must
3191 * push a macro-end marker for it on to istk->expansion.
3192 * After that, it will take care of propagating itself (a
3193 * macro-end marker line for a macro which is really a %rep
3194 * block will cause the macro to be re-expanded, complete
3195 * with another macro-end marker to ensure the process
3196 * continues) until the whole expansion is forcibly removed
3197 * from istk->expansion by a %exitrep.
3198 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003199 ed = defining;
3200 defining = ed->prev;
3201 ed->prev = expansions;
3202 expansions = ed;
3203 ei = new_ExpInv(EXP_REP, ed);
3204 ei->current = ed->line;
3205 ei->emitting = ((ed->max_depth > 0) ? true : false);
3206 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3207 ei->prev = istk->expansion;
3208 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003209 free_tlist(origline);
3210 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003211
H. Peter Anvine2c80182005-01-15 22:15:51 +00003212 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003213 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3214 /*
3215 * We must search along istk->expansion until we hit a
3216 * rep invocation. Then we disable the emitting state(s)
3217 * between exitrep and endrep.
3218 */
3219 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3220 if (ei->type == EXP_REP) {
3221 break;
3222 }
3223 }
3224
3225 if (ei != NULL) {
3226 /*
3227 * Set all invocations leading back to the rep
3228 * invocation to a non-emitting state.
3229 */
3230 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3231 eei->emitting = false;
3232 }
3233 eei->emitting = false;
3234 eei->current = NULL;
3235 eei->def->cur_depth = eei->def->max_depth;
3236 } else {
3237 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3238 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003239 free_tlist(origline);
3240 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003241
H. Peter Anvine2c80182005-01-15 22:15:51 +00003242 case PP_XDEFINE:
3243 case PP_IXDEFINE:
3244 case PP_DEFINE:
3245 case PP_IDEFINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003246 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003247 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003248
H. Peter Anvine2c80182005-01-15 22:15:51 +00003249 tline = tline->next;
3250 skip_white_(tline);
3251 tline = expand_id(tline);
3252 if (!tline || (tline->type != TOK_ID &&
3253 (tline->type != TOK_PREPROC_ID ||
3254 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003255 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003256 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003257 free_tlist(origline);
3258 return DIRECTIVE_FOUND;
3259 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003260
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003261 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003262 last = tline;
3263 param_start = tline = tline->next;
3264 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003265
H. Peter Anvine2c80182005-01-15 22:15:51 +00003266 /* Expand the macro definition now for %xdefine and %ixdefine */
3267 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3268 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003269
H. Peter Anvine2c80182005-01-15 22:15:51 +00003270 if (tok_is_(tline, "(")) {
3271 /*
3272 * This macro has parameters.
3273 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003274
H. Peter Anvine2c80182005-01-15 22:15:51 +00003275 tline = tline->next;
3276 while (1) {
3277 skip_white_(tline);
3278 if (!tline) {
3279 error(ERR_NONFATAL, "parameter identifier expected");
3280 free_tlist(origline);
3281 return DIRECTIVE_FOUND;
3282 }
3283 if (tline->type != TOK_ID) {
3284 error(ERR_NONFATAL,
3285 "`%s': parameter identifier expected",
3286 tline->text);
3287 free_tlist(origline);
3288 return DIRECTIVE_FOUND;
3289 }
3290 tline->type = TOK_SMAC_PARAM + nparam++;
3291 tline = tline->next;
3292 skip_white_(tline);
3293 if (tok_is_(tline, ",")) {
3294 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003295 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003296 if (!tok_is_(tline, ")")) {
3297 error(ERR_NONFATAL,
3298 "`)' expected to terminate macro template");
3299 free_tlist(origline);
3300 return DIRECTIVE_FOUND;
3301 }
3302 break;
3303 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003304 }
3305 last = tline;
3306 tline = tline->next;
3307 }
3308 if (tok_type_(tline, TOK_WHITESPACE))
3309 last = tline, tline = tline->next;
3310 macro_start = NULL;
3311 last->next = NULL;
3312 t = tline;
3313 while (t) {
3314 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003315 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003316 if (tt->type >= TOK_SMAC_PARAM &&
3317 !strcmp(tt->text, t->text))
3318 t->type = tt->type;
3319 }
3320 tt = t->next;
3321 t->next = macro_start;
3322 macro_start = t;
3323 t = tt;
3324 }
3325 /*
3326 * Good. We now have a macro name, a parameter count, and a
3327 * token list (in reverse order) for an expansion. We ought
3328 * to be OK just to create an SMacro, store it, and let
3329 * free_tlist have the rest of the line (which we have
3330 * carefully re-terminated after chopping off the expansion
3331 * from the end).
3332 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003333 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003334 free_tlist(origline);
3335 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003336
H. Peter Anvine2c80182005-01-15 22:15:51 +00003337 case PP_UNDEF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003338 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003339 tline = tline->next;
3340 skip_white_(tline);
3341 tline = expand_id(tline);
3342 if (!tline || (tline->type != TOK_ID &&
3343 (tline->type != TOK_PREPROC_ID ||
3344 tline->text[1] != '$'))) {
3345 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3346 free_tlist(origline);
3347 return DIRECTIVE_FOUND;
3348 }
3349 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003350 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003351 "trailing garbage after macro name ignored");
3352 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003353
H. Peter Anvine2c80182005-01-15 22:15:51 +00003354 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003355 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003356 undef_smacro(ctx, mname);
3357 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003358 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003359
H. Peter Anvin9e200162008-06-04 17:23:14 -07003360 case PP_DEFSTR:
3361 case PP_IDEFSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003362 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003363 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003364
3365 tline = tline->next;
3366 skip_white_(tline);
3367 tline = expand_id(tline);
3368 if (!tline || (tline->type != TOK_ID &&
3369 (tline->type != TOK_PREPROC_ID ||
3370 tline->text[1] != '$'))) {
3371 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003372 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003373 free_tlist(origline);
3374 return DIRECTIVE_FOUND;
3375 }
3376
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003377 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003378 last = tline;
3379 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003380 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003381
3382 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003383 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003384
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003385 p = detoken(tline, false);
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003386 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003387 macro_start->text = nasm_quote(p, strlen(p));
3388 macro_start->type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003389 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003390
3391 /*
3392 * We now have a macro name, an implicit parameter count of
3393 * zero, and a string token to use as an expansion. Create
3394 * and store an SMacro.
3395 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003396 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003397 free_tlist(origline);
3398 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003399
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003400 case PP_DEFTOK:
3401 case PP_IDEFTOK:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003402 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003403 casesense = (i == PP_DEFTOK);
3404
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003405 tline = tline->next;
3406 skip_white_(tline);
3407 tline = expand_id(tline);
3408 if (!tline || (tline->type != TOK_ID &&
3409 (tline->type != TOK_PREPROC_ID ||
3410 tline->text[1] != '$'))) {
3411 error(ERR_NONFATAL,
3412 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003413 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003414 free_tlist(origline);
3415 return DIRECTIVE_FOUND;
3416 }
3417 ctx = get_ctx(tline->text, &mname, false);
3418 last = tline;
3419 tline = expand_smacro(tline->next);
3420 last->next = NULL;
3421
3422 t = tline;
3423 while (tok_type_(t, TOK_WHITESPACE))
3424 t = t->next;
3425 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003426 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003427 error(ERR_NONFATAL,
3428 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003429 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003430 free_tlist(tline);
3431 free_tlist(origline);
3432 return DIRECTIVE_FOUND;
3433 }
3434
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003435 /*
3436 * Convert the string to a token stream. Note that smacros
3437 * are stored with the token stream reversed, so we have to
3438 * reverse the output of tokenize().
3439 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003440 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003441 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003442
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003443 /*
3444 * We now have a macro name, an implicit parameter count of
3445 * zero, and a numeric token to use as an expansion. Create
3446 * and store an SMacro.
3447 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003448 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003449 free_tlist(tline);
3450 free_tlist(origline);
3451 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003452
H. Peter Anvin418ca702008-05-30 10:42:30 -07003453 case PP_PATHSEARCH:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003454 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003455 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003456 FILE *fp;
3457 StrList *xsl = NULL;
3458 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003459
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003460 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003461
3462 tline = tline->next;
3463 skip_white_(tline);
3464 tline = expand_id(tline);
3465 if (!tline || (tline->type != TOK_ID &&
3466 (tline->type != TOK_PREPROC_ID ||
3467 tline->text[1] != '$'))) {
3468 error(ERR_NONFATAL,
3469 "`%%pathsearch' expects a macro identifier as first parameter");
3470 free_tlist(origline);
3471 return DIRECTIVE_FOUND;
3472 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003473 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003474 last = tline;
3475 tline = expand_smacro(tline->next);
3476 last->next = NULL;
3477
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003478 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003479 while (tok_type_(t, TOK_WHITESPACE))
3480 t = t->next;
3481
3482 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003483 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003484 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003485 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003486 free_tlist(origline);
3487 return DIRECTIVE_FOUND; /* but we did _something_ */
3488 }
3489 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003490 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003491 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003492 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003493 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003494 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003495
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003496 fp = inc_fopen(p, &xsl, &xst, true);
3497 if (fp) {
3498 p = xsl->str;
3499 fclose(fp); /* Don't actually care about the file */
3500 }
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003501 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003502 macro_start->text = nasm_quote(p, strlen(p));
3503 macro_start->type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003504 if (xsl)
3505 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003506
3507 /*
3508 * We now have a macro name, an implicit parameter count of
3509 * zero, and a string token to use as an expansion. Create
3510 * and store an SMacro.
3511 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003512 define_smacro(ctx, mname, casesense, 0, macro_start);
3513 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003514 free_tlist(origline);
3515 return DIRECTIVE_FOUND;
3516 }
3517
H. Peter Anvine2c80182005-01-15 22:15:51 +00003518 case PP_STRLEN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003519 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003520 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003521
H. Peter Anvine2c80182005-01-15 22:15:51 +00003522 tline = tline->next;
3523 skip_white_(tline);
3524 tline = expand_id(tline);
3525 if (!tline || (tline->type != TOK_ID &&
3526 (tline->type != TOK_PREPROC_ID ||
3527 tline->text[1] != '$'))) {
3528 error(ERR_NONFATAL,
3529 "`%%strlen' expects a macro identifier as first parameter");
3530 free_tlist(origline);
3531 return DIRECTIVE_FOUND;
3532 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003533 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003534 last = tline;
3535 tline = expand_smacro(tline->next);
3536 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003537
H. Peter Anvine2c80182005-01-15 22:15:51 +00003538 t = tline;
3539 while (tok_type_(t, TOK_WHITESPACE))
3540 t = t->next;
3541 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003542 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003543 error(ERR_NONFATAL,
3544 "`%%strlen` requires string as second parameter");
3545 free_tlist(tline);
3546 free_tlist(origline);
3547 return DIRECTIVE_FOUND;
3548 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003549
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003550 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003551 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003552
H. Peter Anvine2c80182005-01-15 22:15:51 +00003553 /*
3554 * We now have a macro name, an implicit parameter count of
3555 * zero, and a numeric token to use as an expansion. Create
3556 * and store an SMacro.
3557 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003558 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003559 free_tlist(tline);
3560 free_tlist(origline);
3561 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003562
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003563 case PP_STRCAT:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003564 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003565 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003566
3567 tline = tline->next;
3568 skip_white_(tline);
3569 tline = expand_id(tline);
3570 if (!tline || (tline->type != TOK_ID &&
3571 (tline->type != TOK_PREPROC_ID ||
3572 tline->text[1] != '$'))) {
3573 error(ERR_NONFATAL,
3574 "`%%strcat' expects a macro identifier as first parameter");
3575 free_tlist(origline);
3576 return DIRECTIVE_FOUND;
3577 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003578 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003579 last = tline;
3580 tline = expand_smacro(tline->next);
3581 last->next = NULL;
3582
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003583 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003584 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003585 switch (t->type) {
3586 case TOK_WHITESPACE:
3587 break;
3588 case TOK_STRING:
3589 len += t->a.len = nasm_unquote(t->text, NULL);
3590 break;
3591 case TOK_OTHER:
3592 if (!strcmp(t->text, ",")) /* permit comma separators */
3593 break;
3594 /* else fall through */
3595 default:
3596 error(ERR_NONFATAL,
3597 "non-string passed to `%%strcat' (%d)", t->type);
3598 free_tlist(tline);
3599 free_tlist(origline);
3600 return DIRECTIVE_FOUND;
3601 }
3602 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003603
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003604 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003605 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003606 if (t->type == TOK_STRING) {
3607 memcpy(p, t->text, t->a.len);
3608 p += t->a.len;
3609 }
3610 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003611
3612 /*
3613 * We now have a macro name, an implicit parameter count of
3614 * zero, and a numeric token to use as an expansion. Create
3615 * and store an SMacro.
3616 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003617 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3618 macro_start->text = nasm_quote(pp, len);
3619 nasm_free(pp);
3620 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003621 free_tlist(tline);
3622 free_tlist(origline);
3623 return DIRECTIVE_FOUND;
3624
H. Peter Anvine2c80182005-01-15 22:15:51 +00003625 case PP_SUBSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003626 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003627 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003628 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003629 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003630
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003631 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003632
H. Peter Anvine2c80182005-01-15 22:15:51 +00003633 tline = tline->next;
3634 skip_white_(tline);
3635 tline = expand_id(tline);
3636 if (!tline || (tline->type != TOK_ID &&
3637 (tline->type != TOK_PREPROC_ID ||
3638 tline->text[1] != '$'))) {
3639 error(ERR_NONFATAL,
3640 "`%%substr' expects a macro identifier as first parameter");
3641 free_tlist(origline);
3642 return DIRECTIVE_FOUND;
3643 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003644 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003645 last = tline;
3646 tline = expand_smacro(tline->next);
3647 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003648
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003649 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003650 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003651 while (tok_type_(t, TOK_WHITESPACE))
3652 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003653
H. Peter Anvine2c80182005-01-15 22:15:51 +00003654 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003655 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003656 error(ERR_NONFATAL,
3657 "`%%substr` requires string as second parameter");
3658 free_tlist(tline);
3659 free_tlist(origline);
3660 return DIRECTIVE_FOUND;
3661 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003662
H. Peter Anvine2c80182005-01-15 22:15:51 +00003663 tt = t->next;
3664 tptr = &tt;
3665 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003666 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003667 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003668 if (!evalresult) {
3669 free_tlist(tline);
3670 free_tlist(origline);
3671 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003672 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003673 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3674 free_tlist(tline);
3675 free_tlist(origline);
3676 return DIRECTIVE_FOUND;
3677 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003678 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003679
3680 while (tok_type_(tt, TOK_WHITESPACE))
3681 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003682 if (!tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003683 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003684 } else {
3685 tokval.t_type = TOKEN_INVALID;
3686 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3687 pass, error, NULL);
3688 if (!evalresult) {
3689 free_tlist(tline);
3690 free_tlist(origline);
3691 return DIRECTIVE_FOUND;
3692 } else if (!is_simple(evalresult)) {
3693 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3694 free_tlist(tline);
3695 free_tlist(origline);
3696 return DIRECTIVE_FOUND;
3697 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003698 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003699 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003700
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003701 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003702 /* make start and count being in range */
3703 if (start < 0)
3704 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003705 if (count < 0)
3706 count = len + count + 1 - start;
3707 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003708 count = len - start;
3709 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003710 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003711
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003712 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003713 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003714 macro_start->type = TOK_STRING;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003715
H. Peter Anvine2c80182005-01-15 22:15:51 +00003716 /*
3717 * We now have a macro name, an implicit parameter count of
3718 * zero, and a numeric token to use as an expansion. Create
3719 * and store an SMacro.
3720 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003721 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003722 free_tlist(tline);
3723 free_tlist(origline);
3724 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003725 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003726
H. Peter Anvine2c80182005-01-15 22:15:51 +00003727 case PP_ASSIGN:
3728 case PP_IASSIGN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003729 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003730 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003731
H. Peter Anvine2c80182005-01-15 22:15:51 +00003732 tline = tline->next;
3733 skip_white_(tline);
3734 tline = expand_id(tline);
3735 if (!tline || (tline->type != TOK_ID &&
3736 (tline->type != TOK_PREPROC_ID ||
3737 tline->text[1] != '$'))) {
3738 error(ERR_NONFATAL,
3739 "`%%%sassign' expects a macro identifier",
3740 (i == PP_IASSIGN ? "i" : ""));
3741 free_tlist(origline);
3742 return DIRECTIVE_FOUND;
3743 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003744 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003745 last = tline;
3746 tline = expand_smacro(tline->next);
3747 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003748
H. Peter Anvine2c80182005-01-15 22:15:51 +00003749 t = tline;
3750 tptr = &t;
3751 tokval.t_type = TOKEN_INVALID;
3752 evalresult =
3753 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3754 free_tlist(tline);
3755 if (!evalresult) {
3756 free_tlist(origline);
3757 return DIRECTIVE_FOUND;
3758 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003759
H. Peter Anvine2c80182005-01-15 22:15:51 +00003760 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003761 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003762 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003763
H. Peter Anvine2c80182005-01-15 22:15:51 +00003764 if (!is_simple(evalresult)) {
3765 error(ERR_NONFATAL,
3766 "non-constant value given to `%%%sassign'",
3767 (i == PP_IASSIGN ? "i" : ""));
3768 free_tlist(origline);
3769 return DIRECTIVE_FOUND;
3770 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003771
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003772 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003773 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003774
H. Peter Anvine2c80182005-01-15 22:15:51 +00003775 /*
3776 * We now have a macro name, an implicit parameter count of
3777 * zero, and a numeric token to use as an expansion. Create
3778 * and store an SMacro.
3779 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003780 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003781 free_tlist(origline);
3782 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003783
H. Peter Anvine2c80182005-01-15 22:15:51 +00003784 case PP_LINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003785 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003786 /*
3787 * Syntax is `%line nnn[+mmm] [filename]'
3788 */
3789 tline = tline->next;
3790 skip_white_(tline);
3791 if (!tok_type_(tline, TOK_NUMBER)) {
3792 error(ERR_NONFATAL, "`%%line' expects line number");
3793 free_tlist(origline);
3794 return DIRECTIVE_FOUND;
3795 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003796 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003797 m = 1;
3798 tline = tline->next;
3799 if (tok_is_(tline, "+")) {
3800 tline = tline->next;
3801 if (!tok_type_(tline, TOK_NUMBER)) {
3802 error(ERR_NONFATAL, "`%%line' expects line increment");
3803 free_tlist(origline);
3804 return DIRECTIVE_FOUND;
3805 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003806 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003807 tline = tline->next;
3808 }
3809 skip_white_(tline);
3810 src_set_linnum(k);
3811 istk->lineinc = m;
3812 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003813 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003814 }
3815 free_tlist(origline);
3816 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003817
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003818 case PP_WHILE:
3819 if (defining != NULL) {
3820 if (defining->type == EXP_WHILE) {
3821 defining->def_depth ++;
3822 }
3823 return NO_DIRECTIVE_FOUND;
3824 }
3825 l = NULL;
3826 if ((istk->expansion != NULL) &&
3827 (istk->expansion->emitting == false)) {
3828 j = COND_NEVER;
3829 } else {
3830 l = new_Line();
3831 l->first = copy_Token(tline->next);
3832 j = if_condition(tline->next, i);
3833 tline->next = NULL; /* it got freed */
3834 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
3835 }
3836 ed = new_ExpDef(EXP_WHILE);
3837 ed->state = j;
3838 ed->cur_depth = 1;
3839 ed->max_depth = DEADMAN_LIMIT;
3840 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
3841 if (ed->ignoring == false) {
3842 ed->line = l;
3843 ed->last = l;
3844 } else if (l != NULL) {
3845 delete_Token(l->first);
3846 nasm_free(l);
3847 l = NULL;
3848 }
3849 ed->prev = defining;
3850 defining = ed;
3851 free_tlist(origline);
3852 return DIRECTIVE_FOUND;
3853
3854 case PP_ENDWHILE:
3855 if (defining != NULL) {
3856 if (defining->type == EXP_WHILE) {
3857 if (defining->def_depth > 0) {
3858 defining->def_depth --;
3859 return NO_DIRECTIVE_FOUND;
3860 }
3861 } else {
3862 return NO_DIRECTIVE_FOUND;
3863 }
3864 }
3865 if (tline->next != NULL) {
3866 error_precond(ERR_WARNING|ERR_PASS1,
3867 "trailing garbage after `%%endwhile' ignored");
3868 }
3869 if ((defining == NULL) || (defining->type != EXP_WHILE)) {
3870 error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'");
3871 return DIRECTIVE_FOUND;
3872 }
3873 ed = defining;
3874 defining = ed->prev;
3875 if (ed->ignoring == false) {
3876 ed->prev = expansions;
3877 expansions = ed;
3878 ei = new_ExpInv(EXP_WHILE, ed);
3879 ei->current = ed->line->next;
3880 ei->emitting = true;
3881 ei->prev = istk->expansion;
3882 istk->expansion = ei;
3883 } else {
3884 nasm_free(ed);
3885 }
3886 free_tlist(origline);
3887 return DIRECTIVE_FOUND;
3888
3889 case PP_EXITWHILE:
3890 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3891 /*
3892 * We must search along istk->expansion until we hit a
3893 * while invocation. Then we disable the emitting state(s)
3894 * between exitwhile and endwhile.
3895 */
3896 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3897 if (ei->type == EXP_WHILE) {
3898 break;
3899 }
3900 }
3901
3902 if (ei != NULL) {
3903 /*
3904 * Set all invocations leading back to the while
3905 * invocation to a non-emitting state.
3906 */
3907 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3908 eei->emitting = false;
3909 }
3910 eei->emitting = false;
3911 eei->current = NULL;
3912 eei->def->cur_depth = eei->def->max_depth;
3913 } else {
3914 error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block");
3915 }
3916 free_tlist(origline);
3917 return DIRECTIVE_FOUND;
3918
3919 case PP_COMMENT:
3920 if (defining != NULL) {
3921 if (defining->type == EXP_COMMENT) {
3922 defining->def_depth ++;
3923 }
3924 return NO_DIRECTIVE_FOUND;
3925 }
3926 ed = new_ExpDef(EXP_COMMENT);
3927 ed->ignoring = true;
3928 ed->prev = defining;
3929 defining = ed;
3930 free_tlist(origline);
3931 return DIRECTIVE_FOUND;
3932
3933 case PP_ENDCOMMENT:
3934 if (defining != NULL) {
3935 if (defining->type == EXP_COMMENT) {
3936 if (defining->def_depth > 0) {
3937 defining->def_depth --;
3938 return NO_DIRECTIVE_FOUND;
3939 }
3940 } else {
3941 return NO_DIRECTIVE_FOUND;
3942 }
3943 }
3944 if ((defining == NULL) || (defining->type != EXP_COMMENT)) {
3945 error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'");
3946 return DIRECTIVE_FOUND;
3947 }
3948 ed = defining;
3949 defining = ed->prev;
3950 nasm_free(ed);
3951 free_tlist(origline);
3952 return DIRECTIVE_FOUND;
3953
3954 case PP_FINAL:
3955 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3956 if (in_final != false) {
3957 error(ERR_FATAL, "`%%final' cannot be used recursively");
3958 }
3959 tline = tline->next;
3960 skip_white_(tline);
3961 if (tline == NULL) {
3962 error(ERR_NONFATAL, "`%%final' expects at least one parameter");
3963 } else {
3964 l = new_Line();
3965 l->first = copy_Token(tline);
3966 l->next = finals;
3967 finals = l;
3968 }
3969 free_tlist(origline);
3970 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003971
H. Peter Anvine2c80182005-01-15 22:15:51 +00003972 default:
3973 error(ERR_FATAL,
3974 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003975 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003976 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003977 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003978}
3979
3980/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003981 * Ensure that a macro parameter contains a condition code and
3982 * nothing else. Return the condition code index if so, or -1
3983 * otherwise.
3984 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003985static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003986{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003987 Token *tt;
3988 int i, j, k, m;
3989
H. Peter Anvin25a99342007-09-22 17:45:45 -07003990 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003991 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003992
H. Peter Anvineba20a72002-04-30 20:53:55 +00003993 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003994 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003995 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003996 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003997 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003998 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003999 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004000
4001 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04004002 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004003 while (j - i > 1) {
4004 k = (j + i) / 2;
4005 m = nasm_stricmp(t->text, conditions[k]);
4006 if (m == 0) {
4007 i = k;
4008 j = -2;
4009 break;
4010 } else if (m < 0) {
4011 j = k;
4012 } else
4013 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004014 }
4015 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004016 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004017 return i;
4018}
4019
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004020static bool paste_tokens(Token **head, const struct tokseq_match *m,
4021 int mnum, bool handle_paste_tokens)
H. Peter Anvind784a082009-04-20 14:01:18 -07004022{
4023 Token **tail, *t, *tt;
4024 Token **paste_head;
4025 bool did_paste = false;
4026 char *tmp;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004027 int i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004028
4029 /* Now handle token pasting... */
4030 paste_head = NULL;
4031 tail = head;
4032 while ((t = *tail) && (tt = t->next)) {
4033 switch (t->type) {
4034 case TOK_WHITESPACE:
4035 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004036 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07004037 t->next = delete_Token(tt);
4038 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004039 /* Do not advance paste_head here */
4040 tail = &t->next;
4041 }
H. Peter Anvind784a082009-04-20 14:01:18 -07004042 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004043 case TOK_PASTE: /* %+ */
4044 if (handle_paste_tokens) {
4045 /* Zap %+ and whitespace tokens to the right */
4046 while (t && (t->type == TOK_WHITESPACE ||
4047 t->type == TOK_PASTE))
4048 t = *tail = delete_Token(t);
4049 if (!paste_head || !t)
4050 break; /* Nothing to paste with */
4051 tail = paste_head;
4052 t = *tail;
4053 tt = t->next;
4054 while (tok_type_(tt, TOK_WHITESPACE))
4055 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004056 if (tt) {
4057 tmp = nasm_strcat(t->text, tt->text);
4058 delete_Token(t);
4059 tt = delete_Token(tt);
4060 t = *tail = tokenize(tmp);
4061 nasm_free(tmp);
4062 while (t->next) {
4063 tail = &t->next;
4064 t = t->next;
4065 }
4066 t->next = tt; /* Attach the remaining token chain */
4067 did_paste = true;
4068 }
4069 paste_head = tail;
4070 tail = &t->next;
4071 break;
4072 }
4073 /* else fall through */
4074 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004075 /*
4076 * Concatenation of tokens might look nontrivial
4077 * but in real it's pretty simple -- the caller
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004078 * prepares the masks of token types to be concatenated
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004079 * and we simply find matched sequences and slip
4080 * them together
4081 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004082 for (i = 0; i < mnum; i++) {
4083 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
4084 size_t len = 0;
4085 char *tmp, *p;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004086
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004087 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004088 len += strlen(tt->text);
4089 tt = tt->next;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004090 }
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004091
Cyrill Gorcunovfdd0ac52011-06-27 01:22:27 +04004092 nasm_dump_token(tt);
4093
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004094 /*
4095 * Now tt points to the first token after
4096 * the potential paste area...
4097 */
4098 if (tt != t->next) {
4099 /* We have at least two tokens... */
4100 len += strlen(t->text);
4101 p = tmp = nasm_malloc(len+1);
4102 while (t != tt) {
4103 strcpy(p, t->text);
4104 p = strchr(p, '\0');
4105 t = delete_Token(t);
4106 }
4107 t = *tail = tokenize(tmp);
4108 nasm_free(tmp);
4109 while (t->next) {
4110 tail = &t->next;
4111 t = t->next;
4112 }
4113 t->next = tt; /* Attach the remaining token chain */
4114 did_paste = true;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004115 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004116 paste_head = tail;
4117 tail = &t->next;
4118 break;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004119 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004120 }
4121 if (i >= mnum) { /* no match */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004122 tail = &t->next;
4123 if (!tok_type_(t->next, TOK_WHITESPACE))
4124 paste_head = tail;
4125 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004126 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004127 }
4128 }
4129 return did_paste;
4130}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004131
4132/*
4133 * expands to a list of tokens from %{x:y}
4134 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004135static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004136{
4137 Token *t = tline, **tt, *tm, *head;
4138 char *pos;
4139 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004140
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004141 pos = strchr(tline->text, ':');
4142 nasm_assert(pos);
4143
4144 lst = atoi(pos + 1);
4145 fst = atoi(tline->text + 1);
4146
4147 /*
4148 * only macros params are accounted so
4149 * if someone passes %0 -- we reject such
4150 * value(s)
4151 */
4152 if (lst == 0 || fst == 0)
4153 goto err;
4154
4155 /* the values should be sane */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004156 if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) ||
4157 (lst > (int)ei->nparam || lst < (-(int)ei->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004158 goto err;
4159
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004160 fst = fst < 0 ? fst + (int)ei->nparam + 1: fst;
4161 lst = lst < 0 ? lst + (int)ei->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004162
4163 /* counted from zero */
4164 fst--, lst--;
4165
4166 /*
4167 * it will be at least one token
4168 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004169 tm = ei->params[(fst + ei->rotate) % ei->nparam];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004170 t = new_Token(NULL, tm->type, tm->text, 0);
4171 head = t, tt = &t->next;
4172 if (fst < lst) {
4173 for (i = fst + 1; i <= lst; i++) {
4174 t = new_Token(NULL, TOK_OTHER, ",", 0);
4175 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004176 j = (i + ei->rotate) % ei->nparam;
4177 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004178 t = new_Token(NULL, tm->type, tm->text, 0);
4179 *tt = t, tt = &t->next;
4180 }
4181 } else {
4182 for (i = fst - 1; i >= lst; i--) {
4183 t = new_Token(NULL, TOK_OTHER, ",", 0);
4184 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004185 j = (i + ei->rotate) % ei->nparam;
4186 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004187 t = new_Token(NULL, tm->type, tm->text, 0);
4188 *tt = t, tt = &t->next;
4189 }
4190 }
4191
4192 *last = tt;
4193 return head;
4194
4195err:
4196 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
4197 &tline->text[1]);
4198 return tline;
4199}
4200
H. Peter Anvin76690a12002-04-30 20:52:49 +00004201/*
4202 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004203 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004204 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004205 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004206static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004207{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004208 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004209 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004210 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004211
4212 tail = &thead;
4213 thead = NULL;
4214
Cyrill Gorcunov2e046002011-06-26 23:33:56 +04004215 nasm_dump_stream(tline);
4216
H. Peter Anvine2c80182005-01-15 22:15:51 +00004217 while (tline) {
4218 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004219 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4220 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4221 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004222 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004223 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004224 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004225 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004226 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004227 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004228
H. Peter Anvine2c80182005-01-15 22:15:51 +00004229 t = tline;
4230 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004231
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004232 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
4233 if (ei->type == EXP_MMACRO) {
4234 break;
4235 }
4236 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004237 if (ei == NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004238 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004239 } else {
4240 pos = strchr(t->text, ':');
4241 if (!pos) {
4242 switch (t->text[1]) {
4243 /*
4244 * We have to make a substitution of one of the
4245 * forms %1, %-1, %+1, %%foo, %0.
4246 */
4247 case '0':
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004248 if ((strlen(t->text) > 2) && (t->text[2] == '0')) {
4249 type = TOK_ID;
4250 text = nasm_strdup(ei->label_text);
4251 } else {
4252 type = TOK_NUMBER;
4253 snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam);
4254 text = nasm_strdup(tmpbuf);
4255 }
4256 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004257 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004258 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004259 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004260 ei->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004261 text = nasm_strcat(tmpbuf, t->text + 2);
4262 break;
4263 case '-':
4264 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004265 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004266 tt = NULL;
4267 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004268 if (ei->nparam > 1)
4269 n = (n + ei->rotate) % ei->nparam;
4270 tt = ei->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004271 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004272 cc = find_cc(tt);
4273 if (cc == -1) {
4274 error(ERR_NONFATAL,
4275 "macro parameter %d is not a condition code",
4276 n + 1);
4277 text = NULL;
4278 } else {
4279 type = TOK_ID;
4280 if (inverse_ccs[cc] == -1) {
4281 error(ERR_NONFATAL,
4282 "condition code `%s' is not invertible",
4283 conditions[cc]);
4284 text = NULL;
4285 } else
4286 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4287 }
4288 break;
4289 case '+':
4290 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004291 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004292 tt = NULL;
4293 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004294 if (ei->nparam > 1)
4295 n = (n + ei->rotate) % ei->nparam;
4296 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004297 }
4298 cc = find_cc(tt);
4299 if (cc == -1) {
4300 error(ERR_NONFATAL,
4301 "macro parameter %d is not a condition code",
4302 n + 1);
4303 text = NULL;
4304 } else {
4305 type = TOK_ID;
4306 text = nasm_strdup(conditions[cc]);
4307 }
4308 break;
4309 default:
4310 n = atoi(t->text + 1) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004311 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004312 tt = NULL;
4313 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004314 if (ei->nparam > 1)
4315 n = (n + ei->rotate) % ei->nparam;
4316 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004317 }
4318 if (tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004319 for (i = 0; i < ei->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004320 *tail = new_Token(NULL, tt->type, tt->text, 0);
4321 tail = &(*tail)->next;
4322 tt = tt->next;
4323 }
4324 }
4325 text = NULL; /* we've done it here */
4326 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004327 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004328 } else {
4329 /*
4330 * seems we have a parameters range here
4331 */
4332 Token *head, **last;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004333 head = expand_mmac_params_range(ei, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004334 if (head != t) {
4335 *tail = head;
4336 *last = tline;
4337 tline = head;
4338 text = NULL;
4339 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004340 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004341 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004342 if (!text) {
4343 delete_Token(t);
4344 } else {
4345 *tail = t;
4346 tail = &t->next;
4347 t->type = type;
4348 nasm_free(t->text);
4349 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004350 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004351 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004352 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004353 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004354 } else if (tline->type == TOK_INDIRECT) {
4355 t = tline;
4356 tline = tline->next;
4357 tt = tokenize(t->text);
4358 tt = expand_mmac_params(tt);
4359 tt = expand_smacro(tt);
4360 *tail = tt;
4361 while (tt) {
4362 tt->a.mac = NULL; /* Necessary? */
4363 tail = &tt->next;
4364 tt = tt->next;
4365 }
4366 delete_Token(t);
4367 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004368 } else {
4369 t = *tail = tline;
4370 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004371 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004372 tail = &t->next;
4373 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004374 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004375 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004376
Cyrill Gorcunovcb00cd12011-06-13 21:25:10 +04004377 if (changed)
4378 paste_tokens(&thead, pp_concat_match,
4379 ARRAY_SIZE(pp_concat_match),
4380 false);
H. Peter Anvin6125b622009-04-08 14:02:25 -07004381
Cyrill Gorcunov2e046002011-06-26 23:33:56 +04004382 nasm_dump_token(thead);
4383
H. Peter Anvin76690a12002-04-30 20:52:49 +00004384 return thead;
4385}
4386
4387/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004388 * Expand all single-line macro calls made in the given line.
4389 * Return the expanded version of the line. The original is deemed
4390 * to be destroyed in the process. (In reality we'll just move
4391 * Tokens from input to output a lot of the time, rather than
4392 * actually bothering to destroy and replicate.)
4393 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004394
H. Peter Anvine2c80182005-01-15 22:15:51 +00004395static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004396{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004397 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004398 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004399 Token **params;
4400 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004401 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004402 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004403 Token *org_tline = tline;
4404 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004405 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004406 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004407 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004408
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004409 /*
4410 * Trick: we should avoid changing the start token pointer since it can
4411 * be contained in "next" field of other token. Because of this
4412 * we allocate a copy of first token and work with it; at the end of
4413 * routine we copy it back
4414 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004415 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004416 tline = new_Token(org_tline->next, org_tline->type,
4417 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004418 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004419 nasm_free(org_tline->text);
4420 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004421 }
4422
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004423 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004424
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004425again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004426 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004427 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004428
H. Peter Anvine2c80182005-01-15 22:15:51 +00004429 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004430 if (!--deadman) {
4431 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004432 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004433 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004434
H. Peter Anvine2c80182005-01-15 22:15:51 +00004435 if ((mname = tline->text)) {
4436 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004437 if (tline->type == TOK_ID) {
4438 head = (SMacro *)hash_findix(&smacros, mname);
4439 } else if (tline->type == TOK_PREPROC_ID) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004440 ctx = get_ctx(mname, &mname, false);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004441 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4442 } else
4443 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004444
H. Peter Anvine2c80182005-01-15 22:15:51 +00004445 /*
4446 * We've hit an identifier. As in is_mmacro below, we first
4447 * check whether the identifier is a single-line macro at
4448 * all, then think about checking for parameters if
4449 * necessary.
4450 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004451 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004452 if (!mstrcmp(m->name, mname, m->casesense))
4453 break;
4454 if (m) {
4455 mstart = tline;
4456 params = NULL;
4457 paramsize = NULL;
4458 if (m->nparam == 0) {
4459 /*
4460 * Simple case: the macro is parameterless. Discard the
4461 * one token that the macro call took, and push the
4462 * expansion back on the to-do stack.
4463 */
4464 if (!m->expansion) {
4465 if (!strcmp("__FILE__", m->name)) {
4466 int32_t num = 0;
4467 char *file = NULL;
4468 src_get(&num, &file);
4469 tline->text = nasm_quote(file, strlen(file));
4470 tline->type = TOK_STRING;
4471 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004472 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004473 }
4474 if (!strcmp("__LINE__", m->name)) {
4475 nasm_free(tline->text);
4476 make_tok_num(tline, src_get_linnum());
4477 continue;
4478 }
4479 if (!strcmp("__BITS__", m->name)) {
4480 nasm_free(tline->text);
4481 make_tok_num(tline, globalbits);
4482 continue;
4483 }
4484 tline = delete_Token(tline);
4485 continue;
4486 }
4487 } else {
4488 /*
4489 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004490 * exists and takes parameters. We must find the
4491 * parameters in the call, count them, find the SMacro
4492 * that corresponds to that form of the macro call, and
4493 * substitute for the parameters when we expand. What a
4494 * pain.
4495 */
4496 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004497 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004498 do {
4499 t = tline->next;
4500 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004501 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004502 t->text = NULL;
4503 t = tline->next = delete_Token(t);
4504 }
4505 tline = t;
4506 } while (tok_type_(tline, TOK_WHITESPACE));
4507 if (!tok_is_(tline, "(")) {
4508 /*
4509 * This macro wasn't called with parameters: ignore
4510 * the call. (Behaviour borrowed from gnu cpp.)
4511 */
4512 tline = mstart;
4513 m = NULL;
4514 } else {
4515 int paren = 0;
4516 int white = 0;
4517 brackets = 0;
4518 nparam = 0;
4519 sparam = PARAM_DELTA;
4520 params = nasm_malloc(sparam * sizeof(Token *));
4521 params[0] = tline->next;
4522 paramsize = nasm_malloc(sparam * sizeof(int));
4523 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004524 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004525 /*
4526 * For some unusual expansions
4527 * which concatenates function call
4528 */
4529 t = tline->next;
4530 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004531 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004532 t->text = NULL;
4533 t = tline->next = delete_Token(t);
4534 }
4535 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004536
H. Peter Anvine2c80182005-01-15 22:15:51 +00004537 if (!tline) {
4538 error(ERR_NONFATAL,
4539 "macro call expects terminating `)'");
4540 break;
4541 }
4542 if (tline->type == TOK_WHITESPACE
4543 && brackets <= 0) {
4544 if (paramsize[nparam])
4545 white++;
4546 else
4547 params[nparam] = tline->next;
4548 continue; /* parameter loop */
4549 }
4550 if (tline->type == TOK_OTHER
4551 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004552 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004553 if (ch == ',' && !paren && brackets <= 0) {
4554 if (++nparam >= sparam) {
4555 sparam += PARAM_DELTA;
4556 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004557 sparam * sizeof(Token *));
4558 paramsize = nasm_realloc(paramsize,
4559 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004560 }
4561 params[nparam] = tline->next;
4562 paramsize[nparam] = 0;
4563 white = 0;
4564 continue; /* parameter loop */
4565 }
4566 if (ch == '{' &&
4567 (brackets > 0 || (brackets == 0 &&
4568 !paramsize[nparam])))
4569 {
4570 if (!(brackets++)) {
4571 params[nparam] = tline->next;
4572 continue; /* parameter loop */
4573 }
4574 }
4575 if (ch == '}' && brackets > 0)
4576 if (--brackets == 0) {
4577 brackets = -1;
4578 continue; /* parameter loop */
4579 }
4580 if (ch == '(' && !brackets)
4581 paren++;
4582 if (ch == ')' && brackets <= 0)
4583 if (--paren < 0)
4584 break;
4585 }
4586 if (brackets < 0) {
4587 brackets = 0;
4588 error(ERR_NONFATAL, "braces do not "
4589 "enclose all of macro parameter");
4590 }
4591 paramsize[nparam] += white + 1;
4592 white = 0;
4593 } /* parameter loop */
4594 nparam++;
4595 while (m && (m->nparam != nparam ||
4596 mstrcmp(m->name, mname,
4597 m->casesense)))
4598 m = m->next;
4599 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004600 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004601 "macro `%s' exists, "
4602 "but not taking %d parameters",
4603 mstart->text, nparam);
4604 }
4605 }
4606 if (m && m->in_progress)
4607 m = NULL;
4608 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004609 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004610 * Design question: should we handle !tline, which
4611 * indicates missing ')' here, or expand those
4612 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004613 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004614 */
4615 nasm_free(params);
4616 nasm_free(paramsize);
4617 tline = mstart;
4618 } else {
4619 /*
4620 * Expand the macro: we are placed on the last token of the
4621 * call, so that we can easily split the call from the
4622 * following tokens. We also start by pushing an SMAC_END
4623 * token for the cycle removal.
4624 */
4625 t = tline;
4626 if (t) {
4627 tline = t->next;
4628 t->next = NULL;
4629 }
4630 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004631 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004632 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004633 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004634 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004635 if (t->type >= TOK_SMAC_PARAM) {
4636 Token *pcopy = tline, **ptail = &pcopy;
4637 Token *ttt, *pt;
4638 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004639
H. Peter Anvine2c80182005-01-15 22:15:51 +00004640 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004641 i = paramsize[t->type - TOK_SMAC_PARAM];
4642 while (--i >= 0) {
4643 pt = *ptail = new_Token(tline, ttt->type,
4644 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004645 ptail = &pt->next;
4646 ttt = ttt->next;
4647 }
4648 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004649 } else if (t->type == TOK_PREPROC_Q) {
4650 tt = new_Token(tline, TOK_ID, mname, 0);
4651 tline = tt;
4652 } else if (t->type == TOK_PREPROC_QQ) {
4653 tt = new_Token(tline, TOK_ID, m->name, 0);
4654 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004655 } else {
4656 tt = new_Token(tline, t->type, t->text, 0);
4657 tline = tt;
4658 }
4659 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004660
H. Peter Anvine2c80182005-01-15 22:15:51 +00004661 /*
4662 * Having done that, get rid of the macro call, and clean
4663 * up the parameters.
4664 */
4665 nasm_free(params);
4666 nasm_free(paramsize);
4667 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004668 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004669 continue; /* main token loop */
4670 }
4671 }
4672 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004673
H. Peter Anvine2c80182005-01-15 22:15:51 +00004674 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004675 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004676 tline = delete_Token(tline);
4677 } else {
4678 t = *tail = tline;
4679 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004680 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004681 t->next = NULL;
4682 tail = &t->next;
4683 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004684 }
4685
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004686 /*
4687 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004688 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004689 * TOK_IDs should be concatenated.
4690 * Also we look for %+ tokens and concatenate the tokens before and after
4691 * them (without white spaces in between).
4692 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004693 if (expanded) {
Cyrill Gorcunovcb00cd12011-06-13 21:25:10 +04004694 if (paste_tokens(&thead, pp_concat_match,
4695 ARRAY_SIZE(pp_concat_match),
4696 true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004697 /*
4698 * If we concatenated something, *and* we had previously expanded
4699 * an actual macro, scan the lines again for macros...
4700 */
4701 tline = thead;
4702 expanded = false;
4703 goto again;
4704 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004705 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004706
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004707err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004708 if (org_tline) {
4709 if (thead) {
4710 *org_tline = *thead;
4711 /* since we just gave text to org_line, don't free it */
4712 thead->text = NULL;
4713 delete_Token(thead);
4714 } else {
4715 /* the expression expanded to empty line;
4716 we can't return NULL for some reasons
4717 we just set the line to a single WHITESPACE token. */
4718 memset(org_tline, 0, sizeof(*org_tline));
4719 org_tline->text = NULL;
4720 org_tline->type = TOK_WHITESPACE;
4721 }
4722 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004723 }
4724
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004725 return thead;
4726}
4727
4728/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004729 * Similar to expand_smacro but used exclusively with macro identifiers
4730 * right before they are fetched in. The reason is that there can be
4731 * identifiers consisting of several subparts. We consider that if there
4732 * are more than one element forming the name, user wants a expansion,
4733 * otherwise it will be left as-is. Example:
4734 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004735 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004736 *
4737 * the identifier %$abc will be left as-is so that the handler for %define
4738 * will suck it and define the corresponding value. Other case:
4739 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004740 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004741 *
4742 * In this case user wants name to be expanded *before* %define starts
4743 * working, so we'll expand %$abc into something (if it has a value;
4744 * otherwise it will be left as-is) then concatenate all successive
4745 * PP_IDs into one.
4746 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004747static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004748{
4749 Token *cur, *oldnext = NULL;
4750
H. Peter Anvin734b1882002-04-30 21:01:08 +00004751 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004752 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004753
4754 cur = tline;
4755 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004756 (cur->next->type == TOK_ID ||
4757 cur->next->type == TOK_PREPROC_ID
4758 || cur->next->type == TOK_NUMBER))
4759 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004760
4761 /* If identifier consists of just one token, don't expand */
4762 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004763 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004764
H. Peter Anvine2c80182005-01-15 22:15:51 +00004765 if (cur) {
4766 oldnext = cur->next; /* Detach the tail past identifier */
4767 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004768 }
4769
H. Peter Anvin734b1882002-04-30 21:01:08 +00004770 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004771
H. Peter Anvine2c80182005-01-15 22:15:51 +00004772 if (cur) {
4773 /* expand_smacro possibly changhed tline; re-scan for EOL */
4774 cur = tline;
4775 while (cur && cur->next)
4776 cur = cur->next;
4777 if (cur)
4778 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004779 }
4780
4781 return tline;
4782}
4783
4784/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004785 * Determine whether the given line constitutes a multi-line macro
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004786 * call, and return the ExpDef structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004787 * to check for an initial label - that's taken care of in
4788 * expand_mmacro - but must check numbers of parameters. Guaranteed
4789 * to be called with tline->type == TOK_ID, so the putative macro
4790 * name is easy to find.
4791 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004792static ExpDef *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004793{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004794 ExpDef *head, *ed;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004795 Token **params;
4796 int nparam;
4797
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004798 head = (ExpDef *) hash_findix(&expdefs, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004799
4800 /*
4801 * Efficiency: first we see if any macro exists with the given
4802 * name. If not, we can return NULL immediately. _Then_ we
4803 * count the parameters, and then we look further along the
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004804 * list if necessary to find the proper ExpDef.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004805 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004806 list_for_each(ed, head)
4807 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004808 break;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004809 if (!ed)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004810 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004811
4812 /*
4813 * OK, we have a potential macro. Count and demarcate the
4814 * parameters.
4815 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004816 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004817
4818 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004819 * So we know how many parameters we've got. Find the ExpDef
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004820 * structure that handles this number.
4821 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004822 while (ed) {
4823 if (ed->nparam_min <= nparam
4824 && (ed->plus || nparam <= ed->nparam_max)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004825 /*
4826 * It's right, and we can use it. Add its default
4827 * parameters to the end of our list if necessary.
4828 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004829 if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004830 params =
4831 nasm_realloc(params,
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004832 ((ed->nparam_min + ed->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004833 1) * sizeof(*params)));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004834 while (nparam < ed->nparam_min + ed->ndefs) {
4835 params[nparam] = ed->defaults[nparam - ed->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004836 nparam++;
4837 }
4838 }
4839 /*
4840 * If we've gone over the maximum parameter count (and
4841 * we're in Plus mode), ignore parameters beyond
4842 * nparam_max.
4843 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004844 if (ed->plus && nparam > ed->nparam_max)
4845 nparam = ed->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004846 /*
4847 * Then terminate the parameter list, and leave.
4848 */
4849 if (!params) { /* need this special case */
4850 params = nasm_malloc(sizeof(*params));
4851 nparam = 0;
4852 }
4853 params[nparam] = NULL;
4854 *params_array = params;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004855 return ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004856 }
4857 /*
4858 * This one wasn't right: look for the next one with the
4859 * same name.
4860 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004861 list_for_each(ed, ed->next)
4862 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004863 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004864 }
4865
4866 /*
4867 * After all that, we didn't find one with the right number of
4868 * parameters. Issue a warning, and fail to expand the macro.
4869 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004870 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004871 "macro `%s' exists, but not taking %d parameters",
4872 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004873 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004874 return NULL;
4875}
4876
4877/*
4878 * Expand the multi-line macro call made by the given line, if
4879 * there is one to be expanded. If there is, push the expansion on
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004880 * istk->expansion and return true. Otherwise return false.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004881 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004882static bool expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004883{
H. Peter Anvineba20a72002-04-30 20:53:55 +00004884 Token *label = NULL;
4885 int dont_prepend = 0;
Cyrill Gorcunovfb27fc22011-06-25 12:08:30 +04004886 Token **params, *t;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004887 Line *l = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004888 ExpDef *ed;
4889 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004890 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004891 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004892
4893 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004894 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004895 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004896 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004897 return false;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004898 ed = is_mmacro(t, &params);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004899 if (ed != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004900 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004901 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004902 Token *last;
4903 /*
4904 * We have an id which isn't a macro call. We'll assume
4905 * it might be a label; we'll also check to see if a
4906 * colon follows it. Then, if there's another id after
4907 * that lot, we'll check it again for macro-hood.
4908 */
4909 label = last = t;
4910 t = t->next;
4911 if (tok_type_(t, TOK_WHITESPACE))
4912 last = t, t = t->next;
4913 if (tok_is_(t, ":")) {
4914 dont_prepend = 1;
4915 last = t, t = t->next;
4916 if (tok_type_(t, TOK_WHITESPACE))
4917 last = t, t = t->next;
4918 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004919 if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, &params)))
4920 return false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004921 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004922 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004923 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004924 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004925
4926 /*
4927 * Fix up the parameters: this involves stripping leading and
4928 * trailing whitespace, then stripping braces if they are
4929 * present.
4930 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004931 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004932 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004933
H. Peter Anvine2c80182005-01-15 22:15:51 +00004934 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004935 int brace = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004936 int comma = (!ed->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004937
H. Peter Anvine2c80182005-01-15 22:15:51 +00004938 t = params[i];
4939 skip_white_(t);
4940 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004941 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004942 params[i] = t;
4943 paramlen[i] = 0;
4944 while (t) {
4945 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4946 break; /* ... because we have hit a comma */
4947 if (comma && t->type == TOK_WHITESPACE
4948 && tok_is_(t->next, ","))
4949 break; /* ... or a space then a comma */
4950 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4951 break; /* ... or a brace */
4952 t = t->next;
4953 paramlen[i]++;
4954 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004955 }
4956
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004957 if (ed->cur_depth >= ed->max_depth) {
4958 if (ed->max_depth > 1) {
4959 error(ERR_WARNING,
4960 "reached maximum macro recursion depth of %i for %s",
4961 ed->max_depth,ed->name);
4962 }
4963 return false;
4964 } else {
4965 ed->cur_depth ++;
4966 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004967
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004968 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004969 * OK, we have found a ExpDef structure representing a
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004970 * previously defined mmacro. Create an expansion invocation
4971 * and point it back to the expansion definition. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004972 * parameter tokens and macro-local tokens doesn't get done
4973 * until the single-line macro substitution process; this is
4974 * because delaying them allows us to change the semantics
4975 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004976 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004977 ei = new_ExpInv(EXP_MMACRO, ed);
4978 ei->name = nasm_strdup(mname);
4979 //ei->label = label;
4980 //ei->label_text = detoken(label, false);
4981 ei->current = ed->line;
4982 ei->emitting = true;
4983 //ei->iline = tline;
4984 ei->params = params;
4985 ei->nparam = nparam;
4986 ei->rotate = 0;
4987 ei->paramlen = paramlen;
4988 ei->lineno = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004989
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004990 ei->prev = istk->expansion;
4991 istk->expansion = ei;
4992
4993 /*
4994 * Special case: detect %00 on first invocation; if found,
4995 * avoid emitting any labels that precede the mmacro call.
4996 * ed->prepend is set to -1 when %00 is detected, else 1.
4997 */
4998 if (ed->prepend == 0) {
4999 for (l = ed->line; l != NULL; l = l->next) {
5000 for (t = l->first; t != NULL; t = t->next) {
5001 if ((t->type == TOK_PREPROC_ID) &&
5002 (strlen(t->text) == 3) &&
5003 (t->text[1] == '0') && (t->text[2] == '0')) {
5004 dont_prepend = -1;
5005 break;
5006 }
5007 }
5008 if (dont_prepend < 0) {
5009 break;
5010 }
5011 }
5012 ed->prepend = ((dont_prepend < 0) ? -1 : 1);
5013 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005014
5015 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00005016 * If we had a label, push it on as the first line of
5017 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005018 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005019 if (label != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005020 if (ed->prepend < 0) {
5021 ei->label_text = detoken(label, false);
5022 } else {
5023 if (dont_prepend == 0) {
5024 t = label;
5025 while (t->next != NULL) {
5026 t = t->next;
5027 }
5028 t->next = new_Token(NULL, TOK_OTHER, ":", 0);
5029 }
5030 l = new_Line();
5031 l->first = copy_Token(label);
5032 l->next = ei->current;
5033 ei->current = l;
5034 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005035 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005036
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005037 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005038
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005039 istk->mmac_depth++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005040 return true;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005041}
5042
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005043/* The function that actually does the error reporting */
5044static void verror(int severity, const char *fmt, va_list arg)
5045{
5046 char buff[1024];
5047
5048 vsnprintf(buff, sizeof(buff), fmt, arg);
5049
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005050 if (istk && istk->mmac_depth > 0) {
5051 ExpInv *ei = istk->expansion;
5052 int lineno = ei->lineno;
5053 while (ei) {
5054 if (ei->type == EXP_MMACRO)
5055 break;
5056 lineno += ei->relno;
5057 ei = ei->prev;
5058 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005059 nasm_error(severity, "(%s:%d) %s", ei->def->name,
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005060 lineno, buff);
5061 } else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005062 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005063}
5064
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005065/*
5066 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07005067 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005068 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005069static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005070{
5071 va_list arg;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005072 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005073 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00005074 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005075}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005076
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005077/*
5078 * Because %else etc are evaluated in the state context
5079 * of the previous branch, errors might get lost with error():
5080 * %if 0 ... %else trailing garbage ... %endif
5081 * So %else etc should report errors with this function.
5082 */
5083static void error_precond(int severity, const char *fmt, ...)
5084{
5085 va_list arg;
5086
5087 /* Only ignore the error if it's really in a dead branch */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005088 if ((istk != NULL) &&
5089 (istk->expansion != NULL) &&
5090 (istk->expansion->type == EXP_IF) &&
5091 (istk->expansion->def->state == COND_NEVER))
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005092 return;
5093
5094 va_start(arg, fmt);
5095 verror(severity, fmt, arg);
5096 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005097}
5098
H. Peter Anvin734b1882002-04-30 21:01:08 +00005099static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005100pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005101{
H. Peter Anvin7383b402008-09-24 10:20:40 -07005102 Token *t;
5103
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005104 cstk = NULL;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005105 istk = nasm_zalloc(sizeof(Include));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005106 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00005107 src_set_fname(nasm_strdup(file));
5108 src_set_linnum(0);
5109 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005110 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07005111 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00005112 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005113 defining = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005114 finals = NULL;
5115 in_final = false;
Charles Crayned4200be2008-07-12 16:42:33 -07005116 nested_mac_count = 0;
5117 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005118 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005119 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005120 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005121 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005122 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005123 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005124 }
H. Peter Anvind2456592008-06-19 15:04:18 -07005125 any_extrastdmac = extrastdmac && *extrastdmac;
5126 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005127 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005128
5129 /*
5130 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5131 * The caller, however, will also pass in 3 for preprocess-only so
5132 * we can set __PASS__ accordingly.
5133 */
5134 pass = apass > 2 ? 2 : apass;
5135
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005136 dephead = deptail = deplist;
5137 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005138 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
5139 sl->next = NULL;
5140 strcpy(sl->str, file);
5141 *deptail = sl;
5142 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005143 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07005144
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005145 /*
5146 * Define the __PASS__ macro. This is defined here unlike
5147 * all the other builtins, because it is special -- it varies between
5148 * passes.
5149 */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005150 t = nasm_zalloc(sizeof(*t));
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005151 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07005152 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005153}
5154
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005155static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005156{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005157 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005158 Token *tline;
Keith Kanios9858cec2010-11-08 00:58:02 -06005159 ExpDef *ed;
5160 ExpInv *ei;
5161 Line *l;
5162 int j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005163
H. Peter Anvine2c80182005-01-15 22:15:51 +00005164 while (1) {
5165 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005166 * Fetch a tokenized line, either from the expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005167 * buffer or from the input file.
5168 */
5169 tline = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005170
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005171 while (1) { /* until we get a line we can use */
5172 /*
5173 * Fetch a tokenized line from the expansion buffer
5174 */
5175 if (istk->expansion != NULL) {
5176 ei = istk->expansion;
5177 if (ei->current != NULL) {
5178 if (ei->emitting == false) {
5179 ei->current = NULL;
5180 continue;
5181 }
5182 l = ei->current;
5183 ei->current = l->next;
5184 ei->lineno++;
5185 tline = copy_Token(l->first);
5186 if (((ei->type == EXP_REP) ||
5187 (ei->type == EXP_MMACRO) ||
5188 (ei->type == EXP_WHILE))
5189 && (ei->def->nolist == false)) {
5190 char *p = detoken(tline, false);
5191 list->line(LIST_MACRO, p);
5192 nasm_free(p);
5193 }
5194 if (ei->linnum > -1) {
5195 src_set_linnum(src_get_linnum() + 1);
5196 }
5197 break;
5198 } else if ((ei->type == EXP_REP) &&
5199 (ei->def->cur_depth < ei->def->max_depth)) {
5200 ei->def->cur_depth ++;
5201 ei->current = ei->def->line;
5202 ei->lineno = 0;
5203 continue;
5204 } else if ((ei->type == EXP_WHILE) &&
5205 (ei->def->cur_depth < ei->def->max_depth)) {
5206 ei->current = ei->def->line;
5207 ei->lineno = 0;
5208 tline = copy_Token(ei->current->first);
5209 j = if_condition(tline, PP_WHILE);
5210 tline = NULL;
5211 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
5212 if (j == COND_IF_TRUE) {
5213 ei->current = ei->current->next;
5214 ei->def->cur_depth ++;
5215 } else {
5216 ei->emitting = false;
5217 ei->current = NULL;
5218 ei->def->cur_depth = ei->def->max_depth;
5219 }
5220 continue;
5221 } else {
5222 istk->expansion = ei->prev;
5223 ed = ei->def;
5224 if (ed != NULL) {
5225 if ((ei->emitting == true) &&
5226 (ed->max_depth == DEADMAN_LIMIT) &&
5227 (ed->cur_depth == DEADMAN_LIMIT)
5228 ) {
5229 error(ERR_FATAL, "runaway expansion detected, aborting");
5230 }
5231 if (ed->cur_depth > 0) {
5232 ed->cur_depth --;
Keith Kanios94124652010-12-18 11:47:28 -06005233 } else if (ed->type != EXP_MMACRO) {
Keith Kanios6a7c3e92010-12-18 11:49:53 -06005234 expansions = ed->prev;
5235 free_expdef(ed);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005236 }
5237 if ((ei->type == EXP_REP) ||
5238 (ei->type == EXP_MMACRO) ||
5239 (ei->type == EXP_WHILE)) {
5240 list->downlevel(LIST_MACRO);
5241 if (ei->type == EXP_MMACRO) {
5242 istk->mmac_depth--;
5243 }
5244 }
5245 }
5246 if (ei->linnum > -1) {
5247 src_set_linnum(ei->linnum);
5248 }
5249 free_expinv(ei);
5250 continue;
5251 }
5252 }
5253
5254 /*
5255 * Read in line from input and tokenize
5256 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005257 line = read_line();
5258 if (line) { /* from the current input file */
5259 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005260 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005261 nasm_free(line);
5262 break;
5263 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005264
H. Peter Anvine2c80182005-01-15 22:15:51 +00005265 /*
5266 * The current file has ended; work down the istk
5267 */
5268 {
5269 Include *i = istk;
5270 fclose(i->fp);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005271 if (i->expansion != NULL) {
5272 error(ERR_FATAL,
5273 "end of file while still in an expansion");
5274 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005275 /* only set line and file name if there's a next node */
5276 if (i->next) {
5277 src_set_linnum(i->lineno);
Cyrill Gorcunovd34a1082011-03-07 11:23:08 +03005278 nasm_free(src_set_fname(nasm_strdup(i->fname)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005279 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005280 if ((i->next == NULL) && (finals != NULL)) {
5281 in_final = true;
5282 ei = new_ExpInv(EXP_FINAL, NULL);
5283 ei->emitting = true;
5284 ei->current = finals;
5285 istk->expansion = ei;
5286 finals = NULL;
5287 continue;
5288 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005289 istk = i->next;
5290 list->downlevel(LIST_INCLUDE);
5291 nasm_free(i);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005292 if (istk == NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005293 if (finals != NULL) {
5294 in_final = true;
5295 } else {
5296 return NULL;
5297 }
5298 }
5299 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005300 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005301 }
5302
5303 if (defining == NULL) {
5304 tline = expand_mmac_params(tline);
5305 }
5306
H. Peter Anvine2c80182005-01-15 22:15:51 +00005307 /*
5308 * Check the line to see if it's a preprocessor directive.
5309 */
5310 if (do_directive(tline) == DIRECTIVE_FOUND) {
5311 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005312 } else if (defining != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005313 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005314 * We're defining an expansion. We emit nothing at all,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005315 * and just shove the tokenized line on to the definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005316 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005317 if (defining->ignoring == false) {
5318 Line *l = new_Line();
5319 l->first = tline;
5320 if (defining->line == NULL) {
5321 defining->line = l;
5322 defining->last = l;
5323 } else {
5324 defining->last->next = l;
5325 defining->last = l;
5326 }
5327 } else {
Keith Kanios104803d2010-12-18 11:05:46 -06005328 free_tlist(tline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005329 }
5330 defining->linecount++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005331 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005332 } else if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005333 (istk->expansion->emitting != true)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005334 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005335 * We're in a non-emitting branch of an expansion.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005336 * Emit nothing at all, not even a blank line: when we
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005337 * emerge from the expansion we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005338 * directive so we keep our place correctly.
5339 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005340 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005341 continue;
5342 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005343 tline = expand_smacro(tline);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005344 if (expand_mmacro(tline) != true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005345 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005346 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005347 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005348 line = detoken(tline, true);
5349 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005350 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005351 } else {
5352 continue;
5353 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005354 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005355 }
5356 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005357}
5358
H. Peter Anvine2c80182005-01-15 22:15:51 +00005359static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005360{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005361 if (defining != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005362 error(ERR_NONFATAL, "end of file while still defining an expansion");
5363 while (defining != NULL) {
5364 ExpDef *ed = defining;
5365 defining = ed->prev;
5366 free_expdef(ed);
5367 }
5368 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005369 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005370 while (cstk != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005371 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005372 free_macros();
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005373 while (istk != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005374 Include *i = istk;
5375 istk = istk->next;
5376 fclose(i->fp);
5377 nasm_free(i->fname);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005378 while (i->expansion != NULL) {
5379 ExpInv *ei = i->expansion;
5380 i->expansion = ei->prev;
5381 free_expinv(ei);
5382 }
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005383 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005384 }
5385 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005386 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005387 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005388 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005389 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005390 free_llist(predef);
5391 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005392 while ((i = ipath)) {
5393 ipath = i->next;
5394 if (i->path)
5395 nasm_free(i->path);
5396 nasm_free(i);
5397 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005398 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005399}
5400
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005401void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005402{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005403 IncPath *i = nasm_zalloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005404
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005405 if (path)
5406 i->path = nasm_strdup(path);
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005407
H. Peter Anvin89cee572009-07-15 09:16:54 -04005408 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005409 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005410 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005411 j = j->next;
5412 j->next = i;
5413 } else {
5414 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005415 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005416}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005417
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005418void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005419{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005420 Token *inc, *space, *name;
5421 Line *l;
5422
H. Peter Anvin734b1882002-04-30 21:01:08 +00005423 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5424 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5425 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005426
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005427 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005428 l->next = predef;
5429 l->first = inc;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005430 predef = l;
5431}
5432
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005433void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005434{
5435 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005436 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005437 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005438
5439 equals = strchr(definition, '=');
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, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005442 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005443 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005444 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005445 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005446 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005447
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005448 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005449 l->next = predef;
5450 l->first = def;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005451 predef = l;
5452}
5453
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005454void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005455{
5456 Token *def, *space;
5457 Line *l;
5458
H. Peter Anvin734b1882002-04-30 21:01:08 +00005459 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5460 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005461 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005462
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005463 l = new_Line();
H. Peter Anvin620515a2002-04-30 20:57:38 +00005464 l->next = predef;
5465 l->first = def;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005466 predef = l;
5467}
5468
Keith Kaniosb7a89542007-04-12 02:40:54 +00005469/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005470 * This function is used to assist with "runtime" preprocessor
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005471 * directives, e.g. pp_runtime("%define __BITS__ 64");
Keith Kaniosb7a89542007-04-12 02:40:54 +00005472 *
5473 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5474 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5475 */
5476
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005477void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005478{
5479 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005480
Keith Kaniosb7a89542007-04-12 02:40:54 +00005481 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005482 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005483 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005484
Keith Kaniosb7a89542007-04-12 02:40:54 +00005485}
5486
H. Peter Anvina70547f2008-07-19 21:44:26 -07005487void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005488{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005489 extrastdmac = macros;
5490}
5491
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005492static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005493{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005494 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005495 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005496 tok->text = nasm_strdup(numbuf);
5497 tok->type = TOK_NUMBER;
5498}
5499
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005500Preproc nasmpp = {
5501 pp_reset,
5502 pp_getline,
5503 pp_cleanup
5504};