blob: 4abddbf1e012b1c8573e604194e14fe34198313d [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
3 * Copyright 1996-2010 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000065#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000066#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000067#include <stdlib.h>
68#include <stddef.h>
69#include <string.h>
70#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000071#include <limits.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000072#include <inttypes.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000073
74#include "nasm.h"
75#include "nasmlib.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000076#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070077#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070078#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070079#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070080#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070081#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070082#include "tables.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000083
84typedef struct SMacro SMacro;
Keith Kaniosb307a4f2010-11-06 17:41:51 -050085typedef struct ExpDef ExpDef;
86typedef struct ExpInv ExpInv;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000087typedef struct Context Context;
88typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000089typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000090typedef struct Line Line;
91typedef struct Include Include;
92typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000093typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000094
95/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070096 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
102 */
103
104/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 * Store the definition of a single-line macro.
106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107struct SMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000108 SMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000109 char *name;
H. Peter Anvin70055962007-10-11 00:05:31 -0700110 bool casesense;
H. Peter Anvin16ed4382007-10-11 10:06:19 -0700111 bool in_progress;
H. Peter Anvin70055962007-10-11 00:05:31 -0700112 unsigned int nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000113 Token *expansion;
114};
115
116/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000117 * The context stack is composed of a linked list of these.
118 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000119struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000120 Context *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000121 char *name;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700122 struct hash_table localmac;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000123 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000124};
125
126/*
127 * This is the internal form which we break input lines up into.
128 * Typically stored in linked lists.
129 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000130 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
131 * necessarily used as-is, but is intended to denote the number of
132 * the substituted parameter. So in the definition
133 *
134 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700135 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000136 * the token representing `x' will have its type changed to
137 * TOK_SMAC_PARAM, but the one representing `y' will be
138 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000139 *
140 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
141 * which doesn't need quotes around it. Used in the pre-include
142 * mechanism as an alternative to trying to find a sensible type of
143 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000144 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000145enum pp_token_type {
146 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
147 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700148 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
149 TOK_INTERNAL_STRING,
150 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300151 TOK_PASTE, /* %+ */
152 TOK_INDIRECT, /* %[...] */
153 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
154 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000155};
156
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400157#define PP_CONCAT_MASK(x) (1 << (x))
158
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400159struct tokseq_match {
160 int mask_head;
161 int mask_tail;
162};
163
H. Peter Anvine2c80182005-01-15 22:15:51 +0000164struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000165 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000166 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700167 union {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300168 SMacro *mac; /* associated macro for TOK_SMAC_END */
169 size_t len; /* scratch length field */
170 } a; /* Auxiliary data */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000171 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000172};
173
174/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500175 * Expansion definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000176 * these, which is essentially a container to allow several linked
177 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700178 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000179 * Note that in this module, linked lists are treated as stacks
180 * wherever possible. For this reason, Lines are _pushed_ on to the
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500181 * `last' field in ExpDef structures, so that the linked list,
182 * if walked, would emit the expansion lines in the proper order.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000183 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000184struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000185 Line *next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000186 Token *first;
187};
188
189/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500190 * Expansion Types
191 */
192enum pp_exp_type {
193 EXP_NONE = 0, EXP_PREDEF,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300194 EXP_MMACRO, EXP_REP,
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500195 EXP_IF, EXP_WHILE,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300196 EXP_COMMENT, EXP_FINAL,
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500197 EXP_MAX = INT_MAX /* Keep compiler from reducing the range */
198};
199
200/*
201 * Store the definition of an expansion, in which is any
202 * preprocessor directive that has an ending pair.
203 *
204 * This design allows for arbitrary expansion/recursion depth,
205 * upto the DEADMAN_LIMIT.
206 *
207 * The `next' field is used for storing ExpDef in hash tables; the
208 * `prev' field is for the global `expansions` linked-list.
209 */
210struct ExpDef {
211 ExpDef *prev; /* previous definition */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300212 ExpDef *next; /* next in hash table */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500213 enum pp_exp_type type; /* expansion type */
214 char *name; /* definition name */
215 int nparam_min, nparam_max;
216 bool casesense;
217 bool plus; /* is the last parameter greedy? */
218 bool nolist; /* is this expansion listing-inhibited? */
219 Token *dlist; /* all defaults as one list */
220 Token **defaults; /* parameter default pointers */
221 int ndefs; /* number of default parameters */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300222
223 int prepend; /* label prepend state */
224 Line *label;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500225 Line *line;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300226 Line *last;
227 int linecount; /* number of lines within expansion */
228
229 int64_t def_depth; /* current number of definition pairs deep */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500230 int64_t cur_depth; /* current number of expansions */
231 int64_t max_depth; /* maximum number of expansions allowed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300232
233 int state; /* condition state */
234 bool ignoring; /* ignoring definition lines */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500235};
236
237/*
238 * Store the invocation of an expansion.
239 *
240 * The `prev' field is for the `istk->expansion` linked-list.
241 *
242 * When an expansion is being expanded, `params', `iline', `nparam',
243 * `paramlen', `rotate' and `unique' are local to the invocation.
244 */
245struct ExpInv {
246 ExpInv *prev; /* previous invocation */
247 enum pp_exp_type type; /* expansion type */
248 ExpDef *def; /* pointer to expansion definition */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300249 char *name; /* invocation name */
250 Line *label; /* pointer to label */
251 char *label_text; /* pointer to label text */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500252 Line *current; /* pointer to current line in invocation */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300253
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500254 Token **params; /* actual parameters */
255 Token *iline; /* invocation line */
256 unsigned int nparam, rotate;
257 int *paramlen;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300258
259 uint64_t unique;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500260 bool emitting;
261 int lineno; /* current line number in expansion */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300262 int linnum; /* line number at invocation */
263 int relno; /* relative line number at invocation */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500264};
265
266/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000267 * To handle an arbitrary level of file inclusion, we maintain a
268 * stack (ie linked list) of these things.
269 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000270struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000271 Include *next;
272 FILE *fp;
273 Cond *conds;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500274 ExpInv *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000275 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000276 int lineno, lineinc;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300277 int mmac_depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000278};
279
280/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000281 * Include search path. This is simply a list of strings which get
282 * prepended, in turn, to the name of an include file, in an
283 * attempt to find the file if it's not in the current directory.
284 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000285struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000286 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000287 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000288};
289
290/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000291 * Conditional assembly: we maintain a separate stack of these for
292 * each level of file inclusion. (The only reason we keep the
293 * stacks separate is to ensure that a stray `%endif' in a file
294 * included from within the true branch of a `%if' won't terminate
295 * it and cause confusion: instead, rightly, it'll cause an error.)
296 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000297enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000298 /*
299 * These states are for use just after %if or %elif: IF_TRUE
300 * means the condition has evaluated to truth so we are
301 * currently emitting, whereas IF_FALSE means we are not
302 * currently emitting but will start doing so if a %else comes
303 * up. In these states, all directives are admissible: %elif,
304 * %else and %endif. (And of course %if.)
305 */
306 COND_IF_TRUE, COND_IF_FALSE,
307 /*
308 * These states come up after a %else: ELSE_TRUE means we're
309 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
310 * any %elif or %else will cause an error.
311 */
312 COND_ELSE_TRUE, COND_ELSE_FALSE,
313 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200314 * These states mean that we're not emitting now, and also that
315 * nothing until %endif will be emitted at all. COND_DONE is
316 * used when we've had our moment of emission
317 * and have now started seeing %elifs. COND_NEVER is used when
318 * the condition construct in question is contained within a
319 * non-emitting branch of a larger condition construct,
320 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000321 */
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200322 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000323};
324#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
325
H. Peter Anvin70653092007-10-19 14:42:29 -0700326/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000327 * These defines are used as the possible return values for do_directive
328 */
329#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300330#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000331
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000332/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500333 * This define sets the upper limit for smacro and expansions
Keith Kanios852f1ee2009-07-12 00:19:55 -0500334 */
335#define DEADMAN_LIMIT (1 << 20)
336
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400337/* max reps */
338#define REP_LIMIT ((INT64_C(1) << 62))
339
Keith Kanios852f1ee2009-07-12 00:19:55 -0500340/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341 * Condition codes. Note that we use c_ prefix not C_ because C_ is
342 * used in nasm.h for the "real" condition codes. At _this_ level,
343 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
344 * ones, so we need a different enum...
345 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700346static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000347 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
348 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000349 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000350};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700351enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000352 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
353 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
H. Peter Anvin476d2862007-10-02 22:04:15 -0700354 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
355 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000356};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700357static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000358 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
359 c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
H. Peter Anvince9be342007-09-12 00:22:29 +0000360 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000361};
362
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000363/* For TASM compatibility we need to be able to recognise TASM compatible
364 * conditional compilation directives. Using the NASM pre-processor does
365 * not work, so we look for them specifically from the following list and
366 * then jam in the equivalent NASM directive into the input stream.
367 */
368
H. Peter Anvine2c80182005-01-15 22:15:51 +0000369enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000370 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
371 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
372};
373
H. Peter Anvin476d2862007-10-02 22:04:15 -0700374static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000375 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
376 "ifndef", "include", "local"
377};
378
379static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000380static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000381static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800382static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000383
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000384static Context *cstk;
385static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000386static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000387
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300388static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700389static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000390
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300391static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000392
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000393static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700394static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000395
396static ListGen *list;
397
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000398/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500399 * The current set of expansion definitions we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000400 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500401static struct hash_table expdefs;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000402
403/*
404 * The current set of single-line macros we have defined.
405 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700406static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000407
408/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500409 * Linked List of all active expansion definitions
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000410 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500411struct ExpDef *expansions = NULL;
412
413/*
414 * The expansion we are currently defining
415 */
416static ExpDef *defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000417
Charles Crayned4200be2008-07-12 16:42:33 -0700418static uint64_t nested_mac_count;
419static uint64_t nested_rep_count;
420
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000421/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500422 * Linked-list of lines to preprocess, prior to cleanup
423 */
424static Line *finals = NULL;
425static bool in_final = false;
426
427/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000428 * The number of macro parameters to allocate space for at a time.
429 */
430#define PARAM_DELTA 16
431
432/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700433 * The standard macro set: defined in macros.c in the array nasm_stdmac.
434 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000435 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700436static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000437
438/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000439 * The extra standard macros that come from the object format, if
440 * any.
441 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700442static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700443static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000444
445/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000446 * Tokens are allocated in blocks to improve speed
447 */
448#define TOKEN_BLOCKSIZE 4096
449static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000450struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000451 Blocks *next;
452 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000453};
454
455static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000456
457/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000458 * Forward declarations.
459 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000460static Token *expand_mmac_params(Token * tline);
461static Token *expand_smacro(Token * tline);
462static Token *expand_id(Token * tline);
H. Peter Anvinf8ad5322009-02-21 17:55:08 -0800463static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300464 bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700465static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000466static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200467static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000468static void *new_Block(size_t size);
469static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700470static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300471 const char *text, int txtlen);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500472static Token *copy_Token(Token * tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000473static Token *delete_Token(Token * t);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500474static Line *new_Line(void);
475static ExpDef *new_ExpDef(int exp_type);
476static ExpInv *new_ExpInv(int exp_type, ExpDef *ed);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000477
478/*
479 * Macros for safe checking of token pointers, avoid *(NULL)
480 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300481#define tok_type_(x,t) ((x) && (x)->type == (t))
482#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
483#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
484#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000485
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300486/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700487 * nasm_unquote with error if the string contains NUL characters.
488 * If the string contains NUL characters, issue an error and return
489 * the C len, i.e. truncate at the NUL.
490 */
491static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
492{
493 size_t len = nasm_unquote(qstr, NULL);
494 size_t clen = strlen(qstr);
495
496 if (len != clen)
497 error(ERR_NONFATAL, "NUL character in `%s' directive",
498 pp_directives[directive]);
499
500 return clen;
501}
502
503/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700504 * In-place reverse a list of tokens.
505 */
506static Token *reverse_tokens(Token *t)
507{
508 Token *prev = NULL;
509 Token *next;
510
511 while (t) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500512 next = t->next;
513 t->next = prev;
514 prev = t;
515 t = next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300516 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700517
518 return prev;
519}
520
521/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300522 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000523 * front of them. We do it here because I could not find any other
524 * place to do it for the moment, and it is a hack (ideally it would
525 * be nice to be able to use the NASM pre-processor to do it).
526 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000527static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000528{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000529 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400530 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000531
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400532 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000533
534 /* Binary search for the directive name */
535 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400536 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400537 q = nasm_skip_word(p);
538 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000539 if (len) {
540 oldchar = p[len];
541 p[len] = 0;
542 while (j - i > 1) {
543 k = (j + i) / 2;
544 m = nasm_stricmp(p, tasm_directives[k]);
545 if (m == 0) {
546 /* We have found a directive, so jam a % in front of it
547 * so that NASM will then recognise it as one if it's own.
548 */
549 p[len] = oldchar;
550 len = strlen(p);
551 oldline = line;
552 line = nasm_malloc(len + 2);
553 line[0] = '%';
554 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700555 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300556 * NASM does not recognise IFDIFI, so we convert
557 * it to %if 0. This is not used in NASM
558 * compatible code, but does need to parse for the
559 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000560 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700561 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000562 } else {
563 memcpy(line + 1, p, len + 1);
564 }
565 nasm_free(oldline);
566 return line;
567 } else if (m < 0) {
568 j = k;
569 } else
570 i = k;
571 }
572 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000573 }
574 return line;
575}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000576
H. Peter Anvin76690a12002-04-30 20:52:49 +0000577/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000578 * The pre-preprocessing stage... This function translates line
579 * number indications as they emerge from GNU cpp (`# lineno "file"
580 * flags') into NASM preprocessor line number indications (`%line
581 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000582 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000583static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000584{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000585 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000586 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000587
H. Peter Anvine2c80182005-01-15 22:15:51 +0000588 if (line[0] == '#' && line[1] == ' ') {
589 oldline = line;
590 fname = oldline + 2;
591 lineno = atoi(fname);
592 fname += strspn(fname, "0123456789 ");
593 if (*fname == '"')
594 fname++;
595 fnlen = strcspn(fname, "\"");
596 line = nasm_malloc(20 + fnlen);
597 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
598 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000599 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000600 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000601 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000602 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000603}
604
605/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000606 * Free a linked list of tokens.
607 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000608static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000609{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400610 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000611 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000612}
613
614/*
615 * Free a linked list of lines.
616 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000617static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000618{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400619 Line *l, *tmp;
620 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000621 free_tlist(l->first);
622 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000623 }
624}
625
626/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500627 * Free an ExpDef
H. Peter Anvineba20a72002-04-30 20:53:55 +0000628 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500629static void free_expdef(ExpDef * ed)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000630{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500631 nasm_free(ed->name);
632 free_tlist(ed->dlist);
633 nasm_free(ed->defaults);
634 free_llist(ed->line);
635 nasm_free(ed);
636}
637
638/*
639 * Free an ExpInv
640 */
641static void free_expinv(ExpInv * ei)
642{
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300643 if (ei->name != NULL)
644 nasm_free(ei->name);
645 if (ei->label_text != NULL)
646 nasm_free(ei->label_text);
647 nasm_free(ei);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000648}
649
650/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700651 * Free all currently defined macros, and free the hash tables
652 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700653static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700654{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400655 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700656 const char *key;
657 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700658
H. Peter Anvin072771e2008-05-22 13:17:51 -0700659 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300660 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400661 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300662 nasm_free(s->name);
663 free_tlist(s->expansion);
664 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300665 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700666 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700667 hash_free(smt);
668}
669
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500670static void free_expdef_table(struct hash_table *edt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700671{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500672 ExpDef *ed, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700673 const char *key;
674 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700675
676 it = NULL;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500677 while ((ed = hash_iterate(edt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300678 nasm_free((void *)key);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500679 list_for_each_safe(ed ,tmp, ed)
680 free_expdef(ed);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700681 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500682 hash_free(edt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700683}
684
685static void free_macros(void)
686{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700687 free_smacro_table(&smacros);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500688 free_expdef_table(&expdefs);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700689}
690
691/*
692 * Initialize the hash tables
693 */
694static void init_macros(void)
695{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700696 hash_init(&smacros, HASH_LARGE);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500697 hash_init(&expdefs, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700698}
699
700/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000701 * Pop the context stack.
702 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000703static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000704{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000705 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000706
707 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700708 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000709 nasm_free(c->name);
710 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000711}
712
H. Peter Anvin072771e2008-05-22 13:17:51 -0700713/*
714 * Search for a key in the hash index; adding it if necessary
715 * (in which case we initialize the data pointer to NULL.)
716 */
717static void **
718hash_findi_add(struct hash_table *hash, const char *str)
719{
720 struct hash_insert hi;
721 void **r;
722 char *strx;
723
724 r = hash_findi(hash, str, &hi);
725 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300726 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700727
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300728 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700729 return hash_add(&hi, strx, NULL);
730}
731
732/*
733 * Like hash_findi, but returns the data element rather than a pointer
734 * to it. Used only when not adding a new element, hence no third
735 * argument.
736 */
737static void *
738hash_findix(struct hash_table *hash, const char *str)
739{
740 void **p;
741
742 p = hash_findi(hash, str, NULL);
743 return p ? *p : NULL;
744}
745
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400746/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500747 * read line from standard macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400748 * if there no more left -- return NULL
749 */
750static char *line_from_stdmac(void)
751{
752 unsigned char c;
753 const unsigned char *p = stdmacpos;
754 char *line, *q;
755 size_t len = 0;
756
757 if (!stdmacpos)
758 return NULL;
759
760 while ((c = *p++)) {
761 if (c >= 0x80)
762 len += pp_directives_len[c - 0x80] + 1;
763 else
764 len++;
765 }
766
767 line = nasm_malloc(len + 1);
768 q = line;
769 while ((c = *stdmacpos++)) {
770 if (c >= 0x80) {
771 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
772 q += pp_directives_len[c - 0x80];
773 *q++ = ' ';
774 } else {
775 *q++ = c;
776 }
777 }
778 stdmacpos = p;
779 *q = '\0';
780
781 if (!*stdmacpos) {
782 /* This was the last of the standard macro chain... */
783 stdmacpos = NULL;
784 if (any_extrastdmac) {
785 stdmacpos = extrastdmac;
786 any_extrastdmac = false;
787 } else if (do_predef) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300788 ExpInv *ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400789 Line *pd, *l;
790 Token *head, **tail, *t;
791
792 /*
793 * Nasty hack: here we push the contents of
794 * `predef' on to the top-level expansion stack,
795 * since this is the most convenient way to
796 * implement the pre-include and pre-define
797 * features.
798 */
799 list_for_each(pd, predef) {
800 head = NULL;
801 tail = &head;
802 list_for_each(t, pd->first) {
803 *tail = new_Token(NULL, t->type, t->text, 0);
804 tail = &(*tail)->next;
805 }
806
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500807 l = new_Line();
808 l->first = head;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300809 ei = new_ExpInv(EXP_PREDEF, NULL);
810 ei->current = l;
811 ei->emitting = true;
812 ei->prev = istk->expansion;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500813 istk->expansion = ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400814 }
815 do_predef = false;
816 }
817 }
818
819 return line;
820}
821
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000822#define BUF_DELTA 512
823/*
824 * Read a line from the top file in istk, handling multiple CR/LFs
825 * at the end of the line read, and handling spurious ^Zs. Will
826 * return lines from the standard macro set if this has not already
827 * been done.
828 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000829static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000830{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000831 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000832 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000833
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400834 /*
835 * standart macros set (predefined) goes first
836 */
837 p = line_from_stdmac();
838 if (p)
839 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700840
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400841 /*
842 * regular read from a file
843 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000844 bufsize = BUF_DELTA;
845 buffer = nasm_malloc(BUF_DELTA);
846 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000847 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000848 while (1) {
849 q = fgets(p, bufsize - (p - buffer), istk->fp);
850 if (!q)
851 break;
852 p += strlen(p);
853 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300854 /*
855 * Convert backslash-CRLF line continuation sequences into
856 * nothing at all (for DOS and Windows)
857 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000858 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
859 p -= 3;
860 *p = 0;
861 continued_count++;
862 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300863 /*
864 * Also convert backslash-LF line continuation sequences into
865 * nothing at all (for Unix)
866 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000867 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
868 p -= 2;
869 *p = 0;
870 continued_count++;
871 } else {
872 break;
873 }
874 }
875 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000876 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000877 bufsize += BUF_DELTA;
878 buffer = nasm_realloc(buffer, bufsize);
879 p = buffer + offset; /* prevent stale-pointer problems */
880 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000881 }
882
H. Peter Anvine2c80182005-01-15 22:15:51 +0000883 if (!q && p == buffer) {
884 nasm_free(buffer);
885 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000886 }
887
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300888 src_set_linnum(src_get_linnum() + istk->lineinc +
889 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000890
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000891 /*
892 * Play safe: remove CRs as well as LFs, if any of either are
893 * present at the end of the line.
894 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000895 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000896 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000897
898 /*
899 * Handle spurious ^Z, which may be inserted into source files
900 * by some file transfer utilities.
901 */
902 buffer[strcspn(buffer, "\032")] = '\0';
903
H. Peter Anvin734b1882002-04-30 21:01:08 +0000904 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000905
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000906 return buffer;
907}
908
909/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000910 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000911 * don't need to parse the value out of e.g. numeric tokens: we
912 * simply split one string into many.
913 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000914static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000915{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700916 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000917 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000918 Token *list = NULL;
919 Token *t, **tail = &list;
920
H. Peter Anvine2c80182005-01-15 22:15:51 +0000921 while (*line) {
922 p = line;
923 if (*p == '%') {
924 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300925 if (*p == '+' && !nasm_isdigit(p[1])) {
926 p++;
927 type = TOK_PASTE;
928 } else if (nasm_isdigit(*p) ||
929 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000930 do {
931 p++;
932 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700933 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 type = TOK_PREPROC_ID;
935 } else if (*p == '{') {
936 p++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500937 while (*p && *p != '}') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000938 p[-1] = *p;
939 p++;
940 }
941 p[-1] = '\0';
942 if (*p)
943 p++;
944 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300945 } else if (*p == '[') {
946 int lvl = 1;
947 line += 2; /* Skip the leading %[ */
948 p++;
949 while (lvl && (c = *p++)) {
950 switch (c) {
951 case ']':
952 lvl--;
953 break;
954 case '%':
955 if (*p == '[')
956 lvl++;
957 break;
958 case '\'':
959 case '\"':
960 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400961 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300962 break;
963 default:
964 break;
965 }
966 }
967 p--;
968 if (*p)
969 *p++ = '\0';
970 if (lvl)
971 error(ERR_NONFATAL, "unterminated %[ construct");
972 type = TOK_INDIRECT;
973 } else if (*p == '?') {
974 type = TOK_PREPROC_Q; /* %? */
975 p++;
976 if (*p == '?') {
977 type = TOK_PREPROC_QQ; /* %?? */
978 p++;
979 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +0400980 } else if (*p == '!') {
981 type = TOK_PREPROC_ID;
982 p++;
983 if (isidchar(*p)) {
984 do {
985 p++;
986 } while (isidchar(*p));
987 } else if (*p == '\'' || *p == '\"' || *p == '`') {
988 p = nasm_skip_string(p);
989 if (*p)
990 p++;
991 else
992 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
993 } else {
994 /* %! without string or identifier */
995 type = TOK_OTHER; /* Legacy behavior... */
996 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 } else if (isidchar(*p) ||
998 ((*p == '!' || *p == '%' || *p == '$') &&
999 isidchar(p[1]))) {
1000 do {
1001 p++;
1002 }
1003 while (isidchar(*p));
1004 type = TOK_PREPROC_ID;
1005 } else {
1006 type = TOK_OTHER;
1007 if (*p == '%')
1008 p++;
1009 }
1010 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1011 type = TOK_ID;
1012 p++;
1013 while (*p && isidchar(*p))
1014 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001015 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 /*
1017 * A string token.
1018 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001019 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001020 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001021
H. Peter Anvine2c80182005-01-15 22:15:51 +00001022 if (*p) {
1023 p++;
1024 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001025 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001026 /* Handling unterminated strings by UNV */
1027 /* type = -1; */
1028 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001029 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001030 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001031 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001032 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001033 bool is_hex = false;
1034 bool is_float = false;
1035 bool has_e = false;
1036 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001037
H. Peter Anvine2c80182005-01-15 22:15:51 +00001038 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001039 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001040 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001041
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001042 if (*p == '$') {
1043 p++;
1044 is_hex = true;
1045 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001046
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001047 for (;;) {
1048 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001049
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001050 if (!is_hex && (c == 'e' || c == 'E')) {
1051 has_e = true;
1052 if (*p == '+' || *p == '-') {
1053 /*
1054 * e can only be followed by +/- if it is either a
1055 * prefixed hex number or a floating-point number
1056 */
1057 p++;
1058 is_float = true;
1059 }
1060 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1061 is_hex = true;
1062 } else if (c == 'P' || c == 'p') {
1063 is_float = true;
1064 if (*p == '+' || *p == '-')
1065 p++;
1066 } else if (isnumchar(c) || c == '_')
1067 ; /* just advance */
1068 else if (c == '.') {
1069 /*
1070 * we need to deal with consequences of the legacy
1071 * parser, like "1.nolist" being two tokens
1072 * (TOK_NUMBER, TOK_ID) here; at least give it
1073 * a shot for now. In the future, we probably need
1074 * a flex-based scanner with proper pattern matching
1075 * to do it as well as it can be done. Nothing in
1076 * the world is going to help the person who wants
1077 * 0x123.p16 interpreted as two tokens, though.
1078 */
1079 r = p;
1080 while (*r == '_')
1081 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001082
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001083 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1084 (!is_hex && (*r == 'e' || *r == 'E')) ||
1085 (*r == 'p' || *r == 'P')) {
1086 p = r;
1087 is_float = true;
1088 } else
1089 break; /* Terminate the token */
1090 } else
1091 break;
1092 }
1093 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001094
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001095 if (p == line+1 && *line == '$') {
1096 type = TOK_OTHER; /* TOKEN_HERE */
1097 } else {
1098 if (has_e && !is_hex) {
1099 /* 1e13 is floating-point, but 1e13h is not */
1100 is_float = true;
1101 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001102
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001103 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1104 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001105 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001106 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001107 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001108 /*
1109 * Whitespace just before end-of-line is discarded by
1110 * pretending it's a comment; whitespace just before a
1111 * comment gets lumped into the comment.
1112 */
1113 if (!*p || *p == ';') {
1114 type = TOK_COMMENT;
1115 while (*p)
1116 p++;
1117 }
1118 } else if (*p == ';') {
1119 type = TOK_COMMENT;
1120 while (*p)
1121 p++;
1122 } else {
1123 /*
1124 * Anything else is an operator of some kind. We check
1125 * for all the double-character operators (>>, <<, //,
1126 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001127 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128 */
1129 type = TOK_OTHER;
1130 if ((p[0] == '>' && p[1] == '>') ||
1131 (p[0] == '<' && p[1] == '<') ||
1132 (p[0] == '/' && p[1] == '/') ||
1133 (p[0] == '<' && p[1] == '=') ||
1134 (p[0] == '>' && p[1] == '=') ||
1135 (p[0] == '=' && p[1] == '=') ||
1136 (p[0] == '!' && p[1] == '=') ||
1137 (p[0] == '<' && p[1] == '>') ||
1138 (p[0] == '&' && p[1] == '&') ||
1139 (p[0] == '|' && p[1] == '|') ||
1140 (p[0] == '^' && p[1] == '^')) {
1141 p++;
1142 }
1143 p++;
1144 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001145
H. Peter Anvine2c80182005-01-15 22:15:51 +00001146 /* Handling unterminated string by UNV */
1147 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001148 {
1149 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1150 t->text[p-line] = *line;
1151 tail = &t->next;
1152 }
1153 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001154 if (type != TOK_COMMENT) {
1155 *tail = t = new_Token(NULL, type, line, p - line);
1156 tail = &t->next;
1157 }
1158 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001159 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001160 return list;
1161}
1162
H. Peter Anvince616072002-04-30 21:02:23 +00001163/*
1164 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001165 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001166 * deleted only all at once by the delete_Blocks function.
1167 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001168static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001169{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001170 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001171
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001172 /* first, get to the end of the linked list */
1173 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001174 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001175 /* now allocate the requested chunk */
1176 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001178 /* now allocate a new block for the next request */
1179 b->next = nasm_malloc(sizeof(Blocks));
1180 /* and initialize the contents of the new block */
1181 b->next->next = NULL;
1182 b->next->chunk = NULL;
1183 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001184}
1185
1186/*
1187 * this function deletes all managed blocks of memory
1188 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001189static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001190{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001191 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001192
H. Peter Anvin70653092007-10-19 14:42:29 -07001193 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001194 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001195 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001196 * free it.
1197 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198 while (b) {
1199 if (b->chunk)
1200 nasm_free(b->chunk);
1201 a = b;
1202 b = b->next;
1203 if (a != &blocks)
1204 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001205 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001206}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001207
1208/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001209 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001210 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001211 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001212 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001213static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001214 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001215{
1216 Token *t;
1217 int i;
1218
H. Peter Anvin89cee572009-07-15 09:16:54 -04001219 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001220 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1221 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1222 freeTokens[i].next = &freeTokens[i + 1];
1223 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001224 }
1225 t = freeTokens;
1226 freeTokens = t->next;
1227 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001228 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001229 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001230 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001231 t->text = NULL;
1232 } else {
1233 if (txtlen == 0)
1234 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001235 t->text = nasm_malloc(txtlen+1);
1236 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001237 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001238 }
1239 return t;
1240}
1241
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001242static Token *copy_Token(Token * tline)
1243{
1244 Token *t, *tt, *first = NULL, *prev = NULL;
1245 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001246 for (tt = tline; tt != NULL; tt = tt->next) {
1247 if (!freeTokens) {
1248 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1249 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1250 freeTokens[i].next = &freeTokens[i + 1];
1251 freeTokens[i].next = NULL;
1252 }
1253 t = freeTokens;
1254 freeTokens = t->next;
1255 t->next = NULL;
1256 t->text = tt->text ? nasm_strdup(tt->text) : NULL;
1257 t->a.mac = tt->a.mac;
1258 t->a.len = tt->a.len;
1259 t->type = tt->type;
1260 if (prev != NULL) {
1261 prev->next = t;
1262 } else {
1263 first = t;
1264 }
1265 prev = t;
1266 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001267 return first;
1268}
1269
H. Peter Anvine2c80182005-01-15 22:15:51 +00001270static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001271{
1272 Token *next = t->next;
1273 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001274 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001275 freeTokens = t;
1276 return next;
1277}
1278
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001279/*
1280 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001281 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1282 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001283 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001284static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001285{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001286 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001287 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001288 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001289 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001290
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001291 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001293 char *v;
1294 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001295
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001296 v = t->text + 2;
1297 if (*v == '\'' || *v == '\"' || *v == '`') {
1298 size_t len = nasm_unquote(v, NULL);
1299 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001300
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001301 if (len != clen) {
1302 error(ERR_NONFATAL | ERR_PASS1,
1303 "NUL character in %! string");
1304 v = NULL;
1305 }
1306 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001307
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001308 if (v) {
1309 char *p = getenv(v);
1310 if (!p) {
1311 error(ERR_NONFATAL | ERR_PASS1,
1312 "nonexistent environment variable `%s'", v);
1313 p = "";
1314 }
1315 t->text = nasm_strdup(p);
1316 }
1317 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001318 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001319
H. Peter Anvine2c80182005-01-15 22:15:51 +00001320 /* Expand local macros here and not during preprocessing */
1321 if (expand_locals &&
1322 t->type == TOK_PREPROC_ID && t->text &&
1323 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001324 const char *q;
1325 char *p;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001326 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001328 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001329 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001330 p = nasm_strcat(buffer, q);
1331 nasm_free(t->text);
1332 t->text = p;
1333 }
1334 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001335
1336 /* Expand %? and %?? directives */
1337 if (expand_locals && (istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001338 ((t->type == TOK_PREPROC_Q) ||
1339 (t->type == TOK_PREPROC_QQ))) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001340 ExpInv *ei;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001341 for (ei = istk->expansion; ei != NULL; ei = ei->prev){
1342 if (ei->type == EXP_MMACRO) {
1343 nasm_free(t->text);
1344 if (t->type == TOK_PREPROC_Q) {
1345 t->text = nasm_strdup(ei->name);
1346 } else {
1347 t->text = nasm_strdup(ei->def->name);
1348 }
1349 break;
1350 }
1351 }
1352 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001353
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001354 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001355 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001356 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001357 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001358 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001359
H. Peter Anvin734b1882002-04-30 21:01:08 +00001360 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001361
1362 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001363 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001364 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001366 q = t->text;
1367 while (*q)
1368 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001369 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001370 }
1371 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001372
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001373 return line;
1374}
1375
1376/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001377 * Initialize a new Line
1378 */
Cyrill Gorcunov29cb0bb2010-11-11 11:19:43 +03001379static inline Line *new_Line(void)
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001380{
Cyrill Gorcunov29cb0bb2010-11-11 11:19:43 +03001381 return (Line *)nasm_zalloc(sizeof(Line));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001382}
1383
1384
1385/*
1386 * Initialize a new Expansion Definition
1387 */
1388static ExpDef *new_ExpDef(int exp_type)
1389{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001390 ExpDef *ed = (ExpDef*)nasm_zalloc(sizeof(ExpDef));
1391 ed->type = exp_type;
1392 ed->casesense = true;
1393 ed->state = COND_NEVER;
1394
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001395 return ed;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001396}
1397
1398
1399/*
1400 * Initialize a new Expansion Instance
1401 */
1402static ExpInv *new_ExpInv(int exp_type, ExpDef *ed)
1403{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001404 ExpInv *ei = (ExpInv*)nasm_zalloc(sizeof(ExpInv));
1405 ei->type = exp_type;
1406 ei->def = ed;
1407 ei->unique = ++unique;
1408
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001409 if ((istk->mmac_depth < 1) &&
1410 (istk->expansion == NULL) &&
1411 (ed != NULL) &&
1412 (ed->type != EXP_MMACRO) &&
1413 (ed->type != EXP_REP) &&
1414 (ed->type != EXP_WHILE)) {
1415 ei->linnum = src_get_linnum();
1416 src_set_linnum(ei->linnum - ed->linecount - 1);
1417 } else {
1418 ei->linnum = -1;
1419 }
1420 if ((istk->expansion == NULL) ||
1421 (ei->type == EXP_MMACRO)) {
1422 ei->relno = 0;
1423 } else {
1424 ei->relno = istk->expansion->lineno;
1425 if (ed != NULL) {
1426 ei->relno -= (ed->linecount + 1);
1427 }
1428 }
1429 return ei;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001430}
1431
1432/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001433 * A scanner, suitable for use by the expression evaluator, which
1434 * operates on a line of Tokens. Expects a pointer to a pointer to
1435 * the first token in the line to be passed in as its private_data
1436 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001437 *
1438 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001439 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001440static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001441{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001442 Token **tlineptr = private_data;
1443 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001444 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001445
H. Peter Anvine2c80182005-01-15 22:15:51 +00001446 do {
1447 tline = *tlineptr;
1448 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001449 } while (tline && (tline->type == TOK_WHITESPACE ||
1450 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001451
1452 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001453 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001454
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001455 tokval->t_charptr = tline->text;
1456
H. Peter Anvin76690a12002-04-30 20:52:49 +00001457 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001458 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001459 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001460 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001461
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001463 p = tokval->t_charptr = tline->text;
1464 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001465 tokval->t_charptr++;
1466 return tokval->t_type = TOKEN_ID;
1467 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001468
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001469 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001470 if (r >= p+MAX_KEYWORD)
1471 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001472 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001473 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001474 *s = '\0';
1475 /* right, so we have an identifier sitting in temp storage. now,
1476 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001477 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001478 }
1479
H. Peter Anvine2c80182005-01-15 22:15:51 +00001480 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001481 bool rn_error;
1482 tokval->t_integer = readnum(tline->text, &rn_error);
1483 tokval->t_charptr = tline->text;
1484 if (rn_error)
1485 return tokval->t_type = TOKEN_ERRNUM;
1486 else
1487 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001488 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001489
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001490 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001491 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001492 }
1493
H. Peter Anvine2c80182005-01-15 22:15:51 +00001494 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001495 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001496
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001497 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001498 tokval->t_charptr = tline->text;
1499 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001500
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001501 if (ep[0] != bq || ep[1] != '\0')
1502 return tokval->t_type = TOKEN_ERRSTR;
1503 else
1504 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001505 }
1506
H. Peter Anvine2c80182005-01-15 22:15:51 +00001507 if (tline->type == TOK_OTHER) {
1508 if (!strcmp(tline->text, "<<"))
1509 return tokval->t_type = TOKEN_SHL;
1510 if (!strcmp(tline->text, ">>"))
1511 return tokval->t_type = TOKEN_SHR;
1512 if (!strcmp(tline->text, "//"))
1513 return tokval->t_type = TOKEN_SDIV;
1514 if (!strcmp(tline->text, "%%"))
1515 return tokval->t_type = TOKEN_SMOD;
1516 if (!strcmp(tline->text, "=="))
1517 return tokval->t_type = TOKEN_EQ;
1518 if (!strcmp(tline->text, "<>"))
1519 return tokval->t_type = TOKEN_NE;
1520 if (!strcmp(tline->text, "!="))
1521 return tokval->t_type = TOKEN_NE;
1522 if (!strcmp(tline->text, "<="))
1523 return tokval->t_type = TOKEN_LE;
1524 if (!strcmp(tline->text, ">="))
1525 return tokval->t_type = TOKEN_GE;
1526 if (!strcmp(tline->text, "&&"))
1527 return tokval->t_type = TOKEN_DBL_AND;
1528 if (!strcmp(tline->text, "^^"))
1529 return tokval->t_type = TOKEN_DBL_XOR;
1530 if (!strcmp(tline->text, "||"))
1531 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001532 }
1533
1534 /*
1535 * We have no other options: just return the first character of
1536 * the token text.
1537 */
1538 return tokval->t_type = tline->text[0];
1539}
1540
1541/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001542 * Compare a string to the name of an existing macro; this is a
1543 * simple wrapper which calls either strcmp or nasm_stricmp
1544 * depending on the value of the `casesense' parameter.
1545 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001546static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001547{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001548 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001549}
1550
1551/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001552 * Compare a string to the name of an existing macro; this is a
1553 * simple wrapper which calls either strcmp or nasm_stricmp
1554 * depending on the value of the `casesense' parameter.
1555 */
1556static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1557{
1558 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1559}
1560
1561/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001562 * Return the Context structure associated with a %$ token. Return
1563 * NULL, having _already_ reported an error condition, if the
1564 * context stack isn't deep enough for the supplied number of $
1565 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001566 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001567 * also scanned for such smacro, until it is found; if not -
1568 * only the context that directly results from the number of $'s
1569 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001570 *
1571 * If "namep" is non-NULL, set it to the pointer to the macro name
1572 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001573 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001574static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001575 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001576{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001577 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001578 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001579 int i;
1580
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001581 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001582 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001583
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001584 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001585 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001586
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 if (!cstk) {
1588 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1589 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001590 }
1591
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001592 name += 2;
1593 ctx = cstk;
1594 i = 0;
1595 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001596 name++;
1597 i++;
1598 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001599 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001600 if (!ctx) {
1601 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001602 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001603 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001604 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001605
1606 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001607 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001608
Keith Kanios404589e2010-08-10 20:12:57 -05001609 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001610 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001611
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001612 do {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001613 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001614 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 while (m) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001616 if (!mstrcmp(m->name, name, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 return ctx;
1618 m = m->next;
1619 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001620 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001621 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001622 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001623 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001624}
1625
1626/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001627 * Check to see if a file is already in a string list
1628 */
1629static bool in_list(const StrList *list, const char *str)
1630{
1631 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001632 if (!strcmp(list->str, str))
1633 return true;
1634 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001635 }
1636 return false;
1637}
1638
1639/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001640 * Open an include file. This routine must always return a valid
1641 * file pointer if it returns - it's responsible for throwing an
1642 * ERR_FATAL and bombing out completely if not. It should also try
1643 * the include path one by one until it finds the file or reaches
1644 * the end of the path.
1645 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001646static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001647 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001648{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001649 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001650 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001651 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001652 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001653 size_t prefix_len = 0;
1654 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001655
H. Peter Anvine2c80182005-01-15 22:15:51 +00001656 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001657 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunov6f38fe62010-11-11 11:32:16 +03001658 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001659 memcpy(sl->str, prefix, prefix_len);
1660 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001661 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001662 if (fp && dhead && !in_list(*dhead, sl->str)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001663 **dtail = sl;
1664 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001665 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001666 nasm_free(sl);
1667 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001668 if (fp)
1669 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001670 if (!ip) {
1671 if (!missing_ok)
1672 break;
1673 prefix = NULL;
1674 } else {
1675 prefix = ip->path;
1676 ip = ip->next;
1677 }
1678 if (prefix) {
1679 prefix_len = strlen(prefix);
1680 } else {
1681 /* -MG given and file not found */
1682 if (dhead && !in_list(*dhead, file)) {
1683 sl = nasm_malloc(len+1+sizeof sl->next);
1684 sl->next = NULL;
1685 strcpy(sl->str, file);
1686 **dtail = sl;
1687 *dtail = &sl->next;
1688 }
1689 return NULL;
1690 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001691 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001692
H. Peter Anvin734b1882002-04-30 21:01:08 +00001693 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001694 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001695}
1696
1697/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001698 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001699 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001700 * return true if _any_ single-line macro of that name is defined.
1701 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001702 * `nparam' or no parameters is defined.
1703 *
1704 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001705 * defined, or nparam is -1, the address of the definition structure
1706 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001707 * is NULL, no action will be taken regarding its contents, and no
1708 * error will occur.
1709 *
1710 * Note that this is also called with nparam zero to resolve
1711 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001712 *
1713 * If you already know which context macro belongs to, you can pass
1714 * the context pointer as first parameter; if you won't but name begins
1715 * with %$ the context will be automatically computed. If all_contexts
1716 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001717 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001718static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001719smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001720 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001721{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001722 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001723 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001724
H. Peter Anvin97a23472007-09-16 17:57:25 -07001725 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001726 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001727 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001728 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001729 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001730 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001731 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001732 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001733 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001734 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001735 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001736 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001737
H. Peter Anvine2c80182005-01-15 22:15:51 +00001738 while (m) {
1739 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001740 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001741 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001742 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001743 *defn = m;
1744 else
1745 *defn = NULL;
1746 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001747 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001748 }
1749 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001750 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001751
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001752 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001753}
1754
1755/*
1756 * Count and mark off the parameters in a multi-line macro call.
1757 * This is called both from within the multi-line macro expansion
1758 * code, and also to mark off the default parameters when provided
1759 * in a %macro definition line.
1760 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001761static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001762{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001763 int paramsize, brace;
1764
1765 *nparam = paramsize = 0;
1766 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001767 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001768 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001769 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001770 paramsize += PARAM_DELTA;
1771 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1772 }
1773 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001774 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001775 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001776 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001777 (*params)[(*nparam)++] = t;
1778 while (tok_isnt_(t, brace ? "}" : ","))
1779 t = t->next;
1780 if (t) { /* got a comma/brace */
1781 t = t->next;
1782 if (brace) {
1783 /*
1784 * Now we've found the closing brace, look further
1785 * for the comma.
1786 */
1787 skip_white_(t);
1788 if (tok_isnt_(t, ",")) {
1789 error(ERR_NONFATAL,
1790 "braces do not enclose all of macro parameter");
1791 while (tok_isnt_(t, ","))
1792 t = t->next;
1793 }
1794 if (t)
1795 t = t->next; /* eat the comma */
1796 }
1797 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001798 }
1799}
1800
1801/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001802 * Determine whether one of the various `if' conditions is true or
1803 * not.
1804 *
1805 * We must free the tline we get passed.
1806 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001807static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001808{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001809 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001810 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001811 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001812 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001813 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001814 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001815 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001816
1817 origline = tline;
1818
H. Peter Anvine2c80182005-01-15 22:15:51 +00001819 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001820 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001821 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001822 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001824 if (!tline)
1825 break;
1826 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001827 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001828 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001829 free_tlist(origline);
1830 return -1;
1831 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001832 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001833 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 tline = tline->next;
1835 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001836 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001837
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001838 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001839 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001840 while (tline) {
1841 skip_white_(tline);
1842 if (!tline || (tline->type != TOK_ID &&
1843 (tline->type != TOK_PREPROC_ID ||
1844 tline->text[1] != '$'))) {
1845 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001846 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001847 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001848 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001849 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001850 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001851 tline = tline->next;
1852 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001853 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001854
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001855 case PPC_IFENV:
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001856 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001857 j = false; /* have we matched yet? */
1858 while (tline) {
1859 skip_white_(tline);
1860 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001861 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001862 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001863 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001864 error(ERR_NONFATAL,
1865 "`%s' expects environment variable names",
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001866 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001867 goto fail;
1868 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001869 p = tline->text;
1870 if (tline->type == TOK_PREPROC_ID)
1871 p += 2; /* Skip leading %! */
1872 if (*p == '\'' || *p == '\"' || *p == '`')
1873 nasm_unquote_cstr(p, ct);
1874 if (getenv(p))
1875 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001876 tline = tline->next;
1877 }
1878 break;
1879
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001880 case PPC_IFIDN:
1881 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001882 tline = expand_smacro(tline);
1883 t = tt = tline;
1884 while (tok_isnt_(tt, ","))
1885 tt = tt->next;
1886 if (!tt) {
1887 error(ERR_NONFATAL,
1888 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001889 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001890 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001891 }
1892 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001893 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001894 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1895 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1896 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001897 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001898 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001899 }
1900 if (t->type == TOK_WHITESPACE) {
1901 t = t->next;
1902 continue;
1903 }
1904 if (tt->type == TOK_WHITESPACE) {
1905 tt = tt->next;
1906 continue;
1907 }
1908 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001909 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001910 break;
1911 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001912 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001913 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001914 size_t l1 = nasm_unquote(t->text, NULL);
1915 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001916
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001917 if (l1 != l2) {
1918 j = false;
1919 break;
1920 }
1921 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1922 j = false;
1923 break;
1924 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001925 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001926 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001927 break;
1928 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001929
H. Peter Anvine2c80182005-01-15 22:15:51 +00001930 t = t->next;
1931 tt = tt->next;
1932 }
1933 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001934 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001935 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001936
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001937 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001938 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001939 bool found = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001940 ExpDef searching, *ed;
H. Peter Anvin65747262002-05-07 00:10:05 +00001941
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001942 skip_white_(tline);
1943 tline = expand_id(tline);
1944 if (!tok_type_(tline, TOK_ID)) {
1945 error(ERR_NONFATAL,
1946 "`%s' expects a macro name", pp_directives[ct]);
1947 goto fail;
1948 }
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03001949 memset(&searching, 0, sizeof(searching));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001950 searching.name = nasm_strdup(tline->text);
1951 searching.casesense = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001952 searching.nparam_max = INT_MAX;
1953 tline = expand_smacro(tline->next);
1954 skip_white_(tline);
1955 if (!tline) {
1956 } else if (!tok_type_(tline, TOK_NUMBER)) {
1957 error(ERR_NONFATAL,
1958 "`%s' expects a parameter count or nothing",
1959 pp_directives[ct]);
1960 } else {
1961 searching.nparam_min = searching.nparam_max =
1962 readnum(tline->text, &j);
1963 if (j)
1964 error(ERR_NONFATAL,
1965 "unable to parse parameter count `%s'",
1966 tline->text);
1967 }
1968 if (tline && tok_is_(tline->next, "-")) {
1969 tline = tline->next->next;
1970 if (tok_is_(tline, "*"))
1971 searching.nparam_max = INT_MAX;
1972 else if (!tok_type_(tline, TOK_NUMBER))
1973 error(ERR_NONFATAL,
1974 "`%s' expects a parameter count after `-'",
1975 pp_directives[ct]);
1976 else {
1977 searching.nparam_max = readnum(tline->text, &j);
1978 if (j)
1979 error(ERR_NONFATAL,
1980 "unable to parse parameter count `%s'",
1981 tline->text);
1982 if (searching.nparam_min > searching.nparam_max)
1983 error(ERR_NONFATAL,
1984 "minimum parameter count exceeds maximum");
1985 }
1986 }
1987 if (tline && tok_is_(tline->next, "+")) {
1988 tline = tline->next;
1989 searching.plus = true;
1990 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001991 ed = (ExpDef *) hash_findix(&expdefs, searching.name);
1992 while (ed != NULL) {
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03001993 if (!strcmp(ed->name, searching.name) &&
1994 (ed->nparam_min <= searching.nparam_max || searching.plus) &&
1995 (searching.nparam_min <= ed->nparam_max || ed->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001996 found = true;
1997 break;
1998 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001999 ed = ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002000 }
2001 if (tline && tline->next)
2002 error(ERR_WARNING|ERR_PASS1,
2003 "trailing garbage after %%ifmacro ignored");
2004 nasm_free(searching.name);
2005 j = found;
2006 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002007 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002008
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002009 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002010 needtype = TOK_ID;
2011 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002012 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002013 needtype = TOK_NUMBER;
2014 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002015 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002016 needtype = TOK_STRING;
2017 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002018
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002019iftype:
2020 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002021
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002022 while (tok_type_(t, TOK_WHITESPACE) ||
2023 (needtype == TOK_NUMBER &&
2024 tok_type_(t, TOK_OTHER) &&
2025 (t->text[0] == '-' || t->text[0] == '+') &&
2026 !t->text[1]))
2027 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002028
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002029 j = tok_type_(t, needtype);
2030 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002031
2032 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002033 t = tline = expand_smacro(tline);
2034 while (tok_type_(t, TOK_WHITESPACE))
2035 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002036
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002037 j = false;
2038 if (t) {
2039 t = t->next; /* Skip the actual token */
2040 while (tok_type_(t, TOK_WHITESPACE))
2041 t = t->next;
2042 j = !t; /* Should be nothing left */
2043 }
2044 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002045
H. Peter Anvin134b9462008-02-16 17:01:40 -08002046 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002047 t = tline = expand_smacro(tline);
2048 while (tok_type_(t, TOK_WHITESPACE))
2049 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002050
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002051 j = !t; /* Should be empty */
2052 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002053
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002054 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002055 t = tline = expand_smacro(tline);
2056 tptr = &t;
2057 tokval.t_type = TOKEN_INVALID;
2058 evalresult = evaluate(ppscan, tptr, &tokval,
2059 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002060 if (!evalresult)
2061 return -1;
2062 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002063 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002064 "trailing garbage after expression ignored");
2065 if (!is_simple(evalresult)) {
2066 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002067 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002068 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002069 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002070 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002071 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002072
H. Peter Anvine2c80182005-01-15 22:15:51 +00002073 default:
2074 error(ERR_FATAL,
2075 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002076 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002077 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002078 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002079
2080 free_tlist(origline);
2081 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002082
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002083fail:
2084 free_tlist(origline);
2085 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002086}
2087
2088/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002089 * Common code for defining an smacro
2090 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002091static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002092 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002093{
2094 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002095 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002096
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002097 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002098 if (!smac) {
2099 error(ERR_WARNING|ERR_PASS1,
2100 "single-line macro `%s' defined both with and"
2101 " without parameters", mname);
2102 /*
2103 * Some instances of the old code considered this a failure,
2104 * some others didn't. What is the right thing to do here?
2105 */
2106 free_tlist(expansion);
2107 return false; /* Failure */
2108 } else {
2109 /*
2110 * We're redefining, so we have to take over an
2111 * existing SMacro structure. This means freeing
2112 * what was already in it.
2113 */
2114 nasm_free(smac->name);
2115 free_tlist(smac->expansion);
2116 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002117 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002118 smtbl = ctx ? &ctx->localmac : &smacros;
2119 smhead = (SMacro **) hash_findi_add(smtbl, mname);
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002120 smac = nasm_zalloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002121 smac->next = *smhead;
2122 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002123 }
2124 smac->name = nasm_strdup(mname);
2125 smac->casesense = casesense;
2126 smac->nparam = nparam;
2127 smac->expansion = expansion;
2128 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002129 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002130}
2131
2132/*
2133 * Undefine an smacro
2134 */
2135static void undef_smacro(Context *ctx, const char *mname)
2136{
2137 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002138 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002139
H. Peter Anvin166c2472008-05-28 12:28:58 -07002140 smtbl = ctx ? &ctx->localmac : &smacros;
2141 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002142
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002143 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002144 /*
2145 * We now have a macro name... go hunt for it.
2146 */
2147 sp = smhead;
2148 while ((s = *sp) != NULL) {
2149 if (!mstrcmp(s->name, mname, s->casesense)) {
2150 *sp = s->next;
2151 nasm_free(s->name);
2152 free_tlist(s->expansion);
2153 nasm_free(s);
2154 } else {
2155 sp = &s->next;
2156 }
2157 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002158 }
2159}
2160
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002161/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002162 * Parse a mmacro specification.
2163 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002164static bool parse_mmacro_spec(Token *tline, ExpDef *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002165{
2166 bool err;
2167
2168 tline = tline->next;
2169 skip_white_(tline);
2170 tline = expand_id(tline);
2171 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002172 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2173 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002174 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002175
H. Peter Anvina26433d2008-07-16 14:40:01 -07002176 def->name = nasm_strdup(tline->text);
2177 def->plus = false;
2178 def->nolist = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002179// def->in_progress = 0;
2180// def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002181 def->nparam_min = 0;
2182 def->nparam_max = 0;
2183
H. Peter Anvina26433d2008-07-16 14:40:01 -07002184 tline = expand_smacro(tline->next);
2185 skip_white_(tline);
2186 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002187 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002188 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002189 def->nparam_min = def->nparam_max =
2190 readnum(tline->text, &err);
2191 if (err)
2192 error(ERR_NONFATAL,
2193 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002194 }
2195 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002196 tline = tline->next->next;
2197 if (tok_is_(tline, "*")) {
2198 def->nparam_max = INT_MAX;
2199 } else if (!tok_type_(tline, TOK_NUMBER)) {
2200 error(ERR_NONFATAL,
2201 "`%s' expects a parameter count after `-'", directive);
2202 } else {
2203 def->nparam_max = readnum(tline->text, &err);
2204 if (err) {
2205 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2206 tline->text);
2207 }
2208 if (def->nparam_min > def->nparam_max) {
2209 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2210 }
2211 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002212 }
2213 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002214 tline = tline->next;
2215 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002216 }
2217 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002218 !nasm_stricmp(tline->next->text, ".nolist")) {
2219 tline = tline->next;
2220 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002221 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002222
H. Peter Anvina26433d2008-07-16 14:40:01 -07002223 /*
2224 * Handle default parameters.
2225 */
2226 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002227 def->dlist = tline->next;
2228 tline->next = NULL;
2229 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002230 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002231 def->dlist = NULL;
2232 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002233 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002234 def->line = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002235
H. Peter Anvin89cee572009-07-15 09:16:54 -04002236 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002237 !def->plus)
2238 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2239 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002240
H. Peter Anvina26433d2008-07-16 14:40:01 -07002241 return true;
2242}
2243
2244
2245/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002246 * Decode a size directive
2247 */
2248static int parse_size(const char *str) {
2249 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002250 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002251 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002252 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002253
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002254 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002255}
2256
Ed Beroset3ab3f412002-06-11 03:31:49 +00002257/**
2258 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002259 * Find out if a line contains a preprocessor directive, and deal
2260 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002261 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002262 * If a directive _is_ found, it is the responsibility of this routine
2263 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002264 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002265 * @param tline a pointer to the current tokeninzed line linked list
2266 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002267 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002268 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002269static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002270{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002271 enum preproc_token i;
2272 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002273 bool err;
2274 int nparam;
2275 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002276 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002277 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002278 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002279 char *p, *pp;
2280 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002281 Include *inc;
2282 Context *ctx;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002283 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002284 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002285 struct tokenval tokval;
2286 expr *evalresult;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002287 ExpDef *ed, *eed, **edhead;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002288 ExpInv *ei, *eei;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002289 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002290 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002291 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002292
2293 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002294
H. Peter Anvineba20a72002-04-30 20:53:55 +00002295 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002296 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002297 (tline->text[1] == '%' || tline->text[1] == '$'
2298 || tline->text[1] == '!'))
2299 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002300
H. Peter Anvin4169a472007-09-12 01:29:43 +00002301 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002302
H. Peter Anvin4169a472007-09-12 01:29:43 +00002303 switch (i) {
2304 case PP_INVALID:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002305 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002306 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2307 tline->text);
2308 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002309
H. Peter Anvine2c80182005-01-15 22:15:51 +00002310 case PP_STACKSIZE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002311 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002312 /* Directive to tell NASM what the default stack size is. The
2313 * default is for a 16-bit stack, and this can be overriden with
2314 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002315 */
2316 tline = tline->next;
2317 if (tline && tline->type == TOK_WHITESPACE)
2318 tline = tline->next;
2319 if (!tline || tline->type != TOK_ID) {
2320 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2321 free_tlist(origline);
2322 return DIRECTIVE_FOUND;
2323 }
2324 if (nasm_stricmp(tline->text, "flat") == 0) {
2325 /* All subsequent ARG directives are for a 32-bit stack */
2326 StackSize = 4;
2327 StackPointer = "ebp";
2328 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002329 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002330 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2331 /* All subsequent ARG directives are for a 64-bit stack */
2332 StackSize = 8;
2333 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002334 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002335 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002336 } else if (nasm_stricmp(tline->text, "large") == 0) {
2337 /* All subsequent ARG directives are for a 16-bit stack,
2338 * far function call.
2339 */
2340 StackSize = 2;
2341 StackPointer = "bp";
2342 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002343 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002344 } else if (nasm_stricmp(tline->text, "small") == 0) {
2345 /* All subsequent ARG directives are for a 16-bit stack,
2346 * far function call. We don't support near functions.
2347 */
2348 StackSize = 2;
2349 StackPointer = "bp";
2350 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002351 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002352 } else {
2353 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2354 free_tlist(origline);
2355 return DIRECTIVE_FOUND;
2356 }
2357 free_tlist(origline);
2358 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002359
H. Peter Anvine2c80182005-01-15 22:15:51 +00002360 case PP_ARG:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002361 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002362 /* TASM like ARG directive to define arguments to functions, in
2363 * the following form:
2364 *
2365 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2366 */
2367 offset = ArgOffset;
2368 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002369 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002370 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002371
H. Peter Anvine2c80182005-01-15 22:15:51 +00002372 /* Find the argument name */
2373 tline = tline->next;
2374 if (tline && tline->type == TOK_WHITESPACE)
2375 tline = tline->next;
2376 if (!tline || tline->type != TOK_ID) {
2377 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2378 free_tlist(origline);
2379 return DIRECTIVE_FOUND;
2380 }
2381 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002382
H. Peter Anvine2c80182005-01-15 22:15:51 +00002383 /* Find the argument size type */
2384 tline = tline->next;
2385 if (!tline || tline->type != TOK_OTHER
2386 || tline->text[0] != ':') {
2387 error(ERR_NONFATAL,
2388 "Syntax error processing `%%arg' directive");
2389 free_tlist(origline);
2390 return DIRECTIVE_FOUND;
2391 }
2392 tline = tline->next;
2393 if (!tline || tline->type != TOK_ID) {
2394 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2395 free_tlist(origline);
2396 return DIRECTIVE_FOUND;
2397 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002398
H. Peter Anvine2c80182005-01-15 22:15:51 +00002399 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002400 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002401 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002402 size = parse_size(tt->text);
2403 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002404 error(ERR_NONFATAL,
2405 "Invalid size type for `%%arg' missing directive");
2406 free_tlist(tt);
2407 free_tlist(origline);
2408 return DIRECTIVE_FOUND;
2409 }
2410 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002411
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002412 /* Round up to even stack slots */
2413 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002414
H. Peter Anvine2c80182005-01-15 22:15:51 +00002415 /* Now define the macro for the argument */
2416 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2417 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002418 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002419 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002420
H. Peter Anvine2c80182005-01-15 22:15:51 +00002421 /* Move to the next argument in the list */
2422 tline = tline->next;
2423 if (tline && tline->type == TOK_WHITESPACE)
2424 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002425 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002426 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002427 free_tlist(origline);
2428 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002429
H. Peter Anvine2c80182005-01-15 22:15:51 +00002430 case PP_LOCAL:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002431 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002432 /* TASM like LOCAL directive to define local variables for a
2433 * function, in the following form:
2434 *
2435 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2436 *
2437 * The '= LocalSize' at the end is ignored by NASM, but is
2438 * required by TASM to define the local parameter size (and used
2439 * by the TASM macro package).
2440 */
2441 offset = LocalOffset;
2442 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002443 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002444 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002445
H. Peter Anvine2c80182005-01-15 22:15:51 +00002446 /* Find the argument name */
2447 tline = tline->next;
2448 if (tline && tline->type == TOK_WHITESPACE)
2449 tline = tline->next;
2450 if (!tline || tline->type != TOK_ID) {
2451 error(ERR_NONFATAL,
2452 "`%%local' missing argument parameter");
2453 free_tlist(origline);
2454 return DIRECTIVE_FOUND;
2455 }
2456 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002457
H. Peter Anvine2c80182005-01-15 22:15:51 +00002458 /* Find the argument size type */
2459 tline = tline->next;
2460 if (!tline || tline->type != TOK_OTHER
2461 || tline->text[0] != ':') {
2462 error(ERR_NONFATAL,
2463 "Syntax error processing `%%local' directive");
2464 free_tlist(origline);
2465 return DIRECTIVE_FOUND;
2466 }
2467 tline = tline->next;
2468 if (!tline || tline->type != TOK_ID) {
2469 error(ERR_NONFATAL,
2470 "`%%local' missing size type parameter");
2471 free_tlist(origline);
2472 return DIRECTIVE_FOUND;
2473 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002474
H. Peter Anvine2c80182005-01-15 22:15:51 +00002475 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002476 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002477 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002478 size = parse_size(tt->text);
2479 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002480 error(ERR_NONFATAL,
2481 "Invalid size type for `%%local' missing directive");
2482 free_tlist(tt);
2483 free_tlist(origline);
2484 return DIRECTIVE_FOUND;
2485 }
2486 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002487
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002488 /* Round up to even stack slots */
2489 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002490
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002491 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002492
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002493 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002494 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2495 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002496 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002497
H. Peter Anvine2c80182005-01-15 22:15:51 +00002498 /* Now define the assign to setup the enter_c macro correctly */
2499 snprintf(directive, sizeof(directive),
2500 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002501 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002502
H. Peter Anvine2c80182005-01-15 22:15:51 +00002503 /* Move to the next argument in the list */
2504 tline = tline->next;
2505 if (tline && tline->type == TOK_WHITESPACE)
2506 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002507 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002508 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002509 free_tlist(origline);
2510 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002511
H. Peter Anvine2c80182005-01-15 22:15:51 +00002512 case PP_CLEAR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002513 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002514 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002515 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002516 "trailing garbage after `%%clear' ignored");
2517 free_macros();
2518 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002519 free_tlist(origline);
2520 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002521
H. Peter Anvin418ca702008-05-30 10:42:30 -07002522 case PP_DEPEND:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002523 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002524 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002525 skip_white_(t);
2526 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002527 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002528 error(ERR_NONFATAL, "`%%depend' expects a file name");
2529 free_tlist(origline);
2530 return DIRECTIVE_FOUND; /* but we did _something_ */
2531 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002532 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002533 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002534 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002535 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002536 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002537 nasm_unquote_cstr(p, i);
2538 if (dephead && !in_list(*dephead, p)) {
2539 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2540 sl->next = NULL;
2541 strcpy(sl->str, p);
2542 *deptail = sl;
2543 deptail = &sl->next;
2544 }
2545 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002546 return DIRECTIVE_FOUND;
2547
2548 case PP_INCLUDE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002549 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002550 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002551 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002552
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002553 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002554 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002555 error(ERR_NONFATAL, "`%%include' expects a file name");
2556 free_tlist(origline);
2557 return DIRECTIVE_FOUND; /* but we did _something_ */
2558 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002559 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002560 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002561 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002562 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002563 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002564 nasm_unquote_cstr(p, i);
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03002565 inc = nasm_zalloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002566 inc->next = istk;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002567 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002568 if (!inc->fp) {
2569 /* -MG given but file not found */
2570 nasm_free(inc);
2571 } else {
2572 inc->fname = src_set_fname(nasm_strdup(p));
2573 inc->lineno = src_set_linnum(0);
2574 inc->lineinc = 1;
2575 inc->expansion = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002576 istk = inc;
2577 list->uplevel(LIST_INCLUDE);
2578 }
2579 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002580 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002581
H. Peter Anvind2456592008-06-19 15:04:18 -07002582 case PP_USE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002583 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002584 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002585 static macros_t *use_pkg;
2586 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002587
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002588 tline = tline->next;
2589 skip_white_(tline);
2590 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002591
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002592 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002593 tline->type != TOK_INTERNAL_STRING &&
2594 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002595 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002596 free_tlist(origline);
2597 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002598 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002599 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002600 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002601 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002602 if (tline->type == TOK_STRING)
2603 nasm_unquote_cstr(tline->text, i);
2604 use_pkg = nasm_stdmac_find_package(tline->text);
2605 if (!use_pkg)
2606 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2607 else
2608 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002609 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002610 /* Not already included, go ahead and include it */
2611 stdmacpos = use_pkg;
2612 }
2613 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002614 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002615 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002616 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002617 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002618 case PP_POP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002619 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002620 tline = tline->next;
2621 skip_white_(tline);
2622 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002623 if (tline) {
2624 if (!tok_type_(tline, TOK_ID)) {
2625 error(ERR_NONFATAL, "`%s' expects a context identifier",
2626 pp_directives[i]);
2627 free_tlist(origline);
2628 return DIRECTIVE_FOUND; /* but we did _something_ */
2629 }
2630 if (tline->next)
2631 error(ERR_WARNING|ERR_PASS1,
2632 "trailing garbage after `%s' ignored",
2633 pp_directives[i]);
2634 p = nasm_strdup(tline->text);
2635 } else {
2636 p = NULL; /* Anonymous */
2637 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002638
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002639 if (i == PP_PUSH) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002640 ctx = nasm_zalloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002641 ctx->next = cstk;
2642 hash_init(&ctx->localmac, HASH_SMALL);
2643 ctx->name = p;
2644 ctx->number = unique++;
2645 cstk = ctx;
2646 } else {
2647 /* %pop or %repl */
2648 if (!cstk) {
2649 error(ERR_NONFATAL, "`%s': context stack is empty",
2650 pp_directives[i]);
2651 } else if (i == PP_POP) {
2652 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2653 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2654 "expected %s",
2655 cstk->name ? cstk->name : "anonymous", p);
2656 else
2657 ctx_pop();
2658 } else {
2659 /* i == PP_REPL */
2660 nasm_free(cstk->name);
2661 cstk->name = p;
2662 p = NULL;
2663 }
2664 nasm_free(p);
2665 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002666 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002667 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002668 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002669 severity = ERR_FATAL;
2670 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002671 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002672 severity = ERR_NONFATAL;
2673 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002674 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002675 severity = ERR_WARNING|ERR_WARN_USER;
2676 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002677
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002678issue_error:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002679 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002680 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002681 /* Only error out if this is the final pass */
2682 if (pass != 2 && i != PP_FATAL)
2683 return DIRECTIVE_FOUND;
2684
2685 tline->next = expand_smacro(tline->next);
2686 tline = tline->next;
2687 skip_white_(tline);
2688 t = tline ? tline->next : NULL;
2689 skip_white_(t);
2690 if (tok_type_(tline, TOK_STRING) && !t) {
2691 /* The line contains only a quoted string */
2692 p = tline->text;
2693 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2694 error(severity, "%s", p);
2695 } else {
2696 /* Not a quoted string, or more than a quoted string */
2697 p = detoken(tline, false);
2698 error(severity, "%s", p);
2699 nasm_free(p);
2700 }
2701 free_tlist(origline);
2702 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002703 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002704
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002705 CASE_PP_IF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002706 if (defining != NULL) {
2707 if (defining->type == EXP_IF) {
2708 defining->def_depth ++;
2709 }
2710 return NO_DIRECTIVE_FOUND;
2711 }
2712 if ((istk->expansion != NULL) &&
2713 (istk->expansion->emitting == false)) {
2714 j = COND_NEVER;
2715 } else {
2716 j = if_condition(tline->next, i);
2717 tline->next = NULL; /* it got freed */
2718 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
2719 }
2720 ed = new_ExpDef(EXP_IF);
2721 ed->state = j;
2722 ed->nolist = NULL;
2723 ed->def_depth = 0;
2724 ed->cur_depth = 0;
2725 ed->max_depth = 0;
2726 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
2727 ed->prev = defining;
2728 defining = ed;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002729 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002730 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002731
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002732 CASE_PP_ELIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002733 if (defining != NULL) {
2734 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2735 return NO_DIRECTIVE_FOUND;
2736 }
2737 }
2738 if ((defining == NULL) || (defining->type != EXP_IF)) {
2739 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2740 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002741 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002742 case COND_IF_TRUE:
2743 defining->state = COND_DONE;
2744 defining->ignoring = true;
2745 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002746
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002747 case COND_DONE:
2748 case COND_NEVER:
2749 defining->ignoring = true;
2750 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002751
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002752 case COND_ELSE_TRUE:
2753 case COND_ELSE_FALSE:
2754 error_precond(ERR_WARNING|ERR_PASS1,
2755 "`%%elif' after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002756 defining->state = COND_NEVER;
2757 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002758 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002759
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002760 case COND_IF_FALSE:
2761 /*
2762 * IMPORTANT: In the case of %if, we will already have
2763 * called expand_mmac_params(); however, if we're
2764 * processing an %elif we must have been in a
2765 * non-emitting mode, which would have inhibited
2766 * the normal invocation of expand_mmac_params().
2767 * Therefore, we have to do it explicitly here.
2768 */
2769 j = if_condition(expand_mmac_params(tline->next), i);
2770 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002771 defining->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002772 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002773 defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002774 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002775 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002776 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002777 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002778
H. Peter Anvine2c80182005-01-15 22:15:51 +00002779 case PP_ELSE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002780 if (defining != NULL) {
2781 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2782 return NO_DIRECTIVE_FOUND;
2783 }
2784 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002785 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002786 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002787 "trailing garbage after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002788 if ((defining == NULL) || (defining->type != EXP_IF)) {
2789 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2790 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002791 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002792 case COND_IF_TRUE:
2793 case COND_DONE:
2794 defining->state = COND_ELSE_FALSE;
2795 defining->ignoring = true;
2796 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002797
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002798 case COND_NEVER:
2799 defining->ignoring = true;
2800 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002801
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002802 case COND_IF_FALSE:
2803 defining->state = COND_ELSE_TRUE;
2804 defining->ignoring = false;
2805 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002806
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002807 case COND_ELSE_TRUE:
2808 case COND_ELSE_FALSE:
2809 error_precond(ERR_WARNING|ERR_PASS1,
2810 "`%%else' after `%%else' ignored.");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002811 defining->state = COND_NEVER;
2812 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002813 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002814 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002815 free_tlist(origline);
2816 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002817
H. Peter Anvine2c80182005-01-15 22:15:51 +00002818 case PP_ENDIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002819 if (defining != NULL) {
2820 if (defining->type == EXP_IF) {
2821 if (defining->def_depth > 0) {
2822 defining->def_depth --;
2823 return NO_DIRECTIVE_FOUND;
2824 }
2825 } else {
2826 return NO_DIRECTIVE_FOUND;
2827 }
2828 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002829 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002830 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002831 "trailing garbage after `%%endif' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002832 if ((defining == NULL) || (defining->type != EXP_IF)) {
2833 error(ERR_NONFATAL, "`%%endif': no matching `%%if'");
2834 return DIRECTIVE_FOUND;
2835 }
2836 ed = defining;
2837 defining = ed->prev;
2838 ed->prev = expansions;
2839 expansions = ed;
2840 ei = new_ExpInv(EXP_IF, ed);
2841 ei->current = ed->line;
2842 ei->emitting = true;
2843 ei->prev = istk->expansion;
2844 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002845 free_tlist(origline);
2846 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002847
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002848 case PP_RMACRO:
2849 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002850 case PP_MACRO:
2851 case PP_IMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002852 if (defining != NULL) {
2853 if (defining->type == EXP_MMACRO) {
2854 defining->def_depth ++;
2855 }
2856 return NO_DIRECTIVE_FOUND;
2857 }
2858 ed = new_ExpDef(EXP_MMACRO);
2859 ed->max_depth =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002860 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002861 ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002862 if (!parse_mmacro_spec(tline, ed, pp_directives[i])) {
2863 nasm_free(ed);
2864 ed = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002865 return DIRECTIVE_FOUND;
2866 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002867 ed->def_depth = 0;
2868 ed->cur_depth = 0;
2869 ed->max_depth = (ed->max_depth + 1);
2870 ed->ignoring = false;
2871 ed->prev = defining;
2872 defining = ed;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002873
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002874 eed = (ExpDef *) hash_findix(&expdefs, ed->name);
2875 while (eed) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002876 if (!strcmp(eed->name, ed->name) &&
2877 (eed->nparam_min <= ed->nparam_max || ed->plus) &&
2878 (ed->nparam_min <= eed->nparam_max || eed->plus)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002879 error(ERR_WARNING|ERR_PASS1,
2880 "redefining multi-line macro `%s'", ed->name);
2881 return DIRECTIVE_FOUND;
2882 }
2883 eed = eed->next;
2884 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002885 free_tlist(origline);
2886 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002887
H. Peter Anvine2c80182005-01-15 22:15:51 +00002888 case PP_ENDM:
2889 case PP_ENDMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002890 if (defining != NULL) {
2891 if (defining->type == EXP_MMACRO) {
2892 if (defining->def_depth > 0) {
2893 defining->def_depth --;
2894 return NO_DIRECTIVE_FOUND;
2895 }
2896 } else {
2897 return NO_DIRECTIVE_FOUND;
2898 }
2899 }
2900 if (!(defining) || (defining->type != EXP_MMACRO)) {
2901 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2902 return DIRECTIVE_FOUND;
2903 }
2904 edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name);
2905 defining->next = *edhead;
2906 *edhead = defining;
2907 ed = defining;
2908 defining = ed->prev;
2909 ed->prev = expansions;
2910 expansions = ed;
2911 ed = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002912 free_tlist(origline);
2913 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002914
H. Peter Anvin89cee572009-07-15 09:16:54 -04002915 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002916 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2917 /*
2918 * We must search along istk->expansion until we hit a
2919 * macro invocation. Then we disable the emitting state(s)
2920 * between exitmacro and endmacro.
2921 */
2922 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
2923 if(ei->type == EXP_MMACRO) {
2924 break;
2925 }
2926 }
2927
2928 if (ei != NULL) {
2929 /*
2930 * Set all invocations leading back to the macro
2931 * invocation to a non-emitting state.
2932 */
2933 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
2934 eei->emitting = false;
2935 }
2936 eei->emitting = false;
2937 } else {
2938 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2939 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002940 free_tlist(origline);
2941 return DIRECTIVE_FOUND;
2942
H. Peter Anvina26433d2008-07-16 14:40:01 -07002943 case PP_UNMACRO:
2944 case PP_UNIMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002945 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002946 {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002947 ExpDef **ed_p;
2948 ExpDef spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002949
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002950 spec.casesense = (i == PP_UNMACRO);
2951 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2952 return DIRECTIVE_FOUND;
2953 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002954 ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL);
2955 while (ed_p && *ed_p) {
2956 ed = *ed_p;
2957 if (ed->casesense == spec.casesense &&
2958 !mstrcmp(ed->name, spec.name, spec.casesense) &&
2959 ed->nparam_min == spec.nparam_min &&
2960 ed->nparam_max == spec.nparam_max &&
2961 ed->plus == spec.plus) {
2962 *ed_p = ed->next;
2963 free_expdef(ed);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002964 } else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002965 ed_p = &ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002966 }
2967 }
2968 free_tlist(origline);
2969 free_tlist(spec.dlist);
2970 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002971 }
2972
H. Peter Anvine2c80182005-01-15 22:15:51 +00002973 case PP_ROTATE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002974 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002975 if (tline->next && tline->next->type == TOK_WHITESPACE)
2976 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002977 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002978 free_tlist(origline);
2979 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2980 return DIRECTIVE_FOUND;
2981 }
2982 t = expand_smacro(tline->next);
2983 tline->next = NULL;
2984 free_tlist(origline);
2985 tline = t;
2986 tptr = &t;
2987 tokval.t_type = TOKEN_INVALID;
2988 evalresult =
2989 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2990 free_tlist(tline);
2991 if (!evalresult)
2992 return DIRECTIVE_FOUND;
2993 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002994 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002995 "trailing garbage after expression ignored");
2996 if (!is_simple(evalresult)) {
2997 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2998 return DIRECTIVE_FOUND;
2999 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003000 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3001 if (ei->type == EXP_MMACRO) {
3002 break;
3003 }
3004 }
3005 if (ei == NULL) {
3006 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
3007 } else if (ei->nparam == 0) {
3008 error(ERR_NONFATAL,
3009 "`%%rotate' invoked within macro without parameters");
3010 } else {
3011 int rotate = ei->rotate + reloc_value(evalresult);
3012
3013 rotate %= (int)ei->nparam;
3014 if (rotate < 0)
3015 rotate += ei->nparam;
3016 ei->rotate = rotate;
3017 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003018 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003019
H. Peter Anvine2c80182005-01-15 22:15:51 +00003020 case PP_REP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003021 if (defining != NULL) {
3022 if (defining->type == EXP_REP) {
3023 defining->def_depth ++;
3024 }
3025 return NO_DIRECTIVE_FOUND;
3026 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003027 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003028 do {
3029 tline = tline->next;
3030 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003031
H. Peter Anvine2c80182005-01-15 22:15:51 +00003032 if (tok_type_(tline, TOK_ID) &&
3033 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003034 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003035 do {
3036 tline = tline->next;
3037 } while (tok_type_(tline, TOK_WHITESPACE));
3038 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003039
H. Peter Anvine2c80182005-01-15 22:15:51 +00003040 if (tline) {
3041 t = expand_smacro(tline);
3042 tptr = &t;
3043 tokval.t_type = TOKEN_INVALID;
3044 evalresult =
3045 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3046 if (!evalresult) {
3047 free_tlist(origline);
3048 return DIRECTIVE_FOUND;
3049 }
3050 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003051 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003052 "trailing garbage after expression ignored");
3053 if (!is_simple(evalresult)) {
3054 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
3055 return DIRECTIVE_FOUND;
3056 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003057 count = reloc_value(evalresult);
3058 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04003059 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003060 count = 0;
3061 } else
3062 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003063 } else {
3064 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003065 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003066 }
3067 free_tlist(origline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003068 ed = new_ExpDef(EXP_REP);
3069 ed->nolist = nolist;
3070 ed->def_depth = 0;
3071 ed->cur_depth = 1;
3072 ed->max_depth = (count - 1);
3073 ed->ignoring = false;
3074 ed->prev = defining;
3075 defining = ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003076 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003077
H. Peter Anvine2c80182005-01-15 22:15:51 +00003078 case PP_ENDREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003079 if (defining != NULL) {
3080 if (defining->type == EXP_REP) {
3081 if (defining->def_depth > 0) {
3082 defining->def_depth --;
3083 return NO_DIRECTIVE_FOUND;
3084 }
3085 } else {
3086 return NO_DIRECTIVE_FOUND;
3087 }
3088 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003089 if ((defining == NULL) || (defining->type != EXP_REP)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003090 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
3091 return DIRECTIVE_FOUND;
3092 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003093
H. Peter Anvine2c80182005-01-15 22:15:51 +00003094 /*
3095 * Now we have a "macro" defined - although it has no name
3096 * and we won't be entering it in the hash tables - we must
3097 * push a macro-end marker for it on to istk->expansion.
3098 * After that, it will take care of propagating itself (a
3099 * macro-end marker line for a macro which is really a %rep
3100 * block will cause the macro to be re-expanded, complete
3101 * with another macro-end marker to ensure the process
3102 * continues) until the whole expansion is forcibly removed
3103 * from istk->expansion by a %exitrep.
3104 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003105 ed = defining;
3106 defining = ed->prev;
3107 ed->prev = expansions;
3108 expansions = ed;
3109 ei = new_ExpInv(EXP_REP, ed);
3110 ei->current = ed->line;
3111 ei->emitting = ((ed->max_depth > 0) ? true : false);
3112 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3113 ei->prev = istk->expansion;
3114 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003115 free_tlist(origline);
3116 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003117
H. Peter Anvine2c80182005-01-15 22:15:51 +00003118 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003119 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3120 /*
3121 * We must search along istk->expansion until we hit a
3122 * rep invocation. Then we disable the emitting state(s)
3123 * between exitrep and endrep.
3124 */
3125 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3126 if (ei->type == EXP_REP) {
3127 break;
3128 }
3129 }
3130
3131 if (ei != NULL) {
3132 /*
3133 * Set all invocations leading back to the rep
3134 * invocation to a non-emitting state.
3135 */
3136 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3137 eei->emitting = false;
3138 }
3139 eei->emitting = false;
3140 eei->current = NULL;
3141 eei->def->cur_depth = eei->def->max_depth;
3142 } else {
3143 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3144 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003145 free_tlist(origline);
3146 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003147
H. Peter Anvine2c80182005-01-15 22:15:51 +00003148 case PP_XDEFINE:
3149 case PP_IXDEFINE:
3150 case PP_DEFINE:
3151 case PP_IDEFINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003152 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003153 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003154
H. Peter Anvine2c80182005-01-15 22:15:51 +00003155 tline = tline->next;
3156 skip_white_(tline);
3157 tline = expand_id(tline);
3158 if (!tline || (tline->type != TOK_ID &&
3159 (tline->type != TOK_PREPROC_ID ||
3160 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003161 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003162 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003163 free_tlist(origline);
3164 return DIRECTIVE_FOUND;
3165 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003166
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003167 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003168 last = tline;
3169 param_start = tline = tline->next;
3170 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003171
H. Peter Anvine2c80182005-01-15 22:15:51 +00003172 /* Expand the macro definition now for %xdefine and %ixdefine */
3173 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3174 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003175
H. Peter Anvine2c80182005-01-15 22:15:51 +00003176 if (tok_is_(tline, "(")) {
3177 /*
3178 * This macro has parameters.
3179 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003180
H. Peter Anvine2c80182005-01-15 22:15:51 +00003181 tline = tline->next;
3182 while (1) {
3183 skip_white_(tline);
3184 if (!tline) {
3185 error(ERR_NONFATAL, "parameter identifier expected");
3186 free_tlist(origline);
3187 return DIRECTIVE_FOUND;
3188 }
3189 if (tline->type != TOK_ID) {
3190 error(ERR_NONFATAL,
3191 "`%s': parameter identifier expected",
3192 tline->text);
3193 free_tlist(origline);
3194 return DIRECTIVE_FOUND;
3195 }
3196 tline->type = TOK_SMAC_PARAM + nparam++;
3197 tline = tline->next;
3198 skip_white_(tline);
3199 if (tok_is_(tline, ",")) {
3200 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003201 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003202 if (!tok_is_(tline, ")")) {
3203 error(ERR_NONFATAL,
3204 "`)' expected to terminate macro template");
3205 free_tlist(origline);
3206 return DIRECTIVE_FOUND;
3207 }
3208 break;
3209 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003210 }
3211 last = tline;
3212 tline = tline->next;
3213 }
3214 if (tok_type_(tline, TOK_WHITESPACE))
3215 last = tline, tline = tline->next;
3216 macro_start = NULL;
3217 last->next = NULL;
3218 t = tline;
3219 while (t) {
3220 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003221 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003222 if (tt->type >= TOK_SMAC_PARAM &&
3223 !strcmp(tt->text, t->text))
3224 t->type = tt->type;
3225 }
3226 tt = t->next;
3227 t->next = macro_start;
3228 macro_start = t;
3229 t = tt;
3230 }
3231 /*
3232 * Good. We now have a macro name, a parameter count, and a
3233 * token list (in reverse order) for an expansion. We ought
3234 * to be OK just to create an SMacro, store it, and let
3235 * free_tlist have the rest of the line (which we have
3236 * carefully re-terminated after chopping off the expansion
3237 * from the end).
3238 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003239 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003240 free_tlist(origline);
3241 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003242
H. Peter Anvine2c80182005-01-15 22:15:51 +00003243 case PP_UNDEF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003244 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003245 tline = tline->next;
3246 skip_white_(tline);
3247 tline = expand_id(tline);
3248 if (!tline || (tline->type != TOK_ID &&
3249 (tline->type != TOK_PREPROC_ID ||
3250 tline->text[1] != '$'))) {
3251 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3252 free_tlist(origline);
3253 return DIRECTIVE_FOUND;
3254 }
3255 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003256 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003257 "trailing garbage after macro name ignored");
3258 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003259
H. Peter Anvine2c80182005-01-15 22:15:51 +00003260 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003261 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003262 undef_smacro(ctx, mname);
3263 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003264 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003265
H. Peter Anvin9e200162008-06-04 17:23:14 -07003266 case PP_DEFSTR:
3267 case PP_IDEFSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003268 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003269 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003270
3271 tline = tline->next;
3272 skip_white_(tline);
3273 tline = expand_id(tline);
3274 if (!tline || (tline->type != TOK_ID &&
3275 (tline->type != TOK_PREPROC_ID ||
3276 tline->text[1] != '$'))) {
3277 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003278 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003279 free_tlist(origline);
3280 return DIRECTIVE_FOUND;
3281 }
3282
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003283 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003284 last = tline;
3285 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003286 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003287
3288 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003289 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003290
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003291 p = detoken(tline, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003292 macro_start = nasm_malloc(sizeof(*macro_start));
3293 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003294 macro_start->text = nasm_quote(p, strlen(p));
3295 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003296 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003297 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003298
3299 /*
3300 * We now have a macro name, an implicit parameter count of
3301 * zero, and a string token to use as an expansion. Create
3302 * and store an SMacro.
3303 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003304 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003305 free_tlist(origline);
3306 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003307
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003308 case PP_DEFTOK:
3309 case PP_IDEFTOK:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003310 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003311 casesense = (i == PP_DEFTOK);
3312
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003313 tline = tline->next;
3314 skip_white_(tline);
3315 tline = expand_id(tline);
3316 if (!tline || (tline->type != TOK_ID &&
3317 (tline->type != TOK_PREPROC_ID ||
3318 tline->text[1] != '$'))) {
3319 error(ERR_NONFATAL,
3320 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003321 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003322 free_tlist(origline);
3323 return DIRECTIVE_FOUND;
3324 }
3325 ctx = get_ctx(tline->text, &mname, false);
3326 last = tline;
3327 tline = expand_smacro(tline->next);
3328 last->next = NULL;
3329
3330 t = tline;
3331 while (tok_type_(t, TOK_WHITESPACE))
3332 t = t->next;
3333 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003334 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003335 error(ERR_NONFATAL,
3336 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003337 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003338 free_tlist(tline);
3339 free_tlist(origline);
3340 return DIRECTIVE_FOUND;
3341 }
3342
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003343 /*
3344 * Convert the string to a token stream. Note that smacros
3345 * are stored with the token stream reversed, so we have to
3346 * reverse the output of tokenize().
3347 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003348 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003349 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003350
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003351 /*
3352 * We now have a macro name, an implicit parameter count of
3353 * zero, and a numeric token to use as an expansion. Create
3354 * and store an SMacro.
3355 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003356 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003357 free_tlist(tline);
3358 free_tlist(origline);
3359 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003360
H. Peter Anvin418ca702008-05-30 10:42:30 -07003361 case PP_PATHSEARCH:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003362 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003363 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003364 FILE *fp;
3365 StrList *xsl = NULL;
3366 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003367
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003368 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003369
3370 tline = tline->next;
3371 skip_white_(tline);
3372 tline = expand_id(tline);
3373 if (!tline || (tline->type != TOK_ID &&
3374 (tline->type != TOK_PREPROC_ID ||
3375 tline->text[1] != '$'))) {
3376 error(ERR_NONFATAL,
3377 "`%%pathsearch' expects a macro identifier as first parameter");
3378 free_tlist(origline);
3379 return DIRECTIVE_FOUND;
3380 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003381 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003382 last = tline;
3383 tline = expand_smacro(tline->next);
3384 last->next = NULL;
3385
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003386 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003387 while (tok_type_(t, TOK_WHITESPACE))
3388 t = t->next;
3389
3390 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003391 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003392 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003393 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003394 free_tlist(origline);
3395 return DIRECTIVE_FOUND; /* but we did _something_ */
3396 }
3397 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003398 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003399 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003400 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003401 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003402 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003403
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003404 fp = inc_fopen(p, &xsl, &xst, true);
3405 if (fp) {
3406 p = xsl->str;
3407 fclose(fp); /* Don't actually care about the file */
3408 }
H. Peter Anvin418ca702008-05-30 10:42:30 -07003409 macro_start = nasm_malloc(sizeof(*macro_start));
3410 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003411 macro_start->text = nasm_quote(p, strlen(p));
3412 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003413 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003414 if (xsl)
3415 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003416
3417 /*
3418 * We now have a macro name, an implicit parameter count of
3419 * zero, and a string token to use as an expansion. Create
3420 * and store an SMacro.
3421 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003422 define_smacro(ctx, mname, casesense, 0, macro_start);
3423 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003424 free_tlist(origline);
3425 return DIRECTIVE_FOUND;
3426 }
3427
H. Peter Anvine2c80182005-01-15 22:15:51 +00003428 case PP_STRLEN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003429 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003430 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003431
H. Peter Anvine2c80182005-01-15 22:15:51 +00003432 tline = tline->next;
3433 skip_white_(tline);
3434 tline = expand_id(tline);
3435 if (!tline || (tline->type != TOK_ID &&
3436 (tline->type != TOK_PREPROC_ID ||
3437 tline->text[1] != '$'))) {
3438 error(ERR_NONFATAL,
3439 "`%%strlen' expects a macro identifier as first parameter");
3440 free_tlist(origline);
3441 return DIRECTIVE_FOUND;
3442 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003443 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003444 last = tline;
3445 tline = expand_smacro(tline->next);
3446 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003447
H. Peter Anvine2c80182005-01-15 22:15:51 +00003448 t = tline;
3449 while (tok_type_(t, TOK_WHITESPACE))
3450 t = t->next;
3451 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003452 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003453 error(ERR_NONFATAL,
3454 "`%%strlen` requires string as second parameter");
3455 free_tlist(tline);
3456 free_tlist(origline);
3457 return DIRECTIVE_FOUND;
3458 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003459
H. Peter Anvine2c80182005-01-15 22:15:51 +00003460 macro_start = nasm_malloc(sizeof(*macro_start));
3461 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003462 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003463 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003464
H. Peter Anvine2c80182005-01-15 22:15:51 +00003465 /*
3466 * We now have a macro name, an implicit parameter count of
3467 * zero, and a numeric token to use as an expansion. Create
3468 * and store an SMacro.
3469 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003470 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003471 free_tlist(tline);
3472 free_tlist(origline);
3473 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003474
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003475 case PP_STRCAT:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003476 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003477 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003478
3479 tline = tline->next;
3480 skip_white_(tline);
3481 tline = expand_id(tline);
3482 if (!tline || (tline->type != TOK_ID &&
3483 (tline->type != TOK_PREPROC_ID ||
3484 tline->text[1] != '$'))) {
3485 error(ERR_NONFATAL,
3486 "`%%strcat' expects a macro identifier as first parameter");
3487 free_tlist(origline);
3488 return DIRECTIVE_FOUND;
3489 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003490 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003491 last = tline;
3492 tline = expand_smacro(tline->next);
3493 last->next = NULL;
3494
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003495 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003496 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003497 switch (t->type) {
3498 case TOK_WHITESPACE:
3499 break;
3500 case TOK_STRING:
3501 len += t->a.len = nasm_unquote(t->text, NULL);
3502 break;
3503 case TOK_OTHER:
3504 if (!strcmp(t->text, ",")) /* permit comma separators */
3505 break;
3506 /* else fall through */
3507 default:
3508 error(ERR_NONFATAL,
3509 "non-string passed to `%%strcat' (%d)", t->type);
3510 free_tlist(tline);
3511 free_tlist(origline);
3512 return DIRECTIVE_FOUND;
3513 }
3514 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003515
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003516 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003517 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003518 if (t->type == TOK_STRING) {
3519 memcpy(p, t->text, t->a.len);
3520 p += t->a.len;
3521 }
3522 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003523
3524 /*
3525 * We now have a macro name, an implicit parameter count of
3526 * zero, and a numeric token to use as an expansion. Create
3527 * and store an SMacro.
3528 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003529 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3530 macro_start->text = nasm_quote(pp, len);
3531 nasm_free(pp);
3532 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003533 free_tlist(tline);
3534 free_tlist(origline);
3535 return DIRECTIVE_FOUND;
3536
H. Peter Anvine2c80182005-01-15 22:15:51 +00003537 case PP_SUBSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003538 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003539 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003540 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003541 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003542
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003543 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003544
H. Peter Anvine2c80182005-01-15 22:15:51 +00003545 tline = tline->next;
3546 skip_white_(tline);
3547 tline = expand_id(tline);
3548 if (!tline || (tline->type != TOK_ID &&
3549 (tline->type != TOK_PREPROC_ID ||
3550 tline->text[1] != '$'))) {
3551 error(ERR_NONFATAL,
3552 "`%%substr' expects a macro identifier as first parameter");
3553 free_tlist(origline);
3554 return DIRECTIVE_FOUND;
3555 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003556 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003557 last = tline;
3558 tline = expand_smacro(tline->next);
3559 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003560
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003561 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003562 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003563 while (tok_type_(t, TOK_WHITESPACE))
3564 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003565
H. Peter Anvine2c80182005-01-15 22:15:51 +00003566 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003567 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003568 error(ERR_NONFATAL,
3569 "`%%substr` requires string as second parameter");
3570 free_tlist(tline);
3571 free_tlist(origline);
3572 return DIRECTIVE_FOUND;
3573 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003574
H. Peter Anvine2c80182005-01-15 22:15:51 +00003575 tt = t->next;
3576 tptr = &tt;
3577 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003578 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003579 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003580 if (!evalresult) {
3581 free_tlist(tline);
3582 free_tlist(origline);
3583 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003584 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003585 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3586 free_tlist(tline);
3587 free_tlist(origline);
3588 return DIRECTIVE_FOUND;
3589 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003590 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003591
3592 while (tok_type_(tt, TOK_WHITESPACE))
3593 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003594 if (!tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003595 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003596 } else {
3597 tokval.t_type = TOKEN_INVALID;
3598 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3599 pass, error, NULL);
3600 if (!evalresult) {
3601 free_tlist(tline);
3602 free_tlist(origline);
3603 return DIRECTIVE_FOUND;
3604 } else if (!is_simple(evalresult)) {
3605 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3606 free_tlist(tline);
3607 free_tlist(origline);
3608 return DIRECTIVE_FOUND;
3609 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003610 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003611 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003612
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003613 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003614 /* make start and count being in range */
3615 if (start < 0)
3616 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003617 if (count < 0)
3618 count = len + count + 1 - start;
3619 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003620 count = len - start;
3621 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003622 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003623
H. Peter Anvine2c80182005-01-15 22:15:51 +00003624 macro_start = nasm_malloc(sizeof(*macro_start));
3625 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003626 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003627 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003628 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003629
H. Peter Anvine2c80182005-01-15 22:15:51 +00003630 /*
3631 * We now have a macro name, an implicit parameter count of
3632 * zero, and a numeric token to use as an expansion. Create
3633 * and store an SMacro.
3634 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003635 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003636 free_tlist(tline);
3637 free_tlist(origline);
3638 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003639 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003640
H. Peter Anvine2c80182005-01-15 22:15:51 +00003641 case PP_ASSIGN:
3642 case PP_IASSIGN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003643 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003644 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003645
H. Peter Anvine2c80182005-01-15 22:15:51 +00003646 tline = tline->next;
3647 skip_white_(tline);
3648 tline = expand_id(tline);
3649 if (!tline || (tline->type != TOK_ID &&
3650 (tline->type != TOK_PREPROC_ID ||
3651 tline->text[1] != '$'))) {
3652 error(ERR_NONFATAL,
3653 "`%%%sassign' expects a macro identifier",
3654 (i == PP_IASSIGN ? "i" : ""));
3655 free_tlist(origline);
3656 return DIRECTIVE_FOUND;
3657 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003658 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003659 last = tline;
3660 tline = expand_smacro(tline->next);
3661 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003662
H. Peter Anvine2c80182005-01-15 22:15:51 +00003663 t = tline;
3664 tptr = &t;
3665 tokval.t_type = TOKEN_INVALID;
3666 evalresult =
3667 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3668 free_tlist(tline);
3669 if (!evalresult) {
3670 free_tlist(origline);
3671 return DIRECTIVE_FOUND;
3672 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003673
H. Peter Anvine2c80182005-01-15 22:15:51 +00003674 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003675 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003676 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003677
H. Peter Anvine2c80182005-01-15 22:15:51 +00003678 if (!is_simple(evalresult)) {
3679 error(ERR_NONFATAL,
3680 "non-constant value given to `%%%sassign'",
3681 (i == PP_IASSIGN ? "i" : ""));
3682 free_tlist(origline);
3683 return DIRECTIVE_FOUND;
3684 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003685
H. Peter Anvine2c80182005-01-15 22:15:51 +00003686 macro_start = nasm_malloc(sizeof(*macro_start));
3687 macro_start->next = NULL;
3688 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003689 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003690
H. Peter Anvine2c80182005-01-15 22:15:51 +00003691 /*
3692 * We now have a macro name, an implicit parameter count of
3693 * zero, and a numeric token to use as an expansion. Create
3694 * and store an SMacro.
3695 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003696 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003697 free_tlist(origline);
3698 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003699
H. Peter Anvine2c80182005-01-15 22:15:51 +00003700 case PP_LINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003701 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003702 /*
3703 * Syntax is `%line nnn[+mmm] [filename]'
3704 */
3705 tline = tline->next;
3706 skip_white_(tline);
3707 if (!tok_type_(tline, TOK_NUMBER)) {
3708 error(ERR_NONFATAL, "`%%line' expects line number");
3709 free_tlist(origline);
3710 return DIRECTIVE_FOUND;
3711 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003712 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003713 m = 1;
3714 tline = tline->next;
3715 if (tok_is_(tline, "+")) {
3716 tline = tline->next;
3717 if (!tok_type_(tline, TOK_NUMBER)) {
3718 error(ERR_NONFATAL, "`%%line' expects line increment");
3719 free_tlist(origline);
3720 return DIRECTIVE_FOUND;
3721 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003722 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003723 tline = tline->next;
3724 }
3725 skip_white_(tline);
3726 src_set_linnum(k);
3727 istk->lineinc = m;
3728 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003729 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003730 }
3731 free_tlist(origline);
3732 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003733
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003734 case PP_WHILE:
3735 if (defining != NULL) {
3736 if (defining->type == EXP_WHILE) {
3737 defining->def_depth ++;
3738 }
3739 return NO_DIRECTIVE_FOUND;
3740 }
3741 l = NULL;
3742 if ((istk->expansion != NULL) &&
3743 (istk->expansion->emitting == false)) {
3744 j = COND_NEVER;
3745 } else {
3746 l = new_Line();
3747 l->first = copy_Token(tline->next);
3748 j = if_condition(tline->next, i);
3749 tline->next = NULL; /* it got freed */
3750 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
3751 }
3752 ed = new_ExpDef(EXP_WHILE);
3753 ed->state = j;
3754 ed->cur_depth = 1;
3755 ed->max_depth = DEADMAN_LIMIT;
3756 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
3757 if (ed->ignoring == false) {
3758 ed->line = l;
3759 ed->last = l;
3760 } else if (l != NULL) {
3761 delete_Token(l->first);
3762 nasm_free(l);
3763 l = NULL;
3764 }
3765 ed->prev = defining;
3766 defining = ed;
3767 free_tlist(origline);
3768 return DIRECTIVE_FOUND;
3769
3770 case PP_ENDWHILE:
3771 if (defining != NULL) {
3772 if (defining->type == EXP_WHILE) {
3773 if (defining->def_depth > 0) {
3774 defining->def_depth --;
3775 return NO_DIRECTIVE_FOUND;
3776 }
3777 } else {
3778 return NO_DIRECTIVE_FOUND;
3779 }
3780 }
3781 if (tline->next != NULL) {
3782 error_precond(ERR_WARNING|ERR_PASS1,
3783 "trailing garbage after `%%endwhile' ignored");
3784 }
3785 if ((defining == NULL) || (defining->type != EXP_WHILE)) {
3786 error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'");
3787 return DIRECTIVE_FOUND;
3788 }
3789 ed = defining;
3790 defining = ed->prev;
3791 if (ed->ignoring == false) {
3792 ed->prev = expansions;
3793 expansions = ed;
3794 ei = new_ExpInv(EXP_WHILE, ed);
3795 ei->current = ed->line->next;
3796 ei->emitting = true;
3797 ei->prev = istk->expansion;
3798 istk->expansion = ei;
3799 } else {
3800 nasm_free(ed);
3801 }
3802 free_tlist(origline);
3803 return DIRECTIVE_FOUND;
3804
3805 case PP_EXITWHILE:
3806 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3807 /*
3808 * We must search along istk->expansion until we hit a
3809 * while invocation. Then we disable the emitting state(s)
3810 * between exitwhile and endwhile.
3811 */
3812 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3813 if (ei->type == EXP_WHILE) {
3814 break;
3815 }
3816 }
3817
3818 if (ei != NULL) {
3819 /*
3820 * Set all invocations leading back to the while
3821 * invocation to a non-emitting state.
3822 */
3823 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3824 eei->emitting = false;
3825 }
3826 eei->emitting = false;
3827 eei->current = NULL;
3828 eei->def->cur_depth = eei->def->max_depth;
3829 } else {
3830 error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block");
3831 }
3832 free_tlist(origline);
3833 return DIRECTIVE_FOUND;
3834
3835 case PP_COMMENT:
3836 if (defining != NULL) {
3837 if (defining->type == EXP_COMMENT) {
3838 defining->def_depth ++;
3839 }
3840 return NO_DIRECTIVE_FOUND;
3841 }
3842 ed = new_ExpDef(EXP_COMMENT);
3843 ed->ignoring = true;
3844 ed->prev = defining;
3845 defining = ed;
3846 free_tlist(origline);
3847 return DIRECTIVE_FOUND;
3848
3849 case PP_ENDCOMMENT:
3850 if (defining != NULL) {
3851 if (defining->type == EXP_COMMENT) {
3852 if (defining->def_depth > 0) {
3853 defining->def_depth --;
3854 return NO_DIRECTIVE_FOUND;
3855 }
3856 } else {
3857 return NO_DIRECTIVE_FOUND;
3858 }
3859 }
3860 if ((defining == NULL) || (defining->type != EXP_COMMENT)) {
3861 error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'");
3862 return DIRECTIVE_FOUND;
3863 }
3864 ed = defining;
3865 defining = ed->prev;
3866 nasm_free(ed);
3867 free_tlist(origline);
3868 return DIRECTIVE_FOUND;
3869
3870 case PP_FINAL:
3871 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3872 if (in_final != false) {
3873 error(ERR_FATAL, "`%%final' cannot be used recursively");
3874 }
3875 tline = tline->next;
3876 skip_white_(tline);
3877 if (tline == NULL) {
3878 error(ERR_NONFATAL, "`%%final' expects at least one parameter");
3879 } else {
3880 l = new_Line();
3881 l->first = copy_Token(tline);
3882 l->next = finals;
3883 finals = l;
3884 }
3885 free_tlist(origline);
3886 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003887
H. Peter Anvine2c80182005-01-15 22:15:51 +00003888 default:
3889 error(ERR_FATAL,
3890 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003891 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003892 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003893 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003894}
3895
3896/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003897 * Ensure that a macro parameter contains a condition code and
3898 * nothing else. Return the condition code index if so, or -1
3899 * otherwise.
3900 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003901static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003902{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003903 Token *tt;
3904 int i, j, k, m;
3905
H. Peter Anvin25a99342007-09-22 17:45:45 -07003906 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003907 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003908
H. Peter Anvineba20a72002-04-30 20:53:55 +00003909 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003910 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003911 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003912 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003913 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003914 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003915 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003916
3917 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003918 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003919 while (j - i > 1) {
3920 k = (j + i) / 2;
3921 m = nasm_stricmp(t->text, conditions[k]);
3922 if (m == 0) {
3923 i = k;
3924 j = -2;
3925 break;
3926 } else if (m < 0) {
3927 j = k;
3928 } else
3929 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003930 }
3931 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003932 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003933 return i;
3934}
3935
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003936static bool paste_tokens(Token **head, const struct tokseq_match *m,
3937 int mnum, bool handle_paste_tokens)
H. Peter Anvind784a082009-04-20 14:01:18 -07003938{
3939 Token **tail, *t, *tt;
3940 Token **paste_head;
3941 bool did_paste = false;
3942 char *tmp;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003943 int i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003944
3945 /* Now handle token pasting... */
3946 paste_head = NULL;
3947 tail = head;
3948 while ((t = *tail) && (tt = t->next)) {
3949 switch (t->type) {
3950 case TOK_WHITESPACE:
3951 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003952 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003953 t->next = delete_Token(tt);
3954 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003955 /* Do not advance paste_head here */
3956 tail = &t->next;
3957 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003958 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003959 case TOK_PASTE: /* %+ */
3960 if (handle_paste_tokens) {
3961 /* Zap %+ and whitespace tokens to the right */
3962 while (t && (t->type == TOK_WHITESPACE ||
3963 t->type == TOK_PASTE))
3964 t = *tail = delete_Token(t);
3965 if (!paste_head || !t)
3966 break; /* Nothing to paste with */
3967 tail = paste_head;
3968 t = *tail;
3969 tt = t->next;
3970 while (tok_type_(tt, TOK_WHITESPACE))
3971 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003972 if (tt) {
3973 tmp = nasm_strcat(t->text, tt->text);
3974 delete_Token(t);
3975 tt = delete_Token(tt);
3976 t = *tail = tokenize(tmp);
3977 nasm_free(tmp);
3978 while (t->next) {
3979 tail = &t->next;
3980 t = t->next;
3981 }
3982 t->next = tt; /* Attach the remaining token chain */
3983 did_paste = true;
3984 }
3985 paste_head = tail;
3986 tail = &t->next;
3987 break;
3988 }
3989 /* else fall through */
3990 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003991 /*
3992 * Concatenation of tokens might look nontrivial
3993 * but in real it's pretty simple -- the caller
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003994 * prepares the masks of token types to be concatenated
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003995 * and we simply find matched sequences and slip
3996 * them together
3997 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003998 for (i = 0; i < mnum; i++) {
3999 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
4000 size_t len = 0;
4001 char *tmp, *p;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004002
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004003 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004004 len += strlen(tt->text);
4005 tt = tt->next;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004006 }
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004007
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004008 /*
4009 * Now tt points to the first token after
4010 * the potential paste area...
4011 */
4012 if (tt != t->next) {
4013 /* We have at least two tokens... */
4014 len += strlen(t->text);
4015 p = tmp = nasm_malloc(len+1);
4016 while (t != tt) {
4017 strcpy(p, t->text);
4018 p = strchr(p, '\0');
4019 t = delete_Token(t);
4020 }
4021 t = *tail = tokenize(tmp);
4022 nasm_free(tmp);
4023 while (t->next) {
4024 tail = &t->next;
4025 t = t->next;
4026 }
4027 t->next = tt; /* Attach the remaining token chain */
4028 did_paste = true;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004029 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004030 paste_head = tail;
4031 tail = &t->next;
4032 break;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004033 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004034 }
4035 if (i >= mnum) { /* no match */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004036 tail = &t->next;
4037 if (!tok_type_(t->next, TOK_WHITESPACE))
4038 paste_head = tail;
4039 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004040 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004041 }
4042 }
4043 return did_paste;
4044}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004045
4046/*
4047 * expands to a list of tokens from %{x:y}
4048 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004049static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004050{
4051 Token *t = tline, **tt, *tm, *head;
4052 char *pos;
4053 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004054
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004055 pos = strchr(tline->text, ':');
4056 nasm_assert(pos);
4057
4058 lst = atoi(pos + 1);
4059 fst = atoi(tline->text + 1);
4060
4061 /*
4062 * only macros params are accounted so
4063 * if someone passes %0 -- we reject such
4064 * value(s)
4065 */
4066 if (lst == 0 || fst == 0)
4067 goto err;
4068
4069 /* the values should be sane */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004070 if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) ||
4071 (lst > (int)ei->nparam || lst < (-(int)ei->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004072 goto err;
4073
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004074 fst = fst < 0 ? fst + (int)ei->nparam + 1: fst;
4075 lst = lst < 0 ? lst + (int)ei->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004076
4077 /* counted from zero */
4078 fst--, lst--;
4079
4080 /*
4081 * it will be at least one token
4082 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004083 tm = ei->params[(fst + ei->rotate) % ei->nparam];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004084 t = new_Token(NULL, tm->type, tm->text, 0);
4085 head = t, tt = &t->next;
4086 if (fst < lst) {
4087 for (i = fst + 1; i <= lst; i++) {
4088 t = new_Token(NULL, TOK_OTHER, ",", 0);
4089 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004090 j = (i + ei->rotate) % ei->nparam;
4091 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004092 t = new_Token(NULL, tm->type, tm->text, 0);
4093 *tt = t, tt = &t->next;
4094 }
4095 } else {
4096 for (i = fst - 1; i >= lst; i--) {
4097 t = new_Token(NULL, TOK_OTHER, ",", 0);
4098 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004099 j = (i + ei->rotate) % ei->nparam;
4100 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004101 t = new_Token(NULL, tm->type, tm->text, 0);
4102 *tt = t, tt = &t->next;
4103 }
4104 }
4105
4106 *last = tt;
4107 return head;
4108
4109err:
4110 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
4111 &tline->text[1]);
4112 return tline;
4113}
4114
H. Peter Anvin76690a12002-04-30 20:52:49 +00004115/*
4116 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004117 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004118 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004119 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004120static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004121{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004122 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004123 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004124 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004125
4126 tail = &thead;
4127 thead = NULL;
4128
H. Peter Anvine2c80182005-01-15 22:15:51 +00004129 while (tline) {
4130 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004131 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4132 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4133 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004134 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004135 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004136 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004137 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004138 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004139 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004140
H. Peter Anvine2c80182005-01-15 22:15:51 +00004141 t = tline;
4142 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004143
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004144 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
4145 if (ei->type == EXP_MMACRO) {
4146 break;
4147 }
4148 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004149 if (ei == NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004150 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004151 } else {
4152 pos = strchr(t->text, ':');
4153 if (!pos) {
4154 switch (t->text[1]) {
4155 /*
4156 * We have to make a substitution of one of the
4157 * forms %1, %-1, %+1, %%foo, %0.
4158 */
4159 case '0':
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004160 if ((strlen(t->text) > 2) && (t->text[2] == '0')) {
4161 type = TOK_ID;
4162 text = nasm_strdup(ei->label_text);
4163 } else {
4164 type = TOK_NUMBER;
4165 snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam);
4166 text = nasm_strdup(tmpbuf);
4167 }
4168 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004169 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004170 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004171 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004172 ei->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004173 text = nasm_strcat(tmpbuf, t->text + 2);
4174 break;
4175 case '-':
4176 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004177 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004178 tt = NULL;
4179 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004180 if (ei->nparam > 1)
4181 n = (n + ei->rotate) % ei->nparam;
4182 tt = ei->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004183 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004184 cc = find_cc(tt);
4185 if (cc == -1) {
4186 error(ERR_NONFATAL,
4187 "macro parameter %d is not a condition code",
4188 n + 1);
4189 text = NULL;
4190 } else {
4191 type = TOK_ID;
4192 if (inverse_ccs[cc] == -1) {
4193 error(ERR_NONFATAL,
4194 "condition code `%s' is not invertible",
4195 conditions[cc]);
4196 text = NULL;
4197 } else
4198 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4199 }
4200 break;
4201 case '+':
4202 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004203 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004204 tt = NULL;
4205 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004206 if (ei->nparam > 1)
4207 n = (n + ei->rotate) % ei->nparam;
4208 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004209 }
4210 cc = find_cc(tt);
4211 if (cc == -1) {
4212 error(ERR_NONFATAL,
4213 "macro parameter %d is not a condition code",
4214 n + 1);
4215 text = NULL;
4216 } else {
4217 type = TOK_ID;
4218 text = nasm_strdup(conditions[cc]);
4219 }
4220 break;
4221 default:
4222 n = atoi(t->text + 1) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004223 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004224 tt = NULL;
4225 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004226 if (ei->nparam > 1)
4227 n = (n + ei->rotate) % ei->nparam;
4228 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004229 }
4230 if (tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004231 for (i = 0; i < ei->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004232 *tail = new_Token(NULL, tt->type, tt->text, 0);
4233 tail = &(*tail)->next;
4234 tt = tt->next;
4235 }
4236 }
4237 text = NULL; /* we've done it here */
4238 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004239 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004240 } else {
4241 /*
4242 * seems we have a parameters range here
4243 */
4244 Token *head, **last;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004245 head = expand_mmac_params_range(ei, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004246 if (head != t) {
4247 *tail = head;
4248 *last = tline;
4249 tline = head;
4250 text = NULL;
4251 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004252 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004253 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004254 if (!text) {
4255 delete_Token(t);
4256 } else {
4257 *tail = t;
4258 tail = &t->next;
4259 t->type = type;
4260 nasm_free(t->text);
4261 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004262 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004263 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004264 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004265 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004266 } else if (tline->type == TOK_INDIRECT) {
4267 t = tline;
4268 tline = tline->next;
4269 tt = tokenize(t->text);
4270 tt = expand_mmac_params(tt);
4271 tt = expand_smacro(tt);
4272 *tail = tt;
4273 while (tt) {
4274 tt->a.mac = NULL; /* Necessary? */
4275 tail = &tt->next;
4276 tt = tt->next;
4277 }
4278 delete_Token(t);
4279 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004280 } else {
4281 t = *tail = tline;
4282 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004283 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004284 tail = &t->next;
4285 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004286 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004287 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004288
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004289 if (changed) {
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004290 const struct tokseq_match t[] = {
4291 {
4292 PP_CONCAT_MASK(TOK_ID) |
4293 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4294 PP_CONCAT_MASK(TOK_ID) |
4295 PP_CONCAT_MASK(TOK_NUMBER) |
4296 PP_CONCAT_MASK(TOK_FLOAT) |
4297 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4298 },
4299 {
4300 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4301 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4302 }
4303 };
4304 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004305 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004306
H. Peter Anvin76690a12002-04-30 20:52:49 +00004307 return thead;
4308}
4309
4310/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004311 * Expand all single-line macro calls made in the given line.
4312 * Return the expanded version of the line. The original is deemed
4313 * to be destroyed in the process. (In reality we'll just move
4314 * Tokens from input to output a lot of the time, rather than
4315 * actually bothering to destroy and replicate.)
4316 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004317
H. Peter Anvine2c80182005-01-15 22:15:51 +00004318static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004319{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004320 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004321 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004322 Token **params;
4323 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004324 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004325 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004326 Token *org_tline = tline;
4327 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004328 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004329 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004330 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004331
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004332 /*
4333 * Trick: we should avoid changing the start token pointer since it can
4334 * be contained in "next" field of other token. Because of this
4335 * we allocate a copy of first token and work with it; at the end of
4336 * routine we copy it back
4337 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004338 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004339 tline = new_Token(org_tline->next, org_tline->type,
4340 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004341 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004342 nasm_free(org_tline->text);
4343 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004344 }
4345
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004346 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004347
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004348again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004349 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004350 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004351
H. Peter Anvine2c80182005-01-15 22:15:51 +00004352 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004353 if (!--deadman) {
4354 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004355 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004356 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004357
H. Peter Anvine2c80182005-01-15 22:15:51 +00004358 if ((mname = tline->text)) {
4359 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004360 if (tline->type == TOK_ID) {
4361 head = (SMacro *)hash_findix(&smacros, mname);
4362 } else if (tline->type == TOK_PREPROC_ID) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004363 ctx = get_ctx(mname, &mname, false);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004364 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4365 } else
4366 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004367
H. Peter Anvine2c80182005-01-15 22:15:51 +00004368 /*
4369 * We've hit an identifier. As in is_mmacro below, we first
4370 * check whether the identifier is a single-line macro at
4371 * all, then think about checking for parameters if
4372 * necessary.
4373 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004374 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004375 if (!mstrcmp(m->name, mname, m->casesense))
4376 break;
4377 if (m) {
4378 mstart = tline;
4379 params = NULL;
4380 paramsize = NULL;
4381 if (m->nparam == 0) {
4382 /*
4383 * Simple case: the macro is parameterless. Discard the
4384 * one token that the macro call took, and push the
4385 * expansion back on the to-do stack.
4386 */
4387 if (!m->expansion) {
4388 if (!strcmp("__FILE__", m->name)) {
4389 int32_t num = 0;
4390 char *file = NULL;
4391 src_get(&num, &file);
4392 tline->text = nasm_quote(file, strlen(file));
4393 tline->type = TOK_STRING;
4394 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004395 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004396 }
4397 if (!strcmp("__LINE__", m->name)) {
4398 nasm_free(tline->text);
4399 make_tok_num(tline, src_get_linnum());
4400 continue;
4401 }
4402 if (!strcmp("__BITS__", m->name)) {
4403 nasm_free(tline->text);
4404 make_tok_num(tline, globalbits);
4405 continue;
4406 }
4407 tline = delete_Token(tline);
4408 continue;
4409 }
4410 } else {
4411 /*
4412 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004413 * exists and takes parameters. We must find the
4414 * parameters in the call, count them, find the SMacro
4415 * that corresponds to that form of the macro call, and
4416 * substitute for the parameters when we expand. What a
4417 * pain.
4418 */
4419 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004420 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004421 do {
4422 t = tline->next;
4423 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004424 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004425 t->text = NULL;
4426 t = tline->next = delete_Token(t);
4427 }
4428 tline = t;
4429 } while (tok_type_(tline, TOK_WHITESPACE));
4430 if (!tok_is_(tline, "(")) {
4431 /*
4432 * This macro wasn't called with parameters: ignore
4433 * the call. (Behaviour borrowed from gnu cpp.)
4434 */
4435 tline = mstart;
4436 m = NULL;
4437 } else {
4438 int paren = 0;
4439 int white = 0;
4440 brackets = 0;
4441 nparam = 0;
4442 sparam = PARAM_DELTA;
4443 params = nasm_malloc(sparam * sizeof(Token *));
4444 params[0] = tline->next;
4445 paramsize = nasm_malloc(sparam * sizeof(int));
4446 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004447 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004448 /*
4449 * For some unusual expansions
4450 * which concatenates function call
4451 */
4452 t = tline->next;
4453 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004454 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004455 t->text = NULL;
4456 t = tline->next = delete_Token(t);
4457 }
4458 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004459
H. Peter Anvine2c80182005-01-15 22:15:51 +00004460 if (!tline) {
4461 error(ERR_NONFATAL,
4462 "macro call expects terminating `)'");
4463 break;
4464 }
4465 if (tline->type == TOK_WHITESPACE
4466 && brackets <= 0) {
4467 if (paramsize[nparam])
4468 white++;
4469 else
4470 params[nparam] = tline->next;
4471 continue; /* parameter loop */
4472 }
4473 if (tline->type == TOK_OTHER
4474 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004475 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004476 if (ch == ',' && !paren && brackets <= 0) {
4477 if (++nparam >= sparam) {
4478 sparam += PARAM_DELTA;
4479 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004480 sparam * sizeof(Token *));
4481 paramsize = nasm_realloc(paramsize,
4482 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004483 }
4484 params[nparam] = tline->next;
4485 paramsize[nparam] = 0;
4486 white = 0;
4487 continue; /* parameter loop */
4488 }
4489 if (ch == '{' &&
4490 (brackets > 0 || (brackets == 0 &&
4491 !paramsize[nparam])))
4492 {
4493 if (!(brackets++)) {
4494 params[nparam] = tline->next;
4495 continue; /* parameter loop */
4496 }
4497 }
4498 if (ch == '}' && brackets > 0)
4499 if (--brackets == 0) {
4500 brackets = -1;
4501 continue; /* parameter loop */
4502 }
4503 if (ch == '(' && !brackets)
4504 paren++;
4505 if (ch == ')' && brackets <= 0)
4506 if (--paren < 0)
4507 break;
4508 }
4509 if (brackets < 0) {
4510 brackets = 0;
4511 error(ERR_NONFATAL, "braces do not "
4512 "enclose all of macro parameter");
4513 }
4514 paramsize[nparam] += white + 1;
4515 white = 0;
4516 } /* parameter loop */
4517 nparam++;
4518 while (m && (m->nparam != nparam ||
4519 mstrcmp(m->name, mname,
4520 m->casesense)))
4521 m = m->next;
4522 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004523 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004524 "macro `%s' exists, "
4525 "but not taking %d parameters",
4526 mstart->text, nparam);
4527 }
4528 }
4529 if (m && m->in_progress)
4530 m = NULL;
4531 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004532 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004533 * Design question: should we handle !tline, which
4534 * indicates missing ')' here, or expand those
4535 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004536 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004537 */
4538 nasm_free(params);
4539 nasm_free(paramsize);
4540 tline = mstart;
4541 } else {
4542 /*
4543 * Expand the macro: we are placed on the last token of the
4544 * call, so that we can easily split the call from the
4545 * following tokens. We also start by pushing an SMAC_END
4546 * token for the cycle removal.
4547 */
4548 t = tline;
4549 if (t) {
4550 tline = t->next;
4551 t->next = NULL;
4552 }
4553 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004554 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004555 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004556 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004557 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004558 if (t->type >= TOK_SMAC_PARAM) {
4559 Token *pcopy = tline, **ptail = &pcopy;
4560 Token *ttt, *pt;
4561 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004562
H. Peter Anvine2c80182005-01-15 22:15:51 +00004563 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004564 i = paramsize[t->type - TOK_SMAC_PARAM];
4565 while (--i >= 0) {
4566 pt = *ptail = new_Token(tline, ttt->type,
4567 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004568 ptail = &pt->next;
4569 ttt = ttt->next;
4570 }
4571 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004572 } else if (t->type == TOK_PREPROC_Q) {
4573 tt = new_Token(tline, TOK_ID, mname, 0);
4574 tline = tt;
4575 } else if (t->type == TOK_PREPROC_QQ) {
4576 tt = new_Token(tline, TOK_ID, m->name, 0);
4577 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004578 } else {
4579 tt = new_Token(tline, t->type, t->text, 0);
4580 tline = tt;
4581 }
4582 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004583
H. Peter Anvine2c80182005-01-15 22:15:51 +00004584 /*
4585 * Having done that, get rid of the macro call, and clean
4586 * up the parameters.
4587 */
4588 nasm_free(params);
4589 nasm_free(paramsize);
4590 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004591 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004592 continue; /* main token loop */
4593 }
4594 }
4595 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004596
H. Peter Anvine2c80182005-01-15 22:15:51 +00004597 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004598 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004599 tline = delete_Token(tline);
4600 } else {
4601 t = *tail = tline;
4602 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004603 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004604 t->next = NULL;
4605 tail = &t->next;
4606 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004607 }
4608
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004609 /*
4610 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004611 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004612 * TOK_IDs should be concatenated.
4613 * Also we look for %+ tokens and concatenate the tokens before and after
4614 * them (without white spaces in between).
4615 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004616 if (expanded) {
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004617 const struct tokseq_match t[] = {
4618 {
4619 PP_CONCAT_MASK(TOK_ID) |
4620 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4621 PP_CONCAT_MASK(TOK_ID) |
4622 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4623 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4624 }
4625 };
4626 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004627 /*
4628 * If we concatenated something, *and* we had previously expanded
4629 * an actual macro, scan the lines again for macros...
4630 */
4631 tline = thead;
4632 expanded = false;
4633 goto again;
4634 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004635 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004636
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004637err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004638 if (org_tline) {
4639 if (thead) {
4640 *org_tline = *thead;
4641 /* since we just gave text to org_line, don't free it */
4642 thead->text = NULL;
4643 delete_Token(thead);
4644 } else {
4645 /* the expression expanded to empty line;
4646 we can't return NULL for some reasons
4647 we just set the line to a single WHITESPACE token. */
4648 memset(org_tline, 0, sizeof(*org_tline));
4649 org_tline->text = NULL;
4650 org_tline->type = TOK_WHITESPACE;
4651 }
4652 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004653 }
4654
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004655 return thead;
4656}
4657
4658/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004659 * Similar to expand_smacro but used exclusively with macro identifiers
4660 * right before they are fetched in. The reason is that there can be
4661 * identifiers consisting of several subparts. We consider that if there
4662 * are more than one element forming the name, user wants a expansion,
4663 * otherwise it will be left as-is. Example:
4664 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004665 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004666 *
4667 * the identifier %$abc will be left as-is so that the handler for %define
4668 * will suck it and define the corresponding value. Other case:
4669 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004670 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004671 *
4672 * In this case user wants name to be expanded *before* %define starts
4673 * working, so we'll expand %$abc into something (if it has a value;
4674 * otherwise it will be left as-is) then concatenate all successive
4675 * PP_IDs into one.
4676 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004677static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004678{
4679 Token *cur, *oldnext = NULL;
4680
H. Peter Anvin734b1882002-04-30 21:01:08 +00004681 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004682 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004683
4684 cur = tline;
4685 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004686 (cur->next->type == TOK_ID ||
4687 cur->next->type == TOK_PREPROC_ID
4688 || cur->next->type == TOK_NUMBER))
4689 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004690
4691 /* If identifier consists of just one token, don't expand */
4692 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004693 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004694
H. Peter Anvine2c80182005-01-15 22:15:51 +00004695 if (cur) {
4696 oldnext = cur->next; /* Detach the tail past identifier */
4697 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004698 }
4699
H. Peter Anvin734b1882002-04-30 21:01:08 +00004700 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004701
H. Peter Anvine2c80182005-01-15 22:15:51 +00004702 if (cur) {
4703 /* expand_smacro possibly changhed tline; re-scan for EOL */
4704 cur = tline;
4705 while (cur && cur->next)
4706 cur = cur->next;
4707 if (cur)
4708 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004709 }
4710
4711 return tline;
4712}
4713
4714/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004715 * Determine whether the given line constitutes a multi-line macro
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004716 * call, and return the ExpDef structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004717 * to check for an initial label - that's taken care of in
4718 * expand_mmacro - but must check numbers of parameters. Guaranteed
4719 * to be called with tline->type == TOK_ID, so the putative macro
4720 * name is easy to find.
4721 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004722static ExpDef *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004723{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004724 ExpDef *head, *ed;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004725 Token **params;
4726 int nparam;
4727
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004728 head = (ExpDef *) hash_findix(&expdefs, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004729
4730 /*
4731 * Efficiency: first we see if any macro exists with the given
4732 * name. If not, we can return NULL immediately. _Then_ we
4733 * count the parameters, and then we look further along the
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004734 * list if necessary to find the proper ExpDef.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004735 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004736 list_for_each(ed, head)
4737 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004738 break;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004739 if (!ed)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004740 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004741
4742 /*
4743 * OK, we have a potential macro. Count and demarcate the
4744 * parameters.
4745 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004746 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004747
4748 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004749 * So we know how many parameters we've got. Find the ExpDef
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004750 * structure that handles this number.
4751 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004752 while (ed) {
4753 if (ed->nparam_min <= nparam
4754 && (ed->plus || nparam <= ed->nparam_max)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004755 /*
4756 * It's right, and we can use it. Add its default
4757 * parameters to the end of our list if necessary.
4758 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004759 if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004760 params =
4761 nasm_realloc(params,
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004762 ((ed->nparam_min + ed->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004763 1) * sizeof(*params)));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004764 while (nparam < ed->nparam_min + ed->ndefs) {
4765 params[nparam] = ed->defaults[nparam - ed->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004766 nparam++;
4767 }
4768 }
4769 /*
4770 * If we've gone over the maximum parameter count (and
4771 * we're in Plus mode), ignore parameters beyond
4772 * nparam_max.
4773 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004774 if (ed->plus && nparam > ed->nparam_max)
4775 nparam = ed->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004776 /*
4777 * Then terminate the parameter list, and leave.
4778 */
4779 if (!params) { /* need this special case */
4780 params = nasm_malloc(sizeof(*params));
4781 nparam = 0;
4782 }
4783 params[nparam] = NULL;
4784 *params_array = params;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004785 return ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004786 }
4787 /*
4788 * This one wasn't right: look for the next one with the
4789 * same name.
4790 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004791 list_for_each(ed, ed->next)
4792 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004793 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004794 }
4795
4796 /*
4797 * After all that, we didn't find one with the right number of
4798 * parameters. Issue a warning, and fail to expand the macro.
4799 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004800 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004801 "macro `%s' exists, but not taking %d parameters",
4802 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004803 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004804 return NULL;
4805}
4806
4807/*
4808 * Expand the multi-line macro call made by the given line, if
4809 * there is one to be expanded. If there is, push the expansion on
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004810 * istk->expansion and return true. Otherwise return false.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004811 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004812static bool expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004813{
H. Peter Anvineba20a72002-04-30 20:53:55 +00004814 Token *label = NULL;
4815 int dont_prepend = 0;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004816 Token **params, *t, *mtok;
4817 Line *l = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004818 ExpDef *ed;
4819 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004820 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004821 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004822
4823 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004824 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004825 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004826 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004827 return false;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004828 mtok = t;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004829 ed = is_mmacro(t, &params);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004830 if (ed != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004831 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004832 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004833 Token *last;
4834 /*
4835 * We have an id which isn't a macro call. We'll assume
4836 * it might be a label; we'll also check to see if a
4837 * colon follows it. Then, if there's another id after
4838 * that lot, we'll check it again for macro-hood.
4839 */
4840 label = last = t;
4841 t = t->next;
4842 if (tok_type_(t, TOK_WHITESPACE))
4843 last = t, t = t->next;
4844 if (tok_is_(t, ":")) {
4845 dont_prepend = 1;
4846 last = t, t = t->next;
4847 if (tok_type_(t, TOK_WHITESPACE))
4848 last = t, t = t->next;
4849 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004850 if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, &params)))
4851 return false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004852 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004853 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004854 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004855 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004856
4857 /*
4858 * Fix up the parameters: this involves stripping leading and
4859 * trailing whitespace, then stripping braces if they are
4860 * present.
4861 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004862 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004863 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004864
H. Peter Anvine2c80182005-01-15 22:15:51 +00004865 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004866 int brace = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004867 int comma = (!ed->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004868
H. Peter Anvine2c80182005-01-15 22:15:51 +00004869 t = params[i];
4870 skip_white_(t);
4871 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004872 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004873 params[i] = t;
4874 paramlen[i] = 0;
4875 while (t) {
4876 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4877 break; /* ... because we have hit a comma */
4878 if (comma && t->type == TOK_WHITESPACE
4879 && tok_is_(t->next, ","))
4880 break; /* ... or a space then a comma */
4881 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4882 break; /* ... or a brace */
4883 t = t->next;
4884 paramlen[i]++;
4885 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004886 }
4887
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004888 if (ed->cur_depth >= ed->max_depth) {
4889 if (ed->max_depth > 1) {
4890 error(ERR_WARNING,
4891 "reached maximum macro recursion depth of %i for %s",
4892 ed->max_depth,ed->name);
4893 }
4894 return false;
4895 } else {
4896 ed->cur_depth ++;
4897 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004898
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004899 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004900 * OK, we have found a ExpDef structure representing a
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004901 * previously defined mmacro. Create an expansion invocation
4902 * and point it back to the expansion definition. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004903 * parameter tokens and macro-local tokens doesn't get done
4904 * until the single-line macro substitution process; this is
4905 * because delaying them allows us to change the semantics
4906 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004907 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004908 ei = new_ExpInv(EXP_MMACRO, ed);
4909 ei->name = nasm_strdup(mname);
4910 //ei->label = label;
4911 //ei->label_text = detoken(label, false);
4912 ei->current = ed->line;
4913 ei->emitting = true;
4914 //ei->iline = tline;
4915 ei->params = params;
4916 ei->nparam = nparam;
4917 ei->rotate = 0;
4918 ei->paramlen = paramlen;
4919 ei->lineno = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004920
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004921 ei->prev = istk->expansion;
4922 istk->expansion = ei;
4923
4924 /*
4925 * Special case: detect %00 on first invocation; if found,
4926 * avoid emitting any labels that precede the mmacro call.
4927 * ed->prepend is set to -1 when %00 is detected, else 1.
4928 */
4929 if (ed->prepend == 0) {
4930 for (l = ed->line; l != NULL; l = l->next) {
4931 for (t = l->first; t != NULL; t = t->next) {
4932 if ((t->type == TOK_PREPROC_ID) &&
4933 (strlen(t->text) == 3) &&
4934 (t->text[1] == '0') && (t->text[2] == '0')) {
4935 dont_prepend = -1;
4936 break;
4937 }
4938 }
4939 if (dont_prepend < 0) {
4940 break;
4941 }
4942 }
4943 ed->prepend = ((dont_prepend < 0) ? -1 : 1);
4944 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004945
4946 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004947 * If we had a label, push it on as the first line of
4948 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004949 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004950 if (label != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004951 if (ed->prepend < 0) {
4952 ei->label_text = detoken(label, false);
4953 } else {
4954 if (dont_prepend == 0) {
4955 t = label;
4956 while (t->next != NULL) {
4957 t = t->next;
4958 }
4959 t->next = new_Token(NULL, TOK_OTHER, ":", 0);
4960 }
4961 l = new_Line();
4962 l->first = copy_Token(label);
4963 l->next = ei->current;
4964 ei->current = l;
4965 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004966 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004967
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004968 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004969
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004970 istk->mmac_depth++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004971 return true;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004972}
4973
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004974/* The function that actually does the error reporting */
4975static void verror(int severity, const char *fmt, va_list arg)
4976{
4977 char buff[1024];
4978
4979 vsnprintf(buff, sizeof(buff), fmt, arg);
4980
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03004981 if (istk && istk->mmac_depth > 0) {
4982 ExpInv *ei = istk->expansion;
4983 int lineno = ei->lineno;
4984 while (ei) {
4985 if (ei->type == EXP_MMACRO)
4986 break;
4987 lineno += ei->relno;
4988 ei = ei->prev;
4989 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004990 nasm_error(severity, "(%s:%d) %s", ei->def->name,
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03004991 lineno, buff);
4992 } else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07004993 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004994}
4995
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004996/*
4997 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004998 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004999 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005000static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005001{
5002 va_list arg;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005003 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005004 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00005005 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005006}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005007
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005008/*
5009 * Because %else etc are evaluated in the state context
5010 * of the previous branch, errors might get lost with error():
5011 * %if 0 ... %else trailing garbage ... %endif
5012 * So %else etc should report errors with this function.
5013 */
5014static void error_precond(int severity, const char *fmt, ...)
5015{
5016 va_list arg;
5017
5018 /* Only ignore the error if it's really in a dead branch */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005019 if ((istk != NULL) &&
5020 (istk->expansion != NULL) &&
5021 (istk->expansion->type == EXP_IF) &&
5022 (istk->expansion->def->state == COND_NEVER))
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005023 return;
5024
5025 va_start(arg, fmt);
5026 verror(severity, fmt, arg);
5027 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005028}
5029
H. Peter Anvin734b1882002-04-30 21:01:08 +00005030static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005031pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005032{
H. Peter Anvin7383b402008-09-24 10:20:40 -07005033 Token *t;
5034
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005035 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005036 istk = nasm_malloc(sizeof(Include));
5037 istk->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005038 istk->expansion = NULL;
5039 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00005040 istk->fname = NULL;
5041 src_set_fname(nasm_strdup(file));
5042 src_set_linnum(0);
5043 istk->lineinc = 1;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005044 istk->mmac_depth = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005045 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07005046 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00005047 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005048 defining = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005049 finals = NULL;
5050 in_final = false;
Charles Crayned4200be2008-07-12 16:42:33 -07005051 nested_mac_count = 0;
5052 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005053 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005054 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005055 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005056 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005057 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005058 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005059 }
H. Peter Anvind2456592008-06-19 15:04:18 -07005060 any_extrastdmac = extrastdmac && *extrastdmac;
5061 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005062 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005063
5064 /*
5065 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5066 * The caller, however, will also pass in 3 for preprocess-only so
5067 * we can set __PASS__ accordingly.
5068 */
5069 pass = apass > 2 ? 2 : apass;
5070
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005071 dephead = deptail = deplist;
5072 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005073 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
5074 sl->next = NULL;
5075 strcpy(sl->str, file);
5076 *deptail = sl;
5077 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005078 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07005079
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005080 /*
5081 * Define the __PASS__ macro. This is defined here unlike
5082 * all the other builtins, because it is special -- it varies between
5083 * passes.
5084 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07005085 t = nasm_malloc(sizeof(*t));
5086 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005087 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07005088 t->a.mac = NULL;
5089 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005090}
5091
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005092static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005093{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005094 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005095 Token *tline;
Keith Kanios9858cec2010-11-08 00:58:02 -06005096 ExpDef *ed;
5097 ExpInv *ei;
5098 Line *l;
5099 int j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005100
H. Peter Anvine2c80182005-01-15 22:15:51 +00005101 while (1) {
5102 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005103 * Fetch a tokenized line, either from the expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005104 * buffer or from the input file.
5105 */
5106 tline = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005107
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005108 while (1) { /* until we get a line we can use */
5109 /*
5110 * Fetch a tokenized line from the expansion buffer
5111 */
5112 if (istk->expansion != NULL) {
5113 ei = istk->expansion;
5114 if (ei->current != NULL) {
5115 if (ei->emitting == false) {
5116 ei->current = NULL;
5117 continue;
5118 }
5119 l = ei->current;
5120 ei->current = l->next;
5121 ei->lineno++;
5122 tline = copy_Token(l->first);
5123 if (((ei->type == EXP_REP) ||
5124 (ei->type == EXP_MMACRO) ||
5125 (ei->type == EXP_WHILE))
5126 && (ei->def->nolist == false)) {
5127 char *p = detoken(tline, false);
5128 list->line(LIST_MACRO, p);
5129 nasm_free(p);
5130 }
5131 if (ei->linnum > -1) {
5132 src_set_linnum(src_get_linnum() + 1);
5133 }
5134 break;
5135 } else if ((ei->type == EXP_REP) &&
5136 (ei->def->cur_depth < ei->def->max_depth)) {
5137 ei->def->cur_depth ++;
5138 ei->current = ei->def->line;
5139 ei->lineno = 0;
5140 continue;
5141 } else if ((ei->type == EXP_WHILE) &&
5142 (ei->def->cur_depth < ei->def->max_depth)) {
5143 ei->current = ei->def->line;
5144 ei->lineno = 0;
5145 tline = copy_Token(ei->current->first);
5146 j = if_condition(tline, PP_WHILE);
5147 tline = NULL;
5148 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
5149 if (j == COND_IF_TRUE) {
5150 ei->current = ei->current->next;
5151 ei->def->cur_depth ++;
5152 } else {
5153 ei->emitting = false;
5154 ei->current = NULL;
5155 ei->def->cur_depth = ei->def->max_depth;
5156 }
5157 continue;
5158 } else {
5159 istk->expansion = ei->prev;
5160 ed = ei->def;
5161 if (ed != NULL) {
5162 if ((ei->emitting == true) &&
5163 (ed->max_depth == DEADMAN_LIMIT) &&
5164 (ed->cur_depth == DEADMAN_LIMIT)
5165 ) {
5166 error(ERR_FATAL, "runaway expansion detected, aborting");
5167 }
5168 if (ed->cur_depth > 0) {
5169 ed->cur_depth --;
5170 } else if ((ed->type != EXP_MMACRO) && (ed->type != EXP_IF)) {
5171 /***** should this really be right here??? *****/
5172 /*
5173 Line *l = NULL, *ll = NULL;
5174 for (l = ed->line; l != NULL;) {
5175 if (l->first != NULL) {
5176 free_tlist(l->first);
5177 l->first = NULL;
5178 }
5179 ll = l;
5180 l = l->next;
5181 nasm_free(ll);
5182 }
5183 expansions = ed->prev;
5184 nasm_free(ed);
5185 */
5186 }
5187 if ((ei->type == EXP_REP) ||
5188 (ei->type == EXP_MMACRO) ||
5189 (ei->type == EXP_WHILE)) {
5190 list->downlevel(LIST_MACRO);
5191 if (ei->type == EXP_MMACRO) {
5192 istk->mmac_depth--;
5193 }
5194 }
5195 }
5196 if (ei->linnum > -1) {
5197 src_set_linnum(ei->linnum);
5198 }
5199 free_expinv(ei);
5200 continue;
5201 }
5202 }
5203
5204 /*
5205 * Read in line from input and tokenize
5206 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005207 line = read_line();
5208 if (line) { /* from the current input file */
5209 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005210 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005211 nasm_free(line);
5212 break;
5213 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005214
H. Peter Anvine2c80182005-01-15 22:15:51 +00005215 /*
5216 * The current file has ended; work down the istk
5217 */
5218 {
5219 Include *i = istk;
5220 fclose(i->fp);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005221 if (i->expansion != NULL) {
5222 error(ERR_FATAL,
5223 "end of file while still in an expansion");
5224 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005225 /* only set line and file name if there's a next node */
5226 if (i->next) {
5227 src_set_linnum(i->lineno);
5228 nasm_free(src_set_fname(i->fname));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005229 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005230 if ((i->next == NULL) && (finals != NULL)) {
5231 in_final = true;
5232 ei = new_ExpInv(EXP_FINAL, NULL);
5233 ei->emitting = true;
5234 ei->current = finals;
5235 istk->expansion = ei;
5236 finals = NULL;
5237 continue;
5238 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005239 istk = i->next;
5240 list->downlevel(LIST_INCLUDE);
5241 nasm_free(i);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005242 if (istk == NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005243 if (finals != NULL) {
5244 in_final = true;
5245 } else {
5246 return NULL;
5247 }
5248 }
5249 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005250 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005251 }
5252
5253 if (defining == NULL) {
5254 tline = expand_mmac_params(tline);
5255 }
5256
H. Peter Anvine2c80182005-01-15 22:15:51 +00005257 /*
5258 * Check the line to see if it's a preprocessor directive.
5259 */
5260 if (do_directive(tline) == DIRECTIVE_FOUND) {
5261 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005262 } else if (defining != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005263 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005264 * We're defining an expansion. We emit nothing at all,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005265 * and just shove the tokenized line on to the definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005266 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005267 if (defining->ignoring == false) {
5268 Line *l = new_Line();
5269 l->first = tline;
5270 if (defining->line == NULL) {
5271 defining->line = l;
5272 defining->last = l;
5273 } else {
5274 defining->last->next = l;
5275 defining->last = l;
5276 }
5277 } else {
5278 //free_tlist(tline); /***** sanity check: is this supposed to be here? *****/
5279 }
5280 defining->linecount++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005281 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005282 } else if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005283 (istk->expansion->emitting != true)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005284 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005285 * We're in a non-emitting branch of an expansion.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005286 * Emit nothing at all, not even a blank line: when we
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005287 * emerge from the expansion we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005288 * directive so we keep our place correctly.
5289 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005290 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005291 continue;
5292 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005293 tline = expand_smacro(tline);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005294 if (expand_mmacro(tline) != true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005295 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005296 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005297 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005298 line = detoken(tline, true);
5299 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005300 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005301 } else {
5302 continue;
5303 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005304 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005305 }
5306 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005307}
5308
H. Peter Anvine2c80182005-01-15 22:15:51 +00005309static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005310{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005311 if (defining != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005312 error(ERR_NONFATAL, "end of file while still defining an expansion");
5313 while (defining != NULL) {
5314 ExpDef *ed = defining;
5315 defining = ed->prev;
5316 free_expdef(ed);
5317 }
5318 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005319 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005320 while (cstk != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005321 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005322 free_macros();
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005323 while (istk != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005324 Include *i = istk;
5325 istk = istk->next;
5326 fclose(i->fp);
5327 nasm_free(i->fname);
5328 nasm_free(i);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005329 while (i->expansion != NULL) {
5330 ExpInv *ei = i->expansion;
5331 i->expansion = ei->prev;
5332 free_expinv(ei);
5333 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005334 }
5335 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005336 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005337 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005338 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005339 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005340 free_llist(predef);
5341 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005342 while ((i = ipath)) {
5343 ipath = i->next;
5344 if (i->path)
5345 nasm_free(i->path);
5346 nasm_free(i);
5347 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005348 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005349}
5350
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005351void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005352{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005353 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005354
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005355 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005356 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005357 i->next = NULL;
5358
H. Peter Anvin89cee572009-07-15 09:16:54 -04005359 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005360 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005361 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005362 j = j->next;
5363 j->next = i;
5364 } else {
5365 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005366 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005367}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005368
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005369void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005370{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005371 Token *inc, *space, *name;
5372 Line *l;
5373
H. Peter Anvin734b1882002-04-30 21:01:08 +00005374 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5375 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5376 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005377
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005378 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005379 l->next = predef;
5380 l->first = inc;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005381 predef = l;
5382}
5383
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005384void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005385{
5386 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005387 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005388 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005389
5390 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005391 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5392 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005393 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005394 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005395 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005396 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005397 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005398
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005399 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005400 l->next = predef;
5401 l->first = def;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005402 predef = l;
5403}
5404
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005405void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005406{
5407 Token *def, *space;
5408 Line *l;
5409
H. Peter Anvin734b1882002-04-30 21:01:08 +00005410 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5411 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005412 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005413
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005414 l = new_Line();
H. Peter Anvin620515a2002-04-30 20:57:38 +00005415 l->next = predef;
5416 l->first = def;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005417 predef = l;
5418}
5419
Keith Kaniosb7a89542007-04-12 02:40:54 +00005420/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005421 * This function is used to assist with "runtime" preprocessor
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005422 * directives, e.g. pp_runtime("%define __BITS__ 64");
Keith Kaniosb7a89542007-04-12 02:40:54 +00005423 *
5424 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5425 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5426 */
5427
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005428void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005429{
5430 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005431
Keith Kaniosb7a89542007-04-12 02:40:54 +00005432 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005433 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005434 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005435
Keith Kaniosb7a89542007-04-12 02:40:54 +00005436}
5437
H. Peter Anvina70547f2008-07-19 21:44:26 -07005438void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005439{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005440 extrastdmac = macros;
5441}
5442
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005443static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005444{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005445 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005446 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005447 tok->text = nasm_strdup(numbuf);
5448 tok->type = TOK_NUMBER;
5449}
5450
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005451Preproc nasmpp = {
5452 pp_reset,
5453 pp_getline,
5454 pp_cleanup
5455};