blob: 5c56db929fa2a0b1b54fc5fd2627c3e7f2b20cb7 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
3 * Copyright 1996-2010 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000065#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000066#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000067#include <stdlib.h>
68#include <stddef.h>
69#include <string.h>
70#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000071#include <limits.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000072#include <inttypes.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000073
74#include "nasm.h"
75#include "nasmlib.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000076#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070077#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070078#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070079#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070080#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070081#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070082#include "tables.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000083
84typedef struct SMacro SMacro;
Keith Kaniosb307a4f2010-11-06 17:41:51 -050085typedef struct ExpDef ExpDef;
86typedef struct ExpInv ExpInv;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000087typedef struct Context Context;
88typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000089typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000090typedef struct Line Line;
91typedef struct Include Include;
92typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000093typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000094
95/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070096 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
102 */
103
104/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 * Store the definition of a single-line macro.
106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107struct SMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000108 SMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000109 char *name;
H. Peter Anvin70055962007-10-11 00:05:31 -0700110 bool casesense;
H. Peter Anvin16ed4382007-10-11 10:06:19 -0700111 bool in_progress;
H. Peter Anvin70055962007-10-11 00:05:31 -0700112 unsigned int nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000113 Token *expansion;
114};
115
116/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000117 * The context stack is composed of a linked list of these.
118 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000119struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000120 Context *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000121 char *name;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700122 struct hash_table localmac;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000123 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000124};
125
126/*
127 * This is the internal form which we break input lines up into.
128 * Typically stored in linked lists.
129 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000130 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
131 * necessarily used as-is, but is intended to denote the number of
132 * the substituted parameter. So in the definition
133 *
134 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700135 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000136 * the token representing `x' will have its type changed to
137 * TOK_SMAC_PARAM, but the one representing `y' will be
138 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000139 *
140 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
141 * which doesn't need quotes around it. Used in the pre-include
142 * mechanism as an alternative to trying to find a sensible type of
143 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000144 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000145enum pp_token_type {
146 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
147 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700148 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
149 TOK_INTERNAL_STRING,
150 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300151 TOK_PASTE, /* %+ */
152 TOK_INDIRECT, /* %[...] */
153 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
154 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000155};
156
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400157#define PP_CONCAT_MASK(x) (1 << (x))
158
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400159struct tokseq_match {
160 int mask_head;
161 int mask_tail;
162};
163
H. Peter Anvine2c80182005-01-15 22:15:51 +0000164struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000165 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000166 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700167 union {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300168 SMacro *mac; /* associated macro for TOK_SMAC_END */
169 size_t len; /* scratch length field */
170 } a; /* Auxiliary data */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000171 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000172};
173
174/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500175 * Expansion definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000176 * these, which is essentially a container to allow several linked
177 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700178 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000179 * Note that in this module, linked lists are treated as stacks
180 * wherever possible. For this reason, Lines are _pushed_ on to the
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500181 * `last' field in ExpDef structures, so that the linked list,
182 * if walked, would emit the expansion lines in the proper order.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000183 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000184struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000185 Line *next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000186 Token *first;
187};
188
189/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500190 * Expansion Types
191 */
192enum pp_exp_type {
193 EXP_NONE = 0, EXP_PREDEF,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300194 EXP_MMACRO, EXP_REP,
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500195 EXP_IF, EXP_WHILE,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300196 EXP_COMMENT, EXP_FINAL,
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500197 EXP_MAX = INT_MAX /* Keep compiler from reducing the range */
198};
199
200/*
201 * Store the definition of an expansion, in which is any
202 * preprocessor directive that has an ending pair.
203 *
204 * This design allows for arbitrary expansion/recursion depth,
205 * upto the DEADMAN_LIMIT.
206 *
207 * The `next' field is used for storing ExpDef in hash tables; the
208 * `prev' field is for the global `expansions` linked-list.
209 */
210struct ExpDef {
211 ExpDef *prev; /* previous definition */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300212 ExpDef *next; /* next in hash table */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500213 enum pp_exp_type type; /* expansion type */
214 char *name; /* definition name */
215 int nparam_min, nparam_max;
216 bool casesense;
217 bool plus; /* is the last parameter greedy? */
218 bool nolist; /* is this expansion listing-inhibited? */
219 Token *dlist; /* all defaults as one list */
220 Token **defaults; /* parameter default pointers */
221 int ndefs; /* number of default parameters */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300222
223 int prepend; /* label prepend state */
224 Line *label;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500225 Line *line;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300226 Line *last;
227 int linecount; /* number of lines within expansion */
228
229 int64_t def_depth; /* current number of definition pairs deep */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500230 int64_t cur_depth; /* current number of expansions */
231 int64_t max_depth; /* maximum number of expansions allowed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300232
233 int state; /* condition state */
234 bool ignoring; /* ignoring definition lines */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500235};
236
237/*
238 * Store the invocation of an expansion.
239 *
240 * The `prev' field is for the `istk->expansion` linked-list.
241 *
242 * When an expansion is being expanded, `params', `iline', `nparam',
243 * `paramlen', `rotate' and `unique' are local to the invocation.
244 */
245struct ExpInv {
246 ExpInv *prev; /* previous invocation */
247 enum pp_exp_type type; /* expansion type */
248 ExpDef *def; /* pointer to expansion definition */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300249 char *name; /* invocation name */
250 Line *label; /* pointer to label */
251 char *label_text; /* pointer to label text */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500252 Line *current; /* pointer to current line in invocation */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300253
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500254 Token **params; /* actual parameters */
255 Token *iline; /* invocation line */
256 unsigned int nparam, rotate;
257 int *paramlen;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300258
259 uint64_t unique;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500260 bool emitting;
261 int lineno; /* current line number in expansion */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300262 int linnum; /* line number at invocation */
263 int relno; /* relative line number at invocation */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500264};
265
266/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000267 * To handle an arbitrary level of file inclusion, we maintain a
268 * stack (ie linked list) of these things.
269 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000270struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000271 Include *next;
272 FILE *fp;
273 Cond *conds;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500274 ExpInv *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000275 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000276 int lineno, lineinc;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300277 int mmac_depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000278};
279
280/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000281 * Include search path. This is simply a list of strings which get
282 * prepended, in turn, to the name of an include file, in an
283 * attempt to find the file if it's not in the current directory.
284 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000285struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000286 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000287 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000288};
289
290/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000291 * Conditional assembly: we maintain a separate stack of these for
292 * each level of file inclusion. (The only reason we keep the
293 * stacks separate is to ensure that a stray `%endif' in a file
294 * included from within the true branch of a `%if' won't terminate
295 * it and cause confusion: instead, rightly, it'll cause an error.)
296 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000297enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000298 /*
299 * These states are for use just after %if or %elif: IF_TRUE
300 * means the condition has evaluated to truth so we are
301 * currently emitting, whereas IF_FALSE means we are not
302 * currently emitting but will start doing so if a %else comes
303 * up. In these states, all directives are admissible: %elif,
304 * %else and %endif. (And of course %if.)
305 */
306 COND_IF_TRUE, COND_IF_FALSE,
307 /*
308 * These states come up after a %else: ELSE_TRUE means we're
309 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
310 * any %elif or %else will cause an error.
311 */
312 COND_ELSE_TRUE, COND_ELSE_FALSE,
313 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200314 * These states mean that we're not emitting now, and also that
315 * nothing until %endif will be emitted at all. COND_DONE is
316 * used when we've had our moment of emission
317 * and have now started seeing %elifs. COND_NEVER is used when
318 * the condition construct in question is contained within a
319 * non-emitting branch of a larger condition construct,
320 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000321 */
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200322 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000323};
324#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
325
H. Peter Anvin70653092007-10-19 14:42:29 -0700326/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000327 * These defines are used as the possible return values for do_directive
328 */
329#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300330#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000331
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000332/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500333 * This define sets the upper limit for smacro and expansions
Keith Kanios852f1ee2009-07-12 00:19:55 -0500334 */
335#define DEADMAN_LIMIT (1 << 20)
336
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400337/* max reps */
338#define REP_LIMIT ((INT64_C(1) << 62))
339
Keith Kanios852f1ee2009-07-12 00:19:55 -0500340/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341 * Condition codes. Note that we use c_ prefix not C_ because C_ is
342 * used in nasm.h for the "real" condition codes. At _this_ level,
343 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
344 * ones, so we need a different enum...
345 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700346static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000347 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
348 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000349 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000350};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700351enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000352 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
353 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
H. Peter Anvin476d2862007-10-02 22:04:15 -0700354 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
355 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000356};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700357static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000358 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
359 c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
H. Peter Anvince9be342007-09-12 00:22:29 +0000360 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000361};
362
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000363/* For TASM compatibility we need to be able to recognise TASM compatible
364 * conditional compilation directives. Using the NASM pre-processor does
365 * not work, so we look for them specifically from the following list and
366 * then jam in the equivalent NASM directive into the input stream.
367 */
368
H. Peter Anvine2c80182005-01-15 22:15:51 +0000369enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000370 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
371 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
372};
373
H. Peter Anvin476d2862007-10-02 22:04:15 -0700374static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000375 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
376 "ifndef", "include", "local"
377};
378
379static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000380static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000381static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800382static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000383
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000384static Context *cstk;
385static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000386static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000387
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300388static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700389static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000390
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300391static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000392
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000393static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700394static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000395
396static ListGen *list;
397
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000398/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500399 * The current set of expansion definitions we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000400 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500401static struct hash_table expdefs;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000402
403/*
404 * The current set of single-line macros we have defined.
405 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700406static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000407
408/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500409 * Linked List of all active expansion definitions
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000410 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500411struct ExpDef *expansions = NULL;
412
413/*
414 * The expansion we are currently defining
415 */
416static ExpDef *defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000417
Charles Crayned4200be2008-07-12 16:42:33 -0700418static uint64_t nested_mac_count;
419static uint64_t nested_rep_count;
420
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000421/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500422 * Linked-list of lines to preprocess, prior to cleanup
423 */
424static Line *finals = NULL;
425static bool in_final = false;
426
427/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000428 * The number of macro parameters to allocate space for at a time.
429 */
430#define PARAM_DELTA 16
431
432/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700433 * The standard macro set: defined in macros.c in the array nasm_stdmac.
434 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000435 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700436static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000437
438/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000439 * The extra standard macros that come from the object format, if
440 * any.
441 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700442static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700443static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000444
445/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000446 * Tokens are allocated in blocks to improve speed
447 */
448#define TOKEN_BLOCKSIZE 4096
449static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000450struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000451 Blocks *next;
452 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000453};
454
455static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000456
457/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000458 * Forward declarations.
459 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000460static Token *expand_mmac_params(Token * tline);
461static Token *expand_smacro(Token * tline);
462static Token *expand_id(Token * tline);
H. Peter Anvinf8ad5322009-02-21 17:55:08 -0800463static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300464 bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700465static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000466static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200467static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000468static void *new_Block(size_t size);
469static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700470static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300471 const char *text, int txtlen);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500472static Token *copy_Token(Token * tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000473static Token *delete_Token(Token * t);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500474static Line *new_Line(void);
475static ExpDef *new_ExpDef(int exp_type);
476static ExpInv *new_ExpInv(int exp_type, ExpDef *ed);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000477
478/*
479 * Macros for safe checking of token pointers, avoid *(NULL)
480 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300481#define tok_type_(x,t) ((x) && (x)->type == (t))
482#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
483#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
484#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000485
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300486#ifdef NASM_TRACE
487
488#define dump_token(t) raw_dump_token(t, __FILE__, __LINE__, __func__);
489static void raw_dump_token(Token *token, const char *file, int line, const char *func)
490{
491 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
492 if (token) {
493 Token *t;
494 list_for_each(t, token) {
495 if (t->text)
496 printf("'%s' ", t->text);
497 }
498 printf("\n");
499 }
500}
501
502#endif
503
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300504/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700505 * nasm_unquote with error if the string contains NUL characters.
506 * If the string contains NUL characters, issue an error and return
507 * the C len, i.e. truncate at the NUL.
508 */
509static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
510{
511 size_t len = nasm_unquote(qstr, NULL);
512 size_t clen = strlen(qstr);
513
514 if (len != clen)
515 error(ERR_NONFATAL, "NUL character in `%s' directive",
516 pp_directives[directive]);
517
518 return clen;
519}
520
521/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700522 * In-place reverse a list of tokens.
523 */
524static Token *reverse_tokens(Token *t)
525{
526 Token *prev = NULL;
527 Token *next;
528
529 while (t) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500530 next = t->next;
531 t->next = prev;
532 prev = t;
533 t = next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300534 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700535
536 return prev;
537}
538
539/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300540 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000541 * front of them. We do it here because I could not find any other
542 * place to do it for the moment, and it is a hack (ideally it would
543 * be nice to be able to use the NASM pre-processor to do it).
544 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000545static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000546{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000547 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400548 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000549
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400550 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000551
552 /* Binary search for the directive name */
553 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400554 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400555 q = nasm_skip_word(p);
556 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000557 if (len) {
558 oldchar = p[len];
559 p[len] = 0;
560 while (j - i > 1) {
561 k = (j + i) / 2;
562 m = nasm_stricmp(p, tasm_directives[k]);
563 if (m == 0) {
564 /* We have found a directive, so jam a % in front of it
565 * so that NASM will then recognise it as one if it's own.
566 */
567 p[len] = oldchar;
568 len = strlen(p);
569 oldline = line;
570 line = nasm_malloc(len + 2);
571 line[0] = '%';
572 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700573 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300574 * NASM does not recognise IFDIFI, so we convert
575 * it to %if 0. This is not used in NASM
576 * compatible code, but does need to parse for the
577 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000578 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700579 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000580 } else {
581 memcpy(line + 1, p, len + 1);
582 }
583 nasm_free(oldline);
584 return line;
585 } else if (m < 0) {
586 j = k;
587 } else
588 i = k;
589 }
590 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000591 }
592 return line;
593}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000594
H. Peter Anvin76690a12002-04-30 20:52:49 +0000595/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000596 * The pre-preprocessing stage... This function translates line
597 * number indications as they emerge from GNU cpp (`# lineno "file"
598 * flags') into NASM preprocessor line number indications (`%line
599 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000600 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000601static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000602{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000603 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000604 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000605
H. Peter Anvine2c80182005-01-15 22:15:51 +0000606 if (line[0] == '#' && line[1] == ' ') {
607 oldline = line;
608 fname = oldline + 2;
609 lineno = atoi(fname);
610 fname += strspn(fname, "0123456789 ");
611 if (*fname == '"')
612 fname++;
613 fnlen = strcspn(fname, "\"");
614 line = nasm_malloc(20 + fnlen);
615 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
616 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000617 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000618 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000619 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000620 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000621}
622
623/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000624 * Free a linked list of tokens.
625 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000626static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000627{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400628 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000629 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000630}
631
632/*
633 * Free a linked list of lines.
634 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000635static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000636{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400637 Line *l, *tmp;
638 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000639 free_tlist(l->first);
640 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000641 }
642}
643
644/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500645 * Free an ExpDef
H. Peter Anvineba20a72002-04-30 20:53:55 +0000646 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500647static void free_expdef(ExpDef * ed)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000648{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500649 nasm_free(ed->name);
650 free_tlist(ed->dlist);
651 nasm_free(ed->defaults);
652 free_llist(ed->line);
653 nasm_free(ed);
654}
655
656/*
657 * Free an ExpInv
658 */
659static void free_expinv(ExpInv * ei)
660{
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300661 if (ei->name != NULL)
662 nasm_free(ei->name);
663 if (ei->label_text != NULL)
664 nasm_free(ei->label_text);
665 nasm_free(ei);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000666}
667
668/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700669 * Free all currently defined macros, and free the hash tables
670 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700671static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700672{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400673 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700674 const char *key;
675 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700676
H. Peter Anvin072771e2008-05-22 13:17:51 -0700677 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300678 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400679 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300680 nasm_free(s->name);
681 free_tlist(s->expansion);
682 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300683 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700684 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700685 hash_free(smt);
686}
687
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500688static void free_expdef_table(struct hash_table *edt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700689{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500690 ExpDef *ed, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700691 const char *key;
692 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700693
694 it = NULL;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500695 while ((ed = hash_iterate(edt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300696 nasm_free((void *)key);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500697 list_for_each_safe(ed ,tmp, ed)
698 free_expdef(ed);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700699 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500700 hash_free(edt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700701}
702
703static void free_macros(void)
704{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700705 free_smacro_table(&smacros);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500706 free_expdef_table(&expdefs);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700707}
708
709/*
710 * Initialize the hash tables
711 */
712static void init_macros(void)
713{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700714 hash_init(&smacros, HASH_LARGE);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500715 hash_init(&expdefs, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700716}
717
718/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000719 * Pop the context stack.
720 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000721static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000722{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000723 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000724
725 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700726 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000727 nasm_free(c->name);
728 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000729}
730
H. Peter Anvin072771e2008-05-22 13:17:51 -0700731/*
732 * Search for a key in the hash index; adding it if necessary
733 * (in which case we initialize the data pointer to NULL.)
734 */
735static void **
736hash_findi_add(struct hash_table *hash, const char *str)
737{
738 struct hash_insert hi;
739 void **r;
740 char *strx;
741
742 r = hash_findi(hash, str, &hi);
743 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300744 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700745
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300746 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700747 return hash_add(&hi, strx, NULL);
748}
749
750/*
751 * Like hash_findi, but returns the data element rather than a pointer
752 * to it. Used only when not adding a new element, hence no third
753 * argument.
754 */
755static void *
756hash_findix(struct hash_table *hash, const char *str)
757{
758 void **p;
759
760 p = hash_findi(hash, str, NULL);
761 return p ? *p : NULL;
762}
763
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400764/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500765 * read line from standard macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400766 * if there no more left -- return NULL
767 */
768static char *line_from_stdmac(void)
769{
770 unsigned char c;
771 const unsigned char *p = stdmacpos;
772 char *line, *q;
773 size_t len = 0;
774
775 if (!stdmacpos)
776 return NULL;
777
778 while ((c = *p++)) {
779 if (c >= 0x80)
780 len += pp_directives_len[c - 0x80] + 1;
781 else
782 len++;
783 }
784
785 line = nasm_malloc(len + 1);
786 q = line;
787 while ((c = *stdmacpos++)) {
788 if (c >= 0x80) {
789 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
790 q += pp_directives_len[c - 0x80];
791 *q++ = ' ';
792 } else {
793 *q++ = c;
794 }
795 }
796 stdmacpos = p;
797 *q = '\0';
798
799 if (!*stdmacpos) {
800 /* This was the last of the standard macro chain... */
801 stdmacpos = NULL;
802 if (any_extrastdmac) {
803 stdmacpos = extrastdmac;
804 any_extrastdmac = false;
805 } else if (do_predef) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300806 ExpInv *ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400807 Line *pd, *l;
808 Token *head, **tail, *t;
809
810 /*
811 * Nasty hack: here we push the contents of
812 * `predef' on to the top-level expansion stack,
813 * since this is the most convenient way to
814 * implement the pre-include and pre-define
815 * features.
816 */
817 list_for_each(pd, predef) {
818 head = NULL;
819 tail = &head;
820 list_for_each(t, pd->first) {
821 *tail = new_Token(NULL, t->type, t->text, 0);
822 tail = &(*tail)->next;
823 }
824
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500825 l = new_Line();
826 l->first = head;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300827 ei = new_ExpInv(EXP_PREDEF, NULL);
828 ei->current = l;
829 ei->emitting = true;
830 ei->prev = istk->expansion;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500831 istk->expansion = ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400832 }
833 do_predef = false;
834 }
835 }
836
837 return line;
838}
839
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000840#define BUF_DELTA 512
841/*
842 * Read a line from the top file in istk, handling multiple CR/LFs
843 * at the end of the line read, and handling spurious ^Zs. Will
844 * return lines from the standard macro set if this has not already
845 * been done.
846 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000847static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000848{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000849 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000850 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000851
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400852 /*
853 * standart macros set (predefined) goes first
854 */
855 p = line_from_stdmac();
856 if (p)
857 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700858
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400859 /*
860 * regular read from a file
861 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000862 bufsize = BUF_DELTA;
863 buffer = nasm_malloc(BUF_DELTA);
864 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000865 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000866 while (1) {
867 q = fgets(p, bufsize - (p - buffer), istk->fp);
868 if (!q)
869 break;
870 p += strlen(p);
871 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300872 /*
873 * Convert backslash-CRLF line continuation sequences into
874 * nothing at all (for DOS and Windows)
875 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000876 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
877 p -= 3;
878 *p = 0;
879 continued_count++;
880 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300881 /*
882 * Also convert backslash-LF line continuation sequences into
883 * nothing at all (for Unix)
884 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000885 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
886 p -= 2;
887 *p = 0;
888 continued_count++;
889 } else {
890 break;
891 }
892 }
893 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000894 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000895 bufsize += BUF_DELTA;
896 buffer = nasm_realloc(buffer, bufsize);
897 p = buffer + offset; /* prevent stale-pointer problems */
898 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000899 }
900
H. Peter Anvine2c80182005-01-15 22:15:51 +0000901 if (!q && p == buffer) {
902 nasm_free(buffer);
903 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000904 }
905
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300906 src_set_linnum(src_get_linnum() + istk->lineinc +
907 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000908
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000909 /*
910 * Play safe: remove CRs as well as LFs, if any of either are
911 * present at the end of the line.
912 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000913 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000914 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000915
916 /*
917 * Handle spurious ^Z, which may be inserted into source files
918 * by some file transfer utilities.
919 */
920 buffer[strcspn(buffer, "\032")] = '\0';
921
H. Peter Anvin734b1882002-04-30 21:01:08 +0000922 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000923
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000924 return buffer;
925}
926
927/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000928 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000929 * don't need to parse the value out of e.g. numeric tokens: we
930 * simply split one string into many.
931 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000932static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000933{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700934 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000935 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000936 Token *list = NULL;
937 Token *t, **tail = &list;
Keith Kanios6faad4e2010-12-18 14:08:02 -0600938 bool verbose = true;
939
940 if ((defining != NULL) && (defining->ignoring == true)) {
941 verbose = false;
942 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000943
H. Peter Anvine2c80182005-01-15 22:15:51 +0000944 while (*line) {
945 p = line;
946 if (*p == '%') {
947 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300948 if (*p == '+' && !nasm_isdigit(p[1])) {
949 p++;
950 type = TOK_PASTE;
951 } else if (nasm_isdigit(*p) ||
952 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953 do {
954 p++;
955 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700956 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000957 type = TOK_PREPROC_ID;
958 } else if (*p == '{') {
959 p++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500960 while (*p && *p != '}') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000961 p[-1] = *p;
962 p++;
963 }
964 p[-1] = '\0';
965 if (*p)
966 p++;
967 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300968 } else if (*p == '[') {
969 int lvl = 1;
970 line += 2; /* Skip the leading %[ */
971 p++;
972 while (lvl && (c = *p++)) {
973 switch (c) {
974 case ']':
975 lvl--;
976 break;
977 case '%':
978 if (*p == '[')
979 lvl++;
980 break;
981 case '\'':
982 case '\"':
983 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400984 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300985 break;
986 default:
987 break;
988 }
989 }
990 p--;
991 if (*p)
992 *p++ = '\0';
Keith Kanios6faad4e2010-12-18 14:08:02 -0600993 if (lvl && verbose)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300994 error(ERR_NONFATAL, "unterminated %[ construct");
995 type = TOK_INDIRECT;
996 } else if (*p == '?') {
997 type = TOK_PREPROC_Q; /* %? */
998 p++;
999 if (*p == '?') {
1000 type = TOK_PREPROC_QQ; /* %?? */
1001 p++;
1002 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001003 } else if (*p == '!') {
1004 type = TOK_PREPROC_ID;
1005 p++;
1006 if (isidchar(*p)) {
1007 do {
1008 p++;
1009 } while (isidchar(*p));
1010 } else if (*p == '\'' || *p == '\"' || *p == '`') {
1011 p = nasm_skip_string(p);
1012 if (*p)
1013 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001014 else if(verbose)
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001015 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
1016 } else {
1017 /* %! without string or identifier */
1018 type = TOK_OTHER; /* Legacy behavior... */
1019 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001020 } else if (isidchar(*p) ||
1021 ((*p == '!' || *p == '%' || *p == '$') &&
1022 isidchar(p[1]))) {
1023 do {
1024 p++;
1025 }
1026 while (isidchar(*p));
1027 type = TOK_PREPROC_ID;
1028 } else {
1029 type = TOK_OTHER;
1030 if (*p == '%')
1031 p++;
1032 }
1033 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1034 type = TOK_ID;
1035 p++;
1036 while (*p && isidchar(*p))
1037 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001038 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001039 /*
1040 * A string token.
1041 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001042 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001043 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001044
H. Peter Anvine2c80182005-01-15 22:15:51 +00001045 if (*p) {
1046 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001047 } else if(verbose) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001048 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001049 /* Handling unterminated strings by UNV */
1050 /* type = -1; */
1051 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001052 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001053 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001054 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001055 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001056 bool is_hex = false;
1057 bool is_float = false;
1058 bool has_e = false;
1059 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001060
H. Peter Anvine2c80182005-01-15 22:15:51 +00001061 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001062 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001063 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001064
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001065 if (*p == '$') {
1066 p++;
1067 is_hex = true;
1068 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001069
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001070 for (;;) {
1071 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001072
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001073 if (!is_hex && (c == 'e' || c == 'E')) {
1074 has_e = true;
1075 if (*p == '+' || *p == '-') {
1076 /*
1077 * e can only be followed by +/- if it is either a
1078 * prefixed hex number or a floating-point number
1079 */
1080 p++;
1081 is_float = true;
1082 }
1083 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1084 is_hex = true;
1085 } else if (c == 'P' || c == 'p') {
1086 is_float = true;
1087 if (*p == '+' || *p == '-')
1088 p++;
1089 } else if (isnumchar(c) || c == '_')
1090 ; /* just advance */
1091 else if (c == '.') {
1092 /*
1093 * we need to deal with consequences of the legacy
1094 * parser, like "1.nolist" being two tokens
1095 * (TOK_NUMBER, TOK_ID) here; at least give it
1096 * a shot for now. In the future, we probably need
1097 * a flex-based scanner with proper pattern matching
1098 * to do it as well as it can be done. Nothing in
1099 * the world is going to help the person who wants
1100 * 0x123.p16 interpreted as two tokens, though.
1101 */
1102 r = p;
1103 while (*r == '_')
1104 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001105
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001106 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1107 (!is_hex && (*r == 'e' || *r == 'E')) ||
1108 (*r == 'p' || *r == 'P')) {
1109 p = r;
1110 is_float = true;
1111 } else
1112 break; /* Terminate the token */
1113 } else
1114 break;
1115 }
1116 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001117
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001118 if (p == line+1 && *line == '$') {
1119 type = TOK_OTHER; /* TOKEN_HERE */
1120 } else {
1121 if (has_e && !is_hex) {
1122 /* 1e13 is floating-point, but 1e13h is not */
1123 is_float = true;
1124 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001125
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001126 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1127 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001128 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001130 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001131 /*
1132 * Whitespace just before end-of-line is discarded by
1133 * pretending it's a comment; whitespace just before a
1134 * comment gets lumped into the comment.
1135 */
1136 if (!*p || *p == ';') {
1137 type = TOK_COMMENT;
1138 while (*p)
1139 p++;
1140 }
1141 } else if (*p == ';') {
1142 type = TOK_COMMENT;
1143 while (*p)
1144 p++;
1145 } else {
1146 /*
1147 * Anything else is an operator of some kind. We check
1148 * for all the double-character operators (>>, <<, //,
1149 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001150 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151 */
1152 type = TOK_OTHER;
1153 if ((p[0] == '>' && p[1] == '>') ||
1154 (p[0] == '<' && p[1] == '<') ||
1155 (p[0] == '/' && p[1] == '/') ||
1156 (p[0] == '<' && p[1] == '=') ||
1157 (p[0] == '>' && p[1] == '=') ||
1158 (p[0] == '=' && p[1] == '=') ||
1159 (p[0] == '!' && p[1] == '=') ||
1160 (p[0] == '<' && p[1] == '>') ||
1161 (p[0] == '&' && p[1] == '&') ||
1162 (p[0] == '|' && p[1] == '|') ||
1163 (p[0] == '^' && p[1] == '^')) {
1164 p++;
1165 }
1166 p++;
1167 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001168
H. Peter Anvine2c80182005-01-15 22:15:51 +00001169 /* Handling unterminated string by UNV */
1170 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001171 {
1172 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1173 t->text[p-line] = *line;
1174 tail = &t->next;
1175 }
1176 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177 if (type != TOK_COMMENT) {
1178 *tail = t = new_Token(NULL, type, line, p - line);
1179 tail = &t->next;
1180 }
1181 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001182 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001183 return list;
1184}
1185
H. Peter Anvince616072002-04-30 21:02:23 +00001186/*
1187 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001188 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001189 * deleted only all at once by the delete_Blocks function.
1190 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001191static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001192{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001193 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001194
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001195 /* first, get to the end of the linked list */
1196 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001197 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001198 /* now allocate the requested chunk */
1199 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001200
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001201 /* now allocate a new block for the next request */
1202 b->next = nasm_malloc(sizeof(Blocks));
1203 /* and initialize the contents of the new block */
1204 b->next->next = NULL;
1205 b->next->chunk = NULL;
1206 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001207}
1208
1209/*
1210 * this function deletes all managed blocks of memory
1211 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001212static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001213{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001214 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001215
H. Peter Anvin70653092007-10-19 14:42:29 -07001216 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001217 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001218 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001219 * free it.
1220 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001221 while (b) {
1222 if (b->chunk)
1223 nasm_free(b->chunk);
1224 a = b;
1225 b = b->next;
1226 if (a != &blocks)
1227 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001228 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001229}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001230
1231/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001232 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001233 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001234 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001235 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001236static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001237 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001238{
1239 Token *t;
1240 int i;
1241
H. Peter Anvin89cee572009-07-15 09:16:54 -04001242 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001243 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1244 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1245 freeTokens[i].next = &freeTokens[i + 1];
1246 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001247 }
1248 t = freeTokens;
1249 freeTokens = t->next;
1250 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001251 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001252 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001253 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001254 t->text = NULL;
1255 } else {
1256 if (txtlen == 0)
1257 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001258 t->text = nasm_malloc(txtlen+1);
1259 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001260 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001261 }
1262 return t;
1263}
1264
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001265static Token *copy_Token(Token * tline)
1266{
1267 Token *t, *tt, *first = NULL, *prev = NULL;
1268 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001269 for (tt = tline; tt != NULL; tt = tt->next) {
1270 if (!freeTokens) {
1271 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1272 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1273 freeTokens[i].next = &freeTokens[i + 1];
1274 freeTokens[i].next = NULL;
1275 }
1276 t = freeTokens;
1277 freeTokens = t->next;
1278 t->next = NULL;
1279 t->text = tt->text ? nasm_strdup(tt->text) : NULL;
1280 t->a.mac = tt->a.mac;
1281 t->a.len = tt->a.len;
1282 t->type = tt->type;
1283 if (prev != NULL) {
1284 prev->next = t;
1285 } else {
1286 first = t;
1287 }
1288 prev = t;
1289 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001290 return first;
1291}
1292
H. Peter Anvine2c80182005-01-15 22:15:51 +00001293static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001294{
1295 Token *next = t->next;
1296 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001297 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001298 freeTokens = t;
1299 return next;
1300}
1301
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001302/*
1303 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001304 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1305 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001306 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001307static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001308{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001309 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001310 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001311 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001312 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001313
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001314 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001315 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001316 char *v;
1317 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001318
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001319 v = t->text + 2;
1320 if (*v == '\'' || *v == '\"' || *v == '`') {
1321 size_t len = nasm_unquote(v, NULL);
1322 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001323
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001324 if (len != clen) {
1325 error(ERR_NONFATAL | ERR_PASS1,
1326 "NUL character in %! string");
1327 v = NULL;
1328 }
1329 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001330
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001331 if (v) {
1332 char *p = getenv(v);
1333 if (!p) {
1334 error(ERR_NONFATAL | ERR_PASS1,
1335 "nonexistent environment variable `%s'", v);
1336 p = "";
1337 }
1338 t->text = nasm_strdup(p);
1339 }
1340 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001342
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 /* Expand local macros here and not during preprocessing */
1344 if (expand_locals &&
1345 t->type == TOK_PREPROC_ID && t->text &&
1346 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001347 const char *q;
1348 char *p;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001349 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001351 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001352 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001353 p = nasm_strcat(buffer, q);
1354 nasm_free(t->text);
1355 t->text = p;
1356 }
1357 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001358
1359 /* Expand %? and %?? directives */
Keith Kanios3136d482010-11-13 09:34:34 -06001360 if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001361 ((t->type == TOK_PREPROC_Q) ||
1362 (t->type == TOK_PREPROC_QQ))) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001363 ExpInv *ei;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001364 for (ei = istk->expansion; ei != NULL; ei = ei->prev){
1365 if (ei->type == EXP_MMACRO) {
1366 nasm_free(t->text);
1367 if (t->type == TOK_PREPROC_Q) {
1368 t->text = nasm_strdup(ei->name);
1369 } else {
1370 t->text = nasm_strdup(ei->def->name);
1371 }
1372 break;
1373 }
1374 }
1375 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001376
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001377 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001378 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001379 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001381 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001382
H. Peter Anvin734b1882002-04-30 21:01:08 +00001383 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001384
1385 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001386 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001387 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001388 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001389 q = t->text;
1390 while (*q)
1391 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001392 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001393 }
1394 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001395
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001396 return line;
1397}
1398
1399/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001400 * Initialize a new Line
1401 */
Cyrill Gorcunov29cb0bb2010-11-11 11:19:43 +03001402static inline Line *new_Line(void)
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001403{
Cyrill Gorcunoveb7bf982010-11-11 23:08:14 +03001404 Line *l = nasm_malloc(sizeof(Line));
1405 l->next = NULL;
1406 l->first = NULL;
1407 return l;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001408}
1409
1410
1411/*
1412 * Initialize a new Expansion Definition
1413 */
1414static ExpDef *new_ExpDef(int exp_type)
1415{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001416 ExpDef *ed = (ExpDef*)nasm_zalloc(sizeof(ExpDef));
1417 ed->type = exp_type;
1418 ed->casesense = true;
1419 ed->state = COND_NEVER;
1420
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001421 return ed;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001422}
1423
1424
1425/*
1426 * Initialize a new Expansion Instance
1427 */
1428static ExpInv *new_ExpInv(int exp_type, ExpDef *ed)
1429{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001430 ExpInv *ei = (ExpInv*)nasm_zalloc(sizeof(ExpInv));
1431 ei->type = exp_type;
1432 ei->def = ed;
1433 ei->unique = ++unique;
1434
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001435 if ((istk->mmac_depth < 1) &&
1436 (istk->expansion == NULL) &&
1437 (ed != NULL) &&
1438 (ed->type != EXP_MMACRO) &&
1439 (ed->type != EXP_REP) &&
1440 (ed->type != EXP_WHILE)) {
1441 ei->linnum = src_get_linnum();
1442 src_set_linnum(ei->linnum - ed->linecount - 1);
1443 } else {
1444 ei->linnum = -1;
1445 }
1446 if ((istk->expansion == NULL) ||
1447 (ei->type == EXP_MMACRO)) {
1448 ei->relno = 0;
1449 } else {
1450 ei->relno = istk->expansion->lineno;
1451 if (ed != NULL) {
1452 ei->relno -= (ed->linecount + 1);
1453 }
1454 }
1455 return ei;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001456}
1457
1458/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001459 * A scanner, suitable for use by the expression evaluator, which
1460 * operates on a line of Tokens. Expects a pointer to a pointer to
1461 * the first token in the line to be passed in as its private_data
1462 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001463 *
1464 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001465 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001466static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001467{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001468 Token **tlineptr = private_data;
1469 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001470 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001471
H. Peter Anvine2c80182005-01-15 22:15:51 +00001472 do {
1473 tline = *tlineptr;
1474 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001475 } while (tline && (tline->type == TOK_WHITESPACE ||
1476 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001477
1478 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001479 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001480
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001481 tokval->t_charptr = tline->text;
1482
H. Peter Anvin76690a12002-04-30 20:52:49 +00001483 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001484 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001485 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001486 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001487
H. Peter Anvine2c80182005-01-15 22:15:51 +00001488 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001489 p = tokval->t_charptr = tline->text;
1490 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001491 tokval->t_charptr++;
1492 return tokval->t_type = TOKEN_ID;
1493 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001494
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001495 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001496 if (r >= p+MAX_KEYWORD)
1497 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001498 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001499 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001500 *s = '\0';
1501 /* right, so we have an identifier sitting in temp storage. now,
1502 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001503 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001504 }
1505
H. Peter Anvine2c80182005-01-15 22:15:51 +00001506 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001507 bool rn_error;
1508 tokval->t_integer = readnum(tline->text, &rn_error);
1509 tokval->t_charptr = tline->text;
1510 if (rn_error)
1511 return tokval->t_type = TOKEN_ERRNUM;
1512 else
1513 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001514 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001515
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001516 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001517 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001518 }
1519
H. Peter Anvine2c80182005-01-15 22:15:51 +00001520 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001521 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001522
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001523 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001524 tokval->t_charptr = tline->text;
1525 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001526
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001527 if (ep[0] != bq || ep[1] != '\0')
1528 return tokval->t_type = TOKEN_ERRSTR;
1529 else
1530 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001531 }
1532
H. Peter Anvine2c80182005-01-15 22:15:51 +00001533 if (tline->type == TOK_OTHER) {
1534 if (!strcmp(tline->text, "<<"))
1535 return tokval->t_type = TOKEN_SHL;
1536 if (!strcmp(tline->text, ">>"))
1537 return tokval->t_type = TOKEN_SHR;
1538 if (!strcmp(tline->text, "//"))
1539 return tokval->t_type = TOKEN_SDIV;
1540 if (!strcmp(tline->text, "%%"))
1541 return tokval->t_type = TOKEN_SMOD;
1542 if (!strcmp(tline->text, "=="))
1543 return tokval->t_type = TOKEN_EQ;
1544 if (!strcmp(tline->text, "<>"))
1545 return tokval->t_type = TOKEN_NE;
1546 if (!strcmp(tline->text, "!="))
1547 return tokval->t_type = TOKEN_NE;
1548 if (!strcmp(tline->text, "<="))
1549 return tokval->t_type = TOKEN_LE;
1550 if (!strcmp(tline->text, ">="))
1551 return tokval->t_type = TOKEN_GE;
1552 if (!strcmp(tline->text, "&&"))
1553 return tokval->t_type = TOKEN_DBL_AND;
1554 if (!strcmp(tline->text, "^^"))
1555 return tokval->t_type = TOKEN_DBL_XOR;
1556 if (!strcmp(tline->text, "||"))
1557 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001558 }
1559
1560 /*
1561 * We have no other options: just return the first character of
1562 * the token text.
1563 */
1564 return tokval->t_type = tline->text[0];
1565}
1566
1567/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001568 * Compare a string to the name of an existing macro; this is a
1569 * simple wrapper which calls either strcmp or nasm_stricmp
1570 * depending on the value of the `casesense' parameter.
1571 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001572static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001573{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001574 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001575}
1576
1577/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001578 * Compare a string to the name of an existing macro; this is a
1579 * simple wrapper which calls either strcmp or nasm_stricmp
1580 * depending on the value of the `casesense' parameter.
1581 */
1582static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1583{
1584 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1585}
1586
1587/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001588 * Return the Context structure associated with a %$ token. Return
1589 * NULL, having _already_ reported an error condition, if the
1590 * context stack isn't deep enough for the supplied number of $
1591 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001592 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001593 * also scanned for such smacro, until it is found; if not -
1594 * only the context that directly results from the number of $'s
1595 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001596 *
1597 * If "namep" is non-NULL, set it to the pointer to the macro name
1598 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001599 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001600static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001601 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001602{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001603 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001604 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001605 int i;
1606
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001607 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001608 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001609
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001610 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001611 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001612
H. Peter Anvine2c80182005-01-15 22:15:51 +00001613 if (!cstk) {
1614 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1615 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001616 }
1617
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001618 name += 2;
1619 ctx = cstk;
1620 i = 0;
1621 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001622 name++;
1623 i++;
1624 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001625 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001626 if (!ctx) {
1627 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001628 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001629 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001630 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001631
1632 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001633 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001634
Keith Kanios404589e2010-08-10 20:12:57 -05001635 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001636 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001637
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001638 do {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001640 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001641 while (m) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001642 if (!mstrcmp(m->name, name, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001643 return ctx;
1644 m = m->next;
1645 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001646 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001647 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001648 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001649 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001650}
1651
1652/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001653 * Check to see if a file is already in a string list
1654 */
1655static bool in_list(const StrList *list, const char *str)
1656{
1657 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001658 if (!strcmp(list->str, str))
1659 return true;
1660 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001661 }
1662 return false;
1663}
1664
1665/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001666 * Open an include file. This routine must always return a valid
1667 * file pointer if it returns - it's responsible for throwing an
1668 * ERR_FATAL and bombing out completely if not. It should also try
1669 * the include path one by one until it finds the file or reaches
1670 * the end of the path.
1671 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001672static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001673 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001674{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001675 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001676 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001677 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001678 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001679 size_t prefix_len = 0;
1680 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001681
H. Peter Anvine2c80182005-01-15 22:15:51 +00001682 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001683 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunov6f38fe62010-11-11 11:32:16 +03001684 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001685 memcpy(sl->str, prefix, prefix_len);
1686 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001687 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001688 if (fp && dhead && !in_list(*dhead, sl->str)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001689 **dtail = sl;
1690 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001691 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001692 nasm_free(sl);
1693 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001694 if (fp)
1695 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001696 if (!ip) {
1697 if (!missing_ok)
1698 break;
1699 prefix = NULL;
1700 } else {
1701 prefix = ip->path;
1702 ip = ip->next;
1703 }
1704 if (prefix) {
1705 prefix_len = strlen(prefix);
1706 } else {
1707 /* -MG given and file not found */
1708 if (dhead && !in_list(*dhead, file)) {
1709 sl = nasm_malloc(len+1+sizeof sl->next);
1710 sl->next = NULL;
1711 strcpy(sl->str, file);
1712 **dtail = sl;
1713 *dtail = &sl->next;
1714 }
1715 return NULL;
1716 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001717 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001718
H. Peter Anvin734b1882002-04-30 21:01:08 +00001719 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001720 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001721}
1722
1723/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001724 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001725 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001726 * return true if _any_ single-line macro of that name is defined.
1727 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001728 * `nparam' or no parameters is defined.
1729 *
1730 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001731 * defined, or nparam is -1, the address of the definition structure
1732 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001733 * is NULL, no action will be taken regarding its contents, and no
1734 * error will occur.
1735 *
1736 * Note that this is also called with nparam zero to resolve
1737 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001738 *
1739 * If you already know which context macro belongs to, you can pass
1740 * the context pointer as first parameter; if you won't but name begins
1741 * with %$ the context will be automatically computed. If all_contexts
1742 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001743 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001744static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001745smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001746 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001747{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001748 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001749 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001750
H. Peter Anvin97a23472007-09-16 17:57:25 -07001751 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001752 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001753 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001754 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001755 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001756 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001757 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001758 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001759 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001760 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001761 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001762 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001763
H. Peter Anvine2c80182005-01-15 22:15:51 +00001764 while (m) {
1765 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001766 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001767 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001768 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001769 *defn = m;
1770 else
1771 *defn = NULL;
1772 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001773 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001774 }
1775 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001776 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001777
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001778 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001779}
1780
1781/*
1782 * Count and mark off the parameters in a multi-line macro call.
1783 * This is called both from within the multi-line macro expansion
1784 * code, and also to mark off the default parameters when provided
1785 * in a %macro definition line.
1786 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001787static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001788{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001789 int paramsize, brace;
1790
1791 *nparam = paramsize = 0;
1792 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001793 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001794 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001795 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001796 paramsize += PARAM_DELTA;
1797 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1798 }
1799 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001800 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001801 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001802 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001803 (*params)[(*nparam)++] = t;
1804 while (tok_isnt_(t, brace ? "}" : ","))
1805 t = t->next;
1806 if (t) { /* got a comma/brace */
1807 t = t->next;
1808 if (brace) {
1809 /*
1810 * Now we've found the closing brace, look further
1811 * for the comma.
1812 */
1813 skip_white_(t);
1814 if (tok_isnt_(t, ",")) {
1815 error(ERR_NONFATAL,
1816 "braces do not enclose all of macro parameter");
1817 while (tok_isnt_(t, ","))
1818 t = t->next;
1819 }
1820 if (t)
1821 t = t->next; /* eat the comma */
1822 }
1823 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001824 }
1825}
1826
1827/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001828 * Determine whether one of the various `if' conditions is true or
1829 * not.
1830 *
1831 * We must free the tline we get passed.
1832 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001833static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001834{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001835 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001836 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001837 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001838 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001839 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001840 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001841 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001842
1843 origline = tline;
1844
H. Peter Anvine2c80182005-01-15 22:15:51 +00001845 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001846 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001847 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001848 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001849 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001850 if (!tline)
1851 break;
1852 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001853 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001854 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001855 free_tlist(origline);
1856 return -1;
1857 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001858 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001859 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001860 tline = tline->next;
1861 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001862 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001863
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001864 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001865 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001866 while (tline) {
1867 skip_white_(tline);
1868 if (!tline || (tline->type != TOK_ID &&
1869 (tline->type != TOK_PREPROC_ID ||
1870 tline->text[1] != '$'))) {
1871 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001872 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001873 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001874 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001875 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001876 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001877 tline = tline->next;
1878 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001879 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001880
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001881 case PPC_IFENV:
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001882 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001883 j = false; /* have we matched yet? */
1884 while (tline) {
1885 skip_white_(tline);
1886 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001887 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001888 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001889 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001890 error(ERR_NONFATAL,
1891 "`%s' expects environment variable names",
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001892 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001893 goto fail;
1894 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001895 p = tline->text;
1896 if (tline->type == TOK_PREPROC_ID)
1897 p += 2; /* Skip leading %! */
1898 if (*p == '\'' || *p == '\"' || *p == '`')
1899 nasm_unquote_cstr(p, ct);
1900 if (getenv(p))
1901 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001902 tline = tline->next;
1903 }
1904 break;
1905
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001906 case PPC_IFIDN:
1907 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001908 tline = expand_smacro(tline);
1909 t = tt = tline;
1910 while (tok_isnt_(tt, ","))
1911 tt = tt->next;
1912 if (!tt) {
1913 error(ERR_NONFATAL,
1914 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001915 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001916 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001917 }
1918 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001919 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001920 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1921 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1922 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001923 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001924 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001925 }
1926 if (t->type == TOK_WHITESPACE) {
1927 t = t->next;
1928 continue;
1929 }
1930 if (tt->type == TOK_WHITESPACE) {
1931 tt = tt->next;
1932 continue;
1933 }
1934 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001935 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001936 break;
1937 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001938 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001939 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001940 size_t l1 = nasm_unquote(t->text, NULL);
1941 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001942
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001943 if (l1 != l2) {
1944 j = false;
1945 break;
1946 }
1947 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1948 j = false;
1949 break;
1950 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001951 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001952 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001953 break;
1954 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001955
H. Peter Anvine2c80182005-01-15 22:15:51 +00001956 t = t->next;
1957 tt = tt->next;
1958 }
1959 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001960 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001961 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001962
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001963 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001964 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001965 bool found = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001966 ExpDef searching, *ed;
H. Peter Anvin65747262002-05-07 00:10:05 +00001967
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001968 skip_white_(tline);
1969 tline = expand_id(tline);
1970 if (!tok_type_(tline, TOK_ID)) {
1971 error(ERR_NONFATAL,
1972 "`%s' expects a macro name", pp_directives[ct]);
1973 goto fail;
1974 }
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03001975 memset(&searching, 0, sizeof(searching));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001976 searching.name = nasm_strdup(tline->text);
1977 searching.casesense = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001978 searching.nparam_max = INT_MAX;
1979 tline = expand_smacro(tline->next);
1980 skip_white_(tline);
1981 if (!tline) {
1982 } else if (!tok_type_(tline, TOK_NUMBER)) {
1983 error(ERR_NONFATAL,
1984 "`%s' expects a parameter count or nothing",
1985 pp_directives[ct]);
1986 } else {
1987 searching.nparam_min = searching.nparam_max =
1988 readnum(tline->text, &j);
1989 if (j)
1990 error(ERR_NONFATAL,
1991 "unable to parse parameter count `%s'",
1992 tline->text);
1993 }
1994 if (tline && tok_is_(tline->next, "-")) {
1995 tline = tline->next->next;
1996 if (tok_is_(tline, "*"))
1997 searching.nparam_max = INT_MAX;
1998 else if (!tok_type_(tline, TOK_NUMBER))
1999 error(ERR_NONFATAL,
2000 "`%s' expects a parameter count after `-'",
2001 pp_directives[ct]);
2002 else {
2003 searching.nparam_max = readnum(tline->text, &j);
2004 if (j)
2005 error(ERR_NONFATAL,
2006 "unable to parse parameter count `%s'",
2007 tline->text);
2008 if (searching.nparam_min > searching.nparam_max)
2009 error(ERR_NONFATAL,
2010 "minimum parameter count exceeds maximum");
2011 }
2012 }
2013 if (tline && tok_is_(tline->next, "+")) {
2014 tline = tline->next;
2015 searching.plus = true;
2016 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002017 ed = (ExpDef *) hash_findix(&expdefs, searching.name);
2018 while (ed != NULL) {
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002019 if (!strcmp(ed->name, searching.name) &&
2020 (ed->nparam_min <= searching.nparam_max || searching.plus) &&
2021 (searching.nparam_min <= ed->nparam_max || ed->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002022 found = true;
2023 break;
2024 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002025 ed = ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002026 }
2027 if (tline && tline->next)
2028 error(ERR_WARNING|ERR_PASS1,
2029 "trailing garbage after %%ifmacro ignored");
2030 nasm_free(searching.name);
2031 j = found;
2032 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002033 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002034
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002035 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002036 needtype = TOK_ID;
2037 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002038 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002039 needtype = TOK_NUMBER;
2040 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002041 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002042 needtype = TOK_STRING;
2043 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002044
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002045iftype:
2046 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002047
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002048 while (tok_type_(t, TOK_WHITESPACE) ||
2049 (needtype == TOK_NUMBER &&
2050 tok_type_(t, TOK_OTHER) &&
2051 (t->text[0] == '-' || t->text[0] == '+') &&
2052 !t->text[1]))
2053 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002054
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002055 j = tok_type_(t, needtype);
2056 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002057
2058 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002059 t = tline = expand_smacro(tline);
2060 while (tok_type_(t, TOK_WHITESPACE))
2061 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002062
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002063 j = false;
2064 if (t) {
2065 t = t->next; /* Skip the actual token */
2066 while (tok_type_(t, TOK_WHITESPACE))
2067 t = t->next;
2068 j = !t; /* Should be nothing left */
2069 }
2070 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002071
H. Peter Anvin134b9462008-02-16 17:01:40 -08002072 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002073 t = tline = expand_smacro(tline);
2074 while (tok_type_(t, TOK_WHITESPACE))
2075 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002076
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002077 j = !t; /* Should be empty */
2078 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002079
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002080 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002081 t = tline = expand_smacro(tline);
2082 tptr = &t;
2083 tokval.t_type = TOKEN_INVALID;
2084 evalresult = evaluate(ppscan, tptr, &tokval,
2085 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002086 if (!evalresult)
2087 return -1;
2088 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002089 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002090 "trailing garbage after expression ignored");
2091 if (!is_simple(evalresult)) {
2092 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002093 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002094 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002095 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002096 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002097 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002098
H. Peter Anvine2c80182005-01-15 22:15:51 +00002099 default:
2100 error(ERR_FATAL,
2101 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002102 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002103 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002104 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002105
2106 free_tlist(origline);
2107 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002108
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002109fail:
2110 free_tlist(origline);
2111 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002112}
2113
2114/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002115 * Common code for defining an smacro
2116 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002117static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002118 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002119{
2120 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002121 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002122
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002123 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002124 if (!smac) {
2125 error(ERR_WARNING|ERR_PASS1,
2126 "single-line macro `%s' defined both with and"
2127 " without parameters", mname);
2128 /*
2129 * Some instances of the old code considered this a failure,
2130 * some others didn't. What is the right thing to do here?
2131 */
2132 free_tlist(expansion);
2133 return false; /* Failure */
2134 } else {
2135 /*
2136 * We're redefining, so we have to take over an
2137 * existing SMacro structure. This means freeing
2138 * what was already in it.
2139 */
2140 nasm_free(smac->name);
2141 free_tlist(smac->expansion);
2142 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002143 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002144 smtbl = ctx ? &ctx->localmac : &smacros;
2145 smhead = (SMacro **) hash_findi_add(smtbl, mname);
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002146 smac = nasm_zalloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002147 smac->next = *smhead;
2148 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002149 }
2150 smac->name = nasm_strdup(mname);
2151 smac->casesense = casesense;
2152 smac->nparam = nparam;
2153 smac->expansion = expansion;
2154 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002155 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002156}
2157
2158/*
2159 * Undefine an smacro
2160 */
2161static void undef_smacro(Context *ctx, const char *mname)
2162{
2163 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002164 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002165
H. Peter Anvin166c2472008-05-28 12:28:58 -07002166 smtbl = ctx ? &ctx->localmac : &smacros;
2167 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002168
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002169 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002170 /*
2171 * We now have a macro name... go hunt for it.
2172 */
2173 sp = smhead;
2174 while ((s = *sp) != NULL) {
2175 if (!mstrcmp(s->name, mname, s->casesense)) {
2176 *sp = s->next;
2177 nasm_free(s->name);
2178 free_tlist(s->expansion);
2179 nasm_free(s);
2180 } else {
2181 sp = &s->next;
2182 }
2183 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002184 }
2185}
2186
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002187/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002188 * Parse a mmacro specification.
2189 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002190static bool parse_mmacro_spec(Token *tline, ExpDef *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002191{
2192 bool err;
2193
2194 tline = tline->next;
2195 skip_white_(tline);
2196 tline = expand_id(tline);
2197 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002198 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2199 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002200 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002201
H. Peter Anvina26433d2008-07-16 14:40:01 -07002202 def->name = nasm_strdup(tline->text);
2203 def->plus = false;
2204 def->nolist = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002205// def->in_progress = 0;
2206// def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002207 def->nparam_min = 0;
2208 def->nparam_max = 0;
2209
H. Peter Anvina26433d2008-07-16 14:40:01 -07002210 tline = expand_smacro(tline->next);
2211 skip_white_(tline);
2212 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002213 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002214 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002215 def->nparam_min = def->nparam_max =
2216 readnum(tline->text, &err);
2217 if (err)
2218 error(ERR_NONFATAL,
2219 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002220 }
2221 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002222 tline = tline->next->next;
2223 if (tok_is_(tline, "*")) {
2224 def->nparam_max = INT_MAX;
2225 } else if (!tok_type_(tline, TOK_NUMBER)) {
2226 error(ERR_NONFATAL,
2227 "`%s' expects a parameter count after `-'", directive);
2228 } else {
2229 def->nparam_max = readnum(tline->text, &err);
2230 if (err) {
2231 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2232 tline->text);
2233 }
2234 if (def->nparam_min > def->nparam_max) {
2235 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2236 }
2237 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002238 }
2239 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002240 tline = tline->next;
2241 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002242 }
2243 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002244 !nasm_stricmp(tline->next->text, ".nolist")) {
2245 tline = tline->next;
2246 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002247 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002248
H. Peter Anvina26433d2008-07-16 14:40:01 -07002249 /*
2250 * Handle default parameters.
2251 */
2252 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002253 def->dlist = tline->next;
2254 tline->next = NULL;
2255 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002256 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002257 def->dlist = NULL;
2258 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002259 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002260 def->line = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002261
H. Peter Anvin89cee572009-07-15 09:16:54 -04002262 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002263 !def->plus)
2264 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2265 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002266
H. Peter Anvina26433d2008-07-16 14:40:01 -07002267 return true;
2268}
2269
2270
2271/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002272 * Decode a size directive
2273 */
2274static int parse_size(const char *str) {
2275 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002276 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002277 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002278 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002279
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002280 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002281}
2282
Ed Beroset3ab3f412002-06-11 03:31:49 +00002283/**
2284 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002285 * Find out if a line contains a preprocessor directive, and deal
2286 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002287 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002288 * If a directive _is_ found, it is the responsibility of this routine
2289 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002290 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002291 * @param tline a pointer to the current tokeninzed line linked list
2292 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002293 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002294 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002295static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002296{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002297 enum preproc_token i;
2298 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002299 bool err;
2300 int nparam;
2301 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002302 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002303 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002304 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002305 char *p, *pp;
2306 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002307 Include *inc;
2308 Context *ctx;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002309 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002310 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002311 struct tokenval tokval;
2312 expr *evalresult;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002313 ExpDef *ed, *eed, **edhead;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002314 ExpInv *ei, *eei;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002315 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002316 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002317 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002318
2319 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002320
H. Peter Anvineba20a72002-04-30 20:53:55 +00002321 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002322 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002323 (tline->text[1] == '%' || tline->text[1] == '$'
2324 || tline->text[1] == '!'))
2325 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002326
H. Peter Anvin4169a472007-09-12 01:29:43 +00002327 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002328
H. Peter Anvin4169a472007-09-12 01:29:43 +00002329 switch (i) {
2330 case PP_INVALID:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002331 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002332 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2333 tline->text);
2334 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002335
H. Peter Anvine2c80182005-01-15 22:15:51 +00002336 case PP_STACKSIZE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002337 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002338 /* Directive to tell NASM what the default stack size is. The
2339 * default is for a 16-bit stack, and this can be overriden with
2340 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002341 */
2342 tline = tline->next;
2343 if (tline && tline->type == TOK_WHITESPACE)
2344 tline = tline->next;
2345 if (!tline || tline->type != TOK_ID) {
2346 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2347 free_tlist(origline);
2348 return DIRECTIVE_FOUND;
2349 }
2350 if (nasm_stricmp(tline->text, "flat") == 0) {
2351 /* All subsequent ARG directives are for a 32-bit stack */
2352 StackSize = 4;
2353 StackPointer = "ebp";
2354 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002355 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002356 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2357 /* All subsequent ARG directives are for a 64-bit stack */
2358 StackSize = 8;
2359 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002360 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002361 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002362 } else if (nasm_stricmp(tline->text, "large") == 0) {
2363 /* All subsequent ARG directives are for a 16-bit stack,
2364 * far function call.
2365 */
2366 StackSize = 2;
2367 StackPointer = "bp";
2368 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002369 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002370 } else if (nasm_stricmp(tline->text, "small") == 0) {
2371 /* All subsequent ARG directives are for a 16-bit stack,
2372 * far function call. We don't support near functions.
2373 */
2374 StackSize = 2;
2375 StackPointer = "bp";
2376 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002377 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002378 } else {
2379 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2380 free_tlist(origline);
2381 return DIRECTIVE_FOUND;
2382 }
2383 free_tlist(origline);
2384 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002385
H. Peter Anvine2c80182005-01-15 22:15:51 +00002386 case PP_ARG:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002387 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002388 /* TASM like ARG directive to define arguments to functions, in
2389 * the following form:
2390 *
2391 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2392 */
2393 offset = ArgOffset;
2394 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002395 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002396 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002397
H. Peter Anvine2c80182005-01-15 22:15:51 +00002398 /* Find the argument name */
2399 tline = tline->next;
2400 if (tline && tline->type == TOK_WHITESPACE)
2401 tline = tline->next;
2402 if (!tline || tline->type != TOK_ID) {
2403 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2404 free_tlist(origline);
2405 return DIRECTIVE_FOUND;
2406 }
2407 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002408
H. Peter Anvine2c80182005-01-15 22:15:51 +00002409 /* Find the argument size type */
2410 tline = tline->next;
2411 if (!tline || tline->type != TOK_OTHER
2412 || tline->text[0] != ':') {
2413 error(ERR_NONFATAL,
2414 "Syntax error processing `%%arg' directive");
2415 free_tlist(origline);
2416 return DIRECTIVE_FOUND;
2417 }
2418 tline = tline->next;
2419 if (!tline || tline->type != TOK_ID) {
2420 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2421 free_tlist(origline);
2422 return DIRECTIVE_FOUND;
2423 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002424
H. Peter Anvine2c80182005-01-15 22:15:51 +00002425 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002426 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002427 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002428 size = parse_size(tt->text);
2429 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002430 error(ERR_NONFATAL,
2431 "Invalid size type for `%%arg' missing directive");
2432 free_tlist(tt);
2433 free_tlist(origline);
2434 return DIRECTIVE_FOUND;
2435 }
2436 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002437
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002438 /* Round up to even stack slots */
2439 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002440
H. Peter Anvine2c80182005-01-15 22:15:51 +00002441 /* Now define the macro for the argument */
2442 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2443 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002444 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002445 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002446
H. Peter Anvine2c80182005-01-15 22:15:51 +00002447 /* Move to the next argument in the list */
2448 tline = tline->next;
2449 if (tline && tline->type == TOK_WHITESPACE)
2450 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002451 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002452 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002453 free_tlist(origline);
2454 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002455
H. Peter Anvine2c80182005-01-15 22:15:51 +00002456 case PP_LOCAL:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002457 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002458 /* TASM like LOCAL directive to define local variables for a
2459 * function, in the following form:
2460 *
2461 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2462 *
2463 * The '= LocalSize' at the end is ignored by NASM, but is
2464 * required by TASM to define the local parameter size (and used
2465 * by the TASM macro package).
2466 */
2467 offset = LocalOffset;
2468 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002469 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002470 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002471
H. Peter Anvine2c80182005-01-15 22:15:51 +00002472 /* Find the argument name */
2473 tline = tline->next;
2474 if (tline && tline->type == TOK_WHITESPACE)
2475 tline = tline->next;
2476 if (!tline || tline->type != TOK_ID) {
2477 error(ERR_NONFATAL,
2478 "`%%local' missing argument parameter");
2479 free_tlist(origline);
2480 return DIRECTIVE_FOUND;
2481 }
2482 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002483
H. Peter Anvine2c80182005-01-15 22:15:51 +00002484 /* Find the argument size type */
2485 tline = tline->next;
2486 if (!tline || tline->type != TOK_OTHER
2487 || tline->text[0] != ':') {
2488 error(ERR_NONFATAL,
2489 "Syntax error processing `%%local' directive");
2490 free_tlist(origline);
2491 return DIRECTIVE_FOUND;
2492 }
2493 tline = tline->next;
2494 if (!tline || tline->type != TOK_ID) {
2495 error(ERR_NONFATAL,
2496 "`%%local' missing size type parameter");
2497 free_tlist(origline);
2498 return DIRECTIVE_FOUND;
2499 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002500
H. Peter Anvine2c80182005-01-15 22:15:51 +00002501 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002502 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002503 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002504 size = parse_size(tt->text);
2505 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002506 error(ERR_NONFATAL,
2507 "Invalid size type for `%%local' missing directive");
2508 free_tlist(tt);
2509 free_tlist(origline);
2510 return DIRECTIVE_FOUND;
2511 }
2512 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002513
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002514 /* Round up to even stack slots */
2515 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002516
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002517 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002518
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002519 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002520 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2521 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002522 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002523
H. Peter Anvine2c80182005-01-15 22:15:51 +00002524 /* Now define the assign to setup the enter_c macro correctly */
2525 snprintf(directive, sizeof(directive),
2526 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002527 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002528
H. Peter Anvine2c80182005-01-15 22:15:51 +00002529 /* Move to the next argument in the list */
2530 tline = tline->next;
2531 if (tline && tline->type == TOK_WHITESPACE)
2532 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002533 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002534 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002535 free_tlist(origline);
2536 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002537
H. Peter Anvine2c80182005-01-15 22:15:51 +00002538 case PP_CLEAR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002539 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002540 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002541 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002542 "trailing garbage after `%%clear' ignored");
2543 free_macros();
2544 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002545 free_tlist(origline);
2546 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002547
H. Peter Anvin418ca702008-05-30 10:42:30 -07002548 case PP_DEPEND:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002549 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002550 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002551 skip_white_(t);
2552 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002553 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002554 error(ERR_NONFATAL, "`%%depend' expects a file name");
2555 free_tlist(origline);
2556 return DIRECTIVE_FOUND; /* but we did _something_ */
2557 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002558 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002559 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002560 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002561 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002562 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002563 nasm_unquote_cstr(p, i);
2564 if (dephead && !in_list(*dephead, p)) {
2565 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2566 sl->next = NULL;
2567 strcpy(sl->str, p);
2568 *deptail = sl;
2569 deptail = &sl->next;
2570 }
2571 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002572 return DIRECTIVE_FOUND;
2573
2574 case PP_INCLUDE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002575 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002576 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002577 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002578
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002579 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002580 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002581 error(ERR_NONFATAL, "`%%include' expects a file name");
2582 free_tlist(origline);
2583 return DIRECTIVE_FOUND; /* but we did _something_ */
2584 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002585 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002586 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002587 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002588 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002589 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002590 nasm_unquote_cstr(p, i);
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03002591 inc = nasm_zalloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002592 inc->next = istk;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002593 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002594 if (!inc->fp) {
2595 /* -MG given but file not found */
2596 nasm_free(inc);
2597 } else {
2598 inc->fname = src_set_fname(nasm_strdup(p));
2599 inc->lineno = src_set_linnum(0);
2600 inc->lineinc = 1;
2601 inc->expansion = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002602 istk = inc;
2603 list->uplevel(LIST_INCLUDE);
2604 }
2605 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002606 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002607
H. Peter Anvind2456592008-06-19 15:04:18 -07002608 case PP_USE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002609 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002610 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002611 static macros_t *use_pkg;
2612 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002613
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002614 tline = tline->next;
2615 skip_white_(tline);
2616 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002617
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002618 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002619 tline->type != TOK_INTERNAL_STRING &&
2620 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002621 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002622 free_tlist(origline);
2623 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002624 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002625 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002626 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002627 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002628 if (tline->type == TOK_STRING)
2629 nasm_unquote_cstr(tline->text, i);
2630 use_pkg = nasm_stdmac_find_package(tline->text);
2631 if (!use_pkg)
2632 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2633 else
2634 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002635 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002636 /* Not already included, go ahead and include it */
2637 stdmacpos = use_pkg;
2638 }
2639 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002640 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002641 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002642 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002643 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002644 case PP_POP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002645 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002646 tline = tline->next;
2647 skip_white_(tline);
2648 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002649 if (tline) {
2650 if (!tok_type_(tline, TOK_ID)) {
2651 error(ERR_NONFATAL, "`%s' expects a context identifier",
2652 pp_directives[i]);
2653 free_tlist(origline);
2654 return DIRECTIVE_FOUND; /* but we did _something_ */
2655 }
2656 if (tline->next)
2657 error(ERR_WARNING|ERR_PASS1,
2658 "trailing garbage after `%s' ignored",
2659 pp_directives[i]);
2660 p = nasm_strdup(tline->text);
2661 } else {
2662 p = NULL; /* Anonymous */
2663 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002664
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002665 if (i == PP_PUSH) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002666 ctx = nasm_zalloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002667 ctx->next = cstk;
2668 hash_init(&ctx->localmac, HASH_SMALL);
2669 ctx->name = p;
2670 ctx->number = unique++;
2671 cstk = ctx;
2672 } else {
2673 /* %pop or %repl */
2674 if (!cstk) {
2675 error(ERR_NONFATAL, "`%s': context stack is empty",
2676 pp_directives[i]);
2677 } else if (i == PP_POP) {
2678 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2679 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2680 "expected %s",
2681 cstk->name ? cstk->name : "anonymous", p);
2682 else
2683 ctx_pop();
2684 } else {
2685 /* i == PP_REPL */
2686 nasm_free(cstk->name);
2687 cstk->name = p;
2688 p = NULL;
2689 }
2690 nasm_free(p);
2691 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002692 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002693 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002694 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002695 severity = ERR_FATAL;
2696 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002697 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002698 severity = ERR_NONFATAL;
2699 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002700 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002701 severity = ERR_WARNING|ERR_WARN_USER;
2702 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002703
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002704issue_error:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002705 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002706 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002707 /* Only error out if this is the final pass */
2708 if (pass != 2 && i != PP_FATAL)
2709 return DIRECTIVE_FOUND;
2710
2711 tline->next = expand_smacro(tline->next);
2712 tline = tline->next;
2713 skip_white_(tline);
2714 t = tline ? tline->next : NULL;
2715 skip_white_(t);
2716 if (tok_type_(tline, TOK_STRING) && !t) {
2717 /* The line contains only a quoted string */
2718 p = tline->text;
2719 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2720 error(severity, "%s", p);
2721 } else {
2722 /* Not a quoted string, or more than a quoted string */
2723 p = detoken(tline, false);
2724 error(severity, "%s", p);
2725 nasm_free(p);
2726 }
2727 free_tlist(origline);
2728 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002729 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002730
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002731 CASE_PP_IF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002732 if (defining != NULL) {
2733 if (defining->type == EXP_IF) {
2734 defining->def_depth ++;
2735 }
2736 return NO_DIRECTIVE_FOUND;
2737 }
2738 if ((istk->expansion != NULL) &&
2739 (istk->expansion->emitting == false)) {
2740 j = COND_NEVER;
2741 } else {
2742 j = if_condition(tline->next, i);
2743 tline->next = NULL; /* it got freed */
2744 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
2745 }
2746 ed = new_ExpDef(EXP_IF);
2747 ed->state = j;
2748 ed->nolist = NULL;
2749 ed->def_depth = 0;
2750 ed->cur_depth = 0;
2751 ed->max_depth = 0;
2752 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
2753 ed->prev = defining;
2754 defining = ed;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002755 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002756 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002757
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002758 CASE_PP_ELIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002759 if (defining != NULL) {
2760 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2761 return NO_DIRECTIVE_FOUND;
2762 }
2763 }
2764 if ((defining == NULL) || (defining->type != EXP_IF)) {
2765 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2766 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002767 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002768 case COND_IF_TRUE:
2769 defining->state = COND_DONE;
2770 defining->ignoring = true;
2771 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002772
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002773 case COND_DONE:
2774 case COND_NEVER:
2775 defining->ignoring = true;
2776 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002777
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002778 case COND_ELSE_TRUE:
2779 case COND_ELSE_FALSE:
2780 error_precond(ERR_WARNING|ERR_PASS1,
2781 "`%%elif' after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002782 defining->state = COND_NEVER;
2783 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002784 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002785
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002786 case COND_IF_FALSE:
2787 /*
2788 * IMPORTANT: In the case of %if, we will already have
2789 * called expand_mmac_params(); however, if we're
2790 * processing an %elif we must have been in a
2791 * non-emitting mode, which would have inhibited
2792 * the normal invocation of expand_mmac_params().
2793 * Therefore, we have to do it explicitly here.
2794 */
2795 j = if_condition(expand_mmac_params(tline->next), i);
2796 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002797 defining->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002798 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002799 defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002800 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002801 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002802 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002803 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002804
H. Peter Anvine2c80182005-01-15 22:15:51 +00002805 case PP_ELSE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002806 if (defining != NULL) {
2807 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2808 return NO_DIRECTIVE_FOUND;
2809 }
2810 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002811 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002812 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002813 "trailing garbage after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002814 if ((defining == NULL) || (defining->type != EXP_IF)) {
2815 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2816 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002817 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002818 case COND_IF_TRUE:
2819 case COND_DONE:
2820 defining->state = COND_ELSE_FALSE;
2821 defining->ignoring = true;
2822 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002823
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002824 case COND_NEVER:
2825 defining->ignoring = true;
2826 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002827
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002828 case COND_IF_FALSE:
2829 defining->state = COND_ELSE_TRUE;
2830 defining->ignoring = false;
2831 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002832
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002833 case COND_ELSE_TRUE:
2834 case COND_ELSE_FALSE:
2835 error_precond(ERR_WARNING|ERR_PASS1,
2836 "`%%else' after `%%else' ignored.");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002837 defining->state = COND_NEVER;
2838 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002839 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002840 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002841 free_tlist(origline);
2842 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002843
H. Peter Anvine2c80182005-01-15 22:15:51 +00002844 case PP_ENDIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002845 if (defining != NULL) {
2846 if (defining->type == EXP_IF) {
2847 if (defining->def_depth > 0) {
2848 defining->def_depth --;
2849 return NO_DIRECTIVE_FOUND;
2850 }
2851 } else {
2852 return NO_DIRECTIVE_FOUND;
2853 }
2854 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002855 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002856 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002857 "trailing garbage after `%%endif' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002858 if ((defining == NULL) || (defining->type != EXP_IF)) {
2859 error(ERR_NONFATAL, "`%%endif': no matching `%%if'");
2860 return DIRECTIVE_FOUND;
2861 }
2862 ed = defining;
2863 defining = ed->prev;
2864 ed->prev = expansions;
2865 expansions = ed;
2866 ei = new_ExpInv(EXP_IF, ed);
2867 ei->current = ed->line;
2868 ei->emitting = true;
2869 ei->prev = istk->expansion;
2870 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002871 free_tlist(origline);
2872 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002873
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002874 case PP_RMACRO:
2875 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002876 case PP_MACRO:
2877 case PP_IMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002878 if (defining != NULL) {
2879 if (defining->type == EXP_MMACRO) {
2880 defining->def_depth ++;
2881 }
2882 return NO_DIRECTIVE_FOUND;
2883 }
2884 ed = new_ExpDef(EXP_MMACRO);
2885 ed->max_depth =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002886 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002887 ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002888 if (!parse_mmacro_spec(tline, ed, pp_directives[i])) {
2889 nasm_free(ed);
2890 ed = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002891 return DIRECTIVE_FOUND;
2892 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002893 ed->def_depth = 0;
2894 ed->cur_depth = 0;
2895 ed->max_depth = (ed->max_depth + 1);
2896 ed->ignoring = false;
2897 ed->prev = defining;
2898 defining = ed;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002899
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002900 eed = (ExpDef *) hash_findix(&expdefs, ed->name);
2901 while (eed) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002902 if (!strcmp(eed->name, ed->name) &&
2903 (eed->nparam_min <= ed->nparam_max || ed->plus) &&
2904 (ed->nparam_min <= eed->nparam_max || eed->plus)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002905 error(ERR_WARNING|ERR_PASS1,
2906 "redefining multi-line macro `%s'", ed->name);
2907 return DIRECTIVE_FOUND;
2908 }
2909 eed = eed->next;
2910 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002911 free_tlist(origline);
2912 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002913
H. Peter Anvine2c80182005-01-15 22:15:51 +00002914 case PP_ENDM:
2915 case PP_ENDMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002916 if (defining != NULL) {
2917 if (defining->type == EXP_MMACRO) {
2918 if (defining->def_depth > 0) {
2919 defining->def_depth --;
2920 return NO_DIRECTIVE_FOUND;
2921 }
2922 } else {
2923 return NO_DIRECTIVE_FOUND;
2924 }
2925 }
2926 if (!(defining) || (defining->type != EXP_MMACRO)) {
2927 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2928 return DIRECTIVE_FOUND;
2929 }
2930 edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name);
2931 defining->next = *edhead;
2932 *edhead = defining;
2933 ed = defining;
2934 defining = ed->prev;
2935 ed->prev = expansions;
2936 expansions = ed;
2937 ed = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002938 free_tlist(origline);
2939 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002940
H. Peter Anvin89cee572009-07-15 09:16:54 -04002941 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002942 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2943 /*
2944 * We must search along istk->expansion until we hit a
2945 * macro invocation. Then we disable the emitting state(s)
2946 * between exitmacro and endmacro.
2947 */
2948 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
2949 if(ei->type == EXP_MMACRO) {
2950 break;
2951 }
2952 }
2953
2954 if (ei != NULL) {
2955 /*
2956 * Set all invocations leading back to the macro
2957 * invocation to a non-emitting state.
2958 */
2959 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
2960 eei->emitting = false;
2961 }
2962 eei->emitting = false;
2963 } else {
2964 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2965 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002966 free_tlist(origline);
2967 return DIRECTIVE_FOUND;
2968
H. Peter Anvina26433d2008-07-16 14:40:01 -07002969 case PP_UNMACRO:
2970 case PP_UNIMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002971 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002972 {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002973 ExpDef **ed_p;
2974 ExpDef spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002975
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002976 spec.casesense = (i == PP_UNMACRO);
2977 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2978 return DIRECTIVE_FOUND;
2979 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002980 ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL);
2981 while (ed_p && *ed_p) {
2982 ed = *ed_p;
2983 if (ed->casesense == spec.casesense &&
2984 !mstrcmp(ed->name, spec.name, spec.casesense) &&
2985 ed->nparam_min == spec.nparam_min &&
2986 ed->nparam_max == spec.nparam_max &&
2987 ed->plus == spec.plus) {
Keith Kaniosc98a5b42010-12-18 12:17:31 -06002988 if (ed->cur_depth > 0) {
Keith Kanios21d885b2010-12-18 12:22:21 -06002989 error(ERR_NONFATAL, "`%s' ignored on active macro",
Keith Kaniosc98a5b42010-12-18 12:17:31 -06002990 pp_directives[i]);
2991 break;
2992 } else {
2993 *ed_p = ed->next;
2994 free_expdef(ed);
2995 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002996 } else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002997 ed_p = &ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002998 }
2999 }
3000 free_tlist(origline);
3001 free_tlist(spec.dlist);
3002 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003003 }
3004
H. Peter Anvine2c80182005-01-15 22:15:51 +00003005 case PP_ROTATE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003006 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003007 if (tline->next && tline->next->type == TOK_WHITESPACE)
3008 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003009 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003010 free_tlist(origline);
3011 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
3012 return DIRECTIVE_FOUND;
3013 }
3014 t = expand_smacro(tline->next);
3015 tline->next = NULL;
3016 free_tlist(origline);
3017 tline = t;
3018 tptr = &t;
3019 tokval.t_type = TOKEN_INVALID;
3020 evalresult =
3021 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3022 free_tlist(tline);
3023 if (!evalresult)
3024 return DIRECTIVE_FOUND;
3025 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003026 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003027 "trailing garbage after expression ignored");
3028 if (!is_simple(evalresult)) {
3029 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
3030 return DIRECTIVE_FOUND;
3031 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003032 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3033 if (ei->type == EXP_MMACRO) {
3034 break;
3035 }
3036 }
3037 if (ei == NULL) {
3038 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
3039 } else if (ei->nparam == 0) {
3040 error(ERR_NONFATAL,
3041 "`%%rotate' invoked within macro without parameters");
3042 } else {
3043 int rotate = ei->rotate + reloc_value(evalresult);
3044
3045 rotate %= (int)ei->nparam;
3046 if (rotate < 0)
3047 rotate += ei->nparam;
3048 ei->rotate = rotate;
3049 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003050 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003051
H. Peter Anvine2c80182005-01-15 22:15:51 +00003052 case PP_REP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003053 if (defining != NULL) {
3054 if (defining->type == EXP_REP) {
3055 defining->def_depth ++;
3056 }
3057 return NO_DIRECTIVE_FOUND;
3058 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003059 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003060 do {
3061 tline = tline->next;
3062 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003063
H. Peter Anvine2c80182005-01-15 22:15:51 +00003064 if (tok_type_(tline, TOK_ID) &&
3065 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003066 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003067 do {
3068 tline = tline->next;
3069 } while (tok_type_(tline, TOK_WHITESPACE));
3070 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003071
H. Peter Anvine2c80182005-01-15 22:15:51 +00003072 if (tline) {
3073 t = expand_smacro(tline);
3074 tptr = &t;
3075 tokval.t_type = TOKEN_INVALID;
3076 evalresult =
3077 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3078 if (!evalresult) {
3079 free_tlist(origline);
3080 return DIRECTIVE_FOUND;
3081 }
3082 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003083 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003084 "trailing garbage after expression ignored");
3085 if (!is_simple(evalresult)) {
3086 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
3087 return DIRECTIVE_FOUND;
3088 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003089 count = reloc_value(evalresult);
3090 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04003091 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003092 count = 0;
3093 } else
3094 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003095 } else {
3096 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003097 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003098 }
3099 free_tlist(origline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003100 ed = new_ExpDef(EXP_REP);
3101 ed->nolist = nolist;
3102 ed->def_depth = 0;
3103 ed->cur_depth = 1;
3104 ed->max_depth = (count - 1);
3105 ed->ignoring = false;
3106 ed->prev = defining;
3107 defining = ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003108 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003109
H. Peter Anvine2c80182005-01-15 22:15:51 +00003110 case PP_ENDREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003111 if (defining != NULL) {
3112 if (defining->type == EXP_REP) {
3113 if (defining->def_depth > 0) {
3114 defining->def_depth --;
3115 return NO_DIRECTIVE_FOUND;
3116 }
3117 } else {
3118 return NO_DIRECTIVE_FOUND;
3119 }
3120 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003121 if ((defining == NULL) || (defining->type != EXP_REP)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003122 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
3123 return DIRECTIVE_FOUND;
3124 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003125
H. Peter Anvine2c80182005-01-15 22:15:51 +00003126 /*
3127 * Now we have a "macro" defined - although it has no name
3128 * and we won't be entering it in the hash tables - we must
3129 * push a macro-end marker for it on to istk->expansion.
3130 * After that, it will take care of propagating itself (a
3131 * macro-end marker line for a macro which is really a %rep
3132 * block will cause the macro to be re-expanded, complete
3133 * with another macro-end marker to ensure the process
3134 * continues) until the whole expansion is forcibly removed
3135 * from istk->expansion by a %exitrep.
3136 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003137 ed = defining;
3138 defining = ed->prev;
3139 ed->prev = expansions;
3140 expansions = ed;
3141 ei = new_ExpInv(EXP_REP, ed);
3142 ei->current = ed->line;
3143 ei->emitting = ((ed->max_depth > 0) ? true : false);
3144 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3145 ei->prev = istk->expansion;
3146 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003147 free_tlist(origline);
3148 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003149
H. Peter Anvine2c80182005-01-15 22:15:51 +00003150 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003151 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3152 /*
3153 * We must search along istk->expansion until we hit a
3154 * rep invocation. Then we disable the emitting state(s)
3155 * between exitrep and endrep.
3156 */
3157 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3158 if (ei->type == EXP_REP) {
3159 break;
3160 }
3161 }
3162
3163 if (ei != NULL) {
3164 /*
3165 * Set all invocations leading back to the rep
3166 * invocation to a non-emitting state.
3167 */
3168 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3169 eei->emitting = false;
3170 }
3171 eei->emitting = false;
3172 eei->current = NULL;
3173 eei->def->cur_depth = eei->def->max_depth;
3174 } else {
3175 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3176 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003177 free_tlist(origline);
3178 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003179
H. Peter Anvine2c80182005-01-15 22:15:51 +00003180 case PP_XDEFINE:
3181 case PP_IXDEFINE:
3182 case PP_DEFINE:
3183 case PP_IDEFINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003184 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003185 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003186
H. Peter Anvine2c80182005-01-15 22:15:51 +00003187 tline = tline->next;
3188 skip_white_(tline);
3189 tline = expand_id(tline);
3190 if (!tline || (tline->type != TOK_ID &&
3191 (tline->type != TOK_PREPROC_ID ||
3192 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003193 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003194 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003195 free_tlist(origline);
3196 return DIRECTIVE_FOUND;
3197 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003198
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003199 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003200 last = tline;
3201 param_start = tline = tline->next;
3202 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003203
H. Peter Anvine2c80182005-01-15 22:15:51 +00003204 /* Expand the macro definition now for %xdefine and %ixdefine */
3205 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3206 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003207
H. Peter Anvine2c80182005-01-15 22:15:51 +00003208 if (tok_is_(tline, "(")) {
3209 /*
3210 * This macro has parameters.
3211 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003212
H. Peter Anvine2c80182005-01-15 22:15:51 +00003213 tline = tline->next;
3214 while (1) {
3215 skip_white_(tline);
3216 if (!tline) {
3217 error(ERR_NONFATAL, "parameter identifier expected");
3218 free_tlist(origline);
3219 return DIRECTIVE_FOUND;
3220 }
3221 if (tline->type != TOK_ID) {
3222 error(ERR_NONFATAL,
3223 "`%s': parameter identifier expected",
3224 tline->text);
3225 free_tlist(origline);
3226 return DIRECTIVE_FOUND;
3227 }
3228 tline->type = TOK_SMAC_PARAM + nparam++;
3229 tline = tline->next;
3230 skip_white_(tline);
3231 if (tok_is_(tline, ",")) {
3232 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003233 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003234 if (!tok_is_(tline, ")")) {
3235 error(ERR_NONFATAL,
3236 "`)' expected to terminate macro template");
3237 free_tlist(origline);
3238 return DIRECTIVE_FOUND;
3239 }
3240 break;
3241 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003242 }
3243 last = tline;
3244 tline = tline->next;
3245 }
3246 if (tok_type_(tline, TOK_WHITESPACE))
3247 last = tline, tline = tline->next;
3248 macro_start = NULL;
3249 last->next = NULL;
3250 t = tline;
3251 while (t) {
3252 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003253 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003254 if (tt->type >= TOK_SMAC_PARAM &&
3255 !strcmp(tt->text, t->text))
3256 t->type = tt->type;
3257 }
3258 tt = t->next;
3259 t->next = macro_start;
3260 macro_start = t;
3261 t = tt;
3262 }
3263 /*
3264 * Good. We now have a macro name, a parameter count, and a
3265 * token list (in reverse order) for an expansion. We ought
3266 * to be OK just to create an SMacro, store it, and let
3267 * free_tlist have the rest of the line (which we have
3268 * carefully re-terminated after chopping off the expansion
3269 * from the end).
3270 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003271 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003272 free_tlist(origline);
3273 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003274
H. Peter Anvine2c80182005-01-15 22:15:51 +00003275 case PP_UNDEF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003276 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003277 tline = tline->next;
3278 skip_white_(tline);
3279 tline = expand_id(tline);
3280 if (!tline || (tline->type != TOK_ID &&
3281 (tline->type != TOK_PREPROC_ID ||
3282 tline->text[1] != '$'))) {
3283 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3284 free_tlist(origline);
3285 return DIRECTIVE_FOUND;
3286 }
3287 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003288 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003289 "trailing garbage after macro name ignored");
3290 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003291
H. Peter Anvine2c80182005-01-15 22:15:51 +00003292 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003293 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003294 undef_smacro(ctx, mname);
3295 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003296 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003297
H. Peter Anvin9e200162008-06-04 17:23:14 -07003298 case PP_DEFSTR:
3299 case PP_IDEFSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003300 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003301 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003302
3303 tline = tline->next;
3304 skip_white_(tline);
3305 tline = expand_id(tline);
3306 if (!tline || (tline->type != TOK_ID &&
3307 (tline->type != TOK_PREPROC_ID ||
3308 tline->text[1] != '$'))) {
3309 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003310 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003311 free_tlist(origline);
3312 return DIRECTIVE_FOUND;
3313 }
3314
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003315 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003316 last = tline;
3317 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003318 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003319
3320 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003321 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003322
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003323 p = detoken(tline, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003324 macro_start = nasm_malloc(sizeof(*macro_start));
3325 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003326 macro_start->text = nasm_quote(p, strlen(p));
3327 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003328 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003329 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003330
3331 /*
3332 * We now have a macro name, an implicit parameter count of
3333 * zero, and a string token to use as an expansion. Create
3334 * and store an SMacro.
3335 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003336 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003337 free_tlist(origline);
3338 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003339
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003340 case PP_DEFTOK:
3341 case PP_IDEFTOK:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003342 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003343 casesense = (i == PP_DEFTOK);
3344
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003345 tline = tline->next;
3346 skip_white_(tline);
3347 tline = expand_id(tline);
3348 if (!tline || (tline->type != TOK_ID &&
3349 (tline->type != TOK_PREPROC_ID ||
3350 tline->text[1] != '$'))) {
3351 error(ERR_NONFATAL,
3352 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003353 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003354 free_tlist(origline);
3355 return DIRECTIVE_FOUND;
3356 }
3357 ctx = get_ctx(tline->text, &mname, false);
3358 last = tline;
3359 tline = expand_smacro(tline->next);
3360 last->next = NULL;
3361
3362 t = tline;
3363 while (tok_type_(t, TOK_WHITESPACE))
3364 t = t->next;
3365 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003366 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003367 error(ERR_NONFATAL,
3368 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003369 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003370 free_tlist(tline);
3371 free_tlist(origline);
3372 return DIRECTIVE_FOUND;
3373 }
3374
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003375 /*
3376 * Convert the string to a token stream. Note that smacros
3377 * are stored with the token stream reversed, so we have to
3378 * reverse the output of tokenize().
3379 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003380 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003381 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003382
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003383 /*
3384 * We now have a macro name, an implicit parameter count of
3385 * zero, and a numeric token to use as an expansion. Create
3386 * and store an SMacro.
3387 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003388 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003389 free_tlist(tline);
3390 free_tlist(origline);
3391 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003392
H. Peter Anvin418ca702008-05-30 10:42:30 -07003393 case PP_PATHSEARCH:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003394 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003395 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003396 FILE *fp;
3397 StrList *xsl = NULL;
3398 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003399
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003400 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003401
3402 tline = tline->next;
3403 skip_white_(tline);
3404 tline = expand_id(tline);
3405 if (!tline || (tline->type != TOK_ID &&
3406 (tline->type != TOK_PREPROC_ID ||
3407 tline->text[1] != '$'))) {
3408 error(ERR_NONFATAL,
3409 "`%%pathsearch' expects a macro identifier as first parameter");
3410 free_tlist(origline);
3411 return DIRECTIVE_FOUND;
3412 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003413 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003414 last = tline;
3415 tline = expand_smacro(tline->next);
3416 last->next = NULL;
3417
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003418 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003419 while (tok_type_(t, TOK_WHITESPACE))
3420 t = t->next;
3421
3422 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003423 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003424 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003425 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003426 free_tlist(origline);
3427 return DIRECTIVE_FOUND; /* but we did _something_ */
3428 }
3429 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003430 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003431 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003432 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003433 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003434 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003435
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003436 fp = inc_fopen(p, &xsl, &xst, true);
3437 if (fp) {
3438 p = xsl->str;
3439 fclose(fp); /* Don't actually care about the file */
3440 }
H. Peter Anvin418ca702008-05-30 10:42:30 -07003441 macro_start = nasm_malloc(sizeof(*macro_start));
3442 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003443 macro_start->text = nasm_quote(p, strlen(p));
3444 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003445 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003446 if (xsl)
3447 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003448
3449 /*
3450 * We now have a macro name, an implicit parameter count of
3451 * zero, and a string token to use as an expansion. Create
3452 * and store an SMacro.
3453 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003454 define_smacro(ctx, mname, casesense, 0, macro_start);
3455 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003456 free_tlist(origline);
3457 return DIRECTIVE_FOUND;
3458 }
3459
H. Peter Anvine2c80182005-01-15 22:15:51 +00003460 case PP_STRLEN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003461 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003462 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003463
H. Peter Anvine2c80182005-01-15 22:15:51 +00003464 tline = tline->next;
3465 skip_white_(tline);
3466 tline = expand_id(tline);
3467 if (!tline || (tline->type != TOK_ID &&
3468 (tline->type != TOK_PREPROC_ID ||
3469 tline->text[1] != '$'))) {
3470 error(ERR_NONFATAL,
3471 "`%%strlen' expects a macro identifier as first parameter");
3472 free_tlist(origline);
3473 return DIRECTIVE_FOUND;
3474 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003475 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003476 last = tline;
3477 tline = expand_smacro(tline->next);
3478 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003479
H. Peter Anvine2c80182005-01-15 22:15:51 +00003480 t = tline;
3481 while (tok_type_(t, TOK_WHITESPACE))
3482 t = t->next;
3483 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003484 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003485 error(ERR_NONFATAL,
3486 "`%%strlen` requires string as second parameter");
3487 free_tlist(tline);
3488 free_tlist(origline);
3489 return DIRECTIVE_FOUND;
3490 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003491
H. Peter Anvine2c80182005-01-15 22:15:51 +00003492 macro_start = nasm_malloc(sizeof(*macro_start));
3493 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003494 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003495 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003496
H. Peter Anvine2c80182005-01-15 22:15:51 +00003497 /*
3498 * We now have a macro name, an implicit parameter count of
3499 * zero, and a numeric token to use as an expansion. Create
3500 * and store an SMacro.
3501 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003502 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003503 free_tlist(tline);
3504 free_tlist(origline);
3505 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003506
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003507 case PP_STRCAT:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003508 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003509 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003510
3511 tline = tline->next;
3512 skip_white_(tline);
3513 tline = expand_id(tline);
3514 if (!tline || (tline->type != TOK_ID &&
3515 (tline->type != TOK_PREPROC_ID ||
3516 tline->text[1] != '$'))) {
3517 error(ERR_NONFATAL,
3518 "`%%strcat' expects a macro identifier as first parameter");
3519 free_tlist(origline);
3520 return DIRECTIVE_FOUND;
3521 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003522 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003523 last = tline;
3524 tline = expand_smacro(tline->next);
3525 last->next = NULL;
3526
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003527 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003528 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003529 switch (t->type) {
3530 case TOK_WHITESPACE:
3531 break;
3532 case TOK_STRING:
3533 len += t->a.len = nasm_unquote(t->text, NULL);
3534 break;
3535 case TOK_OTHER:
3536 if (!strcmp(t->text, ",")) /* permit comma separators */
3537 break;
3538 /* else fall through */
3539 default:
3540 error(ERR_NONFATAL,
3541 "non-string passed to `%%strcat' (%d)", t->type);
3542 free_tlist(tline);
3543 free_tlist(origline);
3544 return DIRECTIVE_FOUND;
3545 }
3546 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003547
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003548 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003549 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003550 if (t->type == TOK_STRING) {
3551 memcpy(p, t->text, t->a.len);
3552 p += t->a.len;
3553 }
3554 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003555
3556 /*
3557 * We now have a macro name, an implicit parameter count of
3558 * zero, and a numeric token to use as an expansion. Create
3559 * and store an SMacro.
3560 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003561 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3562 macro_start->text = nasm_quote(pp, len);
3563 nasm_free(pp);
3564 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003565 free_tlist(tline);
3566 free_tlist(origline);
3567 return DIRECTIVE_FOUND;
3568
H. Peter Anvine2c80182005-01-15 22:15:51 +00003569 case PP_SUBSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003570 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003571 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003572 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003573 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003574
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003575 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003576
H. Peter Anvine2c80182005-01-15 22:15:51 +00003577 tline = tline->next;
3578 skip_white_(tline);
3579 tline = expand_id(tline);
3580 if (!tline || (tline->type != TOK_ID &&
3581 (tline->type != TOK_PREPROC_ID ||
3582 tline->text[1] != '$'))) {
3583 error(ERR_NONFATAL,
3584 "`%%substr' expects a macro identifier as first parameter");
3585 free_tlist(origline);
3586 return DIRECTIVE_FOUND;
3587 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003588 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003589 last = tline;
3590 tline = expand_smacro(tline->next);
3591 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003592
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003593 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003594 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003595 while (tok_type_(t, TOK_WHITESPACE))
3596 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003597
H. Peter Anvine2c80182005-01-15 22:15:51 +00003598 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003599 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003600 error(ERR_NONFATAL,
3601 "`%%substr` requires string as second parameter");
3602 free_tlist(tline);
3603 free_tlist(origline);
3604 return DIRECTIVE_FOUND;
3605 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003606
H. Peter Anvine2c80182005-01-15 22:15:51 +00003607 tt = t->next;
3608 tptr = &tt;
3609 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003610 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003611 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003612 if (!evalresult) {
3613 free_tlist(tline);
3614 free_tlist(origline);
3615 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003616 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003617 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3618 free_tlist(tline);
3619 free_tlist(origline);
3620 return DIRECTIVE_FOUND;
3621 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003622 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003623
3624 while (tok_type_(tt, TOK_WHITESPACE))
3625 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003626 if (!tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003627 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003628 } else {
3629 tokval.t_type = TOKEN_INVALID;
3630 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3631 pass, error, NULL);
3632 if (!evalresult) {
3633 free_tlist(tline);
3634 free_tlist(origline);
3635 return DIRECTIVE_FOUND;
3636 } else if (!is_simple(evalresult)) {
3637 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3638 free_tlist(tline);
3639 free_tlist(origline);
3640 return DIRECTIVE_FOUND;
3641 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003642 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003643 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003644
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003645 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003646 /* make start and count being in range */
3647 if (start < 0)
3648 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003649 if (count < 0)
3650 count = len + count + 1 - start;
3651 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003652 count = len - start;
3653 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003654 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003655
H. Peter Anvine2c80182005-01-15 22:15:51 +00003656 macro_start = nasm_malloc(sizeof(*macro_start));
3657 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003658 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003659 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003660 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003661
H. Peter Anvine2c80182005-01-15 22:15:51 +00003662 /*
3663 * We now have a macro name, an implicit parameter count of
3664 * zero, and a numeric token to use as an expansion. Create
3665 * and store an SMacro.
3666 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003667 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003668 free_tlist(tline);
3669 free_tlist(origline);
3670 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003671 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003672
H. Peter Anvine2c80182005-01-15 22:15:51 +00003673 case PP_ASSIGN:
3674 case PP_IASSIGN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003675 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003676 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003677
H. Peter Anvine2c80182005-01-15 22:15:51 +00003678 tline = tline->next;
3679 skip_white_(tline);
3680 tline = expand_id(tline);
3681 if (!tline || (tline->type != TOK_ID &&
3682 (tline->type != TOK_PREPROC_ID ||
3683 tline->text[1] != '$'))) {
3684 error(ERR_NONFATAL,
3685 "`%%%sassign' expects a macro identifier",
3686 (i == PP_IASSIGN ? "i" : ""));
3687 free_tlist(origline);
3688 return DIRECTIVE_FOUND;
3689 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003690 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003691 last = tline;
3692 tline = expand_smacro(tline->next);
3693 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003694
H. Peter Anvine2c80182005-01-15 22:15:51 +00003695 t = tline;
3696 tptr = &t;
3697 tokval.t_type = TOKEN_INVALID;
3698 evalresult =
3699 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3700 free_tlist(tline);
3701 if (!evalresult) {
3702 free_tlist(origline);
3703 return DIRECTIVE_FOUND;
3704 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003705
H. Peter Anvine2c80182005-01-15 22:15:51 +00003706 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003707 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003708 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003709
H. Peter Anvine2c80182005-01-15 22:15:51 +00003710 if (!is_simple(evalresult)) {
3711 error(ERR_NONFATAL,
3712 "non-constant value given to `%%%sassign'",
3713 (i == PP_IASSIGN ? "i" : ""));
3714 free_tlist(origline);
3715 return DIRECTIVE_FOUND;
3716 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003717
H. Peter Anvine2c80182005-01-15 22:15:51 +00003718 macro_start = nasm_malloc(sizeof(*macro_start));
3719 macro_start->next = NULL;
3720 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003721 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003722
H. Peter Anvine2c80182005-01-15 22:15:51 +00003723 /*
3724 * We now have a macro name, an implicit parameter count of
3725 * zero, and a numeric token to use as an expansion. Create
3726 * and store an SMacro.
3727 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003728 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003729 free_tlist(origline);
3730 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003731
H. Peter Anvine2c80182005-01-15 22:15:51 +00003732 case PP_LINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003733 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003734 /*
3735 * Syntax is `%line nnn[+mmm] [filename]'
3736 */
3737 tline = tline->next;
3738 skip_white_(tline);
3739 if (!tok_type_(tline, TOK_NUMBER)) {
3740 error(ERR_NONFATAL, "`%%line' expects line number");
3741 free_tlist(origline);
3742 return DIRECTIVE_FOUND;
3743 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003744 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003745 m = 1;
3746 tline = tline->next;
3747 if (tok_is_(tline, "+")) {
3748 tline = tline->next;
3749 if (!tok_type_(tline, TOK_NUMBER)) {
3750 error(ERR_NONFATAL, "`%%line' expects line increment");
3751 free_tlist(origline);
3752 return DIRECTIVE_FOUND;
3753 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003754 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003755 tline = tline->next;
3756 }
3757 skip_white_(tline);
3758 src_set_linnum(k);
3759 istk->lineinc = m;
3760 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003761 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003762 }
3763 free_tlist(origline);
3764 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003765
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003766 case PP_WHILE:
3767 if (defining != NULL) {
3768 if (defining->type == EXP_WHILE) {
3769 defining->def_depth ++;
3770 }
3771 return NO_DIRECTIVE_FOUND;
3772 }
3773 l = NULL;
3774 if ((istk->expansion != NULL) &&
3775 (istk->expansion->emitting == false)) {
3776 j = COND_NEVER;
3777 } else {
3778 l = new_Line();
3779 l->first = copy_Token(tline->next);
3780 j = if_condition(tline->next, i);
3781 tline->next = NULL; /* it got freed */
3782 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
3783 }
3784 ed = new_ExpDef(EXP_WHILE);
3785 ed->state = j;
3786 ed->cur_depth = 1;
3787 ed->max_depth = DEADMAN_LIMIT;
3788 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
3789 if (ed->ignoring == false) {
3790 ed->line = l;
3791 ed->last = l;
3792 } else if (l != NULL) {
3793 delete_Token(l->first);
3794 nasm_free(l);
3795 l = NULL;
3796 }
3797 ed->prev = defining;
3798 defining = ed;
3799 free_tlist(origline);
3800 return DIRECTIVE_FOUND;
3801
3802 case PP_ENDWHILE:
3803 if (defining != NULL) {
3804 if (defining->type == EXP_WHILE) {
3805 if (defining->def_depth > 0) {
3806 defining->def_depth --;
3807 return NO_DIRECTIVE_FOUND;
3808 }
3809 } else {
3810 return NO_DIRECTIVE_FOUND;
3811 }
3812 }
3813 if (tline->next != NULL) {
3814 error_precond(ERR_WARNING|ERR_PASS1,
3815 "trailing garbage after `%%endwhile' ignored");
3816 }
3817 if ((defining == NULL) || (defining->type != EXP_WHILE)) {
3818 error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'");
3819 return DIRECTIVE_FOUND;
3820 }
3821 ed = defining;
3822 defining = ed->prev;
3823 if (ed->ignoring == false) {
3824 ed->prev = expansions;
3825 expansions = ed;
3826 ei = new_ExpInv(EXP_WHILE, ed);
3827 ei->current = ed->line->next;
3828 ei->emitting = true;
3829 ei->prev = istk->expansion;
3830 istk->expansion = ei;
3831 } else {
3832 nasm_free(ed);
3833 }
3834 free_tlist(origline);
3835 return DIRECTIVE_FOUND;
3836
3837 case PP_EXITWHILE:
3838 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3839 /*
3840 * We must search along istk->expansion until we hit a
3841 * while invocation. Then we disable the emitting state(s)
3842 * between exitwhile and endwhile.
3843 */
3844 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3845 if (ei->type == EXP_WHILE) {
3846 break;
3847 }
3848 }
3849
3850 if (ei != NULL) {
3851 /*
3852 * Set all invocations leading back to the while
3853 * invocation to a non-emitting state.
3854 */
3855 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3856 eei->emitting = false;
3857 }
3858 eei->emitting = false;
3859 eei->current = NULL;
3860 eei->def->cur_depth = eei->def->max_depth;
3861 } else {
3862 error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block");
3863 }
3864 free_tlist(origline);
3865 return DIRECTIVE_FOUND;
3866
3867 case PP_COMMENT:
3868 if (defining != NULL) {
3869 if (defining->type == EXP_COMMENT) {
3870 defining->def_depth ++;
3871 }
3872 return NO_DIRECTIVE_FOUND;
3873 }
3874 ed = new_ExpDef(EXP_COMMENT);
3875 ed->ignoring = true;
3876 ed->prev = defining;
3877 defining = ed;
3878 free_tlist(origline);
3879 return DIRECTIVE_FOUND;
3880
3881 case PP_ENDCOMMENT:
3882 if (defining != NULL) {
3883 if (defining->type == EXP_COMMENT) {
3884 if (defining->def_depth > 0) {
3885 defining->def_depth --;
3886 return NO_DIRECTIVE_FOUND;
3887 }
3888 } else {
3889 return NO_DIRECTIVE_FOUND;
3890 }
3891 }
3892 if ((defining == NULL) || (defining->type != EXP_COMMENT)) {
3893 error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'");
3894 return DIRECTIVE_FOUND;
3895 }
3896 ed = defining;
3897 defining = ed->prev;
3898 nasm_free(ed);
3899 free_tlist(origline);
3900 return DIRECTIVE_FOUND;
3901
3902 case PP_FINAL:
3903 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3904 if (in_final != false) {
3905 error(ERR_FATAL, "`%%final' cannot be used recursively");
3906 }
3907 tline = tline->next;
3908 skip_white_(tline);
3909 if (tline == NULL) {
3910 error(ERR_NONFATAL, "`%%final' expects at least one parameter");
3911 } else {
3912 l = new_Line();
3913 l->first = copy_Token(tline);
3914 l->next = finals;
3915 finals = l;
3916 }
3917 free_tlist(origline);
3918 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003919
H. Peter Anvine2c80182005-01-15 22:15:51 +00003920 default:
3921 error(ERR_FATAL,
3922 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003923 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003924 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003925 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003926}
3927
3928/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003929 * Ensure that a macro parameter contains a condition code and
3930 * nothing else. Return the condition code index if so, or -1
3931 * otherwise.
3932 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003933static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003934{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003935 Token *tt;
3936 int i, j, k, m;
3937
H. Peter Anvin25a99342007-09-22 17:45:45 -07003938 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003939 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003940
H. Peter Anvineba20a72002-04-30 20:53:55 +00003941 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003942 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003943 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003944 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003945 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003946 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003947 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003948
3949 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003950 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003951 while (j - i > 1) {
3952 k = (j + i) / 2;
3953 m = nasm_stricmp(t->text, conditions[k]);
3954 if (m == 0) {
3955 i = k;
3956 j = -2;
3957 break;
3958 } else if (m < 0) {
3959 j = k;
3960 } else
3961 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003962 }
3963 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003964 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003965 return i;
3966}
3967
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003968static bool paste_tokens(Token **head, const struct tokseq_match *m,
3969 int mnum, bool handle_paste_tokens)
H. Peter Anvind784a082009-04-20 14:01:18 -07003970{
3971 Token **tail, *t, *tt;
3972 Token **paste_head;
3973 bool did_paste = false;
3974 char *tmp;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003975 int i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003976
3977 /* Now handle token pasting... */
3978 paste_head = NULL;
3979 tail = head;
3980 while ((t = *tail) && (tt = t->next)) {
3981 switch (t->type) {
3982 case TOK_WHITESPACE:
3983 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003984 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003985 t->next = delete_Token(tt);
3986 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003987 /* Do not advance paste_head here */
3988 tail = &t->next;
3989 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003990 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003991 case TOK_PASTE: /* %+ */
3992 if (handle_paste_tokens) {
3993 /* Zap %+ and whitespace tokens to the right */
3994 while (t && (t->type == TOK_WHITESPACE ||
3995 t->type == TOK_PASTE))
3996 t = *tail = delete_Token(t);
3997 if (!paste_head || !t)
3998 break; /* Nothing to paste with */
3999 tail = paste_head;
4000 t = *tail;
4001 tt = t->next;
4002 while (tok_type_(tt, TOK_WHITESPACE))
4003 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004004 if (tt) {
4005 tmp = nasm_strcat(t->text, tt->text);
4006 delete_Token(t);
4007 tt = delete_Token(tt);
4008 t = *tail = tokenize(tmp);
4009 nasm_free(tmp);
4010 while (t->next) {
4011 tail = &t->next;
4012 t = t->next;
4013 }
4014 t->next = tt; /* Attach the remaining token chain */
4015 did_paste = true;
4016 }
4017 paste_head = tail;
4018 tail = &t->next;
4019 break;
4020 }
4021 /* else fall through */
4022 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004023 /*
4024 * Concatenation of tokens might look nontrivial
4025 * but in real it's pretty simple -- the caller
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004026 * prepares the masks of token types to be concatenated
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004027 * and we simply find matched sequences and slip
4028 * them together
4029 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004030 for (i = 0; i < mnum; i++) {
4031 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
4032 size_t len = 0;
4033 char *tmp, *p;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004034
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004035 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004036 len += strlen(tt->text);
4037 tt = tt->next;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004038 }
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004039
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004040 /*
4041 * Now tt points to the first token after
4042 * the potential paste area...
4043 */
4044 if (tt != t->next) {
4045 /* We have at least two tokens... */
4046 len += strlen(t->text);
4047 p = tmp = nasm_malloc(len+1);
4048 while (t != tt) {
4049 strcpy(p, t->text);
4050 p = strchr(p, '\0');
4051 t = delete_Token(t);
4052 }
4053 t = *tail = tokenize(tmp);
4054 nasm_free(tmp);
4055 while (t->next) {
4056 tail = &t->next;
4057 t = t->next;
4058 }
4059 t->next = tt; /* Attach the remaining token chain */
4060 did_paste = true;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004061 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004062 paste_head = tail;
4063 tail = &t->next;
4064 break;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004065 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004066 }
4067 if (i >= mnum) { /* no match */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004068 tail = &t->next;
4069 if (!tok_type_(t->next, TOK_WHITESPACE))
4070 paste_head = tail;
4071 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004072 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004073 }
4074 }
4075 return did_paste;
4076}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004077
4078/*
4079 * expands to a list of tokens from %{x:y}
4080 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004081static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004082{
4083 Token *t = tline, **tt, *tm, *head;
4084 char *pos;
4085 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004086
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004087 pos = strchr(tline->text, ':');
4088 nasm_assert(pos);
4089
4090 lst = atoi(pos + 1);
4091 fst = atoi(tline->text + 1);
4092
4093 /*
4094 * only macros params are accounted so
4095 * if someone passes %0 -- we reject such
4096 * value(s)
4097 */
4098 if (lst == 0 || fst == 0)
4099 goto err;
4100
4101 /* the values should be sane */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004102 if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) ||
4103 (lst > (int)ei->nparam || lst < (-(int)ei->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004104 goto err;
4105
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004106 fst = fst < 0 ? fst + (int)ei->nparam + 1: fst;
4107 lst = lst < 0 ? lst + (int)ei->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004108
4109 /* counted from zero */
4110 fst--, lst--;
4111
4112 /*
4113 * it will be at least one token
4114 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004115 tm = ei->params[(fst + ei->rotate) % ei->nparam];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004116 t = new_Token(NULL, tm->type, tm->text, 0);
4117 head = t, tt = &t->next;
4118 if (fst < lst) {
4119 for (i = fst + 1; i <= lst; i++) {
4120 t = new_Token(NULL, TOK_OTHER, ",", 0);
4121 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004122 j = (i + ei->rotate) % ei->nparam;
4123 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004124 t = new_Token(NULL, tm->type, tm->text, 0);
4125 *tt = t, tt = &t->next;
4126 }
4127 } else {
4128 for (i = fst - 1; i >= lst; i--) {
4129 t = new_Token(NULL, TOK_OTHER, ",", 0);
4130 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004131 j = (i + ei->rotate) % ei->nparam;
4132 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004133 t = new_Token(NULL, tm->type, tm->text, 0);
4134 *tt = t, tt = &t->next;
4135 }
4136 }
4137
4138 *last = tt;
4139 return head;
4140
4141err:
4142 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
4143 &tline->text[1]);
4144 return tline;
4145}
4146
H. Peter Anvin76690a12002-04-30 20:52:49 +00004147/*
4148 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004149 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004150 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004151 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004152static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004153{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004154 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004155 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004156 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004157
4158 tail = &thead;
4159 thead = NULL;
4160
H. Peter Anvine2c80182005-01-15 22:15:51 +00004161 while (tline) {
4162 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004163 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4164 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4165 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004166 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004167 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004168 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004169 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004170 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004171 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004172
H. Peter Anvine2c80182005-01-15 22:15:51 +00004173 t = tline;
4174 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004175
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004176 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
4177 if (ei->type == EXP_MMACRO) {
4178 break;
4179 }
4180 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004181 if (ei == NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004182 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004183 } else {
4184 pos = strchr(t->text, ':');
4185 if (!pos) {
4186 switch (t->text[1]) {
4187 /*
4188 * We have to make a substitution of one of the
4189 * forms %1, %-1, %+1, %%foo, %0.
4190 */
4191 case '0':
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004192 if ((strlen(t->text) > 2) && (t->text[2] == '0')) {
4193 type = TOK_ID;
4194 text = nasm_strdup(ei->label_text);
4195 } else {
4196 type = TOK_NUMBER;
4197 snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam);
4198 text = nasm_strdup(tmpbuf);
4199 }
4200 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004201 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004202 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004203 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004204 ei->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004205 text = nasm_strcat(tmpbuf, t->text + 2);
4206 break;
4207 case '-':
4208 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004209 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004210 tt = NULL;
4211 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004212 if (ei->nparam > 1)
4213 n = (n + ei->rotate) % ei->nparam;
4214 tt = ei->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004215 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004216 cc = find_cc(tt);
4217 if (cc == -1) {
4218 error(ERR_NONFATAL,
4219 "macro parameter %d is not a condition code",
4220 n + 1);
4221 text = NULL;
4222 } else {
4223 type = TOK_ID;
4224 if (inverse_ccs[cc] == -1) {
4225 error(ERR_NONFATAL,
4226 "condition code `%s' is not invertible",
4227 conditions[cc]);
4228 text = NULL;
4229 } else
4230 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4231 }
4232 break;
4233 case '+':
4234 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004235 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004236 tt = NULL;
4237 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004238 if (ei->nparam > 1)
4239 n = (n + ei->rotate) % ei->nparam;
4240 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004241 }
4242 cc = find_cc(tt);
4243 if (cc == -1) {
4244 error(ERR_NONFATAL,
4245 "macro parameter %d is not a condition code",
4246 n + 1);
4247 text = NULL;
4248 } else {
4249 type = TOK_ID;
4250 text = nasm_strdup(conditions[cc]);
4251 }
4252 break;
4253 default:
4254 n = atoi(t->text + 1) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004255 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004256 tt = NULL;
4257 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004258 if (ei->nparam > 1)
4259 n = (n + ei->rotate) % ei->nparam;
4260 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004261 }
4262 if (tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004263 for (i = 0; i < ei->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004264 *tail = new_Token(NULL, tt->type, tt->text, 0);
4265 tail = &(*tail)->next;
4266 tt = tt->next;
4267 }
4268 }
4269 text = NULL; /* we've done it here */
4270 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004271 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004272 } else {
4273 /*
4274 * seems we have a parameters range here
4275 */
4276 Token *head, **last;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004277 head = expand_mmac_params_range(ei, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004278 if (head != t) {
4279 *tail = head;
4280 *last = tline;
4281 tline = head;
4282 text = NULL;
4283 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004284 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004285 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004286 if (!text) {
4287 delete_Token(t);
4288 } else {
4289 *tail = t;
4290 tail = &t->next;
4291 t->type = type;
4292 nasm_free(t->text);
4293 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004294 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004295 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004296 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004297 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004298 } else if (tline->type == TOK_INDIRECT) {
4299 t = tline;
4300 tline = tline->next;
4301 tt = tokenize(t->text);
4302 tt = expand_mmac_params(tt);
4303 tt = expand_smacro(tt);
4304 *tail = tt;
4305 while (tt) {
4306 tt->a.mac = NULL; /* Necessary? */
4307 tail = &tt->next;
4308 tt = tt->next;
4309 }
4310 delete_Token(t);
4311 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004312 } else {
4313 t = *tail = tline;
4314 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004315 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004316 tail = &t->next;
4317 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004318 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004319 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004320
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004321 if (changed) {
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004322 const struct tokseq_match t[] = {
4323 {
4324 PP_CONCAT_MASK(TOK_ID) |
4325 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4326 PP_CONCAT_MASK(TOK_ID) |
4327 PP_CONCAT_MASK(TOK_NUMBER) |
4328 PP_CONCAT_MASK(TOK_FLOAT) |
4329 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4330 },
4331 {
4332 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4333 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4334 }
4335 };
4336 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004337 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004338
H. Peter Anvin76690a12002-04-30 20:52:49 +00004339 return thead;
4340}
4341
4342/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004343 * Expand all single-line macro calls made in the given line.
4344 * Return the expanded version of the line. The original is deemed
4345 * to be destroyed in the process. (In reality we'll just move
4346 * Tokens from input to output a lot of the time, rather than
4347 * actually bothering to destroy and replicate.)
4348 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004349
H. Peter Anvine2c80182005-01-15 22:15:51 +00004350static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004351{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004352 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004353 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004354 Token **params;
4355 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004356 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004357 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004358 Token *org_tline = tline;
4359 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004360 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004361 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004362 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004363
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004364 /*
4365 * Trick: we should avoid changing the start token pointer since it can
4366 * be contained in "next" field of other token. Because of this
4367 * we allocate a copy of first token and work with it; at the end of
4368 * routine we copy it back
4369 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004370 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004371 tline = new_Token(org_tline->next, org_tline->type,
4372 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004373 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004374 nasm_free(org_tline->text);
4375 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004376 }
4377
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004378 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004379
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004380again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004381 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004382 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004383
H. Peter Anvine2c80182005-01-15 22:15:51 +00004384 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004385 if (!--deadman) {
4386 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004387 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004388 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004389
H. Peter Anvine2c80182005-01-15 22:15:51 +00004390 if ((mname = tline->text)) {
4391 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004392 if (tline->type == TOK_ID) {
4393 head = (SMacro *)hash_findix(&smacros, mname);
4394 } else if (tline->type == TOK_PREPROC_ID) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004395 ctx = get_ctx(mname, &mname, false);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004396 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4397 } else
4398 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004399
H. Peter Anvine2c80182005-01-15 22:15:51 +00004400 /*
4401 * We've hit an identifier. As in is_mmacro below, we first
4402 * check whether the identifier is a single-line macro at
4403 * all, then think about checking for parameters if
4404 * necessary.
4405 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004406 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004407 if (!mstrcmp(m->name, mname, m->casesense))
4408 break;
4409 if (m) {
4410 mstart = tline;
4411 params = NULL;
4412 paramsize = NULL;
4413 if (m->nparam == 0) {
4414 /*
4415 * Simple case: the macro is parameterless. Discard the
4416 * one token that the macro call took, and push the
4417 * expansion back on the to-do stack.
4418 */
4419 if (!m->expansion) {
4420 if (!strcmp("__FILE__", m->name)) {
4421 int32_t num = 0;
4422 char *file = NULL;
4423 src_get(&num, &file);
4424 tline->text = nasm_quote(file, strlen(file));
4425 tline->type = TOK_STRING;
4426 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004427 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004428 }
4429 if (!strcmp("__LINE__", m->name)) {
4430 nasm_free(tline->text);
4431 make_tok_num(tline, src_get_linnum());
4432 continue;
4433 }
4434 if (!strcmp("__BITS__", m->name)) {
4435 nasm_free(tline->text);
4436 make_tok_num(tline, globalbits);
4437 continue;
4438 }
4439 tline = delete_Token(tline);
4440 continue;
4441 }
4442 } else {
4443 /*
4444 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004445 * exists and takes parameters. We must find the
4446 * parameters in the call, count them, find the SMacro
4447 * that corresponds to that form of the macro call, and
4448 * substitute for the parameters when we expand. What a
4449 * pain.
4450 */
4451 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004452 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004453 do {
4454 t = tline->next;
4455 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004456 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004457 t->text = NULL;
4458 t = tline->next = delete_Token(t);
4459 }
4460 tline = t;
4461 } while (tok_type_(tline, TOK_WHITESPACE));
4462 if (!tok_is_(tline, "(")) {
4463 /*
4464 * This macro wasn't called with parameters: ignore
4465 * the call. (Behaviour borrowed from gnu cpp.)
4466 */
4467 tline = mstart;
4468 m = NULL;
4469 } else {
4470 int paren = 0;
4471 int white = 0;
4472 brackets = 0;
4473 nparam = 0;
4474 sparam = PARAM_DELTA;
4475 params = nasm_malloc(sparam * sizeof(Token *));
4476 params[0] = tline->next;
4477 paramsize = nasm_malloc(sparam * sizeof(int));
4478 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004479 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004480 /*
4481 * For some unusual expansions
4482 * which concatenates function call
4483 */
4484 t = tline->next;
4485 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004486 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004487 t->text = NULL;
4488 t = tline->next = delete_Token(t);
4489 }
4490 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004491
H. Peter Anvine2c80182005-01-15 22:15:51 +00004492 if (!tline) {
4493 error(ERR_NONFATAL,
4494 "macro call expects terminating `)'");
4495 break;
4496 }
4497 if (tline->type == TOK_WHITESPACE
4498 && brackets <= 0) {
4499 if (paramsize[nparam])
4500 white++;
4501 else
4502 params[nparam] = tline->next;
4503 continue; /* parameter loop */
4504 }
4505 if (tline->type == TOK_OTHER
4506 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004507 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004508 if (ch == ',' && !paren && brackets <= 0) {
4509 if (++nparam >= sparam) {
4510 sparam += PARAM_DELTA;
4511 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004512 sparam * sizeof(Token *));
4513 paramsize = nasm_realloc(paramsize,
4514 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004515 }
4516 params[nparam] = tline->next;
4517 paramsize[nparam] = 0;
4518 white = 0;
4519 continue; /* parameter loop */
4520 }
4521 if (ch == '{' &&
4522 (brackets > 0 || (brackets == 0 &&
4523 !paramsize[nparam])))
4524 {
4525 if (!(brackets++)) {
4526 params[nparam] = tline->next;
4527 continue; /* parameter loop */
4528 }
4529 }
4530 if (ch == '}' && brackets > 0)
4531 if (--brackets == 0) {
4532 brackets = -1;
4533 continue; /* parameter loop */
4534 }
4535 if (ch == '(' && !brackets)
4536 paren++;
4537 if (ch == ')' && brackets <= 0)
4538 if (--paren < 0)
4539 break;
4540 }
4541 if (brackets < 0) {
4542 brackets = 0;
4543 error(ERR_NONFATAL, "braces do not "
4544 "enclose all of macro parameter");
4545 }
4546 paramsize[nparam] += white + 1;
4547 white = 0;
4548 } /* parameter loop */
4549 nparam++;
4550 while (m && (m->nparam != nparam ||
4551 mstrcmp(m->name, mname,
4552 m->casesense)))
4553 m = m->next;
4554 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004555 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004556 "macro `%s' exists, "
4557 "but not taking %d parameters",
4558 mstart->text, nparam);
4559 }
4560 }
4561 if (m && m->in_progress)
4562 m = NULL;
4563 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004564 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004565 * Design question: should we handle !tline, which
4566 * indicates missing ')' here, or expand those
4567 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004568 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004569 */
4570 nasm_free(params);
4571 nasm_free(paramsize);
4572 tline = mstart;
4573 } else {
4574 /*
4575 * Expand the macro: we are placed on the last token of the
4576 * call, so that we can easily split the call from the
4577 * following tokens. We also start by pushing an SMAC_END
4578 * token for the cycle removal.
4579 */
4580 t = tline;
4581 if (t) {
4582 tline = t->next;
4583 t->next = NULL;
4584 }
4585 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004586 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004587 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004588 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004589 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004590 if (t->type >= TOK_SMAC_PARAM) {
4591 Token *pcopy = tline, **ptail = &pcopy;
4592 Token *ttt, *pt;
4593 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004594
H. Peter Anvine2c80182005-01-15 22:15:51 +00004595 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004596 i = paramsize[t->type - TOK_SMAC_PARAM];
4597 while (--i >= 0) {
4598 pt = *ptail = new_Token(tline, ttt->type,
4599 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004600 ptail = &pt->next;
4601 ttt = ttt->next;
4602 }
4603 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004604 } else if (t->type == TOK_PREPROC_Q) {
4605 tt = new_Token(tline, TOK_ID, mname, 0);
4606 tline = tt;
4607 } else if (t->type == TOK_PREPROC_QQ) {
4608 tt = new_Token(tline, TOK_ID, m->name, 0);
4609 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004610 } else {
4611 tt = new_Token(tline, t->type, t->text, 0);
4612 tline = tt;
4613 }
4614 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004615
H. Peter Anvine2c80182005-01-15 22:15:51 +00004616 /*
4617 * Having done that, get rid of the macro call, and clean
4618 * up the parameters.
4619 */
4620 nasm_free(params);
4621 nasm_free(paramsize);
4622 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004623 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004624 continue; /* main token loop */
4625 }
4626 }
4627 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004628
H. Peter Anvine2c80182005-01-15 22:15:51 +00004629 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004630 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004631 tline = delete_Token(tline);
4632 } else {
4633 t = *tail = tline;
4634 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004635 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004636 t->next = NULL;
4637 tail = &t->next;
4638 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004639 }
4640
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004641 /*
4642 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004643 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004644 * TOK_IDs should be concatenated.
4645 * Also we look for %+ tokens and concatenate the tokens before and after
4646 * them (without white spaces in between).
4647 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004648 if (expanded) {
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004649 const struct tokseq_match t[] = {
4650 {
4651 PP_CONCAT_MASK(TOK_ID) |
4652 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4653 PP_CONCAT_MASK(TOK_ID) |
4654 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4655 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4656 }
4657 };
4658 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004659 /*
4660 * If we concatenated something, *and* we had previously expanded
4661 * an actual macro, scan the lines again for macros...
4662 */
4663 tline = thead;
4664 expanded = false;
4665 goto again;
4666 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004667 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004668
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004669err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004670 if (org_tline) {
4671 if (thead) {
4672 *org_tline = *thead;
4673 /* since we just gave text to org_line, don't free it */
4674 thead->text = NULL;
4675 delete_Token(thead);
4676 } else {
4677 /* the expression expanded to empty line;
4678 we can't return NULL for some reasons
4679 we just set the line to a single WHITESPACE token. */
4680 memset(org_tline, 0, sizeof(*org_tline));
4681 org_tline->text = NULL;
4682 org_tline->type = TOK_WHITESPACE;
4683 }
4684 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004685 }
4686
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004687 return thead;
4688}
4689
4690/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004691 * Similar to expand_smacro but used exclusively with macro identifiers
4692 * right before they are fetched in. The reason is that there can be
4693 * identifiers consisting of several subparts. We consider that if there
4694 * are more than one element forming the name, user wants a expansion,
4695 * otherwise it will be left as-is. Example:
4696 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004697 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004698 *
4699 * the identifier %$abc will be left as-is so that the handler for %define
4700 * will suck it and define the corresponding value. Other case:
4701 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004702 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004703 *
4704 * In this case user wants name to be expanded *before* %define starts
4705 * working, so we'll expand %$abc into something (if it has a value;
4706 * otherwise it will be left as-is) then concatenate all successive
4707 * PP_IDs into one.
4708 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004709static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004710{
4711 Token *cur, *oldnext = NULL;
4712
H. Peter Anvin734b1882002-04-30 21:01:08 +00004713 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004714 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004715
4716 cur = tline;
4717 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004718 (cur->next->type == TOK_ID ||
4719 cur->next->type == TOK_PREPROC_ID
4720 || cur->next->type == TOK_NUMBER))
4721 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004722
4723 /* If identifier consists of just one token, don't expand */
4724 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004725 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004726
H. Peter Anvine2c80182005-01-15 22:15:51 +00004727 if (cur) {
4728 oldnext = cur->next; /* Detach the tail past identifier */
4729 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004730 }
4731
H. Peter Anvin734b1882002-04-30 21:01:08 +00004732 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004733
H. Peter Anvine2c80182005-01-15 22:15:51 +00004734 if (cur) {
4735 /* expand_smacro possibly changhed tline; re-scan for EOL */
4736 cur = tline;
4737 while (cur && cur->next)
4738 cur = cur->next;
4739 if (cur)
4740 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004741 }
4742
4743 return tline;
4744}
4745
4746/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004747 * Determine whether the given line constitutes a multi-line macro
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004748 * call, and return the ExpDef structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004749 * to check for an initial label - that's taken care of in
4750 * expand_mmacro - but must check numbers of parameters. Guaranteed
4751 * to be called with tline->type == TOK_ID, so the putative macro
4752 * name is easy to find.
4753 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004754static ExpDef *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004755{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004756 ExpDef *head, *ed;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004757 Token **params;
4758 int nparam;
4759
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004760 head = (ExpDef *) hash_findix(&expdefs, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004761
4762 /*
4763 * Efficiency: first we see if any macro exists with the given
4764 * name. If not, we can return NULL immediately. _Then_ we
4765 * count the parameters, and then we look further along the
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004766 * list if necessary to find the proper ExpDef.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004767 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004768 list_for_each(ed, head)
4769 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004770 break;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004771 if (!ed)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004772 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004773
4774 /*
4775 * OK, we have a potential macro. Count and demarcate the
4776 * parameters.
4777 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004778 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004779
4780 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004781 * So we know how many parameters we've got. Find the ExpDef
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004782 * structure that handles this number.
4783 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004784 while (ed) {
4785 if (ed->nparam_min <= nparam
4786 && (ed->plus || nparam <= ed->nparam_max)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004787 /*
4788 * It's right, and we can use it. Add its default
4789 * parameters to the end of our list if necessary.
4790 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004791 if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004792 params =
4793 nasm_realloc(params,
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004794 ((ed->nparam_min + ed->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004795 1) * sizeof(*params)));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004796 while (nparam < ed->nparam_min + ed->ndefs) {
4797 params[nparam] = ed->defaults[nparam - ed->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004798 nparam++;
4799 }
4800 }
4801 /*
4802 * If we've gone over the maximum parameter count (and
4803 * we're in Plus mode), ignore parameters beyond
4804 * nparam_max.
4805 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004806 if (ed->plus && nparam > ed->nparam_max)
4807 nparam = ed->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004808 /*
4809 * Then terminate the parameter list, and leave.
4810 */
4811 if (!params) { /* need this special case */
4812 params = nasm_malloc(sizeof(*params));
4813 nparam = 0;
4814 }
4815 params[nparam] = NULL;
4816 *params_array = params;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004817 return ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004818 }
4819 /*
4820 * This one wasn't right: look for the next one with the
4821 * same name.
4822 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004823 list_for_each(ed, ed->next)
4824 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004825 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004826 }
4827
4828 /*
4829 * After all that, we didn't find one with the right number of
4830 * parameters. Issue a warning, and fail to expand the macro.
4831 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004832 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004833 "macro `%s' exists, but not taking %d parameters",
4834 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004835 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004836 return NULL;
4837}
4838
4839/*
4840 * Expand the multi-line macro call made by the given line, if
4841 * there is one to be expanded. If there is, push the expansion on
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004842 * istk->expansion and return true. Otherwise return false.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004843 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004844static bool expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004845{
H. Peter Anvineba20a72002-04-30 20:53:55 +00004846 Token *label = NULL;
4847 int dont_prepend = 0;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004848 Token **params, *t, *mtok;
4849 Line *l = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004850 ExpDef *ed;
4851 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004852 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004853 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004854
4855 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004856 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004857 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004858 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004859 return false;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004860 mtok = t;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004861 ed = is_mmacro(t, &params);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004862 if (ed != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004863 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004864 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004865 Token *last;
4866 /*
4867 * We have an id which isn't a macro call. We'll assume
4868 * it might be a label; we'll also check to see if a
4869 * colon follows it. Then, if there's another id after
4870 * that lot, we'll check it again for macro-hood.
4871 */
4872 label = last = t;
4873 t = t->next;
4874 if (tok_type_(t, TOK_WHITESPACE))
4875 last = t, t = t->next;
4876 if (tok_is_(t, ":")) {
4877 dont_prepend = 1;
4878 last = t, t = t->next;
4879 if (tok_type_(t, TOK_WHITESPACE))
4880 last = t, t = t->next;
4881 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004882 if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, &params)))
4883 return false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004884 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004885 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004886 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004887 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004888
4889 /*
4890 * Fix up the parameters: this involves stripping leading and
4891 * trailing whitespace, then stripping braces if they are
4892 * present.
4893 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004894 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004895 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004896
H. Peter Anvine2c80182005-01-15 22:15:51 +00004897 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004898 int brace = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004899 int comma = (!ed->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004900
H. Peter Anvine2c80182005-01-15 22:15:51 +00004901 t = params[i];
4902 skip_white_(t);
4903 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004904 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004905 params[i] = t;
4906 paramlen[i] = 0;
4907 while (t) {
4908 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4909 break; /* ... because we have hit a comma */
4910 if (comma && t->type == TOK_WHITESPACE
4911 && tok_is_(t->next, ","))
4912 break; /* ... or a space then a comma */
4913 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4914 break; /* ... or a brace */
4915 t = t->next;
4916 paramlen[i]++;
4917 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004918 }
4919
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004920 if (ed->cur_depth >= ed->max_depth) {
4921 if (ed->max_depth > 1) {
4922 error(ERR_WARNING,
4923 "reached maximum macro recursion depth of %i for %s",
4924 ed->max_depth,ed->name);
4925 }
4926 return false;
4927 } else {
4928 ed->cur_depth ++;
4929 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004930
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004931 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004932 * OK, we have found a ExpDef structure representing a
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004933 * previously defined mmacro. Create an expansion invocation
4934 * and point it back to the expansion definition. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004935 * parameter tokens and macro-local tokens doesn't get done
4936 * until the single-line macro substitution process; this is
4937 * because delaying them allows us to change the semantics
4938 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004939 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004940 ei = new_ExpInv(EXP_MMACRO, ed);
4941 ei->name = nasm_strdup(mname);
4942 //ei->label = label;
4943 //ei->label_text = detoken(label, false);
4944 ei->current = ed->line;
4945 ei->emitting = true;
4946 //ei->iline = tline;
4947 ei->params = params;
4948 ei->nparam = nparam;
4949 ei->rotate = 0;
4950 ei->paramlen = paramlen;
4951 ei->lineno = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004952
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004953 ei->prev = istk->expansion;
4954 istk->expansion = ei;
4955
4956 /*
4957 * Special case: detect %00 on first invocation; if found,
4958 * avoid emitting any labels that precede the mmacro call.
4959 * ed->prepend is set to -1 when %00 is detected, else 1.
4960 */
4961 if (ed->prepend == 0) {
4962 for (l = ed->line; l != NULL; l = l->next) {
4963 for (t = l->first; t != NULL; t = t->next) {
4964 if ((t->type == TOK_PREPROC_ID) &&
4965 (strlen(t->text) == 3) &&
4966 (t->text[1] == '0') && (t->text[2] == '0')) {
4967 dont_prepend = -1;
4968 break;
4969 }
4970 }
4971 if (dont_prepend < 0) {
4972 break;
4973 }
4974 }
4975 ed->prepend = ((dont_prepend < 0) ? -1 : 1);
4976 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004977
4978 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004979 * If we had a label, push it on as the first line of
4980 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004981 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004982 if (label != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004983 if (ed->prepend < 0) {
4984 ei->label_text = detoken(label, false);
4985 } else {
4986 if (dont_prepend == 0) {
4987 t = label;
4988 while (t->next != NULL) {
4989 t = t->next;
4990 }
4991 t->next = new_Token(NULL, TOK_OTHER, ":", 0);
4992 }
4993 l = new_Line();
4994 l->first = copy_Token(label);
4995 l->next = ei->current;
4996 ei->current = l;
4997 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004998 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004999
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005000 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005001
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005002 istk->mmac_depth++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005003 return true;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005004}
5005
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005006/* The function that actually does the error reporting */
5007static void verror(int severity, const char *fmt, va_list arg)
5008{
5009 char buff[1024];
5010
5011 vsnprintf(buff, sizeof(buff), fmt, arg);
5012
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005013 if (istk && istk->mmac_depth > 0) {
5014 ExpInv *ei = istk->expansion;
5015 int lineno = ei->lineno;
5016 while (ei) {
5017 if (ei->type == EXP_MMACRO)
5018 break;
5019 lineno += ei->relno;
5020 ei = ei->prev;
5021 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005022 nasm_error(severity, "(%s:%d) %s", ei->def->name,
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005023 lineno, buff);
5024 } else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005025 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005026}
5027
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005028/*
5029 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07005030 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005031 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005032static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005033{
5034 va_list arg;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005035 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005036 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00005037 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005038}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005039
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005040/*
5041 * Because %else etc are evaluated in the state context
5042 * of the previous branch, errors might get lost with error():
5043 * %if 0 ... %else trailing garbage ... %endif
5044 * So %else etc should report errors with this function.
5045 */
5046static void error_precond(int severity, const char *fmt, ...)
5047{
5048 va_list arg;
5049
5050 /* Only ignore the error if it's really in a dead branch */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005051 if ((istk != NULL) &&
5052 (istk->expansion != NULL) &&
5053 (istk->expansion->type == EXP_IF) &&
5054 (istk->expansion->def->state == COND_NEVER))
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005055 return;
5056
5057 va_start(arg, fmt);
5058 verror(severity, fmt, arg);
5059 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005060}
5061
H. Peter Anvin734b1882002-04-30 21:01:08 +00005062static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005063pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005064{
H. Peter Anvin7383b402008-09-24 10:20:40 -07005065 Token *t;
5066
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005067 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005068 istk = nasm_malloc(sizeof(Include));
5069 istk->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005070 istk->expansion = NULL;
5071 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00005072 istk->fname = NULL;
5073 src_set_fname(nasm_strdup(file));
5074 src_set_linnum(0);
5075 istk->lineinc = 1;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005076 istk->mmac_depth = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005077 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07005078 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00005079 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005080 defining = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005081 finals = NULL;
5082 in_final = false;
Charles Crayned4200be2008-07-12 16:42:33 -07005083 nested_mac_count = 0;
5084 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005085 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005086 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005087 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005088 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005089 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005090 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005091 }
H. Peter Anvind2456592008-06-19 15:04:18 -07005092 any_extrastdmac = extrastdmac && *extrastdmac;
5093 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005094 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005095
5096 /*
5097 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5098 * The caller, however, will also pass in 3 for preprocess-only so
5099 * we can set __PASS__ accordingly.
5100 */
5101 pass = apass > 2 ? 2 : apass;
5102
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005103 dephead = deptail = deplist;
5104 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005105 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
5106 sl->next = NULL;
5107 strcpy(sl->str, file);
5108 *deptail = sl;
5109 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005110 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07005111
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005112 /*
5113 * Define the __PASS__ macro. This is defined here unlike
5114 * all the other builtins, because it is special -- it varies between
5115 * passes.
5116 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07005117 t = nasm_malloc(sizeof(*t));
5118 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005119 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07005120 t->a.mac = NULL;
5121 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005122}
5123
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005124static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005125{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005126 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005127 Token *tline;
Keith Kanios9858cec2010-11-08 00:58:02 -06005128 ExpDef *ed;
5129 ExpInv *ei;
5130 Line *l;
5131 int j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005132
H. Peter Anvine2c80182005-01-15 22:15:51 +00005133 while (1) {
5134 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005135 * Fetch a tokenized line, either from the expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005136 * buffer or from the input file.
5137 */
5138 tline = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005139
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005140 while (1) { /* until we get a line we can use */
5141 /*
5142 * Fetch a tokenized line from the expansion buffer
5143 */
5144 if (istk->expansion != NULL) {
5145 ei = istk->expansion;
5146 if (ei->current != NULL) {
5147 if (ei->emitting == false) {
5148 ei->current = NULL;
5149 continue;
5150 }
5151 l = ei->current;
5152 ei->current = l->next;
5153 ei->lineno++;
5154 tline = copy_Token(l->first);
5155 if (((ei->type == EXP_REP) ||
5156 (ei->type == EXP_MMACRO) ||
5157 (ei->type == EXP_WHILE))
5158 && (ei->def->nolist == false)) {
5159 char *p = detoken(tline, false);
5160 list->line(LIST_MACRO, p);
5161 nasm_free(p);
5162 }
5163 if (ei->linnum > -1) {
5164 src_set_linnum(src_get_linnum() + 1);
5165 }
5166 break;
5167 } else if ((ei->type == EXP_REP) &&
5168 (ei->def->cur_depth < ei->def->max_depth)) {
5169 ei->def->cur_depth ++;
5170 ei->current = ei->def->line;
5171 ei->lineno = 0;
5172 continue;
5173 } else if ((ei->type == EXP_WHILE) &&
5174 (ei->def->cur_depth < ei->def->max_depth)) {
5175 ei->current = ei->def->line;
5176 ei->lineno = 0;
5177 tline = copy_Token(ei->current->first);
5178 j = if_condition(tline, PP_WHILE);
5179 tline = NULL;
5180 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
5181 if (j == COND_IF_TRUE) {
5182 ei->current = ei->current->next;
5183 ei->def->cur_depth ++;
5184 } else {
5185 ei->emitting = false;
5186 ei->current = NULL;
5187 ei->def->cur_depth = ei->def->max_depth;
5188 }
5189 continue;
5190 } else {
5191 istk->expansion = ei->prev;
5192 ed = ei->def;
5193 if (ed != NULL) {
5194 if ((ei->emitting == true) &&
5195 (ed->max_depth == DEADMAN_LIMIT) &&
5196 (ed->cur_depth == DEADMAN_LIMIT)
5197 ) {
5198 error(ERR_FATAL, "runaway expansion detected, aborting");
5199 }
5200 if (ed->cur_depth > 0) {
5201 ed->cur_depth --;
Keith Kanios94124652010-12-18 11:47:28 -06005202 } else if (ed->type != EXP_MMACRO) {
Keith Kanios6a7c3e92010-12-18 11:49:53 -06005203 expansions = ed->prev;
5204 free_expdef(ed);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005205 }
5206 if ((ei->type == EXP_REP) ||
5207 (ei->type == EXP_MMACRO) ||
5208 (ei->type == EXP_WHILE)) {
5209 list->downlevel(LIST_MACRO);
5210 if (ei->type == EXP_MMACRO) {
5211 istk->mmac_depth--;
5212 }
5213 }
5214 }
5215 if (ei->linnum > -1) {
5216 src_set_linnum(ei->linnum);
5217 }
5218 free_expinv(ei);
5219 continue;
5220 }
5221 }
5222
5223 /*
5224 * Read in line from input and tokenize
5225 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005226 line = read_line();
5227 if (line) { /* from the current input file */
5228 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005229 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005230 nasm_free(line);
5231 break;
5232 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005233
H. Peter Anvine2c80182005-01-15 22:15:51 +00005234 /*
5235 * The current file has ended; work down the istk
5236 */
5237 {
5238 Include *i = istk;
5239 fclose(i->fp);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005240 if (i->expansion != NULL) {
5241 error(ERR_FATAL,
5242 "end of file while still in an expansion");
5243 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005244 /* only set line and file name if there's a next node */
5245 if (i->next) {
5246 src_set_linnum(i->lineno);
5247 nasm_free(src_set_fname(i->fname));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005248 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005249 if ((i->next == NULL) && (finals != NULL)) {
5250 in_final = true;
5251 ei = new_ExpInv(EXP_FINAL, NULL);
5252 ei->emitting = true;
5253 ei->current = finals;
5254 istk->expansion = ei;
5255 finals = NULL;
5256 continue;
5257 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005258 istk = i->next;
5259 list->downlevel(LIST_INCLUDE);
5260 nasm_free(i);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005261 if (istk == NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005262 if (finals != NULL) {
5263 in_final = true;
5264 } else {
5265 return NULL;
5266 }
5267 }
5268 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005269 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005270 }
5271
5272 if (defining == NULL) {
5273 tline = expand_mmac_params(tline);
5274 }
5275
H. Peter Anvine2c80182005-01-15 22:15:51 +00005276 /*
5277 * Check the line to see if it's a preprocessor directive.
5278 */
5279 if (do_directive(tline) == DIRECTIVE_FOUND) {
5280 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005281 } else if (defining != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005282 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005283 * We're defining an expansion. We emit nothing at all,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005284 * and just shove the tokenized line on to the definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005285 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005286 if (defining->ignoring == false) {
5287 Line *l = new_Line();
5288 l->first = tline;
5289 if (defining->line == NULL) {
5290 defining->line = l;
5291 defining->last = l;
5292 } else {
5293 defining->last->next = l;
5294 defining->last = l;
5295 }
5296 } else {
Keith Kanios104803d2010-12-18 11:05:46 -06005297 free_tlist(tline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005298 }
5299 defining->linecount++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005300 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005301 } else if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005302 (istk->expansion->emitting != true)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005303 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005304 * We're in a non-emitting branch of an expansion.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005305 * Emit nothing at all, not even a blank line: when we
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005306 * emerge from the expansion we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005307 * directive so we keep our place correctly.
5308 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005309 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005310 continue;
5311 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005312 tline = expand_smacro(tline);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005313 if (expand_mmacro(tline) != true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005314 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005315 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005316 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005317 line = detoken(tline, true);
5318 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005319 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005320 } else {
5321 continue;
5322 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005323 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005324 }
5325 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005326}
5327
H. Peter Anvine2c80182005-01-15 22:15:51 +00005328static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005329{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005330 if (defining != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005331 error(ERR_NONFATAL, "end of file while still defining an expansion");
5332 while (defining != NULL) {
5333 ExpDef *ed = defining;
5334 defining = ed->prev;
5335 free_expdef(ed);
5336 }
5337 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005338 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005339 while (cstk != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005340 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005341 free_macros();
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005342 while (istk != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005343 Include *i = istk;
5344 istk = istk->next;
5345 fclose(i->fp);
5346 nasm_free(i->fname);
5347 nasm_free(i);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005348 while (i->expansion != NULL) {
5349 ExpInv *ei = i->expansion;
5350 i->expansion = ei->prev;
5351 free_expinv(ei);
5352 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005353 }
5354 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005355 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005356 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005357 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005358 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005359 free_llist(predef);
5360 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005361 while ((i = ipath)) {
5362 ipath = i->next;
5363 if (i->path)
5364 nasm_free(i->path);
5365 nasm_free(i);
5366 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005367 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005368}
5369
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005370void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005371{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005372 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005373
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005374 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005375 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005376 i->next = NULL;
5377
H. Peter Anvin89cee572009-07-15 09:16:54 -04005378 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005379 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005380 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005381 j = j->next;
5382 j->next = i;
5383 } else {
5384 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005385 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005386}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005387
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005388void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005389{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005390 Token *inc, *space, *name;
5391 Line *l;
5392
H. Peter Anvin734b1882002-04-30 21:01:08 +00005393 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5394 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5395 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005396
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005397 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005398 l->next = predef;
5399 l->first = inc;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005400 predef = l;
5401}
5402
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005403void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005404{
5405 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005406 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005407 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005408
5409 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005410 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5411 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005412 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005413 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005414 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005415 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005416 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005417
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005418 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005419 l->next = predef;
5420 l->first = def;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005421 predef = l;
5422}
5423
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005424void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005425{
5426 Token *def, *space;
5427 Line *l;
5428
H. Peter Anvin734b1882002-04-30 21:01:08 +00005429 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5430 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005431 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005432
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005433 l = new_Line();
H. Peter Anvin620515a2002-04-30 20:57:38 +00005434 l->next = predef;
5435 l->first = def;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005436 predef = l;
5437}
5438
Keith Kaniosb7a89542007-04-12 02:40:54 +00005439/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005440 * This function is used to assist with "runtime" preprocessor
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005441 * directives, e.g. pp_runtime("%define __BITS__ 64");
Keith Kaniosb7a89542007-04-12 02:40:54 +00005442 *
5443 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5444 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5445 */
5446
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005447void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005448{
5449 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005450
Keith Kaniosb7a89542007-04-12 02:40:54 +00005451 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005452 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005453 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005454
Keith Kaniosb7a89542007-04-12 02:40:54 +00005455}
5456
H. Peter Anvina70547f2008-07-19 21:44:26 -07005457void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005458{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005459 extrastdmac = macros;
5460}
5461
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005462static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005463{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005464 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005465 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005466 tok->text = nasm_strdup(numbuf);
5467 tok->type = TOK_NUMBER;
5468}
5469
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005470Preproc nasmpp = {
5471 pp_reset,
5472 pp_getline,
5473 pp_cleanup
5474};