blob: 4927772932051668d72aed06b4fb0bbbc081fead [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
Cyrill Gorcunov99a055a2012-02-27 11:11:33 +04004029 nasm_dump_stream(*head);
4030 nasm_dump_token(*head);
4031
H. Peter Anvind784a082009-04-20 14:01:18 -07004032 /* Now handle token pasting... */
4033 paste_head = NULL;
4034 tail = head;
4035 while ((t = *tail) && (tt = t->next)) {
4036 switch (t->type) {
4037 case TOK_WHITESPACE:
4038 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004039 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07004040 t->next = delete_Token(tt);
4041 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004042 /* Do not advance paste_head here */
4043 tail = &t->next;
4044 }
H. Peter Anvind784a082009-04-20 14:01:18 -07004045 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004046 case TOK_PASTE: /* %+ */
4047 if (handle_paste_tokens) {
4048 /* Zap %+ and whitespace tokens to the right */
4049 while (t && (t->type == TOK_WHITESPACE ||
4050 t->type == TOK_PASTE))
4051 t = *tail = delete_Token(t);
Cyrill Gorcunov99a055a2012-02-27 11:11:33 +04004052 if (!t) { /* Dangling %+ term */
4053 if (paste_head)
4054 (*paste_head)->next = NULL;
4055 else
4056 *head = NULL;
4057 return did_paste;
4058 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004059 tail = paste_head;
4060 t = *tail;
4061 tt = t->next;
4062 while (tok_type_(tt, TOK_WHITESPACE))
4063 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004064 if (tt) {
4065 tmp = nasm_strcat(t->text, tt->text);
4066 delete_Token(t);
4067 tt = delete_Token(tt);
4068 t = *tail = tokenize(tmp);
4069 nasm_free(tmp);
4070 while (t->next) {
4071 tail = &t->next;
4072 t = t->next;
4073 }
4074 t->next = tt; /* Attach the remaining token chain */
4075 did_paste = true;
4076 }
4077 paste_head = tail;
4078 tail = &t->next;
4079 break;
4080 }
4081 /* else fall through */
4082 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004083 /*
4084 * Concatenation of tokens might look nontrivial
4085 * but in real it's pretty simple -- the caller
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004086 * prepares the masks of token types to be concatenated
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004087 * and we simply find matched sequences and slip
4088 * them together
4089 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004090 for (i = 0; i < mnum; i++) {
4091 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
4092 size_t len = 0;
4093 char *tmp, *p;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004094
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004095 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004096 len += strlen(tt->text);
4097 tt = tt->next;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004098 }
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004099
Cyrill Gorcunovfdd0ac52011-06-27 01:22:27 +04004100 nasm_dump_token(tt);
4101
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004102 /*
4103 * Now tt points to the first token after
4104 * the potential paste area...
4105 */
4106 if (tt != t->next) {
4107 /* We have at least two tokens... */
4108 len += strlen(t->text);
4109 p = tmp = nasm_malloc(len+1);
4110 while (t != tt) {
4111 strcpy(p, t->text);
4112 p = strchr(p, '\0');
4113 t = delete_Token(t);
4114 }
4115 t = *tail = tokenize(tmp);
4116 nasm_free(tmp);
4117 while (t->next) {
4118 tail = &t->next;
4119 t = t->next;
4120 }
4121 t->next = tt; /* Attach the remaining token chain */
4122 did_paste = true;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004123 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004124 paste_head = tail;
4125 tail = &t->next;
4126 break;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004127 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004128 }
4129 if (i >= mnum) { /* no match */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004130 tail = &t->next;
4131 if (!tok_type_(t->next, TOK_WHITESPACE))
4132 paste_head = tail;
4133 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004134 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004135 }
4136 }
4137 return did_paste;
4138}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004139
4140/*
4141 * expands to a list of tokens from %{x:y}
4142 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004143static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004144{
4145 Token *t = tline, **tt, *tm, *head;
4146 char *pos;
4147 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004148
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004149 pos = strchr(tline->text, ':');
4150 nasm_assert(pos);
4151
4152 lst = atoi(pos + 1);
4153 fst = atoi(tline->text + 1);
4154
4155 /*
4156 * only macros params are accounted so
4157 * if someone passes %0 -- we reject such
4158 * value(s)
4159 */
4160 if (lst == 0 || fst == 0)
4161 goto err;
4162
4163 /* the values should be sane */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004164 if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) ||
4165 (lst > (int)ei->nparam || lst < (-(int)ei->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004166 goto err;
4167
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004168 fst = fst < 0 ? fst + (int)ei->nparam + 1: fst;
4169 lst = lst < 0 ? lst + (int)ei->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004170
4171 /* counted from zero */
4172 fst--, lst--;
4173
4174 /*
4175 * it will be at least one token
4176 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004177 tm = ei->params[(fst + ei->rotate) % ei->nparam];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004178 t = new_Token(NULL, tm->type, tm->text, 0);
4179 head = t, tt = &t->next;
4180 if (fst < lst) {
4181 for (i = fst + 1; i <= lst; i++) {
4182 t = new_Token(NULL, TOK_OTHER, ",", 0);
4183 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004184 j = (i + ei->rotate) % ei->nparam;
4185 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004186 t = new_Token(NULL, tm->type, tm->text, 0);
4187 *tt = t, tt = &t->next;
4188 }
4189 } else {
4190 for (i = fst - 1; i >= lst; i--) {
4191 t = new_Token(NULL, TOK_OTHER, ",", 0);
4192 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004193 j = (i + ei->rotate) % ei->nparam;
4194 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004195 t = new_Token(NULL, tm->type, tm->text, 0);
4196 *tt = t, tt = &t->next;
4197 }
4198 }
4199
4200 *last = tt;
4201 return head;
4202
4203err:
4204 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
4205 &tline->text[1]);
4206 return tline;
4207}
4208
H. Peter Anvin76690a12002-04-30 20:52:49 +00004209/*
4210 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004211 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004212 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004213 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004214static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004215{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004216 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004217 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004218 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004219
4220 tail = &thead;
4221 thead = NULL;
4222
Cyrill Gorcunov2e046002011-06-26 23:33:56 +04004223 nasm_dump_stream(tline);
4224
H. Peter Anvine2c80182005-01-15 22:15:51 +00004225 while (tline) {
4226 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004227 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4228 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4229 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004230 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004231 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004232 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004233 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004234 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004235 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004236
H. Peter Anvine2c80182005-01-15 22:15:51 +00004237 t = tline;
4238 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004239
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004240 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
4241 if (ei->type == EXP_MMACRO) {
4242 break;
4243 }
4244 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004245 if (ei == NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004246 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004247 } else {
4248 pos = strchr(t->text, ':');
4249 if (!pos) {
4250 switch (t->text[1]) {
4251 /*
4252 * We have to make a substitution of one of the
4253 * forms %1, %-1, %+1, %%foo, %0.
4254 */
4255 case '0':
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004256 if ((strlen(t->text) > 2) && (t->text[2] == '0')) {
4257 type = TOK_ID;
4258 text = nasm_strdup(ei->label_text);
4259 } else {
4260 type = TOK_NUMBER;
4261 snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam);
4262 text = nasm_strdup(tmpbuf);
4263 }
4264 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004265 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004266 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004267 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004268 ei->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004269 text = nasm_strcat(tmpbuf, t->text + 2);
4270 break;
4271 case '-':
4272 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004273 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004274 tt = NULL;
4275 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004276 if (ei->nparam > 1)
4277 n = (n + ei->rotate) % ei->nparam;
4278 tt = ei->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004279 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004280 cc = find_cc(tt);
4281 if (cc == -1) {
4282 error(ERR_NONFATAL,
4283 "macro parameter %d is not a condition code",
4284 n + 1);
4285 text = NULL;
4286 } else {
4287 type = TOK_ID;
4288 if (inverse_ccs[cc] == -1) {
4289 error(ERR_NONFATAL,
4290 "condition code `%s' is not invertible",
4291 conditions[cc]);
4292 text = NULL;
4293 } else
4294 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4295 }
4296 break;
4297 case '+':
4298 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004299 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004300 tt = NULL;
4301 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004302 if (ei->nparam > 1)
4303 n = (n + ei->rotate) % ei->nparam;
4304 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004305 }
4306 cc = find_cc(tt);
4307 if (cc == -1) {
4308 error(ERR_NONFATAL,
4309 "macro parameter %d is not a condition code",
4310 n + 1);
4311 text = NULL;
4312 } else {
4313 type = TOK_ID;
4314 text = nasm_strdup(conditions[cc]);
4315 }
4316 break;
4317 default:
4318 n = atoi(t->text + 1) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004319 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004320 tt = NULL;
4321 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004322 if (ei->nparam > 1)
4323 n = (n + ei->rotate) % ei->nparam;
4324 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004325 }
4326 if (tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004327 for (i = 0; i < ei->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004328 *tail = new_Token(NULL, tt->type, tt->text, 0);
4329 tail = &(*tail)->next;
4330 tt = tt->next;
4331 }
4332 }
4333 text = NULL; /* we've done it here */
4334 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004335 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004336 } else {
4337 /*
4338 * seems we have a parameters range here
4339 */
4340 Token *head, **last;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004341 head = expand_mmac_params_range(ei, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004342 if (head != t) {
4343 *tail = head;
4344 *last = tline;
4345 tline = head;
4346 text = NULL;
4347 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004348 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004349 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004350 if (!text) {
4351 delete_Token(t);
4352 } else {
4353 *tail = t;
4354 tail = &t->next;
4355 t->type = type;
4356 nasm_free(t->text);
4357 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004358 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004359 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004360 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004361 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004362 } else if (tline->type == TOK_INDIRECT) {
4363 t = tline;
4364 tline = tline->next;
4365 tt = tokenize(t->text);
4366 tt = expand_mmac_params(tt);
4367 tt = expand_smacro(tt);
4368 *tail = tt;
4369 while (tt) {
4370 tt->a.mac = NULL; /* Necessary? */
4371 tail = &tt->next;
4372 tt = tt->next;
4373 }
4374 delete_Token(t);
4375 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004376 } else {
4377 t = *tail = tline;
4378 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004379 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004380 tail = &t->next;
4381 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004382 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004383 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004384
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004385 if (changed) {
4386 const struct tokseq_match t[] = {
4387 {
4388 PP_CONCAT_MASK(TOK_ID) |
4389 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4390 PP_CONCAT_MASK(TOK_ID) |
4391 PP_CONCAT_MASK(TOK_NUMBER) |
4392 PP_CONCAT_MASK(TOK_FLOAT) |
4393 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4394 },
4395 {
4396 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4397 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4398 }
4399 };
4400 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4401 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004402
Cyrill Gorcunov2e046002011-06-26 23:33:56 +04004403 nasm_dump_token(thead);
4404
H. Peter Anvin76690a12002-04-30 20:52:49 +00004405 return thead;
4406}
4407
4408/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004409 * Expand all single-line macro calls made in the given line.
4410 * Return the expanded version of the line. The original is deemed
4411 * to be destroyed in the process. (In reality we'll just move
4412 * Tokens from input to output a lot of the time, rather than
4413 * actually bothering to destroy and replicate.)
4414 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004415
H. Peter Anvine2c80182005-01-15 22:15:51 +00004416static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004417{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004418 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004419 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004420 Token **params;
4421 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004422 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004423 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004424 Token *org_tline = tline;
4425 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004426 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004427 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004428 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004429
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004430 /*
4431 * Trick: we should avoid changing the start token pointer since it can
4432 * be contained in "next" field of other token. Because of this
4433 * we allocate a copy of first token and work with it; at the end of
4434 * routine we copy it back
4435 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004436 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004437 tline = new_Token(org_tline->next, org_tline->type,
4438 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004439 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004440 nasm_free(org_tline->text);
4441 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004442 }
4443
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004444 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004445
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004446again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004447 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004448 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004449
H. Peter Anvine2c80182005-01-15 22:15:51 +00004450 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004451 if (!--deadman) {
4452 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004453 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004454 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004455
H. Peter Anvine2c80182005-01-15 22:15:51 +00004456 if ((mname = tline->text)) {
4457 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004458 if (tline->type == TOK_ID) {
4459 head = (SMacro *)hash_findix(&smacros, mname);
4460 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04004461 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004462 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4463 } else
4464 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004465
H. Peter Anvine2c80182005-01-15 22:15:51 +00004466 /*
4467 * We've hit an identifier. As in is_mmacro below, we first
4468 * check whether the identifier is a single-line macro at
4469 * all, then think about checking for parameters if
4470 * necessary.
4471 */
Cyrill Gorcunov8a887502011-11-20 10:42:13 +04004472 list_for_each(m, head) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004473 if (!mstrcmp(m->name, mname, m->casesense))
4474 break;
Cyrill Gorcunov8a887502011-11-20 10:42:13 +04004475 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004476 if (m) {
4477 mstart = tline;
4478 params = NULL;
4479 paramsize = NULL;
4480 if (m->nparam == 0) {
4481 /*
4482 * Simple case: the macro is parameterless. Discard the
4483 * one token that the macro call took, and push the
4484 * expansion back on the to-do stack.
4485 */
4486 if (!m->expansion) {
4487 if (!strcmp("__FILE__", m->name)) {
4488 int32_t num = 0;
4489 char *file = NULL;
4490 src_get(&num, &file);
4491 tline->text = nasm_quote(file, strlen(file));
4492 tline->type = TOK_STRING;
4493 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004494 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004495 }
4496 if (!strcmp("__LINE__", m->name)) {
4497 nasm_free(tline->text);
4498 make_tok_num(tline, src_get_linnum());
4499 continue;
4500 }
4501 if (!strcmp("__BITS__", m->name)) {
4502 nasm_free(tline->text);
4503 make_tok_num(tline, globalbits);
4504 continue;
4505 }
4506 tline = delete_Token(tline);
4507 continue;
4508 }
4509 } else {
4510 /*
4511 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004512 * exists and takes parameters. We must find the
4513 * parameters in the call, count them, find the SMacro
4514 * that corresponds to that form of the macro call, and
4515 * substitute for the parameters when we expand. What a
4516 * pain.
4517 */
4518 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004519 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004520 do {
4521 t = tline->next;
4522 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004523 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004524 t->text = NULL;
4525 t = tline->next = delete_Token(t);
4526 }
4527 tline = t;
4528 } while (tok_type_(tline, TOK_WHITESPACE));
4529 if (!tok_is_(tline, "(")) {
4530 /*
4531 * This macro wasn't called with parameters: ignore
4532 * the call. (Behaviour borrowed from gnu cpp.)
4533 */
4534 tline = mstart;
4535 m = NULL;
4536 } else {
4537 int paren = 0;
4538 int white = 0;
4539 brackets = 0;
4540 nparam = 0;
4541 sparam = PARAM_DELTA;
4542 params = nasm_malloc(sparam * sizeof(Token *));
4543 params[0] = tline->next;
4544 paramsize = nasm_malloc(sparam * sizeof(int));
4545 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004546 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004547 /*
4548 * For some unusual expansions
4549 * which concatenates function call
4550 */
4551 t = tline->next;
4552 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004553 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004554 t->text = NULL;
4555 t = tline->next = delete_Token(t);
4556 }
4557 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004558
H. Peter Anvine2c80182005-01-15 22:15:51 +00004559 if (!tline) {
4560 error(ERR_NONFATAL,
4561 "macro call expects terminating `)'");
4562 break;
4563 }
4564 if (tline->type == TOK_WHITESPACE
4565 && brackets <= 0) {
4566 if (paramsize[nparam])
4567 white++;
4568 else
4569 params[nparam] = tline->next;
4570 continue; /* parameter loop */
4571 }
4572 if (tline->type == TOK_OTHER
4573 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004574 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004575 if (ch == ',' && !paren && brackets <= 0) {
4576 if (++nparam >= sparam) {
4577 sparam += PARAM_DELTA;
4578 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004579 sparam * sizeof(Token *));
4580 paramsize = nasm_realloc(paramsize,
4581 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004582 }
4583 params[nparam] = tline->next;
4584 paramsize[nparam] = 0;
4585 white = 0;
4586 continue; /* parameter loop */
4587 }
4588 if (ch == '{' &&
4589 (brackets > 0 || (brackets == 0 &&
4590 !paramsize[nparam])))
4591 {
4592 if (!(brackets++)) {
4593 params[nparam] = tline->next;
4594 continue; /* parameter loop */
4595 }
4596 }
4597 if (ch == '}' && brackets > 0)
4598 if (--brackets == 0) {
4599 brackets = -1;
4600 continue; /* parameter loop */
4601 }
4602 if (ch == '(' && !brackets)
4603 paren++;
4604 if (ch == ')' && brackets <= 0)
4605 if (--paren < 0)
4606 break;
4607 }
4608 if (brackets < 0) {
4609 brackets = 0;
4610 error(ERR_NONFATAL, "braces do not "
4611 "enclose all of macro parameter");
4612 }
4613 paramsize[nparam] += white + 1;
4614 white = 0;
4615 } /* parameter loop */
4616 nparam++;
4617 while (m && (m->nparam != nparam ||
4618 mstrcmp(m->name, mname,
4619 m->casesense)))
4620 m = m->next;
4621 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004622 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004623 "macro `%s' exists, "
4624 "but not taking %d parameters",
4625 mstart->text, nparam);
4626 }
4627 }
4628 if (m && m->in_progress)
4629 m = NULL;
4630 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004631 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004632 * Design question: should we handle !tline, which
4633 * indicates missing ')' here, or expand those
4634 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004635 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004636 */
4637 nasm_free(params);
4638 nasm_free(paramsize);
4639 tline = mstart;
4640 } else {
4641 /*
4642 * Expand the macro: we are placed on the last token of the
4643 * call, so that we can easily split the call from the
4644 * following tokens. We also start by pushing an SMAC_END
4645 * token for the cycle removal.
4646 */
4647 t = tline;
4648 if (t) {
4649 tline = t->next;
4650 t->next = NULL;
4651 }
4652 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004653 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004654 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004655 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004656 list_for_each(t, m->expansion) {
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04004657 if (is_smacro_param(t)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004658 Token *pcopy = tline, **ptail = &pcopy;
Cyrill Gorcunov0ad6a7b2011-06-30 01:36:45 +04004659 Token *ttt;
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04004660 int i, idx;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004661
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04004662 idx = smacro_get_param_idx(t);
4663 ttt = params[idx];
Cyrill Gorcunov0ad6a7b2011-06-30 01:36:45 +04004664
4665 /*
4666 * We need smacro paramters appended.
4667 */
4668 for (i = paramsize[idx]; i > 0; i--) {
4669 *ptail = new_Token(tline, ttt->type, ttt->text, 0);
4670 ptail = &(*ptail)->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004671 ttt = ttt->next;
4672 }
Cyrill Gorcunov0ad6a7b2011-06-30 01:36:45 +04004673
H. Peter Anvine2c80182005-01-15 22:15:51 +00004674 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004675 } else if (t->type == TOK_PREPROC_Q) {
4676 tt = new_Token(tline, TOK_ID, mname, 0);
4677 tline = tt;
4678 } else if (t->type == TOK_PREPROC_QQ) {
4679 tt = new_Token(tline, TOK_ID, m->name, 0);
4680 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004681 } else {
4682 tt = new_Token(tline, t->type, t->text, 0);
4683 tline = tt;
4684 }
4685 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004686
H. Peter Anvine2c80182005-01-15 22:15:51 +00004687 /*
4688 * Having done that, get rid of the macro call, and clean
4689 * up the parameters.
4690 */
4691 nasm_free(params);
4692 nasm_free(paramsize);
4693 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004694 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004695 continue; /* main token loop */
4696 }
4697 }
4698 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004699
H. Peter Anvine2c80182005-01-15 22:15:51 +00004700 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004701 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004702 tline = delete_Token(tline);
4703 } else {
4704 t = *tail = tline;
4705 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004706 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004707 t->next = NULL;
4708 tail = &t->next;
4709 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004710 }
4711
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004712 /*
4713 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004714 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004715 * TOK_IDs should be concatenated.
4716 * Also we look for %+ tokens and concatenate the tokens before and after
4717 * them (without white spaces in between).
4718 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004719 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004720 const struct tokseq_match t[] = {
4721 {
4722 PP_CONCAT_MASK(TOK_ID) |
4723 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4724 PP_CONCAT_MASK(TOK_ID) |
4725 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4726 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4727 }
4728 };
4729 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004730 /*
4731 * If we concatenated something, *and* we had previously expanded
4732 * an actual macro, scan the lines again for macros...
4733 */
4734 tline = thead;
4735 expanded = false;
4736 goto again;
4737 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004738 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004739
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004740err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004741 if (org_tline) {
4742 if (thead) {
4743 *org_tline = *thead;
4744 /* since we just gave text to org_line, don't free it */
4745 thead->text = NULL;
4746 delete_Token(thead);
4747 } else {
4748 /* the expression expanded to empty line;
4749 we can't return NULL for some reasons
4750 we just set the line to a single WHITESPACE token. */
4751 memset(org_tline, 0, sizeof(*org_tline));
4752 org_tline->text = NULL;
4753 org_tline->type = TOK_WHITESPACE;
4754 }
4755 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004756 }
4757
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004758 return thead;
4759}
4760
4761/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004762 * Similar to expand_smacro but used exclusively with macro identifiers
4763 * right before they are fetched in. The reason is that there can be
4764 * identifiers consisting of several subparts. We consider that if there
4765 * are more than one element forming the name, user wants a expansion,
4766 * otherwise it will be left as-is. Example:
4767 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004768 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004769 *
4770 * the identifier %$abc will be left as-is so that the handler for %define
4771 * will suck it and define the corresponding value. Other case:
4772 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004773 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004774 *
4775 * In this case user wants name to be expanded *before* %define starts
4776 * working, so we'll expand %$abc into something (if it has a value;
4777 * otherwise it will be left as-is) then concatenate all successive
4778 * PP_IDs into one.
4779 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004780static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004781{
4782 Token *cur, *oldnext = NULL;
4783
H. Peter Anvin734b1882002-04-30 21:01:08 +00004784 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004785 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004786
4787 cur = tline;
4788 while (cur->next &&
Cyrill Gorcunov5b6c96b2011-06-30 00:22:53 +04004789 (cur->next->type == TOK_ID ||
4790 cur->next->type == TOK_PREPROC_ID ||
4791 cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004792 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004793
4794 /* If identifier consists of just one token, don't expand */
4795 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004796 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004797
H. Peter Anvine2c80182005-01-15 22:15:51 +00004798 if (cur) {
4799 oldnext = cur->next; /* Detach the tail past identifier */
4800 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004801 }
4802
H. Peter Anvin734b1882002-04-30 21:01:08 +00004803 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004804
H. Peter Anvine2c80182005-01-15 22:15:51 +00004805 if (cur) {
4806 /* expand_smacro possibly changhed tline; re-scan for EOL */
4807 cur = tline;
4808 while (cur && cur->next)
4809 cur = cur->next;
4810 if (cur)
4811 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004812 }
4813
4814 return tline;
4815}
4816
4817/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004818 * Determine whether the given line constitutes a multi-line macro
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004819 * call, and return the ExpDef structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004820 * to check for an initial label - that's taken care of in
4821 * expand_mmacro - but must check numbers of parameters. Guaranteed
4822 * to be called with tline->type == TOK_ID, so the putative macro
4823 * name is easy to find.
4824 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004825static ExpDef *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004826{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004827 ExpDef *head, *ed;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004828 Token **params;
4829 int nparam;
4830
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004831 head = (ExpDef *) hash_findix(&expdefs, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004832
4833 /*
4834 * Efficiency: first we see if any macro exists with the given
4835 * name. If not, we can return NULL immediately. _Then_ we
4836 * count the parameters, and then we look further along the
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004837 * list if necessary to find the proper ExpDef.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004838 */
Cyrill Gorcunov8a887502011-11-20 10:42:13 +04004839 list_for_each(ed, head) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004840 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004841 break;
Cyrill Gorcunov8a887502011-11-20 10:42:13 +04004842 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004843 if (!ed)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004844 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004845
4846 /*
4847 * OK, we have a potential macro. Count and demarcate the
4848 * parameters.
4849 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004850 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004851
4852 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004853 * So we know how many parameters we've got. Find the ExpDef
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004854 * structure that handles this number.
4855 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004856 while (ed) {
4857 if (ed->nparam_min <= nparam
4858 && (ed->plus || nparam <= ed->nparam_max)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004859 /*
4860 * It's right, and we can use it. Add its default
4861 * parameters to the end of our list if necessary.
4862 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004863 if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004864 params =
4865 nasm_realloc(params,
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004866 ((ed->nparam_min + ed->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004867 1) * sizeof(*params)));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004868 while (nparam < ed->nparam_min + ed->ndefs) {
4869 params[nparam] = ed->defaults[nparam - ed->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004870 nparam++;
4871 }
4872 }
4873 /*
4874 * If we've gone over the maximum parameter count (and
4875 * we're in Plus mode), ignore parameters beyond
4876 * nparam_max.
4877 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004878 if (ed->plus && nparam > ed->nparam_max)
4879 nparam = ed->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004880 /*
4881 * Then terminate the parameter list, and leave.
4882 */
4883 if (!params) { /* need this special case */
4884 params = nasm_malloc(sizeof(*params));
4885 nparam = 0;
4886 }
4887 params[nparam] = NULL;
4888 *params_array = params;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004889 return ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004890 }
4891 /*
4892 * This one wasn't right: look for the next one with the
4893 * same name.
4894 */
Cyrill Gorcunov8a887502011-11-20 10:42:13 +04004895 list_for_each(ed, ed->next) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004896 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004897 break;
Cyrill Gorcunov8a887502011-11-20 10:42:13 +04004898 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004899 }
4900
4901 /*
4902 * After all that, we didn't find one with the right number of
4903 * parameters. Issue a warning, and fail to expand the macro.
4904 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004905 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004906 "macro `%s' exists, but not taking %d parameters",
4907 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004908 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004909 return NULL;
4910}
4911
4912/*
4913 * Expand the multi-line macro call made by the given line, if
4914 * there is one to be expanded. If there is, push the expansion on
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004915 * istk->expansion and return true. Otherwise return false.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004916 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004917static bool expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004918{
H. Peter Anvineba20a72002-04-30 20:53:55 +00004919 Token *label = NULL;
4920 int dont_prepend = 0;
Cyrill Gorcunovfb27fc22011-06-25 12:08:30 +04004921 Token **params, *t;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004922 Line *l = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004923 ExpDef *ed;
4924 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004925 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004926 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004927
4928 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004929 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004930 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004931 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004932 return false;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004933 ed = is_mmacro(t, &params);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004934 if (ed != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004935 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004936 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004937 Token *last;
4938 /*
4939 * We have an id which isn't a macro call. We'll assume
4940 * it might be a label; we'll also check to see if a
4941 * colon follows it. Then, if there's another id after
4942 * that lot, we'll check it again for macro-hood.
4943 */
4944 label = last = t;
4945 t = t->next;
4946 if (tok_type_(t, TOK_WHITESPACE))
4947 last = t, t = t->next;
4948 if (tok_is_(t, ":")) {
4949 dont_prepend = 1;
4950 last = t, t = t->next;
4951 if (tok_type_(t, TOK_WHITESPACE))
4952 last = t, t = t->next;
4953 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004954 if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, &params)))
4955 return false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004956 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004957 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004958 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004959 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004960
4961 /*
4962 * Fix up the parameters: this involves stripping leading and
4963 * trailing whitespace, then stripping braces if they are
4964 * present.
4965 */
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004966 for (nparam = 0; params[nparam]; nparam++)
4967 ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004968 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004969
H. Peter Anvine2c80182005-01-15 22:15:51 +00004970 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004971 int brace = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004972 int comma = (!ed->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004973
H. Peter Anvine2c80182005-01-15 22:15:51 +00004974 t = params[i];
4975 skip_white_(t);
4976 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004977 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004978 params[i] = t;
4979 paramlen[i] = 0;
4980 while (t) {
4981 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4982 break; /* ... because we have hit a comma */
4983 if (comma && t->type == TOK_WHITESPACE
4984 && tok_is_(t->next, ","))
4985 break; /* ... or a space then a comma */
4986 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4987 break; /* ... or a brace */
4988 t = t->next;
4989 paramlen[i]++;
4990 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004991 }
4992
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004993 if (ed->cur_depth >= ed->max_depth) {
4994 if (ed->max_depth > 1) {
4995 error(ERR_WARNING,
4996 "reached maximum macro recursion depth of %i for %s",
4997 ed->max_depth,ed->name);
4998 }
4999 return false;
5000 } else {
5001 ed->cur_depth ++;
5002 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005003
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005004 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005005 * OK, we have found a ExpDef structure representing a
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005006 * previously defined mmacro. Create an expansion invocation
5007 * and point it back to the expansion definition. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005008 * parameter tokens and macro-local tokens doesn't get done
5009 * until the single-line macro substitution process; this is
5010 * because delaying them allows us to change the semantics
5011 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005012 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005013 ei = new_ExpInv(EXP_MMACRO, ed);
5014 ei->name = nasm_strdup(mname);
5015 //ei->label = label;
5016 //ei->label_text = detoken(label, false);
5017 ei->current = ed->line;
5018 ei->emitting = true;
5019 //ei->iline = tline;
5020 ei->params = params;
5021 ei->nparam = nparam;
5022 ei->rotate = 0;
5023 ei->paramlen = paramlen;
5024 ei->lineno = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005025
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005026 ei->prev = istk->expansion;
5027 istk->expansion = ei;
5028
5029 /*
5030 * Special case: detect %00 on first invocation; if found,
5031 * avoid emitting any labels that precede the mmacro call.
5032 * ed->prepend is set to -1 when %00 is detected, else 1.
5033 */
5034 if (ed->prepend == 0) {
5035 for (l = ed->line; l != NULL; l = l->next) {
5036 for (t = l->first; t != NULL; t = t->next) {
5037 if ((t->type == TOK_PREPROC_ID) &&
5038 (strlen(t->text) == 3) &&
5039 (t->text[1] == '0') && (t->text[2] == '0')) {
5040 dont_prepend = -1;
5041 break;
5042 }
5043 }
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005044 if (dont_prepend < 0)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005045 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005046 }
5047 ed->prepend = ((dont_prepend < 0) ? -1 : 1);
5048 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005049
5050 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00005051 * If we had a label, push it on as the first line of
5052 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005053 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005054 if (label != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005055 if (ed->prepend < 0) {
5056 ei->label_text = detoken(label, false);
5057 } else {
5058 if (dont_prepend == 0) {
5059 t = label;
5060 while (t->next != NULL) {
5061 t = t->next;
5062 }
5063 t->next = new_Token(NULL, TOK_OTHER, ":", 0);
5064 }
5065 l = new_Line();
5066 l->first = copy_Token(label);
5067 l->next = ei->current;
5068 ei->current = l;
5069 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005070 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005071
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005072 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005073
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005074 istk->mmac_depth++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005075 return true;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005076}
5077
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005078/* The function that actually does the error reporting */
5079static void verror(int severity, const char *fmt, va_list arg)
5080{
5081 char buff[1024];
5082
5083 vsnprintf(buff, sizeof(buff), fmt, arg);
5084
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005085 if (istk && istk->mmac_depth > 0) {
5086 ExpInv *ei = istk->expansion;
5087 int lineno = ei->lineno;
5088 while (ei) {
5089 if (ei->type == EXP_MMACRO)
5090 break;
5091 lineno += ei->relno;
5092 ei = ei->prev;
5093 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005094 nasm_error(severity, "(%s:%d) %s", ei->def->name,
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005095 lineno, buff);
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005096 } else {
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005097 nasm_error(severity, "%s", buff);
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005098 }
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005099}
5100
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005101/*
5102 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07005103 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005104 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005105static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005106{
5107 va_list arg;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005108 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005109 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00005110 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005111}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005112
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005113/*
5114 * Because %else etc are evaluated in the state context
5115 * of the previous branch, errors might get lost with error():
5116 * %if 0 ... %else trailing garbage ... %endif
5117 * So %else etc should report errors with this function.
5118 */
5119static void error_precond(int severity, const char *fmt, ...)
5120{
5121 va_list arg;
5122
5123 /* Only ignore the error if it's really in a dead branch */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005124 if ((istk != NULL) &&
5125 (istk->expansion != NULL) &&
5126 (istk->expansion->type == EXP_IF) &&
5127 (istk->expansion->def->state == COND_NEVER))
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005128 return;
5129
5130 va_start(arg, fmt);
5131 verror(severity, fmt, arg);
5132 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005133}
5134
H. Peter Anvin734b1882002-04-30 21:01:08 +00005135static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005136pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005137{
H. Peter Anvin7383b402008-09-24 10:20:40 -07005138 Token *t;
5139
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005140 cstk = NULL;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005141 istk = nasm_zalloc(sizeof(Include));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005142 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00005143 src_set_fname(nasm_strdup(file));
5144 src_set_linnum(0);
5145 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005146 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07005147 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00005148 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005149 defining = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005150 finals = NULL;
5151 in_final = false;
Charles Crayned4200be2008-07-12 16:42:33 -07005152 nested_mac_count = 0;
5153 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005154 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005155 unique = 0;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005156 if (tasm_compatible_mode)
H. Peter Anvina4835d42008-05-20 14:21:29 -07005157 stdmacpos = nasm_stdmac;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005158 else
H. Peter Anvina4835d42008-05-20 14:21:29 -07005159 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvind2456592008-06-19 15:04:18 -07005160 any_extrastdmac = extrastdmac && *extrastdmac;
5161 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005162 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005163
5164 /*
5165 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5166 * The caller, however, will also pass in 3 for preprocess-only so
5167 * we can set __PASS__ accordingly.
5168 */
5169 pass = apass > 2 ? 2 : apass;
5170
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005171 dephead = deptail = deplist;
5172 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005173 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
5174 sl->next = NULL;
5175 strcpy(sl->str, file);
5176 *deptail = sl;
5177 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005178 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07005179
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005180 /*
5181 * Define the __PASS__ macro. This is defined here unlike
5182 * all the other builtins, because it is special -- it varies between
5183 * passes.
5184 */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005185 t = nasm_zalloc(sizeof(*t));
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005186 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07005187 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005188}
5189
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005190static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005191{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005192 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005193 Token *tline;
Keith Kanios9858cec2010-11-08 00:58:02 -06005194 ExpDef *ed;
5195 ExpInv *ei;
5196 Line *l;
5197 int j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005198
H. Peter Anvine2c80182005-01-15 22:15:51 +00005199 while (1) {
5200 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005201 * Fetch a tokenized line, either from the expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005202 * buffer or from the input file.
5203 */
5204 tline = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005205
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005206 while (1) { /* until we get a line we can use */
5207 /*
5208 * Fetch a tokenized line from the expansion buffer
5209 */
5210 if (istk->expansion != NULL) {
5211 ei = istk->expansion;
5212 if (ei->current != NULL) {
5213 if (ei->emitting == false) {
5214 ei->current = NULL;
5215 continue;
5216 }
5217 l = ei->current;
5218 ei->current = l->next;
5219 ei->lineno++;
5220 tline = copy_Token(l->first);
5221 if (((ei->type == EXP_REP) ||
5222 (ei->type == EXP_MMACRO) ||
5223 (ei->type == EXP_WHILE))
5224 && (ei->def->nolist == false)) {
5225 char *p = detoken(tline, false);
5226 list->line(LIST_MACRO, p);
5227 nasm_free(p);
5228 }
5229 if (ei->linnum > -1) {
5230 src_set_linnum(src_get_linnum() + 1);
5231 }
5232 break;
5233 } else if ((ei->type == EXP_REP) &&
5234 (ei->def->cur_depth < ei->def->max_depth)) {
5235 ei->def->cur_depth ++;
5236 ei->current = ei->def->line;
5237 ei->lineno = 0;
5238 continue;
5239 } else if ((ei->type == EXP_WHILE) &&
5240 (ei->def->cur_depth < ei->def->max_depth)) {
5241 ei->current = ei->def->line;
5242 ei->lineno = 0;
5243 tline = copy_Token(ei->current->first);
5244 j = if_condition(tline, PP_WHILE);
5245 tline = NULL;
5246 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
5247 if (j == COND_IF_TRUE) {
5248 ei->current = ei->current->next;
5249 ei->def->cur_depth ++;
5250 } else {
5251 ei->emitting = false;
5252 ei->current = NULL;
5253 ei->def->cur_depth = ei->def->max_depth;
5254 }
5255 continue;
5256 } else {
5257 istk->expansion = ei->prev;
5258 ed = ei->def;
5259 if (ed != NULL) {
5260 if ((ei->emitting == true) &&
5261 (ed->max_depth == DEADMAN_LIMIT) &&
5262 (ed->cur_depth == DEADMAN_LIMIT)
5263 ) {
5264 error(ERR_FATAL, "runaway expansion detected, aborting");
5265 }
5266 if (ed->cur_depth > 0) {
5267 ed->cur_depth --;
Keith Kanios94124652010-12-18 11:47:28 -06005268 } else if (ed->type != EXP_MMACRO) {
Keith Kanios6a7c3e92010-12-18 11:49:53 -06005269 expansions = ed->prev;
5270 free_expdef(ed);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005271 }
5272 if ((ei->type == EXP_REP) ||
5273 (ei->type == EXP_MMACRO) ||
5274 (ei->type == EXP_WHILE)) {
5275 list->downlevel(LIST_MACRO);
5276 if (ei->type == EXP_MMACRO) {
5277 istk->mmac_depth--;
5278 }
5279 }
5280 }
5281 if (ei->linnum > -1) {
5282 src_set_linnum(ei->linnum);
5283 }
5284 free_expinv(ei);
5285 continue;
5286 }
5287 }
5288
5289 /*
5290 * Read in line from input and tokenize
5291 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005292 line = read_line();
5293 if (line) { /* from the current input file */
5294 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005295 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005296 nasm_free(line);
5297 break;
5298 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005299
H. Peter Anvine2c80182005-01-15 22:15:51 +00005300 /*
5301 * The current file has ended; work down the istk
5302 */
5303 {
5304 Include *i = istk;
5305 fclose(i->fp);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005306 if (i->expansion != NULL) {
5307 error(ERR_FATAL,
5308 "end of file while still in an expansion");
5309 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005310 /* only set line and file name if there's a next node */
5311 if (i->next) {
5312 src_set_linnum(i->lineno);
Cyrill Gorcunovd34a1082011-03-07 11:23:08 +03005313 nasm_free(src_set_fname(nasm_strdup(i->fname)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005314 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005315 if ((i->next == NULL) && (finals != NULL)) {
5316 in_final = true;
5317 ei = new_ExpInv(EXP_FINAL, NULL);
5318 ei->emitting = true;
5319 ei->current = finals;
5320 istk->expansion = ei;
5321 finals = NULL;
5322 continue;
5323 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005324 istk = i->next;
5325 list->downlevel(LIST_INCLUDE);
5326 nasm_free(i);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005327 if (istk == NULL) {
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005328 if (finals != NULL)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005329 in_final = true;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005330 else
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005331 return NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005332 }
5333 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005334 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005335 }
5336
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005337 if (defining == NULL)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005338 tline = expand_mmac_params(tline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005339
H. Peter Anvine2c80182005-01-15 22:15:51 +00005340 /*
5341 * Check the line to see if it's a preprocessor directive.
5342 */
5343 if (do_directive(tline) == DIRECTIVE_FOUND) {
5344 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005345 } else if (defining != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005346 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005347 * We're defining an expansion. We emit nothing at all,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005348 * and just shove the tokenized line on to the definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005349 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005350 if (defining->ignoring == false) {
5351 Line *l = new_Line();
5352 l->first = tline;
5353 if (defining->line == NULL) {
5354 defining->line = l;
5355 defining->last = l;
5356 } else {
5357 defining->last->next = l;
5358 defining->last = l;
5359 }
5360 } else {
Keith Kanios104803d2010-12-18 11:05:46 -06005361 free_tlist(tline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005362 }
5363 defining->linecount++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005364 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005365 } else if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005366 (istk->expansion->emitting != true)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005367 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005368 * We're in a non-emitting branch of an expansion.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005369 * Emit nothing at all, not even a blank line: when we
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005370 * emerge from the expansion we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005371 * directive so we keep our place correctly.
5372 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005373 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005374 continue;
5375 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005376 tline = expand_smacro(tline);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005377 if (expand_mmacro(tline) != true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005378 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005379 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005380 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005381 line = detoken(tline, true);
5382 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005383 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005384 } else {
5385 continue;
5386 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005387 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005388 }
5389 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005390}
5391
H. Peter Anvine2c80182005-01-15 22:15:51 +00005392static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005393{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005394 if (defining != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005395 error(ERR_NONFATAL, "end of file while still defining an expansion");
5396 while (defining != NULL) {
5397 ExpDef *ed = defining;
5398 defining = ed->prev;
5399 free_expdef(ed);
5400 }
5401 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005402 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005403 while (cstk != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005404 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005405 free_macros();
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005406 while (istk != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005407 Include *i = istk;
5408 istk = istk->next;
5409 fclose(i->fp);
5410 nasm_free(i->fname);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005411 while (i->expansion != NULL) {
5412 ExpInv *ei = i->expansion;
5413 i->expansion = ei->prev;
5414 free_expinv(ei);
5415 }
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005416 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005417 }
5418 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005419 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005420 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005421 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005422 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005423 free_llist(predef);
5424 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005425 while ((i = ipath)) {
5426 ipath = i->next;
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04005427 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005428 nasm_free(i);
5429 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005430 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005431}
5432
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005433void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005434{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005435 IncPath *i = nasm_zalloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005436
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005437 if (path)
5438 i->path = nasm_strdup(path);
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005439
H. Peter Anvin89cee572009-07-15 09:16:54 -04005440 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005441 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005442 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005443 j = j->next;
5444 j->next = i;
5445 } else {
5446 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005447 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005448}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005449
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005450void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005451{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005452 Token *inc, *space, *name;
5453 Line *l;
5454
H. Peter Anvin734b1882002-04-30 21:01:08 +00005455 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5456 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5457 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005458
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005459 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005460 l->next = predef;
5461 l->first = inc;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005462 predef = l;
5463}
5464
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005465void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005466{
5467 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005468 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005469 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005470
5471 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005472 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5473 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005474 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005475 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005476 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005477 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005478 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005479
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005480 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005481 l->next = predef;
5482 l->first = def;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005483 predef = l;
5484}
5485
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005486void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005487{
5488 Token *def, *space;
5489 Line *l;
5490
H. Peter Anvin734b1882002-04-30 21:01:08 +00005491 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5492 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005493 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005494
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005495 l = new_Line();
H. Peter Anvin620515a2002-04-30 20:57:38 +00005496 l->next = predef;
5497 l->first = def;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005498 predef = l;
5499}
5500
Keith Kaniosb7a89542007-04-12 02:40:54 +00005501/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005502 * This function is used to assist with "runtime" preprocessor
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005503 * directives, e.g. pp_runtime("%define __BITS__ 64");
Keith Kaniosb7a89542007-04-12 02:40:54 +00005504 *
5505 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5506 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5507 */
5508
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005509void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005510{
5511 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005512
Keith Kaniosb7a89542007-04-12 02:40:54 +00005513 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005514 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005515 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005516
Keith Kaniosb7a89542007-04-12 02:40:54 +00005517}
5518
H. Peter Anvina70547f2008-07-19 21:44:26 -07005519void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005520{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005521 extrastdmac = macros;
5522}
5523
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005524static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005525{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005526 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005527 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005528 tok->text = nasm_strdup(numbuf);
5529 tok->type = TOK_NUMBER;
5530}
5531
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +04005532struct preproc_ops nasmpp = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005533 pp_reset,
5534 pp_getline,
5535 pp_cleanup
5536};