blob: f5d0fd150f9ce9d25e2095523be73ebe31113141 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +04003 * Copyright 1996-2011 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000065#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000066#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000067#include <stdlib.h>
68#include <stddef.h>
69#include <string.h>
70#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000071#include <limits.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000072#include <inttypes.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000073
74#include "nasm.h"
75#include "nasmlib.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000076#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070077#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070078#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070079#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070080#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070081#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070082#include "tables.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000083
84typedef struct SMacro SMacro;
Keith Kaniosb307a4f2010-11-06 17:41:51 -050085typedef struct ExpDef ExpDef;
86typedef struct ExpInv ExpInv;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000087typedef struct Context Context;
88typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000089typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000090typedef struct Line Line;
91typedef struct Include Include;
92typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000093typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000094
95/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070096 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
102 */
103
104/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 * Store the definition of a single-line macro.
106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107struct SMacro {
Cyrill Gorcunov10083ae2011-07-17 20:06:20 +0400108 SMacro *next;
109 char *name;
110 Token *expansion;
111 unsigned int nparam;
112 bool casesense;
113 bool in_progress;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000114};
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 {
Cyrill Gorcunovd57a0312011-07-17 20:11:08 +0400246 ExpInv *prev; /* previous invocation */
247 ExpDef *def; /* pointer to expansion definition */
248 char *name; /* invocation name */
249 Line *label; /* pointer to label */
250 char *label_text; /* pointer to label text */
251 Line *current; /* pointer to current line in invocation */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300252
Cyrill Gorcunovd57a0312011-07-17 20:11:08 +0400253 Token **params; /* actual parameters */
254 Token *iline; /* invocation line */
255 int *paramlen;
256 unsigned int nparam;
257 unsigned int rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300258
Cyrill Gorcunovd57a0312011-07-17 20:11:08 +0400259 uint64_t unique;
260 int lineno; /* current line number in expansion */
261 int linnum; /* line number at invocation */
262 int relno; /* relative line number at invocation */
263 enum pp_exp_type type; /* expansion type */
264 bool emitting;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500265};
266
267/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000268 * To handle an arbitrary level of file inclusion, we maintain a
269 * stack (ie linked list) of these things.
270 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000271struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000272 Include *next;
273 FILE *fp;
274 Cond *conds;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500275 ExpInv *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000276 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000277 int lineno, lineinc;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300278 int mmac_depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000279};
280
281/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000282 * Include search path. This is simply a list of strings which get
283 * prepended, in turn, to the name of an include file, in an
284 * attempt to find the file if it's not in the current directory.
285 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000286struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000287 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000288 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000289};
290
291/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000292 * Conditional assembly: we maintain a separate stack of these for
293 * each level of file inclusion. (The only reason we keep the
294 * stacks separate is to ensure that a stray `%endif' in a file
295 * included from within the true branch of a `%if' won't terminate
296 * it and cause confusion: instead, rightly, it'll cause an error.)
297 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000298enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000299 /*
300 * These states are for use just after %if or %elif: IF_TRUE
301 * means the condition has evaluated to truth so we are
302 * currently emitting, whereas IF_FALSE means we are not
303 * currently emitting but will start doing so if a %else comes
304 * up. In these states, all directives are admissible: %elif,
305 * %else and %endif. (And of course %if.)
306 */
307 COND_IF_TRUE, COND_IF_FALSE,
308 /*
309 * These states come up after a %else: ELSE_TRUE means we're
310 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
311 * any %elif or %else will cause an error.
312 */
313 COND_ELSE_TRUE, COND_ELSE_FALSE,
314 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200315 * These states mean that we're not emitting now, and also that
316 * nothing until %endif will be emitted at all. COND_DONE is
317 * used when we've had our moment of emission
318 * and have now started seeing %elifs. COND_NEVER is used when
319 * the condition construct in question is contained within a
320 * non-emitting branch of a larger condition construct,
321 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000322 */
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200323 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000324};
325#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
326
H. Peter Anvin70653092007-10-19 14:42:29 -0700327/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000328 * These defines are used as the possible return values for do_directive
329 */
330#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300331#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000332
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000333/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500334 * This define sets the upper limit for smacro and expansions
Keith Kanios852f1ee2009-07-12 00:19:55 -0500335 */
336#define DEADMAN_LIMIT (1 << 20)
337
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400338/* max reps */
339#define REP_LIMIT ((INT64_C(1) << 62))
340
Keith Kanios852f1ee2009-07-12 00:19:55 -0500341/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000342 * Condition codes. Note that we use c_ prefix not C_ because C_ is
343 * used in nasm.h for the "real" condition codes. At _this_ level,
344 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
345 * ones, so we need a different enum...
346 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700347static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000348 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
349 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000350 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000351};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700352enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000353 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
354 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 -0700355 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
356 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000357};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700358static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000359 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
360 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 +0000361 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000362};
363
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000364/* For TASM compatibility we need to be able to recognise TASM compatible
365 * conditional compilation directives. Using the NASM pre-processor does
366 * not work, so we look for them specifically from the following list and
367 * then jam in the equivalent NASM directive into the input stream.
368 */
369
H. Peter Anvine2c80182005-01-15 22:15:51 +0000370enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000371 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
372 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
373};
374
H. Peter Anvin476d2862007-10-02 22:04:15 -0700375static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000376 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
377 "ifndef", "include", "local"
378};
379
380static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000381static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000382static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800383static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000384
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000385static Context *cstk;
386static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000387static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000388
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300389static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700390static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000391
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300392static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000393
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000394static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700395static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000396
397static ListGen *list;
398
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000399/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500400 * The current set of expansion definitions we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000401 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500402static struct hash_table expdefs;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000403
404/*
405 * The current set of single-line macros we have defined.
406 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700407static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000408
409/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500410 * Linked List of all active expansion definitions
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000411 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500412struct ExpDef *expansions = NULL;
413
414/*
415 * The expansion we are currently defining
416 */
417static ExpDef *defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000418
Charles Crayned4200be2008-07-12 16:42:33 -0700419static uint64_t nested_mac_count;
420static uint64_t nested_rep_count;
421
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000422/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500423 * Linked-list of lines to preprocess, prior to cleanup
424 */
425static Line *finals = NULL;
426static bool in_final = false;
427
428/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000429 * The number of macro parameters to allocate space for at a time.
430 */
431#define PARAM_DELTA 16
432
433/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700434 * The standard macro set: defined in macros.c in the array nasm_stdmac.
435 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000436 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700437static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000438
439/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000440 * The extra standard macros that come from the object format, if
441 * any.
442 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700443static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700444static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000445
446/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000447 * Tokens are allocated in blocks to improve speed
448 */
449#define TOKEN_BLOCKSIZE 4096
450static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000451struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000452 Blocks *next;
453 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000454};
455
456static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000457
458/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000459 * Forward declarations.
460 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000461static Token *expand_mmac_params(Token * tline);
462static Token *expand_smacro(Token * tline);
463static Token *expand_id(Token * tline);
Cyrill Gorcunov290eac72011-06-28 01:59:05 +0400464static Context *get_ctx(const char *name, const char **namep);
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 Gorcunov194ba892011-06-30 01:16:35 +0400486/*
487 * A few helpers for single macros
488 */
489
490/* We might be not smacro parameter at all */
491static bool is_smacro_param(Token *t)
492{
493 return t->type >= TOK_SMAC_PARAM;
494}
495
496/* smacro parameters are counted in a special way */
497static int smacro_get_param_idx(Token *t)
498{
499 return t->type - TOK_SMAC_PARAM;
500}
501
502/* encode smacro parameter index */
503static int smacro_set_param_idx(Token *t, unsigned int index)
504{
505 return t->type = TOK_SMAC_PARAM + index;
506}
507
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300508#ifdef NASM_TRACE
509
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400510#define stringify(x) #x
511
Cyrill Gorcunov9d1141a2011-06-26 23:07:35 +0400512#define nasm_trace(msg, ...) printf("(%s:%d): " msg "\n", __func__, __LINE__, ##__VA_ARGS__)
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400513#define nasm_dump_token(t) nasm_raw_dump_token(t, __FILE__, __LINE__, __func__);
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400514#define nasm_dump_stream(t) nasm_raw_dump_stream(t, __FILE__, __LINE__, __func__);
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400515
516/* FIXME: we really need some compound type here instead of inplace code */
517static const char *nasm_get_tok_type_str(enum pp_token_type type)
518{
519#define SWITCH_TOK_NAME(type) \
520 case (type): \
521 return stringify(type)
522
523 switch (type) {
524 SWITCH_TOK_NAME(TOK_NONE);
525 SWITCH_TOK_NAME(TOK_WHITESPACE);
526 SWITCH_TOK_NAME(TOK_COMMENT);
527 SWITCH_TOK_NAME(TOK_ID);
528 SWITCH_TOK_NAME(TOK_PREPROC_ID);
529 SWITCH_TOK_NAME(TOK_STRING);
530 SWITCH_TOK_NAME(TOK_NUMBER);
531 SWITCH_TOK_NAME(TOK_FLOAT);
532 SWITCH_TOK_NAME(TOK_SMAC_END);
533 SWITCH_TOK_NAME(TOK_OTHER);
534 SWITCH_TOK_NAME(TOK_INTERNAL_STRING);
535 SWITCH_TOK_NAME(TOK_PREPROC_Q);
536 SWITCH_TOK_NAME(TOK_PREPROC_QQ);
537 SWITCH_TOK_NAME(TOK_PASTE);
538 SWITCH_TOK_NAME(TOK_INDIRECT);
539 SWITCH_TOK_NAME(TOK_SMAC_PARAM);
540 SWITCH_TOK_NAME(TOK_MAX);
541 }
542
543 return NULL;
544}
545
546static void nasm_raw_dump_token(Token *token, const char *file, int line, const char *func)
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300547{
548 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
549 if (token) {
550 Token *t;
551 list_for_each(t, token) {
552 if (t->text)
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400553 printf("'%s'(%s) ", t->text,
554 nasm_get_tok_type_str(t->type));
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300555 }
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400556 printf("\n\n");
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300557 }
558}
559
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400560static void nasm_raw_dump_stream(Token *token, const char *file, int line, const char *func)
561{
562 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
563 if (token) {
564 Token *t;
565 list_for_each(t, token)
566 printf("%s", t->text ? t->text : " ");
567 printf("\n\n");
568 }
569}
570
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400571#else
572#define nasm_trace(msg, ...)
573#define nasm_dump_token(t)
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400574#define nasm_dump_stream(t)
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300575#endif
576
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300577/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700578 * nasm_unquote with error if the string contains NUL characters.
579 * If the string contains NUL characters, issue an error and return
580 * the C len, i.e. truncate at the NUL.
581 */
582static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
583{
584 size_t len = nasm_unquote(qstr, NULL);
585 size_t clen = strlen(qstr);
586
587 if (len != clen)
588 error(ERR_NONFATAL, "NUL character in `%s' directive",
589 pp_directives[directive]);
590
591 return clen;
592}
593
594/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700595 * In-place reverse a list of tokens.
596 */
597static Token *reverse_tokens(Token *t)
598{
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400599 Token *prev, *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700600
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400601 list_reverse(t, prev, next);
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700602
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400603 return t;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700604}
605
606/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300607 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000608 * front of them. We do it here because I could not find any other
609 * place to do it for the moment, and it is a hack (ideally it would
610 * be nice to be able to use the NASM pre-processor to do it).
611 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000612static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000613{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000614 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400615 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000616
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400617 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000618
619 /* Binary search for the directive name */
620 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400621 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400622 q = nasm_skip_word(p);
623 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000624 if (len) {
625 oldchar = p[len];
626 p[len] = 0;
627 while (j - i > 1) {
628 k = (j + i) / 2;
629 m = nasm_stricmp(p, tasm_directives[k]);
630 if (m == 0) {
631 /* We have found a directive, so jam a % in front of it
632 * so that NASM will then recognise it as one if it's own.
633 */
634 p[len] = oldchar;
635 len = strlen(p);
636 oldline = line;
637 line = nasm_malloc(len + 2);
638 line[0] = '%';
639 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700640 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300641 * NASM does not recognise IFDIFI, so we convert
642 * it to %if 0. This is not used in NASM
643 * compatible code, but does need to parse for the
644 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000645 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700646 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000647 } else {
648 memcpy(line + 1, p, len + 1);
649 }
650 nasm_free(oldline);
651 return line;
652 } else if (m < 0) {
653 j = k;
654 } else
655 i = k;
656 }
657 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000658 }
659 return line;
660}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000661
H. Peter Anvin76690a12002-04-30 20:52:49 +0000662/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000663 * The pre-preprocessing stage... This function translates line
664 * number indications as they emerge from GNU cpp (`# lineno "file"
665 * flags') into NASM preprocessor line number indications (`%line
666 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000667 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000668static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000669{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000670 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000671 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000672
H. Peter Anvine2c80182005-01-15 22:15:51 +0000673 if (line[0] == '#' && line[1] == ' ') {
674 oldline = line;
675 fname = oldline + 2;
676 lineno = atoi(fname);
677 fname += strspn(fname, "0123456789 ");
678 if (*fname == '"')
679 fname++;
680 fnlen = strcspn(fname, "\"");
681 line = nasm_malloc(20 + fnlen);
682 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
683 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000684 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000685 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000686 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000687 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000688}
689
690/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000691 * Free a linked list of tokens.
692 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000693static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000694{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400695 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000696 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000697}
698
699/*
700 * Free a linked list of lines.
701 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000702static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000703{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400704 Line *l, *tmp;
705 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000706 free_tlist(l->first);
707 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000708 }
709}
710
711/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500712 * Free an ExpDef
H. Peter Anvineba20a72002-04-30 20:53:55 +0000713 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500714static void free_expdef(ExpDef * ed)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000715{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500716 nasm_free(ed->name);
717 free_tlist(ed->dlist);
718 nasm_free(ed->defaults);
719 free_llist(ed->line);
720 nasm_free(ed);
721}
722
723/*
724 * Free an ExpInv
725 */
726static void free_expinv(ExpInv * ei)
727{
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +0400728 nasm_free(ei->name);
729 nasm_free(ei->label_text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300730 nasm_free(ei);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000731}
732
733/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700734 * Free all currently defined macros, and free the hash tables
735 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700736static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700737{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400738 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700739 const char *key;
740 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700741
H. Peter Anvin072771e2008-05-22 13:17:51 -0700742 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300743 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400744 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300745 nasm_free(s->name);
746 free_tlist(s->expansion);
747 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300748 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700749 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700750 hash_free(smt);
751}
752
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500753static void free_expdef_table(struct hash_table *edt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700754{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500755 ExpDef *ed, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700756 const char *key;
757 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700758
759 it = NULL;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500760 while ((ed = hash_iterate(edt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300761 nasm_free((void *)key);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500762 list_for_each_safe(ed ,tmp, ed)
763 free_expdef(ed);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700764 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500765 hash_free(edt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700766}
767
768static void free_macros(void)
769{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700770 free_smacro_table(&smacros);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500771 free_expdef_table(&expdefs);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700772}
773
774/*
775 * Initialize the hash tables
776 */
777static void init_macros(void)
778{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700779 hash_init(&smacros, HASH_LARGE);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500780 hash_init(&expdefs, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700781}
782
783/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000784 * Pop the context stack.
785 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000786static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000787{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000788 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000789
790 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700791 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000792 nasm_free(c->name);
793 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000794}
795
H. Peter Anvin072771e2008-05-22 13:17:51 -0700796/*
797 * Search for a key in the hash index; adding it if necessary
798 * (in which case we initialize the data pointer to NULL.)
799 */
800static void **
801hash_findi_add(struct hash_table *hash, const char *str)
802{
803 struct hash_insert hi;
804 void **r;
805 char *strx;
806
807 r = hash_findi(hash, str, &hi);
808 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300809 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700810
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300811 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700812 return hash_add(&hi, strx, NULL);
813}
814
815/*
816 * Like hash_findi, but returns the data element rather than a pointer
817 * to it. Used only when not adding a new element, hence no third
818 * argument.
819 */
820static void *
821hash_findix(struct hash_table *hash, const char *str)
822{
823 void **p;
824
825 p = hash_findi(hash, str, NULL);
826 return p ? *p : NULL;
827}
828
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400829/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500830 * read line from standard macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400831 * if there no more left -- return NULL
832 */
833static char *line_from_stdmac(void)
834{
835 unsigned char c;
836 const unsigned char *p = stdmacpos;
837 char *line, *q;
838 size_t len = 0;
839
840 if (!stdmacpos)
841 return NULL;
842
843 while ((c = *p++)) {
844 if (c >= 0x80)
845 len += pp_directives_len[c - 0x80] + 1;
846 else
847 len++;
848 }
849
850 line = nasm_malloc(len + 1);
851 q = line;
852 while ((c = *stdmacpos++)) {
853 if (c >= 0x80) {
854 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
855 q += pp_directives_len[c - 0x80];
856 *q++ = ' ';
857 } else {
858 *q++ = c;
859 }
860 }
861 stdmacpos = p;
862 *q = '\0';
863
864 if (!*stdmacpos) {
865 /* This was the last of the standard macro chain... */
866 stdmacpos = NULL;
867 if (any_extrastdmac) {
868 stdmacpos = extrastdmac;
869 any_extrastdmac = false;
870 } else if (do_predef) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300871 ExpInv *ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400872 Line *pd, *l;
873 Token *head, **tail, *t;
874
875 /*
876 * Nasty hack: here we push the contents of
877 * `predef' on to the top-level expansion stack,
878 * since this is the most convenient way to
879 * implement the pre-include and pre-define
880 * features.
881 */
882 list_for_each(pd, predef) {
883 head = NULL;
884 tail = &head;
885 list_for_each(t, pd->first) {
886 *tail = new_Token(NULL, t->type, t->text, 0);
887 tail = &(*tail)->next;
888 }
889
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500890 l = new_Line();
891 l->first = head;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300892 ei = new_ExpInv(EXP_PREDEF, NULL);
893 ei->current = l;
894 ei->emitting = true;
895 ei->prev = istk->expansion;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500896 istk->expansion = ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400897 }
898 do_predef = false;
899 }
900 }
901
902 return line;
903}
904
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000905#define BUF_DELTA 512
906/*
907 * Read a line from the top file in istk, handling multiple CR/LFs
908 * at the end of the line read, and handling spurious ^Zs. Will
909 * return lines from the standard macro set if this has not already
910 * been done.
911 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000912static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000913{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000914 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000915 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000916
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400917 /*
918 * standart macros set (predefined) goes first
919 */
920 p = line_from_stdmac();
921 if (p)
922 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700923
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400924 /*
925 * regular read from a file
926 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000927 bufsize = BUF_DELTA;
928 buffer = nasm_malloc(BUF_DELTA);
929 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000930 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000931 while (1) {
932 q = fgets(p, bufsize - (p - buffer), istk->fp);
933 if (!q)
934 break;
935 p += strlen(p);
936 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300937 /*
938 * Convert backslash-CRLF line continuation sequences into
939 * nothing at all (for DOS and Windows)
940 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000941 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
942 p -= 3;
943 *p = 0;
944 continued_count++;
945 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300946 /*
947 * Also convert backslash-LF line continuation sequences into
948 * nothing at all (for Unix)
949 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000950 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
951 p -= 2;
952 *p = 0;
953 continued_count++;
954 } else {
955 break;
956 }
957 }
958 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000959 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000960 bufsize += BUF_DELTA;
961 buffer = nasm_realloc(buffer, bufsize);
962 p = buffer + offset; /* prevent stale-pointer problems */
963 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000964 }
965
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966 if (!q && p == buffer) {
967 nasm_free(buffer);
968 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000969 }
970
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300971 src_set_linnum(src_get_linnum() + istk->lineinc +
972 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000973
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000974 /*
975 * Play safe: remove CRs as well as LFs, if any of either are
976 * present at the end of the line.
977 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000978 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000979 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000980
981 /*
982 * Handle spurious ^Z, which may be inserted into source files
983 * by some file transfer utilities.
984 */
985 buffer[strcspn(buffer, "\032")] = '\0';
986
H. Peter Anvin734b1882002-04-30 21:01:08 +0000987 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000988
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000989 return buffer;
990}
991
992/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000993 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000994 * don't need to parse the value out of e.g. numeric tokens: we
995 * simply split one string into many.
996 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000997static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000998{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700999 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001000 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001001 Token *list = NULL;
1002 Token *t, **tail = &list;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001003 bool verbose = true;
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +04001004
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +04001005 nasm_trace("Tokenize for '%s'", line);
1006
Keith Kanios6faad4e2010-12-18 14:08:02 -06001007 if ((defining != NULL) && (defining->ignoring == true)) {
1008 verbose = false;
1009 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001010
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 while (*line) {
1012 p = line;
1013 if (*p == '%') {
1014 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001015 if (*p == '+' && !nasm_isdigit(p[1])) {
1016 p++;
1017 type = TOK_PASTE;
1018 } else if (nasm_isdigit(*p) ||
1019 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001020 do {
1021 p++;
1022 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001023 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001024 type = TOK_PREPROC_ID;
1025 } else if (*p == '{') {
1026 p++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001027 while (*p && *p != '}') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001028 p[-1] = *p;
1029 p++;
1030 }
1031 p[-1] = '\0';
1032 if (*p)
1033 p++;
1034 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001035 } else if (*p == '[') {
1036 int lvl = 1;
1037 line += 2; /* Skip the leading %[ */
1038 p++;
1039 while (lvl && (c = *p++)) {
1040 switch (c) {
1041 case ']':
1042 lvl--;
1043 break;
1044 case '%':
1045 if (*p == '[')
1046 lvl++;
1047 break;
1048 case '\'':
1049 case '\"':
1050 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +04001051 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001052 break;
1053 default:
1054 break;
1055 }
1056 }
1057 p--;
1058 if (*p)
1059 *p++ = '\0';
Keith Kanios6faad4e2010-12-18 14:08:02 -06001060 if (lvl && verbose)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001061 error(ERR_NONFATAL, "unterminated %[ construct");
1062 type = TOK_INDIRECT;
1063 } else if (*p == '?') {
1064 type = TOK_PREPROC_Q; /* %? */
1065 p++;
1066 if (*p == '?') {
1067 type = TOK_PREPROC_QQ; /* %?? */
1068 p++;
1069 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001070 } else if (*p == '!') {
1071 type = TOK_PREPROC_ID;
1072 p++;
1073 if (isidchar(*p)) {
1074 do {
1075 p++;
1076 } while (isidchar(*p));
1077 } else if (*p == '\'' || *p == '\"' || *p == '`') {
1078 p = nasm_skip_string(p);
1079 if (*p)
1080 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001081 else if(verbose)
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001082 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
1083 } else {
1084 /* %! without string or identifier */
1085 type = TOK_OTHER; /* Legacy behavior... */
1086 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001087 } else if (isidchar(*p) ||
1088 ((*p == '!' || *p == '%' || *p == '$') &&
1089 isidchar(p[1]))) {
1090 do {
1091 p++;
1092 }
1093 while (isidchar(*p));
1094 type = TOK_PREPROC_ID;
1095 } else {
1096 type = TOK_OTHER;
1097 if (*p == '%')
1098 p++;
1099 }
1100 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1101 type = TOK_ID;
1102 p++;
1103 while (*p && isidchar(*p))
1104 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001105 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001106 /*
1107 * A string token.
1108 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001109 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001110 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001111
H. Peter Anvine2c80182005-01-15 22:15:51 +00001112 if (*p) {
1113 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001114 } else if(verbose) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001115 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001116 /* Handling unterminated strings by UNV */
1117 /* type = -1; */
1118 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001119 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001120 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001121 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001122 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001123 bool is_hex = false;
1124 bool is_float = false;
1125 bool has_e = false;
1126 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001127
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001129 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001130 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001131
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001132 if (*p == '$') {
1133 p++;
1134 is_hex = true;
1135 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001136
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001137 for (;;) {
1138 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001139
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001140 if (!is_hex && (c == 'e' || c == 'E')) {
1141 has_e = true;
1142 if (*p == '+' || *p == '-') {
1143 /*
1144 * e can only be followed by +/- if it is either a
1145 * prefixed hex number or a floating-point number
1146 */
1147 p++;
1148 is_float = true;
1149 }
1150 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1151 is_hex = true;
1152 } else if (c == 'P' || c == 'p') {
1153 is_float = true;
1154 if (*p == '+' || *p == '-')
1155 p++;
1156 } else if (isnumchar(c) || c == '_')
1157 ; /* just advance */
1158 else if (c == '.') {
1159 /*
1160 * we need to deal with consequences of the legacy
1161 * parser, like "1.nolist" being two tokens
1162 * (TOK_NUMBER, TOK_ID) here; at least give it
1163 * a shot for now. In the future, we probably need
1164 * a flex-based scanner with proper pattern matching
1165 * to do it as well as it can be done. Nothing in
1166 * the world is going to help the person who wants
1167 * 0x123.p16 interpreted as two tokens, though.
1168 */
1169 r = p;
1170 while (*r == '_')
1171 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001172
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001173 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1174 (!is_hex && (*r == 'e' || *r == 'E')) ||
1175 (*r == 'p' || *r == 'P')) {
1176 p = r;
1177 is_float = true;
1178 } else
1179 break; /* Terminate the token */
1180 } else
1181 break;
1182 }
1183 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001184
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001185 if (p == line+1 && *line == '$') {
1186 type = TOK_OTHER; /* TOKEN_HERE */
1187 } else {
1188 if (has_e && !is_hex) {
1189 /* 1e13 is floating-point, but 1e13h is not */
1190 is_float = true;
1191 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001192
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001193 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1194 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001195 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001196 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001197 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198 /*
1199 * Whitespace just before end-of-line is discarded by
1200 * pretending it's a comment; whitespace just before a
1201 * comment gets lumped into the comment.
1202 */
1203 if (!*p || *p == ';') {
1204 type = TOK_COMMENT;
1205 while (*p)
1206 p++;
1207 }
1208 } else if (*p == ';') {
1209 type = TOK_COMMENT;
1210 while (*p)
1211 p++;
1212 } else {
1213 /*
1214 * Anything else is an operator of some kind. We check
1215 * for all the double-character operators (>>, <<, //,
1216 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001217 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001218 */
1219 type = TOK_OTHER;
1220 if ((p[0] == '>' && p[1] == '>') ||
1221 (p[0] == '<' && p[1] == '<') ||
1222 (p[0] == '/' && p[1] == '/') ||
1223 (p[0] == '<' && p[1] == '=') ||
1224 (p[0] == '>' && p[1] == '=') ||
1225 (p[0] == '=' && p[1] == '=') ||
1226 (p[0] == '!' && p[1] == '=') ||
1227 (p[0] == '<' && p[1] == '>') ||
1228 (p[0] == '&' && p[1] == '&') ||
1229 (p[0] == '|' && p[1] == '|') ||
1230 (p[0] == '^' && p[1] == '^')) {
1231 p++;
1232 }
1233 p++;
1234 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001235
H. Peter Anvine2c80182005-01-15 22:15:51 +00001236 /* Handling unterminated string by UNV */
1237 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001238 {
1239 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1240 t->text[p-line] = *line;
1241 tail = &t->next;
1242 }
1243 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001244 if (type != TOK_COMMENT) {
1245 *tail = t = new_Token(NULL, type, line, p - line);
1246 tail = &t->next;
1247 }
1248 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001249 }
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +04001250
1251 nasm_dump_token(list);
1252
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001253 return list;
1254}
1255
H. Peter Anvince616072002-04-30 21:02:23 +00001256/*
1257 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001258 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001259 * deleted only all at once by the delete_Blocks function.
1260 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001261static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001262{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001263 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001264
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001265 /* first, get to the end of the linked list */
1266 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001267 b = b->next;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001268
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001269 /* now allocate the requested chunk */
1270 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001271
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001272 /* now allocate a new block for the next request */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001273 b->next = nasm_zalloc(sizeof(Blocks));
1274
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001275 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001276}
1277
1278/*
1279 * this function deletes all managed blocks of memory
1280 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001281static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001282{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001283 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001284
H. Peter Anvin70653092007-10-19 14:42:29 -07001285 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001286 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001287 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001288 * free it.
1289 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001290 while (b) {
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04001291 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 a = b;
1293 b = b->next;
1294 if (a != &blocks)
1295 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001296 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001297}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001298
1299/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001300 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001301 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001302 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001303 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001304static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001305 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001306{
1307 Token *t;
1308 int i;
1309
H. Peter Anvin89cee572009-07-15 09:16:54 -04001310 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1312 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1313 freeTokens[i].next = &freeTokens[i + 1];
1314 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001315 }
1316 t = freeTokens;
1317 freeTokens = t->next;
1318 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001319 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001320 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001321 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001322 t->text = NULL;
1323 } else {
1324 if (txtlen == 0)
1325 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001326 t->text = nasm_malloc(txtlen+1);
1327 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001328 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001329 }
1330 return t;
1331}
1332
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001333static Token *copy_Token(Token * tline)
1334{
1335 Token *t, *tt, *first = NULL, *prev = NULL;
1336 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001337 for (tt = tline; tt != NULL; tt = tt->next) {
1338 if (!freeTokens) {
1339 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1340 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1341 freeTokens[i].next = &freeTokens[i + 1];
1342 freeTokens[i].next = NULL;
1343 }
1344 t = freeTokens;
1345 freeTokens = t->next;
1346 t->next = NULL;
1347 t->text = tt->text ? nasm_strdup(tt->text) : NULL;
1348 t->a.mac = tt->a.mac;
1349 t->a.len = tt->a.len;
1350 t->type = tt->type;
1351 if (prev != NULL) {
1352 prev->next = t;
1353 } else {
1354 first = t;
1355 }
1356 prev = t;
1357 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001358 return first;
1359}
1360
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001362{
1363 Token *next = t->next;
1364 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001365 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001366 freeTokens = t;
1367 return next;
1368}
1369
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001370/*
1371 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001372 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1373 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001374 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001375static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001376{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001377 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001378 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001379 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001380 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001381
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001382 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001383 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001384 char *v;
1385 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001386
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001387 v = t->text + 2;
1388 if (*v == '\'' || *v == '\"' || *v == '`') {
1389 size_t len = nasm_unquote(v, NULL);
1390 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001391
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001392 if (len != clen) {
1393 error(ERR_NONFATAL | ERR_PASS1,
1394 "NUL character in %! string");
1395 v = NULL;
1396 }
1397 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001398
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001399 if (v) {
1400 char *p = getenv(v);
1401 if (!p) {
1402 error(ERR_NONFATAL | ERR_PASS1,
1403 "nonexistent environment variable `%s'", v);
1404 p = "";
1405 }
1406 t->text = nasm_strdup(p);
1407 }
1408 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001409 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001410
H. Peter Anvine2c80182005-01-15 22:15:51 +00001411 /* Expand local macros here and not during preprocessing */
1412 if (expand_locals &&
1413 t->type == TOK_PREPROC_ID && t->text &&
1414 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001415 const char *q;
1416 char *p;
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001417 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001418 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001419 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001420 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001421 p = nasm_strcat(buffer, q);
1422 nasm_free(t->text);
1423 t->text = p;
1424 }
1425 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001426
1427 /* Expand %? and %?? directives */
Keith Kanios3136d482010-11-13 09:34:34 -06001428 if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001429 ((t->type == TOK_PREPROC_Q) ||
1430 (t->type == TOK_PREPROC_QQ))) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001431 ExpInv *ei;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001432 for (ei = istk->expansion; ei != NULL; ei = ei->prev){
1433 if (ei->type == EXP_MMACRO) {
1434 nasm_free(t->text);
1435 if (t->type == TOK_PREPROC_Q) {
1436 t->text = nasm_strdup(ei->name);
1437 } else {
1438 t->text = nasm_strdup(ei->def->name);
1439 }
1440 break;
1441 }
1442 }
1443 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001444
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001445 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001446 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001447 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001448 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001449 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001450
H. Peter Anvin734b1882002-04-30 21:01:08 +00001451 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001452
1453 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001454 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001455 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001456 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001457 q = t->text;
1458 while (*q)
1459 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001460 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001461 }
1462 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001463
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001464 return line;
1465}
1466
1467/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001468 * Initialize a new Line
1469 */
Cyrill Gorcunov29cb0bb2010-11-11 11:19:43 +03001470static inline Line *new_Line(void)
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001471{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001472 return (Line *)nasm_zalloc(sizeof(Line));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001473}
1474
1475
1476/*
1477 * Initialize a new Expansion Definition
1478 */
1479static ExpDef *new_ExpDef(int exp_type)
1480{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001481 ExpDef *ed = (ExpDef*)nasm_zalloc(sizeof(ExpDef));
1482 ed->type = exp_type;
1483 ed->casesense = true;
1484 ed->state = COND_NEVER;
1485
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001486 return ed;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001487}
1488
1489
1490/*
1491 * Initialize a new Expansion Instance
1492 */
1493static ExpInv *new_ExpInv(int exp_type, ExpDef *ed)
1494{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001495 ExpInv *ei = (ExpInv*)nasm_zalloc(sizeof(ExpInv));
1496 ei->type = exp_type;
1497 ei->def = ed;
1498 ei->unique = ++unique;
1499
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001500 if ((istk->mmac_depth < 1) &&
1501 (istk->expansion == NULL) &&
1502 (ed != NULL) &&
1503 (ed->type != EXP_MMACRO) &&
1504 (ed->type != EXP_REP) &&
1505 (ed->type != EXP_WHILE)) {
1506 ei->linnum = src_get_linnum();
1507 src_set_linnum(ei->linnum - ed->linecount - 1);
1508 } else {
1509 ei->linnum = -1;
1510 }
1511 if ((istk->expansion == NULL) ||
1512 (ei->type == EXP_MMACRO)) {
1513 ei->relno = 0;
1514 } else {
1515 ei->relno = istk->expansion->lineno;
1516 if (ed != NULL) {
1517 ei->relno -= (ed->linecount + 1);
1518 }
1519 }
1520 return ei;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001521}
1522
1523/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001524 * A scanner, suitable for use by the expression evaluator, which
1525 * operates on a line of Tokens. Expects a pointer to a pointer to
1526 * the first token in the line to be passed in as its private_data
1527 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001528 *
1529 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001530 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001531static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001532{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001533 Token **tlineptr = private_data;
1534 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001535 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001536
H. Peter Anvine2c80182005-01-15 22:15:51 +00001537 do {
1538 tline = *tlineptr;
1539 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001540 } while (tline && (tline->type == TOK_WHITESPACE ||
1541 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001542
1543 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001544 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001545
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001546 tokval->t_charptr = tline->text;
1547
H. Peter Anvin76690a12002-04-30 20:52:49 +00001548 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001549 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001550 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001551 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001552
H. Peter Anvine2c80182005-01-15 22:15:51 +00001553 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001554 p = tokval->t_charptr = tline->text;
1555 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001556 tokval->t_charptr++;
1557 return tokval->t_type = TOKEN_ID;
1558 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001559
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001560 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001561 if (r >= p+MAX_KEYWORD)
1562 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001563 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001564 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001565 *s = '\0';
1566 /* right, so we have an identifier sitting in temp storage. now,
1567 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001568 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001569 }
1570
H. Peter Anvine2c80182005-01-15 22:15:51 +00001571 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001572 bool rn_error;
1573 tokval->t_integer = readnum(tline->text, &rn_error);
1574 tokval->t_charptr = tline->text;
1575 if (rn_error)
1576 return tokval->t_type = TOKEN_ERRNUM;
1577 else
1578 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001579 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001580
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001581 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001582 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001583 }
1584
H. Peter Anvine2c80182005-01-15 22:15:51 +00001585 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001586 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001587
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001588 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001589 tokval->t_charptr = tline->text;
1590 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001591
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001592 if (ep[0] != bq || ep[1] != '\0')
1593 return tokval->t_type = TOKEN_ERRSTR;
1594 else
1595 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001596 }
1597
H. Peter Anvine2c80182005-01-15 22:15:51 +00001598 if (tline->type == TOK_OTHER) {
1599 if (!strcmp(tline->text, "<<"))
1600 return tokval->t_type = TOKEN_SHL;
1601 if (!strcmp(tline->text, ">>"))
1602 return tokval->t_type = TOKEN_SHR;
1603 if (!strcmp(tline->text, "//"))
1604 return tokval->t_type = TOKEN_SDIV;
1605 if (!strcmp(tline->text, "%%"))
1606 return tokval->t_type = TOKEN_SMOD;
1607 if (!strcmp(tline->text, "=="))
1608 return tokval->t_type = TOKEN_EQ;
1609 if (!strcmp(tline->text, "<>"))
1610 return tokval->t_type = TOKEN_NE;
1611 if (!strcmp(tline->text, "!="))
1612 return tokval->t_type = TOKEN_NE;
1613 if (!strcmp(tline->text, "<="))
1614 return tokval->t_type = TOKEN_LE;
1615 if (!strcmp(tline->text, ">="))
1616 return tokval->t_type = TOKEN_GE;
1617 if (!strcmp(tline->text, "&&"))
1618 return tokval->t_type = TOKEN_DBL_AND;
1619 if (!strcmp(tline->text, "^^"))
1620 return tokval->t_type = TOKEN_DBL_XOR;
1621 if (!strcmp(tline->text, "||"))
1622 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001623 }
1624
1625 /*
1626 * We have no other options: just return the first character of
1627 * the token text.
1628 */
1629 return tokval->t_type = tline->text[0];
1630}
1631
1632/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001633 * Compare a string to the name of an existing macro; this is a
1634 * simple wrapper which calls either strcmp or nasm_stricmp
1635 * depending on the value of the `casesense' parameter.
1636 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001637static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001638{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001639 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001640}
1641
1642/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001643 * Compare a string to the name of an existing macro; this is a
1644 * simple wrapper which calls either strcmp or nasm_stricmp
1645 * depending on the value of the `casesense' parameter.
1646 */
1647static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1648{
1649 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1650}
1651
1652/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001653 * Return the Context structure associated with a %$ token. Return
1654 * NULL, having _already_ reported an error condition, if the
1655 * context stack isn't deep enough for the supplied number of $
1656 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001657 *
1658 * If "namep" is non-NULL, set it to the pointer to the macro name
1659 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001660 */
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001661static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001662{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001663 Context *ctx;
1664 int i;
1665
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001666 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001667 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001668
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001669 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001670 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001671
H. Peter Anvine2c80182005-01-15 22:15:51 +00001672 if (!cstk) {
1673 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1674 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001675 }
1676
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001677 name += 2;
1678 ctx = cstk;
1679 i = 0;
1680 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001681 name++;
1682 i++;
1683 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001684 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001685
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 if (!ctx) {
1687 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001688 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001689 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001690 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001691
1692 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001693 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001694
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001695 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001696}
1697
1698/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001699 * Check to see if a file is already in a string list
1700 */
1701static bool in_list(const StrList *list, const char *str)
1702{
1703 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001704 if (!strcmp(list->str, str))
1705 return true;
1706 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001707 }
1708 return false;
1709}
1710
1711/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001712 * Open an include file. This routine must always return a valid
1713 * file pointer if it returns - it's responsible for throwing an
1714 * ERR_FATAL and bombing out completely if not. It should also try
1715 * the include path one by one until it finds the file or reaches
1716 * the end of the path.
1717 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001718static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001719 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001720{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001721 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001722 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001723 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001724 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001725 size_t prefix_len = 0;
1726 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001727
H. Peter Anvine2c80182005-01-15 22:15:51 +00001728 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001729 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunov6f38fe62010-11-11 11:32:16 +03001730 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001731 memcpy(sl->str, prefix, prefix_len);
1732 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001733 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001734 if (fp && dhead && !in_list(*dhead, sl->str)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001735 **dtail = sl;
1736 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001737 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001738 nasm_free(sl);
1739 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001740 if (fp)
1741 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001742 if (!ip) {
1743 if (!missing_ok)
1744 break;
1745 prefix = NULL;
1746 } else {
1747 prefix = ip->path;
1748 ip = ip->next;
1749 }
1750 if (prefix) {
1751 prefix_len = strlen(prefix);
1752 } else {
1753 /* -MG given and file not found */
1754 if (dhead && !in_list(*dhead, file)) {
1755 sl = nasm_malloc(len+1+sizeof sl->next);
1756 sl->next = NULL;
1757 strcpy(sl->str, file);
1758 **dtail = sl;
1759 *dtail = &sl->next;
1760 }
1761 return NULL;
1762 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001763 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001764
H. Peter Anvin734b1882002-04-30 21:01:08 +00001765 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001766 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001767}
1768
1769/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001770 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001771 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001772 * return true if _any_ single-line macro of that name is defined.
1773 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001774 * `nparam' or no parameters is defined.
1775 *
1776 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001777 * defined, or nparam is -1, the address of the definition structure
1778 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001779 * is NULL, no action will be taken regarding its contents, and no
1780 * error will occur.
1781 *
1782 * Note that this is also called with nparam zero to resolve
1783 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001784 *
1785 * If you already know which context macro belongs to, you can pass
1786 * the context pointer as first parameter; if you won't but name begins
1787 * with %$ the context will be automatically computed. If all_contexts
1788 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001789 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001790static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001791smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001792 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001793{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001794 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001795 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001796
H. Peter Anvin97a23472007-09-16 17:57:25 -07001797 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001798 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001799 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001800 if (cstk)
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001801 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001803 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001804 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001805 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001806 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001807 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001808 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001809
H. Peter Anvine2c80182005-01-15 22:15:51 +00001810 while (m) {
1811 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001812 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001813 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001814 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001815 *defn = m;
1816 else
1817 *defn = NULL;
1818 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001819 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001820 }
1821 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001822 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001823
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001824 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001825}
1826
1827/*
1828 * Count and mark off the parameters in a multi-line macro call.
1829 * This is called both from within the multi-line macro expansion
1830 * code, and also to mark off the default parameters when provided
1831 * in a %macro definition line.
1832 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001833static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001834{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001835 int paramsize, brace;
1836
1837 *nparam = paramsize = 0;
1838 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001839 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001840 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001841 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001842 paramsize += PARAM_DELTA;
1843 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1844 }
1845 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001846 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001847 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001848 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001849 (*params)[(*nparam)++] = t;
1850 while (tok_isnt_(t, brace ? "}" : ","))
1851 t = t->next;
1852 if (t) { /* got a comma/brace */
1853 t = t->next;
1854 if (brace) {
1855 /*
1856 * Now we've found the closing brace, look further
1857 * for the comma.
1858 */
1859 skip_white_(t);
1860 if (tok_isnt_(t, ",")) {
1861 error(ERR_NONFATAL,
1862 "braces do not enclose all of macro parameter");
1863 while (tok_isnt_(t, ","))
1864 t = t->next;
1865 }
1866 if (t)
1867 t = t->next; /* eat the comma */
1868 }
1869 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001870 }
1871}
1872
1873/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001874 * Determine whether one of the various `if' conditions is true or
1875 * not.
1876 *
1877 * We must free the tline we get passed.
1878 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001879static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001880{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001881 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001882 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001883 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001884 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001885 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001886 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001887 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001888
1889 origline = tline;
1890
H. Peter Anvine2c80182005-01-15 22:15:51 +00001891 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001892 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001893 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001894 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001895 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001896 if (!tline)
1897 break;
1898 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001899 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001900 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001901 free_tlist(origline);
1902 return -1;
1903 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001904 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001905 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001906 tline = tline->next;
1907 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001908 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001909
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001910 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001911 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001912 while (tline) {
1913 skip_white_(tline);
1914 if (!tline || (tline->type != TOK_ID &&
1915 (tline->type != TOK_PREPROC_ID ||
1916 tline->text[1] != '$'))) {
1917 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001918 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001919 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001920 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001921 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001922 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001923 tline = tline->next;
1924 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001925 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001926
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001927 case PPC_IFENV:
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001928 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001929 j = false; /* have we matched yet? */
1930 while (tline) {
1931 skip_white_(tline);
1932 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001933 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001934 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001935 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001936 error(ERR_NONFATAL,
1937 "`%s' expects environment variable names",
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001938 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001939 goto fail;
1940 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001941 p = tline->text;
1942 if (tline->type == TOK_PREPROC_ID)
1943 p += 2; /* Skip leading %! */
1944 if (*p == '\'' || *p == '\"' || *p == '`')
1945 nasm_unquote_cstr(p, ct);
1946 if (getenv(p))
1947 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001948 tline = tline->next;
1949 }
1950 break;
1951
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001952 case PPC_IFIDN:
1953 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001954 tline = expand_smacro(tline);
1955 t = tt = tline;
1956 while (tok_isnt_(tt, ","))
1957 tt = tt->next;
1958 if (!tt) {
1959 error(ERR_NONFATAL,
1960 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001961 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001962 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001963 }
1964 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001965 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001966 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1967 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1968 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001969 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001970 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001971 }
1972 if (t->type == TOK_WHITESPACE) {
1973 t = t->next;
1974 continue;
1975 }
1976 if (tt->type == TOK_WHITESPACE) {
1977 tt = tt->next;
1978 continue;
1979 }
1980 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001981 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001982 break;
1983 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001984 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001985 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001986 size_t l1 = nasm_unquote(t->text, NULL);
1987 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001988
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001989 if (l1 != l2) {
1990 j = false;
1991 break;
1992 }
1993 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1994 j = false;
1995 break;
1996 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001997 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001998 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001999 break;
2000 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002001
H. Peter Anvine2c80182005-01-15 22:15:51 +00002002 t = t->next;
2003 tt = tt->next;
2004 }
2005 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002006 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002007 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002008
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002009 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002010 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002011 bool found = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002012 ExpDef searching, *ed;
H. Peter Anvin65747262002-05-07 00:10:05 +00002013
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002014 skip_white_(tline);
2015 tline = expand_id(tline);
2016 if (!tok_type_(tline, TOK_ID)) {
2017 error(ERR_NONFATAL,
2018 "`%s' expects a macro name", pp_directives[ct]);
2019 goto fail;
2020 }
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002021 memset(&searching, 0, sizeof(searching));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002022 searching.name = nasm_strdup(tline->text);
2023 searching.casesense = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002024 searching.nparam_max = INT_MAX;
2025 tline = expand_smacro(tline->next);
2026 skip_white_(tline);
2027 if (!tline) {
2028 } else if (!tok_type_(tline, TOK_NUMBER)) {
2029 error(ERR_NONFATAL,
2030 "`%s' expects a parameter count or nothing",
2031 pp_directives[ct]);
2032 } else {
2033 searching.nparam_min = searching.nparam_max =
2034 readnum(tline->text, &j);
2035 if (j)
2036 error(ERR_NONFATAL,
2037 "unable to parse parameter count `%s'",
2038 tline->text);
2039 }
2040 if (tline && tok_is_(tline->next, "-")) {
2041 tline = tline->next->next;
2042 if (tok_is_(tline, "*"))
2043 searching.nparam_max = INT_MAX;
2044 else if (!tok_type_(tline, TOK_NUMBER))
2045 error(ERR_NONFATAL,
2046 "`%s' expects a parameter count after `-'",
2047 pp_directives[ct]);
2048 else {
2049 searching.nparam_max = readnum(tline->text, &j);
2050 if (j)
2051 error(ERR_NONFATAL,
2052 "unable to parse parameter count `%s'",
2053 tline->text);
2054 if (searching.nparam_min > searching.nparam_max)
2055 error(ERR_NONFATAL,
2056 "minimum parameter count exceeds maximum");
2057 }
2058 }
2059 if (tline && tok_is_(tline->next, "+")) {
2060 tline = tline->next;
2061 searching.plus = true;
2062 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002063 ed = (ExpDef *) hash_findix(&expdefs, searching.name);
2064 while (ed != NULL) {
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002065 if (!strcmp(ed->name, searching.name) &&
2066 (ed->nparam_min <= searching.nparam_max || searching.plus) &&
2067 (searching.nparam_min <= ed->nparam_max || ed->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002068 found = true;
2069 break;
2070 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002071 ed = ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002072 }
2073 if (tline && tline->next)
2074 error(ERR_WARNING|ERR_PASS1,
2075 "trailing garbage after %%ifmacro ignored");
2076 nasm_free(searching.name);
2077 j = found;
2078 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002079 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002080
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002081 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002082 needtype = TOK_ID;
2083 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002084 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002085 needtype = TOK_NUMBER;
2086 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002087 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002088 needtype = TOK_STRING;
2089 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002090
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002091iftype:
2092 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002093
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002094 while (tok_type_(t, TOK_WHITESPACE) ||
2095 (needtype == TOK_NUMBER &&
2096 tok_type_(t, TOK_OTHER) &&
2097 (t->text[0] == '-' || t->text[0] == '+') &&
2098 !t->text[1]))
2099 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002100
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002101 j = tok_type_(t, needtype);
2102 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002103
2104 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002105 t = tline = expand_smacro(tline);
2106 while (tok_type_(t, TOK_WHITESPACE))
2107 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002108
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002109 j = false;
2110 if (t) {
2111 t = t->next; /* Skip the actual token */
2112 while (tok_type_(t, TOK_WHITESPACE))
2113 t = t->next;
2114 j = !t; /* Should be nothing left */
2115 }
2116 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002117
H. Peter Anvin134b9462008-02-16 17:01:40 -08002118 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002119 t = tline = expand_smacro(tline);
2120 while (tok_type_(t, TOK_WHITESPACE))
2121 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002122
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002123 j = !t; /* Should be empty */
2124 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002125
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002126 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002127 t = tline = expand_smacro(tline);
2128 tptr = &t;
2129 tokval.t_type = TOKEN_INVALID;
2130 evalresult = evaluate(ppscan, tptr, &tokval,
2131 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002132 if (!evalresult)
2133 return -1;
2134 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002135 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002136 "trailing garbage after expression ignored");
2137 if (!is_simple(evalresult)) {
2138 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002139 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002140 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002141 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002142 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002143 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002144
H. Peter Anvine2c80182005-01-15 22:15:51 +00002145 default:
2146 error(ERR_FATAL,
2147 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002148 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002149 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002150 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002151
2152 free_tlist(origline);
2153 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002154
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002155fail:
2156 free_tlist(origline);
2157 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002158}
2159
2160/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002161 * Common code for defining an smacro
2162 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002163static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002164 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002165{
2166 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002167 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002168
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002169 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002170 if (!smac) {
2171 error(ERR_WARNING|ERR_PASS1,
2172 "single-line macro `%s' defined both with and"
2173 " without parameters", mname);
2174 /*
2175 * Some instances of the old code considered this a failure,
2176 * some others didn't. What is the right thing to do here?
2177 */
2178 free_tlist(expansion);
2179 return false; /* Failure */
2180 } else {
2181 /*
2182 * We're redefining, so we have to take over an
2183 * existing SMacro structure. This means freeing
2184 * what was already in it.
2185 */
2186 nasm_free(smac->name);
2187 free_tlist(smac->expansion);
2188 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002189 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002190 smtbl = ctx ? &ctx->localmac : &smacros;
2191 smhead = (SMacro **) hash_findi_add(smtbl, mname);
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002192 smac = nasm_zalloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002193 smac->next = *smhead;
2194 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002195 }
2196 smac->name = nasm_strdup(mname);
2197 smac->casesense = casesense;
2198 smac->nparam = nparam;
2199 smac->expansion = expansion;
2200 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002201 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002202}
2203
2204/*
2205 * Undefine an smacro
2206 */
2207static void undef_smacro(Context *ctx, const char *mname)
2208{
2209 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002210 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002211
H. Peter Anvin166c2472008-05-28 12:28:58 -07002212 smtbl = ctx ? &ctx->localmac : &smacros;
2213 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002214
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002215 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002216 /*
2217 * We now have a macro name... go hunt for it.
2218 */
2219 sp = smhead;
2220 while ((s = *sp) != NULL) {
2221 if (!mstrcmp(s->name, mname, s->casesense)) {
2222 *sp = s->next;
2223 nasm_free(s->name);
2224 free_tlist(s->expansion);
2225 nasm_free(s);
2226 } else {
2227 sp = &s->next;
2228 }
2229 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002230 }
2231}
2232
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002233/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002234 * Parse a mmacro specification.
2235 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002236static bool parse_mmacro_spec(Token *tline, ExpDef *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002237{
2238 bool err;
2239
2240 tline = tline->next;
2241 skip_white_(tline);
2242 tline = expand_id(tline);
2243 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002244 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2245 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002246 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002247
H. Peter Anvina26433d2008-07-16 14:40:01 -07002248 def->name = nasm_strdup(tline->text);
2249 def->plus = false;
2250 def->nolist = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002251// def->in_progress = 0;
2252// def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002253 def->nparam_min = 0;
2254 def->nparam_max = 0;
2255
H. Peter Anvina26433d2008-07-16 14:40:01 -07002256 tline = expand_smacro(tline->next);
2257 skip_white_(tline);
2258 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002259 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002260 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002261 def->nparam_min = def->nparam_max =
2262 readnum(tline->text, &err);
2263 if (err)
2264 error(ERR_NONFATAL,
2265 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002266 }
2267 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002268 tline = tline->next->next;
2269 if (tok_is_(tline, "*")) {
2270 def->nparam_max = INT_MAX;
2271 } else if (!tok_type_(tline, TOK_NUMBER)) {
2272 error(ERR_NONFATAL,
2273 "`%s' expects a parameter count after `-'", directive);
2274 } else {
2275 def->nparam_max = readnum(tline->text, &err);
2276 if (err) {
2277 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2278 tline->text);
2279 }
2280 if (def->nparam_min > def->nparam_max) {
2281 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2282 }
2283 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002284 }
2285 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002286 tline = tline->next;
2287 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002288 }
2289 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002290 !nasm_stricmp(tline->next->text, ".nolist")) {
2291 tline = tline->next;
2292 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002293 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002294
H. Peter Anvina26433d2008-07-16 14:40:01 -07002295 /*
2296 * Handle default parameters.
2297 */
2298 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002299 def->dlist = tline->next;
2300 tline->next = NULL;
2301 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002302 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002303 def->dlist = NULL;
2304 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002305 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002306 def->line = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002307
H. Peter Anvin89cee572009-07-15 09:16:54 -04002308 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002309 !def->plus)
2310 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2311 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002312
H. Peter Anvina26433d2008-07-16 14:40:01 -07002313 return true;
2314}
2315
2316
2317/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002318 * Decode a size directive
2319 */
2320static int parse_size(const char *str) {
2321 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002322 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002323 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002324 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002325
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002326 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002327}
2328
Ed Beroset3ab3f412002-06-11 03:31:49 +00002329/**
2330 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002331 * Find out if a line contains a preprocessor directive, and deal
2332 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002333 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002334 * If a directive _is_ found, it is the responsibility of this routine
2335 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002336 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002337 * @param tline a pointer to the current tokeninzed line linked list
2338 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002339 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002340 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002341static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002342{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002343 enum preproc_token i;
2344 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002345 bool err;
2346 int nparam;
2347 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002348 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002349 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002350 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002351 char *p, *pp;
2352 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002353 Include *inc;
2354 Context *ctx;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002355 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002356 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002357 struct tokenval tokval;
2358 expr *evalresult;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002359 ExpDef *ed, *eed, **edhead;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002360 ExpInv *ei, *eei;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002361 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002362 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002363 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002364
2365 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002366
H. Peter Anvineba20a72002-04-30 20:53:55 +00002367 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002368 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002369 (tline->text[1] == '%' || tline->text[1] == '$'
2370 || tline->text[1] == '!'))
2371 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002372
H. Peter Anvin4169a472007-09-12 01:29:43 +00002373 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002374
H. Peter Anvin4169a472007-09-12 01:29:43 +00002375 switch (i) {
2376 case PP_INVALID:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002377 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002378 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2379 tline->text);
2380 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002381
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 case PP_STACKSIZE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002383 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 /* Directive to tell NASM what the default stack size is. The
2385 * default is for a 16-bit stack, and this can be overriden with
2386 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002387 */
2388 tline = tline->next;
2389 if (tline && tline->type == TOK_WHITESPACE)
2390 tline = tline->next;
2391 if (!tline || tline->type != TOK_ID) {
2392 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2393 free_tlist(origline);
2394 return DIRECTIVE_FOUND;
2395 }
2396 if (nasm_stricmp(tline->text, "flat") == 0) {
2397 /* All subsequent ARG directives are for a 32-bit stack */
2398 StackSize = 4;
2399 StackPointer = "ebp";
2400 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002401 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002402 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2403 /* All subsequent ARG directives are for a 64-bit stack */
2404 StackSize = 8;
2405 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002406 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002407 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002408 } else if (nasm_stricmp(tline->text, "large") == 0) {
2409 /* All subsequent ARG directives are for a 16-bit stack,
2410 * far function call.
2411 */
2412 StackSize = 2;
2413 StackPointer = "bp";
2414 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002415 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002416 } else if (nasm_stricmp(tline->text, "small") == 0) {
2417 /* All subsequent ARG directives are for a 16-bit stack,
2418 * far function call. We don't support near functions.
2419 */
2420 StackSize = 2;
2421 StackPointer = "bp";
2422 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002423 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002424 } else {
2425 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2426 free_tlist(origline);
2427 return DIRECTIVE_FOUND;
2428 }
2429 free_tlist(origline);
2430 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002431
H. Peter Anvine2c80182005-01-15 22:15:51 +00002432 case PP_ARG:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002433 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002434 /* TASM like ARG directive to define arguments to functions, in
2435 * the following form:
2436 *
2437 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2438 */
2439 offset = ArgOffset;
2440 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002441 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002442 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002443
H. Peter Anvine2c80182005-01-15 22:15:51 +00002444 /* Find the argument name */
2445 tline = tline->next;
2446 if (tline && tline->type == TOK_WHITESPACE)
2447 tline = tline->next;
2448 if (!tline || tline->type != TOK_ID) {
2449 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2450 free_tlist(origline);
2451 return DIRECTIVE_FOUND;
2452 }
2453 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002454
H. Peter Anvine2c80182005-01-15 22:15:51 +00002455 /* Find the argument size type */
2456 tline = tline->next;
2457 if (!tline || tline->type != TOK_OTHER
2458 || tline->text[0] != ':') {
2459 error(ERR_NONFATAL,
2460 "Syntax error processing `%%arg' directive");
2461 free_tlist(origline);
2462 return DIRECTIVE_FOUND;
2463 }
2464 tline = tline->next;
2465 if (!tline || tline->type != TOK_ID) {
2466 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2467 free_tlist(origline);
2468 return DIRECTIVE_FOUND;
2469 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002470
H. Peter Anvine2c80182005-01-15 22:15:51 +00002471 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002472 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002473 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002474 size = parse_size(tt->text);
2475 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002476 error(ERR_NONFATAL,
2477 "Invalid size type for `%%arg' missing directive");
2478 free_tlist(tt);
2479 free_tlist(origline);
2480 return DIRECTIVE_FOUND;
2481 }
2482 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002483
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002484 /* Round up to even stack slots */
2485 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002486
H. Peter Anvine2c80182005-01-15 22:15:51 +00002487 /* Now define the macro for the argument */
2488 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2489 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002490 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002491 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002492
H. Peter Anvine2c80182005-01-15 22:15:51 +00002493 /* Move to the next argument in the list */
2494 tline = tline->next;
2495 if (tline && tline->type == TOK_WHITESPACE)
2496 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002497 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002498 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002499 free_tlist(origline);
2500 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002501
H. Peter Anvine2c80182005-01-15 22:15:51 +00002502 case PP_LOCAL:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002503 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002504 /* TASM like LOCAL directive to define local variables for a
2505 * function, in the following form:
2506 *
2507 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2508 *
2509 * The '= LocalSize' at the end is ignored by NASM, but is
2510 * required by TASM to define the local parameter size (and used
2511 * by the TASM macro package).
2512 */
2513 offset = LocalOffset;
2514 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002515 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002516 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002517
H. Peter Anvine2c80182005-01-15 22:15:51 +00002518 /* Find the argument name */
2519 tline = tline->next;
2520 if (tline && tline->type == TOK_WHITESPACE)
2521 tline = tline->next;
2522 if (!tline || tline->type != TOK_ID) {
2523 error(ERR_NONFATAL,
2524 "`%%local' missing argument parameter");
2525 free_tlist(origline);
2526 return DIRECTIVE_FOUND;
2527 }
2528 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002529
H. Peter Anvine2c80182005-01-15 22:15:51 +00002530 /* Find the argument size type */
2531 tline = tline->next;
2532 if (!tline || tline->type != TOK_OTHER
2533 || tline->text[0] != ':') {
2534 error(ERR_NONFATAL,
2535 "Syntax error processing `%%local' directive");
2536 free_tlist(origline);
2537 return DIRECTIVE_FOUND;
2538 }
2539 tline = tline->next;
2540 if (!tline || tline->type != TOK_ID) {
2541 error(ERR_NONFATAL,
2542 "`%%local' missing size type parameter");
2543 free_tlist(origline);
2544 return DIRECTIVE_FOUND;
2545 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002546
H. Peter Anvine2c80182005-01-15 22:15:51 +00002547 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002548 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002549 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002550 size = parse_size(tt->text);
2551 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002552 error(ERR_NONFATAL,
2553 "Invalid size type for `%%local' missing directive");
2554 free_tlist(tt);
2555 free_tlist(origline);
2556 return DIRECTIVE_FOUND;
2557 }
2558 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002559
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002560 /* Round up to even stack slots */
2561 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002562
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002563 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002564
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002565 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002566 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2567 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002568 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002569
H. Peter Anvine2c80182005-01-15 22:15:51 +00002570 /* Now define the assign to setup the enter_c macro correctly */
2571 snprintf(directive, sizeof(directive),
2572 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002573 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002574
H. Peter Anvine2c80182005-01-15 22:15:51 +00002575 /* Move to the next argument in the list */
2576 tline = tline->next;
2577 if (tline && tline->type == TOK_WHITESPACE)
2578 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002579 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002580 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002581 free_tlist(origline);
2582 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002583
H. Peter Anvine2c80182005-01-15 22:15:51 +00002584 case PP_CLEAR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002585 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002586 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002587 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002588 "trailing garbage after `%%clear' ignored");
2589 free_macros();
2590 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002591 free_tlist(origline);
2592 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002593
H. Peter Anvin418ca702008-05-30 10:42:30 -07002594 case PP_DEPEND:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002595 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002596 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002597 skip_white_(t);
2598 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002599 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002600 error(ERR_NONFATAL, "`%%depend' expects a file name");
2601 free_tlist(origline);
2602 return DIRECTIVE_FOUND; /* but we did _something_ */
2603 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002604 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002605 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002606 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002607 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002608 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002609 nasm_unquote_cstr(p, i);
2610 if (dephead && !in_list(*dephead, p)) {
2611 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2612 sl->next = NULL;
2613 strcpy(sl->str, p);
2614 *deptail = sl;
2615 deptail = &sl->next;
2616 }
2617 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002618 return DIRECTIVE_FOUND;
2619
2620 case PP_INCLUDE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002621 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002622 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002623 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002624
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002625 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002626 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002627 error(ERR_NONFATAL, "`%%include' expects a file name");
2628 free_tlist(origline);
2629 return DIRECTIVE_FOUND; /* but we did _something_ */
2630 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002631 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002632 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002633 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002634 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002635 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002636 nasm_unquote_cstr(p, i);
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03002637 inc = nasm_zalloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002638 inc->next = istk;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002639 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002640 if (!inc->fp) {
2641 /* -MG given but file not found */
2642 nasm_free(inc);
2643 } else {
2644 inc->fname = src_set_fname(nasm_strdup(p));
2645 inc->lineno = src_set_linnum(0);
2646 inc->lineinc = 1;
2647 inc->expansion = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002648 istk = inc;
2649 list->uplevel(LIST_INCLUDE);
2650 }
2651 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002652 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002653
H. Peter Anvind2456592008-06-19 15:04:18 -07002654 case PP_USE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002655 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002656 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002657 static macros_t *use_pkg;
2658 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002659
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002660 tline = tline->next;
2661 skip_white_(tline);
2662 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002663
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002664 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002665 tline->type != TOK_INTERNAL_STRING &&
2666 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002667 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002668 free_tlist(origline);
2669 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002670 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002671 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002672 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002673 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002674 if (tline->type == TOK_STRING)
2675 nasm_unquote_cstr(tline->text, i);
2676 use_pkg = nasm_stdmac_find_package(tline->text);
2677 if (!use_pkg)
2678 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2679 else
2680 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002681 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002682 /* Not already included, go ahead and include it */
2683 stdmacpos = use_pkg;
2684 }
2685 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002686 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002687 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002688 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002689 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002690 case PP_POP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002691 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002692 tline = tline->next;
2693 skip_white_(tline);
2694 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002695 if (tline) {
2696 if (!tok_type_(tline, TOK_ID)) {
2697 error(ERR_NONFATAL, "`%s' expects a context identifier",
2698 pp_directives[i]);
2699 free_tlist(origline);
2700 return DIRECTIVE_FOUND; /* but we did _something_ */
2701 }
2702 if (tline->next)
2703 error(ERR_WARNING|ERR_PASS1,
2704 "trailing garbage after `%s' ignored",
2705 pp_directives[i]);
2706 p = nasm_strdup(tline->text);
2707 } else {
2708 p = NULL; /* Anonymous */
2709 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002710
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002711 if (i == PP_PUSH) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002712 ctx = nasm_zalloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002713 ctx->next = cstk;
2714 hash_init(&ctx->localmac, HASH_SMALL);
2715 ctx->name = p;
2716 ctx->number = unique++;
2717 cstk = ctx;
2718 } else {
2719 /* %pop or %repl */
2720 if (!cstk) {
2721 error(ERR_NONFATAL, "`%s': context stack is empty",
2722 pp_directives[i]);
2723 } else if (i == PP_POP) {
2724 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2725 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2726 "expected %s",
2727 cstk->name ? cstk->name : "anonymous", p);
2728 else
2729 ctx_pop();
2730 } else {
2731 /* i == PP_REPL */
2732 nasm_free(cstk->name);
2733 cstk->name = p;
2734 p = NULL;
2735 }
2736 nasm_free(p);
2737 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002738 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002739 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002740 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002741 severity = ERR_FATAL;
2742 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002743 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002744 severity = ERR_NONFATAL;
2745 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002746 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002747 severity = ERR_WARNING|ERR_WARN_USER;
2748 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002749
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002750issue_error:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002751 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002752 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002753 /* Only error out if this is the final pass */
2754 if (pass != 2 && i != PP_FATAL)
2755 return DIRECTIVE_FOUND;
2756
2757 tline->next = expand_smacro(tline->next);
2758 tline = tline->next;
2759 skip_white_(tline);
2760 t = tline ? tline->next : NULL;
2761 skip_white_(t);
2762 if (tok_type_(tline, TOK_STRING) && !t) {
2763 /* The line contains only a quoted string */
2764 p = tline->text;
2765 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2766 error(severity, "%s", p);
2767 } else {
2768 /* Not a quoted string, or more than a quoted string */
2769 p = detoken(tline, false);
2770 error(severity, "%s", p);
2771 nasm_free(p);
2772 }
2773 free_tlist(origline);
2774 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002775 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002776
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002777 CASE_PP_IF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002778 if (defining != NULL) {
2779 if (defining->type == EXP_IF) {
2780 defining->def_depth ++;
2781 }
2782 return NO_DIRECTIVE_FOUND;
2783 }
2784 if ((istk->expansion != NULL) &&
2785 (istk->expansion->emitting == false)) {
2786 j = COND_NEVER;
2787 } else {
2788 j = if_condition(tline->next, i);
2789 tline->next = NULL; /* it got freed */
2790 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
2791 }
2792 ed = new_ExpDef(EXP_IF);
2793 ed->state = j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002794 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
2795 ed->prev = defining;
2796 defining = ed;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002797 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002798 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002799
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002800 CASE_PP_ELIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002801 if (defining != NULL) {
2802 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2803 return NO_DIRECTIVE_FOUND;
2804 }
2805 }
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +04002806 if ((defining == NULL) || (defining->type != EXP_IF)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002807 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2808 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002809 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002810 case COND_IF_TRUE:
2811 defining->state = COND_DONE;
2812 defining->ignoring = true;
2813 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002814
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002815 case COND_DONE:
2816 case COND_NEVER:
2817 defining->ignoring = true;
2818 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002819
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002820 case COND_ELSE_TRUE:
2821 case COND_ELSE_FALSE:
2822 error_precond(ERR_WARNING|ERR_PASS1,
2823 "`%%elif' after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002824 defining->state = COND_NEVER;
2825 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002826 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002827
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002828 case COND_IF_FALSE:
2829 /*
2830 * IMPORTANT: In the case of %if, we will already have
2831 * called expand_mmac_params(); however, if we're
2832 * processing an %elif we must have been in a
2833 * non-emitting mode, which would have inhibited
2834 * the normal invocation of expand_mmac_params().
2835 * Therefore, we have to do it explicitly here.
2836 */
2837 j = if_condition(expand_mmac_params(tline->next), i);
2838 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002839 defining->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002840 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002841 defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002842 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002843 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002844 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002845 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002846
H. Peter Anvine2c80182005-01-15 22:15:51 +00002847 case PP_ELSE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002848 if (defining != NULL) {
2849 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2850 return NO_DIRECTIVE_FOUND;
2851 }
2852 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002853 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002854 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002855 "trailing garbage after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002856 if ((defining == NULL) || (defining->type != EXP_IF)) {
2857 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2858 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002859 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002860 case COND_IF_TRUE:
2861 case COND_DONE:
2862 defining->state = COND_ELSE_FALSE;
2863 defining->ignoring = true;
2864 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002865
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002866 case COND_NEVER:
2867 defining->ignoring = true;
2868 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002869
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002870 case COND_IF_FALSE:
2871 defining->state = COND_ELSE_TRUE;
2872 defining->ignoring = false;
2873 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002874
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002875 case COND_ELSE_TRUE:
2876 case COND_ELSE_FALSE:
2877 error_precond(ERR_WARNING|ERR_PASS1,
2878 "`%%else' after `%%else' ignored.");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002879 defining->state = COND_NEVER;
2880 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002881 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002882 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002883 free_tlist(origline);
2884 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002885
H. Peter Anvine2c80182005-01-15 22:15:51 +00002886 case PP_ENDIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002887 if (defining != NULL) {
2888 if (defining->type == EXP_IF) {
2889 if (defining->def_depth > 0) {
2890 defining->def_depth --;
2891 return NO_DIRECTIVE_FOUND;
2892 }
2893 } else {
2894 return NO_DIRECTIVE_FOUND;
2895 }
2896 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002897 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002898 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002899 "trailing garbage after `%%endif' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002900 if ((defining == NULL) || (defining->type != EXP_IF)) {
2901 error(ERR_NONFATAL, "`%%endif': no matching `%%if'");
2902 return DIRECTIVE_FOUND;
2903 }
2904 ed = defining;
2905 defining = ed->prev;
2906 ed->prev = expansions;
2907 expansions = ed;
2908 ei = new_ExpInv(EXP_IF, ed);
2909 ei->current = ed->line;
2910 ei->emitting = true;
2911 ei->prev = istk->expansion;
2912 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002913 free_tlist(origline);
2914 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002915
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002916 case PP_RMACRO:
2917 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002918 case PP_MACRO:
2919 case PP_IMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002920 if (defining != NULL) {
2921 if (defining->type == EXP_MMACRO) {
2922 defining->def_depth ++;
2923 }
2924 return NO_DIRECTIVE_FOUND;
2925 }
2926 ed = new_ExpDef(EXP_MMACRO);
2927 ed->max_depth =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002928 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002929 ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002930 if (!parse_mmacro_spec(tline, ed, pp_directives[i])) {
2931 nasm_free(ed);
2932 ed = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002933 return DIRECTIVE_FOUND;
2934 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002935 ed->def_depth = 0;
2936 ed->cur_depth = 0;
2937 ed->max_depth = (ed->max_depth + 1);
2938 ed->ignoring = false;
2939 ed->prev = defining;
2940 defining = ed;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002941
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002942 eed = (ExpDef *) hash_findix(&expdefs, ed->name);
2943 while (eed) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002944 if (!strcmp(eed->name, ed->name) &&
2945 (eed->nparam_min <= ed->nparam_max || ed->plus) &&
2946 (ed->nparam_min <= eed->nparam_max || eed->plus)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002947 error(ERR_WARNING|ERR_PASS1,
2948 "redefining multi-line macro `%s'", ed->name);
2949 return DIRECTIVE_FOUND;
2950 }
2951 eed = eed->next;
2952 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002953 free_tlist(origline);
2954 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002955
H. Peter Anvine2c80182005-01-15 22:15:51 +00002956 case PP_ENDM:
2957 case PP_ENDMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002958 if (defining != NULL) {
2959 if (defining->type == EXP_MMACRO) {
2960 if (defining->def_depth > 0) {
2961 defining->def_depth --;
2962 return NO_DIRECTIVE_FOUND;
2963 }
2964 } else {
2965 return NO_DIRECTIVE_FOUND;
2966 }
2967 }
2968 if (!(defining) || (defining->type != EXP_MMACRO)) {
2969 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2970 return DIRECTIVE_FOUND;
2971 }
2972 edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name);
2973 defining->next = *edhead;
2974 *edhead = defining;
2975 ed = defining;
2976 defining = ed->prev;
2977 ed->prev = expansions;
2978 expansions = ed;
2979 ed = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002980 free_tlist(origline);
2981 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002982
H. Peter Anvin89cee572009-07-15 09:16:54 -04002983 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002984 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2985 /*
2986 * We must search along istk->expansion until we hit a
2987 * macro invocation. Then we disable the emitting state(s)
2988 * between exitmacro and endmacro.
2989 */
2990 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
2991 if(ei->type == EXP_MMACRO) {
2992 break;
2993 }
2994 }
2995
2996 if (ei != NULL) {
2997 /*
2998 * Set all invocations leading back to the macro
2999 * invocation to a non-emitting state.
3000 */
3001 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3002 eei->emitting = false;
3003 }
3004 eei->emitting = false;
3005 } else {
3006 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
3007 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05003008 free_tlist(origline);
3009 return DIRECTIVE_FOUND;
3010
H. Peter Anvina26433d2008-07-16 14:40:01 -07003011 case PP_UNMACRO:
3012 case PP_UNIMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003013 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003014 {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003015 ExpDef **ed_p;
3016 ExpDef spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003017
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003018 spec.casesense = (i == PP_UNMACRO);
3019 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
3020 return DIRECTIVE_FOUND;
3021 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003022 ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL);
3023 while (ed_p && *ed_p) {
3024 ed = *ed_p;
3025 if (ed->casesense == spec.casesense &&
3026 !mstrcmp(ed->name, spec.name, spec.casesense) &&
3027 ed->nparam_min == spec.nparam_min &&
3028 ed->nparam_max == spec.nparam_max &&
3029 ed->plus == spec.plus) {
Keith Kaniosc98a5b42010-12-18 12:17:31 -06003030 if (ed->cur_depth > 0) {
Keith Kanios21d885b2010-12-18 12:22:21 -06003031 error(ERR_NONFATAL, "`%s' ignored on active macro",
Keith Kaniosc98a5b42010-12-18 12:17:31 -06003032 pp_directives[i]);
3033 break;
3034 } else {
3035 *ed_p = ed->next;
3036 free_expdef(ed);
3037 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003038 } else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003039 ed_p = &ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003040 }
3041 }
3042 free_tlist(origline);
3043 free_tlist(spec.dlist);
3044 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003045 }
3046
H. Peter Anvine2c80182005-01-15 22:15:51 +00003047 case PP_ROTATE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003048 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003049 if (tline->next && tline->next->type == TOK_WHITESPACE)
3050 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003051 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003052 free_tlist(origline);
3053 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
3054 return DIRECTIVE_FOUND;
3055 }
3056 t = expand_smacro(tline->next);
3057 tline->next = NULL;
3058 free_tlist(origline);
3059 tline = t;
3060 tptr = &t;
3061 tokval.t_type = TOKEN_INVALID;
3062 evalresult =
3063 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3064 free_tlist(tline);
3065 if (!evalresult)
3066 return DIRECTIVE_FOUND;
3067 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003068 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003069 "trailing garbage after expression ignored");
3070 if (!is_simple(evalresult)) {
3071 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
3072 return DIRECTIVE_FOUND;
3073 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003074 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3075 if (ei->type == EXP_MMACRO) {
3076 break;
3077 }
3078 }
3079 if (ei == NULL) {
3080 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
3081 } else if (ei->nparam == 0) {
3082 error(ERR_NONFATAL,
3083 "`%%rotate' invoked within macro without parameters");
3084 } else {
3085 int rotate = ei->rotate + reloc_value(evalresult);
3086
3087 rotate %= (int)ei->nparam;
3088 if (rotate < 0)
3089 rotate += ei->nparam;
3090 ei->rotate = rotate;
3091 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003092 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003093
H. Peter Anvine2c80182005-01-15 22:15:51 +00003094 case PP_REP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003095 if (defining != NULL) {
3096 if (defining->type == EXP_REP) {
3097 defining->def_depth ++;
3098 }
3099 return NO_DIRECTIVE_FOUND;
3100 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003101 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003102 do {
3103 tline = tline->next;
3104 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003105
H. Peter Anvine2c80182005-01-15 22:15:51 +00003106 if (tok_type_(tline, TOK_ID) &&
3107 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003108 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003109 do {
3110 tline = tline->next;
3111 } while (tok_type_(tline, TOK_WHITESPACE));
3112 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003113
H. Peter Anvine2c80182005-01-15 22:15:51 +00003114 if (tline) {
3115 t = expand_smacro(tline);
3116 tptr = &t;
3117 tokval.t_type = TOKEN_INVALID;
3118 evalresult =
3119 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3120 if (!evalresult) {
3121 free_tlist(origline);
3122 return DIRECTIVE_FOUND;
3123 }
3124 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003125 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003126 "trailing garbage after expression ignored");
3127 if (!is_simple(evalresult)) {
3128 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
3129 return DIRECTIVE_FOUND;
3130 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003131 count = reloc_value(evalresult);
3132 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04003133 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003134 count = 0;
3135 } else
3136 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003137 } else {
3138 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003139 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003140 }
3141 free_tlist(origline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003142 ed = new_ExpDef(EXP_REP);
3143 ed->nolist = nolist;
3144 ed->def_depth = 0;
3145 ed->cur_depth = 1;
3146 ed->max_depth = (count - 1);
3147 ed->ignoring = false;
3148 ed->prev = defining;
3149 defining = ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003150 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003151
H. Peter Anvine2c80182005-01-15 22:15:51 +00003152 case PP_ENDREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003153 if (defining != NULL) {
3154 if (defining->type == EXP_REP) {
3155 if (defining->def_depth > 0) {
3156 defining->def_depth --;
3157 return NO_DIRECTIVE_FOUND;
3158 }
3159 } else {
3160 return NO_DIRECTIVE_FOUND;
3161 }
3162 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003163 if ((defining == NULL) || (defining->type != EXP_REP)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003164 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
3165 return DIRECTIVE_FOUND;
3166 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003167
H. Peter Anvine2c80182005-01-15 22:15:51 +00003168 /*
3169 * Now we have a "macro" defined - although it has no name
3170 * and we won't be entering it in the hash tables - we must
3171 * push a macro-end marker for it on to istk->expansion.
3172 * After that, it will take care of propagating itself (a
3173 * macro-end marker line for a macro which is really a %rep
3174 * block will cause the macro to be re-expanded, complete
3175 * with another macro-end marker to ensure the process
3176 * continues) until the whole expansion is forcibly removed
3177 * from istk->expansion by a %exitrep.
3178 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003179 ed = defining;
3180 defining = ed->prev;
3181 ed->prev = expansions;
3182 expansions = ed;
3183 ei = new_ExpInv(EXP_REP, ed);
3184 ei->current = ed->line;
3185 ei->emitting = ((ed->max_depth > 0) ? true : false);
3186 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3187 ei->prev = istk->expansion;
3188 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003189 free_tlist(origline);
3190 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003191
H. Peter Anvine2c80182005-01-15 22:15:51 +00003192 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003193 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3194 /*
3195 * We must search along istk->expansion until we hit a
3196 * rep invocation. Then we disable the emitting state(s)
3197 * between exitrep and endrep.
3198 */
3199 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3200 if (ei->type == EXP_REP) {
3201 break;
3202 }
3203 }
3204
3205 if (ei != NULL) {
3206 /*
3207 * Set all invocations leading back to the rep
3208 * invocation to a non-emitting state.
3209 */
3210 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3211 eei->emitting = false;
3212 }
3213 eei->emitting = false;
3214 eei->current = NULL;
3215 eei->def->cur_depth = eei->def->max_depth;
3216 } else {
3217 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3218 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003219 free_tlist(origline);
3220 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003221
H. Peter Anvine2c80182005-01-15 22:15:51 +00003222 case PP_XDEFINE:
3223 case PP_IXDEFINE:
3224 case PP_DEFINE:
3225 case PP_IDEFINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003226 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003227 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003228
H. Peter Anvine2c80182005-01-15 22:15:51 +00003229 tline = tline->next;
3230 skip_white_(tline);
3231 tline = expand_id(tline);
3232 if (!tline || (tline->type != TOK_ID &&
3233 (tline->type != TOK_PREPROC_ID ||
3234 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003235 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003236 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003237 free_tlist(origline);
3238 return DIRECTIVE_FOUND;
3239 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003240
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003241 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003242 last = tline;
3243 param_start = tline = tline->next;
3244 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003245
H. Peter Anvine2c80182005-01-15 22:15:51 +00003246 /* Expand the macro definition now for %xdefine and %ixdefine */
3247 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3248 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003249
H. Peter Anvine2c80182005-01-15 22:15:51 +00003250 if (tok_is_(tline, "(")) {
3251 /*
3252 * This macro has parameters.
3253 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003254
H. Peter Anvine2c80182005-01-15 22:15:51 +00003255 tline = tline->next;
3256 while (1) {
3257 skip_white_(tline);
3258 if (!tline) {
3259 error(ERR_NONFATAL, "parameter identifier expected");
3260 free_tlist(origline);
3261 return DIRECTIVE_FOUND;
3262 }
3263 if (tline->type != TOK_ID) {
3264 error(ERR_NONFATAL,
3265 "`%s': parameter identifier expected",
3266 tline->text);
3267 free_tlist(origline);
3268 return DIRECTIVE_FOUND;
3269 }
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04003270
3271 smacro_set_param_idx(tline, nparam);
3272 nparam++;
3273
H. Peter Anvine2c80182005-01-15 22:15:51 +00003274 tline = tline->next;
3275 skip_white_(tline);
3276 if (tok_is_(tline, ",")) {
3277 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003278 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003279 if (!tok_is_(tline, ")")) {
3280 error(ERR_NONFATAL,
3281 "`)' expected to terminate macro template");
3282 free_tlist(origline);
3283 return DIRECTIVE_FOUND;
3284 }
3285 break;
3286 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003287 }
3288 last = tline;
3289 tline = tline->next;
3290 }
3291 if (tok_type_(tline, TOK_WHITESPACE))
3292 last = tline, tline = tline->next;
3293 macro_start = NULL;
3294 last->next = NULL;
3295 t = tline;
3296 while (t) {
3297 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003298 list_for_each(tt, param_start)
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04003299 if (is_smacro_param(tt) &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003300 !strcmp(tt->text, t->text))
3301 t->type = tt->type;
3302 }
3303 tt = t->next;
3304 t->next = macro_start;
3305 macro_start = t;
3306 t = tt;
3307 }
3308 /*
3309 * Good. We now have a macro name, a parameter count, and a
3310 * token list (in reverse order) for an expansion. We ought
3311 * to be OK just to create an SMacro, store it, and let
3312 * free_tlist have the rest of the line (which we have
3313 * carefully re-terminated after chopping off the expansion
3314 * from the end).
3315 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003316 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003317 free_tlist(origline);
3318 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003319
H. Peter Anvine2c80182005-01-15 22:15:51 +00003320 case PP_UNDEF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003321 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003322 tline = tline->next;
3323 skip_white_(tline);
3324 tline = expand_id(tline);
3325 if (!tline || (tline->type != TOK_ID &&
3326 (tline->type != TOK_PREPROC_ID ||
3327 tline->text[1] != '$'))) {
3328 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3329 free_tlist(origline);
3330 return DIRECTIVE_FOUND;
3331 }
3332 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003333 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003334 "trailing garbage after macro name ignored");
3335 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003336
H. Peter Anvine2c80182005-01-15 22:15:51 +00003337 /* Find the context that symbol belongs to */
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003338 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003339 undef_smacro(ctx, mname);
3340 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003341 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003342
H. Peter Anvin9e200162008-06-04 17:23:14 -07003343 case PP_DEFSTR:
3344 case PP_IDEFSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003345 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003346 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003347
3348 tline = tline->next;
3349 skip_white_(tline);
3350 tline = expand_id(tline);
3351 if (!tline || (tline->type != TOK_ID &&
3352 (tline->type != TOK_PREPROC_ID ||
3353 tline->text[1] != '$'))) {
3354 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003355 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003356 free_tlist(origline);
3357 return DIRECTIVE_FOUND;
3358 }
3359
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003360 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003361 last = tline;
3362 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003363 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003364
3365 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003366 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003367
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003368 p = detoken(tline, false);
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003369 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003370 macro_start->text = nasm_quote(p, strlen(p));
3371 macro_start->type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003372 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003373
3374 /*
3375 * We now have a macro name, an implicit parameter count of
3376 * zero, and a string token to use as an expansion. Create
3377 * and store an SMacro.
3378 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003379 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003380 free_tlist(origline);
3381 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003382
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003383 case PP_DEFTOK:
3384 case PP_IDEFTOK:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003385 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003386 casesense = (i == PP_DEFTOK);
3387
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003388 tline = tline->next;
3389 skip_white_(tline);
3390 tline = expand_id(tline);
3391 if (!tline || (tline->type != TOK_ID &&
3392 (tline->type != TOK_PREPROC_ID ||
3393 tline->text[1] != '$'))) {
3394 error(ERR_NONFATAL,
3395 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003396 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003397 free_tlist(origline);
3398 return DIRECTIVE_FOUND;
3399 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003400 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003401 last = tline;
3402 tline = expand_smacro(tline->next);
3403 last->next = NULL;
3404
3405 t = tline;
3406 while (tok_type_(t, TOK_WHITESPACE))
3407 t = t->next;
3408 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003409 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003410 error(ERR_NONFATAL,
3411 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003412 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003413 free_tlist(tline);
3414 free_tlist(origline);
3415 return DIRECTIVE_FOUND;
3416 }
3417
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003418 /*
3419 * Convert the string to a token stream. Note that smacros
3420 * are stored with the token stream reversed, so we have to
3421 * reverse the output of tokenize().
3422 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003423 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003424 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003425
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003426 /*
3427 * We now have a macro name, an implicit parameter count of
3428 * zero, and a numeric token to use as an expansion. Create
3429 * and store an SMacro.
3430 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003431 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003432 free_tlist(tline);
3433 free_tlist(origline);
3434 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003435
H. Peter Anvin418ca702008-05-30 10:42:30 -07003436 case PP_PATHSEARCH:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003437 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003438 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003439 FILE *fp;
3440 StrList *xsl = NULL;
3441 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003442
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003443 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003444
3445 tline = tline->next;
3446 skip_white_(tline);
3447 tline = expand_id(tline);
3448 if (!tline || (tline->type != TOK_ID &&
3449 (tline->type != TOK_PREPROC_ID ||
3450 tline->text[1] != '$'))) {
3451 error(ERR_NONFATAL,
3452 "`%%pathsearch' expects a macro identifier as first parameter");
3453 free_tlist(origline);
3454 return DIRECTIVE_FOUND;
3455 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003456 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003457 last = tline;
3458 tline = expand_smacro(tline->next);
3459 last->next = NULL;
3460
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003461 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003462 while (tok_type_(t, TOK_WHITESPACE))
3463 t = t->next;
3464
3465 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003466 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003467 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003468 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003469 free_tlist(origline);
3470 return DIRECTIVE_FOUND; /* but we did _something_ */
3471 }
3472 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003473 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003474 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003475 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003476 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003477 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003478
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003479 fp = inc_fopen(p, &xsl, &xst, true);
3480 if (fp) {
3481 p = xsl->str;
3482 fclose(fp); /* Don't actually care about the file */
3483 }
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003484 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003485 macro_start->text = nasm_quote(p, strlen(p));
3486 macro_start->type = TOK_STRING;
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04003487 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003488
3489 /*
3490 * We now have a macro name, an implicit parameter count of
3491 * zero, and a string token to use as an expansion. Create
3492 * and store an SMacro.
3493 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003494 define_smacro(ctx, mname, casesense, 0, macro_start);
3495 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003496 free_tlist(origline);
3497 return DIRECTIVE_FOUND;
3498 }
3499
H. Peter Anvine2c80182005-01-15 22:15:51 +00003500 case PP_STRLEN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003501 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003502 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003503
H. Peter Anvine2c80182005-01-15 22:15:51 +00003504 tline = tline->next;
3505 skip_white_(tline);
3506 tline = expand_id(tline);
3507 if (!tline || (tline->type != TOK_ID &&
3508 (tline->type != TOK_PREPROC_ID ||
3509 tline->text[1] != '$'))) {
3510 error(ERR_NONFATAL,
3511 "`%%strlen' expects a macro identifier as first parameter");
3512 free_tlist(origline);
3513 return DIRECTIVE_FOUND;
3514 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003515 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003516 last = tline;
3517 tline = expand_smacro(tline->next);
3518 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003519
H. Peter Anvine2c80182005-01-15 22:15:51 +00003520 t = tline;
3521 while (tok_type_(t, TOK_WHITESPACE))
3522 t = t->next;
3523 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003524 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003525 error(ERR_NONFATAL,
3526 "`%%strlen` requires string as second parameter");
3527 free_tlist(tline);
3528 free_tlist(origline);
3529 return DIRECTIVE_FOUND;
3530 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003531
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003532 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003533 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003534
H. Peter Anvine2c80182005-01-15 22:15:51 +00003535 /*
3536 * We now have a macro name, an implicit parameter count of
3537 * zero, and a numeric token to use as an expansion. Create
3538 * and store an SMacro.
3539 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003540 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003541 free_tlist(tline);
3542 free_tlist(origline);
3543 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003544
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003545 case PP_STRCAT:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003546 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003547 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003548
3549 tline = tline->next;
3550 skip_white_(tline);
3551 tline = expand_id(tline);
3552 if (!tline || (tline->type != TOK_ID &&
3553 (tline->type != TOK_PREPROC_ID ||
3554 tline->text[1] != '$'))) {
3555 error(ERR_NONFATAL,
3556 "`%%strcat' expects a macro identifier as first parameter");
3557 free_tlist(origline);
3558 return DIRECTIVE_FOUND;
3559 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003560 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003561 last = tline;
3562 tline = expand_smacro(tline->next);
3563 last->next = NULL;
3564
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003565 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003566 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003567 switch (t->type) {
3568 case TOK_WHITESPACE:
3569 break;
3570 case TOK_STRING:
3571 len += t->a.len = nasm_unquote(t->text, NULL);
3572 break;
3573 case TOK_OTHER:
3574 if (!strcmp(t->text, ",")) /* permit comma separators */
3575 break;
3576 /* else fall through */
3577 default:
3578 error(ERR_NONFATAL,
3579 "non-string passed to `%%strcat' (%d)", t->type);
3580 free_tlist(tline);
3581 free_tlist(origline);
3582 return DIRECTIVE_FOUND;
3583 }
3584 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003585
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003586 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003587 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003588 if (t->type == TOK_STRING) {
3589 memcpy(p, t->text, t->a.len);
3590 p += t->a.len;
3591 }
3592 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003593
3594 /*
3595 * We now have a macro name, an implicit parameter count of
3596 * zero, and a numeric token to use as an expansion. Create
3597 * and store an SMacro.
3598 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003599 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3600 macro_start->text = nasm_quote(pp, len);
3601 nasm_free(pp);
3602 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003603 free_tlist(tline);
3604 free_tlist(origline);
3605 return DIRECTIVE_FOUND;
3606
H. Peter Anvine2c80182005-01-15 22:15:51 +00003607 case PP_SUBSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003608 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003609 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003610 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003611 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003612
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003613 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003614
H. Peter Anvine2c80182005-01-15 22:15:51 +00003615 tline = tline->next;
3616 skip_white_(tline);
3617 tline = expand_id(tline);
3618 if (!tline || (tline->type != TOK_ID &&
3619 (tline->type != TOK_PREPROC_ID ||
3620 tline->text[1] != '$'))) {
3621 error(ERR_NONFATAL,
3622 "`%%substr' expects a macro identifier as first parameter");
3623 free_tlist(origline);
3624 return DIRECTIVE_FOUND;
3625 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003626 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003627 last = tline;
3628 tline = expand_smacro(tline->next);
3629 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003630
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003631 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003632 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003633 while (tok_type_(t, TOK_WHITESPACE))
3634 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003635
H. Peter Anvine2c80182005-01-15 22:15:51 +00003636 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003637 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003638 error(ERR_NONFATAL,
3639 "`%%substr` requires string as second parameter");
3640 free_tlist(tline);
3641 free_tlist(origline);
3642 return DIRECTIVE_FOUND;
3643 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003644
H. Peter Anvine2c80182005-01-15 22:15:51 +00003645 tt = t->next;
3646 tptr = &tt;
3647 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003648 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003649 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003650 if (!evalresult) {
3651 free_tlist(tline);
3652 free_tlist(origline);
3653 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003654 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003655 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3656 free_tlist(tline);
3657 free_tlist(origline);
3658 return DIRECTIVE_FOUND;
3659 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003660 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003661
3662 while (tok_type_(tt, TOK_WHITESPACE))
3663 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003664 if (!tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003665 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003666 } else {
3667 tokval.t_type = TOKEN_INVALID;
3668 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3669 pass, error, NULL);
3670 if (!evalresult) {
3671 free_tlist(tline);
3672 free_tlist(origline);
3673 return DIRECTIVE_FOUND;
3674 } else if (!is_simple(evalresult)) {
3675 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3676 free_tlist(tline);
3677 free_tlist(origline);
3678 return DIRECTIVE_FOUND;
3679 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003680 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003681 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003682
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003683 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003684 /* make start and count being in range */
3685 if (start < 0)
3686 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003687 if (count < 0)
3688 count = len + count + 1 - start;
3689 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003690 count = len - start;
3691 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003692 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003693
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003694 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003695 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003696 macro_start->type = TOK_STRING;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003697
H. Peter Anvine2c80182005-01-15 22:15:51 +00003698 /*
3699 * We now have a macro name, an implicit parameter count of
3700 * zero, and a numeric token to use as an expansion. Create
3701 * and store an SMacro.
3702 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003703 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003704 free_tlist(tline);
3705 free_tlist(origline);
3706 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003707 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003708
H. Peter Anvine2c80182005-01-15 22:15:51 +00003709 case PP_ASSIGN:
3710 case PP_IASSIGN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003711 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003712 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003713
H. Peter Anvine2c80182005-01-15 22:15:51 +00003714 tline = tline->next;
3715 skip_white_(tline);
3716 tline = expand_id(tline);
3717 if (!tline || (tline->type != TOK_ID &&
3718 (tline->type != TOK_PREPROC_ID ||
3719 tline->text[1] != '$'))) {
3720 error(ERR_NONFATAL,
3721 "`%%%sassign' expects a macro identifier",
3722 (i == PP_IASSIGN ? "i" : ""));
3723 free_tlist(origline);
3724 return DIRECTIVE_FOUND;
3725 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003726 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003727 last = tline;
3728 tline = expand_smacro(tline->next);
3729 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003730
H. Peter Anvine2c80182005-01-15 22:15:51 +00003731 t = tline;
3732 tptr = &t;
3733 tokval.t_type = TOKEN_INVALID;
3734 evalresult =
3735 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3736 free_tlist(tline);
3737 if (!evalresult) {
3738 free_tlist(origline);
3739 return DIRECTIVE_FOUND;
3740 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003741
H. Peter Anvine2c80182005-01-15 22:15:51 +00003742 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003743 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003744 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003745
H. Peter Anvine2c80182005-01-15 22:15:51 +00003746 if (!is_simple(evalresult)) {
3747 error(ERR_NONFATAL,
3748 "non-constant value given to `%%%sassign'",
3749 (i == PP_IASSIGN ? "i" : ""));
3750 free_tlist(origline);
3751 return DIRECTIVE_FOUND;
3752 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003753
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003754 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003755 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003756
H. Peter Anvine2c80182005-01-15 22:15:51 +00003757 /*
3758 * We now have a macro name, an implicit parameter count of
3759 * zero, and a numeric token to use as an expansion. Create
3760 * and store an SMacro.
3761 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003762 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003763 free_tlist(origline);
3764 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003765
H. Peter Anvine2c80182005-01-15 22:15:51 +00003766 case PP_LINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003767 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003768 /*
3769 * Syntax is `%line nnn[+mmm] [filename]'
3770 */
3771 tline = tline->next;
3772 skip_white_(tline);
3773 if (!tok_type_(tline, TOK_NUMBER)) {
3774 error(ERR_NONFATAL, "`%%line' expects line number");
3775 free_tlist(origline);
3776 return DIRECTIVE_FOUND;
3777 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003778 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003779 m = 1;
3780 tline = tline->next;
3781 if (tok_is_(tline, "+")) {
3782 tline = tline->next;
3783 if (!tok_type_(tline, TOK_NUMBER)) {
3784 error(ERR_NONFATAL, "`%%line' expects line increment");
3785 free_tlist(origline);
3786 return DIRECTIVE_FOUND;
3787 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003788 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003789 tline = tline->next;
3790 }
3791 skip_white_(tline);
3792 src_set_linnum(k);
3793 istk->lineinc = m;
3794 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003795 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003796 }
3797 free_tlist(origline);
3798 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003799
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003800 case PP_WHILE:
3801 if (defining != NULL) {
3802 if (defining->type == EXP_WHILE) {
3803 defining->def_depth ++;
3804 }
3805 return NO_DIRECTIVE_FOUND;
3806 }
3807 l = NULL;
3808 if ((istk->expansion != NULL) &&
3809 (istk->expansion->emitting == false)) {
3810 j = COND_NEVER;
3811 } else {
3812 l = new_Line();
3813 l->first = copy_Token(tline->next);
3814 j = if_condition(tline->next, i);
3815 tline->next = NULL; /* it got freed */
3816 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
3817 }
3818 ed = new_ExpDef(EXP_WHILE);
3819 ed->state = j;
3820 ed->cur_depth = 1;
3821 ed->max_depth = DEADMAN_LIMIT;
3822 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
3823 if (ed->ignoring == false) {
3824 ed->line = l;
3825 ed->last = l;
3826 } else if (l != NULL) {
3827 delete_Token(l->first);
3828 nasm_free(l);
3829 l = NULL;
3830 }
3831 ed->prev = defining;
3832 defining = ed;
3833 free_tlist(origline);
3834 return DIRECTIVE_FOUND;
3835
3836 case PP_ENDWHILE:
3837 if (defining != NULL) {
3838 if (defining->type == EXP_WHILE) {
3839 if (defining->def_depth > 0) {
3840 defining->def_depth --;
3841 return NO_DIRECTIVE_FOUND;
3842 }
3843 } else {
3844 return NO_DIRECTIVE_FOUND;
3845 }
3846 }
3847 if (tline->next != NULL) {
3848 error_precond(ERR_WARNING|ERR_PASS1,
3849 "trailing garbage after `%%endwhile' ignored");
3850 }
3851 if ((defining == NULL) || (defining->type != EXP_WHILE)) {
3852 error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'");
3853 return DIRECTIVE_FOUND;
3854 }
3855 ed = defining;
3856 defining = ed->prev;
3857 if (ed->ignoring == false) {
3858 ed->prev = expansions;
3859 expansions = ed;
3860 ei = new_ExpInv(EXP_WHILE, ed);
3861 ei->current = ed->line->next;
3862 ei->emitting = true;
3863 ei->prev = istk->expansion;
3864 istk->expansion = ei;
3865 } else {
3866 nasm_free(ed);
3867 }
3868 free_tlist(origline);
3869 return DIRECTIVE_FOUND;
3870
3871 case PP_EXITWHILE:
3872 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3873 /*
3874 * We must search along istk->expansion until we hit a
3875 * while invocation. Then we disable the emitting state(s)
3876 * between exitwhile and endwhile.
3877 */
3878 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3879 if (ei->type == EXP_WHILE) {
3880 break;
3881 }
3882 }
3883
3884 if (ei != NULL) {
3885 /*
3886 * Set all invocations leading back to the while
3887 * invocation to a non-emitting state.
3888 */
3889 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3890 eei->emitting = false;
3891 }
3892 eei->emitting = false;
3893 eei->current = NULL;
3894 eei->def->cur_depth = eei->def->max_depth;
3895 } else {
3896 error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block");
3897 }
3898 free_tlist(origline);
3899 return DIRECTIVE_FOUND;
3900
3901 case PP_COMMENT:
3902 if (defining != NULL) {
3903 if (defining->type == EXP_COMMENT) {
3904 defining->def_depth ++;
3905 }
3906 return NO_DIRECTIVE_FOUND;
3907 }
3908 ed = new_ExpDef(EXP_COMMENT);
3909 ed->ignoring = true;
3910 ed->prev = defining;
3911 defining = ed;
3912 free_tlist(origline);
3913 return DIRECTIVE_FOUND;
3914
3915 case PP_ENDCOMMENT:
3916 if (defining != NULL) {
3917 if (defining->type == EXP_COMMENT) {
3918 if (defining->def_depth > 0) {
3919 defining->def_depth --;
3920 return NO_DIRECTIVE_FOUND;
3921 }
3922 } else {
3923 return NO_DIRECTIVE_FOUND;
3924 }
3925 }
3926 if ((defining == NULL) || (defining->type != EXP_COMMENT)) {
3927 error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'");
3928 return DIRECTIVE_FOUND;
3929 }
3930 ed = defining;
3931 defining = ed->prev;
3932 nasm_free(ed);
3933 free_tlist(origline);
3934 return DIRECTIVE_FOUND;
3935
3936 case PP_FINAL:
3937 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3938 if (in_final != false) {
3939 error(ERR_FATAL, "`%%final' cannot be used recursively");
3940 }
3941 tline = tline->next;
3942 skip_white_(tline);
3943 if (tline == NULL) {
3944 error(ERR_NONFATAL, "`%%final' expects at least one parameter");
3945 } else {
3946 l = new_Line();
3947 l->first = copy_Token(tline);
3948 l->next = finals;
3949 finals = l;
3950 }
3951 free_tlist(origline);
3952 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003953
H. Peter Anvine2c80182005-01-15 22:15:51 +00003954 default:
3955 error(ERR_FATAL,
3956 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003957 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003958 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003959 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003960}
3961
3962/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003963 * Ensure that a macro parameter contains a condition code and
3964 * nothing else. Return the condition code index if so, or -1
3965 * otherwise.
3966 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003967static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003968{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003969 Token *tt;
3970 int i, j, k, m;
3971
H. Peter Anvin25a99342007-09-22 17:45:45 -07003972 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003973 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003974
H. Peter Anvineba20a72002-04-30 20:53:55 +00003975 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003976 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003977 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003978 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003979 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003980 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003981 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003982
3983 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003984 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003985 while (j - i > 1) {
3986 k = (j + i) / 2;
3987 m = nasm_stricmp(t->text, conditions[k]);
3988 if (m == 0) {
3989 i = k;
3990 j = -2;
3991 break;
3992 } else if (m < 0) {
3993 j = k;
3994 } else
3995 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003996 }
3997 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003998 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003999 return i;
4000}
4001
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004002static bool paste_tokens(Token **head, const struct tokseq_match *m,
4003 int mnum, bool handle_paste_tokens)
H. Peter Anvind784a082009-04-20 14:01:18 -07004004{
4005 Token **tail, *t, *tt;
4006 Token **paste_head;
4007 bool did_paste = false;
4008 char *tmp;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004009 int i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004010
4011 /* Now handle token pasting... */
4012 paste_head = NULL;
4013 tail = head;
4014 while ((t = *tail) && (tt = t->next)) {
4015 switch (t->type) {
4016 case TOK_WHITESPACE:
4017 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004018 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07004019 t->next = delete_Token(tt);
4020 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004021 /* Do not advance paste_head here */
4022 tail = &t->next;
4023 }
H. Peter Anvind784a082009-04-20 14:01:18 -07004024 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004025 case TOK_PASTE: /* %+ */
4026 if (handle_paste_tokens) {
4027 /* Zap %+ and whitespace tokens to the right */
4028 while (t && (t->type == TOK_WHITESPACE ||
4029 t->type == TOK_PASTE))
4030 t = *tail = delete_Token(t);
4031 if (!paste_head || !t)
4032 break; /* Nothing to paste with */
4033 tail = paste_head;
4034 t = *tail;
4035 tt = t->next;
4036 while (tok_type_(tt, TOK_WHITESPACE))
4037 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004038 if (tt) {
4039 tmp = nasm_strcat(t->text, tt->text);
4040 delete_Token(t);
4041 tt = delete_Token(tt);
4042 t = *tail = tokenize(tmp);
4043 nasm_free(tmp);
4044 while (t->next) {
4045 tail = &t->next;
4046 t = t->next;
4047 }
4048 t->next = tt; /* Attach the remaining token chain */
4049 did_paste = true;
4050 }
4051 paste_head = tail;
4052 tail = &t->next;
4053 break;
4054 }
4055 /* else fall through */
4056 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004057 /*
4058 * Concatenation of tokens might look nontrivial
4059 * but in real it's pretty simple -- the caller
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004060 * prepares the masks of token types to be concatenated
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004061 * and we simply find matched sequences and slip
4062 * them together
4063 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004064 for (i = 0; i < mnum; i++) {
4065 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
4066 size_t len = 0;
4067 char *tmp, *p;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004068
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004069 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004070 len += strlen(tt->text);
4071 tt = tt->next;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004072 }
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004073
Cyrill Gorcunovfdd0ac52011-06-27 01:22:27 +04004074 nasm_dump_token(tt);
4075
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004076 /*
4077 * Now tt points to the first token after
4078 * the potential paste area...
4079 */
4080 if (tt != t->next) {
4081 /* We have at least two tokens... */
4082 len += strlen(t->text);
4083 p = tmp = nasm_malloc(len+1);
4084 while (t != tt) {
4085 strcpy(p, t->text);
4086 p = strchr(p, '\0');
4087 t = delete_Token(t);
4088 }
4089 t = *tail = tokenize(tmp);
4090 nasm_free(tmp);
4091 while (t->next) {
4092 tail = &t->next;
4093 t = t->next;
4094 }
4095 t->next = tt; /* Attach the remaining token chain */
4096 did_paste = true;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004097 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004098 paste_head = tail;
4099 tail = &t->next;
4100 break;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004101 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004102 }
4103 if (i >= mnum) { /* no match */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004104 tail = &t->next;
4105 if (!tok_type_(t->next, TOK_WHITESPACE))
4106 paste_head = tail;
4107 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004108 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004109 }
4110 }
4111 return did_paste;
4112}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004113
4114/*
4115 * expands to a list of tokens from %{x:y}
4116 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004117static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004118{
4119 Token *t = tline, **tt, *tm, *head;
4120 char *pos;
4121 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004122
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004123 pos = strchr(tline->text, ':');
4124 nasm_assert(pos);
4125
4126 lst = atoi(pos + 1);
4127 fst = atoi(tline->text + 1);
4128
4129 /*
4130 * only macros params are accounted so
4131 * if someone passes %0 -- we reject such
4132 * value(s)
4133 */
4134 if (lst == 0 || fst == 0)
4135 goto err;
4136
4137 /* the values should be sane */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004138 if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) ||
4139 (lst > (int)ei->nparam || lst < (-(int)ei->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004140 goto err;
4141
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004142 fst = fst < 0 ? fst + (int)ei->nparam + 1: fst;
4143 lst = lst < 0 ? lst + (int)ei->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004144
4145 /* counted from zero */
4146 fst--, lst--;
4147
4148 /*
4149 * it will be at least one token
4150 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004151 tm = ei->params[(fst + ei->rotate) % ei->nparam];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004152 t = new_Token(NULL, tm->type, tm->text, 0);
4153 head = t, tt = &t->next;
4154 if (fst < lst) {
4155 for (i = fst + 1; i <= lst; i++) {
4156 t = new_Token(NULL, TOK_OTHER, ",", 0);
4157 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004158 j = (i + ei->rotate) % ei->nparam;
4159 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004160 t = new_Token(NULL, tm->type, tm->text, 0);
4161 *tt = t, tt = &t->next;
4162 }
4163 } else {
4164 for (i = fst - 1; i >= lst; i--) {
4165 t = new_Token(NULL, TOK_OTHER, ",", 0);
4166 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004167 j = (i + ei->rotate) % ei->nparam;
4168 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004169 t = new_Token(NULL, tm->type, tm->text, 0);
4170 *tt = t, tt = &t->next;
4171 }
4172 }
4173
4174 *last = tt;
4175 return head;
4176
4177err:
4178 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
4179 &tline->text[1]);
4180 return tline;
4181}
4182
H. Peter Anvin76690a12002-04-30 20:52:49 +00004183/*
4184 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004185 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004186 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004187 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004188static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004189{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004190 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004191 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004192 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004193
4194 tail = &thead;
4195 thead = NULL;
4196
Cyrill Gorcunov2e046002011-06-26 23:33:56 +04004197 nasm_dump_stream(tline);
4198
H. Peter Anvine2c80182005-01-15 22:15:51 +00004199 while (tline) {
4200 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004201 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4202 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4203 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004204 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004205 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004206 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004207 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004208 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004209 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004210
H. Peter Anvine2c80182005-01-15 22:15:51 +00004211 t = tline;
4212 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004213
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004214 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
4215 if (ei->type == EXP_MMACRO) {
4216 break;
4217 }
4218 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004219 if (ei == NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004220 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004221 } else {
4222 pos = strchr(t->text, ':');
4223 if (!pos) {
4224 switch (t->text[1]) {
4225 /*
4226 * We have to make a substitution of one of the
4227 * forms %1, %-1, %+1, %%foo, %0.
4228 */
4229 case '0':
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004230 if ((strlen(t->text) > 2) && (t->text[2] == '0')) {
4231 type = TOK_ID;
4232 text = nasm_strdup(ei->label_text);
4233 } else {
4234 type = TOK_NUMBER;
4235 snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam);
4236 text = nasm_strdup(tmpbuf);
4237 }
4238 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004239 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004240 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004241 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004242 ei->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004243 text = nasm_strcat(tmpbuf, t->text + 2);
4244 break;
4245 case '-':
4246 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004247 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004248 tt = NULL;
4249 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004250 if (ei->nparam > 1)
4251 n = (n + ei->rotate) % ei->nparam;
4252 tt = ei->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004253 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004254 cc = find_cc(tt);
4255 if (cc == -1) {
4256 error(ERR_NONFATAL,
4257 "macro parameter %d is not a condition code",
4258 n + 1);
4259 text = NULL;
4260 } else {
4261 type = TOK_ID;
4262 if (inverse_ccs[cc] == -1) {
4263 error(ERR_NONFATAL,
4264 "condition code `%s' is not invertible",
4265 conditions[cc]);
4266 text = NULL;
4267 } else
4268 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4269 }
4270 break;
4271 case '+':
4272 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004273 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004274 tt = NULL;
4275 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004276 if (ei->nparam > 1)
4277 n = (n + ei->rotate) % ei->nparam;
4278 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004279 }
4280 cc = find_cc(tt);
4281 if (cc == -1) {
4282 error(ERR_NONFATAL,
4283 "macro parameter %d is not a condition code",
4284 n + 1);
4285 text = NULL;
4286 } else {
4287 type = TOK_ID;
4288 text = nasm_strdup(conditions[cc]);
4289 }
4290 break;
4291 default:
4292 n = atoi(t->text + 1) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004293 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004294 tt = NULL;
4295 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004296 if (ei->nparam > 1)
4297 n = (n + ei->rotate) % ei->nparam;
4298 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004299 }
4300 if (tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004301 for (i = 0; i < ei->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004302 *tail = new_Token(NULL, tt->type, tt->text, 0);
4303 tail = &(*tail)->next;
4304 tt = tt->next;
4305 }
4306 }
4307 text = NULL; /* we've done it here */
4308 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004309 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004310 } else {
4311 /*
4312 * seems we have a parameters range here
4313 */
4314 Token *head, **last;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004315 head = expand_mmac_params_range(ei, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004316 if (head != t) {
4317 *tail = head;
4318 *last = tline;
4319 tline = head;
4320 text = NULL;
4321 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004322 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004323 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004324 if (!text) {
4325 delete_Token(t);
4326 } else {
4327 *tail = t;
4328 tail = &t->next;
4329 t->type = type;
4330 nasm_free(t->text);
4331 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004332 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004333 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004334 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004335 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004336 } else if (tline->type == TOK_INDIRECT) {
4337 t = tline;
4338 tline = tline->next;
4339 tt = tokenize(t->text);
4340 tt = expand_mmac_params(tt);
4341 tt = expand_smacro(tt);
4342 *tail = tt;
4343 while (tt) {
4344 tt->a.mac = NULL; /* Necessary? */
4345 tail = &tt->next;
4346 tt = tt->next;
4347 }
4348 delete_Token(t);
4349 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004350 } else {
4351 t = *tail = tline;
4352 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004353 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004354 tail = &t->next;
4355 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004356 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004357 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004358
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004359 if (changed) {
4360 const struct tokseq_match t[] = {
4361 {
4362 PP_CONCAT_MASK(TOK_ID) |
4363 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4364 PP_CONCAT_MASK(TOK_ID) |
4365 PP_CONCAT_MASK(TOK_NUMBER) |
4366 PP_CONCAT_MASK(TOK_FLOAT) |
4367 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4368 },
4369 {
4370 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4371 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4372 }
4373 };
4374 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4375 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004376
Cyrill Gorcunov2e046002011-06-26 23:33:56 +04004377 nasm_dump_token(thead);
4378
H. Peter Anvin76690a12002-04-30 20:52:49 +00004379 return thead;
4380}
4381
4382/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004383 * Expand all single-line macro calls made in the given line.
4384 * Return the expanded version of the line. The original is deemed
4385 * to be destroyed in the process. (In reality we'll just move
4386 * Tokens from input to output a lot of the time, rather than
4387 * actually bothering to destroy and replicate.)
4388 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004389
H. Peter Anvine2c80182005-01-15 22:15:51 +00004390static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004391{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004392 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004393 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004394 Token **params;
4395 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004396 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004397 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004398 Token *org_tline = tline;
4399 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004400 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004401 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004402 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004403
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004404 /*
4405 * Trick: we should avoid changing the start token pointer since it can
4406 * be contained in "next" field of other token. Because of this
4407 * we allocate a copy of first token and work with it; at the end of
4408 * routine we copy it back
4409 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004410 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004411 tline = new_Token(org_tline->next, org_tline->type,
4412 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004413 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004414 nasm_free(org_tline->text);
4415 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004416 }
4417
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004418 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004419
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004420again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004421 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004422 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004423
H. Peter Anvine2c80182005-01-15 22:15:51 +00004424 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004425 if (!--deadman) {
4426 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004427 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004428 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004429
H. Peter Anvine2c80182005-01-15 22:15:51 +00004430 if ((mname = tline->text)) {
4431 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004432 if (tline->type == TOK_ID) {
4433 head = (SMacro *)hash_findix(&smacros, mname);
4434 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04004435 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004436 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4437 } else
4438 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004439
H. Peter Anvine2c80182005-01-15 22:15:51 +00004440 /*
4441 * We've hit an identifier. As in is_mmacro below, we first
4442 * check whether the identifier is a single-line macro at
4443 * all, then think about checking for parameters if
4444 * necessary.
4445 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004446 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004447 if (!mstrcmp(m->name, mname, m->casesense))
4448 break;
4449 if (m) {
4450 mstart = tline;
4451 params = NULL;
4452 paramsize = NULL;
4453 if (m->nparam == 0) {
4454 /*
4455 * Simple case: the macro is parameterless. Discard the
4456 * one token that the macro call took, and push the
4457 * expansion back on the to-do stack.
4458 */
4459 if (!m->expansion) {
4460 if (!strcmp("__FILE__", m->name)) {
4461 int32_t num = 0;
4462 char *file = NULL;
4463 src_get(&num, &file);
4464 tline->text = nasm_quote(file, strlen(file));
4465 tline->type = TOK_STRING;
4466 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004467 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004468 }
4469 if (!strcmp("__LINE__", m->name)) {
4470 nasm_free(tline->text);
4471 make_tok_num(tline, src_get_linnum());
4472 continue;
4473 }
4474 if (!strcmp("__BITS__", m->name)) {
4475 nasm_free(tline->text);
4476 make_tok_num(tline, globalbits);
4477 continue;
4478 }
4479 tline = delete_Token(tline);
4480 continue;
4481 }
4482 } else {
4483 /*
4484 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004485 * exists and takes parameters. We must find the
4486 * parameters in the call, count them, find the SMacro
4487 * that corresponds to that form of the macro call, and
4488 * substitute for the parameters when we expand. What a
4489 * pain.
4490 */
4491 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004492 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004493 do {
4494 t = tline->next;
4495 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004496 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004497 t->text = NULL;
4498 t = tline->next = delete_Token(t);
4499 }
4500 tline = t;
4501 } while (tok_type_(tline, TOK_WHITESPACE));
4502 if (!tok_is_(tline, "(")) {
4503 /*
4504 * This macro wasn't called with parameters: ignore
4505 * the call. (Behaviour borrowed from gnu cpp.)
4506 */
4507 tline = mstart;
4508 m = NULL;
4509 } else {
4510 int paren = 0;
4511 int white = 0;
4512 brackets = 0;
4513 nparam = 0;
4514 sparam = PARAM_DELTA;
4515 params = nasm_malloc(sparam * sizeof(Token *));
4516 params[0] = tline->next;
4517 paramsize = nasm_malloc(sparam * sizeof(int));
4518 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004519 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004520 /*
4521 * For some unusual expansions
4522 * which concatenates function call
4523 */
4524 t = tline->next;
4525 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004526 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004527 t->text = NULL;
4528 t = tline->next = delete_Token(t);
4529 }
4530 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004531
H. Peter Anvine2c80182005-01-15 22:15:51 +00004532 if (!tline) {
4533 error(ERR_NONFATAL,
4534 "macro call expects terminating `)'");
4535 break;
4536 }
4537 if (tline->type == TOK_WHITESPACE
4538 && brackets <= 0) {
4539 if (paramsize[nparam])
4540 white++;
4541 else
4542 params[nparam] = tline->next;
4543 continue; /* parameter loop */
4544 }
4545 if (tline->type == TOK_OTHER
4546 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004547 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004548 if (ch == ',' && !paren && brackets <= 0) {
4549 if (++nparam >= sparam) {
4550 sparam += PARAM_DELTA;
4551 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004552 sparam * sizeof(Token *));
4553 paramsize = nasm_realloc(paramsize,
4554 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004555 }
4556 params[nparam] = tline->next;
4557 paramsize[nparam] = 0;
4558 white = 0;
4559 continue; /* parameter loop */
4560 }
4561 if (ch == '{' &&
4562 (brackets > 0 || (brackets == 0 &&
4563 !paramsize[nparam])))
4564 {
4565 if (!(brackets++)) {
4566 params[nparam] = tline->next;
4567 continue; /* parameter loop */
4568 }
4569 }
4570 if (ch == '}' && brackets > 0)
4571 if (--brackets == 0) {
4572 brackets = -1;
4573 continue; /* parameter loop */
4574 }
4575 if (ch == '(' && !brackets)
4576 paren++;
4577 if (ch == ')' && brackets <= 0)
4578 if (--paren < 0)
4579 break;
4580 }
4581 if (brackets < 0) {
4582 brackets = 0;
4583 error(ERR_NONFATAL, "braces do not "
4584 "enclose all of macro parameter");
4585 }
4586 paramsize[nparam] += white + 1;
4587 white = 0;
4588 } /* parameter loop */
4589 nparam++;
4590 while (m && (m->nparam != nparam ||
4591 mstrcmp(m->name, mname,
4592 m->casesense)))
4593 m = m->next;
4594 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004595 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004596 "macro `%s' exists, "
4597 "but not taking %d parameters",
4598 mstart->text, nparam);
4599 }
4600 }
4601 if (m && m->in_progress)
4602 m = NULL;
4603 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004604 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004605 * Design question: should we handle !tline, which
4606 * indicates missing ')' here, or expand those
4607 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004608 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004609 */
4610 nasm_free(params);
4611 nasm_free(paramsize);
4612 tline = mstart;
4613 } else {
4614 /*
4615 * Expand the macro: we are placed on the last token of the
4616 * call, so that we can easily split the call from the
4617 * following tokens. We also start by pushing an SMAC_END
4618 * token for the cycle removal.
4619 */
4620 t = tline;
4621 if (t) {
4622 tline = t->next;
4623 t->next = NULL;
4624 }
4625 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004626 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004627 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004628 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004629 list_for_each(t, m->expansion) {
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04004630 if (is_smacro_param(t)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004631 Token *pcopy = tline, **ptail = &pcopy;
Cyrill Gorcunov0ad6a7b2011-06-30 01:36:45 +04004632 Token *ttt;
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04004633 int i, idx;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004634
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04004635 idx = smacro_get_param_idx(t);
4636 ttt = params[idx];
Cyrill Gorcunov0ad6a7b2011-06-30 01:36:45 +04004637
4638 /*
4639 * We need smacro paramters appended.
4640 */
4641 for (i = paramsize[idx]; i > 0; i--) {
4642 *ptail = new_Token(tline, ttt->type, ttt->text, 0);
4643 ptail = &(*ptail)->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004644 ttt = ttt->next;
4645 }
Cyrill Gorcunov0ad6a7b2011-06-30 01:36:45 +04004646
H. Peter Anvine2c80182005-01-15 22:15:51 +00004647 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004648 } else if (t->type == TOK_PREPROC_Q) {
4649 tt = new_Token(tline, TOK_ID, mname, 0);
4650 tline = tt;
4651 } else if (t->type == TOK_PREPROC_QQ) {
4652 tt = new_Token(tline, TOK_ID, m->name, 0);
4653 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004654 } else {
4655 tt = new_Token(tline, t->type, t->text, 0);
4656 tline = tt;
4657 }
4658 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004659
H. Peter Anvine2c80182005-01-15 22:15:51 +00004660 /*
4661 * Having done that, get rid of the macro call, and clean
4662 * up the parameters.
4663 */
4664 nasm_free(params);
4665 nasm_free(paramsize);
4666 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004667 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004668 continue; /* main token loop */
4669 }
4670 }
4671 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004672
H. Peter Anvine2c80182005-01-15 22:15:51 +00004673 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004674 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004675 tline = delete_Token(tline);
4676 } else {
4677 t = *tail = tline;
4678 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004679 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004680 t->next = NULL;
4681 tail = &t->next;
4682 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004683 }
4684
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004685 /*
4686 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004687 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004688 * TOK_IDs should be concatenated.
4689 * Also we look for %+ tokens and concatenate the tokens before and after
4690 * them (without white spaces in between).
4691 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004692 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004693 const struct tokseq_match t[] = {
4694 {
4695 PP_CONCAT_MASK(TOK_ID) |
4696 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4697 PP_CONCAT_MASK(TOK_ID) |
4698 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4699 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4700 }
4701 };
4702 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004703 /*
4704 * If we concatenated something, *and* we had previously expanded
4705 * an actual macro, scan the lines again for macros...
4706 */
4707 tline = thead;
4708 expanded = false;
4709 goto again;
4710 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004711 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004712
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004713err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004714 if (org_tline) {
4715 if (thead) {
4716 *org_tline = *thead;
4717 /* since we just gave text to org_line, don't free it */
4718 thead->text = NULL;
4719 delete_Token(thead);
4720 } else {
4721 /* the expression expanded to empty line;
4722 we can't return NULL for some reasons
4723 we just set the line to a single WHITESPACE token. */
4724 memset(org_tline, 0, sizeof(*org_tline));
4725 org_tline->text = NULL;
4726 org_tline->type = TOK_WHITESPACE;
4727 }
4728 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004729 }
4730
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004731 return thead;
4732}
4733
4734/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004735 * Similar to expand_smacro but used exclusively with macro identifiers
4736 * right before they are fetched in. The reason is that there can be
4737 * identifiers consisting of several subparts. We consider that if there
4738 * are more than one element forming the name, user wants a expansion,
4739 * otherwise it will be left as-is. Example:
4740 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004741 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004742 *
4743 * the identifier %$abc will be left as-is so that the handler for %define
4744 * will suck it and define the corresponding value. Other case:
4745 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004746 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004747 *
4748 * In this case user wants name to be expanded *before* %define starts
4749 * working, so we'll expand %$abc into something (if it has a value;
4750 * otherwise it will be left as-is) then concatenate all successive
4751 * PP_IDs into one.
4752 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004753static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004754{
4755 Token *cur, *oldnext = NULL;
4756
H. Peter Anvin734b1882002-04-30 21:01:08 +00004757 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004758 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004759
4760 cur = tline;
4761 while (cur->next &&
Cyrill Gorcunov5b6c96b2011-06-30 00:22:53 +04004762 (cur->next->type == TOK_ID ||
4763 cur->next->type == TOK_PREPROC_ID ||
4764 cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004765 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004766
4767 /* If identifier consists of just one token, don't expand */
4768 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004769 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004770
H. Peter Anvine2c80182005-01-15 22:15:51 +00004771 if (cur) {
4772 oldnext = cur->next; /* Detach the tail past identifier */
4773 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004774 }
4775
H. Peter Anvin734b1882002-04-30 21:01:08 +00004776 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004777
H. Peter Anvine2c80182005-01-15 22:15:51 +00004778 if (cur) {
4779 /* expand_smacro possibly changhed tline; re-scan for EOL */
4780 cur = tline;
4781 while (cur && cur->next)
4782 cur = cur->next;
4783 if (cur)
4784 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004785 }
4786
4787 return tline;
4788}
4789
4790/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004791 * Determine whether the given line constitutes a multi-line macro
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004792 * call, and return the ExpDef structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004793 * to check for an initial label - that's taken care of in
4794 * expand_mmacro - but must check numbers of parameters. Guaranteed
4795 * to be called with tline->type == TOK_ID, so the putative macro
4796 * name is easy to find.
4797 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004798static ExpDef *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004799{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004800 ExpDef *head, *ed;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004801 Token **params;
4802 int nparam;
4803
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004804 head = (ExpDef *) hash_findix(&expdefs, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004805
4806 /*
4807 * Efficiency: first we see if any macro exists with the given
4808 * name. If not, we can return NULL immediately. _Then_ we
4809 * count the parameters, and then we look further along the
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004810 * list if necessary to find the proper ExpDef.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004811 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004812 list_for_each(ed, head)
4813 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004814 break;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004815 if (!ed)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004816 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004817
4818 /*
4819 * OK, we have a potential macro. Count and demarcate the
4820 * parameters.
4821 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004822 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004823
4824 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004825 * So we know how many parameters we've got. Find the ExpDef
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004826 * structure that handles this number.
4827 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004828 while (ed) {
4829 if (ed->nparam_min <= nparam
4830 && (ed->plus || nparam <= ed->nparam_max)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004831 /*
4832 * It's right, and we can use it. Add its default
4833 * parameters to the end of our list if necessary.
4834 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004835 if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004836 params =
4837 nasm_realloc(params,
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004838 ((ed->nparam_min + ed->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004839 1) * sizeof(*params)));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004840 while (nparam < ed->nparam_min + ed->ndefs) {
4841 params[nparam] = ed->defaults[nparam - ed->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004842 nparam++;
4843 }
4844 }
4845 /*
4846 * If we've gone over the maximum parameter count (and
4847 * we're in Plus mode), ignore parameters beyond
4848 * nparam_max.
4849 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004850 if (ed->plus && nparam > ed->nparam_max)
4851 nparam = ed->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004852 /*
4853 * Then terminate the parameter list, and leave.
4854 */
4855 if (!params) { /* need this special case */
4856 params = nasm_malloc(sizeof(*params));
4857 nparam = 0;
4858 }
4859 params[nparam] = NULL;
4860 *params_array = params;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004861 return ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004862 }
4863 /*
4864 * This one wasn't right: look for the next one with the
4865 * same name.
4866 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004867 list_for_each(ed, ed->next)
4868 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004869 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004870 }
4871
4872 /*
4873 * After all that, we didn't find one with the right number of
4874 * parameters. Issue a warning, and fail to expand the macro.
4875 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004876 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004877 "macro `%s' exists, but not taking %d parameters",
4878 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004879 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004880 return NULL;
4881}
4882
4883/*
4884 * Expand the multi-line macro call made by the given line, if
4885 * there is one to be expanded. If there is, push the expansion on
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004886 * istk->expansion and return true. Otherwise return false.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004887 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004888static bool expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004889{
H. Peter Anvineba20a72002-04-30 20:53:55 +00004890 Token *label = NULL;
4891 int dont_prepend = 0;
Cyrill Gorcunovfb27fc22011-06-25 12:08:30 +04004892 Token **params, *t;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004893 Line *l = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004894 ExpDef *ed;
4895 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004896 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004897 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004898
4899 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004900 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004901 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004902 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004903 return false;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004904 ed = is_mmacro(t, &params);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004905 if (ed != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004906 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004907 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004908 Token *last;
4909 /*
4910 * We have an id which isn't a macro call. We'll assume
4911 * it might be a label; we'll also check to see if a
4912 * colon follows it. Then, if there's another id after
4913 * that lot, we'll check it again for macro-hood.
4914 */
4915 label = last = t;
4916 t = t->next;
4917 if (tok_type_(t, TOK_WHITESPACE))
4918 last = t, t = t->next;
4919 if (tok_is_(t, ":")) {
4920 dont_prepend = 1;
4921 last = t, t = t->next;
4922 if (tok_type_(t, TOK_WHITESPACE))
4923 last = t, t = t->next;
4924 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004925 if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, &params)))
4926 return false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004927 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004928 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004929 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004930 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004931
4932 /*
4933 * Fix up the parameters: this involves stripping leading and
4934 * trailing whitespace, then stripping braces if they are
4935 * present.
4936 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004937 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004938 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004939
H. Peter Anvine2c80182005-01-15 22:15:51 +00004940 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004941 int brace = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004942 int comma = (!ed->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004943
H. Peter Anvine2c80182005-01-15 22:15:51 +00004944 t = params[i];
4945 skip_white_(t);
4946 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004947 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004948 params[i] = t;
4949 paramlen[i] = 0;
4950 while (t) {
4951 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4952 break; /* ... because we have hit a comma */
4953 if (comma && t->type == TOK_WHITESPACE
4954 && tok_is_(t->next, ","))
4955 break; /* ... or a space then a comma */
4956 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4957 break; /* ... or a brace */
4958 t = t->next;
4959 paramlen[i]++;
4960 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004961 }
4962
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004963 if (ed->cur_depth >= ed->max_depth) {
4964 if (ed->max_depth > 1) {
4965 error(ERR_WARNING,
4966 "reached maximum macro recursion depth of %i for %s",
4967 ed->max_depth,ed->name);
4968 }
4969 return false;
4970 } else {
4971 ed->cur_depth ++;
4972 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004973
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004974 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004975 * OK, we have found a ExpDef structure representing a
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004976 * previously defined mmacro. Create an expansion invocation
4977 * and point it back to the expansion definition. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004978 * parameter tokens and macro-local tokens doesn't get done
4979 * until the single-line macro substitution process; this is
4980 * because delaying them allows us to change the semantics
4981 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004982 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004983 ei = new_ExpInv(EXP_MMACRO, ed);
4984 ei->name = nasm_strdup(mname);
4985 //ei->label = label;
4986 //ei->label_text = detoken(label, false);
4987 ei->current = ed->line;
4988 ei->emitting = true;
4989 //ei->iline = tline;
4990 ei->params = params;
4991 ei->nparam = nparam;
4992 ei->rotate = 0;
4993 ei->paramlen = paramlen;
4994 ei->lineno = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004995
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004996 ei->prev = istk->expansion;
4997 istk->expansion = ei;
4998
4999 /*
5000 * Special case: detect %00 on first invocation; if found,
5001 * avoid emitting any labels that precede the mmacro call.
5002 * ed->prepend is set to -1 when %00 is detected, else 1.
5003 */
5004 if (ed->prepend == 0) {
5005 for (l = ed->line; l != NULL; l = l->next) {
5006 for (t = l->first; t != NULL; t = t->next) {
5007 if ((t->type == TOK_PREPROC_ID) &&
5008 (strlen(t->text) == 3) &&
5009 (t->text[1] == '0') && (t->text[2] == '0')) {
5010 dont_prepend = -1;
5011 break;
5012 }
5013 }
5014 if (dont_prepend < 0) {
5015 break;
5016 }
5017 }
5018 ed->prepend = ((dont_prepend < 0) ? -1 : 1);
5019 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005020
5021 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00005022 * If we had a label, push it on as the first line of
5023 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005024 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005025 if (label != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005026 if (ed->prepend < 0) {
5027 ei->label_text = detoken(label, false);
5028 } else {
5029 if (dont_prepend == 0) {
5030 t = label;
5031 while (t->next != NULL) {
5032 t = t->next;
5033 }
5034 t->next = new_Token(NULL, TOK_OTHER, ":", 0);
5035 }
5036 l = new_Line();
5037 l->first = copy_Token(label);
5038 l->next = ei->current;
5039 ei->current = l;
5040 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005041 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005042
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005043 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005044
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005045 istk->mmac_depth++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005046 return true;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005047}
5048
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005049/* The function that actually does the error reporting */
5050static void verror(int severity, const char *fmt, va_list arg)
5051{
5052 char buff[1024];
5053
5054 vsnprintf(buff, sizeof(buff), fmt, arg);
5055
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005056 if (istk && istk->mmac_depth > 0) {
5057 ExpInv *ei = istk->expansion;
5058 int lineno = ei->lineno;
5059 while (ei) {
5060 if (ei->type == EXP_MMACRO)
5061 break;
5062 lineno += ei->relno;
5063 ei = ei->prev;
5064 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005065 nasm_error(severity, "(%s:%d) %s", ei->def->name,
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005066 lineno, buff);
5067 } else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005068 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005069}
5070
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005071/*
5072 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07005073 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005074 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005075static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005076{
5077 va_list arg;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005078 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005079 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00005080 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005081}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005082
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005083/*
5084 * Because %else etc are evaluated in the state context
5085 * of the previous branch, errors might get lost with error():
5086 * %if 0 ... %else trailing garbage ... %endif
5087 * So %else etc should report errors with this function.
5088 */
5089static void error_precond(int severity, const char *fmt, ...)
5090{
5091 va_list arg;
5092
5093 /* Only ignore the error if it's really in a dead branch */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005094 if ((istk != NULL) &&
5095 (istk->expansion != NULL) &&
5096 (istk->expansion->type == EXP_IF) &&
5097 (istk->expansion->def->state == COND_NEVER))
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005098 return;
5099
5100 va_start(arg, fmt);
5101 verror(severity, fmt, arg);
5102 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005103}
5104
H. Peter Anvin734b1882002-04-30 21:01:08 +00005105static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005106pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005107{
H. Peter Anvin7383b402008-09-24 10:20:40 -07005108 Token *t;
5109
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005110 cstk = NULL;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005111 istk = nasm_zalloc(sizeof(Include));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005112 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00005113 src_set_fname(nasm_strdup(file));
5114 src_set_linnum(0);
5115 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005116 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07005117 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00005118 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005119 defining = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005120 finals = NULL;
5121 in_final = false;
Charles Crayned4200be2008-07-12 16:42:33 -07005122 nested_mac_count = 0;
5123 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005124 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005125 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005126 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005127 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005128 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005129 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005130 }
H. Peter Anvind2456592008-06-19 15:04:18 -07005131 any_extrastdmac = extrastdmac && *extrastdmac;
5132 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005133 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005134
5135 /*
5136 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5137 * The caller, however, will also pass in 3 for preprocess-only so
5138 * we can set __PASS__ accordingly.
5139 */
5140 pass = apass > 2 ? 2 : apass;
5141
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005142 dephead = deptail = deplist;
5143 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005144 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
5145 sl->next = NULL;
5146 strcpy(sl->str, file);
5147 *deptail = sl;
5148 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005149 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07005150
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005151 /*
5152 * Define the __PASS__ macro. This is defined here unlike
5153 * all the other builtins, because it is special -- it varies between
5154 * passes.
5155 */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005156 t = nasm_zalloc(sizeof(*t));
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005157 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07005158 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005159}
5160
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005161static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005162{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005163 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005164 Token *tline;
Keith Kanios9858cec2010-11-08 00:58:02 -06005165 ExpDef *ed;
5166 ExpInv *ei;
5167 Line *l;
5168 int j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005169
H. Peter Anvine2c80182005-01-15 22:15:51 +00005170 while (1) {
5171 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005172 * Fetch a tokenized line, either from the expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005173 * buffer or from the input file.
5174 */
5175 tline = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005176
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005177 while (1) { /* until we get a line we can use */
5178 /*
5179 * Fetch a tokenized line from the expansion buffer
5180 */
5181 if (istk->expansion != NULL) {
5182 ei = istk->expansion;
5183 if (ei->current != NULL) {
5184 if (ei->emitting == false) {
5185 ei->current = NULL;
5186 continue;
5187 }
5188 l = ei->current;
5189 ei->current = l->next;
5190 ei->lineno++;
5191 tline = copy_Token(l->first);
5192 if (((ei->type == EXP_REP) ||
5193 (ei->type == EXP_MMACRO) ||
5194 (ei->type == EXP_WHILE))
5195 && (ei->def->nolist == false)) {
5196 char *p = detoken(tline, false);
5197 list->line(LIST_MACRO, p);
5198 nasm_free(p);
5199 }
5200 if (ei->linnum > -1) {
5201 src_set_linnum(src_get_linnum() + 1);
5202 }
5203 break;
5204 } else if ((ei->type == EXP_REP) &&
5205 (ei->def->cur_depth < ei->def->max_depth)) {
5206 ei->def->cur_depth ++;
5207 ei->current = ei->def->line;
5208 ei->lineno = 0;
5209 continue;
5210 } else if ((ei->type == EXP_WHILE) &&
5211 (ei->def->cur_depth < ei->def->max_depth)) {
5212 ei->current = ei->def->line;
5213 ei->lineno = 0;
5214 tline = copy_Token(ei->current->first);
5215 j = if_condition(tline, PP_WHILE);
5216 tline = NULL;
5217 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
5218 if (j == COND_IF_TRUE) {
5219 ei->current = ei->current->next;
5220 ei->def->cur_depth ++;
5221 } else {
5222 ei->emitting = false;
5223 ei->current = NULL;
5224 ei->def->cur_depth = ei->def->max_depth;
5225 }
5226 continue;
5227 } else {
5228 istk->expansion = ei->prev;
5229 ed = ei->def;
5230 if (ed != NULL) {
5231 if ((ei->emitting == true) &&
5232 (ed->max_depth == DEADMAN_LIMIT) &&
5233 (ed->cur_depth == DEADMAN_LIMIT)
5234 ) {
5235 error(ERR_FATAL, "runaway expansion detected, aborting");
5236 }
5237 if (ed->cur_depth > 0) {
5238 ed->cur_depth --;
Keith Kanios94124652010-12-18 11:47:28 -06005239 } else if (ed->type != EXP_MMACRO) {
Keith Kanios6a7c3e92010-12-18 11:49:53 -06005240 expansions = ed->prev;
5241 free_expdef(ed);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005242 }
5243 if ((ei->type == EXP_REP) ||
5244 (ei->type == EXP_MMACRO) ||
5245 (ei->type == EXP_WHILE)) {
5246 list->downlevel(LIST_MACRO);
5247 if (ei->type == EXP_MMACRO) {
5248 istk->mmac_depth--;
5249 }
5250 }
5251 }
5252 if (ei->linnum > -1) {
5253 src_set_linnum(ei->linnum);
5254 }
5255 free_expinv(ei);
5256 continue;
5257 }
5258 }
5259
5260 /*
5261 * Read in line from input and tokenize
5262 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005263 line = read_line();
5264 if (line) { /* from the current input file */
5265 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005266 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005267 nasm_free(line);
5268 break;
5269 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005270
H. Peter Anvine2c80182005-01-15 22:15:51 +00005271 /*
5272 * The current file has ended; work down the istk
5273 */
5274 {
5275 Include *i = istk;
5276 fclose(i->fp);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005277 if (i->expansion != NULL) {
5278 error(ERR_FATAL,
5279 "end of file while still in an expansion");
5280 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005281 /* only set line and file name if there's a next node */
5282 if (i->next) {
5283 src_set_linnum(i->lineno);
Cyrill Gorcunovd34a1082011-03-07 11:23:08 +03005284 nasm_free(src_set_fname(nasm_strdup(i->fname)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005285 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005286 if ((i->next == NULL) && (finals != NULL)) {
5287 in_final = true;
5288 ei = new_ExpInv(EXP_FINAL, NULL);
5289 ei->emitting = true;
5290 ei->current = finals;
5291 istk->expansion = ei;
5292 finals = NULL;
5293 continue;
5294 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005295 istk = i->next;
5296 list->downlevel(LIST_INCLUDE);
5297 nasm_free(i);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005298 if (istk == NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005299 if (finals != NULL) {
5300 in_final = true;
5301 } else {
5302 return NULL;
5303 }
5304 }
5305 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005306 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005307 }
5308
5309 if (defining == NULL) {
5310 tline = expand_mmac_params(tline);
5311 }
5312
H. Peter Anvine2c80182005-01-15 22:15:51 +00005313 /*
5314 * Check the line to see if it's a preprocessor directive.
5315 */
5316 if (do_directive(tline) == DIRECTIVE_FOUND) {
5317 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005318 } else if (defining != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005319 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005320 * We're defining an expansion. We emit nothing at all,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005321 * and just shove the tokenized line on to the definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005322 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005323 if (defining->ignoring == false) {
5324 Line *l = new_Line();
5325 l->first = tline;
5326 if (defining->line == NULL) {
5327 defining->line = l;
5328 defining->last = l;
5329 } else {
5330 defining->last->next = l;
5331 defining->last = l;
5332 }
5333 } else {
Keith Kanios104803d2010-12-18 11:05:46 -06005334 free_tlist(tline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005335 }
5336 defining->linecount++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005337 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005338 } else if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005339 (istk->expansion->emitting != true)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005340 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005341 * We're in a non-emitting branch of an expansion.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005342 * Emit nothing at all, not even a blank line: when we
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005343 * emerge from the expansion we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005344 * directive so we keep our place correctly.
5345 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005346 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005347 continue;
5348 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005349 tline = expand_smacro(tline);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005350 if (expand_mmacro(tline) != true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005351 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005352 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005353 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005354 line = detoken(tline, true);
5355 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005356 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005357 } else {
5358 continue;
5359 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005360 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005361 }
5362 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005363}
5364
H. Peter Anvine2c80182005-01-15 22:15:51 +00005365static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005366{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005367 if (defining != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005368 error(ERR_NONFATAL, "end of file while still defining an expansion");
5369 while (defining != NULL) {
5370 ExpDef *ed = defining;
5371 defining = ed->prev;
5372 free_expdef(ed);
5373 }
5374 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005375 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005376 while (cstk != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005377 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005378 free_macros();
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005379 while (istk != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005380 Include *i = istk;
5381 istk = istk->next;
5382 fclose(i->fp);
5383 nasm_free(i->fname);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005384 while (i->expansion != NULL) {
5385 ExpInv *ei = i->expansion;
5386 i->expansion = ei->prev;
5387 free_expinv(ei);
5388 }
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005389 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005390 }
5391 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005392 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005393 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005394 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005395 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005396 free_llist(predef);
5397 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005398 while ((i = ipath)) {
5399 ipath = i->next;
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04005400 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005401 nasm_free(i);
5402 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005403 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005404}
5405
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005406void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005407{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005408 IncPath *i = nasm_zalloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005409
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005410 if (path)
5411 i->path = nasm_strdup(path);
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005412
H. Peter Anvin89cee572009-07-15 09:16:54 -04005413 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005414 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005415 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005416 j = j->next;
5417 j->next = i;
5418 } else {
5419 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005420 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005421}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005422
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005423void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005424{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005425 Token *inc, *space, *name;
5426 Line *l;
5427
H. Peter Anvin734b1882002-04-30 21:01:08 +00005428 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5429 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5430 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005431
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005432 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005433 l->next = predef;
5434 l->first = inc;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005435 predef = l;
5436}
5437
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005438void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005439{
5440 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005441 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005442 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005443
5444 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005445 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5446 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005447 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005448 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005449 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005450 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005451 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005452
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005453 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005454 l->next = predef;
5455 l->first = def;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005456 predef = l;
5457}
5458
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005459void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005460{
5461 Token *def, *space;
5462 Line *l;
5463
H. Peter Anvin734b1882002-04-30 21:01:08 +00005464 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5465 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005466 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005467
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005468 l = new_Line();
H. Peter Anvin620515a2002-04-30 20:57:38 +00005469 l->next = predef;
5470 l->first = def;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005471 predef = l;
5472}
5473
Keith Kaniosb7a89542007-04-12 02:40:54 +00005474/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005475 * This function is used to assist with "runtime" preprocessor
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005476 * directives, e.g. pp_runtime("%define __BITS__ 64");
Keith Kaniosb7a89542007-04-12 02:40:54 +00005477 *
5478 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5479 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5480 */
5481
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005482void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005483{
5484 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005485
Keith Kaniosb7a89542007-04-12 02:40:54 +00005486 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005487 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005488 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005489
Keith Kaniosb7a89542007-04-12 02:40:54 +00005490}
5491
H. Peter Anvina70547f2008-07-19 21:44:26 -07005492void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005493{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005494 extrastdmac = macros;
5495}
5496
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005497static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005498{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005499 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005500 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005501 tok->text = nasm_strdup(numbuf);
5502 tok->type = TOK_NUMBER;
5503}
5504
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +04005505struct preproc_ops nasmpp = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005506 pp_reset,
5507 pp_getline,
5508 pp_cleanup
5509};