blob: 00850382b5b807104ac68ab9a6f28cb214da13b6 [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};
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000327
H. Peter Anvin70653092007-10-19 14:42:29 -0700328/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000329 * These defines are used as the possible return values for do_directive
330 */
331#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300332#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000333
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000334/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500335 * This define sets the upper limit for smacro and expansions
Keith Kanios852f1ee2009-07-12 00:19:55 -0500336 */
337#define DEADMAN_LIMIT (1 << 20)
338
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400339/* max reps */
340#define REP_LIMIT ((INT64_C(1) << 62))
341
Keith Kanios852f1ee2009-07-12 00:19:55 -0500342/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000343 * Condition codes. Note that we use c_ prefix not C_ because C_ is
344 * used in nasm.h for the "real" condition codes. At _this_ level,
345 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
346 * ones, so we need a different enum...
347 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700348static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000349 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
350 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000351 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000352};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700353enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000354 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
355 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 -0700356 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
357 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000358};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700359static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000360 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
361 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 +0000362 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000363};
364
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000365/* For TASM compatibility we need to be able to recognise TASM compatible
366 * conditional compilation directives. Using the NASM pre-processor does
367 * not work, so we look for them specifically from the following list and
368 * then jam in the equivalent NASM directive into the input stream.
369 */
370
H. Peter Anvine2c80182005-01-15 22:15:51 +0000371enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000372 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
373 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
374};
375
H. Peter Anvin476d2862007-10-02 22:04:15 -0700376static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000377 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
378 "ifndef", "include", "local"
379};
380
381static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000382static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000383static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800384static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000385
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000386static Context *cstk;
387static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000388static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000389
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300390static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700391static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000392
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300393static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000394
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000395static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700396static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000397
398static ListGen *list;
399
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000400/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500401 * The current set of expansion definitions we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000402 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500403static struct hash_table expdefs;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000404
405/*
406 * The current set of single-line macros we have defined.
407 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700408static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000409
410/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500411 * Linked List of all active expansion definitions
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000412 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500413struct ExpDef *expansions = NULL;
414
415/*
416 * The expansion we are currently defining
417 */
418static ExpDef *defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000419
Charles Crayned4200be2008-07-12 16:42:33 -0700420static uint64_t nested_mac_count;
421static uint64_t nested_rep_count;
422
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000423/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500424 * Linked-list of lines to preprocess, prior to cleanup
425 */
426static Line *finals = NULL;
427static bool in_final = false;
428
429/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000430 * The number of macro parameters to allocate space for at a time.
431 */
432#define PARAM_DELTA 16
433
434/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700435 * The standard macro set: defined in macros.c in the array nasm_stdmac.
436 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000437 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700438static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000439
440/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000441 * The extra standard macros that come from the object format, if
442 * any.
443 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700444static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700445static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000446
447/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000448 * Tokens are allocated in blocks to improve speed
449 */
450#define TOKEN_BLOCKSIZE 4096
451static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000452struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000453 Blocks *next;
454 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000455};
456
457static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000458
459/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000460 * Forward declarations.
461 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000462static Token *expand_mmac_params(Token * tline);
463static Token *expand_smacro(Token * tline);
464static Token *expand_id(Token * tline);
Cyrill Gorcunov290eac72011-06-28 01:59:05 +0400465static Context *get_ctx(const char *name, const char **namep);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700466static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000467static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200468static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000469static void *new_Block(size_t size);
470static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700471static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300472 const char *text, int txtlen);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500473static Token *copy_Token(Token * tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000474static Token *delete_Token(Token * t);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500475static Line *new_Line(void);
476static ExpDef *new_ExpDef(int exp_type);
477static ExpInv *new_ExpInv(int exp_type, ExpDef *ed);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000478
479/*
480 * Macros for safe checking of token pointers, avoid *(NULL)
481 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300482#define tok_type_(x,t) ((x) && (x)->type == (t))
483#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
484#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
485#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000486
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400487/*
488 * A few helpers for single macros
489 */
490
491/* We might be not smacro parameter at all */
492static bool is_smacro_param(Token *t)
493{
494 return t->type >= TOK_SMAC_PARAM;
495}
496
497/* smacro parameters are counted in a special way */
498static int smacro_get_param_idx(Token *t)
499{
500 return t->type - TOK_SMAC_PARAM;
501}
502
503/* encode smacro parameter index */
504static int smacro_set_param_idx(Token *t, unsigned int index)
505{
506 return t->type = TOK_SMAC_PARAM + index;
507}
508
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300509#ifdef NASM_TRACE
510
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400511#define stringify(x) #x
512
Cyrill Gorcunov9d1141a2011-06-26 23:07:35 +0400513#define nasm_trace(msg, ...) printf("(%s:%d): " msg "\n", __func__, __LINE__, ##__VA_ARGS__)
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400514#define nasm_dump_token(t) nasm_raw_dump_token(t, __FILE__, __LINE__, __func__);
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400515#define nasm_dump_stream(t) nasm_raw_dump_stream(t, __FILE__, __LINE__, __func__);
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400516
517/* FIXME: we really need some compound type here instead of inplace code */
518static const char *nasm_get_tok_type_str(enum pp_token_type type)
519{
520#define SWITCH_TOK_NAME(type) \
521 case (type): \
522 return stringify(type)
523
524 switch (type) {
525 SWITCH_TOK_NAME(TOK_NONE);
526 SWITCH_TOK_NAME(TOK_WHITESPACE);
527 SWITCH_TOK_NAME(TOK_COMMENT);
528 SWITCH_TOK_NAME(TOK_ID);
529 SWITCH_TOK_NAME(TOK_PREPROC_ID);
530 SWITCH_TOK_NAME(TOK_STRING);
531 SWITCH_TOK_NAME(TOK_NUMBER);
532 SWITCH_TOK_NAME(TOK_FLOAT);
533 SWITCH_TOK_NAME(TOK_SMAC_END);
534 SWITCH_TOK_NAME(TOK_OTHER);
535 SWITCH_TOK_NAME(TOK_INTERNAL_STRING);
536 SWITCH_TOK_NAME(TOK_PREPROC_Q);
537 SWITCH_TOK_NAME(TOK_PREPROC_QQ);
538 SWITCH_TOK_NAME(TOK_PASTE);
539 SWITCH_TOK_NAME(TOK_INDIRECT);
540 SWITCH_TOK_NAME(TOK_SMAC_PARAM);
541 SWITCH_TOK_NAME(TOK_MAX);
542 }
543
544 return NULL;
545}
546
547static void nasm_raw_dump_token(Token *token, const char *file, int line, const char *func)
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300548{
549 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
550 if (token) {
551 Token *t;
552 list_for_each(t, token) {
553 if (t->text)
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400554 printf("'%s'(%s) ", t->text,
555 nasm_get_tok_type_str(t->type));
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300556 }
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400557 printf("\n\n");
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300558 }
559}
560
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400561static void nasm_raw_dump_stream(Token *token, const char *file, int line, const char *func)
562{
563 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
564 if (token) {
565 Token *t;
566 list_for_each(t, token)
567 printf("%s", t->text ? t->text : " ");
568 printf("\n\n");
569 }
570}
571
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400572#else
573#define nasm_trace(msg, ...)
574#define nasm_dump_token(t)
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400575#define nasm_dump_stream(t)
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300576#endif
577
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300578/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700579 * nasm_unquote with error if the string contains NUL characters.
580 * If the string contains NUL characters, issue an error and return
581 * the C len, i.e. truncate at the NUL.
582 */
583static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
584{
585 size_t len = nasm_unquote(qstr, NULL);
586 size_t clen = strlen(qstr);
587
588 if (len != clen)
589 error(ERR_NONFATAL, "NUL character in `%s' directive",
590 pp_directives[directive]);
591
592 return clen;
593}
594
595/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700596 * In-place reverse a list of tokens.
597 */
598static Token *reverse_tokens(Token *t)
599{
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400600 Token *prev, *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700601
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400602 list_reverse(t, prev, next);
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700603
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400604 return t;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700605}
606
607/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300608 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000609 * front of them. We do it here because I could not find any other
610 * place to do it for the moment, and it is a hack (ideally it would
611 * be nice to be able to use the NASM pre-processor to do it).
612 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000613static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000614{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000615 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400616 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000617
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400618 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000619
620 /* Binary search for the directive name */
621 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400622 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400623 q = nasm_skip_word(p);
624 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000625 if (len) {
626 oldchar = p[len];
627 p[len] = 0;
628 while (j - i > 1) {
629 k = (j + i) / 2;
630 m = nasm_stricmp(p, tasm_directives[k]);
631 if (m == 0) {
632 /* We have found a directive, so jam a % in front of it
633 * so that NASM will then recognise it as one if it's own.
634 */
635 p[len] = oldchar;
636 len = strlen(p);
637 oldline = line;
638 line = nasm_malloc(len + 2);
639 line[0] = '%';
640 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700641 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300642 * NASM does not recognise IFDIFI, so we convert
643 * it to %if 0. This is not used in NASM
644 * compatible code, but does need to parse for the
645 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000646 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700647 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000648 } else {
649 memcpy(line + 1, p, len + 1);
650 }
651 nasm_free(oldline);
652 return line;
653 } else if (m < 0) {
654 j = k;
655 } else
656 i = k;
657 }
658 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000659 }
660 return line;
661}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000662
H. Peter Anvin76690a12002-04-30 20:52:49 +0000663/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000664 * The pre-preprocessing stage... This function translates line
665 * number indications as they emerge from GNU cpp (`# lineno "file"
666 * flags') into NASM preprocessor line number indications (`%line
667 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000668 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000669static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000670{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000671 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000672 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000673
H. Peter Anvine2c80182005-01-15 22:15:51 +0000674 if (line[0] == '#' && line[1] == ' ') {
675 oldline = line;
676 fname = oldline + 2;
677 lineno = atoi(fname);
678 fname += strspn(fname, "0123456789 ");
679 if (*fname == '"')
680 fname++;
681 fnlen = strcspn(fname, "\"");
682 line = nasm_malloc(20 + fnlen);
683 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
684 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000685 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000686 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000687 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000688 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000689}
690
691/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000692 * Free a linked list of tokens.
693 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000694static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000695{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400696 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000697 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000698}
699
700/*
701 * Free a linked list of lines.
702 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000703static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000704{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400705 Line *l, *tmp;
706 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000707 free_tlist(l->first);
708 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000709 }
710}
711
712/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500713 * Free an ExpDef
H. Peter Anvineba20a72002-04-30 20:53:55 +0000714 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500715static void free_expdef(ExpDef * ed)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000716{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500717 nasm_free(ed->name);
718 free_tlist(ed->dlist);
719 nasm_free(ed->defaults);
720 free_llist(ed->line);
721 nasm_free(ed);
722}
723
724/*
725 * Free an ExpInv
726 */
727static void free_expinv(ExpInv * ei)
728{
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +0400729 nasm_free(ei->name);
730 nasm_free(ei->label_text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300731 nasm_free(ei);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000732}
733
734/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700735 * Free all currently defined macros, and free the hash tables
736 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700737static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700738{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400739 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700740 const char *key;
741 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700742
H. Peter Anvin072771e2008-05-22 13:17:51 -0700743 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300744 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400745 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300746 nasm_free(s->name);
747 free_tlist(s->expansion);
748 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300749 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700750 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700751 hash_free(smt);
752}
753
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500754static void free_expdef_table(struct hash_table *edt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700755{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500756 ExpDef *ed, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700757 const char *key;
758 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700759
760 it = NULL;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500761 while ((ed = hash_iterate(edt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300762 nasm_free((void *)key);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500763 list_for_each_safe(ed ,tmp, ed)
764 free_expdef(ed);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700765 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500766 hash_free(edt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700767}
768
769static void free_macros(void)
770{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700771 free_smacro_table(&smacros);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500772 free_expdef_table(&expdefs);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700773}
774
775/*
776 * Initialize the hash tables
777 */
778static void init_macros(void)
779{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700780 hash_init(&smacros, HASH_LARGE);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500781 hash_init(&expdefs, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700782}
783
784/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000785 * Pop the context stack.
786 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000787static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000788{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000789 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000790
791 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700792 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000793 nasm_free(c->name);
794 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000795}
796
H. Peter Anvin072771e2008-05-22 13:17:51 -0700797/*
798 * Search for a key in the hash index; adding it if necessary
799 * (in which case we initialize the data pointer to NULL.)
800 */
801static void **
802hash_findi_add(struct hash_table *hash, const char *str)
803{
804 struct hash_insert hi;
805 void **r;
806 char *strx;
807
808 r = hash_findi(hash, str, &hi);
809 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300810 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700811
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300812 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700813 return hash_add(&hi, strx, NULL);
814}
815
816/*
817 * Like hash_findi, but returns the data element rather than a pointer
818 * to it. Used only when not adding a new element, hence no third
819 * argument.
820 */
821static void *
822hash_findix(struct hash_table *hash, const char *str)
823{
824 void **p;
825
826 p = hash_findi(hash, str, NULL);
827 return p ? *p : NULL;
828}
829
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400830/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500831 * read line from standard macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400832 * if there no more left -- return NULL
833 */
834static char *line_from_stdmac(void)
835{
836 unsigned char c;
837 const unsigned char *p = stdmacpos;
838 char *line, *q;
839 size_t len = 0;
840
841 if (!stdmacpos)
842 return NULL;
843
844 while ((c = *p++)) {
845 if (c >= 0x80)
846 len += pp_directives_len[c - 0x80] + 1;
847 else
848 len++;
849 }
850
851 line = nasm_malloc(len + 1);
852 q = line;
853 while ((c = *stdmacpos++)) {
854 if (c >= 0x80) {
855 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
856 q += pp_directives_len[c - 0x80];
857 *q++ = ' ';
858 } else {
859 *q++ = c;
860 }
861 }
862 stdmacpos = p;
863 *q = '\0';
864
865 if (!*stdmacpos) {
866 /* This was the last of the standard macro chain... */
867 stdmacpos = NULL;
868 if (any_extrastdmac) {
869 stdmacpos = extrastdmac;
870 any_extrastdmac = false;
871 } else if (do_predef) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300872 ExpInv *ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400873 Line *pd, *l;
874 Token *head, **tail, *t;
875
876 /*
877 * Nasty hack: here we push the contents of
878 * `predef' on to the top-level expansion stack,
879 * since this is the most convenient way to
880 * implement the pre-include and pre-define
881 * features.
882 */
883 list_for_each(pd, predef) {
884 head = NULL;
885 tail = &head;
886 list_for_each(t, pd->first) {
887 *tail = new_Token(NULL, t->type, t->text, 0);
888 tail = &(*tail)->next;
889 }
890
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500891 l = new_Line();
892 l->first = head;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300893 ei = new_ExpInv(EXP_PREDEF, NULL);
894 ei->current = l;
895 ei->emitting = true;
896 ei->prev = istk->expansion;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500897 istk->expansion = ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400898 }
899 do_predef = false;
900 }
901 }
902
903 return line;
904}
905
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000906#define BUF_DELTA 512
907/*
908 * Read a line from the top file in istk, handling multiple CR/LFs
909 * at the end of the line read, and handling spurious ^Zs. Will
910 * return lines from the standard macro set if this has not already
911 * been done.
912 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000913static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000914{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000915 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000916 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000917
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400918 /*
919 * standart macros set (predefined) goes first
920 */
921 p = line_from_stdmac();
922 if (p)
923 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700924
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400925 /*
926 * regular read from a file
927 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000928 bufsize = BUF_DELTA;
929 buffer = nasm_malloc(BUF_DELTA);
930 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000931 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000932 while (1) {
933 q = fgets(p, bufsize - (p - buffer), istk->fp);
934 if (!q)
935 break;
936 p += strlen(p);
937 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300938 /*
939 * Convert backslash-CRLF line continuation sequences into
940 * nothing at all (for DOS and Windows)
941 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000942 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
943 p -= 3;
944 *p = 0;
945 continued_count++;
946 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300947 /*
948 * Also convert backslash-LF line continuation sequences into
949 * nothing at all (for Unix)
950 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000951 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
952 p -= 2;
953 *p = 0;
954 continued_count++;
955 } else {
956 break;
957 }
958 }
959 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000960 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000961 bufsize += BUF_DELTA;
962 buffer = nasm_realloc(buffer, bufsize);
963 p = buffer + offset; /* prevent stale-pointer problems */
964 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000965 }
966
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967 if (!q && p == buffer) {
968 nasm_free(buffer);
969 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000970 }
971
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300972 src_set_linnum(src_get_linnum() + istk->lineinc +
973 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000974
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000975 /*
976 * Play safe: remove CRs as well as LFs, if any of either are
977 * present at the end of the line.
978 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000979 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000980 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000981
982 /*
983 * Handle spurious ^Z, which may be inserted into source files
984 * by some file transfer utilities.
985 */
986 buffer[strcspn(buffer, "\032")] = '\0';
987
H. Peter Anvin734b1882002-04-30 21:01:08 +0000988 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000989
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000990 return buffer;
991}
992
993/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000994 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000995 * don't need to parse the value out of e.g. numeric tokens: we
996 * simply split one string into many.
997 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000998static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000999{
H. Peter Anvinca544db2008-10-19 19:30:11 -07001000 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001001 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001002 Token *list = NULL;
1003 Token *t, **tail = &list;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001004 bool verbose = true;
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +04001005
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +04001006 nasm_trace("Tokenize for '%s'", line);
1007
Keith Kanios6faad4e2010-12-18 14:08:02 -06001008 if ((defining != NULL) && (defining->ignoring == true)) {
1009 verbose = false;
1010 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001011
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012 while (*line) {
1013 p = line;
1014 if (*p == '%') {
1015 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001016 if (*p == '+' && !nasm_isdigit(p[1])) {
1017 p++;
1018 type = TOK_PASTE;
1019 } else if (nasm_isdigit(*p) ||
1020 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001021 do {
1022 p++;
1023 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001024 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001025 type = TOK_PREPROC_ID;
1026 } else if (*p == '{') {
1027 p++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001028 while (*p && *p != '}') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 p[-1] = *p;
1030 p++;
1031 }
1032 p[-1] = '\0';
1033 if (*p)
1034 p++;
1035 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001036 } else if (*p == '[') {
1037 int lvl = 1;
1038 line += 2; /* Skip the leading %[ */
1039 p++;
1040 while (lvl && (c = *p++)) {
1041 switch (c) {
1042 case ']':
1043 lvl--;
1044 break;
1045 case '%':
1046 if (*p == '[')
1047 lvl++;
1048 break;
1049 case '\'':
1050 case '\"':
1051 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +04001052 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001053 break;
1054 default:
1055 break;
1056 }
1057 }
1058 p--;
1059 if (*p)
1060 *p++ = '\0';
Keith Kanios6faad4e2010-12-18 14:08:02 -06001061 if (lvl && verbose)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001062 error(ERR_NONFATAL, "unterminated %[ construct");
1063 type = TOK_INDIRECT;
1064 } else if (*p == '?') {
1065 type = TOK_PREPROC_Q; /* %? */
1066 p++;
1067 if (*p == '?') {
1068 type = TOK_PREPROC_QQ; /* %?? */
1069 p++;
1070 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001071 } else if (*p == '!') {
1072 type = TOK_PREPROC_ID;
1073 p++;
1074 if (isidchar(*p)) {
1075 do {
1076 p++;
1077 } while (isidchar(*p));
1078 } else if (*p == '\'' || *p == '\"' || *p == '`') {
1079 p = nasm_skip_string(p);
1080 if (*p)
1081 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001082 else if(verbose)
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001083 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
1084 } else {
1085 /* %! without string or identifier */
1086 type = TOK_OTHER; /* Legacy behavior... */
1087 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001088 } else if (isidchar(*p) ||
1089 ((*p == '!' || *p == '%' || *p == '$') &&
1090 isidchar(p[1]))) {
1091 do {
1092 p++;
1093 }
1094 while (isidchar(*p));
1095 type = TOK_PREPROC_ID;
1096 } else {
1097 type = TOK_OTHER;
1098 if (*p == '%')
1099 p++;
1100 }
1101 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1102 type = TOK_ID;
1103 p++;
1104 while (*p && isidchar(*p))
1105 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001106 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001107 /*
1108 * A string token.
1109 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001110 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001111 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001112
H. Peter Anvine2c80182005-01-15 22:15:51 +00001113 if (*p) {
1114 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001115 } else if(verbose) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001116 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001117 /* Handling unterminated strings by UNV */
1118 /* type = -1; */
1119 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001120 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001121 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001122 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001123 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001124 bool is_hex = false;
1125 bool is_float = false;
1126 bool has_e = false;
1127 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001128
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001130 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001131 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001132
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001133 if (*p == '$') {
1134 p++;
1135 is_hex = true;
1136 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001137
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001138 for (;;) {
1139 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001140
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001141 if (!is_hex && (c == 'e' || c == 'E')) {
1142 has_e = true;
1143 if (*p == '+' || *p == '-') {
1144 /*
1145 * e can only be followed by +/- if it is either a
1146 * prefixed hex number or a floating-point number
1147 */
1148 p++;
1149 is_float = true;
1150 }
1151 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1152 is_hex = true;
1153 } else if (c == 'P' || c == 'p') {
1154 is_float = true;
1155 if (*p == '+' || *p == '-')
1156 p++;
1157 } else if (isnumchar(c) || c == '_')
1158 ; /* just advance */
1159 else if (c == '.') {
1160 /*
1161 * we need to deal with consequences of the legacy
1162 * parser, like "1.nolist" being two tokens
1163 * (TOK_NUMBER, TOK_ID) here; at least give it
1164 * a shot for now. In the future, we probably need
1165 * a flex-based scanner with proper pattern matching
1166 * to do it as well as it can be done. Nothing in
1167 * the world is going to help the person who wants
1168 * 0x123.p16 interpreted as two tokens, though.
1169 */
1170 r = p;
1171 while (*r == '_')
1172 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001173
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001174 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1175 (!is_hex && (*r == 'e' || *r == 'E')) ||
1176 (*r == 'p' || *r == 'P')) {
1177 p = r;
1178 is_float = true;
1179 } else
1180 break; /* Terminate the token */
1181 } else
1182 break;
1183 }
1184 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001185
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001186 if (p == line+1 && *line == '$') {
1187 type = TOK_OTHER; /* TOKEN_HERE */
1188 } else {
1189 if (has_e && !is_hex) {
1190 /* 1e13 is floating-point, but 1e13h is not */
1191 is_float = true;
1192 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001193
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001194 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1195 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001196 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001197 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001198 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001199 /*
1200 * Whitespace just before end-of-line is discarded by
1201 * pretending it's a comment; whitespace just before a
1202 * comment gets lumped into the comment.
1203 */
1204 if (!*p || *p == ';') {
1205 type = TOK_COMMENT;
1206 while (*p)
1207 p++;
1208 }
1209 } else if (*p == ';') {
1210 type = TOK_COMMENT;
1211 while (*p)
1212 p++;
1213 } else {
1214 /*
1215 * Anything else is an operator of some kind. We check
1216 * for all the double-character operators (>>, <<, //,
1217 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001218 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001219 */
1220 type = TOK_OTHER;
1221 if ((p[0] == '>' && p[1] == '>') ||
1222 (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++;
1233 }
1234 p++;
1235 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001236
H. Peter Anvine2c80182005-01-15 22:15:51 +00001237 /* Handling unterminated string by UNV */
1238 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001239 {
1240 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1241 t->text[p-line] = *line;
1242 tail = &t->next;
1243 }
1244 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001245 if (type != TOK_COMMENT) {
1246 *tail = t = new_Token(NULL, type, line, p - line);
1247 tail = &t->next;
1248 }
1249 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001250 }
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +04001251
1252 nasm_dump_token(list);
1253
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001254 return list;
1255}
1256
H. Peter Anvince616072002-04-30 21:02:23 +00001257/*
1258 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001259 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001260 * deleted only all at once by the delete_Blocks function.
1261 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001262static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001263{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001264 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001265
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001266 /* first, get to the end of the linked list */
1267 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001268 b = b->next;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001269
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001270 /* now allocate the requested chunk */
1271 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001272
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001273 /* now allocate a new block for the next request */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001274 b->next = nasm_zalloc(sizeof(Blocks));
1275
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001276 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001277}
1278
1279/*
1280 * this function deletes all managed blocks of memory
1281 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001282static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001283{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001284 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001285
H. Peter Anvin70653092007-10-19 14:42:29 -07001286 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001287 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001288 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001289 * free it.
1290 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001291 while (b) {
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04001292 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001293 a = b;
1294 b = b->next;
1295 if (a != &blocks)
1296 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001297 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001298}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001299
1300/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001301 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001302 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001303 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001304 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001305static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001306 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001307{
1308 Token *t;
1309 int i;
1310
H. Peter Anvin89cee572009-07-15 09:16:54 -04001311 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001312 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1313 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1314 freeTokens[i].next = &freeTokens[i + 1];
1315 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001316 }
1317 t = freeTokens;
1318 freeTokens = t->next;
1319 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001320 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001321 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001322 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001323 t->text = NULL;
1324 } else {
1325 if (txtlen == 0)
1326 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001327 t->text = nasm_malloc(txtlen+1);
1328 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001329 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001330 }
1331 return t;
1332}
1333
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001334static Token *copy_Token(Token * tline)
1335{
1336 Token *t, *tt, *first = NULL, *prev = NULL;
1337 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001338 for (tt = tline; tt != NULL; tt = tt->next) {
1339 if (!freeTokens) {
1340 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1341 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1342 freeTokens[i].next = &freeTokens[i + 1];
1343 freeTokens[i].next = NULL;
1344 }
1345 t = freeTokens;
1346 freeTokens = t->next;
1347 t->next = NULL;
1348 t->text = tt->text ? nasm_strdup(tt->text) : NULL;
1349 t->a.mac = tt->a.mac;
1350 t->a.len = tt->a.len;
1351 t->type = tt->type;
1352 if (prev != NULL) {
1353 prev->next = t;
1354 } else {
1355 first = t;
1356 }
1357 prev = t;
1358 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001359 return first;
1360}
1361
H. Peter Anvine2c80182005-01-15 22:15:51 +00001362static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001363{
1364 Token *next = t->next;
1365 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001366 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001367 freeTokens = t;
1368 return next;
1369}
1370
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001371/*
1372 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001373 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1374 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001375 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001376static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001377{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001378 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001379 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001380 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001381 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001382
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001383 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001384 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001385 char *v;
1386 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001387
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001388 v = t->text + 2;
1389 if (*v == '\'' || *v == '\"' || *v == '`') {
1390 size_t len = nasm_unquote(v, NULL);
1391 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001392
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001393 if (len != clen) {
1394 error(ERR_NONFATAL | ERR_PASS1,
1395 "NUL character in %! string");
1396 v = NULL;
1397 }
1398 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001399
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001400 if (v) {
1401 char *p = getenv(v);
1402 if (!p) {
1403 error(ERR_NONFATAL | ERR_PASS1,
1404 "nonexistent environment variable `%s'", v);
1405 p = "";
1406 }
1407 t->text = nasm_strdup(p);
1408 }
1409 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001410 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001411
H. Peter Anvine2c80182005-01-15 22:15:51 +00001412 /* Expand local macros here and not during preprocessing */
1413 if (expand_locals &&
1414 t->type == TOK_PREPROC_ID && t->text &&
1415 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001416 const char *q;
1417 char *p;
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001418 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001419 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001420 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001421 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001422 p = nasm_strcat(buffer, q);
1423 nasm_free(t->text);
1424 t->text = p;
1425 }
1426 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001427
1428 /* Expand %? and %?? directives */
Keith Kanios3136d482010-11-13 09:34:34 -06001429 if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001430 ((t->type == TOK_PREPROC_Q) ||
1431 (t->type == TOK_PREPROC_QQ))) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001432 ExpInv *ei;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001433 for (ei = istk->expansion; ei != NULL; ei = ei->prev){
1434 if (ei->type == EXP_MMACRO) {
1435 nasm_free(t->text);
1436 if (t->type == TOK_PREPROC_Q) {
1437 t->text = nasm_strdup(ei->name);
1438 } else {
1439 t->text = nasm_strdup(ei->def->name);
1440 }
1441 break;
1442 }
1443 }
1444 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001445
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001446 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001447 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001448 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001449 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001450 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001451
H. Peter Anvin734b1882002-04-30 21:01:08 +00001452 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001453
1454 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001455 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001456 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001457 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001458 q = t->text;
1459 while (*q)
1460 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001461 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001462 }
1463 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001464
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001465 return line;
1466}
1467
1468/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001469 * Initialize a new Line
1470 */
Cyrill Gorcunov29cb0bb2010-11-11 11:19:43 +03001471static inline Line *new_Line(void)
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001472{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001473 return (Line *)nasm_zalloc(sizeof(Line));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001474}
1475
1476
1477/*
1478 * Initialize a new Expansion Definition
1479 */
1480static ExpDef *new_ExpDef(int exp_type)
1481{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001482 ExpDef *ed = (ExpDef*)nasm_zalloc(sizeof(ExpDef));
1483 ed->type = exp_type;
1484 ed->casesense = true;
1485 ed->state = COND_NEVER;
1486
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001487 return ed;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001488}
1489
1490
1491/*
1492 * Initialize a new Expansion Instance
1493 */
1494static ExpInv *new_ExpInv(int exp_type, ExpDef *ed)
1495{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001496 ExpInv *ei = (ExpInv*)nasm_zalloc(sizeof(ExpInv));
1497 ei->type = exp_type;
1498 ei->def = ed;
1499 ei->unique = ++unique;
1500
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001501 if ((istk->mmac_depth < 1) &&
1502 (istk->expansion == NULL) &&
1503 (ed != NULL) &&
1504 (ed->type != EXP_MMACRO) &&
1505 (ed->type != EXP_REP) &&
1506 (ed->type != EXP_WHILE)) {
1507 ei->linnum = src_get_linnum();
1508 src_set_linnum(ei->linnum - ed->linecount - 1);
1509 } else {
1510 ei->linnum = -1;
1511 }
1512 if ((istk->expansion == NULL) ||
1513 (ei->type == EXP_MMACRO)) {
1514 ei->relno = 0;
1515 } else {
1516 ei->relno = istk->expansion->lineno;
1517 if (ed != NULL) {
1518 ei->relno -= (ed->linecount + 1);
1519 }
1520 }
1521 return ei;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001522}
1523
1524/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001525 * A scanner, suitable for use by the expression evaluator, which
1526 * operates on a line of Tokens. Expects a pointer to a pointer to
1527 * the first token in the line to be passed in as its private_data
1528 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001529 *
1530 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001531 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001532static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001533{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001534 Token **tlineptr = private_data;
1535 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001536 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001537
H. Peter Anvine2c80182005-01-15 22:15:51 +00001538 do {
1539 tline = *tlineptr;
1540 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001541 } while (tline && (tline->type == TOK_WHITESPACE ||
1542 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001543
1544 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001545 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001546
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001547 tokval->t_charptr = tline->text;
1548
H. Peter Anvin76690a12002-04-30 20:52:49 +00001549 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001550 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001551 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001552 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001553
H. Peter Anvine2c80182005-01-15 22:15:51 +00001554 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001555 p = tokval->t_charptr = tline->text;
1556 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001557 tokval->t_charptr++;
1558 return tokval->t_type = TOKEN_ID;
1559 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001560
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001561 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001562 if (r >= p+MAX_KEYWORD)
1563 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001564 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001565 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001566 *s = '\0';
1567 /* right, so we have an identifier sitting in temp storage. now,
1568 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001569 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001570 }
1571
H. Peter Anvine2c80182005-01-15 22:15:51 +00001572 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001573 bool rn_error;
1574 tokval->t_integer = readnum(tline->text, &rn_error);
1575 tokval->t_charptr = tline->text;
1576 if (rn_error)
1577 return tokval->t_type = TOKEN_ERRNUM;
1578 else
1579 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001580 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001581
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001582 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001583 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001584 }
1585
H. Peter Anvine2c80182005-01-15 22:15:51 +00001586 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001587 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001588
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001589 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001590 tokval->t_charptr = tline->text;
1591 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001592
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001593 if (ep[0] != bq || ep[1] != '\0')
1594 return tokval->t_type = TOKEN_ERRSTR;
1595 else
1596 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001597 }
1598
H. Peter Anvine2c80182005-01-15 22:15:51 +00001599 if (tline->type == TOK_OTHER) {
1600 if (!strcmp(tline->text, "<<"))
1601 return tokval->t_type = TOKEN_SHL;
1602 if (!strcmp(tline->text, ">>"))
1603 return tokval->t_type = TOKEN_SHR;
1604 if (!strcmp(tline->text, "//"))
1605 return tokval->t_type = TOKEN_SDIV;
1606 if (!strcmp(tline->text, "%%"))
1607 return tokval->t_type = TOKEN_SMOD;
1608 if (!strcmp(tline->text, "=="))
1609 return tokval->t_type = TOKEN_EQ;
1610 if (!strcmp(tline->text, "<>"))
1611 return tokval->t_type = TOKEN_NE;
1612 if (!strcmp(tline->text, "!="))
1613 return tokval->t_type = TOKEN_NE;
1614 if (!strcmp(tline->text, "<="))
1615 return tokval->t_type = TOKEN_LE;
1616 if (!strcmp(tline->text, ">="))
1617 return tokval->t_type = TOKEN_GE;
1618 if (!strcmp(tline->text, "&&"))
1619 return tokval->t_type = TOKEN_DBL_AND;
1620 if (!strcmp(tline->text, "^^"))
1621 return tokval->t_type = TOKEN_DBL_XOR;
1622 if (!strcmp(tline->text, "||"))
1623 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001624 }
1625
1626 /*
1627 * We have no other options: just return the first character of
1628 * the token text.
1629 */
1630 return tokval->t_type = tline->text[0];
1631}
1632
1633/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001634 * Compare a string to the name of an existing macro; this is a
1635 * simple wrapper which calls either strcmp or nasm_stricmp
1636 * depending on the value of the `casesense' parameter.
1637 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001638static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001639{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001640 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001641}
1642
1643/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001644 * Compare a string to the name of an existing macro; this is a
1645 * simple wrapper which calls either strcmp or nasm_stricmp
1646 * depending on the value of the `casesense' parameter.
1647 */
1648static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1649{
1650 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1651}
1652
1653/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001654 * Return the Context structure associated with a %$ token. Return
1655 * NULL, having _already_ reported an error condition, if the
1656 * context stack isn't deep enough for the supplied number of $
1657 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001658 *
1659 * If "namep" is non-NULL, set it to the pointer to the macro name
1660 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001661 */
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001662static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001663{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001664 Context *ctx;
1665 int i;
1666
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001667 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001668 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001669
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001670 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001671 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001672
H. Peter Anvine2c80182005-01-15 22:15:51 +00001673 if (!cstk) {
1674 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1675 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001676 }
1677
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001678 name += 2;
1679 ctx = cstk;
1680 i = 0;
1681 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001682 name++;
1683 i++;
1684 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001685 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001686
H. Peter Anvine2c80182005-01-15 22:15:51 +00001687 if (!ctx) {
1688 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001689 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001691 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001692
1693 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001694 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001695
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001696 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001697}
1698
1699/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001700 * Check to see if a file is already in a string list
1701 */
1702static bool in_list(const StrList *list, const char *str)
1703{
1704 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001705 if (!strcmp(list->str, str))
1706 return true;
1707 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001708 }
1709 return false;
1710}
1711
1712/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001713 * Open an include file. This routine must always return a valid
1714 * file pointer if it returns - it's responsible for throwing an
1715 * ERR_FATAL and bombing out completely if not. It should also try
1716 * the include path one by one until it finds the file or reaches
1717 * the end of the path.
1718 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001719static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001720 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001721{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001722 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001723 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001724 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001725 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001726 size_t prefix_len = 0;
1727 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001728
H. Peter Anvine2c80182005-01-15 22:15:51 +00001729 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001730 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunov6f38fe62010-11-11 11:32:16 +03001731 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001732 memcpy(sl->str, prefix, prefix_len);
1733 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001734 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001735 if (fp && dhead && !in_list(*dhead, sl->str)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001736 **dtail = sl;
1737 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001738 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001739 nasm_free(sl);
1740 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001741 if (fp)
1742 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001743 if (!ip) {
1744 if (!missing_ok)
1745 break;
1746 prefix = NULL;
1747 } else {
1748 prefix = ip->path;
1749 ip = ip->next;
1750 }
1751 if (prefix) {
1752 prefix_len = strlen(prefix);
1753 } else {
1754 /* -MG given and file not found */
1755 if (dhead && !in_list(*dhead, file)) {
1756 sl = nasm_malloc(len+1+sizeof sl->next);
1757 sl->next = NULL;
1758 strcpy(sl->str, file);
1759 **dtail = sl;
1760 *dtail = &sl->next;
1761 }
1762 return NULL;
1763 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001764 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001765
H. Peter Anvin734b1882002-04-30 21:01:08 +00001766 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001767 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001768}
1769
1770/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001771 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001772 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001773 * return true if _any_ single-line macro of that name is defined.
1774 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001775 * `nparam' or no parameters is defined.
1776 *
1777 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001778 * defined, or nparam is -1, the address of the definition structure
1779 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001780 * is NULL, no action will be taken regarding its contents, and no
1781 * error will occur.
1782 *
1783 * Note that this is also called with nparam zero to resolve
1784 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001785 *
1786 * If you already know which context macro belongs to, you can pass
1787 * the context pointer as first parameter; if you won't but name begins
1788 * with %$ the context will be automatically computed. If all_contexts
1789 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001790 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001791static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001792smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001793 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001794{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001795 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001796 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001797
H. Peter Anvin97a23472007-09-16 17:57:25 -07001798 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001799 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001800 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001801 if (cstk)
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001802 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001803 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001804 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001805 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001806 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001807 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001808 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001809 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001810
H. Peter Anvine2c80182005-01-15 22:15:51 +00001811 while (m) {
1812 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001813 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001814 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001815 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001816 *defn = m;
1817 else
1818 *defn = NULL;
1819 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001820 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001821 }
1822 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001823 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001824
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001825 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001826}
1827
1828/*
1829 * Count and mark off the parameters in a multi-line macro call.
1830 * This is called both from within the multi-line macro expansion
1831 * code, and also to mark off the default parameters when provided
1832 * in a %macro definition line.
1833 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001835{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001836 int paramsize, brace;
1837
1838 *nparam = paramsize = 0;
1839 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001840 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001841 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001842 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001843 paramsize += PARAM_DELTA;
1844 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1845 }
1846 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001847 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001848 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001849 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001850 (*params)[(*nparam)++] = t;
1851 while (tok_isnt_(t, brace ? "}" : ","))
1852 t = t->next;
1853 if (t) { /* got a comma/brace */
1854 t = t->next;
1855 if (brace) {
1856 /*
1857 * Now we've found the closing brace, look further
1858 * for the comma.
1859 */
1860 skip_white_(t);
1861 if (tok_isnt_(t, ",")) {
1862 error(ERR_NONFATAL,
1863 "braces do not enclose all of macro parameter");
1864 while (tok_isnt_(t, ","))
1865 t = t->next;
1866 }
1867 if (t)
1868 t = t->next; /* eat the comma */
1869 }
1870 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001871 }
1872}
1873
1874/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001875 * Determine whether one of the various `if' conditions is true or
1876 * not.
1877 *
1878 * We must free the tline we get passed.
1879 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001880static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001881{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001882 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001883 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001884 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001885 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001886 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001887 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001888 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001889
1890 origline = tline;
1891
H. Peter Anvine2c80182005-01-15 22:15:51 +00001892 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001893 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001894 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001895 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001896 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001897 if (!tline)
1898 break;
1899 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001900 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001901 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001902 free_tlist(origline);
1903 return -1;
1904 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001905 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001906 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001907 tline = tline->next;
1908 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001909 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001910
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001911 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001912 j = false; /* have we matched yet? */
Cyrill Gorcunov4c6f82f2011-10-02 01:06:29 +04001913 skip_white_(tline);
1914 do {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001915 if (!tline || (tline->type != TOK_ID &&
1916 (tline->type != TOK_PREPROC_ID ||
1917 tline->text[1] != '$'))) {
1918 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001919 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001920 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001921 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001922 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001923 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001924 tline = tline->next;
Cyrill Gorcunov4c6f82f2011-10-02 01:06:29 +04001925 skip_white_(tline);
1926 } while (tline);
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? */
Cyrill Gorcunov6acada62011-10-02 09:18:34 +04001932 skip_white_(tline);
1933 do {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001934 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;
Cyrill Gorcunov6acada62011-10-02 09:18:34 +04001951 skip_white_(tline);
1952 } while (tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001953 break;
1954
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001955 case PPC_IFIDN:
1956 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001957 tline = expand_smacro(tline);
1958 t = tt = tline;
1959 while (tok_isnt_(tt, ","))
1960 tt = tt->next;
1961 if (!tt) {
1962 error(ERR_NONFATAL,
1963 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001964 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001965 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001966 }
1967 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001968 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001969 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1970 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1971 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001972 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001973 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001974 }
1975 if (t->type == TOK_WHITESPACE) {
1976 t = t->next;
1977 continue;
1978 }
1979 if (tt->type == TOK_WHITESPACE) {
1980 tt = tt->next;
1981 continue;
1982 }
1983 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001984 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001985 break;
1986 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001987 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001988 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001989 size_t l1 = nasm_unquote(t->text, NULL);
1990 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001991
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001992 if (l1 != l2) {
1993 j = false;
1994 break;
1995 }
1996 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1997 j = false;
1998 break;
1999 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002000 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002001 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002002 break;
2003 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002004
H. Peter Anvine2c80182005-01-15 22:15:51 +00002005 t = t->next;
2006 tt = tt->next;
2007 }
2008 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002009 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002010 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002011
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002012 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002013 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002014 bool found = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002015 ExpDef searching, *ed;
H. Peter Anvin65747262002-05-07 00:10:05 +00002016
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002017 skip_white_(tline);
2018 tline = expand_id(tline);
2019 if (!tok_type_(tline, TOK_ID)) {
2020 error(ERR_NONFATAL,
2021 "`%s' expects a macro name", pp_directives[ct]);
2022 goto fail;
2023 }
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002024 memset(&searching, 0, sizeof(searching));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002025 searching.name = nasm_strdup(tline->text);
2026 searching.casesense = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002027 searching.nparam_max = INT_MAX;
2028 tline = expand_smacro(tline->next);
2029 skip_white_(tline);
2030 if (!tline) {
2031 } else if (!tok_type_(tline, TOK_NUMBER)) {
2032 error(ERR_NONFATAL,
2033 "`%s' expects a parameter count or nothing",
2034 pp_directives[ct]);
2035 } else {
2036 searching.nparam_min = searching.nparam_max =
2037 readnum(tline->text, &j);
2038 if (j)
2039 error(ERR_NONFATAL,
2040 "unable to parse parameter count `%s'",
2041 tline->text);
2042 }
2043 if (tline && tok_is_(tline->next, "-")) {
2044 tline = tline->next->next;
2045 if (tok_is_(tline, "*"))
2046 searching.nparam_max = INT_MAX;
2047 else if (!tok_type_(tline, TOK_NUMBER))
2048 error(ERR_NONFATAL,
2049 "`%s' expects a parameter count after `-'",
2050 pp_directives[ct]);
2051 else {
2052 searching.nparam_max = readnum(tline->text, &j);
2053 if (j)
2054 error(ERR_NONFATAL,
2055 "unable to parse parameter count `%s'",
2056 tline->text);
2057 if (searching.nparam_min > searching.nparam_max)
2058 error(ERR_NONFATAL,
2059 "minimum parameter count exceeds maximum");
2060 }
2061 }
2062 if (tline && tok_is_(tline->next, "+")) {
2063 tline = tline->next;
2064 searching.plus = true;
2065 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002066 ed = (ExpDef *) hash_findix(&expdefs, searching.name);
2067 while (ed != NULL) {
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002068 if (!strcmp(ed->name, searching.name) &&
2069 (ed->nparam_min <= searching.nparam_max || searching.plus) &&
2070 (searching.nparam_min <= ed->nparam_max || ed->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002071 found = true;
2072 break;
2073 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002074 ed = ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002075 }
2076 if (tline && tline->next)
2077 error(ERR_WARNING|ERR_PASS1,
2078 "trailing garbage after %%ifmacro ignored");
2079 nasm_free(searching.name);
2080 j = found;
2081 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002082 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002083
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002084 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002085 needtype = TOK_ID;
2086 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002087 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002088 needtype = TOK_NUMBER;
2089 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002090 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002091 needtype = TOK_STRING;
2092 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002093
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002094iftype:
2095 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002096
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002097 while (tok_type_(t, TOK_WHITESPACE) ||
2098 (needtype == TOK_NUMBER &&
2099 tok_type_(t, TOK_OTHER) &&
2100 (t->text[0] == '-' || t->text[0] == '+') &&
2101 !t->text[1]))
2102 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002103
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002104 j = tok_type_(t, needtype);
2105 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002106
2107 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002108 t = tline = expand_smacro(tline);
2109 while (tok_type_(t, TOK_WHITESPACE))
2110 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002111
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002112 j = false;
2113 if (t) {
2114 t = t->next; /* Skip the actual token */
2115 while (tok_type_(t, TOK_WHITESPACE))
2116 t = t->next;
2117 j = !t; /* Should be nothing left */
2118 }
2119 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002120
H. Peter Anvin134b9462008-02-16 17:01:40 -08002121 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002122 t = tline = expand_smacro(tline);
2123 while (tok_type_(t, TOK_WHITESPACE))
2124 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002125
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002126 j = !t; /* Should be empty */
2127 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002128
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002129 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002130 t = tline = expand_smacro(tline);
2131 tptr = &t;
2132 tokval.t_type = TOKEN_INVALID;
2133 evalresult = evaluate(ppscan, tptr, &tokval,
2134 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002135 if (!evalresult)
2136 return -1;
2137 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002138 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002139 "trailing garbage after expression ignored");
2140 if (!is_simple(evalresult)) {
2141 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002142 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002143 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002144 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002145 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002146 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002147
H. Peter Anvine2c80182005-01-15 22:15:51 +00002148 default:
2149 error(ERR_FATAL,
2150 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002151 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002152 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002153 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002154
2155 free_tlist(origline);
2156 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002157
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002158fail:
2159 free_tlist(origline);
2160 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002161}
2162
2163/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002164 * Common code for defining an smacro
2165 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002166static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002167 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002168{
2169 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002170 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002171
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002172 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002173 if (!smac) {
2174 error(ERR_WARNING|ERR_PASS1,
2175 "single-line macro `%s' defined both with and"
2176 " without parameters", mname);
2177 /*
2178 * Some instances of the old code considered this a failure,
2179 * some others didn't. What is the right thing to do here?
2180 */
2181 free_tlist(expansion);
2182 return false; /* Failure */
2183 } else {
2184 /*
2185 * We're redefining, so we have to take over an
2186 * existing SMacro structure. This means freeing
2187 * what was already in it.
2188 */
2189 nasm_free(smac->name);
2190 free_tlist(smac->expansion);
2191 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002192 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002193 smtbl = ctx ? &ctx->localmac : &smacros;
2194 smhead = (SMacro **) hash_findi_add(smtbl, mname);
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002195 smac = nasm_zalloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002196 smac->next = *smhead;
2197 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002198 }
2199 smac->name = nasm_strdup(mname);
2200 smac->casesense = casesense;
2201 smac->nparam = nparam;
2202 smac->expansion = expansion;
2203 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002204 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002205}
2206
2207/*
2208 * Undefine an smacro
2209 */
2210static void undef_smacro(Context *ctx, const char *mname)
2211{
2212 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002213 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002214
H. Peter Anvin166c2472008-05-28 12:28:58 -07002215 smtbl = ctx ? &ctx->localmac : &smacros;
2216 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002217
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002218 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002219 /*
2220 * We now have a macro name... go hunt for it.
2221 */
2222 sp = smhead;
2223 while ((s = *sp) != NULL) {
2224 if (!mstrcmp(s->name, mname, s->casesense)) {
2225 *sp = s->next;
2226 nasm_free(s->name);
2227 free_tlist(s->expansion);
2228 nasm_free(s);
2229 } else {
2230 sp = &s->next;
2231 }
2232 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002233 }
2234}
2235
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002236/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002237 * Parse a mmacro specification.
2238 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002239static bool parse_mmacro_spec(Token *tline, ExpDef *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002240{
2241 bool err;
2242
2243 tline = tline->next;
2244 skip_white_(tline);
2245 tline = expand_id(tline);
2246 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002247 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2248 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002249 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002250
H. Peter Anvina26433d2008-07-16 14:40:01 -07002251 def->name = nasm_strdup(tline->text);
2252 def->plus = false;
2253 def->nolist = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002254// def->in_progress = 0;
2255// def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002256 def->nparam_min = 0;
2257 def->nparam_max = 0;
2258
H. Peter Anvina26433d2008-07-16 14:40:01 -07002259 tline = expand_smacro(tline->next);
2260 skip_white_(tline);
2261 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002262 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002263 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002264 def->nparam_min = def->nparam_max =
2265 readnum(tline->text, &err);
2266 if (err)
2267 error(ERR_NONFATAL,
2268 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002269 }
2270 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002271 tline = tline->next->next;
2272 if (tok_is_(tline, "*")) {
2273 def->nparam_max = INT_MAX;
2274 } else if (!tok_type_(tline, TOK_NUMBER)) {
2275 error(ERR_NONFATAL,
2276 "`%s' expects a parameter count after `-'", directive);
2277 } else {
2278 def->nparam_max = readnum(tline->text, &err);
2279 if (err) {
2280 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2281 tline->text);
2282 }
2283 if (def->nparam_min > def->nparam_max) {
2284 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2285 }
2286 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002287 }
2288 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002289 tline = tline->next;
2290 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002291 }
2292 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002293 !nasm_stricmp(tline->next->text, ".nolist")) {
2294 tline = tline->next;
2295 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002296 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002297
H. Peter Anvina26433d2008-07-16 14:40:01 -07002298 /*
2299 * Handle default parameters.
2300 */
2301 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002302 def->dlist = tline->next;
2303 tline->next = NULL;
2304 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002305 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002306 def->dlist = NULL;
2307 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002308 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002309 def->line = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002310
H. Peter Anvin89cee572009-07-15 09:16:54 -04002311 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002312 !def->plus)
2313 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2314 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002315
H. Peter Anvina26433d2008-07-16 14:40:01 -07002316 return true;
2317}
2318
2319
2320/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002321 * Decode a size directive
2322 */
2323static int parse_size(const char *str) {
2324 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002325 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002326 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002327 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002328
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002329 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002330}
2331
Ed Beroset3ab3f412002-06-11 03:31:49 +00002332/**
2333 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002334 * Find out if a line contains a preprocessor directive, and deal
2335 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002336 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002337 * If a directive _is_ found, it is the responsibility of this routine
2338 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002339 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002340 * @param tline a pointer to the current tokeninzed line linked list
2341 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002342 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002343 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002344static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002345{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002346 enum preproc_token i;
2347 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002348 bool err;
2349 int nparam;
2350 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002351 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002352 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002353 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002354 char *p, *pp;
2355 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002356 Include *inc;
2357 Context *ctx;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002358 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002359 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002360 struct tokenval tokval;
2361 expr *evalresult;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002362 ExpDef *ed, *eed, **edhead;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002363 ExpInv *ei, *eei;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002364 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002365 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002366 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002367
2368 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002369
H. Peter Anvineba20a72002-04-30 20:53:55 +00002370 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002371 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002372 (tline->text[1] == '%' || tline->text[1] == '$'
2373 || tline->text[1] == '!'))
2374 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002375
H. Peter Anvin4169a472007-09-12 01:29:43 +00002376 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002377
H. Peter Anvin4169a472007-09-12 01:29:43 +00002378 switch (i) {
2379 case PP_INVALID:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002380 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002381 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2382 tline->text);
2383 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002384
H. Peter Anvine2c80182005-01-15 22:15:51 +00002385 case PP_STACKSIZE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002386 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002387 /* Directive to tell NASM what the default stack size is. The
2388 * default is for a 16-bit stack, and this can be overriden with
2389 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002390 */
2391 tline = tline->next;
2392 if (tline && tline->type == TOK_WHITESPACE)
2393 tline = tline->next;
2394 if (!tline || tline->type != TOK_ID) {
2395 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2396 free_tlist(origline);
2397 return DIRECTIVE_FOUND;
2398 }
2399 if (nasm_stricmp(tline->text, "flat") == 0) {
2400 /* All subsequent ARG directives are for a 32-bit stack */
2401 StackSize = 4;
2402 StackPointer = "ebp";
2403 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002404 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002405 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2406 /* All subsequent ARG directives are for a 64-bit stack */
2407 StackSize = 8;
2408 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002409 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002410 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002411 } else if (nasm_stricmp(tline->text, "large") == 0) {
2412 /* All subsequent ARG directives are for a 16-bit stack,
2413 * far function call.
2414 */
2415 StackSize = 2;
2416 StackPointer = "bp";
2417 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002418 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002419 } else if (nasm_stricmp(tline->text, "small") == 0) {
2420 /* All subsequent ARG directives are for a 16-bit stack,
2421 * far function call. We don't support near functions.
2422 */
2423 StackSize = 2;
2424 StackPointer = "bp";
2425 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002426 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002427 } else {
2428 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2429 free_tlist(origline);
2430 return DIRECTIVE_FOUND;
2431 }
2432 free_tlist(origline);
2433 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002434
H. Peter Anvine2c80182005-01-15 22:15:51 +00002435 case PP_ARG:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002436 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002437 /* TASM like ARG directive to define arguments to functions, in
2438 * the following form:
2439 *
2440 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2441 */
2442 offset = ArgOffset;
2443 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002444 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002445 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002446
H. Peter Anvine2c80182005-01-15 22:15:51 +00002447 /* Find the argument name */
2448 tline = tline->next;
2449 if (tline && tline->type == TOK_WHITESPACE)
2450 tline = tline->next;
2451 if (!tline || tline->type != TOK_ID) {
2452 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2453 free_tlist(origline);
2454 return DIRECTIVE_FOUND;
2455 }
2456 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002457
H. Peter Anvine2c80182005-01-15 22:15:51 +00002458 /* Find the argument size type */
2459 tline = tline->next;
2460 if (!tline || tline->type != TOK_OTHER
2461 || tline->text[0] != ':') {
2462 error(ERR_NONFATAL,
2463 "Syntax error processing `%%arg' directive");
2464 free_tlist(origline);
2465 return DIRECTIVE_FOUND;
2466 }
2467 tline = tline->next;
2468 if (!tline || tline->type != TOK_ID) {
2469 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2470 free_tlist(origline);
2471 return DIRECTIVE_FOUND;
2472 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002473
H. Peter Anvine2c80182005-01-15 22:15:51 +00002474 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002475 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002476 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002477 size = parse_size(tt->text);
2478 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002479 error(ERR_NONFATAL,
2480 "Invalid size type for `%%arg' missing directive");
2481 free_tlist(tt);
2482 free_tlist(origline);
2483 return DIRECTIVE_FOUND;
2484 }
2485 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002486
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002487 /* Round up to even stack slots */
2488 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002489
H. Peter Anvine2c80182005-01-15 22:15:51 +00002490 /* Now define the macro for the argument */
2491 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2492 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002493 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002494 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002495
H. Peter Anvine2c80182005-01-15 22:15:51 +00002496 /* Move to the next argument in the list */
2497 tline = tline->next;
2498 if (tline && tline->type == TOK_WHITESPACE)
2499 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002500 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002501 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002502 free_tlist(origline);
2503 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002504
H. Peter Anvine2c80182005-01-15 22:15:51 +00002505 case PP_LOCAL:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002506 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002507 /* TASM like LOCAL directive to define local variables for a
2508 * function, in the following form:
2509 *
2510 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2511 *
2512 * The '= LocalSize' at the end is ignored by NASM, but is
2513 * required by TASM to define the local parameter size (and used
2514 * by the TASM macro package).
2515 */
2516 offset = LocalOffset;
2517 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002518 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002519 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002520
H. Peter Anvine2c80182005-01-15 22:15:51 +00002521 /* Find the argument name */
2522 tline = tline->next;
2523 if (tline && tline->type == TOK_WHITESPACE)
2524 tline = tline->next;
2525 if (!tline || tline->type != TOK_ID) {
2526 error(ERR_NONFATAL,
2527 "`%%local' missing argument parameter");
2528 free_tlist(origline);
2529 return DIRECTIVE_FOUND;
2530 }
2531 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002532
H. Peter Anvine2c80182005-01-15 22:15:51 +00002533 /* Find the argument size type */
2534 tline = tline->next;
2535 if (!tline || tline->type != TOK_OTHER
2536 || tline->text[0] != ':') {
2537 error(ERR_NONFATAL,
2538 "Syntax error processing `%%local' directive");
2539 free_tlist(origline);
2540 return DIRECTIVE_FOUND;
2541 }
2542 tline = tline->next;
2543 if (!tline || tline->type != TOK_ID) {
2544 error(ERR_NONFATAL,
2545 "`%%local' missing size type parameter");
2546 free_tlist(origline);
2547 return DIRECTIVE_FOUND;
2548 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002549
H. Peter Anvine2c80182005-01-15 22:15:51 +00002550 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002551 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002552 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002553 size = parse_size(tt->text);
2554 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002555 error(ERR_NONFATAL,
2556 "Invalid size type for `%%local' missing directive");
2557 free_tlist(tt);
2558 free_tlist(origline);
2559 return DIRECTIVE_FOUND;
2560 }
2561 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002562
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002563 /* Round up to even stack slots */
2564 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002565
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002566 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002567
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002568 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002569 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2570 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002571 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002572
H. Peter Anvine2c80182005-01-15 22:15:51 +00002573 /* Now define the assign to setup the enter_c macro correctly */
2574 snprintf(directive, sizeof(directive),
2575 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002576 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002577
H. Peter Anvine2c80182005-01-15 22:15:51 +00002578 /* Move to the next argument in the list */
2579 tline = tline->next;
2580 if (tline && tline->type == TOK_WHITESPACE)
2581 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002582 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002583 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002584 free_tlist(origline);
2585 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002586
H. Peter Anvine2c80182005-01-15 22:15:51 +00002587 case PP_CLEAR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002588 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002589 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002590 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002591 "trailing garbage after `%%clear' ignored");
2592 free_macros();
2593 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002594 free_tlist(origline);
2595 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002596
H. Peter Anvin418ca702008-05-30 10:42:30 -07002597 case PP_DEPEND:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002598 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002599 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002600 skip_white_(t);
2601 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002602 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002603 error(ERR_NONFATAL, "`%%depend' expects a file name");
2604 free_tlist(origline);
2605 return DIRECTIVE_FOUND; /* but we did _something_ */
2606 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002607 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002608 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002609 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002610 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002611 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002612 nasm_unquote_cstr(p, i);
2613 if (dephead && !in_list(*dephead, p)) {
2614 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2615 sl->next = NULL;
2616 strcpy(sl->str, p);
2617 *deptail = sl;
2618 deptail = &sl->next;
2619 }
2620 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002621 return DIRECTIVE_FOUND;
2622
2623 case PP_INCLUDE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002624 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002625 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002626 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002627
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002628 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002629 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002630 error(ERR_NONFATAL, "`%%include' expects a file name");
2631 free_tlist(origline);
2632 return DIRECTIVE_FOUND; /* but we did _something_ */
2633 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002634 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002635 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002636 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002637 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002638 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002639 nasm_unquote_cstr(p, i);
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03002640 inc = nasm_zalloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002641 inc->next = istk;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002642 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002643 if (!inc->fp) {
2644 /* -MG given but file not found */
2645 nasm_free(inc);
2646 } else {
2647 inc->fname = src_set_fname(nasm_strdup(p));
2648 inc->lineno = src_set_linnum(0);
2649 inc->lineinc = 1;
2650 inc->expansion = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002651 istk = inc;
2652 list->uplevel(LIST_INCLUDE);
2653 }
2654 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002655 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002656
H. Peter Anvind2456592008-06-19 15:04:18 -07002657 case PP_USE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002658 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002659 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002660 static macros_t *use_pkg;
2661 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002662
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002663 tline = tline->next;
2664 skip_white_(tline);
2665 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002666
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002667 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002668 tline->type != TOK_INTERNAL_STRING &&
2669 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002670 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002671 free_tlist(origline);
2672 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002673 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002674 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002675 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002676 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002677 if (tline->type == TOK_STRING)
2678 nasm_unquote_cstr(tline->text, i);
2679 use_pkg = nasm_stdmac_find_package(tline->text);
2680 if (!use_pkg)
2681 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2682 else
2683 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002684 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002685 /* Not already included, go ahead and include it */
2686 stdmacpos = use_pkg;
2687 }
2688 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002689 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002690 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002691 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002692 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002693 case PP_POP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002694 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002695 tline = tline->next;
2696 skip_white_(tline);
2697 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002698 if (tline) {
2699 if (!tok_type_(tline, TOK_ID)) {
2700 error(ERR_NONFATAL, "`%s' expects a context identifier",
2701 pp_directives[i]);
2702 free_tlist(origline);
2703 return DIRECTIVE_FOUND; /* but we did _something_ */
2704 }
2705 if (tline->next)
2706 error(ERR_WARNING|ERR_PASS1,
2707 "trailing garbage after `%s' ignored",
2708 pp_directives[i]);
2709 p = nasm_strdup(tline->text);
2710 } else {
2711 p = NULL; /* Anonymous */
2712 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002713
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002714 if (i == PP_PUSH) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002715 ctx = nasm_zalloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002716 ctx->next = cstk;
2717 hash_init(&ctx->localmac, HASH_SMALL);
2718 ctx->name = p;
2719 ctx->number = unique++;
2720 cstk = ctx;
2721 } else {
2722 /* %pop or %repl */
2723 if (!cstk) {
2724 error(ERR_NONFATAL, "`%s': context stack is empty",
2725 pp_directives[i]);
2726 } else if (i == PP_POP) {
2727 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2728 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2729 "expected %s",
2730 cstk->name ? cstk->name : "anonymous", p);
2731 else
2732 ctx_pop();
2733 } else {
2734 /* i == PP_REPL */
2735 nasm_free(cstk->name);
2736 cstk->name = p;
2737 p = NULL;
2738 }
2739 nasm_free(p);
2740 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002741 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002742 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002743 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002744 severity = ERR_FATAL;
2745 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002746 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002747 severity = ERR_NONFATAL;
2748 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002749 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002750 severity = ERR_WARNING|ERR_WARN_USER;
2751 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002752
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002753issue_error:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002754 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002755 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002756 /* Only error out if this is the final pass */
2757 if (pass != 2 && i != PP_FATAL)
2758 return DIRECTIVE_FOUND;
2759
2760 tline->next = expand_smacro(tline->next);
2761 tline = tline->next;
2762 skip_white_(tline);
2763 t = tline ? tline->next : NULL;
2764 skip_white_(t);
2765 if (tok_type_(tline, TOK_STRING) && !t) {
2766 /* The line contains only a quoted string */
2767 p = tline->text;
2768 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2769 error(severity, "%s", p);
2770 } else {
2771 /* Not a quoted string, or more than a quoted string */
2772 p = detoken(tline, false);
2773 error(severity, "%s", p);
2774 nasm_free(p);
2775 }
2776 free_tlist(origline);
2777 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002778 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002779
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002780 CASE_PP_IF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002781 if (defining != NULL) {
2782 if (defining->type == EXP_IF) {
2783 defining->def_depth ++;
2784 }
2785 return NO_DIRECTIVE_FOUND;
2786 }
2787 if ((istk->expansion != NULL) &&
2788 (istk->expansion->emitting == false)) {
2789 j = COND_NEVER;
2790 } else {
2791 j = if_condition(tline->next, i);
2792 tline->next = NULL; /* it got freed */
2793 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
2794 }
2795 ed = new_ExpDef(EXP_IF);
2796 ed->state = j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002797 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
2798 ed->prev = defining;
2799 defining = ed;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002800 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002801 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002802
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002803 CASE_PP_ELIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002804 if (defining != NULL) {
2805 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2806 return NO_DIRECTIVE_FOUND;
2807 }
2808 }
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +04002809 if ((defining == NULL) || (defining->type != EXP_IF)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002810 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2811 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002812 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002813 case COND_IF_TRUE:
2814 defining->state = COND_DONE;
2815 defining->ignoring = true;
2816 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002817
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002818 case COND_DONE:
2819 case COND_NEVER:
2820 defining->ignoring = true;
2821 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002822
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002823 case COND_ELSE_TRUE:
2824 case COND_ELSE_FALSE:
2825 error_precond(ERR_WARNING|ERR_PASS1,
2826 "`%%elif' after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002827 defining->state = COND_NEVER;
2828 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002829 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002830
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002831 case COND_IF_FALSE:
2832 /*
2833 * IMPORTANT: In the case of %if, we will already have
2834 * called expand_mmac_params(); however, if we're
2835 * processing an %elif we must have been in a
2836 * non-emitting mode, which would have inhibited
2837 * the normal invocation of expand_mmac_params().
2838 * Therefore, we have to do it explicitly here.
2839 */
2840 j = if_condition(expand_mmac_params(tline->next), i);
2841 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002842 defining->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002843 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002844 defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002845 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002846 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002847 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002848 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002849
H. Peter Anvine2c80182005-01-15 22:15:51 +00002850 case PP_ELSE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002851 if (defining != NULL) {
2852 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2853 return NO_DIRECTIVE_FOUND;
2854 }
2855 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002856 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002857 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002858 "trailing garbage after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002859 if ((defining == NULL) || (defining->type != EXP_IF)) {
2860 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2861 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002862 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002863 case COND_IF_TRUE:
2864 case COND_DONE:
2865 defining->state = COND_ELSE_FALSE;
2866 defining->ignoring = true;
2867 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002868
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002869 case COND_NEVER:
2870 defining->ignoring = true;
2871 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002872
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002873 case COND_IF_FALSE:
2874 defining->state = COND_ELSE_TRUE;
2875 defining->ignoring = false;
2876 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002877
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002878 case COND_ELSE_TRUE:
2879 case COND_ELSE_FALSE:
2880 error_precond(ERR_WARNING|ERR_PASS1,
2881 "`%%else' after `%%else' ignored.");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002882 defining->state = COND_NEVER;
2883 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002884 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002885 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002886 free_tlist(origline);
2887 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002888
H. Peter Anvine2c80182005-01-15 22:15:51 +00002889 case PP_ENDIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002890 if (defining != NULL) {
2891 if (defining->type == EXP_IF) {
2892 if (defining->def_depth > 0) {
2893 defining->def_depth --;
2894 return NO_DIRECTIVE_FOUND;
2895 }
2896 } else {
2897 return NO_DIRECTIVE_FOUND;
2898 }
2899 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002900 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002901 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002902 "trailing garbage after `%%endif' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002903 if ((defining == NULL) || (defining->type != EXP_IF)) {
2904 error(ERR_NONFATAL, "`%%endif': no matching `%%if'");
2905 return DIRECTIVE_FOUND;
2906 }
2907 ed = defining;
2908 defining = ed->prev;
2909 ed->prev = expansions;
2910 expansions = ed;
2911 ei = new_ExpInv(EXP_IF, ed);
2912 ei->current = ed->line;
2913 ei->emitting = true;
2914 ei->prev = istk->expansion;
2915 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002916 free_tlist(origline);
2917 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002918
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002919 case PP_RMACRO:
2920 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002921 case PP_MACRO:
2922 case PP_IMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002923 if (defining != NULL) {
2924 if (defining->type == EXP_MMACRO) {
2925 defining->def_depth ++;
2926 }
2927 return NO_DIRECTIVE_FOUND;
2928 }
2929 ed = new_ExpDef(EXP_MMACRO);
2930 ed->max_depth =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002931 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002932 ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002933 if (!parse_mmacro_spec(tline, ed, pp_directives[i])) {
2934 nasm_free(ed);
2935 ed = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002936 return DIRECTIVE_FOUND;
2937 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002938 ed->def_depth = 0;
2939 ed->cur_depth = 0;
2940 ed->max_depth = (ed->max_depth + 1);
2941 ed->ignoring = false;
2942 ed->prev = defining;
2943 defining = ed;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002944
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002945 eed = (ExpDef *) hash_findix(&expdefs, ed->name);
2946 while (eed) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002947 if (!strcmp(eed->name, ed->name) &&
2948 (eed->nparam_min <= ed->nparam_max || ed->plus) &&
2949 (ed->nparam_min <= eed->nparam_max || eed->plus)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002950 error(ERR_WARNING|ERR_PASS1,
2951 "redefining multi-line macro `%s'", ed->name);
2952 return DIRECTIVE_FOUND;
2953 }
2954 eed = eed->next;
2955 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002956 free_tlist(origline);
2957 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002958
H. Peter Anvine2c80182005-01-15 22:15:51 +00002959 case PP_ENDM:
2960 case PP_ENDMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002961 if (defining != NULL) {
2962 if (defining->type == EXP_MMACRO) {
2963 if (defining->def_depth > 0) {
2964 defining->def_depth --;
2965 return NO_DIRECTIVE_FOUND;
2966 }
2967 } else {
2968 return NO_DIRECTIVE_FOUND;
2969 }
2970 }
2971 if (!(defining) || (defining->type != EXP_MMACRO)) {
2972 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2973 return DIRECTIVE_FOUND;
2974 }
2975 edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name);
2976 defining->next = *edhead;
2977 *edhead = defining;
2978 ed = defining;
2979 defining = ed->prev;
2980 ed->prev = expansions;
2981 expansions = ed;
2982 ed = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002983 free_tlist(origline);
2984 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002985
H. Peter Anvin89cee572009-07-15 09:16:54 -04002986 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002987 if (defining != NULL) return NO_DIRECTIVE_FOUND;
2988 /*
2989 * We must search along istk->expansion until we hit a
2990 * macro invocation. Then we disable the emitting state(s)
2991 * between exitmacro and endmacro.
2992 */
2993 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
2994 if(ei->type == EXP_MMACRO) {
2995 break;
2996 }
2997 }
2998
2999 if (ei != NULL) {
3000 /*
3001 * Set all invocations leading back to the macro
3002 * invocation to a non-emitting state.
3003 */
3004 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3005 eei->emitting = false;
3006 }
3007 eei->emitting = false;
3008 } else {
3009 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
3010 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05003011 free_tlist(origline);
3012 return DIRECTIVE_FOUND;
3013
H. Peter Anvina26433d2008-07-16 14:40:01 -07003014 case PP_UNMACRO:
3015 case PP_UNIMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003016 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003017 {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003018 ExpDef **ed_p;
3019 ExpDef spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003020
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003021 spec.casesense = (i == PP_UNMACRO);
3022 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
3023 return DIRECTIVE_FOUND;
3024 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003025 ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL);
3026 while (ed_p && *ed_p) {
3027 ed = *ed_p;
3028 if (ed->casesense == spec.casesense &&
3029 !mstrcmp(ed->name, spec.name, spec.casesense) &&
3030 ed->nparam_min == spec.nparam_min &&
3031 ed->nparam_max == spec.nparam_max &&
3032 ed->plus == spec.plus) {
Keith Kaniosc98a5b42010-12-18 12:17:31 -06003033 if (ed->cur_depth > 0) {
Keith Kanios21d885b2010-12-18 12:22:21 -06003034 error(ERR_NONFATAL, "`%s' ignored on active macro",
Keith Kaniosc98a5b42010-12-18 12:17:31 -06003035 pp_directives[i]);
3036 break;
3037 } else {
3038 *ed_p = ed->next;
3039 free_expdef(ed);
3040 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003041 } else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003042 ed_p = &ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003043 }
3044 }
3045 free_tlist(origline);
3046 free_tlist(spec.dlist);
3047 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003048 }
3049
H. Peter Anvine2c80182005-01-15 22:15:51 +00003050 case PP_ROTATE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003051 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003052 if (tline->next && tline->next->type == TOK_WHITESPACE)
3053 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003054 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003055 free_tlist(origline);
3056 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
3057 return DIRECTIVE_FOUND;
3058 }
3059 t = expand_smacro(tline->next);
3060 tline->next = NULL;
3061 free_tlist(origline);
3062 tline = t;
3063 tptr = &t;
3064 tokval.t_type = TOKEN_INVALID;
3065 evalresult =
3066 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3067 free_tlist(tline);
3068 if (!evalresult)
3069 return DIRECTIVE_FOUND;
3070 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003071 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003072 "trailing garbage after expression ignored");
3073 if (!is_simple(evalresult)) {
3074 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
3075 return DIRECTIVE_FOUND;
3076 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003077 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3078 if (ei->type == EXP_MMACRO) {
3079 break;
3080 }
3081 }
3082 if (ei == NULL) {
3083 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
3084 } else if (ei->nparam == 0) {
3085 error(ERR_NONFATAL,
3086 "`%%rotate' invoked within macro without parameters");
3087 } else {
3088 int rotate = ei->rotate + reloc_value(evalresult);
3089
3090 rotate %= (int)ei->nparam;
3091 if (rotate < 0)
3092 rotate += ei->nparam;
3093 ei->rotate = rotate;
3094 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003095 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003096
H. Peter Anvine2c80182005-01-15 22:15:51 +00003097 case PP_REP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003098 if (defining != NULL) {
3099 if (defining->type == EXP_REP) {
3100 defining->def_depth ++;
3101 }
3102 return NO_DIRECTIVE_FOUND;
3103 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003104 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003105 do {
3106 tline = tline->next;
3107 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003108
H. Peter Anvine2c80182005-01-15 22:15:51 +00003109 if (tok_type_(tline, TOK_ID) &&
3110 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003111 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003112 do {
3113 tline = tline->next;
3114 } while (tok_type_(tline, TOK_WHITESPACE));
3115 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003116
H. Peter Anvine2c80182005-01-15 22:15:51 +00003117 if (tline) {
3118 t = expand_smacro(tline);
3119 tptr = &t;
3120 tokval.t_type = TOKEN_INVALID;
3121 evalresult =
3122 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3123 if (!evalresult) {
3124 free_tlist(origline);
3125 return DIRECTIVE_FOUND;
3126 }
3127 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003128 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003129 "trailing garbage after expression ignored");
3130 if (!is_simple(evalresult)) {
3131 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
3132 return DIRECTIVE_FOUND;
3133 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003134 count = reloc_value(evalresult);
3135 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04003136 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003137 count = 0;
3138 } else
3139 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003140 } else {
3141 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003142 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003143 }
3144 free_tlist(origline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003145 ed = new_ExpDef(EXP_REP);
3146 ed->nolist = nolist;
3147 ed->def_depth = 0;
3148 ed->cur_depth = 1;
3149 ed->max_depth = (count - 1);
3150 ed->ignoring = false;
3151 ed->prev = defining;
3152 defining = ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003153 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003154
H. Peter Anvine2c80182005-01-15 22:15:51 +00003155 case PP_ENDREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003156 if (defining != NULL) {
3157 if (defining->type == EXP_REP) {
3158 if (defining->def_depth > 0) {
3159 defining->def_depth --;
3160 return NO_DIRECTIVE_FOUND;
3161 }
3162 } else {
3163 return NO_DIRECTIVE_FOUND;
3164 }
3165 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003166 if ((defining == NULL) || (defining->type != EXP_REP)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003167 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
3168 return DIRECTIVE_FOUND;
3169 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003170
H. Peter Anvine2c80182005-01-15 22:15:51 +00003171 /*
3172 * Now we have a "macro" defined - although it has no name
3173 * and we won't be entering it in the hash tables - we must
3174 * push a macro-end marker for it on to istk->expansion.
3175 * After that, it will take care of propagating itself (a
3176 * macro-end marker line for a macro which is really a %rep
3177 * block will cause the macro to be re-expanded, complete
3178 * with another macro-end marker to ensure the process
3179 * continues) until the whole expansion is forcibly removed
3180 * from istk->expansion by a %exitrep.
3181 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003182 ed = defining;
3183 defining = ed->prev;
3184 ed->prev = expansions;
3185 expansions = ed;
3186 ei = new_ExpInv(EXP_REP, ed);
3187 ei->current = ed->line;
3188 ei->emitting = ((ed->max_depth > 0) ? true : false);
3189 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3190 ei->prev = istk->expansion;
3191 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003192 free_tlist(origline);
3193 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003194
H. Peter Anvine2c80182005-01-15 22:15:51 +00003195 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003196 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3197 /*
3198 * We must search along istk->expansion until we hit a
3199 * rep invocation. Then we disable the emitting state(s)
3200 * between exitrep and endrep.
3201 */
3202 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3203 if (ei->type == EXP_REP) {
3204 break;
3205 }
3206 }
3207
3208 if (ei != NULL) {
3209 /*
3210 * Set all invocations leading back to the rep
3211 * invocation to a non-emitting state.
3212 */
3213 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3214 eei->emitting = false;
3215 }
3216 eei->emitting = false;
3217 eei->current = NULL;
3218 eei->def->cur_depth = eei->def->max_depth;
3219 } else {
3220 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3221 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003222 free_tlist(origline);
3223 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003224
H. Peter Anvine2c80182005-01-15 22:15:51 +00003225 case PP_XDEFINE:
3226 case PP_IXDEFINE:
3227 case PP_DEFINE:
3228 case PP_IDEFINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003229 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003230 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003231
H. Peter Anvine2c80182005-01-15 22:15:51 +00003232 tline = tline->next;
3233 skip_white_(tline);
3234 tline = expand_id(tline);
3235 if (!tline || (tline->type != TOK_ID &&
3236 (tline->type != TOK_PREPROC_ID ||
3237 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003238 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003239 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003240 free_tlist(origline);
3241 return DIRECTIVE_FOUND;
3242 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003243
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003244 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003245 last = tline;
3246 param_start = tline = tline->next;
3247 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003248
H. Peter Anvine2c80182005-01-15 22:15:51 +00003249 /* Expand the macro definition now for %xdefine and %ixdefine */
3250 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3251 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003252
H. Peter Anvine2c80182005-01-15 22:15:51 +00003253 if (tok_is_(tline, "(")) {
3254 /*
3255 * This macro has parameters.
3256 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003257
H. Peter Anvine2c80182005-01-15 22:15:51 +00003258 tline = tline->next;
3259 while (1) {
3260 skip_white_(tline);
3261 if (!tline) {
3262 error(ERR_NONFATAL, "parameter identifier expected");
3263 free_tlist(origline);
3264 return DIRECTIVE_FOUND;
3265 }
3266 if (tline->type != TOK_ID) {
3267 error(ERR_NONFATAL,
3268 "`%s': parameter identifier expected",
3269 tline->text);
3270 free_tlist(origline);
3271 return DIRECTIVE_FOUND;
3272 }
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04003273
3274 smacro_set_param_idx(tline, nparam);
3275 nparam++;
3276
H. Peter Anvine2c80182005-01-15 22:15:51 +00003277 tline = tline->next;
3278 skip_white_(tline);
3279 if (tok_is_(tline, ",")) {
3280 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003281 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003282 if (!tok_is_(tline, ")")) {
3283 error(ERR_NONFATAL,
3284 "`)' expected to terminate macro template");
3285 free_tlist(origline);
3286 return DIRECTIVE_FOUND;
3287 }
3288 break;
3289 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003290 }
3291 last = tline;
3292 tline = tline->next;
3293 }
3294 if (tok_type_(tline, TOK_WHITESPACE))
3295 last = tline, tline = tline->next;
3296 macro_start = NULL;
3297 last->next = NULL;
3298 t = tline;
3299 while (t) {
3300 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003301 list_for_each(tt, param_start)
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04003302 if (is_smacro_param(tt) &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003303 !strcmp(tt->text, t->text))
3304 t->type = tt->type;
3305 }
3306 tt = t->next;
3307 t->next = macro_start;
3308 macro_start = t;
3309 t = tt;
3310 }
3311 /*
3312 * Good. We now have a macro name, a parameter count, and a
3313 * token list (in reverse order) for an expansion. We ought
3314 * to be OK just to create an SMacro, store it, and let
3315 * free_tlist have the rest of the line (which we have
3316 * carefully re-terminated after chopping off the expansion
3317 * from the end).
3318 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003319 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003320 free_tlist(origline);
3321 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003322
H. Peter Anvine2c80182005-01-15 22:15:51 +00003323 case PP_UNDEF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003324 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003325 tline = tline->next;
3326 skip_white_(tline);
3327 tline = expand_id(tline);
3328 if (!tline || (tline->type != TOK_ID &&
3329 (tline->type != TOK_PREPROC_ID ||
3330 tline->text[1] != '$'))) {
3331 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3332 free_tlist(origline);
3333 return DIRECTIVE_FOUND;
3334 }
3335 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003336 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003337 "trailing garbage after macro name ignored");
3338 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003339
H. Peter Anvine2c80182005-01-15 22:15:51 +00003340 /* Find the context that symbol belongs to */
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003341 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003342 undef_smacro(ctx, mname);
3343 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003344 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003345
H. Peter Anvin9e200162008-06-04 17:23:14 -07003346 case PP_DEFSTR:
3347 case PP_IDEFSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003348 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003349 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003350
3351 tline = tline->next;
3352 skip_white_(tline);
3353 tline = expand_id(tline);
3354 if (!tline || (tline->type != TOK_ID &&
3355 (tline->type != TOK_PREPROC_ID ||
3356 tline->text[1] != '$'))) {
3357 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003358 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003359 free_tlist(origline);
3360 return DIRECTIVE_FOUND;
3361 }
3362
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003363 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003364 last = tline;
3365 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003366 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003367
3368 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003369 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003370
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003371 p = detoken(tline, false);
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003372 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003373 macro_start->text = nasm_quote(p, strlen(p));
3374 macro_start->type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003375 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003376
3377 /*
3378 * We now have a macro name, an implicit parameter count of
3379 * zero, and a string token to use as an expansion. Create
3380 * and store an SMacro.
3381 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003382 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003383 free_tlist(origline);
3384 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003385
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003386 case PP_DEFTOK:
3387 case PP_IDEFTOK:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003388 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003389 casesense = (i == PP_DEFTOK);
3390
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003391 tline = tline->next;
3392 skip_white_(tline);
3393 tline = expand_id(tline);
3394 if (!tline || (tline->type != TOK_ID &&
3395 (tline->type != TOK_PREPROC_ID ||
3396 tline->text[1] != '$'))) {
3397 error(ERR_NONFATAL,
3398 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003399 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003400 free_tlist(origline);
3401 return DIRECTIVE_FOUND;
3402 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003403 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003404 last = tline;
3405 tline = expand_smacro(tline->next);
3406 last->next = NULL;
3407
3408 t = tline;
3409 while (tok_type_(t, TOK_WHITESPACE))
3410 t = t->next;
3411 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003412 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003413 error(ERR_NONFATAL,
3414 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003415 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003416 free_tlist(tline);
3417 free_tlist(origline);
3418 return DIRECTIVE_FOUND;
3419 }
3420
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003421 /*
3422 * Convert the string to a token stream. Note that smacros
3423 * are stored with the token stream reversed, so we have to
3424 * reverse the output of tokenize().
3425 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003426 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003427 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003428
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003429 /*
3430 * We now have a macro name, an implicit parameter count of
3431 * zero, and a numeric token to use as an expansion. Create
3432 * and store an SMacro.
3433 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003434 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003435 free_tlist(tline);
3436 free_tlist(origline);
3437 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003438
H. Peter Anvin418ca702008-05-30 10:42:30 -07003439 case PP_PATHSEARCH:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003440 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003441 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003442 FILE *fp;
3443 StrList *xsl = NULL;
3444 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003445
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003446 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003447
3448 tline = tline->next;
3449 skip_white_(tline);
3450 tline = expand_id(tline);
3451 if (!tline || (tline->type != TOK_ID &&
3452 (tline->type != TOK_PREPROC_ID ||
3453 tline->text[1] != '$'))) {
3454 error(ERR_NONFATAL,
3455 "`%%pathsearch' expects a macro identifier as first parameter");
3456 free_tlist(origline);
3457 return DIRECTIVE_FOUND;
3458 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003459 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003460 last = tline;
3461 tline = expand_smacro(tline->next);
3462 last->next = NULL;
3463
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003464 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003465 while (tok_type_(t, TOK_WHITESPACE))
3466 t = t->next;
3467
3468 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003469 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003470 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003471 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003472 free_tlist(origline);
3473 return DIRECTIVE_FOUND; /* but we did _something_ */
3474 }
3475 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003476 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003477 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003478 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003479 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003480 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003481
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003482 fp = inc_fopen(p, &xsl, &xst, true);
3483 if (fp) {
3484 p = xsl->str;
3485 fclose(fp); /* Don't actually care about the file */
3486 }
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003487 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003488 macro_start->text = nasm_quote(p, strlen(p));
3489 macro_start->type = TOK_STRING;
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04003490 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003491
3492 /*
3493 * We now have a macro name, an implicit parameter count of
3494 * zero, and a string token to use as an expansion. Create
3495 * and store an SMacro.
3496 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003497 define_smacro(ctx, mname, casesense, 0, macro_start);
3498 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003499 free_tlist(origline);
3500 return DIRECTIVE_FOUND;
3501 }
3502
H. Peter Anvine2c80182005-01-15 22:15:51 +00003503 case PP_STRLEN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003504 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003505 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003506
H. Peter Anvine2c80182005-01-15 22:15:51 +00003507 tline = tline->next;
3508 skip_white_(tline);
3509 tline = expand_id(tline);
3510 if (!tline || (tline->type != TOK_ID &&
3511 (tline->type != TOK_PREPROC_ID ||
3512 tline->text[1] != '$'))) {
3513 error(ERR_NONFATAL,
3514 "`%%strlen' expects a macro identifier as first parameter");
3515 free_tlist(origline);
3516 return DIRECTIVE_FOUND;
3517 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003518 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003519 last = tline;
3520 tline = expand_smacro(tline->next);
3521 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003522
H. Peter Anvine2c80182005-01-15 22:15:51 +00003523 t = tline;
3524 while (tok_type_(t, TOK_WHITESPACE))
3525 t = t->next;
3526 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003527 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003528 error(ERR_NONFATAL,
3529 "`%%strlen` requires string as second parameter");
3530 free_tlist(tline);
3531 free_tlist(origline);
3532 return DIRECTIVE_FOUND;
3533 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003534
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003535 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003536 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003537
H. Peter Anvine2c80182005-01-15 22:15:51 +00003538 /*
3539 * We now have a macro name, an implicit parameter count of
3540 * zero, and a numeric token to use as an expansion. Create
3541 * and store an SMacro.
3542 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003543 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003544 free_tlist(tline);
3545 free_tlist(origline);
3546 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003547
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003548 case PP_STRCAT:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003549 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003550 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003551
3552 tline = tline->next;
3553 skip_white_(tline);
3554 tline = expand_id(tline);
3555 if (!tline || (tline->type != TOK_ID &&
3556 (tline->type != TOK_PREPROC_ID ||
3557 tline->text[1] != '$'))) {
3558 error(ERR_NONFATAL,
3559 "`%%strcat' expects a macro identifier as first parameter");
3560 free_tlist(origline);
3561 return DIRECTIVE_FOUND;
3562 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003563 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003564 last = tline;
3565 tline = expand_smacro(tline->next);
3566 last->next = NULL;
3567
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003568 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003569 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003570 switch (t->type) {
3571 case TOK_WHITESPACE:
3572 break;
3573 case TOK_STRING:
3574 len += t->a.len = nasm_unquote(t->text, NULL);
3575 break;
3576 case TOK_OTHER:
3577 if (!strcmp(t->text, ",")) /* permit comma separators */
3578 break;
3579 /* else fall through */
3580 default:
3581 error(ERR_NONFATAL,
3582 "non-string passed to `%%strcat' (%d)", t->type);
3583 free_tlist(tline);
3584 free_tlist(origline);
3585 return DIRECTIVE_FOUND;
3586 }
3587 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003588
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003589 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003590 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003591 if (t->type == TOK_STRING) {
3592 memcpy(p, t->text, t->a.len);
3593 p += t->a.len;
3594 }
3595 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003596
3597 /*
3598 * We now have a macro name, an implicit parameter count of
3599 * zero, and a numeric token to use as an expansion. Create
3600 * and store an SMacro.
3601 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003602 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3603 macro_start->text = nasm_quote(pp, len);
3604 nasm_free(pp);
3605 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003606 free_tlist(tline);
3607 free_tlist(origline);
3608 return DIRECTIVE_FOUND;
3609
H. Peter Anvine2c80182005-01-15 22:15:51 +00003610 case PP_SUBSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003611 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003612 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003613 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003614 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003615
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003616 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003617
H. Peter Anvine2c80182005-01-15 22:15:51 +00003618 tline = tline->next;
3619 skip_white_(tline);
3620 tline = expand_id(tline);
3621 if (!tline || (tline->type != TOK_ID &&
3622 (tline->type != TOK_PREPROC_ID ||
3623 tline->text[1] != '$'))) {
3624 error(ERR_NONFATAL,
3625 "`%%substr' expects a macro identifier as first parameter");
3626 free_tlist(origline);
3627 return DIRECTIVE_FOUND;
3628 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003629 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003630 last = tline;
3631 tline = expand_smacro(tline->next);
3632 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003633
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003634 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003635 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003636 while (tok_type_(t, TOK_WHITESPACE))
3637 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003638
H. Peter Anvine2c80182005-01-15 22:15:51 +00003639 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003640 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003641 error(ERR_NONFATAL,
3642 "`%%substr` requires string as second parameter");
3643 free_tlist(tline);
3644 free_tlist(origline);
3645 return DIRECTIVE_FOUND;
3646 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003647
H. Peter Anvine2c80182005-01-15 22:15:51 +00003648 tt = t->next;
3649 tptr = &tt;
3650 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003651 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003652 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003653 if (!evalresult) {
3654 free_tlist(tline);
3655 free_tlist(origline);
3656 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003657 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003658 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3659 free_tlist(tline);
3660 free_tlist(origline);
3661 return DIRECTIVE_FOUND;
3662 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003663 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003664
3665 while (tok_type_(tt, TOK_WHITESPACE))
3666 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003667 if (!tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003668 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003669 } else {
3670 tokval.t_type = TOKEN_INVALID;
3671 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3672 pass, error, NULL);
3673 if (!evalresult) {
3674 free_tlist(tline);
3675 free_tlist(origline);
3676 return DIRECTIVE_FOUND;
3677 } else if (!is_simple(evalresult)) {
3678 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3679 free_tlist(tline);
3680 free_tlist(origline);
3681 return DIRECTIVE_FOUND;
3682 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003683 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003684 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003685
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003686 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003687 /* make start and count being in range */
3688 if (start < 0)
3689 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003690 if (count < 0)
3691 count = len + count + 1 - start;
3692 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003693 count = len - start;
3694 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003695 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003696
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003697 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003698 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003699 macro_start->type = TOK_STRING;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003700
H. Peter Anvine2c80182005-01-15 22:15:51 +00003701 /*
3702 * We now have a macro name, an implicit parameter count of
3703 * zero, and a numeric token to use as an expansion. Create
3704 * and store an SMacro.
3705 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003706 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003707 free_tlist(tline);
3708 free_tlist(origline);
3709 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003710 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003711
H. Peter Anvine2c80182005-01-15 22:15:51 +00003712 case PP_ASSIGN:
3713 case PP_IASSIGN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003714 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003715 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003716
H. Peter Anvine2c80182005-01-15 22:15:51 +00003717 tline = tline->next;
3718 skip_white_(tline);
3719 tline = expand_id(tline);
3720 if (!tline || (tline->type != TOK_ID &&
3721 (tline->type != TOK_PREPROC_ID ||
3722 tline->text[1] != '$'))) {
3723 error(ERR_NONFATAL,
3724 "`%%%sassign' expects a macro identifier",
3725 (i == PP_IASSIGN ? "i" : ""));
3726 free_tlist(origline);
3727 return DIRECTIVE_FOUND;
3728 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003729 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003730 last = tline;
3731 tline = expand_smacro(tline->next);
3732 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003733
H. Peter Anvine2c80182005-01-15 22:15:51 +00003734 t = tline;
3735 tptr = &t;
3736 tokval.t_type = TOKEN_INVALID;
3737 evalresult =
3738 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3739 free_tlist(tline);
3740 if (!evalresult) {
3741 free_tlist(origline);
3742 return DIRECTIVE_FOUND;
3743 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003744
H. Peter Anvine2c80182005-01-15 22:15:51 +00003745 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003746 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003747 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003748
H. Peter Anvine2c80182005-01-15 22:15:51 +00003749 if (!is_simple(evalresult)) {
3750 error(ERR_NONFATAL,
3751 "non-constant value given to `%%%sassign'",
3752 (i == PP_IASSIGN ? "i" : ""));
3753 free_tlist(origline);
3754 return DIRECTIVE_FOUND;
3755 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003756
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003757 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003758 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003759
H. Peter Anvine2c80182005-01-15 22:15:51 +00003760 /*
3761 * We now have a macro name, an implicit parameter count of
3762 * zero, and a numeric token to use as an expansion. Create
3763 * and store an SMacro.
3764 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003765 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003766 free_tlist(origline);
3767 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003768
H. Peter Anvine2c80182005-01-15 22:15:51 +00003769 case PP_LINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003770 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003771 /*
3772 * Syntax is `%line nnn[+mmm] [filename]'
3773 */
3774 tline = tline->next;
3775 skip_white_(tline);
3776 if (!tok_type_(tline, TOK_NUMBER)) {
3777 error(ERR_NONFATAL, "`%%line' expects line number");
3778 free_tlist(origline);
3779 return DIRECTIVE_FOUND;
3780 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003781 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003782 m = 1;
3783 tline = tline->next;
3784 if (tok_is_(tline, "+")) {
3785 tline = tline->next;
3786 if (!tok_type_(tline, TOK_NUMBER)) {
3787 error(ERR_NONFATAL, "`%%line' expects line increment");
3788 free_tlist(origline);
3789 return DIRECTIVE_FOUND;
3790 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003791 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003792 tline = tline->next;
3793 }
3794 skip_white_(tline);
3795 src_set_linnum(k);
3796 istk->lineinc = m;
3797 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003798 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003799 }
3800 free_tlist(origline);
3801 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003802
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003803 case PP_WHILE:
3804 if (defining != NULL) {
3805 if (defining->type == EXP_WHILE) {
3806 defining->def_depth ++;
3807 }
3808 return NO_DIRECTIVE_FOUND;
3809 }
3810 l = NULL;
3811 if ((istk->expansion != NULL) &&
3812 (istk->expansion->emitting == false)) {
3813 j = COND_NEVER;
3814 } else {
3815 l = new_Line();
3816 l->first = copy_Token(tline->next);
3817 j = if_condition(tline->next, i);
3818 tline->next = NULL; /* it got freed */
3819 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
3820 }
3821 ed = new_ExpDef(EXP_WHILE);
3822 ed->state = j;
3823 ed->cur_depth = 1;
3824 ed->max_depth = DEADMAN_LIMIT;
3825 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
3826 if (ed->ignoring == false) {
3827 ed->line = l;
3828 ed->last = l;
3829 } else if (l != NULL) {
3830 delete_Token(l->first);
3831 nasm_free(l);
3832 l = NULL;
3833 }
3834 ed->prev = defining;
3835 defining = ed;
3836 free_tlist(origline);
3837 return DIRECTIVE_FOUND;
3838
3839 case PP_ENDWHILE:
3840 if (defining != NULL) {
3841 if (defining->type == EXP_WHILE) {
3842 if (defining->def_depth > 0) {
3843 defining->def_depth --;
3844 return NO_DIRECTIVE_FOUND;
3845 }
3846 } else {
3847 return NO_DIRECTIVE_FOUND;
3848 }
3849 }
3850 if (tline->next != NULL) {
3851 error_precond(ERR_WARNING|ERR_PASS1,
3852 "trailing garbage after `%%endwhile' ignored");
3853 }
3854 if ((defining == NULL) || (defining->type != EXP_WHILE)) {
3855 error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'");
3856 return DIRECTIVE_FOUND;
3857 }
3858 ed = defining;
3859 defining = ed->prev;
3860 if (ed->ignoring == false) {
3861 ed->prev = expansions;
3862 expansions = ed;
3863 ei = new_ExpInv(EXP_WHILE, ed);
3864 ei->current = ed->line->next;
3865 ei->emitting = true;
3866 ei->prev = istk->expansion;
3867 istk->expansion = ei;
3868 } else {
3869 nasm_free(ed);
3870 }
3871 free_tlist(origline);
3872 return DIRECTIVE_FOUND;
3873
3874 case PP_EXITWHILE:
3875 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3876 /*
3877 * We must search along istk->expansion until we hit a
3878 * while invocation. Then we disable the emitting state(s)
3879 * between exitwhile and endwhile.
3880 */
3881 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3882 if (ei->type == EXP_WHILE) {
3883 break;
3884 }
3885 }
3886
3887 if (ei != NULL) {
3888 /*
3889 * Set all invocations leading back to the while
3890 * invocation to a non-emitting state.
3891 */
3892 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3893 eei->emitting = false;
3894 }
3895 eei->emitting = false;
3896 eei->current = NULL;
3897 eei->def->cur_depth = eei->def->max_depth;
3898 } else {
3899 error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block");
3900 }
3901 free_tlist(origline);
3902 return DIRECTIVE_FOUND;
3903
3904 case PP_COMMENT:
3905 if (defining != NULL) {
3906 if (defining->type == EXP_COMMENT) {
3907 defining->def_depth ++;
3908 }
3909 return NO_DIRECTIVE_FOUND;
3910 }
3911 ed = new_ExpDef(EXP_COMMENT);
3912 ed->ignoring = true;
3913 ed->prev = defining;
3914 defining = ed;
3915 free_tlist(origline);
3916 return DIRECTIVE_FOUND;
3917
3918 case PP_ENDCOMMENT:
3919 if (defining != NULL) {
3920 if (defining->type == EXP_COMMENT) {
3921 if (defining->def_depth > 0) {
3922 defining->def_depth --;
3923 return NO_DIRECTIVE_FOUND;
3924 }
3925 } else {
3926 return NO_DIRECTIVE_FOUND;
3927 }
3928 }
3929 if ((defining == NULL) || (defining->type != EXP_COMMENT)) {
3930 error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'");
3931 return DIRECTIVE_FOUND;
3932 }
3933 ed = defining;
3934 defining = ed->prev;
3935 nasm_free(ed);
3936 free_tlist(origline);
3937 return DIRECTIVE_FOUND;
3938
3939 case PP_FINAL:
3940 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3941 if (in_final != false) {
3942 error(ERR_FATAL, "`%%final' cannot be used recursively");
3943 }
3944 tline = tline->next;
3945 skip_white_(tline);
3946 if (tline == NULL) {
3947 error(ERR_NONFATAL, "`%%final' expects at least one parameter");
3948 } else {
3949 l = new_Line();
3950 l->first = copy_Token(tline);
3951 l->next = finals;
3952 finals = l;
3953 }
3954 free_tlist(origline);
3955 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003956
H. Peter Anvine2c80182005-01-15 22:15:51 +00003957 default:
3958 error(ERR_FATAL,
3959 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003960 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003961 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003962 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003963}
3964
3965/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003966 * Ensure that a macro parameter contains a condition code and
3967 * nothing else. Return the condition code index if so, or -1
3968 * otherwise.
3969 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003970static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003971{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003972 Token *tt;
3973 int i, j, k, m;
3974
H. Peter Anvin25a99342007-09-22 17:45:45 -07003975 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003976 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003977
H. Peter Anvineba20a72002-04-30 20:53:55 +00003978 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003979 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003980 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003981 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003982 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003983 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003984 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003985
3986 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04003987 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003988 while (j - i > 1) {
3989 k = (j + i) / 2;
3990 m = nasm_stricmp(t->text, conditions[k]);
3991 if (m == 0) {
3992 i = k;
3993 j = -2;
3994 break;
3995 } else if (m < 0) {
3996 j = k;
3997 } else
3998 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003999 }
4000 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004001 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004002 return i;
4003}
4004
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004005static bool paste_tokens(Token **head, const struct tokseq_match *m,
4006 int mnum, bool handle_paste_tokens)
H. Peter Anvind784a082009-04-20 14:01:18 -07004007{
4008 Token **tail, *t, *tt;
4009 Token **paste_head;
4010 bool did_paste = false;
4011 char *tmp;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004012 int i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004013
4014 /* Now handle token pasting... */
4015 paste_head = NULL;
4016 tail = head;
4017 while ((t = *tail) && (tt = t->next)) {
4018 switch (t->type) {
4019 case TOK_WHITESPACE:
4020 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004021 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07004022 t->next = delete_Token(tt);
4023 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004024 /* Do not advance paste_head here */
4025 tail = &t->next;
4026 }
H. Peter Anvind784a082009-04-20 14:01:18 -07004027 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004028 case TOK_PASTE: /* %+ */
4029 if (handle_paste_tokens) {
4030 /* Zap %+ and whitespace tokens to the right */
4031 while (t && (t->type == TOK_WHITESPACE ||
4032 t->type == TOK_PASTE))
4033 t = *tail = delete_Token(t);
4034 if (!paste_head || !t)
4035 break; /* Nothing to paste with */
4036 tail = paste_head;
4037 t = *tail;
4038 tt = t->next;
4039 while (tok_type_(tt, TOK_WHITESPACE))
4040 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004041 if (tt) {
4042 tmp = nasm_strcat(t->text, tt->text);
4043 delete_Token(t);
4044 tt = delete_Token(tt);
4045 t = *tail = tokenize(tmp);
4046 nasm_free(tmp);
4047 while (t->next) {
4048 tail = &t->next;
4049 t = t->next;
4050 }
4051 t->next = tt; /* Attach the remaining token chain */
4052 did_paste = true;
4053 }
4054 paste_head = tail;
4055 tail = &t->next;
4056 break;
4057 }
4058 /* else fall through */
4059 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004060 /*
4061 * Concatenation of tokens might look nontrivial
4062 * but in real it's pretty simple -- the caller
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004063 * prepares the masks of token types to be concatenated
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004064 * and we simply find matched sequences and slip
4065 * them together
4066 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004067 for (i = 0; i < mnum; i++) {
4068 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
4069 size_t len = 0;
4070 char *tmp, *p;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004071
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004072 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004073 len += strlen(tt->text);
4074 tt = tt->next;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004075 }
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004076
Cyrill Gorcunovfdd0ac52011-06-27 01:22:27 +04004077 nasm_dump_token(tt);
4078
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004079 /*
4080 * Now tt points to the first token after
4081 * the potential paste area...
4082 */
4083 if (tt != t->next) {
4084 /* We have at least two tokens... */
4085 len += strlen(t->text);
4086 p = tmp = nasm_malloc(len+1);
4087 while (t != tt) {
4088 strcpy(p, t->text);
4089 p = strchr(p, '\0');
4090 t = delete_Token(t);
4091 }
4092 t = *tail = tokenize(tmp);
4093 nasm_free(tmp);
4094 while (t->next) {
4095 tail = &t->next;
4096 t = t->next;
4097 }
4098 t->next = tt; /* Attach the remaining token chain */
4099 did_paste = true;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004100 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004101 paste_head = tail;
4102 tail = &t->next;
4103 break;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004104 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004105 }
4106 if (i >= mnum) { /* no match */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004107 tail = &t->next;
4108 if (!tok_type_(t->next, TOK_WHITESPACE))
4109 paste_head = tail;
4110 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004111 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004112 }
4113 }
4114 return did_paste;
4115}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004116
4117/*
4118 * expands to a list of tokens from %{x:y}
4119 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004120static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004121{
4122 Token *t = tline, **tt, *tm, *head;
4123 char *pos;
4124 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004125
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004126 pos = strchr(tline->text, ':');
4127 nasm_assert(pos);
4128
4129 lst = atoi(pos + 1);
4130 fst = atoi(tline->text + 1);
4131
4132 /*
4133 * only macros params are accounted so
4134 * if someone passes %0 -- we reject such
4135 * value(s)
4136 */
4137 if (lst == 0 || fst == 0)
4138 goto err;
4139
4140 /* the values should be sane */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004141 if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) ||
4142 (lst > (int)ei->nparam || lst < (-(int)ei->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004143 goto err;
4144
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004145 fst = fst < 0 ? fst + (int)ei->nparam + 1: fst;
4146 lst = lst < 0 ? lst + (int)ei->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004147
4148 /* counted from zero */
4149 fst--, lst--;
4150
4151 /*
4152 * it will be at least one token
4153 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004154 tm = ei->params[(fst + ei->rotate) % ei->nparam];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004155 t = new_Token(NULL, tm->type, tm->text, 0);
4156 head = t, tt = &t->next;
4157 if (fst < lst) {
4158 for (i = fst + 1; i <= lst; i++) {
4159 t = new_Token(NULL, TOK_OTHER, ",", 0);
4160 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004161 j = (i + ei->rotate) % ei->nparam;
4162 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004163 t = new_Token(NULL, tm->type, tm->text, 0);
4164 *tt = t, tt = &t->next;
4165 }
4166 } else {
4167 for (i = fst - 1; i >= lst; i--) {
4168 t = new_Token(NULL, TOK_OTHER, ",", 0);
4169 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004170 j = (i + ei->rotate) % ei->nparam;
4171 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004172 t = new_Token(NULL, tm->type, tm->text, 0);
4173 *tt = t, tt = &t->next;
4174 }
4175 }
4176
4177 *last = tt;
4178 return head;
4179
4180err:
4181 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
4182 &tline->text[1]);
4183 return tline;
4184}
4185
H. Peter Anvin76690a12002-04-30 20:52:49 +00004186/*
4187 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004188 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004189 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004190 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004191static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004192{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004193 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004194 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004195 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004196
4197 tail = &thead;
4198 thead = NULL;
4199
Cyrill Gorcunov2e046002011-06-26 23:33:56 +04004200 nasm_dump_stream(tline);
4201
H. Peter Anvine2c80182005-01-15 22:15:51 +00004202 while (tline) {
4203 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004204 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4205 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4206 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004207 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004208 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004209 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004210 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004211 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004212 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004213
H. Peter Anvine2c80182005-01-15 22:15:51 +00004214 t = tline;
4215 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004216
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004217 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
4218 if (ei->type == EXP_MMACRO) {
4219 break;
4220 }
4221 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004222 if (ei == NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004223 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004224 } else {
4225 pos = strchr(t->text, ':');
4226 if (!pos) {
4227 switch (t->text[1]) {
4228 /*
4229 * We have to make a substitution of one of the
4230 * forms %1, %-1, %+1, %%foo, %0.
4231 */
4232 case '0':
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004233 if ((strlen(t->text) > 2) && (t->text[2] == '0')) {
4234 type = TOK_ID;
4235 text = nasm_strdup(ei->label_text);
4236 } else {
4237 type = TOK_NUMBER;
4238 snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam);
4239 text = nasm_strdup(tmpbuf);
4240 }
4241 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004242 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004243 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004244 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004245 ei->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004246 text = nasm_strcat(tmpbuf, t->text + 2);
4247 break;
4248 case '-':
4249 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004250 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004251 tt = NULL;
4252 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004253 if (ei->nparam > 1)
4254 n = (n + ei->rotate) % ei->nparam;
4255 tt = ei->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004256 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004257 cc = find_cc(tt);
4258 if (cc == -1) {
4259 error(ERR_NONFATAL,
4260 "macro parameter %d is not a condition code",
4261 n + 1);
4262 text = NULL;
4263 } else {
4264 type = TOK_ID;
4265 if (inverse_ccs[cc] == -1) {
4266 error(ERR_NONFATAL,
4267 "condition code `%s' is not invertible",
4268 conditions[cc]);
4269 text = NULL;
4270 } else
4271 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4272 }
4273 break;
4274 case '+':
4275 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004276 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004277 tt = NULL;
4278 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004279 if (ei->nparam > 1)
4280 n = (n + ei->rotate) % ei->nparam;
4281 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004282 }
4283 cc = find_cc(tt);
4284 if (cc == -1) {
4285 error(ERR_NONFATAL,
4286 "macro parameter %d is not a condition code",
4287 n + 1);
4288 text = NULL;
4289 } else {
4290 type = TOK_ID;
4291 text = nasm_strdup(conditions[cc]);
4292 }
4293 break;
4294 default:
4295 n = atoi(t->text + 1) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004296 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004297 tt = NULL;
4298 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004299 if (ei->nparam > 1)
4300 n = (n + ei->rotate) % ei->nparam;
4301 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004302 }
4303 if (tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004304 for (i = 0; i < ei->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004305 *tail = new_Token(NULL, tt->type, tt->text, 0);
4306 tail = &(*tail)->next;
4307 tt = tt->next;
4308 }
4309 }
4310 text = NULL; /* we've done it here */
4311 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004312 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004313 } else {
4314 /*
4315 * seems we have a parameters range here
4316 */
4317 Token *head, **last;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004318 head = expand_mmac_params_range(ei, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004319 if (head != t) {
4320 *tail = head;
4321 *last = tline;
4322 tline = head;
4323 text = NULL;
4324 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004325 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004326 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004327 if (!text) {
4328 delete_Token(t);
4329 } else {
4330 *tail = t;
4331 tail = &t->next;
4332 t->type = type;
4333 nasm_free(t->text);
4334 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004335 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004336 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004337 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004338 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004339 } else if (tline->type == TOK_INDIRECT) {
4340 t = tline;
4341 tline = tline->next;
4342 tt = tokenize(t->text);
4343 tt = expand_mmac_params(tt);
4344 tt = expand_smacro(tt);
4345 *tail = tt;
4346 while (tt) {
4347 tt->a.mac = NULL; /* Necessary? */
4348 tail = &tt->next;
4349 tt = tt->next;
4350 }
4351 delete_Token(t);
4352 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004353 } else {
4354 t = *tail = tline;
4355 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004356 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004357 tail = &t->next;
4358 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004359 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004360 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004361
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004362 if (changed) {
4363 const struct tokseq_match t[] = {
4364 {
4365 PP_CONCAT_MASK(TOK_ID) |
4366 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4367 PP_CONCAT_MASK(TOK_ID) |
4368 PP_CONCAT_MASK(TOK_NUMBER) |
4369 PP_CONCAT_MASK(TOK_FLOAT) |
4370 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4371 },
4372 {
4373 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4374 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4375 }
4376 };
4377 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4378 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004379
Cyrill Gorcunov2e046002011-06-26 23:33:56 +04004380 nasm_dump_token(thead);
4381
H. Peter Anvin76690a12002-04-30 20:52:49 +00004382 return thead;
4383}
4384
4385/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004386 * Expand all single-line macro calls made in the given line.
4387 * Return the expanded version of the line. The original is deemed
4388 * to be destroyed in the process. (In reality we'll just move
4389 * Tokens from input to output a lot of the time, rather than
4390 * actually bothering to destroy and replicate.)
4391 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004392
H. Peter Anvine2c80182005-01-15 22:15:51 +00004393static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004394{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004395 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004396 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004397 Token **params;
4398 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004399 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004400 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004401 Token *org_tline = tline;
4402 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004403 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004404 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004405 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004406
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004407 /*
4408 * Trick: we should avoid changing the start token pointer since it can
4409 * be contained in "next" field of other token. Because of this
4410 * we allocate a copy of first token and work with it; at the end of
4411 * routine we copy it back
4412 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004413 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004414 tline = new_Token(org_tline->next, org_tline->type,
4415 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004416 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004417 nasm_free(org_tline->text);
4418 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004419 }
4420
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004421 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004422
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004423again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004424 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004425 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004426
H. Peter Anvine2c80182005-01-15 22:15:51 +00004427 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004428 if (!--deadman) {
4429 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004430 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004431 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004432
H. Peter Anvine2c80182005-01-15 22:15:51 +00004433 if ((mname = tline->text)) {
4434 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004435 if (tline->type == TOK_ID) {
4436 head = (SMacro *)hash_findix(&smacros, mname);
4437 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04004438 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004439 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4440 } else
4441 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004442
H. Peter Anvine2c80182005-01-15 22:15:51 +00004443 /*
4444 * We've hit an identifier. As in is_mmacro below, we first
4445 * check whether the identifier is a single-line macro at
4446 * all, then think about checking for parameters if
4447 * necessary.
4448 */
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004449 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004450 if (!mstrcmp(m->name, mname, m->casesense))
4451 break;
4452 if (m) {
4453 mstart = tline;
4454 params = NULL;
4455 paramsize = NULL;
4456 if (m->nparam == 0) {
4457 /*
4458 * Simple case: the macro is parameterless. Discard the
4459 * one token that the macro call took, and push the
4460 * expansion back on the to-do stack.
4461 */
4462 if (!m->expansion) {
4463 if (!strcmp("__FILE__", m->name)) {
4464 int32_t num = 0;
4465 char *file = NULL;
4466 src_get(&num, &file);
4467 tline->text = nasm_quote(file, strlen(file));
4468 tline->type = TOK_STRING;
4469 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004470 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004471 }
4472 if (!strcmp("__LINE__", m->name)) {
4473 nasm_free(tline->text);
4474 make_tok_num(tline, src_get_linnum());
4475 continue;
4476 }
4477 if (!strcmp("__BITS__", m->name)) {
4478 nasm_free(tline->text);
4479 make_tok_num(tline, globalbits);
4480 continue;
4481 }
4482 tline = delete_Token(tline);
4483 continue;
4484 }
4485 } else {
4486 /*
4487 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004488 * exists and takes parameters. We must find the
4489 * parameters in the call, count them, find the SMacro
4490 * that corresponds to that form of the macro call, and
4491 * substitute for the parameters when we expand. What a
4492 * pain.
4493 */
4494 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004495 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004496 do {
4497 t = tline->next;
4498 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004499 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004500 t->text = NULL;
4501 t = tline->next = delete_Token(t);
4502 }
4503 tline = t;
4504 } while (tok_type_(tline, TOK_WHITESPACE));
4505 if (!tok_is_(tline, "(")) {
4506 /*
4507 * This macro wasn't called with parameters: ignore
4508 * the call. (Behaviour borrowed from gnu cpp.)
4509 */
4510 tline = mstart;
4511 m = NULL;
4512 } else {
4513 int paren = 0;
4514 int white = 0;
4515 brackets = 0;
4516 nparam = 0;
4517 sparam = PARAM_DELTA;
4518 params = nasm_malloc(sparam * sizeof(Token *));
4519 params[0] = tline->next;
4520 paramsize = nasm_malloc(sparam * sizeof(int));
4521 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004522 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004523 /*
4524 * For some unusual expansions
4525 * which concatenates function call
4526 */
4527 t = tline->next;
4528 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004529 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004530 t->text = NULL;
4531 t = tline->next = delete_Token(t);
4532 }
4533 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004534
H. Peter Anvine2c80182005-01-15 22:15:51 +00004535 if (!tline) {
4536 error(ERR_NONFATAL,
4537 "macro call expects terminating `)'");
4538 break;
4539 }
4540 if (tline->type == TOK_WHITESPACE
4541 && brackets <= 0) {
4542 if (paramsize[nparam])
4543 white++;
4544 else
4545 params[nparam] = tline->next;
4546 continue; /* parameter loop */
4547 }
4548 if (tline->type == TOK_OTHER
4549 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004550 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004551 if (ch == ',' && !paren && brackets <= 0) {
4552 if (++nparam >= sparam) {
4553 sparam += PARAM_DELTA;
4554 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004555 sparam * sizeof(Token *));
4556 paramsize = nasm_realloc(paramsize,
4557 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004558 }
4559 params[nparam] = tline->next;
4560 paramsize[nparam] = 0;
4561 white = 0;
4562 continue; /* parameter loop */
4563 }
4564 if (ch == '{' &&
4565 (brackets > 0 || (brackets == 0 &&
4566 !paramsize[nparam])))
4567 {
4568 if (!(brackets++)) {
4569 params[nparam] = tline->next;
4570 continue; /* parameter loop */
4571 }
4572 }
4573 if (ch == '}' && brackets > 0)
4574 if (--brackets == 0) {
4575 brackets = -1;
4576 continue; /* parameter loop */
4577 }
4578 if (ch == '(' && !brackets)
4579 paren++;
4580 if (ch == ')' && brackets <= 0)
4581 if (--paren < 0)
4582 break;
4583 }
4584 if (brackets < 0) {
4585 brackets = 0;
4586 error(ERR_NONFATAL, "braces do not "
4587 "enclose all of macro parameter");
4588 }
4589 paramsize[nparam] += white + 1;
4590 white = 0;
4591 } /* parameter loop */
4592 nparam++;
4593 while (m && (m->nparam != nparam ||
4594 mstrcmp(m->name, mname,
4595 m->casesense)))
4596 m = m->next;
4597 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004598 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004599 "macro `%s' exists, "
4600 "but not taking %d parameters",
4601 mstart->text, nparam);
4602 }
4603 }
4604 if (m && m->in_progress)
4605 m = NULL;
4606 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004607 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004608 * Design question: should we handle !tline, which
4609 * indicates missing ')' here, or expand those
4610 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004611 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004612 */
4613 nasm_free(params);
4614 nasm_free(paramsize);
4615 tline = mstart;
4616 } else {
4617 /*
4618 * Expand the macro: we are placed on the last token of the
4619 * call, so that we can easily split the call from the
4620 * following tokens. We also start by pushing an SMAC_END
4621 * token for the cycle removal.
4622 */
4623 t = tline;
4624 if (t) {
4625 tline = t->next;
4626 t->next = NULL;
4627 }
4628 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004629 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004630 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004631 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004632 list_for_each(t, m->expansion) {
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04004633 if (is_smacro_param(t)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004634 Token *pcopy = tline, **ptail = &pcopy;
Cyrill Gorcunov0ad6a7b2011-06-30 01:36:45 +04004635 Token *ttt;
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04004636 int i, idx;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004637
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04004638 idx = smacro_get_param_idx(t);
4639 ttt = params[idx];
Cyrill Gorcunov0ad6a7b2011-06-30 01:36:45 +04004640
4641 /*
4642 * We need smacro paramters appended.
4643 */
4644 for (i = paramsize[idx]; i > 0; i--) {
4645 *ptail = new_Token(tline, ttt->type, ttt->text, 0);
4646 ptail = &(*ptail)->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004647 ttt = ttt->next;
4648 }
Cyrill Gorcunov0ad6a7b2011-06-30 01:36:45 +04004649
H. Peter Anvine2c80182005-01-15 22:15:51 +00004650 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004651 } else if (t->type == TOK_PREPROC_Q) {
4652 tt = new_Token(tline, TOK_ID, mname, 0);
4653 tline = tt;
4654 } else if (t->type == TOK_PREPROC_QQ) {
4655 tt = new_Token(tline, TOK_ID, m->name, 0);
4656 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004657 } else {
4658 tt = new_Token(tline, t->type, t->text, 0);
4659 tline = tt;
4660 }
4661 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004662
H. Peter Anvine2c80182005-01-15 22:15:51 +00004663 /*
4664 * Having done that, get rid of the macro call, and clean
4665 * up the parameters.
4666 */
4667 nasm_free(params);
4668 nasm_free(paramsize);
4669 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004670 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004671 continue; /* main token loop */
4672 }
4673 }
4674 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004675
H. Peter Anvine2c80182005-01-15 22:15:51 +00004676 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004677 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004678 tline = delete_Token(tline);
4679 } else {
4680 t = *tail = tline;
4681 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004682 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004683 t->next = NULL;
4684 tail = &t->next;
4685 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004686 }
4687
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004688 /*
4689 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004690 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004691 * TOK_IDs should be concatenated.
4692 * Also we look for %+ tokens and concatenate the tokens before and after
4693 * them (without white spaces in between).
4694 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004695 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004696 const struct tokseq_match t[] = {
4697 {
4698 PP_CONCAT_MASK(TOK_ID) |
4699 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4700 PP_CONCAT_MASK(TOK_ID) |
4701 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4702 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4703 }
4704 };
4705 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004706 /*
4707 * If we concatenated something, *and* we had previously expanded
4708 * an actual macro, scan the lines again for macros...
4709 */
4710 tline = thead;
4711 expanded = false;
4712 goto again;
4713 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004714 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004715
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004716err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004717 if (org_tline) {
4718 if (thead) {
4719 *org_tline = *thead;
4720 /* since we just gave text to org_line, don't free it */
4721 thead->text = NULL;
4722 delete_Token(thead);
4723 } else {
4724 /* the expression expanded to empty line;
4725 we can't return NULL for some reasons
4726 we just set the line to a single WHITESPACE token. */
4727 memset(org_tline, 0, sizeof(*org_tline));
4728 org_tline->text = NULL;
4729 org_tline->type = TOK_WHITESPACE;
4730 }
4731 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004732 }
4733
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004734 return thead;
4735}
4736
4737/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004738 * Similar to expand_smacro but used exclusively with macro identifiers
4739 * right before they are fetched in. The reason is that there can be
4740 * identifiers consisting of several subparts. We consider that if there
4741 * are more than one element forming the name, user wants a expansion,
4742 * otherwise it will be left as-is. Example:
4743 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004744 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004745 *
4746 * the identifier %$abc will be left as-is so that the handler for %define
4747 * will suck it and define the corresponding value. Other case:
4748 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004749 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004750 *
4751 * In this case user wants name to be expanded *before* %define starts
4752 * working, so we'll expand %$abc into something (if it has a value;
4753 * otherwise it will be left as-is) then concatenate all successive
4754 * PP_IDs into one.
4755 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004756static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004757{
4758 Token *cur, *oldnext = NULL;
4759
H. Peter Anvin734b1882002-04-30 21:01:08 +00004760 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004761 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004762
4763 cur = tline;
4764 while (cur->next &&
Cyrill Gorcunov5b6c96b2011-06-30 00:22:53 +04004765 (cur->next->type == TOK_ID ||
4766 cur->next->type == TOK_PREPROC_ID ||
4767 cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004768 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004769
4770 /* If identifier consists of just one token, don't expand */
4771 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004772 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004773
H. Peter Anvine2c80182005-01-15 22:15:51 +00004774 if (cur) {
4775 oldnext = cur->next; /* Detach the tail past identifier */
4776 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004777 }
4778
H. Peter Anvin734b1882002-04-30 21:01:08 +00004779 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004780
H. Peter Anvine2c80182005-01-15 22:15:51 +00004781 if (cur) {
4782 /* expand_smacro possibly changhed tline; re-scan for EOL */
4783 cur = tline;
4784 while (cur && cur->next)
4785 cur = cur->next;
4786 if (cur)
4787 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004788 }
4789
4790 return tline;
4791}
4792
4793/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004794 * Determine whether the given line constitutes a multi-line macro
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004795 * call, and return the ExpDef structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004796 * to check for an initial label - that's taken care of in
4797 * expand_mmacro - but must check numbers of parameters. Guaranteed
4798 * to be called with tline->type == TOK_ID, so the putative macro
4799 * name is easy to find.
4800 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004801static ExpDef *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004802{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004803 ExpDef *head, *ed;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004804 Token **params;
4805 int nparam;
4806
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004807 head = (ExpDef *) hash_findix(&expdefs, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004808
4809 /*
4810 * Efficiency: first we see if any macro exists with the given
4811 * name. If not, we can return NULL immediately. _Then_ we
4812 * count the parameters, and then we look further along the
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004813 * list if necessary to find the proper ExpDef.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004814 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004815 list_for_each(ed, head)
4816 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004817 break;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004818 if (!ed)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004819 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004820
4821 /*
4822 * OK, we have a potential macro. Count and demarcate the
4823 * parameters.
4824 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004825 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004826
4827 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004828 * So we know how many parameters we've got. Find the ExpDef
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004829 * structure that handles this number.
4830 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004831 while (ed) {
4832 if (ed->nparam_min <= nparam
4833 && (ed->plus || nparam <= ed->nparam_max)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004834 /*
4835 * It's right, and we can use it. Add its default
4836 * parameters to the end of our list if necessary.
4837 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004838 if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004839 params =
4840 nasm_realloc(params,
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004841 ((ed->nparam_min + ed->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004842 1) * sizeof(*params)));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004843 while (nparam < ed->nparam_min + ed->ndefs) {
4844 params[nparam] = ed->defaults[nparam - ed->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004845 nparam++;
4846 }
4847 }
4848 /*
4849 * If we've gone over the maximum parameter count (and
4850 * we're in Plus mode), ignore parameters beyond
4851 * nparam_max.
4852 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004853 if (ed->plus && nparam > ed->nparam_max)
4854 nparam = ed->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004855 /*
4856 * Then terminate the parameter list, and leave.
4857 */
4858 if (!params) { /* need this special case */
4859 params = nasm_malloc(sizeof(*params));
4860 nparam = 0;
4861 }
4862 params[nparam] = NULL;
4863 *params_array = params;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004864 return ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004865 }
4866 /*
4867 * This one wasn't right: look for the next one with the
4868 * same name.
4869 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004870 list_for_each(ed, ed->next)
4871 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004872 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004873 }
4874
4875 /*
4876 * After all that, we didn't find one with the right number of
4877 * parameters. Issue a warning, and fail to expand the macro.
4878 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004879 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004880 "macro `%s' exists, but not taking %d parameters",
4881 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004882 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004883 return NULL;
4884}
4885
4886/*
4887 * Expand the multi-line macro call made by the given line, if
4888 * there is one to be expanded. If there is, push the expansion on
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004889 * istk->expansion and return true. Otherwise return false.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004890 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004891static bool expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004892{
H. Peter Anvineba20a72002-04-30 20:53:55 +00004893 Token *label = NULL;
4894 int dont_prepend = 0;
Cyrill Gorcunovfb27fc22011-06-25 12:08:30 +04004895 Token **params, *t;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004896 Line *l = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004897 ExpDef *ed;
4898 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004899 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004900 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004901
4902 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004903 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004904 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004905 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004906 return false;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004907 ed = is_mmacro(t, &params);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004908 if (ed != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004909 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004910 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004911 Token *last;
4912 /*
4913 * We have an id which isn't a macro call. We'll assume
4914 * it might be a label; we'll also check to see if a
4915 * colon follows it. Then, if there's another id after
4916 * that lot, we'll check it again for macro-hood.
4917 */
4918 label = last = t;
4919 t = t->next;
4920 if (tok_type_(t, TOK_WHITESPACE))
4921 last = t, t = t->next;
4922 if (tok_is_(t, ":")) {
4923 dont_prepend = 1;
4924 last = t, t = t->next;
4925 if (tok_type_(t, TOK_WHITESPACE))
4926 last = t, t = t->next;
4927 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004928 if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, &params)))
4929 return false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004930 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004931 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004932 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004933 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004934
4935 /*
4936 * Fix up the parameters: this involves stripping leading and
4937 * trailing whitespace, then stripping braces if they are
4938 * present.
4939 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004940 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004941 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004942
H. Peter Anvine2c80182005-01-15 22:15:51 +00004943 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004944 int brace = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004945 int comma = (!ed->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004946
H. Peter Anvine2c80182005-01-15 22:15:51 +00004947 t = params[i];
4948 skip_white_(t);
4949 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004950 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004951 params[i] = t;
4952 paramlen[i] = 0;
4953 while (t) {
4954 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4955 break; /* ... because we have hit a comma */
4956 if (comma && t->type == TOK_WHITESPACE
4957 && tok_is_(t->next, ","))
4958 break; /* ... or a space then a comma */
4959 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4960 break; /* ... or a brace */
4961 t = t->next;
4962 paramlen[i]++;
4963 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004964 }
4965
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004966 if (ed->cur_depth >= ed->max_depth) {
4967 if (ed->max_depth > 1) {
4968 error(ERR_WARNING,
4969 "reached maximum macro recursion depth of %i for %s",
4970 ed->max_depth,ed->name);
4971 }
4972 return false;
4973 } else {
4974 ed->cur_depth ++;
4975 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004976
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004977 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004978 * OK, we have found a ExpDef structure representing a
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004979 * previously defined mmacro. Create an expansion invocation
4980 * and point it back to the expansion definition. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004981 * parameter tokens and macro-local tokens doesn't get done
4982 * until the single-line macro substitution process; this is
4983 * because delaying them allows us to change the semantics
4984 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004985 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004986 ei = new_ExpInv(EXP_MMACRO, ed);
4987 ei->name = nasm_strdup(mname);
4988 //ei->label = label;
4989 //ei->label_text = detoken(label, false);
4990 ei->current = ed->line;
4991 ei->emitting = true;
4992 //ei->iline = tline;
4993 ei->params = params;
4994 ei->nparam = nparam;
4995 ei->rotate = 0;
4996 ei->paramlen = paramlen;
4997 ei->lineno = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004998
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004999 ei->prev = istk->expansion;
5000 istk->expansion = ei;
5001
5002 /*
5003 * Special case: detect %00 on first invocation; if found,
5004 * avoid emitting any labels that precede the mmacro call.
5005 * ed->prepend is set to -1 when %00 is detected, else 1.
5006 */
5007 if (ed->prepend == 0) {
5008 for (l = ed->line; l != NULL; l = l->next) {
5009 for (t = l->first; t != NULL; t = t->next) {
5010 if ((t->type == TOK_PREPROC_ID) &&
5011 (strlen(t->text) == 3) &&
5012 (t->text[1] == '0') && (t->text[2] == '0')) {
5013 dont_prepend = -1;
5014 break;
5015 }
5016 }
5017 if (dont_prepend < 0) {
5018 break;
5019 }
5020 }
5021 ed->prepend = ((dont_prepend < 0) ? -1 : 1);
5022 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005023
5024 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00005025 * If we had a label, push it on as the first line of
5026 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005027 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005028 if (label != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005029 if (ed->prepend < 0) {
5030 ei->label_text = detoken(label, false);
5031 } else {
5032 if (dont_prepend == 0) {
5033 t = label;
5034 while (t->next != NULL) {
5035 t = t->next;
5036 }
5037 t->next = new_Token(NULL, TOK_OTHER, ":", 0);
5038 }
5039 l = new_Line();
5040 l->first = copy_Token(label);
5041 l->next = ei->current;
5042 ei->current = l;
5043 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005044 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005045
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005046 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005047
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005048 istk->mmac_depth++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005049 return true;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005050}
5051
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005052/* The function that actually does the error reporting */
5053static void verror(int severity, const char *fmt, va_list arg)
5054{
5055 char buff[1024];
5056
5057 vsnprintf(buff, sizeof(buff), fmt, arg);
5058
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005059 if (istk && istk->mmac_depth > 0) {
5060 ExpInv *ei = istk->expansion;
5061 int lineno = ei->lineno;
5062 while (ei) {
5063 if (ei->type == EXP_MMACRO)
5064 break;
5065 lineno += ei->relno;
5066 ei = ei->prev;
5067 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005068 nasm_error(severity, "(%s:%d) %s", ei->def->name,
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005069 lineno, buff);
5070 } else
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005071 nasm_error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005072}
5073
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005074/*
5075 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07005076 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005077 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005078static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005079{
5080 va_list arg;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005081 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005082 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00005083 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005084}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005085
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005086/*
5087 * Because %else etc are evaluated in the state context
5088 * of the previous branch, errors might get lost with error():
5089 * %if 0 ... %else trailing garbage ... %endif
5090 * So %else etc should report errors with this function.
5091 */
5092static void error_precond(int severity, const char *fmt, ...)
5093{
5094 va_list arg;
5095
5096 /* Only ignore the error if it's really in a dead branch */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005097 if ((istk != NULL) &&
5098 (istk->expansion != NULL) &&
5099 (istk->expansion->type == EXP_IF) &&
5100 (istk->expansion->def->state == COND_NEVER))
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005101 return;
5102
5103 va_start(arg, fmt);
5104 verror(severity, fmt, arg);
5105 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005106}
5107
H. Peter Anvin734b1882002-04-30 21:01:08 +00005108static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005109pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005110{
H. Peter Anvin7383b402008-09-24 10:20:40 -07005111 Token *t;
5112
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005113 cstk = NULL;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005114 istk = nasm_zalloc(sizeof(Include));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005115 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00005116 src_set_fname(nasm_strdup(file));
5117 src_set_linnum(0);
5118 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005119 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07005120 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00005121 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005122 defining = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005123 finals = NULL;
5124 in_final = false;
Charles Crayned4200be2008-07-12 16:42:33 -07005125 nested_mac_count = 0;
5126 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005127 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005128 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005129 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005130 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005131 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07005132 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005133 }
H. Peter Anvind2456592008-06-19 15:04:18 -07005134 any_extrastdmac = extrastdmac && *extrastdmac;
5135 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005136 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005137
5138 /*
5139 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5140 * The caller, however, will also pass in 3 for preprocess-only so
5141 * we can set __PASS__ accordingly.
5142 */
5143 pass = apass > 2 ? 2 : apass;
5144
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005145 dephead = deptail = deplist;
5146 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005147 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
5148 sl->next = NULL;
5149 strcpy(sl->str, file);
5150 *deptail = sl;
5151 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005152 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07005153
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005154 /*
5155 * Define the __PASS__ macro. This is defined here unlike
5156 * all the other builtins, because it is special -- it varies between
5157 * passes.
5158 */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005159 t = nasm_zalloc(sizeof(*t));
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005160 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07005161 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005162}
5163
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005164static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005165{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005166 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005167 Token *tline;
Keith Kanios9858cec2010-11-08 00:58:02 -06005168 ExpDef *ed;
5169 ExpInv *ei;
5170 Line *l;
5171 int j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005172
H. Peter Anvine2c80182005-01-15 22:15:51 +00005173 while (1) {
5174 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005175 * Fetch a tokenized line, either from the expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005176 * buffer or from the input file.
5177 */
5178 tline = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005179
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005180 while (1) { /* until we get a line we can use */
5181 /*
5182 * Fetch a tokenized line from the expansion buffer
5183 */
5184 if (istk->expansion != NULL) {
5185 ei = istk->expansion;
5186 if (ei->current != NULL) {
5187 if (ei->emitting == false) {
5188 ei->current = NULL;
5189 continue;
5190 }
5191 l = ei->current;
5192 ei->current = l->next;
5193 ei->lineno++;
5194 tline = copy_Token(l->first);
5195 if (((ei->type == EXP_REP) ||
5196 (ei->type == EXP_MMACRO) ||
5197 (ei->type == EXP_WHILE))
5198 && (ei->def->nolist == false)) {
5199 char *p = detoken(tline, false);
5200 list->line(LIST_MACRO, p);
5201 nasm_free(p);
5202 }
5203 if (ei->linnum > -1) {
5204 src_set_linnum(src_get_linnum() + 1);
5205 }
5206 break;
5207 } else if ((ei->type == EXP_REP) &&
5208 (ei->def->cur_depth < ei->def->max_depth)) {
5209 ei->def->cur_depth ++;
5210 ei->current = ei->def->line;
5211 ei->lineno = 0;
5212 continue;
5213 } else if ((ei->type == EXP_WHILE) &&
5214 (ei->def->cur_depth < ei->def->max_depth)) {
5215 ei->current = ei->def->line;
5216 ei->lineno = 0;
5217 tline = copy_Token(ei->current->first);
5218 j = if_condition(tline, PP_WHILE);
5219 tline = NULL;
5220 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
5221 if (j == COND_IF_TRUE) {
5222 ei->current = ei->current->next;
5223 ei->def->cur_depth ++;
5224 } else {
5225 ei->emitting = false;
5226 ei->current = NULL;
5227 ei->def->cur_depth = ei->def->max_depth;
5228 }
5229 continue;
5230 } else {
5231 istk->expansion = ei->prev;
5232 ed = ei->def;
5233 if (ed != NULL) {
5234 if ((ei->emitting == true) &&
5235 (ed->max_depth == DEADMAN_LIMIT) &&
5236 (ed->cur_depth == DEADMAN_LIMIT)
5237 ) {
5238 error(ERR_FATAL, "runaway expansion detected, aborting");
5239 }
5240 if (ed->cur_depth > 0) {
5241 ed->cur_depth --;
Keith Kanios94124652010-12-18 11:47:28 -06005242 } else if (ed->type != EXP_MMACRO) {
Keith Kanios6a7c3e92010-12-18 11:49:53 -06005243 expansions = ed->prev;
5244 free_expdef(ed);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005245 }
5246 if ((ei->type == EXP_REP) ||
5247 (ei->type == EXP_MMACRO) ||
5248 (ei->type == EXP_WHILE)) {
5249 list->downlevel(LIST_MACRO);
5250 if (ei->type == EXP_MMACRO) {
5251 istk->mmac_depth--;
5252 }
5253 }
5254 }
5255 if (ei->linnum > -1) {
5256 src_set_linnum(ei->linnum);
5257 }
5258 free_expinv(ei);
5259 continue;
5260 }
5261 }
5262
5263 /*
5264 * Read in line from input and tokenize
5265 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005266 line = read_line();
5267 if (line) { /* from the current input file */
5268 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005269 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005270 nasm_free(line);
5271 break;
5272 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005273
H. Peter Anvine2c80182005-01-15 22:15:51 +00005274 /*
5275 * The current file has ended; work down the istk
5276 */
5277 {
5278 Include *i = istk;
5279 fclose(i->fp);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005280 if (i->expansion != NULL) {
5281 error(ERR_FATAL,
5282 "end of file while still in an expansion");
5283 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005284 /* only set line and file name if there's a next node */
5285 if (i->next) {
5286 src_set_linnum(i->lineno);
Cyrill Gorcunovd34a1082011-03-07 11:23:08 +03005287 nasm_free(src_set_fname(nasm_strdup(i->fname)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005288 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005289 if ((i->next == NULL) && (finals != NULL)) {
5290 in_final = true;
5291 ei = new_ExpInv(EXP_FINAL, NULL);
5292 ei->emitting = true;
5293 ei->current = finals;
5294 istk->expansion = ei;
5295 finals = NULL;
5296 continue;
5297 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005298 istk = i->next;
5299 list->downlevel(LIST_INCLUDE);
5300 nasm_free(i);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005301 if (istk == NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005302 if (finals != NULL) {
5303 in_final = true;
5304 } else {
5305 return NULL;
5306 }
5307 }
5308 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005309 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005310 }
5311
5312 if (defining == NULL) {
5313 tline = expand_mmac_params(tline);
5314 }
5315
H. Peter Anvine2c80182005-01-15 22:15:51 +00005316 /*
5317 * Check the line to see if it's a preprocessor directive.
5318 */
5319 if (do_directive(tline) == DIRECTIVE_FOUND) {
5320 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005321 } else if (defining != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005322 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005323 * We're defining an expansion. We emit nothing at all,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005324 * and just shove the tokenized line on to the definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005325 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005326 if (defining->ignoring == false) {
5327 Line *l = new_Line();
5328 l->first = tline;
5329 if (defining->line == NULL) {
5330 defining->line = l;
5331 defining->last = l;
5332 } else {
5333 defining->last->next = l;
5334 defining->last = l;
5335 }
5336 } else {
Keith Kanios104803d2010-12-18 11:05:46 -06005337 free_tlist(tline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005338 }
5339 defining->linecount++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005340 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005341 } else if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005342 (istk->expansion->emitting != true)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005343 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005344 * We're in a non-emitting branch of an expansion.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005345 * Emit nothing at all, not even a blank line: when we
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005346 * emerge from the expansion we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005347 * directive so we keep our place correctly.
5348 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005349 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005350 continue;
5351 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005352 tline = expand_smacro(tline);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005353 if (expand_mmacro(tline) != true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005354 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005355 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005356 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005357 line = detoken(tline, true);
5358 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005359 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005360 } else {
5361 continue;
5362 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005363 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005364 }
5365 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005366}
5367
H. Peter Anvine2c80182005-01-15 22:15:51 +00005368static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005369{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005370 if (defining != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005371 error(ERR_NONFATAL, "end of file while still defining an expansion");
5372 while (defining != NULL) {
5373 ExpDef *ed = defining;
5374 defining = ed->prev;
5375 free_expdef(ed);
5376 }
5377 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005378 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005379 while (cstk != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005380 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005381 free_macros();
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005382 while (istk != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005383 Include *i = istk;
5384 istk = istk->next;
5385 fclose(i->fp);
5386 nasm_free(i->fname);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005387 while (i->expansion != NULL) {
5388 ExpInv *ei = i->expansion;
5389 i->expansion = ei->prev;
5390 free_expinv(ei);
5391 }
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005392 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005393 }
5394 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005395 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005396 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005397 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005398 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005399 free_llist(predef);
5400 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005401 while ((i = ipath)) {
5402 ipath = i->next;
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04005403 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005404 nasm_free(i);
5405 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005406 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005407}
5408
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005409void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005410{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005411 IncPath *i = nasm_zalloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005412
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005413 if (path)
5414 i->path = nasm_strdup(path);
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005415
H. Peter Anvin89cee572009-07-15 09:16:54 -04005416 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005417 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005418 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005419 j = j->next;
5420 j->next = i;
5421 } else {
5422 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005423 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005424}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005425
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005426void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005427{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005428 Token *inc, *space, *name;
5429 Line *l;
5430
H. Peter Anvin734b1882002-04-30 21:01:08 +00005431 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5432 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5433 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005434
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005435 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005436 l->next = predef;
5437 l->first = inc;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005438 predef = l;
5439}
5440
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005441void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005442{
5443 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005444 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005445 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005446
5447 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005448 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5449 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005450 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005451 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005452 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005453 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005454 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005455
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005456 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005457 l->next = predef;
5458 l->first = def;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005459 predef = l;
5460}
5461
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005462void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005463{
5464 Token *def, *space;
5465 Line *l;
5466
H. Peter Anvin734b1882002-04-30 21:01:08 +00005467 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5468 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005469 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005470
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005471 l = new_Line();
H. Peter Anvin620515a2002-04-30 20:57:38 +00005472 l->next = predef;
5473 l->first = def;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005474 predef = l;
5475}
5476
Keith Kaniosb7a89542007-04-12 02:40:54 +00005477/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005478 * This function is used to assist with "runtime" preprocessor
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005479 * directives, e.g. pp_runtime("%define __BITS__ 64");
Keith Kaniosb7a89542007-04-12 02:40:54 +00005480 *
5481 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5482 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5483 */
5484
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005485void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005486{
5487 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005488
Keith Kaniosb7a89542007-04-12 02:40:54 +00005489 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005490 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005491 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005492
Keith Kaniosb7a89542007-04-12 02:40:54 +00005493}
5494
H. Peter Anvina70547f2008-07-19 21:44:26 -07005495void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005496{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005497 extrastdmac = macros;
5498}
5499
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005500static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005501{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005502 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005503 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005504 tok->text = nasm_strdup(numbuf);
5505 tok->type = TOK_NUMBER;
5506}
5507
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +04005508struct preproc_ops nasmpp = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005509 pp_reset,
5510 pp_getline,
5511 pp_cleanup
5512};