blob: df59cf057caaa4e56a18604e6b5e54138ba0565b [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 Gorcunovaccda192010-02-16 10:27:56 +03001658 memcpy(sl->str, prefix, prefix_len);
1659 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001660 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001661 if (fp && dhead && !in_list(*dhead, sl->str)) {
1662 sl->next = NULL;
1663 **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 }
1949 searching.name = nasm_strdup(tline->text);
1950 searching.casesense = true;
1951 searching.plus = false;
1952 searching.nolist = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001953 //searching.in_progress = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001954 searching.max_depth = 0;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001955 //searching.rep_nest = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001956 searching.nparam_min = 0;
1957 searching.nparam_max = INT_MAX;
1958 tline = expand_smacro(tline->next);
1959 skip_white_(tline);
1960 if (!tline) {
1961 } else if (!tok_type_(tline, TOK_NUMBER)) {
1962 error(ERR_NONFATAL,
1963 "`%s' expects a parameter count or nothing",
1964 pp_directives[ct]);
1965 } else {
1966 searching.nparam_min = searching.nparam_max =
1967 readnum(tline->text, &j);
1968 if (j)
1969 error(ERR_NONFATAL,
1970 "unable to parse parameter count `%s'",
1971 tline->text);
1972 }
1973 if (tline && tok_is_(tline->next, "-")) {
1974 tline = tline->next->next;
1975 if (tok_is_(tline, "*"))
1976 searching.nparam_max = INT_MAX;
1977 else if (!tok_type_(tline, TOK_NUMBER))
1978 error(ERR_NONFATAL,
1979 "`%s' expects a parameter count after `-'",
1980 pp_directives[ct]);
1981 else {
1982 searching.nparam_max = readnum(tline->text, &j);
1983 if (j)
1984 error(ERR_NONFATAL,
1985 "unable to parse parameter count `%s'",
1986 tline->text);
1987 if (searching.nparam_min > searching.nparam_max)
1988 error(ERR_NONFATAL,
1989 "minimum parameter count exceeds maximum");
1990 }
1991 }
1992 if (tline && tok_is_(tline->next, "+")) {
1993 tline = tline->next;
1994 searching.plus = true;
1995 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001996 ed = (ExpDef *) hash_findix(&expdefs, searching.name);
1997 while (ed != NULL) {
1998 if (!strcmp(ed->name, searching.name) &&
1999 (ed->nparam_min <= searching.nparam_max
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002000 || searching.plus)
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002001 && (searching.nparam_min <= ed->nparam_max
2002 || ed->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002003 found = true;
2004 break;
2005 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002006 ed = ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002007 }
2008 if (tline && tline->next)
2009 error(ERR_WARNING|ERR_PASS1,
2010 "trailing garbage after %%ifmacro ignored");
2011 nasm_free(searching.name);
2012 j = found;
2013 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002014 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002015
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002016 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002017 needtype = TOK_ID;
2018 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002019 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002020 needtype = TOK_NUMBER;
2021 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002022 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002023 needtype = TOK_STRING;
2024 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002025
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002026iftype:
2027 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002028
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002029 while (tok_type_(t, TOK_WHITESPACE) ||
2030 (needtype == TOK_NUMBER &&
2031 tok_type_(t, TOK_OTHER) &&
2032 (t->text[0] == '-' || t->text[0] == '+') &&
2033 !t->text[1]))
2034 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002035
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002036 j = tok_type_(t, needtype);
2037 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002038
2039 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002040 t = tline = expand_smacro(tline);
2041 while (tok_type_(t, TOK_WHITESPACE))
2042 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002043
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002044 j = false;
2045 if (t) {
2046 t = t->next; /* Skip the actual token */
2047 while (tok_type_(t, TOK_WHITESPACE))
2048 t = t->next;
2049 j = !t; /* Should be nothing left */
2050 }
2051 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002052
H. Peter Anvin134b9462008-02-16 17:01:40 -08002053 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002054 t = tline = expand_smacro(tline);
2055 while (tok_type_(t, TOK_WHITESPACE))
2056 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002057
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002058 j = !t; /* Should be empty */
2059 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002060
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002061 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002062 t = tline = expand_smacro(tline);
2063 tptr = &t;
2064 tokval.t_type = TOKEN_INVALID;
2065 evalresult = evaluate(ppscan, tptr, &tokval,
2066 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002067 if (!evalresult)
2068 return -1;
2069 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002070 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002071 "trailing garbage after expression ignored");
2072 if (!is_simple(evalresult)) {
2073 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002074 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002075 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002076 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002077 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002078 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002079
H. Peter Anvine2c80182005-01-15 22:15:51 +00002080 default:
2081 error(ERR_FATAL,
2082 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002083 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002084 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002085 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002086
2087 free_tlist(origline);
2088 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002089
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002090fail:
2091 free_tlist(origline);
2092 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002093}
2094
2095/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002096 * Common code for defining an smacro
2097 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002098static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002099 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002100{
2101 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002102 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002103
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002104 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002105 if (!smac) {
2106 error(ERR_WARNING|ERR_PASS1,
2107 "single-line macro `%s' defined both with and"
2108 " without parameters", mname);
2109 /*
2110 * Some instances of the old code considered this a failure,
2111 * some others didn't. What is the right thing to do here?
2112 */
2113 free_tlist(expansion);
2114 return false; /* Failure */
2115 } else {
2116 /*
2117 * We're redefining, so we have to take over an
2118 * existing SMacro structure. This means freeing
2119 * what was already in it.
2120 */
2121 nasm_free(smac->name);
2122 free_tlist(smac->expansion);
2123 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002124 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002125 smtbl = ctx ? &ctx->localmac : &smacros;
2126 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2127 smac = nasm_malloc(sizeof(SMacro));
2128 smac->next = *smhead;
2129 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002130 }
2131 smac->name = nasm_strdup(mname);
2132 smac->casesense = casesense;
2133 smac->nparam = nparam;
2134 smac->expansion = expansion;
2135 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002136 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002137}
2138
2139/*
2140 * Undefine an smacro
2141 */
2142static void undef_smacro(Context *ctx, const char *mname)
2143{
2144 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002145 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002146
H. Peter Anvin166c2472008-05-28 12:28:58 -07002147 smtbl = ctx ? &ctx->localmac : &smacros;
2148 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002149
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002150 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002151 /*
2152 * We now have a macro name... go hunt for it.
2153 */
2154 sp = smhead;
2155 while ((s = *sp) != NULL) {
2156 if (!mstrcmp(s->name, mname, s->casesense)) {
2157 *sp = s->next;
2158 nasm_free(s->name);
2159 free_tlist(s->expansion);
2160 nasm_free(s);
2161 } else {
2162 sp = &s->next;
2163 }
2164 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002165 }
2166}
2167
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002168/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002169 * Parse a mmacro specification.
2170 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002171static bool parse_mmacro_spec(Token *tline, ExpDef *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002172{
2173 bool err;
2174
2175 tline = tline->next;
2176 skip_white_(tline);
2177 tline = expand_id(tline);
2178 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002179 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2180 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002181 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002182
H. Peter Anvina26433d2008-07-16 14:40:01 -07002183 def->name = nasm_strdup(tline->text);
2184 def->plus = false;
2185 def->nolist = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002186// def->in_progress = 0;
2187// def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002188 def->nparam_min = 0;
2189 def->nparam_max = 0;
2190
H. Peter Anvina26433d2008-07-16 14:40:01 -07002191 tline = expand_smacro(tline->next);
2192 skip_white_(tline);
2193 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002194 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002195 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002196 def->nparam_min = def->nparam_max =
2197 readnum(tline->text, &err);
2198 if (err)
2199 error(ERR_NONFATAL,
2200 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002201 }
2202 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002203 tline = tline->next->next;
2204 if (tok_is_(tline, "*")) {
2205 def->nparam_max = INT_MAX;
2206 } else if (!tok_type_(tline, TOK_NUMBER)) {
2207 error(ERR_NONFATAL,
2208 "`%s' expects a parameter count after `-'", directive);
2209 } else {
2210 def->nparam_max = readnum(tline->text, &err);
2211 if (err) {
2212 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2213 tline->text);
2214 }
2215 if (def->nparam_min > def->nparam_max) {
2216 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2217 }
2218 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002219 }
2220 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002221 tline = tline->next;
2222 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002223 }
2224 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002225 !nasm_stricmp(tline->next->text, ".nolist")) {
2226 tline = tline->next;
2227 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002228 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002229
H. Peter Anvina26433d2008-07-16 14:40:01 -07002230 /*
2231 * Handle default parameters.
2232 */
2233 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002234 def->dlist = tline->next;
2235 tline->next = NULL;
2236 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002237 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002238 def->dlist = NULL;
2239 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002240 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002241 def->line = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002242
H. Peter Anvin89cee572009-07-15 09:16:54 -04002243 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002244 !def->plus)
2245 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2246 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002247
H. Peter Anvina26433d2008-07-16 14:40:01 -07002248 return true;
2249}
2250
2251
2252/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002253 * Decode a size directive
2254 */
2255static int parse_size(const char *str) {
2256 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002257 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002258 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002259 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002260
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002261 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002262}
2263
Ed Beroset3ab3f412002-06-11 03:31:49 +00002264/**
2265 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002266 * Find out if a line contains a preprocessor directive, and deal
2267 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002268 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002269 * If a directive _is_ found, it is the responsibility of this routine
2270 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002271 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002272 * @param tline a pointer to the current tokeninzed line linked list
2273 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002274 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002275 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002276static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002277{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002278 enum preproc_token i;
2279 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002280 bool err;
2281 int nparam;
2282 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002283 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002284 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002285 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002286 char *p, *pp;
2287 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002288 Include *inc;
2289 Context *ctx;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002290 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002291 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002292 struct tokenval tokval;
2293 expr *evalresult;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002294 ExpDef *ed, *eed, **edhead;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002295 ExpInv *ei, *eei;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002296 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002297 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002298 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002299
2300 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002301
H. Peter Anvineba20a72002-04-30 20:53:55 +00002302 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002303 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002304 (tline->text[1] == '%' || tline->text[1] == '$'
2305 || tline->text[1] == '!'))
2306 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002307
H. Peter Anvin4169a472007-09-12 01:29:43 +00002308 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002309
H. Peter Anvin4169a472007-09-12 01:29:43 +00002310 switch (i) {
2311 case PP_INVALID:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002312 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002313 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2314 tline->text);
2315 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002316
H. Peter Anvine2c80182005-01-15 22:15:51 +00002317 case PP_STACKSIZE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002318 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002319 /* Directive to tell NASM what the default stack size is. The
2320 * default is for a 16-bit stack, and this can be overriden with
2321 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002322 */
2323 tline = tline->next;
2324 if (tline && tline->type == TOK_WHITESPACE)
2325 tline = tline->next;
2326 if (!tline || tline->type != TOK_ID) {
2327 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2328 free_tlist(origline);
2329 return DIRECTIVE_FOUND;
2330 }
2331 if (nasm_stricmp(tline->text, "flat") == 0) {
2332 /* All subsequent ARG directives are for a 32-bit stack */
2333 StackSize = 4;
2334 StackPointer = "ebp";
2335 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002336 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002337 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2338 /* All subsequent ARG directives are for a 64-bit stack */
2339 StackSize = 8;
2340 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002341 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002342 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002343 } else if (nasm_stricmp(tline->text, "large") == 0) {
2344 /* All subsequent ARG directives are for a 16-bit stack,
2345 * far function call.
2346 */
2347 StackSize = 2;
2348 StackPointer = "bp";
2349 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002350 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002351 } else if (nasm_stricmp(tline->text, "small") == 0) {
2352 /* All subsequent ARG directives are for a 16-bit stack,
2353 * far function call. We don't support near functions.
2354 */
2355 StackSize = 2;
2356 StackPointer = "bp";
2357 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002358 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002359 } else {
2360 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2361 free_tlist(origline);
2362 return DIRECTIVE_FOUND;
2363 }
2364 free_tlist(origline);
2365 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002366
H. Peter Anvine2c80182005-01-15 22:15:51 +00002367 case PP_ARG:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002368 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002369 /* TASM like ARG directive to define arguments to functions, in
2370 * the following form:
2371 *
2372 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2373 */
2374 offset = ArgOffset;
2375 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002376 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002377 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002378
H. Peter Anvine2c80182005-01-15 22:15:51 +00002379 /* Find the argument name */
2380 tline = tline->next;
2381 if (tline && tline->type == TOK_WHITESPACE)
2382 tline = tline->next;
2383 if (!tline || tline->type != TOK_ID) {
2384 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2385 free_tlist(origline);
2386 return DIRECTIVE_FOUND;
2387 }
2388 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002389
H. Peter Anvine2c80182005-01-15 22:15:51 +00002390 /* Find the argument size type */
2391 tline = tline->next;
2392 if (!tline || tline->type != TOK_OTHER
2393 || tline->text[0] != ':') {
2394 error(ERR_NONFATAL,
2395 "Syntax error processing `%%arg' directive");
2396 free_tlist(origline);
2397 return DIRECTIVE_FOUND;
2398 }
2399 tline = tline->next;
2400 if (!tline || tline->type != TOK_ID) {
2401 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2402 free_tlist(origline);
2403 return DIRECTIVE_FOUND;
2404 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002405
H. Peter Anvine2c80182005-01-15 22:15:51 +00002406 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002407 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002408 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002409 size = parse_size(tt->text);
2410 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002411 error(ERR_NONFATAL,
2412 "Invalid size type for `%%arg' missing directive");
2413 free_tlist(tt);
2414 free_tlist(origline);
2415 return DIRECTIVE_FOUND;
2416 }
2417 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002418
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002419 /* Round up to even stack slots */
2420 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002421
H. Peter Anvine2c80182005-01-15 22:15:51 +00002422 /* Now define the macro for the argument */
2423 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2424 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002425 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002426 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002427
H. Peter Anvine2c80182005-01-15 22:15:51 +00002428 /* Move to the next argument in the list */
2429 tline = tline->next;
2430 if (tline && tline->type == TOK_WHITESPACE)
2431 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002432 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002433 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002434 free_tlist(origline);
2435 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002436
H. Peter Anvine2c80182005-01-15 22:15:51 +00002437 case PP_LOCAL:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002438 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002439 /* TASM like LOCAL directive to define local variables for a
2440 * function, in the following form:
2441 *
2442 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2443 *
2444 * The '= LocalSize' at the end is ignored by NASM, but is
2445 * required by TASM to define the local parameter size (and used
2446 * by the TASM macro package).
2447 */
2448 offset = LocalOffset;
2449 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002450 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002451 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002452
H. Peter Anvine2c80182005-01-15 22:15:51 +00002453 /* Find the argument name */
2454 tline = tline->next;
2455 if (tline && tline->type == TOK_WHITESPACE)
2456 tline = tline->next;
2457 if (!tline || tline->type != TOK_ID) {
2458 error(ERR_NONFATAL,
2459 "`%%local' missing argument parameter");
2460 free_tlist(origline);
2461 return DIRECTIVE_FOUND;
2462 }
2463 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002464
H. Peter Anvine2c80182005-01-15 22:15:51 +00002465 /* Find the argument size type */
2466 tline = tline->next;
2467 if (!tline || tline->type != TOK_OTHER
2468 || tline->text[0] != ':') {
2469 error(ERR_NONFATAL,
2470 "Syntax error processing `%%local' directive");
2471 free_tlist(origline);
2472 return DIRECTIVE_FOUND;
2473 }
2474 tline = tline->next;
2475 if (!tline || tline->type != TOK_ID) {
2476 error(ERR_NONFATAL,
2477 "`%%local' missing size type parameter");
2478 free_tlist(origline);
2479 return DIRECTIVE_FOUND;
2480 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002481
H. Peter Anvine2c80182005-01-15 22:15:51 +00002482 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002483 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002484 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002485 size = parse_size(tt->text);
2486 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002487 error(ERR_NONFATAL,
2488 "Invalid size type for `%%local' missing directive");
2489 free_tlist(tt);
2490 free_tlist(origline);
2491 return DIRECTIVE_FOUND;
2492 }
2493 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002494
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002495 /* Round up to even stack slots */
2496 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002497
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002498 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002499
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002500 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002501 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2502 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002503 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002504
H. Peter Anvine2c80182005-01-15 22:15:51 +00002505 /* Now define the assign to setup the enter_c macro correctly */
2506 snprintf(directive, sizeof(directive),
2507 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002508 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002509
H. Peter Anvine2c80182005-01-15 22:15:51 +00002510 /* Move to the next argument in the list */
2511 tline = tline->next;
2512 if (tline && tline->type == TOK_WHITESPACE)
2513 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002514 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002515 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002516 free_tlist(origline);
2517 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002518
H. Peter Anvine2c80182005-01-15 22:15:51 +00002519 case PP_CLEAR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002520 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002521 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002522 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002523 "trailing garbage after `%%clear' ignored");
2524 free_macros();
2525 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002526 free_tlist(origline);
2527 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002528
H. Peter Anvin418ca702008-05-30 10:42:30 -07002529 case PP_DEPEND:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002530 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002531 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002532 skip_white_(t);
2533 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002534 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002535 error(ERR_NONFATAL, "`%%depend' expects a file name");
2536 free_tlist(origline);
2537 return DIRECTIVE_FOUND; /* but we did _something_ */
2538 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002539 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002540 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002541 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002542 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002543 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002544 nasm_unquote_cstr(p, i);
2545 if (dephead && !in_list(*dephead, p)) {
2546 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2547 sl->next = NULL;
2548 strcpy(sl->str, p);
2549 *deptail = sl;
2550 deptail = &sl->next;
2551 }
2552 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002553 return DIRECTIVE_FOUND;
2554
2555 case PP_INCLUDE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002556 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002557 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002558 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002559
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002560 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002561 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002562 error(ERR_NONFATAL, "`%%include' expects a file name");
2563 free_tlist(origline);
2564 return DIRECTIVE_FOUND; /* but we did _something_ */
2565 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002566 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002567 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002568 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002569 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002570 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002571 nasm_unquote_cstr(p, i);
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03002572 inc = nasm_zalloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002573 inc->next = istk;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002574 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002575 if (!inc->fp) {
2576 /* -MG given but file not found */
2577 nasm_free(inc);
2578 } else {
2579 inc->fname = src_set_fname(nasm_strdup(p));
2580 inc->lineno = src_set_linnum(0);
2581 inc->lineinc = 1;
2582 inc->expansion = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002583 istk = inc;
2584 list->uplevel(LIST_INCLUDE);
2585 }
2586 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002587 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002588
H. Peter Anvind2456592008-06-19 15:04:18 -07002589 case PP_USE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002590 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002591 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002592 static macros_t *use_pkg;
2593 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002594
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002595 tline = tline->next;
2596 skip_white_(tline);
2597 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002598
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002599 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002600 tline->type != TOK_INTERNAL_STRING &&
2601 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002602 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002603 free_tlist(origline);
2604 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002605 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002606 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002607 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002608 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002609 if (tline->type == TOK_STRING)
2610 nasm_unquote_cstr(tline->text, i);
2611 use_pkg = nasm_stdmac_find_package(tline->text);
2612 if (!use_pkg)
2613 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2614 else
2615 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002616 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002617 /* Not already included, go ahead and include it */
2618 stdmacpos = use_pkg;
2619 }
2620 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002621 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002622 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002623 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002624 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002625 case PP_POP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002626 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002627 tline = tline->next;
2628 skip_white_(tline);
2629 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002630 if (tline) {
2631 if (!tok_type_(tline, TOK_ID)) {
2632 error(ERR_NONFATAL, "`%s' expects a context identifier",
2633 pp_directives[i]);
2634 free_tlist(origline);
2635 return DIRECTIVE_FOUND; /* but we did _something_ */
2636 }
2637 if (tline->next)
2638 error(ERR_WARNING|ERR_PASS1,
2639 "trailing garbage after `%s' ignored",
2640 pp_directives[i]);
2641 p = nasm_strdup(tline->text);
2642 } else {
2643 p = NULL; /* Anonymous */
2644 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002645
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002646 if (i == PP_PUSH) {
2647 ctx = nasm_malloc(sizeof(Context));
2648 ctx->next = cstk;
2649 hash_init(&ctx->localmac, HASH_SMALL);
2650 ctx->name = p;
2651 ctx->number = unique++;
2652 cstk = ctx;
2653 } else {
2654 /* %pop or %repl */
2655 if (!cstk) {
2656 error(ERR_NONFATAL, "`%s': context stack is empty",
2657 pp_directives[i]);
2658 } else if (i == PP_POP) {
2659 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2660 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2661 "expected %s",
2662 cstk->name ? cstk->name : "anonymous", p);
2663 else
2664 ctx_pop();
2665 } else {
2666 /* i == PP_REPL */
2667 nasm_free(cstk->name);
2668 cstk->name = p;
2669 p = NULL;
2670 }
2671 nasm_free(p);
2672 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002673 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002674 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002675 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002676 severity = ERR_FATAL;
2677 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002678 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002679 severity = ERR_NONFATAL;
2680 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002681 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002682 severity = ERR_WARNING|ERR_WARN_USER;
2683 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002684
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002685issue_error:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002686 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002687 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002688 /* Only error out if this is the final pass */
2689 if (pass != 2 && i != PP_FATAL)
2690 return DIRECTIVE_FOUND;
2691
2692 tline->next = expand_smacro(tline->next);
2693 tline = tline->next;
2694 skip_white_(tline);
2695 t = tline ? tline->next : NULL;
2696 skip_white_(t);
2697 if (tok_type_(tline, TOK_STRING) && !t) {
2698 /* The line contains only a quoted string */
2699 p = tline->text;
2700 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2701 error(severity, "%s", p);
2702 } else {
2703 /* Not a quoted string, or more than a quoted string */
2704 p = detoken(tline, false);
2705 error(severity, "%s", p);
2706 nasm_free(p);
2707 }
2708 free_tlist(origline);
2709 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002710 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002711
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002712 CASE_PP_IF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002713 if (defining != NULL) {
2714 if (defining->type == EXP_IF) {
2715 defining->def_depth ++;
2716 }
2717 return NO_DIRECTIVE_FOUND;
2718 }
2719 if ((istk->expansion != NULL) &&
2720 (istk->expansion->emitting == false)) {
2721 j = COND_NEVER;
2722 } else {
2723 j = if_condition(tline->next, i);
2724 tline->next = NULL; /* it got freed */
2725 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
2726 }
2727 ed = new_ExpDef(EXP_IF);
2728 ed->state = j;
2729 ed->nolist = NULL;
2730 ed->def_depth = 0;
2731 ed->cur_depth = 0;
2732 ed->max_depth = 0;
2733 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
2734 ed->prev = defining;
2735 defining = ed;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002736 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002737 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002738
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002739 CASE_PP_ELIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002740 if (defining != NULL) {
2741 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2742 return NO_DIRECTIVE_FOUND;
2743 }
2744 }
2745 if ((defining == NULL) || (defining->type != EXP_IF)) {
2746 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2747 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002748 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002749 case COND_IF_TRUE:
2750 defining->state = COND_DONE;
2751 defining->ignoring = true;
2752 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002753
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002754 case COND_DONE:
2755 case COND_NEVER:
2756 defining->ignoring = true;
2757 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002758
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002759 case COND_ELSE_TRUE:
2760 case COND_ELSE_FALSE:
2761 error_precond(ERR_WARNING|ERR_PASS1,
2762 "`%%elif' after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002763 defining->state = COND_NEVER;
2764 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002765 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002766
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002767 case COND_IF_FALSE:
2768 /*
2769 * IMPORTANT: In the case of %if, we will already have
2770 * called expand_mmac_params(); however, if we're
2771 * processing an %elif we must have been in a
2772 * non-emitting mode, which would have inhibited
2773 * the normal invocation of expand_mmac_params().
2774 * Therefore, we have to do it explicitly here.
2775 */
2776 j = if_condition(expand_mmac_params(tline->next), i);
2777 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002778 defining->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002779 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002780 defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002781 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002782 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002783 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002784 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002785
H. Peter Anvine2c80182005-01-15 22:15:51 +00002786 case PP_ELSE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002787 if (defining != NULL) {
2788 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2789 return NO_DIRECTIVE_FOUND;
2790 }
2791 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002792 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002793 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002794 "trailing garbage after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002795 if ((defining == NULL) || (defining->type != EXP_IF)) {
2796 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2797 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002798 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002799 case COND_IF_TRUE:
2800 case COND_DONE:
2801 defining->state = COND_ELSE_FALSE;
2802 defining->ignoring = true;
2803 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002804
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002805 case COND_NEVER:
2806 defining->ignoring = true;
2807 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002808
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002809 case COND_IF_FALSE:
2810 defining->state = COND_ELSE_TRUE;
2811 defining->ignoring = false;
2812 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002813
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002814 case COND_ELSE_TRUE:
2815 case COND_ELSE_FALSE:
2816 error_precond(ERR_WARNING|ERR_PASS1,
2817 "`%%else' after `%%else' ignored.");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002818 defining->state = COND_NEVER;
2819 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002820 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002821 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002822 free_tlist(origline);
2823 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002824
H. Peter Anvine2c80182005-01-15 22:15:51 +00002825 case PP_ENDIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002826 if (defining != NULL) {
2827 if (defining->type == EXP_IF) {
2828 if (defining->def_depth > 0) {
2829 defining->def_depth --;
2830 return NO_DIRECTIVE_FOUND;
2831 }
2832 } else {
2833 return NO_DIRECTIVE_FOUND;
2834 }
2835 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002836 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002837 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002838 "trailing garbage after `%%endif' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002839 if ((defining == NULL) || (defining->type != EXP_IF)) {
2840 error(ERR_NONFATAL, "`%%endif': no matching `%%if'");
2841 return DIRECTIVE_FOUND;
2842 }
2843 ed = defining;
2844 defining = ed->prev;
2845 ed->prev = expansions;
2846 expansions = ed;
2847 ei = new_ExpInv(EXP_IF, ed);
2848 ei->current = ed->line;
2849 ei->emitting = true;
2850 ei->prev = istk->expansion;
2851 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002852 free_tlist(origline);
2853 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002854
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002855 case PP_RMACRO:
2856 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002857 case PP_MACRO:
2858 case PP_IMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002859 if (defining != NULL) {
2860 if (defining->type == EXP_MMACRO) {
2861 defining->def_depth ++;
2862 }
2863 return NO_DIRECTIVE_FOUND;
2864 }
2865 ed = new_ExpDef(EXP_MMACRO);
2866 ed->max_depth =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002867 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002868 ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002869 if (!parse_mmacro_spec(tline, ed, pp_directives[i])) {
2870 nasm_free(ed);
2871 ed = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002872 return DIRECTIVE_FOUND;
2873 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002874 ed->def_depth = 0;
2875 ed->cur_depth = 0;
2876 ed->max_depth = (ed->max_depth + 1);
2877 ed->ignoring = false;
2878 ed->prev = defining;
2879 defining = ed;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002880
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002881 eed = (ExpDef *) hash_findix(&expdefs, ed->name);
2882 while (eed) {
2883 if (!strcmp(eed->name, ed->name) &&
2884 (eed->nparam_min <= ed->nparam_max
2885 || ed->plus)
2886 && (ed->nparam_min <= eed->nparam_max
2887 || eed->plus)) {
2888 error(ERR_WARNING|ERR_PASS1,
2889 "redefining multi-line macro `%s'", ed->name);
2890 return DIRECTIVE_FOUND;
2891 }
2892 eed = eed->next;
2893 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002894 free_tlist(origline);
2895 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002896
H. Peter Anvine2c80182005-01-15 22:15:51 +00002897 case PP_ENDM:
2898 case PP_ENDMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002899 if (defining != NULL) {
2900 if (defining->type == EXP_MMACRO) {
2901 if (defining->def_depth > 0) {
2902 defining->def_depth --;
2903 return NO_DIRECTIVE_FOUND;
2904 }
2905 } else {
2906 return NO_DIRECTIVE_FOUND;
2907 }
2908 }
2909 if (!(defining) || (defining->type != EXP_MMACRO)) {
2910 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2911 return DIRECTIVE_FOUND;
2912 }
2913 edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name);
2914 defining->next = *edhead;
2915 *edhead = defining;
2916 ed = defining;
2917 defining = ed->prev;
2918 ed->prev = expansions;
2919 expansions = ed;
2920 ed = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002921 free_tlist(origline);
2922 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002923
H. Peter Anvin89cee572009-07-15 09:16:54 -04002924 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002925 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2926 /*
2927 * We must search along istk->expansion until we hit a
2928 * macro invocation. Then we disable the emitting state(s)
2929 * between exitmacro and endmacro.
2930 */
2931 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
2932 if(ei->type == EXP_MMACRO) {
2933 break;
2934 }
2935 }
2936
2937 if (ei != NULL) {
2938 /*
2939 * Set all invocations leading back to the macro
2940 * invocation to a non-emitting state.
2941 */
2942 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
2943 eei->emitting = false;
2944 }
2945 eei->emitting = false;
2946 } else {
2947 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2948 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002949 free_tlist(origline);
2950 return DIRECTIVE_FOUND;
2951
H. Peter Anvina26433d2008-07-16 14:40:01 -07002952 case PP_UNMACRO:
2953 case PP_UNIMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002954 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002955 {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002956 ExpDef **ed_p;
2957 ExpDef spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002958
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002959 spec.casesense = (i == PP_UNMACRO);
2960 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2961 return DIRECTIVE_FOUND;
2962 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002963 ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL);
2964 while (ed_p && *ed_p) {
2965 ed = *ed_p;
2966 if (ed->casesense == spec.casesense &&
2967 !mstrcmp(ed->name, spec.name, spec.casesense) &&
2968 ed->nparam_min == spec.nparam_min &&
2969 ed->nparam_max == spec.nparam_max &&
2970 ed->plus == spec.plus) {
2971 *ed_p = ed->next;
2972 free_expdef(ed);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002973 } else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002974 ed_p = &ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002975 }
2976 }
2977 free_tlist(origline);
2978 free_tlist(spec.dlist);
2979 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002980 }
2981
H. Peter Anvine2c80182005-01-15 22:15:51 +00002982 case PP_ROTATE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002983 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002984 if (tline->next && tline->next->type == TOK_WHITESPACE)
2985 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002986 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002987 free_tlist(origline);
2988 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2989 return DIRECTIVE_FOUND;
2990 }
2991 t = expand_smacro(tline->next);
2992 tline->next = NULL;
2993 free_tlist(origline);
2994 tline = t;
2995 tptr = &t;
2996 tokval.t_type = TOKEN_INVALID;
2997 evalresult =
2998 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2999 free_tlist(tline);
3000 if (!evalresult)
3001 return DIRECTIVE_FOUND;
3002 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003003 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003004 "trailing garbage after expression ignored");
3005 if (!is_simple(evalresult)) {
3006 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
3007 return DIRECTIVE_FOUND;
3008 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003009 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3010 if (ei->type == EXP_MMACRO) {
3011 break;
3012 }
3013 }
3014 if (ei == NULL) {
3015 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
3016 } else if (ei->nparam == 0) {
3017 error(ERR_NONFATAL,
3018 "`%%rotate' invoked within macro without parameters");
3019 } else {
3020 int rotate = ei->rotate + reloc_value(evalresult);
3021
3022 rotate %= (int)ei->nparam;
3023 if (rotate < 0)
3024 rotate += ei->nparam;
3025 ei->rotate = rotate;
3026 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003027 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003028
H. Peter Anvine2c80182005-01-15 22:15:51 +00003029 case PP_REP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003030 if (defining != NULL) {
3031 if (defining->type == EXP_REP) {
3032 defining->def_depth ++;
3033 }
3034 return NO_DIRECTIVE_FOUND;
3035 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003036 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003037 do {
3038 tline = tline->next;
3039 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003040
H. Peter Anvine2c80182005-01-15 22:15:51 +00003041 if (tok_type_(tline, TOK_ID) &&
3042 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003043 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003044 do {
3045 tline = tline->next;
3046 } while (tok_type_(tline, TOK_WHITESPACE));
3047 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003048
H. Peter Anvine2c80182005-01-15 22:15:51 +00003049 if (tline) {
3050 t = expand_smacro(tline);
3051 tptr = &t;
3052 tokval.t_type = TOKEN_INVALID;
3053 evalresult =
3054 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3055 if (!evalresult) {
3056 free_tlist(origline);
3057 return DIRECTIVE_FOUND;
3058 }
3059 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003060 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003061 "trailing garbage after expression ignored");
3062 if (!is_simple(evalresult)) {
3063 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
3064 return DIRECTIVE_FOUND;
3065 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003066 count = reloc_value(evalresult);
3067 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04003068 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003069 count = 0;
3070 } else
3071 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003072 } else {
3073 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003074 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003075 }
3076 free_tlist(origline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003077 ed = new_ExpDef(EXP_REP);
3078 ed->nolist = nolist;
3079 ed->def_depth = 0;
3080 ed->cur_depth = 1;
3081 ed->max_depth = (count - 1);
3082 ed->ignoring = false;
3083 ed->prev = defining;
3084 defining = ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003085 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003086
H. Peter Anvine2c80182005-01-15 22:15:51 +00003087 case PP_ENDREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003088 if (defining != NULL) {
3089 if (defining->type == EXP_REP) {
3090 if (defining->def_depth > 0) {
3091 defining->def_depth --;
3092 return NO_DIRECTIVE_FOUND;
3093 }
3094 } else {
3095 return NO_DIRECTIVE_FOUND;
3096 }
3097 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003098 if ((defining == NULL) || (defining->type != EXP_REP)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003099 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
3100 return DIRECTIVE_FOUND;
3101 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003102
H. Peter Anvine2c80182005-01-15 22:15:51 +00003103 /*
3104 * Now we have a "macro" defined - although it has no name
3105 * and we won't be entering it in the hash tables - we must
3106 * push a macro-end marker for it on to istk->expansion.
3107 * After that, it will take care of propagating itself (a
3108 * macro-end marker line for a macro which is really a %rep
3109 * block will cause the macro to be re-expanded, complete
3110 * with another macro-end marker to ensure the process
3111 * continues) until the whole expansion is forcibly removed
3112 * from istk->expansion by a %exitrep.
3113 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003114 ed = defining;
3115 defining = ed->prev;
3116 ed->prev = expansions;
3117 expansions = ed;
3118 ei = new_ExpInv(EXP_REP, ed);
3119 ei->current = ed->line;
3120 ei->emitting = ((ed->max_depth > 0) ? true : false);
3121 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3122 ei->prev = istk->expansion;
3123 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003124 free_tlist(origline);
3125 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003126
H. Peter Anvine2c80182005-01-15 22:15:51 +00003127 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003128 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3129 /*
3130 * We must search along istk->expansion until we hit a
3131 * rep invocation. Then we disable the emitting state(s)
3132 * between exitrep and endrep.
3133 */
3134 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3135 if (ei->type == EXP_REP) {
3136 break;
3137 }
3138 }
3139
3140 if (ei != NULL) {
3141 /*
3142 * Set all invocations leading back to the rep
3143 * invocation to a non-emitting state.
3144 */
3145 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3146 eei->emitting = false;
3147 }
3148 eei->emitting = false;
3149 eei->current = NULL;
3150 eei->def->cur_depth = eei->def->max_depth;
3151 } else {
3152 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3153 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003154 free_tlist(origline);
3155 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003156
H. Peter Anvine2c80182005-01-15 22:15:51 +00003157 case PP_XDEFINE:
3158 case PP_IXDEFINE:
3159 case PP_DEFINE:
3160 case PP_IDEFINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003161 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003162 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003163
H. Peter Anvine2c80182005-01-15 22:15:51 +00003164 tline = tline->next;
3165 skip_white_(tline);
3166 tline = expand_id(tline);
3167 if (!tline || (tline->type != TOK_ID &&
3168 (tline->type != TOK_PREPROC_ID ||
3169 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003170 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003171 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003172 free_tlist(origline);
3173 return DIRECTIVE_FOUND;
3174 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003175
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003176 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003177 last = tline;
3178 param_start = tline = tline->next;
3179 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003180
H. Peter Anvine2c80182005-01-15 22:15:51 +00003181 /* Expand the macro definition now for %xdefine and %ixdefine */
3182 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3183 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003184
H. Peter Anvine2c80182005-01-15 22:15:51 +00003185 if (tok_is_(tline, "(")) {
3186 /*
3187 * This macro has parameters.
3188 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003189
H. Peter Anvine2c80182005-01-15 22:15:51 +00003190 tline = tline->next;
3191 while (1) {
3192 skip_white_(tline);
3193 if (!tline) {
3194 error(ERR_NONFATAL, "parameter identifier expected");
3195 free_tlist(origline);
3196 return DIRECTIVE_FOUND;
3197 }
3198 if (tline->type != TOK_ID) {
3199 error(ERR_NONFATAL,
3200 "`%s': parameter identifier expected",
3201 tline->text);
3202 free_tlist(origline);
3203 return DIRECTIVE_FOUND;
3204 }
3205 tline->type = TOK_SMAC_PARAM + nparam++;
3206 tline = tline->next;
3207 skip_white_(tline);
3208 if (tok_is_(tline, ",")) {
3209 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003210 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003211 if (!tok_is_(tline, ")")) {
3212 error(ERR_NONFATAL,
3213 "`)' expected to terminate macro template");
3214 free_tlist(origline);
3215 return DIRECTIVE_FOUND;
3216 }
3217 break;
3218 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003219 }
3220 last = tline;
3221 tline = tline->next;
3222 }
3223 if (tok_type_(tline, TOK_WHITESPACE))
3224 last = tline, tline = tline->next;
3225 macro_start = NULL;
3226 last->next = NULL;
3227 t = tline;
3228 while (t) {
3229 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003230 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003231 if (tt->type >= TOK_SMAC_PARAM &&
3232 !strcmp(tt->text, t->text))
3233 t->type = tt->type;
3234 }
3235 tt = t->next;
3236 t->next = macro_start;
3237 macro_start = t;
3238 t = tt;
3239 }
3240 /*
3241 * Good. We now have a macro name, a parameter count, and a
3242 * token list (in reverse order) for an expansion. We ought
3243 * to be OK just to create an SMacro, store it, and let
3244 * free_tlist have the rest of the line (which we have
3245 * carefully re-terminated after chopping off the expansion
3246 * from the end).
3247 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003248 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003249 free_tlist(origline);
3250 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003251
H. Peter Anvine2c80182005-01-15 22:15:51 +00003252 case PP_UNDEF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003253 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003254 tline = tline->next;
3255 skip_white_(tline);
3256 tline = expand_id(tline);
3257 if (!tline || (tline->type != TOK_ID &&
3258 (tline->type != TOK_PREPROC_ID ||
3259 tline->text[1] != '$'))) {
3260 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3261 free_tlist(origline);
3262 return DIRECTIVE_FOUND;
3263 }
3264 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003265 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003266 "trailing garbage after macro name ignored");
3267 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003268
H. Peter Anvine2c80182005-01-15 22:15:51 +00003269 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003270 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003271 undef_smacro(ctx, mname);
3272 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003273 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003274
H. Peter Anvin9e200162008-06-04 17:23:14 -07003275 case PP_DEFSTR:
3276 case PP_IDEFSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003277 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003278 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003279
3280 tline = tline->next;
3281 skip_white_(tline);
3282 tline = expand_id(tline);
3283 if (!tline || (tline->type != TOK_ID &&
3284 (tline->type != TOK_PREPROC_ID ||
3285 tline->text[1] != '$'))) {
3286 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003287 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003288 free_tlist(origline);
3289 return DIRECTIVE_FOUND;
3290 }
3291
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003292 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003293 last = tline;
3294 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003295 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003296
3297 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003298 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003299
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003300 p = detoken(tline, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003301 macro_start = nasm_malloc(sizeof(*macro_start));
3302 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003303 macro_start->text = nasm_quote(p, strlen(p));
3304 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003305 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003306 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003307
3308 /*
3309 * We now have a macro name, an implicit parameter count of
3310 * zero, and a string token to use as an expansion. Create
3311 * and store an SMacro.
3312 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003313 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003314 free_tlist(origline);
3315 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003316
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003317 case PP_DEFTOK:
3318 case PP_IDEFTOK:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003319 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003320 casesense = (i == PP_DEFTOK);
3321
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003322 tline = tline->next;
3323 skip_white_(tline);
3324 tline = expand_id(tline);
3325 if (!tline || (tline->type != TOK_ID &&
3326 (tline->type != TOK_PREPROC_ID ||
3327 tline->text[1] != '$'))) {
3328 error(ERR_NONFATAL,
3329 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003330 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003331 free_tlist(origline);
3332 return DIRECTIVE_FOUND;
3333 }
3334 ctx = get_ctx(tline->text, &mname, false);
3335 last = tline;
3336 tline = expand_smacro(tline->next);
3337 last->next = NULL;
3338
3339 t = tline;
3340 while (tok_type_(t, TOK_WHITESPACE))
3341 t = t->next;
3342 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003343 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003344 error(ERR_NONFATAL,
3345 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003346 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003347 free_tlist(tline);
3348 free_tlist(origline);
3349 return DIRECTIVE_FOUND;
3350 }
3351
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003352 /*
3353 * Convert the string to a token stream. Note that smacros
3354 * are stored with the token stream reversed, so we have to
3355 * reverse the output of tokenize().
3356 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003357 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003358 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003359
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003360 /*
3361 * We now have a macro name, an implicit parameter count of
3362 * zero, and a numeric token to use as an expansion. Create
3363 * and store an SMacro.
3364 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003365 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003366 free_tlist(tline);
3367 free_tlist(origline);
3368 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003369
H. Peter Anvin418ca702008-05-30 10:42:30 -07003370 case PP_PATHSEARCH:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003371 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003372 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003373 FILE *fp;
3374 StrList *xsl = NULL;
3375 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003376
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003377 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003378
3379 tline = tline->next;
3380 skip_white_(tline);
3381 tline = expand_id(tline);
3382 if (!tline || (tline->type != TOK_ID &&
3383 (tline->type != TOK_PREPROC_ID ||
3384 tline->text[1] != '$'))) {
3385 error(ERR_NONFATAL,
3386 "`%%pathsearch' expects a macro identifier as first parameter");
3387 free_tlist(origline);
3388 return DIRECTIVE_FOUND;
3389 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003390 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003391 last = tline;
3392 tline = expand_smacro(tline->next);
3393 last->next = NULL;
3394
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003395 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003396 while (tok_type_(t, TOK_WHITESPACE))
3397 t = t->next;
3398
3399 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003400 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003401 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003402 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003403 free_tlist(origline);
3404 return DIRECTIVE_FOUND; /* but we did _something_ */
3405 }
3406 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003407 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003408 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003409 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003410 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003411 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003412
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003413 fp = inc_fopen(p, &xsl, &xst, true);
3414 if (fp) {
3415 p = xsl->str;
3416 fclose(fp); /* Don't actually care about the file */
3417 }
H. Peter Anvin418ca702008-05-30 10:42:30 -07003418 macro_start = nasm_malloc(sizeof(*macro_start));
3419 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003420 macro_start->text = nasm_quote(p, strlen(p));
3421 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003422 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003423 if (xsl)
3424 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003425
3426 /*
3427 * We now have a macro name, an implicit parameter count of
3428 * zero, and a string token to use as an expansion. Create
3429 * and store an SMacro.
3430 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003431 define_smacro(ctx, mname, casesense, 0, macro_start);
3432 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003433 free_tlist(origline);
3434 return DIRECTIVE_FOUND;
3435 }
3436
H. Peter Anvine2c80182005-01-15 22:15:51 +00003437 case PP_STRLEN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003438 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003439 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003440
H. Peter Anvine2c80182005-01-15 22:15:51 +00003441 tline = tline->next;
3442 skip_white_(tline);
3443 tline = expand_id(tline);
3444 if (!tline || (tline->type != TOK_ID &&
3445 (tline->type != TOK_PREPROC_ID ||
3446 tline->text[1] != '$'))) {
3447 error(ERR_NONFATAL,
3448 "`%%strlen' expects a macro identifier as first parameter");
3449 free_tlist(origline);
3450 return DIRECTIVE_FOUND;
3451 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003452 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003453 last = tline;
3454 tline = expand_smacro(tline->next);
3455 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003456
H. Peter Anvine2c80182005-01-15 22:15:51 +00003457 t = tline;
3458 while (tok_type_(t, TOK_WHITESPACE))
3459 t = t->next;
3460 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003461 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003462 error(ERR_NONFATAL,
3463 "`%%strlen` requires string as second parameter");
3464 free_tlist(tline);
3465 free_tlist(origline);
3466 return DIRECTIVE_FOUND;
3467 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003468
H. Peter Anvine2c80182005-01-15 22:15:51 +00003469 macro_start = nasm_malloc(sizeof(*macro_start));
3470 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003471 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003472 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003473
H. Peter Anvine2c80182005-01-15 22:15:51 +00003474 /*
3475 * We now have a macro name, an implicit parameter count of
3476 * zero, and a numeric token to use as an expansion. Create
3477 * and store an SMacro.
3478 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003479 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003480 free_tlist(tline);
3481 free_tlist(origline);
3482 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003483
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003484 case PP_STRCAT:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003485 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003486 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003487
3488 tline = tline->next;
3489 skip_white_(tline);
3490 tline = expand_id(tline);
3491 if (!tline || (tline->type != TOK_ID &&
3492 (tline->type != TOK_PREPROC_ID ||
3493 tline->text[1] != '$'))) {
3494 error(ERR_NONFATAL,
3495 "`%%strcat' expects a macro identifier as first parameter");
3496 free_tlist(origline);
3497 return DIRECTIVE_FOUND;
3498 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003499 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003500 last = tline;
3501 tline = expand_smacro(tline->next);
3502 last->next = NULL;
3503
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003504 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003505 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003506 switch (t->type) {
3507 case TOK_WHITESPACE:
3508 break;
3509 case TOK_STRING:
3510 len += t->a.len = nasm_unquote(t->text, NULL);
3511 break;
3512 case TOK_OTHER:
3513 if (!strcmp(t->text, ",")) /* permit comma separators */
3514 break;
3515 /* else fall through */
3516 default:
3517 error(ERR_NONFATAL,
3518 "non-string passed to `%%strcat' (%d)", t->type);
3519 free_tlist(tline);
3520 free_tlist(origline);
3521 return DIRECTIVE_FOUND;
3522 }
3523 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003524
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003525 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003526 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003527 if (t->type == TOK_STRING) {
3528 memcpy(p, t->text, t->a.len);
3529 p += t->a.len;
3530 }
3531 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003532
3533 /*
3534 * We now have a macro name, an implicit parameter count of
3535 * zero, and a numeric token to use as an expansion. Create
3536 * and store an SMacro.
3537 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003538 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3539 macro_start->text = nasm_quote(pp, len);
3540 nasm_free(pp);
3541 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003542 free_tlist(tline);
3543 free_tlist(origline);
3544 return DIRECTIVE_FOUND;
3545
H. Peter Anvine2c80182005-01-15 22:15:51 +00003546 case PP_SUBSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003547 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003548 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003549 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003550 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003551
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003552 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003553
H. Peter Anvine2c80182005-01-15 22:15:51 +00003554 tline = tline->next;
3555 skip_white_(tline);
3556 tline = expand_id(tline);
3557 if (!tline || (tline->type != TOK_ID &&
3558 (tline->type != TOK_PREPROC_ID ||
3559 tline->text[1] != '$'))) {
3560 error(ERR_NONFATAL,
3561 "`%%substr' expects a macro identifier as first parameter");
3562 free_tlist(origline);
3563 return DIRECTIVE_FOUND;
3564 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003565 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003566 last = tline;
3567 tline = expand_smacro(tline->next);
3568 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003569
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003570 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003571 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003572 while (tok_type_(t, TOK_WHITESPACE))
3573 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003574
H. Peter Anvine2c80182005-01-15 22:15:51 +00003575 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003576 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003577 error(ERR_NONFATAL,
3578 "`%%substr` requires string as second parameter");
3579 free_tlist(tline);
3580 free_tlist(origline);
3581 return DIRECTIVE_FOUND;
3582 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003583
H. Peter Anvine2c80182005-01-15 22:15:51 +00003584 tt = t->next;
3585 tptr = &tt;
3586 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003587 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003588 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003589 if (!evalresult) {
3590 free_tlist(tline);
3591 free_tlist(origline);
3592 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003593 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003594 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3595 free_tlist(tline);
3596 free_tlist(origline);
3597 return DIRECTIVE_FOUND;
3598 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003599 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003600
3601 while (tok_type_(tt, TOK_WHITESPACE))
3602 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003603 if (!tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003604 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003605 } else {
3606 tokval.t_type = TOKEN_INVALID;
3607 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3608 pass, error, NULL);
3609 if (!evalresult) {
3610 free_tlist(tline);
3611 free_tlist(origline);
3612 return DIRECTIVE_FOUND;
3613 } else if (!is_simple(evalresult)) {
3614 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3615 free_tlist(tline);
3616 free_tlist(origline);
3617 return DIRECTIVE_FOUND;
3618 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003619 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003620 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003621
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003622 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003623 /* make start and count being in range */
3624 if (start < 0)
3625 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003626 if (count < 0)
3627 count = len + count + 1 - start;
3628 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003629 count = len - start;
3630 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003631 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003632
H. Peter Anvine2c80182005-01-15 22:15:51 +00003633 macro_start = nasm_malloc(sizeof(*macro_start));
3634 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003635 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003636 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003637 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003638
H. Peter Anvine2c80182005-01-15 22:15:51 +00003639 /*
3640 * We now have a macro name, an implicit parameter count of
3641 * zero, and a numeric token to use as an expansion. Create
3642 * and store an SMacro.
3643 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003644 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003645 free_tlist(tline);
3646 free_tlist(origline);
3647 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003648 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003649
H. Peter Anvine2c80182005-01-15 22:15:51 +00003650 case PP_ASSIGN:
3651 case PP_IASSIGN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003652 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003653 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003654
H. Peter Anvine2c80182005-01-15 22:15:51 +00003655 tline = tline->next;
3656 skip_white_(tline);
3657 tline = expand_id(tline);
3658 if (!tline || (tline->type != TOK_ID &&
3659 (tline->type != TOK_PREPROC_ID ||
3660 tline->text[1] != '$'))) {
3661 error(ERR_NONFATAL,
3662 "`%%%sassign' expects a macro identifier",
3663 (i == PP_IASSIGN ? "i" : ""));
3664 free_tlist(origline);
3665 return DIRECTIVE_FOUND;
3666 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003667 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003668 last = tline;
3669 tline = expand_smacro(tline->next);
3670 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003671
H. Peter Anvine2c80182005-01-15 22:15:51 +00003672 t = tline;
3673 tptr = &t;
3674 tokval.t_type = TOKEN_INVALID;
3675 evalresult =
3676 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3677 free_tlist(tline);
3678 if (!evalresult) {
3679 free_tlist(origline);
3680 return DIRECTIVE_FOUND;
3681 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003682
H. Peter Anvine2c80182005-01-15 22:15:51 +00003683 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003684 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003685 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003686
H. Peter Anvine2c80182005-01-15 22:15:51 +00003687 if (!is_simple(evalresult)) {
3688 error(ERR_NONFATAL,
3689 "non-constant value given to `%%%sassign'",
3690 (i == PP_IASSIGN ? "i" : ""));
3691 free_tlist(origline);
3692 return DIRECTIVE_FOUND;
3693 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003694
H. Peter Anvine2c80182005-01-15 22:15:51 +00003695 macro_start = nasm_malloc(sizeof(*macro_start));
3696 macro_start->next = NULL;
3697 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003698 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003699
H. Peter Anvine2c80182005-01-15 22:15:51 +00003700 /*
3701 * We now have a macro name, an implicit parameter count of
3702 * zero, and a numeric token to use as an expansion. Create
3703 * and store an SMacro.
3704 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003705 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003706 free_tlist(origline);
3707 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003708
H. Peter Anvine2c80182005-01-15 22:15:51 +00003709 case PP_LINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003710 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003711 /*
3712 * Syntax is `%line nnn[+mmm] [filename]'
3713 */
3714 tline = tline->next;
3715 skip_white_(tline);
3716 if (!tok_type_(tline, TOK_NUMBER)) {
3717 error(ERR_NONFATAL, "`%%line' expects line number");
3718 free_tlist(origline);
3719 return DIRECTIVE_FOUND;
3720 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003721 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003722 m = 1;
3723 tline = tline->next;
3724 if (tok_is_(tline, "+")) {
3725 tline = tline->next;
3726 if (!tok_type_(tline, TOK_NUMBER)) {
3727 error(ERR_NONFATAL, "`%%line' expects line increment");
3728 free_tlist(origline);
3729 return DIRECTIVE_FOUND;
3730 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003731 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003732 tline = tline->next;
3733 }
3734 skip_white_(tline);
3735 src_set_linnum(k);
3736 istk->lineinc = m;
3737 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003738 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003739 }
3740 free_tlist(origline);
3741 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003742
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003743 case PP_WHILE:
3744 if (defining != NULL) {
3745 if (defining->type == EXP_WHILE) {
3746 defining->def_depth ++;
3747 }
3748 return NO_DIRECTIVE_FOUND;
3749 }
3750 l = NULL;
3751 if ((istk->expansion != NULL) &&
3752 (istk->expansion->emitting == false)) {
3753 j = COND_NEVER;
3754 } else {
3755 l = new_Line();
3756 l->first = copy_Token(tline->next);
3757 j = if_condition(tline->next, i);
3758 tline->next = NULL; /* it got freed */
3759 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
3760 }
3761 ed = new_ExpDef(EXP_WHILE);
3762 ed->state = j;
3763 ed->cur_depth = 1;
3764 ed->max_depth = DEADMAN_LIMIT;
3765 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
3766 if (ed->ignoring == false) {
3767 ed->line = l;
3768 ed->last = l;
3769 } else if (l != NULL) {
3770 delete_Token(l->first);
3771 nasm_free(l);
3772 l = NULL;
3773 }
3774 ed->prev = defining;
3775 defining = ed;
3776 free_tlist(origline);
3777 return DIRECTIVE_FOUND;
3778
3779 case PP_ENDWHILE:
3780 if (defining != NULL) {
3781 if (defining->type == EXP_WHILE) {
3782 if (defining->def_depth > 0) {
3783 defining->def_depth --;
3784 return NO_DIRECTIVE_FOUND;
3785 }
3786 } else {
3787 return NO_DIRECTIVE_FOUND;
3788 }
3789 }
3790 if (tline->next != NULL) {
3791 error_precond(ERR_WARNING|ERR_PASS1,
3792 "trailing garbage after `%%endwhile' ignored");
3793 }
3794 if ((defining == NULL) || (defining->type != EXP_WHILE)) {
3795 error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'");
3796 return DIRECTIVE_FOUND;
3797 }
3798 ed = defining;
3799 defining = ed->prev;
3800 if (ed->ignoring == false) {
3801 ed->prev = expansions;
3802 expansions = ed;
3803 ei = new_ExpInv(EXP_WHILE, ed);
3804 ei->current = ed->line->next;
3805 ei->emitting = true;
3806 ei->prev = istk->expansion;
3807 istk->expansion = ei;
3808 } else {
3809 nasm_free(ed);
3810 }
3811 free_tlist(origline);
3812 return DIRECTIVE_FOUND;
3813
3814 case PP_EXITWHILE:
3815 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3816 /*
3817 * We must search along istk->expansion until we hit a
3818 * while invocation. Then we disable the emitting state(s)
3819 * between exitwhile and endwhile.
3820 */
3821 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3822 if (ei->type == EXP_WHILE) {
3823 break;
3824 }
3825 }
3826
3827 if (ei != NULL) {
3828 /*
3829 * Set all invocations leading back to the while
3830 * invocation to a non-emitting state.
3831 */
3832 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3833 eei->emitting = false;
3834 }
3835 eei->emitting = false;
3836 eei->current = NULL;
3837 eei->def->cur_depth = eei->def->max_depth;
3838 } else {
3839 error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block");
3840 }
3841 free_tlist(origline);
3842 return DIRECTIVE_FOUND;
3843
3844 case PP_COMMENT:
3845 if (defining != NULL) {
3846 if (defining->type == EXP_COMMENT) {
3847 defining->def_depth ++;
3848 }
3849 return NO_DIRECTIVE_FOUND;
3850 }
3851 ed = new_ExpDef(EXP_COMMENT);
3852 ed->ignoring = true;
3853 ed->prev = defining;
3854 defining = ed;
3855 free_tlist(origline);
3856 return DIRECTIVE_FOUND;
3857
3858 case PP_ENDCOMMENT:
3859 if (defining != NULL) {
3860 if (defining->type == EXP_COMMENT) {
3861 if (defining->def_depth > 0) {
3862 defining->def_depth --;
3863 return NO_DIRECTIVE_FOUND;
3864 }
3865 } else {
3866 return NO_DIRECTIVE_FOUND;
3867 }
3868 }
3869 if ((defining == NULL) || (defining->type != EXP_COMMENT)) {
3870 error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'");
3871 return DIRECTIVE_FOUND;
3872 }
3873 ed = defining;
3874 defining = ed->prev;
3875 nasm_free(ed);
3876 free_tlist(origline);
3877 return DIRECTIVE_FOUND;
3878
3879 case PP_FINAL:
3880 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3881 if (in_final != false) {
3882 error(ERR_FATAL, "`%%final' cannot be used recursively");
3883 }
3884 tline = tline->next;
3885 skip_white_(tline);
3886 if (tline == NULL) {
3887 error(ERR_NONFATAL, "`%%final' expects at least one parameter");
3888 } else {
3889 l = new_Line();
3890 l->first = copy_Token(tline);
3891 l->next = finals;
3892 finals = l;
3893 }
3894 free_tlist(origline);
3895 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003896
H. Peter Anvine2c80182005-01-15 22:15:51 +00003897 default:
3898 error(ERR_FATAL,
3899 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003900 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003901 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003902 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003903}
3904
3905/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003906 * Ensure that a macro parameter contains a condition code and
3907 * nothing else. Return the condition code index if so, or -1
3908 * otherwise.
3909 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003910static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003911{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003912 Token *tt;
3913 int i, j, k, m;
3914
H. Peter Anvin25a99342007-09-22 17:45:45 -07003915 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003916 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003917
H. Peter Anvineba20a72002-04-30 20:53:55 +00003918 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003919 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003920 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003921 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003922 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003923 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003924 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003925
3926 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003927 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003928 while (j - i > 1) {
3929 k = (j + i) / 2;
3930 m = nasm_stricmp(t->text, conditions[k]);
3931 if (m == 0) {
3932 i = k;
3933 j = -2;
3934 break;
3935 } else if (m < 0) {
3936 j = k;
3937 } else
3938 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003939 }
3940 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003941 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003942 return i;
3943}
3944
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003945static bool paste_tokens(Token **head, const struct tokseq_match *m,
3946 int mnum, bool handle_paste_tokens)
H. Peter Anvind784a082009-04-20 14:01:18 -07003947{
3948 Token **tail, *t, *tt;
3949 Token **paste_head;
3950 bool did_paste = false;
3951 char *tmp;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003952 int i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003953
3954 /* Now handle token pasting... */
3955 paste_head = NULL;
3956 tail = head;
3957 while ((t = *tail) && (tt = t->next)) {
3958 switch (t->type) {
3959 case TOK_WHITESPACE:
3960 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003961 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003962 t->next = delete_Token(tt);
3963 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003964 /* Do not advance paste_head here */
3965 tail = &t->next;
3966 }
H. Peter Anvind784a082009-04-20 14:01:18 -07003967 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003968 case TOK_PASTE: /* %+ */
3969 if (handle_paste_tokens) {
3970 /* Zap %+ and whitespace tokens to the right */
3971 while (t && (t->type == TOK_WHITESPACE ||
3972 t->type == TOK_PASTE))
3973 t = *tail = delete_Token(t);
3974 if (!paste_head || !t)
3975 break; /* Nothing to paste with */
3976 tail = paste_head;
3977 t = *tail;
3978 tt = t->next;
3979 while (tok_type_(tt, TOK_WHITESPACE))
3980 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003981 if (tt) {
3982 tmp = nasm_strcat(t->text, tt->text);
3983 delete_Token(t);
3984 tt = delete_Token(tt);
3985 t = *tail = tokenize(tmp);
3986 nasm_free(tmp);
3987 while (t->next) {
3988 tail = &t->next;
3989 t = t->next;
3990 }
3991 t->next = tt; /* Attach the remaining token chain */
3992 did_paste = true;
3993 }
3994 paste_head = tail;
3995 tail = &t->next;
3996 break;
3997 }
3998 /* else fall through */
3999 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004000 /*
4001 * Concatenation of tokens might look nontrivial
4002 * but in real it's pretty simple -- the caller
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004003 * prepares the masks of token types to be concatenated
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004004 * and we simply find matched sequences and slip
4005 * them together
4006 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004007 for (i = 0; i < mnum; i++) {
4008 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
4009 size_t len = 0;
4010 char *tmp, *p;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004011
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004012 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004013 len += strlen(tt->text);
4014 tt = tt->next;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004015 }
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004016
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004017 /*
4018 * Now tt points to the first token after
4019 * the potential paste area...
4020 */
4021 if (tt != t->next) {
4022 /* We have at least two tokens... */
4023 len += strlen(t->text);
4024 p = tmp = nasm_malloc(len+1);
4025 while (t != tt) {
4026 strcpy(p, t->text);
4027 p = strchr(p, '\0');
4028 t = delete_Token(t);
4029 }
4030 t = *tail = tokenize(tmp);
4031 nasm_free(tmp);
4032 while (t->next) {
4033 tail = &t->next;
4034 t = t->next;
4035 }
4036 t->next = tt; /* Attach the remaining token chain */
4037 did_paste = true;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004038 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004039 paste_head = tail;
4040 tail = &t->next;
4041 break;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004042 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004043 }
4044 if (i >= mnum) { /* no match */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004045 tail = &t->next;
4046 if (!tok_type_(t->next, TOK_WHITESPACE))
4047 paste_head = tail;
4048 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004049 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004050 }
4051 }
4052 return did_paste;
4053}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004054
4055/*
4056 * expands to a list of tokens from %{x:y}
4057 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004058static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004059{
4060 Token *t = tline, **tt, *tm, *head;
4061 char *pos;
4062 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004063
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004064 pos = strchr(tline->text, ':');
4065 nasm_assert(pos);
4066
4067 lst = atoi(pos + 1);
4068 fst = atoi(tline->text + 1);
4069
4070 /*
4071 * only macros params are accounted so
4072 * if someone passes %0 -- we reject such
4073 * value(s)
4074 */
4075 if (lst == 0 || fst == 0)
4076 goto err;
4077
4078 /* the values should be sane */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004079 if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) ||
4080 (lst > (int)ei->nparam || lst < (-(int)ei->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004081 goto err;
4082
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004083 fst = fst < 0 ? fst + (int)ei->nparam + 1: fst;
4084 lst = lst < 0 ? lst + (int)ei->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004085
4086 /* counted from zero */
4087 fst--, lst--;
4088
4089 /*
4090 * it will be at least one token
4091 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004092 tm = ei->params[(fst + ei->rotate) % ei->nparam];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004093 t = new_Token(NULL, tm->type, tm->text, 0);
4094 head = t, tt = &t->next;
4095 if (fst < lst) {
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 } else {
4105 for (i = fst - 1; i >= lst; i--) {
4106 t = new_Token(NULL, TOK_OTHER, ",", 0);
4107 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004108 j = (i + ei->rotate) % ei->nparam;
4109 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004110 t = new_Token(NULL, tm->type, tm->text, 0);
4111 *tt = t, tt = &t->next;
4112 }
4113 }
4114
4115 *last = tt;
4116 return head;
4117
4118err:
4119 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
4120 &tline->text[1]);
4121 return tline;
4122}
4123
H. Peter Anvin76690a12002-04-30 20:52:49 +00004124/*
4125 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004126 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004127 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004128 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004129static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004130{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004131 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004132 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004133 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004134
4135 tail = &thead;
4136 thead = NULL;
4137
H. Peter Anvine2c80182005-01-15 22:15:51 +00004138 while (tline) {
4139 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004140 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4141 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4142 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004143 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004144 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004145 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004146 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004147 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004148 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004149
H. Peter Anvine2c80182005-01-15 22:15:51 +00004150 t = tline;
4151 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004152
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004153 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
4154 if (ei->type == EXP_MMACRO) {
4155 break;
4156 }
4157 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004158 if (ei == NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004159 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004160 } else {
4161 pos = strchr(t->text, ':');
4162 if (!pos) {
4163 switch (t->text[1]) {
4164 /*
4165 * We have to make a substitution of one of the
4166 * forms %1, %-1, %+1, %%foo, %0.
4167 */
4168 case '0':
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004169 if ((strlen(t->text) > 2) && (t->text[2] == '0')) {
4170 type = TOK_ID;
4171 text = nasm_strdup(ei->label_text);
4172 } else {
4173 type = TOK_NUMBER;
4174 snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam);
4175 text = nasm_strdup(tmpbuf);
4176 }
4177 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004178 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004179 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004180 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004181 ei->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004182 text = nasm_strcat(tmpbuf, t->text + 2);
4183 break;
4184 case '-':
4185 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004186 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004187 tt = NULL;
4188 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004189 if (ei->nparam > 1)
4190 n = (n + ei->rotate) % ei->nparam;
4191 tt = ei->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004192 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004193 cc = find_cc(tt);
4194 if (cc == -1) {
4195 error(ERR_NONFATAL,
4196 "macro parameter %d is not a condition code",
4197 n + 1);
4198 text = NULL;
4199 } else {
4200 type = TOK_ID;
4201 if (inverse_ccs[cc] == -1) {
4202 error(ERR_NONFATAL,
4203 "condition code `%s' is not invertible",
4204 conditions[cc]);
4205 text = NULL;
4206 } else
4207 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4208 }
4209 break;
4210 case '+':
4211 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004212 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004213 tt = NULL;
4214 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004215 if (ei->nparam > 1)
4216 n = (n + ei->rotate) % ei->nparam;
4217 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004218 }
4219 cc = find_cc(tt);
4220 if (cc == -1) {
4221 error(ERR_NONFATAL,
4222 "macro parameter %d is not a condition code",
4223 n + 1);
4224 text = NULL;
4225 } else {
4226 type = TOK_ID;
4227 text = nasm_strdup(conditions[cc]);
4228 }
4229 break;
4230 default:
4231 n = atoi(t->text + 1) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004232 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004233 tt = NULL;
4234 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004235 if (ei->nparam > 1)
4236 n = (n + ei->rotate) % ei->nparam;
4237 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004238 }
4239 if (tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004240 for (i = 0; i < ei->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004241 *tail = new_Token(NULL, tt->type, tt->text, 0);
4242 tail = &(*tail)->next;
4243 tt = tt->next;
4244 }
4245 }
4246 text = NULL; /* we've done it here */
4247 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004248 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004249 } else {
4250 /*
4251 * seems we have a parameters range here
4252 */
4253 Token *head, **last;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004254 head = expand_mmac_params_range(ei, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004255 if (head != t) {
4256 *tail = head;
4257 *last = tline;
4258 tline = head;
4259 text = NULL;
4260 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004261 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004262 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004263 if (!text) {
4264 delete_Token(t);
4265 } else {
4266 *tail = t;
4267 tail = &t->next;
4268 t->type = type;
4269 nasm_free(t->text);
4270 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004271 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004272 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004273 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004274 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004275 } else if (tline->type == TOK_INDIRECT) {
4276 t = tline;
4277 tline = tline->next;
4278 tt = tokenize(t->text);
4279 tt = expand_mmac_params(tt);
4280 tt = expand_smacro(tt);
4281 *tail = tt;
4282 while (tt) {
4283 tt->a.mac = NULL; /* Necessary? */
4284 tail = &tt->next;
4285 tt = tt->next;
4286 }
4287 delete_Token(t);
4288 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004289 } else {
4290 t = *tail = tline;
4291 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004292 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004293 tail = &t->next;
4294 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004295 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004296 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004297
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004298 if (changed) {
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004299 const struct tokseq_match t[] = {
4300 {
4301 PP_CONCAT_MASK(TOK_ID) |
4302 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4303 PP_CONCAT_MASK(TOK_ID) |
4304 PP_CONCAT_MASK(TOK_NUMBER) |
4305 PP_CONCAT_MASK(TOK_FLOAT) |
4306 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4307 },
4308 {
4309 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4310 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4311 }
4312 };
4313 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004314 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004315
H. Peter Anvin76690a12002-04-30 20:52:49 +00004316 return thead;
4317}
4318
4319/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004320 * Expand all single-line macro calls made in the given line.
4321 * Return the expanded version of the line. The original is deemed
4322 * to be destroyed in the process. (In reality we'll just move
4323 * Tokens from input to output a lot of the time, rather than
4324 * actually bothering to destroy and replicate.)
4325 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004326
H. Peter Anvine2c80182005-01-15 22:15:51 +00004327static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004328{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004329 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004330 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004331 Token **params;
4332 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004333 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004334 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004335 Token *org_tline = tline;
4336 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004337 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004338 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004339 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004340
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004341 /*
4342 * Trick: we should avoid changing the start token pointer since it can
4343 * be contained in "next" field of other token. Because of this
4344 * we allocate a copy of first token and work with it; at the end of
4345 * routine we copy it back
4346 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004347 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004348 tline = new_Token(org_tline->next, org_tline->type,
4349 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004350 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004351 nasm_free(org_tline->text);
4352 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004353 }
4354
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004355 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004356
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004357again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004358 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004359 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004360
H. Peter Anvine2c80182005-01-15 22:15:51 +00004361 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004362 if (!--deadman) {
4363 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004364 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004365 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004366
H. Peter Anvine2c80182005-01-15 22:15:51 +00004367 if ((mname = tline->text)) {
4368 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004369 if (tline->type == TOK_ID) {
4370 head = (SMacro *)hash_findix(&smacros, mname);
4371 } else if (tline->type == TOK_PREPROC_ID) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004372 ctx = get_ctx(mname, &mname, false);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004373 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4374 } else
4375 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004376
H. Peter Anvine2c80182005-01-15 22:15:51 +00004377 /*
4378 * We've hit an identifier. As in is_mmacro below, we first
4379 * check whether the identifier is a single-line macro at
4380 * all, then think about checking for parameters if
4381 * necessary.
4382 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004383 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004384 if (!mstrcmp(m->name, mname, m->casesense))
4385 break;
4386 if (m) {
4387 mstart = tline;
4388 params = NULL;
4389 paramsize = NULL;
4390 if (m->nparam == 0) {
4391 /*
4392 * Simple case: the macro is parameterless. Discard the
4393 * one token that the macro call took, and push the
4394 * expansion back on the to-do stack.
4395 */
4396 if (!m->expansion) {
4397 if (!strcmp("__FILE__", m->name)) {
4398 int32_t num = 0;
4399 char *file = NULL;
4400 src_get(&num, &file);
4401 tline->text = nasm_quote(file, strlen(file));
4402 tline->type = TOK_STRING;
4403 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004404 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004405 }
4406 if (!strcmp("__LINE__", m->name)) {
4407 nasm_free(tline->text);
4408 make_tok_num(tline, src_get_linnum());
4409 continue;
4410 }
4411 if (!strcmp("__BITS__", m->name)) {
4412 nasm_free(tline->text);
4413 make_tok_num(tline, globalbits);
4414 continue;
4415 }
4416 tline = delete_Token(tline);
4417 continue;
4418 }
4419 } else {
4420 /*
4421 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004422 * exists and takes parameters. We must find the
4423 * parameters in the call, count them, find the SMacro
4424 * that corresponds to that form of the macro call, and
4425 * substitute for the parameters when we expand. What a
4426 * pain.
4427 */
4428 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004429 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004430 do {
4431 t = tline->next;
4432 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004433 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004434 t->text = NULL;
4435 t = tline->next = delete_Token(t);
4436 }
4437 tline = t;
4438 } while (tok_type_(tline, TOK_WHITESPACE));
4439 if (!tok_is_(tline, "(")) {
4440 /*
4441 * This macro wasn't called with parameters: ignore
4442 * the call. (Behaviour borrowed from gnu cpp.)
4443 */
4444 tline = mstart;
4445 m = NULL;
4446 } else {
4447 int paren = 0;
4448 int white = 0;
4449 brackets = 0;
4450 nparam = 0;
4451 sparam = PARAM_DELTA;
4452 params = nasm_malloc(sparam * sizeof(Token *));
4453 params[0] = tline->next;
4454 paramsize = nasm_malloc(sparam * sizeof(int));
4455 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004456 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004457 /*
4458 * For some unusual expansions
4459 * which concatenates function call
4460 */
4461 t = tline->next;
4462 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004463 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004464 t->text = NULL;
4465 t = tline->next = delete_Token(t);
4466 }
4467 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004468
H. Peter Anvine2c80182005-01-15 22:15:51 +00004469 if (!tline) {
4470 error(ERR_NONFATAL,
4471 "macro call expects terminating `)'");
4472 break;
4473 }
4474 if (tline->type == TOK_WHITESPACE
4475 && brackets <= 0) {
4476 if (paramsize[nparam])
4477 white++;
4478 else
4479 params[nparam] = tline->next;
4480 continue; /* parameter loop */
4481 }
4482 if (tline->type == TOK_OTHER
4483 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004484 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004485 if (ch == ',' && !paren && brackets <= 0) {
4486 if (++nparam >= sparam) {
4487 sparam += PARAM_DELTA;
4488 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004489 sparam * sizeof(Token *));
4490 paramsize = nasm_realloc(paramsize,
4491 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004492 }
4493 params[nparam] = tline->next;
4494 paramsize[nparam] = 0;
4495 white = 0;
4496 continue; /* parameter loop */
4497 }
4498 if (ch == '{' &&
4499 (brackets > 0 || (brackets == 0 &&
4500 !paramsize[nparam])))
4501 {
4502 if (!(brackets++)) {
4503 params[nparam] = tline->next;
4504 continue; /* parameter loop */
4505 }
4506 }
4507 if (ch == '}' && brackets > 0)
4508 if (--brackets == 0) {
4509 brackets = -1;
4510 continue; /* parameter loop */
4511 }
4512 if (ch == '(' && !brackets)
4513 paren++;
4514 if (ch == ')' && brackets <= 0)
4515 if (--paren < 0)
4516 break;
4517 }
4518 if (brackets < 0) {
4519 brackets = 0;
4520 error(ERR_NONFATAL, "braces do not "
4521 "enclose all of macro parameter");
4522 }
4523 paramsize[nparam] += white + 1;
4524 white = 0;
4525 } /* parameter loop */
4526 nparam++;
4527 while (m && (m->nparam != nparam ||
4528 mstrcmp(m->name, mname,
4529 m->casesense)))
4530 m = m->next;
4531 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004532 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004533 "macro `%s' exists, "
4534 "but not taking %d parameters",
4535 mstart->text, nparam);
4536 }
4537 }
4538 if (m && m->in_progress)
4539 m = NULL;
4540 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004541 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004542 * Design question: should we handle !tline, which
4543 * indicates missing ')' here, or expand those
4544 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004545 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004546 */
4547 nasm_free(params);
4548 nasm_free(paramsize);
4549 tline = mstart;
4550 } else {
4551 /*
4552 * Expand the macro: we are placed on the last token of the
4553 * call, so that we can easily split the call from the
4554 * following tokens. We also start by pushing an SMAC_END
4555 * token for the cycle removal.
4556 */
4557 t = tline;
4558 if (t) {
4559 tline = t->next;
4560 t->next = NULL;
4561 }
4562 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004563 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004564 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004565 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004566 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004567 if (t->type >= TOK_SMAC_PARAM) {
4568 Token *pcopy = tline, **ptail = &pcopy;
4569 Token *ttt, *pt;
4570 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004571
H. Peter Anvine2c80182005-01-15 22:15:51 +00004572 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004573 i = paramsize[t->type - TOK_SMAC_PARAM];
4574 while (--i >= 0) {
4575 pt = *ptail = new_Token(tline, ttt->type,
4576 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004577 ptail = &pt->next;
4578 ttt = ttt->next;
4579 }
4580 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004581 } else if (t->type == TOK_PREPROC_Q) {
4582 tt = new_Token(tline, TOK_ID, mname, 0);
4583 tline = tt;
4584 } else if (t->type == TOK_PREPROC_QQ) {
4585 tt = new_Token(tline, TOK_ID, m->name, 0);
4586 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004587 } else {
4588 tt = new_Token(tline, t->type, t->text, 0);
4589 tline = tt;
4590 }
4591 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004592
H. Peter Anvine2c80182005-01-15 22:15:51 +00004593 /*
4594 * Having done that, get rid of the macro call, and clean
4595 * up the parameters.
4596 */
4597 nasm_free(params);
4598 nasm_free(paramsize);
4599 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004600 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004601 continue; /* main token loop */
4602 }
4603 }
4604 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004605
H. Peter Anvine2c80182005-01-15 22:15:51 +00004606 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004607 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004608 tline = delete_Token(tline);
4609 } else {
4610 t = *tail = tline;
4611 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004612 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004613 t->next = NULL;
4614 tail = &t->next;
4615 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004616 }
4617
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004618 /*
4619 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004620 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004621 * TOK_IDs should be concatenated.
4622 * Also we look for %+ tokens and concatenate the tokens before and after
4623 * them (without white spaces in between).
4624 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004625 if (expanded) {
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004626 const struct tokseq_match t[] = {
4627 {
4628 PP_CONCAT_MASK(TOK_ID) |
4629 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4630 PP_CONCAT_MASK(TOK_ID) |
4631 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4632 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4633 }
4634 };
4635 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004636 /*
4637 * If we concatenated something, *and* we had previously expanded
4638 * an actual macro, scan the lines again for macros...
4639 */
4640 tline = thead;
4641 expanded = false;
4642 goto again;
4643 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004644 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004645
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004646err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004647 if (org_tline) {
4648 if (thead) {
4649 *org_tline = *thead;
4650 /* since we just gave text to org_line, don't free it */
4651 thead->text = NULL;
4652 delete_Token(thead);
4653 } else {
4654 /* the expression expanded to empty line;
4655 we can't return NULL for some reasons
4656 we just set the line to a single WHITESPACE token. */
4657 memset(org_tline, 0, sizeof(*org_tline));
4658 org_tline->text = NULL;
4659 org_tline->type = TOK_WHITESPACE;
4660 }
4661 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004662 }
4663
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004664 return thead;
4665}
4666
4667/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004668 * Similar to expand_smacro but used exclusively with macro identifiers
4669 * right before they are fetched in. The reason is that there can be
4670 * identifiers consisting of several subparts. We consider that if there
4671 * are more than one element forming the name, user wants a expansion,
4672 * otherwise it will be left as-is. Example:
4673 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004674 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004675 *
4676 * the identifier %$abc will be left as-is so that the handler for %define
4677 * will suck it and define the corresponding value. Other case:
4678 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004679 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004680 *
4681 * In this case user wants name to be expanded *before* %define starts
4682 * working, so we'll expand %$abc into something (if it has a value;
4683 * otherwise it will be left as-is) then concatenate all successive
4684 * PP_IDs into one.
4685 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004686static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004687{
4688 Token *cur, *oldnext = NULL;
4689
H. Peter Anvin734b1882002-04-30 21:01:08 +00004690 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004691 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004692
4693 cur = tline;
4694 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004695 (cur->next->type == TOK_ID ||
4696 cur->next->type == TOK_PREPROC_ID
4697 || cur->next->type == TOK_NUMBER))
4698 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004699
4700 /* If identifier consists of just one token, don't expand */
4701 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004702 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004703
H. Peter Anvine2c80182005-01-15 22:15:51 +00004704 if (cur) {
4705 oldnext = cur->next; /* Detach the tail past identifier */
4706 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004707 }
4708
H. Peter Anvin734b1882002-04-30 21:01:08 +00004709 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004710
H. Peter Anvine2c80182005-01-15 22:15:51 +00004711 if (cur) {
4712 /* expand_smacro possibly changhed tline; re-scan for EOL */
4713 cur = tline;
4714 while (cur && cur->next)
4715 cur = cur->next;
4716 if (cur)
4717 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004718 }
4719
4720 return tline;
4721}
4722
4723/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004724 * Determine whether the given line constitutes a multi-line macro
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004725 * call, and return the ExpDef structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004726 * to check for an initial label - that's taken care of in
4727 * expand_mmacro - but must check numbers of parameters. Guaranteed
4728 * to be called with tline->type == TOK_ID, so the putative macro
4729 * name is easy to find.
4730 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004731static ExpDef *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004732{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004733 ExpDef *head, *ed;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004734 Token **params;
4735 int nparam;
4736
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004737 head = (ExpDef *) hash_findix(&expdefs, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004738
4739 /*
4740 * Efficiency: first we see if any macro exists with the given
4741 * name. If not, we can return NULL immediately. _Then_ we
4742 * count the parameters, and then we look further along the
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004743 * list if necessary to find the proper ExpDef.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004744 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004745 list_for_each(ed, head)
4746 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004747 break;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004748 if (!ed)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004749 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004750
4751 /*
4752 * OK, we have a potential macro. Count and demarcate the
4753 * parameters.
4754 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004755 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004756
4757 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004758 * So we know how many parameters we've got. Find the ExpDef
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004759 * structure that handles this number.
4760 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004761 while (ed) {
4762 if (ed->nparam_min <= nparam
4763 && (ed->plus || nparam <= ed->nparam_max)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004764 /*
4765 * It's right, and we can use it. Add its default
4766 * parameters to the end of our list if necessary.
4767 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004768 if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004769 params =
4770 nasm_realloc(params,
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004771 ((ed->nparam_min + ed->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004772 1) * sizeof(*params)));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004773 while (nparam < ed->nparam_min + ed->ndefs) {
4774 params[nparam] = ed->defaults[nparam - ed->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004775 nparam++;
4776 }
4777 }
4778 /*
4779 * If we've gone over the maximum parameter count (and
4780 * we're in Plus mode), ignore parameters beyond
4781 * nparam_max.
4782 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004783 if (ed->plus && nparam > ed->nparam_max)
4784 nparam = ed->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004785 /*
4786 * Then terminate the parameter list, and leave.
4787 */
4788 if (!params) { /* need this special case */
4789 params = nasm_malloc(sizeof(*params));
4790 nparam = 0;
4791 }
4792 params[nparam] = NULL;
4793 *params_array = params;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004794 return ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004795 }
4796 /*
4797 * This one wasn't right: look for the next one with the
4798 * same name.
4799 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004800 list_for_each(ed, ed->next)
4801 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004802 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004803 }
4804
4805 /*
4806 * After all that, we didn't find one with the right number of
4807 * parameters. Issue a warning, and fail to expand the macro.
4808 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004809 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004810 "macro `%s' exists, but not taking %d parameters",
4811 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004812 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004813 return NULL;
4814}
4815
4816/*
4817 * Expand the multi-line macro call made by the given line, if
4818 * there is one to be expanded. If there is, push the expansion on
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004819 * istk->expansion and return true. Otherwise return false.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004820 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004821static bool expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004822{
H. Peter Anvineba20a72002-04-30 20:53:55 +00004823 Token *label = NULL;
4824 int dont_prepend = 0;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004825 Token **params, *t, *mtok;
4826 Line *l = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004827 ExpDef *ed;
4828 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004829 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004830 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004831
4832 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004833 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004834 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004835 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004836 return false;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004837 mtok = t;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004838 ed = is_mmacro(t, &params);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004839 if (ed != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004840 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004841 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004842 Token *last;
4843 /*
4844 * We have an id which isn't a macro call. We'll assume
4845 * it might be a label; we'll also check to see if a
4846 * colon follows it. Then, if there's another id after
4847 * that lot, we'll check it again for macro-hood.
4848 */
4849 label = last = t;
4850 t = t->next;
4851 if (tok_type_(t, TOK_WHITESPACE))
4852 last = t, t = t->next;
4853 if (tok_is_(t, ":")) {
4854 dont_prepend = 1;
4855 last = t, t = t->next;
4856 if (tok_type_(t, TOK_WHITESPACE))
4857 last = t, t = t->next;
4858 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004859 if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, &params)))
4860 return false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004861 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004862 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004863 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004864 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004865
4866 /*
4867 * Fix up the parameters: this involves stripping leading and
4868 * trailing whitespace, then stripping braces if they are
4869 * present.
4870 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004871 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004872 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004873
H. Peter Anvine2c80182005-01-15 22:15:51 +00004874 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004875 int brace = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004876 int comma = (!ed->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004877
H. Peter Anvine2c80182005-01-15 22:15:51 +00004878 t = params[i];
4879 skip_white_(t);
4880 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004881 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004882 params[i] = t;
4883 paramlen[i] = 0;
4884 while (t) {
4885 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4886 break; /* ... because we have hit a comma */
4887 if (comma && t->type == TOK_WHITESPACE
4888 && tok_is_(t->next, ","))
4889 break; /* ... or a space then a comma */
4890 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4891 break; /* ... or a brace */
4892 t = t->next;
4893 paramlen[i]++;
4894 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004895 }
4896
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004897 if (ed->cur_depth >= ed->max_depth) {
4898 if (ed->max_depth > 1) {
4899 error(ERR_WARNING,
4900 "reached maximum macro recursion depth of %i for %s",
4901 ed->max_depth,ed->name);
4902 }
4903 return false;
4904 } else {
4905 ed->cur_depth ++;
4906 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004907
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004908 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004909 * OK, we have found a ExpDef structure representing a
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004910 * previously defined mmacro. Create an expansion invocation
4911 * and point it back to the expansion definition. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004912 * parameter tokens and macro-local tokens doesn't get done
4913 * until the single-line macro substitution process; this is
4914 * because delaying them allows us to change the semantics
4915 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004916 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004917 ei = new_ExpInv(EXP_MMACRO, ed);
4918 ei->name = nasm_strdup(mname);
4919 //ei->label = label;
4920 //ei->label_text = detoken(label, false);
4921 ei->current = ed->line;
4922 ei->emitting = true;
4923 //ei->iline = tline;
4924 ei->params = params;
4925 ei->nparam = nparam;
4926 ei->rotate = 0;
4927 ei->paramlen = paramlen;
4928 ei->lineno = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004929
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004930 ei->prev = istk->expansion;
4931 istk->expansion = ei;
4932
4933 /*
4934 * Special case: detect %00 on first invocation; if found,
4935 * avoid emitting any labels that precede the mmacro call.
4936 * ed->prepend is set to -1 when %00 is detected, else 1.
4937 */
4938 if (ed->prepend == 0) {
4939 for (l = ed->line; l != NULL; l = l->next) {
4940 for (t = l->first; t != NULL; t = t->next) {
4941 if ((t->type == TOK_PREPROC_ID) &&
4942 (strlen(t->text) == 3) &&
4943 (t->text[1] == '0') && (t->text[2] == '0')) {
4944 dont_prepend = -1;
4945 break;
4946 }
4947 }
4948 if (dont_prepend < 0) {
4949 break;
4950 }
4951 }
4952 ed->prepend = ((dont_prepend < 0) ? -1 : 1);
4953 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004954
4955 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004956 * If we had a label, push it on as the first line of
4957 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004958 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004959 if (label != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004960 if (ed->prepend < 0) {
4961 ei->label_text = detoken(label, false);
4962 } else {
4963 if (dont_prepend == 0) {
4964 t = label;
4965 while (t->next != NULL) {
4966 t = t->next;
4967 }
4968 t->next = new_Token(NULL, TOK_OTHER, ":", 0);
4969 }
4970 l = new_Line();
4971 l->first = copy_Token(label);
4972 l->next = ei->current;
4973 ei->current = l;
4974 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004975 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004976
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004977 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004978
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004979 istk->mmac_depth++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004980 return true;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004981}
4982
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004983/* The function that actually does the error reporting */
4984static void verror(int severity, const char *fmt, va_list arg)
4985{
4986 char buff[1024];
4987
4988 vsnprintf(buff, sizeof(buff), fmt, arg);
4989
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03004990 if (istk && istk->mmac_depth > 0) {
4991 ExpInv *ei = istk->expansion;
4992 int lineno = ei->lineno;
4993 while (ei) {
4994 if (ei->type == EXP_MMACRO)
4995 break;
4996 lineno += ei->relno;
4997 ei = ei->prev;
4998 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004999 nasm_error(severity, "(%s:%d) %s", ei->def->name,
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005000 lineno, buff);
5001 } else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005002 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005003}
5004
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005005/*
5006 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07005007 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005008 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005009static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005010{
5011 va_list arg;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005012 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005013 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00005014 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005015}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005016
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005017/*
5018 * Because %else etc are evaluated in the state context
5019 * of the previous branch, errors might get lost with error():
5020 * %if 0 ... %else trailing garbage ... %endif
5021 * So %else etc should report errors with this function.
5022 */
5023static void error_precond(int severity, const char *fmt, ...)
5024{
5025 va_list arg;
5026
5027 /* Only ignore the error if it's really in a dead branch */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005028 if ((istk != NULL) &&
5029 (istk->expansion != NULL) &&
5030 (istk->expansion->type == EXP_IF) &&
5031 (istk->expansion->def->state == COND_NEVER))
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005032 return;
5033
5034 va_start(arg, fmt);
5035 verror(severity, fmt, arg);
5036 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005037}
5038
H. Peter Anvin734b1882002-04-30 21:01:08 +00005039static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005040pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005041{
H. Peter Anvin7383b402008-09-24 10:20:40 -07005042 Token *t;
5043
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005044 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005045 istk = nasm_malloc(sizeof(Include));
5046 istk->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005047 istk->expansion = NULL;
5048 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00005049 istk->fname = NULL;
5050 src_set_fname(nasm_strdup(file));
5051 src_set_linnum(0);
5052 istk->lineinc = 1;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005053 istk->mmac_depth = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005054 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07005055 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00005056 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005057 defining = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005058 finals = NULL;
5059 in_final = false;
Charles Crayned4200be2008-07-12 16:42:33 -07005060 nested_mac_count = 0;
5061 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005062 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005063 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005064 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005065 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005066 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005067 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005068 }
H. Peter Anvind2456592008-06-19 15:04:18 -07005069 any_extrastdmac = extrastdmac && *extrastdmac;
5070 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005071 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005072
5073 /*
5074 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5075 * The caller, however, will also pass in 3 for preprocess-only so
5076 * we can set __PASS__ accordingly.
5077 */
5078 pass = apass > 2 ? 2 : apass;
5079
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005080 dephead = deptail = deplist;
5081 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005082 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
5083 sl->next = NULL;
5084 strcpy(sl->str, file);
5085 *deptail = sl;
5086 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005087 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07005088
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005089 /*
5090 * Define the __PASS__ macro. This is defined here unlike
5091 * all the other builtins, because it is special -- it varies between
5092 * passes.
5093 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07005094 t = nasm_malloc(sizeof(*t));
5095 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005096 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07005097 t->a.mac = NULL;
5098 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005099}
5100
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005101static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005102{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005103 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005104 Token *tline;
Keith Kanios9858cec2010-11-08 00:58:02 -06005105 ExpDef *ed;
5106 ExpInv *ei;
5107 Line *l;
5108 int j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005109
H. Peter Anvine2c80182005-01-15 22:15:51 +00005110 while (1) {
5111 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005112 * Fetch a tokenized line, either from the expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005113 * buffer or from the input file.
5114 */
5115 tline = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005116
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005117 while (1) { /* until we get a line we can use */
5118 /*
5119 * Fetch a tokenized line from the expansion buffer
5120 */
5121 if (istk->expansion != NULL) {
5122 ei = istk->expansion;
5123 if (ei->current != NULL) {
5124 if (ei->emitting == false) {
5125 ei->current = NULL;
5126 continue;
5127 }
5128 l = ei->current;
5129 ei->current = l->next;
5130 ei->lineno++;
5131 tline = copy_Token(l->first);
5132 if (((ei->type == EXP_REP) ||
5133 (ei->type == EXP_MMACRO) ||
5134 (ei->type == EXP_WHILE))
5135 && (ei->def->nolist == false)) {
5136 char *p = detoken(tline, false);
5137 list->line(LIST_MACRO, p);
5138 nasm_free(p);
5139 }
5140 if (ei->linnum > -1) {
5141 src_set_linnum(src_get_linnum() + 1);
5142 }
5143 break;
5144 } else if ((ei->type == EXP_REP) &&
5145 (ei->def->cur_depth < ei->def->max_depth)) {
5146 ei->def->cur_depth ++;
5147 ei->current = ei->def->line;
5148 ei->lineno = 0;
5149 continue;
5150 } else if ((ei->type == EXP_WHILE) &&
5151 (ei->def->cur_depth < ei->def->max_depth)) {
5152 ei->current = ei->def->line;
5153 ei->lineno = 0;
5154 tline = copy_Token(ei->current->first);
5155 j = if_condition(tline, PP_WHILE);
5156 tline = NULL;
5157 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
5158 if (j == COND_IF_TRUE) {
5159 ei->current = ei->current->next;
5160 ei->def->cur_depth ++;
5161 } else {
5162 ei->emitting = false;
5163 ei->current = NULL;
5164 ei->def->cur_depth = ei->def->max_depth;
5165 }
5166 continue;
5167 } else {
5168 istk->expansion = ei->prev;
5169 ed = ei->def;
5170 if (ed != NULL) {
5171 if ((ei->emitting == true) &&
5172 (ed->max_depth == DEADMAN_LIMIT) &&
5173 (ed->cur_depth == DEADMAN_LIMIT)
5174 ) {
5175 error(ERR_FATAL, "runaway expansion detected, aborting");
5176 }
5177 if (ed->cur_depth > 0) {
5178 ed->cur_depth --;
5179 } else if ((ed->type != EXP_MMACRO) && (ed->type != EXP_IF)) {
5180 /***** should this really be right here??? *****/
5181 /*
5182 Line *l = NULL, *ll = NULL;
5183 for (l = ed->line; l != NULL;) {
5184 if (l->first != NULL) {
5185 free_tlist(l->first);
5186 l->first = NULL;
5187 }
5188 ll = l;
5189 l = l->next;
5190 nasm_free(ll);
5191 }
5192 expansions = ed->prev;
5193 nasm_free(ed);
5194 */
5195 }
5196 if ((ei->type == EXP_REP) ||
5197 (ei->type == EXP_MMACRO) ||
5198 (ei->type == EXP_WHILE)) {
5199 list->downlevel(LIST_MACRO);
5200 if (ei->type == EXP_MMACRO) {
5201 istk->mmac_depth--;
5202 }
5203 }
5204 }
5205 if (ei->linnum > -1) {
5206 src_set_linnum(ei->linnum);
5207 }
5208 free_expinv(ei);
5209 continue;
5210 }
5211 }
5212
5213 /*
5214 * Read in line from input and tokenize
5215 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005216 line = read_line();
5217 if (line) { /* from the current input file */
5218 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005219 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005220 nasm_free(line);
5221 break;
5222 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005223
H. Peter Anvine2c80182005-01-15 22:15:51 +00005224 /*
5225 * The current file has ended; work down the istk
5226 */
5227 {
5228 Include *i = istk;
5229 fclose(i->fp);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005230 if (i->expansion != NULL) {
5231 error(ERR_FATAL,
5232 "end of file while still in an expansion");
5233 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005234 /* only set line and file name if there's a next node */
5235 if (i->next) {
5236 src_set_linnum(i->lineno);
5237 nasm_free(src_set_fname(i->fname));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005238 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005239 if ((i->next == NULL) && (finals != NULL)) {
5240 in_final = true;
5241 ei = new_ExpInv(EXP_FINAL, NULL);
5242 ei->emitting = true;
5243 ei->current = finals;
5244 istk->expansion = ei;
5245 finals = NULL;
5246 continue;
5247 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005248 istk = i->next;
5249 list->downlevel(LIST_INCLUDE);
5250 nasm_free(i);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005251 if (istk == NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005252 if (finals != NULL) {
5253 in_final = true;
5254 } else {
5255 return NULL;
5256 }
5257 }
5258 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005259 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005260 }
5261
5262 if (defining == NULL) {
5263 tline = expand_mmac_params(tline);
5264 }
5265
H. Peter Anvine2c80182005-01-15 22:15:51 +00005266 /*
5267 * Check the line to see if it's a preprocessor directive.
5268 */
5269 if (do_directive(tline) == DIRECTIVE_FOUND) {
5270 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005271 } else if (defining != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005272 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005273 * We're defining an expansion. We emit nothing at all,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005274 * and just shove the tokenized line on to the definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005275 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005276 if (defining->ignoring == false) {
5277 Line *l = new_Line();
5278 l->first = tline;
5279 if (defining->line == NULL) {
5280 defining->line = l;
5281 defining->last = l;
5282 } else {
5283 defining->last->next = l;
5284 defining->last = l;
5285 }
5286 } else {
5287 //free_tlist(tline); /***** sanity check: is this supposed to be here? *****/
5288 }
5289 defining->linecount++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005290 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005291 } else if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005292 (istk->expansion->emitting != true)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005293 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005294 * We're in a non-emitting branch of an expansion.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005295 * Emit nothing at all, not even a blank line: when we
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005296 * emerge from the expansion we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005297 * directive so we keep our place correctly.
5298 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005299 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005300 continue;
5301 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005302 tline = expand_smacro(tline);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005303 if (expand_mmacro(tline) != true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005304 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005305 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005306 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005307 line = detoken(tline, true);
5308 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005309 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005310 } else {
5311 continue;
5312 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005313 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005314 }
5315 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005316}
5317
H. Peter Anvine2c80182005-01-15 22:15:51 +00005318static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005319{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005320 if (defining != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005321 error(ERR_NONFATAL, "end of file while still defining an expansion");
5322 while (defining != NULL) {
5323 ExpDef *ed = defining;
5324 defining = ed->prev;
5325 free_expdef(ed);
5326 }
5327 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005328 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005329 while (cstk != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005330 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005331 free_macros();
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005332 while (istk != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005333 Include *i = istk;
5334 istk = istk->next;
5335 fclose(i->fp);
5336 nasm_free(i->fname);
5337 nasm_free(i);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005338 while (i->expansion != NULL) {
5339 ExpInv *ei = i->expansion;
5340 i->expansion = ei->prev;
5341 free_expinv(ei);
5342 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005343 }
5344 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005345 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005346 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005347 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005348 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005349 free_llist(predef);
5350 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005351 while ((i = ipath)) {
5352 ipath = i->next;
5353 if (i->path)
5354 nasm_free(i->path);
5355 nasm_free(i);
5356 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005357 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005358}
5359
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005360void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005361{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005362 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005363
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005364 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005365 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005366 i->next = NULL;
5367
H. Peter Anvin89cee572009-07-15 09:16:54 -04005368 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005369 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005370 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005371 j = j->next;
5372 j->next = i;
5373 } else {
5374 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005375 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005376}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005377
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005378void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005379{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005380 Token *inc, *space, *name;
5381 Line *l;
5382
H. Peter Anvin734b1882002-04-30 21:01:08 +00005383 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5384 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5385 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005386
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005387 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005388 l->next = predef;
5389 l->first = inc;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005390 predef = l;
5391}
5392
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005393void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005394{
5395 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005396 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005397 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005398
5399 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005400 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5401 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005402 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005403 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005404 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005405 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005406 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005407
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005408 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005409 l->next = predef;
5410 l->first = def;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005411 predef = l;
5412}
5413
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005414void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005415{
5416 Token *def, *space;
5417 Line *l;
5418
H. Peter Anvin734b1882002-04-30 21:01:08 +00005419 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5420 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005421 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005422
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005423 l = new_Line();
H. Peter Anvin620515a2002-04-30 20:57:38 +00005424 l->next = predef;
5425 l->first = def;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005426 predef = l;
5427}
5428
Keith Kaniosb7a89542007-04-12 02:40:54 +00005429/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005430 * This function is used to assist with "runtime" preprocessor
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005431 * directives, e.g. pp_runtime("%define __BITS__ 64");
Keith Kaniosb7a89542007-04-12 02:40:54 +00005432 *
5433 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5434 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5435 */
5436
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005437void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005438{
5439 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005440
Keith Kaniosb7a89542007-04-12 02:40:54 +00005441 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005442 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005443 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005444
Keith Kaniosb7a89542007-04-12 02:40:54 +00005445}
5446
H. Peter Anvina70547f2008-07-19 21:44:26 -07005447void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005448{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005449 extrastdmac = macros;
5450}
5451
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005452static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005453{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005454 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005455 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005456 tok->text = nasm_strdup(numbuf);
5457 tok->type = TOK_NUMBER;
5458}
5459
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005460Preproc nasmpp = {
5461 pp_reset,
5462 pp_getline,
5463 pp_cleanup
5464};