blob: 1759d03b6ca030e3598c11780951f11f334a2fe3 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +04003 * Copyright 1996-2011 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000065#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000066#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000067#include <stdlib.h>
68#include <stddef.h>
69#include <string.h>
70#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000071#include <limits.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000072#include <inttypes.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000073
74#include "nasm.h"
75#include "nasmlib.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000076#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070077#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070078#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070079#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070080#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070081#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070082#include "tables.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000083
84typedef struct SMacro SMacro;
Keith Kaniosb307a4f2010-11-06 17:41:51 -050085typedef struct ExpDef ExpDef;
86typedef struct ExpInv ExpInv;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000087typedef struct Context Context;
88typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000089typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000090typedef struct Line Line;
91typedef struct Include Include;
92typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000093typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000094
95/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070096 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
102 */
103
104/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 * Store the definition of a single-line macro.
106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107struct SMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000108 SMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000109 char *name;
H. Peter Anvin70055962007-10-11 00:05:31 -0700110 bool casesense;
H. Peter Anvin16ed4382007-10-11 10:06:19 -0700111 bool in_progress;
H. Peter Anvin70055962007-10-11 00:05:31 -0700112 unsigned int nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000113 Token *expansion;
114};
115
116/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000117 * The context stack is composed of a linked list of these.
118 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000119struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000120 Context *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000121 char *name;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700122 struct hash_table localmac;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000123 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000124};
125
126/*
127 * This is the internal form which we break input lines up into.
128 * Typically stored in linked lists.
129 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000130 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
131 * necessarily used as-is, but is intended to denote the number of
132 * the substituted parameter. So in the definition
133 *
134 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700135 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000136 * the token representing `x' will have its type changed to
137 * TOK_SMAC_PARAM, but the one representing `y' will be
138 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000139 *
140 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
141 * which doesn't need quotes around it. Used in the pre-include
142 * mechanism as an alternative to trying to find a sensible type of
143 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000144 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000145enum pp_token_type {
146 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
147 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700148 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
149 TOK_INTERNAL_STRING,
150 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300151 TOK_PASTE, /* %+ */
152 TOK_INDIRECT, /* %[...] */
153 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
154 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000155};
156
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400157#define PP_CONCAT_MASK(x) (1 << (x))
158
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400159struct tokseq_match {
160 int mask_head;
161 int mask_tail;
162};
163
H. Peter Anvine2c80182005-01-15 22:15:51 +0000164struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000165 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000166 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700167 union {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300168 SMacro *mac; /* associated macro for TOK_SMAC_END */
169 size_t len; /* scratch length field */
170 } a; /* Auxiliary data */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000171 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000172};
173
174/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500175 * Expansion definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000176 * these, which is essentially a container to allow several linked
177 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700178 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000179 * Note that in this module, linked lists are treated as stacks
180 * wherever possible. For this reason, Lines are _pushed_ on to the
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500181 * `last' field in ExpDef structures, so that the linked list,
182 * if walked, would emit the expansion lines in the proper order.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000183 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000184struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000185 Line *next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000186 Token *first;
187};
188
189/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500190 * Expansion Types
191 */
192enum pp_exp_type {
193 EXP_NONE = 0, EXP_PREDEF,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300194 EXP_MMACRO, EXP_REP,
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500195 EXP_IF, EXP_WHILE,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300196 EXP_COMMENT, EXP_FINAL,
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500197 EXP_MAX = INT_MAX /* Keep compiler from reducing the range */
198};
199
200/*
201 * Store the definition of an expansion, in which is any
202 * preprocessor directive that has an ending pair.
203 *
204 * This design allows for arbitrary expansion/recursion depth,
205 * upto the DEADMAN_LIMIT.
206 *
207 * The `next' field is used for storing ExpDef in hash tables; the
208 * `prev' field is for the global `expansions` linked-list.
209 */
210struct ExpDef {
211 ExpDef *prev; /* previous definition */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300212 ExpDef *next; /* next in hash table */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500213 enum pp_exp_type type; /* expansion type */
214 char *name; /* definition name */
215 int nparam_min, nparam_max;
216 bool casesense;
217 bool plus; /* is the last parameter greedy? */
218 bool nolist; /* is this expansion listing-inhibited? */
219 Token *dlist; /* all defaults as one list */
220 Token **defaults; /* parameter default pointers */
221 int ndefs; /* number of default parameters */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300222
223 int prepend; /* label prepend state */
224 Line *label;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500225 Line *line;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300226 Line *last;
227 int linecount; /* number of lines within expansion */
228
229 int64_t def_depth; /* current number of definition pairs deep */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500230 int64_t cur_depth; /* current number of expansions */
231 int64_t max_depth; /* maximum number of expansions allowed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300232
233 int state; /* condition state */
234 bool ignoring; /* ignoring definition lines */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500235};
236
237/*
238 * Store the invocation of an expansion.
239 *
240 * The `prev' field is for the `istk->expansion` linked-list.
241 *
242 * When an expansion is being expanded, `params', `iline', `nparam',
243 * `paramlen', `rotate' and `unique' are local to the invocation.
244 */
245struct ExpInv {
246 ExpInv *prev; /* previous invocation */
247 enum pp_exp_type type; /* expansion type */
248 ExpDef *def; /* pointer to expansion definition */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300249 char *name; /* invocation name */
250 Line *label; /* pointer to label */
251 char *label_text; /* pointer to label text */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500252 Line *current; /* pointer to current line in invocation */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300253
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500254 Token **params; /* actual parameters */
255 Token *iline; /* invocation line */
256 unsigned int nparam, rotate;
257 int *paramlen;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300258
259 uint64_t unique;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500260 bool emitting;
261 int lineno; /* current line number in expansion */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300262 int linnum; /* line number at invocation */
263 int relno; /* relative line number at invocation */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500264};
265
266/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000267 * To handle an arbitrary level of file inclusion, we maintain a
268 * stack (ie linked list) of these things.
269 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000270struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000271 Include *next;
272 FILE *fp;
273 Cond *conds;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500274 ExpInv *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000275 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000276 int lineno, lineinc;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300277 int mmac_depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000278};
279
280/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000281 * Include search path. This is simply a list of strings which get
282 * prepended, in turn, to the name of an include file, in an
283 * attempt to find the file if it's not in the current directory.
284 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000285struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000286 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000287 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000288};
289
290/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000291 * Conditional assembly: we maintain a separate stack of these for
292 * each level of file inclusion. (The only reason we keep the
293 * stacks separate is to ensure that a stray `%endif' in a file
294 * included from within the true branch of a `%if' won't terminate
295 * it and cause confusion: instead, rightly, it'll cause an error.)
296 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000297enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000298 /*
299 * These states are for use just after %if or %elif: IF_TRUE
300 * means the condition has evaluated to truth so we are
301 * currently emitting, whereas IF_FALSE means we are not
302 * currently emitting but will start doing so if a %else comes
303 * up. In these states, all directives are admissible: %elif,
304 * %else and %endif. (And of course %if.)
305 */
306 COND_IF_TRUE, COND_IF_FALSE,
307 /*
308 * These states come up after a %else: ELSE_TRUE means we're
309 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
310 * any %elif or %else will cause an error.
311 */
312 COND_ELSE_TRUE, COND_ELSE_FALSE,
313 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200314 * These states mean that we're not emitting now, and also that
315 * nothing until %endif will be emitted at all. COND_DONE is
316 * used when we've had our moment of emission
317 * and have now started seeing %elifs. COND_NEVER is used when
318 * the condition construct in question is contained within a
319 * non-emitting branch of a larger condition construct,
320 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000321 */
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200322 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000323};
324#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
325
H. Peter Anvin70653092007-10-19 14:42:29 -0700326/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000327 * These defines are used as the possible return values for do_directive
328 */
329#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300330#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000331
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000332/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500333 * This define sets the upper limit for smacro and expansions
Keith Kanios852f1ee2009-07-12 00:19:55 -0500334 */
335#define DEADMAN_LIMIT (1 << 20)
336
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400337/* max reps */
338#define REP_LIMIT ((INT64_C(1) << 62))
339
Keith Kanios852f1ee2009-07-12 00:19:55 -0500340/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341 * Condition codes. Note that we use c_ prefix not C_ because C_ is
342 * used in nasm.h for the "real" condition codes. At _this_ level,
343 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
344 * ones, so we need a different enum...
345 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700346static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000347 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
348 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000349 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000350};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700351enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000352 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
353 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
H. Peter Anvin476d2862007-10-02 22:04:15 -0700354 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
355 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000356};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700357static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000358 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
359 c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
H. Peter Anvince9be342007-09-12 00:22:29 +0000360 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000361};
362
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000363/* For TASM compatibility we need to be able to recognise TASM compatible
364 * conditional compilation directives. Using the NASM pre-processor does
365 * not work, so we look for them specifically from the following list and
366 * then jam in the equivalent NASM directive into the input stream.
367 */
368
H. Peter Anvine2c80182005-01-15 22:15:51 +0000369enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000370 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
371 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
372};
373
H. Peter Anvin476d2862007-10-02 22:04:15 -0700374static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000375 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
376 "ifndef", "include", "local"
377};
378
379static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000380static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000381static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800382static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000383
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000384static Context *cstk;
385static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000386static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000387
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300388static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700389static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000390
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300391static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000392
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000393static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700394static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000395
396static ListGen *list;
397
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000398/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500399 * The current set of expansion definitions we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000400 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500401static struct hash_table expdefs;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000402
403/*
404 * The current set of single-line macros we have defined.
405 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700406static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000407
408/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500409 * Linked List of all active expansion definitions
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000410 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500411struct ExpDef *expansions = NULL;
412
413/*
414 * The expansion we are currently defining
415 */
416static ExpDef *defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000417
Charles Crayned4200be2008-07-12 16:42:33 -0700418static uint64_t nested_mac_count;
419static uint64_t nested_rep_count;
420
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000421/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500422 * Linked-list of lines to preprocess, prior to cleanup
423 */
424static Line *finals = NULL;
425static bool in_final = false;
426
427/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000428 * The number of macro parameters to allocate space for at a time.
429 */
430#define PARAM_DELTA 16
431
432/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700433 * The standard macro set: defined in macros.c in the array nasm_stdmac.
434 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000435 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700436static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000437
438/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000439 * The extra standard macros that come from the object format, if
440 * any.
441 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700442static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700443static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000444
445/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000446 * Tokens are allocated in blocks to improve speed
447 */
448#define TOKEN_BLOCKSIZE 4096
449static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000450struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000451 Blocks *next;
452 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000453};
454
455static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000456
457/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000458 * Forward declarations.
459 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000460static Token *expand_mmac_params(Token * tline);
461static Token *expand_smacro(Token * tline);
462static Token *expand_id(Token * tline);
Cyrill Gorcunov290eac72011-06-28 01:59:05 +0400463static Context *get_ctx(const char *name, const char **namep);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700464static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000465static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200466static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000467static void *new_Block(size_t size);
468static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700469static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300470 const char *text, int txtlen);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500471static Token *copy_Token(Token * tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000472static Token *delete_Token(Token * t);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500473static Line *new_Line(void);
474static ExpDef *new_ExpDef(int exp_type);
475static ExpInv *new_ExpInv(int exp_type, ExpDef *ed);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000476
477/*
478 * Macros for safe checking of token pointers, avoid *(NULL)
479 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300480#define tok_type_(x,t) ((x) && (x)->type == (t))
481#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
482#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
483#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000484
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300485#ifdef NASM_TRACE
486
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400487#define stringify(x) #x
488
Cyrill Gorcunov9d1141a2011-06-26 23:07:35 +0400489#define nasm_trace(msg, ...) printf("(%s:%d): " msg "\n", __func__, __LINE__, ##__VA_ARGS__)
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400490#define nasm_dump_token(t) nasm_raw_dump_token(t, __FILE__, __LINE__, __func__);
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400491#define nasm_dump_stream(t) nasm_raw_dump_stream(t, __FILE__, __LINE__, __func__);
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400492
493/* FIXME: we really need some compound type here instead of inplace code */
494static const char *nasm_get_tok_type_str(enum pp_token_type type)
495{
496#define SWITCH_TOK_NAME(type) \
497 case (type): \
498 return stringify(type)
499
500 switch (type) {
501 SWITCH_TOK_NAME(TOK_NONE);
502 SWITCH_TOK_NAME(TOK_WHITESPACE);
503 SWITCH_TOK_NAME(TOK_COMMENT);
504 SWITCH_TOK_NAME(TOK_ID);
505 SWITCH_TOK_NAME(TOK_PREPROC_ID);
506 SWITCH_TOK_NAME(TOK_STRING);
507 SWITCH_TOK_NAME(TOK_NUMBER);
508 SWITCH_TOK_NAME(TOK_FLOAT);
509 SWITCH_TOK_NAME(TOK_SMAC_END);
510 SWITCH_TOK_NAME(TOK_OTHER);
511 SWITCH_TOK_NAME(TOK_INTERNAL_STRING);
512 SWITCH_TOK_NAME(TOK_PREPROC_Q);
513 SWITCH_TOK_NAME(TOK_PREPROC_QQ);
514 SWITCH_TOK_NAME(TOK_PASTE);
515 SWITCH_TOK_NAME(TOK_INDIRECT);
516 SWITCH_TOK_NAME(TOK_SMAC_PARAM);
517 SWITCH_TOK_NAME(TOK_MAX);
518 }
519
520 return NULL;
521}
522
523static void nasm_raw_dump_token(Token *token, const char *file, int line, const char *func)
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300524{
525 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
526 if (token) {
527 Token *t;
528 list_for_each(t, token) {
529 if (t->text)
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400530 printf("'%s'(%s) ", t->text,
531 nasm_get_tok_type_str(t->type));
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300532 }
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400533 printf("\n\n");
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300534 }
535}
536
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400537static void nasm_raw_dump_stream(Token *token, const char *file, int line, const char *func)
538{
539 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
540 if (token) {
541 Token *t;
542 list_for_each(t, token)
543 printf("%s", t->text ? t->text : " ");
544 printf("\n\n");
545 }
546}
547
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400548#else
549#define nasm_trace(msg, ...)
550#define nasm_dump_token(t)
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400551#define nasm_dump_stream(t)
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300552#endif
553
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300554/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700555 * nasm_unquote with error if the string contains NUL characters.
556 * If the string contains NUL characters, issue an error and return
557 * the C len, i.e. truncate at the NUL.
558 */
559static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
560{
561 size_t len = nasm_unquote(qstr, NULL);
562 size_t clen = strlen(qstr);
563
564 if (len != clen)
565 error(ERR_NONFATAL, "NUL character in `%s' directive",
566 pp_directives[directive]);
567
568 return clen;
569}
570
571/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700572 * In-place reverse a list of tokens.
573 */
574static Token *reverse_tokens(Token *t)
575{
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400576 Token *prev, *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700577
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400578 list_reverse(t, prev, next);
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700579
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400580 return t;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700581}
582
583/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300584 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000585 * front of them. We do it here because I could not find any other
586 * place to do it for the moment, and it is a hack (ideally it would
587 * be nice to be able to use the NASM pre-processor to do it).
588 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000589static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000590{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000591 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400592 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000593
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400594 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000595
596 /* Binary search for the directive name */
597 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400598 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400599 q = nasm_skip_word(p);
600 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000601 if (len) {
602 oldchar = p[len];
603 p[len] = 0;
604 while (j - i > 1) {
605 k = (j + i) / 2;
606 m = nasm_stricmp(p, tasm_directives[k]);
607 if (m == 0) {
608 /* We have found a directive, so jam a % in front of it
609 * so that NASM will then recognise it as one if it's own.
610 */
611 p[len] = oldchar;
612 len = strlen(p);
613 oldline = line;
614 line = nasm_malloc(len + 2);
615 line[0] = '%';
616 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700617 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300618 * NASM does not recognise IFDIFI, so we convert
619 * it to %if 0. This is not used in NASM
620 * compatible code, but does need to parse for the
621 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000622 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700623 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000624 } else {
625 memcpy(line + 1, p, len + 1);
626 }
627 nasm_free(oldline);
628 return line;
629 } else if (m < 0) {
630 j = k;
631 } else
632 i = k;
633 }
634 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000635 }
636 return line;
637}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000638
H. Peter Anvin76690a12002-04-30 20:52:49 +0000639/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000640 * The pre-preprocessing stage... This function translates line
641 * number indications as they emerge from GNU cpp (`# lineno "file"
642 * flags') into NASM preprocessor line number indications (`%line
643 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000644 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000645static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000646{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000647 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000648 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000649
H. Peter Anvine2c80182005-01-15 22:15:51 +0000650 if (line[0] == '#' && line[1] == ' ') {
651 oldline = line;
652 fname = oldline + 2;
653 lineno = atoi(fname);
654 fname += strspn(fname, "0123456789 ");
655 if (*fname == '"')
656 fname++;
657 fnlen = strcspn(fname, "\"");
658 line = nasm_malloc(20 + fnlen);
659 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
660 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000661 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000662 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000663 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000664 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000665}
666
667/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000668 * Free a linked list of tokens.
669 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000670static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000671{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400672 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000673 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000674}
675
676/*
677 * Free a linked list of lines.
678 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000679static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000680{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400681 Line *l, *tmp;
682 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000683 free_tlist(l->first);
684 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000685 }
686}
687
688/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500689 * Free an ExpDef
H. Peter Anvineba20a72002-04-30 20:53:55 +0000690 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500691static void free_expdef(ExpDef * ed)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000692{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500693 nasm_free(ed->name);
694 free_tlist(ed->dlist);
695 nasm_free(ed->defaults);
696 free_llist(ed->line);
697 nasm_free(ed);
698}
699
700/*
701 * Free an ExpInv
702 */
703static void free_expinv(ExpInv * ei)
704{
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +0400705 nasm_free(ei->name);
706 nasm_free(ei->label_text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300707 nasm_free(ei);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000708}
709
710/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700711 * Free all currently defined macros, and free the hash tables
712 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700713static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700714{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400715 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700716 const char *key;
717 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700718
H. Peter Anvin072771e2008-05-22 13:17:51 -0700719 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300720 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400721 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300722 nasm_free(s->name);
723 free_tlist(s->expansion);
724 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300725 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700726 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700727 hash_free(smt);
728}
729
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500730static void free_expdef_table(struct hash_table *edt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700731{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500732 ExpDef *ed, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700733 const char *key;
734 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700735
736 it = NULL;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500737 while ((ed = hash_iterate(edt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300738 nasm_free((void *)key);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500739 list_for_each_safe(ed ,tmp, ed)
740 free_expdef(ed);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700741 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500742 hash_free(edt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700743}
744
745static void free_macros(void)
746{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700747 free_smacro_table(&smacros);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500748 free_expdef_table(&expdefs);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700749}
750
751/*
752 * Initialize the hash tables
753 */
754static void init_macros(void)
755{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700756 hash_init(&smacros, HASH_LARGE);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500757 hash_init(&expdefs, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700758}
759
760/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000761 * Pop the context stack.
762 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000763static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000764{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000765 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000766
767 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700768 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000769 nasm_free(c->name);
770 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000771}
772
H. Peter Anvin072771e2008-05-22 13:17:51 -0700773/*
774 * Search for a key in the hash index; adding it if necessary
775 * (in which case we initialize the data pointer to NULL.)
776 */
777static void **
778hash_findi_add(struct hash_table *hash, const char *str)
779{
780 struct hash_insert hi;
781 void **r;
782 char *strx;
783
784 r = hash_findi(hash, str, &hi);
785 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300786 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700787
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300788 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700789 return hash_add(&hi, strx, NULL);
790}
791
792/*
793 * Like hash_findi, but returns the data element rather than a pointer
794 * to it. Used only when not adding a new element, hence no third
795 * argument.
796 */
797static void *
798hash_findix(struct hash_table *hash, const char *str)
799{
800 void **p;
801
802 p = hash_findi(hash, str, NULL);
803 return p ? *p : NULL;
804}
805
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400806/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500807 * read line from standard macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400808 * if there no more left -- return NULL
809 */
810static char *line_from_stdmac(void)
811{
812 unsigned char c;
813 const unsigned char *p = stdmacpos;
814 char *line, *q;
815 size_t len = 0;
816
817 if (!stdmacpos)
818 return NULL;
819
820 while ((c = *p++)) {
821 if (c >= 0x80)
822 len += pp_directives_len[c - 0x80] + 1;
823 else
824 len++;
825 }
826
827 line = nasm_malloc(len + 1);
828 q = line;
829 while ((c = *stdmacpos++)) {
830 if (c >= 0x80) {
831 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
832 q += pp_directives_len[c - 0x80];
833 *q++ = ' ';
834 } else {
835 *q++ = c;
836 }
837 }
838 stdmacpos = p;
839 *q = '\0';
840
841 if (!*stdmacpos) {
842 /* This was the last of the standard macro chain... */
843 stdmacpos = NULL;
844 if (any_extrastdmac) {
845 stdmacpos = extrastdmac;
846 any_extrastdmac = false;
847 } else if (do_predef) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300848 ExpInv *ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400849 Line *pd, *l;
850 Token *head, **tail, *t;
851
852 /*
853 * Nasty hack: here we push the contents of
854 * `predef' on to the top-level expansion stack,
855 * since this is the most convenient way to
856 * implement the pre-include and pre-define
857 * features.
858 */
859 list_for_each(pd, predef) {
860 head = NULL;
861 tail = &head;
862 list_for_each(t, pd->first) {
863 *tail = new_Token(NULL, t->type, t->text, 0);
864 tail = &(*tail)->next;
865 }
866
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500867 l = new_Line();
868 l->first = head;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300869 ei = new_ExpInv(EXP_PREDEF, NULL);
870 ei->current = l;
871 ei->emitting = true;
872 ei->prev = istk->expansion;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500873 istk->expansion = ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400874 }
875 do_predef = false;
876 }
877 }
878
879 return line;
880}
881
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000882#define BUF_DELTA 512
883/*
884 * Read a line from the top file in istk, handling multiple CR/LFs
885 * at the end of the line read, and handling spurious ^Zs. Will
886 * return lines from the standard macro set if this has not already
887 * been done.
888 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000889static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000890{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000891 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000892 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000893
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400894 /*
895 * standart macros set (predefined) goes first
896 */
897 p = line_from_stdmac();
898 if (p)
899 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700900
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400901 /*
902 * regular read from a file
903 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000904 bufsize = BUF_DELTA;
905 buffer = nasm_malloc(BUF_DELTA);
906 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000907 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000908 while (1) {
909 q = fgets(p, bufsize - (p - buffer), istk->fp);
910 if (!q)
911 break;
912 p += strlen(p);
913 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300914 /*
915 * Convert backslash-CRLF line continuation sequences into
916 * nothing at all (for DOS and Windows)
917 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000918 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
919 p -= 3;
920 *p = 0;
921 continued_count++;
922 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300923 /*
924 * Also convert backslash-LF line continuation sequences into
925 * nothing at all (for Unix)
926 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000927 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
928 p -= 2;
929 *p = 0;
930 continued_count++;
931 } else {
932 break;
933 }
934 }
935 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000936 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000937 bufsize += BUF_DELTA;
938 buffer = nasm_realloc(buffer, bufsize);
939 p = buffer + offset; /* prevent stale-pointer problems */
940 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000941 }
942
H. Peter Anvine2c80182005-01-15 22:15:51 +0000943 if (!q && p == buffer) {
944 nasm_free(buffer);
945 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000946 }
947
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300948 src_set_linnum(src_get_linnum() + istk->lineinc +
949 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000950
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000951 /*
952 * Play safe: remove CRs as well as LFs, if any of either are
953 * present at the end of the line.
954 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000955 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000956 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000957
958 /*
959 * Handle spurious ^Z, which may be inserted into source files
960 * by some file transfer utilities.
961 */
962 buffer[strcspn(buffer, "\032")] = '\0';
963
H. Peter Anvin734b1882002-04-30 21:01:08 +0000964 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000965
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000966 return buffer;
967}
968
969/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000970 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000971 * don't need to parse the value out of e.g. numeric tokens: we
972 * simply split one string into many.
973 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000974static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000975{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700976 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000977 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000978 Token *list = NULL;
979 Token *t, **tail = &list;
Keith Kanios6faad4e2010-12-18 14:08:02 -0600980 bool verbose = true;
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +0400981
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400982 nasm_trace("Tokenize for '%s'", line);
983
Keith Kanios6faad4e2010-12-18 14:08:02 -0600984 if ((defining != NULL) && (defining->ignoring == true)) {
985 verbose = false;
986 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000987
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 while (*line) {
989 p = line;
990 if (*p == '%') {
991 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300992 if (*p == '+' && !nasm_isdigit(p[1])) {
993 p++;
994 type = TOK_PASTE;
995 } else if (nasm_isdigit(*p) ||
996 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 do {
998 p++;
999 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001000 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 type = TOK_PREPROC_ID;
1002 } else if (*p == '{') {
1003 p++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001004 while (*p && *p != '}') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 p[-1] = *p;
1006 p++;
1007 }
1008 p[-1] = '\0';
1009 if (*p)
1010 p++;
1011 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001012 } else if (*p == '[') {
1013 int lvl = 1;
1014 line += 2; /* Skip the leading %[ */
1015 p++;
1016 while (lvl && (c = *p++)) {
1017 switch (c) {
1018 case ']':
1019 lvl--;
1020 break;
1021 case '%':
1022 if (*p == '[')
1023 lvl++;
1024 break;
1025 case '\'':
1026 case '\"':
1027 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +04001028 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001029 break;
1030 default:
1031 break;
1032 }
1033 }
1034 p--;
1035 if (*p)
1036 *p++ = '\0';
Keith Kanios6faad4e2010-12-18 14:08:02 -06001037 if (lvl && verbose)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001038 error(ERR_NONFATAL, "unterminated %[ construct");
1039 type = TOK_INDIRECT;
1040 } else if (*p == '?') {
1041 type = TOK_PREPROC_Q; /* %? */
1042 p++;
1043 if (*p == '?') {
1044 type = TOK_PREPROC_QQ; /* %?? */
1045 p++;
1046 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001047 } else if (*p == '!') {
1048 type = TOK_PREPROC_ID;
1049 p++;
1050 if (isidchar(*p)) {
1051 do {
1052 p++;
1053 } while (isidchar(*p));
1054 } else if (*p == '\'' || *p == '\"' || *p == '`') {
1055 p = nasm_skip_string(p);
1056 if (*p)
1057 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001058 else if(verbose)
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001059 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
1060 } else {
1061 /* %! without string or identifier */
1062 type = TOK_OTHER; /* Legacy behavior... */
1063 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 } else if (isidchar(*p) ||
1065 ((*p == '!' || *p == '%' || *p == '$') &&
1066 isidchar(p[1]))) {
1067 do {
1068 p++;
1069 }
1070 while (isidchar(*p));
1071 type = TOK_PREPROC_ID;
1072 } else {
1073 type = TOK_OTHER;
1074 if (*p == '%')
1075 p++;
1076 }
1077 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1078 type = TOK_ID;
1079 p++;
1080 while (*p && isidchar(*p))
1081 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001082 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001083 /*
1084 * A string token.
1085 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001087 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001088
H. Peter Anvine2c80182005-01-15 22:15:51 +00001089 if (*p) {
1090 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001091 } else if(verbose) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001092 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093 /* Handling unterminated strings by UNV */
1094 /* type = -1; */
1095 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001096 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001097 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001098 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001100 bool is_hex = false;
1101 bool is_float = false;
1102 bool has_e = false;
1103 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001104
H. Peter Anvine2c80182005-01-15 22:15:51 +00001105 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001106 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001107 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001108
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001109 if (*p == '$') {
1110 p++;
1111 is_hex = true;
1112 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001113
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001114 for (;;) {
1115 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001116
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001117 if (!is_hex && (c == 'e' || c == 'E')) {
1118 has_e = true;
1119 if (*p == '+' || *p == '-') {
1120 /*
1121 * e can only be followed by +/- if it is either a
1122 * prefixed hex number or a floating-point number
1123 */
1124 p++;
1125 is_float = true;
1126 }
1127 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1128 is_hex = true;
1129 } else if (c == 'P' || c == 'p') {
1130 is_float = true;
1131 if (*p == '+' || *p == '-')
1132 p++;
1133 } else if (isnumchar(c) || c == '_')
1134 ; /* just advance */
1135 else if (c == '.') {
1136 /*
1137 * we need to deal with consequences of the legacy
1138 * parser, like "1.nolist" being two tokens
1139 * (TOK_NUMBER, TOK_ID) here; at least give it
1140 * a shot for now. In the future, we probably need
1141 * a flex-based scanner with proper pattern matching
1142 * to do it as well as it can be done. Nothing in
1143 * the world is going to help the person who wants
1144 * 0x123.p16 interpreted as two tokens, though.
1145 */
1146 r = p;
1147 while (*r == '_')
1148 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001149
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001150 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1151 (!is_hex && (*r == 'e' || *r == 'E')) ||
1152 (*r == 'p' || *r == 'P')) {
1153 p = r;
1154 is_float = true;
1155 } else
1156 break; /* Terminate the token */
1157 } else
1158 break;
1159 }
1160 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001161
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001162 if (p == line+1 && *line == '$') {
1163 type = TOK_OTHER; /* TOKEN_HERE */
1164 } else {
1165 if (has_e && !is_hex) {
1166 /* 1e13 is floating-point, but 1e13h is not */
1167 is_float = true;
1168 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001169
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001170 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1171 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001172 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001173 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001174 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001175 /*
1176 * Whitespace just before end-of-line is discarded by
1177 * pretending it's a comment; whitespace just before a
1178 * comment gets lumped into the comment.
1179 */
1180 if (!*p || *p == ';') {
1181 type = TOK_COMMENT;
1182 while (*p)
1183 p++;
1184 }
1185 } else if (*p == ';') {
1186 type = TOK_COMMENT;
1187 while (*p)
1188 p++;
1189 } else {
1190 /*
1191 * Anything else is an operator of some kind. We check
1192 * for all the double-character operators (>>, <<, //,
1193 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001194 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001195 */
1196 type = TOK_OTHER;
1197 if ((p[0] == '>' && p[1] == '>') ||
1198 (p[0] == '<' && p[1] == '<') ||
1199 (p[0] == '/' && p[1] == '/') ||
1200 (p[0] == '<' && p[1] == '=') ||
1201 (p[0] == '>' && p[1] == '=') ||
1202 (p[0] == '=' && p[1] == '=') ||
1203 (p[0] == '!' && p[1] == '=') ||
1204 (p[0] == '<' && p[1] == '>') ||
1205 (p[0] == '&' && p[1] == '&') ||
1206 (p[0] == '|' && p[1] == '|') ||
1207 (p[0] == '^' && p[1] == '^')) {
1208 p++;
1209 }
1210 p++;
1211 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001212
H. Peter Anvine2c80182005-01-15 22:15:51 +00001213 /* Handling unterminated string by UNV */
1214 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001215 {
1216 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1217 t->text[p-line] = *line;
1218 tail = &t->next;
1219 }
1220 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001221 if (type != TOK_COMMENT) {
1222 *tail = t = new_Token(NULL, type, line, p - line);
1223 tail = &t->next;
1224 }
1225 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001226 }
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +04001227
1228 nasm_dump_token(list);
1229
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001230 return list;
1231}
1232
H. Peter Anvince616072002-04-30 21:02:23 +00001233/*
1234 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001235 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001236 * deleted only all at once by the delete_Blocks function.
1237 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001238static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001239{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001240 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001241
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001242 /* first, get to the end of the linked list */
1243 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001244 b = b->next;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001245
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001246 /* now allocate the requested chunk */
1247 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001248
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001249 /* now allocate a new block for the next request */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001250 b->next = nasm_zalloc(sizeof(Blocks));
1251
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001252 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001253}
1254
1255/*
1256 * this function deletes all managed blocks of memory
1257 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001258static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001259{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001260 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001261
H. Peter Anvin70653092007-10-19 14:42:29 -07001262 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001263 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001264 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001265 * free it.
1266 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001267 while (b) {
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04001268 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001269 a = b;
1270 b = b->next;
1271 if (a != &blocks)
1272 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001273 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001274}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001275
1276/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001277 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001278 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001279 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001280 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001281static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001282 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001283{
1284 Token *t;
1285 int i;
1286
H. Peter Anvin89cee572009-07-15 09:16:54 -04001287 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001288 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1289 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1290 freeTokens[i].next = &freeTokens[i + 1];
1291 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001292 }
1293 t = freeTokens;
1294 freeTokens = t->next;
1295 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001296 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001297 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001298 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299 t->text = NULL;
1300 } else {
1301 if (txtlen == 0)
1302 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001303 t->text = nasm_malloc(txtlen+1);
1304 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001305 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001306 }
1307 return t;
1308}
1309
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001310static Token *copy_Token(Token * tline)
1311{
1312 Token *t, *tt, *first = NULL, *prev = NULL;
1313 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001314 for (tt = tline; tt != NULL; tt = tt->next) {
1315 if (!freeTokens) {
1316 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1317 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1318 freeTokens[i].next = &freeTokens[i + 1];
1319 freeTokens[i].next = NULL;
1320 }
1321 t = freeTokens;
1322 freeTokens = t->next;
1323 t->next = NULL;
1324 t->text = tt->text ? nasm_strdup(tt->text) : NULL;
1325 t->a.mac = tt->a.mac;
1326 t->a.len = tt->a.len;
1327 t->type = tt->type;
1328 if (prev != NULL) {
1329 prev->next = t;
1330 } else {
1331 first = t;
1332 }
1333 prev = t;
1334 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001335 return first;
1336}
1337
H. Peter Anvine2c80182005-01-15 22:15:51 +00001338static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001339{
1340 Token *next = t->next;
1341 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001342 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001343 freeTokens = t;
1344 return next;
1345}
1346
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001347/*
1348 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001349 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1350 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001351 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001352static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001353{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001354 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001355 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001356 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001357 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001358
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001359 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001360 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001361 char *v;
1362 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001363
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001364 v = t->text + 2;
1365 if (*v == '\'' || *v == '\"' || *v == '`') {
1366 size_t len = nasm_unquote(v, NULL);
1367 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001368
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001369 if (len != clen) {
1370 error(ERR_NONFATAL | ERR_PASS1,
1371 "NUL character in %! string");
1372 v = NULL;
1373 }
1374 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001375
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001376 if (v) {
1377 char *p = getenv(v);
1378 if (!p) {
1379 error(ERR_NONFATAL | ERR_PASS1,
1380 "nonexistent environment variable `%s'", v);
1381 p = "";
1382 }
1383 t->text = nasm_strdup(p);
1384 }
1385 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001386 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001387
H. Peter Anvine2c80182005-01-15 22:15:51 +00001388 /* Expand local macros here and not during preprocessing */
1389 if (expand_locals &&
1390 t->type == TOK_PREPROC_ID && t->text &&
1391 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001392 const char *q;
1393 char *p;
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001394 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001395 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001396 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001397 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001398 p = nasm_strcat(buffer, q);
1399 nasm_free(t->text);
1400 t->text = p;
1401 }
1402 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001403
1404 /* Expand %? and %?? directives */
Keith Kanios3136d482010-11-13 09:34:34 -06001405 if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001406 ((t->type == TOK_PREPROC_Q) ||
1407 (t->type == TOK_PREPROC_QQ))) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001408 ExpInv *ei;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001409 for (ei = istk->expansion; ei != NULL; ei = ei->prev){
1410 if (ei->type == EXP_MMACRO) {
1411 nasm_free(t->text);
1412 if (t->type == TOK_PREPROC_Q) {
1413 t->text = nasm_strdup(ei->name);
1414 } else {
1415 t->text = nasm_strdup(ei->def->name);
1416 }
1417 break;
1418 }
1419 }
1420 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001421
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001422 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001423 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001424 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001425 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001426 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001427
H. Peter Anvin734b1882002-04-30 21:01:08 +00001428 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001429
1430 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001431 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001432 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001434 q = t->text;
1435 while (*q)
1436 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001438 }
1439 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001440
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001441 return line;
1442}
1443
1444/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001445 * Initialize a new Line
1446 */
Cyrill Gorcunov29cb0bb2010-11-11 11:19:43 +03001447static inline Line *new_Line(void)
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001448{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001449 return (Line *)nasm_zalloc(sizeof(Line));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001450}
1451
1452
1453/*
1454 * Initialize a new Expansion Definition
1455 */
1456static ExpDef *new_ExpDef(int exp_type)
1457{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001458 ExpDef *ed = (ExpDef*)nasm_zalloc(sizeof(ExpDef));
1459 ed->type = exp_type;
1460 ed->casesense = true;
1461 ed->state = COND_NEVER;
1462
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001463 return ed;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001464}
1465
1466
1467/*
1468 * Initialize a new Expansion Instance
1469 */
1470static ExpInv *new_ExpInv(int exp_type, ExpDef *ed)
1471{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001472 ExpInv *ei = (ExpInv*)nasm_zalloc(sizeof(ExpInv));
1473 ei->type = exp_type;
1474 ei->def = ed;
1475 ei->unique = ++unique;
1476
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001477 if ((istk->mmac_depth < 1) &&
1478 (istk->expansion == NULL) &&
1479 (ed != NULL) &&
1480 (ed->type != EXP_MMACRO) &&
1481 (ed->type != EXP_REP) &&
1482 (ed->type != EXP_WHILE)) {
1483 ei->linnum = src_get_linnum();
1484 src_set_linnum(ei->linnum - ed->linecount - 1);
1485 } else {
1486 ei->linnum = -1;
1487 }
1488 if ((istk->expansion == NULL) ||
1489 (ei->type == EXP_MMACRO)) {
1490 ei->relno = 0;
1491 } else {
1492 ei->relno = istk->expansion->lineno;
1493 if (ed != NULL) {
1494 ei->relno -= (ed->linecount + 1);
1495 }
1496 }
1497 return ei;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001498}
1499
1500/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001501 * A scanner, suitable for use by the expression evaluator, which
1502 * operates on a line of Tokens. Expects a pointer to a pointer to
1503 * the first token in the line to be passed in as its private_data
1504 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001505 *
1506 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001507 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001508static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001509{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001510 Token **tlineptr = private_data;
1511 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001512 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001513
H. Peter Anvine2c80182005-01-15 22:15:51 +00001514 do {
1515 tline = *tlineptr;
1516 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001517 } while (tline && (tline->type == TOK_WHITESPACE ||
1518 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001519
1520 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001522
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001523 tokval->t_charptr = tline->text;
1524
H. Peter Anvin76690a12002-04-30 20:52:49 +00001525 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001526 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001527 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001528 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001529
H. Peter Anvine2c80182005-01-15 22:15:51 +00001530 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001531 p = tokval->t_charptr = tline->text;
1532 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001533 tokval->t_charptr++;
1534 return tokval->t_type = TOKEN_ID;
1535 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001536
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001537 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001538 if (r >= p+MAX_KEYWORD)
1539 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001540 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001541 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001542 *s = '\0';
1543 /* right, so we have an identifier sitting in temp storage. now,
1544 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001545 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001546 }
1547
H. Peter Anvine2c80182005-01-15 22:15:51 +00001548 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001549 bool rn_error;
1550 tokval->t_integer = readnum(tline->text, &rn_error);
1551 tokval->t_charptr = tline->text;
1552 if (rn_error)
1553 return tokval->t_type = TOKEN_ERRNUM;
1554 else
1555 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001556 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001557
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001558 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001559 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001560 }
1561
H. Peter Anvine2c80182005-01-15 22:15:51 +00001562 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001563 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001564
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001565 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001566 tokval->t_charptr = tline->text;
1567 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001568
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001569 if (ep[0] != bq || ep[1] != '\0')
1570 return tokval->t_type = TOKEN_ERRSTR;
1571 else
1572 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001573 }
1574
H. Peter Anvine2c80182005-01-15 22:15:51 +00001575 if (tline->type == TOK_OTHER) {
1576 if (!strcmp(tline->text, "<<"))
1577 return tokval->t_type = TOKEN_SHL;
1578 if (!strcmp(tline->text, ">>"))
1579 return tokval->t_type = TOKEN_SHR;
1580 if (!strcmp(tline->text, "//"))
1581 return tokval->t_type = TOKEN_SDIV;
1582 if (!strcmp(tline->text, "%%"))
1583 return tokval->t_type = TOKEN_SMOD;
1584 if (!strcmp(tline->text, "=="))
1585 return tokval->t_type = TOKEN_EQ;
1586 if (!strcmp(tline->text, "<>"))
1587 return tokval->t_type = TOKEN_NE;
1588 if (!strcmp(tline->text, "!="))
1589 return tokval->t_type = TOKEN_NE;
1590 if (!strcmp(tline->text, "<="))
1591 return tokval->t_type = TOKEN_LE;
1592 if (!strcmp(tline->text, ">="))
1593 return tokval->t_type = TOKEN_GE;
1594 if (!strcmp(tline->text, "&&"))
1595 return tokval->t_type = TOKEN_DBL_AND;
1596 if (!strcmp(tline->text, "^^"))
1597 return tokval->t_type = TOKEN_DBL_XOR;
1598 if (!strcmp(tline->text, "||"))
1599 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001600 }
1601
1602 /*
1603 * We have no other options: just return the first character of
1604 * the token text.
1605 */
1606 return tokval->t_type = tline->text[0];
1607}
1608
1609/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001610 * Compare a string to the name of an existing macro; this is a
1611 * simple wrapper which calls either strcmp or nasm_stricmp
1612 * depending on the value of the `casesense' parameter.
1613 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001614static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001615{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001616 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001617}
1618
1619/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001620 * Compare a string to the name of an existing macro; this is a
1621 * simple wrapper which calls either strcmp or nasm_stricmp
1622 * depending on the value of the `casesense' parameter.
1623 */
1624static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1625{
1626 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1627}
1628
1629/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001630 * Return the Context structure associated with a %$ token. Return
1631 * NULL, having _already_ reported an error condition, if the
1632 * context stack isn't deep enough for the supplied number of $
1633 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001634 *
1635 * If "namep" is non-NULL, set it to the pointer to the macro name
1636 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001637 */
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001638static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001639{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001640 Context *ctx;
1641 int i;
1642
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001643 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001644 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001645
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001646 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001647 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001648
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 if (!cstk) {
1650 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1651 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001652 }
1653
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001654 name += 2;
1655 ctx = cstk;
1656 i = 0;
1657 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001658 name++;
1659 i++;
1660 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001661 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001662
H. Peter Anvine2c80182005-01-15 22:15:51 +00001663 if (!ctx) {
1664 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001665 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001666 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001667 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001668
1669 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001670 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001671
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001672 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001673}
1674
1675/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001676 * Check to see if a file is already in a string list
1677 */
1678static bool in_list(const StrList *list, const char *str)
1679{
1680 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001681 if (!strcmp(list->str, str))
1682 return true;
1683 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001684 }
1685 return false;
1686}
1687
1688/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001689 * Open an include file. This routine must always return a valid
1690 * file pointer if it returns - it's responsible for throwing an
1691 * ERR_FATAL and bombing out completely if not. It should also try
1692 * the include path one by one until it finds the file or reaches
1693 * the end of the path.
1694 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001695static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001696 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001697{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001698 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001699 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001700 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001701 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001702 size_t prefix_len = 0;
1703 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001704
H. Peter Anvine2c80182005-01-15 22:15:51 +00001705 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001706 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunov6f38fe62010-11-11 11:32:16 +03001707 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001708 memcpy(sl->str, prefix, prefix_len);
1709 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001710 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001711 if (fp && dhead && !in_list(*dhead, sl->str)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001712 **dtail = sl;
1713 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001714 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001715 nasm_free(sl);
1716 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001717 if (fp)
1718 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001719 if (!ip) {
1720 if (!missing_ok)
1721 break;
1722 prefix = NULL;
1723 } else {
1724 prefix = ip->path;
1725 ip = ip->next;
1726 }
1727 if (prefix) {
1728 prefix_len = strlen(prefix);
1729 } else {
1730 /* -MG given and file not found */
1731 if (dhead && !in_list(*dhead, file)) {
1732 sl = nasm_malloc(len+1+sizeof sl->next);
1733 sl->next = NULL;
1734 strcpy(sl->str, file);
1735 **dtail = sl;
1736 *dtail = &sl->next;
1737 }
1738 return NULL;
1739 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001740 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001741
H. Peter Anvin734b1882002-04-30 21:01:08 +00001742 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001743 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001744}
1745
1746/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001747 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001748 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001749 * return true if _any_ single-line macro of that name is defined.
1750 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001751 * `nparam' or no parameters is defined.
1752 *
1753 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001754 * defined, or nparam is -1, the address of the definition structure
1755 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001756 * is NULL, no action will be taken regarding its contents, and no
1757 * error will occur.
1758 *
1759 * Note that this is also called with nparam zero to resolve
1760 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001761 *
1762 * If you already know which context macro belongs to, you can pass
1763 * the context pointer as first parameter; if you won't but name begins
1764 * with %$ the context will be automatically computed. If all_contexts
1765 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001766 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001767static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001768smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001769 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001770{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001771 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001772 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001773
H. Peter Anvin97a23472007-09-16 17:57:25 -07001774 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001775 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001776 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001777 if (cstk)
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001778 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001779 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001780 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001781 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001782 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001783 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001784 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001785 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001786
H. Peter Anvine2c80182005-01-15 22:15:51 +00001787 while (m) {
1788 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001789 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001790 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001791 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001792 *defn = m;
1793 else
1794 *defn = NULL;
1795 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001796 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001797 }
1798 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001799 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001800
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001801 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001802}
1803
1804/*
1805 * Count and mark off the parameters in a multi-line macro call.
1806 * This is called both from within the multi-line macro expansion
1807 * code, and also to mark off the default parameters when provided
1808 * in a %macro definition line.
1809 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001810static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001811{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001812 int paramsize, brace;
1813
1814 *nparam = paramsize = 0;
1815 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001816 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001817 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001818 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001819 paramsize += PARAM_DELTA;
1820 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1821 }
1822 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001823 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001824 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001825 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001826 (*params)[(*nparam)++] = t;
1827 while (tok_isnt_(t, brace ? "}" : ","))
1828 t = t->next;
1829 if (t) { /* got a comma/brace */
1830 t = t->next;
1831 if (brace) {
1832 /*
1833 * Now we've found the closing brace, look further
1834 * for the comma.
1835 */
1836 skip_white_(t);
1837 if (tok_isnt_(t, ",")) {
1838 error(ERR_NONFATAL,
1839 "braces do not enclose all of macro parameter");
1840 while (tok_isnt_(t, ","))
1841 t = t->next;
1842 }
1843 if (t)
1844 t = t->next; /* eat the comma */
1845 }
1846 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001847 }
1848}
1849
1850/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001851 * Determine whether one of the various `if' conditions is true or
1852 * not.
1853 *
1854 * We must free the tline we get passed.
1855 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001856static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001857{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001858 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001859 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001860 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001861 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001862 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001863 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001864 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001865
1866 origline = tline;
1867
H. Peter Anvine2c80182005-01-15 22:15:51 +00001868 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001869 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001870 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001871 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001872 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001873 if (!tline)
1874 break;
1875 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001876 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001877 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001878 free_tlist(origline);
1879 return -1;
1880 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001881 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001882 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001883 tline = tline->next;
1884 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001885 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001886
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001887 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001888 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001889 while (tline) {
1890 skip_white_(tline);
1891 if (!tline || (tline->type != TOK_ID &&
1892 (tline->type != TOK_PREPROC_ID ||
1893 tline->text[1] != '$'))) {
1894 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001895 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001896 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001897 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001898 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001899 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001900 tline = tline->next;
1901 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001902 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001903
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001904 case PPC_IFENV:
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001905 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001906 j = false; /* have we matched yet? */
1907 while (tline) {
1908 skip_white_(tline);
1909 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001910 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001911 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001912 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001913 error(ERR_NONFATAL,
1914 "`%s' expects environment variable names",
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001915 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001916 goto fail;
1917 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001918 p = tline->text;
1919 if (tline->type == TOK_PREPROC_ID)
1920 p += 2; /* Skip leading %! */
1921 if (*p == '\'' || *p == '\"' || *p == '`')
1922 nasm_unquote_cstr(p, ct);
1923 if (getenv(p))
1924 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001925 tline = tline->next;
1926 }
1927 break;
1928
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001929 case PPC_IFIDN:
1930 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001931 tline = expand_smacro(tline);
1932 t = tt = tline;
1933 while (tok_isnt_(tt, ","))
1934 tt = tt->next;
1935 if (!tt) {
1936 error(ERR_NONFATAL,
1937 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001938 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001939 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001940 }
1941 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001942 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001943 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1944 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1945 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001946 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001947 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001948 }
1949 if (t->type == TOK_WHITESPACE) {
1950 t = t->next;
1951 continue;
1952 }
1953 if (tt->type == TOK_WHITESPACE) {
1954 tt = tt->next;
1955 continue;
1956 }
1957 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001958 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001959 break;
1960 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001961 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001962 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001963 size_t l1 = nasm_unquote(t->text, NULL);
1964 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001965
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001966 if (l1 != l2) {
1967 j = false;
1968 break;
1969 }
1970 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1971 j = false;
1972 break;
1973 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001974 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001975 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001976 break;
1977 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001978
H. Peter Anvine2c80182005-01-15 22:15:51 +00001979 t = t->next;
1980 tt = tt->next;
1981 }
1982 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001983 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001984 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001985
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001986 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001987 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001988 bool found = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001989 ExpDef searching, *ed;
H. Peter Anvin65747262002-05-07 00:10:05 +00001990
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001991 skip_white_(tline);
1992 tline = expand_id(tline);
1993 if (!tok_type_(tline, TOK_ID)) {
1994 error(ERR_NONFATAL,
1995 "`%s' expects a macro name", pp_directives[ct]);
1996 goto fail;
1997 }
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03001998 memset(&searching, 0, sizeof(searching));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001999 searching.name = nasm_strdup(tline->text);
2000 searching.casesense = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002001 searching.nparam_max = INT_MAX;
2002 tline = expand_smacro(tline->next);
2003 skip_white_(tline);
2004 if (!tline) {
2005 } else if (!tok_type_(tline, TOK_NUMBER)) {
2006 error(ERR_NONFATAL,
2007 "`%s' expects a parameter count or nothing",
2008 pp_directives[ct]);
2009 } else {
2010 searching.nparam_min = searching.nparam_max =
2011 readnum(tline->text, &j);
2012 if (j)
2013 error(ERR_NONFATAL,
2014 "unable to parse parameter count `%s'",
2015 tline->text);
2016 }
2017 if (tline && tok_is_(tline->next, "-")) {
2018 tline = tline->next->next;
2019 if (tok_is_(tline, "*"))
2020 searching.nparam_max = INT_MAX;
2021 else if (!tok_type_(tline, TOK_NUMBER))
2022 error(ERR_NONFATAL,
2023 "`%s' expects a parameter count after `-'",
2024 pp_directives[ct]);
2025 else {
2026 searching.nparam_max = readnum(tline->text, &j);
2027 if (j)
2028 error(ERR_NONFATAL,
2029 "unable to parse parameter count `%s'",
2030 tline->text);
2031 if (searching.nparam_min > searching.nparam_max)
2032 error(ERR_NONFATAL,
2033 "minimum parameter count exceeds maximum");
2034 }
2035 }
2036 if (tline && tok_is_(tline->next, "+")) {
2037 tline = tline->next;
2038 searching.plus = true;
2039 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002040 ed = (ExpDef *) hash_findix(&expdefs, searching.name);
2041 while (ed != NULL) {
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002042 if (!strcmp(ed->name, searching.name) &&
2043 (ed->nparam_min <= searching.nparam_max || searching.plus) &&
2044 (searching.nparam_min <= ed->nparam_max || ed->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002045 found = true;
2046 break;
2047 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002048 ed = ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002049 }
2050 if (tline && tline->next)
2051 error(ERR_WARNING|ERR_PASS1,
2052 "trailing garbage after %%ifmacro ignored");
2053 nasm_free(searching.name);
2054 j = found;
2055 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002056 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002057
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002058 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002059 needtype = TOK_ID;
2060 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002061 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002062 needtype = TOK_NUMBER;
2063 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002064 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002065 needtype = TOK_STRING;
2066 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002067
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002068iftype:
2069 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002070
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002071 while (tok_type_(t, TOK_WHITESPACE) ||
2072 (needtype == TOK_NUMBER &&
2073 tok_type_(t, TOK_OTHER) &&
2074 (t->text[0] == '-' || t->text[0] == '+') &&
2075 !t->text[1]))
2076 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002077
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002078 j = tok_type_(t, needtype);
2079 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002080
2081 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002082 t = tline = expand_smacro(tline);
2083 while (tok_type_(t, TOK_WHITESPACE))
2084 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002085
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002086 j = false;
2087 if (t) {
2088 t = t->next; /* Skip the actual token */
2089 while (tok_type_(t, TOK_WHITESPACE))
2090 t = t->next;
2091 j = !t; /* Should be nothing left */
2092 }
2093 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002094
H. Peter Anvin134b9462008-02-16 17:01:40 -08002095 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002096 t = tline = expand_smacro(tline);
2097 while (tok_type_(t, TOK_WHITESPACE))
2098 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002099
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002100 j = !t; /* Should be empty */
2101 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002102
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002103 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002104 t = tline = expand_smacro(tline);
2105 tptr = &t;
2106 tokval.t_type = TOKEN_INVALID;
2107 evalresult = evaluate(ppscan, tptr, &tokval,
2108 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002109 if (!evalresult)
2110 return -1;
2111 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002112 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002113 "trailing garbage after expression ignored");
2114 if (!is_simple(evalresult)) {
2115 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002116 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002117 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002118 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002119 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002120 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002121
H. Peter Anvine2c80182005-01-15 22:15:51 +00002122 default:
2123 error(ERR_FATAL,
2124 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002125 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002126 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002127 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002128
2129 free_tlist(origline);
2130 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002131
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002132fail:
2133 free_tlist(origline);
2134 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002135}
2136
2137/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002138 * Common code for defining an smacro
2139 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002140static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002141 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002142{
2143 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002144 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002145
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002146 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002147 if (!smac) {
2148 error(ERR_WARNING|ERR_PASS1,
2149 "single-line macro `%s' defined both with and"
2150 " without parameters", mname);
2151 /*
2152 * Some instances of the old code considered this a failure,
2153 * some others didn't. What is the right thing to do here?
2154 */
2155 free_tlist(expansion);
2156 return false; /* Failure */
2157 } else {
2158 /*
2159 * We're redefining, so we have to take over an
2160 * existing SMacro structure. This means freeing
2161 * what was already in it.
2162 */
2163 nasm_free(smac->name);
2164 free_tlist(smac->expansion);
2165 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002166 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002167 smtbl = ctx ? &ctx->localmac : &smacros;
2168 smhead = (SMacro **) hash_findi_add(smtbl, mname);
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002169 smac = nasm_zalloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002170 smac->next = *smhead;
2171 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002172 }
2173 smac->name = nasm_strdup(mname);
2174 smac->casesense = casesense;
2175 smac->nparam = nparam;
2176 smac->expansion = expansion;
2177 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002178 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002179}
2180
2181/*
2182 * Undefine an smacro
2183 */
2184static void undef_smacro(Context *ctx, const char *mname)
2185{
2186 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002187 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002188
H. Peter Anvin166c2472008-05-28 12:28:58 -07002189 smtbl = ctx ? &ctx->localmac : &smacros;
2190 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002191
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002192 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002193 /*
2194 * We now have a macro name... go hunt for it.
2195 */
2196 sp = smhead;
2197 while ((s = *sp) != NULL) {
2198 if (!mstrcmp(s->name, mname, s->casesense)) {
2199 *sp = s->next;
2200 nasm_free(s->name);
2201 free_tlist(s->expansion);
2202 nasm_free(s);
2203 } else {
2204 sp = &s->next;
2205 }
2206 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002207 }
2208}
2209
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002210/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002211 * Parse a mmacro specification.
2212 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002213static bool parse_mmacro_spec(Token *tline, ExpDef *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002214{
2215 bool err;
2216
2217 tline = tline->next;
2218 skip_white_(tline);
2219 tline = expand_id(tline);
2220 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002221 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2222 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002223 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002224
H. Peter Anvina26433d2008-07-16 14:40:01 -07002225 def->name = nasm_strdup(tline->text);
2226 def->plus = false;
2227 def->nolist = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002228// def->in_progress = 0;
2229// def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002230 def->nparam_min = 0;
2231 def->nparam_max = 0;
2232
H. Peter Anvina26433d2008-07-16 14:40:01 -07002233 tline = expand_smacro(tline->next);
2234 skip_white_(tline);
2235 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002236 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002237 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002238 def->nparam_min = def->nparam_max =
2239 readnum(tline->text, &err);
2240 if (err)
2241 error(ERR_NONFATAL,
2242 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002243 }
2244 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002245 tline = tline->next->next;
2246 if (tok_is_(tline, "*")) {
2247 def->nparam_max = INT_MAX;
2248 } else if (!tok_type_(tline, TOK_NUMBER)) {
2249 error(ERR_NONFATAL,
2250 "`%s' expects a parameter count after `-'", directive);
2251 } else {
2252 def->nparam_max = readnum(tline->text, &err);
2253 if (err) {
2254 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2255 tline->text);
2256 }
2257 if (def->nparam_min > def->nparam_max) {
2258 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2259 }
2260 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002261 }
2262 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002263 tline = tline->next;
2264 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002265 }
2266 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002267 !nasm_stricmp(tline->next->text, ".nolist")) {
2268 tline = tline->next;
2269 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002270 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002271
H. Peter Anvina26433d2008-07-16 14:40:01 -07002272 /*
2273 * Handle default parameters.
2274 */
2275 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002276 def->dlist = tline->next;
2277 tline->next = NULL;
2278 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002279 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002280 def->dlist = NULL;
2281 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002282 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002283 def->line = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002284
H. Peter Anvin89cee572009-07-15 09:16:54 -04002285 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002286 !def->plus)
2287 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2288 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002289
H. Peter Anvina26433d2008-07-16 14:40:01 -07002290 return true;
2291}
2292
2293
2294/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002295 * Decode a size directive
2296 */
2297static int parse_size(const char *str) {
2298 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002299 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002300 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002301 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002302
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002303 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002304}
2305
Ed Beroset3ab3f412002-06-11 03:31:49 +00002306/**
2307 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002308 * Find out if a line contains a preprocessor directive, and deal
2309 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002310 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002311 * If a directive _is_ found, it is the responsibility of this routine
2312 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002313 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002314 * @param tline a pointer to the current tokeninzed line linked list
2315 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002316 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002317 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002318static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002319{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002320 enum preproc_token i;
2321 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002322 bool err;
2323 int nparam;
2324 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002325 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002326 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002327 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002328 char *p, *pp;
2329 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002330 Include *inc;
2331 Context *ctx;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002332 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002333 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002334 struct tokenval tokval;
2335 expr *evalresult;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002336 ExpDef *ed, *eed, **edhead;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002337 ExpInv *ei, *eei;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002338 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002339 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002340 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002341
2342 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002343
H. Peter Anvineba20a72002-04-30 20:53:55 +00002344 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002345 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002346 (tline->text[1] == '%' || tline->text[1] == '$'
2347 || tline->text[1] == '!'))
2348 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002349
H. Peter Anvin4169a472007-09-12 01:29:43 +00002350 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002351
H. Peter Anvin4169a472007-09-12 01:29:43 +00002352 switch (i) {
2353 case PP_INVALID:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002354 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002355 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2356 tline->text);
2357 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002358
H. Peter Anvine2c80182005-01-15 22:15:51 +00002359 case PP_STACKSIZE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002360 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002361 /* Directive to tell NASM what the default stack size is. The
2362 * default is for a 16-bit stack, and this can be overriden with
2363 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002364 */
2365 tline = tline->next;
2366 if (tline && tline->type == TOK_WHITESPACE)
2367 tline = tline->next;
2368 if (!tline || tline->type != TOK_ID) {
2369 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2370 free_tlist(origline);
2371 return DIRECTIVE_FOUND;
2372 }
2373 if (nasm_stricmp(tline->text, "flat") == 0) {
2374 /* All subsequent ARG directives are for a 32-bit stack */
2375 StackSize = 4;
2376 StackPointer = "ebp";
2377 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002378 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002379 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2380 /* All subsequent ARG directives are for a 64-bit stack */
2381 StackSize = 8;
2382 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002383 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002384 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002385 } else if (nasm_stricmp(tline->text, "large") == 0) {
2386 /* All subsequent ARG directives are for a 16-bit stack,
2387 * far function call.
2388 */
2389 StackSize = 2;
2390 StackPointer = "bp";
2391 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002392 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002393 } else if (nasm_stricmp(tline->text, "small") == 0) {
2394 /* All subsequent ARG directives are for a 16-bit stack,
2395 * far function call. We don't support near functions.
2396 */
2397 StackSize = 2;
2398 StackPointer = "bp";
2399 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002400 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002401 } else {
2402 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2403 free_tlist(origline);
2404 return DIRECTIVE_FOUND;
2405 }
2406 free_tlist(origline);
2407 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002408
H. Peter Anvine2c80182005-01-15 22:15:51 +00002409 case PP_ARG:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002410 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002411 /* TASM like ARG directive to define arguments to functions, in
2412 * the following form:
2413 *
2414 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2415 */
2416 offset = ArgOffset;
2417 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002418 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002419 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002420
H. Peter Anvine2c80182005-01-15 22:15:51 +00002421 /* Find the argument name */
2422 tline = tline->next;
2423 if (tline && tline->type == TOK_WHITESPACE)
2424 tline = tline->next;
2425 if (!tline || tline->type != TOK_ID) {
2426 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2427 free_tlist(origline);
2428 return DIRECTIVE_FOUND;
2429 }
2430 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002431
H. Peter Anvine2c80182005-01-15 22:15:51 +00002432 /* Find the argument size type */
2433 tline = tline->next;
2434 if (!tline || tline->type != TOK_OTHER
2435 || tline->text[0] != ':') {
2436 error(ERR_NONFATAL,
2437 "Syntax error processing `%%arg' directive");
2438 free_tlist(origline);
2439 return DIRECTIVE_FOUND;
2440 }
2441 tline = tline->next;
2442 if (!tline || tline->type != TOK_ID) {
2443 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2444 free_tlist(origline);
2445 return DIRECTIVE_FOUND;
2446 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002447
H. Peter Anvine2c80182005-01-15 22:15:51 +00002448 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002449 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002450 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002451 size = parse_size(tt->text);
2452 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002453 error(ERR_NONFATAL,
2454 "Invalid size type for `%%arg' missing directive");
2455 free_tlist(tt);
2456 free_tlist(origline);
2457 return DIRECTIVE_FOUND;
2458 }
2459 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002460
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002461 /* Round up to even stack slots */
2462 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002463
H. Peter Anvine2c80182005-01-15 22:15:51 +00002464 /* Now define the macro for the argument */
2465 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2466 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002467 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002468 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002469
H. Peter Anvine2c80182005-01-15 22:15:51 +00002470 /* Move to the next argument in the list */
2471 tline = tline->next;
2472 if (tline && tline->type == TOK_WHITESPACE)
2473 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002474 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002475 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002476 free_tlist(origline);
2477 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002478
H. Peter Anvine2c80182005-01-15 22:15:51 +00002479 case PP_LOCAL:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002480 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002481 /* TASM like LOCAL directive to define local variables for a
2482 * function, in the following form:
2483 *
2484 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2485 *
2486 * The '= LocalSize' at the end is ignored by NASM, but is
2487 * required by TASM to define the local parameter size (and used
2488 * by the TASM macro package).
2489 */
2490 offset = LocalOffset;
2491 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002492 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002493 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002494
H. Peter Anvine2c80182005-01-15 22:15:51 +00002495 /* Find the argument name */
2496 tline = tline->next;
2497 if (tline && tline->type == TOK_WHITESPACE)
2498 tline = tline->next;
2499 if (!tline || tline->type != TOK_ID) {
2500 error(ERR_NONFATAL,
2501 "`%%local' missing argument parameter");
2502 free_tlist(origline);
2503 return DIRECTIVE_FOUND;
2504 }
2505 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002506
H. Peter Anvine2c80182005-01-15 22:15:51 +00002507 /* Find the argument size type */
2508 tline = tline->next;
2509 if (!tline || tline->type != TOK_OTHER
2510 || tline->text[0] != ':') {
2511 error(ERR_NONFATAL,
2512 "Syntax error processing `%%local' directive");
2513 free_tlist(origline);
2514 return DIRECTIVE_FOUND;
2515 }
2516 tline = tline->next;
2517 if (!tline || tline->type != TOK_ID) {
2518 error(ERR_NONFATAL,
2519 "`%%local' missing size type parameter");
2520 free_tlist(origline);
2521 return DIRECTIVE_FOUND;
2522 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002523
H. Peter Anvine2c80182005-01-15 22:15:51 +00002524 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002525 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002526 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002527 size = parse_size(tt->text);
2528 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002529 error(ERR_NONFATAL,
2530 "Invalid size type for `%%local' missing directive");
2531 free_tlist(tt);
2532 free_tlist(origline);
2533 return DIRECTIVE_FOUND;
2534 }
2535 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002536
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002537 /* Round up to even stack slots */
2538 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002539
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002540 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002541
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002542 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002543 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2544 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002545 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002546
H. Peter Anvine2c80182005-01-15 22:15:51 +00002547 /* Now define the assign to setup the enter_c macro correctly */
2548 snprintf(directive, sizeof(directive),
2549 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002550 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002551
H. Peter Anvine2c80182005-01-15 22:15:51 +00002552 /* Move to the next argument in the list */
2553 tline = tline->next;
2554 if (tline && tline->type == TOK_WHITESPACE)
2555 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002556 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002557 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002558 free_tlist(origline);
2559 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002560
H. Peter Anvine2c80182005-01-15 22:15:51 +00002561 case PP_CLEAR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002562 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002563 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002564 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002565 "trailing garbage after `%%clear' ignored");
2566 free_macros();
2567 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002568 free_tlist(origline);
2569 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002570
H. Peter Anvin418ca702008-05-30 10:42:30 -07002571 case PP_DEPEND:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002572 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002573 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002574 skip_white_(t);
2575 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002576 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002577 error(ERR_NONFATAL, "`%%depend' expects a file name");
2578 free_tlist(origline);
2579 return DIRECTIVE_FOUND; /* but we did _something_ */
2580 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002581 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002582 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002583 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002584 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002585 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002586 nasm_unquote_cstr(p, i);
2587 if (dephead && !in_list(*dephead, p)) {
2588 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2589 sl->next = NULL;
2590 strcpy(sl->str, p);
2591 *deptail = sl;
2592 deptail = &sl->next;
2593 }
2594 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002595 return DIRECTIVE_FOUND;
2596
2597 case PP_INCLUDE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002598 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002599 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002600 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002601
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002602 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002603 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002604 error(ERR_NONFATAL, "`%%include' expects a file name");
2605 free_tlist(origline);
2606 return DIRECTIVE_FOUND; /* but we did _something_ */
2607 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002608 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002609 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002610 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002611 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002612 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002613 nasm_unquote_cstr(p, i);
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03002614 inc = nasm_zalloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002615 inc->next = istk;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002616 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002617 if (!inc->fp) {
2618 /* -MG given but file not found */
2619 nasm_free(inc);
2620 } else {
2621 inc->fname = src_set_fname(nasm_strdup(p));
2622 inc->lineno = src_set_linnum(0);
2623 inc->lineinc = 1;
2624 inc->expansion = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002625 istk = inc;
2626 list->uplevel(LIST_INCLUDE);
2627 }
2628 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002629 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002630
H. Peter Anvind2456592008-06-19 15:04:18 -07002631 case PP_USE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002632 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002633 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002634 static macros_t *use_pkg;
2635 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002636
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002637 tline = tline->next;
2638 skip_white_(tline);
2639 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002640
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002641 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002642 tline->type != TOK_INTERNAL_STRING &&
2643 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002644 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002645 free_tlist(origline);
2646 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002647 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002648 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002649 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002650 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002651 if (tline->type == TOK_STRING)
2652 nasm_unquote_cstr(tline->text, i);
2653 use_pkg = nasm_stdmac_find_package(tline->text);
2654 if (!use_pkg)
2655 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2656 else
2657 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002658 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002659 /* Not already included, go ahead and include it */
2660 stdmacpos = use_pkg;
2661 }
2662 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002663 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002664 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002665 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002666 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002667 case PP_POP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002668 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002669 tline = tline->next;
2670 skip_white_(tline);
2671 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002672 if (tline) {
2673 if (!tok_type_(tline, TOK_ID)) {
2674 error(ERR_NONFATAL, "`%s' expects a context identifier",
2675 pp_directives[i]);
2676 free_tlist(origline);
2677 return DIRECTIVE_FOUND; /* but we did _something_ */
2678 }
2679 if (tline->next)
2680 error(ERR_WARNING|ERR_PASS1,
2681 "trailing garbage after `%s' ignored",
2682 pp_directives[i]);
2683 p = nasm_strdup(tline->text);
2684 } else {
2685 p = NULL; /* Anonymous */
2686 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002687
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002688 if (i == PP_PUSH) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002689 ctx = nasm_zalloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002690 ctx->next = cstk;
2691 hash_init(&ctx->localmac, HASH_SMALL);
2692 ctx->name = p;
2693 ctx->number = unique++;
2694 cstk = ctx;
2695 } else {
2696 /* %pop or %repl */
2697 if (!cstk) {
2698 error(ERR_NONFATAL, "`%s': context stack is empty",
2699 pp_directives[i]);
2700 } else if (i == PP_POP) {
2701 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2702 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2703 "expected %s",
2704 cstk->name ? cstk->name : "anonymous", p);
2705 else
2706 ctx_pop();
2707 } else {
2708 /* i == PP_REPL */
2709 nasm_free(cstk->name);
2710 cstk->name = p;
2711 p = NULL;
2712 }
2713 nasm_free(p);
2714 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002715 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002716 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002717 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002718 severity = ERR_FATAL;
2719 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002720 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002721 severity = ERR_NONFATAL;
2722 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002723 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002724 severity = ERR_WARNING|ERR_WARN_USER;
2725 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002726
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002727issue_error:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002728 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002729 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002730 /* Only error out if this is the final pass */
2731 if (pass != 2 && i != PP_FATAL)
2732 return DIRECTIVE_FOUND;
2733
2734 tline->next = expand_smacro(tline->next);
2735 tline = tline->next;
2736 skip_white_(tline);
2737 t = tline ? tline->next : NULL;
2738 skip_white_(t);
2739 if (tok_type_(tline, TOK_STRING) && !t) {
2740 /* The line contains only a quoted string */
2741 p = tline->text;
2742 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2743 error(severity, "%s", p);
2744 } else {
2745 /* Not a quoted string, or more than a quoted string */
2746 p = detoken(tline, false);
2747 error(severity, "%s", p);
2748 nasm_free(p);
2749 }
2750 free_tlist(origline);
2751 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002752 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002753
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002754 CASE_PP_IF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002755 if (defining != NULL) {
2756 if (defining->type == EXP_IF) {
2757 defining->def_depth ++;
2758 }
2759 return NO_DIRECTIVE_FOUND;
2760 }
2761 if ((istk->expansion != NULL) &&
2762 (istk->expansion->emitting == false)) {
2763 j = COND_NEVER;
2764 } else {
2765 j = if_condition(tline->next, i);
2766 tline->next = NULL; /* it got freed */
2767 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
2768 }
2769 ed = new_ExpDef(EXP_IF);
2770 ed->state = j;
2771 ed->nolist = NULL;
2772 ed->def_depth = 0;
2773 ed->cur_depth = 0;
2774 ed->max_depth = 0;
2775 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
2776 ed->prev = defining;
2777 defining = ed;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002778 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002779 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002780
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002781 CASE_PP_ELIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002782 if (defining != NULL) {
2783 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2784 return NO_DIRECTIVE_FOUND;
2785 }
2786 }
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +04002787 if ((defining == NULL) || (defining->type != EXP_IF)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002788 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2789 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002790 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002791 case COND_IF_TRUE:
2792 defining->state = COND_DONE;
2793 defining->ignoring = true;
2794 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002795
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002796 case COND_DONE:
2797 case COND_NEVER:
2798 defining->ignoring = true;
2799 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002800
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002801 case COND_ELSE_TRUE:
2802 case COND_ELSE_FALSE:
2803 error_precond(ERR_WARNING|ERR_PASS1,
2804 "`%%elif' after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002805 defining->state = COND_NEVER;
2806 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002807 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002808
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002809 case COND_IF_FALSE:
2810 /*
2811 * IMPORTANT: In the case of %if, we will already have
2812 * called expand_mmac_params(); however, if we're
2813 * processing an %elif we must have been in a
2814 * non-emitting mode, which would have inhibited
2815 * the normal invocation of expand_mmac_params().
2816 * Therefore, we have to do it explicitly here.
2817 */
2818 j = if_condition(expand_mmac_params(tline->next), i);
2819 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002820 defining->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002821 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002822 defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002823 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002824 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002825 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002826 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002827
H. Peter Anvine2c80182005-01-15 22:15:51 +00002828 case PP_ELSE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002829 if (defining != NULL) {
2830 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2831 return NO_DIRECTIVE_FOUND;
2832 }
2833 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002834 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002835 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002836 "trailing garbage after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002837 if ((defining == NULL) || (defining->type != EXP_IF)) {
2838 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2839 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002840 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002841 case COND_IF_TRUE:
2842 case COND_DONE:
2843 defining->state = COND_ELSE_FALSE;
2844 defining->ignoring = true;
2845 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002846
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002847 case COND_NEVER:
2848 defining->ignoring = true;
2849 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002850
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002851 case COND_IF_FALSE:
2852 defining->state = COND_ELSE_TRUE;
2853 defining->ignoring = false;
2854 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002855
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002856 case COND_ELSE_TRUE:
2857 case COND_ELSE_FALSE:
2858 error_precond(ERR_WARNING|ERR_PASS1,
2859 "`%%else' after `%%else' ignored.");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002860 defining->state = COND_NEVER;
2861 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002862 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002863 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002864 free_tlist(origline);
2865 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002866
H. Peter Anvine2c80182005-01-15 22:15:51 +00002867 case PP_ENDIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002868 if (defining != NULL) {
2869 if (defining->type == EXP_IF) {
2870 if (defining->def_depth > 0) {
2871 defining->def_depth --;
2872 return NO_DIRECTIVE_FOUND;
2873 }
2874 } else {
2875 return NO_DIRECTIVE_FOUND;
2876 }
2877 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002878 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002879 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002880 "trailing garbage after `%%endif' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002881 if ((defining == NULL) || (defining->type != EXP_IF)) {
2882 error(ERR_NONFATAL, "`%%endif': no matching `%%if'");
2883 return DIRECTIVE_FOUND;
2884 }
2885 ed = defining;
2886 defining = ed->prev;
2887 ed->prev = expansions;
2888 expansions = ed;
2889 ei = new_ExpInv(EXP_IF, ed);
2890 ei->current = ed->line;
2891 ei->emitting = true;
2892 ei->prev = istk->expansion;
2893 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002894 free_tlist(origline);
2895 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002896
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002897 case PP_RMACRO:
2898 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002899 case PP_MACRO:
2900 case PP_IMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002901 if (defining != NULL) {
2902 if (defining->type == EXP_MMACRO) {
2903 defining->def_depth ++;
2904 }
2905 return NO_DIRECTIVE_FOUND;
2906 }
2907 ed = new_ExpDef(EXP_MMACRO);
2908 ed->max_depth =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002909 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002910 ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002911 if (!parse_mmacro_spec(tline, ed, pp_directives[i])) {
2912 nasm_free(ed);
2913 ed = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002914 return DIRECTIVE_FOUND;
2915 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002916 ed->def_depth = 0;
2917 ed->cur_depth = 0;
2918 ed->max_depth = (ed->max_depth + 1);
2919 ed->ignoring = false;
2920 ed->prev = defining;
2921 defining = ed;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002922
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002923 eed = (ExpDef *) hash_findix(&expdefs, ed->name);
2924 while (eed) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002925 if (!strcmp(eed->name, ed->name) &&
2926 (eed->nparam_min <= ed->nparam_max || ed->plus) &&
2927 (ed->nparam_min <= eed->nparam_max || eed->plus)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002928 error(ERR_WARNING|ERR_PASS1,
2929 "redefining multi-line macro `%s'", ed->name);
2930 return DIRECTIVE_FOUND;
2931 }
2932 eed = eed->next;
2933 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002934 free_tlist(origline);
2935 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002936
H. Peter Anvine2c80182005-01-15 22:15:51 +00002937 case PP_ENDM:
2938 case PP_ENDMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002939 if (defining != NULL) {
2940 if (defining->type == EXP_MMACRO) {
2941 if (defining->def_depth > 0) {
2942 defining->def_depth --;
2943 return NO_DIRECTIVE_FOUND;
2944 }
2945 } else {
2946 return NO_DIRECTIVE_FOUND;
2947 }
2948 }
2949 if (!(defining) || (defining->type != EXP_MMACRO)) {
2950 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2951 return DIRECTIVE_FOUND;
2952 }
2953 edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name);
2954 defining->next = *edhead;
2955 *edhead = defining;
2956 ed = defining;
2957 defining = ed->prev;
2958 ed->prev = expansions;
2959 expansions = ed;
2960 ed = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002961 free_tlist(origline);
2962 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002963
H. Peter Anvin89cee572009-07-15 09:16:54 -04002964 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002965 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2966 /*
2967 * We must search along istk->expansion until we hit a
2968 * macro invocation. Then we disable the emitting state(s)
2969 * between exitmacro and endmacro.
2970 */
2971 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
2972 if(ei->type == EXP_MMACRO) {
2973 break;
2974 }
2975 }
2976
2977 if (ei != NULL) {
2978 /*
2979 * Set all invocations leading back to the macro
2980 * invocation to a non-emitting state.
2981 */
2982 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
2983 eei->emitting = false;
2984 }
2985 eei->emitting = false;
2986 } else {
2987 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2988 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002989 free_tlist(origline);
2990 return DIRECTIVE_FOUND;
2991
H. Peter Anvina26433d2008-07-16 14:40:01 -07002992 case PP_UNMACRO:
2993 case PP_UNIMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002994 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002995 {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002996 ExpDef **ed_p;
2997 ExpDef spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002998
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002999 spec.casesense = (i == PP_UNMACRO);
3000 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
3001 return DIRECTIVE_FOUND;
3002 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003003 ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL);
3004 while (ed_p && *ed_p) {
3005 ed = *ed_p;
3006 if (ed->casesense == spec.casesense &&
3007 !mstrcmp(ed->name, spec.name, spec.casesense) &&
3008 ed->nparam_min == spec.nparam_min &&
3009 ed->nparam_max == spec.nparam_max &&
3010 ed->plus == spec.plus) {
Keith Kaniosc98a5b42010-12-18 12:17:31 -06003011 if (ed->cur_depth > 0) {
Keith Kanios21d885b2010-12-18 12:22:21 -06003012 error(ERR_NONFATAL, "`%s' ignored on active macro",
Keith Kaniosc98a5b42010-12-18 12:17:31 -06003013 pp_directives[i]);
3014 break;
3015 } else {
3016 *ed_p = ed->next;
3017 free_expdef(ed);
3018 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003019 } else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003020 ed_p = &ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003021 }
3022 }
3023 free_tlist(origline);
3024 free_tlist(spec.dlist);
3025 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003026 }
3027
H. Peter Anvine2c80182005-01-15 22:15:51 +00003028 case PP_ROTATE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003029 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003030 if (tline->next && tline->next->type == TOK_WHITESPACE)
3031 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003032 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003033 free_tlist(origline);
3034 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
3035 return DIRECTIVE_FOUND;
3036 }
3037 t = expand_smacro(tline->next);
3038 tline->next = NULL;
3039 free_tlist(origline);
3040 tline = t;
3041 tptr = &t;
3042 tokval.t_type = TOKEN_INVALID;
3043 evalresult =
3044 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3045 free_tlist(tline);
3046 if (!evalresult)
3047 return DIRECTIVE_FOUND;
3048 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003049 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003050 "trailing garbage after expression ignored");
3051 if (!is_simple(evalresult)) {
3052 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
3053 return DIRECTIVE_FOUND;
3054 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003055 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3056 if (ei->type == EXP_MMACRO) {
3057 break;
3058 }
3059 }
3060 if (ei == NULL) {
3061 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
3062 } else if (ei->nparam == 0) {
3063 error(ERR_NONFATAL,
3064 "`%%rotate' invoked within macro without parameters");
3065 } else {
3066 int rotate = ei->rotate + reloc_value(evalresult);
3067
3068 rotate %= (int)ei->nparam;
3069 if (rotate < 0)
3070 rotate += ei->nparam;
3071 ei->rotate = rotate;
3072 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003073 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003074
H. Peter Anvine2c80182005-01-15 22:15:51 +00003075 case PP_REP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003076 if (defining != NULL) {
3077 if (defining->type == EXP_REP) {
3078 defining->def_depth ++;
3079 }
3080 return NO_DIRECTIVE_FOUND;
3081 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003082 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003083 do {
3084 tline = tline->next;
3085 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003086
H. Peter Anvine2c80182005-01-15 22:15:51 +00003087 if (tok_type_(tline, TOK_ID) &&
3088 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003089 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003090 do {
3091 tline = tline->next;
3092 } while (tok_type_(tline, TOK_WHITESPACE));
3093 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003094
H. Peter Anvine2c80182005-01-15 22:15:51 +00003095 if (tline) {
3096 t = expand_smacro(tline);
3097 tptr = &t;
3098 tokval.t_type = TOKEN_INVALID;
3099 evalresult =
3100 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3101 if (!evalresult) {
3102 free_tlist(origline);
3103 return DIRECTIVE_FOUND;
3104 }
3105 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003106 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003107 "trailing garbage after expression ignored");
3108 if (!is_simple(evalresult)) {
3109 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
3110 return DIRECTIVE_FOUND;
3111 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003112 count = reloc_value(evalresult);
3113 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04003114 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003115 count = 0;
3116 } else
3117 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003118 } else {
3119 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003120 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003121 }
3122 free_tlist(origline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003123 ed = new_ExpDef(EXP_REP);
3124 ed->nolist = nolist;
3125 ed->def_depth = 0;
3126 ed->cur_depth = 1;
3127 ed->max_depth = (count - 1);
3128 ed->ignoring = false;
3129 ed->prev = defining;
3130 defining = ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003131 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003132
H. Peter Anvine2c80182005-01-15 22:15:51 +00003133 case PP_ENDREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003134 if (defining != NULL) {
3135 if (defining->type == EXP_REP) {
3136 if (defining->def_depth > 0) {
3137 defining->def_depth --;
3138 return NO_DIRECTIVE_FOUND;
3139 }
3140 } else {
3141 return NO_DIRECTIVE_FOUND;
3142 }
3143 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003144 if ((defining == NULL) || (defining->type != EXP_REP)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003145 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
3146 return DIRECTIVE_FOUND;
3147 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003148
H. Peter Anvine2c80182005-01-15 22:15:51 +00003149 /*
3150 * Now we have a "macro" defined - although it has no name
3151 * and we won't be entering it in the hash tables - we must
3152 * push a macro-end marker for it on to istk->expansion.
3153 * After that, it will take care of propagating itself (a
3154 * macro-end marker line for a macro which is really a %rep
3155 * block will cause the macro to be re-expanded, complete
3156 * with another macro-end marker to ensure the process
3157 * continues) until the whole expansion is forcibly removed
3158 * from istk->expansion by a %exitrep.
3159 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003160 ed = defining;
3161 defining = ed->prev;
3162 ed->prev = expansions;
3163 expansions = ed;
3164 ei = new_ExpInv(EXP_REP, ed);
3165 ei->current = ed->line;
3166 ei->emitting = ((ed->max_depth > 0) ? true : false);
3167 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3168 ei->prev = istk->expansion;
3169 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003170 free_tlist(origline);
3171 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003172
H. Peter Anvine2c80182005-01-15 22:15:51 +00003173 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003174 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3175 /*
3176 * We must search along istk->expansion until we hit a
3177 * rep invocation. Then we disable the emitting state(s)
3178 * between exitrep and endrep.
3179 */
3180 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3181 if (ei->type == EXP_REP) {
3182 break;
3183 }
3184 }
3185
3186 if (ei != NULL) {
3187 /*
3188 * Set all invocations leading back to the rep
3189 * invocation to a non-emitting state.
3190 */
3191 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3192 eei->emitting = false;
3193 }
3194 eei->emitting = false;
3195 eei->current = NULL;
3196 eei->def->cur_depth = eei->def->max_depth;
3197 } else {
3198 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3199 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003200 free_tlist(origline);
3201 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003202
H. Peter Anvine2c80182005-01-15 22:15:51 +00003203 case PP_XDEFINE:
3204 case PP_IXDEFINE:
3205 case PP_DEFINE:
3206 case PP_IDEFINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003207 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003208 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003209
H. Peter Anvine2c80182005-01-15 22:15:51 +00003210 tline = tline->next;
3211 skip_white_(tline);
3212 tline = expand_id(tline);
3213 if (!tline || (tline->type != TOK_ID &&
3214 (tline->type != TOK_PREPROC_ID ||
3215 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003216 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003217 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003218 free_tlist(origline);
3219 return DIRECTIVE_FOUND;
3220 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003221
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003222 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003223 last = tline;
3224 param_start = tline = tline->next;
3225 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003226
H. Peter Anvine2c80182005-01-15 22:15:51 +00003227 /* Expand the macro definition now for %xdefine and %ixdefine */
3228 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3229 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003230
H. Peter Anvine2c80182005-01-15 22:15:51 +00003231 if (tok_is_(tline, "(")) {
3232 /*
3233 * This macro has parameters.
3234 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003235
H. Peter Anvine2c80182005-01-15 22:15:51 +00003236 tline = tline->next;
3237 while (1) {
3238 skip_white_(tline);
3239 if (!tline) {
3240 error(ERR_NONFATAL, "parameter identifier expected");
3241 free_tlist(origline);
3242 return DIRECTIVE_FOUND;
3243 }
3244 if (tline->type != TOK_ID) {
3245 error(ERR_NONFATAL,
3246 "`%s': parameter identifier expected",
3247 tline->text);
3248 free_tlist(origline);
3249 return DIRECTIVE_FOUND;
3250 }
3251 tline->type = TOK_SMAC_PARAM + nparam++;
3252 tline = tline->next;
3253 skip_white_(tline);
3254 if (tok_is_(tline, ",")) {
3255 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003256 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003257 if (!tok_is_(tline, ")")) {
3258 error(ERR_NONFATAL,
3259 "`)' expected to terminate macro template");
3260 free_tlist(origline);
3261 return DIRECTIVE_FOUND;
3262 }
3263 break;
3264 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003265 }
3266 last = tline;
3267 tline = tline->next;
3268 }
3269 if (tok_type_(tline, TOK_WHITESPACE))
3270 last = tline, tline = tline->next;
3271 macro_start = NULL;
3272 last->next = NULL;
3273 t = tline;
3274 while (t) {
3275 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003276 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003277 if (tt->type >= TOK_SMAC_PARAM &&
3278 !strcmp(tt->text, t->text))
3279 t->type = tt->type;
3280 }
3281 tt = t->next;
3282 t->next = macro_start;
3283 macro_start = t;
3284 t = tt;
3285 }
3286 /*
3287 * Good. We now have a macro name, a parameter count, and a
3288 * token list (in reverse order) for an expansion. We ought
3289 * to be OK just to create an SMacro, store it, and let
3290 * free_tlist have the rest of the line (which we have
3291 * carefully re-terminated after chopping off the expansion
3292 * from the end).
3293 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003294 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003295 free_tlist(origline);
3296 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003297
H. Peter Anvine2c80182005-01-15 22:15:51 +00003298 case PP_UNDEF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003299 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003300 tline = tline->next;
3301 skip_white_(tline);
3302 tline = expand_id(tline);
3303 if (!tline || (tline->type != TOK_ID &&
3304 (tline->type != TOK_PREPROC_ID ||
3305 tline->text[1] != '$'))) {
3306 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3307 free_tlist(origline);
3308 return DIRECTIVE_FOUND;
3309 }
3310 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003311 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003312 "trailing garbage after macro name ignored");
3313 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003314
H. Peter Anvine2c80182005-01-15 22:15:51 +00003315 /* Find the context that symbol belongs to */
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003316 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003317 undef_smacro(ctx, mname);
3318 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003319 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003320
H. Peter Anvin9e200162008-06-04 17:23:14 -07003321 case PP_DEFSTR:
3322 case PP_IDEFSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003323 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003324 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003325
3326 tline = tline->next;
3327 skip_white_(tline);
3328 tline = expand_id(tline);
3329 if (!tline || (tline->type != TOK_ID &&
3330 (tline->type != TOK_PREPROC_ID ||
3331 tline->text[1] != '$'))) {
3332 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003333 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003334 free_tlist(origline);
3335 return DIRECTIVE_FOUND;
3336 }
3337
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003338 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003339 last = tline;
3340 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003341 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003342
3343 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003344 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003345
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003346 p = detoken(tline, false);
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003347 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003348 macro_start->text = nasm_quote(p, strlen(p));
3349 macro_start->type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003350 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003351
3352 /*
3353 * We now have a macro name, an implicit parameter count of
3354 * zero, and a string token to use as an expansion. Create
3355 * and store an SMacro.
3356 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003357 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003358 free_tlist(origline);
3359 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003360
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003361 case PP_DEFTOK:
3362 case PP_IDEFTOK:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003363 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003364 casesense = (i == PP_DEFTOK);
3365
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003366 tline = tline->next;
3367 skip_white_(tline);
3368 tline = expand_id(tline);
3369 if (!tline || (tline->type != TOK_ID &&
3370 (tline->type != TOK_PREPROC_ID ||
3371 tline->text[1] != '$'))) {
3372 error(ERR_NONFATAL,
3373 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003374 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003375 free_tlist(origline);
3376 return DIRECTIVE_FOUND;
3377 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003378 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003379 last = tline;
3380 tline = expand_smacro(tline->next);
3381 last->next = NULL;
3382
3383 t = tline;
3384 while (tok_type_(t, TOK_WHITESPACE))
3385 t = t->next;
3386 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003387 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003388 error(ERR_NONFATAL,
3389 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003390 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003391 free_tlist(tline);
3392 free_tlist(origline);
3393 return DIRECTIVE_FOUND;
3394 }
3395
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003396 /*
3397 * Convert the string to a token stream. Note that smacros
3398 * are stored with the token stream reversed, so we have to
3399 * reverse the output of tokenize().
3400 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003401 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003402 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003403
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003404 /*
3405 * We now have a macro name, an implicit parameter count of
3406 * zero, and a numeric token to use as an expansion. Create
3407 * and store an SMacro.
3408 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003409 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003410 free_tlist(tline);
3411 free_tlist(origline);
3412 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003413
H. Peter Anvin418ca702008-05-30 10:42:30 -07003414 case PP_PATHSEARCH:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003415 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003416 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003417 FILE *fp;
3418 StrList *xsl = NULL;
3419 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003420
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003421 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003422
3423 tline = tline->next;
3424 skip_white_(tline);
3425 tline = expand_id(tline);
3426 if (!tline || (tline->type != TOK_ID &&
3427 (tline->type != TOK_PREPROC_ID ||
3428 tline->text[1] != '$'))) {
3429 error(ERR_NONFATAL,
3430 "`%%pathsearch' expects a macro identifier as first parameter");
3431 free_tlist(origline);
3432 return DIRECTIVE_FOUND;
3433 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003434 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003435 last = tline;
3436 tline = expand_smacro(tline->next);
3437 last->next = NULL;
3438
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003439 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003440 while (tok_type_(t, TOK_WHITESPACE))
3441 t = t->next;
3442
3443 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003444 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003445 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003446 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003447 free_tlist(origline);
3448 return DIRECTIVE_FOUND; /* but we did _something_ */
3449 }
3450 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003451 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003452 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003453 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003454 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003455 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003456
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003457 fp = inc_fopen(p, &xsl, &xst, true);
3458 if (fp) {
3459 p = xsl->str;
3460 fclose(fp); /* Don't actually care about the file */
3461 }
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003462 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003463 macro_start->text = nasm_quote(p, strlen(p));
3464 macro_start->type = TOK_STRING;
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04003465 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003466
3467 /*
3468 * We now have a macro name, an implicit parameter count of
3469 * zero, and a string token to use as an expansion. Create
3470 * and store an SMacro.
3471 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003472 define_smacro(ctx, mname, casesense, 0, macro_start);
3473 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003474 free_tlist(origline);
3475 return DIRECTIVE_FOUND;
3476 }
3477
H. Peter Anvine2c80182005-01-15 22:15:51 +00003478 case PP_STRLEN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003479 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003480 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003481
H. Peter Anvine2c80182005-01-15 22:15:51 +00003482 tline = tline->next;
3483 skip_white_(tline);
3484 tline = expand_id(tline);
3485 if (!tline || (tline->type != TOK_ID &&
3486 (tline->type != TOK_PREPROC_ID ||
3487 tline->text[1] != '$'))) {
3488 error(ERR_NONFATAL,
3489 "`%%strlen' expects a macro identifier as first parameter");
3490 free_tlist(origline);
3491 return DIRECTIVE_FOUND;
3492 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003493 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003494 last = tline;
3495 tline = expand_smacro(tline->next);
3496 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003497
H. Peter Anvine2c80182005-01-15 22:15:51 +00003498 t = tline;
3499 while (tok_type_(t, TOK_WHITESPACE))
3500 t = t->next;
3501 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003502 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003503 error(ERR_NONFATAL,
3504 "`%%strlen` requires string as second parameter");
3505 free_tlist(tline);
3506 free_tlist(origline);
3507 return DIRECTIVE_FOUND;
3508 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003509
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003510 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003511 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003512
H. Peter Anvine2c80182005-01-15 22:15:51 +00003513 /*
3514 * We now have a macro name, an implicit parameter count of
3515 * zero, and a numeric token to use as an expansion. Create
3516 * and store an SMacro.
3517 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003518 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003519 free_tlist(tline);
3520 free_tlist(origline);
3521 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003522
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003523 case PP_STRCAT:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003524 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003525 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003526
3527 tline = tline->next;
3528 skip_white_(tline);
3529 tline = expand_id(tline);
3530 if (!tline || (tline->type != TOK_ID &&
3531 (tline->type != TOK_PREPROC_ID ||
3532 tline->text[1] != '$'))) {
3533 error(ERR_NONFATAL,
3534 "`%%strcat' expects a macro identifier as first parameter");
3535 free_tlist(origline);
3536 return DIRECTIVE_FOUND;
3537 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003538 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003539 last = tline;
3540 tline = expand_smacro(tline->next);
3541 last->next = NULL;
3542
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003543 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003544 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003545 switch (t->type) {
3546 case TOK_WHITESPACE:
3547 break;
3548 case TOK_STRING:
3549 len += t->a.len = nasm_unquote(t->text, NULL);
3550 break;
3551 case TOK_OTHER:
3552 if (!strcmp(t->text, ",")) /* permit comma separators */
3553 break;
3554 /* else fall through */
3555 default:
3556 error(ERR_NONFATAL,
3557 "non-string passed to `%%strcat' (%d)", t->type);
3558 free_tlist(tline);
3559 free_tlist(origline);
3560 return DIRECTIVE_FOUND;
3561 }
3562 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003563
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003564 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003565 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003566 if (t->type == TOK_STRING) {
3567 memcpy(p, t->text, t->a.len);
3568 p += t->a.len;
3569 }
3570 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003571
3572 /*
3573 * We now have a macro name, an implicit parameter count of
3574 * zero, and a numeric token to use as an expansion. Create
3575 * and store an SMacro.
3576 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003577 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3578 macro_start->text = nasm_quote(pp, len);
3579 nasm_free(pp);
3580 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003581 free_tlist(tline);
3582 free_tlist(origline);
3583 return DIRECTIVE_FOUND;
3584
H. Peter Anvine2c80182005-01-15 22:15:51 +00003585 case PP_SUBSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003586 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003587 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003588 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003589 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003590
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003591 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003592
H. Peter Anvine2c80182005-01-15 22:15:51 +00003593 tline = tline->next;
3594 skip_white_(tline);
3595 tline = expand_id(tline);
3596 if (!tline || (tline->type != TOK_ID &&
3597 (tline->type != TOK_PREPROC_ID ||
3598 tline->text[1] != '$'))) {
3599 error(ERR_NONFATAL,
3600 "`%%substr' expects a macro identifier as first parameter");
3601 free_tlist(origline);
3602 return DIRECTIVE_FOUND;
3603 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003604 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003605 last = tline;
3606 tline = expand_smacro(tline->next);
3607 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003608
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003609 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003610 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003611 while (tok_type_(t, TOK_WHITESPACE))
3612 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003613
H. Peter Anvine2c80182005-01-15 22:15:51 +00003614 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003615 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003616 error(ERR_NONFATAL,
3617 "`%%substr` requires string as second parameter");
3618 free_tlist(tline);
3619 free_tlist(origline);
3620 return DIRECTIVE_FOUND;
3621 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003622
H. Peter Anvine2c80182005-01-15 22:15:51 +00003623 tt = t->next;
3624 tptr = &tt;
3625 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003626 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003627 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003628 if (!evalresult) {
3629 free_tlist(tline);
3630 free_tlist(origline);
3631 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003632 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003633 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3634 free_tlist(tline);
3635 free_tlist(origline);
3636 return DIRECTIVE_FOUND;
3637 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003638 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003639
3640 while (tok_type_(tt, TOK_WHITESPACE))
3641 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003642 if (!tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003643 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003644 } else {
3645 tokval.t_type = TOKEN_INVALID;
3646 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3647 pass, error, NULL);
3648 if (!evalresult) {
3649 free_tlist(tline);
3650 free_tlist(origline);
3651 return DIRECTIVE_FOUND;
3652 } else if (!is_simple(evalresult)) {
3653 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3654 free_tlist(tline);
3655 free_tlist(origline);
3656 return DIRECTIVE_FOUND;
3657 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003658 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003659 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003660
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003661 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003662 /* make start and count being in range */
3663 if (start < 0)
3664 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003665 if (count < 0)
3666 count = len + count + 1 - start;
3667 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003668 count = len - start;
3669 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003670 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003671
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003672 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003673 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003674 macro_start->type = TOK_STRING;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003675
H. Peter Anvine2c80182005-01-15 22:15:51 +00003676 /*
3677 * We now have a macro name, an implicit parameter count of
3678 * zero, and a numeric token to use as an expansion. Create
3679 * and store an SMacro.
3680 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003681 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003682 free_tlist(tline);
3683 free_tlist(origline);
3684 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003685 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003686
H. Peter Anvine2c80182005-01-15 22:15:51 +00003687 case PP_ASSIGN:
3688 case PP_IASSIGN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003689 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003690 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003691
H. Peter Anvine2c80182005-01-15 22:15:51 +00003692 tline = tline->next;
3693 skip_white_(tline);
3694 tline = expand_id(tline);
3695 if (!tline || (tline->type != TOK_ID &&
3696 (tline->type != TOK_PREPROC_ID ||
3697 tline->text[1] != '$'))) {
3698 error(ERR_NONFATAL,
3699 "`%%%sassign' expects a macro identifier",
3700 (i == PP_IASSIGN ? "i" : ""));
3701 free_tlist(origline);
3702 return DIRECTIVE_FOUND;
3703 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003704 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003705 last = tline;
3706 tline = expand_smacro(tline->next);
3707 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003708
H. Peter Anvine2c80182005-01-15 22:15:51 +00003709 t = tline;
3710 tptr = &t;
3711 tokval.t_type = TOKEN_INVALID;
3712 evalresult =
3713 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3714 free_tlist(tline);
3715 if (!evalresult) {
3716 free_tlist(origline);
3717 return DIRECTIVE_FOUND;
3718 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003719
H. Peter Anvine2c80182005-01-15 22:15:51 +00003720 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003721 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003722 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003723
H. Peter Anvine2c80182005-01-15 22:15:51 +00003724 if (!is_simple(evalresult)) {
3725 error(ERR_NONFATAL,
3726 "non-constant value given to `%%%sassign'",
3727 (i == PP_IASSIGN ? "i" : ""));
3728 free_tlist(origline);
3729 return DIRECTIVE_FOUND;
3730 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003731
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003732 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003733 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003734
H. Peter Anvine2c80182005-01-15 22:15:51 +00003735 /*
3736 * We now have a macro name, an implicit parameter count of
3737 * zero, and a numeric token to use as an expansion. Create
3738 * and store an SMacro.
3739 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003740 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003741 free_tlist(origline);
3742 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003743
H. Peter Anvine2c80182005-01-15 22:15:51 +00003744 case PP_LINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003745 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003746 /*
3747 * Syntax is `%line nnn[+mmm] [filename]'
3748 */
3749 tline = tline->next;
3750 skip_white_(tline);
3751 if (!tok_type_(tline, TOK_NUMBER)) {
3752 error(ERR_NONFATAL, "`%%line' expects line number");
3753 free_tlist(origline);
3754 return DIRECTIVE_FOUND;
3755 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003756 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003757 m = 1;
3758 tline = tline->next;
3759 if (tok_is_(tline, "+")) {
3760 tline = tline->next;
3761 if (!tok_type_(tline, TOK_NUMBER)) {
3762 error(ERR_NONFATAL, "`%%line' expects line increment");
3763 free_tlist(origline);
3764 return DIRECTIVE_FOUND;
3765 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003766 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003767 tline = tline->next;
3768 }
3769 skip_white_(tline);
3770 src_set_linnum(k);
3771 istk->lineinc = m;
3772 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003773 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003774 }
3775 free_tlist(origline);
3776 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003777
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003778 case PP_WHILE:
3779 if (defining != NULL) {
3780 if (defining->type == EXP_WHILE) {
3781 defining->def_depth ++;
3782 }
3783 return NO_DIRECTIVE_FOUND;
3784 }
3785 l = NULL;
3786 if ((istk->expansion != NULL) &&
3787 (istk->expansion->emitting == false)) {
3788 j = COND_NEVER;
3789 } else {
3790 l = new_Line();
3791 l->first = copy_Token(tline->next);
3792 j = if_condition(tline->next, i);
3793 tline->next = NULL; /* it got freed */
3794 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
3795 }
3796 ed = new_ExpDef(EXP_WHILE);
3797 ed->state = j;
3798 ed->cur_depth = 1;
3799 ed->max_depth = DEADMAN_LIMIT;
3800 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
3801 if (ed->ignoring == false) {
3802 ed->line = l;
3803 ed->last = l;
3804 } else if (l != NULL) {
3805 delete_Token(l->first);
3806 nasm_free(l);
3807 l = NULL;
3808 }
3809 ed->prev = defining;
3810 defining = ed;
3811 free_tlist(origline);
3812 return DIRECTIVE_FOUND;
3813
3814 case PP_ENDWHILE:
3815 if (defining != NULL) {
3816 if (defining->type == EXP_WHILE) {
3817 if (defining->def_depth > 0) {
3818 defining->def_depth --;
3819 return NO_DIRECTIVE_FOUND;
3820 }
3821 } else {
3822 return NO_DIRECTIVE_FOUND;
3823 }
3824 }
3825 if (tline->next != NULL) {
3826 error_precond(ERR_WARNING|ERR_PASS1,
3827 "trailing garbage after `%%endwhile' ignored");
3828 }
3829 if ((defining == NULL) || (defining->type != EXP_WHILE)) {
3830 error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'");
3831 return DIRECTIVE_FOUND;
3832 }
3833 ed = defining;
3834 defining = ed->prev;
3835 if (ed->ignoring == false) {
3836 ed->prev = expansions;
3837 expansions = ed;
3838 ei = new_ExpInv(EXP_WHILE, ed);
3839 ei->current = ed->line->next;
3840 ei->emitting = true;
3841 ei->prev = istk->expansion;
3842 istk->expansion = ei;
3843 } else {
3844 nasm_free(ed);
3845 }
3846 free_tlist(origline);
3847 return DIRECTIVE_FOUND;
3848
3849 case PP_EXITWHILE:
3850 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3851 /*
3852 * We must search along istk->expansion until we hit a
3853 * while invocation. Then we disable the emitting state(s)
3854 * between exitwhile and endwhile.
3855 */
3856 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3857 if (ei->type == EXP_WHILE) {
3858 break;
3859 }
3860 }
3861
3862 if (ei != NULL) {
3863 /*
3864 * Set all invocations leading back to the while
3865 * invocation to a non-emitting state.
3866 */
3867 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3868 eei->emitting = false;
3869 }
3870 eei->emitting = false;
3871 eei->current = NULL;
3872 eei->def->cur_depth = eei->def->max_depth;
3873 } else {
3874 error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block");
3875 }
3876 free_tlist(origline);
3877 return DIRECTIVE_FOUND;
3878
3879 case PP_COMMENT:
3880 if (defining != NULL) {
3881 if (defining->type == EXP_COMMENT) {
3882 defining->def_depth ++;
3883 }
3884 return NO_DIRECTIVE_FOUND;
3885 }
3886 ed = new_ExpDef(EXP_COMMENT);
3887 ed->ignoring = true;
3888 ed->prev = defining;
3889 defining = ed;
3890 free_tlist(origline);
3891 return DIRECTIVE_FOUND;
3892
3893 case PP_ENDCOMMENT:
3894 if (defining != NULL) {
3895 if (defining->type == EXP_COMMENT) {
3896 if (defining->def_depth > 0) {
3897 defining->def_depth --;
3898 return NO_DIRECTIVE_FOUND;
3899 }
3900 } else {
3901 return NO_DIRECTIVE_FOUND;
3902 }
3903 }
3904 if ((defining == NULL) || (defining->type != EXP_COMMENT)) {
3905 error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'");
3906 return DIRECTIVE_FOUND;
3907 }
3908 ed = defining;
3909 defining = ed->prev;
3910 nasm_free(ed);
3911 free_tlist(origline);
3912 return DIRECTIVE_FOUND;
3913
3914 case PP_FINAL:
3915 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3916 if (in_final != false) {
3917 error(ERR_FATAL, "`%%final' cannot be used recursively");
3918 }
3919 tline = tline->next;
3920 skip_white_(tline);
3921 if (tline == NULL) {
3922 error(ERR_NONFATAL, "`%%final' expects at least one parameter");
3923 } else {
3924 l = new_Line();
3925 l->first = copy_Token(tline);
3926 l->next = finals;
3927 finals = l;
3928 }
3929 free_tlist(origline);
3930 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003931
H. Peter Anvine2c80182005-01-15 22:15:51 +00003932 default:
3933 error(ERR_FATAL,
3934 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003935 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003936 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003937 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003938}
3939
3940/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003941 * Ensure that a macro parameter contains a condition code and
3942 * nothing else. Return the condition code index if so, or -1
3943 * otherwise.
3944 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003945static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003946{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003947 Token *tt;
3948 int i, j, k, m;
3949
H. Peter Anvin25a99342007-09-22 17:45:45 -07003950 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003951 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003952
H. Peter Anvineba20a72002-04-30 20:53:55 +00003953 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003954 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003955 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003956 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003957 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003958 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003959 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003960
3961 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003962 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003963 while (j - i > 1) {
3964 k = (j + i) / 2;
3965 m = nasm_stricmp(t->text, conditions[k]);
3966 if (m == 0) {
3967 i = k;
3968 j = -2;
3969 break;
3970 } else if (m < 0) {
3971 j = k;
3972 } else
3973 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003974 }
3975 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003976 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003977 return i;
3978}
3979
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003980static bool paste_tokens(Token **head, const struct tokseq_match *m,
3981 int mnum, bool handle_paste_tokens)
H. Peter Anvind784a082009-04-20 14:01:18 -07003982{
3983 Token **tail, *t, *tt;
3984 Token **paste_head;
3985 bool did_paste = false;
3986 char *tmp;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003987 int i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003988
3989 /* Now handle token pasting... */
3990 paste_head = NULL;
3991 tail = head;
3992 while ((t = *tail) && (tt = t->next)) {
3993 switch (t->type) {
3994 case TOK_WHITESPACE:
3995 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003996 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003997 t->next = delete_Token(tt);
3998 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003999 /* Do not advance paste_head here */
4000 tail = &t->next;
4001 }
H. Peter Anvind784a082009-04-20 14:01:18 -07004002 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004003 case TOK_PASTE: /* %+ */
4004 if (handle_paste_tokens) {
4005 /* Zap %+ and whitespace tokens to the right */
4006 while (t && (t->type == TOK_WHITESPACE ||
4007 t->type == TOK_PASTE))
4008 t = *tail = delete_Token(t);
4009 if (!paste_head || !t)
4010 break; /* Nothing to paste with */
4011 tail = paste_head;
4012 t = *tail;
4013 tt = t->next;
4014 while (tok_type_(tt, TOK_WHITESPACE))
4015 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004016 if (tt) {
4017 tmp = nasm_strcat(t->text, tt->text);
4018 delete_Token(t);
4019 tt = delete_Token(tt);
4020 t = *tail = tokenize(tmp);
4021 nasm_free(tmp);
4022 while (t->next) {
4023 tail = &t->next;
4024 t = t->next;
4025 }
4026 t->next = tt; /* Attach the remaining token chain */
4027 did_paste = true;
4028 }
4029 paste_head = tail;
4030 tail = &t->next;
4031 break;
4032 }
4033 /* else fall through */
4034 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004035 /*
4036 * Concatenation of tokens might look nontrivial
4037 * but in real it's pretty simple -- the caller
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004038 * prepares the masks of token types to be concatenated
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004039 * and we simply find matched sequences and slip
4040 * them together
4041 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004042 for (i = 0; i < mnum; i++) {
4043 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
4044 size_t len = 0;
4045 char *tmp, *p;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004046
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004047 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004048 len += strlen(tt->text);
4049 tt = tt->next;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004050 }
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004051
Cyrill Gorcunovfdd0ac52011-06-27 01:22:27 +04004052 nasm_dump_token(tt);
4053
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004054 /*
4055 * Now tt points to the first token after
4056 * the potential paste area...
4057 */
4058 if (tt != t->next) {
4059 /* We have at least two tokens... */
4060 len += strlen(t->text);
4061 p = tmp = nasm_malloc(len+1);
4062 while (t != tt) {
4063 strcpy(p, t->text);
4064 p = strchr(p, '\0');
4065 t = delete_Token(t);
4066 }
4067 t = *tail = tokenize(tmp);
4068 nasm_free(tmp);
4069 while (t->next) {
4070 tail = &t->next;
4071 t = t->next;
4072 }
4073 t->next = tt; /* Attach the remaining token chain */
4074 did_paste = true;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004075 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004076 paste_head = tail;
4077 tail = &t->next;
4078 break;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004079 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004080 }
4081 if (i >= mnum) { /* no match */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004082 tail = &t->next;
4083 if (!tok_type_(t->next, TOK_WHITESPACE))
4084 paste_head = tail;
4085 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004086 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004087 }
4088 }
4089 return did_paste;
4090}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004091
4092/*
4093 * expands to a list of tokens from %{x:y}
4094 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004095static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004096{
4097 Token *t = tline, **tt, *tm, *head;
4098 char *pos;
4099 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004100
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004101 pos = strchr(tline->text, ':');
4102 nasm_assert(pos);
4103
4104 lst = atoi(pos + 1);
4105 fst = atoi(tline->text + 1);
4106
4107 /*
4108 * only macros params are accounted so
4109 * if someone passes %0 -- we reject such
4110 * value(s)
4111 */
4112 if (lst == 0 || fst == 0)
4113 goto err;
4114
4115 /* the values should be sane */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004116 if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) ||
4117 (lst > (int)ei->nparam || lst < (-(int)ei->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004118 goto err;
4119
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004120 fst = fst < 0 ? fst + (int)ei->nparam + 1: fst;
4121 lst = lst < 0 ? lst + (int)ei->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004122
4123 /* counted from zero */
4124 fst--, lst--;
4125
4126 /*
4127 * it will be at least one token
4128 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004129 tm = ei->params[(fst + ei->rotate) % ei->nparam];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004130 t = new_Token(NULL, tm->type, tm->text, 0);
4131 head = t, tt = &t->next;
4132 if (fst < lst) {
4133 for (i = fst + 1; i <= lst; i++) {
4134 t = new_Token(NULL, TOK_OTHER, ",", 0);
4135 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004136 j = (i + ei->rotate) % ei->nparam;
4137 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004138 t = new_Token(NULL, tm->type, tm->text, 0);
4139 *tt = t, tt = &t->next;
4140 }
4141 } else {
4142 for (i = fst - 1; i >= lst; i--) {
4143 t = new_Token(NULL, TOK_OTHER, ",", 0);
4144 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004145 j = (i + ei->rotate) % ei->nparam;
4146 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004147 t = new_Token(NULL, tm->type, tm->text, 0);
4148 *tt = t, tt = &t->next;
4149 }
4150 }
4151
4152 *last = tt;
4153 return head;
4154
4155err:
4156 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
4157 &tline->text[1]);
4158 return tline;
4159}
4160
H. Peter Anvin76690a12002-04-30 20:52:49 +00004161/*
4162 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004163 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004164 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004165 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004166static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004167{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004168 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004169 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004170 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004171
4172 tail = &thead;
4173 thead = NULL;
4174
Cyrill Gorcunov2e046002011-06-26 23:33:56 +04004175 nasm_dump_stream(tline);
4176
H. Peter Anvine2c80182005-01-15 22:15:51 +00004177 while (tline) {
4178 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004179 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4180 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4181 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004182 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004183 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004184 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004185 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004186 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004187 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004188
H. Peter Anvine2c80182005-01-15 22:15:51 +00004189 t = tline;
4190 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004191
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004192 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
4193 if (ei->type == EXP_MMACRO) {
4194 break;
4195 }
4196 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004197 if (ei == NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004198 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004199 } else {
4200 pos = strchr(t->text, ':');
4201 if (!pos) {
4202 switch (t->text[1]) {
4203 /*
4204 * We have to make a substitution of one of the
4205 * forms %1, %-1, %+1, %%foo, %0.
4206 */
4207 case '0':
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004208 if ((strlen(t->text) > 2) && (t->text[2] == '0')) {
4209 type = TOK_ID;
4210 text = nasm_strdup(ei->label_text);
4211 } else {
4212 type = TOK_NUMBER;
4213 snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam);
4214 text = nasm_strdup(tmpbuf);
4215 }
4216 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004217 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004218 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004219 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004220 ei->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004221 text = nasm_strcat(tmpbuf, t->text + 2);
4222 break;
4223 case '-':
4224 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004225 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004226 tt = NULL;
4227 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004228 if (ei->nparam > 1)
4229 n = (n + ei->rotate) % ei->nparam;
4230 tt = ei->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004231 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004232 cc = find_cc(tt);
4233 if (cc == -1) {
4234 error(ERR_NONFATAL,
4235 "macro parameter %d is not a condition code",
4236 n + 1);
4237 text = NULL;
4238 } else {
4239 type = TOK_ID;
4240 if (inverse_ccs[cc] == -1) {
4241 error(ERR_NONFATAL,
4242 "condition code `%s' is not invertible",
4243 conditions[cc]);
4244 text = NULL;
4245 } else
4246 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4247 }
4248 break;
4249 case '+':
4250 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004251 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004252 tt = NULL;
4253 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004254 if (ei->nparam > 1)
4255 n = (n + ei->rotate) % ei->nparam;
4256 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004257 }
4258 cc = find_cc(tt);
4259 if (cc == -1) {
4260 error(ERR_NONFATAL,
4261 "macro parameter %d is not a condition code",
4262 n + 1);
4263 text = NULL;
4264 } else {
4265 type = TOK_ID;
4266 text = nasm_strdup(conditions[cc]);
4267 }
4268 break;
4269 default:
4270 n = atoi(t->text + 1) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004271 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004272 tt = NULL;
4273 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004274 if (ei->nparam > 1)
4275 n = (n + ei->rotate) % ei->nparam;
4276 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004277 }
4278 if (tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004279 for (i = 0; i < ei->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004280 *tail = new_Token(NULL, tt->type, tt->text, 0);
4281 tail = &(*tail)->next;
4282 tt = tt->next;
4283 }
4284 }
4285 text = NULL; /* we've done it here */
4286 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004287 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004288 } else {
4289 /*
4290 * seems we have a parameters range here
4291 */
4292 Token *head, **last;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004293 head = expand_mmac_params_range(ei, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004294 if (head != t) {
4295 *tail = head;
4296 *last = tline;
4297 tline = head;
4298 text = NULL;
4299 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004300 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004301 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004302 if (!text) {
4303 delete_Token(t);
4304 } else {
4305 *tail = t;
4306 tail = &t->next;
4307 t->type = type;
4308 nasm_free(t->text);
4309 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004310 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004311 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004312 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004313 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004314 } else if (tline->type == TOK_INDIRECT) {
4315 t = tline;
4316 tline = tline->next;
4317 tt = tokenize(t->text);
4318 tt = expand_mmac_params(tt);
4319 tt = expand_smacro(tt);
4320 *tail = tt;
4321 while (tt) {
4322 tt->a.mac = NULL; /* Necessary? */
4323 tail = &tt->next;
4324 tt = tt->next;
4325 }
4326 delete_Token(t);
4327 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004328 } else {
4329 t = *tail = tline;
4330 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004331 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004332 tail = &t->next;
4333 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004334 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004335 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004336
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004337 if (changed) {
4338 const struct tokseq_match t[] = {
4339 {
4340 PP_CONCAT_MASK(TOK_ID) |
4341 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4342 PP_CONCAT_MASK(TOK_ID) |
4343 PP_CONCAT_MASK(TOK_NUMBER) |
4344 PP_CONCAT_MASK(TOK_FLOAT) |
4345 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4346 },
4347 {
4348 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4349 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4350 }
4351 };
4352 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4353 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004354
Cyrill Gorcunov2e046002011-06-26 23:33:56 +04004355 nasm_dump_token(thead);
4356
H. Peter Anvin76690a12002-04-30 20:52:49 +00004357 return thead;
4358}
4359
4360/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004361 * Expand all single-line macro calls made in the given line.
4362 * Return the expanded version of the line. The original is deemed
4363 * to be destroyed in the process. (In reality we'll just move
4364 * Tokens from input to output a lot of the time, rather than
4365 * actually bothering to destroy and replicate.)
4366 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004367
H. Peter Anvine2c80182005-01-15 22:15:51 +00004368static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004369{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004370 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004371 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004372 Token **params;
4373 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004374 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004375 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004376 Token *org_tline = tline;
4377 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004378 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004379 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004380 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004381
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004382 /*
4383 * Trick: we should avoid changing the start token pointer since it can
4384 * be contained in "next" field of other token. Because of this
4385 * we allocate a copy of first token and work with it; at the end of
4386 * routine we copy it back
4387 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004388 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004389 tline = new_Token(org_tline->next, org_tline->type,
4390 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004391 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004392 nasm_free(org_tline->text);
4393 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004394 }
4395
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004396 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004397
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004398again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004399 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004400 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004401
H. Peter Anvine2c80182005-01-15 22:15:51 +00004402 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004403 if (!--deadman) {
4404 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004405 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004406 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004407
H. Peter Anvine2c80182005-01-15 22:15:51 +00004408 if ((mname = tline->text)) {
4409 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004410 if (tline->type == TOK_ID) {
4411 head = (SMacro *)hash_findix(&smacros, mname);
4412 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04004413 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004414 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4415 } else
4416 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004417
H. Peter Anvine2c80182005-01-15 22:15:51 +00004418 /*
4419 * We've hit an identifier. As in is_mmacro below, we first
4420 * check whether the identifier is a single-line macro at
4421 * all, then think about checking for parameters if
4422 * necessary.
4423 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004424 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004425 if (!mstrcmp(m->name, mname, m->casesense))
4426 break;
4427 if (m) {
4428 mstart = tline;
4429 params = NULL;
4430 paramsize = NULL;
4431 if (m->nparam == 0) {
4432 /*
4433 * Simple case: the macro is parameterless. Discard the
4434 * one token that the macro call took, and push the
4435 * expansion back on the to-do stack.
4436 */
4437 if (!m->expansion) {
4438 if (!strcmp("__FILE__", m->name)) {
4439 int32_t num = 0;
4440 char *file = NULL;
4441 src_get(&num, &file);
4442 tline->text = nasm_quote(file, strlen(file));
4443 tline->type = TOK_STRING;
4444 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004445 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004446 }
4447 if (!strcmp("__LINE__", m->name)) {
4448 nasm_free(tline->text);
4449 make_tok_num(tline, src_get_linnum());
4450 continue;
4451 }
4452 if (!strcmp("__BITS__", m->name)) {
4453 nasm_free(tline->text);
4454 make_tok_num(tline, globalbits);
4455 continue;
4456 }
4457 tline = delete_Token(tline);
4458 continue;
4459 }
4460 } else {
4461 /*
4462 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004463 * exists and takes parameters. We must find the
4464 * parameters in the call, count them, find the SMacro
4465 * that corresponds to that form of the macro call, and
4466 * substitute for the parameters when we expand. What a
4467 * pain.
4468 */
4469 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004470 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004471 do {
4472 t = tline->next;
4473 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004474 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004475 t->text = NULL;
4476 t = tline->next = delete_Token(t);
4477 }
4478 tline = t;
4479 } while (tok_type_(tline, TOK_WHITESPACE));
4480 if (!tok_is_(tline, "(")) {
4481 /*
4482 * This macro wasn't called with parameters: ignore
4483 * the call. (Behaviour borrowed from gnu cpp.)
4484 */
4485 tline = mstart;
4486 m = NULL;
4487 } else {
4488 int paren = 0;
4489 int white = 0;
4490 brackets = 0;
4491 nparam = 0;
4492 sparam = PARAM_DELTA;
4493 params = nasm_malloc(sparam * sizeof(Token *));
4494 params[0] = tline->next;
4495 paramsize = nasm_malloc(sparam * sizeof(int));
4496 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004497 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004498 /*
4499 * For some unusual expansions
4500 * which concatenates function call
4501 */
4502 t = tline->next;
4503 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004504 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004505 t->text = NULL;
4506 t = tline->next = delete_Token(t);
4507 }
4508 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004509
H. Peter Anvine2c80182005-01-15 22:15:51 +00004510 if (!tline) {
4511 error(ERR_NONFATAL,
4512 "macro call expects terminating `)'");
4513 break;
4514 }
4515 if (tline->type == TOK_WHITESPACE
4516 && brackets <= 0) {
4517 if (paramsize[nparam])
4518 white++;
4519 else
4520 params[nparam] = tline->next;
4521 continue; /* parameter loop */
4522 }
4523 if (tline->type == TOK_OTHER
4524 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004525 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004526 if (ch == ',' && !paren && brackets <= 0) {
4527 if (++nparam >= sparam) {
4528 sparam += PARAM_DELTA;
4529 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004530 sparam * sizeof(Token *));
4531 paramsize = nasm_realloc(paramsize,
4532 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004533 }
4534 params[nparam] = tline->next;
4535 paramsize[nparam] = 0;
4536 white = 0;
4537 continue; /* parameter loop */
4538 }
4539 if (ch == '{' &&
4540 (brackets > 0 || (brackets == 0 &&
4541 !paramsize[nparam])))
4542 {
4543 if (!(brackets++)) {
4544 params[nparam] = tline->next;
4545 continue; /* parameter loop */
4546 }
4547 }
4548 if (ch == '}' && brackets > 0)
4549 if (--brackets == 0) {
4550 brackets = -1;
4551 continue; /* parameter loop */
4552 }
4553 if (ch == '(' && !brackets)
4554 paren++;
4555 if (ch == ')' && brackets <= 0)
4556 if (--paren < 0)
4557 break;
4558 }
4559 if (brackets < 0) {
4560 brackets = 0;
4561 error(ERR_NONFATAL, "braces do not "
4562 "enclose all of macro parameter");
4563 }
4564 paramsize[nparam] += white + 1;
4565 white = 0;
4566 } /* parameter loop */
4567 nparam++;
4568 while (m && (m->nparam != nparam ||
4569 mstrcmp(m->name, mname,
4570 m->casesense)))
4571 m = m->next;
4572 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004573 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004574 "macro `%s' exists, "
4575 "but not taking %d parameters",
4576 mstart->text, nparam);
4577 }
4578 }
4579 if (m && m->in_progress)
4580 m = NULL;
4581 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004582 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004583 * Design question: should we handle !tline, which
4584 * indicates missing ')' here, or expand those
4585 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004586 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004587 */
4588 nasm_free(params);
4589 nasm_free(paramsize);
4590 tline = mstart;
4591 } else {
4592 /*
4593 * Expand the macro: we are placed on the last token of the
4594 * call, so that we can easily split the call from the
4595 * following tokens. We also start by pushing an SMAC_END
4596 * token for the cycle removal.
4597 */
4598 t = tline;
4599 if (t) {
4600 tline = t->next;
4601 t->next = NULL;
4602 }
4603 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004604 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004605 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004606 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004607 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004608 if (t->type >= TOK_SMAC_PARAM) {
4609 Token *pcopy = tline, **ptail = &pcopy;
4610 Token *ttt, *pt;
4611 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004612
H. Peter Anvine2c80182005-01-15 22:15:51 +00004613 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004614 i = paramsize[t->type - TOK_SMAC_PARAM];
4615 while (--i >= 0) {
4616 pt = *ptail = new_Token(tline, ttt->type,
4617 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004618 ptail = &pt->next;
4619 ttt = ttt->next;
4620 }
4621 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004622 } else if (t->type == TOK_PREPROC_Q) {
4623 tt = new_Token(tline, TOK_ID, mname, 0);
4624 tline = tt;
4625 } else if (t->type == TOK_PREPROC_QQ) {
4626 tt = new_Token(tline, TOK_ID, m->name, 0);
4627 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004628 } else {
4629 tt = new_Token(tline, t->type, t->text, 0);
4630 tline = tt;
4631 }
4632 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004633
H. Peter Anvine2c80182005-01-15 22:15:51 +00004634 /*
4635 * Having done that, get rid of the macro call, and clean
4636 * up the parameters.
4637 */
4638 nasm_free(params);
4639 nasm_free(paramsize);
4640 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004641 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004642 continue; /* main token loop */
4643 }
4644 }
4645 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004646
H. Peter Anvine2c80182005-01-15 22:15:51 +00004647 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004648 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004649 tline = delete_Token(tline);
4650 } else {
4651 t = *tail = tline;
4652 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004653 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004654 t->next = NULL;
4655 tail = &t->next;
4656 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004657 }
4658
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004659 /*
4660 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004661 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004662 * TOK_IDs should be concatenated.
4663 * Also we look for %+ tokens and concatenate the tokens before and after
4664 * them (without white spaces in between).
4665 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004666 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004667 const struct tokseq_match t[] = {
4668 {
4669 PP_CONCAT_MASK(TOK_ID) |
4670 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4671 PP_CONCAT_MASK(TOK_ID) |
4672 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4673 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4674 }
4675 };
4676 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004677 /*
4678 * If we concatenated something, *and* we had previously expanded
4679 * an actual macro, scan the lines again for macros...
4680 */
4681 tline = thead;
4682 expanded = false;
4683 goto again;
4684 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004685 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004686
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004687err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004688 if (org_tline) {
4689 if (thead) {
4690 *org_tline = *thead;
4691 /* since we just gave text to org_line, don't free it */
4692 thead->text = NULL;
4693 delete_Token(thead);
4694 } else {
4695 /* the expression expanded to empty line;
4696 we can't return NULL for some reasons
4697 we just set the line to a single WHITESPACE token. */
4698 memset(org_tline, 0, sizeof(*org_tline));
4699 org_tline->text = NULL;
4700 org_tline->type = TOK_WHITESPACE;
4701 }
4702 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004703 }
4704
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004705 return thead;
4706}
4707
4708/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004709 * Similar to expand_smacro but used exclusively with macro identifiers
4710 * right before they are fetched in. The reason is that there can be
4711 * identifiers consisting of several subparts. We consider that if there
4712 * are more than one element forming the name, user wants a expansion,
4713 * otherwise it will be left as-is. Example:
4714 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004715 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004716 *
4717 * the identifier %$abc will be left as-is so that the handler for %define
4718 * will suck it and define the corresponding value. Other case:
4719 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004720 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004721 *
4722 * In this case user wants name to be expanded *before* %define starts
4723 * working, so we'll expand %$abc into something (if it has a value;
4724 * otherwise it will be left as-is) then concatenate all successive
4725 * PP_IDs into one.
4726 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004727static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004728{
4729 Token *cur, *oldnext = NULL;
4730
H. Peter Anvin734b1882002-04-30 21:01:08 +00004731 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004732 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004733
4734 cur = tline;
4735 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004736 (cur->next->type == TOK_ID ||
4737 cur->next->type == TOK_PREPROC_ID
4738 || cur->next->type == TOK_NUMBER))
4739 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004740
4741 /* If identifier consists of just one token, don't expand */
4742 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004743 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004744
H. Peter Anvine2c80182005-01-15 22:15:51 +00004745 if (cur) {
4746 oldnext = cur->next; /* Detach the tail past identifier */
4747 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004748 }
4749
H. Peter Anvin734b1882002-04-30 21:01:08 +00004750 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004751
H. Peter Anvine2c80182005-01-15 22:15:51 +00004752 if (cur) {
4753 /* expand_smacro possibly changhed tline; re-scan for EOL */
4754 cur = tline;
4755 while (cur && cur->next)
4756 cur = cur->next;
4757 if (cur)
4758 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004759 }
4760
4761 return tline;
4762}
4763
4764/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004765 * Determine whether the given line constitutes a multi-line macro
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004766 * call, and return the ExpDef structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004767 * to check for an initial label - that's taken care of in
4768 * expand_mmacro - but must check numbers of parameters. Guaranteed
4769 * to be called with tline->type == TOK_ID, so the putative macro
4770 * name is easy to find.
4771 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004772static ExpDef *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004773{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004774 ExpDef *head, *ed;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004775 Token **params;
4776 int nparam;
4777
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004778 head = (ExpDef *) hash_findix(&expdefs, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004779
4780 /*
4781 * Efficiency: first we see if any macro exists with the given
4782 * name. If not, we can return NULL immediately. _Then_ we
4783 * count the parameters, and then we look further along the
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004784 * list if necessary to find the proper ExpDef.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004785 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004786 list_for_each(ed, head)
4787 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004788 break;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004789 if (!ed)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004790 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004791
4792 /*
4793 * OK, we have a potential macro. Count and demarcate the
4794 * parameters.
4795 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004796 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004797
4798 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004799 * So we know how many parameters we've got. Find the ExpDef
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004800 * structure that handles this number.
4801 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004802 while (ed) {
4803 if (ed->nparam_min <= nparam
4804 && (ed->plus || nparam <= ed->nparam_max)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004805 /*
4806 * It's right, and we can use it. Add its default
4807 * parameters to the end of our list if necessary.
4808 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004809 if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004810 params =
4811 nasm_realloc(params,
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004812 ((ed->nparam_min + ed->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004813 1) * sizeof(*params)));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004814 while (nparam < ed->nparam_min + ed->ndefs) {
4815 params[nparam] = ed->defaults[nparam - ed->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004816 nparam++;
4817 }
4818 }
4819 /*
4820 * If we've gone over the maximum parameter count (and
4821 * we're in Plus mode), ignore parameters beyond
4822 * nparam_max.
4823 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004824 if (ed->plus && nparam > ed->nparam_max)
4825 nparam = ed->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004826 /*
4827 * Then terminate the parameter list, and leave.
4828 */
4829 if (!params) { /* need this special case */
4830 params = nasm_malloc(sizeof(*params));
4831 nparam = 0;
4832 }
4833 params[nparam] = NULL;
4834 *params_array = params;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004835 return ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004836 }
4837 /*
4838 * This one wasn't right: look for the next one with the
4839 * same name.
4840 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004841 list_for_each(ed, ed->next)
4842 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004843 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004844 }
4845
4846 /*
4847 * After all that, we didn't find one with the right number of
4848 * parameters. Issue a warning, and fail to expand the macro.
4849 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004850 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004851 "macro `%s' exists, but not taking %d parameters",
4852 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004853 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004854 return NULL;
4855}
4856
4857/*
4858 * Expand the multi-line macro call made by the given line, if
4859 * there is one to be expanded. If there is, push the expansion on
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004860 * istk->expansion and return true. Otherwise return false.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004861 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004862static bool expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004863{
H. Peter Anvineba20a72002-04-30 20:53:55 +00004864 Token *label = NULL;
4865 int dont_prepend = 0;
Cyrill Gorcunovfb27fc22011-06-25 12:08:30 +04004866 Token **params, *t;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004867 Line *l = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004868 ExpDef *ed;
4869 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004870 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004871 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004872
4873 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004874 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004875 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004876 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004877 return false;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004878 ed = is_mmacro(t, &params);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004879 if (ed != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004880 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004881 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004882 Token *last;
4883 /*
4884 * We have an id which isn't a macro call. We'll assume
4885 * it might be a label; we'll also check to see if a
4886 * colon follows it. Then, if there's another id after
4887 * that lot, we'll check it again for macro-hood.
4888 */
4889 label = last = t;
4890 t = t->next;
4891 if (tok_type_(t, TOK_WHITESPACE))
4892 last = t, t = t->next;
4893 if (tok_is_(t, ":")) {
4894 dont_prepend = 1;
4895 last = t, t = t->next;
4896 if (tok_type_(t, TOK_WHITESPACE))
4897 last = t, t = t->next;
4898 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004899 if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, &params)))
4900 return false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004901 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004902 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004903 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004904 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004905
4906 /*
4907 * Fix up the parameters: this involves stripping leading and
4908 * trailing whitespace, then stripping braces if they are
4909 * present.
4910 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004911 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004912 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004913
H. Peter Anvine2c80182005-01-15 22:15:51 +00004914 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004915 int brace = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004916 int comma = (!ed->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004917
H. Peter Anvine2c80182005-01-15 22:15:51 +00004918 t = params[i];
4919 skip_white_(t);
4920 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004921 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004922 params[i] = t;
4923 paramlen[i] = 0;
4924 while (t) {
4925 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4926 break; /* ... because we have hit a comma */
4927 if (comma && t->type == TOK_WHITESPACE
4928 && tok_is_(t->next, ","))
4929 break; /* ... or a space then a comma */
4930 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4931 break; /* ... or a brace */
4932 t = t->next;
4933 paramlen[i]++;
4934 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004935 }
4936
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004937 if (ed->cur_depth >= ed->max_depth) {
4938 if (ed->max_depth > 1) {
4939 error(ERR_WARNING,
4940 "reached maximum macro recursion depth of %i for %s",
4941 ed->max_depth,ed->name);
4942 }
4943 return false;
4944 } else {
4945 ed->cur_depth ++;
4946 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004947
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004948 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004949 * OK, we have found a ExpDef structure representing a
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004950 * previously defined mmacro. Create an expansion invocation
4951 * and point it back to the expansion definition. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004952 * parameter tokens and macro-local tokens doesn't get done
4953 * until the single-line macro substitution process; this is
4954 * because delaying them allows us to change the semantics
4955 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004956 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004957 ei = new_ExpInv(EXP_MMACRO, ed);
4958 ei->name = nasm_strdup(mname);
4959 //ei->label = label;
4960 //ei->label_text = detoken(label, false);
4961 ei->current = ed->line;
4962 ei->emitting = true;
4963 //ei->iline = tline;
4964 ei->params = params;
4965 ei->nparam = nparam;
4966 ei->rotate = 0;
4967 ei->paramlen = paramlen;
4968 ei->lineno = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004969
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004970 ei->prev = istk->expansion;
4971 istk->expansion = ei;
4972
4973 /*
4974 * Special case: detect %00 on first invocation; if found,
4975 * avoid emitting any labels that precede the mmacro call.
4976 * ed->prepend is set to -1 when %00 is detected, else 1.
4977 */
4978 if (ed->prepend == 0) {
4979 for (l = ed->line; l != NULL; l = l->next) {
4980 for (t = l->first; t != NULL; t = t->next) {
4981 if ((t->type == TOK_PREPROC_ID) &&
4982 (strlen(t->text) == 3) &&
4983 (t->text[1] == '0') && (t->text[2] == '0')) {
4984 dont_prepend = -1;
4985 break;
4986 }
4987 }
4988 if (dont_prepend < 0) {
4989 break;
4990 }
4991 }
4992 ed->prepend = ((dont_prepend < 0) ? -1 : 1);
4993 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004994
4995 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004996 * If we had a label, push it on as the first line of
4997 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004998 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004999 if (label != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005000 if (ed->prepend < 0) {
5001 ei->label_text = detoken(label, false);
5002 } else {
5003 if (dont_prepend == 0) {
5004 t = label;
5005 while (t->next != NULL) {
5006 t = t->next;
5007 }
5008 t->next = new_Token(NULL, TOK_OTHER, ":", 0);
5009 }
5010 l = new_Line();
5011 l->first = copy_Token(label);
5012 l->next = ei->current;
5013 ei->current = l;
5014 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005015 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005016
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005017 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005018
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005019 istk->mmac_depth++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005020 return true;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005021}
5022
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005023/* The function that actually does the error reporting */
5024static void verror(int severity, const char *fmt, va_list arg)
5025{
5026 char buff[1024];
5027
5028 vsnprintf(buff, sizeof(buff), fmt, arg);
5029
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005030 if (istk && istk->mmac_depth > 0) {
5031 ExpInv *ei = istk->expansion;
5032 int lineno = ei->lineno;
5033 while (ei) {
5034 if (ei->type == EXP_MMACRO)
5035 break;
5036 lineno += ei->relno;
5037 ei = ei->prev;
5038 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005039 nasm_error(severity, "(%s:%d) %s", ei->def->name,
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005040 lineno, buff);
5041 } else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005042 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005043}
5044
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005045/*
5046 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07005047 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005048 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005049static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005050{
5051 va_list arg;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005052 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005053 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00005054 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005055}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005056
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005057/*
5058 * Because %else etc are evaluated in the state context
5059 * of the previous branch, errors might get lost with error():
5060 * %if 0 ... %else trailing garbage ... %endif
5061 * So %else etc should report errors with this function.
5062 */
5063static void error_precond(int severity, const char *fmt, ...)
5064{
5065 va_list arg;
5066
5067 /* Only ignore the error if it's really in a dead branch */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005068 if ((istk != NULL) &&
5069 (istk->expansion != NULL) &&
5070 (istk->expansion->type == EXP_IF) &&
5071 (istk->expansion->def->state == COND_NEVER))
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005072 return;
5073
5074 va_start(arg, fmt);
5075 verror(severity, fmt, arg);
5076 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005077}
5078
H. Peter Anvin734b1882002-04-30 21:01:08 +00005079static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005080pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005081{
H. Peter Anvin7383b402008-09-24 10:20:40 -07005082 Token *t;
5083
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005084 cstk = NULL;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005085 istk = nasm_zalloc(sizeof(Include));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005086 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00005087 src_set_fname(nasm_strdup(file));
5088 src_set_linnum(0);
5089 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005090 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07005091 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00005092 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005093 defining = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005094 finals = NULL;
5095 in_final = false;
Charles Crayned4200be2008-07-12 16:42:33 -07005096 nested_mac_count = 0;
5097 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005098 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005099 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005100 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005101 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005102 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005103 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005104 }
H. Peter Anvind2456592008-06-19 15:04:18 -07005105 any_extrastdmac = extrastdmac && *extrastdmac;
5106 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005107 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005108
5109 /*
5110 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5111 * The caller, however, will also pass in 3 for preprocess-only so
5112 * we can set __PASS__ accordingly.
5113 */
5114 pass = apass > 2 ? 2 : apass;
5115
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005116 dephead = deptail = deplist;
5117 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005118 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
5119 sl->next = NULL;
5120 strcpy(sl->str, file);
5121 *deptail = sl;
5122 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005123 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07005124
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005125 /*
5126 * Define the __PASS__ macro. This is defined here unlike
5127 * all the other builtins, because it is special -- it varies between
5128 * passes.
5129 */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005130 t = nasm_zalloc(sizeof(*t));
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005131 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07005132 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005133}
5134
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005135static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005136{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005137 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005138 Token *tline;
Keith Kanios9858cec2010-11-08 00:58:02 -06005139 ExpDef *ed;
5140 ExpInv *ei;
5141 Line *l;
5142 int j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005143
H. Peter Anvine2c80182005-01-15 22:15:51 +00005144 while (1) {
5145 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005146 * Fetch a tokenized line, either from the expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005147 * buffer or from the input file.
5148 */
5149 tline = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005150
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005151 while (1) { /* until we get a line we can use */
5152 /*
5153 * Fetch a tokenized line from the expansion buffer
5154 */
5155 if (istk->expansion != NULL) {
5156 ei = istk->expansion;
5157 if (ei->current != NULL) {
5158 if (ei->emitting == false) {
5159 ei->current = NULL;
5160 continue;
5161 }
5162 l = ei->current;
5163 ei->current = l->next;
5164 ei->lineno++;
5165 tline = copy_Token(l->first);
5166 if (((ei->type == EXP_REP) ||
5167 (ei->type == EXP_MMACRO) ||
5168 (ei->type == EXP_WHILE))
5169 && (ei->def->nolist == false)) {
5170 char *p = detoken(tline, false);
5171 list->line(LIST_MACRO, p);
5172 nasm_free(p);
5173 }
5174 if (ei->linnum > -1) {
5175 src_set_linnum(src_get_linnum() + 1);
5176 }
5177 break;
5178 } else if ((ei->type == EXP_REP) &&
5179 (ei->def->cur_depth < ei->def->max_depth)) {
5180 ei->def->cur_depth ++;
5181 ei->current = ei->def->line;
5182 ei->lineno = 0;
5183 continue;
5184 } else if ((ei->type == EXP_WHILE) &&
5185 (ei->def->cur_depth < ei->def->max_depth)) {
5186 ei->current = ei->def->line;
5187 ei->lineno = 0;
5188 tline = copy_Token(ei->current->first);
5189 j = if_condition(tline, PP_WHILE);
5190 tline = NULL;
5191 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
5192 if (j == COND_IF_TRUE) {
5193 ei->current = ei->current->next;
5194 ei->def->cur_depth ++;
5195 } else {
5196 ei->emitting = false;
5197 ei->current = NULL;
5198 ei->def->cur_depth = ei->def->max_depth;
5199 }
5200 continue;
5201 } else {
5202 istk->expansion = ei->prev;
5203 ed = ei->def;
5204 if (ed != NULL) {
5205 if ((ei->emitting == true) &&
5206 (ed->max_depth == DEADMAN_LIMIT) &&
5207 (ed->cur_depth == DEADMAN_LIMIT)
5208 ) {
5209 error(ERR_FATAL, "runaway expansion detected, aborting");
5210 }
5211 if (ed->cur_depth > 0) {
5212 ed->cur_depth --;
Keith Kanios94124652010-12-18 11:47:28 -06005213 } else if (ed->type != EXP_MMACRO) {
Keith Kanios6a7c3e92010-12-18 11:49:53 -06005214 expansions = ed->prev;
5215 free_expdef(ed);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005216 }
5217 if ((ei->type == EXP_REP) ||
5218 (ei->type == EXP_MMACRO) ||
5219 (ei->type == EXP_WHILE)) {
5220 list->downlevel(LIST_MACRO);
5221 if (ei->type == EXP_MMACRO) {
5222 istk->mmac_depth--;
5223 }
5224 }
5225 }
5226 if (ei->linnum > -1) {
5227 src_set_linnum(ei->linnum);
5228 }
5229 free_expinv(ei);
5230 continue;
5231 }
5232 }
5233
5234 /*
5235 * Read in line from input and tokenize
5236 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005237 line = read_line();
5238 if (line) { /* from the current input file */
5239 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005240 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005241 nasm_free(line);
5242 break;
5243 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005244
H. Peter Anvine2c80182005-01-15 22:15:51 +00005245 /*
5246 * The current file has ended; work down the istk
5247 */
5248 {
5249 Include *i = istk;
5250 fclose(i->fp);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005251 if (i->expansion != NULL) {
5252 error(ERR_FATAL,
5253 "end of file while still in an expansion");
5254 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005255 /* only set line and file name if there's a next node */
5256 if (i->next) {
5257 src_set_linnum(i->lineno);
Cyrill Gorcunovd34a1082011-03-07 11:23:08 +03005258 nasm_free(src_set_fname(nasm_strdup(i->fname)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005259 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005260 if ((i->next == NULL) && (finals != NULL)) {
5261 in_final = true;
5262 ei = new_ExpInv(EXP_FINAL, NULL);
5263 ei->emitting = true;
5264 ei->current = finals;
5265 istk->expansion = ei;
5266 finals = NULL;
5267 continue;
5268 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005269 istk = i->next;
5270 list->downlevel(LIST_INCLUDE);
5271 nasm_free(i);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005272 if (istk == NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005273 if (finals != NULL) {
5274 in_final = true;
5275 } else {
5276 return NULL;
5277 }
5278 }
5279 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005280 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005281 }
5282
5283 if (defining == NULL) {
5284 tline = expand_mmac_params(tline);
5285 }
5286
H. Peter Anvine2c80182005-01-15 22:15:51 +00005287 /*
5288 * Check the line to see if it's a preprocessor directive.
5289 */
5290 if (do_directive(tline) == DIRECTIVE_FOUND) {
5291 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005292 } else if (defining != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005293 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005294 * We're defining an expansion. We emit nothing at all,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005295 * and just shove the tokenized line on to the definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005296 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005297 if (defining->ignoring == false) {
5298 Line *l = new_Line();
5299 l->first = tline;
5300 if (defining->line == NULL) {
5301 defining->line = l;
5302 defining->last = l;
5303 } else {
5304 defining->last->next = l;
5305 defining->last = l;
5306 }
5307 } else {
Keith Kanios104803d2010-12-18 11:05:46 -06005308 free_tlist(tline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005309 }
5310 defining->linecount++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005311 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005312 } else if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005313 (istk->expansion->emitting != true)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005314 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005315 * We're in a non-emitting branch of an expansion.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005316 * Emit nothing at all, not even a blank line: when we
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005317 * emerge from the expansion we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005318 * directive so we keep our place correctly.
5319 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005320 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005321 continue;
5322 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005323 tline = expand_smacro(tline);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005324 if (expand_mmacro(tline) != true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005325 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005326 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005327 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005328 line = detoken(tline, true);
5329 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005330 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005331 } else {
5332 continue;
5333 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005334 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005335 }
5336 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005337}
5338
H. Peter Anvine2c80182005-01-15 22:15:51 +00005339static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005340{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005341 if (defining != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005342 error(ERR_NONFATAL, "end of file while still defining an expansion");
5343 while (defining != NULL) {
5344 ExpDef *ed = defining;
5345 defining = ed->prev;
5346 free_expdef(ed);
5347 }
5348 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005349 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005350 while (cstk != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005351 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005352 free_macros();
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005353 while (istk != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005354 Include *i = istk;
5355 istk = istk->next;
5356 fclose(i->fp);
5357 nasm_free(i->fname);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005358 while (i->expansion != NULL) {
5359 ExpInv *ei = i->expansion;
5360 i->expansion = ei->prev;
5361 free_expinv(ei);
5362 }
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005363 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005364 }
5365 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005366 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005367 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005368 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005369 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005370 free_llist(predef);
5371 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005372 while ((i = ipath)) {
5373 ipath = i->next;
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04005374 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005375 nasm_free(i);
5376 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005377 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005378}
5379
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005380void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005381{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005382 IncPath *i = nasm_zalloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005383
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005384 if (path)
5385 i->path = nasm_strdup(path);
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005386
H. Peter Anvin89cee572009-07-15 09:16:54 -04005387 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005388 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005389 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005390 j = j->next;
5391 j->next = i;
5392 } else {
5393 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005394 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005395}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005396
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005397void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005398{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005399 Token *inc, *space, *name;
5400 Line *l;
5401
H. Peter Anvin734b1882002-04-30 21:01:08 +00005402 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5403 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5404 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005405
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005406 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005407 l->next = predef;
5408 l->first = inc;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005409 predef = l;
5410}
5411
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005412void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005413{
5414 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005415 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005416 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005417
5418 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005419 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5420 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005421 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005422 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005423 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005424 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005425 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005426
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005427 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005428 l->next = predef;
5429 l->first = def;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005430 predef = l;
5431}
5432
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005433void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005434{
5435 Token *def, *space;
5436 Line *l;
5437
H. Peter Anvin734b1882002-04-30 21:01:08 +00005438 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5439 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005440 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005441
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005442 l = new_Line();
H. Peter Anvin620515a2002-04-30 20:57:38 +00005443 l->next = predef;
5444 l->first = def;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005445 predef = l;
5446}
5447
Keith Kaniosb7a89542007-04-12 02:40:54 +00005448/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005449 * This function is used to assist with "runtime" preprocessor
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005450 * directives, e.g. pp_runtime("%define __BITS__ 64");
Keith Kaniosb7a89542007-04-12 02:40:54 +00005451 *
5452 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5453 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5454 */
5455
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005456void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005457{
5458 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005459
Keith Kaniosb7a89542007-04-12 02:40:54 +00005460 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005461 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005462 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005463
Keith Kaniosb7a89542007-04-12 02:40:54 +00005464}
5465
H. Peter Anvina70547f2008-07-19 21:44:26 -07005466void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005467{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005468 extrastdmac = macros;
5469}
5470
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005471static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005472{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005473 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005474 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005475 tok->text = nasm_strdup(numbuf);
5476 tok->type = TOK_NUMBER;
5477}
5478
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005479Preproc nasmpp = {
5480 pp_reset,
5481 pp_getline,
5482 pp_cleanup
5483};