blob: 835ad3e68298967af5e1ace1830ddd50b9d752c7 [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 {
146 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
147 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700148 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
149 TOK_INTERNAL_STRING,
150 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300151 TOK_PASTE, /* %+ */
152 TOK_INDIRECT, /* %[...] */
153 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
154 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000155};
156
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400157#define PP_CONCAT_MASK(x) (1 << (x))
158
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400159struct tokseq_match {
160 int mask_head;
161 int mask_tail;
162};
163
H. Peter Anvine2c80182005-01-15 22:15:51 +0000164struct Token {
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400165 Token *next;
166 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700167 union {
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400168 SMacro *mac; /* associated macro for TOK_SMAC_END */
169 size_t len; /* scratch length field */
170 } a; /* Auxiliary data */
171 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000172};
173
174/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500175 * Expansion definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000176 * these, which is essentially a container to allow several linked
177 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700178 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000179 * Note that in this module, linked lists are treated as stacks
180 * wherever possible. For this reason, Lines are _pushed_ on to the
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500181 * `last' field in ExpDef structures, so that the linked list,
182 * if walked, would emit the expansion lines in the proper order.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000183 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000184struct Line {
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400185 Line *next;
186 Token *first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000187};
188
189/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500190 * Expansion Types
191 */
192enum pp_exp_type {
193 EXP_NONE = 0, EXP_PREDEF,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300194 EXP_MMACRO, EXP_REP,
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500195 EXP_IF, EXP_WHILE,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300196 EXP_COMMENT, EXP_FINAL,
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500197 EXP_MAX = INT_MAX /* Keep compiler from reducing the range */
198};
199
200/*
201 * Store the definition of an expansion, in which is any
202 * preprocessor directive that has an ending pair.
203 *
204 * This design allows for arbitrary expansion/recursion depth,
205 * upto the DEADMAN_LIMIT.
206 *
207 * The `next' field is used for storing ExpDef in hash tables; the
208 * `prev' field is for the global `expansions` linked-list.
209 */
210struct ExpDef {
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400211 ExpDef *prev; /* previous definition */
212 ExpDef *next; /* next in hash table */
213 enum pp_exp_type type; /* expansion type */
214 char *name; /* definition name */
215 int nparam_min;
216 int nparam_max;
217 bool casesense;
218 bool plus; /* is the last parameter greedy? */
219 bool nolist; /* is this expansion listing-inhibited? */
220 Token *dlist; /* all defaults as one list */
221 Token **defaults; /* parameter default pointers */
222 int ndefs; /* number of default parameters */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300223
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400224 int prepend; /* label prepend state */
225 Line *label;
226 Line *line;
227 Line *last;
228 int linecount; /* number of lines within expansion */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300229
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400230 int64_t def_depth; /* current number of definition pairs deep */
231 int64_t cur_depth; /* current number of expansions */
232 int64_t max_depth; /* maximum number of expansions allowed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300233
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400234 int state; /* condition state */
235 bool ignoring; /* ignoring definition lines */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500236};
237
238/*
239 * Store the invocation of an expansion.
240 *
241 * The `prev' field is for the `istk->expansion` linked-list.
242 *
243 * When an expansion is being expanded, `params', `iline', `nparam',
244 * `paramlen', `rotate' and `unique' are local to the invocation.
245 */
246struct ExpInv {
Cyrill Gorcunovd57a0312011-07-17 20:11:08 +0400247 ExpInv *prev; /* previous invocation */
248 ExpDef *def; /* pointer to expansion definition */
249 char *name; /* invocation name */
250 Line *label; /* pointer to label */
251 char *label_text; /* pointer to label text */
252 Line *current; /* pointer to current line in invocation */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300253
Cyrill Gorcunovd57a0312011-07-17 20:11:08 +0400254 Token **params; /* actual parameters */
255 Token *iline; /* invocation line */
256 int *paramlen;
257 unsigned int nparam;
258 unsigned int rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300259
Cyrill Gorcunovd57a0312011-07-17 20:11:08 +0400260 uint64_t unique;
261 int lineno; /* current line number in expansion */
262 int linnum; /* line number at invocation */
263 int relno; /* relative line number at invocation */
264 enum pp_exp_type type; /* expansion type */
265 bool emitting;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500266};
267
268/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000269 * To handle an arbitrary level of file inclusion, we maintain a
270 * stack (ie linked list) of these things.
271 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000272struct Include {
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400273 Include *next;
274 FILE *fp;
275 Cond *conds;
276 ExpInv *expansion;
277 char *fname;
278 int lineno;
279 int lineinc;
280 int mmac_depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000281};
282
283/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000284 * Include search path. This is simply a list of strings which get
285 * prepended, in turn, to the name of an include file, in an
286 * attempt to find the file if it's not in the current directory.
287 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000288struct IncPath {
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400289 IncPath *next;
290 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000291};
292
293/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000294 * Conditional assembly: we maintain a separate stack of these for
295 * each level of file inclusion. (The only reason we keep the
296 * stacks separate is to ensure that a stray `%endif' in a file
297 * included from within the true branch of a `%if' won't terminate
298 * it and cause confusion: instead, rightly, it'll cause an error.)
299 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000300enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000301 /*
302 * These states are for use just after %if or %elif: IF_TRUE
303 * means the condition has evaluated to truth so we are
304 * currently emitting, whereas IF_FALSE means we are not
305 * currently emitting but will start doing so if a %else comes
306 * up. In these states, all directives are admissible: %elif,
307 * %else and %endif. (And of course %if.)
308 */
309 COND_IF_TRUE, COND_IF_FALSE,
310 /*
311 * These states come up after a %else: ELSE_TRUE means we're
312 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
313 * any %elif or %else will cause an error.
314 */
315 COND_ELSE_TRUE, COND_ELSE_FALSE,
316 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200317 * These states mean that we're not emitting now, and also that
318 * nothing until %endif will be emitted at all. COND_DONE is
319 * used when we've had our moment of emission
320 * and have now started seeing %elifs. COND_NEVER is used when
321 * the condition construct in question is contained within a
322 * non-emitting branch of a larger condition construct,
323 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000324 */
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200325 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000326};
327#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
328
H. Peter Anvin70653092007-10-19 14:42:29 -0700329/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000330 * These defines are used as the possible return values for do_directive
331 */
332#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300333#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000334
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000335/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500336 * This define sets the upper limit for smacro and expansions
Keith Kanios852f1ee2009-07-12 00:19:55 -0500337 */
338#define DEADMAN_LIMIT (1 << 20)
339
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400340/* max reps */
341#define REP_LIMIT ((INT64_C(1) << 62))
342
Keith Kanios852f1ee2009-07-12 00:19:55 -0500343/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000344 * Condition codes. Note that we use c_ prefix not C_ because C_ is
345 * used in nasm.h for the "real" condition codes. At _this_ level,
346 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
347 * ones, so we need a different enum...
348 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700349static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000350 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
351 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000352 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000353};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700354enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000355 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
356 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 -0700357 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
358 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000359};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700360static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000361 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
362 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 +0000363 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000364};
365
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000366/* For TASM compatibility we need to be able to recognise TASM compatible
367 * conditional compilation directives. Using the NASM pre-processor does
368 * not work, so we look for them specifically from the following list and
369 * then jam in the equivalent NASM directive into the input stream.
370 */
371
H. Peter Anvine2c80182005-01-15 22:15:51 +0000372enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000373 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
374 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
375};
376
H. Peter Anvin476d2862007-10-02 22:04:15 -0700377static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000378 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
379 "ifndef", "include", "local"
380};
381
382static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000383static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000384static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800385static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000386
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000387static Context *cstk;
388static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000389static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000390
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300391static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700392static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000393
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300394static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000395
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000396static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700397static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000398
399static ListGen *list;
400
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000401/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500402 * The current set of expansion definitions we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000403 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500404static struct hash_table expdefs;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000405
406/*
407 * The current set of single-line macros we have defined.
408 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700409static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000410
411/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500412 * Linked List of all active expansion definitions
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000413 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500414struct ExpDef *expansions = NULL;
415
416/*
417 * The expansion we are currently defining
418 */
419static ExpDef *defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000420
Charles Crayned4200be2008-07-12 16:42:33 -0700421static uint64_t nested_mac_count;
422static uint64_t nested_rep_count;
423
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000424/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500425 * Linked-list of lines to preprocess, prior to cleanup
426 */
427static Line *finals = NULL;
428static bool in_final = false;
429
430/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000431 * The number of macro parameters to allocate space for at a time.
432 */
433#define PARAM_DELTA 16
434
435/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700436 * The standard macro set: defined in macros.c in the array nasm_stdmac.
437 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000438 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700439static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000440
441/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000442 * The extra standard macros that come from the object format, if
443 * any.
444 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700445static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700446static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000447
448/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000449 * Tokens are allocated in blocks to improve speed
450 */
451#define TOKEN_BLOCKSIZE 4096
452static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000453struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000454 Blocks *next;
455 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000456};
457
458static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000459
460/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000461 * Forward declarations.
462 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000463static Token *expand_mmac_params(Token * tline);
464static Token *expand_smacro(Token * tline);
465static Token *expand_id(Token * tline);
Cyrill Gorcunov290eac72011-06-28 01:59:05 +0400466static Context *get_ctx(const char *name, const char **namep);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700467static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000468static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200469static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000470static void *new_Block(size_t size);
471static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700472static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300473 const char *text, int txtlen);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500474static Token *copy_Token(Token * tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000475static Token *delete_Token(Token * t);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500476static Line *new_Line(void);
477static ExpDef *new_ExpDef(int exp_type);
478static ExpInv *new_ExpInv(int exp_type, ExpDef *ed);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000479
480/*
481 * Macros for safe checking of token pointers, avoid *(NULL)
482 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300483#define tok_type_(x,t) ((x) && (x)->type == (t))
484#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
485#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
486#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000487
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400488/*
489 * A few helpers for single macros
490 */
491
492/* We might be not smacro parameter at all */
493static bool is_smacro_param(Token *t)
494{
495 return t->type >= TOK_SMAC_PARAM;
496}
497
498/* smacro parameters are counted in a special way */
499static int smacro_get_param_idx(Token *t)
500{
501 return t->type - TOK_SMAC_PARAM;
502}
503
504/* encode smacro parameter index */
505static int smacro_set_param_idx(Token *t, unsigned int index)
506{
507 return t->type = TOK_SMAC_PARAM + index;
508}
509
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300510#ifdef NASM_TRACE
511
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400512#define stringify(x) #x
513
Cyrill Gorcunov9d1141a2011-06-26 23:07:35 +0400514#define nasm_trace(msg, ...) printf("(%s:%d): " msg "\n", __func__, __LINE__, ##__VA_ARGS__)
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400515#define nasm_dump_token(t) nasm_raw_dump_token(t, __FILE__, __LINE__, __func__);
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400516#define nasm_dump_stream(t) nasm_raw_dump_stream(t, __FILE__, __LINE__, __func__);
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400517
518/* FIXME: we really need some compound type here instead of inplace code */
519static const char *nasm_get_tok_type_str(enum pp_token_type type)
520{
521#define SWITCH_TOK_NAME(type) \
522 case (type): \
523 return stringify(type)
524
525 switch (type) {
526 SWITCH_TOK_NAME(TOK_NONE);
527 SWITCH_TOK_NAME(TOK_WHITESPACE);
528 SWITCH_TOK_NAME(TOK_COMMENT);
529 SWITCH_TOK_NAME(TOK_ID);
530 SWITCH_TOK_NAME(TOK_PREPROC_ID);
531 SWITCH_TOK_NAME(TOK_STRING);
532 SWITCH_TOK_NAME(TOK_NUMBER);
533 SWITCH_TOK_NAME(TOK_FLOAT);
534 SWITCH_TOK_NAME(TOK_SMAC_END);
535 SWITCH_TOK_NAME(TOK_OTHER);
536 SWITCH_TOK_NAME(TOK_INTERNAL_STRING);
537 SWITCH_TOK_NAME(TOK_PREPROC_Q);
538 SWITCH_TOK_NAME(TOK_PREPROC_QQ);
539 SWITCH_TOK_NAME(TOK_PASTE);
540 SWITCH_TOK_NAME(TOK_INDIRECT);
541 SWITCH_TOK_NAME(TOK_SMAC_PARAM);
542 SWITCH_TOK_NAME(TOK_MAX);
543 }
544
545 return NULL;
546}
547
548static void nasm_raw_dump_token(Token *token, const char *file, int line, const char *func)
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300549{
550 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
551 if (token) {
552 Token *t;
553 list_for_each(t, token) {
554 if (t->text)
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400555 printf("'%s'(%s) ", t->text,
556 nasm_get_tok_type_str(t->type));
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300557 }
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400558 printf("\n\n");
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300559 }
560}
561
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400562static void nasm_raw_dump_stream(Token *token, const char *file, int line, const char *func)
563{
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 printf("%s", t->text ? t->text : " ");
569 printf("\n\n");
570 }
571}
572
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400573#else
574#define nasm_trace(msg, ...)
575#define nasm_dump_token(t)
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400576#define nasm_dump_stream(t)
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300577#endif
578
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300579/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700580 * nasm_unquote with error if the string contains NUL characters.
581 * If the string contains NUL characters, issue an error and return
582 * the C len, i.e. truncate at the NUL.
583 */
584static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
585{
586 size_t len = nasm_unquote(qstr, NULL);
587 size_t clen = strlen(qstr);
588
589 if (len != clen)
590 error(ERR_NONFATAL, "NUL character in `%s' directive",
591 pp_directives[directive]);
592
593 return clen;
594}
595
596/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700597 * In-place reverse a list of tokens.
598 */
599static Token *reverse_tokens(Token *t)
600{
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400601 Token *prev, *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700602
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400603 list_reverse(t, prev, next);
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700604
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400605 return t;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700606}
607
608/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300609 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000610 * front of them. We do it here because I could not find any other
611 * place to do it for the moment, and it is a hack (ideally it would
612 * be nice to be able to use the NASM pre-processor to do it).
613 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000614static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000615{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000616 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400617 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000618
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400619 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000620
621 /* Binary search for the directive name */
622 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400623 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400624 q = nasm_skip_word(p);
625 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000626 if (len) {
627 oldchar = p[len];
628 p[len] = 0;
629 while (j - i > 1) {
630 k = (j + i) / 2;
631 m = nasm_stricmp(p, tasm_directives[k]);
632 if (m == 0) {
633 /* We have found a directive, so jam a % in front of it
634 * so that NASM will then recognise it as one if it's own.
635 */
636 p[len] = oldchar;
637 len = strlen(p);
638 oldline = line;
639 line = nasm_malloc(len + 2);
640 line[0] = '%';
641 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700642 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300643 * NASM does not recognise IFDIFI, so we convert
644 * it to %if 0. This is not used in NASM
645 * compatible code, but does need to parse for the
646 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000647 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700648 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000649 } else {
650 memcpy(line + 1, p, len + 1);
651 }
652 nasm_free(oldline);
653 return line;
654 } else if (m < 0) {
655 j = k;
656 } else
657 i = k;
658 }
659 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000660 }
661 return line;
662}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000663
H. Peter Anvin76690a12002-04-30 20:52:49 +0000664/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000665 * The pre-preprocessing stage... This function translates line
666 * number indications as they emerge from GNU cpp (`# lineno "file"
667 * flags') into NASM preprocessor line number indications (`%line
668 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000669 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000670static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000671{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000672 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000673 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000674
H. Peter Anvine2c80182005-01-15 22:15:51 +0000675 if (line[0] == '#' && line[1] == ' ') {
676 oldline = line;
677 fname = oldline + 2;
678 lineno = atoi(fname);
679 fname += strspn(fname, "0123456789 ");
680 if (*fname == '"')
681 fname++;
682 fnlen = strcspn(fname, "\"");
683 line = nasm_malloc(20 + fnlen);
684 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
685 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000686 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000687 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000688 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000689 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000690}
691
692/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000693 * Free a linked list of tokens.
694 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000695static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000696{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400697 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000698 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000699}
700
701/*
702 * Free a linked list of lines.
703 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000704static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000705{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400706 Line *l, *tmp;
707 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000708 free_tlist(l->first);
709 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000710 }
711}
712
713/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500714 * Free an ExpDef
H. Peter Anvineba20a72002-04-30 20:53:55 +0000715 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500716static void free_expdef(ExpDef * ed)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000717{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500718 nasm_free(ed->name);
719 free_tlist(ed->dlist);
720 nasm_free(ed->defaults);
721 free_llist(ed->line);
722 nasm_free(ed);
723}
724
725/*
726 * Free an ExpInv
727 */
728static void free_expinv(ExpInv * ei)
729{
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +0400730 nasm_free(ei->name);
731 nasm_free(ei->label_text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300732 nasm_free(ei);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000733}
734
735/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700736 * Free all currently defined macros, and free the hash tables
737 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700738static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700739{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400740 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700741 const char *key;
742 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700743
H. Peter Anvin072771e2008-05-22 13:17:51 -0700744 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300745 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400746 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300747 nasm_free(s->name);
748 free_tlist(s->expansion);
749 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300750 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700751 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700752 hash_free(smt);
753}
754
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500755static void free_expdef_table(struct hash_table *edt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700756{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500757 ExpDef *ed, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700758 const char *key;
759 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700760
761 it = NULL;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500762 while ((ed = hash_iterate(edt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300763 nasm_free((void *)key);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500764 list_for_each_safe(ed ,tmp, ed)
765 free_expdef(ed);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700766 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500767 hash_free(edt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700768}
769
770static void free_macros(void)
771{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700772 free_smacro_table(&smacros);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500773 free_expdef_table(&expdefs);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700774}
775
776/*
777 * Initialize the hash tables
778 */
779static void init_macros(void)
780{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700781 hash_init(&smacros, HASH_LARGE);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500782 hash_init(&expdefs, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700783}
784
785/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000786 * Pop the context stack.
787 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000788static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000789{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000790 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000791
792 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700793 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000794 nasm_free(c->name);
795 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000796}
797
H. Peter Anvin072771e2008-05-22 13:17:51 -0700798/*
799 * Search for a key in the hash index; adding it if necessary
800 * (in which case we initialize the data pointer to NULL.)
801 */
802static void **
803hash_findi_add(struct hash_table *hash, const char *str)
804{
805 struct hash_insert hi;
806 void **r;
807 char *strx;
808
809 r = hash_findi(hash, str, &hi);
810 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300811 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700812
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300813 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700814 return hash_add(&hi, strx, NULL);
815}
816
817/*
818 * Like hash_findi, but returns the data element rather than a pointer
819 * to it. Used only when not adding a new element, hence no third
820 * argument.
821 */
822static void *
823hash_findix(struct hash_table *hash, const char *str)
824{
825 void **p;
826
827 p = hash_findi(hash, str, NULL);
828 return p ? *p : NULL;
829}
830
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400831/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500832 * read line from standard macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400833 * if there no more left -- return NULL
834 */
835static char *line_from_stdmac(void)
836{
837 unsigned char c;
838 const unsigned char *p = stdmacpos;
839 char *line, *q;
840 size_t len = 0;
841
842 if (!stdmacpos)
843 return NULL;
844
845 while ((c = *p++)) {
846 if (c >= 0x80)
847 len += pp_directives_len[c - 0x80] + 1;
848 else
849 len++;
850 }
851
852 line = nasm_malloc(len + 1);
853 q = line;
854 while ((c = *stdmacpos++)) {
855 if (c >= 0x80) {
856 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
857 q += pp_directives_len[c - 0x80];
858 *q++ = ' ';
859 } else {
860 *q++ = c;
861 }
862 }
863 stdmacpos = p;
864 *q = '\0';
865
866 if (!*stdmacpos) {
867 /* This was the last of the standard macro chain... */
868 stdmacpos = NULL;
869 if (any_extrastdmac) {
870 stdmacpos = extrastdmac;
871 any_extrastdmac = false;
872 } else if (do_predef) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300873 ExpInv *ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400874 Line *pd, *l;
875 Token *head, **tail, *t;
876
877 /*
878 * Nasty hack: here we push the contents of
879 * `predef' on to the top-level expansion stack,
880 * since this is the most convenient way to
881 * implement the pre-include and pre-define
882 * features.
883 */
884 list_for_each(pd, predef) {
885 head = NULL;
886 tail = &head;
887 list_for_each(t, pd->first) {
888 *tail = new_Token(NULL, t->type, t->text, 0);
889 tail = &(*tail)->next;
890 }
891
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500892 l = new_Line();
893 l->first = head;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300894 ei = new_ExpInv(EXP_PREDEF, NULL);
895 ei->current = l;
896 ei->emitting = true;
897 ei->prev = istk->expansion;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500898 istk->expansion = ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400899 }
900 do_predef = false;
901 }
902 }
903
904 return line;
905}
906
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000907#define BUF_DELTA 512
908/*
909 * Read a line from the top file in istk, handling multiple CR/LFs
910 * at the end of the line read, and handling spurious ^Zs. Will
911 * return lines from the standard macro set if this has not already
912 * been done.
913 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000914static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000915{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000916 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000917 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000918
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400919 /*
920 * standart macros set (predefined) goes first
921 */
922 p = line_from_stdmac();
923 if (p)
924 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700925
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400926 /*
927 * regular read from a file
928 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000929 bufsize = BUF_DELTA;
930 buffer = nasm_malloc(BUF_DELTA);
931 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000932 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000933 while (1) {
934 q = fgets(p, bufsize - (p - buffer), istk->fp);
935 if (!q)
936 break;
937 p += strlen(p);
938 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300939 /*
940 * Convert backslash-CRLF line continuation sequences into
941 * nothing at all (for DOS and Windows)
942 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000943 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
944 p -= 3;
945 *p = 0;
946 continued_count++;
947 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300948 /*
949 * Also convert backslash-LF line continuation sequences into
950 * nothing at all (for Unix)
951 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000952 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
953 p -= 2;
954 *p = 0;
955 continued_count++;
956 } else {
957 break;
958 }
959 }
960 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000961 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000962 bufsize += BUF_DELTA;
963 buffer = nasm_realloc(buffer, bufsize);
964 p = buffer + offset; /* prevent stale-pointer problems */
965 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000966 }
967
H. Peter Anvine2c80182005-01-15 22:15:51 +0000968 if (!q && p == buffer) {
969 nasm_free(buffer);
970 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000971 }
972
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300973 src_set_linnum(src_get_linnum() + istk->lineinc +
974 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000975
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000976 /*
977 * Play safe: remove CRs as well as LFs, if any of either are
978 * present at the end of the line.
979 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000980 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000981 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000982
983 /*
984 * Handle spurious ^Z, which may be inserted into source files
985 * by some file transfer utilities.
986 */
987 buffer[strcspn(buffer, "\032")] = '\0';
988
H. Peter Anvin734b1882002-04-30 21:01:08 +0000989 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000990
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000991 return buffer;
992}
993
994/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000995 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000996 * don't need to parse the value out of e.g. numeric tokens: we
997 * simply split one string into many.
998 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000999static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001000{
H. Peter Anvinca544db2008-10-19 19:30:11 -07001001 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001002 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001003 Token *list = NULL;
1004 Token *t, **tail = &list;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001005 bool verbose = true;
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +04001006
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +04001007 nasm_trace("Tokenize for '%s'", line);
1008
Keith Kanios6faad4e2010-12-18 14:08:02 -06001009 if ((defining != NULL) && (defining->ignoring == true)) {
1010 verbose = false;
1011 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001012
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 while (*line) {
1014 p = line;
1015 if (*p == '%') {
1016 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001017 if (*p == '+' && !nasm_isdigit(p[1])) {
1018 p++;
1019 type = TOK_PASTE;
1020 } else if (nasm_isdigit(*p) ||
1021 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001022 do {
1023 p++;
1024 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001025 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001026 type = TOK_PREPROC_ID;
1027 } else if (*p == '{') {
1028 p++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001029 while (*p && *p != '}') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001030 p[-1] = *p;
1031 p++;
1032 }
1033 p[-1] = '\0';
1034 if (*p)
1035 p++;
1036 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001037 } else if (*p == '[') {
1038 int lvl = 1;
1039 line += 2; /* Skip the leading %[ */
1040 p++;
1041 while (lvl && (c = *p++)) {
1042 switch (c) {
1043 case ']':
1044 lvl--;
1045 break;
1046 case '%':
1047 if (*p == '[')
1048 lvl++;
1049 break;
1050 case '\'':
1051 case '\"':
1052 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +04001053 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001054 break;
1055 default:
1056 break;
1057 }
1058 }
1059 p--;
1060 if (*p)
1061 *p++ = '\0';
Keith Kanios6faad4e2010-12-18 14:08:02 -06001062 if (lvl && verbose)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001063 error(ERR_NONFATAL, "unterminated %[ construct");
1064 type = TOK_INDIRECT;
1065 } else if (*p == '?') {
1066 type = TOK_PREPROC_Q; /* %? */
1067 p++;
1068 if (*p == '?') {
1069 type = TOK_PREPROC_QQ; /* %?? */
1070 p++;
1071 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001072 } else if (*p == '!') {
1073 type = TOK_PREPROC_ID;
1074 p++;
1075 if (isidchar(*p)) {
1076 do {
1077 p++;
1078 } while (isidchar(*p));
1079 } else if (*p == '\'' || *p == '\"' || *p == '`') {
1080 p = nasm_skip_string(p);
1081 if (*p)
1082 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001083 else if(verbose)
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001084 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
1085 } else {
1086 /* %! without string or identifier */
1087 type = TOK_OTHER; /* Legacy behavior... */
1088 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001089 } else if (isidchar(*p) ||
1090 ((*p == '!' || *p == '%' || *p == '$') &&
1091 isidchar(p[1]))) {
1092 do {
1093 p++;
1094 }
1095 while (isidchar(*p));
1096 type = TOK_PREPROC_ID;
1097 } else {
1098 type = TOK_OTHER;
1099 if (*p == '%')
1100 p++;
1101 }
1102 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1103 type = TOK_ID;
1104 p++;
1105 while (*p && isidchar(*p))
1106 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001107 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001108 /*
1109 * A string token.
1110 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001112 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001113
H. Peter Anvine2c80182005-01-15 22:15:51 +00001114 if (*p) {
1115 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001116 } else if(verbose) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001117 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001118 /* Handling unterminated strings by UNV */
1119 /* type = -1; */
1120 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001121 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001122 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001123 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001125 bool is_hex = false;
1126 bool is_float = false;
1127 bool has_e = false;
1128 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001129
H. Peter Anvine2c80182005-01-15 22:15:51 +00001130 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001131 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001133
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001134 if (*p == '$') {
1135 p++;
1136 is_hex = true;
1137 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001138
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001139 for (;;) {
1140 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001141
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001142 if (!is_hex && (c == 'e' || c == 'E')) {
1143 has_e = true;
1144 if (*p == '+' || *p == '-') {
1145 /*
1146 * e can only be followed by +/- if it is either a
1147 * prefixed hex number or a floating-point number
1148 */
1149 p++;
1150 is_float = true;
1151 }
1152 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1153 is_hex = true;
1154 } else if (c == 'P' || c == 'p') {
1155 is_float = true;
1156 if (*p == '+' || *p == '-')
1157 p++;
1158 } else if (isnumchar(c) || c == '_')
1159 ; /* just advance */
1160 else if (c == '.') {
1161 /*
1162 * we need to deal with consequences of the legacy
1163 * parser, like "1.nolist" being two tokens
1164 * (TOK_NUMBER, TOK_ID) here; at least give it
1165 * a shot for now. In the future, we probably need
1166 * a flex-based scanner with proper pattern matching
1167 * to do it as well as it can be done. Nothing in
1168 * the world is going to help the person who wants
1169 * 0x123.p16 interpreted as two tokens, though.
1170 */
1171 r = p;
1172 while (*r == '_')
1173 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001174
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001175 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1176 (!is_hex && (*r == 'e' || *r == 'E')) ||
1177 (*r == 'p' || *r == 'P')) {
1178 p = r;
1179 is_float = true;
1180 } else
1181 break; /* Terminate the token */
1182 } else
1183 break;
1184 }
1185 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001186
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001187 if (p == line+1 && *line == '$') {
1188 type = TOK_OTHER; /* TOKEN_HERE */
1189 } else {
1190 if (has_e && !is_hex) {
1191 /* 1e13 is floating-point, but 1e13h is not */
1192 is_float = true;
1193 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001194
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001195 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1196 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001197 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001199 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001200 /*
1201 * Whitespace just before end-of-line is discarded by
1202 * pretending it's a comment; whitespace just before a
1203 * comment gets lumped into the comment.
1204 */
1205 if (!*p || *p == ';') {
1206 type = TOK_COMMENT;
1207 while (*p)
1208 p++;
1209 }
1210 } else if (*p == ';') {
1211 type = TOK_COMMENT;
1212 while (*p)
1213 p++;
1214 } else {
1215 /*
1216 * Anything else is an operator of some kind. We check
1217 * for all the double-character operators (>>, <<, //,
1218 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001219 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001220 */
1221 type = TOK_OTHER;
1222 if ((p[0] == '>' && p[1] == '>') ||
1223 (p[0] == '<' && p[1] == '<') ||
1224 (p[0] == '/' && p[1] == '/') ||
1225 (p[0] == '<' && p[1] == '=') ||
1226 (p[0] == '>' && p[1] == '=') ||
1227 (p[0] == '=' && p[1] == '=') ||
1228 (p[0] == '!' && p[1] == '=') ||
1229 (p[0] == '<' && p[1] == '>') ||
1230 (p[0] == '&' && p[1] == '&') ||
1231 (p[0] == '|' && p[1] == '|') ||
1232 (p[0] == '^' && p[1] == '^')) {
1233 p++;
1234 }
1235 p++;
1236 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001237
H. Peter Anvine2c80182005-01-15 22:15:51 +00001238 /* Handling unterminated string by UNV */
1239 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001240 {
1241 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1242 t->text[p-line] = *line;
1243 tail = &t->next;
1244 }
1245 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001246 if (type != TOK_COMMENT) {
1247 *tail = t = new_Token(NULL, type, line, p - line);
1248 tail = &t->next;
1249 }
1250 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001251 }
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +04001252
1253 nasm_dump_token(list);
1254
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001255 return list;
1256}
1257
H. Peter Anvince616072002-04-30 21:02:23 +00001258/*
1259 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001260 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001261 * deleted only all at once by the delete_Blocks function.
1262 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001264{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001265 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001266
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001267 /* first, get to the end of the linked list */
1268 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001269 b = b->next;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001270
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001271 /* now allocate the requested chunk */
1272 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001273
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001274 /* now allocate a new block for the next request */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001275 b->next = nasm_zalloc(sizeof(Blocks));
1276
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001277 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001278}
1279
1280/*
1281 * this function deletes all managed blocks of memory
1282 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001283static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001284{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001285 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001286
H. Peter Anvin70653092007-10-19 14:42:29 -07001287 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001288 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001289 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001290 * free it.
1291 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 while (b) {
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04001293 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001294 a = b;
1295 b = b->next;
1296 if (a != &blocks)
1297 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001298 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001300
1301/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001302 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001303 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001304 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001305 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001306static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001307 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001308{
1309 Token *t;
1310 int i;
1311
H. Peter Anvin89cee572009-07-15 09:16:54 -04001312 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1314 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1315 freeTokens[i].next = &freeTokens[i + 1];
1316 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001317 }
1318 t = freeTokens;
1319 freeTokens = t->next;
1320 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001321 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001322 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001323 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001324 t->text = NULL;
1325 } else {
1326 if (txtlen == 0)
1327 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001328 t->text = nasm_malloc(txtlen+1);
1329 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001330 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001331 }
1332 return t;
1333}
1334
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001335static Token *copy_Token(Token * tline)
1336{
1337 Token *t, *tt, *first = NULL, *prev = NULL;
1338 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001339 for (tt = tline; tt != NULL; tt = tt->next) {
1340 if (!freeTokens) {
1341 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1342 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1343 freeTokens[i].next = &freeTokens[i + 1];
1344 freeTokens[i].next = NULL;
1345 }
1346 t = freeTokens;
1347 freeTokens = t->next;
1348 t->next = NULL;
1349 t->text = tt->text ? nasm_strdup(tt->text) : NULL;
1350 t->a.mac = tt->a.mac;
1351 t->a.len = tt->a.len;
1352 t->type = tt->type;
1353 if (prev != NULL) {
1354 prev->next = t;
1355 } else {
1356 first = t;
1357 }
1358 prev = t;
1359 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001360 return first;
1361}
1362
H. Peter Anvine2c80182005-01-15 22:15:51 +00001363static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001364{
1365 Token *next = t->next;
1366 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001367 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001368 freeTokens = t;
1369 return next;
1370}
1371
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001372/*
1373 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001374 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1375 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001376 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001377static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001378{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001379 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001380 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001381 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001382 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001383
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001384 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001385 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001386 char *v;
1387 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001388
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001389 v = t->text + 2;
1390 if (*v == '\'' || *v == '\"' || *v == '`') {
1391 size_t len = nasm_unquote(v, NULL);
1392 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001393
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001394 if (len != clen) {
1395 error(ERR_NONFATAL | ERR_PASS1,
1396 "NUL character in %! string");
1397 v = NULL;
1398 }
1399 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001400
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001401 if (v) {
1402 char *p = getenv(v);
1403 if (!p) {
1404 error(ERR_NONFATAL | ERR_PASS1,
1405 "nonexistent environment variable `%s'", v);
1406 p = "";
1407 }
1408 t->text = nasm_strdup(p);
1409 }
1410 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001411 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001412
H. Peter Anvine2c80182005-01-15 22:15:51 +00001413 /* Expand local macros here and not during preprocessing */
1414 if (expand_locals &&
1415 t->type == TOK_PREPROC_ID && t->text &&
1416 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001417 const char *q;
1418 char *p;
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001419 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001420 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001421 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001422 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001423 p = nasm_strcat(buffer, q);
1424 nasm_free(t->text);
1425 t->text = p;
1426 }
1427 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001428
1429 /* Expand %? and %?? directives */
Keith Kanios3136d482010-11-13 09:34:34 -06001430 if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001431 ((t->type == TOK_PREPROC_Q) ||
1432 (t->type == TOK_PREPROC_QQ))) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001433 ExpInv *ei;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001434 for (ei = istk->expansion; ei != NULL; ei = ei->prev){
1435 if (ei->type == EXP_MMACRO) {
1436 nasm_free(t->text);
1437 if (t->type == TOK_PREPROC_Q) {
1438 t->text = nasm_strdup(ei->name);
1439 } else {
1440 t->text = nasm_strdup(ei->def->name);
1441 }
1442 break;
1443 }
1444 }
1445 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001446
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001447 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001448 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001449 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001450 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001451 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001452
H. Peter Anvin734b1882002-04-30 21:01:08 +00001453 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001454
1455 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001456 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001457 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001458 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001459 q = t->text;
1460 while (*q)
1461 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001463 }
1464 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001465
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001466 return line;
1467}
1468
1469/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001470 * Initialize a new Line
1471 */
Cyrill Gorcunov29cb0bb2010-11-11 11:19:43 +03001472static inline Line *new_Line(void)
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001473{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001474 return (Line *)nasm_zalloc(sizeof(Line));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001475}
1476
1477
1478/*
1479 * Initialize a new Expansion Definition
1480 */
1481static ExpDef *new_ExpDef(int exp_type)
1482{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001483 ExpDef *ed = (ExpDef*)nasm_zalloc(sizeof(ExpDef));
1484 ed->type = exp_type;
1485 ed->casesense = true;
1486 ed->state = COND_NEVER;
1487
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001488 return ed;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001489}
1490
1491
1492/*
1493 * Initialize a new Expansion Instance
1494 */
1495static ExpInv *new_ExpInv(int exp_type, ExpDef *ed)
1496{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001497 ExpInv *ei = (ExpInv*)nasm_zalloc(sizeof(ExpInv));
1498 ei->type = exp_type;
1499 ei->def = ed;
1500 ei->unique = ++unique;
1501
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001502 if ((istk->mmac_depth < 1) &&
1503 (istk->expansion == NULL) &&
1504 (ed != NULL) &&
1505 (ed->type != EXP_MMACRO) &&
1506 (ed->type != EXP_REP) &&
1507 (ed->type != EXP_WHILE)) {
1508 ei->linnum = src_get_linnum();
1509 src_set_linnum(ei->linnum - ed->linecount - 1);
1510 } else {
1511 ei->linnum = -1;
1512 }
1513 if ((istk->expansion == NULL) ||
1514 (ei->type == EXP_MMACRO)) {
1515 ei->relno = 0;
1516 } else {
1517 ei->relno = istk->expansion->lineno;
1518 if (ed != NULL) {
1519 ei->relno -= (ed->linecount + 1);
1520 }
1521 }
1522 return ei;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001523}
1524
1525/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001526 * A scanner, suitable for use by the expression evaluator, which
1527 * operates on a line of Tokens. Expects a pointer to a pointer to
1528 * the first token in the line to be passed in as its private_data
1529 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001530 *
1531 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001532 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001533static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001534{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001535 Token **tlineptr = private_data;
1536 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001537 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001538
H. Peter Anvine2c80182005-01-15 22:15:51 +00001539 do {
1540 tline = *tlineptr;
1541 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001542 } while (tline && (tline->type == TOK_WHITESPACE ||
1543 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001544
1545 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001546 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001547
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001548 tokval->t_charptr = tline->text;
1549
H. Peter Anvin76690a12002-04-30 20:52:49 +00001550 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001551 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001552 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001553 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001554
H. Peter Anvine2c80182005-01-15 22:15:51 +00001555 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001556 p = tokval->t_charptr = tline->text;
1557 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001558 tokval->t_charptr++;
1559 return tokval->t_type = TOKEN_ID;
1560 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001561
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001562 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001563 if (r >= p+MAX_KEYWORD)
1564 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001565 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001566 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001567 *s = '\0';
1568 /* right, so we have an identifier sitting in temp storage. now,
1569 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001570 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001571 }
1572
H. Peter Anvine2c80182005-01-15 22:15:51 +00001573 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001574 bool rn_error;
1575 tokval->t_integer = readnum(tline->text, &rn_error);
1576 tokval->t_charptr = tline->text;
1577 if (rn_error)
1578 return tokval->t_type = TOKEN_ERRNUM;
1579 else
1580 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001581 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001582
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001583 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001584 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001585 }
1586
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001588 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001589
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001590 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001591 tokval->t_charptr = tline->text;
1592 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001593
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001594 if (ep[0] != bq || ep[1] != '\0')
1595 return tokval->t_type = TOKEN_ERRSTR;
1596 else
1597 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001598 }
1599
H. Peter Anvine2c80182005-01-15 22:15:51 +00001600 if (tline->type == TOK_OTHER) {
1601 if (!strcmp(tline->text, "<<"))
1602 return tokval->t_type = TOKEN_SHL;
1603 if (!strcmp(tline->text, ">>"))
1604 return tokval->t_type = TOKEN_SHR;
1605 if (!strcmp(tline->text, "//"))
1606 return tokval->t_type = TOKEN_SDIV;
1607 if (!strcmp(tline->text, "%%"))
1608 return tokval->t_type = TOKEN_SMOD;
1609 if (!strcmp(tline->text, "=="))
1610 return tokval->t_type = TOKEN_EQ;
1611 if (!strcmp(tline->text, "<>"))
1612 return tokval->t_type = TOKEN_NE;
1613 if (!strcmp(tline->text, "!="))
1614 return tokval->t_type = TOKEN_NE;
1615 if (!strcmp(tline->text, "<="))
1616 return tokval->t_type = TOKEN_LE;
1617 if (!strcmp(tline->text, ">="))
1618 return tokval->t_type = TOKEN_GE;
1619 if (!strcmp(tline->text, "&&"))
1620 return tokval->t_type = TOKEN_DBL_AND;
1621 if (!strcmp(tline->text, "^^"))
1622 return tokval->t_type = TOKEN_DBL_XOR;
1623 if (!strcmp(tline->text, "||"))
1624 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001625 }
1626
1627 /*
1628 * We have no other options: just return the first character of
1629 * the token text.
1630 */
1631 return tokval->t_type = tline->text[0];
1632}
1633
1634/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001635 * Compare a string to the name of an existing macro; this is a
1636 * simple wrapper which calls either strcmp or nasm_stricmp
1637 * depending on the value of the `casesense' parameter.
1638 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001639static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001640{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001641 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001642}
1643
1644/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001645 * Compare a string to the name of an existing macro; this is a
1646 * simple wrapper which calls either strcmp or nasm_stricmp
1647 * depending on the value of the `casesense' parameter.
1648 */
1649static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1650{
1651 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1652}
1653
1654/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001655 * Return the Context structure associated with a %$ token. Return
1656 * NULL, having _already_ reported an error condition, if the
1657 * context stack isn't deep enough for the supplied number of $
1658 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001659 *
1660 * If "namep" is non-NULL, set it to the pointer to the macro name
1661 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001662 */
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001663static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001664{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001665 Context *ctx;
1666 int i;
1667
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001668 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001669 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001670
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001671 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001672 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001673
H. Peter Anvine2c80182005-01-15 22:15:51 +00001674 if (!cstk) {
1675 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1676 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001677 }
1678
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001679 name += 2;
1680 ctx = cstk;
1681 i = 0;
1682 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001683 name++;
1684 i++;
1685 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001686 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001687
H. Peter Anvine2c80182005-01-15 22:15:51 +00001688 if (!ctx) {
1689 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001690 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001691 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001692 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001693
1694 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001695 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001696
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001697 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001698}
1699
1700/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001701 * Check to see if a file is already in a string list
1702 */
1703static bool in_list(const StrList *list, const char *str)
1704{
1705 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001706 if (!strcmp(list->str, str))
1707 return true;
1708 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001709 }
1710 return false;
1711}
1712
1713/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001714 * Open an include file. This routine must always return a valid
1715 * file pointer if it returns - it's responsible for throwing an
1716 * ERR_FATAL and bombing out completely if not. It should also try
1717 * the include path one by one until it finds the file or reaches
1718 * the end of the path.
1719 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001720static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001721 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001722{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001723 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001724 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001725 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001726 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001727 size_t prefix_len = 0;
1728 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001729
H. Peter Anvine2c80182005-01-15 22:15:51 +00001730 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001731 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunov6f38fe62010-11-11 11:32:16 +03001732 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001733 memcpy(sl->str, prefix, prefix_len);
1734 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001735 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001736 if (fp && dhead && !in_list(*dhead, sl->str)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001737 **dtail = sl;
1738 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001739 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001740 nasm_free(sl);
1741 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 if (fp)
1743 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001744 if (!ip) {
1745 if (!missing_ok)
1746 break;
1747 prefix = NULL;
1748 } else {
1749 prefix = ip->path;
1750 ip = ip->next;
1751 }
1752 if (prefix) {
1753 prefix_len = strlen(prefix);
1754 } else {
1755 /* -MG given and file not found */
1756 if (dhead && !in_list(*dhead, file)) {
1757 sl = nasm_malloc(len+1+sizeof sl->next);
1758 sl->next = NULL;
1759 strcpy(sl->str, file);
1760 **dtail = sl;
1761 *dtail = &sl->next;
1762 }
1763 return NULL;
1764 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001765 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001766
H. Peter Anvin734b1882002-04-30 21:01:08 +00001767 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001768 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001769}
1770
1771/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001772 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001773 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001774 * return true if _any_ single-line macro of that name is defined.
1775 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001776 * `nparam' or no parameters is defined.
1777 *
1778 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001779 * defined, or nparam is -1, the address of the definition structure
1780 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001781 * is NULL, no action will be taken regarding its contents, and no
1782 * error will occur.
1783 *
1784 * Note that this is also called with nparam zero to resolve
1785 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001786 *
1787 * If you already know which context macro belongs to, you can pass
1788 * the context pointer as first parameter; if you won't but name begins
1789 * with %$ the context will be automatically computed. If all_contexts
1790 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001791 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001792static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001793smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001794 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001795{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001796 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001797 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001798
H. Peter Anvin97a23472007-09-16 17:57:25 -07001799 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001800 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001801 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001802 if (cstk)
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001803 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001804 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001805 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001806 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001807 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001808 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001809 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001810 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001811
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 while (m) {
1813 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001814 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001815 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001816 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 *defn = m;
1818 else
1819 *defn = NULL;
1820 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001821 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001822 }
1823 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001824 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001825
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001826 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001827}
1828
1829/*
1830 * Count and mark off the parameters in a multi-line macro call.
1831 * This is called both from within the multi-line macro expansion
1832 * code, and also to mark off the default parameters when provided
1833 * in a %macro definition line.
1834 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001835static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001836{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001837 int paramsize, brace;
1838
1839 *nparam = paramsize = 0;
1840 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001841 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001842 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001843 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001844 paramsize += PARAM_DELTA;
1845 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1846 }
1847 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001848 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001849 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001850 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001851 (*params)[(*nparam)++] = t;
1852 while (tok_isnt_(t, brace ? "}" : ","))
1853 t = t->next;
1854 if (t) { /* got a comma/brace */
1855 t = t->next;
1856 if (brace) {
1857 /*
1858 * Now we've found the closing brace, look further
1859 * for the comma.
1860 */
1861 skip_white_(t);
1862 if (tok_isnt_(t, ",")) {
1863 error(ERR_NONFATAL,
1864 "braces do not enclose all of macro parameter");
1865 while (tok_isnt_(t, ","))
1866 t = t->next;
1867 }
1868 if (t)
1869 t = t->next; /* eat the comma */
1870 }
1871 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001872 }
1873}
1874
1875/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001876 * Determine whether one of the various `if' conditions is true or
1877 * not.
1878 *
1879 * We must free the tline we get passed.
1880 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001881static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001882{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001883 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001884 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001885 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001886 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001887 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001888 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001889 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001890
1891 origline = tline;
1892
H. Peter Anvine2c80182005-01-15 22:15:51 +00001893 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001894 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001895 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001896 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001897 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001898 if (!tline)
1899 break;
1900 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001901 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001902 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001903 free_tlist(origline);
1904 return -1;
1905 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001906 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001907 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001908 tline = tline->next;
1909 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001910 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001911
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001912 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001913 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001914 while (tline) {
1915 skip_white_(tline);
1916 if (!tline || (tline->type != TOK_ID &&
1917 (tline->type != TOK_PREPROC_ID ||
1918 tline->text[1] != '$'))) {
1919 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001920 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001921 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001922 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001923 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001924 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001925 tline = tline->next;
1926 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001927 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001928
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001929 case PPC_IFENV:
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001930 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001931 j = false; /* have we matched yet? */
1932 while (tline) {
1933 skip_white_(tline);
1934 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001935 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001936 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001937 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001938 error(ERR_NONFATAL,
1939 "`%s' expects environment variable names",
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001940 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001941 goto fail;
1942 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001943 p = tline->text;
1944 if (tline->type == TOK_PREPROC_ID)
1945 p += 2; /* Skip leading %! */
1946 if (*p == '\'' || *p == '\"' || *p == '`')
1947 nasm_unquote_cstr(p, ct);
1948 if (getenv(p))
1949 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001950 tline = tline->next;
1951 }
1952 break;
1953
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001954 case PPC_IFIDN:
1955 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001956 tline = expand_smacro(tline);
1957 t = tt = tline;
1958 while (tok_isnt_(tt, ","))
1959 tt = tt->next;
1960 if (!tt) {
1961 error(ERR_NONFATAL,
1962 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001963 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001964 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001965 }
1966 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001967 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001968 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1969 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1970 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001971 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001972 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001973 }
1974 if (t->type == TOK_WHITESPACE) {
1975 t = t->next;
1976 continue;
1977 }
1978 if (tt->type == TOK_WHITESPACE) {
1979 tt = tt->next;
1980 continue;
1981 }
1982 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001983 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001984 break;
1985 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001986 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001987 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001988 size_t l1 = nasm_unquote(t->text, NULL);
1989 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001990
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001991 if (l1 != l2) {
1992 j = false;
1993 break;
1994 }
1995 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1996 j = false;
1997 break;
1998 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001999 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002000 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002001 break;
2002 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002003
H. Peter Anvine2c80182005-01-15 22:15:51 +00002004 t = t->next;
2005 tt = tt->next;
2006 }
2007 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002008 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002009 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002010
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002011 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002012 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002013 bool found = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002014 ExpDef searching, *ed;
H. Peter Anvin65747262002-05-07 00:10:05 +00002015
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002016 skip_white_(tline);
2017 tline = expand_id(tline);
2018 if (!tok_type_(tline, TOK_ID)) {
2019 error(ERR_NONFATAL,
2020 "`%s' expects a macro name", pp_directives[ct]);
2021 goto fail;
2022 }
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002023 memset(&searching, 0, sizeof(searching));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002024 searching.name = nasm_strdup(tline->text);
2025 searching.casesense = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002026 searching.nparam_max = INT_MAX;
2027 tline = expand_smacro(tline->next);
2028 skip_white_(tline);
2029 if (!tline) {
2030 } else if (!tok_type_(tline, TOK_NUMBER)) {
2031 error(ERR_NONFATAL,
2032 "`%s' expects a parameter count or nothing",
2033 pp_directives[ct]);
2034 } else {
2035 searching.nparam_min = searching.nparam_max =
2036 readnum(tline->text, &j);
2037 if (j)
2038 error(ERR_NONFATAL,
2039 "unable to parse parameter count `%s'",
2040 tline->text);
2041 }
2042 if (tline && tok_is_(tline->next, "-")) {
2043 tline = tline->next->next;
2044 if (tok_is_(tline, "*"))
2045 searching.nparam_max = INT_MAX;
2046 else if (!tok_type_(tline, TOK_NUMBER))
2047 error(ERR_NONFATAL,
2048 "`%s' expects a parameter count after `-'",
2049 pp_directives[ct]);
2050 else {
2051 searching.nparam_max = readnum(tline->text, &j);
2052 if (j)
2053 error(ERR_NONFATAL,
2054 "unable to parse parameter count `%s'",
2055 tline->text);
2056 if (searching.nparam_min > searching.nparam_max)
2057 error(ERR_NONFATAL,
2058 "minimum parameter count exceeds maximum");
2059 }
2060 }
2061 if (tline && tok_is_(tline->next, "+")) {
2062 tline = tline->next;
2063 searching.plus = true;
2064 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002065 ed = (ExpDef *) hash_findix(&expdefs, searching.name);
2066 while (ed != NULL) {
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002067 if (!strcmp(ed->name, searching.name) &&
2068 (ed->nparam_min <= searching.nparam_max || searching.plus) &&
2069 (searching.nparam_min <= ed->nparam_max || ed->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002070 found = true;
2071 break;
2072 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002073 ed = ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002074 }
2075 if (tline && tline->next)
2076 error(ERR_WARNING|ERR_PASS1,
2077 "trailing garbage after %%ifmacro ignored");
2078 nasm_free(searching.name);
2079 j = found;
2080 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002081 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002082
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002083 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002084 needtype = TOK_ID;
2085 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002086 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002087 needtype = TOK_NUMBER;
2088 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002089 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002090 needtype = TOK_STRING;
2091 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002092
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002093iftype:
2094 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002095
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002096 while (tok_type_(t, TOK_WHITESPACE) ||
2097 (needtype == TOK_NUMBER &&
2098 tok_type_(t, TOK_OTHER) &&
2099 (t->text[0] == '-' || t->text[0] == '+') &&
2100 !t->text[1]))
2101 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002102
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002103 j = tok_type_(t, needtype);
2104 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002105
2106 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002107 t = tline = expand_smacro(tline);
2108 while (tok_type_(t, TOK_WHITESPACE))
2109 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002110
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002111 j = false;
2112 if (t) {
2113 t = t->next; /* Skip the actual token */
2114 while (tok_type_(t, TOK_WHITESPACE))
2115 t = t->next;
2116 j = !t; /* Should be nothing left */
2117 }
2118 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002119
H. Peter Anvin134b9462008-02-16 17:01:40 -08002120 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002121 t = tline = expand_smacro(tline);
2122 while (tok_type_(t, TOK_WHITESPACE))
2123 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002124
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002125 j = !t; /* Should be empty */
2126 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002127
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002128 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002129 t = tline = expand_smacro(tline);
2130 tptr = &t;
2131 tokval.t_type = TOKEN_INVALID;
2132 evalresult = evaluate(ppscan, tptr, &tokval,
2133 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002134 if (!evalresult)
2135 return -1;
2136 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002137 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002138 "trailing garbage after expression ignored");
2139 if (!is_simple(evalresult)) {
2140 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002141 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002142 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002143 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002144 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002145 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002146
H. Peter Anvine2c80182005-01-15 22:15:51 +00002147 default:
2148 error(ERR_FATAL,
2149 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002150 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002151 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002152 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002153
2154 free_tlist(origline);
2155 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002156
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002157fail:
2158 free_tlist(origline);
2159 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002160}
2161
2162/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002163 * Common code for defining an smacro
2164 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002165static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002166 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002167{
2168 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002169 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002170
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002171 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002172 if (!smac) {
2173 error(ERR_WARNING|ERR_PASS1,
2174 "single-line macro `%s' defined both with and"
2175 " without parameters", mname);
2176 /*
2177 * Some instances of the old code considered this a failure,
2178 * some others didn't. What is the right thing to do here?
2179 */
2180 free_tlist(expansion);
2181 return false; /* Failure */
2182 } else {
2183 /*
2184 * We're redefining, so we have to take over an
2185 * existing SMacro structure. This means freeing
2186 * what was already in it.
2187 */
2188 nasm_free(smac->name);
2189 free_tlist(smac->expansion);
2190 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002191 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002192 smtbl = ctx ? &ctx->localmac : &smacros;
2193 smhead = (SMacro **) hash_findi_add(smtbl, mname);
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002194 smac = nasm_zalloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002195 smac->next = *smhead;
2196 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002197 }
2198 smac->name = nasm_strdup(mname);
2199 smac->casesense = casesense;
2200 smac->nparam = nparam;
2201 smac->expansion = expansion;
2202 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002203 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002204}
2205
2206/*
2207 * Undefine an smacro
2208 */
2209static void undef_smacro(Context *ctx, const char *mname)
2210{
2211 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002212 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002213
H. Peter Anvin166c2472008-05-28 12:28:58 -07002214 smtbl = ctx ? &ctx->localmac : &smacros;
2215 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002216
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002217 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002218 /*
2219 * We now have a macro name... go hunt for it.
2220 */
2221 sp = smhead;
2222 while ((s = *sp) != NULL) {
2223 if (!mstrcmp(s->name, mname, s->casesense)) {
2224 *sp = s->next;
2225 nasm_free(s->name);
2226 free_tlist(s->expansion);
2227 nasm_free(s);
2228 } else {
2229 sp = &s->next;
2230 }
2231 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002232 }
2233}
2234
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002235/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002236 * Parse a mmacro specification.
2237 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002238static bool parse_mmacro_spec(Token *tline, ExpDef *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002239{
2240 bool err;
2241
2242 tline = tline->next;
2243 skip_white_(tline);
2244 tline = expand_id(tline);
2245 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002246 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2247 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002248 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002249
H. Peter Anvina26433d2008-07-16 14:40:01 -07002250 def->name = nasm_strdup(tline->text);
2251 def->plus = false;
2252 def->nolist = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002253// def->in_progress = 0;
2254// def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002255 def->nparam_min = 0;
2256 def->nparam_max = 0;
2257
H. Peter Anvina26433d2008-07-16 14:40:01 -07002258 tline = expand_smacro(tline->next);
2259 skip_white_(tline);
2260 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002261 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002262 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002263 def->nparam_min = def->nparam_max =
2264 readnum(tline->text, &err);
2265 if (err)
2266 error(ERR_NONFATAL,
2267 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002268 }
2269 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002270 tline = tline->next->next;
2271 if (tok_is_(tline, "*")) {
2272 def->nparam_max = INT_MAX;
2273 } else if (!tok_type_(tline, TOK_NUMBER)) {
2274 error(ERR_NONFATAL,
2275 "`%s' expects a parameter count after `-'", directive);
2276 } else {
2277 def->nparam_max = readnum(tline->text, &err);
2278 if (err) {
2279 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2280 tline->text);
2281 }
2282 if (def->nparam_min > def->nparam_max) {
2283 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2284 }
2285 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002286 }
2287 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002288 tline = tline->next;
2289 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002290 }
2291 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002292 !nasm_stricmp(tline->next->text, ".nolist")) {
2293 tline = tline->next;
2294 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002295 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002296
H. Peter Anvina26433d2008-07-16 14:40:01 -07002297 /*
2298 * Handle default parameters.
2299 */
2300 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002301 def->dlist = tline->next;
2302 tline->next = NULL;
2303 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002304 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002305 def->dlist = NULL;
2306 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002307 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002308 def->line = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002309
H. Peter Anvin89cee572009-07-15 09:16:54 -04002310 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002311 !def->plus)
2312 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2313 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002314
H. Peter Anvina26433d2008-07-16 14:40:01 -07002315 return true;
2316}
2317
2318
2319/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002320 * Decode a size directive
2321 */
2322static int parse_size(const char *str) {
2323 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002324 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002325 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002326 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002327
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002328 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002329}
2330
Ed Beroset3ab3f412002-06-11 03:31:49 +00002331/**
2332 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002333 * Find out if a line contains a preprocessor directive, and deal
2334 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002335 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002336 * If a directive _is_ found, it is the responsibility of this routine
2337 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002338 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002339 * @param tline a pointer to the current tokeninzed line linked list
2340 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002341 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002342 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002343static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002344{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002345 enum preproc_token i;
2346 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002347 bool err;
2348 int nparam;
2349 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002350 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002351 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002352 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002353 char *p, *pp;
2354 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002355 Include *inc;
2356 Context *ctx;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002357 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002358 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002359 struct tokenval tokval;
2360 expr *evalresult;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002361 ExpDef *ed, *eed, **edhead;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002362 ExpInv *ei, *eei;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002363 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002364 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002365 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002366
2367 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002368
H. Peter Anvineba20a72002-04-30 20:53:55 +00002369 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002370 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002371 (tline->text[1] == '%' || tline->text[1] == '$'
2372 || tline->text[1] == '!'))
2373 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002374
H. Peter Anvin4169a472007-09-12 01:29:43 +00002375 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002376
H. Peter Anvin4169a472007-09-12 01:29:43 +00002377 switch (i) {
2378 case PP_INVALID:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002379 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002380 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2381 tline->text);
2382 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002383
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 case PP_STACKSIZE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002385 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002386 /* Directive to tell NASM what the default stack size is. The
2387 * default is for a 16-bit stack, and this can be overriden with
2388 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002389 */
2390 tline = tline->next;
2391 if (tline && tline->type == TOK_WHITESPACE)
2392 tline = tline->next;
2393 if (!tline || tline->type != TOK_ID) {
2394 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2395 free_tlist(origline);
2396 return DIRECTIVE_FOUND;
2397 }
2398 if (nasm_stricmp(tline->text, "flat") == 0) {
2399 /* All subsequent ARG directives are for a 32-bit stack */
2400 StackSize = 4;
2401 StackPointer = "ebp";
2402 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002403 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002404 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2405 /* All subsequent ARG directives are for a 64-bit stack */
2406 StackSize = 8;
2407 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002408 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002409 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002410 } else if (nasm_stricmp(tline->text, "large") == 0) {
2411 /* All subsequent ARG directives are for a 16-bit stack,
2412 * far function call.
2413 */
2414 StackSize = 2;
2415 StackPointer = "bp";
2416 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002417 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002418 } else if (nasm_stricmp(tline->text, "small") == 0) {
2419 /* All subsequent ARG directives are for a 16-bit stack,
2420 * far function call. We don't support near functions.
2421 */
2422 StackSize = 2;
2423 StackPointer = "bp";
2424 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002425 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002426 } else {
2427 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2428 free_tlist(origline);
2429 return DIRECTIVE_FOUND;
2430 }
2431 free_tlist(origline);
2432 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002433
H. Peter Anvine2c80182005-01-15 22:15:51 +00002434 case PP_ARG:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002435 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002436 /* TASM like ARG directive to define arguments to functions, in
2437 * the following form:
2438 *
2439 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2440 */
2441 offset = ArgOffset;
2442 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002443 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002444 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002445
H. Peter Anvine2c80182005-01-15 22:15:51 +00002446 /* Find the argument name */
2447 tline = tline->next;
2448 if (tline && tline->type == TOK_WHITESPACE)
2449 tline = tline->next;
2450 if (!tline || tline->type != TOK_ID) {
2451 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2452 free_tlist(origline);
2453 return DIRECTIVE_FOUND;
2454 }
2455 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002456
H. Peter Anvine2c80182005-01-15 22:15:51 +00002457 /* Find the argument size type */
2458 tline = tline->next;
2459 if (!tline || tline->type != TOK_OTHER
2460 || tline->text[0] != ':') {
2461 error(ERR_NONFATAL,
2462 "Syntax error processing `%%arg' directive");
2463 free_tlist(origline);
2464 return DIRECTIVE_FOUND;
2465 }
2466 tline = tline->next;
2467 if (!tline || tline->type != TOK_ID) {
2468 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2469 free_tlist(origline);
2470 return DIRECTIVE_FOUND;
2471 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002472
H. Peter Anvine2c80182005-01-15 22:15:51 +00002473 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002474 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002475 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002476 size = parse_size(tt->text);
2477 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002478 error(ERR_NONFATAL,
2479 "Invalid size type for `%%arg' missing directive");
2480 free_tlist(tt);
2481 free_tlist(origline);
2482 return DIRECTIVE_FOUND;
2483 }
2484 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002485
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002486 /* Round up to even stack slots */
2487 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002488
H. Peter Anvine2c80182005-01-15 22:15:51 +00002489 /* Now define the macro for the argument */
2490 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2491 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002492 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002493 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002494
H. Peter Anvine2c80182005-01-15 22:15:51 +00002495 /* Move to the next argument in the list */
2496 tline = tline->next;
2497 if (tline && tline->type == TOK_WHITESPACE)
2498 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002499 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002500 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002501 free_tlist(origline);
2502 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002503
H. Peter Anvine2c80182005-01-15 22:15:51 +00002504 case PP_LOCAL:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002505 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002506 /* TASM like LOCAL directive to define local variables for a
2507 * function, in the following form:
2508 *
2509 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2510 *
2511 * The '= LocalSize' at the end is ignored by NASM, but is
2512 * required by TASM to define the local parameter size (and used
2513 * by the TASM macro package).
2514 */
2515 offset = LocalOffset;
2516 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002517 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002518 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002519
H. Peter Anvine2c80182005-01-15 22:15:51 +00002520 /* Find the argument name */
2521 tline = tline->next;
2522 if (tline && tline->type == TOK_WHITESPACE)
2523 tline = tline->next;
2524 if (!tline || tline->type != TOK_ID) {
2525 error(ERR_NONFATAL,
2526 "`%%local' missing argument parameter");
2527 free_tlist(origline);
2528 return DIRECTIVE_FOUND;
2529 }
2530 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002531
H. Peter Anvine2c80182005-01-15 22:15:51 +00002532 /* Find the argument size type */
2533 tline = tline->next;
2534 if (!tline || tline->type != TOK_OTHER
2535 || tline->text[0] != ':') {
2536 error(ERR_NONFATAL,
2537 "Syntax error processing `%%local' directive");
2538 free_tlist(origline);
2539 return DIRECTIVE_FOUND;
2540 }
2541 tline = tline->next;
2542 if (!tline || tline->type != TOK_ID) {
2543 error(ERR_NONFATAL,
2544 "`%%local' missing size type parameter");
2545 free_tlist(origline);
2546 return DIRECTIVE_FOUND;
2547 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002548
H. Peter Anvine2c80182005-01-15 22:15:51 +00002549 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002550 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002551 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002552 size = parse_size(tt->text);
2553 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002554 error(ERR_NONFATAL,
2555 "Invalid size type for `%%local' missing directive");
2556 free_tlist(tt);
2557 free_tlist(origline);
2558 return DIRECTIVE_FOUND;
2559 }
2560 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002561
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002562 /* Round up to even stack slots */
2563 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002564
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002565 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002566
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002567 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002568 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2569 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002570 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002571
H. Peter Anvine2c80182005-01-15 22:15:51 +00002572 /* Now define the assign to setup the enter_c macro correctly */
2573 snprintf(directive, sizeof(directive),
2574 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002575 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002576
H. Peter Anvine2c80182005-01-15 22:15:51 +00002577 /* Move to the next argument in the list */
2578 tline = tline->next;
2579 if (tline && tline->type == TOK_WHITESPACE)
2580 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002581 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002582 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002583 free_tlist(origline);
2584 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002585
H. Peter Anvine2c80182005-01-15 22:15:51 +00002586 case PP_CLEAR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002587 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002588 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002589 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002590 "trailing garbage after `%%clear' ignored");
2591 free_macros();
2592 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002593 free_tlist(origline);
2594 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002595
H. Peter Anvin418ca702008-05-30 10:42:30 -07002596 case PP_DEPEND:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002597 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002598 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002599 skip_white_(t);
2600 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002601 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002602 error(ERR_NONFATAL, "`%%depend' expects a file name");
2603 free_tlist(origline);
2604 return DIRECTIVE_FOUND; /* but we did _something_ */
2605 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002606 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002607 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002608 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002609 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002610 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002611 nasm_unquote_cstr(p, i);
2612 if (dephead && !in_list(*dephead, p)) {
2613 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2614 sl->next = NULL;
2615 strcpy(sl->str, p);
2616 *deptail = sl;
2617 deptail = &sl->next;
2618 }
2619 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002620 return DIRECTIVE_FOUND;
2621
2622 case PP_INCLUDE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002623 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002624 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002625 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002626
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002627 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002628 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002629 error(ERR_NONFATAL, "`%%include' expects a file name");
2630 free_tlist(origline);
2631 return DIRECTIVE_FOUND; /* but we did _something_ */
2632 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002633 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002634 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002635 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002636 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002637 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002638 nasm_unquote_cstr(p, i);
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03002639 inc = nasm_zalloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002640 inc->next = istk;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002641 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002642 if (!inc->fp) {
2643 /* -MG given but file not found */
2644 nasm_free(inc);
2645 } else {
2646 inc->fname = src_set_fname(nasm_strdup(p));
2647 inc->lineno = src_set_linnum(0);
2648 inc->lineinc = 1;
2649 inc->expansion = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002650 istk = inc;
2651 list->uplevel(LIST_INCLUDE);
2652 }
2653 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002654 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002655
H. Peter Anvind2456592008-06-19 15:04:18 -07002656 case PP_USE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002657 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002658 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002659 static macros_t *use_pkg;
2660 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002661
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002662 tline = tline->next;
2663 skip_white_(tline);
2664 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002665
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002666 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002667 tline->type != TOK_INTERNAL_STRING &&
2668 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002669 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002670 free_tlist(origline);
2671 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002672 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002673 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002674 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002675 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002676 if (tline->type == TOK_STRING)
2677 nasm_unquote_cstr(tline->text, i);
2678 use_pkg = nasm_stdmac_find_package(tline->text);
2679 if (!use_pkg)
2680 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2681 else
2682 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002683 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002684 /* Not already included, go ahead and include it */
2685 stdmacpos = use_pkg;
2686 }
2687 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002688 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002689 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002690 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002691 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002692 case PP_POP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002693 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002694 tline = tline->next;
2695 skip_white_(tline);
2696 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002697 if (tline) {
2698 if (!tok_type_(tline, TOK_ID)) {
2699 error(ERR_NONFATAL, "`%s' expects a context identifier",
2700 pp_directives[i]);
2701 free_tlist(origline);
2702 return DIRECTIVE_FOUND; /* but we did _something_ */
2703 }
2704 if (tline->next)
2705 error(ERR_WARNING|ERR_PASS1,
2706 "trailing garbage after `%s' ignored",
2707 pp_directives[i]);
2708 p = nasm_strdup(tline->text);
2709 } else {
2710 p = NULL; /* Anonymous */
2711 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002712
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002713 if (i == PP_PUSH) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002714 ctx = nasm_zalloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002715 ctx->next = cstk;
2716 hash_init(&ctx->localmac, HASH_SMALL);
2717 ctx->name = p;
2718 ctx->number = unique++;
2719 cstk = ctx;
2720 } else {
2721 /* %pop or %repl */
2722 if (!cstk) {
2723 error(ERR_NONFATAL, "`%s': context stack is empty",
2724 pp_directives[i]);
2725 } else if (i == PP_POP) {
2726 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2727 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2728 "expected %s",
2729 cstk->name ? cstk->name : "anonymous", p);
2730 else
2731 ctx_pop();
2732 } else {
2733 /* i == PP_REPL */
2734 nasm_free(cstk->name);
2735 cstk->name = p;
2736 p = NULL;
2737 }
2738 nasm_free(p);
2739 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002740 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002741 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002742 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002743 severity = ERR_FATAL;
2744 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002745 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002746 severity = ERR_NONFATAL;
2747 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002748 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002749 severity = ERR_WARNING|ERR_WARN_USER;
2750 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002751
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002752issue_error:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002753 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002754 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002755 /* Only error out if this is the final pass */
2756 if (pass != 2 && i != PP_FATAL)
2757 return DIRECTIVE_FOUND;
2758
2759 tline->next = expand_smacro(tline->next);
2760 tline = tline->next;
2761 skip_white_(tline);
2762 t = tline ? tline->next : NULL;
2763 skip_white_(t);
2764 if (tok_type_(tline, TOK_STRING) && !t) {
2765 /* The line contains only a quoted string */
2766 p = tline->text;
2767 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2768 error(severity, "%s", p);
2769 } else {
2770 /* Not a quoted string, or more than a quoted string */
2771 p = detoken(tline, false);
2772 error(severity, "%s", p);
2773 nasm_free(p);
2774 }
2775 free_tlist(origline);
2776 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002777 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002778
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002779 CASE_PP_IF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002780 if (defining != NULL) {
2781 if (defining->type == EXP_IF) {
2782 defining->def_depth ++;
2783 }
2784 return NO_DIRECTIVE_FOUND;
2785 }
2786 if ((istk->expansion != NULL) &&
2787 (istk->expansion->emitting == false)) {
2788 j = COND_NEVER;
2789 } else {
2790 j = if_condition(tline->next, i);
2791 tline->next = NULL; /* it got freed */
2792 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
2793 }
2794 ed = new_ExpDef(EXP_IF);
2795 ed->state = j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002796 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
2797 ed->prev = defining;
2798 defining = ed;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002799 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002800 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002801
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002802 CASE_PP_ELIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002803 if (defining != NULL) {
2804 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2805 return NO_DIRECTIVE_FOUND;
2806 }
2807 }
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +04002808 if ((defining == NULL) || (defining->type != EXP_IF)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002809 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2810 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002811 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002812 case COND_IF_TRUE:
2813 defining->state = COND_DONE;
2814 defining->ignoring = true;
2815 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002816
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002817 case COND_DONE:
2818 case COND_NEVER:
2819 defining->ignoring = true;
2820 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002821
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002822 case COND_ELSE_TRUE:
2823 case COND_ELSE_FALSE:
2824 error_precond(ERR_WARNING|ERR_PASS1,
2825 "`%%elif' after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002826 defining->state = COND_NEVER;
2827 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002828 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002829
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002830 case COND_IF_FALSE:
2831 /*
2832 * IMPORTANT: In the case of %if, we will already have
2833 * called expand_mmac_params(); however, if we're
2834 * processing an %elif we must have been in a
2835 * non-emitting mode, which would have inhibited
2836 * the normal invocation of expand_mmac_params().
2837 * Therefore, we have to do it explicitly here.
2838 */
2839 j = if_condition(expand_mmac_params(tline->next), i);
2840 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002841 defining->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002842 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002843 defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002844 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002845 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002846 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002847 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002848
H. Peter Anvine2c80182005-01-15 22:15:51 +00002849 case PP_ELSE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002850 if (defining != NULL) {
2851 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2852 return NO_DIRECTIVE_FOUND;
2853 }
2854 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002855 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002856 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002857 "trailing garbage after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002858 if ((defining == NULL) || (defining->type != EXP_IF)) {
2859 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2860 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002861 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002862 case COND_IF_TRUE:
2863 case COND_DONE:
2864 defining->state = COND_ELSE_FALSE;
2865 defining->ignoring = true;
2866 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002867
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002868 case COND_NEVER:
2869 defining->ignoring = true;
2870 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002871
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002872 case COND_IF_FALSE:
2873 defining->state = COND_ELSE_TRUE;
2874 defining->ignoring = false;
2875 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002876
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002877 case COND_ELSE_TRUE:
2878 case COND_ELSE_FALSE:
2879 error_precond(ERR_WARNING|ERR_PASS1,
2880 "`%%else' after `%%else' ignored.");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002881 defining->state = COND_NEVER;
2882 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002883 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002884 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002885 free_tlist(origline);
2886 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002887
H. Peter Anvine2c80182005-01-15 22:15:51 +00002888 case PP_ENDIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002889 if (defining != NULL) {
2890 if (defining->type == EXP_IF) {
2891 if (defining->def_depth > 0) {
2892 defining->def_depth --;
2893 return NO_DIRECTIVE_FOUND;
2894 }
2895 } else {
2896 return NO_DIRECTIVE_FOUND;
2897 }
2898 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002899 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002900 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002901 "trailing garbage after `%%endif' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002902 if ((defining == NULL) || (defining->type != EXP_IF)) {
2903 error(ERR_NONFATAL, "`%%endif': no matching `%%if'");
2904 return DIRECTIVE_FOUND;
2905 }
2906 ed = defining;
2907 defining = ed->prev;
2908 ed->prev = expansions;
2909 expansions = ed;
2910 ei = new_ExpInv(EXP_IF, ed);
2911 ei->current = ed->line;
2912 ei->emitting = true;
2913 ei->prev = istk->expansion;
2914 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002915 free_tlist(origline);
2916 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002917
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002918 case PP_RMACRO:
2919 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002920 case PP_MACRO:
2921 case PP_IMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002922 if (defining != NULL) {
2923 if (defining->type == EXP_MMACRO) {
2924 defining->def_depth ++;
2925 }
2926 return NO_DIRECTIVE_FOUND;
2927 }
2928 ed = new_ExpDef(EXP_MMACRO);
2929 ed->max_depth =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002930 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002931 ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002932 if (!parse_mmacro_spec(tline, ed, pp_directives[i])) {
2933 nasm_free(ed);
2934 ed = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002935 return DIRECTIVE_FOUND;
2936 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002937 ed->def_depth = 0;
2938 ed->cur_depth = 0;
2939 ed->max_depth = (ed->max_depth + 1);
2940 ed->ignoring = false;
2941 ed->prev = defining;
2942 defining = ed;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002943
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002944 eed = (ExpDef *) hash_findix(&expdefs, ed->name);
2945 while (eed) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002946 if (!strcmp(eed->name, ed->name) &&
2947 (eed->nparam_min <= ed->nparam_max || ed->plus) &&
2948 (ed->nparam_min <= eed->nparam_max || eed->plus)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002949 error(ERR_WARNING|ERR_PASS1,
2950 "redefining multi-line macro `%s'", ed->name);
2951 return DIRECTIVE_FOUND;
2952 }
2953 eed = eed->next;
2954 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002955 free_tlist(origline);
2956 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002957
H. Peter Anvine2c80182005-01-15 22:15:51 +00002958 case PP_ENDM:
2959 case PP_ENDMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002960 if (defining != NULL) {
2961 if (defining->type == EXP_MMACRO) {
2962 if (defining->def_depth > 0) {
2963 defining->def_depth --;
2964 return NO_DIRECTIVE_FOUND;
2965 }
2966 } else {
2967 return NO_DIRECTIVE_FOUND;
2968 }
2969 }
2970 if (!(defining) || (defining->type != EXP_MMACRO)) {
2971 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2972 return DIRECTIVE_FOUND;
2973 }
2974 edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name);
2975 defining->next = *edhead;
2976 *edhead = defining;
2977 ed = defining;
2978 defining = ed->prev;
2979 ed->prev = expansions;
2980 expansions = ed;
2981 ed = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002982 free_tlist(origline);
2983 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002984
H. Peter Anvin89cee572009-07-15 09:16:54 -04002985 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002986 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2987 /*
2988 * We must search along istk->expansion until we hit a
2989 * macro invocation. Then we disable the emitting state(s)
2990 * between exitmacro and endmacro.
2991 */
2992 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
2993 if(ei->type == EXP_MMACRO) {
2994 break;
2995 }
2996 }
2997
2998 if (ei != NULL) {
2999 /*
3000 * Set all invocations leading back to the macro
3001 * invocation to a non-emitting state.
3002 */
3003 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3004 eei->emitting = false;
3005 }
3006 eei->emitting = false;
3007 } else {
3008 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
3009 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05003010 free_tlist(origline);
3011 return DIRECTIVE_FOUND;
3012
H. Peter Anvina26433d2008-07-16 14:40:01 -07003013 case PP_UNMACRO:
3014 case PP_UNIMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003015 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003016 {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003017 ExpDef **ed_p;
3018 ExpDef spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003019
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003020 spec.casesense = (i == PP_UNMACRO);
3021 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
3022 return DIRECTIVE_FOUND;
3023 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003024 ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL);
3025 while (ed_p && *ed_p) {
3026 ed = *ed_p;
3027 if (ed->casesense == spec.casesense &&
3028 !mstrcmp(ed->name, spec.name, spec.casesense) &&
3029 ed->nparam_min == spec.nparam_min &&
3030 ed->nparam_max == spec.nparam_max &&
3031 ed->plus == spec.plus) {
Keith Kaniosc98a5b42010-12-18 12:17:31 -06003032 if (ed->cur_depth > 0) {
Keith Kanios21d885b2010-12-18 12:22:21 -06003033 error(ERR_NONFATAL, "`%s' ignored on active macro",
Keith Kaniosc98a5b42010-12-18 12:17:31 -06003034 pp_directives[i]);
3035 break;
3036 } else {
3037 *ed_p = ed->next;
3038 free_expdef(ed);
3039 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003040 } else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003041 ed_p = &ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003042 }
3043 }
3044 free_tlist(origline);
3045 free_tlist(spec.dlist);
3046 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003047 }
3048
H. Peter Anvine2c80182005-01-15 22:15:51 +00003049 case PP_ROTATE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003050 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003051 if (tline->next && tline->next->type == TOK_WHITESPACE)
3052 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003053 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003054 free_tlist(origline);
3055 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
3056 return DIRECTIVE_FOUND;
3057 }
3058 t = expand_smacro(tline->next);
3059 tline->next = NULL;
3060 free_tlist(origline);
3061 tline = t;
3062 tptr = &t;
3063 tokval.t_type = TOKEN_INVALID;
3064 evalresult =
3065 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3066 free_tlist(tline);
3067 if (!evalresult)
3068 return DIRECTIVE_FOUND;
3069 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003070 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003071 "trailing garbage after expression ignored");
3072 if (!is_simple(evalresult)) {
3073 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
3074 return DIRECTIVE_FOUND;
3075 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003076 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3077 if (ei->type == EXP_MMACRO) {
3078 break;
3079 }
3080 }
3081 if (ei == NULL) {
3082 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
3083 } else if (ei->nparam == 0) {
3084 error(ERR_NONFATAL,
3085 "`%%rotate' invoked within macro without parameters");
3086 } else {
3087 int rotate = ei->rotate + reloc_value(evalresult);
3088
3089 rotate %= (int)ei->nparam;
3090 if (rotate < 0)
3091 rotate += ei->nparam;
3092 ei->rotate = rotate;
3093 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003094 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003095
H. Peter Anvine2c80182005-01-15 22:15:51 +00003096 case PP_REP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003097 if (defining != NULL) {
3098 if (defining->type == EXP_REP) {
3099 defining->def_depth ++;
3100 }
3101 return NO_DIRECTIVE_FOUND;
3102 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003103 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003104 do {
3105 tline = tline->next;
3106 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003107
H. Peter Anvine2c80182005-01-15 22:15:51 +00003108 if (tok_type_(tline, TOK_ID) &&
3109 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003110 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003111 do {
3112 tline = tline->next;
3113 } while (tok_type_(tline, TOK_WHITESPACE));
3114 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003115
H. Peter Anvine2c80182005-01-15 22:15:51 +00003116 if (tline) {
3117 t = expand_smacro(tline);
3118 tptr = &t;
3119 tokval.t_type = TOKEN_INVALID;
3120 evalresult =
3121 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3122 if (!evalresult) {
3123 free_tlist(origline);
3124 return DIRECTIVE_FOUND;
3125 }
3126 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003127 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003128 "trailing garbage after expression ignored");
3129 if (!is_simple(evalresult)) {
3130 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
3131 return DIRECTIVE_FOUND;
3132 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003133 count = reloc_value(evalresult);
3134 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04003135 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003136 count = 0;
3137 } else
3138 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003139 } else {
3140 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003141 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003142 }
3143 free_tlist(origline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003144 ed = new_ExpDef(EXP_REP);
3145 ed->nolist = nolist;
3146 ed->def_depth = 0;
3147 ed->cur_depth = 1;
3148 ed->max_depth = (count - 1);
3149 ed->ignoring = false;
3150 ed->prev = defining;
3151 defining = ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003152 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003153
H. Peter Anvine2c80182005-01-15 22:15:51 +00003154 case PP_ENDREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003155 if (defining != NULL) {
3156 if (defining->type == EXP_REP) {
3157 if (defining->def_depth > 0) {
3158 defining->def_depth --;
3159 return NO_DIRECTIVE_FOUND;
3160 }
3161 } else {
3162 return NO_DIRECTIVE_FOUND;
3163 }
3164 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003165 if ((defining == NULL) || (defining->type != EXP_REP)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003166 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
3167 return DIRECTIVE_FOUND;
3168 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003169
H. Peter Anvine2c80182005-01-15 22:15:51 +00003170 /*
3171 * Now we have a "macro" defined - although it has no name
3172 * and we won't be entering it in the hash tables - we must
3173 * push a macro-end marker for it on to istk->expansion.
3174 * After that, it will take care of propagating itself (a
3175 * macro-end marker line for a macro which is really a %rep
3176 * block will cause the macro to be re-expanded, complete
3177 * with another macro-end marker to ensure the process
3178 * continues) until the whole expansion is forcibly removed
3179 * from istk->expansion by a %exitrep.
3180 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003181 ed = defining;
3182 defining = ed->prev;
3183 ed->prev = expansions;
3184 expansions = ed;
3185 ei = new_ExpInv(EXP_REP, ed);
3186 ei->current = ed->line;
3187 ei->emitting = ((ed->max_depth > 0) ? true : false);
3188 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3189 ei->prev = istk->expansion;
3190 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003191 free_tlist(origline);
3192 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003193
H. Peter Anvine2c80182005-01-15 22:15:51 +00003194 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003195 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3196 /*
3197 * We must search along istk->expansion until we hit a
3198 * rep invocation. Then we disable the emitting state(s)
3199 * between exitrep and endrep.
3200 */
3201 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3202 if (ei->type == EXP_REP) {
3203 break;
3204 }
3205 }
3206
3207 if (ei != NULL) {
3208 /*
3209 * Set all invocations leading back to the rep
3210 * invocation to a non-emitting state.
3211 */
3212 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3213 eei->emitting = false;
3214 }
3215 eei->emitting = false;
3216 eei->current = NULL;
3217 eei->def->cur_depth = eei->def->max_depth;
3218 } else {
3219 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3220 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003221 free_tlist(origline);
3222 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003223
H. Peter Anvine2c80182005-01-15 22:15:51 +00003224 case PP_XDEFINE:
3225 case PP_IXDEFINE:
3226 case PP_DEFINE:
3227 case PP_IDEFINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003228 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003229 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003230
H. Peter Anvine2c80182005-01-15 22:15:51 +00003231 tline = tline->next;
3232 skip_white_(tline);
3233 tline = expand_id(tline);
3234 if (!tline || (tline->type != TOK_ID &&
3235 (tline->type != TOK_PREPROC_ID ||
3236 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003237 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003238 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003239 free_tlist(origline);
3240 return DIRECTIVE_FOUND;
3241 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003242
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003243 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003244 last = tline;
3245 param_start = tline = tline->next;
3246 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003247
H. Peter Anvine2c80182005-01-15 22:15:51 +00003248 /* Expand the macro definition now for %xdefine and %ixdefine */
3249 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3250 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003251
H. Peter Anvine2c80182005-01-15 22:15:51 +00003252 if (tok_is_(tline, "(")) {
3253 /*
3254 * This macro has parameters.
3255 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003256
H. Peter Anvine2c80182005-01-15 22:15:51 +00003257 tline = tline->next;
3258 while (1) {
3259 skip_white_(tline);
3260 if (!tline) {
3261 error(ERR_NONFATAL, "parameter identifier expected");
3262 free_tlist(origline);
3263 return DIRECTIVE_FOUND;
3264 }
3265 if (tline->type != TOK_ID) {
3266 error(ERR_NONFATAL,
3267 "`%s': parameter identifier expected",
3268 tline->text);
3269 free_tlist(origline);
3270 return DIRECTIVE_FOUND;
3271 }
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04003272
3273 smacro_set_param_idx(tline, nparam);
3274 nparam++;
3275
H. Peter Anvine2c80182005-01-15 22:15:51 +00003276 tline = tline->next;
3277 skip_white_(tline);
3278 if (tok_is_(tline, ",")) {
3279 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003280 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003281 if (!tok_is_(tline, ")")) {
3282 error(ERR_NONFATAL,
3283 "`)' expected to terminate macro template");
3284 free_tlist(origline);
3285 return DIRECTIVE_FOUND;
3286 }
3287 break;
3288 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003289 }
3290 last = tline;
3291 tline = tline->next;
3292 }
3293 if (tok_type_(tline, TOK_WHITESPACE))
3294 last = tline, tline = tline->next;
3295 macro_start = NULL;
3296 last->next = NULL;
3297 t = tline;
3298 while (t) {
3299 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003300 list_for_each(tt, param_start)
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04003301 if (is_smacro_param(tt) &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003302 !strcmp(tt->text, t->text))
3303 t->type = tt->type;
3304 }
3305 tt = t->next;
3306 t->next = macro_start;
3307 macro_start = t;
3308 t = tt;
3309 }
3310 /*
3311 * Good. We now have a macro name, a parameter count, and a
3312 * token list (in reverse order) for an expansion. We ought
3313 * to be OK just to create an SMacro, store it, and let
3314 * free_tlist have the rest of the line (which we have
3315 * carefully re-terminated after chopping off the expansion
3316 * from the end).
3317 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003318 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003319 free_tlist(origline);
3320 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003321
H. Peter Anvine2c80182005-01-15 22:15:51 +00003322 case PP_UNDEF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003323 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003324 tline = tline->next;
3325 skip_white_(tline);
3326 tline = expand_id(tline);
3327 if (!tline || (tline->type != TOK_ID &&
3328 (tline->type != TOK_PREPROC_ID ||
3329 tline->text[1] != '$'))) {
3330 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3331 free_tlist(origline);
3332 return DIRECTIVE_FOUND;
3333 }
3334 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003335 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003336 "trailing garbage after macro name ignored");
3337 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003338
H. Peter Anvine2c80182005-01-15 22:15:51 +00003339 /* Find the context that symbol belongs to */
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003340 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003341 undef_smacro(ctx, mname);
3342 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003343 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003344
H. Peter Anvin9e200162008-06-04 17:23:14 -07003345 case PP_DEFSTR:
3346 case PP_IDEFSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003347 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003348 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003349
3350 tline = tline->next;
3351 skip_white_(tline);
3352 tline = expand_id(tline);
3353 if (!tline || (tline->type != TOK_ID &&
3354 (tline->type != TOK_PREPROC_ID ||
3355 tline->text[1] != '$'))) {
3356 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003357 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003358 free_tlist(origline);
3359 return DIRECTIVE_FOUND;
3360 }
3361
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003362 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003363 last = tline;
3364 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003365 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003366
3367 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003368 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003369
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003370 p = detoken(tline, false);
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003371 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003372 macro_start->text = nasm_quote(p, strlen(p));
3373 macro_start->type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003374 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003375
3376 /*
3377 * We now have a macro name, an implicit parameter count of
3378 * zero, and a string token to use as an expansion. Create
3379 * and store an SMacro.
3380 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003381 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003382 free_tlist(origline);
3383 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003384
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003385 case PP_DEFTOK:
3386 case PP_IDEFTOK:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003387 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003388 casesense = (i == PP_DEFTOK);
3389
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003390 tline = tline->next;
3391 skip_white_(tline);
3392 tline = expand_id(tline);
3393 if (!tline || (tline->type != TOK_ID &&
3394 (tline->type != TOK_PREPROC_ID ||
3395 tline->text[1] != '$'))) {
3396 error(ERR_NONFATAL,
3397 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003398 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003399 free_tlist(origline);
3400 return DIRECTIVE_FOUND;
3401 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003402 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003403 last = tline;
3404 tline = expand_smacro(tline->next);
3405 last->next = NULL;
3406
3407 t = tline;
3408 while (tok_type_(t, TOK_WHITESPACE))
3409 t = t->next;
3410 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003411 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003412 error(ERR_NONFATAL,
3413 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003414 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003415 free_tlist(tline);
3416 free_tlist(origline);
3417 return DIRECTIVE_FOUND;
3418 }
3419
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003420 /*
3421 * Convert the string to a token stream. Note that smacros
3422 * are stored with the token stream reversed, so we have to
3423 * reverse the output of tokenize().
3424 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003425 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003426 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003427
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003428 /*
3429 * We now have a macro name, an implicit parameter count of
3430 * zero, and a numeric token to use as an expansion. Create
3431 * and store an SMacro.
3432 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003433 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003434 free_tlist(tline);
3435 free_tlist(origline);
3436 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003437
H. Peter Anvin418ca702008-05-30 10:42:30 -07003438 case PP_PATHSEARCH:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003439 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003440 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003441 FILE *fp;
3442 StrList *xsl = NULL;
3443 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003444
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003445 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003446
3447 tline = tline->next;
3448 skip_white_(tline);
3449 tline = expand_id(tline);
3450 if (!tline || (tline->type != TOK_ID &&
3451 (tline->type != TOK_PREPROC_ID ||
3452 tline->text[1] != '$'))) {
3453 error(ERR_NONFATAL,
3454 "`%%pathsearch' expects a macro identifier as first parameter");
3455 free_tlist(origline);
3456 return DIRECTIVE_FOUND;
3457 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003458 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003459 last = tline;
3460 tline = expand_smacro(tline->next);
3461 last->next = NULL;
3462
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003463 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003464 while (tok_type_(t, TOK_WHITESPACE))
3465 t = t->next;
3466
3467 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003468 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003469 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003470 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003471 free_tlist(origline);
3472 return DIRECTIVE_FOUND; /* but we did _something_ */
3473 }
3474 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003475 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003476 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003477 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003478 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003479 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003480
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003481 fp = inc_fopen(p, &xsl, &xst, true);
3482 if (fp) {
3483 p = xsl->str;
3484 fclose(fp); /* Don't actually care about the file */
3485 }
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003486 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003487 macro_start->text = nasm_quote(p, strlen(p));
3488 macro_start->type = TOK_STRING;
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04003489 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003490
3491 /*
3492 * We now have a macro name, an implicit parameter count of
3493 * zero, and a string token to use as an expansion. Create
3494 * and store an SMacro.
3495 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003496 define_smacro(ctx, mname, casesense, 0, macro_start);
3497 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003498 free_tlist(origline);
3499 return DIRECTIVE_FOUND;
3500 }
3501
H. Peter Anvine2c80182005-01-15 22:15:51 +00003502 case PP_STRLEN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003503 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003504 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003505
H. Peter Anvine2c80182005-01-15 22:15:51 +00003506 tline = tline->next;
3507 skip_white_(tline);
3508 tline = expand_id(tline);
3509 if (!tline || (tline->type != TOK_ID &&
3510 (tline->type != TOK_PREPROC_ID ||
3511 tline->text[1] != '$'))) {
3512 error(ERR_NONFATAL,
3513 "`%%strlen' expects a macro identifier as first parameter");
3514 free_tlist(origline);
3515 return DIRECTIVE_FOUND;
3516 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003517 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003518 last = tline;
3519 tline = expand_smacro(tline->next);
3520 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003521
H. Peter Anvine2c80182005-01-15 22:15:51 +00003522 t = tline;
3523 while (tok_type_(t, TOK_WHITESPACE))
3524 t = t->next;
3525 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003526 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003527 error(ERR_NONFATAL,
3528 "`%%strlen` requires string as second parameter");
3529 free_tlist(tline);
3530 free_tlist(origline);
3531 return DIRECTIVE_FOUND;
3532 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003533
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003534 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003535 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003536
H. Peter Anvine2c80182005-01-15 22:15:51 +00003537 /*
3538 * We now have a macro name, an implicit parameter count of
3539 * zero, and a numeric token to use as an expansion. Create
3540 * and store an SMacro.
3541 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003542 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003543 free_tlist(tline);
3544 free_tlist(origline);
3545 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003546
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003547 case PP_STRCAT:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003548 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003549 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003550
3551 tline = tline->next;
3552 skip_white_(tline);
3553 tline = expand_id(tline);
3554 if (!tline || (tline->type != TOK_ID &&
3555 (tline->type != TOK_PREPROC_ID ||
3556 tline->text[1] != '$'))) {
3557 error(ERR_NONFATAL,
3558 "`%%strcat' expects a macro identifier as first parameter");
3559 free_tlist(origline);
3560 return DIRECTIVE_FOUND;
3561 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003562 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003563 last = tline;
3564 tline = expand_smacro(tline->next);
3565 last->next = NULL;
3566
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003567 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003568 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003569 switch (t->type) {
3570 case TOK_WHITESPACE:
3571 break;
3572 case TOK_STRING:
3573 len += t->a.len = nasm_unquote(t->text, NULL);
3574 break;
3575 case TOK_OTHER:
3576 if (!strcmp(t->text, ",")) /* permit comma separators */
3577 break;
3578 /* else fall through */
3579 default:
3580 error(ERR_NONFATAL,
3581 "non-string passed to `%%strcat' (%d)", t->type);
3582 free_tlist(tline);
3583 free_tlist(origline);
3584 return DIRECTIVE_FOUND;
3585 }
3586 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003587
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003588 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003589 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003590 if (t->type == TOK_STRING) {
3591 memcpy(p, t->text, t->a.len);
3592 p += t->a.len;
3593 }
3594 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003595
3596 /*
3597 * We now have a macro name, an implicit parameter count of
3598 * zero, and a numeric token to use as an expansion. Create
3599 * and store an SMacro.
3600 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003601 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3602 macro_start->text = nasm_quote(pp, len);
3603 nasm_free(pp);
3604 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003605 free_tlist(tline);
3606 free_tlist(origline);
3607 return DIRECTIVE_FOUND;
3608
H. Peter Anvine2c80182005-01-15 22:15:51 +00003609 case PP_SUBSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003610 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003611 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003612 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003613 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003614
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003615 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003616
H. Peter Anvine2c80182005-01-15 22:15:51 +00003617 tline = tline->next;
3618 skip_white_(tline);
3619 tline = expand_id(tline);
3620 if (!tline || (tline->type != TOK_ID &&
3621 (tline->type != TOK_PREPROC_ID ||
3622 tline->text[1] != '$'))) {
3623 error(ERR_NONFATAL,
3624 "`%%substr' expects a macro identifier as first parameter");
3625 free_tlist(origline);
3626 return DIRECTIVE_FOUND;
3627 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003628 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003629 last = tline;
3630 tline = expand_smacro(tline->next);
3631 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003632
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003633 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003634 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003635 while (tok_type_(t, TOK_WHITESPACE))
3636 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003637
H. Peter Anvine2c80182005-01-15 22:15:51 +00003638 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003639 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003640 error(ERR_NONFATAL,
3641 "`%%substr` requires string as second parameter");
3642 free_tlist(tline);
3643 free_tlist(origline);
3644 return DIRECTIVE_FOUND;
3645 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003646
H. Peter Anvine2c80182005-01-15 22:15:51 +00003647 tt = t->next;
3648 tptr = &tt;
3649 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003650 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003651 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003652 if (!evalresult) {
3653 free_tlist(tline);
3654 free_tlist(origline);
3655 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003656 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003657 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3658 free_tlist(tline);
3659 free_tlist(origline);
3660 return DIRECTIVE_FOUND;
3661 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003662 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003663
3664 while (tok_type_(tt, TOK_WHITESPACE))
3665 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003666 if (!tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003667 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003668 } else {
3669 tokval.t_type = TOKEN_INVALID;
3670 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3671 pass, error, NULL);
3672 if (!evalresult) {
3673 free_tlist(tline);
3674 free_tlist(origline);
3675 return DIRECTIVE_FOUND;
3676 } else if (!is_simple(evalresult)) {
3677 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3678 free_tlist(tline);
3679 free_tlist(origline);
3680 return DIRECTIVE_FOUND;
3681 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003682 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003683 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003684
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003685 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003686 /* make start and count being in range */
3687 if (start < 0)
3688 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003689 if (count < 0)
3690 count = len + count + 1 - start;
3691 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003692 count = len - start;
3693 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003694 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003695
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003696 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003697 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003698 macro_start->type = TOK_STRING;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003699
H. Peter Anvine2c80182005-01-15 22:15:51 +00003700 /*
3701 * We now have a macro name, an implicit parameter count of
3702 * zero, and a numeric token to use as an expansion. Create
3703 * and store an SMacro.
3704 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003705 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003706 free_tlist(tline);
3707 free_tlist(origline);
3708 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003709 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003710
H. Peter Anvine2c80182005-01-15 22:15:51 +00003711 case PP_ASSIGN:
3712 case PP_IASSIGN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003713 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003714 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003715
H. Peter Anvine2c80182005-01-15 22:15:51 +00003716 tline = tline->next;
3717 skip_white_(tline);
3718 tline = expand_id(tline);
3719 if (!tline || (tline->type != TOK_ID &&
3720 (tline->type != TOK_PREPROC_ID ||
3721 tline->text[1] != '$'))) {
3722 error(ERR_NONFATAL,
3723 "`%%%sassign' expects a macro identifier",
3724 (i == PP_IASSIGN ? "i" : ""));
3725 free_tlist(origline);
3726 return DIRECTIVE_FOUND;
3727 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003728 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003729 last = tline;
3730 tline = expand_smacro(tline->next);
3731 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003732
H. Peter Anvine2c80182005-01-15 22:15:51 +00003733 t = tline;
3734 tptr = &t;
3735 tokval.t_type = TOKEN_INVALID;
3736 evalresult =
3737 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3738 free_tlist(tline);
3739 if (!evalresult) {
3740 free_tlist(origline);
3741 return DIRECTIVE_FOUND;
3742 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003743
H. Peter Anvine2c80182005-01-15 22:15:51 +00003744 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003745 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003746 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003747
H. Peter Anvine2c80182005-01-15 22:15:51 +00003748 if (!is_simple(evalresult)) {
3749 error(ERR_NONFATAL,
3750 "non-constant value given to `%%%sassign'",
3751 (i == PP_IASSIGN ? "i" : ""));
3752 free_tlist(origline);
3753 return DIRECTIVE_FOUND;
3754 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003755
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003756 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003757 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003758
H. Peter Anvine2c80182005-01-15 22:15:51 +00003759 /*
3760 * We now have a macro name, an implicit parameter count of
3761 * zero, and a numeric token to use as an expansion. Create
3762 * and store an SMacro.
3763 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003764 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003765 free_tlist(origline);
3766 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003767
H. Peter Anvine2c80182005-01-15 22:15:51 +00003768 case PP_LINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003769 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003770 /*
3771 * Syntax is `%line nnn[+mmm] [filename]'
3772 */
3773 tline = tline->next;
3774 skip_white_(tline);
3775 if (!tok_type_(tline, TOK_NUMBER)) {
3776 error(ERR_NONFATAL, "`%%line' expects line number");
3777 free_tlist(origline);
3778 return DIRECTIVE_FOUND;
3779 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003780 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003781 m = 1;
3782 tline = tline->next;
3783 if (tok_is_(tline, "+")) {
3784 tline = tline->next;
3785 if (!tok_type_(tline, TOK_NUMBER)) {
3786 error(ERR_NONFATAL, "`%%line' expects line increment");
3787 free_tlist(origline);
3788 return DIRECTIVE_FOUND;
3789 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003790 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003791 tline = tline->next;
3792 }
3793 skip_white_(tline);
3794 src_set_linnum(k);
3795 istk->lineinc = m;
3796 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003797 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003798 }
3799 free_tlist(origline);
3800 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003801
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003802 case PP_WHILE:
3803 if (defining != NULL) {
3804 if (defining->type == EXP_WHILE) {
3805 defining->def_depth ++;
3806 }
3807 return NO_DIRECTIVE_FOUND;
3808 }
3809 l = NULL;
3810 if ((istk->expansion != NULL) &&
3811 (istk->expansion->emitting == false)) {
3812 j = COND_NEVER;
3813 } else {
3814 l = new_Line();
3815 l->first = copy_Token(tline->next);
3816 j = if_condition(tline->next, i);
3817 tline->next = NULL; /* it got freed */
3818 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
3819 }
3820 ed = new_ExpDef(EXP_WHILE);
3821 ed->state = j;
3822 ed->cur_depth = 1;
3823 ed->max_depth = DEADMAN_LIMIT;
3824 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
3825 if (ed->ignoring == false) {
3826 ed->line = l;
3827 ed->last = l;
3828 } else if (l != NULL) {
3829 delete_Token(l->first);
3830 nasm_free(l);
3831 l = NULL;
3832 }
3833 ed->prev = defining;
3834 defining = ed;
3835 free_tlist(origline);
3836 return DIRECTIVE_FOUND;
3837
3838 case PP_ENDWHILE:
3839 if (defining != NULL) {
3840 if (defining->type == EXP_WHILE) {
3841 if (defining->def_depth > 0) {
3842 defining->def_depth --;
3843 return NO_DIRECTIVE_FOUND;
3844 }
3845 } else {
3846 return NO_DIRECTIVE_FOUND;
3847 }
3848 }
3849 if (tline->next != NULL) {
3850 error_precond(ERR_WARNING|ERR_PASS1,
3851 "trailing garbage after `%%endwhile' ignored");
3852 }
3853 if ((defining == NULL) || (defining->type != EXP_WHILE)) {
3854 error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'");
3855 return DIRECTIVE_FOUND;
3856 }
3857 ed = defining;
3858 defining = ed->prev;
3859 if (ed->ignoring == false) {
3860 ed->prev = expansions;
3861 expansions = ed;
3862 ei = new_ExpInv(EXP_WHILE, ed);
3863 ei->current = ed->line->next;
3864 ei->emitting = true;
3865 ei->prev = istk->expansion;
3866 istk->expansion = ei;
3867 } else {
3868 nasm_free(ed);
3869 }
3870 free_tlist(origline);
3871 return DIRECTIVE_FOUND;
3872
3873 case PP_EXITWHILE:
3874 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3875 /*
3876 * We must search along istk->expansion until we hit a
3877 * while invocation. Then we disable the emitting state(s)
3878 * between exitwhile and endwhile.
3879 */
3880 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3881 if (ei->type == EXP_WHILE) {
3882 break;
3883 }
3884 }
3885
3886 if (ei != NULL) {
3887 /*
3888 * Set all invocations leading back to the while
3889 * invocation to a non-emitting state.
3890 */
3891 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3892 eei->emitting = false;
3893 }
3894 eei->emitting = false;
3895 eei->current = NULL;
3896 eei->def->cur_depth = eei->def->max_depth;
3897 } else {
3898 error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block");
3899 }
3900 free_tlist(origline);
3901 return DIRECTIVE_FOUND;
3902
3903 case PP_COMMENT:
3904 if (defining != NULL) {
3905 if (defining->type == EXP_COMMENT) {
3906 defining->def_depth ++;
3907 }
3908 return NO_DIRECTIVE_FOUND;
3909 }
3910 ed = new_ExpDef(EXP_COMMENT);
3911 ed->ignoring = true;
3912 ed->prev = defining;
3913 defining = ed;
3914 free_tlist(origline);
3915 return DIRECTIVE_FOUND;
3916
3917 case PP_ENDCOMMENT:
3918 if (defining != NULL) {
3919 if (defining->type == EXP_COMMENT) {
3920 if (defining->def_depth > 0) {
3921 defining->def_depth --;
3922 return NO_DIRECTIVE_FOUND;
3923 }
3924 } else {
3925 return NO_DIRECTIVE_FOUND;
3926 }
3927 }
3928 if ((defining == NULL) || (defining->type != EXP_COMMENT)) {
3929 error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'");
3930 return DIRECTIVE_FOUND;
3931 }
3932 ed = defining;
3933 defining = ed->prev;
3934 nasm_free(ed);
3935 free_tlist(origline);
3936 return DIRECTIVE_FOUND;
3937
3938 case PP_FINAL:
3939 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3940 if (in_final != false) {
3941 error(ERR_FATAL, "`%%final' cannot be used recursively");
3942 }
3943 tline = tline->next;
3944 skip_white_(tline);
3945 if (tline == NULL) {
3946 error(ERR_NONFATAL, "`%%final' expects at least one parameter");
3947 } else {
3948 l = new_Line();
3949 l->first = copy_Token(tline);
3950 l->next = finals;
3951 finals = l;
3952 }
3953 free_tlist(origline);
3954 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003955
H. Peter Anvine2c80182005-01-15 22:15:51 +00003956 default:
3957 error(ERR_FATAL,
3958 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003959 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003960 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003961 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003962}
3963
3964/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003965 * Ensure that a macro parameter contains a condition code and
3966 * nothing else. Return the condition code index if so, or -1
3967 * otherwise.
3968 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003969static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003970{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003971 Token *tt;
3972 int i, j, k, m;
3973
H. Peter Anvin25a99342007-09-22 17:45:45 -07003974 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003975 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003976
H. Peter Anvineba20a72002-04-30 20:53:55 +00003977 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003978 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003979 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003980 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003981 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003982 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003983 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003984
3985 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003986 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003987 while (j - i > 1) {
3988 k = (j + i) / 2;
3989 m = nasm_stricmp(t->text, conditions[k]);
3990 if (m == 0) {
3991 i = k;
3992 j = -2;
3993 break;
3994 } else if (m < 0) {
3995 j = k;
3996 } else
3997 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003998 }
3999 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004000 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004001 return i;
4002}
4003
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004004static bool paste_tokens(Token **head, const struct tokseq_match *m,
4005 int mnum, bool handle_paste_tokens)
H. Peter Anvind784a082009-04-20 14:01:18 -07004006{
4007 Token **tail, *t, *tt;
4008 Token **paste_head;
4009 bool did_paste = false;
4010 char *tmp;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004011 int i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004012
4013 /* Now handle token pasting... */
4014 paste_head = NULL;
4015 tail = head;
4016 while ((t = *tail) && (tt = t->next)) {
4017 switch (t->type) {
4018 case TOK_WHITESPACE:
4019 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004020 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07004021 t->next = delete_Token(tt);
4022 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004023 /* Do not advance paste_head here */
4024 tail = &t->next;
4025 }
H. Peter Anvind784a082009-04-20 14:01:18 -07004026 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004027 case TOK_PASTE: /* %+ */
4028 if (handle_paste_tokens) {
4029 /* Zap %+ and whitespace tokens to the right */
4030 while (t && (t->type == TOK_WHITESPACE ||
4031 t->type == TOK_PASTE))
4032 t = *tail = delete_Token(t);
4033 if (!paste_head || !t)
4034 break; /* Nothing to paste with */
4035 tail = paste_head;
4036 t = *tail;
4037 tt = t->next;
4038 while (tok_type_(tt, TOK_WHITESPACE))
4039 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004040 if (tt) {
4041 tmp = nasm_strcat(t->text, tt->text);
4042 delete_Token(t);
4043 tt = delete_Token(tt);
4044 t = *tail = tokenize(tmp);
4045 nasm_free(tmp);
4046 while (t->next) {
4047 tail = &t->next;
4048 t = t->next;
4049 }
4050 t->next = tt; /* Attach the remaining token chain */
4051 did_paste = true;
4052 }
4053 paste_head = tail;
4054 tail = &t->next;
4055 break;
4056 }
4057 /* else fall through */
4058 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004059 /*
4060 * Concatenation of tokens might look nontrivial
4061 * but in real it's pretty simple -- the caller
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004062 * prepares the masks of token types to be concatenated
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004063 * and we simply find matched sequences and slip
4064 * them together
4065 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004066 for (i = 0; i < mnum; i++) {
4067 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
4068 size_t len = 0;
4069 char *tmp, *p;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004070
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004071 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004072 len += strlen(tt->text);
4073 tt = tt->next;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004074 }
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004075
Cyrill Gorcunovfdd0ac52011-06-27 01:22:27 +04004076 nasm_dump_token(tt);
4077
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004078 /*
4079 * Now tt points to the first token after
4080 * the potential paste area...
4081 */
4082 if (tt != t->next) {
4083 /* We have at least two tokens... */
4084 len += strlen(t->text);
4085 p = tmp = nasm_malloc(len+1);
4086 while (t != tt) {
4087 strcpy(p, t->text);
4088 p = strchr(p, '\0');
4089 t = delete_Token(t);
4090 }
4091 t = *tail = tokenize(tmp);
4092 nasm_free(tmp);
4093 while (t->next) {
4094 tail = &t->next;
4095 t = t->next;
4096 }
4097 t->next = tt; /* Attach the remaining token chain */
4098 did_paste = true;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004099 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004100 paste_head = tail;
4101 tail = &t->next;
4102 break;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004103 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004104 }
4105 if (i >= mnum) { /* no match */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004106 tail = &t->next;
4107 if (!tok_type_(t->next, TOK_WHITESPACE))
4108 paste_head = tail;
4109 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004110 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004111 }
4112 }
4113 return did_paste;
4114}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004115
4116/*
4117 * expands to a list of tokens from %{x:y}
4118 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004119static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004120{
4121 Token *t = tline, **tt, *tm, *head;
4122 char *pos;
4123 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004124
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004125 pos = strchr(tline->text, ':');
4126 nasm_assert(pos);
4127
4128 lst = atoi(pos + 1);
4129 fst = atoi(tline->text + 1);
4130
4131 /*
4132 * only macros params are accounted so
4133 * if someone passes %0 -- we reject such
4134 * value(s)
4135 */
4136 if (lst == 0 || fst == 0)
4137 goto err;
4138
4139 /* the values should be sane */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004140 if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) ||
4141 (lst > (int)ei->nparam || lst < (-(int)ei->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004142 goto err;
4143
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004144 fst = fst < 0 ? fst + (int)ei->nparam + 1: fst;
4145 lst = lst < 0 ? lst + (int)ei->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004146
4147 /* counted from zero */
4148 fst--, lst--;
4149
4150 /*
4151 * it will be at least one token
4152 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004153 tm = ei->params[(fst + ei->rotate) % ei->nparam];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004154 t = new_Token(NULL, tm->type, tm->text, 0);
4155 head = t, tt = &t->next;
4156 if (fst < lst) {
4157 for (i = fst + 1; i <= lst; i++) {
4158 t = new_Token(NULL, TOK_OTHER, ",", 0);
4159 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004160 j = (i + ei->rotate) % ei->nparam;
4161 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004162 t = new_Token(NULL, tm->type, tm->text, 0);
4163 *tt = t, tt = &t->next;
4164 }
4165 } else {
4166 for (i = fst - 1; i >= lst; i--) {
4167 t = new_Token(NULL, TOK_OTHER, ",", 0);
4168 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004169 j = (i + ei->rotate) % ei->nparam;
4170 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004171 t = new_Token(NULL, tm->type, tm->text, 0);
4172 *tt = t, tt = &t->next;
4173 }
4174 }
4175
4176 *last = tt;
4177 return head;
4178
4179err:
4180 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
4181 &tline->text[1]);
4182 return tline;
4183}
4184
H. Peter Anvin76690a12002-04-30 20:52:49 +00004185/*
4186 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004187 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004188 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004189 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004190static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004191{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004192 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004193 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004194 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004195
4196 tail = &thead;
4197 thead = NULL;
4198
Cyrill Gorcunov2e046002011-06-26 23:33:56 +04004199 nasm_dump_stream(tline);
4200
H. Peter Anvine2c80182005-01-15 22:15:51 +00004201 while (tline) {
4202 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004203 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4204 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4205 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004206 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004207 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004208 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004209 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004210 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004211 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004212
H. Peter Anvine2c80182005-01-15 22:15:51 +00004213 t = tline;
4214 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004215
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004216 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
4217 if (ei->type == EXP_MMACRO) {
4218 break;
4219 }
4220 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004221 if (ei == NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004222 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004223 } else {
4224 pos = strchr(t->text, ':');
4225 if (!pos) {
4226 switch (t->text[1]) {
4227 /*
4228 * We have to make a substitution of one of the
4229 * forms %1, %-1, %+1, %%foo, %0.
4230 */
4231 case '0':
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004232 if ((strlen(t->text) > 2) && (t->text[2] == '0')) {
4233 type = TOK_ID;
4234 text = nasm_strdup(ei->label_text);
4235 } else {
4236 type = TOK_NUMBER;
4237 snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam);
4238 text = nasm_strdup(tmpbuf);
4239 }
4240 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004241 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004242 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004243 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004244 ei->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004245 text = nasm_strcat(tmpbuf, t->text + 2);
4246 break;
4247 case '-':
4248 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004249 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004250 tt = NULL;
4251 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004252 if (ei->nparam > 1)
4253 n = (n + ei->rotate) % ei->nparam;
4254 tt = ei->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004255 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004256 cc = find_cc(tt);
4257 if (cc == -1) {
4258 error(ERR_NONFATAL,
4259 "macro parameter %d is not a condition code",
4260 n + 1);
4261 text = NULL;
4262 } else {
4263 type = TOK_ID;
4264 if (inverse_ccs[cc] == -1) {
4265 error(ERR_NONFATAL,
4266 "condition code `%s' is not invertible",
4267 conditions[cc]);
4268 text = NULL;
4269 } else
4270 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4271 }
4272 break;
4273 case '+':
4274 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004275 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004276 tt = NULL;
4277 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004278 if (ei->nparam > 1)
4279 n = (n + ei->rotate) % ei->nparam;
4280 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004281 }
4282 cc = find_cc(tt);
4283 if (cc == -1) {
4284 error(ERR_NONFATAL,
4285 "macro parameter %d is not a condition code",
4286 n + 1);
4287 text = NULL;
4288 } else {
4289 type = TOK_ID;
4290 text = nasm_strdup(conditions[cc]);
4291 }
4292 break;
4293 default:
4294 n = atoi(t->text + 1) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004295 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004296 tt = NULL;
4297 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004298 if (ei->nparam > 1)
4299 n = (n + ei->rotate) % ei->nparam;
4300 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004301 }
4302 if (tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004303 for (i = 0; i < ei->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004304 *tail = new_Token(NULL, tt->type, tt->text, 0);
4305 tail = &(*tail)->next;
4306 tt = tt->next;
4307 }
4308 }
4309 text = NULL; /* we've done it here */
4310 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004311 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004312 } else {
4313 /*
4314 * seems we have a parameters range here
4315 */
4316 Token *head, **last;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004317 head = expand_mmac_params_range(ei, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004318 if (head != t) {
4319 *tail = head;
4320 *last = tline;
4321 tline = head;
4322 text = NULL;
4323 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004324 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004325 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004326 if (!text) {
4327 delete_Token(t);
4328 } else {
4329 *tail = t;
4330 tail = &t->next;
4331 t->type = type;
4332 nasm_free(t->text);
4333 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004334 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004335 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004336 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004337 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004338 } else if (tline->type == TOK_INDIRECT) {
4339 t = tline;
4340 tline = tline->next;
4341 tt = tokenize(t->text);
4342 tt = expand_mmac_params(tt);
4343 tt = expand_smacro(tt);
4344 *tail = tt;
4345 while (tt) {
4346 tt->a.mac = NULL; /* Necessary? */
4347 tail = &tt->next;
4348 tt = tt->next;
4349 }
4350 delete_Token(t);
4351 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004352 } else {
4353 t = *tail = tline;
4354 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004355 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004356 tail = &t->next;
4357 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004358 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004359 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004360
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004361 if (changed) {
4362 const struct tokseq_match t[] = {
4363 {
4364 PP_CONCAT_MASK(TOK_ID) |
4365 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4366 PP_CONCAT_MASK(TOK_ID) |
4367 PP_CONCAT_MASK(TOK_NUMBER) |
4368 PP_CONCAT_MASK(TOK_FLOAT) |
4369 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4370 },
4371 {
4372 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4373 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4374 }
4375 };
4376 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4377 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004378
Cyrill Gorcunov2e046002011-06-26 23:33:56 +04004379 nasm_dump_token(thead);
4380
H. Peter Anvin76690a12002-04-30 20:52:49 +00004381 return thead;
4382}
4383
4384/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004385 * Expand all single-line macro calls made in the given line.
4386 * Return the expanded version of the line. The original is deemed
4387 * to be destroyed in the process. (In reality we'll just move
4388 * Tokens from input to output a lot of the time, rather than
4389 * actually bothering to destroy and replicate.)
4390 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004391
H. Peter Anvine2c80182005-01-15 22:15:51 +00004392static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004393{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004394 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004395 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004396 Token **params;
4397 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004398 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004399 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004400 Token *org_tline = tline;
4401 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004402 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004403 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004404 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004405
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004406 /*
4407 * Trick: we should avoid changing the start token pointer since it can
4408 * be contained in "next" field of other token. Because of this
4409 * we allocate a copy of first token and work with it; at the end of
4410 * routine we copy it back
4411 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004412 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004413 tline = new_Token(org_tline->next, org_tline->type,
4414 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004415 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004416 nasm_free(org_tline->text);
4417 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004418 }
4419
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004420 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004421
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004422again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004423 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004424 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004425
H. Peter Anvine2c80182005-01-15 22:15:51 +00004426 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004427 if (!--deadman) {
4428 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004429 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004430 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004431
H. Peter Anvine2c80182005-01-15 22:15:51 +00004432 if ((mname = tline->text)) {
4433 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004434 if (tline->type == TOK_ID) {
4435 head = (SMacro *)hash_findix(&smacros, mname);
4436 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04004437 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004438 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4439 } else
4440 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004441
H. Peter Anvine2c80182005-01-15 22:15:51 +00004442 /*
4443 * We've hit an identifier. As in is_mmacro below, we first
4444 * check whether the identifier is a single-line macro at
4445 * all, then think about checking for parameters if
4446 * necessary.
4447 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004448 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004449 if (!mstrcmp(m->name, mname, m->casesense))
4450 break;
4451 if (m) {
4452 mstart = tline;
4453 params = NULL;
4454 paramsize = NULL;
4455 if (m->nparam == 0) {
4456 /*
4457 * Simple case: the macro is parameterless. Discard the
4458 * one token that the macro call took, and push the
4459 * expansion back on the to-do stack.
4460 */
4461 if (!m->expansion) {
4462 if (!strcmp("__FILE__", m->name)) {
4463 int32_t num = 0;
4464 char *file = NULL;
4465 src_get(&num, &file);
4466 tline->text = nasm_quote(file, strlen(file));
4467 tline->type = TOK_STRING;
4468 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004469 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004470 }
4471 if (!strcmp("__LINE__", m->name)) {
4472 nasm_free(tline->text);
4473 make_tok_num(tline, src_get_linnum());
4474 continue;
4475 }
4476 if (!strcmp("__BITS__", m->name)) {
4477 nasm_free(tline->text);
4478 make_tok_num(tline, globalbits);
4479 continue;
4480 }
4481 tline = delete_Token(tline);
4482 continue;
4483 }
4484 } else {
4485 /*
4486 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004487 * exists and takes parameters. We must find the
4488 * parameters in the call, count them, find the SMacro
4489 * that corresponds to that form of the macro call, and
4490 * substitute for the parameters when we expand. What a
4491 * pain.
4492 */
4493 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004494 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004495 do {
4496 t = tline->next;
4497 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004498 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004499 t->text = NULL;
4500 t = tline->next = delete_Token(t);
4501 }
4502 tline = t;
4503 } while (tok_type_(tline, TOK_WHITESPACE));
4504 if (!tok_is_(tline, "(")) {
4505 /*
4506 * This macro wasn't called with parameters: ignore
4507 * the call. (Behaviour borrowed from gnu cpp.)
4508 */
4509 tline = mstart;
4510 m = NULL;
4511 } else {
4512 int paren = 0;
4513 int white = 0;
4514 brackets = 0;
4515 nparam = 0;
4516 sparam = PARAM_DELTA;
4517 params = nasm_malloc(sparam * sizeof(Token *));
4518 params[0] = tline->next;
4519 paramsize = nasm_malloc(sparam * sizeof(int));
4520 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004521 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004522 /*
4523 * For some unusual expansions
4524 * which concatenates function call
4525 */
4526 t = tline->next;
4527 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004528 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004529 t->text = NULL;
4530 t = tline->next = delete_Token(t);
4531 }
4532 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004533
H. Peter Anvine2c80182005-01-15 22:15:51 +00004534 if (!tline) {
4535 error(ERR_NONFATAL,
4536 "macro call expects terminating `)'");
4537 break;
4538 }
4539 if (tline->type == TOK_WHITESPACE
4540 && brackets <= 0) {
4541 if (paramsize[nparam])
4542 white++;
4543 else
4544 params[nparam] = tline->next;
4545 continue; /* parameter loop */
4546 }
4547 if (tline->type == TOK_OTHER
4548 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004549 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004550 if (ch == ',' && !paren && brackets <= 0) {
4551 if (++nparam >= sparam) {
4552 sparam += PARAM_DELTA;
4553 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004554 sparam * sizeof(Token *));
4555 paramsize = nasm_realloc(paramsize,
4556 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004557 }
4558 params[nparam] = tline->next;
4559 paramsize[nparam] = 0;
4560 white = 0;
4561 continue; /* parameter loop */
4562 }
4563 if (ch == '{' &&
4564 (brackets > 0 || (brackets == 0 &&
4565 !paramsize[nparam])))
4566 {
4567 if (!(brackets++)) {
4568 params[nparam] = tline->next;
4569 continue; /* parameter loop */
4570 }
4571 }
4572 if (ch == '}' && brackets > 0)
4573 if (--brackets == 0) {
4574 brackets = -1;
4575 continue; /* parameter loop */
4576 }
4577 if (ch == '(' && !brackets)
4578 paren++;
4579 if (ch == ')' && brackets <= 0)
4580 if (--paren < 0)
4581 break;
4582 }
4583 if (brackets < 0) {
4584 brackets = 0;
4585 error(ERR_NONFATAL, "braces do not "
4586 "enclose all of macro parameter");
4587 }
4588 paramsize[nparam] += white + 1;
4589 white = 0;
4590 } /* parameter loop */
4591 nparam++;
4592 while (m && (m->nparam != nparam ||
4593 mstrcmp(m->name, mname,
4594 m->casesense)))
4595 m = m->next;
4596 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004597 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004598 "macro `%s' exists, "
4599 "but not taking %d parameters",
4600 mstart->text, nparam);
4601 }
4602 }
4603 if (m && m->in_progress)
4604 m = NULL;
4605 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004606 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004607 * Design question: should we handle !tline, which
4608 * indicates missing ')' here, or expand those
4609 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004610 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004611 */
4612 nasm_free(params);
4613 nasm_free(paramsize);
4614 tline = mstart;
4615 } else {
4616 /*
4617 * Expand the macro: we are placed on the last token of the
4618 * call, so that we can easily split the call from the
4619 * following tokens. We also start by pushing an SMAC_END
4620 * token for the cycle removal.
4621 */
4622 t = tline;
4623 if (t) {
4624 tline = t->next;
4625 t->next = NULL;
4626 }
4627 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004628 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004629 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004630 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004631 list_for_each(t, m->expansion) {
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04004632 if (is_smacro_param(t)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004633 Token *pcopy = tline, **ptail = &pcopy;
Cyrill Gorcunov0ad6a7b2011-06-30 01:36:45 +04004634 Token *ttt;
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04004635 int i, idx;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004636
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04004637 idx = smacro_get_param_idx(t);
4638 ttt = params[idx];
Cyrill Gorcunov0ad6a7b2011-06-30 01:36:45 +04004639
4640 /*
4641 * We need smacro paramters appended.
4642 */
4643 for (i = paramsize[idx]; i > 0; i--) {
4644 *ptail = new_Token(tline, ttt->type, ttt->text, 0);
4645 ptail = &(*ptail)->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004646 ttt = ttt->next;
4647 }
Cyrill Gorcunov0ad6a7b2011-06-30 01:36:45 +04004648
H. Peter Anvine2c80182005-01-15 22:15:51 +00004649 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004650 } else if (t->type == TOK_PREPROC_Q) {
4651 tt = new_Token(tline, TOK_ID, mname, 0);
4652 tline = tt;
4653 } else if (t->type == TOK_PREPROC_QQ) {
4654 tt = new_Token(tline, TOK_ID, m->name, 0);
4655 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004656 } else {
4657 tt = new_Token(tline, t->type, t->text, 0);
4658 tline = tt;
4659 }
4660 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004661
H. Peter Anvine2c80182005-01-15 22:15:51 +00004662 /*
4663 * Having done that, get rid of the macro call, and clean
4664 * up the parameters.
4665 */
4666 nasm_free(params);
4667 nasm_free(paramsize);
4668 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004669 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004670 continue; /* main token loop */
4671 }
4672 }
4673 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004674
H. Peter Anvine2c80182005-01-15 22:15:51 +00004675 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004676 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004677 tline = delete_Token(tline);
4678 } else {
4679 t = *tail = tline;
4680 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004681 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004682 t->next = NULL;
4683 tail = &t->next;
4684 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004685 }
4686
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004687 /*
4688 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004689 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004690 * TOK_IDs should be concatenated.
4691 * Also we look for %+ tokens and concatenate the tokens before and after
4692 * them (without white spaces in between).
4693 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004694 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004695 const struct tokseq_match t[] = {
4696 {
4697 PP_CONCAT_MASK(TOK_ID) |
4698 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4699 PP_CONCAT_MASK(TOK_ID) |
4700 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4701 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4702 }
4703 };
4704 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004705 /*
4706 * If we concatenated something, *and* we had previously expanded
4707 * an actual macro, scan the lines again for macros...
4708 */
4709 tline = thead;
4710 expanded = false;
4711 goto again;
4712 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004713 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004714
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004715err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004716 if (org_tline) {
4717 if (thead) {
4718 *org_tline = *thead;
4719 /* since we just gave text to org_line, don't free it */
4720 thead->text = NULL;
4721 delete_Token(thead);
4722 } else {
4723 /* the expression expanded to empty line;
4724 we can't return NULL for some reasons
4725 we just set the line to a single WHITESPACE token. */
4726 memset(org_tline, 0, sizeof(*org_tline));
4727 org_tline->text = NULL;
4728 org_tline->type = TOK_WHITESPACE;
4729 }
4730 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004731 }
4732
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004733 return thead;
4734}
4735
4736/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004737 * Similar to expand_smacro but used exclusively with macro identifiers
4738 * right before they are fetched in. The reason is that there can be
4739 * identifiers consisting of several subparts. We consider that if there
4740 * are more than one element forming the name, user wants a expansion,
4741 * otherwise it will be left as-is. Example:
4742 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004743 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004744 *
4745 * the identifier %$abc will be left as-is so that the handler for %define
4746 * will suck it and define the corresponding value. Other case:
4747 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004748 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004749 *
4750 * In this case user wants name to be expanded *before* %define starts
4751 * working, so we'll expand %$abc into something (if it has a value;
4752 * otherwise it will be left as-is) then concatenate all successive
4753 * PP_IDs into one.
4754 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004755static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004756{
4757 Token *cur, *oldnext = NULL;
4758
H. Peter Anvin734b1882002-04-30 21:01:08 +00004759 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004760 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004761
4762 cur = tline;
4763 while (cur->next &&
Cyrill Gorcunov5b6c96b2011-06-30 00:22:53 +04004764 (cur->next->type == TOK_ID ||
4765 cur->next->type == TOK_PREPROC_ID ||
4766 cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004767 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004768
4769 /* If identifier consists of just one token, don't expand */
4770 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004771 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004772
H. Peter Anvine2c80182005-01-15 22:15:51 +00004773 if (cur) {
4774 oldnext = cur->next; /* Detach the tail past identifier */
4775 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004776 }
4777
H. Peter Anvin734b1882002-04-30 21:01:08 +00004778 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004779
H. Peter Anvine2c80182005-01-15 22:15:51 +00004780 if (cur) {
4781 /* expand_smacro possibly changhed tline; re-scan for EOL */
4782 cur = tline;
4783 while (cur && cur->next)
4784 cur = cur->next;
4785 if (cur)
4786 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004787 }
4788
4789 return tline;
4790}
4791
4792/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004793 * Determine whether the given line constitutes a multi-line macro
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004794 * call, and return the ExpDef structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004795 * to check for an initial label - that's taken care of in
4796 * expand_mmacro - but must check numbers of parameters. Guaranteed
4797 * to be called with tline->type == TOK_ID, so the putative macro
4798 * name is easy to find.
4799 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004800static ExpDef *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004801{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004802 ExpDef *head, *ed;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004803 Token **params;
4804 int nparam;
4805
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004806 head = (ExpDef *) hash_findix(&expdefs, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004807
4808 /*
4809 * Efficiency: first we see if any macro exists with the given
4810 * name. If not, we can return NULL immediately. _Then_ we
4811 * count the parameters, and then we look further along the
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004812 * list if necessary to find the proper ExpDef.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004813 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004814 list_for_each(ed, head)
4815 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004816 break;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004817 if (!ed)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004818 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004819
4820 /*
4821 * OK, we have a potential macro. Count and demarcate the
4822 * parameters.
4823 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004824 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004825
4826 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004827 * So we know how many parameters we've got. Find the ExpDef
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004828 * structure that handles this number.
4829 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004830 while (ed) {
4831 if (ed->nparam_min <= nparam
4832 && (ed->plus || nparam <= ed->nparam_max)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004833 /*
4834 * It's right, and we can use it. Add its default
4835 * parameters to the end of our list if necessary.
4836 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004837 if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004838 params =
4839 nasm_realloc(params,
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004840 ((ed->nparam_min + ed->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004841 1) * sizeof(*params)));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004842 while (nparam < ed->nparam_min + ed->ndefs) {
4843 params[nparam] = ed->defaults[nparam - ed->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004844 nparam++;
4845 }
4846 }
4847 /*
4848 * If we've gone over the maximum parameter count (and
4849 * we're in Plus mode), ignore parameters beyond
4850 * nparam_max.
4851 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004852 if (ed->plus && nparam > ed->nparam_max)
4853 nparam = ed->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004854 /*
4855 * Then terminate the parameter list, and leave.
4856 */
4857 if (!params) { /* need this special case */
4858 params = nasm_malloc(sizeof(*params));
4859 nparam = 0;
4860 }
4861 params[nparam] = NULL;
4862 *params_array = params;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004863 return ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004864 }
4865 /*
4866 * This one wasn't right: look for the next one with the
4867 * same name.
4868 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004869 list_for_each(ed, ed->next)
4870 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004871 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004872 }
4873
4874 /*
4875 * After all that, we didn't find one with the right number of
4876 * parameters. Issue a warning, and fail to expand the macro.
4877 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004878 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004879 "macro `%s' exists, but not taking %d parameters",
4880 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004881 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004882 return NULL;
4883}
4884
4885/*
4886 * Expand the multi-line macro call made by the given line, if
4887 * there is one to be expanded. If there is, push the expansion on
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004888 * istk->expansion and return true. Otherwise return false.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004889 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004890static bool expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004891{
H. Peter Anvineba20a72002-04-30 20:53:55 +00004892 Token *label = NULL;
4893 int dont_prepend = 0;
Cyrill Gorcunovfb27fc22011-06-25 12:08:30 +04004894 Token **params, *t;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004895 Line *l = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004896 ExpDef *ed;
4897 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004898 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004899 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004900
4901 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004902 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004903 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004904 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004905 return false;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004906 ed = is_mmacro(t, &params);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004907 if (ed != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004908 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004909 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004910 Token *last;
4911 /*
4912 * We have an id which isn't a macro call. We'll assume
4913 * it might be a label; we'll also check to see if a
4914 * colon follows it. Then, if there's another id after
4915 * that lot, we'll check it again for macro-hood.
4916 */
4917 label = last = t;
4918 t = t->next;
4919 if (tok_type_(t, TOK_WHITESPACE))
4920 last = t, t = t->next;
4921 if (tok_is_(t, ":")) {
4922 dont_prepend = 1;
4923 last = t, t = t->next;
4924 if (tok_type_(t, TOK_WHITESPACE))
4925 last = t, t = t->next;
4926 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004927 if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, &params)))
4928 return false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004929 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004930 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004931 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004932 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004933
4934 /*
4935 * Fix up the parameters: this involves stripping leading and
4936 * trailing whitespace, then stripping braces if they are
4937 * present.
4938 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004939 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004940 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004941
H. Peter Anvine2c80182005-01-15 22:15:51 +00004942 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004943 int brace = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004944 int comma = (!ed->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004945
H. Peter Anvine2c80182005-01-15 22:15:51 +00004946 t = params[i];
4947 skip_white_(t);
4948 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004949 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004950 params[i] = t;
4951 paramlen[i] = 0;
4952 while (t) {
4953 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4954 break; /* ... because we have hit a comma */
4955 if (comma && t->type == TOK_WHITESPACE
4956 && tok_is_(t->next, ","))
4957 break; /* ... or a space then a comma */
4958 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4959 break; /* ... or a brace */
4960 t = t->next;
4961 paramlen[i]++;
4962 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004963 }
4964
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004965 if (ed->cur_depth >= ed->max_depth) {
4966 if (ed->max_depth > 1) {
4967 error(ERR_WARNING,
4968 "reached maximum macro recursion depth of %i for %s",
4969 ed->max_depth,ed->name);
4970 }
4971 return false;
4972 } else {
4973 ed->cur_depth ++;
4974 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004975
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004976 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004977 * OK, we have found a ExpDef structure representing a
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004978 * previously defined mmacro. Create an expansion invocation
4979 * and point it back to the expansion definition. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004980 * parameter tokens and macro-local tokens doesn't get done
4981 * until the single-line macro substitution process; this is
4982 * because delaying them allows us to change the semantics
4983 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004984 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004985 ei = new_ExpInv(EXP_MMACRO, ed);
4986 ei->name = nasm_strdup(mname);
4987 //ei->label = label;
4988 //ei->label_text = detoken(label, false);
4989 ei->current = ed->line;
4990 ei->emitting = true;
4991 //ei->iline = tline;
4992 ei->params = params;
4993 ei->nparam = nparam;
4994 ei->rotate = 0;
4995 ei->paramlen = paramlen;
4996 ei->lineno = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004997
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004998 ei->prev = istk->expansion;
4999 istk->expansion = ei;
5000
5001 /*
5002 * Special case: detect %00 on first invocation; if found,
5003 * avoid emitting any labels that precede the mmacro call.
5004 * ed->prepend is set to -1 when %00 is detected, else 1.
5005 */
5006 if (ed->prepend == 0) {
5007 for (l = ed->line; l != NULL; l = l->next) {
5008 for (t = l->first; t != NULL; t = t->next) {
5009 if ((t->type == TOK_PREPROC_ID) &&
5010 (strlen(t->text) == 3) &&
5011 (t->text[1] == '0') && (t->text[2] == '0')) {
5012 dont_prepend = -1;
5013 break;
5014 }
5015 }
5016 if (dont_prepend < 0) {
5017 break;
5018 }
5019 }
5020 ed->prepend = ((dont_prepend < 0) ? -1 : 1);
5021 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005022
5023 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00005024 * If we had a label, push it on as the first line of
5025 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005026 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005027 if (label != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005028 if (ed->prepend < 0) {
5029 ei->label_text = detoken(label, false);
5030 } else {
5031 if (dont_prepend == 0) {
5032 t = label;
5033 while (t->next != NULL) {
5034 t = t->next;
5035 }
5036 t->next = new_Token(NULL, TOK_OTHER, ":", 0);
5037 }
5038 l = new_Line();
5039 l->first = copy_Token(label);
5040 l->next = ei->current;
5041 ei->current = l;
5042 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005043 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005044
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005045 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005046
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005047 istk->mmac_depth++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005048 return true;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005049}
5050
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005051/* The function that actually does the error reporting */
5052static void verror(int severity, const char *fmt, va_list arg)
5053{
5054 char buff[1024];
5055
5056 vsnprintf(buff, sizeof(buff), fmt, arg);
5057
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005058 if (istk && istk->mmac_depth > 0) {
5059 ExpInv *ei = istk->expansion;
5060 int lineno = ei->lineno;
5061 while (ei) {
5062 if (ei->type == EXP_MMACRO)
5063 break;
5064 lineno += ei->relno;
5065 ei = ei->prev;
5066 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005067 nasm_error(severity, "(%s:%d) %s", ei->def->name,
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005068 lineno, buff);
5069 } else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005070 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005071}
5072
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005073/*
5074 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07005075 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005076 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005077static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005078{
5079 va_list arg;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005080 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005081 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00005082 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005083}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005084
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005085/*
5086 * Because %else etc are evaluated in the state context
5087 * of the previous branch, errors might get lost with error():
5088 * %if 0 ... %else trailing garbage ... %endif
5089 * So %else etc should report errors with this function.
5090 */
5091static void error_precond(int severity, const char *fmt, ...)
5092{
5093 va_list arg;
5094
5095 /* Only ignore the error if it's really in a dead branch */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005096 if ((istk != NULL) &&
5097 (istk->expansion != NULL) &&
5098 (istk->expansion->type == EXP_IF) &&
5099 (istk->expansion->def->state == COND_NEVER))
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005100 return;
5101
5102 va_start(arg, fmt);
5103 verror(severity, fmt, arg);
5104 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005105}
5106
H. Peter Anvin734b1882002-04-30 21:01:08 +00005107static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005108pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005109{
H. Peter Anvin7383b402008-09-24 10:20:40 -07005110 Token *t;
5111
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005112 cstk = NULL;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005113 istk = nasm_zalloc(sizeof(Include));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005114 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00005115 src_set_fname(nasm_strdup(file));
5116 src_set_linnum(0);
5117 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005118 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07005119 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00005120 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005121 defining = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005122 finals = NULL;
5123 in_final = false;
Charles Crayned4200be2008-07-12 16:42:33 -07005124 nested_mac_count = 0;
5125 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005126 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005127 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005128 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005129 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005130 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005131 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005132 }
H. Peter Anvind2456592008-06-19 15:04:18 -07005133 any_extrastdmac = extrastdmac && *extrastdmac;
5134 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005135 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005136
5137 /*
5138 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5139 * The caller, however, will also pass in 3 for preprocess-only so
5140 * we can set __PASS__ accordingly.
5141 */
5142 pass = apass > 2 ? 2 : apass;
5143
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005144 dephead = deptail = deplist;
5145 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005146 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
5147 sl->next = NULL;
5148 strcpy(sl->str, file);
5149 *deptail = sl;
5150 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005151 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07005152
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005153 /*
5154 * Define the __PASS__ macro. This is defined here unlike
5155 * all the other builtins, because it is special -- it varies between
5156 * passes.
5157 */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005158 t = nasm_zalloc(sizeof(*t));
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005159 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07005160 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005161}
5162
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005163static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005164{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005165 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005166 Token *tline;
Keith Kanios9858cec2010-11-08 00:58:02 -06005167 ExpDef *ed;
5168 ExpInv *ei;
5169 Line *l;
5170 int j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005171
H. Peter Anvine2c80182005-01-15 22:15:51 +00005172 while (1) {
5173 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005174 * Fetch a tokenized line, either from the expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005175 * buffer or from the input file.
5176 */
5177 tline = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005178
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005179 while (1) { /* until we get a line we can use */
5180 /*
5181 * Fetch a tokenized line from the expansion buffer
5182 */
5183 if (istk->expansion != NULL) {
5184 ei = istk->expansion;
5185 if (ei->current != NULL) {
5186 if (ei->emitting == false) {
5187 ei->current = NULL;
5188 continue;
5189 }
5190 l = ei->current;
5191 ei->current = l->next;
5192 ei->lineno++;
5193 tline = copy_Token(l->first);
5194 if (((ei->type == EXP_REP) ||
5195 (ei->type == EXP_MMACRO) ||
5196 (ei->type == EXP_WHILE))
5197 && (ei->def->nolist == false)) {
5198 char *p = detoken(tline, false);
5199 list->line(LIST_MACRO, p);
5200 nasm_free(p);
5201 }
5202 if (ei->linnum > -1) {
5203 src_set_linnum(src_get_linnum() + 1);
5204 }
5205 break;
5206 } else if ((ei->type == EXP_REP) &&
5207 (ei->def->cur_depth < ei->def->max_depth)) {
5208 ei->def->cur_depth ++;
5209 ei->current = ei->def->line;
5210 ei->lineno = 0;
5211 continue;
5212 } else if ((ei->type == EXP_WHILE) &&
5213 (ei->def->cur_depth < ei->def->max_depth)) {
5214 ei->current = ei->def->line;
5215 ei->lineno = 0;
5216 tline = copy_Token(ei->current->first);
5217 j = if_condition(tline, PP_WHILE);
5218 tline = NULL;
5219 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
5220 if (j == COND_IF_TRUE) {
5221 ei->current = ei->current->next;
5222 ei->def->cur_depth ++;
5223 } else {
5224 ei->emitting = false;
5225 ei->current = NULL;
5226 ei->def->cur_depth = ei->def->max_depth;
5227 }
5228 continue;
5229 } else {
5230 istk->expansion = ei->prev;
5231 ed = ei->def;
5232 if (ed != NULL) {
5233 if ((ei->emitting == true) &&
5234 (ed->max_depth == DEADMAN_LIMIT) &&
5235 (ed->cur_depth == DEADMAN_LIMIT)
5236 ) {
5237 error(ERR_FATAL, "runaway expansion detected, aborting");
5238 }
5239 if (ed->cur_depth > 0) {
5240 ed->cur_depth --;
Keith Kanios94124652010-12-18 11:47:28 -06005241 } else if (ed->type != EXP_MMACRO) {
Keith Kanios6a7c3e92010-12-18 11:49:53 -06005242 expansions = ed->prev;
5243 free_expdef(ed);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005244 }
5245 if ((ei->type == EXP_REP) ||
5246 (ei->type == EXP_MMACRO) ||
5247 (ei->type == EXP_WHILE)) {
5248 list->downlevel(LIST_MACRO);
5249 if (ei->type == EXP_MMACRO) {
5250 istk->mmac_depth--;
5251 }
5252 }
5253 }
5254 if (ei->linnum > -1) {
5255 src_set_linnum(ei->linnum);
5256 }
5257 free_expinv(ei);
5258 continue;
5259 }
5260 }
5261
5262 /*
5263 * Read in line from input and tokenize
5264 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005265 line = read_line();
5266 if (line) { /* from the current input file */
5267 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005268 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005269 nasm_free(line);
5270 break;
5271 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005272
H. Peter Anvine2c80182005-01-15 22:15:51 +00005273 /*
5274 * The current file has ended; work down the istk
5275 */
5276 {
5277 Include *i = istk;
5278 fclose(i->fp);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005279 if (i->expansion != NULL) {
5280 error(ERR_FATAL,
5281 "end of file while still in an expansion");
5282 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005283 /* only set line and file name if there's a next node */
5284 if (i->next) {
5285 src_set_linnum(i->lineno);
Cyrill Gorcunovd34a1082011-03-07 11:23:08 +03005286 nasm_free(src_set_fname(nasm_strdup(i->fname)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005287 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005288 if ((i->next == NULL) && (finals != NULL)) {
5289 in_final = true;
5290 ei = new_ExpInv(EXP_FINAL, NULL);
5291 ei->emitting = true;
5292 ei->current = finals;
5293 istk->expansion = ei;
5294 finals = NULL;
5295 continue;
5296 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005297 istk = i->next;
5298 list->downlevel(LIST_INCLUDE);
5299 nasm_free(i);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005300 if (istk == NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005301 if (finals != NULL) {
5302 in_final = true;
5303 } else {
5304 return NULL;
5305 }
5306 }
5307 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005308 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005309 }
5310
5311 if (defining == NULL) {
5312 tline = expand_mmac_params(tline);
5313 }
5314
H. Peter Anvine2c80182005-01-15 22:15:51 +00005315 /*
5316 * Check the line to see if it's a preprocessor directive.
5317 */
5318 if (do_directive(tline) == DIRECTIVE_FOUND) {
5319 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005320 } else if (defining != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005321 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005322 * We're defining an expansion. We emit nothing at all,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005323 * and just shove the tokenized line on to the definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005324 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005325 if (defining->ignoring == false) {
5326 Line *l = new_Line();
5327 l->first = tline;
5328 if (defining->line == NULL) {
5329 defining->line = l;
5330 defining->last = l;
5331 } else {
5332 defining->last->next = l;
5333 defining->last = l;
5334 }
5335 } else {
Keith Kanios104803d2010-12-18 11:05:46 -06005336 free_tlist(tline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005337 }
5338 defining->linecount++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005339 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005340 } else if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005341 (istk->expansion->emitting != true)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005342 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005343 * We're in a non-emitting branch of an expansion.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005344 * Emit nothing at all, not even a blank line: when we
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005345 * emerge from the expansion we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005346 * directive so we keep our place correctly.
5347 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005348 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005349 continue;
5350 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005351 tline = expand_smacro(tline);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005352 if (expand_mmacro(tline) != true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005353 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005354 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005355 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005356 line = detoken(tline, true);
5357 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005358 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005359 } else {
5360 continue;
5361 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005362 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005363 }
5364 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005365}
5366
H. Peter Anvine2c80182005-01-15 22:15:51 +00005367static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005368{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005369 if (defining != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005370 error(ERR_NONFATAL, "end of file while still defining an expansion");
5371 while (defining != NULL) {
5372 ExpDef *ed = defining;
5373 defining = ed->prev;
5374 free_expdef(ed);
5375 }
5376 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005377 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005378 while (cstk != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005379 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005380 free_macros();
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005381 while (istk != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005382 Include *i = istk;
5383 istk = istk->next;
5384 fclose(i->fp);
5385 nasm_free(i->fname);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005386 while (i->expansion != NULL) {
5387 ExpInv *ei = i->expansion;
5388 i->expansion = ei->prev;
5389 free_expinv(ei);
5390 }
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005391 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005392 }
5393 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005394 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005395 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005396 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005397 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005398 free_llist(predef);
5399 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005400 while ((i = ipath)) {
5401 ipath = i->next;
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04005402 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005403 nasm_free(i);
5404 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005405 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005406}
5407
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005408void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005409{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005410 IncPath *i = nasm_zalloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005411
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005412 if (path)
5413 i->path = nasm_strdup(path);
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005414
H. Peter Anvin89cee572009-07-15 09:16:54 -04005415 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005416 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005417 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005418 j = j->next;
5419 j->next = i;
5420 } else {
5421 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005422 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005423}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005424
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005425void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005426{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005427 Token *inc, *space, *name;
5428 Line *l;
5429
H. Peter Anvin734b1882002-04-30 21:01:08 +00005430 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5431 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5432 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005433
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005434 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005435 l->next = predef;
5436 l->first = inc;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005437 predef = l;
5438}
5439
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005440void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005441{
5442 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005443 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005444 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005445
5446 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005447 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5448 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005449 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005450 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005451 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005452 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005453 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005454
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005455 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005456 l->next = predef;
5457 l->first = def;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005458 predef = l;
5459}
5460
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005461void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005462{
5463 Token *def, *space;
5464 Line *l;
5465
H. Peter Anvin734b1882002-04-30 21:01:08 +00005466 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5467 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005468 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005469
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005470 l = new_Line();
H. Peter Anvin620515a2002-04-30 20:57:38 +00005471 l->next = predef;
5472 l->first = def;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005473 predef = l;
5474}
5475
Keith Kaniosb7a89542007-04-12 02:40:54 +00005476/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005477 * This function is used to assist with "runtime" preprocessor
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005478 * directives, e.g. pp_runtime("%define __BITS__ 64");
Keith Kaniosb7a89542007-04-12 02:40:54 +00005479 *
5480 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5481 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5482 */
5483
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005484void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005485{
5486 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005487
Keith Kaniosb7a89542007-04-12 02:40:54 +00005488 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005489 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005490 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005491
Keith Kaniosb7a89542007-04-12 02:40:54 +00005492}
5493
H. Peter Anvina70547f2008-07-19 21:44:26 -07005494void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005495{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005496 extrastdmac = macros;
5497}
5498
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005499static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005500{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005501 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005502 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005503 tok->text = nasm_strdup(numbuf);
5504 tok->type = TOK_NUMBER;
5505}
5506
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +04005507struct preproc_ops nasmpp = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005508 pp_reset,
5509 pp_getline,
5510 pp_cleanup
5511};