blob: 76b0ac1647d879027b811cbc1a07fcc0bbf9a5ca [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +04003 * Copyright 1996-2011 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000065#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000066#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000067#include <stdlib.h>
68#include <stddef.h>
69#include <string.h>
70#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000071#include <limits.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000072#include <inttypes.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000073
74#include "nasm.h"
75#include "nasmlib.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000076#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070077#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070078#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070079#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070080#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070081#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070082#include "tables.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000083
84typedef struct SMacro SMacro;
Keith Kaniosb307a4f2010-11-06 17:41:51 -050085typedef struct ExpDef ExpDef;
86typedef struct ExpInv ExpInv;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000087typedef struct Context Context;
88typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000089typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000090typedef struct Line Line;
91typedef struct Include Include;
92typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000093typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000094
95/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070096 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
102 */
103
104/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 * Store the definition of a single-line macro.
106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107struct SMacro {
Cyrill Gorcunov10083ae2011-07-17 20:06:20 +0400108 SMacro *next;
109 char *name;
110 Token *expansion;
111 unsigned int nparam;
112 bool casesense;
113 bool in_progress;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000114};
115
116/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000117 * The context stack is composed of a linked list of these.
118 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000119struct Context {
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400120 Context *next;
121 char *name;
122 struct hash_table localmac;
123 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 {
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +0400146 TOK_NONE = 0,
147 TOK_WHITESPACE,
148 TOK_COMMENT,
149 TOK_ID,
150 TOK_PREPROC_ID,
151 TOK_STRING,
152 TOK_NUMBER,
153 TOK_FLOAT,
154 TOK_SMAC_END,
155 TOK_OTHER,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700156 TOK_INTERNAL_STRING,
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +0400157 TOK_PREPROC_Q,
158 TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300159 TOK_PASTE, /* %+ */
160 TOK_INDIRECT, /* %[...] */
161 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
162 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000163};
164
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400165#define PP_CONCAT_MASK(x) (1 << (x))
166
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400167struct tokseq_match {
168 int mask_head;
169 int mask_tail;
170};
171
H. Peter Anvine2c80182005-01-15 22:15:51 +0000172struct Token {
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400173 Token *next;
174 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700175 union {
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400176 SMacro *mac; /* associated macro for TOK_SMAC_END */
177 size_t len; /* scratch length field */
178 } a; /* Auxiliary data */
179 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000180};
181
182/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500183 * Expansion definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000184 * these, which is essentially a container to allow several linked
185 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700186 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000187 * Note that in this module, linked lists are treated as stacks
188 * wherever possible. For this reason, Lines are _pushed_ on to the
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500189 * `last' field in ExpDef structures, so that the linked list,
190 * if walked, would emit the expansion lines in the proper order.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000191 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000192struct Line {
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400193 Line *next;
194 Token *first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000195};
196
197/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500198 * Expansion Types
199 */
200enum pp_exp_type {
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +0400201 EXP_NONE = 0,
202 EXP_PREDEF,
203 EXP_MMACRO,
204 EXP_REP,
205 EXP_IF,
206 EXP_WHILE,
207 EXP_COMMENT,
208 EXP_FINAL,
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500209 EXP_MAX = INT_MAX /* Keep compiler from reducing the range */
210};
211
212/*
213 * Store the definition of an expansion, in which is any
214 * preprocessor directive that has an ending pair.
215 *
216 * This design allows for arbitrary expansion/recursion depth,
217 * upto the DEADMAN_LIMIT.
218 *
219 * The `next' field is used for storing ExpDef in hash tables; the
220 * `prev' field is for the global `expansions` linked-list.
221 */
222struct ExpDef {
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400223 ExpDef *prev; /* previous definition */
224 ExpDef *next; /* next in hash table */
225 enum pp_exp_type type; /* expansion type */
226 char *name; /* definition name */
227 int nparam_min;
228 int nparam_max;
229 bool casesense;
230 bool plus; /* is the last parameter greedy? */
231 bool nolist; /* is this expansion listing-inhibited? */
232 Token *dlist; /* all defaults as one list */
233 Token **defaults; /* parameter default pointers */
234 int ndefs; /* number of default parameters */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300235
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400236 int prepend; /* label prepend state */
237 Line *label;
238 Line *line;
239 Line *last;
240 int linecount; /* number of lines within expansion */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300241
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400242 int64_t def_depth; /* current number of definition pairs deep */
243 int64_t cur_depth; /* current number of expansions */
244 int64_t max_depth; /* maximum number of expansions allowed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300245
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400246 int state; /* condition state */
247 bool ignoring; /* ignoring definition lines */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500248};
249
250/*
251 * Store the invocation of an expansion.
252 *
253 * The `prev' field is for the `istk->expansion` linked-list.
254 *
255 * When an expansion is being expanded, `params', `iline', `nparam',
256 * `paramlen', `rotate' and `unique' are local to the invocation.
257 */
258struct ExpInv {
Cyrill Gorcunovd57a0312011-07-17 20:11:08 +0400259 ExpInv *prev; /* previous invocation */
260 ExpDef *def; /* pointer to expansion definition */
261 char *name; /* invocation name */
262 Line *label; /* pointer to label */
263 char *label_text; /* pointer to label text */
264 Line *current; /* pointer to current line in invocation */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300265
Cyrill Gorcunovd57a0312011-07-17 20:11:08 +0400266 Token **params; /* actual parameters */
267 Token *iline; /* invocation line */
268 int *paramlen;
269 unsigned int nparam;
270 unsigned int rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300271
Cyrill Gorcunovd57a0312011-07-17 20:11:08 +0400272 uint64_t unique;
273 int lineno; /* current line number in expansion */
274 int linnum; /* line number at invocation */
275 int relno; /* relative line number at invocation */
276 enum pp_exp_type type; /* expansion type */
277 bool emitting;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500278};
279
280/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000281 * To handle an arbitrary level of file inclusion, we maintain a
282 * stack (ie linked list) of these things.
283 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000284struct Include {
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400285 Include *next;
286 FILE *fp;
287 Cond *conds;
288 ExpInv *expansion;
289 char *fname;
290 int lineno;
291 int lineinc;
292 int mmac_depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000293};
294
295/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000296 * Include search path. This is simply a list of strings which get
297 * prepended, in turn, to the name of an include file, in an
298 * attempt to find the file if it's not in the current directory.
299 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000300struct IncPath {
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400301 IncPath *next;
302 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000303};
304
305/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000306 * Conditional assembly: we maintain a separate stack of these for
307 * each level of file inclusion. (The only reason we keep the
308 * stacks separate is to ensure that a stray `%endif' in a file
309 * included from within the true branch of a `%if' won't terminate
310 * it and cause confusion: instead, rightly, it'll cause an error.)
311 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000312enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000313 /*
314 * These states are for use just after %if or %elif: IF_TRUE
315 * means the condition has evaluated to truth so we are
316 * currently emitting, whereas IF_FALSE means we are not
317 * currently emitting but will start doing so if a %else comes
318 * up. In these states, all directives are admissible: %elif,
319 * %else and %endif. (And of course %if.)
320 */
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +0400321 COND_IF_TRUE,
322 COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000323 /*
324 * These states come up after a %else: ELSE_TRUE means we're
325 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
326 * any %elif or %else will cause an error.
327 */
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +0400328 COND_ELSE_TRUE,
329 COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000330 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200331 * These states mean that we're not emitting now, and also that
332 * nothing until %endif will be emitted at all. COND_DONE is
333 * used when we've had our moment of emission
334 * and have now started seeing %elifs. COND_NEVER is used when
335 * the condition construct in question is contained within a
336 * non-emitting branch of a larger condition construct,
337 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000338 */
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +0400339 COND_DONE,
340 COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341};
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000342
H. Peter Anvin70653092007-10-19 14:42:29 -0700343/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000344 * These defines are used as the possible return values for do_directive
345 */
346#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300347#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000348
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000349/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500350 * This define sets the upper limit for smacro and expansions
Keith Kanios852f1ee2009-07-12 00:19:55 -0500351 */
352#define DEADMAN_LIMIT (1 << 20)
353
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400354/* max reps */
355#define REP_LIMIT ((INT64_C(1) << 62))
356
Keith Kanios852f1ee2009-07-12 00:19:55 -0500357/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000358 * Condition codes. Note that we use c_ prefix not C_ because C_ is
359 * used in nasm.h for the "real" condition codes. At _this_ level,
360 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
361 * ones, so we need a different enum...
362 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700363static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000364 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
365 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000366 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000367};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700368enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000369 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
370 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 -0700371 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
372 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000373};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700374static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000375 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
376 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 +0000377 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000378};
379
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000380/* For TASM compatibility we need to be able to recognise TASM compatible
381 * conditional compilation directives. Using the NASM pre-processor does
382 * not work, so we look for them specifically from the following list and
383 * then jam in the equivalent NASM directive into the input stream.
384 */
385
H. Peter Anvine2c80182005-01-15 22:15:51 +0000386enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000387 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
388 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
389};
390
H. Peter Anvin476d2862007-10-02 22:04:15 -0700391static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000392 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
393 "ifndef", "include", "local"
394};
395
396static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000397static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000398static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800399static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000400
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000401static Context *cstk;
402static Include *istk;
Cyrill Gorcunov5c607762011-10-09 19:04:14 +0400403static IncPath *ipath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000404
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300405static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700406static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000407
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300408static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000409
Cyrill Gorcunov5c607762011-10-09 19:04:14 +0400410static Line *predef;
H. Peter Anvind2456592008-06-19 15:04:18 -0700411static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000412
413static ListGen *list;
414
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000415/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500416 * The current set of expansion definitions we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000417 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500418static struct hash_table expdefs;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000419
420/*
421 * The current set of single-line macros we have defined.
422 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700423static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000424
425/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500426 * Linked List of all active expansion definitions
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000427 */
Cyrill Gorcunov5c607762011-10-09 19:04:14 +0400428struct ExpDef *expansions;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500429
430/*
431 * The expansion we are currently defining
432 */
Cyrill Gorcunov5c607762011-10-09 19:04:14 +0400433static ExpDef *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000434
Charles Crayned4200be2008-07-12 16:42:33 -0700435static uint64_t nested_mac_count;
436static uint64_t nested_rep_count;
437
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000438/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500439 * Linked-list of lines to preprocess, prior to cleanup
440 */
Cyrill Gorcunov5c607762011-10-09 19:04:14 +0400441static Line *finals;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500442static bool in_final = false;
443
444/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000445 * The number of macro parameters to allocate space for at a time.
446 */
447#define PARAM_DELTA 16
448
449/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700450 * The standard macro set: defined in macros.c in the array nasm_stdmac.
451 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000452 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700453static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000454
455/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000456 * The extra standard macros that come from the object format, if
457 * any.
458 */
Cyrill Gorcunov5c607762011-10-09 19:04:14 +0400459static macros_t *extrastdmac;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700460static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000461
462/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000463 * Tokens are allocated in blocks to improve speed
464 */
465#define TOKEN_BLOCKSIZE 4096
Cyrill Gorcunov5c607762011-10-09 19:04:14 +0400466static Token *freeTokens;
H. Peter Anvince616072002-04-30 21:02:23 +0000467struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000468 Blocks *next;
469 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000470};
471
Cyrill Gorcunov5c607762011-10-09 19:04:14 +0400472static Blocks blocks;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000473
474/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000475 * Forward declarations.
476 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000477static Token *expand_mmac_params(Token * tline);
478static Token *expand_smacro(Token * tline);
479static Token *expand_id(Token * tline);
Cyrill Gorcunov290eac72011-06-28 01:59:05 +0400480static Context *get_ctx(const char *name, const char **namep);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700481static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000482static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200483static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000484static void *new_Block(size_t size);
485static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700486static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300487 const char *text, int txtlen);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500488static Token *copy_Token(Token * tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000489static Token *delete_Token(Token * t);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500490static Line *new_Line(void);
491static ExpDef *new_ExpDef(int exp_type);
492static ExpInv *new_ExpInv(int exp_type, ExpDef *ed);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000493
494/*
495 * Macros for safe checking of token pointers, avoid *(NULL)
496 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300497#define tok_type_(x,t) ((x) && (x)->type == (t))
498#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
499#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
500#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000501
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400502/*
503 * A few helpers for single macros
504 */
505
506/* We might be not smacro parameter at all */
507static bool is_smacro_param(Token *t)
508{
509 return t->type >= TOK_SMAC_PARAM;
510}
511
512/* smacro parameters are counted in a special way */
513static int smacro_get_param_idx(Token *t)
514{
515 return t->type - TOK_SMAC_PARAM;
516}
517
518/* encode smacro parameter index */
519static int smacro_set_param_idx(Token *t, unsigned int index)
520{
521 return t->type = TOK_SMAC_PARAM + index;
522}
523
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300524#ifdef NASM_TRACE
525
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400526#define stringify(x) #x
527
Cyrill Gorcunov9d1141a2011-06-26 23:07:35 +0400528#define nasm_trace(msg, ...) printf("(%s:%d): " msg "\n", __func__, __LINE__, ##__VA_ARGS__)
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400529#define nasm_dump_token(t) nasm_raw_dump_token(t, __FILE__, __LINE__, __func__);
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400530#define nasm_dump_stream(t) nasm_raw_dump_stream(t, __FILE__, __LINE__, __func__);
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400531
532/* FIXME: we really need some compound type here instead of inplace code */
533static const char *nasm_get_tok_type_str(enum pp_token_type type)
534{
535#define SWITCH_TOK_NAME(type) \
536 case (type): \
537 return stringify(type)
538
539 switch (type) {
540 SWITCH_TOK_NAME(TOK_NONE);
541 SWITCH_TOK_NAME(TOK_WHITESPACE);
542 SWITCH_TOK_NAME(TOK_COMMENT);
543 SWITCH_TOK_NAME(TOK_ID);
544 SWITCH_TOK_NAME(TOK_PREPROC_ID);
545 SWITCH_TOK_NAME(TOK_STRING);
546 SWITCH_TOK_NAME(TOK_NUMBER);
547 SWITCH_TOK_NAME(TOK_FLOAT);
548 SWITCH_TOK_NAME(TOK_SMAC_END);
549 SWITCH_TOK_NAME(TOK_OTHER);
550 SWITCH_TOK_NAME(TOK_INTERNAL_STRING);
551 SWITCH_TOK_NAME(TOK_PREPROC_Q);
552 SWITCH_TOK_NAME(TOK_PREPROC_QQ);
553 SWITCH_TOK_NAME(TOK_PASTE);
554 SWITCH_TOK_NAME(TOK_INDIRECT);
555 SWITCH_TOK_NAME(TOK_SMAC_PARAM);
556 SWITCH_TOK_NAME(TOK_MAX);
557 }
558
559 return NULL;
560}
561
562static void nasm_raw_dump_token(Token *token, const char *file, int line, const char *func)
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300563{
564 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
565 if (token) {
566 Token *t;
567 list_for_each(t, token) {
568 if (t->text)
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400569 printf("'%s'(%s) ", t->text,
570 nasm_get_tok_type_str(t->type));
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300571 }
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400572 printf("\n\n");
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300573 }
574}
575
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400576static void nasm_raw_dump_stream(Token *token, const char *file, int line, const char *func)
577{
578 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
579 if (token) {
580 Token *t;
581 list_for_each(t, token)
582 printf("%s", t->text ? t->text : " ");
583 printf("\n\n");
584 }
585}
586
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400587#else
588#define nasm_trace(msg, ...)
589#define nasm_dump_token(t)
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400590#define nasm_dump_stream(t)
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300591#endif
592
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300593/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700594 * nasm_unquote with error if the string contains NUL characters.
595 * If the string contains NUL characters, issue an error and return
596 * the C len, i.e. truncate at the NUL.
597 */
598static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
599{
600 size_t len = nasm_unquote(qstr, NULL);
601 size_t clen = strlen(qstr);
602
603 if (len != clen)
604 error(ERR_NONFATAL, "NUL character in `%s' directive",
605 pp_directives[directive]);
606
607 return clen;
608}
609
610/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700611 * In-place reverse a list of tokens.
612 */
613static Token *reverse_tokens(Token *t)
614{
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400615 Token *prev, *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700616
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400617 list_reverse(t, prev, next);
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700618
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400619 return t;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700620}
621
622/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300623 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000624 * front of them. We do it here because I could not find any other
625 * place to do it for the moment, and it is a hack (ideally it would
626 * be nice to be able to use the NASM pre-processor to do it).
627 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000628static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000629{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000630 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400631 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000632
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400633 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000634
635 /* Binary search for the directive name */
636 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400637 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400638 q = nasm_skip_word(p);
639 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000640 if (len) {
641 oldchar = p[len];
642 p[len] = 0;
643 while (j - i > 1) {
644 k = (j + i) / 2;
645 m = nasm_stricmp(p, tasm_directives[k]);
646 if (m == 0) {
647 /* We have found a directive, so jam a % in front of it
648 * so that NASM will then recognise it as one if it's own.
649 */
650 p[len] = oldchar;
651 len = strlen(p);
652 oldline = line;
653 line = nasm_malloc(len + 2);
654 line[0] = '%';
655 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700656 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300657 * NASM does not recognise IFDIFI, so we convert
658 * it to %if 0. This is not used in NASM
659 * compatible code, but does need to parse for the
660 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000661 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700662 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000663 } else {
664 memcpy(line + 1, p, len + 1);
665 }
666 nasm_free(oldline);
667 return line;
668 } else if (m < 0) {
669 j = k;
670 } else
671 i = k;
672 }
673 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000674 }
675 return line;
676}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000677
H. Peter Anvin76690a12002-04-30 20:52:49 +0000678/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000679 * The pre-preprocessing stage... This function translates line
680 * number indications as they emerge from GNU cpp (`# lineno "file"
681 * flags') into NASM preprocessor line number indications (`%line
682 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000683 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000684static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000685{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000686 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000687 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000688
H. Peter Anvine2c80182005-01-15 22:15:51 +0000689 if (line[0] == '#' && line[1] == ' ') {
690 oldline = line;
691 fname = oldline + 2;
692 lineno = atoi(fname);
693 fname += strspn(fname, "0123456789 ");
694 if (*fname == '"')
695 fname++;
696 fnlen = strcspn(fname, "\"");
697 line = nasm_malloc(20 + fnlen);
698 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
699 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000700 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000701 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000702 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000703 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000704}
705
706/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000707 * Free a linked list of tokens.
708 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000709static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000710{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400711 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000712 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000713}
714
715/*
716 * Free a linked list of lines.
717 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000718static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000719{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400720 Line *l, *tmp;
721 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000722 free_tlist(l->first);
723 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000724 }
725}
726
727/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500728 * Free an ExpDef
H. Peter Anvineba20a72002-04-30 20:53:55 +0000729 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500730static void free_expdef(ExpDef * ed)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000731{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500732 nasm_free(ed->name);
733 free_tlist(ed->dlist);
734 nasm_free(ed->defaults);
735 free_llist(ed->line);
736 nasm_free(ed);
737}
738
739/*
740 * Free an ExpInv
741 */
742static void free_expinv(ExpInv * ei)
743{
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +0400744 nasm_free(ei->name);
745 nasm_free(ei->label_text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300746 nasm_free(ei);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000747}
748
749/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700750 * Free all currently defined macros, and free the hash tables
751 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700752static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700753{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400754 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700755 const char *key;
756 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700757
H. Peter Anvin072771e2008-05-22 13:17:51 -0700758 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300759 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400760 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300761 nasm_free(s->name);
762 free_tlist(s->expansion);
763 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300764 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700765 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700766 hash_free(smt);
767}
768
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500769static void free_expdef_table(struct hash_table *edt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700770{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500771 ExpDef *ed, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700772 const char *key;
773 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700774
775 it = NULL;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500776 while ((ed = hash_iterate(edt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300777 nasm_free((void *)key);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500778 list_for_each_safe(ed ,tmp, ed)
779 free_expdef(ed);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700780 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500781 hash_free(edt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700782}
783
784static void free_macros(void)
785{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700786 free_smacro_table(&smacros);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500787 free_expdef_table(&expdefs);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700788}
789
790/*
791 * Initialize the hash tables
792 */
793static void init_macros(void)
794{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700795 hash_init(&smacros, HASH_LARGE);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500796 hash_init(&expdefs, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700797}
798
799/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000800 * Pop the context stack.
801 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000802static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000803{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000804 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000805
806 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700807 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000808 nasm_free(c->name);
809 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000810}
811
H. Peter Anvin072771e2008-05-22 13:17:51 -0700812/*
813 * Search for a key in the hash index; adding it if necessary
814 * (in which case we initialize the data pointer to NULL.)
815 */
816static void **
817hash_findi_add(struct hash_table *hash, const char *str)
818{
819 struct hash_insert hi;
820 void **r;
821 char *strx;
822
823 r = hash_findi(hash, str, &hi);
824 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300825 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700826
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300827 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700828 return hash_add(&hi, strx, NULL);
829}
830
831/*
832 * Like hash_findi, but returns the data element rather than a pointer
833 * to it. Used only when not adding a new element, hence no third
834 * argument.
835 */
836static void *
837hash_findix(struct hash_table *hash, const char *str)
838{
839 void **p;
840
841 p = hash_findi(hash, str, NULL);
842 return p ? *p : NULL;
843}
844
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400845/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500846 * read line from standard macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400847 * if there no more left -- return NULL
848 */
849static char *line_from_stdmac(void)
850{
851 unsigned char c;
852 const unsigned char *p = stdmacpos;
853 char *line, *q;
854 size_t len = 0;
855
856 if (!stdmacpos)
857 return NULL;
858
859 while ((c = *p++)) {
860 if (c >= 0x80)
861 len += pp_directives_len[c - 0x80] + 1;
862 else
863 len++;
864 }
865
866 line = nasm_malloc(len + 1);
867 q = line;
868 while ((c = *stdmacpos++)) {
869 if (c >= 0x80) {
870 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
871 q += pp_directives_len[c - 0x80];
872 *q++ = ' ';
873 } else {
874 *q++ = c;
875 }
876 }
877 stdmacpos = p;
878 *q = '\0';
879
880 if (!*stdmacpos) {
881 /* This was the last of the standard macro chain... */
882 stdmacpos = NULL;
883 if (any_extrastdmac) {
884 stdmacpos = extrastdmac;
885 any_extrastdmac = false;
886 } else if (do_predef) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300887 ExpInv *ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400888 Line *pd, *l;
889 Token *head, **tail, *t;
890
891 /*
892 * Nasty hack: here we push the contents of
893 * `predef' on to the top-level expansion stack,
894 * since this is the most convenient way to
895 * implement the pre-include and pre-define
896 * features.
897 */
898 list_for_each(pd, predef) {
899 head = NULL;
900 tail = &head;
901 list_for_each(t, pd->first) {
902 *tail = new_Token(NULL, t->type, t->text, 0);
903 tail = &(*tail)->next;
904 }
905
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500906 l = new_Line();
907 l->first = head;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300908 ei = new_ExpInv(EXP_PREDEF, NULL);
909 ei->current = l;
910 ei->emitting = true;
911 ei->prev = istk->expansion;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500912 istk->expansion = ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400913 }
914 do_predef = false;
915 }
916 }
917
918 return line;
919}
920
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000921#define BUF_DELTA 512
922/*
923 * Read a line from the top file in istk, handling multiple CR/LFs
924 * at the end of the line read, and handling spurious ^Zs. Will
925 * return lines from the standard macro set if this has not already
926 * been done.
927 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000928static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000929{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000930 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000931 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000932
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400933 /*
934 * standart macros set (predefined) goes first
935 */
936 p = line_from_stdmac();
937 if (p)
938 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700939
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400940 /*
941 * regular read from a file
942 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000943 bufsize = BUF_DELTA;
944 buffer = nasm_malloc(BUF_DELTA);
945 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000946 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000947 while (1) {
948 q = fgets(p, bufsize - (p - buffer), istk->fp);
949 if (!q)
950 break;
951 p += strlen(p);
952 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300953 /*
954 * Convert backslash-CRLF line continuation sequences into
955 * nothing at all (for DOS and Windows)
956 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000957 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
958 p -= 3;
959 *p = 0;
960 continued_count++;
961 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300962 /*
963 * Also convert backslash-LF line continuation sequences into
964 * nothing at all (for Unix)
965 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
967 p -= 2;
968 *p = 0;
969 continued_count++;
970 } else {
971 break;
972 }
973 }
974 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000975 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000976 bufsize += BUF_DELTA;
977 buffer = nasm_realloc(buffer, bufsize);
978 p = buffer + offset; /* prevent stale-pointer problems */
979 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000980 }
981
H. Peter Anvine2c80182005-01-15 22:15:51 +0000982 if (!q && p == buffer) {
983 nasm_free(buffer);
984 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000985 }
986
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300987 src_set_linnum(src_get_linnum() + istk->lineinc +
988 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000989
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000990 /*
991 * Play safe: remove CRs as well as LFs, if any of either are
992 * present at the end of the line.
993 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000994 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000996
997 /*
998 * Handle spurious ^Z, which may be inserted into source files
999 * by some file transfer utilities.
1000 */
1001 buffer[strcspn(buffer, "\032")] = '\0';
1002
H. Peter Anvin734b1882002-04-30 21:01:08 +00001003 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001004
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001005 return buffer;
1006}
1007
1008/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00001009 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001010 * don't need to parse the value out of e.g. numeric tokens: we
1011 * simply split one string into many.
1012 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001013static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001014{
H. Peter Anvinca544db2008-10-19 19:30:11 -07001015 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001016 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001017 Token *list = NULL;
1018 Token *t, **tail = &list;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001019 bool verbose = true;
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +04001020
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +04001021 nasm_trace("Tokenize for '%s'", line);
1022
Keith Kanios6faad4e2010-12-18 14:08:02 -06001023 if ((defining != NULL) && (defining->ignoring == true)) {
1024 verbose = false;
1025 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001026
H. Peter Anvine2c80182005-01-15 22:15:51 +00001027 while (*line) {
1028 p = line;
1029 if (*p == '%') {
1030 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001031 if (*p == '+' && !nasm_isdigit(p[1])) {
1032 p++;
1033 type = TOK_PASTE;
1034 } else if (nasm_isdigit(*p) ||
1035 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001036 do {
1037 p++;
1038 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001039 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001040 type = TOK_PREPROC_ID;
1041 } else if (*p == '{') {
1042 p++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001043 while (*p && *p != '}') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001044 p[-1] = *p;
1045 p++;
1046 }
1047 p[-1] = '\0';
1048 if (*p)
1049 p++;
1050 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001051 } else if (*p == '[') {
1052 int lvl = 1;
1053 line += 2; /* Skip the leading %[ */
1054 p++;
1055 while (lvl && (c = *p++)) {
1056 switch (c) {
1057 case ']':
1058 lvl--;
1059 break;
1060 case '%':
1061 if (*p == '[')
1062 lvl++;
1063 break;
1064 case '\'':
1065 case '\"':
1066 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +04001067 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001068 break;
1069 default:
1070 break;
1071 }
1072 }
1073 p--;
1074 if (*p)
1075 *p++ = '\0';
Keith Kanios6faad4e2010-12-18 14:08:02 -06001076 if (lvl && verbose)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001077 error(ERR_NONFATAL, "unterminated %[ construct");
1078 type = TOK_INDIRECT;
1079 } else if (*p == '?') {
1080 type = TOK_PREPROC_Q; /* %? */
1081 p++;
1082 if (*p == '?') {
1083 type = TOK_PREPROC_QQ; /* %?? */
1084 p++;
1085 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001086 } else if (*p == '!') {
1087 type = TOK_PREPROC_ID;
1088 p++;
1089 if (isidchar(*p)) {
1090 do {
1091 p++;
1092 } while (isidchar(*p));
1093 } else if (*p == '\'' || *p == '\"' || *p == '`') {
1094 p = nasm_skip_string(p);
1095 if (*p)
1096 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001097 else if(verbose)
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001098 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
1099 } else {
1100 /* %! without string or identifier */
1101 type = TOK_OTHER; /* Legacy behavior... */
1102 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001103 } else if (isidchar(*p) ||
1104 ((*p == '!' || *p == '%' || *p == '$') &&
1105 isidchar(p[1]))) {
1106 do {
1107 p++;
1108 }
1109 while (isidchar(*p));
1110 type = TOK_PREPROC_ID;
1111 } else {
1112 type = TOK_OTHER;
1113 if (*p == '%')
1114 p++;
1115 }
1116 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1117 type = TOK_ID;
1118 p++;
1119 while (*p && isidchar(*p))
1120 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001121 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001122 /*
1123 * A string token.
1124 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001125 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001126 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001127
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128 if (*p) {
1129 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001130 } else if(verbose) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001131 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 /* Handling unterminated strings by UNV */
1133 /* type = -1; */
1134 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001135 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001136 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001137 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001138 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001139 bool is_hex = false;
1140 bool is_float = false;
1141 bool has_e = false;
1142 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001143
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001145 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001146 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001147
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001148 if (*p == '$') {
1149 p++;
1150 is_hex = true;
1151 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001152
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001153 for (;;) {
1154 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001155
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001156 if (!is_hex && (c == 'e' || c == 'E')) {
1157 has_e = true;
1158 if (*p == '+' || *p == '-') {
1159 /*
1160 * e can only be followed by +/- if it is either a
1161 * prefixed hex number or a floating-point number
1162 */
1163 p++;
1164 is_float = true;
1165 }
1166 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1167 is_hex = true;
1168 } else if (c == 'P' || c == 'p') {
1169 is_float = true;
1170 if (*p == '+' || *p == '-')
1171 p++;
1172 } else if (isnumchar(c) || c == '_')
1173 ; /* just advance */
1174 else if (c == '.') {
1175 /*
1176 * we need to deal with consequences of the legacy
1177 * parser, like "1.nolist" being two tokens
1178 * (TOK_NUMBER, TOK_ID) here; at least give it
1179 * a shot for now. In the future, we probably need
1180 * a flex-based scanner with proper pattern matching
1181 * to do it as well as it can be done. Nothing in
1182 * the world is going to help the person who wants
1183 * 0x123.p16 interpreted as two tokens, though.
1184 */
1185 r = p;
1186 while (*r == '_')
1187 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001188
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001189 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1190 (!is_hex && (*r == 'e' || *r == 'E')) ||
1191 (*r == 'p' || *r == 'P')) {
1192 p = r;
1193 is_float = true;
1194 } else
1195 break; /* Terminate the token */
1196 } else
1197 break;
1198 }
1199 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001200
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001201 if (p == line+1 && *line == '$') {
1202 type = TOK_OTHER; /* TOKEN_HERE */
1203 } else {
1204 if (has_e && !is_hex) {
1205 /* 1e13 is floating-point, but 1e13h is not */
1206 is_float = true;
1207 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001208
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001209 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1210 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001211 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001212 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001213 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001214 /*
1215 * Whitespace just before end-of-line is discarded by
1216 * pretending it's a comment; whitespace just before a
1217 * comment gets lumped into the comment.
1218 */
1219 if (!*p || *p == ';') {
1220 type = TOK_COMMENT;
1221 while (*p)
1222 p++;
1223 }
1224 } else if (*p == ';') {
1225 type = TOK_COMMENT;
1226 while (*p)
1227 p++;
1228 } else {
1229 /*
1230 * Anything else is an operator of some kind. We check
1231 * for all the double-character operators (>>, <<, //,
1232 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001233 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001234 */
1235 type = TOK_OTHER;
1236 if ((p[0] == '>' && p[1] == '>') ||
1237 (p[0] == '<' && p[1] == '<') ||
1238 (p[0] == '/' && p[1] == '/') ||
1239 (p[0] == '<' && p[1] == '=') ||
1240 (p[0] == '>' && p[1] == '=') ||
1241 (p[0] == '=' && p[1] == '=') ||
1242 (p[0] == '!' && p[1] == '=') ||
1243 (p[0] == '<' && p[1] == '>') ||
1244 (p[0] == '&' && p[1] == '&') ||
1245 (p[0] == '|' && p[1] == '|') ||
1246 (p[0] == '^' && p[1] == '^')) {
1247 p++;
1248 }
1249 p++;
1250 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001251
H. Peter Anvine2c80182005-01-15 22:15:51 +00001252 /* Handling unterminated string by UNV */
1253 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001254 {
1255 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1256 t->text[p-line] = *line;
1257 tail = &t->next;
1258 }
1259 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001260 if (type != TOK_COMMENT) {
1261 *tail = t = new_Token(NULL, type, line, p - line);
1262 tail = &t->next;
1263 }
1264 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001265 }
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +04001266
1267 nasm_dump_token(list);
1268
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001269 return list;
1270}
1271
H. Peter Anvince616072002-04-30 21:02:23 +00001272/*
1273 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001274 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001275 * deleted only all at once by the delete_Blocks function.
1276 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001277static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001278{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001279 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001280
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001281 /* first, get to the end of the linked list */
1282 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001283 b = b->next;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001284
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001285 /* now allocate the requested chunk */
1286 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001287
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001288 /* now allocate a new block for the next request */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001289 b->next = nasm_zalloc(sizeof(Blocks));
1290
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001291 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001292}
1293
1294/*
1295 * this function deletes all managed blocks of memory
1296 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001297static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001298{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001300
H. Peter Anvin70653092007-10-19 14:42:29 -07001301 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001302 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001303 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001304 * free it.
1305 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001306 while (b) {
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04001307 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001308 a = b;
1309 b = b->next;
1310 if (a != &blocks)
1311 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001312 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001314
1315/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001316 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001317 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001318 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001319 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001320static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001321 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001322{
1323 Token *t;
1324 int i;
1325
H. Peter Anvin89cee572009-07-15 09:16:54 -04001326 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1328 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1329 freeTokens[i].next = &freeTokens[i + 1];
1330 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001331 }
1332 t = freeTokens;
1333 freeTokens = t->next;
1334 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001335 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001336 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001337 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001338 t->text = NULL;
1339 } else {
1340 if (txtlen == 0)
1341 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001342 t->text = nasm_malloc(txtlen+1);
1343 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001344 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001345 }
1346 return t;
1347}
1348
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001349static Token *copy_Token(Token * tline)
1350{
1351 Token *t, *tt, *first = NULL, *prev = NULL;
1352 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001353 for (tt = tline; tt != NULL; tt = tt->next) {
1354 if (!freeTokens) {
1355 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1356 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1357 freeTokens[i].next = &freeTokens[i + 1];
1358 freeTokens[i].next = NULL;
1359 }
1360 t = freeTokens;
1361 freeTokens = t->next;
1362 t->next = NULL;
1363 t->text = tt->text ? nasm_strdup(tt->text) : NULL;
1364 t->a.mac = tt->a.mac;
1365 t->a.len = tt->a.len;
1366 t->type = tt->type;
1367 if (prev != NULL) {
1368 prev->next = t;
1369 } else {
1370 first = t;
1371 }
1372 prev = t;
1373 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001374 return first;
1375}
1376
H. Peter Anvine2c80182005-01-15 22:15:51 +00001377static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001378{
1379 Token *next = t->next;
1380 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001381 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001382 freeTokens = t;
1383 return next;
1384}
1385
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001386/*
1387 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001388 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1389 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001390 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001391static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001392{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001393 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001394 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001395 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001396 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001397
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001398 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001399 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001400 char *v;
1401 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001402
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001403 v = t->text + 2;
1404 if (*v == '\'' || *v == '\"' || *v == '`') {
1405 size_t len = nasm_unquote(v, NULL);
1406 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001407
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001408 if (len != clen) {
1409 error(ERR_NONFATAL | ERR_PASS1,
1410 "NUL character in %! string");
1411 v = NULL;
1412 }
1413 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001414
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001415 if (v) {
1416 char *p = getenv(v);
1417 if (!p) {
1418 error(ERR_NONFATAL | ERR_PASS1,
1419 "nonexistent environment variable `%s'", v);
1420 p = "";
1421 }
1422 t->text = nasm_strdup(p);
1423 }
1424 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001425 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001426
H. Peter Anvine2c80182005-01-15 22:15:51 +00001427 /* Expand local macros here and not during preprocessing */
1428 if (expand_locals &&
1429 t->type == TOK_PREPROC_ID && t->text &&
1430 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001431 const char *q;
1432 char *p;
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001433 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001434 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001435 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001436 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 p = nasm_strcat(buffer, q);
1438 nasm_free(t->text);
1439 t->text = p;
1440 }
1441 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001442
1443 /* Expand %? and %?? directives */
Keith Kanios3136d482010-11-13 09:34:34 -06001444 if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001445 ((t->type == TOK_PREPROC_Q) ||
1446 (t->type == TOK_PREPROC_QQ))) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001447 ExpInv *ei;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001448 for (ei = istk->expansion; ei != NULL; ei = ei->prev){
1449 if (ei->type == EXP_MMACRO) {
1450 nasm_free(t->text);
1451 if (t->type == TOK_PREPROC_Q) {
1452 t->text = nasm_strdup(ei->name);
1453 } else {
1454 t->text = nasm_strdup(ei->def->name);
1455 }
1456 break;
1457 }
1458 }
1459 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001460
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001461 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001463 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001464 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001465 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001466
H. Peter Anvin734b1882002-04-30 21:01:08 +00001467 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001468
1469 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001470 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001471 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001472 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001473 q = t->text;
1474 while (*q)
1475 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001476 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001477 }
1478 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001479
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001480 return line;
1481}
1482
1483/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001484 * Initialize a new Line
1485 */
Cyrill Gorcunov29cb0bb2010-11-11 11:19:43 +03001486static inline Line *new_Line(void)
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001487{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001488 return (Line *)nasm_zalloc(sizeof(Line));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001489}
1490
1491
1492/*
1493 * Initialize a new Expansion Definition
1494 */
1495static ExpDef *new_ExpDef(int exp_type)
1496{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001497 ExpDef *ed = (ExpDef*)nasm_zalloc(sizeof(ExpDef));
1498 ed->type = exp_type;
1499 ed->casesense = true;
1500 ed->state = COND_NEVER;
1501
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001502 return ed;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001503}
1504
1505
1506/*
1507 * Initialize a new Expansion Instance
1508 */
1509static ExpInv *new_ExpInv(int exp_type, ExpDef *ed)
1510{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001511 ExpInv *ei = (ExpInv*)nasm_zalloc(sizeof(ExpInv));
1512 ei->type = exp_type;
1513 ei->def = ed;
1514 ei->unique = ++unique;
1515
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001516 if ((istk->mmac_depth < 1) &&
1517 (istk->expansion == NULL) &&
1518 (ed != NULL) &&
1519 (ed->type != EXP_MMACRO) &&
1520 (ed->type != EXP_REP) &&
1521 (ed->type != EXP_WHILE)) {
1522 ei->linnum = src_get_linnum();
1523 src_set_linnum(ei->linnum - ed->linecount - 1);
1524 } else {
1525 ei->linnum = -1;
1526 }
1527 if ((istk->expansion == NULL) ||
1528 (ei->type == EXP_MMACRO)) {
1529 ei->relno = 0;
1530 } else {
1531 ei->relno = istk->expansion->lineno;
1532 if (ed != NULL) {
1533 ei->relno -= (ed->linecount + 1);
1534 }
1535 }
1536 return ei;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001537}
1538
1539/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001540 * A scanner, suitable for use by the expression evaluator, which
1541 * operates on a line of Tokens. Expects a pointer to a pointer to
1542 * the first token in the line to be passed in as its private_data
1543 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001544 *
1545 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001546 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001547static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001548{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001549 Token **tlineptr = private_data;
1550 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001551 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001552
H. Peter Anvine2c80182005-01-15 22:15:51 +00001553 do {
1554 tline = *tlineptr;
1555 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001556 } while (tline && (tline->type == TOK_WHITESPACE ||
1557 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001558
1559 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001560 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001561
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001562 tokval->t_charptr = tline->text;
1563
H. Peter Anvin76690a12002-04-30 20:52:49 +00001564 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001565 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001566 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001567 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001568
H. Peter Anvine2c80182005-01-15 22:15:51 +00001569 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001570 p = tokval->t_charptr = tline->text;
1571 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001572 tokval->t_charptr++;
1573 return tokval->t_type = TOKEN_ID;
1574 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001575
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001576 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001577 if (r >= p+MAX_KEYWORD)
1578 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001579 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001580 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001581 *s = '\0';
1582 /* right, so we have an identifier sitting in temp storage. now,
1583 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001584 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001585 }
1586
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001588 bool rn_error;
1589 tokval->t_integer = readnum(tline->text, &rn_error);
1590 tokval->t_charptr = tline->text;
1591 if (rn_error)
1592 return tokval->t_type = TOKEN_ERRNUM;
1593 else
1594 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001595 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001596
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001597 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001598 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001599 }
1600
H. Peter Anvine2c80182005-01-15 22:15:51 +00001601 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001602 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001603
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001604 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001605 tokval->t_charptr = tline->text;
1606 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001607
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001608 if (ep[0] != bq || ep[1] != '\0')
1609 return tokval->t_type = TOKEN_ERRSTR;
1610 else
1611 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001612 }
1613
H. Peter Anvine2c80182005-01-15 22:15:51 +00001614 if (tline->type == TOK_OTHER) {
1615 if (!strcmp(tline->text, "<<"))
1616 return tokval->t_type = TOKEN_SHL;
1617 if (!strcmp(tline->text, ">>"))
1618 return tokval->t_type = TOKEN_SHR;
1619 if (!strcmp(tline->text, "//"))
1620 return tokval->t_type = TOKEN_SDIV;
1621 if (!strcmp(tline->text, "%%"))
1622 return tokval->t_type = TOKEN_SMOD;
1623 if (!strcmp(tline->text, "=="))
1624 return tokval->t_type = TOKEN_EQ;
1625 if (!strcmp(tline->text, "<>"))
1626 return tokval->t_type = TOKEN_NE;
1627 if (!strcmp(tline->text, "!="))
1628 return tokval->t_type = TOKEN_NE;
1629 if (!strcmp(tline->text, "<="))
1630 return tokval->t_type = TOKEN_LE;
1631 if (!strcmp(tline->text, ">="))
1632 return tokval->t_type = TOKEN_GE;
1633 if (!strcmp(tline->text, "&&"))
1634 return tokval->t_type = TOKEN_DBL_AND;
1635 if (!strcmp(tline->text, "^^"))
1636 return tokval->t_type = TOKEN_DBL_XOR;
1637 if (!strcmp(tline->text, "||"))
1638 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001639 }
1640
1641 /*
1642 * We have no other options: just return the first character of
1643 * the token text.
1644 */
1645 return tokval->t_type = tline->text[0];
1646}
1647
1648/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001649 * Compare a string to the name of an existing macro; this is a
1650 * simple wrapper which calls either strcmp or nasm_stricmp
1651 * depending on the value of the `casesense' parameter.
1652 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001653static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001654{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001655 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001656}
1657
1658/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001659 * Compare a string to the name of an existing macro; this is a
1660 * simple wrapper which calls either strcmp or nasm_stricmp
1661 * depending on the value of the `casesense' parameter.
1662 */
1663static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1664{
1665 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1666}
1667
1668/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001669 * Return the Context structure associated with a %$ token. Return
1670 * NULL, having _already_ reported an error condition, if the
1671 * context stack isn't deep enough for the supplied number of $
1672 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001673 *
1674 * If "namep" is non-NULL, set it to the pointer to the macro name
1675 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001676 */
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001677static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001678{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001679 Context *ctx;
1680 int i;
1681
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001682 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001683 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001684
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001685 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001687
H. Peter Anvine2c80182005-01-15 22:15:51 +00001688 if (!cstk) {
1689 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1690 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001691 }
1692
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001693 name += 2;
1694 ctx = cstk;
1695 i = 0;
1696 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001697 name++;
1698 i++;
1699 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001700 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001701
H. Peter Anvine2c80182005-01-15 22:15:51 +00001702 if (!ctx) {
1703 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001704 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001705 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001706 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001707
1708 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001709 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001710
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001711 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001712}
1713
1714/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001715 * Check to see if a file is already in a string list
1716 */
1717static bool in_list(const StrList *list, const char *str)
1718{
1719 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001720 if (!strcmp(list->str, str))
1721 return true;
1722 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001723 }
1724 return false;
1725}
1726
1727/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001728 * Open an include file. This routine must always return a valid
1729 * file pointer if it returns - it's responsible for throwing an
1730 * ERR_FATAL and bombing out completely if not. It should also try
1731 * the include path one by one until it finds the file or reaches
1732 * the end of the path.
1733 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001734static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001735 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001736{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001737 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001738 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001739 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001740 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001741 size_t prefix_len = 0;
1742 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001743
H. Peter Anvine2c80182005-01-15 22:15:51 +00001744 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001745 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunov6f38fe62010-11-11 11:32:16 +03001746 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001747 memcpy(sl->str, prefix, prefix_len);
1748 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001749 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001750 if (fp && dhead && !in_list(*dhead, sl->str)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001751 **dtail = sl;
1752 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001753 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001754 nasm_free(sl);
1755 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001756 if (fp)
1757 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001758 if (!ip) {
1759 if (!missing_ok)
1760 break;
1761 prefix = NULL;
1762 } else {
1763 prefix = ip->path;
1764 ip = ip->next;
1765 }
1766 if (prefix) {
1767 prefix_len = strlen(prefix);
1768 } else {
1769 /* -MG given and file not found */
1770 if (dhead && !in_list(*dhead, file)) {
1771 sl = nasm_malloc(len+1+sizeof sl->next);
1772 sl->next = NULL;
1773 strcpy(sl->str, file);
1774 **dtail = sl;
1775 *dtail = &sl->next;
1776 }
1777 return NULL;
1778 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001779 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001780
H. Peter Anvin734b1882002-04-30 21:01:08 +00001781 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001782 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001783}
1784
1785/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001786 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001787 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001788 * return true if _any_ single-line macro of that name is defined.
1789 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001790 * `nparam' or no parameters is defined.
1791 *
1792 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001793 * defined, or nparam is -1, the address of the definition structure
1794 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001795 * is NULL, no action will be taken regarding its contents, and no
1796 * error will occur.
1797 *
1798 * Note that this is also called with nparam zero to resolve
1799 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001800 *
1801 * If you already know which context macro belongs to, you can pass
1802 * the context pointer as first parameter; if you won't but name begins
1803 * with %$ the context will be automatically computed. If all_contexts
1804 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001805 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001806static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001807smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001808 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001809{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001810 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001811 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001812
H. Peter Anvin97a23472007-09-16 17:57:25 -07001813 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001814 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001815 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001816 if (cstk)
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001817 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001818 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001819 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001820 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001821 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001822 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001823 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001824 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001825
H. Peter Anvine2c80182005-01-15 22:15:51 +00001826 while (m) {
1827 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001828 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001829 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001830 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831 *defn = m;
1832 else
1833 *defn = NULL;
1834 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001835 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001836 }
1837 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001838 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001839
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001840 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001841}
1842
1843/*
1844 * Count and mark off the parameters in a multi-line macro call.
1845 * This is called both from within the multi-line macro expansion
1846 * code, and also to mark off the default parameters when provided
1847 * in a %macro definition line.
1848 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001849static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001850{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001851 int paramsize, brace;
1852
1853 *nparam = paramsize = 0;
1854 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001855 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001856 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001857 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001858 paramsize += PARAM_DELTA;
1859 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1860 }
1861 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001862 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001863 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001864 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001865 (*params)[(*nparam)++] = t;
1866 while (tok_isnt_(t, brace ? "}" : ","))
1867 t = t->next;
1868 if (t) { /* got a comma/brace */
1869 t = t->next;
1870 if (brace) {
1871 /*
1872 * Now we've found the closing brace, look further
1873 * for the comma.
1874 */
1875 skip_white_(t);
1876 if (tok_isnt_(t, ",")) {
1877 error(ERR_NONFATAL,
1878 "braces do not enclose all of macro parameter");
1879 while (tok_isnt_(t, ","))
1880 t = t->next;
1881 }
1882 if (t)
1883 t = t->next; /* eat the comma */
1884 }
1885 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001886 }
1887}
1888
1889/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001890 * Determine whether one of the various `if' conditions is true or
1891 * not.
1892 *
1893 * We must free the tline we get passed.
1894 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001895static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001896{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001897 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001898 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001899 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001900 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001901 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001902 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001903 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001904
1905 origline = tline;
1906
H. Peter Anvine2c80182005-01-15 22:15:51 +00001907 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001908 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001909 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001910 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001911 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001912 if (!tline)
1913 break;
1914 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001915 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001916 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001917 free_tlist(origline);
1918 return -1;
1919 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001920 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001921 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001922 tline = tline->next;
1923 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001924 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001925
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001926 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001927 j = false; /* have we matched yet? */
Cyrill Gorcunov4c6f82f2011-10-02 01:06:29 +04001928 skip_white_(tline);
1929 do {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001930 if (!tline || (tline->type != TOK_ID &&
1931 (tline->type != TOK_PREPROC_ID ||
1932 tline->text[1] != '$'))) {
1933 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001934 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001935 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001936 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001937 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001938 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001939 tline = tline->next;
Cyrill Gorcunov4c6f82f2011-10-02 01:06:29 +04001940 skip_white_(tline);
1941 } while (tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001942 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001943
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001944 case PPC_IFENV:
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001945 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001946 j = false; /* have we matched yet? */
Cyrill Gorcunov6acada62011-10-02 09:18:34 +04001947 skip_white_(tline);
1948 do {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001949 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001950 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001951 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001952 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001953 error(ERR_NONFATAL,
1954 "`%s' expects environment variable names",
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001955 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001956 goto fail;
1957 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001958 p = tline->text;
1959 if (tline->type == TOK_PREPROC_ID)
1960 p += 2; /* Skip leading %! */
1961 if (*p == '\'' || *p == '\"' || *p == '`')
1962 nasm_unquote_cstr(p, ct);
1963 if (getenv(p))
1964 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001965 tline = tline->next;
Cyrill Gorcunov6acada62011-10-02 09:18:34 +04001966 skip_white_(tline);
1967 } while (tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001968 break;
1969
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001970 case PPC_IFIDN:
1971 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001972 tline = expand_smacro(tline);
1973 t = tt = tline;
1974 while (tok_isnt_(tt, ","))
1975 tt = tt->next;
1976 if (!tt) {
1977 error(ERR_NONFATAL,
1978 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001979 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001980 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001981 }
1982 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001983 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001984 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1985 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1986 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001987 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001988 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001989 }
1990 if (t->type == TOK_WHITESPACE) {
1991 t = t->next;
1992 continue;
1993 }
1994 if (tt->type == TOK_WHITESPACE) {
1995 tt = tt->next;
1996 continue;
1997 }
1998 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001999 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002000 break;
2001 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002002 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002003 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002004 size_t l1 = nasm_unquote(t->text, NULL);
2005 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07002006
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002007 if (l1 != l2) {
2008 j = false;
2009 break;
2010 }
2011 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
2012 j = false;
2013 break;
2014 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002015 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002016 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002017 break;
2018 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002019
H. Peter Anvine2c80182005-01-15 22:15:51 +00002020 t = t->next;
2021 tt = tt->next;
2022 }
2023 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002024 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002025 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002026
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002027 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002028 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002029 bool found = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002030 ExpDef searching, *ed;
H. Peter Anvin65747262002-05-07 00:10:05 +00002031
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002032 skip_white_(tline);
2033 tline = expand_id(tline);
2034 if (!tok_type_(tline, TOK_ID)) {
2035 error(ERR_NONFATAL,
2036 "`%s' expects a macro name", pp_directives[ct]);
2037 goto fail;
2038 }
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002039 memset(&searching, 0, sizeof(searching));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002040 searching.name = nasm_strdup(tline->text);
2041 searching.casesense = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002042 searching.nparam_max = INT_MAX;
2043 tline = expand_smacro(tline->next);
2044 skip_white_(tline);
2045 if (!tline) {
2046 } else if (!tok_type_(tline, TOK_NUMBER)) {
2047 error(ERR_NONFATAL,
2048 "`%s' expects a parameter count or nothing",
2049 pp_directives[ct]);
2050 } else {
2051 searching.nparam_min = searching.nparam_max =
2052 readnum(tline->text, &j);
2053 if (j)
2054 error(ERR_NONFATAL,
2055 "unable to parse parameter count `%s'",
2056 tline->text);
2057 }
2058 if (tline && tok_is_(tline->next, "-")) {
2059 tline = tline->next->next;
2060 if (tok_is_(tline, "*"))
2061 searching.nparam_max = INT_MAX;
2062 else if (!tok_type_(tline, TOK_NUMBER))
2063 error(ERR_NONFATAL,
2064 "`%s' expects a parameter count after `-'",
2065 pp_directives[ct]);
2066 else {
2067 searching.nparam_max = readnum(tline->text, &j);
2068 if (j)
2069 error(ERR_NONFATAL,
2070 "unable to parse parameter count `%s'",
2071 tline->text);
2072 if (searching.nparam_min > searching.nparam_max)
2073 error(ERR_NONFATAL,
2074 "minimum parameter count exceeds maximum");
2075 }
2076 }
2077 if (tline && tok_is_(tline->next, "+")) {
2078 tline = tline->next;
2079 searching.plus = true;
2080 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002081 ed = (ExpDef *) hash_findix(&expdefs, searching.name);
2082 while (ed != NULL) {
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002083 if (!strcmp(ed->name, searching.name) &&
2084 (ed->nparam_min <= searching.nparam_max || searching.plus) &&
2085 (searching.nparam_min <= ed->nparam_max || ed->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002086 found = true;
2087 break;
2088 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002089 ed = ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002090 }
2091 if (tline && tline->next)
2092 error(ERR_WARNING|ERR_PASS1,
2093 "trailing garbage after %%ifmacro ignored");
2094 nasm_free(searching.name);
2095 j = found;
2096 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002097 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002098
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002099 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002100 needtype = TOK_ID;
2101 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002102 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002103 needtype = TOK_NUMBER;
2104 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002105 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002106 needtype = TOK_STRING;
2107 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002108
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002109iftype:
2110 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002111
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002112 while (tok_type_(t, TOK_WHITESPACE) ||
2113 (needtype == TOK_NUMBER &&
2114 tok_type_(t, TOK_OTHER) &&
2115 (t->text[0] == '-' || t->text[0] == '+') &&
2116 !t->text[1]))
2117 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002118
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002119 j = tok_type_(t, needtype);
2120 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002121
2122 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002123 t = tline = expand_smacro(tline);
2124 while (tok_type_(t, TOK_WHITESPACE))
2125 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002126
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002127 j = false;
2128 if (t) {
2129 t = t->next; /* Skip the actual token */
2130 while (tok_type_(t, TOK_WHITESPACE))
2131 t = t->next;
2132 j = !t; /* Should be nothing left */
2133 }
2134 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002135
H. Peter Anvin134b9462008-02-16 17:01:40 -08002136 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002137 t = tline = expand_smacro(tline);
2138 while (tok_type_(t, TOK_WHITESPACE))
2139 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002140
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002141 j = !t; /* Should be empty */
2142 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002143
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002144 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002145 t = tline = expand_smacro(tline);
2146 tptr = &t;
2147 tokval.t_type = TOKEN_INVALID;
2148 evalresult = evaluate(ppscan, tptr, &tokval,
2149 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002150 if (!evalresult)
2151 return -1;
2152 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002153 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002154 "trailing garbage after expression ignored");
2155 if (!is_simple(evalresult)) {
2156 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002157 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002158 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002159 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002160 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002161 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002162
H. Peter Anvine2c80182005-01-15 22:15:51 +00002163 default:
2164 error(ERR_FATAL,
2165 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002166 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002167 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002168 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002169
2170 free_tlist(origline);
2171 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002172
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002173fail:
2174 free_tlist(origline);
2175 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002176}
2177
2178/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002179 * Common code for defining an smacro
2180 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002181static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002182 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002183{
2184 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002185 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002186
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002187 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002188 if (!smac) {
2189 error(ERR_WARNING|ERR_PASS1,
2190 "single-line macro `%s' defined both with and"
2191 " without parameters", mname);
2192 /*
2193 * Some instances of the old code considered this a failure,
2194 * some others didn't. What is the right thing to do here?
2195 */
2196 free_tlist(expansion);
2197 return false; /* Failure */
2198 } else {
2199 /*
2200 * We're redefining, so we have to take over an
2201 * existing SMacro structure. This means freeing
2202 * what was already in it.
2203 */
2204 nasm_free(smac->name);
2205 free_tlist(smac->expansion);
2206 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002207 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002208 smtbl = ctx ? &ctx->localmac : &smacros;
2209 smhead = (SMacro **) hash_findi_add(smtbl, mname);
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002210 smac = nasm_zalloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002211 smac->next = *smhead;
2212 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002213 }
2214 smac->name = nasm_strdup(mname);
2215 smac->casesense = casesense;
2216 smac->nparam = nparam;
2217 smac->expansion = expansion;
2218 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002219 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002220}
2221
2222/*
2223 * Undefine an smacro
2224 */
2225static void undef_smacro(Context *ctx, const char *mname)
2226{
2227 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002228 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002229
H. Peter Anvin166c2472008-05-28 12:28:58 -07002230 smtbl = ctx ? &ctx->localmac : &smacros;
2231 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002232
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002233 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002234 /*
2235 * We now have a macro name... go hunt for it.
2236 */
2237 sp = smhead;
2238 while ((s = *sp) != NULL) {
2239 if (!mstrcmp(s->name, mname, s->casesense)) {
2240 *sp = s->next;
2241 nasm_free(s->name);
2242 free_tlist(s->expansion);
2243 nasm_free(s);
2244 } else {
2245 sp = &s->next;
2246 }
2247 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002248 }
2249}
2250
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002251/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002252 * Parse a mmacro specification.
2253 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002254static bool parse_mmacro_spec(Token *tline, ExpDef *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002255{
2256 bool err;
2257
2258 tline = tline->next;
2259 skip_white_(tline);
2260 tline = expand_id(tline);
2261 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002262 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2263 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002264 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002265
H. Peter Anvina26433d2008-07-16 14:40:01 -07002266 def->name = nasm_strdup(tline->text);
2267 def->plus = false;
2268 def->nolist = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002269// def->in_progress = 0;
2270// def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002271 def->nparam_min = 0;
2272 def->nparam_max = 0;
2273
H. Peter Anvina26433d2008-07-16 14:40:01 -07002274 tline = expand_smacro(tline->next);
2275 skip_white_(tline);
2276 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002277 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002278 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002279 def->nparam_min = def->nparam_max =
2280 readnum(tline->text, &err);
2281 if (err)
2282 error(ERR_NONFATAL,
2283 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002284 }
2285 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002286 tline = tline->next->next;
2287 if (tok_is_(tline, "*")) {
2288 def->nparam_max = INT_MAX;
2289 } else if (!tok_type_(tline, TOK_NUMBER)) {
2290 error(ERR_NONFATAL,
2291 "`%s' expects a parameter count after `-'", directive);
2292 } else {
2293 def->nparam_max = readnum(tline->text, &err);
2294 if (err) {
2295 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2296 tline->text);
2297 }
2298 if (def->nparam_min > def->nparam_max) {
2299 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2300 }
2301 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002302 }
2303 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002304 tline = tline->next;
2305 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002306 }
2307 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002308 !nasm_stricmp(tline->next->text, ".nolist")) {
2309 tline = tline->next;
2310 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002311 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002312
H. Peter Anvina26433d2008-07-16 14:40:01 -07002313 /*
2314 * Handle default parameters.
2315 */
2316 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002317 def->dlist = tline->next;
2318 tline->next = NULL;
2319 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002320 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002321 def->dlist = NULL;
2322 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002323 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002324 def->line = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002325
H. Peter Anvin89cee572009-07-15 09:16:54 -04002326 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002327 !def->plus)
2328 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2329 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002330
H. Peter Anvina26433d2008-07-16 14:40:01 -07002331 return true;
2332}
2333
2334
2335/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002336 * Decode a size directive
2337 */
2338static int parse_size(const char *str) {
2339 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002340 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002341 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002342 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002343
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002344 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002345}
2346
Ed Beroset3ab3f412002-06-11 03:31:49 +00002347/**
2348 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002349 * Find out if a line contains a preprocessor directive, and deal
2350 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002351 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002352 * If a directive _is_ found, it is the responsibility of this routine
2353 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002354 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002355 * @param tline a pointer to the current tokeninzed line linked list
2356 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002357 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002358 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002359static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002360{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002361 enum preproc_token i;
2362 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002363 bool err;
2364 int nparam;
2365 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002366 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002367 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002368 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002369 char *p, *pp;
2370 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002371 Include *inc;
2372 Context *ctx;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002373 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002374 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002375 struct tokenval tokval;
2376 expr *evalresult;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002377 ExpDef *ed, *eed, **edhead;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002378 ExpInv *ei, *eei;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002379 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002380 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002381 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002382
2383 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002384
H. Peter Anvineba20a72002-04-30 20:53:55 +00002385 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002386 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002387 (tline->text[1] == '%' || tline->text[1] == '$'
2388 || tline->text[1] == '!'))
2389 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002390
H. Peter Anvin4169a472007-09-12 01:29:43 +00002391 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002392
H. Peter Anvin4169a472007-09-12 01:29:43 +00002393 switch (i) {
2394 case PP_INVALID:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002395 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002396 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2397 tline->text);
2398 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002399
H. Peter Anvine2c80182005-01-15 22:15:51 +00002400 case PP_STACKSIZE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002401 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002402 /* Directive to tell NASM what the default stack size is. The
2403 * default is for a 16-bit stack, and this can be overriden with
2404 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002405 */
2406 tline = tline->next;
2407 if (tline && tline->type == TOK_WHITESPACE)
2408 tline = tline->next;
2409 if (!tline || tline->type != TOK_ID) {
2410 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2411 free_tlist(origline);
2412 return DIRECTIVE_FOUND;
2413 }
2414 if (nasm_stricmp(tline->text, "flat") == 0) {
2415 /* All subsequent ARG directives are for a 32-bit stack */
2416 StackSize = 4;
2417 StackPointer = "ebp";
2418 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002419 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002420 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2421 /* All subsequent ARG directives are for a 64-bit stack */
2422 StackSize = 8;
2423 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002424 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002425 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002426 } else if (nasm_stricmp(tline->text, "large") == 0) {
2427 /* All subsequent ARG directives are for a 16-bit stack,
2428 * far function call.
2429 */
2430 StackSize = 2;
2431 StackPointer = "bp";
2432 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002433 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002434 } else if (nasm_stricmp(tline->text, "small") == 0) {
2435 /* All subsequent ARG directives are for a 16-bit stack,
2436 * far function call. We don't support near functions.
2437 */
2438 StackSize = 2;
2439 StackPointer = "bp";
2440 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002441 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002442 } else {
2443 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2444 free_tlist(origline);
2445 return DIRECTIVE_FOUND;
2446 }
2447 free_tlist(origline);
2448 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002449
H. Peter Anvine2c80182005-01-15 22:15:51 +00002450 case PP_ARG:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002451 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002452 /* TASM like ARG directive to define arguments to functions, in
2453 * the following form:
2454 *
2455 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2456 */
2457 offset = ArgOffset;
2458 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002459 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002460 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002461
H. Peter Anvine2c80182005-01-15 22:15:51 +00002462 /* Find the argument name */
2463 tline = tline->next;
2464 if (tline && tline->type == TOK_WHITESPACE)
2465 tline = tline->next;
2466 if (!tline || tline->type != TOK_ID) {
2467 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2468 free_tlist(origline);
2469 return DIRECTIVE_FOUND;
2470 }
2471 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002472
H. Peter Anvine2c80182005-01-15 22:15:51 +00002473 /* Find the argument size type */
2474 tline = tline->next;
2475 if (!tline || tline->type != TOK_OTHER
2476 || tline->text[0] != ':') {
2477 error(ERR_NONFATAL,
2478 "Syntax error processing `%%arg' directive");
2479 free_tlist(origline);
2480 return DIRECTIVE_FOUND;
2481 }
2482 tline = tline->next;
2483 if (!tline || tline->type != TOK_ID) {
2484 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2485 free_tlist(origline);
2486 return DIRECTIVE_FOUND;
2487 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002488
H. Peter Anvine2c80182005-01-15 22:15:51 +00002489 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002490 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002491 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002492 size = parse_size(tt->text);
2493 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002494 error(ERR_NONFATAL,
2495 "Invalid size type for `%%arg' missing directive");
2496 free_tlist(tt);
2497 free_tlist(origline);
2498 return DIRECTIVE_FOUND;
2499 }
2500 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002501
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002502 /* Round up to even stack slots */
2503 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002504
H. Peter Anvine2c80182005-01-15 22:15:51 +00002505 /* Now define the macro for the argument */
2506 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2507 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002508 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002509 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002510
H. Peter Anvine2c80182005-01-15 22:15:51 +00002511 /* Move to the next argument in the list */
2512 tline = tline->next;
2513 if (tline && tline->type == TOK_WHITESPACE)
2514 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002515 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002516 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002517 free_tlist(origline);
2518 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002519
H. Peter Anvine2c80182005-01-15 22:15:51 +00002520 case PP_LOCAL:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002521 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002522 /* TASM like LOCAL directive to define local variables for a
2523 * function, in the following form:
2524 *
2525 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2526 *
2527 * The '= LocalSize' at the end is ignored by NASM, but is
2528 * required by TASM to define the local parameter size (and used
2529 * by the TASM macro package).
2530 */
2531 offset = LocalOffset;
2532 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002533 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002534 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002535
H. Peter Anvine2c80182005-01-15 22:15:51 +00002536 /* Find the argument name */
2537 tline = tline->next;
2538 if (tline && tline->type == TOK_WHITESPACE)
2539 tline = tline->next;
2540 if (!tline || tline->type != TOK_ID) {
2541 error(ERR_NONFATAL,
2542 "`%%local' missing argument parameter");
2543 free_tlist(origline);
2544 return DIRECTIVE_FOUND;
2545 }
2546 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002547
H. Peter Anvine2c80182005-01-15 22:15:51 +00002548 /* Find the argument size type */
2549 tline = tline->next;
2550 if (!tline || tline->type != TOK_OTHER
2551 || tline->text[0] != ':') {
2552 error(ERR_NONFATAL,
2553 "Syntax error processing `%%local' directive");
2554 free_tlist(origline);
2555 return DIRECTIVE_FOUND;
2556 }
2557 tline = tline->next;
2558 if (!tline || tline->type != TOK_ID) {
2559 error(ERR_NONFATAL,
2560 "`%%local' missing size type parameter");
2561 free_tlist(origline);
2562 return DIRECTIVE_FOUND;
2563 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002564
H. Peter Anvine2c80182005-01-15 22:15:51 +00002565 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002566 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002567 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002568 size = parse_size(tt->text);
2569 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002570 error(ERR_NONFATAL,
2571 "Invalid size type for `%%local' missing directive");
2572 free_tlist(tt);
2573 free_tlist(origline);
2574 return DIRECTIVE_FOUND;
2575 }
2576 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002577
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002578 /* Round up to even stack slots */
2579 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002580
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002581 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002582
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002583 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002584 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2585 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002586 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002587
H. Peter Anvine2c80182005-01-15 22:15:51 +00002588 /* Now define the assign to setup the enter_c macro correctly */
2589 snprintf(directive, sizeof(directive),
2590 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002591 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002592
H. Peter Anvine2c80182005-01-15 22:15:51 +00002593 /* Move to the next argument in the list */
2594 tline = tline->next;
2595 if (tline && tline->type == TOK_WHITESPACE)
2596 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002597 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002598 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002599 free_tlist(origline);
2600 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002601
H. Peter Anvine2c80182005-01-15 22:15:51 +00002602 case PP_CLEAR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002603 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002604 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002605 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002606 "trailing garbage after `%%clear' ignored");
2607 free_macros();
2608 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002609 free_tlist(origline);
2610 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002611
H. Peter Anvin418ca702008-05-30 10:42:30 -07002612 case PP_DEPEND:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002613 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002614 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002615 skip_white_(t);
2616 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002617 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002618 error(ERR_NONFATAL, "`%%depend' expects a file name");
2619 free_tlist(origline);
2620 return DIRECTIVE_FOUND; /* but we did _something_ */
2621 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002622 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002623 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002624 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002625 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002626 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002627 nasm_unquote_cstr(p, i);
2628 if (dephead && !in_list(*dephead, p)) {
2629 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2630 sl->next = NULL;
2631 strcpy(sl->str, p);
2632 *deptail = sl;
2633 deptail = &sl->next;
2634 }
2635 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002636 return DIRECTIVE_FOUND;
2637
2638 case PP_INCLUDE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002639 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002640 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002641 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002642
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002643 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002644 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002645 error(ERR_NONFATAL, "`%%include' expects a file name");
2646 free_tlist(origline);
2647 return DIRECTIVE_FOUND; /* but we did _something_ */
2648 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002649 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002650 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002651 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002652 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002653 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002654 nasm_unquote_cstr(p, i);
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03002655 inc = nasm_zalloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002656 inc->next = istk;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002657 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002658 if (!inc->fp) {
2659 /* -MG given but file not found */
2660 nasm_free(inc);
2661 } else {
2662 inc->fname = src_set_fname(nasm_strdup(p));
2663 inc->lineno = src_set_linnum(0);
2664 inc->lineinc = 1;
2665 inc->expansion = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002666 istk = inc;
2667 list->uplevel(LIST_INCLUDE);
2668 }
2669 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002670 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002671
H. Peter Anvind2456592008-06-19 15:04:18 -07002672 case PP_USE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002673 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002674 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002675 static macros_t *use_pkg;
2676 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002677
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002678 tline = tline->next;
2679 skip_white_(tline);
2680 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002681
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002682 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002683 tline->type != TOK_INTERNAL_STRING &&
2684 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002685 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002686 free_tlist(origline);
2687 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002688 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002689 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002690 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002691 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002692 if (tline->type == TOK_STRING)
2693 nasm_unquote_cstr(tline->text, i);
2694 use_pkg = nasm_stdmac_find_package(tline->text);
2695 if (!use_pkg)
2696 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2697 else
2698 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002699 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002700 /* Not already included, go ahead and include it */
2701 stdmacpos = use_pkg;
2702 }
2703 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002704 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002705 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002706 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002707 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002708 case PP_POP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002709 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002710 tline = tline->next;
2711 skip_white_(tline);
2712 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002713 if (tline) {
2714 if (!tok_type_(tline, TOK_ID)) {
2715 error(ERR_NONFATAL, "`%s' expects a context identifier",
2716 pp_directives[i]);
2717 free_tlist(origline);
2718 return DIRECTIVE_FOUND; /* but we did _something_ */
2719 }
2720 if (tline->next)
2721 error(ERR_WARNING|ERR_PASS1,
2722 "trailing garbage after `%s' ignored",
2723 pp_directives[i]);
2724 p = nasm_strdup(tline->text);
2725 } else {
2726 p = NULL; /* Anonymous */
2727 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002728
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002729 if (i == PP_PUSH) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002730 ctx = nasm_zalloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002731 ctx->next = cstk;
2732 hash_init(&ctx->localmac, HASH_SMALL);
2733 ctx->name = p;
2734 ctx->number = unique++;
2735 cstk = ctx;
2736 } else {
2737 /* %pop or %repl */
2738 if (!cstk) {
2739 error(ERR_NONFATAL, "`%s': context stack is empty",
2740 pp_directives[i]);
2741 } else if (i == PP_POP) {
2742 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2743 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2744 "expected %s",
2745 cstk->name ? cstk->name : "anonymous", p);
2746 else
2747 ctx_pop();
2748 } else {
2749 /* i == PP_REPL */
2750 nasm_free(cstk->name);
2751 cstk->name = p;
2752 p = NULL;
2753 }
2754 nasm_free(p);
2755 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002756 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002757 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002758 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002759 severity = ERR_FATAL;
2760 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002761 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002762 severity = ERR_NONFATAL;
2763 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002764 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002765 severity = ERR_WARNING|ERR_WARN_USER;
2766 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002767
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002768issue_error:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002769 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002770 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002771 /* Only error out if this is the final pass */
2772 if (pass != 2 && i != PP_FATAL)
2773 return DIRECTIVE_FOUND;
2774
2775 tline->next = expand_smacro(tline->next);
2776 tline = tline->next;
2777 skip_white_(tline);
2778 t = tline ? tline->next : NULL;
2779 skip_white_(t);
2780 if (tok_type_(tline, TOK_STRING) && !t) {
2781 /* The line contains only a quoted string */
2782 p = tline->text;
2783 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2784 error(severity, "%s", p);
2785 } else {
2786 /* Not a quoted string, or more than a quoted string */
2787 p = detoken(tline, false);
2788 error(severity, "%s", p);
2789 nasm_free(p);
2790 }
2791 free_tlist(origline);
2792 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002793 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002794
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002795 CASE_PP_IF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002796 if (defining != NULL) {
2797 if (defining->type == EXP_IF) {
2798 defining->def_depth ++;
2799 }
2800 return NO_DIRECTIVE_FOUND;
2801 }
2802 if ((istk->expansion != NULL) &&
2803 (istk->expansion->emitting == false)) {
2804 j = COND_NEVER;
2805 } else {
2806 j = if_condition(tline->next, i);
2807 tline->next = NULL; /* it got freed */
2808 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
2809 }
2810 ed = new_ExpDef(EXP_IF);
2811 ed->state = j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002812 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
2813 ed->prev = defining;
2814 defining = ed;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002815 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002816 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002817
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002818 CASE_PP_ELIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002819 if (defining != NULL) {
2820 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2821 return NO_DIRECTIVE_FOUND;
2822 }
2823 }
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +04002824 if ((defining == NULL) || (defining->type != EXP_IF)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002825 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2826 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002827 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002828 case COND_IF_TRUE:
2829 defining->state = COND_DONE;
2830 defining->ignoring = true;
2831 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002832
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002833 case COND_DONE:
2834 case COND_NEVER:
2835 defining->ignoring = true;
2836 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002837
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002838 case COND_ELSE_TRUE:
2839 case COND_ELSE_FALSE:
2840 error_precond(ERR_WARNING|ERR_PASS1,
2841 "`%%elif' after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002842 defining->state = COND_NEVER;
2843 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002844 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002845
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002846 case COND_IF_FALSE:
2847 /*
2848 * IMPORTANT: In the case of %if, we will already have
2849 * called expand_mmac_params(); however, if we're
2850 * processing an %elif we must have been in a
2851 * non-emitting mode, which would have inhibited
2852 * the normal invocation of expand_mmac_params().
2853 * Therefore, we have to do it explicitly here.
2854 */
2855 j = if_condition(expand_mmac_params(tline->next), i);
2856 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002857 defining->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002858 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002859 defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002860 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002861 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002862 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002863 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002864
H. Peter Anvine2c80182005-01-15 22:15:51 +00002865 case PP_ELSE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002866 if (defining != NULL) {
2867 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2868 return NO_DIRECTIVE_FOUND;
2869 }
2870 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002871 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002872 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002873 "trailing garbage after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002874 if ((defining == NULL) || (defining->type != EXP_IF)) {
2875 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2876 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002877 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002878 case COND_IF_TRUE:
2879 case COND_DONE:
2880 defining->state = COND_ELSE_FALSE;
2881 defining->ignoring = true;
2882 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002883
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002884 case COND_NEVER:
2885 defining->ignoring = true;
2886 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002887
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002888 case COND_IF_FALSE:
2889 defining->state = COND_ELSE_TRUE;
2890 defining->ignoring = false;
2891 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002892
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002893 case COND_ELSE_TRUE:
2894 case COND_ELSE_FALSE:
2895 error_precond(ERR_WARNING|ERR_PASS1,
2896 "`%%else' after `%%else' ignored.");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002897 defining->state = COND_NEVER;
2898 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002899 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002900 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002901 free_tlist(origline);
2902 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002903
H. Peter Anvine2c80182005-01-15 22:15:51 +00002904 case PP_ENDIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002905 if (defining != NULL) {
2906 if (defining->type == EXP_IF) {
2907 if (defining->def_depth > 0) {
2908 defining->def_depth --;
2909 return NO_DIRECTIVE_FOUND;
2910 }
2911 } else {
2912 return NO_DIRECTIVE_FOUND;
2913 }
2914 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002915 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002916 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002917 "trailing garbage after `%%endif' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002918 if ((defining == NULL) || (defining->type != EXP_IF)) {
2919 error(ERR_NONFATAL, "`%%endif': no matching `%%if'");
2920 return DIRECTIVE_FOUND;
2921 }
2922 ed = defining;
2923 defining = ed->prev;
2924 ed->prev = expansions;
2925 expansions = ed;
2926 ei = new_ExpInv(EXP_IF, ed);
2927 ei->current = ed->line;
2928 ei->emitting = true;
2929 ei->prev = istk->expansion;
2930 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002931 free_tlist(origline);
2932 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002933
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002934 case PP_RMACRO:
2935 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002936 case PP_MACRO:
2937 case PP_IMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002938 if (defining != NULL) {
2939 if (defining->type == EXP_MMACRO) {
2940 defining->def_depth ++;
2941 }
2942 return NO_DIRECTIVE_FOUND;
2943 }
2944 ed = new_ExpDef(EXP_MMACRO);
2945 ed->max_depth =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002946 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002947 ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002948 if (!parse_mmacro_spec(tline, ed, pp_directives[i])) {
2949 nasm_free(ed);
2950 ed = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002951 return DIRECTIVE_FOUND;
2952 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002953 ed->def_depth = 0;
2954 ed->cur_depth = 0;
2955 ed->max_depth = (ed->max_depth + 1);
2956 ed->ignoring = false;
2957 ed->prev = defining;
2958 defining = ed;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002959
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002960 eed = (ExpDef *) hash_findix(&expdefs, ed->name);
2961 while (eed) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002962 if (!strcmp(eed->name, ed->name) &&
2963 (eed->nparam_min <= ed->nparam_max || ed->plus) &&
2964 (ed->nparam_min <= eed->nparam_max || eed->plus)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002965 error(ERR_WARNING|ERR_PASS1,
2966 "redefining multi-line macro `%s'", ed->name);
2967 return DIRECTIVE_FOUND;
2968 }
2969 eed = eed->next;
2970 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002971 free_tlist(origline);
2972 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002973
H. Peter Anvine2c80182005-01-15 22:15:51 +00002974 case PP_ENDM:
2975 case PP_ENDMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002976 if (defining != NULL) {
2977 if (defining->type == EXP_MMACRO) {
2978 if (defining->def_depth > 0) {
2979 defining->def_depth --;
2980 return NO_DIRECTIVE_FOUND;
2981 }
2982 } else {
2983 return NO_DIRECTIVE_FOUND;
2984 }
2985 }
2986 if (!(defining) || (defining->type != EXP_MMACRO)) {
2987 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2988 return DIRECTIVE_FOUND;
2989 }
2990 edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name);
2991 defining->next = *edhead;
2992 *edhead = defining;
2993 ed = defining;
2994 defining = ed->prev;
2995 ed->prev = expansions;
2996 expansions = ed;
2997 ed = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002998 free_tlist(origline);
2999 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003000
H. Peter Anvin89cee572009-07-15 09:16:54 -04003001 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003002 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3003 /*
3004 * We must search along istk->expansion until we hit a
3005 * macro invocation. Then we disable the emitting state(s)
3006 * between exitmacro and endmacro.
3007 */
3008 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3009 if(ei->type == EXP_MMACRO) {
3010 break;
3011 }
3012 }
3013
3014 if (ei != NULL) {
3015 /*
3016 * Set all invocations leading back to the macro
3017 * invocation to a non-emitting state.
3018 */
3019 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3020 eei->emitting = false;
3021 }
3022 eei->emitting = false;
3023 } else {
3024 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
3025 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05003026 free_tlist(origline);
3027 return DIRECTIVE_FOUND;
3028
H. Peter Anvina26433d2008-07-16 14:40:01 -07003029 case PP_UNMACRO:
3030 case PP_UNIMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003031 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003032 {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003033 ExpDef **ed_p;
3034 ExpDef spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003035
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003036 spec.casesense = (i == PP_UNMACRO);
3037 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
3038 return DIRECTIVE_FOUND;
3039 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003040 ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL);
3041 while (ed_p && *ed_p) {
3042 ed = *ed_p;
3043 if (ed->casesense == spec.casesense &&
3044 !mstrcmp(ed->name, spec.name, spec.casesense) &&
3045 ed->nparam_min == spec.nparam_min &&
3046 ed->nparam_max == spec.nparam_max &&
3047 ed->plus == spec.plus) {
Keith Kaniosc98a5b42010-12-18 12:17:31 -06003048 if (ed->cur_depth > 0) {
Keith Kanios21d885b2010-12-18 12:22:21 -06003049 error(ERR_NONFATAL, "`%s' ignored on active macro",
Keith Kaniosc98a5b42010-12-18 12:17:31 -06003050 pp_directives[i]);
3051 break;
3052 } else {
3053 *ed_p = ed->next;
3054 free_expdef(ed);
3055 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003056 } else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003057 ed_p = &ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003058 }
3059 }
3060 free_tlist(origline);
3061 free_tlist(spec.dlist);
3062 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003063 }
3064
H. Peter Anvine2c80182005-01-15 22:15:51 +00003065 case PP_ROTATE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003066 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003067 if (tline->next && tline->next->type == TOK_WHITESPACE)
3068 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003069 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003070 free_tlist(origline);
3071 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
3072 return DIRECTIVE_FOUND;
3073 }
3074 t = expand_smacro(tline->next);
3075 tline->next = NULL;
3076 free_tlist(origline);
3077 tline = t;
3078 tptr = &t;
3079 tokval.t_type = TOKEN_INVALID;
3080 evalresult =
3081 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3082 free_tlist(tline);
3083 if (!evalresult)
3084 return DIRECTIVE_FOUND;
3085 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003086 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003087 "trailing garbage after expression ignored");
3088 if (!is_simple(evalresult)) {
3089 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
3090 return DIRECTIVE_FOUND;
3091 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003092 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3093 if (ei->type == EXP_MMACRO) {
3094 break;
3095 }
3096 }
3097 if (ei == NULL) {
3098 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
3099 } else if (ei->nparam == 0) {
3100 error(ERR_NONFATAL,
3101 "`%%rotate' invoked within macro without parameters");
3102 } else {
3103 int rotate = ei->rotate + reloc_value(evalresult);
3104
3105 rotate %= (int)ei->nparam;
3106 if (rotate < 0)
3107 rotate += ei->nparam;
3108 ei->rotate = rotate;
3109 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003110 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003111
H. Peter Anvine2c80182005-01-15 22:15:51 +00003112 case PP_REP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003113 if (defining != NULL) {
3114 if (defining->type == EXP_REP) {
3115 defining->def_depth ++;
3116 }
3117 return NO_DIRECTIVE_FOUND;
3118 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003119 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003120 do {
3121 tline = tline->next;
3122 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003123
H. Peter Anvine2c80182005-01-15 22:15:51 +00003124 if (tok_type_(tline, TOK_ID) &&
3125 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003126 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003127 do {
3128 tline = tline->next;
3129 } while (tok_type_(tline, TOK_WHITESPACE));
3130 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003131
H. Peter Anvine2c80182005-01-15 22:15:51 +00003132 if (tline) {
3133 t = expand_smacro(tline);
3134 tptr = &t;
3135 tokval.t_type = TOKEN_INVALID;
3136 evalresult =
3137 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3138 if (!evalresult) {
3139 free_tlist(origline);
3140 return DIRECTIVE_FOUND;
3141 }
3142 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003143 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003144 "trailing garbage after expression ignored");
3145 if (!is_simple(evalresult)) {
3146 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
3147 return DIRECTIVE_FOUND;
3148 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003149 count = reloc_value(evalresult);
3150 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04003151 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003152 count = 0;
3153 } else
3154 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003155 } else {
3156 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003157 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003158 }
3159 free_tlist(origline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003160 ed = new_ExpDef(EXP_REP);
3161 ed->nolist = nolist;
3162 ed->def_depth = 0;
3163 ed->cur_depth = 1;
3164 ed->max_depth = (count - 1);
3165 ed->ignoring = false;
3166 ed->prev = defining;
3167 defining = ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003168 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003169
H. Peter Anvine2c80182005-01-15 22:15:51 +00003170 case PP_ENDREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003171 if (defining != NULL) {
3172 if (defining->type == EXP_REP) {
3173 if (defining->def_depth > 0) {
3174 defining->def_depth --;
3175 return NO_DIRECTIVE_FOUND;
3176 }
3177 } else {
3178 return NO_DIRECTIVE_FOUND;
3179 }
3180 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003181 if ((defining == NULL) || (defining->type != EXP_REP)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003182 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
3183 return DIRECTIVE_FOUND;
3184 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003185
H. Peter Anvine2c80182005-01-15 22:15:51 +00003186 /*
3187 * Now we have a "macro" defined - although it has no name
3188 * and we won't be entering it in the hash tables - we must
3189 * push a macro-end marker for it on to istk->expansion.
3190 * After that, it will take care of propagating itself (a
3191 * macro-end marker line for a macro which is really a %rep
3192 * block will cause the macro to be re-expanded, complete
3193 * with another macro-end marker to ensure the process
3194 * continues) until the whole expansion is forcibly removed
3195 * from istk->expansion by a %exitrep.
3196 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003197 ed = defining;
3198 defining = ed->prev;
3199 ed->prev = expansions;
3200 expansions = ed;
3201 ei = new_ExpInv(EXP_REP, ed);
3202 ei->current = ed->line;
3203 ei->emitting = ((ed->max_depth > 0) ? true : false);
3204 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3205 ei->prev = istk->expansion;
3206 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003207 free_tlist(origline);
3208 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003209
H. Peter Anvine2c80182005-01-15 22:15:51 +00003210 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003211 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3212 /*
3213 * We must search along istk->expansion until we hit a
3214 * rep invocation. Then we disable the emitting state(s)
3215 * between exitrep and endrep.
3216 */
3217 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3218 if (ei->type == EXP_REP) {
3219 break;
3220 }
3221 }
3222
3223 if (ei != NULL) {
3224 /*
3225 * Set all invocations leading back to the rep
3226 * invocation to a non-emitting state.
3227 */
3228 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3229 eei->emitting = false;
3230 }
3231 eei->emitting = false;
3232 eei->current = NULL;
3233 eei->def->cur_depth = eei->def->max_depth;
3234 } else {
3235 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3236 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003237 free_tlist(origline);
3238 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003239
H. Peter Anvine2c80182005-01-15 22:15:51 +00003240 case PP_XDEFINE:
3241 case PP_IXDEFINE:
3242 case PP_DEFINE:
3243 case PP_IDEFINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003244 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003245 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003246
H. Peter Anvine2c80182005-01-15 22:15:51 +00003247 tline = tline->next;
3248 skip_white_(tline);
3249 tline = expand_id(tline);
3250 if (!tline || (tline->type != TOK_ID &&
3251 (tline->type != TOK_PREPROC_ID ||
3252 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003253 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003254 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003255 free_tlist(origline);
3256 return DIRECTIVE_FOUND;
3257 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003258
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003259 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003260 last = tline;
3261 param_start = tline = tline->next;
3262 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003263
H. Peter Anvine2c80182005-01-15 22:15:51 +00003264 /* Expand the macro definition now for %xdefine and %ixdefine */
3265 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3266 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003267
H. Peter Anvine2c80182005-01-15 22:15:51 +00003268 if (tok_is_(tline, "(")) {
3269 /*
3270 * This macro has parameters.
3271 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003272
H. Peter Anvine2c80182005-01-15 22:15:51 +00003273 tline = tline->next;
3274 while (1) {
3275 skip_white_(tline);
3276 if (!tline) {
3277 error(ERR_NONFATAL, "parameter identifier expected");
3278 free_tlist(origline);
3279 return DIRECTIVE_FOUND;
3280 }
3281 if (tline->type != TOK_ID) {
3282 error(ERR_NONFATAL,
3283 "`%s': parameter identifier expected",
3284 tline->text);
3285 free_tlist(origline);
3286 return DIRECTIVE_FOUND;
3287 }
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04003288
3289 smacro_set_param_idx(tline, nparam);
3290 nparam++;
3291
H. Peter Anvine2c80182005-01-15 22:15:51 +00003292 tline = tline->next;
3293 skip_white_(tline);
3294 if (tok_is_(tline, ",")) {
3295 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003296 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003297 if (!tok_is_(tline, ")")) {
3298 error(ERR_NONFATAL,
3299 "`)' expected to terminate macro template");
3300 free_tlist(origline);
3301 return DIRECTIVE_FOUND;
3302 }
3303 break;
3304 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003305 }
3306 last = tline;
3307 tline = tline->next;
3308 }
3309 if (tok_type_(tline, TOK_WHITESPACE))
3310 last = tline, tline = tline->next;
3311 macro_start = NULL;
3312 last->next = NULL;
3313 t = tline;
3314 while (t) {
3315 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003316 list_for_each(tt, param_start)
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04003317 if (is_smacro_param(tt) &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003318 !strcmp(tt->text, t->text))
3319 t->type = tt->type;
3320 }
3321 tt = t->next;
3322 t->next = macro_start;
3323 macro_start = t;
3324 t = tt;
3325 }
3326 /*
3327 * Good. We now have a macro name, a parameter count, and a
3328 * token list (in reverse order) for an expansion. We ought
3329 * to be OK just to create an SMacro, store it, and let
3330 * free_tlist have the rest of the line (which we have
3331 * carefully re-terminated after chopping off the expansion
3332 * from the end).
3333 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003334 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003335 free_tlist(origline);
3336 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003337
H. Peter Anvine2c80182005-01-15 22:15:51 +00003338 case PP_UNDEF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003339 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003340 tline = tline->next;
3341 skip_white_(tline);
3342 tline = expand_id(tline);
3343 if (!tline || (tline->type != TOK_ID &&
3344 (tline->type != TOK_PREPROC_ID ||
3345 tline->text[1] != '$'))) {
3346 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3347 free_tlist(origline);
3348 return DIRECTIVE_FOUND;
3349 }
3350 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003351 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003352 "trailing garbage after macro name ignored");
3353 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003354
H. Peter Anvine2c80182005-01-15 22:15:51 +00003355 /* Find the context that symbol belongs to */
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003356 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003357 undef_smacro(ctx, mname);
3358 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003359 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003360
H. Peter Anvin9e200162008-06-04 17:23:14 -07003361 case PP_DEFSTR:
3362 case PP_IDEFSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003363 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003364 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003365
3366 tline = tline->next;
3367 skip_white_(tline);
3368 tline = expand_id(tline);
3369 if (!tline || (tline->type != TOK_ID &&
3370 (tline->type != TOK_PREPROC_ID ||
3371 tline->text[1] != '$'))) {
3372 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003373 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003374 free_tlist(origline);
3375 return DIRECTIVE_FOUND;
3376 }
3377
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003378 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003379 last = tline;
3380 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003381 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003382
3383 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003384 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003385
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003386 p = detoken(tline, false);
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003387 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003388 macro_start->text = nasm_quote(p, strlen(p));
3389 macro_start->type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003390 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003391
3392 /*
3393 * We now have a macro name, an implicit parameter count of
3394 * zero, and a string token to use as an expansion. Create
3395 * and store an SMacro.
3396 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003397 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003398 free_tlist(origline);
3399 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003400
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003401 case PP_DEFTOK:
3402 case PP_IDEFTOK:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003403 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003404 casesense = (i == PP_DEFTOK);
3405
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003406 tline = tline->next;
3407 skip_white_(tline);
3408 tline = expand_id(tline);
3409 if (!tline || (tline->type != TOK_ID &&
3410 (tline->type != TOK_PREPROC_ID ||
3411 tline->text[1] != '$'))) {
3412 error(ERR_NONFATAL,
3413 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003414 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003415 free_tlist(origline);
3416 return DIRECTIVE_FOUND;
3417 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003418 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003419 last = tline;
3420 tline = expand_smacro(tline->next);
3421 last->next = NULL;
3422
3423 t = tline;
3424 while (tok_type_(t, TOK_WHITESPACE))
3425 t = t->next;
3426 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003427 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003428 error(ERR_NONFATAL,
3429 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003430 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003431 free_tlist(tline);
3432 free_tlist(origline);
3433 return DIRECTIVE_FOUND;
3434 }
3435
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003436 /*
3437 * Convert the string to a token stream. Note that smacros
3438 * are stored with the token stream reversed, so we have to
3439 * reverse the output of tokenize().
3440 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003441 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003442 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003443
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003444 /*
3445 * We now have a macro name, an implicit parameter count of
3446 * zero, and a numeric token to use as an expansion. Create
3447 * and store an SMacro.
3448 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003449 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003450 free_tlist(tline);
3451 free_tlist(origline);
3452 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003453
H. Peter Anvin418ca702008-05-30 10:42:30 -07003454 case PP_PATHSEARCH:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003455 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003456 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003457 FILE *fp;
3458 StrList *xsl = NULL;
3459 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003460
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003461 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003462
3463 tline = tline->next;
3464 skip_white_(tline);
3465 tline = expand_id(tline);
3466 if (!tline || (tline->type != TOK_ID &&
3467 (tline->type != TOK_PREPROC_ID ||
3468 tline->text[1] != '$'))) {
3469 error(ERR_NONFATAL,
3470 "`%%pathsearch' expects a macro identifier as first parameter");
3471 free_tlist(origline);
3472 return DIRECTIVE_FOUND;
3473 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003474 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003475 last = tline;
3476 tline = expand_smacro(tline->next);
3477 last->next = NULL;
3478
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003479 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003480 while (tok_type_(t, TOK_WHITESPACE))
3481 t = t->next;
3482
3483 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003484 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003485 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003486 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003487 free_tlist(origline);
3488 return DIRECTIVE_FOUND; /* but we did _something_ */
3489 }
3490 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003491 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003492 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003493 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003494 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003495 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003496
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003497 fp = inc_fopen(p, &xsl, &xst, true);
3498 if (fp) {
3499 p = xsl->str;
3500 fclose(fp); /* Don't actually care about the file */
3501 }
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003502 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003503 macro_start->text = nasm_quote(p, strlen(p));
3504 macro_start->type = TOK_STRING;
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04003505 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003506
3507 /*
3508 * We now have a macro name, an implicit parameter count of
3509 * zero, and a string 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);
3513 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003514 free_tlist(origline);
3515 return DIRECTIVE_FOUND;
3516 }
3517
H. Peter Anvine2c80182005-01-15 22:15:51 +00003518 case PP_STRLEN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003519 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003520 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003521
H. Peter Anvine2c80182005-01-15 22:15:51 +00003522 tline = tline->next;
3523 skip_white_(tline);
3524 tline = expand_id(tline);
3525 if (!tline || (tline->type != TOK_ID &&
3526 (tline->type != TOK_PREPROC_ID ||
3527 tline->text[1] != '$'))) {
3528 error(ERR_NONFATAL,
3529 "`%%strlen' expects a macro identifier as first parameter");
3530 free_tlist(origline);
3531 return DIRECTIVE_FOUND;
3532 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003533 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003534 last = tline;
3535 tline = expand_smacro(tline->next);
3536 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003537
H. Peter Anvine2c80182005-01-15 22:15:51 +00003538 t = tline;
3539 while (tok_type_(t, TOK_WHITESPACE))
3540 t = t->next;
3541 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003542 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003543 error(ERR_NONFATAL,
3544 "`%%strlen` requires string as second parameter");
3545 free_tlist(tline);
3546 free_tlist(origline);
3547 return DIRECTIVE_FOUND;
3548 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003549
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003550 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003551 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003552
H. Peter Anvine2c80182005-01-15 22:15:51 +00003553 /*
3554 * We now have a macro name, an implicit parameter count of
3555 * zero, and a numeric token to use as an expansion. Create
3556 * and store an SMacro.
3557 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003558 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003559 free_tlist(tline);
3560 free_tlist(origline);
3561 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003562
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003563 case PP_STRCAT:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003564 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003565 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003566
3567 tline = tline->next;
3568 skip_white_(tline);
3569 tline = expand_id(tline);
3570 if (!tline || (tline->type != TOK_ID &&
3571 (tline->type != TOK_PREPROC_ID ||
3572 tline->text[1] != '$'))) {
3573 error(ERR_NONFATAL,
3574 "`%%strcat' expects a macro identifier as first parameter");
3575 free_tlist(origline);
3576 return DIRECTIVE_FOUND;
3577 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003578 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003579 last = tline;
3580 tline = expand_smacro(tline->next);
3581 last->next = NULL;
3582
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003583 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003584 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003585 switch (t->type) {
3586 case TOK_WHITESPACE:
3587 break;
3588 case TOK_STRING:
3589 len += t->a.len = nasm_unquote(t->text, NULL);
3590 break;
3591 case TOK_OTHER:
3592 if (!strcmp(t->text, ",")) /* permit comma separators */
3593 break;
3594 /* else fall through */
3595 default:
3596 error(ERR_NONFATAL,
3597 "non-string passed to `%%strcat' (%d)", t->type);
3598 free_tlist(tline);
3599 free_tlist(origline);
3600 return DIRECTIVE_FOUND;
3601 }
3602 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003603
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003604 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003605 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003606 if (t->type == TOK_STRING) {
3607 memcpy(p, t->text, t->a.len);
3608 p += t->a.len;
3609 }
3610 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003611
3612 /*
3613 * We now have a macro name, an implicit parameter count of
3614 * zero, and a numeric token to use as an expansion. Create
3615 * and store an SMacro.
3616 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003617 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3618 macro_start->text = nasm_quote(pp, len);
3619 nasm_free(pp);
3620 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003621 free_tlist(tline);
3622 free_tlist(origline);
3623 return DIRECTIVE_FOUND;
3624
H. Peter Anvine2c80182005-01-15 22:15:51 +00003625 case PP_SUBSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003626 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003627 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003628 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003629 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003630
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003631 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003632
H. Peter Anvine2c80182005-01-15 22:15:51 +00003633 tline = tline->next;
3634 skip_white_(tline);
3635 tline = expand_id(tline);
3636 if (!tline || (tline->type != TOK_ID &&
3637 (tline->type != TOK_PREPROC_ID ||
3638 tline->text[1] != '$'))) {
3639 error(ERR_NONFATAL,
3640 "`%%substr' expects a macro identifier as first parameter");
3641 free_tlist(origline);
3642 return DIRECTIVE_FOUND;
3643 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003644 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003645 last = tline;
3646 tline = expand_smacro(tline->next);
3647 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003648
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003649 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003650 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003651 while (tok_type_(t, TOK_WHITESPACE))
3652 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003653
H. Peter Anvine2c80182005-01-15 22:15:51 +00003654 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003655 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003656 error(ERR_NONFATAL,
3657 "`%%substr` requires string as second parameter");
3658 free_tlist(tline);
3659 free_tlist(origline);
3660 return DIRECTIVE_FOUND;
3661 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003662
H. Peter Anvine2c80182005-01-15 22:15:51 +00003663 tt = t->next;
3664 tptr = &tt;
3665 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003666 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003667 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003668 if (!evalresult) {
3669 free_tlist(tline);
3670 free_tlist(origline);
3671 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003672 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003673 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3674 free_tlist(tline);
3675 free_tlist(origline);
3676 return DIRECTIVE_FOUND;
3677 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003678 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003679
3680 while (tok_type_(tt, TOK_WHITESPACE))
3681 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003682 if (!tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003683 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003684 } else {
3685 tokval.t_type = TOKEN_INVALID;
3686 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3687 pass, error, NULL);
3688 if (!evalresult) {
3689 free_tlist(tline);
3690 free_tlist(origline);
3691 return DIRECTIVE_FOUND;
3692 } else if (!is_simple(evalresult)) {
3693 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3694 free_tlist(tline);
3695 free_tlist(origline);
3696 return DIRECTIVE_FOUND;
3697 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003698 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003699 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003700
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003701 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003702 /* make start and count being in range */
3703 if (start < 0)
3704 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003705 if (count < 0)
3706 count = len + count + 1 - start;
3707 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003708 count = len - start;
3709 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003710 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003711
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003712 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003713 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003714 macro_start->type = TOK_STRING;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003715
H. Peter Anvine2c80182005-01-15 22:15:51 +00003716 /*
3717 * We now have a macro name, an implicit parameter count of
3718 * zero, and a numeric token to use as an expansion. Create
3719 * and store an SMacro.
3720 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003721 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003722 free_tlist(tline);
3723 free_tlist(origline);
3724 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003725 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003726
H. Peter Anvine2c80182005-01-15 22:15:51 +00003727 case PP_ASSIGN:
3728 case PP_IASSIGN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003729 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003730 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003731
H. Peter Anvine2c80182005-01-15 22:15:51 +00003732 tline = tline->next;
3733 skip_white_(tline);
3734 tline = expand_id(tline);
3735 if (!tline || (tline->type != TOK_ID &&
3736 (tline->type != TOK_PREPROC_ID ||
3737 tline->text[1] != '$'))) {
3738 error(ERR_NONFATAL,
3739 "`%%%sassign' expects a macro identifier",
3740 (i == PP_IASSIGN ? "i" : ""));
3741 free_tlist(origline);
3742 return DIRECTIVE_FOUND;
3743 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003744 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003745 last = tline;
3746 tline = expand_smacro(tline->next);
3747 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003748
H. Peter Anvine2c80182005-01-15 22:15:51 +00003749 t = tline;
3750 tptr = &t;
3751 tokval.t_type = TOKEN_INVALID;
3752 evalresult =
3753 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3754 free_tlist(tline);
3755 if (!evalresult) {
3756 free_tlist(origline);
3757 return DIRECTIVE_FOUND;
3758 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003759
H. Peter Anvine2c80182005-01-15 22:15:51 +00003760 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003761 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003762 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003763
H. Peter Anvine2c80182005-01-15 22:15:51 +00003764 if (!is_simple(evalresult)) {
3765 error(ERR_NONFATAL,
3766 "non-constant value given to `%%%sassign'",
3767 (i == PP_IASSIGN ? "i" : ""));
3768 free_tlist(origline);
3769 return DIRECTIVE_FOUND;
3770 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003771
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003772 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003773 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003774
H. Peter Anvine2c80182005-01-15 22:15:51 +00003775 /*
3776 * We now have a macro name, an implicit parameter count of
3777 * zero, and a numeric token to use as an expansion. Create
3778 * and store an SMacro.
3779 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003780 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003781 free_tlist(origline);
3782 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003783
H. Peter Anvine2c80182005-01-15 22:15:51 +00003784 case PP_LINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003785 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003786 /*
3787 * Syntax is `%line nnn[+mmm] [filename]'
3788 */
3789 tline = tline->next;
3790 skip_white_(tline);
3791 if (!tok_type_(tline, TOK_NUMBER)) {
3792 error(ERR_NONFATAL, "`%%line' expects line number");
3793 free_tlist(origline);
3794 return DIRECTIVE_FOUND;
3795 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003796 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003797 m = 1;
3798 tline = tline->next;
3799 if (tok_is_(tline, "+")) {
3800 tline = tline->next;
3801 if (!tok_type_(tline, TOK_NUMBER)) {
3802 error(ERR_NONFATAL, "`%%line' expects line increment");
3803 free_tlist(origline);
3804 return DIRECTIVE_FOUND;
3805 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003806 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003807 tline = tline->next;
3808 }
3809 skip_white_(tline);
3810 src_set_linnum(k);
3811 istk->lineinc = m;
3812 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003813 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003814 }
3815 free_tlist(origline);
3816 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003817
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003818 case PP_WHILE:
3819 if (defining != NULL) {
3820 if (defining->type == EXP_WHILE) {
3821 defining->def_depth ++;
3822 }
3823 return NO_DIRECTIVE_FOUND;
3824 }
3825 l = NULL;
3826 if ((istk->expansion != NULL) &&
3827 (istk->expansion->emitting == false)) {
3828 j = COND_NEVER;
3829 } else {
3830 l = new_Line();
3831 l->first = copy_Token(tline->next);
3832 j = if_condition(tline->next, i);
3833 tline->next = NULL; /* it got freed */
3834 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
3835 }
3836 ed = new_ExpDef(EXP_WHILE);
3837 ed->state = j;
3838 ed->cur_depth = 1;
3839 ed->max_depth = DEADMAN_LIMIT;
3840 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
3841 if (ed->ignoring == false) {
3842 ed->line = l;
3843 ed->last = l;
3844 } else if (l != NULL) {
3845 delete_Token(l->first);
3846 nasm_free(l);
3847 l = NULL;
3848 }
3849 ed->prev = defining;
3850 defining = ed;
3851 free_tlist(origline);
3852 return DIRECTIVE_FOUND;
3853
3854 case PP_ENDWHILE:
3855 if (defining != NULL) {
3856 if (defining->type == EXP_WHILE) {
3857 if (defining->def_depth > 0) {
3858 defining->def_depth --;
3859 return NO_DIRECTIVE_FOUND;
3860 }
3861 } else {
3862 return NO_DIRECTIVE_FOUND;
3863 }
3864 }
3865 if (tline->next != NULL) {
3866 error_precond(ERR_WARNING|ERR_PASS1,
3867 "trailing garbage after `%%endwhile' ignored");
3868 }
3869 if ((defining == NULL) || (defining->type != EXP_WHILE)) {
3870 error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'");
3871 return DIRECTIVE_FOUND;
3872 }
3873 ed = defining;
3874 defining = ed->prev;
3875 if (ed->ignoring == false) {
3876 ed->prev = expansions;
3877 expansions = ed;
3878 ei = new_ExpInv(EXP_WHILE, ed);
3879 ei->current = ed->line->next;
3880 ei->emitting = true;
3881 ei->prev = istk->expansion;
3882 istk->expansion = ei;
3883 } else {
3884 nasm_free(ed);
3885 }
3886 free_tlist(origline);
3887 return DIRECTIVE_FOUND;
3888
3889 case PP_EXITWHILE:
3890 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3891 /*
3892 * We must search along istk->expansion until we hit a
3893 * while invocation. Then we disable the emitting state(s)
3894 * between exitwhile and endwhile.
3895 */
3896 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3897 if (ei->type == EXP_WHILE) {
3898 break;
3899 }
3900 }
3901
3902 if (ei != NULL) {
3903 /*
3904 * Set all invocations leading back to the while
3905 * invocation to a non-emitting state.
3906 */
3907 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3908 eei->emitting = false;
3909 }
3910 eei->emitting = false;
3911 eei->current = NULL;
3912 eei->def->cur_depth = eei->def->max_depth;
3913 } else {
3914 error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block");
3915 }
3916 free_tlist(origline);
3917 return DIRECTIVE_FOUND;
3918
3919 case PP_COMMENT:
3920 if (defining != NULL) {
3921 if (defining->type == EXP_COMMENT) {
3922 defining->def_depth ++;
3923 }
3924 return NO_DIRECTIVE_FOUND;
3925 }
3926 ed = new_ExpDef(EXP_COMMENT);
3927 ed->ignoring = true;
3928 ed->prev = defining;
3929 defining = ed;
3930 free_tlist(origline);
3931 return DIRECTIVE_FOUND;
3932
3933 case PP_ENDCOMMENT:
3934 if (defining != NULL) {
3935 if (defining->type == EXP_COMMENT) {
3936 if (defining->def_depth > 0) {
3937 defining->def_depth --;
3938 return NO_DIRECTIVE_FOUND;
3939 }
3940 } else {
3941 return NO_DIRECTIVE_FOUND;
3942 }
3943 }
3944 if ((defining == NULL) || (defining->type != EXP_COMMENT)) {
3945 error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'");
3946 return DIRECTIVE_FOUND;
3947 }
3948 ed = defining;
3949 defining = ed->prev;
3950 nasm_free(ed);
3951 free_tlist(origline);
3952 return DIRECTIVE_FOUND;
3953
3954 case PP_FINAL:
3955 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3956 if (in_final != false) {
3957 error(ERR_FATAL, "`%%final' cannot be used recursively");
3958 }
3959 tline = tline->next;
3960 skip_white_(tline);
3961 if (tline == NULL) {
3962 error(ERR_NONFATAL, "`%%final' expects at least one parameter");
3963 } else {
3964 l = new_Line();
3965 l->first = copy_Token(tline);
3966 l->next = finals;
3967 finals = l;
3968 }
3969 free_tlist(origline);
3970 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003971
H. Peter Anvine2c80182005-01-15 22:15:51 +00003972 default:
3973 error(ERR_FATAL,
3974 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003975 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003976 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003977 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003978}
3979
3980/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003981 * Ensure that a macro parameter contains a condition code and
3982 * nothing else. Return the condition code index if so, or -1
3983 * otherwise.
3984 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003985static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003986{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003987 Token *tt;
3988 int i, j, k, m;
3989
H. Peter Anvin25a99342007-09-22 17:45:45 -07003990 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003991 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003992
H. Peter Anvineba20a72002-04-30 20:53:55 +00003993 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003994 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003995 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003996 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003997 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003998 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003999 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004000
4001 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04004002 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004003 while (j - i > 1) {
4004 k = (j + i) / 2;
4005 m = nasm_stricmp(t->text, conditions[k]);
4006 if (m == 0) {
4007 i = k;
4008 j = -2;
4009 break;
4010 } else if (m < 0) {
4011 j = k;
4012 } else
4013 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004014 }
4015 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004016 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004017 return i;
4018}
4019
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004020static bool paste_tokens(Token **head, const struct tokseq_match *m,
4021 int mnum, bool handle_paste_tokens)
H. Peter Anvind784a082009-04-20 14:01:18 -07004022{
4023 Token **tail, *t, *tt;
4024 Token **paste_head;
4025 bool did_paste = false;
4026 char *tmp;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004027 int i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004028
4029 /* Now handle token pasting... */
4030 paste_head = NULL;
4031 tail = head;
4032 while ((t = *tail) && (tt = t->next)) {
4033 switch (t->type) {
4034 case TOK_WHITESPACE:
4035 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004036 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07004037 t->next = delete_Token(tt);
4038 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004039 /* Do not advance paste_head here */
4040 tail = &t->next;
4041 }
H. Peter Anvind784a082009-04-20 14:01:18 -07004042 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004043 case TOK_PASTE: /* %+ */
4044 if (handle_paste_tokens) {
4045 /* Zap %+ and whitespace tokens to the right */
4046 while (t && (t->type == TOK_WHITESPACE ||
4047 t->type == TOK_PASTE))
4048 t = *tail = delete_Token(t);
4049 if (!paste_head || !t)
4050 break; /* Nothing to paste with */
4051 tail = paste_head;
4052 t = *tail;
4053 tt = t->next;
4054 while (tok_type_(tt, TOK_WHITESPACE))
4055 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004056 if (tt) {
4057 tmp = nasm_strcat(t->text, tt->text);
4058 delete_Token(t);
4059 tt = delete_Token(tt);
4060 t = *tail = tokenize(tmp);
4061 nasm_free(tmp);
4062 while (t->next) {
4063 tail = &t->next;
4064 t = t->next;
4065 }
4066 t->next = tt; /* Attach the remaining token chain */
4067 did_paste = true;
4068 }
4069 paste_head = tail;
4070 tail = &t->next;
4071 break;
4072 }
4073 /* else fall through */
4074 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004075 /*
4076 * Concatenation of tokens might look nontrivial
4077 * but in real it's pretty simple -- the caller
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004078 * prepares the masks of token types to be concatenated
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004079 * and we simply find matched sequences and slip
4080 * them together
4081 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004082 for (i = 0; i < mnum; i++) {
4083 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
4084 size_t len = 0;
4085 char *tmp, *p;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004086
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004087 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004088 len += strlen(tt->text);
4089 tt = tt->next;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004090 }
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004091
Cyrill Gorcunovfdd0ac52011-06-27 01:22:27 +04004092 nasm_dump_token(tt);
4093
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004094 /*
4095 * Now tt points to the first token after
4096 * the potential paste area...
4097 */
4098 if (tt != t->next) {
4099 /* We have at least two tokens... */
4100 len += strlen(t->text);
4101 p = tmp = nasm_malloc(len+1);
4102 while (t != tt) {
4103 strcpy(p, t->text);
4104 p = strchr(p, '\0');
4105 t = delete_Token(t);
4106 }
4107 t = *tail = tokenize(tmp);
4108 nasm_free(tmp);
4109 while (t->next) {
4110 tail = &t->next;
4111 t = t->next;
4112 }
4113 t->next = tt; /* Attach the remaining token chain */
4114 did_paste = true;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004115 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004116 paste_head = tail;
4117 tail = &t->next;
4118 break;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004119 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004120 }
4121 if (i >= mnum) { /* no match */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004122 tail = &t->next;
4123 if (!tok_type_(t->next, TOK_WHITESPACE))
4124 paste_head = tail;
4125 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004126 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004127 }
4128 }
4129 return did_paste;
4130}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004131
4132/*
4133 * expands to a list of tokens from %{x:y}
4134 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004135static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004136{
4137 Token *t = tline, **tt, *tm, *head;
4138 char *pos;
4139 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004140
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004141 pos = strchr(tline->text, ':');
4142 nasm_assert(pos);
4143
4144 lst = atoi(pos + 1);
4145 fst = atoi(tline->text + 1);
4146
4147 /*
4148 * only macros params are accounted so
4149 * if someone passes %0 -- we reject such
4150 * value(s)
4151 */
4152 if (lst == 0 || fst == 0)
4153 goto err;
4154
4155 /* the values should be sane */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004156 if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) ||
4157 (lst > (int)ei->nparam || lst < (-(int)ei->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004158 goto err;
4159
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004160 fst = fst < 0 ? fst + (int)ei->nparam + 1: fst;
4161 lst = lst < 0 ? lst + (int)ei->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004162
4163 /* counted from zero */
4164 fst--, lst--;
4165
4166 /*
4167 * it will be at least one token
4168 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004169 tm = ei->params[(fst + ei->rotate) % ei->nparam];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004170 t = new_Token(NULL, tm->type, tm->text, 0);
4171 head = t, tt = &t->next;
4172 if (fst < lst) {
4173 for (i = fst + 1; i <= lst; i++) {
4174 t = new_Token(NULL, TOK_OTHER, ",", 0);
4175 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004176 j = (i + ei->rotate) % ei->nparam;
4177 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004178 t = new_Token(NULL, tm->type, tm->text, 0);
4179 *tt = t, tt = &t->next;
4180 }
4181 } else {
4182 for (i = fst - 1; i >= lst; i--) {
4183 t = new_Token(NULL, TOK_OTHER, ",", 0);
4184 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004185 j = (i + ei->rotate) % ei->nparam;
4186 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004187 t = new_Token(NULL, tm->type, tm->text, 0);
4188 *tt = t, tt = &t->next;
4189 }
4190 }
4191
4192 *last = tt;
4193 return head;
4194
4195err:
4196 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
4197 &tline->text[1]);
4198 return tline;
4199}
4200
H. Peter Anvin76690a12002-04-30 20:52:49 +00004201/*
4202 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004203 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004204 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004205 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004206static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004207{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004208 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004209 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004210 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004211
4212 tail = &thead;
4213 thead = NULL;
4214
Cyrill Gorcunov2e046002011-06-26 23:33:56 +04004215 nasm_dump_stream(tline);
4216
H. Peter Anvine2c80182005-01-15 22:15:51 +00004217 while (tline) {
4218 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004219 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4220 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4221 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004222 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004223 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004224 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004225 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004226 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004227 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004228
H. Peter Anvine2c80182005-01-15 22:15:51 +00004229 t = tline;
4230 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004231
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004232 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
4233 if (ei->type == EXP_MMACRO) {
4234 break;
4235 }
4236 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004237 if (ei == NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004238 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004239 } else {
4240 pos = strchr(t->text, ':');
4241 if (!pos) {
4242 switch (t->text[1]) {
4243 /*
4244 * We have to make a substitution of one of the
4245 * forms %1, %-1, %+1, %%foo, %0.
4246 */
4247 case '0':
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004248 if ((strlen(t->text) > 2) && (t->text[2] == '0')) {
4249 type = TOK_ID;
4250 text = nasm_strdup(ei->label_text);
4251 } else {
4252 type = TOK_NUMBER;
4253 snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam);
4254 text = nasm_strdup(tmpbuf);
4255 }
4256 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004257 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004258 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004259 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004260 ei->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004261 text = nasm_strcat(tmpbuf, t->text + 2);
4262 break;
4263 case '-':
4264 n = atoi(t->text + 2) - 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];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004271 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004272 cc = find_cc(tt);
4273 if (cc == -1) {
4274 error(ERR_NONFATAL,
4275 "macro parameter %d is not a condition code",
4276 n + 1);
4277 text = NULL;
4278 } else {
4279 type = TOK_ID;
4280 if (inverse_ccs[cc] == -1) {
4281 error(ERR_NONFATAL,
4282 "condition code `%s' is not invertible",
4283 conditions[cc]);
4284 text = NULL;
4285 } else
4286 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4287 }
4288 break;
4289 case '+':
4290 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004291 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004292 tt = NULL;
4293 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004294 if (ei->nparam > 1)
4295 n = (n + ei->rotate) % ei->nparam;
4296 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004297 }
4298 cc = find_cc(tt);
4299 if (cc == -1) {
4300 error(ERR_NONFATAL,
4301 "macro parameter %d is not a condition code",
4302 n + 1);
4303 text = NULL;
4304 } else {
4305 type = TOK_ID;
4306 text = nasm_strdup(conditions[cc]);
4307 }
4308 break;
4309 default:
4310 n = atoi(t->text + 1) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004311 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004312 tt = NULL;
4313 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004314 if (ei->nparam > 1)
4315 n = (n + ei->rotate) % ei->nparam;
4316 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004317 }
4318 if (tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004319 for (i = 0; i < ei->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004320 *tail = new_Token(NULL, tt->type, tt->text, 0);
4321 tail = &(*tail)->next;
4322 tt = tt->next;
4323 }
4324 }
4325 text = NULL; /* we've done it here */
4326 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004327 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004328 } else {
4329 /*
4330 * seems we have a parameters range here
4331 */
4332 Token *head, **last;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004333 head = expand_mmac_params_range(ei, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004334 if (head != t) {
4335 *tail = head;
4336 *last = tline;
4337 tline = head;
4338 text = NULL;
4339 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004340 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004341 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004342 if (!text) {
4343 delete_Token(t);
4344 } else {
4345 *tail = t;
4346 tail = &t->next;
4347 t->type = type;
4348 nasm_free(t->text);
4349 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004350 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004351 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004352 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004353 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004354 } else if (tline->type == TOK_INDIRECT) {
4355 t = tline;
4356 tline = tline->next;
4357 tt = tokenize(t->text);
4358 tt = expand_mmac_params(tt);
4359 tt = expand_smacro(tt);
4360 *tail = tt;
4361 while (tt) {
4362 tt->a.mac = NULL; /* Necessary? */
4363 tail = &tt->next;
4364 tt = tt->next;
4365 }
4366 delete_Token(t);
4367 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004368 } else {
4369 t = *tail = tline;
4370 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004371 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004372 tail = &t->next;
4373 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004374 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004375 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004376
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004377 if (changed) {
4378 const struct tokseq_match t[] = {
4379 {
4380 PP_CONCAT_MASK(TOK_ID) |
4381 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4382 PP_CONCAT_MASK(TOK_ID) |
4383 PP_CONCAT_MASK(TOK_NUMBER) |
4384 PP_CONCAT_MASK(TOK_FLOAT) |
4385 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4386 },
4387 {
4388 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4389 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4390 }
4391 };
4392 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4393 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004394
Cyrill Gorcunov2e046002011-06-26 23:33:56 +04004395 nasm_dump_token(thead);
4396
H. Peter Anvin76690a12002-04-30 20:52:49 +00004397 return thead;
4398}
4399
4400/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004401 * Expand all single-line macro calls made in the given line.
4402 * Return the expanded version of the line. The original is deemed
4403 * to be destroyed in the process. (In reality we'll just move
4404 * Tokens from input to output a lot of the time, rather than
4405 * actually bothering to destroy and replicate.)
4406 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004407
H. Peter Anvine2c80182005-01-15 22:15:51 +00004408static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004409{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004410 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004411 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004412 Token **params;
4413 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004414 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004415 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004416 Token *org_tline = tline;
4417 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004418 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004419 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004420 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004421
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004422 /*
4423 * Trick: we should avoid changing the start token pointer since it can
4424 * be contained in "next" field of other token. Because of this
4425 * we allocate a copy of first token and work with it; at the end of
4426 * routine we copy it back
4427 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004428 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004429 tline = new_Token(org_tline->next, org_tline->type,
4430 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004431 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004432 nasm_free(org_tline->text);
4433 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004434 }
4435
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004436 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004437
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004438again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004439 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004440 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004441
H. Peter Anvine2c80182005-01-15 22:15:51 +00004442 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004443 if (!--deadman) {
4444 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004445 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004446 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004447
H. Peter Anvine2c80182005-01-15 22:15:51 +00004448 if ((mname = tline->text)) {
4449 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004450 if (tline->type == TOK_ID) {
4451 head = (SMacro *)hash_findix(&smacros, mname);
4452 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04004453 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004454 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4455 } else
4456 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004457
H. Peter Anvine2c80182005-01-15 22:15:51 +00004458 /*
4459 * We've hit an identifier. As in is_mmacro below, we first
4460 * check whether the identifier is a single-line macro at
4461 * all, then think about checking for parameters if
4462 * necessary.
4463 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004464 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004465 if (!mstrcmp(m->name, mname, m->casesense))
4466 break;
4467 if (m) {
4468 mstart = tline;
4469 params = NULL;
4470 paramsize = NULL;
4471 if (m->nparam == 0) {
4472 /*
4473 * Simple case: the macro is parameterless. Discard the
4474 * one token that the macro call took, and push the
4475 * expansion back on the to-do stack.
4476 */
4477 if (!m->expansion) {
4478 if (!strcmp("__FILE__", m->name)) {
4479 int32_t num = 0;
4480 char *file = NULL;
4481 src_get(&num, &file);
4482 tline->text = nasm_quote(file, strlen(file));
4483 tline->type = TOK_STRING;
4484 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004485 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004486 }
4487 if (!strcmp("__LINE__", m->name)) {
4488 nasm_free(tline->text);
4489 make_tok_num(tline, src_get_linnum());
4490 continue;
4491 }
4492 if (!strcmp("__BITS__", m->name)) {
4493 nasm_free(tline->text);
4494 make_tok_num(tline, globalbits);
4495 continue;
4496 }
4497 tline = delete_Token(tline);
4498 continue;
4499 }
4500 } else {
4501 /*
4502 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004503 * exists and takes parameters. We must find the
4504 * parameters in the call, count them, find the SMacro
4505 * that corresponds to that form of the macro call, and
4506 * substitute for the parameters when we expand. What a
4507 * pain.
4508 */
4509 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004510 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004511 do {
4512 t = tline->next;
4513 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004514 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004515 t->text = NULL;
4516 t = tline->next = delete_Token(t);
4517 }
4518 tline = t;
4519 } while (tok_type_(tline, TOK_WHITESPACE));
4520 if (!tok_is_(tline, "(")) {
4521 /*
4522 * This macro wasn't called with parameters: ignore
4523 * the call. (Behaviour borrowed from gnu cpp.)
4524 */
4525 tline = mstart;
4526 m = NULL;
4527 } else {
4528 int paren = 0;
4529 int white = 0;
4530 brackets = 0;
4531 nparam = 0;
4532 sparam = PARAM_DELTA;
4533 params = nasm_malloc(sparam * sizeof(Token *));
4534 params[0] = tline->next;
4535 paramsize = nasm_malloc(sparam * sizeof(int));
4536 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004537 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004538 /*
4539 * For some unusual expansions
4540 * which concatenates function call
4541 */
4542 t = tline->next;
4543 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004544 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004545 t->text = NULL;
4546 t = tline->next = delete_Token(t);
4547 }
4548 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004549
H. Peter Anvine2c80182005-01-15 22:15:51 +00004550 if (!tline) {
4551 error(ERR_NONFATAL,
4552 "macro call expects terminating `)'");
4553 break;
4554 }
4555 if (tline->type == TOK_WHITESPACE
4556 && brackets <= 0) {
4557 if (paramsize[nparam])
4558 white++;
4559 else
4560 params[nparam] = tline->next;
4561 continue; /* parameter loop */
4562 }
4563 if (tline->type == TOK_OTHER
4564 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004565 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004566 if (ch == ',' && !paren && brackets <= 0) {
4567 if (++nparam >= sparam) {
4568 sparam += PARAM_DELTA;
4569 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004570 sparam * sizeof(Token *));
4571 paramsize = nasm_realloc(paramsize,
4572 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004573 }
4574 params[nparam] = tline->next;
4575 paramsize[nparam] = 0;
4576 white = 0;
4577 continue; /* parameter loop */
4578 }
4579 if (ch == '{' &&
4580 (brackets > 0 || (brackets == 0 &&
4581 !paramsize[nparam])))
4582 {
4583 if (!(brackets++)) {
4584 params[nparam] = tline->next;
4585 continue; /* parameter loop */
4586 }
4587 }
4588 if (ch == '}' && brackets > 0)
4589 if (--brackets == 0) {
4590 brackets = -1;
4591 continue; /* parameter loop */
4592 }
4593 if (ch == '(' && !brackets)
4594 paren++;
4595 if (ch == ')' && brackets <= 0)
4596 if (--paren < 0)
4597 break;
4598 }
4599 if (brackets < 0) {
4600 brackets = 0;
4601 error(ERR_NONFATAL, "braces do not "
4602 "enclose all of macro parameter");
4603 }
4604 paramsize[nparam] += white + 1;
4605 white = 0;
4606 } /* parameter loop */
4607 nparam++;
4608 while (m && (m->nparam != nparam ||
4609 mstrcmp(m->name, mname,
4610 m->casesense)))
4611 m = m->next;
4612 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004613 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004614 "macro `%s' exists, "
4615 "but not taking %d parameters",
4616 mstart->text, nparam);
4617 }
4618 }
4619 if (m && m->in_progress)
4620 m = NULL;
4621 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004622 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004623 * Design question: should we handle !tline, which
4624 * indicates missing ')' here, or expand those
4625 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004626 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004627 */
4628 nasm_free(params);
4629 nasm_free(paramsize);
4630 tline = mstart;
4631 } else {
4632 /*
4633 * Expand the macro: we are placed on the last token of the
4634 * call, so that we can easily split the call from the
4635 * following tokens. We also start by pushing an SMAC_END
4636 * token for the cycle removal.
4637 */
4638 t = tline;
4639 if (t) {
4640 tline = t->next;
4641 t->next = NULL;
4642 }
4643 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004644 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004645 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004646 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004647 list_for_each(t, m->expansion) {
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04004648 if (is_smacro_param(t)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004649 Token *pcopy = tline, **ptail = &pcopy;
Cyrill Gorcunov0ad6a7b2011-06-30 01:36:45 +04004650 Token *ttt;
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04004651 int i, idx;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004652
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04004653 idx = smacro_get_param_idx(t);
4654 ttt = params[idx];
Cyrill Gorcunov0ad6a7b2011-06-30 01:36:45 +04004655
4656 /*
4657 * We need smacro paramters appended.
4658 */
4659 for (i = paramsize[idx]; i > 0; i--) {
4660 *ptail = new_Token(tline, ttt->type, ttt->text, 0);
4661 ptail = &(*ptail)->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004662 ttt = ttt->next;
4663 }
Cyrill Gorcunov0ad6a7b2011-06-30 01:36:45 +04004664
H. Peter Anvine2c80182005-01-15 22:15:51 +00004665 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004666 } else if (t->type == TOK_PREPROC_Q) {
4667 tt = new_Token(tline, TOK_ID, mname, 0);
4668 tline = tt;
4669 } else if (t->type == TOK_PREPROC_QQ) {
4670 tt = new_Token(tline, TOK_ID, m->name, 0);
4671 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004672 } else {
4673 tt = new_Token(tline, t->type, t->text, 0);
4674 tline = tt;
4675 }
4676 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004677
H. Peter Anvine2c80182005-01-15 22:15:51 +00004678 /*
4679 * Having done that, get rid of the macro call, and clean
4680 * up the parameters.
4681 */
4682 nasm_free(params);
4683 nasm_free(paramsize);
4684 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004685 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004686 continue; /* main token loop */
4687 }
4688 }
4689 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004690
H. Peter Anvine2c80182005-01-15 22:15:51 +00004691 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004692 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004693 tline = delete_Token(tline);
4694 } else {
4695 t = *tail = tline;
4696 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004697 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004698 t->next = NULL;
4699 tail = &t->next;
4700 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004701 }
4702
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004703 /*
4704 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004705 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004706 * TOK_IDs should be concatenated.
4707 * Also we look for %+ tokens and concatenate the tokens before and after
4708 * them (without white spaces in between).
4709 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004710 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004711 const struct tokseq_match t[] = {
4712 {
4713 PP_CONCAT_MASK(TOK_ID) |
4714 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4715 PP_CONCAT_MASK(TOK_ID) |
4716 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4717 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4718 }
4719 };
4720 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004721 /*
4722 * If we concatenated something, *and* we had previously expanded
4723 * an actual macro, scan the lines again for macros...
4724 */
4725 tline = thead;
4726 expanded = false;
4727 goto again;
4728 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004729 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004730
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004731err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004732 if (org_tline) {
4733 if (thead) {
4734 *org_tline = *thead;
4735 /* since we just gave text to org_line, don't free it */
4736 thead->text = NULL;
4737 delete_Token(thead);
4738 } else {
4739 /* the expression expanded to empty line;
4740 we can't return NULL for some reasons
4741 we just set the line to a single WHITESPACE token. */
4742 memset(org_tline, 0, sizeof(*org_tline));
4743 org_tline->text = NULL;
4744 org_tline->type = TOK_WHITESPACE;
4745 }
4746 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004747 }
4748
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004749 return thead;
4750}
4751
4752/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004753 * Similar to expand_smacro but used exclusively with macro identifiers
4754 * right before they are fetched in. The reason is that there can be
4755 * identifiers consisting of several subparts. We consider that if there
4756 * are more than one element forming the name, user wants a expansion,
4757 * otherwise it will be left as-is. Example:
4758 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004759 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004760 *
4761 * the identifier %$abc will be left as-is so that the handler for %define
4762 * will suck it and define the corresponding value. Other case:
4763 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004764 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004765 *
4766 * In this case user wants name to be expanded *before* %define starts
4767 * working, so we'll expand %$abc into something (if it has a value;
4768 * otherwise it will be left as-is) then concatenate all successive
4769 * PP_IDs into one.
4770 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004771static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004772{
4773 Token *cur, *oldnext = NULL;
4774
H. Peter Anvin734b1882002-04-30 21:01:08 +00004775 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004776 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004777
4778 cur = tline;
4779 while (cur->next &&
Cyrill Gorcunov5b6c96b2011-06-30 00:22:53 +04004780 (cur->next->type == TOK_ID ||
4781 cur->next->type == TOK_PREPROC_ID ||
4782 cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004783 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004784
4785 /* If identifier consists of just one token, don't expand */
4786 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004787 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004788
H. Peter Anvine2c80182005-01-15 22:15:51 +00004789 if (cur) {
4790 oldnext = cur->next; /* Detach the tail past identifier */
4791 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004792 }
4793
H. Peter Anvin734b1882002-04-30 21:01:08 +00004794 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004795
H. Peter Anvine2c80182005-01-15 22:15:51 +00004796 if (cur) {
4797 /* expand_smacro possibly changhed tline; re-scan for EOL */
4798 cur = tline;
4799 while (cur && cur->next)
4800 cur = cur->next;
4801 if (cur)
4802 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004803 }
4804
4805 return tline;
4806}
4807
4808/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004809 * Determine whether the given line constitutes a multi-line macro
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004810 * call, and return the ExpDef structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004811 * to check for an initial label - that's taken care of in
4812 * expand_mmacro - but must check numbers of parameters. Guaranteed
4813 * to be called with tline->type == TOK_ID, so the putative macro
4814 * name is easy to find.
4815 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004816static ExpDef *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004817{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004818 ExpDef *head, *ed;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004819 Token **params;
4820 int nparam;
4821
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004822 head = (ExpDef *) hash_findix(&expdefs, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004823
4824 /*
4825 * Efficiency: first we see if any macro exists with the given
4826 * name. If not, we can return NULL immediately. _Then_ we
4827 * count the parameters, and then we look further along the
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004828 * list if necessary to find the proper ExpDef.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004829 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004830 list_for_each(ed, head)
4831 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004832 break;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004833 if (!ed)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004834 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004835
4836 /*
4837 * OK, we have a potential macro. Count and demarcate the
4838 * parameters.
4839 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004840 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004841
4842 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004843 * So we know how many parameters we've got. Find the ExpDef
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004844 * structure that handles this number.
4845 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004846 while (ed) {
4847 if (ed->nparam_min <= nparam
4848 && (ed->plus || nparam <= ed->nparam_max)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004849 /*
4850 * It's right, and we can use it. Add its default
4851 * parameters to the end of our list if necessary.
4852 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004853 if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004854 params =
4855 nasm_realloc(params,
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004856 ((ed->nparam_min + ed->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004857 1) * sizeof(*params)));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004858 while (nparam < ed->nparam_min + ed->ndefs) {
4859 params[nparam] = ed->defaults[nparam - ed->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004860 nparam++;
4861 }
4862 }
4863 /*
4864 * If we've gone over the maximum parameter count (and
4865 * we're in Plus mode), ignore parameters beyond
4866 * nparam_max.
4867 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004868 if (ed->plus && nparam > ed->nparam_max)
4869 nparam = ed->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004870 /*
4871 * Then terminate the parameter list, and leave.
4872 */
4873 if (!params) { /* need this special case */
4874 params = nasm_malloc(sizeof(*params));
4875 nparam = 0;
4876 }
4877 params[nparam] = NULL;
4878 *params_array = params;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004879 return ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004880 }
4881 /*
4882 * This one wasn't right: look for the next one with the
4883 * same name.
4884 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004885 list_for_each(ed, ed->next)
4886 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004887 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004888 }
4889
4890 /*
4891 * After all that, we didn't find one with the right number of
4892 * parameters. Issue a warning, and fail to expand the macro.
4893 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004894 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004895 "macro `%s' exists, but not taking %d parameters",
4896 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004897 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004898 return NULL;
4899}
4900
4901/*
4902 * Expand the multi-line macro call made by the given line, if
4903 * there is one to be expanded. If there is, push the expansion on
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004904 * istk->expansion and return true. Otherwise return false.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004905 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004906static bool expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004907{
H. Peter Anvineba20a72002-04-30 20:53:55 +00004908 Token *label = NULL;
4909 int dont_prepend = 0;
Cyrill Gorcunovfb27fc22011-06-25 12:08:30 +04004910 Token **params, *t;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004911 Line *l = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004912 ExpDef *ed;
4913 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004914 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004915 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004916
4917 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004918 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004919 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004920 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004921 return false;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004922 ed = is_mmacro(t, &params);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004923 if (ed != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004924 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004925 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004926 Token *last;
4927 /*
4928 * We have an id which isn't a macro call. We'll assume
4929 * it might be a label; we'll also check to see if a
4930 * colon follows it. Then, if there's another id after
4931 * that lot, we'll check it again for macro-hood.
4932 */
4933 label = last = t;
4934 t = t->next;
4935 if (tok_type_(t, TOK_WHITESPACE))
4936 last = t, t = t->next;
4937 if (tok_is_(t, ":")) {
4938 dont_prepend = 1;
4939 last = t, t = t->next;
4940 if (tok_type_(t, TOK_WHITESPACE))
4941 last = t, t = t->next;
4942 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004943 if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, &params)))
4944 return false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004945 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004946 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004947 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004948 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004949
4950 /*
4951 * Fix up the parameters: this involves stripping leading and
4952 * trailing whitespace, then stripping braces if they are
4953 * present.
4954 */
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004955 for (nparam = 0; params[nparam]; nparam++)
4956 ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004957 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004958
H. Peter Anvine2c80182005-01-15 22:15:51 +00004959 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004960 int brace = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004961 int comma = (!ed->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004962
H. Peter Anvine2c80182005-01-15 22:15:51 +00004963 t = params[i];
4964 skip_white_(t);
4965 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004966 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004967 params[i] = t;
4968 paramlen[i] = 0;
4969 while (t) {
4970 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4971 break; /* ... because we have hit a comma */
4972 if (comma && t->type == TOK_WHITESPACE
4973 && tok_is_(t->next, ","))
4974 break; /* ... or a space then a comma */
4975 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4976 break; /* ... or a brace */
4977 t = t->next;
4978 paramlen[i]++;
4979 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004980 }
4981
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004982 if (ed->cur_depth >= ed->max_depth) {
4983 if (ed->max_depth > 1) {
4984 error(ERR_WARNING,
4985 "reached maximum macro recursion depth of %i for %s",
4986 ed->max_depth,ed->name);
4987 }
4988 return false;
4989 } else {
4990 ed->cur_depth ++;
4991 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004992
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004993 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004994 * OK, we have found a ExpDef structure representing a
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004995 * previously defined mmacro. Create an expansion invocation
4996 * and point it back to the expansion definition. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004997 * parameter tokens and macro-local tokens doesn't get done
4998 * until the single-line macro substitution process; this is
4999 * because delaying them allows us to change the semantics
5000 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005001 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005002 ei = new_ExpInv(EXP_MMACRO, ed);
5003 ei->name = nasm_strdup(mname);
5004 //ei->label = label;
5005 //ei->label_text = detoken(label, false);
5006 ei->current = ed->line;
5007 ei->emitting = true;
5008 //ei->iline = tline;
5009 ei->params = params;
5010 ei->nparam = nparam;
5011 ei->rotate = 0;
5012 ei->paramlen = paramlen;
5013 ei->lineno = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005014
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005015 ei->prev = istk->expansion;
5016 istk->expansion = ei;
5017
5018 /*
5019 * Special case: detect %00 on first invocation; if found,
5020 * avoid emitting any labels that precede the mmacro call.
5021 * ed->prepend is set to -1 when %00 is detected, else 1.
5022 */
5023 if (ed->prepend == 0) {
5024 for (l = ed->line; l != NULL; l = l->next) {
5025 for (t = l->first; t != NULL; t = t->next) {
5026 if ((t->type == TOK_PREPROC_ID) &&
5027 (strlen(t->text) == 3) &&
5028 (t->text[1] == '0') && (t->text[2] == '0')) {
5029 dont_prepend = -1;
5030 break;
5031 }
5032 }
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005033 if (dont_prepend < 0)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005034 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005035 }
5036 ed->prepend = ((dont_prepend < 0) ? -1 : 1);
5037 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005038
5039 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00005040 * If we had a label, push it on as the first line of
5041 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005042 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005043 if (label != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005044 if (ed->prepend < 0) {
5045 ei->label_text = detoken(label, false);
5046 } else {
5047 if (dont_prepend == 0) {
5048 t = label;
5049 while (t->next != NULL) {
5050 t = t->next;
5051 }
5052 t->next = new_Token(NULL, TOK_OTHER, ":", 0);
5053 }
5054 l = new_Line();
5055 l->first = copy_Token(label);
5056 l->next = ei->current;
5057 ei->current = l;
5058 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005059 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005060
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005061 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005062
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005063 istk->mmac_depth++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005064 return true;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005065}
5066
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005067/* The function that actually does the error reporting */
5068static void verror(int severity, const char *fmt, va_list arg)
5069{
5070 char buff[1024];
5071
5072 vsnprintf(buff, sizeof(buff), fmt, arg);
5073
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005074 if (istk && istk->mmac_depth > 0) {
5075 ExpInv *ei = istk->expansion;
5076 int lineno = ei->lineno;
5077 while (ei) {
5078 if (ei->type == EXP_MMACRO)
5079 break;
5080 lineno += ei->relno;
5081 ei = ei->prev;
5082 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005083 nasm_error(severity, "(%s:%d) %s", ei->def->name,
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005084 lineno, buff);
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005085 } else {
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005086 nasm_error(severity, "%s", buff);
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005087 }
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005088}
5089
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005090/*
5091 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07005092 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005093 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005094static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005095{
5096 va_list arg;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005097 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005098 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00005099 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005100}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005101
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005102/*
5103 * Because %else etc are evaluated in the state context
5104 * of the previous branch, errors might get lost with error():
5105 * %if 0 ... %else trailing garbage ... %endif
5106 * So %else etc should report errors with this function.
5107 */
5108static void error_precond(int severity, const char *fmt, ...)
5109{
5110 va_list arg;
5111
5112 /* Only ignore the error if it's really in a dead branch */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005113 if ((istk != NULL) &&
5114 (istk->expansion != NULL) &&
5115 (istk->expansion->type == EXP_IF) &&
5116 (istk->expansion->def->state == COND_NEVER))
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005117 return;
5118
5119 va_start(arg, fmt);
5120 verror(severity, fmt, arg);
5121 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005122}
5123
H. Peter Anvin734b1882002-04-30 21:01:08 +00005124static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005125pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005126{
H. Peter Anvin7383b402008-09-24 10:20:40 -07005127 Token *t;
5128
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005129 cstk = NULL;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005130 istk = nasm_zalloc(sizeof(Include));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005131 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00005132 src_set_fname(nasm_strdup(file));
5133 src_set_linnum(0);
5134 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005135 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07005136 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00005137 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005138 defining = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005139 finals = NULL;
5140 in_final = false;
Charles Crayned4200be2008-07-12 16:42:33 -07005141 nested_mac_count = 0;
5142 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005143 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005144 unique = 0;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005145 if (tasm_compatible_mode)
H. Peter Anvina4835d42008-05-20 14:21:29 -07005146 stdmacpos = nasm_stdmac;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005147 else
H. Peter Anvina4835d42008-05-20 14:21:29 -07005148 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvind2456592008-06-19 15:04:18 -07005149 any_extrastdmac = extrastdmac && *extrastdmac;
5150 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005151 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005152
5153 /*
5154 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5155 * The caller, however, will also pass in 3 for preprocess-only so
5156 * we can set __PASS__ accordingly.
5157 */
5158 pass = apass > 2 ? 2 : apass;
5159
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005160 dephead = deptail = deplist;
5161 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005162 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
5163 sl->next = NULL;
5164 strcpy(sl->str, file);
5165 *deptail = sl;
5166 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005167 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07005168
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005169 /*
5170 * Define the __PASS__ macro. This is defined here unlike
5171 * all the other builtins, because it is special -- it varies between
5172 * passes.
5173 */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005174 t = nasm_zalloc(sizeof(*t));
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005175 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07005176 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005177}
5178
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005179static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005180{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005181 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005182 Token *tline;
Keith Kanios9858cec2010-11-08 00:58:02 -06005183 ExpDef *ed;
5184 ExpInv *ei;
5185 Line *l;
5186 int j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005187
H. Peter Anvine2c80182005-01-15 22:15:51 +00005188 while (1) {
5189 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005190 * Fetch a tokenized line, either from the expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005191 * buffer or from the input file.
5192 */
5193 tline = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005194
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005195 while (1) { /* until we get a line we can use */
5196 /*
5197 * Fetch a tokenized line from the expansion buffer
5198 */
5199 if (istk->expansion != NULL) {
5200 ei = istk->expansion;
5201 if (ei->current != NULL) {
5202 if (ei->emitting == false) {
5203 ei->current = NULL;
5204 continue;
5205 }
5206 l = ei->current;
5207 ei->current = l->next;
5208 ei->lineno++;
5209 tline = copy_Token(l->first);
5210 if (((ei->type == EXP_REP) ||
5211 (ei->type == EXP_MMACRO) ||
5212 (ei->type == EXP_WHILE))
5213 && (ei->def->nolist == false)) {
5214 char *p = detoken(tline, false);
5215 list->line(LIST_MACRO, p);
5216 nasm_free(p);
5217 }
5218 if (ei->linnum > -1) {
5219 src_set_linnum(src_get_linnum() + 1);
5220 }
5221 break;
5222 } else if ((ei->type == EXP_REP) &&
5223 (ei->def->cur_depth < ei->def->max_depth)) {
5224 ei->def->cur_depth ++;
5225 ei->current = ei->def->line;
5226 ei->lineno = 0;
5227 continue;
5228 } else if ((ei->type == EXP_WHILE) &&
5229 (ei->def->cur_depth < ei->def->max_depth)) {
5230 ei->current = ei->def->line;
5231 ei->lineno = 0;
5232 tline = copy_Token(ei->current->first);
5233 j = if_condition(tline, PP_WHILE);
5234 tline = NULL;
5235 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
5236 if (j == COND_IF_TRUE) {
5237 ei->current = ei->current->next;
5238 ei->def->cur_depth ++;
5239 } else {
5240 ei->emitting = false;
5241 ei->current = NULL;
5242 ei->def->cur_depth = ei->def->max_depth;
5243 }
5244 continue;
5245 } else {
5246 istk->expansion = ei->prev;
5247 ed = ei->def;
5248 if (ed != NULL) {
5249 if ((ei->emitting == true) &&
5250 (ed->max_depth == DEADMAN_LIMIT) &&
5251 (ed->cur_depth == DEADMAN_LIMIT)
5252 ) {
5253 error(ERR_FATAL, "runaway expansion detected, aborting");
5254 }
5255 if (ed->cur_depth > 0) {
5256 ed->cur_depth --;
Keith Kanios94124652010-12-18 11:47:28 -06005257 } else if (ed->type != EXP_MMACRO) {
Keith Kanios6a7c3e92010-12-18 11:49:53 -06005258 expansions = ed->prev;
5259 free_expdef(ed);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005260 }
5261 if ((ei->type == EXP_REP) ||
5262 (ei->type == EXP_MMACRO) ||
5263 (ei->type == EXP_WHILE)) {
5264 list->downlevel(LIST_MACRO);
5265 if (ei->type == EXP_MMACRO) {
5266 istk->mmac_depth--;
5267 }
5268 }
5269 }
5270 if (ei->linnum > -1) {
5271 src_set_linnum(ei->linnum);
5272 }
5273 free_expinv(ei);
5274 continue;
5275 }
5276 }
5277
5278 /*
5279 * Read in line from input and tokenize
5280 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005281 line = read_line();
5282 if (line) { /* from the current input file */
5283 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005284 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005285 nasm_free(line);
5286 break;
5287 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005288
H. Peter Anvine2c80182005-01-15 22:15:51 +00005289 /*
5290 * The current file has ended; work down the istk
5291 */
5292 {
5293 Include *i = istk;
5294 fclose(i->fp);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005295 if (i->expansion != NULL) {
5296 error(ERR_FATAL,
5297 "end of file while still in an expansion");
5298 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005299 /* only set line and file name if there's a next node */
5300 if (i->next) {
5301 src_set_linnum(i->lineno);
Cyrill Gorcunovd34a1082011-03-07 11:23:08 +03005302 nasm_free(src_set_fname(nasm_strdup(i->fname)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005303 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005304 if ((i->next == NULL) && (finals != NULL)) {
5305 in_final = true;
5306 ei = new_ExpInv(EXP_FINAL, NULL);
5307 ei->emitting = true;
5308 ei->current = finals;
5309 istk->expansion = ei;
5310 finals = NULL;
5311 continue;
5312 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005313 istk = i->next;
5314 list->downlevel(LIST_INCLUDE);
5315 nasm_free(i);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005316 if (istk == NULL) {
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005317 if (finals != NULL)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005318 in_final = true;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005319 else
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005320 return NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005321 }
5322 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005323 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005324 }
5325
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005326 if (defining == NULL)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005327 tline = expand_mmac_params(tline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005328
H. Peter Anvine2c80182005-01-15 22:15:51 +00005329 /*
5330 * Check the line to see if it's a preprocessor directive.
5331 */
5332 if (do_directive(tline) == DIRECTIVE_FOUND) {
5333 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005334 } else if (defining != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005335 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005336 * We're defining an expansion. We emit nothing at all,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005337 * and just shove the tokenized line on to the definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005338 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005339 if (defining->ignoring == false) {
5340 Line *l = new_Line();
5341 l->first = tline;
5342 if (defining->line == NULL) {
5343 defining->line = l;
5344 defining->last = l;
5345 } else {
5346 defining->last->next = l;
5347 defining->last = l;
5348 }
5349 } else {
Keith Kanios104803d2010-12-18 11:05:46 -06005350 free_tlist(tline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005351 }
5352 defining->linecount++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005353 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005354 } else if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005355 (istk->expansion->emitting != true)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005356 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005357 * We're in a non-emitting branch of an expansion.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005358 * Emit nothing at all, not even a blank line: when we
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005359 * emerge from the expansion we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005360 * directive so we keep our place correctly.
5361 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005362 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005363 continue;
5364 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005365 tline = expand_smacro(tline);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005366 if (expand_mmacro(tline) != true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005367 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005368 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005369 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005370 line = detoken(tline, true);
5371 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005372 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005373 } else {
5374 continue;
5375 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005376 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005377 }
5378 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005379}
5380
H. Peter Anvine2c80182005-01-15 22:15:51 +00005381static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005382{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005383 if (defining != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005384 error(ERR_NONFATAL, "end of file while still defining an expansion");
5385 while (defining != NULL) {
5386 ExpDef *ed = defining;
5387 defining = ed->prev;
5388 free_expdef(ed);
5389 }
5390 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005391 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005392 while (cstk != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005393 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005394 free_macros();
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005395 while (istk != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005396 Include *i = istk;
5397 istk = istk->next;
5398 fclose(i->fp);
5399 nasm_free(i->fname);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005400 while (i->expansion != NULL) {
5401 ExpInv *ei = i->expansion;
5402 i->expansion = ei->prev;
5403 free_expinv(ei);
5404 }
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005405 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005406 }
5407 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005408 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005409 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005410 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005411 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005412 free_llist(predef);
5413 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005414 while ((i = ipath)) {
5415 ipath = i->next;
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04005416 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005417 nasm_free(i);
5418 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005419 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005420}
5421
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005422void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005423{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005424 IncPath *i = nasm_zalloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005425
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005426 if (path)
5427 i->path = nasm_strdup(path);
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005428
H. Peter Anvin89cee572009-07-15 09:16:54 -04005429 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005430 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005431 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005432 j = j->next;
5433 j->next = i;
5434 } else {
5435 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005436 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005437}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005438
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005439void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005440{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005441 Token *inc, *space, *name;
5442 Line *l;
5443
H. Peter Anvin734b1882002-04-30 21:01:08 +00005444 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5445 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5446 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005447
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005448 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005449 l->next = predef;
5450 l->first = inc;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005451 predef = l;
5452}
5453
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005454void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005455{
5456 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005457 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005458 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005459
5460 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005461 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5462 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005463 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005464 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005465 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005466 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005467 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005468
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005469 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005470 l->next = predef;
5471 l->first = def;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005472 predef = l;
5473}
5474
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005475void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005476{
5477 Token *def, *space;
5478 Line *l;
5479
H. Peter Anvin734b1882002-04-30 21:01:08 +00005480 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5481 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005482 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005483
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005484 l = new_Line();
H. Peter Anvin620515a2002-04-30 20:57:38 +00005485 l->next = predef;
5486 l->first = def;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005487 predef = l;
5488}
5489
Keith Kaniosb7a89542007-04-12 02:40:54 +00005490/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005491 * This function is used to assist with "runtime" preprocessor
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005492 * directives, e.g. pp_runtime("%define __BITS__ 64");
Keith Kaniosb7a89542007-04-12 02:40:54 +00005493 *
5494 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5495 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5496 */
5497
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005498void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005499{
5500 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005501
Keith Kaniosb7a89542007-04-12 02:40:54 +00005502 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005503 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005504 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005505
Keith Kaniosb7a89542007-04-12 02:40:54 +00005506}
5507
H. Peter Anvina70547f2008-07-19 21:44:26 -07005508void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005509{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005510 extrastdmac = macros;
5511}
5512
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005513static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005514{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005515 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005516 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005517 tok->text = nasm_strdup(numbuf);
5518 tok->type = TOK_NUMBER;
5519}
5520
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +04005521struct preproc_ops nasmpp = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005522 pp_reset,
5523 pp_getline,
5524 pp_cleanup
5525};