blob: 5f8a767f1cfa782b5aecf165e3b47c82f353b9f5 [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,
194 EXP_MMACRO, EXP_REP,
195 EXP_IF, EXP_WHILE,
196 EXP_COMMENT, EXP_FINAL,
197 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 */
212 ExpDef *next; /* next in hash table */
213 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 */
222
223 int prepend; /* label prepend state */
224 Line *label;
225 Line *line;
226 Line *last;
227 int linecount; /* number of lines within expansion */
228
229 int64_t def_depth; /* current number of definition pairs deep */
230 int64_t cur_depth; /* current number of expansions */
231 int64_t max_depth; /* maximum number of expansions allowed */
232
233 int state; /* condition state */
234 bool ignoring; /* ignoring definition lines */
235};
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 */
249 char *name; /* invocation name */
250 Line *label; /* pointer to label */
251 char *label_text; /* pointer to label text */
252 Line *current; /* pointer to current line in invocation */
253
254 Token **params; /* actual parameters */
255 Token *iline; /* invocation line */
256 unsigned int nparam, rotate;
257 int *paramlen;
258
259 uint64_t unique;
260 bool emitting;
261 int lineno; /* current line number in expansion */
262 int linnum; /* line number at invocation */
263 int relno; /* relative line number at invocation */
264};
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;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500277 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;
516 }
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{
643 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) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500788 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;
809 ei = new_ExpInv(EXP_PREDEF, NULL);
810 ei->current = l;
811 ei->emitting = true;
812 ei->prev = istk->expansion;
813 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
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500888 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;
1246 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 != NULL) ? 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 }
1267 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) &&
1338 ((t->type == TOK_PREPROC_Q) ||
1339 (t->type == TOK_PREPROC_QQ))) {
1340 ExpInv *ei;
1341 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 }
1353
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 */
1379static Line *new_Line(void)
1380{
1381 Line *l = nasm_malloc(sizeof(Line));
1382 l->next = NULL;
1383 l->first = NULL;
1384 return l;
1385}
1386
1387
1388/*
1389 * Initialize a new Expansion Definition
1390 */
1391static ExpDef *new_ExpDef(int exp_type)
1392{
1393 ExpDef *ed = nasm_malloc(sizeof(ExpDef));
1394 ed->prev = NULL;
1395 ed->next = NULL;
1396 ed->type = exp_type;
1397 ed->name = NULL;
1398 ed->nparam_min = 0;
1399 ed->nparam_max = 0;
1400 ed->casesense = true;
1401 ed->plus = false;
1402 ed->prepend = 0;
1403 ed->label = NULL;
1404 ed->line = NULL;
1405 ed->last = NULL;
1406 ed->linecount = 0;
1407 ed->dlist = NULL;
1408 ed->defaults = NULL;
1409 ed->ndefs = 0;
1410 ed->state = COND_NEVER;
1411 ed->nolist = false;
1412 ed->def_depth = 0;
1413 ed->cur_depth = 0;
1414 ed->max_depth = 0;
1415 ed->ignoring = false;
1416 return ed;
1417}
1418
1419
1420/*
1421 * Initialize a new Expansion Instance
1422 */
1423static ExpInv *new_ExpInv(int exp_type, ExpDef *ed)
1424{
1425 unique ++;
1426 ExpInv *ei = nasm_malloc(sizeof(ExpInv));
1427 ei->prev = NULL;
1428 ei->type = exp_type;
1429 ei->def = ed;
1430 ei->name = NULL;
1431 ei->label = NULL;
1432 ei->label_text = NULL;
1433 ei->current = NULL;
1434 ei->params = NULL;
1435 ei->iline = NULL;
1436 ei->nparam = 0;
1437 ei->rotate = 0;
1438 ei->paramlen = NULL;
1439 ei->unique = unique;
1440 ei->emitting = false;
1441 ei->lineno = 0;
1442 if ((istk->mmac_depth < 1) &&
1443 (istk->expansion == NULL) &&
1444 (ed != NULL) &&
1445 (ed->type != EXP_MMACRO) &&
1446 (ed->type != EXP_REP) &&
1447 (ed->type != EXP_WHILE)) {
1448 ei->linnum = src_get_linnum();
1449 src_set_linnum(ei->linnum - ed->linecount - 1);
1450 } else {
1451 ei->linnum = -1;
1452 }
1453 if ((istk->expansion == NULL) ||
1454 (ei->type == EXP_MMACRO)) {
1455 ei->relno = 0;
1456 } else {
1457 ei->relno = istk->expansion->lineno;
1458 if (ed != NULL) {
1459 ei->relno -= (ed->linecount + 1);
1460 }
1461 }
1462 return ei;
1463}
1464
1465/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001466 * A scanner, suitable for use by the expression evaluator, which
1467 * operates on a line of Tokens. Expects a pointer to a pointer to
1468 * the first token in the line to be passed in as its private_data
1469 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001470 *
1471 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001472 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001473static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001474{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001475 Token **tlineptr = private_data;
1476 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001477 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001478
H. Peter Anvine2c80182005-01-15 22:15:51 +00001479 do {
1480 tline = *tlineptr;
1481 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001482 } while (tline && (tline->type == TOK_WHITESPACE ||
1483 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001484
1485 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001486 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001487
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001488 tokval->t_charptr = tline->text;
1489
H. Peter Anvin76690a12002-04-30 20:52:49 +00001490 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001491 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001492 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001493 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001494
H. Peter Anvine2c80182005-01-15 22:15:51 +00001495 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001496 p = tokval->t_charptr = tline->text;
1497 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001498 tokval->t_charptr++;
1499 return tokval->t_type = TOKEN_ID;
1500 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001501
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001502 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001503 if (r >= p+MAX_KEYWORD)
1504 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001505 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001506 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001507 *s = '\0';
1508 /* right, so we have an identifier sitting in temp storage. now,
1509 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001510 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001511 }
1512
H. Peter Anvine2c80182005-01-15 22:15:51 +00001513 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001514 bool rn_error;
1515 tokval->t_integer = readnum(tline->text, &rn_error);
1516 tokval->t_charptr = tline->text;
1517 if (rn_error)
1518 return tokval->t_type = TOKEN_ERRNUM;
1519 else
1520 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001521 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001522
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001523 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001524 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001525 }
1526
H. Peter Anvine2c80182005-01-15 22:15:51 +00001527 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001528 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001529
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001530 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001531 tokval->t_charptr = tline->text;
1532 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001533
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001534 if (ep[0] != bq || ep[1] != '\0')
1535 return tokval->t_type = TOKEN_ERRSTR;
1536 else
1537 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001538 }
1539
H. Peter Anvine2c80182005-01-15 22:15:51 +00001540 if (tline->type == TOK_OTHER) {
1541 if (!strcmp(tline->text, "<<"))
1542 return tokval->t_type = TOKEN_SHL;
1543 if (!strcmp(tline->text, ">>"))
1544 return tokval->t_type = TOKEN_SHR;
1545 if (!strcmp(tline->text, "//"))
1546 return tokval->t_type = TOKEN_SDIV;
1547 if (!strcmp(tline->text, "%%"))
1548 return tokval->t_type = TOKEN_SMOD;
1549 if (!strcmp(tline->text, "=="))
1550 return tokval->t_type = TOKEN_EQ;
1551 if (!strcmp(tline->text, "<>"))
1552 return tokval->t_type = TOKEN_NE;
1553 if (!strcmp(tline->text, "!="))
1554 return tokval->t_type = TOKEN_NE;
1555 if (!strcmp(tline->text, "<="))
1556 return tokval->t_type = TOKEN_LE;
1557 if (!strcmp(tline->text, ">="))
1558 return tokval->t_type = TOKEN_GE;
1559 if (!strcmp(tline->text, "&&"))
1560 return tokval->t_type = TOKEN_DBL_AND;
1561 if (!strcmp(tline->text, "^^"))
1562 return tokval->t_type = TOKEN_DBL_XOR;
1563 if (!strcmp(tline->text, "||"))
1564 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001565 }
1566
1567 /*
1568 * We have no other options: just return the first character of
1569 * the token text.
1570 */
1571 return tokval->t_type = tline->text[0];
1572}
1573
1574/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001575 * Compare a string to the name of an existing macro; this is a
1576 * simple wrapper which calls either strcmp or nasm_stricmp
1577 * depending on the value of the `casesense' parameter.
1578 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001579static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001580{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001581 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001582}
1583
1584/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001585 * Compare a string to the name of an existing macro; this is a
1586 * simple wrapper which calls either strcmp or nasm_stricmp
1587 * depending on the value of the `casesense' parameter.
1588 */
1589static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1590{
1591 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1592}
1593
1594/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001595 * Return the Context structure associated with a %$ token. Return
1596 * NULL, having _already_ reported an error condition, if the
1597 * context stack isn't deep enough for the supplied number of $
1598 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001599 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001600 * also scanned for such smacro, until it is found; if not -
1601 * only the context that directly results from the number of $'s
1602 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001603 *
1604 * If "namep" is non-NULL, set it to the pointer to the macro name
1605 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001606 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001607static Context *get_ctx(const char *name, const char **namep,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001608 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001609{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001610 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001611 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001612 int i;
1613
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001614 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001615 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001616
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001617 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001618 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001619
H. Peter Anvine2c80182005-01-15 22:15:51 +00001620 if (!cstk) {
1621 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1622 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001623 }
1624
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001625 name += 2;
1626 ctx = cstk;
1627 i = 0;
1628 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001629 name++;
1630 i++;
1631 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001632 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001633 if (!ctx) {
1634 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001635 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001636 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001637 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001638
1639 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001640 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001641
Keith Kanios404589e2010-08-10 20:12:57 -05001642 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001643 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001644
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001645 do {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001646 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001647 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001648 while (m) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001649 if (!mstrcmp(m->name, name, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001650 return ctx;
1651 m = m->next;
1652 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001653 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001654 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001655 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001656 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001657}
1658
1659/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001660 * Check to see if a file is already in a string list
1661 */
1662static bool in_list(const StrList *list, const char *str)
1663{
1664 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001665 if (!strcmp(list->str, str))
1666 return true;
1667 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001668 }
1669 return false;
1670}
1671
1672/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001673 * Open an include file. This routine must always return a valid
1674 * file pointer if it returns - it's responsible for throwing an
1675 * ERR_FATAL and bombing out completely if not. It should also try
1676 * the include path one by one until it finds the file or reaches
1677 * the end of the path.
1678 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001679static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001680 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001681{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001682 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001683 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001684 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001685 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001686 size_t prefix_len = 0;
1687 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001688
H. Peter Anvine2c80182005-01-15 22:15:51 +00001689 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001690 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001691 memcpy(sl->str, prefix, prefix_len);
1692 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001693 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001694 if (fp && dhead && !in_list(*dhead, sl->str)) {
1695 sl->next = NULL;
1696 **dtail = sl;
1697 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001698 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001699 nasm_free(sl);
1700 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001701 if (fp)
1702 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001703 if (!ip) {
1704 if (!missing_ok)
1705 break;
1706 prefix = NULL;
1707 } else {
1708 prefix = ip->path;
1709 ip = ip->next;
1710 }
1711 if (prefix) {
1712 prefix_len = strlen(prefix);
1713 } else {
1714 /* -MG given and file not found */
1715 if (dhead && !in_list(*dhead, file)) {
1716 sl = nasm_malloc(len+1+sizeof sl->next);
1717 sl->next = NULL;
1718 strcpy(sl->str, file);
1719 **dtail = sl;
1720 *dtail = &sl->next;
1721 }
1722 return NULL;
1723 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001724 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001725
H. Peter Anvin734b1882002-04-30 21:01:08 +00001726 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001727 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001728}
1729
1730/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001731 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001732 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001733 * return true if _any_ single-line macro of that name is defined.
1734 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001735 * `nparam' or no parameters is defined.
1736 *
1737 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001738 * defined, or nparam is -1, the address of the definition structure
1739 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001740 * is NULL, no action will be taken regarding its contents, and no
1741 * error will occur.
1742 *
1743 * Note that this is also called with nparam zero to resolve
1744 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001745 *
1746 * If you already know which context macro belongs to, you can pass
1747 * the context pointer as first parameter; if you won't but name begins
1748 * with %$ the context will be automatically computed. If all_contexts
1749 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001750 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001751static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001752smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001753 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001754{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001755 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001756 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001757
H. Peter Anvin97a23472007-09-16 17:57:25 -07001758 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001759 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001760 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001761 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001762 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001763 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001764 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001765 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001766 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001767 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001768 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001769 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001770
H. Peter Anvine2c80182005-01-15 22:15:51 +00001771 while (m) {
1772 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001773 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001774 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001775 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001776 *defn = m;
1777 else
1778 *defn = NULL;
1779 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001780 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001781 }
1782 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001783 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001784
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001785 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001786}
1787
1788/*
1789 * Count and mark off the parameters in a multi-line macro call.
1790 * This is called both from within the multi-line macro expansion
1791 * code, and also to mark off the default parameters when provided
1792 * in a %macro definition line.
1793 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001794static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001795{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001796 int paramsize, brace;
1797
1798 *nparam = paramsize = 0;
1799 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001800 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001801 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001802 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001803 paramsize += PARAM_DELTA;
1804 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1805 }
1806 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001807 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001808 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001809 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001810 (*params)[(*nparam)++] = t;
1811 while (tok_isnt_(t, brace ? "}" : ","))
1812 t = t->next;
1813 if (t) { /* got a comma/brace */
1814 t = t->next;
1815 if (brace) {
1816 /*
1817 * Now we've found the closing brace, look further
1818 * for the comma.
1819 */
1820 skip_white_(t);
1821 if (tok_isnt_(t, ",")) {
1822 error(ERR_NONFATAL,
1823 "braces do not enclose all of macro parameter");
1824 while (tok_isnt_(t, ","))
1825 t = t->next;
1826 }
1827 if (t)
1828 t = t->next; /* eat the comma */
1829 }
1830 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001831 }
1832}
1833
1834/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001835 * Determine whether one of the various `if' conditions is true or
1836 * not.
1837 *
1838 * We must free the tline we get passed.
1839 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001840static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001841{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001842 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001843 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001844 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001845 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001846 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001847 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001848 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001849
1850 origline = tline;
1851
H. Peter Anvine2c80182005-01-15 22:15:51 +00001852 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001853 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001854 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001855 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001856 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001857 if (!tline)
1858 break;
1859 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001860 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001861 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001862 free_tlist(origline);
1863 return -1;
1864 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001865 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001866 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001867 tline = tline->next;
1868 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001869 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001870
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001871 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001872 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001873 while (tline) {
1874 skip_white_(tline);
1875 if (!tline || (tline->type != TOK_ID &&
1876 (tline->type != TOK_PREPROC_ID ||
1877 tline->text[1] != '$'))) {
1878 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001879 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001880 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001881 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001882 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001883 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001884 tline = tline->next;
1885 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001886 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001887
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001888 case PPC_IFENV:
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001889 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001890 j = false; /* have we matched yet? */
1891 while (tline) {
1892 skip_white_(tline);
1893 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001894 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001895 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001896 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001897 error(ERR_NONFATAL,
1898 "`%s' expects environment variable names",
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001899 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001900 goto fail;
1901 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001902 p = tline->text;
1903 if (tline->type == TOK_PREPROC_ID)
1904 p += 2; /* Skip leading %! */
1905 if (*p == '\'' || *p == '\"' || *p == '`')
1906 nasm_unquote_cstr(p, ct);
1907 if (getenv(p))
1908 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001909 tline = tline->next;
1910 }
1911 break;
1912
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001913 case PPC_IFIDN:
1914 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001915 tline = expand_smacro(tline);
1916 t = tt = tline;
1917 while (tok_isnt_(tt, ","))
1918 tt = tt->next;
1919 if (!tt) {
1920 error(ERR_NONFATAL,
1921 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001922 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001923 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001924 }
1925 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001926 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001927 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1928 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1929 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001930 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001931 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001932 }
1933 if (t->type == TOK_WHITESPACE) {
1934 t = t->next;
1935 continue;
1936 }
1937 if (tt->type == TOK_WHITESPACE) {
1938 tt = tt->next;
1939 continue;
1940 }
1941 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001942 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001943 break;
1944 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001945 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001946 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001947 size_t l1 = nasm_unquote(t->text, NULL);
1948 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001949
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001950 if (l1 != l2) {
1951 j = false;
1952 break;
1953 }
1954 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1955 j = false;
1956 break;
1957 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001958 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001959 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001960 break;
1961 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001962
H. Peter Anvine2c80182005-01-15 22:15:51 +00001963 t = t->next;
1964 tt = tt->next;
1965 }
1966 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001967 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001968 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001969
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001970 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001971 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001972 bool found = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001973 ExpDef searching, *ed;
H. Peter Anvin65747262002-05-07 00:10:05 +00001974
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001975 skip_white_(tline);
1976 tline = expand_id(tline);
1977 if (!tok_type_(tline, TOK_ID)) {
1978 error(ERR_NONFATAL,
1979 "`%s' expects a macro name", pp_directives[ct]);
1980 goto fail;
1981 }
1982 searching.name = nasm_strdup(tline->text);
1983 searching.casesense = true;
1984 searching.plus = false;
1985 searching.nolist = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001986 //searching.in_progress = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001987 searching.max_depth = 0;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001988 //searching.rep_nest = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001989 searching.nparam_min = 0;
1990 searching.nparam_max = INT_MAX;
1991 tline = expand_smacro(tline->next);
1992 skip_white_(tline);
1993 if (!tline) {
1994 } else if (!tok_type_(tline, TOK_NUMBER)) {
1995 error(ERR_NONFATAL,
1996 "`%s' expects a parameter count or nothing",
1997 pp_directives[ct]);
1998 } else {
1999 searching.nparam_min = searching.nparam_max =
2000 readnum(tline->text, &j);
2001 if (j)
2002 error(ERR_NONFATAL,
2003 "unable to parse parameter count `%s'",
2004 tline->text);
2005 }
2006 if (tline && tok_is_(tline->next, "-")) {
2007 tline = tline->next->next;
2008 if (tok_is_(tline, "*"))
2009 searching.nparam_max = INT_MAX;
2010 else if (!tok_type_(tline, TOK_NUMBER))
2011 error(ERR_NONFATAL,
2012 "`%s' expects a parameter count after `-'",
2013 pp_directives[ct]);
2014 else {
2015 searching.nparam_max = readnum(tline->text, &j);
2016 if (j)
2017 error(ERR_NONFATAL,
2018 "unable to parse parameter count `%s'",
2019 tline->text);
2020 if (searching.nparam_min > searching.nparam_max)
2021 error(ERR_NONFATAL,
2022 "minimum parameter count exceeds maximum");
2023 }
2024 }
2025 if (tline && tok_is_(tline->next, "+")) {
2026 tline = tline->next;
2027 searching.plus = true;
2028 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002029 ed = (ExpDef *) hash_findix(&expdefs, searching.name);
2030 while (ed != NULL) {
2031 if (!strcmp(ed->name, searching.name) &&
2032 (ed->nparam_min <= searching.nparam_max
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002033 || searching.plus)
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002034 && (searching.nparam_min <= ed->nparam_max
2035 || ed->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002036 found = true;
2037 break;
2038 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002039 ed = ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002040 }
2041 if (tline && tline->next)
2042 error(ERR_WARNING|ERR_PASS1,
2043 "trailing garbage after %%ifmacro ignored");
2044 nasm_free(searching.name);
2045 j = found;
2046 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002047 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002048
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002049 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002050 needtype = TOK_ID;
2051 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002052 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002053 needtype = TOK_NUMBER;
2054 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002055 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002056 needtype = TOK_STRING;
2057 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002058
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002059iftype:
2060 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002061
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002062 while (tok_type_(t, TOK_WHITESPACE) ||
2063 (needtype == TOK_NUMBER &&
2064 tok_type_(t, TOK_OTHER) &&
2065 (t->text[0] == '-' || t->text[0] == '+') &&
2066 !t->text[1]))
2067 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002068
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002069 j = tok_type_(t, needtype);
2070 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002071
2072 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002073 t = tline = expand_smacro(tline);
2074 while (tok_type_(t, TOK_WHITESPACE))
2075 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002076
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002077 j = false;
2078 if (t) {
2079 t = t->next; /* Skip the actual token */
2080 while (tok_type_(t, TOK_WHITESPACE))
2081 t = t->next;
2082 j = !t; /* Should be nothing left */
2083 }
2084 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002085
H. Peter Anvin134b9462008-02-16 17:01:40 -08002086 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002087 t = tline = expand_smacro(tline);
2088 while (tok_type_(t, TOK_WHITESPACE))
2089 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002090
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002091 j = !t; /* Should be empty */
2092 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002093
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002094 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002095 t = tline = expand_smacro(tline);
2096 tptr = &t;
2097 tokval.t_type = TOKEN_INVALID;
2098 evalresult = evaluate(ppscan, tptr, &tokval,
2099 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002100 if (!evalresult)
2101 return -1;
2102 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002103 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002104 "trailing garbage after expression ignored");
2105 if (!is_simple(evalresult)) {
2106 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002107 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002108 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002109 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002110 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002111 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002112
H. Peter Anvine2c80182005-01-15 22:15:51 +00002113 default:
2114 error(ERR_FATAL,
2115 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002116 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002117 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002118 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002119
2120 free_tlist(origline);
2121 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002122
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002123fail:
2124 free_tlist(origline);
2125 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002126}
2127
2128/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002129 * Common code for defining an smacro
2130 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002131static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002132 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002133{
2134 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002135 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002136
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002137 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002138 if (!smac) {
2139 error(ERR_WARNING|ERR_PASS1,
2140 "single-line macro `%s' defined both with and"
2141 " without parameters", mname);
2142 /*
2143 * Some instances of the old code considered this a failure,
2144 * some others didn't. What is the right thing to do here?
2145 */
2146 free_tlist(expansion);
2147 return false; /* Failure */
2148 } else {
2149 /*
2150 * We're redefining, so we have to take over an
2151 * existing SMacro structure. This means freeing
2152 * what was already in it.
2153 */
2154 nasm_free(smac->name);
2155 free_tlist(smac->expansion);
2156 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002157 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002158 smtbl = ctx ? &ctx->localmac : &smacros;
2159 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2160 smac = nasm_malloc(sizeof(SMacro));
2161 smac->next = *smhead;
2162 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002163 }
2164 smac->name = nasm_strdup(mname);
2165 smac->casesense = casesense;
2166 smac->nparam = nparam;
2167 smac->expansion = expansion;
2168 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002169 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002170}
2171
2172/*
2173 * Undefine an smacro
2174 */
2175static void undef_smacro(Context *ctx, const char *mname)
2176{
2177 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002178 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002179
H. Peter Anvin166c2472008-05-28 12:28:58 -07002180 smtbl = ctx ? &ctx->localmac : &smacros;
2181 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002182
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002183 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002184 /*
2185 * We now have a macro name... go hunt for it.
2186 */
2187 sp = smhead;
2188 while ((s = *sp) != NULL) {
2189 if (!mstrcmp(s->name, mname, s->casesense)) {
2190 *sp = s->next;
2191 nasm_free(s->name);
2192 free_tlist(s->expansion);
2193 nasm_free(s);
2194 } else {
2195 sp = &s->next;
2196 }
2197 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002198 }
2199}
2200
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002201/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002202 * Parse a mmacro specification.
2203 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002204static bool parse_mmacro_spec(Token *tline, ExpDef *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002205{
2206 bool err;
2207
2208 tline = tline->next;
2209 skip_white_(tline);
2210 tline = expand_id(tline);
2211 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002212 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2213 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002214 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002215
H. Peter Anvina26433d2008-07-16 14:40:01 -07002216 def->name = nasm_strdup(tline->text);
2217 def->plus = false;
2218 def->nolist = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002219// def->in_progress = 0;
2220// def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002221 def->nparam_min = 0;
2222 def->nparam_max = 0;
2223
H. Peter Anvina26433d2008-07-16 14:40:01 -07002224 tline = expand_smacro(tline->next);
2225 skip_white_(tline);
2226 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002227 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002228 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002229 def->nparam_min = def->nparam_max =
2230 readnum(tline->text, &err);
2231 if (err)
2232 error(ERR_NONFATAL,
2233 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002234 }
2235 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002236 tline = tline->next->next;
2237 if (tok_is_(tline, "*")) {
2238 def->nparam_max = INT_MAX;
2239 } else if (!tok_type_(tline, TOK_NUMBER)) {
2240 error(ERR_NONFATAL,
2241 "`%s' expects a parameter count after `-'", directive);
2242 } else {
2243 def->nparam_max = readnum(tline->text, &err);
2244 if (err) {
2245 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2246 tline->text);
2247 }
2248 if (def->nparam_min > def->nparam_max) {
2249 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2250 }
2251 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002252 }
2253 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002254 tline = tline->next;
2255 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002256 }
2257 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002258 !nasm_stricmp(tline->next->text, ".nolist")) {
2259 tline = tline->next;
2260 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002261 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002262
H. Peter Anvina26433d2008-07-16 14:40:01 -07002263 /*
2264 * Handle default parameters.
2265 */
2266 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002267 def->dlist = tline->next;
2268 tline->next = NULL;
2269 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002270 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002271 def->dlist = NULL;
2272 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002273 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002274 def->line = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002275
H. Peter Anvin89cee572009-07-15 09:16:54 -04002276 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002277 !def->plus)
2278 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2279 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002280
H. Peter Anvina26433d2008-07-16 14:40:01 -07002281 return true;
2282}
2283
2284
2285/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002286 * Decode a size directive
2287 */
2288static int parse_size(const char *str) {
2289 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002290 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002291 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002292 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002293
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002294 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002295}
2296
Ed Beroset3ab3f412002-06-11 03:31:49 +00002297/**
2298 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002299 * Find out if a line contains a preprocessor directive, and deal
2300 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002301 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002302 * If a directive _is_ found, it is the responsibility of this routine
2303 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002304 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002305 * @param tline a pointer to the current tokeninzed line linked list
2306 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002307 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002308 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002309static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002310{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002311 enum preproc_token i;
2312 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002313 bool err;
2314 int nparam;
2315 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002316 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002317 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002318 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002319 char *p, *pp;
2320 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002321 Include *inc;
2322 Context *ctx;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002323 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002324 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002325 struct tokenval tokval;
2326 expr *evalresult;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002327 ExpDef *ed, *eed, **edhead;
2328 ExpInv *ei, *eei;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002329 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002330 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002331 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002332
2333 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002334
H. Peter Anvineba20a72002-04-30 20:53:55 +00002335 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002336 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002337 (tline->text[1] == '%' || tline->text[1] == '$'
2338 || tline->text[1] == '!'))
2339 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002340
H. Peter Anvin4169a472007-09-12 01:29:43 +00002341 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002342
H. Peter Anvin4169a472007-09-12 01:29:43 +00002343 switch (i) {
2344 case PP_INVALID:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002345 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002346 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2347 tline->text);
2348 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002349
H. Peter Anvine2c80182005-01-15 22:15:51 +00002350 case PP_STACKSIZE:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002351 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002352 /* Directive to tell NASM what the default stack size is. The
2353 * default is for a 16-bit stack, and this can be overriden with
2354 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002355 */
2356 tline = tline->next;
2357 if (tline && tline->type == TOK_WHITESPACE)
2358 tline = tline->next;
2359 if (!tline || tline->type != TOK_ID) {
2360 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2361 free_tlist(origline);
2362 return DIRECTIVE_FOUND;
2363 }
2364 if (nasm_stricmp(tline->text, "flat") == 0) {
2365 /* All subsequent ARG directives are for a 32-bit stack */
2366 StackSize = 4;
2367 StackPointer = "ebp";
2368 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002369 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002370 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2371 /* All subsequent ARG directives are for a 64-bit stack */
2372 StackSize = 8;
2373 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002374 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002375 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002376 } else if (nasm_stricmp(tline->text, "large") == 0) {
2377 /* All subsequent ARG directives are for a 16-bit stack,
2378 * far function call.
2379 */
2380 StackSize = 2;
2381 StackPointer = "bp";
2382 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002383 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 } else if (nasm_stricmp(tline->text, "small") == 0) {
2385 /* All subsequent ARG directives are for a 16-bit stack,
2386 * far function call. We don't support near functions.
2387 */
2388 StackSize = 2;
2389 StackPointer = "bp";
2390 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002391 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002392 } else {
2393 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2394 free_tlist(origline);
2395 return DIRECTIVE_FOUND;
2396 }
2397 free_tlist(origline);
2398 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002399
H. Peter Anvine2c80182005-01-15 22:15:51 +00002400 case PP_ARG:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002401 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002402 /* TASM like ARG directive to define arguments to functions, in
2403 * the following form:
2404 *
2405 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2406 */
2407 offset = ArgOffset;
2408 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002409 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002410 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002411
H. Peter Anvine2c80182005-01-15 22:15:51 +00002412 /* Find the argument name */
2413 tline = tline->next;
2414 if (tline && tline->type == TOK_WHITESPACE)
2415 tline = tline->next;
2416 if (!tline || tline->type != TOK_ID) {
2417 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2418 free_tlist(origline);
2419 return DIRECTIVE_FOUND;
2420 }
2421 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002422
H. Peter Anvine2c80182005-01-15 22:15:51 +00002423 /* Find the argument size type */
2424 tline = tline->next;
2425 if (!tline || tline->type != TOK_OTHER
2426 || tline->text[0] != ':') {
2427 error(ERR_NONFATAL,
2428 "Syntax error processing `%%arg' directive");
2429 free_tlist(origline);
2430 return DIRECTIVE_FOUND;
2431 }
2432 tline = tline->next;
2433 if (!tline || tline->type != TOK_ID) {
2434 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2435 free_tlist(origline);
2436 return DIRECTIVE_FOUND;
2437 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002438
H. Peter Anvine2c80182005-01-15 22:15:51 +00002439 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002440 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002441 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002442 size = parse_size(tt->text);
2443 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002444 error(ERR_NONFATAL,
2445 "Invalid size type for `%%arg' missing directive");
2446 free_tlist(tt);
2447 free_tlist(origline);
2448 return DIRECTIVE_FOUND;
2449 }
2450 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002451
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002452 /* Round up to even stack slots */
2453 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002454
H. Peter Anvine2c80182005-01-15 22:15:51 +00002455 /* Now define the macro for the argument */
2456 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2457 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002458 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002459 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002460
H. Peter Anvine2c80182005-01-15 22:15:51 +00002461 /* Move to the next argument in the list */
2462 tline = tline->next;
2463 if (tline && tline->type == TOK_WHITESPACE)
2464 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002465 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002466 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002467 free_tlist(origline);
2468 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002469
H. Peter Anvine2c80182005-01-15 22:15:51 +00002470 case PP_LOCAL:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002471 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002472 /* TASM like LOCAL directive to define local variables for a
2473 * function, in the following form:
2474 *
2475 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2476 *
2477 * The '= LocalSize' at the end is ignored by NASM, but is
2478 * required by TASM to define the local parameter size (and used
2479 * by the TASM macro package).
2480 */
2481 offset = LocalOffset;
2482 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002483 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002484 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002485
H. Peter Anvine2c80182005-01-15 22:15:51 +00002486 /* Find the argument name */
2487 tline = tline->next;
2488 if (tline && tline->type == TOK_WHITESPACE)
2489 tline = tline->next;
2490 if (!tline || tline->type != TOK_ID) {
2491 error(ERR_NONFATAL,
2492 "`%%local' missing argument parameter");
2493 free_tlist(origline);
2494 return DIRECTIVE_FOUND;
2495 }
2496 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002497
H. Peter Anvine2c80182005-01-15 22:15:51 +00002498 /* Find the argument size type */
2499 tline = tline->next;
2500 if (!tline || tline->type != TOK_OTHER
2501 || tline->text[0] != ':') {
2502 error(ERR_NONFATAL,
2503 "Syntax error processing `%%local' directive");
2504 free_tlist(origline);
2505 return DIRECTIVE_FOUND;
2506 }
2507 tline = tline->next;
2508 if (!tline || tline->type != TOK_ID) {
2509 error(ERR_NONFATAL,
2510 "`%%local' missing size type parameter");
2511 free_tlist(origline);
2512 return DIRECTIVE_FOUND;
2513 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002514
H. Peter Anvine2c80182005-01-15 22:15:51 +00002515 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002516 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002517 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002518 size = parse_size(tt->text);
2519 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002520 error(ERR_NONFATAL,
2521 "Invalid size type for `%%local' missing directive");
2522 free_tlist(tt);
2523 free_tlist(origline);
2524 return DIRECTIVE_FOUND;
2525 }
2526 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002527
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002528 /* Round up to even stack slots */
2529 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002530
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002531 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002532
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002533 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002534 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2535 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002536 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002537
H. Peter Anvine2c80182005-01-15 22:15:51 +00002538 /* Now define the assign to setup the enter_c macro correctly */
2539 snprintf(directive, sizeof(directive),
2540 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002541 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002542
H. Peter Anvine2c80182005-01-15 22:15:51 +00002543 /* Move to the next argument in the list */
2544 tline = tline->next;
2545 if (tline && tline->type == TOK_WHITESPACE)
2546 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002547 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002548 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002549 free_tlist(origline);
2550 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002551
H. Peter Anvine2c80182005-01-15 22:15:51 +00002552 case PP_CLEAR:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002553 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002554 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002555 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002556 "trailing garbage after `%%clear' ignored");
2557 free_macros();
2558 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002559 free_tlist(origline);
2560 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002561
H. Peter Anvin418ca702008-05-30 10:42:30 -07002562 case PP_DEPEND:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002563 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002564 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002565 skip_white_(t);
2566 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002567 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002568 error(ERR_NONFATAL, "`%%depend' expects a file name");
2569 free_tlist(origline);
2570 return DIRECTIVE_FOUND; /* but we did _something_ */
2571 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002572 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002573 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002574 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002575 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002576 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002577 nasm_unquote_cstr(p, i);
2578 if (dephead && !in_list(*dephead, p)) {
2579 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2580 sl->next = NULL;
2581 strcpy(sl->str, p);
2582 *deptail = sl;
2583 deptail = &sl->next;
2584 }
2585 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002586 return DIRECTIVE_FOUND;
2587
2588 case PP_INCLUDE:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002589 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002590 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002591 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002592
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002593 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002594 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002595 error(ERR_NONFATAL, "`%%include' expects a file name");
2596 free_tlist(origline);
2597 return DIRECTIVE_FOUND; /* but we did _something_ */
2598 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002599 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002600 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002601 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002602 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002603 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002604 nasm_unquote_cstr(p, i);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002605 inc = nasm_malloc(sizeof(Include));
2606 inc->next = istk;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002607 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002608 if (!inc->fp) {
2609 /* -MG given but file not found */
2610 nasm_free(inc);
2611 } else {
2612 inc->fname = src_set_fname(nasm_strdup(p));
2613 inc->lineno = src_set_linnum(0);
2614 inc->lineinc = 1;
2615 inc->expansion = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002616 istk = inc;
2617 list->uplevel(LIST_INCLUDE);
2618 }
2619 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002620 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002621
H. Peter Anvind2456592008-06-19 15:04:18 -07002622 case PP_USE:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002623 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002624 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002625 static macros_t *use_pkg;
2626 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002627
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002628 tline = tline->next;
2629 skip_white_(tline);
2630 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002631
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002632 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002633 tline->type != TOK_INTERNAL_STRING &&
2634 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002635 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002636 free_tlist(origline);
2637 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002638 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002639 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002640 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002641 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002642 if (tline->type == TOK_STRING)
2643 nasm_unquote_cstr(tline->text, i);
2644 use_pkg = nasm_stdmac_find_package(tline->text);
2645 if (!use_pkg)
2646 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2647 else
2648 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002649 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002650 /* Not already included, go ahead and include it */
2651 stdmacpos = use_pkg;
2652 }
2653 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002654 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002655 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002656 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002657 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002658 case PP_POP:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002659 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002660 tline = tline->next;
2661 skip_white_(tline);
2662 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002663 if (tline) {
2664 if (!tok_type_(tline, TOK_ID)) {
2665 error(ERR_NONFATAL, "`%s' expects a context identifier",
2666 pp_directives[i]);
2667 free_tlist(origline);
2668 return DIRECTIVE_FOUND; /* but we did _something_ */
2669 }
2670 if (tline->next)
2671 error(ERR_WARNING|ERR_PASS1,
2672 "trailing garbage after `%s' ignored",
2673 pp_directives[i]);
2674 p = nasm_strdup(tline->text);
2675 } else {
2676 p = NULL; /* Anonymous */
2677 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002678
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002679 if (i == PP_PUSH) {
2680 ctx = nasm_malloc(sizeof(Context));
2681 ctx->next = cstk;
2682 hash_init(&ctx->localmac, HASH_SMALL);
2683 ctx->name = p;
2684 ctx->number = unique++;
2685 cstk = ctx;
2686 } else {
2687 /* %pop or %repl */
2688 if (!cstk) {
2689 error(ERR_NONFATAL, "`%s': context stack is empty",
2690 pp_directives[i]);
2691 } else if (i == PP_POP) {
2692 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2693 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2694 "expected %s",
2695 cstk->name ? cstk->name : "anonymous", p);
2696 else
2697 ctx_pop();
2698 } else {
2699 /* i == PP_REPL */
2700 nasm_free(cstk->name);
2701 cstk->name = p;
2702 p = NULL;
2703 }
2704 nasm_free(p);
2705 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002706 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002707 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002708 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002709 severity = ERR_FATAL;
2710 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002711 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002712 severity = ERR_NONFATAL;
2713 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002714 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002715 severity = ERR_WARNING|ERR_WARN_USER;
2716 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002717
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002718issue_error:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002719 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002720 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002721 /* Only error out if this is the final pass */
2722 if (pass != 2 && i != PP_FATAL)
2723 return DIRECTIVE_FOUND;
2724
2725 tline->next = expand_smacro(tline->next);
2726 tline = tline->next;
2727 skip_white_(tline);
2728 t = tline ? tline->next : NULL;
2729 skip_white_(t);
2730 if (tok_type_(tline, TOK_STRING) && !t) {
2731 /* The line contains only a quoted string */
2732 p = tline->text;
2733 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2734 error(severity, "%s", p);
2735 } else {
2736 /* Not a quoted string, or more than a quoted string */
2737 p = detoken(tline, false);
2738 error(severity, "%s", p);
2739 nasm_free(p);
2740 }
2741 free_tlist(origline);
2742 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002743 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002744
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002745 CASE_PP_IF:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002746 if (defining != NULL) {
2747 if (defining->type == EXP_IF) {
2748 defining->def_depth ++;
2749 }
2750 return NO_DIRECTIVE_FOUND;
2751 }
2752 if ((istk->expansion != NULL) &&
2753 (istk->expansion->emitting == false)) {
2754 j = COND_NEVER;
2755 } else {
2756 j = if_condition(tline->next, i);
2757 tline->next = NULL; /* it got freed */
2758 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
2759 }
2760 ed = new_ExpDef(EXP_IF);
2761 ed->state = j;
2762 ed->nolist = NULL;
2763 ed->def_depth = 0;
2764 ed->cur_depth = 0;
2765 ed->max_depth = 0;
2766 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
2767 ed->prev = defining;
2768 defining = ed;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002769 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002770 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002771
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002772 CASE_PP_ELIF:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002773 if (defining != NULL) {
2774 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2775 return NO_DIRECTIVE_FOUND;
2776 }
2777 }
2778 if ((defining == NULL) || (defining->type != EXP_IF)) {
2779 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2780 }
2781 switch (defining->state) {
2782 case COND_IF_TRUE:
2783 defining->state = COND_DONE;
2784 defining->ignoring = true;
2785 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002786
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002787 case COND_DONE:
2788 case COND_NEVER:
2789 defining->ignoring = true;
2790 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002791
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002792 case COND_ELSE_TRUE:
2793 case COND_ELSE_FALSE:
2794 error_precond(ERR_WARNING|ERR_PASS1,
2795 "`%%elif' after `%%else' ignored");
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002796 defining->state = COND_NEVER;
2797 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002798 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002799
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002800 case COND_IF_FALSE:
2801 /*
2802 * IMPORTANT: In the case of %if, we will already have
2803 * called expand_mmac_params(); however, if we're
2804 * processing an %elif we must have been in a
2805 * non-emitting mode, which would have inhibited
2806 * the normal invocation of expand_mmac_params().
2807 * Therefore, we have to do it explicitly here.
2808 */
2809 j = if_condition(expand_mmac_params(tline->next), i);
2810 tline->next = NULL; /* it got freed */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002811 defining->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002812 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002813 defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002814 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002815 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002816 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002817 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002818
H. Peter Anvine2c80182005-01-15 22:15:51 +00002819 case PP_ELSE:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002820 if (defining != NULL) {
2821 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2822 return NO_DIRECTIVE_FOUND;
2823 }
2824 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002825 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002826 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002827 "trailing garbage after `%%else' ignored");
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002828 if ((defining == NULL) || (defining->type != EXP_IF)) {
2829 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2830 }
2831 switch (defining->state) {
2832 case COND_IF_TRUE:
2833 case COND_DONE:
2834 defining->state = COND_ELSE_FALSE;
2835 defining->ignoring = true;
2836 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002837
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002838 case COND_NEVER:
2839 defining->ignoring = true;
2840 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002841
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002842 case COND_IF_FALSE:
2843 defining->state = COND_ELSE_TRUE;
2844 defining->ignoring = false;
2845 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002846
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002847 case COND_ELSE_TRUE:
2848 case COND_ELSE_FALSE:
2849 error_precond(ERR_WARNING|ERR_PASS1,
2850 "`%%else' after `%%else' ignored.");
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002851 defining->state = COND_NEVER;
2852 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002853 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002854 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002855 free_tlist(origline);
2856 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002857
H. Peter Anvine2c80182005-01-15 22:15:51 +00002858 case PP_ENDIF:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002859 if (defining != NULL) {
2860 if (defining->type == EXP_IF) {
2861 if (defining->def_depth > 0) {
2862 defining->def_depth --;
2863 return NO_DIRECTIVE_FOUND;
2864 }
2865 } else {
2866 return NO_DIRECTIVE_FOUND;
2867 }
2868 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002869 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002870 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002871 "trailing garbage after `%%endif' ignored");
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002872 if ((defining == NULL) || (defining->type != EXP_IF)) {
2873 error(ERR_NONFATAL, "`%%endif': no matching `%%if'");
2874 return DIRECTIVE_FOUND;
2875 }
2876 ed = defining;
2877 defining = ed->prev;
2878 ed->prev = expansions;
2879 expansions = ed;
2880 ei = new_ExpInv(EXP_IF, ed);
2881 ei->current = ed->line;
2882 ei->emitting = true;
2883 ei->prev = istk->expansion;
2884 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002885 free_tlist(origline);
2886 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002887
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002888 case PP_RMACRO:
2889 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002890 case PP_MACRO:
2891 case PP_IMACRO:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002892 if (defining != NULL) {
2893 if (defining->type == EXP_MMACRO) {
2894 defining->def_depth ++;
2895 }
2896 return NO_DIRECTIVE_FOUND;
2897 }
2898 ed = new_ExpDef(EXP_MMACRO);
2899 ed->max_depth =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002900 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002901 ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2902 if (!parse_mmacro_spec(tline, ed, pp_directives[i])) {
2903 nasm_free(ed);
2904 ed = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002905 return DIRECTIVE_FOUND;
2906 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002907 ed->def_depth = 0;
2908 ed->cur_depth = 0;
2909 ed->max_depth = (ed->max_depth + 1);
2910 ed->ignoring = false;
2911 ed->prev = defining;
2912 defining = ed;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002913
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002914 eed = (ExpDef *) hash_findix(&expdefs, ed->name);
2915 while (eed) {
2916 if (!strcmp(eed->name, ed->name) &&
2917 (eed->nparam_min <= ed->nparam_max
2918 || ed->plus)
2919 && (ed->nparam_min <= eed->nparam_max
2920 || eed->plus)) {
2921 error(ERR_WARNING|ERR_PASS1,
2922 "redefining multi-line macro `%s'", ed->name);
2923 return DIRECTIVE_FOUND;
2924 }
2925 eed = eed->next;
2926 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002927 free_tlist(origline);
2928 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002929
H. Peter Anvine2c80182005-01-15 22:15:51 +00002930 case PP_ENDM:
2931 case PP_ENDMACRO:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002932 if (defining != NULL) {
2933 if (defining->type == EXP_MMACRO) {
2934 if (defining->def_depth > 0) {
2935 defining->def_depth --;
2936 return NO_DIRECTIVE_FOUND;
2937 }
2938 } else {
2939 return NO_DIRECTIVE_FOUND;
2940 }
2941 }
2942 if (!(defining) || (defining->type != EXP_MMACRO)) {
2943 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2944 return DIRECTIVE_FOUND;
2945 }
2946 edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name);
2947 defining->next = *edhead;
2948 *edhead = defining;
2949 ed = defining;
2950 defining = ed->prev;
2951 ed->prev = expansions;
2952 expansions = ed;
2953 ed = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002954 free_tlist(origline);
2955 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002956
H. Peter Anvin89cee572009-07-15 09:16:54 -04002957 case PP_EXITMACRO:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002958 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2959 /*
2960 * We must search along istk->expansion until we hit a
2961 * macro invocation. Then we disable the emitting state(s)
2962 * between exitmacro and endmacro.
2963 */
2964 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
2965 if(ei->type == EXP_MMACRO) {
2966 break;
2967 }
2968 }
2969
2970 if (ei != NULL) {
2971 /*
2972 * Set all invocations leading back to the macro
2973 * invocation to a non-emitting state.
2974 */
2975 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
2976 eei->emitting = false;
2977 }
2978 eei->emitting = false;
2979 } else {
2980 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
2981 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002982 free_tlist(origline);
2983 return DIRECTIVE_FOUND;
2984
H. Peter Anvina26433d2008-07-16 14:40:01 -07002985 case PP_UNMACRO:
2986 case PP_UNIMACRO:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002987 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002988 {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002989 ExpDef **ed_p;
2990 ExpDef spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002991
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002992 spec.casesense = (i == PP_UNMACRO);
2993 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2994 return DIRECTIVE_FOUND;
2995 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002996 ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL);
2997 while (ed_p && *ed_p) {
2998 ed = *ed_p;
2999 if (ed->casesense == spec.casesense &&
3000 !mstrcmp(ed->name, spec.name, spec.casesense) &&
3001 ed->nparam_min == spec.nparam_min &&
3002 ed->nparam_max == spec.nparam_max &&
3003 ed->plus == spec.plus) {
3004 *ed_p = ed->next;
3005 free_expdef(ed);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003006 } else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003007 ed_p = &ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003008 }
3009 }
3010 free_tlist(origline);
3011 free_tlist(spec.dlist);
3012 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003013 }
3014
H. Peter Anvine2c80182005-01-15 22:15:51 +00003015 case PP_ROTATE:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003016 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003017 if (tline->next && tline->next->type == TOK_WHITESPACE)
3018 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003019 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003020 free_tlist(origline);
3021 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
3022 return DIRECTIVE_FOUND;
3023 }
3024 t = expand_smacro(tline->next);
3025 tline->next = NULL;
3026 free_tlist(origline);
3027 tline = t;
3028 tptr = &t;
3029 tokval.t_type = TOKEN_INVALID;
3030 evalresult =
3031 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3032 free_tlist(tline);
3033 if (!evalresult)
3034 return DIRECTIVE_FOUND;
3035 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003036 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003037 "trailing garbage after expression ignored");
3038 if (!is_simple(evalresult)) {
3039 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
3040 return DIRECTIVE_FOUND;
3041 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003042 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3043 if (ei->type == EXP_MMACRO) {
3044 break;
3045 }
3046 }
3047 if (ei == NULL) {
3048 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
3049 } else if (ei->nparam == 0) {
3050 error(ERR_NONFATAL,
3051 "`%%rotate' invoked within macro without parameters");
3052 } else {
3053 int rotate = ei->rotate + reloc_value(evalresult);
3054
3055 rotate %= (int)ei->nparam;
3056 if (rotate < 0)
3057 rotate += ei->nparam;
3058 ei->rotate = rotate;
3059 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003060 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003061
H. Peter Anvine2c80182005-01-15 22:15:51 +00003062 case PP_REP:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003063 if (defining != NULL) {
3064 if (defining->type == EXP_REP) {
3065 defining->def_depth ++;
3066 }
3067 return NO_DIRECTIVE_FOUND;
3068 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003069 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003070 do {
3071 tline = tline->next;
3072 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003073
H. Peter Anvine2c80182005-01-15 22:15:51 +00003074 if (tok_type_(tline, TOK_ID) &&
3075 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003076 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003077 do {
3078 tline = tline->next;
3079 } while (tok_type_(tline, TOK_WHITESPACE));
3080 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003081
H. Peter Anvine2c80182005-01-15 22:15:51 +00003082 if (tline) {
3083 t = expand_smacro(tline);
3084 tptr = &t;
3085 tokval.t_type = TOKEN_INVALID;
3086 evalresult =
3087 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3088 if (!evalresult) {
3089 free_tlist(origline);
3090 return DIRECTIVE_FOUND;
3091 }
3092 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003093 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003094 "trailing garbage after expression ignored");
3095 if (!is_simple(evalresult)) {
3096 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
3097 return DIRECTIVE_FOUND;
3098 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003099 count = reloc_value(evalresult);
3100 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04003101 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003102 count = 0;
3103 } else
3104 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003105 } else {
3106 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003107 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003108 }
3109 free_tlist(origline);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003110 ed = new_ExpDef(EXP_REP);
3111 ed->nolist = nolist;
3112 ed->def_depth = 0;
3113 ed->cur_depth = 1;
3114 ed->max_depth = (count - 1);
3115 ed->ignoring = false;
3116 ed->prev = defining;
3117 defining = ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003118 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003119
H. Peter Anvine2c80182005-01-15 22:15:51 +00003120 case PP_ENDREP:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003121 if (defining != NULL) {
3122 if (defining->type == EXP_REP) {
3123 if (defining->def_depth > 0) {
3124 defining->def_depth --;
3125 return NO_DIRECTIVE_FOUND;
3126 }
3127 } else {
3128 return NO_DIRECTIVE_FOUND;
3129 }
3130 }
3131 if ((defining == NULL) || (defining->type != EXP_REP)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003132 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
3133 return DIRECTIVE_FOUND;
3134 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003135
H. Peter Anvine2c80182005-01-15 22:15:51 +00003136 /*
3137 * Now we have a "macro" defined - although it has no name
3138 * and we won't be entering it in the hash tables - we must
3139 * push a macro-end marker for it on to istk->expansion.
3140 * After that, it will take care of propagating itself (a
3141 * macro-end marker line for a macro which is really a %rep
3142 * block will cause the macro to be re-expanded, complete
3143 * with another macro-end marker to ensure the process
3144 * continues) until the whole expansion is forcibly removed
3145 * from istk->expansion by a %exitrep.
3146 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003147 ed = defining;
3148 defining = ed->prev;
3149 ed->prev = expansions;
3150 expansions = ed;
3151 ei = new_ExpInv(EXP_REP, ed);
3152 ei->current = ed->line;
3153 ei->emitting = ((ed->max_depth > 0) ? true : false);
3154 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3155 ei->prev = istk->expansion;
3156 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003157 free_tlist(origline);
3158 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003159
H. Peter Anvine2c80182005-01-15 22:15:51 +00003160 case PP_EXITREP:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003161 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3162 /*
3163 * We must search along istk->expansion until we hit a
3164 * rep invocation. Then we disable the emitting state(s)
3165 * between exitrep and endrep.
3166 */
3167 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3168 if (ei->type == EXP_REP) {
3169 break;
3170 }
3171 }
3172
3173 if (ei != NULL) {
3174 /*
3175 * Set all invocations leading back to the rep
3176 * invocation to a non-emitting state.
3177 */
3178 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3179 eei->emitting = false;
3180 }
3181 eei->emitting = false;
3182 eei->current = NULL;
3183 eei->def->cur_depth = eei->def->max_depth;
3184 } else {
3185 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3186 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003187 free_tlist(origline);
3188 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003189
H. Peter Anvine2c80182005-01-15 22:15:51 +00003190 case PP_XDEFINE:
3191 case PP_IXDEFINE:
3192 case PP_DEFINE:
3193 case PP_IDEFINE:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003194 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003195 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003196
H. Peter Anvine2c80182005-01-15 22:15:51 +00003197 tline = tline->next;
3198 skip_white_(tline);
3199 tline = expand_id(tline);
3200 if (!tline || (tline->type != TOK_ID &&
3201 (tline->type != TOK_PREPROC_ID ||
3202 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003203 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003204 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003205 free_tlist(origline);
3206 return DIRECTIVE_FOUND;
3207 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003208
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003209 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003210 last = tline;
3211 param_start = tline = tline->next;
3212 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003213
H. Peter Anvine2c80182005-01-15 22:15:51 +00003214 /* Expand the macro definition now for %xdefine and %ixdefine */
3215 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3216 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003217
H. Peter Anvine2c80182005-01-15 22:15:51 +00003218 if (tok_is_(tline, "(")) {
3219 /*
3220 * This macro has parameters.
3221 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003222
H. Peter Anvine2c80182005-01-15 22:15:51 +00003223 tline = tline->next;
3224 while (1) {
3225 skip_white_(tline);
3226 if (!tline) {
3227 error(ERR_NONFATAL, "parameter identifier expected");
3228 free_tlist(origline);
3229 return DIRECTIVE_FOUND;
3230 }
3231 if (tline->type != TOK_ID) {
3232 error(ERR_NONFATAL,
3233 "`%s': parameter identifier expected",
3234 tline->text);
3235 free_tlist(origline);
3236 return DIRECTIVE_FOUND;
3237 }
3238 tline->type = TOK_SMAC_PARAM + nparam++;
3239 tline = tline->next;
3240 skip_white_(tline);
3241 if (tok_is_(tline, ",")) {
3242 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003243 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003244 if (!tok_is_(tline, ")")) {
3245 error(ERR_NONFATAL,
3246 "`)' expected to terminate macro template");
3247 free_tlist(origline);
3248 return DIRECTIVE_FOUND;
3249 }
3250 break;
3251 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003252 }
3253 last = tline;
3254 tline = tline->next;
3255 }
3256 if (tok_type_(tline, TOK_WHITESPACE))
3257 last = tline, tline = tline->next;
3258 macro_start = NULL;
3259 last->next = NULL;
3260 t = tline;
3261 while (t) {
3262 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003263 list_for_each(tt, param_start)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003264 if (tt->type >= TOK_SMAC_PARAM &&
3265 !strcmp(tt->text, t->text))
3266 t->type = tt->type;
3267 }
3268 tt = t->next;
3269 t->next = macro_start;
3270 macro_start = t;
3271 t = tt;
3272 }
3273 /*
3274 * Good. We now have a macro name, a parameter count, and a
3275 * token list (in reverse order) for an expansion. We ought
3276 * to be OK just to create an SMacro, store it, and let
3277 * free_tlist have the rest of the line (which we have
3278 * carefully re-terminated after chopping off the expansion
3279 * from the end).
3280 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003281 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003282 free_tlist(origline);
3283 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003284
H. Peter Anvine2c80182005-01-15 22:15:51 +00003285 case PP_UNDEF:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003286 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003287 tline = tline->next;
3288 skip_white_(tline);
3289 tline = expand_id(tline);
3290 if (!tline || (tline->type != TOK_ID &&
3291 (tline->type != TOK_PREPROC_ID ||
3292 tline->text[1] != '$'))) {
3293 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3294 free_tlist(origline);
3295 return DIRECTIVE_FOUND;
3296 }
3297 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003298 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003299 "trailing garbage after macro name ignored");
3300 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003301
H. Peter Anvine2c80182005-01-15 22:15:51 +00003302 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003303 ctx = get_ctx(tline->text, &mname, false);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003304 undef_smacro(ctx, mname);
3305 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003306 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003307
H. Peter Anvin9e200162008-06-04 17:23:14 -07003308 case PP_DEFSTR:
3309 case PP_IDEFSTR:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003310 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003311 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003312
3313 tline = tline->next;
3314 skip_white_(tline);
3315 tline = expand_id(tline);
3316 if (!tline || (tline->type != TOK_ID &&
3317 (tline->type != TOK_PREPROC_ID ||
3318 tline->text[1] != '$'))) {
3319 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003320 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003321 free_tlist(origline);
3322 return DIRECTIVE_FOUND;
3323 }
3324
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003325 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003326 last = tline;
3327 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003328 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003329
3330 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003331 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003332
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003333 p = detoken(tline, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003334 macro_start = nasm_malloc(sizeof(*macro_start));
3335 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003336 macro_start->text = nasm_quote(p, strlen(p));
3337 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003338 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003339 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003340
3341 /*
3342 * We now have a macro name, an implicit parameter count of
3343 * zero, and a string token to use as an expansion. Create
3344 * and store an SMacro.
3345 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003346 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003347 free_tlist(origline);
3348 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003349
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003350 case PP_DEFTOK:
3351 case PP_IDEFTOK:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003352 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003353 casesense = (i == PP_DEFTOK);
3354
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003355 tline = tline->next;
3356 skip_white_(tline);
3357 tline = expand_id(tline);
3358 if (!tline || (tline->type != TOK_ID &&
3359 (tline->type != TOK_PREPROC_ID ||
3360 tline->text[1] != '$'))) {
3361 error(ERR_NONFATAL,
3362 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003363 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003364 free_tlist(origline);
3365 return DIRECTIVE_FOUND;
3366 }
3367 ctx = get_ctx(tline->text, &mname, false);
3368 last = tline;
3369 tline = expand_smacro(tline->next);
3370 last->next = NULL;
3371
3372 t = tline;
3373 while (tok_type_(t, TOK_WHITESPACE))
3374 t = t->next;
3375 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003376 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003377 error(ERR_NONFATAL,
3378 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003379 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003380 free_tlist(tline);
3381 free_tlist(origline);
3382 return DIRECTIVE_FOUND;
3383 }
3384
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003385 /*
3386 * Convert the string to a token stream. Note that smacros
3387 * are stored with the token stream reversed, so we have to
3388 * reverse the output of tokenize().
3389 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003390 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003391 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003392
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003393 /*
3394 * We now have a macro name, an implicit parameter count of
3395 * zero, and a numeric token to use as an expansion. Create
3396 * and store an SMacro.
3397 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003398 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003399 free_tlist(tline);
3400 free_tlist(origline);
3401 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003402
H. Peter Anvin418ca702008-05-30 10:42:30 -07003403 case PP_PATHSEARCH:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003404 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003405 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003406 FILE *fp;
3407 StrList *xsl = NULL;
3408 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003409
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003410 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003411
3412 tline = tline->next;
3413 skip_white_(tline);
3414 tline = expand_id(tline);
3415 if (!tline || (tline->type != TOK_ID &&
3416 (tline->type != TOK_PREPROC_ID ||
3417 tline->text[1] != '$'))) {
3418 error(ERR_NONFATAL,
3419 "`%%pathsearch' expects a macro identifier as first parameter");
3420 free_tlist(origline);
3421 return DIRECTIVE_FOUND;
3422 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003423 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003424 last = tline;
3425 tline = expand_smacro(tline->next);
3426 last->next = NULL;
3427
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003428 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003429 while (tok_type_(t, TOK_WHITESPACE))
3430 t = t->next;
3431
3432 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003433 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003434 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003435 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003436 free_tlist(origline);
3437 return DIRECTIVE_FOUND; /* but we did _something_ */
3438 }
3439 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003440 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003441 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003442 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003443 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003444 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003445
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003446 fp = inc_fopen(p, &xsl, &xst, true);
3447 if (fp) {
3448 p = xsl->str;
3449 fclose(fp); /* Don't actually care about the file */
3450 }
H. Peter Anvin418ca702008-05-30 10:42:30 -07003451 macro_start = nasm_malloc(sizeof(*macro_start));
3452 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003453 macro_start->text = nasm_quote(p, strlen(p));
3454 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003455 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003456 if (xsl)
3457 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003458
3459 /*
3460 * We now have a macro name, an implicit parameter count of
3461 * zero, and a string token to use as an expansion. Create
3462 * and store an SMacro.
3463 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003464 define_smacro(ctx, mname, casesense, 0, macro_start);
3465 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003466 free_tlist(origline);
3467 return DIRECTIVE_FOUND;
3468 }
3469
H. Peter Anvine2c80182005-01-15 22:15:51 +00003470 case PP_STRLEN:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003471 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003472 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003473
H. Peter Anvine2c80182005-01-15 22:15:51 +00003474 tline = tline->next;
3475 skip_white_(tline);
3476 tline = expand_id(tline);
3477 if (!tline || (tline->type != TOK_ID &&
3478 (tline->type != TOK_PREPROC_ID ||
3479 tline->text[1] != '$'))) {
3480 error(ERR_NONFATAL,
3481 "`%%strlen' expects a macro identifier as first parameter");
3482 free_tlist(origline);
3483 return DIRECTIVE_FOUND;
3484 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003485 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003486 last = tline;
3487 tline = expand_smacro(tline->next);
3488 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003489
H. Peter Anvine2c80182005-01-15 22:15:51 +00003490 t = tline;
3491 while (tok_type_(t, TOK_WHITESPACE))
3492 t = t->next;
3493 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003494 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003495 error(ERR_NONFATAL,
3496 "`%%strlen` requires string as second parameter");
3497 free_tlist(tline);
3498 free_tlist(origline);
3499 return DIRECTIVE_FOUND;
3500 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003501
H. Peter Anvine2c80182005-01-15 22:15:51 +00003502 macro_start = nasm_malloc(sizeof(*macro_start));
3503 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003504 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003505 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003506
H. Peter Anvine2c80182005-01-15 22:15:51 +00003507 /*
3508 * We now have a macro name, an implicit parameter count of
3509 * zero, and a numeric token to use as an expansion. Create
3510 * and store an SMacro.
3511 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003512 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003513 free_tlist(tline);
3514 free_tlist(origline);
3515 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003516
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003517 case PP_STRCAT:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003518 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003519 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003520
3521 tline = tline->next;
3522 skip_white_(tline);
3523 tline = expand_id(tline);
3524 if (!tline || (tline->type != TOK_ID &&
3525 (tline->type != TOK_PREPROC_ID ||
3526 tline->text[1] != '$'))) {
3527 error(ERR_NONFATAL,
3528 "`%%strcat' expects a macro identifier as first parameter");
3529 free_tlist(origline);
3530 return DIRECTIVE_FOUND;
3531 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003532 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003533 last = tline;
3534 tline = expand_smacro(tline->next);
3535 last->next = NULL;
3536
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003537 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003538 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003539 switch (t->type) {
3540 case TOK_WHITESPACE:
3541 break;
3542 case TOK_STRING:
3543 len += t->a.len = nasm_unquote(t->text, NULL);
3544 break;
3545 case TOK_OTHER:
3546 if (!strcmp(t->text, ",")) /* permit comma separators */
3547 break;
3548 /* else fall through */
3549 default:
3550 error(ERR_NONFATAL,
3551 "non-string passed to `%%strcat' (%d)", t->type);
3552 free_tlist(tline);
3553 free_tlist(origline);
3554 return DIRECTIVE_FOUND;
3555 }
3556 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003557
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003558 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003559 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003560 if (t->type == TOK_STRING) {
3561 memcpy(p, t->text, t->a.len);
3562 p += t->a.len;
3563 }
3564 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003565
3566 /*
3567 * We now have a macro name, an implicit parameter count of
3568 * zero, and a numeric token to use as an expansion. Create
3569 * and store an SMacro.
3570 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003571 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3572 macro_start->text = nasm_quote(pp, len);
3573 nasm_free(pp);
3574 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003575 free_tlist(tline);
3576 free_tlist(origline);
3577 return DIRECTIVE_FOUND;
3578
H. Peter Anvine2c80182005-01-15 22:15:51 +00003579 case PP_SUBSTR:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003580 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003581 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003582 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003583 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003584
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003585 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003586
H. Peter Anvine2c80182005-01-15 22:15:51 +00003587 tline = tline->next;
3588 skip_white_(tline);
3589 tline = expand_id(tline);
3590 if (!tline || (tline->type != TOK_ID &&
3591 (tline->type != TOK_PREPROC_ID ||
3592 tline->text[1] != '$'))) {
3593 error(ERR_NONFATAL,
3594 "`%%substr' expects a macro identifier as first parameter");
3595 free_tlist(origline);
3596 return DIRECTIVE_FOUND;
3597 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003598 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003599 last = tline;
3600 tline = expand_smacro(tline->next);
3601 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003602
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003603 if (tline) /* skip expanded id */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003604 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003605 while (tok_type_(t, TOK_WHITESPACE))
3606 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003607
H. Peter Anvine2c80182005-01-15 22:15:51 +00003608 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003609 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003610 error(ERR_NONFATAL,
3611 "`%%substr` requires string as second parameter");
3612 free_tlist(tline);
3613 free_tlist(origline);
3614 return DIRECTIVE_FOUND;
3615 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003616
H. Peter Anvine2c80182005-01-15 22:15:51 +00003617 tt = t->next;
3618 tptr = &tt;
3619 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003620 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003621 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003622 if (!evalresult) {
3623 free_tlist(tline);
3624 free_tlist(origline);
3625 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003626 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003627 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3628 free_tlist(tline);
3629 free_tlist(origline);
3630 return DIRECTIVE_FOUND;
3631 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003632 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003633
3634 while (tok_type_(tt, TOK_WHITESPACE))
3635 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003636 if (!tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003637 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003638 } else {
3639 tokval.t_type = TOKEN_INVALID;
3640 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3641 pass, error, NULL);
3642 if (!evalresult) {
3643 free_tlist(tline);
3644 free_tlist(origline);
3645 return DIRECTIVE_FOUND;
3646 } else if (!is_simple(evalresult)) {
3647 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3648 free_tlist(tline);
3649 free_tlist(origline);
3650 return DIRECTIVE_FOUND;
3651 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003652 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003653 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003654
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003655 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003656 /* make start and count being in range */
3657 if (start < 0)
3658 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003659 if (count < 0)
3660 count = len + count + 1 - start;
3661 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003662 count = len - start;
3663 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003664 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003665
H. Peter Anvine2c80182005-01-15 22:15:51 +00003666 macro_start = nasm_malloc(sizeof(*macro_start));
3667 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003668 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003669 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003670 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003671
H. Peter Anvine2c80182005-01-15 22:15:51 +00003672 /*
3673 * We now have a macro name, an implicit parameter count of
3674 * zero, and a numeric token to use as an expansion. Create
3675 * and store an SMacro.
3676 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003677 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003678 free_tlist(tline);
3679 free_tlist(origline);
3680 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003681 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003682
H. Peter Anvine2c80182005-01-15 22:15:51 +00003683 case PP_ASSIGN:
3684 case PP_IASSIGN:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003685 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003686 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003687
H. Peter Anvine2c80182005-01-15 22:15:51 +00003688 tline = tline->next;
3689 skip_white_(tline);
3690 tline = expand_id(tline);
3691 if (!tline || (tline->type != TOK_ID &&
3692 (tline->type != TOK_PREPROC_ID ||
3693 tline->text[1] != '$'))) {
3694 error(ERR_NONFATAL,
3695 "`%%%sassign' expects a macro identifier",
3696 (i == PP_IASSIGN ? "i" : ""));
3697 free_tlist(origline);
3698 return DIRECTIVE_FOUND;
3699 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003700 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003701 last = tline;
3702 tline = expand_smacro(tline->next);
3703 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003704
H. Peter Anvine2c80182005-01-15 22:15:51 +00003705 t = tline;
3706 tptr = &t;
3707 tokval.t_type = TOKEN_INVALID;
3708 evalresult =
3709 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3710 free_tlist(tline);
3711 if (!evalresult) {
3712 free_tlist(origline);
3713 return DIRECTIVE_FOUND;
3714 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003715
H. Peter Anvine2c80182005-01-15 22:15:51 +00003716 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003717 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003718 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003719
H. Peter Anvine2c80182005-01-15 22:15:51 +00003720 if (!is_simple(evalresult)) {
3721 error(ERR_NONFATAL,
3722 "non-constant value given to `%%%sassign'",
3723 (i == PP_IASSIGN ? "i" : ""));
3724 free_tlist(origline);
3725 return DIRECTIVE_FOUND;
3726 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003727
H. Peter Anvine2c80182005-01-15 22:15:51 +00003728 macro_start = nasm_malloc(sizeof(*macro_start));
3729 macro_start->next = NULL;
3730 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003731 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003732
H. Peter Anvine2c80182005-01-15 22:15:51 +00003733 /*
3734 * We now have a macro name, an implicit parameter count of
3735 * zero, and a numeric token to use as an expansion. Create
3736 * and store an SMacro.
3737 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003738 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003739 free_tlist(origline);
3740 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003741
H. Peter Anvine2c80182005-01-15 22:15:51 +00003742 case PP_LINE:
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003743 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003744 /*
3745 * Syntax is `%line nnn[+mmm] [filename]'
3746 */
3747 tline = tline->next;
3748 skip_white_(tline);
3749 if (!tok_type_(tline, TOK_NUMBER)) {
3750 error(ERR_NONFATAL, "`%%line' expects line number");
3751 free_tlist(origline);
3752 return DIRECTIVE_FOUND;
3753 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003754 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003755 m = 1;
3756 tline = tline->next;
3757 if (tok_is_(tline, "+")) {
3758 tline = tline->next;
3759 if (!tok_type_(tline, TOK_NUMBER)) {
3760 error(ERR_NONFATAL, "`%%line' expects line increment");
3761 free_tlist(origline);
3762 return DIRECTIVE_FOUND;
3763 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003764 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003765 tline = tline->next;
3766 }
3767 skip_white_(tline);
3768 src_set_linnum(k);
3769 istk->lineinc = m;
3770 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003771 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003772 }
3773 free_tlist(origline);
3774 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003775
3776 case PP_WHILE:
3777 if (defining != NULL) {
3778 if (defining->type == EXP_WHILE) {
3779 defining->def_depth ++;
3780 }
3781 return NO_DIRECTIVE_FOUND;
3782 }
3783 l = NULL;
3784 if ((istk->expansion != NULL) &&
3785 (istk->expansion->emitting == false)) {
3786 j = COND_NEVER;
3787 } else {
3788 l = new_Line();
3789 l->first = copy_Token(tline->next);
3790 j = if_condition(tline->next, i);
3791 tline->next = NULL; /* it got freed */
3792 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
3793 }
3794 ed = new_ExpDef(EXP_WHILE);
3795 ed->state = j;
3796 ed->cur_depth = 1;
3797 ed->max_depth = DEADMAN_LIMIT;
3798 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
3799 if (ed->ignoring == false) {
3800 ed->line = l;
3801 ed->last = l;
3802 } else if (l != NULL) {
3803 delete_Token(l->first);
3804 nasm_free(l);
3805 l = NULL;
3806 }
3807 ed->prev = defining;
3808 defining = ed;
3809 free_tlist(origline);
3810 return DIRECTIVE_FOUND;
3811
3812 case PP_ENDWHILE:
3813 if (defining != NULL) {
3814 if (defining->type == EXP_WHILE) {
3815 if (defining->def_depth > 0) {
3816 defining->def_depth --;
3817 return NO_DIRECTIVE_FOUND;
3818 }
3819 } else {
3820 return NO_DIRECTIVE_FOUND;
3821 }
3822 }
3823 if (tline->next != NULL) {
3824 error_precond(ERR_WARNING|ERR_PASS1,
3825 "trailing garbage after `%%endwhile' ignored");
3826 }
3827 if ((defining == NULL) || (defining->type != EXP_WHILE)) {
3828 error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'");
3829 return DIRECTIVE_FOUND;
3830 }
3831 ed = defining;
3832 defining = ed->prev;
3833 if (ed->ignoring == false) {
3834 ed->prev = expansions;
3835 expansions = ed;
3836 ei = new_ExpInv(EXP_WHILE, ed);
3837 ei->current = ed->line->next;
3838 ei->emitting = true;
3839 ei->prev = istk->expansion;
3840 istk->expansion = ei;
3841 } else {
3842 nasm_free(ed);
3843 }
3844 free_tlist(origline);
3845 return DIRECTIVE_FOUND;
3846
3847 case PP_EXITWHILE:
3848 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3849 /*
3850 * We must search along istk->expansion until we hit a
3851 * while invocation. Then we disable the emitting state(s)
3852 * between exitwhile and endwhile.
3853 */
3854 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3855 if (ei->type == EXP_WHILE) {
3856 break;
3857 }
3858 }
3859
3860 if (ei != NULL) {
3861 /*
3862 * Set all invocations leading back to the while
3863 * invocation to a non-emitting state.
3864 */
3865 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3866 eei->emitting = false;
3867 }
3868 eei->emitting = false;
3869 eei->current = NULL;
3870 eei->def->cur_depth = eei->def->max_depth;
3871 } else {
3872 error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block");
3873 }
3874 free_tlist(origline);
3875 return DIRECTIVE_FOUND;
3876
3877 case PP_COMMENT:
3878 if (defining != NULL) {
3879 if (defining->type == EXP_COMMENT) {
3880 defining->def_depth ++;
3881 }
3882 return NO_DIRECTIVE_FOUND;
3883 }
3884 ed = new_ExpDef(EXP_COMMENT);
3885 ed->ignoring = true;
3886 ed->prev = defining;
3887 defining = ed;
3888 free_tlist(origline);
3889 return DIRECTIVE_FOUND;
3890
3891 case PP_ENDCOMMENT:
3892 if (defining != NULL) {
3893 if (defining->type == EXP_COMMENT) {
3894 if (defining->def_depth > 0) {
3895 defining->def_depth --;
3896 return NO_DIRECTIVE_FOUND;
3897 }
3898 } else {
3899 return NO_DIRECTIVE_FOUND;
3900 }
3901 }
3902 if ((defining == NULL) || (defining->type != EXP_COMMENT)) {
3903 error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'");
3904 return DIRECTIVE_FOUND;
3905 }
3906 ed = defining;
3907 defining = ed->prev;
3908 nasm_free(ed);
3909 free_tlist(origline);
3910 return DIRECTIVE_FOUND;
3911
3912 case PP_FINAL:
3913 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3914 if (in_final != false) {
3915 error(ERR_FATAL, "`%%final' cannot be used recursively");
3916 }
3917 tline = tline->next;
3918 skip_white_(tline);
3919 if (tline == NULL) {
3920 error(ERR_NONFATAL, "`%%final' expects at least one parameter");
3921 } else {
3922 l = new_Line();
3923 l->first = copy_Token(tline);
3924 l->next = finals;
3925 finals = l;
3926 }
3927 free_tlist(origline);
3928 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003929
H. Peter Anvine2c80182005-01-15 22:15:51 +00003930 default:
3931 error(ERR_FATAL,
3932 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003933 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003934 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003935 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003936}
3937
3938/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003939 * Ensure that a macro parameter contains a condition code and
3940 * nothing else. Return the condition code index if so, or -1
3941 * otherwise.
3942 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003943static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003944{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003945 Token *tt;
3946 int i, j, k, m;
3947
H. Peter Anvin25a99342007-09-22 17:45:45 -07003948 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003949 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003950
H. Peter Anvineba20a72002-04-30 20:53:55 +00003951 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003952 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003953 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003954 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003955 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003956 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003957 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003958
3959 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003960 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003961 while (j - i > 1) {
3962 k = (j + i) / 2;
3963 m = nasm_stricmp(t->text, conditions[k]);
3964 if (m == 0) {
3965 i = k;
3966 j = -2;
3967 break;
3968 } else if (m < 0) {
3969 j = k;
3970 } else
3971 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003972 }
3973 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003974 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003975 return i;
3976}
3977
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003978static bool paste_tokens(Token **head, const struct tokseq_match *m,
3979 int mnum, bool handle_paste_tokens)
H. Peter Anvind784a082009-04-20 14:01:18 -07003980{
3981 Token **tail, *t, *tt;
3982 Token **paste_head;
3983 bool did_paste = false;
3984 char *tmp;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003985 int i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003986
3987 /* Now handle token pasting... */
3988 paste_head = NULL;
3989 tail = head;
3990 while ((t = *tail) && (tt = t->next)) {
3991 switch (t->type) {
3992 case TOK_WHITESPACE:
3993 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003994 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07003995 t->next = delete_Token(tt);
3996 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003997 /* Do not advance paste_head here */
3998 tail = &t->next;
3999 }
H. Peter Anvind784a082009-04-20 14:01:18 -07004000 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004001 case TOK_PASTE: /* %+ */
4002 if (handle_paste_tokens) {
4003 /* Zap %+ and whitespace tokens to the right */
4004 while (t && (t->type == TOK_WHITESPACE ||
4005 t->type == TOK_PASTE))
4006 t = *tail = delete_Token(t);
4007 if (!paste_head || !t)
4008 break; /* Nothing to paste with */
4009 tail = paste_head;
4010 t = *tail;
4011 tt = t->next;
4012 while (tok_type_(tt, TOK_WHITESPACE))
4013 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004014 if (tt) {
4015 tmp = nasm_strcat(t->text, tt->text);
4016 delete_Token(t);
4017 tt = delete_Token(tt);
4018 t = *tail = tokenize(tmp);
4019 nasm_free(tmp);
4020 while (t->next) {
4021 tail = &t->next;
4022 t = t->next;
4023 }
4024 t->next = tt; /* Attach the remaining token chain */
4025 did_paste = true;
4026 }
4027 paste_head = tail;
4028 tail = &t->next;
4029 break;
4030 }
4031 /* else fall through */
4032 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004033 /*
4034 * Concatenation of tokens might look nontrivial
4035 * but in real it's pretty simple -- the caller
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004036 * prepares the masks of token types to be concatenated
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004037 * and we simply find matched sequences and slip
4038 * them together
4039 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004040 for (i = 0; i < mnum; i++) {
4041 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
4042 size_t len = 0;
4043 char *tmp, *p;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004044
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004045 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004046 len += strlen(tt->text);
4047 tt = tt->next;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004048 }
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004049
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004050 /*
4051 * Now tt points to the first token after
4052 * the potential paste area...
4053 */
4054 if (tt != t->next) {
4055 /* We have at least two tokens... */
4056 len += strlen(t->text);
4057 p = tmp = nasm_malloc(len+1);
4058 while (t != tt) {
4059 strcpy(p, t->text);
4060 p = strchr(p, '\0');
4061 t = delete_Token(t);
4062 }
4063 t = *tail = tokenize(tmp);
4064 nasm_free(tmp);
4065 while (t->next) {
4066 tail = &t->next;
4067 t = t->next;
4068 }
4069 t->next = tt; /* Attach the remaining token chain */
4070 did_paste = true;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004071 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004072 paste_head = tail;
4073 tail = &t->next;
4074 break;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004075 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004076 }
4077 if (i >= mnum) { /* no match */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004078 tail = &t->next;
4079 if (!tok_type_(t->next, TOK_WHITESPACE))
4080 paste_head = tail;
4081 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004082 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004083 }
4084 }
4085 return did_paste;
4086}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004087
4088/*
4089 * expands to a list of tokens from %{x:y}
4090 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004091static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004092{
4093 Token *t = tline, **tt, *tm, *head;
4094 char *pos;
4095 int fst, lst, j, i;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004096
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004097 pos = strchr(tline->text, ':');
4098 nasm_assert(pos);
4099
4100 lst = atoi(pos + 1);
4101 fst = atoi(tline->text + 1);
4102
4103 /*
4104 * only macros params are accounted so
4105 * if someone passes %0 -- we reject such
4106 * value(s)
4107 */
4108 if (lst == 0 || fst == 0)
4109 goto err;
4110
4111 /* the values should be sane */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004112 if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) ||
4113 (lst > (int)ei->nparam || lst < (-(int)ei->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004114 goto err;
4115
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004116 fst = fst < 0 ? fst + (int)ei->nparam + 1: fst;
4117 lst = lst < 0 ? lst + (int)ei->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004118
4119 /* counted from zero */
4120 fst--, lst--;
4121
4122 /*
4123 * it will be at least one token
4124 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004125 tm = ei->params[(fst + ei->rotate) % ei->nparam];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004126 t = new_Token(NULL, tm->type, tm->text, 0);
4127 head = t, tt = &t->next;
4128 if (fst < lst) {
4129 for (i = fst + 1; i <= lst; i++) {
4130 t = new_Token(NULL, TOK_OTHER, ",", 0);
4131 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004132 j = (i + ei->rotate) % ei->nparam;
4133 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004134 t = new_Token(NULL, tm->type, tm->text, 0);
4135 *tt = t, tt = &t->next;
4136 }
4137 } else {
4138 for (i = fst - 1; i >= lst; i--) {
4139 t = new_Token(NULL, TOK_OTHER, ",", 0);
4140 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004141 j = (i + ei->rotate) % ei->nparam;
4142 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004143 t = new_Token(NULL, tm->type, tm->text, 0);
4144 *tt = t, tt = &t->next;
4145 }
4146 }
4147
4148 *last = tt;
4149 return head;
4150
4151err:
4152 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
4153 &tline->text[1]);
4154 return tline;
4155}
4156
H. Peter Anvin76690a12002-04-30 20:52:49 +00004157/*
4158 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004159 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004160 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004161 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004162static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004163{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004164 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004165 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004166 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004167
4168 tail = &thead;
4169 thead = NULL;
4170
H. Peter Anvine2c80182005-01-15 22:15:51 +00004171 while (tline) {
4172 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004173 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4174 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4175 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004176 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004177 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004178 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004179 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004180 int i;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004181 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004182
H. Peter Anvine2c80182005-01-15 22:15:51 +00004183 t = tline;
4184 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004185
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004186 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
4187 if (ei->type == EXP_MMACRO) {
4188 break;
4189 }
4190 }
4191 if (ei == NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004192 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004193 } else {
4194 pos = strchr(t->text, ':');
4195 if (!pos) {
4196 switch (t->text[1]) {
4197 /*
4198 * We have to make a substitution of one of the
4199 * forms %1, %-1, %+1, %%foo, %0.
4200 */
4201 case '0':
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004202 if ((strlen(t->text) > 2) && (t->text[2] == '0')) {
4203 type = TOK_ID;
4204 text = nasm_strdup(ei->label_text);
4205 } else {
4206 type = TOK_NUMBER;
4207 snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam);
4208 text = nasm_strdup(tmpbuf);
4209 }
4210 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004211 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004212 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004213 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004214 ei->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004215 text = nasm_strcat(tmpbuf, t->text + 2);
4216 break;
4217 case '-':
4218 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004219 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004220 tt = NULL;
4221 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004222 if (ei->nparam > 1)
4223 n = (n + ei->rotate) % ei->nparam;
4224 tt = ei->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004225 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004226 cc = find_cc(tt);
4227 if (cc == -1) {
4228 error(ERR_NONFATAL,
4229 "macro parameter %d is not a condition code",
4230 n + 1);
4231 text = NULL;
4232 } else {
4233 type = TOK_ID;
4234 if (inverse_ccs[cc] == -1) {
4235 error(ERR_NONFATAL,
4236 "condition code `%s' is not invertible",
4237 conditions[cc]);
4238 text = NULL;
4239 } else
4240 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4241 }
4242 break;
4243 case '+':
4244 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004245 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004246 tt = NULL;
4247 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004248 if (ei->nparam > 1)
4249 n = (n + ei->rotate) % ei->nparam;
4250 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004251 }
4252 cc = find_cc(tt);
4253 if (cc == -1) {
4254 error(ERR_NONFATAL,
4255 "macro parameter %d is not a condition code",
4256 n + 1);
4257 text = NULL;
4258 } else {
4259 type = TOK_ID;
4260 text = nasm_strdup(conditions[cc]);
4261 }
4262 break;
4263 default:
4264 n = atoi(t->text + 1) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004265 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004266 tt = NULL;
4267 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004268 if (ei->nparam > 1)
4269 n = (n + ei->rotate) % ei->nparam;
4270 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004271 }
4272 if (tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004273 for (i = 0; i < ei->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004274 *tail = new_Token(NULL, tt->type, tt->text, 0);
4275 tail = &(*tail)->next;
4276 tt = tt->next;
4277 }
4278 }
4279 text = NULL; /* we've done it here */
4280 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004281 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004282 } else {
4283 /*
4284 * seems we have a parameters range here
4285 */
4286 Token *head, **last;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004287 head = expand_mmac_params_range(ei, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004288 if (head != t) {
4289 *tail = head;
4290 *last = tline;
4291 tline = head;
4292 text = NULL;
4293 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004294 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004295 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004296 if (!text) {
4297 delete_Token(t);
4298 } else {
4299 *tail = t;
4300 tail = &t->next;
4301 t->type = type;
4302 nasm_free(t->text);
4303 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004304 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004305 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004306 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004307 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004308 } else if (tline->type == TOK_INDIRECT) {
4309 t = tline;
4310 tline = tline->next;
4311 tt = tokenize(t->text);
4312 tt = expand_mmac_params(tt);
4313 tt = expand_smacro(tt);
4314 *tail = tt;
4315 while (tt) {
4316 tt->a.mac = NULL; /* Necessary? */
4317 tail = &tt->next;
4318 tt = tt->next;
4319 }
4320 delete_Token(t);
4321 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004322 } else {
4323 t = *tail = tline;
4324 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004325 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004326 tail = &t->next;
4327 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004328 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004329 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004330
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004331 if (changed) {
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004332 const struct tokseq_match t[] = {
4333 {
4334 PP_CONCAT_MASK(TOK_ID) |
4335 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4336 PP_CONCAT_MASK(TOK_ID) |
4337 PP_CONCAT_MASK(TOK_NUMBER) |
4338 PP_CONCAT_MASK(TOK_FLOAT) |
4339 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4340 },
4341 {
4342 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4343 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4344 }
4345 };
4346 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004347 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004348
H. Peter Anvin76690a12002-04-30 20:52:49 +00004349 return thead;
4350}
4351
4352/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004353 * Expand all single-line macro calls made in the given line.
4354 * Return the expanded version of the line. The original is deemed
4355 * to be destroyed in the process. (In reality we'll just move
4356 * Tokens from input to output a lot of the time, rather than
4357 * actually bothering to destroy and replicate.)
4358 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004359
H. Peter Anvine2c80182005-01-15 22:15:51 +00004360static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004361{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004362 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004363 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004364 Token **params;
4365 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004366 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004367 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004368 Token *org_tline = tline;
4369 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004370 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004371 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004372 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004373
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004374 /*
4375 * Trick: we should avoid changing the start token pointer since it can
4376 * be contained in "next" field of other token. Because of this
4377 * we allocate a copy of first token and work with it; at the end of
4378 * routine we copy it back
4379 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004380 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004381 tline = new_Token(org_tline->next, org_tline->type,
4382 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004383 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004384 nasm_free(org_tline->text);
4385 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004386 }
4387
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004388 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004389
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004390again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004391 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004392 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004393
H. Peter Anvine2c80182005-01-15 22:15:51 +00004394 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004395 if (!--deadman) {
4396 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004397 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004398 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004399
H. Peter Anvine2c80182005-01-15 22:15:51 +00004400 if ((mname = tline->text)) {
4401 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004402 if (tline->type == TOK_ID) {
4403 head = (SMacro *)hash_findix(&smacros, mname);
4404 } else if (tline->type == TOK_PREPROC_ID) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004405 ctx = get_ctx(mname, &mname, false);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004406 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4407 } else
4408 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004409
H. Peter Anvine2c80182005-01-15 22:15:51 +00004410 /*
4411 * We've hit an identifier. As in is_mmacro below, we first
4412 * check whether the identifier is a single-line macro at
4413 * all, then think about checking for parameters if
4414 * necessary.
4415 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004416 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004417 if (!mstrcmp(m->name, mname, m->casesense))
4418 break;
4419 if (m) {
4420 mstart = tline;
4421 params = NULL;
4422 paramsize = NULL;
4423 if (m->nparam == 0) {
4424 /*
4425 * Simple case: the macro is parameterless. Discard the
4426 * one token that the macro call took, and push the
4427 * expansion back on the to-do stack.
4428 */
4429 if (!m->expansion) {
4430 if (!strcmp("__FILE__", m->name)) {
4431 int32_t num = 0;
4432 char *file = NULL;
4433 src_get(&num, &file);
4434 tline->text = nasm_quote(file, strlen(file));
4435 tline->type = TOK_STRING;
4436 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004437 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004438 }
4439 if (!strcmp("__LINE__", m->name)) {
4440 nasm_free(tline->text);
4441 make_tok_num(tline, src_get_linnum());
4442 continue;
4443 }
4444 if (!strcmp("__BITS__", m->name)) {
4445 nasm_free(tline->text);
4446 make_tok_num(tline, globalbits);
4447 continue;
4448 }
4449 tline = delete_Token(tline);
4450 continue;
4451 }
4452 } else {
4453 /*
4454 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004455 * exists and takes parameters. We must find the
4456 * parameters in the call, count them, find the SMacro
4457 * that corresponds to that form of the macro call, and
4458 * substitute for the parameters when we expand. What a
4459 * pain.
4460 */
4461 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004462 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004463 do {
4464 t = tline->next;
4465 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004466 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004467 t->text = NULL;
4468 t = tline->next = delete_Token(t);
4469 }
4470 tline = t;
4471 } while (tok_type_(tline, TOK_WHITESPACE));
4472 if (!tok_is_(tline, "(")) {
4473 /*
4474 * This macro wasn't called with parameters: ignore
4475 * the call. (Behaviour borrowed from gnu cpp.)
4476 */
4477 tline = mstart;
4478 m = NULL;
4479 } else {
4480 int paren = 0;
4481 int white = 0;
4482 brackets = 0;
4483 nparam = 0;
4484 sparam = PARAM_DELTA;
4485 params = nasm_malloc(sparam * sizeof(Token *));
4486 params[0] = tline->next;
4487 paramsize = nasm_malloc(sparam * sizeof(int));
4488 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004489 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004490 /*
4491 * For some unusual expansions
4492 * which concatenates function call
4493 */
4494 t = tline->next;
4495 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004496 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004497 t->text = NULL;
4498 t = tline->next = delete_Token(t);
4499 }
4500 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004501
H. Peter Anvine2c80182005-01-15 22:15:51 +00004502 if (!tline) {
4503 error(ERR_NONFATAL,
4504 "macro call expects terminating `)'");
4505 break;
4506 }
4507 if (tline->type == TOK_WHITESPACE
4508 && brackets <= 0) {
4509 if (paramsize[nparam])
4510 white++;
4511 else
4512 params[nparam] = tline->next;
4513 continue; /* parameter loop */
4514 }
4515 if (tline->type == TOK_OTHER
4516 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004517 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004518 if (ch == ',' && !paren && brackets <= 0) {
4519 if (++nparam >= sparam) {
4520 sparam += PARAM_DELTA;
4521 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004522 sparam * sizeof(Token *));
4523 paramsize = nasm_realloc(paramsize,
4524 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004525 }
4526 params[nparam] = tline->next;
4527 paramsize[nparam] = 0;
4528 white = 0;
4529 continue; /* parameter loop */
4530 }
4531 if (ch == '{' &&
4532 (brackets > 0 || (brackets == 0 &&
4533 !paramsize[nparam])))
4534 {
4535 if (!(brackets++)) {
4536 params[nparam] = tline->next;
4537 continue; /* parameter loop */
4538 }
4539 }
4540 if (ch == '}' && brackets > 0)
4541 if (--brackets == 0) {
4542 brackets = -1;
4543 continue; /* parameter loop */
4544 }
4545 if (ch == '(' && !brackets)
4546 paren++;
4547 if (ch == ')' && brackets <= 0)
4548 if (--paren < 0)
4549 break;
4550 }
4551 if (brackets < 0) {
4552 brackets = 0;
4553 error(ERR_NONFATAL, "braces do not "
4554 "enclose all of macro parameter");
4555 }
4556 paramsize[nparam] += white + 1;
4557 white = 0;
4558 } /* parameter loop */
4559 nparam++;
4560 while (m && (m->nparam != nparam ||
4561 mstrcmp(m->name, mname,
4562 m->casesense)))
4563 m = m->next;
4564 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004565 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004566 "macro `%s' exists, "
4567 "but not taking %d parameters",
4568 mstart->text, nparam);
4569 }
4570 }
4571 if (m && m->in_progress)
4572 m = NULL;
4573 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004574 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004575 * Design question: should we handle !tline, which
4576 * indicates missing ')' here, or expand those
4577 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004578 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004579 */
4580 nasm_free(params);
4581 nasm_free(paramsize);
4582 tline = mstart;
4583 } else {
4584 /*
4585 * Expand the macro: we are placed on the last token of the
4586 * call, so that we can easily split the call from the
4587 * following tokens. We also start by pushing an SMAC_END
4588 * token for the cycle removal.
4589 */
4590 t = tline;
4591 if (t) {
4592 tline = t->next;
4593 t->next = NULL;
4594 }
4595 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004596 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004597 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004598 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004599 list_for_each(t, m->expansion) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004600 if (t->type >= TOK_SMAC_PARAM) {
4601 Token *pcopy = tline, **ptail = &pcopy;
4602 Token *ttt, *pt;
4603 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004604
H. Peter Anvine2c80182005-01-15 22:15:51 +00004605 ttt = params[t->type - TOK_SMAC_PARAM];
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004606 i = paramsize[t->type - TOK_SMAC_PARAM];
4607 while (--i >= 0) {
4608 pt = *ptail = new_Token(tline, ttt->type,
4609 ttt->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004610 ptail = &pt->next;
4611 ttt = ttt->next;
4612 }
4613 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004614 } else if (t->type == TOK_PREPROC_Q) {
4615 tt = new_Token(tline, TOK_ID, mname, 0);
4616 tline = tt;
4617 } else if (t->type == TOK_PREPROC_QQ) {
4618 tt = new_Token(tline, TOK_ID, m->name, 0);
4619 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004620 } else {
4621 tt = new_Token(tline, t->type, t->text, 0);
4622 tline = tt;
4623 }
4624 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004625
H. Peter Anvine2c80182005-01-15 22:15:51 +00004626 /*
4627 * Having done that, get rid of the macro call, and clean
4628 * up the parameters.
4629 */
4630 nasm_free(params);
4631 nasm_free(paramsize);
4632 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004633 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004634 continue; /* main token loop */
4635 }
4636 }
4637 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004638
H. Peter Anvine2c80182005-01-15 22:15:51 +00004639 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004640 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004641 tline = delete_Token(tline);
4642 } else {
4643 t = *tail = tline;
4644 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004645 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004646 t->next = NULL;
4647 tail = &t->next;
4648 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004649 }
4650
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004651 /*
4652 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004653 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004654 * TOK_IDs should be concatenated.
4655 * Also we look for %+ tokens and concatenate the tokens before and after
4656 * them (without white spaces in between).
4657 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004658 if (expanded) {
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004659 const struct tokseq_match t[] = {
4660 {
4661 PP_CONCAT_MASK(TOK_ID) |
4662 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4663 PP_CONCAT_MASK(TOK_ID) |
4664 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4665 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4666 }
4667 };
4668 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004669 /*
4670 * If we concatenated something, *and* we had previously expanded
4671 * an actual macro, scan the lines again for macros...
4672 */
4673 tline = thead;
4674 expanded = false;
4675 goto again;
4676 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004677 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004678
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004679err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004680 if (org_tline) {
4681 if (thead) {
4682 *org_tline = *thead;
4683 /* since we just gave text to org_line, don't free it */
4684 thead->text = NULL;
4685 delete_Token(thead);
4686 } else {
4687 /* the expression expanded to empty line;
4688 we can't return NULL for some reasons
4689 we just set the line to a single WHITESPACE token. */
4690 memset(org_tline, 0, sizeof(*org_tline));
4691 org_tline->text = NULL;
4692 org_tline->type = TOK_WHITESPACE;
4693 }
4694 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004695 }
4696
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004697 return thead;
4698}
4699
4700/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004701 * Similar to expand_smacro but used exclusively with macro identifiers
4702 * right before they are fetched in. The reason is that there can be
4703 * identifiers consisting of several subparts. We consider that if there
4704 * are more than one element forming the name, user wants a expansion,
4705 * otherwise it will be left as-is. Example:
4706 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004707 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004708 *
4709 * the identifier %$abc will be left as-is so that the handler for %define
4710 * will suck it and define the corresponding value. Other case:
4711 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004712 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004713 *
4714 * In this case user wants name to be expanded *before* %define starts
4715 * working, so we'll expand %$abc into something (if it has a value;
4716 * otherwise it will be left as-is) then concatenate all successive
4717 * PP_IDs into one.
4718 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004719static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004720{
4721 Token *cur, *oldnext = NULL;
4722
H. Peter Anvin734b1882002-04-30 21:01:08 +00004723 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004724 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004725
4726 cur = tline;
4727 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00004728 (cur->next->type == TOK_ID ||
4729 cur->next->type == TOK_PREPROC_ID
4730 || cur->next->type == TOK_NUMBER))
4731 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004732
4733 /* If identifier consists of just one token, don't expand */
4734 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004735 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004736
H. Peter Anvine2c80182005-01-15 22:15:51 +00004737 if (cur) {
4738 oldnext = cur->next; /* Detach the tail past identifier */
4739 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004740 }
4741
H. Peter Anvin734b1882002-04-30 21:01:08 +00004742 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004743
H. Peter Anvine2c80182005-01-15 22:15:51 +00004744 if (cur) {
4745 /* expand_smacro possibly changhed tline; re-scan for EOL */
4746 cur = tline;
4747 while (cur && cur->next)
4748 cur = cur->next;
4749 if (cur)
4750 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004751 }
4752
4753 return tline;
4754}
4755
4756/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004757 * Determine whether the given line constitutes a multi-line macro
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004758 * call, and return the ExpDef structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004759 * to check for an initial label - that's taken care of in
4760 * expand_mmacro - but must check numbers of parameters. Guaranteed
4761 * to be called with tline->type == TOK_ID, so the putative macro
4762 * name is easy to find.
4763 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004764static ExpDef *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004765{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004766 ExpDef *head, *ed;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004767 Token **params;
4768 int nparam;
4769
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004770 head = (ExpDef *) hash_findix(&expdefs, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004771
4772 /*
4773 * Efficiency: first we see if any macro exists with the given
4774 * name. If not, we can return NULL immediately. _Then_ we
4775 * count the parameters, and then we look further along the
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004776 * list if necessary to find the proper ExpDef.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004777 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004778 list_for_each(ed, head)
4779 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004780 break;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004781 if (!ed)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004782 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004783
4784 /*
4785 * OK, we have a potential macro. Count and demarcate the
4786 * parameters.
4787 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004788 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004789
4790 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004791 * So we know how many parameters we've got. Find the ExpDef
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004792 * structure that handles this number.
4793 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004794 while (ed) {
4795 if (ed->nparam_min <= nparam
4796 && (ed->plus || nparam <= ed->nparam_max)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004797 /*
4798 * It's right, and we can use it. Add its default
4799 * parameters to the end of our list if necessary.
4800 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004801 if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004802 params =
4803 nasm_realloc(params,
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004804 ((ed->nparam_min + ed->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004805 1) * sizeof(*params)));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004806 while (nparam < ed->nparam_min + ed->ndefs) {
4807 params[nparam] = ed->defaults[nparam - ed->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004808 nparam++;
4809 }
4810 }
4811 /*
4812 * If we've gone over the maximum parameter count (and
4813 * we're in Plus mode), ignore parameters beyond
4814 * nparam_max.
4815 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004816 if (ed->plus && nparam > ed->nparam_max)
4817 nparam = ed->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004818 /*
4819 * Then terminate the parameter list, and leave.
4820 */
4821 if (!params) { /* need this special case */
4822 params = nasm_malloc(sizeof(*params));
4823 nparam = 0;
4824 }
4825 params[nparam] = NULL;
4826 *params_array = params;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004827 return ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004828 }
4829 /*
4830 * This one wasn't right: look for the next one with the
4831 * same name.
4832 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004833 list_for_each(ed, ed->next)
4834 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004835 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004836 }
4837
4838 /*
4839 * After all that, we didn't find one with the right number of
4840 * parameters. Issue a warning, and fail to expand the macro.
4841 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004842 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004843 "macro `%s' exists, but not taking %d parameters",
4844 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004845 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004846 return NULL;
4847}
4848
4849/*
4850 * Expand the multi-line macro call made by the given line, if
4851 * there is one to be expanded. If there is, push the expansion on
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004852 * istk->expansion and return true. Otherwise return false.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004853 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004854static bool expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004855{
H. Peter Anvineba20a72002-04-30 20:53:55 +00004856 Token *label = NULL;
4857 int dont_prepend = 0;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004858 Token **params, *t, *mtok;
4859 Line *l = NULL;
4860 ExpDef *ed;
4861 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004862 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004863 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004864
4865 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004866 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004867 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004868 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004869 return false;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004870 mtok = t;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004871 ed = is_mmacro(t, &params);
4872 if (ed != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004873 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004874 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004875 Token *last;
4876 /*
4877 * We have an id which isn't a macro call. We'll assume
4878 * it might be a label; we'll also check to see if a
4879 * colon follows it. Then, if there's another id after
4880 * that lot, we'll check it again for macro-hood.
4881 */
4882 label = last = t;
4883 t = t->next;
4884 if (tok_type_(t, TOK_WHITESPACE))
4885 last = t, t = t->next;
4886 if (tok_is_(t, ":")) {
4887 dont_prepend = 1;
4888 last = t, t = t->next;
4889 if (tok_type_(t, TOK_WHITESPACE))
4890 last = t, t = t->next;
4891 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004892 if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, &params)))
4893 return false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004894 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004895 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004896 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004897 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004898
4899 /*
4900 * Fix up the parameters: this involves stripping leading and
4901 * trailing whitespace, then stripping braces if they are
4902 * present.
4903 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004904 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004905 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004906
H. Peter Anvine2c80182005-01-15 22:15:51 +00004907 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004908 int brace = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004909 int comma = (!ed->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004910
H. Peter Anvine2c80182005-01-15 22:15:51 +00004911 t = params[i];
4912 skip_white_(t);
4913 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004914 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004915 params[i] = t;
4916 paramlen[i] = 0;
4917 while (t) {
4918 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4919 break; /* ... because we have hit a comma */
4920 if (comma && t->type == TOK_WHITESPACE
4921 && tok_is_(t->next, ","))
4922 break; /* ... or a space then a comma */
4923 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4924 break; /* ... or a brace */
4925 t = t->next;
4926 paramlen[i]++;
4927 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004928 }
4929
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004930 if (ed->cur_depth >= ed->max_depth) {
4931 if (ed->max_depth > 1) {
4932 error(ERR_WARNING,
4933 "reached maximum macro recursion depth of %i for %s",
4934 ed->max_depth,ed->name);
4935 }
4936 return false;
4937 } else {
4938 ed->cur_depth ++;
4939 }
4940
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004941 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004942 * OK, we have found a ExpDef structure representing a
4943 * previously defined mmacro. Create an expansion invocation
4944 * and point it back to the expansion definition. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004945 * parameter tokens and macro-local tokens doesn't get done
4946 * until the single-line macro substitution process; this is
4947 * because delaying them allows us to change the semantics
4948 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004949 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004950 ei = new_ExpInv(EXP_MMACRO, ed);
4951 ei->name = nasm_strdup(mname);
4952// ei->label = label;
4953// ei->label_text = detoken(label, false);
4954 ei->current = ed->line;
4955 ei->emitting = true;
4956// ei->iline = tline;
4957 ei->params = params;
4958 ei->nparam = nparam;
4959 ei->rotate = 0;
4960 ei->paramlen = paramlen;
4961 ei->lineno = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004962
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004963 ei->prev = istk->expansion;
4964 istk->expansion = ei;
4965
4966 /*
4967 * Special case: detect %00 on first invocation; if found,
4968 * avoid emitting any labels that precede the mmacro call.
4969 * ed->prepend is set to -1 when %00 is detected, else 1.
4970 */
4971 if (ed->prepend == 0) {
4972 for (l = ed->line; l != NULL; l = l->next) {
4973 for (t = l->first; t != NULL; t = t->next) {
4974 if ((t->type == TOK_PREPROC_ID) &&
4975 (strlen(t->text) == 3) &&
4976 (t->text[1] == '0') && (t->text[2] == '0')) {
4977 dont_prepend = -1;
4978 break;
4979 }
4980 }
4981 if (dont_prepend < 0) {
4982 break;
4983 }
4984 }
4985 ed->prepend = ((dont_prepend < 0) ? -1 : 1);
4986 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004987
4988 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004989 * If we had a label, push it on as the first line of
4990 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004991 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004992 if (label != NULL) {
4993 if (ed->prepend < 0) {
4994 ei->label_text = detoken(label, false);
4995 } else {
4996 if (dont_prepend == 0) {
4997 t = label;
4998 while (t->next != NULL) {
4999 t = t->next;
5000 }
5001 t->next = new_Token(NULL, TOK_OTHER, ":", 0);
5002 }
5003 Line *l = new_Line();
5004 l->first = copy_Token(label);
5005 l->next = ei->current;
5006 ei->current = l;
5007 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005008 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005009
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005010 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005011
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005012 istk->mmac_depth++;
5013 return true;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005014}
5015
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005016/* The function that actually does the error reporting */
5017static void verror(int severity, const char *fmt, va_list arg)
5018{
5019 char buff[1024];
5020
5021 vsnprintf(buff, sizeof(buff), fmt, arg);
5022
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005023 if ((istk != NULL) && (istk->mmac_depth > 0)) {
5024 ExpInv *ei = istk->expansion;
5025 int lineno = ei->lineno;
5026 while (ei != NULL) {
5027 if (ei->type == EXP_MMACRO) {
5028 break;
5029 }
5030 lineno += ei->relno;
5031 ei = ei->prev;
5032 }
5033 nasm_error(severity, "(%s:%d) %s", ei->def->name,
5034 lineno, buff);
5035 } else {
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005036 nasm_error(severity, "%s", buff);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005037 }
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005038}
5039
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005040/*
5041 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07005042 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005043 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005044static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005045{
5046 va_list arg;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005047 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005048 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00005049 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005050}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005051
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005052/*
5053 * Because %else etc are evaluated in the state context
5054 * of the previous branch, errors might get lost with error():
5055 * %if 0 ... %else trailing garbage ... %endif
5056 * So %else etc should report errors with this function.
5057 */
5058static void error_precond(int severity, const char *fmt, ...)
5059{
5060 va_list arg;
5061
5062 /* Only ignore the error if it's really in a dead branch */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005063 if ((istk != NULL) &&
5064 (istk->expansion != NULL) &&
5065 (istk->expansion->type == EXP_IF) &&
5066 (istk->expansion->def->state == COND_NEVER))
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005067 return;
5068
5069 va_start(arg, fmt);
5070 verror(severity, fmt, arg);
5071 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005072}
5073
H. Peter Anvin734b1882002-04-30 21:01:08 +00005074static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005075pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005076{
H. Peter Anvin7383b402008-09-24 10:20:40 -07005077 Token *t;
5078
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005079 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005080 istk = nasm_malloc(sizeof(Include));
5081 istk->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005082 istk->expansion = NULL;
5083 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00005084 istk->fname = NULL;
5085 src_set_fname(nasm_strdup(file));
5086 src_set_linnum(0);
5087 istk->lineinc = 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005088 istk->mmac_depth = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005089 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07005090 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00005091 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005092 defining = NULL;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005093 finals = NULL;
5094 in_final = false;
Charles Crayned4200be2008-07-12 16:42:33 -07005095 nested_mac_count = 0;
5096 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005097 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005098 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005099 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005100 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005101 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005102 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005103 }
H. Peter Anvind2456592008-06-19 15:04:18 -07005104 any_extrastdmac = extrastdmac && *extrastdmac;
5105 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005106 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005107
5108 /*
5109 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5110 * The caller, however, will also pass in 3 for preprocess-only so
5111 * we can set __PASS__ accordingly.
5112 */
5113 pass = apass > 2 ? 2 : apass;
5114
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005115 dephead = deptail = deplist;
5116 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005117 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
5118 sl->next = NULL;
5119 strcpy(sl->str, file);
5120 *deptail = sl;
5121 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005122 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07005123
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005124 /*
5125 * Define the __PASS__ macro. This is defined here unlike
5126 * all the other builtins, because it is special -- it varies between
5127 * passes.
5128 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07005129 t = nasm_malloc(sizeof(*t));
5130 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005131 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07005132 t->a.mac = NULL;
5133 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005134}
5135
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005136static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005137{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005138 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005139 Token *tline;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005140 ExpInv *ei;
5141
H. Peter Anvine2c80182005-01-15 22:15:51 +00005142 while (1) {
5143 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005144 * Fetch a tokenized line, either from the expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005145 * buffer or from the input file.
5146 */
5147 tline = NULL;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005148
H. Peter Anvine2c80182005-01-15 22:15:51 +00005149 while (1) { /* until we get a line we can use */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005150 /*
5151 * Fetch a tokenized line from the expansion buffer
5152 */
5153 if (istk->expansion != NULL) {
5154 ei = istk->expansion;
5155 if (ei->current != NULL) {
5156 if (ei->emitting == false) {
5157 ei->current = NULL;
5158 continue;
5159 }
5160 Line *l = NULL;
5161 l = ei->current;
5162 ei->current = l->next;
5163 ei->lineno++;
5164 tline = copy_Token(l->first);
5165 if (((ei->type == EXP_REP) ||
5166 (ei->type == EXP_MMACRO) ||
5167 (ei->type == EXP_WHILE))
5168 && (ei->def->nolist == false)) {
5169 char *p = detoken(tline, false);
5170 list->line(LIST_MACRO, p);
5171 nasm_free(p);
5172 }
5173 if (ei->linnum > -1) {
5174 src_set_linnum(src_get_linnum() + 1);
5175 }
5176 break;
5177 } else if ((ei->type == EXP_REP) &&
5178 (ei->def->cur_depth < ei->def->max_depth)) {
5179 ei->def->cur_depth ++;
5180 ei->current = ei->def->line;
5181 ei->lineno = 0;
5182 continue;
5183 } else if ((ei->type == EXP_WHILE) &&
5184 (ei->def->cur_depth < ei->def->max_depth)) {
5185 ei->current = ei->def->line;
5186 ei->lineno = 0;
5187 tline = copy_Token(ei->current->first);
5188 int j = if_condition(tline, PP_WHILE);
5189 tline = NULL;
5190 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
5191 if (j == COND_IF_TRUE) {
5192 ei->current = ei->current->next;
5193 ei->def->cur_depth ++;
5194 } else {
5195 ei->emitting = false;
5196 ei->current = NULL;
5197 ei->def->cur_depth = ei->def->max_depth;
5198 }
5199 continue;
5200 } else {
5201 istk->expansion = ei->prev;
5202 ExpDef *ed = ei->def;
5203 if (ed != NULL) {
5204 if ((ei->emitting == true) &&
5205 (ed->max_depth == DEADMAN_LIMIT) &&
5206 (ed->cur_depth == DEADMAN_LIMIT)
5207 ) {
5208 error(ERR_FATAL, "runaway expansion detected, aborting");
5209 }
5210 if (ed->cur_depth > 0) {
5211 ed->cur_depth --;
5212 } else if ((ed->type != EXP_MMACRO) && (ed->type != EXP_IF)) {
5213 /***** should this really be right here??? *****/
5214 /*
5215 Line *l = NULL, *ll = NULL;
5216 for (l = ed->line; l != NULL;) {
5217 if (l->first != NULL) {
5218 free_tlist(l->first);
5219 l->first = NULL;
5220 }
5221 ll = l;
5222 l = l->next;
5223 nasm_free(ll);
5224 }
5225 expansions = ed->prev;
5226 nasm_free(ed);
5227 */
5228 }
5229 if ((ei->type == EXP_REP) ||
5230 (ei->type == EXP_MMACRO) ||
5231 (ei->type == EXP_WHILE)) {
5232 list->downlevel(LIST_MACRO);
5233 if (ei->type == EXP_MMACRO) {
5234 istk->mmac_depth--;
5235 }
5236 }
5237 }
5238 if (ei->linnum > -1) {
5239 src_set_linnum(ei->linnum);
5240 }
5241 free_expinv(ei);
5242 continue;
5243 }
5244 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00005245
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005246 /*
5247 * Read in line from input and tokenize
5248 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005249 line = read_line();
5250 if (line) { /* from the current input file */
5251 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005252 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005253 nasm_free(line);
5254 break;
5255 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005256
H. Peter Anvine2c80182005-01-15 22:15:51 +00005257 /*
5258 * The current file has ended; work down the istk
5259 */
5260 {
5261 Include *i = istk;
5262 fclose(i->fp);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005263 if (i->expansion != NULL) {
5264 error(ERR_FATAL,
5265 "end of file while still in an expansion");
5266 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005267 /* only set line and file name if there's a next node */
5268 if (i->next) {
5269 src_set_linnum(i->lineno);
5270 nasm_free(src_set_fname(i->fname));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005271 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005272 if ((i->next == NULL) && (finals != NULL)) {
5273 in_final = true;
5274 ei = new_ExpInv(EXP_FINAL, NULL);
5275 ei->emitting = true;
5276 ei->current = finals;
5277 istk->expansion = ei;
5278 finals = NULL;
5279 continue;
5280 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005281 istk = i->next;
5282 list->downlevel(LIST_INCLUDE);
5283 nasm_free(i);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005284 if (istk == NULL) {
5285 if (finals != NULL) {
5286 in_final = true;
5287 } else {
5288 return NULL;
5289 }
5290 }
5291 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005292 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005293 }
5294
5295 if (defining == NULL) {
5296 tline = expand_mmac_params(tline);
5297 }
5298
H. Peter Anvine2c80182005-01-15 22:15:51 +00005299 /*
5300 * Check the line to see if it's a preprocessor directive.
5301 */
5302 if (do_directive(tline) == DIRECTIVE_FOUND) {
5303 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005304 } else if (defining != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005305 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005306 * We're defining an expansion. We emit nothing at all,
5307 * and just shove the tokenized line on to the definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005308 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005309 if (defining->ignoring == false) {
5310 Line *l = new_Line();
5311 l->first = tline;
5312 if (defining->line == NULL) {
5313 defining->line = l;
5314 defining->last = l;
5315 } else {
5316 defining->last->next = l;
5317 defining->last = l;
5318 }
5319 } else {
5320 //free_tlist(tline); /***** sanity check: is this supposed to be here? *****/
5321 }
5322 defining->linecount++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005323 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005324 } else if ((istk->expansion != NULL) &&
5325 (istk->expansion->emitting != true)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005326 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005327 * We're in a non-emitting branch of an expansion.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005328 * Emit nothing at all, not even a blank line: when we
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005329 * emerge from the expansion we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005330 * directive so we keep our place correctly.
5331 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005332 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005333 continue;
5334 } else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005335 tline = expand_smacro(tline);
5336 if (expand_mmacro(tline) != true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005337 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005338 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005339 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005340 line = detoken(tline, true);
5341 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005342 break;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005343 } else {
5344 continue;
5345 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005346 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005347 }
5348 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005349}
5350
H. Peter Anvine2c80182005-01-15 22:15:51 +00005351static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005352{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005353 if (defining != NULL) {
5354 error(ERR_NONFATAL, "end of file while still defining an expansion");
5355 while (defining != NULL) {
5356 ExpDef *ed = defining;
5357 defining = ed->prev;
5358 free_expdef(ed);
5359 }
5360 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005361 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005362 while (cstk != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005363 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005364 free_macros();
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005365 while (istk != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005366 Include *i = istk;
5367 istk = istk->next;
5368 fclose(i->fp);
5369 nasm_free(i->fname);
5370 nasm_free(i);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005371 while (i->expansion != NULL) {
5372 ExpInv *ei = i->expansion;
5373 i->expansion = ei->prev;
5374 free_expinv(ei);
5375 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005376 }
5377 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005378 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005379 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005380 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005381 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005382 free_llist(predef);
5383 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005384 while ((i = ipath)) {
5385 ipath = i->next;
5386 if (i->path)
5387 nasm_free(i->path);
5388 nasm_free(i);
5389 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005390 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005391}
5392
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005393void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005394{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005395 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005396
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005397 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005398 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005399 i->next = NULL;
5400
H. Peter Anvin89cee572009-07-15 09:16:54 -04005401 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005402 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005403 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005404 j = j->next;
5405 j->next = i;
5406 } else {
5407 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005408 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005409}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005410
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005411void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005412{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005413 Token *inc, *space, *name;
5414 Line *l;
5415
H. Peter Anvin734b1882002-04-30 21:01:08 +00005416 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5417 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5418 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005419
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005420 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005421 l->next = predef;
5422 l->first = inc;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005423 predef = l;
5424}
5425
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005426void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005427{
5428 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005429 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005430 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005431
5432 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005433 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5434 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005435 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005436 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005437 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005438 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005439 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005440
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005441 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005442 l->next = predef;
5443 l->first = def;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005444 predef = l;
5445}
5446
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005447void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005448{
5449 Token *def, *space;
5450 Line *l;
5451
H. Peter Anvin734b1882002-04-30 21:01:08 +00005452 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5453 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005454 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005455
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005456 l = new_Line();
H. Peter Anvin620515a2002-04-30 20:57:38 +00005457 l->next = predef;
5458 l->first = def;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005459 predef = l;
5460}
5461
Keith Kaniosb7a89542007-04-12 02:40:54 +00005462/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005463 * This function is used to assist with "runtime" preprocessor
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005464 * directives, e.g. pp_runtime("%define __BITS__ 64");
Keith Kaniosb7a89542007-04-12 02:40:54 +00005465 *
5466 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5467 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5468 */
5469
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005470void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005471{
5472 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005473
Keith Kaniosb7a89542007-04-12 02:40:54 +00005474 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005475 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005476 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005477
Keith Kaniosb7a89542007-04-12 02:40:54 +00005478}
5479
H. Peter Anvina70547f2008-07-19 21:44:26 -07005480void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005481{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005482 extrastdmac = macros;
5483}
5484
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005485static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005486{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005487 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005488 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005489 tok->text = nasm_strdup(numbuf);
5490 tok->type = TOK_NUMBER;
5491}
5492
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005493Preproc nasmpp = {
5494 pp_reset,
5495 pp_getline,
5496 pp_cleanup
5497};