blob: 8c4b173ce0e587093280dab2875ef93d0538a27e [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;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000092typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000093
94/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070095 * Note on the storage of both SMacro and MMacros: the hash table
96 * indexes them case-insensitively, and we then have to go through a
97 * linked list of potential case aliases (and, for MMacros, parameter
98 * ranges); this is to preserve the matching semantics of the earlier
99 * code. If the number of case aliases for a specific macro is a
100 * performance issue, you may want to reconsider your coding style.
101 */
102
103/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000104 * Store the definition of a single-line macro.
105 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000106struct SMacro {
Cyrill Gorcunov10083ae2011-07-17 20:06:20 +0400107 SMacro *next;
108 char *name;
109 Token *expansion;
110 unsigned int nparam;
111 bool casesense;
112 bool in_progress;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000113};
114
115/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000116 * The context stack is composed of a linked list of these.
117 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000118struct Context {
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400119 Context *next;
120 char *name;
121 struct hash_table localmac;
122 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000123};
124
125/*
126 * This is the internal form which we break input lines up into.
127 * Typically stored in linked lists.
128 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000129 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
130 * necessarily used as-is, but is intended to denote the number of
131 * the substituted parameter. So in the definition
132 *
133 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700134 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000135 * the token representing `x' will have its type changed to
136 * TOK_SMAC_PARAM, but the one representing `y' will be
137 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000138 *
139 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
140 * which doesn't need quotes around it. Used in the pre-include
141 * mechanism as an alternative to trying to find a sensible type of
142 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000143 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000144enum pp_token_type {
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +0400145 TOK_NONE = 0,
146 TOK_WHITESPACE,
147 TOK_COMMENT,
148 TOK_ID,
149 TOK_PREPROC_ID,
150 TOK_STRING,
151 TOK_NUMBER,
152 TOK_FLOAT,
153 TOK_SMAC_END,
154 TOK_OTHER,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700155 TOK_INTERNAL_STRING,
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +0400156 TOK_PREPROC_Q,
157 TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300158 TOK_PASTE, /* %+ */
159 TOK_INDIRECT, /* %[...] */
160 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
161 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000162};
163
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400164#define PP_CONCAT_MASK(x) (1 << (x))
165
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400166struct tokseq_match {
167 int mask_head;
168 int mask_tail;
169};
170
H. Peter Anvine2c80182005-01-15 22:15:51 +0000171struct Token {
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400172 Token *next;
173 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700174 union {
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400175 SMacro *mac; /* associated macro for TOK_SMAC_END */
176 size_t len; /* scratch length field */
177 } a; /* Auxiliary data */
178 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000179};
180
181/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500182 * Expansion definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000183 * these, which is essentially a container to allow several linked
184 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700185 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000186 * Note that in this module, linked lists are treated as stacks
187 * wherever possible. For this reason, Lines are _pushed_ on to the
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500188 * `last' field in ExpDef structures, so that the linked list,
189 * if walked, would emit the expansion lines in the proper order.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000190 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000191struct Line {
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400192 Line *next;
193 Token *first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000194};
195
196/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500197 * Expansion Types
198 */
199enum pp_exp_type {
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +0400200 EXP_NONE = 0,
201 EXP_PREDEF,
202 EXP_MMACRO,
203 EXP_REP,
204 EXP_IF,
205 EXP_WHILE,
206 EXP_COMMENT,
207 EXP_FINAL,
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500208 EXP_MAX = INT_MAX /* Keep compiler from reducing the range */
209};
210
211/*
212 * Store the definition of an expansion, in which is any
213 * preprocessor directive that has an ending pair.
214 *
215 * This design allows for arbitrary expansion/recursion depth,
216 * upto the DEADMAN_LIMIT.
217 *
218 * The `next' field is used for storing ExpDef in hash tables; the
219 * `prev' field is for the global `expansions` linked-list.
220 */
221struct ExpDef {
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400222 ExpDef *prev; /* previous definition */
223 ExpDef *next; /* next in hash table */
224 enum pp_exp_type type; /* expansion type */
225 char *name; /* definition name */
226 int nparam_min;
227 int nparam_max;
228 bool casesense;
229 bool plus; /* is the last parameter greedy? */
230 bool nolist; /* is this expansion listing-inhibited? */
231 Token *dlist; /* all defaults as one list */
232 Token **defaults; /* parameter default pointers */
233 int ndefs; /* number of default parameters */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300234
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400235 int prepend; /* label prepend state */
236 Line *label;
237 Line *line;
238 Line *last;
239 int linecount; /* number of lines within expansion */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300240
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400241 int64_t def_depth; /* current number of definition pairs deep */
242 int64_t cur_depth; /* current number of expansions */
243 int64_t max_depth; /* maximum number of expansions allowed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300244
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400245 int state; /* condition state */
246 bool ignoring; /* ignoring definition lines */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500247};
248
249/*
250 * Store the invocation of an expansion.
251 *
252 * The `prev' field is for the `istk->expansion` linked-list.
253 *
254 * When an expansion is being expanded, `params', `iline', `nparam',
255 * `paramlen', `rotate' and `unique' are local to the invocation.
256 */
257struct ExpInv {
Cyrill Gorcunovd57a0312011-07-17 20:11:08 +0400258 ExpInv *prev; /* previous invocation */
259 ExpDef *def; /* pointer to expansion definition */
260 char *name; /* invocation name */
261 Line *label; /* pointer to label */
262 char *label_text; /* pointer to label text */
263 Line *current; /* pointer to current line in invocation */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300264
Cyrill Gorcunovd57a0312011-07-17 20:11:08 +0400265 Token **params; /* actual parameters */
266 Token *iline; /* invocation line */
267 int *paramlen;
268 unsigned int nparam;
269 unsigned int rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300270
Cyrill Gorcunovd57a0312011-07-17 20:11:08 +0400271 uint64_t unique;
272 int lineno; /* current line number in expansion */
273 int linnum; /* line number at invocation */
274 int relno; /* relative line number at invocation */
275 enum pp_exp_type type; /* expansion type */
276 bool emitting;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500277};
278
279/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000280 * To handle an arbitrary level of file inclusion, we maintain a
281 * stack (ie linked list) of these things.
282 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000283struct Include {
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400284 Include *next;
285 FILE *fp;
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400286 ExpInv *expansion;
287 char *fname;
288 int lineno;
289 int lineinc;
290 int mmac_depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000291};
292
293/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000294 * Include search path. This is simply a list of strings which get
295 * prepended, in turn, to the name of an include file, in an
296 * attempt to find the file if it's not in the current directory.
297 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000298struct IncPath {
Cyrill Gorcunovf30cf732011-07-17 20:20:14 +0400299 IncPath *next;
300 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000301};
302
303/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000304 * Conditional assembly: we maintain a separate stack of these for
305 * each level of file inclusion. (The only reason we keep the
306 * stacks separate is to ensure that a stray `%endif' in a file
307 * included from within the true branch of a `%if' won't terminate
308 * it and cause confusion: instead, rightly, it'll cause an error.)
309 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000310enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000311 /*
312 * These states are for use just after %if or %elif: IF_TRUE
313 * means the condition has evaluated to truth so we are
314 * currently emitting, whereas IF_FALSE means we are not
315 * currently emitting but will start doing so if a %else comes
316 * up. In these states, all directives are admissible: %elif,
317 * %else and %endif. (And of course %if.)
318 */
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +0400319 COND_IF_TRUE,
320 COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000321 /*
322 * These states come up after a %else: ELSE_TRUE means we're
323 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
324 * any %elif or %else will cause an error.
325 */
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +0400326 COND_ELSE_TRUE,
327 COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000328 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200329 * These states mean that we're not emitting now, and also that
330 * nothing until %endif will be emitted at all. COND_DONE is
331 * used when we've had our moment of emission
332 * and have now started seeing %elifs. COND_NEVER is used when
333 * the condition construct in question is contained within a
334 * non-emitting branch of a larger condition construct,
335 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000336 */
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +0400337 COND_DONE,
338 COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000339};
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000340
H. Peter Anvin70653092007-10-19 14:42:29 -0700341/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000342 * These defines are used as the possible return values for do_directive
343 */
344#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300345#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000346
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000347/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500348 * This define sets the upper limit for smacro and expansions
Keith Kanios852f1ee2009-07-12 00:19:55 -0500349 */
350#define DEADMAN_LIMIT (1 << 20)
351
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400352/* max reps */
353#define REP_LIMIT ((INT64_C(1) << 62))
354
Keith Kanios852f1ee2009-07-12 00:19:55 -0500355/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000356 * Condition codes. Note that we use c_ prefix not C_ because C_ is
357 * used in nasm.h for the "real" condition codes. At _this_ level,
358 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
359 * ones, so we need a different enum...
360 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700361static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000362 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
363 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000364 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000365};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700366enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000367 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
368 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 -0700369 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
370 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000371};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700372static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000373 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
374 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 +0000375 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000376};
377
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000378/* For TASM compatibility we need to be able to recognise TASM compatible
379 * conditional compilation directives. Using the NASM pre-processor does
380 * not work, so we look for them specifically from the following list and
381 * then jam in the equivalent NASM directive into the input stream.
382 */
383
H. Peter Anvine2c80182005-01-15 22:15:51 +0000384enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000385 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
386 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
387};
388
H. Peter Anvin476d2862007-10-02 22:04:15 -0700389static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000390 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
391 "ifndef", "include", "local"
392};
393
394static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000395static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000396static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800397static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000398
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000399static Context *cstk;
400static Include *istk;
Cyrill Gorcunov5c607762011-10-09 19:04:14 +0400401static IncPath *ipath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000402
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300403static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700404static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000405
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300406static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000407
Cyrill Gorcunov5c607762011-10-09 19:04:14 +0400408static Line *predef;
H. Peter Anvind2456592008-06-19 15:04:18 -0700409static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000410
411static ListGen *list;
412
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000413/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500414 * The current set of expansion definitions we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000415 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500416static struct hash_table expdefs;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000417
418/*
419 * The current set of single-line macros we have defined.
420 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700421static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000422
423/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500424 * Linked List of all active expansion definitions
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000425 */
Cyrill Gorcunov5c607762011-10-09 19:04:14 +0400426struct ExpDef *expansions;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500427
428/*
429 * The expansion we are currently defining
430 */
Cyrill Gorcunov5c607762011-10-09 19:04:14 +0400431static ExpDef *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000432
Charles Crayned4200be2008-07-12 16:42:33 -0700433static uint64_t nested_mac_count;
434static uint64_t nested_rep_count;
435
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000436/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500437 * Linked-list of lines to preprocess, prior to cleanup
438 */
Cyrill Gorcunov5c607762011-10-09 19:04:14 +0400439static Line *finals;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500440static bool in_final = false;
441
442/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000443 * The number of macro parameters to allocate space for at a time.
444 */
445#define PARAM_DELTA 16
446
447/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700448 * The standard macro set: defined in macros.c in the array nasm_stdmac.
449 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000450 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700451static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000452
453/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000454 * The extra standard macros that come from the object format, if
455 * any.
456 */
Cyrill Gorcunov5c607762011-10-09 19:04:14 +0400457static macros_t *extrastdmac;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700458static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000459
460/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000461 * Tokens are allocated in blocks to improve speed
462 */
463#define TOKEN_BLOCKSIZE 4096
Cyrill Gorcunov5c607762011-10-09 19:04:14 +0400464static Token *freeTokens;
H. Peter Anvince616072002-04-30 21:02:23 +0000465struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000466 Blocks *next;
467 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000468};
469
Cyrill Gorcunov5c607762011-10-09 19:04:14 +0400470static Blocks blocks;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000471
472/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000473 * Forward declarations.
474 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000475static Token *expand_mmac_params(Token * tline);
476static Token *expand_smacro(Token * tline);
477static Token *expand_id(Token * tline);
Cyrill Gorcunov290eac72011-06-28 01:59:05 +0400478static Context *get_ctx(const char *name, const char **namep);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700479static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000480static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200481static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000482static void *new_Block(size_t size);
483static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700484static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300485 const char *text, int txtlen);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500486static Token *copy_Token(Token * tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000487static Token *delete_Token(Token * t);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500488static Line *new_Line(void);
489static ExpDef *new_ExpDef(int exp_type);
490static ExpInv *new_ExpInv(int exp_type, ExpDef *ed);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000491
492/*
493 * Macros for safe checking of token pointers, avoid *(NULL)
494 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300495#define tok_type_(x,t) ((x) && (x)->type == (t))
496#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
497#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
498#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000499
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400500/*
501 * A few helpers for single macros
502 */
503
504/* We might be not smacro parameter at all */
505static bool is_smacro_param(Token *t)
506{
507 return t->type >= TOK_SMAC_PARAM;
508}
509
510/* smacro parameters are counted in a special way */
511static int smacro_get_param_idx(Token *t)
512{
513 return t->type - TOK_SMAC_PARAM;
514}
515
516/* encode smacro parameter index */
517static int smacro_set_param_idx(Token *t, unsigned int index)
518{
519 return t->type = TOK_SMAC_PARAM + index;
520}
521
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300522#ifdef NASM_TRACE
523
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400524#define stringify(x) #x
525
Cyrill Gorcunov9d1141a2011-06-26 23:07:35 +0400526#define nasm_trace(msg, ...) printf("(%s:%d): " msg "\n", __func__, __LINE__, ##__VA_ARGS__)
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400527#define nasm_dump_token(t) nasm_raw_dump_token(t, __FILE__, __LINE__, __func__);
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400528#define nasm_dump_stream(t) nasm_raw_dump_stream(t, __FILE__, __LINE__, __func__);
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400529
530/* FIXME: we really need some compound type here instead of inplace code */
531static const char *nasm_get_tok_type_str(enum pp_token_type type)
532{
533#define SWITCH_TOK_NAME(type) \
534 case (type): \
535 return stringify(type)
536
537 switch (type) {
538 SWITCH_TOK_NAME(TOK_NONE);
539 SWITCH_TOK_NAME(TOK_WHITESPACE);
540 SWITCH_TOK_NAME(TOK_COMMENT);
541 SWITCH_TOK_NAME(TOK_ID);
542 SWITCH_TOK_NAME(TOK_PREPROC_ID);
543 SWITCH_TOK_NAME(TOK_STRING);
544 SWITCH_TOK_NAME(TOK_NUMBER);
545 SWITCH_TOK_NAME(TOK_FLOAT);
546 SWITCH_TOK_NAME(TOK_SMAC_END);
547 SWITCH_TOK_NAME(TOK_OTHER);
548 SWITCH_TOK_NAME(TOK_INTERNAL_STRING);
549 SWITCH_TOK_NAME(TOK_PREPROC_Q);
550 SWITCH_TOK_NAME(TOK_PREPROC_QQ);
551 SWITCH_TOK_NAME(TOK_PASTE);
552 SWITCH_TOK_NAME(TOK_INDIRECT);
553 SWITCH_TOK_NAME(TOK_SMAC_PARAM);
554 SWITCH_TOK_NAME(TOK_MAX);
555 }
556
557 return NULL;
558}
559
560static void nasm_raw_dump_token(Token *token, const char *file, int line, const char *func)
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300561{
562 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
563 if (token) {
564 Token *t;
565 list_for_each(t, token) {
566 if (t->text)
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400567 printf("'%s'(%s) ", t->text,
568 nasm_get_tok_type_str(t->type));
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300569 }
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400570 printf("\n\n");
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300571 }
572}
573
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400574static void nasm_raw_dump_stream(Token *token, const char *file, int line, const char *func)
575{
576 printf("---[%s (%s:%d): %p]---\n", func, file, line, (void *)token);
577 if (token) {
578 Token *t;
579 list_for_each(t, token)
580 printf("%s", t->text ? t->text : " ");
581 printf("\n\n");
582 }
583}
584
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +0400585#else
586#define nasm_trace(msg, ...)
587#define nasm_dump_token(t)
Cyrill Gorcunov2e046002011-06-26 23:33:56 +0400588#define nasm_dump_stream(t)
Cyrill Gorcunov49e8f692010-11-11 15:06:12 +0300589#endif
590
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300591/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700592 * nasm_unquote with error if the string contains NUL characters.
593 * If the string contains NUL characters, issue an error and return
594 * the C len, i.e. truncate at the NUL.
595 */
596static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
597{
598 size_t len = nasm_unquote(qstr, NULL);
599 size_t clen = strlen(qstr);
600
601 if (len != clen)
602 error(ERR_NONFATAL, "NUL character in `%s' directive",
603 pp_directives[directive]);
604
605 return clen;
606}
607
608/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700609 * In-place reverse a list of tokens.
610 */
611static Token *reverse_tokens(Token *t)
612{
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400613 Token *prev, *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700614
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400615 list_reverse(t, prev, next);
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700616
Cyrill Gorcunov3eba69a2011-04-13 13:15:02 +0400617 return t;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700618}
619
620/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300621 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000622 * front of them. We do it here because I could not find any other
623 * place to do it for the moment, and it is a hack (ideally it would
624 * be nice to be able to use the NASM pre-processor to do it).
625 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000626static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000627{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000628 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400629 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000630
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400631 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000632
633 /* Binary search for the directive name */
634 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400635 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400636 q = nasm_skip_word(p);
637 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000638 if (len) {
639 oldchar = p[len];
640 p[len] = 0;
641 while (j - i > 1) {
642 k = (j + i) / 2;
643 m = nasm_stricmp(p, tasm_directives[k]);
644 if (m == 0) {
645 /* We have found a directive, so jam a % in front of it
646 * so that NASM will then recognise it as one if it's own.
647 */
648 p[len] = oldchar;
649 len = strlen(p);
650 oldline = line;
651 line = nasm_malloc(len + 2);
652 line[0] = '%';
653 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700654 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300655 * NASM does not recognise IFDIFI, so we convert
656 * it to %if 0. This is not used in NASM
657 * compatible code, but does need to parse for the
658 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000659 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700660 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000661 } else {
662 memcpy(line + 1, p, len + 1);
663 }
664 nasm_free(oldline);
665 return line;
666 } else if (m < 0) {
667 j = k;
668 } else
669 i = k;
670 }
671 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000672 }
673 return line;
674}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000675
H. Peter Anvin76690a12002-04-30 20:52:49 +0000676/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000677 * The pre-preprocessing stage... This function translates line
678 * number indications as they emerge from GNU cpp (`# lineno "file"
679 * flags') into NASM preprocessor line number indications (`%line
680 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000681 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000682static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000683{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000684 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000685 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000686
H. Peter Anvine2c80182005-01-15 22:15:51 +0000687 if (line[0] == '#' && line[1] == ' ') {
688 oldline = line;
689 fname = oldline + 2;
690 lineno = atoi(fname);
691 fname += strspn(fname, "0123456789 ");
692 if (*fname == '"')
693 fname++;
694 fnlen = strcspn(fname, "\"");
695 line = nasm_malloc(20 + fnlen);
696 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
697 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000698 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000699 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000700 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000701 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000702}
703
704/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000705 * Free a linked list of tokens.
706 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000707static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000708{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400709 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000710 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000711}
712
713/*
714 * Free a linked list of lines.
715 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000716static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000717{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400718 Line *l, *tmp;
719 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000720 free_tlist(l->first);
721 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000722 }
723}
724
725/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500726 * Free an ExpDef
H. Peter Anvineba20a72002-04-30 20:53:55 +0000727 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500728static void free_expdef(ExpDef * ed)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000729{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500730 nasm_free(ed->name);
731 free_tlist(ed->dlist);
732 nasm_free(ed->defaults);
733 free_llist(ed->line);
734 nasm_free(ed);
735}
736
737/*
738 * Free an ExpInv
739 */
740static void free_expinv(ExpInv * ei)
741{
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +0400742 nasm_free(ei->name);
743 nasm_free(ei->label_text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300744 nasm_free(ei);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000745}
746
747/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700748 * Free all currently defined macros, and free the hash tables
749 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700750static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700751{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400752 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700753 const char *key;
754 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700755
H. Peter Anvin072771e2008-05-22 13:17:51 -0700756 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300757 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400758 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300759 nasm_free(s->name);
760 free_tlist(s->expansion);
761 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300762 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700763 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700764 hash_free(smt);
765}
766
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500767static void free_expdef_table(struct hash_table *edt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700768{
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500769 ExpDef *ed, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700770 const char *key;
771 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700772
773 it = NULL;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500774 while ((ed = hash_iterate(edt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300775 nasm_free((void *)key);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500776 list_for_each_safe(ed ,tmp, ed)
777 free_expdef(ed);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700778 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500779 hash_free(edt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700780}
781
782static void free_macros(void)
783{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700784 free_smacro_table(&smacros);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500785 free_expdef_table(&expdefs);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700786}
787
788/*
789 * Initialize the hash tables
790 */
791static void init_macros(void)
792{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700793 hash_init(&smacros, HASH_LARGE);
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500794 hash_init(&expdefs, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700795}
796
797/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000798 * Pop the context stack.
799 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000800static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000801{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000802 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000803
804 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700805 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000806 nasm_free(c->name);
807 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000808}
809
H. Peter Anvin072771e2008-05-22 13:17:51 -0700810/*
811 * Search for a key in the hash index; adding it if necessary
812 * (in which case we initialize the data pointer to NULL.)
813 */
814static void **
815hash_findi_add(struct hash_table *hash, const char *str)
816{
817 struct hash_insert hi;
818 void **r;
819 char *strx;
820
821 r = hash_findi(hash, str, &hi);
822 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300823 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700824
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300825 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700826 return hash_add(&hi, strx, NULL);
827}
828
829/*
830 * Like hash_findi, but returns the data element rather than a pointer
831 * to it. Used only when not adding a new element, hence no third
832 * argument.
833 */
834static void *
835hash_findix(struct hash_table *hash, const char *str)
836{
837 void **p;
838
839 p = hash_findi(hash, str, NULL);
840 return p ? *p : NULL;
841}
842
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400843/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500844 * read line from standard macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400845 * if there no more left -- return NULL
846 */
847static char *line_from_stdmac(void)
848{
849 unsigned char c;
850 const unsigned char *p = stdmacpos;
851 char *line, *q;
852 size_t len = 0;
853
854 if (!stdmacpos)
855 return NULL;
856
857 while ((c = *p++)) {
858 if (c >= 0x80)
859 len += pp_directives_len[c - 0x80] + 1;
860 else
861 len++;
862 }
863
864 line = nasm_malloc(len + 1);
865 q = line;
866 while ((c = *stdmacpos++)) {
867 if (c >= 0x80) {
868 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
869 q += pp_directives_len[c - 0x80];
870 *q++ = ' ';
871 } else {
872 *q++ = c;
873 }
874 }
875 stdmacpos = p;
876 *q = '\0';
877
878 if (!*stdmacpos) {
879 /* This was the last of the standard macro chain... */
880 stdmacpos = NULL;
881 if (any_extrastdmac) {
882 stdmacpos = extrastdmac;
883 any_extrastdmac = false;
884 } else if (do_predef) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300885 ExpInv *ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400886 Line *pd, *l;
887 Token *head, **tail, *t;
888
889 /*
890 * Nasty hack: here we push the contents of
891 * `predef' on to the top-level expansion stack,
892 * since this is the most convenient way to
893 * implement the pre-include and pre-define
894 * features.
895 */
896 list_for_each(pd, predef) {
897 head = NULL;
898 tail = &head;
899 list_for_each(t, pd->first) {
900 *tail = new_Token(NULL, t->type, t->text, 0);
901 tail = &(*tail)->next;
902 }
903
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500904 l = new_Line();
905 l->first = head;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300906 ei = new_ExpInv(EXP_PREDEF, NULL);
907 ei->current = l;
908 ei->emitting = true;
909 ei->prev = istk->expansion;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500910 istk->expansion = ei;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400911 }
912 do_predef = false;
913 }
914 }
915
916 return line;
917}
918
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000919#define BUF_DELTA 512
920/*
921 * Read a line from the top file in istk, handling multiple CR/LFs
922 * at the end of the line read, and handling spurious ^Zs. Will
923 * return lines from the standard macro set if this has not already
924 * been done.
925 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000926static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000927{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000928 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000929 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000930
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400931 /*
932 * standart macros set (predefined) goes first
933 */
934 p = line_from_stdmac();
935 if (p)
936 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700937
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400938 /*
939 * regular read from a file
940 */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000941 bufsize = BUF_DELTA;
942 buffer = nasm_malloc(BUF_DELTA);
943 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000944 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000945 while (1) {
946 q = fgets(p, bufsize - (p - buffer), istk->fp);
947 if (!q)
948 break;
949 p += strlen(p);
950 if (p > buffer && p[-1] == '\n') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300951 /*
952 * Convert backslash-CRLF line continuation sequences into
953 * nothing at all (for DOS and Windows)
954 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000955 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
956 p -= 3;
957 *p = 0;
958 continued_count++;
959 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300960 /*
961 * Also convert backslash-LF line continuation sequences into
962 * nothing at all (for Unix)
963 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000964 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
965 p -= 2;
966 *p = 0;
967 continued_count++;
968 } else {
969 break;
970 }
971 }
972 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000973 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 bufsize += BUF_DELTA;
975 buffer = nasm_realloc(buffer, bufsize);
976 p = buffer + offset; /* prevent stale-pointer problems */
977 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000978 }
979
H. Peter Anvine2c80182005-01-15 22:15:51 +0000980 if (!q && p == buffer) {
981 nasm_free(buffer);
982 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000983 }
984
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300985 src_set_linnum(src_get_linnum() + istk->lineinc +
986 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000987
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000988 /*
989 * Play safe: remove CRs as well as LFs, if any of either are
990 * present at the end of the line.
991 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000992 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000994
995 /*
996 * Handle spurious ^Z, which may be inserted into source files
997 * by some file transfer utilities.
998 */
999 buffer[strcspn(buffer, "\032")] = '\0';
1000
H. Peter Anvin734b1882002-04-30 21:01:08 +00001001 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001002
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001003 return buffer;
1004}
1005
1006/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00001007 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001008 * don't need to parse the value out of e.g. numeric tokens: we
1009 * simply split one string into many.
1010 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001011static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001012{
H. Peter Anvinca544db2008-10-19 19:30:11 -07001013 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001014 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001015 Token *list = NULL;
1016 Token *t, **tail = &list;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001017 bool verbose = true;
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +04001018
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +04001019 nasm_trace("Tokenize for '%s'", line);
1020
Keith Kanios6faad4e2010-12-18 14:08:02 -06001021 if ((defining != NULL) && (defining->ignoring == true)) {
1022 verbose = false;
1023 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001024
H. Peter Anvine2c80182005-01-15 22:15:51 +00001025 while (*line) {
1026 p = line;
1027 if (*p == '%') {
1028 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001029 if (*p == '+' && !nasm_isdigit(p[1])) {
1030 p++;
1031 type = TOK_PASTE;
1032 } else if (nasm_isdigit(*p) ||
1033 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001034 do {
1035 p++;
1036 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001037 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001038 type = TOK_PREPROC_ID;
1039 } else if (*p == '{') {
1040 p++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001041 while (*p && *p != '}') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001042 p[-1] = *p;
1043 p++;
1044 }
1045 p[-1] = '\0';
1046 if (*p)
1047 p++;
1048 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001049 } else if (*p == '[') {
1050 int lvl = 1;
1051 line += 2; /* Skip the leading %[ */
1052 p++;
1053 while (lvl && (c = *p++)) {
1054 switch (c) {
1055 case ']':
1056 lvl--;
1057 break;
1058 case '%':
1059 if (*p == '[')
1060 lvl++;
1061 break;
1062 case '\'':
1063 case '\"':
1064 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +04001065 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001066 break;
1067 default:
1068 break;
1069 }
1070 }
1071 p--;
1072 if (*p)
1073 *p++ = '\0';
Keith Kanios6faad4e2010-12-18 14:08:02 -06001074 if (lvl && verbose)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001075 error(ERR_NONFATAL, "unterminated %[ construct");
1076 type = TOK_INDIRECT;
1077 } else if (*p == '?') {
1078 type = TOK_PREPROC_Q; /* %? */
1079 p++;
1080 if (*p == '?') {
1081 type = TOK_PREPROC_QQ; /* %?? */
1082 p++;
1083 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001084 } else if (*p == '!') {
1085 type = TOK_PREPROC_ID;
1086 p++;
1087 if (isidchar(*p)) {
1088 do {
1089 p++;
1090 } while (isidchar(*p));
1091 } else if (*p == '\'' || *p == '\"' || *p == '`') {
1092 p = nasm_skip_string(p);
1093 if (*p)
1094 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001095 else if(verbose)
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001096 error(ERR_NONFATAL|ERR_PASS1, "unterminated %! string");
1097 } else {
1098 /* %! without string or identifier */
1099 type = TOK_OTHER; /* Legacy behavior... */
1100 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101 } else if (isidchar(*p) ||
1102 ((*p == '!' || *p == '%' || *p == '$') &&
1103 isidchar(p[1]))) {
1104 do {
1105 p++;
1106 }
1107 while (isidchar(*p));
1108 type = TOK_PREPROC_ID;
1109 } else {
1110 type = TOK_OTHER;
1111 if (*p == '%')
1112 p++;
1113 }
1114 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1115 type = TOK_ID;
1116 p++;
1117 while (*p && isidchar(*p))
1118 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001119 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001120 /*
1121 * A string token.
1122 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001123 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001124 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001125
H. Peter Anvine2c80182005-01-15 22:15:51 +00001126 if (*p) {
1127 p++;
Keith Kanios6faad4e2010-12-18 14:08:02 -06001128 } else if(verbose) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001129 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001130 /* Handling unterminated strings by UNV */
1131 /* type = -1; */
1132 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001133 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001134 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001135 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001136 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001137 bool is_hex = false;
1138 bool is_float = false;
1139 bool has_e = false;
1140 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001141
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001143 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001145
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001146 if (*p == '$') {
1147 p++;
1148 is_hex = true;
1149 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001150
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001151 for (;;) {
1152 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001153
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001154 if (!is_hex && (c == 'e' || c == 'E')) {
1155 has_e = true;
1156 if (*p == '+' || *p == '-') {
1157 /*
1158 * e can only be followed by +/- if it is either a
1159 * prefixed hex number or a floating-point number
1160 */
1161 p++;
1162 is_float = true;
1163 }
1164 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1165 is_hex = true;
1166 } else if (c == 'P' || c == 'p') {
1167 is_float = true;
1168 if (*p == '+' || *p == '-')
1169 p++;
1170 } else if (isnumchar(c) || c == '_')
1171 ; /* just advance */
1172 else if (c == '.') {
1173 /*
1174 * we need to deal with consequences of the legacy
1175 * parser, like "1.nolist" being two tokens
1176 * (TOK_NUMBER, TOK_ID) here; at least give it
1177 * a shot for now. In the future, we probably need
1178 * a flex-based scanner with proper pattern matching
1179 * to do it as well as it can be done. Nothing in
1180 * the world is going to help the person who wants
1181 * 0x123.p16 interpreted as two tokens, though.
1182 */
1183 r = p;
1184 while (*r == '_')
1185 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001186
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001187 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1188 (!is_hex && (*r == 'e' || *r == 'E')) ||
1189 (*r == 'p' || *r == 'P')) {
1190 p = r;
1191 is_float = true;
1192 } else
1193 break; /* Terminate the token */
1194 } else
1195 break;
1196 }
1197 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001198
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001199 if (p == line+1 && *line == '$') {
1200 type = TOK_OTHER; /* TOKEN_HERE */
1201 } else {
1202 if (has_e && !is_hex) {
1203 /* 1e13 is floating-point, but 1e13h is not */
1204 is_float = true;
1205 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001206
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001207 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1208 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001209 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001210 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001211 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001212 /*
1213 * Whitespace just before end-of-line is discarded by
1214 * pretending it's a comment; whitespace just before a
1215 * comment gets lumped into the comment.
1216 */
1217 if (!*p || *p == ';') {
1218 type = TOK_COMMENT;
1219 while (*p)
1220 p++;
1221 }
1222 } else if (*p == ';') {
1223 type = TOK_COMMENT;
1224 while (*p)
1225 p++;
1226 } else {
1227 /*
1228 * Anything else is an operator of some kind. We check
1229 * for all the double-character operators (>>, <<, //,
1230 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001231 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001232 */
1233 type = TOK_OTHER;
1234 if ((p[0] == '>' && p[1] == '>') ||
1235 (p[0] == '<' && p[1] == '<') ||
1236 (p[0] == '/' && p[1] == '/') ||
1237 (p[0] == '<' && p[1] == '=') ||
1238 (p[0] == '>' && p[1] == '=') ||
1239 (p[0] == '=' && p[1] == '=') ||
1240 (p[0] == '!' && p[1] == '=') ||
1241 (p[0] == '<' && p[1] == '>') ||
1242 (p[0] == '&' && p[1] == '&') ||
1243 (p[0] == '|' && p[1] == '|') ||
1244 (p[0] == '^' && p[1] == '^')) {
1245 p++;
1246 }
1247 p++;
1248 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001249
H. Peter Anvine2c80182005-01-15 22:15:51 +00001250 /* Handling unterminated string by UNV */
1251 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001252 {
1253 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1254 t->text[p-line] = *line;
1255 tail = &t->next;
1256 }
1257 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001258 if (type != TOK_COMMENT) {
1259 *tail = t = new_Token(NULL, type, line, p - line);
1260 tail = &t->next;
1261 }
1262 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001263 }
Cyrill Gorcunovfc0c1282011-06-25 19:51:44 +04001264
1265 nasm_dump_token(list);
1266
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001267 return list;
1268}
1269
H. Peter Anvince616072002-04-30 21:02:23 +00001270/*
1271 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001272 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001273 * deleted only all at once by the delete_Blocks function.
1274 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001275static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001276{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001277 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001278
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001279 /* first, get to the end of the linked list */
1280 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001281 b = b->next;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001282
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001283 /* now allocate the requested chunk */
1284 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001285
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001286 /* now allocate a new block for the next request */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001287 b->next = nasm_zalloc(sizeof(Blocks));
1288
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001289 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001290}
1291
1292/*
1293 * this function deletes all managed blocks of memory
1294 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001295static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001296{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001297 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001298
H. Peter Anvin70653092007-10-19 14:42:29 -07001299 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001300 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001301 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001302 * free it.
1303 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001304 while (b) {
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04001305 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001306 a = b;
1307 b = b->next;
1308 if (a != &blocks)
1309 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001310 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001312
1313/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001314 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001315 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001316 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001317 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001318static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001319 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001320{
1321 Token *t;
1322 int i;
1323
H. Peter Anvin89cee572009-07-15 09:16:54 -04001324 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1326 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1327 freeTokens[i].next = &freeTokens[i + 1];
1328 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001329 }
1330 t = freeTokens;
1331 freeTokens = t->next;
1332 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001333 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001334 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001335 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 t->text = NULL;
1337 } else {
1338 if (txtlen == 0)
1339 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001340 t->text = nasm_malloc(txtlen+1);
1341 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001342 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001343 }
1344 return t;
1345}
1346
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001347static Token *copy_Token(Token * tline)
1348{
1349 Token *t, *tt, *first = NULL, *prev = NULL;
1350 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001351 for (tt = tline; tt != NULL; tt = tt->next) {
1352 if (!freeTokens) {
1353 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1354 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1355 freeTokens[i].next = &freeTokens[i + 1];
1356 freeTokens[i].next = NULL;
1357 }
1358 t = freeTokens;
1359 freeTokens = t->next;
1360 t->next = NULL;
1361 t->text = tt->text ? nasm_strdup(tt->text) : NULL;
1362 t->a.mac = tt->a.mac;
1363 t->a.len = tt->a.len;
1364 t->type = tt->type;
1365 if (prev != NULL) {
1366 prev->next = t;
1367 } else {
1368 first = t;
1369 }
1370 prev = t;
1371 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001372 return first;
1373}
1374
H. Peter Anvine2c80182005-01-15 22:15:51 +00001375static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001376{
1377 Token *next = t->next;
1378 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001379 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001380 freeTokens = t;
1381 return next;
1382}
1383
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001384/*
1385 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001386 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1387 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001388 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001389static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001390{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001391 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001392 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001393 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001394 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001395
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001396 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001397 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001398 char *v;
1399 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001400
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001401 v = t->text + 2;
1402 if (*v == '\'' || *v == '\"' || *v == '`') {
1403 size_t len = nasm_unquote(v, NULL);
1404 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001405
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001406 if (len != clen) {
1407 error(ERR_NONFATAL | ERR_PASS1,
1408 "NUL character in %! string");
1409 v = NULL;
1410 }
1411 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001412
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001413 if (v) {
1414 char *p = getenv(v);
1415 if (!p) {
1416 error(ERR_NONFATAL | ERR_PASS1,
1417 "nonexistent environment variable `%s'", v);
1418 p = "";
1419 }
1420 t->text = nasm_strdup(p);
1421 }
1422 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001423 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001424
H. Peter Anvine2c80182005-01-15 22:15:51 +00001425 /* Expand local macros here and not during preprocessing */
1426 if (expand_locals &&
1427 t->type == TOK_PREPROC_ID && t->text &&
1428 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001429 const char *q;
1430 char *p;
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001431 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001432 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001433 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001434 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001435 p = nasm_strcat(buffer, q);
1436 nasm_free(t->text);
1437 t->text = p;
1438 }
1439 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001440
1441 /* Expand %? and %?? directives */
Keith Kanios3136d482010-11-13 09:34:34 -06001442 if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001443 ((t->type == TOK_PREPROC_Q) ||
1444 (t->type == TOK_PREPROC_QQ))) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001445 ExpInv *ei;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001446 for (ei = istk->expansion; ei != NULL; ei = ei->prev){
1447 if (ei->type == EXP_MMACRO) {
1448 nasm_free(t->text);
1449 if (t->type == TOK_PREPROC_Q) {
1450 t->text = nasm_strdup(ei->name);
1451 } else {
1452 t->text = nasm_strdup(ei->def->name);
1453 }
1454 break;
1455 }
1456 }
1457 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001458
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001459 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001460 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001461 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001463 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001464
H. Peter Anvin734b1882002-04-30 21:01:08 +00001465 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001466
1467 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001468 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001469 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001470 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001471 q = t->text;
1472 while (*q)
1473 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001474 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001475 }
1476 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001477
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001478 return line;
1479}
1480
1481/*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001482 * Initialize a new Line
1483 */
Cyrill Gorcunov29cb0bb2010-11-11 11:19:43 +03001484static inline Line *new_Line(void)
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001485{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03001486 return (Line *)nasm_zalloc(sizeof(Line));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001487}
1488
1489
1490/*
1491 * Initialize a new Expansion Definition
1492 */
1493static ExpDef *new_ExpDef(int exp_type)
1494{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001495 ExpDef *ed = (ExpDef*)nasm_zalloc(sizeof(ExpDef));
1496 ed->type = exp_type;
1497 ed->casesense = true;
1498 ed->state = COND_NEVER;
1499
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001500 return ed;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001501}
1502
1503
1504/*
1505 * Initialize a new Expansion Instance
1506 */
1507static ExpInv *new_ExpInv(int exp_type, ExpDef *ed)
1508{
Cyrill Gorcunovc5157742010-11-11 11:29:40 +03001509 ExpInv *ei = (ExpInv*)nasm_zalloc(sizeof(ExpInv));
1510 ei->type = exp_type;
1511 ei->def = ed;
1512 ei->unique = ++unique;
1513
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03001514 if ((istk->mmac_depth < 1) &&
1515 (istk->expansion == NULL) &&
1516 (ed != NULL) &&
1517 (ed->type != EXP_MMACRO) &&
1518 (ed->type != EXP_REP) &&
1519 (ed->type != EXP_WHILE)) {
1520 ei->linnum = src_get_linnum();
1521 src_set_linnum(ei->linnum - ed->linecount - 1);
1522 } else {
1523 ei->linnum = -1;
1524 }
1525 if ((istk->expansion == NULL) ||
1526 (ei->type == EXP_MMACRO)) {
1527 ei->relno = 0;
1528 } else {
1529 ei->relno = istk->expansion->lineno;
1530 if (ed != NULL) {
1531 ei->relno -= (ed->linecount + 1);
1532 }
1533 }
1534 return ei;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05001535}
1536
1537/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001538 * A scanner, suitable for use by the expression evaluator, which
1539 * operates on a line of Tokens. Expects a pointer to a pointer to
1540 * the first token in the line to be passed in as its private_data
1541 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001542 *
1543 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001544 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001545static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001546{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001547 Token **tlineptr = private_data;
1548 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001549 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001550
H. Peter Anvine2c80182005-01-15 22:15:51 +00001551 do {
1552 tline = *tlineptr;
1553 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001554 } while (tline && (tline->type == TOK_WHITESPACE ||
1555 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001556
1557 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001558 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001559
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001560 tokval->t_charptr = tline->text;
1561
H. Peter Anvin76690a12002-04-30 20:52:49 +00001562 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001563 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001564 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001565 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001566
H. Peter Anvine2c80182005-01-15 22:15:51 +00001567 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001568 p = tokval->t_charptr = tline->text;
1569 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001570 tokval->t_charptr++;
1571 return tokval->t_type = TOKEN_ID;
1572 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001573
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001574 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001575 if (r >= p+MAX_KEYWORD)
1576 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001577 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001578 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001579 *s = '\0';
1580 /* right, so we have an identifier sitting in temp storage. now,
1581 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001582 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001583 }
1584
H. Peter Anvine2c80182005-01-15 22:15:51 +00001585 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001586 bool rn_error;
1587 tokval->t_integer = readnum(tline->text, &rn_error);
1588 tokval->t_charptr = tline->text;
1589 if (rn_error)
1590 return tokval->t_type = TOKEN_ERRNUM;
1591 else
1592 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001593 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001594
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001595 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001596 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001597 }
1598
H. Peter Anvine2c80182005-01-15 22:15:51 +00001599 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001600 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001601
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001602 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001603 tokval->t_charptr = tline->text;
1604 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001605
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001606 if (ep[0] != bq || ep[1] != '\0')
1607 return tokval->t_type = TOKEN_ERRSTR;
1608 else
1609 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001610 }
1611
H. Peter Anvine2c80182005-01-15 22:15:51 +00001612 if (tline->type == TOK_OTHER) {
1613 if (!strcmp(tline->text, "<<"))
1614 return tokval->t_type = TOKEN_SHL;
1615 if (!strcmp(tline->text, ">>"))
1616 return tokval->t_type = TOKEN_SHR;
1617 if (!strcmp(tline->text, "//"))
1618 return tokval->t_type = TOKEN_SDIV;
1619 if (!strcmp(tline->text, "%%"))
1620 return tokval->t_type = TOKEN_SMOD;
1621 if (!strcmp(tline->text, "=="))
1622 return tokval->t_type = TOKEN_EQ;
1623 if (!strcmp(tline->text, "<>"))
1624 return tokval->t_type = TOKEN_NE;
1625 if (!strcmp(tline->text, "!="))
1626 return tokval->t_type = TOKEN_NE;
1627 if (!strcmp(tline->text, "<="))
1628 return tokval->t_type = TOKEN_LE;
1629 if (!strcmp(tline->text, ">="))
1630 return tokval->t_type = TOKEN_GE;
1631 if (!strcmp(tline->text, "&&"))
1632 return tokval->t_type = TOKEN_DBL_AND;
1633 if (!strcmp(tline->text, "^^"))
1634 return tokval->t_type = TOKEN_DBL_XOR;
1635 if (!strcmp(tline->text, "||"))
1636 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001637 }
1638
1639 /*
1640 * We have no other options: just return the first character of
1641 * the token text.
1642 */
1643 return tokval->t_type = tline->text[0];
1644}
1645
1646/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001647 * Compare a string to the name of an existing macro; this is a
1648 * simple wrapper which calls either strcmp or nasm_stricmp
1649 * depending on the value of the `casesense' parameter.
1650 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001651static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001652{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001653 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001654}
1655
1656/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001657 * Compare a string to the name of an existing macro; this is a
1658 * simple wrapper which calls either strcmp or nasm_stricmp
1659 * depending on the value of the `casesense' parameter.
1660 */
1661static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1662{
1663 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1664}
1665
1666/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001667 * Return the Context structure associated with a %$ token. Return
1668 * NULL, having _already_ reported an error condition, if the
1669 * context stack isn't deep enough for the supplied number of $
1670 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001671 *
1672 * If "namep" is non-NULL, set it to the pointer to the macro name
1673 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001674 */
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001675static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001676{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001677 Context *ctx;
1678 int i;
1679
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001680 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001681 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001682
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001683 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001685
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 if (!cstk) {
1687 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1688 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001689 }
1690
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001691 name += 2;
1692 ctx = cstk;
1693 i = 0;
1694 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001695 name++;
1696 i++;
1697 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001698 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001699
H. Peter Anvine2c80182005-01-15 22:15:51 +00001700 if (!ctx) {
1701 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001702 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001703 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001704 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001705
1706 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001707 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001708
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001709 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001710}
1711
1712/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001713 * Check to see if a file is already in a string list
1714 */
1715static bool in_list(const StrList *list, const char *str)
1716{
1717 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001718 if (!strcmp(list->str, str))
1719 return true;
1720 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001721 }
1722 return false;
1723}
1724
1725/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001726 * Open an include file. This routine must always return a valid
1727 * file pointer if it returns - it's responsible for throwing an
1728 * ERR_FATAL and bombing out completely if not. It should also try
1729 * the include path one by one until it finds the file or reaches
1730 * the end of the path.
1731 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001732static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001733 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001734{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001735 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001736 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001737 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001738 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001739 size_t prefix_len = 0;
1740 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001741
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001743 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunov6f38fe62010-11-11 11:32:16 +03001744 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001745 memcpy(sl->str, prefix, prefix_len);
1746 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001747 fp = fopen(sl->str, "r");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001748 if (fp && dhead && !in_list(*dhead, sl->str)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001749 **dtail = sl;
1750 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001751 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001752 nasm_free(sl);
1753 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001754 if (fp)
1755 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001756 if (!ip) {
1757 if (!missing_ok)
1758 break;
1759 prefix = NULL;
1760 } else {
1761 prefix = ip->path;
1762 ip = ip->next;
1763 }
1764 if (prefix) {
1765 prefix_len = strlen(prefix);
1766 } else {
1767 /* -MG given and file not found */
1768 if (dhead && !in_list(*dhead, file)) {
1769 sl = nasm_malloc(len+1+sizeof sl->next);
1770 sl->next = NULL;
1771 strcpy(sl->str, file);
1772 **dtail = sl;
1773 *dtail = &sl->next;
1774 }
1775 return NULL;
1776 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001777 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001778
H. Peter Anvin734b1882002-04-30 21:01:08 +00001779 error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001780 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001781}
1782
1783/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001784 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001785 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001786 * return true if _any_ single-line macro of that name is defined.
1787 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001788 * `nparam' or no parameters is defined.
1789 *
1790 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001791 * defined, or nparam is -1, the address of the definition structure
1792 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001793 * is NULL, no action will be taken regarding its contents, and no
1794 * error will occur.
1795 *
1796 * Note that this is also called with nparam zero to resolve
1797 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001798 *
1799 * If you already know which context macro belongs to, you can pass
1800 * the context pointer as first parameter; if you won't but name begins
1801 * with %$ the context will be automatically computed. If all_contexts
1802 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001803 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001804static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001805smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001806 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001807{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001808 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001809 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001810
H. Peter Anvin97a23472007-09-16 17:57:25 -07001811 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001812 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001813 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001814 if (cstk)
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04001815 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001816 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001817 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001818 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001819 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001820 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001821 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001822 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001823
H. Peter Anvine2c80182005-01-15 22:15:51 +00001824 while (m) {
1825 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001826 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001827 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001828 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001829 *defn = m;
1830 else
1831 *defn = NULL;
1832 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001833 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 }
1835 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001836 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001837
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001838 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001839}
1840
1841/*
1842 * Count and mark off the parameters in a multi-line macro call.
1843 * This is called both from within the multi-line macro expansion
1844 * code, and also to mark off the default parameters when provided
1845 * in a %macro definition line.
1846 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001847static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001848{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001849 int paramsize, brace;
1850
1851 *nparam = paramsize = 0;
1852 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001853 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001854 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001855 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001856 paramsize += PARAM_DELTA;
1857 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1858 }
1859 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001860 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001861 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001862 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001863 (*params)[(*nparam)++] = t;
1864 while (tok_isnt_(t, brace ? "}" : ","))
1865 t = t->next;
1866 if (t) { /* got a comma/brace */
1867 t = t->next;
1868 if (brace) {
1869 /*
1870 * Now we've found the closing brace, look further
1871 * for the comma.
1872 */
1873 skip_white_(t);
1874 if (tok_isnt_(t, ",")) {
1875 error(ERR_NONFATAL,
1876 "braces do not enclose all of macro parameter");
1877 while (tok_isnt_(t, ","))
1878 t = t->next;
1879 }
1880 if (t)
1881 t = t->next; /* eat the comma */
1882 }
1883 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001884 }
1885}
1886
1887/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001888 * Determine whether one of the various `if' conditions is true or
1889 * not.
1890 *
1891 * We must free the tline we get passed.
1892 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001893static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001894{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001895 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001896 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001897 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001898 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001899 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001900 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001901 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001902
1903 origline = tline;
1904
H. Peter Anvine2c80182005-01-15 22:15:51 +00001905 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001906 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001907 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001908 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001909 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001910 if (!tline)
1911 break;
1912 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001913 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001914 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001915 free_tlist(origline);
1916 return -1;
1917 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001918 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001919 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001920 tline = tline->next;
1921 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001922 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001923
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001924 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001925 j = false; /* have we matched yet? */
Cyrill Gorcunov4c6f82f2011-10-02 01:06:29 +04001926 skip_white_(tline);
1927 do {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001928 if (!tline || (tline->type != TOK_ID &&
1929 (tline->type != TOK_PREPROC_ID ||
1930 tline->text[1] != '$'))) {
1931 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001932 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001933 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001934 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001935 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001936 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001937 tline = tline->next;
Cyrill Gorcunov4c6f82f2011-10-02 01:06:29 +04001938 skip_white_(tline);
1939 } while (tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001940 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001941
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001942 case PPC_IFENV:
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001943 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001944 j = false; /* have we matched yet? */
Cyrill Gorcunov6acada62011-10-02 09:18:34 +04001945 skip_white_(tline);
1946 do {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001947 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001948 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001949 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001950 tline->text[1] != '!'))) {
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001951 error(ERR_NONFATAL,
1952 "`%s' expects environment variable names",
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001953 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001954 goto fail;
1955 }
Cyrill Gorcunov84b4cba2010-09-12 21:39:40 +04001956 p = tline->text;
1957 if (tline->type == TOK_PREPROC_ID)
1958 p += 2; /* Skip leading %! */
1959 if (*p == '\'' || *p == '\"' || *p == '`')
1960 nasm_unquote_cstr(p, ct);
1961 if (getenv(p))
1962 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001963 tline = tline->next;
Cyrill Gorcunov6acada62011-10-02 09:18:34 +04001964 skip_white_(tline);
1965 } while (tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001966 break;
1967
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001968 case PPC_IFIDN:
1969 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001970 tline = expand_smacro(tline);
1971 t = tt = tline;
1972 while (tok_isnt_(tt, ","))
1973 tt = tt->next;
1974 if (!tt) {
1975 error(ERR_NONFATAL,
1976 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001977 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001978 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001979 }
1980 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001981 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001982 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1983 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1984 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001985 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001986 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001987 }
1988 if (t->type == TOK_WHITESPACE) {
1989 t = t->next;
1990 continue;
1991 }
1992 if (tt->type == TOK_WHITESPACE) {
1993 tt = tt->next;
1994 continue;
1995 }
1996 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001997 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001998 break;
1999 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002000 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002001 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002002 size_t l1 = nasm_unquote(t->text, NULL);
2003 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07002004
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002005 if (l1 != l2) {
2006 j = false;
2007 break;
2008 }
2009 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
2010 j = false;
2011 break;
2012 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002013 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002014 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002015 break;
2016 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002017
H. Peter Anvine2c80182005-01-15 22:15:51 +00002018 t = t->next;
2019 tt = tt->next;
2020 }
2021 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002022 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002023 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002024
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002025 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002026 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002027 bool found = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002028 ExpDef searching, *ed;
H. Peter Anvin65747262002-05-07 00:10:05 +00002029
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002030 skip_white_(tline);
2031 tline = expand_id(tline);
2032 if (!tok_type_(tline, TOK_ID)) {
2033 error(ERR_NONFATAL,
2034 "`%s' expects a macro name", pp_directives[ct]);
2035 goto fail;
2036 }
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002037 memset(&searching, 0, sizeof(searching));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002038 searching.name = nasm_strdup(tline->text);
2039 searching.casesense = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002040 searching.nparam_max = INT_MAX;
2041 tline = expand_smacro(tline->next);
2042 skip_white_(tline);
2043 if (!tline) {
2044 } else if (!tok_type_(tline, TOK_NUMBER)) {
2045 error(ERR_NONFATAL,
2046 "`%s' expects a parameter count or nothing",
2047 pp_directives[ct]);
2048 } else {
2049 searching.nparam_min = searching.nparam_max =
2050 readnum(tline->text, &j);
2051 if (j)
2052 error(ERR_NONFATAL,
2053 "unable to parse parameter count `%s'",
2054 tline->text);
2055 }
2056 if (tline && tok_is_(tline->next, "-")) {
2057 tline = tline->next->next;
2058 if (tok_is_(tline, "*"))
2059 searching.nparam_max = INT_MAX;
2060 else if (!tok_type_(tline, TOK_NUMBER))
2061 error(ERR_NONFATAL,
2062 "`%s' expects a parameter count after `-'",
2063 pp_directives[ct]);
2064 else {
2065 searching.nparam_max = readnum(tline->text, &j);
2066 if (j)
2067 error(ERR_NONFATAL,
2068 "unable to parse parameter count `%s'",
2069 tline->text);
2070 if (searching.nparam_min > searching.nparam_max)
2071 error(ERR_NONFATAL,
2072 "minimum parameter count exceeds maximum");
2073 }
2074 }
2075 if (tline && tok_is_(tline->next, "+")) {
2076 tline = tline->next;
2077 searching.plus = true;
2078 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002079 ed = (ExpDef *) hash_findix(&expdefs, searching.name);
2080 while (ed != NULL) {
Cyrill Gorcunova22e7a92010-11-11 11:42:40 +03002081 if (!strcmp(ed->name, searching.name) &&
2082 (ed->nparam_min <= searching.nparam_max || searching.plus) &&
2083 (searching.nparam_min <= ed->nparam_max || ed->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002084 found = true;
2085 break;
2086 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002087 ed = ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002088 }
2089 if (tline && tline->next)
2090 error(ERR_WARNING|ERR_PASS1,
2091 "trailing garbage after %%ifmacro ignored");
2092 nasm_free(searching.name);
2093 j = found;
2094 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002095 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002096
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002097 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002098 needtype = TOK_ID;
2099 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002100 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002101 needtype = TOK_NUMBER;
2102 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002103 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002104 needtype = TOK_STRING;
2105 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002106
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002107iftype:
2108 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002109
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002110 while (tok_type_(t, TOK_WHITESPACE) ||
2111 (needtype == TOK_NUMBER &&
2112 tok_type_(t, TOK_OTHER) &&
2113 (t->text[0] == '-' || t->text[0] == '+') &&
2114 !t->text[1]))
2115 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002116
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002117 j = tok_type_(t, needtype);
2118 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002119
2120 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002121 t = tline = expand_smacro(tline);
2122 while (tok_type_(t, TOK_WHITESPACE))
2123 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002124
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002125 j = false;
2126 if (t) {
2127 t = t->next; /* Skip the actual token */
2128 while (tok_type_(t, TOK_WHITESPACE))
2129 t = t->next;
2130 j = !t; /* Should be nothing left */
2131 }
2132 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002133
H. Peter Anvin134b9462008-02-16 17:01:40 -08002134 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002135 t = tline = expand_smacro(tline);
2136 while (tok_type_(t, TOK_WHITESPACE))
2137 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002138
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002139 j = !t; /* Should be empty */
2140 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002141
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002142 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002143 t = tline = expand_smacro(tline);
2144 tptr = &t;
2145 tokval.t_type = TOKEN_INVALID;
2146 evalresult = evaluate(ppscan, tptr, &tokval,
2147 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002148 if (!evalresult)
2149 return -1;
2150 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002151 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002152 "trailing garbage after expression ignored");
2153 if (!is_simple(evalresult)) {
2154 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002155 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002156 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002157 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002158 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002159 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002160
H. Peter Anvine2c80182005-01-15 22:15:51 +00002161 default:
2162 error(ERR_FATAL,
2163 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002164 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002165 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002166 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002167
2168 free_tlist(origline);
2169 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002170
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002171fail:
2172 free_tlist(origline);
2173 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002174}
2175
2176/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002177 * Common code for defining an smacro
2178 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002179static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002180 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002181{
2182 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002183 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002184
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002185 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002186 if (!smac) {
2187 error(ERR_WARNING|ERR_PASS1,
2188 "single-line macro `%s' defined both with and"
2189 " without parameters", mname);
2190 /*
2191 * Some instances of the old code considered this a failure,
2192 * some others didn't. What is the right thing to do here?
2193 */
2194 free_tlist(expansion);
2195 return false; /* Failure */
2196 } else {
2197 /*
2198 * We're redefining, so we have to take over an
2199 * existing SMacro structure. This means freeing
2200 * what was already in it.
2201 */
2202 nasm_free(smac->name);
2203 free_tlist(smac->expansion);
2204 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002205 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002206 smtbl = ctx ? &ctx->localmac : &smacros;
2207 smhead = (SMacro **) hash_findi_add(smtbl, mname);
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002208 smac = nasm_zalloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002209 smac->next = *smhead;
2210 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002211 }
2212 smac->name = nasm_strdup(mname);
2213 smac->casesense = casesense;
2214 smac->nparam = nparam;
2215 smac->expansion = expansion;
2216 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002217 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002218}
2219
2220/*
2221 * Undefine an smacro
2222 */
2223static void undef_smacro(Context *ctx, const char *mname)
2224{
2225 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002226 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002227
H. Peter Anvin166c2472008-05-28 12:28:58 -07002228 smtbl = ctx ? &ctx->localmac : &smacros;
2229 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002230
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002231 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002232 /*
2233 * We now have a macro name... go hunt for it.
2234 */
2235 sp = smhead;
2236 while ((s = *sp) != NULL) {
2237 if (!mstrcmp(s->name, mname, s->casesense)) {
2238 *sp = s->next;
2239 nasm_free(s->name);
2240 free_tlist(s->expansion);
2241 nasm_free(s);
2242 } else {
2243 sp = &s->next;
2244 }
2245 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002246 }
2247}
2248
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002249/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002250 * Parse a mmacro specification.
2251 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002252static bool parse_mmacro_spec(Token *tline, ExpDef *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002253{
2254 bool err;
2255
2256 tline = tline->next;
2257 skip_white_(tline);
2258 tline = expand_id(tline);
2259 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002260 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
2261 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002262 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002263
H. Peter Anvina26433d2008-07-16 14:40:01 -07002264 def->name = nasm_strdup(tline->text);
2265 def->plus = false;
2266 def->nolist = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002267// def->in_progress = 0;
2268// def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002269 def->nparam_min = 0;
2270 def->nparam_max = 0;
2271
H. Peter Anvina26433d2008-07-16 14:40:01 -07002272 tline = expand_smacro(tline->next);
2273 skip_white_(tline);
2274 if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002275 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002276 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002277 def->nparam_min = def->nparam_max =
2278 readnum(tline->text, &err);
2279 if (err)
2280 error(ERR_NONFATAL,
2281 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002282 }
2283 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002284 tline = tline->next->next;
2285 if (tok_is_(tline, "*")) {
2286 def->nparam_max = INT_MAX;
2287 } else if (!tok_type_(tline, TOK_NUMBER)) {
2288 error(ERR_NONFATAL,
2289 "`%s' expects a parameter count after `-'", directive);
2290 } else {
2291 def->nparam_max = readnum(tline->text, &err);
2292 if (err) {
2293 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
2294 tline->text);
2295 }
2296 if (def->nparam_min > def->nparam_max) {
2297 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
2298 }
2299 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002300 }
2301 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002302 tline = tline->next;
2303 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002304 }
2305 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002306 !nasm_stricmp(tline->next->text, ".nolist")) {
2307 tline = tline->next;
2308 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002309 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002310
H. Peter Anvina26433d2008-07-16 14:40:01 -07002311 /*
2312 * Handle default parameters.
2313 */
2314 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002315 def->dlist = tline->next;
2316 tline->next = NULL;
2317 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002318 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002319 def->dlist = NULL;
2320 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002321 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002322 def->line = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002323
H. Peter Anvin89cee572009-07-15 09:16:54 -04002324 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002325 !def->plus)
2326 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
2327 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002328
H. Peter Anvina26433d2008-07-16 14:40:01 -07002329 return true;
2330}
2331
2332
2333/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002334 * Decode a size directive
2335 */
2336static int parse_size(const char *str) {
2337 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002338 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002339 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002340 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002341
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002342 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002343}
2344
Ed Beroset3ab3f412002-06-11 03:31:49 +00002345/**
2346 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002347 * Find out if a line contains a preprocessor directive, and deal
2348 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002349 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002350 * If a directive _is_ found, it is the responsibility of this routine
2351 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002352 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002353 * @param tline a pointer to the current tokeninzed line linked list
2354 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002355 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002356 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002357static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002358{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002359 enum preproc_token i;
2360 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002361 bool err;
2362 int nparam;
2363 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002364 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002365 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002366 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002367 char *p, *pp;
2368 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002369 Include *inc;
2370 Context *ctx;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002371 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002372 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002373 struct tokenval tokval;
2374 expr *evalresult;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002375 ExpDef *ed, *eed, **edhead;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002376 ExpInv *ei, *eei;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002377 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002378 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002379 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002380
2381 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002382
H. Peter Anvineba20a72002-04-30 20:53:55 +00002383 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002384 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002385 (tline->text[1] == '%' || tline->text[1] == '$'
2386 || tline->text[1] == '!'))
2387 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002388
H. Peter Anvin4169a472007-09-12 01:29:43 +00002389 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002390
H. Peter Anvin4169a472007-09-12 01:29:43 +00002391 switch (i) {
2392 case PP_INVALID:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002393 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002394 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2395 tline->text);
2396 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002397
H. Peter Anvine2c80182005-01-15 22:15:51 +00002398 case PP_STACKSIZE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002399 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002400 /* Directive to tell NASM what the default stack size is. The
2401 * default is for a 16-bit stack, and this can be overriden with
2402 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002403 */
2404 tline = tline->next;
2405 if (tline && tline->type == TOK_WHITESPACE)
2406 tline = tline->next;
2407 if (!tline || tline->type != TOK_ID) {
2408 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2409 free_tlist(origline);
2410 return DIRECTIVE_FOUND;
2411 }
2412 if (nasm_stricmp(tline->text, "flat") == 0) {
2413 /* All subsequent ARG directives are for a 32-bit stack */
2414 StackSize = 4;
2415 StackPointer = "ebp";
2416 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002417 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002418 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2419 /* All subsequent ARG directives are for a 64-bit stack */
2420 StackSize = 8;
2421 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002422 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002423 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002424 } else if (nasm_stricmp(tline->text, "large") == 0) {
2425 /* All subsequent ARG directives are for a 16-bit stack,
2426 * far function call.
2427 */
2428 StackSize = 2;
2429 StackPointer = "bp";
2430 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002431 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002432 } else if (nasm_stricmp(tline->text, "small") == 0) {
2433 /* All subsequent ARG directives are for a 16-bit stack,
2434 * far function call. We don't support near functions.
2435 */
2436 StackSize = 2;
2437 StackPointer = "bp";
2438 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002439 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002440 } else {
2441 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2442 free_tlist(origline);
2443 return DIRECTIVE_FOUND;
2444 }
2445 free_tlist(origline);
2446 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002447
H. Peter Anvine2c80182005-01-15 22:15:51 +00002448 case PP_ARG:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002449 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002450 /* TASM like ARG directive to define arguments to functions, in
2451 * the following form:
2452 *
2453 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2454 */
2455 offset = ArgOffset;
2456 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002457 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002458 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002459
H. Peter Anvine2c80182005-01-15 22:15:51 +00002460 /* Find the argument name */
2461 tline = tline->next;
2462 if (tline && tline->type == TOK_WHITESPACE)
2463 tline = tline->next;
2464 if (!tline || tline->type != TOK_ID) {
2465 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2466 free_tlist(origline);
2467 return DIRECTIVE_FOUND;
2468 }
2469 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002470
H. Peter Anvine2c80182005-01-15 22:15:51 +00002471 /* Find the argument size type */
2472 tline = tline->next;
2473 if (!tline || tline->type != TOK_OTHER
2474 || tline->text[0] != ':') {
2475 error(ERR_NONFATAL,
2476 "Syntax error processing `%%arg' directive");
2477 free_tlist(origline);
2478 return DIRECTIVE_FOUND;
2479 }
2480 tline = tline->next;
2481 if (!tline || tline->type != TOK_ID) {
2482 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2483 free_tlist(origline);
2484 return DIRECTIVE_FOUND;
2485 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002486
H. Peter Anvine2c80182005-01-15 22:15:51 +00002487 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002488 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002489 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002490 size = parse_size(tt->text);
2491 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002492 error(ERR_NONFATAL,
2493 "Invalid size type for `%%arg' missing directive");
2494 free_tlist(tt);
2495 free_tlist(origline);
2496 return DIRECTIVE_FOUND;
2497 }
2498 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002499
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002500 /* Round up to even stack slots */
2501 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002502
H. Peter Anvine2c80182005-01-15 22:15:51 +00002503 /* Now define the macro for the argument */
2504 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2505 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002506 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002507 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002508
H. Peter Anvine2c80182005-01-15 22:15:51 +00002509 /* Move to the next argument in the list */
2510 tline = tline->next;
2511 if (tline && tline->type == TOK_WHITESPACE)
2512 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002513 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002514 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002515 free_tlist(origline);
2516 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002517
H. Peter Anvine2c80182005-01-15 22:15:51 +00002518 case PP_LOCAL:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002519 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002520 /* TASM like LOCAL directive to define local variables for a
2521 * function, in the following form:
2522 *
2523 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2524 *
2525 * The '= LocalSize' at the end is ignored by NASM, but is
2526 * required by TASM to define the local parameter size (and used
2527 * by the TASM macro package).
2528 */
2529 offset = LocalOffset;
2530 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002531 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002532 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002533
H. Peter Anvine2c80182005-01-15 22:15:51 +00002534 /* Find the argument name */
2535 tline = tline->next;
2536 if (tline && tline->type == TOK_WHITESPACE)
2537 tline = tline->next;
2538 if (!tline || tline->type != TOK_ID) {
2539 error(ERR_NONFATAL,
2540 "`%%local' missing argument parameter");
2541 free_tlist(origline);
2542 return DIRECTIVE_FOUND;
2543 }
2544 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002545
H. Peter Anvine2c80182005-01-15 22:15:51 +00002546 /* Find the argument size type */
2547 tline = tline->next;
2548 if (!tline || tline->type != TOK_OTHER
2549 || tline->text[0] != ':') {
2550 error(ERR_NONFATAL,
2551 "Syntax error processing `%%local' directive");
2552 free_tlist(origline);
2553 return DIRECTIVE_FOUND;
2554 }
2555 tline = tline->next;
2556 if (!tline || tline->type != TOK_ID) {
2557 error(ERR_NONFATAL,
2558 "`%%local' missing size type parameter");
2559 free_tlist(origline);
2560 return DIRECTIVE_FOUND;
2561 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002562
H. Peter Anvine2c80182005-01-15 22:15:51 +00002563 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002564 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002565 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002566 size = parse_size(tt->text);
2567 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002568 error(ERR_NONFATAL,
2569 "Invalid size type for `%%local' missing directive");
2570 free_tlist(tt);
2571 free_tlist(origline);
2572 return DIRECTIVE_FOUND;
2573 }
2574 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002575
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002576 /* Round up to even stack slots */
2577 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002578
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002579 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002580
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002581 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002582 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2583 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002584 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002585
H. Peter Anvine2c80182005-01-15 22:15:51 +00002586 /* Now define the assign to setup the enter_c macro correctly */
2587 snprintf(directive, sizeof(directive),
2588 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002589 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002590
H. Peter Anvine2c80182005-01-15 22:15:51 +00002591 /* Move to the next argument in the list */
2592 tline = tline->next;
2593 if (tline && tline->type == TOK_WHITESPACE)
2594 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002595 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002596 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002597 free_tlist(origline);
2598 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002599
H. Peter Anvine2c80182005-01-15 22:15:51 +00002600 case PP_CLEAR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002601 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002602 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002603 error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002604 "trailing garbage after `%%clear' ignored");
2605 free_macros();
2606 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002607 free_tlist(origline);
2608 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002609
H. Peter Anvin418ca702008-05-30 10:42:30 -07002610 case PP_DEPEND:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002611 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002612 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002613 skip_white_(t);
2614 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002615 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002616 error(ERR_NONFATAL, "`%%depend' expects a file name");
2617 free_tlist(origline);
2618 return DIRECTIVE_FOUND; /* but we did _something_ */
2619 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002620 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002621 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002622 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002623 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002624 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002625 nasm_unquote_cstr(p, i);
2626 if (dephead && !in_list(*dephead, p)) {
2627 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2628 sl->next = NULL;
2629 strcpy(sl->str, p);
2630 *deptail = sl;
2631 deptail = &sl->next;
2632 }
2633 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002634 return DIRECTIVE_FOUND;
2635
2636 case PP_INCLUDE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002637 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002638 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002639 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002640
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002641 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002642 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002643 error(ERR_NONFATAL, "`%%include' expects a file name");
2644 free_tlist(origline);
2645 return DIRECTIVE_FOUND; /* but we did _something_ */
2646 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002647 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002648 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002649 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002650 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002651 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002652 nasm_unquote_cstr(p, i);
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03002653 inc = nasm_zalloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002654 inc->next = istk;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002655 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002656 if (!inc->fp) {
2657 /* -MG given but file not found */
2658 nasm_free(inc);
2659 } else {
2660 inc->fname = src_set_fname(nasm_strdup(p));
2661 inc->lineno = src_set_linnum(0);
2662 inc->lineinc = 1;
2663 inc->expansion = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002664 istk = inc;
2665 list->uplevel(LIST_INCLUDE);
2666 }
2667 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002668 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002669
H. Peter Anvind2456592008-06-19 15:04:18 -07002670 case PP_USE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002671 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002672 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002673 static macros_t *use_pkg;
2674 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002675
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002676 tline = tline->next;
2677 skip_white_(tline);
2678 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002679
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002680 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002681 tline->type != TOK_INTERNAL_STRING &&
2682 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002683 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002684 free_tlist(origline);
2685 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002686 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002687 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002688 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002689 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002690 if (tline->type == TOK_STRING)
2691 nasm_unquote_cstr(tline->text, i);
2692 use_pkg = nasm_stdmac_find_package(tline->text);
2693 if (!use_pkg)
2694 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
2695 else
2696 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002697 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002698 /* Not already included, go ahead and include it */
2699 stdmacpos = use_pkg;
2700 }
2701 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002702 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002703 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002704 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002705 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002706 case PP_POP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002707 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002708 tline = tline->next;
2709 skip_white_(tline);
2710 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002711 if (tline) {
2712 if (!tok_type_(tline, TOK_ID)) {
2713 error(ERR_NONFATAL, "`%s' expects a context identifier",
2714 pp_directives[i]);
2715 free_tlist(origline);
2716 return DIRECTIVE_FOUND; /* but we did _something_ */
2717 }
2718 if (tline->next)
2719 error(ERR_WARNING|ERR_PASS1,
2720 "trailing garbage after `%s' ignored",
2721 pp_directives[i]);
2722 p = nasm_strdup(tline->text);
2723 } else {
2724 p = NULL; /* Anonymous */
2725 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002726
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002727 if (i == PP_PUSH) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002728 ctx = nasm_zalloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002729 ctx->next = cstk;
2730 hash_init(&ctx->localmac, HASH_SMALL);
2731 ctx->name = p;
2732 ctx->number = unique++;
2733 cstk = ctx;
2734 } else {
2735 /* %pop or %repl */
2736 if (!cstk) {
2737 error(ERR_NONFATAL, "`%s': context stack is empty",
2738 pp_directives[i]);
2739 } else if (i == PP_POP) {
2740 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2741 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2742 "expected %s",
2743 cstk->name ? cstk->name : "anonymous", p);
2744 else
2745 ctx_pop();
2746 } else {
2747 /* i == PP_REPL */
2748 nasm_free(cstk->name);
2749 cstk->name = p;
2750 p = NULL;
2751 }
2752 nasm_free(p);
2753 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002754 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002755 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002756 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002757 severity = ERR_FATAL;
2758 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002759 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002760 severity = ERR_NONFATAL;
2761 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002762 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002763 severity = ERR_WARNING|ERR_WARN_USER;
2764 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002765
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002766issue_error:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002767 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002768 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002769 /* Only error out if this is the final pass */
2770 if (pass != 2 && i != PP_FATAL)
2771 return DIRECTIVE_FOUND;
2772
2773 tline->next = expand_smacro(tline->next);
2774 tline = tline->next;
2775 skip_white_(tline);
2776 t = tline ? tline->next : NULL;
2777 skip_white_(t);
2778 if (tok_type_(tline, TOK_STRING) && !t) {
2779 /* The line contains only a quoted string */
2780 p = tline->text;
2781 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
2782 error(severity, "%s", p);
2783 } else {
2784 /* Not a quoted string, or more than a quoted string */
2785 p = detoken(tline, false);
2786 error(severity, "%s", p);
2787 nasm_free(p);
2788 }
2789 free_tlist(origline);
2790 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002791 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002792
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002793 CASE_PP_IF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002794 if (defining != NULL) {
2795 if (defining->type == EXP_IF) {
2796 defining->def_depth ++;
2797 }
2798 return NO_DIRECTIVE_FOUND;
2799 }
2800 if ((istk->expansion != NULL) &&
2801 (istk->expansion->emitting == false)) {
2802 j = COND_NEVER;
2803 } else {
2804 j = if_condition(tline->next, i);
2805 tline->next = NULL; /* it got freed */
2806 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
2807 }
2808 ed = new_ExpDef(EXP_IF);
2809 ed->state = j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002810 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
2811 ed->prev = defining;
2812 defining = ed;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002813 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002814 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002815
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002816 CASE_PP_ELIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002817 if (defining != NULL) {
2818 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2819 return NO_DIRECTIVE_FOUND;
2820 }
2821 }
Cyrill Gorcunov82667ff2011-06-25 19:34:19 +04002822 if ((defining == NULL) || (defining->type != EXP_IF)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002823 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2824 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002825 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002826 case COND_IF_TRUE:
2827 defining->state = COND_DONE;
2828 defining->ignoring = true;
2829 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002830
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002831 case COND_DONE:
2832 case COND_NEVER:
2833 defining->ignoring = true;
2834 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002835
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002836 case COND_ELSE_TRUE:
2837 case COND_ELSE_FALSE:
2838 error_precond(ERR_WARNING|ERR_PASS1,
2839 "`%%elif' after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002840 defining->state = COND_NEVER;
2841 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002842 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002843
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002844 case COND_IF_FALSE:
2845 /*
2846 * IMPORTANT: In the case of %if, we will already have
2847 * called expand_mmac_params(); however, if we're
2848 * processing an %elif we must have been in a
2849 * non-emitting mode, which would have inhibited
2850 * the normal invocation of expand_mmac_params().
2851 * Therefore, we have to do it explicitly here.
2852 */
2853 j = if_condition(expand_mmac_params(tline->next), i);
2854 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002855 defining->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002856 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002857 defining->ignoring = ((defining->state == COND_IF_TRUE) ? false : true);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002858 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002859 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002860 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002861 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002862
H. Peter Anvine2c80182005-01-15 22:15:51 +00002863 case PP_ELSE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002864 if (defining != NULL) {
2865 if ((defining->type != EXP_IF) || (defining->def_depth > 0)) {
2866 return NO_DIRECTIVE_FOUND;
2867 }
2868 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002869 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002870 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002871 "trailing garbage after `%%else' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002872 if ((defining == NULL) || (defining->type != EXP_IF)) {
2873 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
2874 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002875 switch (defining->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002876 case COND_IF_TRUE:
2877 case COND_DONE:
2878 defining->state = COND_ELSE_FALSE;
2879 defining->ignoring = true;
2880 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002881
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002882 case COND_NEVER:
2883 defining->ignoring = true;
2884 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002885
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002886 case COND_IF_FALSE:
2887 defining->state = COND_ELSE_TRUE;
2888 defining->ignoring = false;
2889 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002890
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002891 case COND_ELSE_TRUE:
2892 case COND_ELSE_FALSE:
2893 error_precond(ERR_WARNING|ERR_PASS1,
2894 "`%%else' after `%%else' ignored.");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002895 defining->state = COND_NEVER;
2896 defining->ignoring = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002897 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002898 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002899 free_tlist(origline);
2900 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002901
H. Peter Anvine2c80182005-01-15 22:15:51 +00002902 case PP_ENDIF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002903 if (defining != NULL) {
2904 if (defining->type == EXP_IF) {
2905 if (defining->def_depth > 0) {
2906 defining->def_depth --;
2907 return NO_DIRECTIVE_FOUND;
2908 }
2909 } else {
2910 return NO_DIRECTIVE_FOUND;
2911 }
2912 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002913 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002914 error_precond(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002915 "trailing garbage after `%%endif' ignored");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002916 if ((defining == NULL) || (defining->type != EXP_IF)) {
2917 error(ERR_NONFATAL, "`%%endif': no matching `%%if'");
2918 return DIRECTIVE_FOUND;
2919 }
2920 ed = defining;
2921 defining = ed->prev;
2922 ed->prev = expansions;
2923 expansions = ed;
2924 ei = new_ExpInv(EXP_IF, ed);
2925 ei->current = ed->line;
2926 ei->emitting = true;
2927 ei->prev = istk->expansion;
2928 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002929 free_tlist(origline);
2930 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002931
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002932 case PP_RMACRO:
2933 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002934 case PP_MACRO:
2935 case PP_IMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002936 if (defining != NULL) {
2937 if (defining->type == EXP_MMACRO) {
2938 defining->def_depth ++;
2939 }
2940 return NO_DIRECTIVE_FOUND;
2941 }
2942 ed = new_ExpDef(EXP_MMACRO);
2943 ed->max_depth =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002944 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002945 ed->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05002946 if (!parse_mmacro_spec(tline, ed, pp_directives[i])) {
2947 nasm_free(ed);
2948 ed = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002949 return DIRECTIVE_FOUND;
2950 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002951 ed->def_depth = 0;
2952 ed->cur_depth = 0;
2953 ed->max_depth = (ed->max_depth + 1);
2954 ed->ignoring = false;
2955 ed->prev = defining;
2956 defining = ed;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002957
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002958 eed = (ExpDef *) hash_findix(&expdefs, ed->name);
2959 while (eed) {
Cyrill Gorcunov574fbf12010-11-11 13:44:51 +03002960 if (!strcmp(eed->name, ed->name) &&
2961 (eed->nparam_min <= ed->nparam_max || ed->plus) &&
2962 (ed->nparam_min <= eed->nparam_max || eed->plus)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002963 error(ERR_WARNING|ERR_PASS1,
2964 "redefining multi-line macro `%s'", ed->name);
2965 return DIRECTIVE_FOUND;
2966 }
2967 eed = eed->next;
2968 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002969 free_tlist(origline);
2970 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002971
H. Peter Anvine2c80182005-01-15 22:15:51 +00002972 case PP_ENDM:
2973 case PP_ENDMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002974 if (defining != NULL) {
2975 if (defining->type == EXP_MMACRO) {
2976 if (defining->def_depth > 0) {
2977 defining->def_depth --;
2978 return NO_DIRECTIVE_FOUND;
2979 }
2980 } else {
2981 return NO_DIRECTIVE_FOUND;
2982 }
2983 }
2984 if (!(defining) || (defining->type != EXP_MMACRO)) {
2985 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2986 return DIRECTIVE_FOUND;
2987 }
2988 edhead = (ExpDef **) hash_findi_add(&expdefs, defining->name);
2989 defining->next = *edhead;
2990 *edhead = defining;
2991 ed = defining;
2992 defining = ed->prev;
2993 ed->prev = expansions;
2994 expansions = ed;
2995 ed = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002996 free_tlist(origline);
2997 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002998
H. Peter Anvin89cee572009-07-15 09:16:54 -04002999 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003000 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3001 /*
3002 * We must search along istk->expansion until we hit a
3003 * macro invocation. Then we disable the emitting state(s)
3004 * between exitmacro and endmacro.
3005 */
3006 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3007 if(ei->type == EXP_MMACRO) {
3008 break;
3009 }
3010 }
3011
3012 if (ei != NULL) {
3013 /*
3014 * Set all invocations leading back to the macro
3015 * invocation to a non-emitting state.
3016 */
3017 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3018 eei->emitting = false;
3019 }
3020 eei->emitting = false;
3021 } else {
3022 error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
3023 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05003024 free_tlist(origline);
3025 return DIRECTIVE_FOUND;
3026
H. Peter Anvina26433d2008-07-16 14:40:01 -07003027 case PP_UNMACRO:
3028 case PP_UNIMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003029 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003030 {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003031 ExpDef **ed_p;
3032 ExpDef spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003033
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003034 spec.casesense = (i == PP_UNMACRO);
3035 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
3036 return DIRECTIVE_FOUND;
3037 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003038 ed_p = (ExpDef **) hash_findi(&expdefs, spec.name, NULL);
3039 while (ed_p && *ed_p) {
3040 ed = *ed_p;
3041 if (ed->casesense == spec.casesense &&
3042 !mstrcmp(ed->name, spec.name, spec.casesense) &&
3043 ed->nparam_min == spec.nparam_min &&
3044 ed->nparam_max == spec.nparam_max &&
3045 ed->plus == spec.plus) {
Keith Kaniosc98a5b42010-12-18 12:17:31 -06003046 if (ed->cur_depth > 0) {
Keith Kanios21d885b2010-12-18 12:22:21 -06003047 error(ERR_NONFATAL, "`%s' ignored on active macro",
Keith Kaniosc98a5b42010-12-18 12:17:31 -06003048 pp_directives[i]);
3049 break;
3050 } else {
3051 *ed_p = ed->next;
3052 free_expdef(ed);
3053 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003054 } else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003055 ed_p = &ed->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003056 }
3057 }
3058 free_tlist(origline);
3059 free_tlist(spec.dlist);
3060 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003061 }
3062
H. Peter Anvine2c80182005-01-15 22:15:51 +00003063 case PP_ROTATE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003064 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003065 if (tline->next && tline->next->type == TOK_WHITESPACE)
3066 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003067 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003068 free_tlist(origline);
3069 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
3070 return DIRECTIVE_FOUND;
3071 }
3072 t = expand_smacro(tline->next);
3073 tline->next = NULL;
3074 free_tlist(origline);
3075 tline = t;
3076 tptr = &t;
3077 tokval.t_type = TOKEN_INVALID;
3078 evalresult =
3079 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3080 free_tlist(tline);
3081 if (!evalresult)
3082 return DIRECTIVE_FOUND;
3083 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003084 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003085 "trailing garbage after expression ignored");
3086 if (!is_simple(evalresult)) {
3087 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
3088 return DIRECTIVE_FOUND;
3089 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003090 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3091 if (ei->type == EXP_MMACRO) {
3092 break;
3093 }
3094 }
3095 if (ei == NULL) {
3096 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
3097 } else if (ei->nparam == 0) {
3098 error(ERR_NONFATAL,
3099 "`%%rotate' invoked within macro without parameters");
3100 } else {
3101 int rotate = ei->rotate + reloc_value(evalresult);
3102
3103 rotate %= (int)ei->nparam;
3104 if (rotate < 0)
3105 rotate += ei->nparam;
3106 ei->rotate = rotate;
3107 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003108 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003109
H. Peter Anvine2c80182005-01-15 22:15:51 +00003110 case PP_REP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003111 if (defining != NULL) {
3112 if (defining->type == EXP_REP) {
3113 defining->def_depth ++;
3114 }
3115 return NO_DIRECTIVE_FOUND;
3116 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003117 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003118 do {
3119 tline = tline->next;
3120 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003121
H. Peter Anvine2c80182005-01-15 22:15:51 +00003122 if (tok_type_(tline, TOK_ID) &&
3123 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003124 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003125 do {
3126 tline = tline->next;
3127 } while (tok_type_(tline, TOK_WHITESPACE));
3128 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003129
H. Peter Anvine2c80182005-01-15 22:15:51 +00003130 if (tline) {
3131 t = expand_smacro(tline);
3132 tptr = &t;
3133 tokval.t_type = TOKEN_INVALID;
3134 evalresult =
3135 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3136 if (!evalresult) {
3137 free_tlist(origline);
3138 return DIRECTIVE_FOUND;
3139 }
3140 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003141 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003142 "trailing garbage after expression ignored");
3143 if (!is_simple(evalresult)) {
3144 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
3145 return DIRECTIVE_FOUND;
3146 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003147 count = reloc_value(evalresult);
3148 if (count >= REP_LIMIT) {
Cyrill Gorcunov71f4f842010-08-09 20:17:17 +04003149 error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003150 count = 0;
3151 } else
3152 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003153 } else {
3154 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003155 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003156 }
3157 free_tlist(origline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003158 ed = new_ExpDef(EXP_REP);
3159 ed->nolist = nolist;
3160 ed->def_depth = 0;
3161 ed->cur_depth = 1;
3162 ed->max_depth = (count - 1);
3163 ed->ignoring = false;
3164 ed->prev = defining;
3165 defining = ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003166 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003167
H. Peter Anvine2c80182005-01-15 22:15:51 +00003168 case PP_ENDREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003169 if (defining != NULL) {
3170 if (defining->type == EXP_REP) {
3171 if (defining->def_depth > 0) {
3172 defining->def_depth --;
3173 return NO_DIRECTIVE_FOUND;
3174 }
3175 } else {
3176 return NO_DIRECTIVE_FOUND;
3177 }
3178 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003179 if ((defining == NULL) || (defining->type != EXP_REP)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003180 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
3181 return DIRECTIVE_FOUND;
3182 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003183
H. Peter Anvine2c80182005-01-15 22:15:51 +00003184 /*
3185 * Now we have a "macro" defined - although it has no name
3186 * and we won't be entering it in the hash tables - we must
3187 * push a macro-end marker for it on to istk->expansion.
3188 * After that, it will take care of propagating itself (a
3189 * macro-end marker line for a macro which is really a %rep
3190 * block will cause the macro to be re-expanded, complete
3191 * with another macro-end marker to ensure the process
3192 * continues) until the whole expansion is forcibly removed
3193 * from istk->expansion by a %exitrep.
3194 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003195 ed = defining;
3196 defining = ed->prev;
3197 ed->prev = expansions;
3198 expansions = ed;
3199 ei = new_ExpInv(EXP_REP, ed);
3200 ei->current = ed->line;
3201 ei->emitting = ((ed->max_depth > 0) ? true : false);
3202 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
3203 ei->prev = istk->expansion;
3204 istk->expansion = ei;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003205 free_tlist(origline);
3206 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003207
H. Peter Anvine2c80182005-01-15 22:15:51 +00003208 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003209 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3210 /*
3211 * We must search along istk->expansion until we hit a
3212 * rep invocation. Then we disable the emitting state(s)
3213 * between exitrep and endrep.
3214 */
3215 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3216 if (ei->type == EXP_REP) {
3217 break;
3218 }
3219 }
3220
3221 if (ei != NULL) {
3222 /*
3223 * Set all invocations leading back to the rep
3224 * invocation to a non-emitting state.
3225 */
3226 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3227 eei->emitting = false;
3228 }
3229 eei->emitting = false;
3230 eei->current = NULL;
3231 eei->def->cur_depth = eei->def->max_depth;
3232 } else {
3233 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
3234 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003235 free_tlist(origline);
3236 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003237
H. Peter Anvine2c80182005-01-15 22:15:51 +00003238 case PP_XDEFINE:
3239 case PP_IXDEFINE:
3240 case PP_DEFINE:
3241 case PP_IDEFINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003242 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003243 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003244
H. Peter Anvine2c80182005-01-15 22:15:51 +00003245 tline = tline->next;
3246 skip_white_(tline);
3247 tline = expand_id(tline);
3248 if (!tline || (tline->type != TOK_ID &&
3249 (tline->type != TOK_PREPROC_ID ||
3250 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003251 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003252 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003253 free_tlist(origline);
3254 return DIRECTIVE_FOUND;
3255 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003256
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003257 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003258 last = tline;
3259 param_start = tline = tline->next;
3260 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003261
H. Peter Anvine2c80182005-01-15 22:15:51 +00003262 /* Expand the macro definition now for %xdefine and %ixdefine */
3263 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3264 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003265
H. Peter Anvine2c80182005-01-15 22:15:51 +00003266 if (tok_is_(tline, "(")) {
3267 /*
3268 * This macro has parameters.
3269 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003270
H. Peter Anvine2c80182005-01-15 22:15:51 +00003271 tline = tline->next;
3272 while (1) {
3273 skip_white_(tline);
3274 if (!tline) {
3275 error(ERR_NONFATAL, "parameter identifier expected");
3276 free_tlist(origline);
3277 return DIRECTIVE_FOUND;
3278 }
3279 if (tline->type != TOK_ID) {
3280 error(ERR_NONFATAL,
3281 "`%s': parameter identifier expected",
3282 tline->text);
3283 free_tlist(origline);
3284 return DIRECTIVE_FOUND;
3285 }
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04003286
3287 smacro_set_param_idx(tline, nparam);
3288 nparam++;
3289
H. Peter Anvine2c80182005-01-15 22:15:51 +00003290 tline = tline->next;
3291 skip_white_(tline);
3292 if (tok_is_(tline, ",")) {
3293 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003294 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003295 if (!tok_is_(tline, ")")) {
3296 error(ERR_NONFATAL,
3297 "`)' expected to terminate macro template");
3298 free_tlist(origline);
3299 return DIRECTIVE_FOUND;
3300 }
3301 break;
3302 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003303 }
3304 last = tline;
3305 tline = tline->next;
3306 }
3307 if (tok_type_(tline, TOK_WHITESPACE))
3308 last = tline, tline = tline->next;
3309 macro_start = NULL;
3310 last->next = NULL;
3311 t = tline;
3312 while (t) {
3313 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003314 list_for_each(tt, param_start)
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04003315 if (is_smacro_param(tt) &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003316 !strcmp(tt->text, t->text))
3317 t->type = tt->type;
3318 }
3319 tt = t->next;
3320 t->next = macro_start;
3321 macro_start = t;
3322 t = tt;
3323 }
3324 /*
3325 * Good. We now have a macro name, a parameter count, and a
3326 * token list (in reverse order) for an expansion. We ought
3327 * to be OK just to create an SMacro, store it, and let
3328 * free_tlist have the rest of the line (which we have
3329 * carefully re-terminated after chopping off the expansion
3330 * from the end).
3331 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003332 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003333 free_tlist(origline);
3334 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003335
H. Peter Anvine2c80182005-01-15 22:15:51 +00003336 case PP_UNDEF:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003337 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003338 tline = tline->next;
3339 skip_white_(tline);
3340 tline = expand_id(tline);
3341 if (!tline || (tline->type != TOK_ID &&
3342 (tline->type != TOK_PREPROC_ID ||
3343 tline->text[1] != '$'))) {
3344 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
3345 free_tlist(origline);
3346 return DIRECTIVE_FOUND;
3347 }
3348 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07003349 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003350 "trailing garbage after macro name ignored");
3351 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003352
H. Peter Anvine2c80182005-01-15 22:15:51 +00003353 /* Find the context that symbol belongs to */
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003354 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003355 undef_smacro(ctx, mname);
3356 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003357 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003358
H. Peter Anvin9e200162008-06-04 17:23:14 -07003359 case PP_DEFSTR:
3360 case PP_IDEFSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003361 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003362 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003363
3364 tline = tline->next;
3365 skip_white_(tline);
3366 tline = expand_id(tline);
3367 if (!tline || (tline->type != TOK_ID &&
3368 (tline->type != TOK_PREPROC_ID ||
3369 tline->text[1] != '$'))) {
3370 error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003371 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003372 free_tlist(origline);
3373 return DIRECTIVE_FOUND;
3374 }
3375
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003376 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003377 last = tline;
3378 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003379 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003380
3381 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003382 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003383
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003384 p = detoken(tline, false);
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003385 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003386 macro_start->text = nasm_quote(p, strlen(p));
3387 macro_start->type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003388 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003389
3390 /*
3391 * We now have a macro name, an implicit parameter count of
3392 * zero, and a string token to use as an expansion. Create
3393 * and store an SMacro.
3394 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003395 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003396 free_tlist(origline);
3397 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003398
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003399 case PP_DEFTOK:
3400 case PP_IDEFTOK:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003401 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003402 casesense = (i == PP_DEFTOK);
3403
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003404 tline = tline->next;
3405 skip_white_(tline);
3406 tline = expand_id(tline);
3407 if (!tline || (tline->type != TOK_ID &&
3408 (tline->type != TOK_PREPROC_ID ||
3409 tline->text[1] != '$'))) {
3410 error(ERR_NONFATAL,
3411 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003412 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003413 free_tlist(origline);
3414 return DIRECTIVE_FOUND;
3415 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003416 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003417 last = tline;
3418 tline = expand_smacro(tline->next);
3419 last->next = NULL;
3420
3421 t = tline;
3422 while (tok_type_(t, TOK_WHITESPACE))
3423 t = t->next;
3424 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003425 if (!tok_type_(t, TOK_STRING)) {
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003426 error(ERR_NONFATAL,
3427 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003428 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003429 free_tlist(tline);
3430 free_tlist(origline);
3431 return DIRECTIVE_FOUND;
3432 }
3433
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003434 /*
3435 * Convert the string to a token stream. Note that smacros
3436 * are stored with the token stream reversed, so we have to
3437 * reverse the output of tokenize().
3438 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003439 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003440 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003441
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003442 /*
3443 * We now have a macro name, an implicit parameter count of
3444 * zero, and a numeric token to use as an expansion. Create
3445 * and store an SMacro.
3446 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003447 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003448 free_tlist(tline);
3449 free_tlist(origline);
3450 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003451
H. Peter Anvin418ca702008-05-30 10:42:30 -07003452 case PP_PATHSEARCH:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003453 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003454 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003455 FILE *fp;
3456 StrList *xsl = NULL;
3457 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003458
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003459 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003460
3461 tline = tline->next;
3462 skip_white_(tline);
3463 tline = expand_id(tline);
3464 if (!tline || (tline->type != TOK_ID &&
3465 (tline->type != TOK_PREPROC_ID ||
3466 tline->text[1] != '$'))) {
3467 error(ERR_NONFATAL,
3468 "`%%pathsearch' expects a macro identifier as first parameter");
3469 free_tlist(origline);
3470 return DIRECTIVE_FOUND;
3471 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003472 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003473 last = tline;
3474 tline = expand_smacro(tline->next);
3475 last->next = NULL;
3476
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003477 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003478 while (tok_type_(t, TOK_WHITESPACE))
3479 t = t->next;
3480
3481 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003482 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07003483 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003484 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003485 free_tlist(origline);
3486 return DIRECTIVE_FOUND; /* but we did _something_ */
3487 }
3488 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003489 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003490 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003491 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003492 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003493 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003494
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003495 fp = inc_fopen(p, &xsl, &xst, true);
3496 if (fp) {
3497 p = xsl->str;
3498 fclose(fp); /* Don't actually care about the file */
3499 }
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003500 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003501 macro_start->text = nasm_quote(p, strlen(p));
3502 macro_start->type = TOK_STRING;
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04003503 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003504
3505 /*
3506 * We now have a macro name, an implicit parameter count of
3507 * zero, and a string token to use as an expansion. Create
3508 * and store an SMacro.
3509 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003510 define_smacro(ctx, mname, casesense, 0, macro_start);
3511 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003512 free_tlist(origline);
3513 return DIRECTIVE_FOUND;
3514 }
3515
H. Peter Anvine2c80182005-01-15 22:15:51 +00003516 case PP_STRLEN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003517 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003518 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003519
H. Peter Anvine2c80182005-01-15 22:15:51 +00003520 tline = tline->next;
3521 skip_white_(tline);
3522 tline = expand_id(tline);
3523 if (!tline || (tline->type != TOK_ID &&
3524 (tline->type != TOK_PREPROC_ID ||
3525 tline->text[1] != '$'))) {
3526 error(ERR_NONFATAL,
3527 "`%%strlen' expects a macro identifier as first parameter");
3528 free_tlist(origline);
3529 return DIRECTIVE_FOUND;
3530 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003531 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003532 last = tline;
3533 tline = expand_smacro(tline->next);
3534 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003535
H. Peter Anvine2c80182005-01-15 22:15:51 +00003536 t = tline;
3537 while (tok_type_(t, TOK_WHITESPACE))
3538 t = t->next;
3539 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003540 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003541 error(ERR_NONFATAL,
3542 "`%%strlen` requires string as second parameter");
3543 free_tlist(tline);
3544 free_tlist(origline);
3545 return DIRECTIVE_FOUND;
3546 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003547
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003548 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003549 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003550
H. Peter Anvine2c80182005-01-15 22:15:51 +00003551 /*
3552 * We now have a macro name, an implicit parameter count of
3553 * zero, and a numeric token to use as an expansion. Create
3554 * and store an SMacro.
3555 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003556 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003557 free_tlist(tline);
3558 free_tlist(origline);
3559 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003560
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003561 case PP_STRCAT:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003562 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003563 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003564
3565 tline = tline->next;
3566 skip_white_(tline);
3567 tline = expand_id(tline);
3568 if (!tline || (tline->type != TOK_ID &&
3569 (tline->type != TOK_PREPROC_ID ||
3570 tline->text[1] != '$'))) {
3571 error(ERR_NONFATAL,
3572 "`%%strcat' expects a macro identifier as first parameter");
3573 free_tlist(origline);
3574 return DIRECTIVE_FOUND;
3575 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003576 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003577 last = tline;
3578 tline = expand_smacro(tline->next);
3579 last->next = NULL;
3580
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003581 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003582 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003583 switch (t->type) {
3584 case TOK_WHITESPACE:
3585 break;
3586 case TOK_STRING:
3587 len += t->a.len = nasm_unquote(t->text, NULL);
3588 break;
3589 case TOK_OTHER:
3590 if (!strcmp(t->text, ",")) /* permit comma separators */
3591 break;
3592 /* else fall through */
3593 default:
3594 error(ERR_NONFATAL,
3595 "non-string passed to `%%strcat' (%d)", t->type);
3596 free_tlist(tline);
3597 free_tlist(origline);
3598 return DIRECTIVE_FOUND;
3599 }
3600 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003601
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003602 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003603 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003604 if (t->type == TOK_STRING) {
3605 memcpy(p, t->text, t->a.len);
3606 p += t->a.len;
3607 }
3608 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003609
3610 /*
3611 * We now have a macro name, an implicit parameter count of
3612 * zero, and a numeric token to use as an expansion. Create
3613 * and store an SMacro.
3614 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003615 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3616 macro_start->text = nasm_quote(pp, len);
3617 nasm_free(pp);
3618 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003619 free_tlist(tline);
3620 free_tlist(origline);
3621 return DIRECTIVE_FOUND;
3622
H. Peter Anvine2c80182005-01-15 22:15:51 +00003623 case PP_SUBSTR:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003624 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003625 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003626 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003627 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003628
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003629 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003630
H. Peter Anvine2c80182005-01-15 22:15:51 +00003631 tline = tline->next;
3632 skip_white_(tline);
3633 tline = expand_id(tline);
3634 if (!tline || (tline->type != TOK_ID &&
3635 (tline->type != TOK_PREPROC_ID ||
3636 tline->text[1] != '$'))) {
3637 error(ERR_NONFATAL,
3638 "`%%substr' expects a macro identifier as first parameter");
3639 free_tlist(origline);
3640 return DIRECTIVE_FOUND;
3641 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003642 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003643 last = tline;
3644 tline = expand_smacro(tline->next);
3645 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003646
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003647 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003648 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003649 while (tok_type_(t, TOK_WHITESPACE))
3650 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003651
H. Peter Anvine2c80182005-01-15 22:15:51 +00003652 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003653 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003654 error(ERR_NONFATAL,
3655 "`%%substr` requires string as second parameter");
3656 free_tlist(tline);
3657 free_tlist(origline);
3658 return DIRECTIVE_FOUND;
3659 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003660
H. Peter Anvine2c80182005-01-15 22:15:51 +00003661 tt = t->next;
3662 tptr = &tt;
3663 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003664 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003665 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003666 if (!evalresult) {
3667 free_tlist(tline);
3668 free_tlist(origline);
3669 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003670 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003671 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3672 free_tlist(tline);
3673 free_tlist(origline);
3674 return DIRECTIVE_FOUND;
3675 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003676 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003677
3678 while (tok_type_(tt, TOK_WHITESPACE))
3679 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003680 if (!tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003681 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003682 } else {
3683 tokval.t_type = TOKEN_INVALID;
3684 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3685 pass, error, NULL);
3686 if (!evalresult) {
3687 free_tlist(tline);
3688 free_tlist(origline);
3689 return DIRECTIVE_FOUND;
3690 } else if (!is_simple(evalresult)) {
3691 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3692 free_tlist(tline);
3693 free_tlist(origline);
3694 return DIRECTIVE_FOUND;
3695 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003696 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003697 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003698
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003699 len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003700 /* make start and count being in range */
3701 if (start < 0)
3702 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003703 if (count < 0)
3704 count = len + count + 1 - start;
3705 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003706 count = len - start;
3707 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003708 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003709
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003710 macro_start = nasm_zalloc(sizeof(*macro_start));
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003711 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003712 macro_start->type = TOK_STRING;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003713
H. Peter Anvine2c80182005-01-15 22:15:51 +00003714 /*
3715 * We now have a macro name, an implicit parameter count of
3716 * zero, and a numeric token to use as an expansion. Create
3717 * and store an SMacro.
3718 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003719 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003720 free_tlist(tline);
3721 free_tlist(origline);
3722 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003723 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003724
H. Peter Anvine2c80182005-01-15 22:15:51 +00003725 case PP_ASSIGN:
3726 case PP_IASSIGN:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003727 if (defining != NULL) return NO_DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003728 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003729
H. Peter Anvine2c80182005-01-15 22:15:51 +00003730 tline = tline->next;
3731 skip_white_(tline);
3732 tline = expand_id(tline);
3733 if (!tline || (tline->type != TOK_ID &&
3734 (tline->type != TOK_PREPROC_ID ||
3735 tline->text[1] != '$'))) {
3736 error(ERR_NONFATAL,
3737 "`%%%sassign' expects a macro identifier",
3738 (i == PP_IASSIGN ? "i" : ""));
3739 free_tlist(origline);
3740 return DIRECTIVE_FOUND;
3741 }
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04003742 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003743 last = tline;
3744 tline = expand_smacro(tline->next);
3745 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003746
H. Peter Anvine2c80182005-01-15 22:15:51 +00003747 t = tline;
3748 tptr = &t;
3749 tokval.t_type = TOKEN_INVALID;
3750 evalresult =
3751 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3752 free_tlist(tline);
3753 if (!evalresult) {
3754 free_tlist(origline);
3755 return DIRECTIVE_FOUND;
3756 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003757
H. Peter Anvine2c80182005-01-15 22:15:51 +00003758 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003759 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003760 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003761
H. Peter Anvine2c80182005-01-15 22:15:51 +00003762 if (!is_simple(evalresult)) {
3763 error(ERR_NONFATAL,
3764 "non-constant value given to `%%%sassign'",
3765 (i == PP_IASSIGN ? "i" : ""));
3766 free_tlist(origline);
3767 return DIRECTIVE_FOUND;
3768 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003769
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03003770 macro_start = nasm_zalloc(sizeof(*macro_start));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003771 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003772
H. Peter Anvine2c80182005-01-15 22:15:51 +00003773 /*
3774 * We now have a macro name, an implicit parameter count of
3775 * zero, and a numeric token to use as an expansion. Create
3776 * and store an SMacro.
3777 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003778 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003779 free_tlist(origline);
3780 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003781
H. Peter Anvine2c80182005-01-15 22:15:51 +00003782 case PP_LINE:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003783 if (defining != NULL) return NO_DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003784 /*
3785 * Syntax is `%line nnn[+mmm] [filename]'
3786 */
3787 tline = tline->next;
3788 skip_white_(tline);
3789 if (!tok_type_(tline, TOK_NUMBER)) {
3790 error(ERR_NONFATAL, "`%%line' expects line number");
3791 free_tlist(origline);
3792 return DIRECTIVE_FOUND;
3793 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003794 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003795 m = 1;
3796 tline = tline->next;
3797 if (tok_is_(tline, "+")) {
3798 tline = tline->next;
3799 if (!tok_type_(tline, TOK_NUMBER)) {
3800 error(ERR_NONFATAL, "`%%line' expects line increment");
3801 free_tlist(origline);
3802 return DIRECTIVE_FOUND;
3803 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003804 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003805 tline = tline->next;
3806 }
3807 skip_white_(tline);
3808 src_set_linnum(k);
3809 istk->lineinc = m;
3810 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003811 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003812 }
3813 free_tlist(origline);
3814 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003815
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003816 case PP_WHILE:
3817 if (defining != NULL) {
3818 if (defining->type == EXP_WHILE) {
3819 defining->def_depth ++;
3820 }
3821 return NO_DIRECTIVE_FOUND;
3822 }
3823 l = NULL;
3824 if ((istk->expansion != NULL) &&
3825 (istk->expansion->emitting == false)) {
3826 j = COND_NEVER;
3827 } else {
3828 l = new_Line();
3829 l->first = copy_Token(tline->next);
3830 j = if_condition(tline->next, i);
3831 tline->next = NULL; /* it got freed */
3832 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
3833 }
3834 ed = new_ExpDef(EXP_WHILE);
3835 ed->state = j;
3836 ed->cur_depth = 1;
3837 ed->max_depth = DEADMAN_LIMIT;
3838 ed->ignoring = ((ed->state == COND_IF_TRUE) ? false : true);
3839 if (ed->ignoring == false) {
3840 ed->line = l;
3841 ed->last = l;
3842 } else if (l != NULL) {
3843 delete_Token(l->first);
3844 nasm_free(l);
3845 l = NULL;
3846 }
3847 ed->prev = defining;
3848 defining = ed;
3849 free_tlist(origline);
3850 return DIRECTIVE_FOUND;
3851
3852 case PP_ENDWHILE:
3853 if (defining != NULL) {
3854 if (defining->type == EXP_WHILE) {
3855 if (defining->def_depth > 0) {
3856 defining->def_depth --;
3857 return NO_DIRECTIVE_FOUND;
3858 }
3859 } else {
3860 return NO_DIRECTIVE_FOUND;
3861 }
3862 }
3863 if (tline->next != NULL) {
3864 error_precond(ERR_WARNING|ERR_PASS1,
3865 "trailing garbage after `%%endwhile' ignored");
3866 }
3867 if ((defining == NULL) || (defining->type != EXP_WHILE)) {
3868 error(ERR_NONFATAL, "`%%endwhile': no matching `%%while'");
3869 return DIRECTIVE_FOUND;
3870 }
3871 ed = defining;
3872 defining = ed->prev;
3873 if (ed->ignoring == false) {
3874 ed->prev = expansions;
3875 expansions = ed;
3876 ei = new_ExpInv(EXP_WHILE, ed);
3877 ei->current = ed->line->next;
3878 ei->emitting = true;
3879 ei->prev = istk->expansion;
3880 istk->expansion = ei;
3881 } else {
3882 nasm_free(ed);
3883 }
3884 free_tlist(origline);
3885 return DIRECTIVE_FOUND;
3886
3887 case PP_EXITWHILE:
3888 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3889 /*
3890 * We must search along istk->expansion until we hit a
3891 * while invocation. Then we disable the emitting state(s)
3892 * between exitwhile and endwhile.
3893 */
3894 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
3895 if (ei->type == EXP_WHILE) {
3896 break;
3897 }
3898 }
3899
3900 if (ei != NULL) {
3901 /*
3902 * Set all invocations leading back to the while
3903 * invocation to a non-emitting state.
3904 */
3905 for (eei = istk->expansion; eei != ei; eei = eei->prev) {
3906 eei->emitting = false;
3907 }
3908 eei->emitting = false;
3909 eei->current = NULL;
3910 eei->def->cur_depth = eei->def->max_depth;
3911 } else {
3912 error(ERR_NONFATAL, "`%%exitwhile' not within `%%while' block");
3913 }
3914 free_tlist(origline);
3915 return DIRECTIVE_FOUND;
3916
3917 case PP_COMMENT:
3918 if (defining != NULL) {
3919 if (defining->type == EXP_COMMENT) {
3920 defining->def_depth ++;
3921 }
3922 return NO_DIRECTIVE_FOUND;
3923 }
3924 ed = new_ExpDef(EXP_COMMENT);
3925 ed->ignoring = true;
3926 ed->prev = defining;
3927 defining = ed;
3928 free_tlist(origline);
3929 return DIRECTIVE_FOUND;
3930
3931 case PP_ENDCOMMENT:
3932 if (defining != NULL) {
3933 if (defining->type == EXP_COMMENT) {
3934 if (defining->def_depth > 0) {
3935 defining->def_depth --;
3936 return NO_DIRECTIVE_FOUND;
3937 }
3938 } else {
3939 return NO_DIRECTIVE_FOUND;
3940 }
3941 }
3942 if ((defining == NULL) || (defining->type != EXP_COMMENT)) {
3943 error(ERR_NONFATAL, "`%%endcomment': no matching `%%comment'");
3944 return DIRECTIVE_FOUND;
3945 }
3946 ed = defining;
3947 defining = ed->prev;
3948 nasm_free(ed);
3949 free_tlist(origline);
3950 return DIRECTIVE_FOUND;
3951
3952 case PP_FINAL:
3953 if (defining != NULL) return NO_DIRECTIVE_FOUND;
3954 if (in_final != false) {
3955 error(ERR_FATAL, "`%%final' cannot be used recursively");
3956 }
3957 tline = tline->next;
3958 skip_white_(tline);
3959 if (tline == NULL) {
3960 error(ERR_NONFATAL, "`%%final' expects at least one parameter");
3961 } else {
3962 l = new_Line();
3963 l->first = copy_Token(tline);
3964 l->next = finals;
3965 finals = l;
3966 }
3967 free_tlist(origline);
3968 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003969
H. Peter Anvine2c80182005-01-15 22:15:51 +00003970 default:
3971 error(ERR_FATAL,
3972 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003973 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003974 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003975 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003976}
3977
3978/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003979 * Ensure that a macro parameter contains a condition code and
3980 * nothing else. Return the condition code index if so, or -1
3981 * otherwise.
3982 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003983static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003984{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003985 Token *tt;
3986 int i, j, k, m;
3987
H. Peter Anvin25a99342007-09-22 17:45:45 -07003988 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003989 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003990
H. Peter Anvineba20a72002-04-30 20:53:55 +00003991 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003992 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003993 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003994 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003995 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003996 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003997 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003998
3999 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +04004000 j = ARRAY_SIZE(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004001 while (j - i > 1) {
4002 k = (j + i) / 2;
4003 m = nasm_stricmp(t->text, conditions[k]);
4004 if (m == 0) {
4005 i = k;
4006 j = -2;
4007 break;
4008 } else if (m < 0) {
4009 j = k;
4010 } else
4011 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004012 }
4013 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004014 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004015 return i;
4016}
4017
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004018static bool paste_tokens(Token **head, const struct tokseq_match *m,
4019 int mnum, bool handle_paste_tokens)
H. Peter Anvind784a082009-04-20 14:01:18 -07004020{
4021 Token **tail, *t, *tt;
4022 Token **paste_head;
4023 bool did_paste = false;
4024 char *tmp;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004025 int i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004026
Cyrill Gorcunov99a055a2012-02-27 11:11:33 +04004027 nasm_dump_stream(*head);
4028 nasm_dump_token(*head);
4029
H. Peter Anvind784a082009-04-20 14:01:18 -07004030 /* Now handle token pasting... */
4031 paste_head = NULL;
4032 tail = head;
4033 while ((t = *tail) && (tt = t->next)) {
4034 switch (t->type) {
4035 case TOK_WHITESPACE:
4036 if (tt->type == TOK_WHITESPACE) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004037 /* Zap adjacent whitespace tokens */
H. Peter Anvind784a082009-04-20 14:01:18 -07004038 t->next = delete_Token(tt);
4039 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004040 /* Do not advance paste_head here */
4041 tail = &t->next;
4042 }
H. Peter Anvind784a082009-04-20 14:01:18 -07004043 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004044 case TOK_PASTE: /* %+ */
4045 if (handle_paste_tokens) {
4046 /* Zap %+ and whitespace tokens to the right */
4047 while (t && (t->type == TOK_WHITESPACE ||
4048 t->type == TOK_PASTE))
4049 t = *tail = delete_Token(t);
Cyrill Gorcunov99a055a2012-02-27 11:11:33 +04004050 if (!t) { /* Dangling %+ term */
4051 if (paste_head)
4052 (*paste_head)->next = NULL;
4053 else
4054 *head = NULL;
4055 return did_paste;
4056 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004057 tail = paste_head;
4058 t = *tail;
4059 tt = t->next;
4060 while (tok_type_(tt, TOK_WHITESPACE))
4061 tt = t->next = delete_Token(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004062 if (tt) {
4063 tmp = nasm_strcat(t->text, tt->text);
4064 delete_Token(t);
4065 tt = delete_Token(tt);
4066 t = *tail = tokenize(tmp);
4067 nasm_free(tmp);
4068 while (t->next) {
4069 tail = &t->next;
4070 t = t->next;
4071 }
4072 t->next = tt; /* Attach the remaining token chain */
4073 did_paste = true;
4074 }
4075 paste_head = tail;
4076 tail = &t->next;
4077 break;
4078 }
4079 /* else fall through */
4080 default:
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004081 /*
4082 * Concatenation of tokens might look nontrivial
4083 * but in real it's pretty simple -- the caller
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004084 * prepares the masks of token types to be concatenated
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004085 * and we simply find matched sequences and slip
4086 * them together
4087 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004088 for (i = 0; i < mnum; i++) {
4089 if (PP_CONCAT_MASK(t->type) & m[i].mask_head) {
4090 size_t len = 0;
4091 char *tmp, *p;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004092
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004093 while (tt && (PP_CONCAT_MASK(tt->type) & m[i].mask_tail)) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004094 len += strlen(tt->text);
4095 tt = tt->next;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004096 }
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004097
Cyrill Gorcunovfdd0ac52011-06-27 01:22:27 +04004098 nasm_dump_token(tt);
4099
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004100 /*
4101 * Now tt points to the first token after
4102 * the potential paste area...
4103 */
4104 if (tt != t->next) {
4105 /* We have at least two tokens... */
4106 len += strlen(t->text);
4107 p = tmp = nasm_malloc(len+1);
4108 while (t != tt) {
4109 strcpy(p, t->text);
4110 p = strchr(p, '\0');
4111 t = delete_Token(t);
4112 }
4113 t = *tail = tokenize(tmp);
4114 nasm_free(tmp);
4115 while (t->next) {
4116 tail = &t->next;
4117 t = t->next;
4118 }
4119 t->next = tt; /* Attach the remaining token chain */
4120 did_paste = true;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004121 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004122 paste_head = tail;
4123 tail = &t->next;
4124 break;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004125 }
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004126 }
4127 if (i >= mnum) { /* no match */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004128 tail = &t->next;
4129 if (!tok_type_(t->next, TOK_WHITESPACE))
4130 paste_head = tail;
4131 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004132 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004133 }
4134 }
4135 return did_paste;
4136}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004137
4138/*
4139 * expands to a list of tokens from %{x:y}
4140 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004141static Token *expand_mmac_params_range(ExpInv *ei, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004142{
4143 Token *t = tline, **tt, *tm, *head;
4144 char *pos;
4145 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004146
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004147 pos = strchr(tline->text, ':');
4148 nasm_assert(pos);
4149
4150 lst = atoi(pos + 1);
4151 fst = atoi(tline->text + 1);
4152
4153 /*
4154 * only macros params are accounted so
4155 * if someone passes %0 -- we reject such
4156 * value(s)
4157 */
4158 if (lst == 0 || fst == 0)
4159 goto err;
4160
4161 /* the values should be sane */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004162 if ((fst > (int)ei->nparam || fst < (-(int)ei->nparam)) ||
4163 (lst > (int)ei->nparam || lst < (-(int)ei->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004164 goto err;
4165
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004166 fst = fst < 0 ? fst + (int)ei->nparam + 1: fst;
4167 lst = lst < 0 ? lst + (int)ei->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004168
4169 /* counted from zero */
4170 fst--, lst--;
4171
4172 /*
4173 * it will be at least one token
4174 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004175 tm = ei->params[(fst + ei->rotate) % ei->nparam];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004176 t = new_Token(NULL, tm->type, tm->text, 0);
4177 head = t, tt = &t->next;
4178 if (fst < lst) {
4179 for (i = fst + 1; i <= lst; i++) {
4180 t = new_Token(NULL, TOK_OTHER, ",", 0);
4181 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004182 j = (i + ei->rotate) % ei->nparam;
4183 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004184 t = new_Token(NULL, tm->type, tm->text, 0);
4185 *tt = t, tt = &t->next;
4186 }
4187 } else {
4188 for (i = fst - 1; i >= lst; i--) {
4189 t = new_Token(NULL, TOK_OTHER, ",", 0);
4190 *tt = t, tt = &t->next;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004191 j = (i + ei->rotate) % ei->nparam;
4192 tm = ei->params[j];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004193 t = new_Token(NULL, tm->type, tm->text, 0);
4194 *tt = t, tt = &t->next;
4195 }
4196 }
4197
4198 *last = tt;
4199 return head;
4200
4201err:
4202 error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
4203 &tline->text[1]);
4204 return tline;
4205}
4206
H. Peter Anvin76690a12002-04-30 20:52:49 +00004207/*
4208 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004209 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004210 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004211 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004212static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004213{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004214 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004215 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004216 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004217
4218 tail = &thead;
4219 thead = NULL;
4220
Cyrill Gorcunov2e046002011-06-26 23:33:56 +04004221 nasm_dump_stream(tline);
4222
H. Peter Anvine2c80182005-01-15 22:15:51 +00004223 while (tline) {
4224 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004225 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4226 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4227 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004228 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004229 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004230 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004231 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004232 int i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004233 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004234
H. Peter Anvine2c80182005-01-15 22:15:51 +00004235 t = tline;
4236 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004237
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004238 for (ei = istk->expansion; ei != NULL; ei = ei->prev) {
4239 if (ei->type == EXP_MMACRO) {
4240 break;
4241 }
4242 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004243 if (ei == NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004244 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004245 } else {
4246 pos = strchr(t->text, ':');
4247 if (!pos) {
4248 switch (t->text[1]) {
4249 /*
4250 * We have to make a substitution of one of the
4251 * forms %1, %-1, %+1, %%foo, %0.
4252 */
4253 case '0':
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004254 if ((strlen(t->text) > 2) && (t->text[2] == '0')) {
4255 type = TOK_ID;
4256 text = nasm_strdup(ei->label_text);
4257 } else {
4258 type = TOK_NUMBER;
4259 snprintf(tmpbuf, sizeof(tmpbuf), "%d", ei->nparam);
4260 text = nasm_strdup(tmpbuf);
4261 }
4262 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004263 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004264 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004265 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004266 ei->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004267 text = nasm_strcat(tmpbuf, t->text + 2);
4268 break;
4269 case '-':
4270 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004271 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004272 tt = NULL;
4273 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004274 if (ei->nparam > 1)
4275 n = (n + ei->rotate) % ei->nparam;
4276 tt = ei->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004277 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004278 cc = find_cc(tt);
4279 if (cc == -1) {
4280 error(ERR_NONFATAL,
4281 "macro parameter %d is not a condition code",
4282 n + 1);
4283 text = NULL;
4284 } else {
4285 type = TOK_ID;
4286 if (inverse_ccs[cc] == -1) {
4287 error(ERR_NONFATAL,
4288 "condition code `%s' is not invertible",
4289 conditions[cc]);
4290 text = NULL;
4291 } else
4292 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4293 }
4294 break;
4295 case '+':
4296 n = atoi(t->text + 2) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004297 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004298 tt = NULL;
4299 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004300 if (ei->nparam > 1)
4301 n = (n + ei->rotate) % ei->nparam;
4302 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004303 }
4304 cc = find_cc(tt);
4305 if (cc == -1) {
4306 error(ERR_NONFATAL,
4307 "macro parameter %d is not a condition code",
4308 n + 1);
4309 text = NULL;
4310 } else {
4311 type = TOK_ID;
4312 text = nasm_strdup(conditions[cc]);
4313 }
4314 break;
4315 default:
4316 n = atoi(t->text + 1) - 1;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004317 if (n >= ei->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004318 tt = NULL;
4319 else {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004320 if (ei->nparam > 1)
4321 n = (n + ei->rotate) % ei->nparam;
4322 tt = ei->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004323 }
4324 if (tt) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004325 for (i = 0; i < ei->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004326 *tail = new_Token(NULL, tt->type, tt->text, 0);
4327 tail = &(*tail)->next;
4328 tt = tt->next;
4329 }
4330 }
4331 text = NULL; /* we've done it here */
4332 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004333 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004334 } else {
4335 /*
4336 * seems we have a parameters range here
4337 */
4338 Token *head, **last;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004339 head = expand_mmac_params_range(ei, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004340 if (head != t) {
4341 *tail = head;
4342 *last = tline;
4343 tline = head;
4344 text = NULL;
4345 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004346 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004347 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004348 if (!text) {
4349 delete_Token(t);
4350 } else {
4351 *tail = t;
4352 tail = &t->next;
4353 t->type = type;
4354 nasm_free(t->text);
4355 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004356 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004357 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004358 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004359 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004360 } else if (tline->type == TOK_INDIRECT) {
4361 t = tline;
4362 tline = tline->next;
4363 tt = tokenize(t->text);
4364 tt = expand_mmac_params(tt);
4365 tt = expand_smacro(tt);
4366 *tail = tt;
4367 while (tt) {
4368 tt->a.mac = NULL; /* Necessary? */
4369 tail = &tt->next;
4370 tt = tt->next;
4371 }
4372 delete_Token(t);
4373 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004374 } else {
4375 t = *tail = tline;
4376 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004377 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004378 tail = &t->next;
4379 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004380 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004381 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004382
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004383 if (changed) {
4384 const struct tokseq_match t[] = {
4385 {
4386 PP_CONCAT_MASK(TOK_ID) |
4387 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4388 PP_CONCAT_MASK(TOK_ID) |
4389 PP_CONCAT_MASK(TOK_NUMBER) |
4390 PP_CONCAT_MASK(TOK_FLOAT) |
4391 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4392 },
4393 {
4394 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4395 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4396 }
4397 };
4398 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4399 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004400
Cyrill Gorcunov2e046002011-06-26 23:33:56 +04004401 nasm_dump_token(thead);
4402
H. Peter Anvin76690a12002-04-30 20:52:49 +00004403 return thead;
4404}
4405
4406/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004407 * Expand all single-line macro calls made in the given line.
4408 * Return the expanded version of the line. The original is deemed
4409 * to be destroyed in the process. (In reality we'll just move
4410 * Tokens from input to output a lot of the time, rather than
4411 * actually bothering to destroy and replicate.)
4412 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004413
H. Peter Anvine2c80182005-01-15 22:15:51 +00004414static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004415{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004416 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004417 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004418 Token **params;
4419 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004420 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004421 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004422 Token *org_tline = tline;
4423 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004424 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004425 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004426 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004427
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004428 /*
4429 * Trick: we should avoid changing the start token pointer since it can
4430 * be contained in "next" field of other token. Because of this
4431 * we allocate a copy of first token and work with it; at the end of
4432 * routine we copy it back
4433 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004434 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004435 tline = new_Token(org_tline->next, org_tline->type,
4436 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004437 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004438 nasm_free(org_tline->text);
4439 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004440 }
4441
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004442 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004443
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004444again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004445 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004446 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004447
H. Peter Anvine2c80182005-01-15 22:15:51 +00004448 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004449 if (!--deadman) {
4450 error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004451 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004452 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004453
H. Peter Anvine2c80182005-01-15 22:15:51 +00004454 if ((mname = tline->text)) {
4455 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004456 if (tline->type == TOK_ID) {
4457 head = (SMacro *)hash_findix(&smacros, mname);
4458 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov290eac72011-06-28 01:59:05 +04004459 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004460 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4461 } else
4462 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004463
H. Peter Anvine2c80182005-01-15 22:15:51 +00004464 /*
4465 * We've hit an identifier. As in is_mmacro below, we first
4466 * check whether the identifier is a single-line macro at
4467 * all, then think about checking for parameters if
4468 * necessary.
4469 */
Cyrill Gorcunov8a887502011-11-20 10:42:13 +04004470 list_for_each(m, head) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004471 if (!mstrcmp(m->name, mname, m->casesense))
4472 break;
Cyrill Gorcunov8a887502011-11-20 10:42:13 +04004473 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004474 if (m) {
4475 mstart = tline;
4476 params = NULL;
4477 paramsize = NULL;
4478 if (m->nparam == 0) {
4479 /*
4480 * Simple case: the macro is parameterless. Discard the
4481 * one token that the macro call took, and push the
4482 * expansion back on the to-do stack.
4483 */
4484 if (!m->expansion) {
4485 if (!strcmp("__FILE__", m->name)) {
4486 int32_t num = 0;
4487 char *file = NULL;
4488 src_get(&num, &file);
4489 tline->text = nasm_quote(file, strlen(file));
4490 tline->type = TOK_STRING;
4491 nasm_free(file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004492 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004493 }
4494 if (!strcmp("__LINE__", m->name)) {
4495 nasm_free(tline->text);
4496 make_tok_num(tline, src_get_linnum());
4497 continue;
4498 }
4499 if (!strcmp("__BITS__", m->name)) {
4500 nasm_free(tline->text);
4501 make_tok_num(tline, globalbits);
4502 continue;
4503 }
4504 tline = delete_Token(tline);
4505 continue;
4506 }
4507 } else {
4508 /*
4509 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004510 * exists and takes parameters. We must find the
4511 * parameters in the call, count them, find the SMacro
4512 * that corresponds to that form of the macro call, and
4513 * substitute for the parameters when we expand. What a
4514 * pain.
4515 */
4516 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004517 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004518 do {
4519 t = tline->next;
4520 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004521 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004522 t->text = NULL;
4523 t = tline->next = delete_Token(t);
4524 }
4525 tline = t;
4526 } while (tok_type_(tline, TOK_WHITESPACE));
4527 if (!tok_is_(tline, "(")) {
4528 /*
4529 * This macro wasn't called with parameters: ignore
4530 * the call. (Behaviour borrowed from gnu cpp.)
4531 */
4532 tline = mstart;
4533 m = NULL;
4534 } else {
4535 int paren = 0;
4536 int white = 0;
4537 brackets = 0;
4538 nparam = 0;
4539 sparam = PARAM_DELTA;
4540 params = nasm_malloc(sparam * sizeof(Token *));
4541 params[0] = tline->next;
4542 paramsize = nasm_malloc(sparam * sizeof(int));
4543 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004544 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004545 /*
4546 * For some unusual expansions
4547 * which concatenates function call
4548 */
4549 t = tline->next;
4550 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004551 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004552 t->text = NULL;
4553 t = tline->next = delete_Token(t);
4554 }
4555 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004556
H. Peter Anvine2c80182005-01-15 22:15:51 +00004557 if (!tline) {
4558 error(ERR_NONFATAL,
4559 "macro call expects terminating `)'");
4560 break;
4561 }
4562 if (tline->type == TOK_WHITESPACE
4563 && brackets <= 0) {
4564 if (paramsize[nparam])
4565 white++;
4566 else
4567 params[nparam] = tline->next;
4568 continue; /* parameter loop */
4569 }
4570 if (tline->type == TOK_OTHER
4571 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004572 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004573 if (ch == ',' && !paren && brackets <= 0) {
4574 if (++nparam >= sparam) {
4575 sparam += PARAM_DELTA;
4576 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004577 sparam * sizeof(Token *));
4578 paramsize = nasm_realloc(paramsize,
4579 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004580 }
4581 params[nparam] = tline->next;
4582 paramsize[nparam] = 0;
4583 white = 0;
4584 continue; /* parameter loop */
4585 }
4586 if (ch == '{' &&
4587 (brackets > 0 || (brackets == 0 &&
4588 !paramsize[nparam])))
4589 {
4590 if (!(brackets++)) {
4591 params[nparam] = tline->next;
4592 continue; /* parameter loop */
4593 }
4594 }
4595 if (ch == '}' && brackets > 0)
4596 if (--brackets == 0) {
4597 brackets = -1;
4598 continue; /* parameter loop */
4599 }
4600 if (ch == '(' && !brackets)
4601 paren++;
4602 if (ch == ')' && brackets <= 0)
4603 if (--paren < 0)
4604 break;
4605 }
4606 if (brackets < 0) {
4607 brackets = 0;
4608 error(ERR_NONFATAL, "braces do not "
4609 "enclose all of macro parameter");
4610 }
4611 paramsize[nparam] += white + 1;
4612 white = 0;
4613 } /* parameter loop */
4614 nparam++;
4615 while (m && (m->nparam != nparam ||
4616 mstrcmp(m->name, mname,
4617 m->casesense)))
4618 m = m->next;
4619 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004620 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004621 "macro `%s' exists, "
4622 "but not taking %d parameters",
4623 mstart->text, nparam);
4624 }
4625 }
4626 if (m && m->in_progress)
4627 m = NULL;
4628 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004629 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004630 * Design question: should we handle !tline, which
4631 * indicates missing ')' here, or expand those
4632 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004633 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004634 */
4635 nasm_free(params);
4636 nasm_free(paramsize);
4637 tline = mstart;
4638 } else {
4639 /*
4640 * Expand the macro: we are placed on the last token of the
4641 * call, so that we can easily split the call from the
4642 * following tokens. We also start by pushing an SMAC_END
4643 * token for the cycle removal.
4644 */
4645 t = tline;
4646 if (t) {
4647 tline = t->next;
4648 t->next = NULL;
4649 }
4650 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004651 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004652 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004653 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004654 list_for_each(t, m->expansion) {
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04004655 if (is_smacro_param(t)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004656 Token *pcopy = tline, **ptail = &pcopy;
Cyrill Gorcunov0ad6a7b2011-06-30 01:36:45 +04004657 Token *ttt;
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04004658 int i, idx;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004659
Cyrill Gorcunov194ba892011-06-30 01:16:35 +04004660 idx = smacro_get_param_idx(t);
4661 ttt = params[idx];
Cyrill Gorcunov0ad6a7b2011-06-30 01:36:45 +04004662
4663 /*
4664 * We need smacro paramters appended.
4665 */
4666 for (i = paramsize[idx]; i > 0; i--) {
4667 *ptail = new_Token(tline, ttt->type, ttt->text, 0);
4668 ptail = &(*ptail)->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004669 ttt = ttt->next;
4670 }
Cyrill Gorcunov0ad6a7b2011-06-30 01:36:45 +04004671
H. Peter Anvine2c80182005-01-15 22:15:51 +00004672 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004673 } else if (t->type == TOK_PREPROC_Q) {
4674 tt = new_Token(tline, TOK_ID, mname, 0);
4675 tline = tt;
4676 } else if (t->type == TOK_PREPROC_QQ) {
4677 tt = new_Token(tline, TOK_ID, m->name, 0);
4678 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004679 } else {
4680 tt = new_Token(tline, t->type, t->text, 0);
4681 tline = tt;
4682 }
4683 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004684
H. Peter Anvine2c80182005-01-15 22:15:51 +00004685 /*
4686 * Having done that, get rid of the macro call, and clean
4687 * up the parameters.
4688 */
4689 nasm_free(params);
4690 nasm_free(paramsize);
4691 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004692 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004693 continue; /* main token loop */
4694 }
4695 }
4696 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004697
H. Peter Anvine2c80182005-01-15 22:15:51 +00004698 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004699 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004700 tline = delete_Token(tline);
4701 } else {
4702 t = *tail = tline;
4703 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004704 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004705 t->next = NULL;
4706 tail = &t->next;
4707 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004708 }
4709
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004710 /*
4711 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004712 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004713 * TOK_IDs should be concatenated.
4714 * Also we look for %+ tokens and concatenate the tokens before and after
4715 * them (without white spaces in between).
4716 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004717 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004718 const struct tokseq_match t[] = {
4719 {
4720 PP_CONCAT_MASK(TOK_ID) |
4721 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4722 PP_CONCAT_MASK(TOK_ID) |
4723 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4724 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4725 }
4726 };
4727 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004728 /*
4729 * If we concatenated something, *and* we had previously expanded
4730 * an actual macro, scan the lines again for macros...
4731 */
4732 tline = thead;
4733 expanded = false;
4734 goto again;
4735 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004736 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004737
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004738err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004739 if (org_tline) {
4740 if (thead) {
4741 *org_tline = *thead;
4742 /* since we just gave text to org_line, don't free it */
4743 thead->text = NULL;
4744 delete_Token(thead);
4745 } else {
4746 /* the expression expanded to empty line;
4747 we can't return NULL for some reasons
4748 we just set the line to a single WHITESPACE token. */
4749 memset(org_tline, 0, sizeof(*org_tline));
4750 org_tline->text = NULL;
4751 org_tline->type = TOK_WHITESPACE;
4752 }
4753 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004754 }
4755
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004756 return thead;
4757}
4758
4759/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004760 * Similar to expand_smacro but used exclusively with macro identifiers
4761 * right before they are fetched in. The reason is that there can be
4762 * identifiers consisting of several subparts. We consider that if there
4763 * are more than one element forming the name, user wants a expansion,
4764 * otherwise it will be left as-is. Example:
4765 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004766 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004767 *
4768 * the identifier %$abc will be left as-is so that the handler for %define
4769 * will suck it and define the corresponding value. Other case:
4770 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004771 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004772 *
4773 * In this case user wants name to be expanded *before* %define starts
4774 * working, so we'll expand %$abc into something (if it has a value;
4775 * otherwise it will be left as-is) then concatenate all successive
4776 * PP_IDs into one.
4777 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004778static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004779{
4780 Token *cur, *oldnext = NULL;
4781
H. Peter Anvin734b1882002-04-30 21:01:08 +00004782 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004783 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004784
4785 cur = tline;
4786 while (cur->next &&
Cyrill Gorcunov5b6c96b2011-06-30 00:22:53 +04004787 (cur->next->type == TOK_ID ||
4788 cur->next->type == TOK_PREPROC_ID ||
4789 cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004790 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004791
4792 /* If identifier consists of just one token, don't expand */
4793 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004794 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004795
H. Peter Anvine2c80182005-01-15 22:15:51 +00004796 if (cur) {
4797 oldnext = cur->next; /* Detach the tail past identifier */
4798 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004799 }
4800
H. Peter Anvin734b1882002-04-30 21:01:08 +00004801 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004802
H. Peter Anvine2c80182005-01-15 22:15:51 +00004803 if (cur) {
4804 /* expand_smacro possibly changhed tline; re-scan for EOL */
4805 cur = tline;
4806 while (cur && cur->next)
4807 cur = cur->next;
4808 if (cur)
4809 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004810 }
4811
4812 return tline;
4813}
4814
4815/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004816 * Determine whether the given line constitutes a multi-line macro
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004817 * call, and return the ExpDef structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004818 * to check for an initial label - that's taken care of in
4819 * expand_mmacro - but must check numbers of parameters. Guaranteed
4820 * to be called with tline->type == TOK_ID, so the putative macro
4821 * name is easy to find.
4822 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004823static ExpDef *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004824{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004825 ExpDef *head, *ed;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004826 Token **params;
4827 int nparam;
4828
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004829 head = (ExpDef *) hash_findix(&expdefs, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004830
4831 /*
4832 * Efficiency: first we see if any macro exists with the given
4833 * name. If not, we can return NULL immediately. _Then_ we
4834 * count the parameters, and then we look further along the
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004835 * list if necessary to find the proper ExpDef.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004836 */
Cyrill Gorcunov8a887502011-11-20 10:42:13 +04004837 list_for_each(ed, head) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004838 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004839 break;
Cyrill Gorcunov8a887502011-11-20 10:42:13 +04004840 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004841 if (!ed)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004842 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004843
4844 /*
4845 * OK, we have a potential macro. Count and demarcate the
4846 * parameters.
4847 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004848 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004849
4850 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004851 * So we know how many parameters we've got. Find the ExpDef
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004852 * structure that handles this number.
4853 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004854 while (ed) {
4855 if (ed->nparam_min <= nparam
4856 && (ed->plus || nparam <= ed->nparam_max)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004857 /*
4858 * It's right, and we can use it. Add its default
4859 * parameters to the end of our list if necessary.
4860 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004861 if (ed->defaults && nparam < ed->nparam_min + ed->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004862 params =
4863 nasm_realloc(params,
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004864 ((ed->nparam_min + ed->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004865 1) * sizeof(*params)));
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004866 while (nparam < ed->nparam_min + ed->ndefs) {
4867 params[nparam] = ed->defaults[nparam - ed->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004868 nparam++;
4869 }
4870 }
4871 /*
4872 * If we've gone over the maximum parameter count (and
4873 * we're in Plus mode), ignore parameters beyond
4874 * nparam_max.
4875 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004876 if (ed->plus && nparam > ed->nparam_max)
4877 nparam = ed->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004878 /*
4879 * Then terminate the parameter list, and leave.
4880 */
4881 if (!params) { /* need this special case */
4882 params = nasm_malloc(sizeof(*params));
4883 nparam = 0;
4884 }
4885 params[nparam] = NULL;
4886 *params_array = params;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004887 return ed;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004888 }
4889 /*
4890 * This one wasn't right: look for the next one with the
4891 * same name.
4892 */
Cyrill Gorcunov8a887502011-11-20 10:42:13 +04004893 list_for_each(ed, ed->next) {
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004894 if (!mstrcmp(ed->name, tline->text, ed->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004895 break;
Cyrill Gorcunov8a887502011-11-20 10:42:13 +04004896 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004897 }
4898
4899 /*
4900 * After all that, we didn't find one with the right number of
4901 * parameters. Issue a warning, and fail to expand the macro.
4902 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004903 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004904 "macro `%s' exists, but not taking %d parameters",
4905 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004906 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004907 return NULL;
4908}
4909
4910/*
4911 * Expand the multi-line macro call made by the given line, if
4912 * there is one to be expanded. If there is, push the expansion on
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004913 * istk->expansion and return true. Otherwise return false.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004914 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004915static bool expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004916{
H. Peter Anvineba20a72002-04-30 20:53:55 +00004917 Token *label = NULL;
4918 int dont_prepend = 0;
Cyrill Gorcunovfb27fc22011-06-25 12:08:30 +04004919 Token **params, *t;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004920 Line *l = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004921 ExpDef *ed;
4922 ExpInv *ei;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004923 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004924 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004925
4926 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004927 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004928 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004929 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004930 return false;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004931 ed = is_mmacro(t, &params);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004932 if (ed != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004933 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004934 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004935 Token *last;
4936 /*
4937 * We have an id which isn't a macro call. We'll assume
4938 * it might be a label; we'll also check to see if a
4939 * colon follows it. Then, if there's another id after
4940 * that lot, we'll check it again for macro-hood.
4941 */
4942 label = last = t;
4943 t = t->next;
4944 if (tok_type_(t, TOK_WHITESPACE))
4945 last = t, t = t->next;
4946 if (tok_is_(t, ":")) {
4947 dont_prepend = 1;
4948 last = t, t = t->next;
4949 if (tok_type_(t, TOK_WHITESPACE))
4950 last = t, t = t->next;
4951 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004952 if (!tok_type_(t, TOK_ID) || !(ed = is_mmacro(t, &params)))
4953 return false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004954 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004955 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004956 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004957 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004958
4959 /*
4960 * Fix up the parameters: this involves stripping leading and
4961 * trailing whitespace, then stripping braces if they are
4962 * present.
4963 */
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004964 for (nparam = 0; params[nparam]; nparam++)
4965 ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004966 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004967
H. Peter Anvine2c80182005-01-15 22:15:51 +00004968 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004969 int brace = false;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05004970 int comma = (!ed->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004971
H. Peter Anvine2c80182005-01-15 22:15:51 +00004972 t = params[i];
4973 skip_white_(t);
4974 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004975 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004976 params[i] = t;
4977 paramlen[i] = 0;
4978 while (t) {
4979 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4980 break; /* ... because we have hit a comma */
4981 if (comma && t->type == TOK_WHITESPACE
4982 && tok_is_(t->next, ","))
4983 break; /* ... or a space then a comma */
4984 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4985 break; /* ... or a brace */
4986 t = t->next;
4987 paramlen[i]++;
4988 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004989 }
4990
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004991 if (ed->cur_depth >= ed->max_depth) {
4992 if (ed->max_depth > 1) {
4993 error(ERR_WARNING,
4994 "reached maximum macro recursion depth of %i for %s",
4995 ed->max_depth,ed->name);
4996 }
4997 return false;
4998 } else {
4999 ed->cur_depth ++;
5000 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005001
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005002 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005003 * OK, we have found a ExpDef structure representing a
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005004 * previously defined mmacro. Create an expansion invocation
5005 * and point it back to the expansion definition. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005006 * parameter tokens and macro-local tokens doesn't get done
5007 * until the single-line macro substitution process; this is
5008 * because delaying them allows us to change the semantics
5009 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005010 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005011 ei = new_ExpInv(EXP_MMACRO, ed);
5012 ei->name = nasm_strdup(mname);
5013 //ei->label = label;
5014 //ei->label_text = detoken(label, false);
5015 ei->current = ed->line;
5016 ei->emitting = true;
5017 //ei->iline = tline;
5018 ei->params = params;
5019 ei->nparam = nparam;
5020 ei->rotate = 0;
5021 ei->paramlen = paramlen;
5022 ei->lineno = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005023
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005024 ei->prev = istk->expansion;
5025 istk->expansion = ei;
5026
5027 /*
5028 * Special case: detect %00 on first invocation; if found,
5029 * avoid emitting any labels that precede the mmacro call.
5030 * ed->prepend is set to -1 when %00 is detected, else 1.
5031 */
5032 if (ed->prepend == 0) {
5033 for (l = ed->line; l != NULL; l = l->next) {
5034 for (t = l->first; t != NULL; t = t->next) {
5035 if ((t->type == TOK_PREPROC_ID) &&
5036 (strlen(t->text) == 3) &&
5037 (t->text[1] == '0') && (t->text[2] == '0')) {
5038 dont_prepend = -1;
5039 break;
5040 }
5041 }
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005042 if (dont_prepend < 0)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005043 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005044 }
5045 ed->prepend = ((dont_prepend < 0) ? -1 : 1);
5046 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005047
5048 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00005049 * If we had a label, push it on as the first line of
5050 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005051 */
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005052 if (label != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005053 if (ed->prepend < 0) {
5054 ei->label_text = detoken(label, false);
5055 } else {
5056 if (dont_prepend == 0) {
5057 t = label;
5058 while (t->next != NULL) {
5059 t = t->next;
5060 }
5061 t->next = new_Token(NULL, TOK_OTHER, ":", 0);
5062 }
5063 l = new_Line();
5064 l->first = copy_Token(label);
5065 l->next = ei->current;
5066 ei->current = l;
5067 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005068 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005069
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005070 list->uplevel(ed->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005071
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005072 istk->mmac_depth++;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005073 return true;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005074}
5075
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005076/* The function that actually does the error reporting */
5077static void verror(int severity, const char *fmt, va_list arg)
5078{
5079 char buff[1024];
5080
5081 vsnprintf(buff, sizeof(buff), fmt, arg);
5082
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005083 if (istk && istk->mmac_depth > 0) {
5084 ExpInv *ei = istk->expansion;
5085 int lineno = ei->lineno;
5086 while (ei) {
5087 if (ei->type == EXP_MMACRO)
5088 break;
5089 lineno += ei->relno;
5090 ei = ei->prev;
5091 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005092 nasm_error(severity, "(%s:%d) %s", ei->def->name,
Cyrill Gorcunov55cc4d02010-11-10 23:12:06 +03005093 lineno, buff);
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005094 } else {
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005095 nasm_error(severity, "%s", buff);
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005096 }
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005097}
5098
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005099/*
5100 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07005101 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005102 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005103static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005104{
5105 va_list arg;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005106 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005107 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00005108 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005109}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005110
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005111/*
5112 * Because %else etc are evaluated in the state context
5113 * of the previous branch, errors might get lost with error():
5114 * %if 0 ... %else trailing garbage ... %endif
5115 * So %else etc should report errors with this function.
5116 */
5117static void error_precond(int severity, const char *fmt, ...)
5118{
5119 va_list arg;
5120
5121 /* Only ignore the error if it's really in a dead branch */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005122 if ((istk != NULL) &&
5123 (istk->expansion != NULL) &&
5124 (istk->expansion->type == EXP_IF) &&
5125 (istk->expansion->def->state == COND_NEVER))
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005126 return;
5127
5128 va_start(arg, fmt);
5129 verror(severity, fmt, arg);
5130 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005131}
5132
H. Peter Anvin734b1882002-04-30 21:01:08 +00005133static void
H. Peter Anvindbb640b2009-07-18 18:57:16 -07005134pp_reset(char *file, int apass, ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005135{
H. Peter Anvin7383b402008-09-24 10:20:40 -07005136 Token *t;
5137
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005138 cstk = NULL;
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005139 istk = nasm_zalloc(sizeof(Include));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005140 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00005141 src_set_fname(nasm_strdup(file));
5142 src_set_linnum(0);
5143 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005144 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07005145 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00005146 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005147 defining = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005148 finals = NULL;
5149 in_final = false;
Charles Crayned4200be2008-07-12 16:42:33 -07005150 nested_mac_count = 0;
5151 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005152 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005153 unique = 0;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005154 if (tasm_compatible_mode)
H. Peter Anvina4835d42008-05-20 14:21:29 -07005155 stdmacpos = nasm_stdmac;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005156 else
H. Peter Anvina4835d42008-05-20 14:21:29 -07005157 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvind2456592008-06-19 15:04:18 -07005158 any_extrastdmac = extrastdmac && *extrastdmac;
5159 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005160 list = listgen;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005161
5162 /*
5163 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
5164 * The caller, however, will also pass in 3 for preprocess-only so
5165 * we can set __PASS__ accordingly.
5166 */
5167 pass = apass > 2 ? 2 : apass;
5168
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005169 dephead = deptail = deplist;
5170 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005171 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
5172 sl->next = NULL;
5173 strcpy(sl->str, file);
5174 *deptail = sl;
5175 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07005176 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07005177
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005178 /*
5179 * Define the __PASS__ macro. This is defined here unlike
5180 * all the other builtins, because it is special -- it varies between
5181 * passes.
5182 */
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005183 t = nasm_zalloc(sizeof(*t));
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005184 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07005185 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005186}
5187
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005188static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005189{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005190 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005191 Token *tline;
Keith Kanios9858cec2010-11-08 00:58:02 -06005192 ExpDef *ed;
5193 ExpInv *ei;
5194 Line *l;
5195 int j;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005196
H. Peter Anvine2c80182005-01-15 22:15:51 +00005197 while (1) {
5198 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005199 * Fetch a tokenized line, either from the expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005200 * buffer or from the input file.
5201 */
5202 tline = NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005203
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005204 while (1) { /* until we get a line we can use */
5205 /*
5206 * Fetch a tokenized line from the expansion buffer
5207 */
5208 if (istk->expansion != NULL) {
5209 ei = istk->expansion;
5210 if (ei->current != NULL) {
5211 if (ei->emitting == false) {
5212 ei->current = NULL;
5213 continue;
5214 }
5215 l = ei->current;
5216 ei->current = l->next;
5217 ei->lineno++;
5218 tline = copy_Token(l->first);
5219 if (((ei->type == EXP_REP) ||
5220 (ei->type == EXP_MMACRO) ||
5221 (ei->type == EXP_WHILE))
5222 && (ei->def->nolist == false)) {
5223 char *p = detoken(tline, false);
5224 list->line(LIST_MACRO, p);
5225 nasm_free(p);
5226 }
5227 if (ei->linnum > -1) {
5228 src_set_linnum(src_get_linnum() + 1);
5229 }
5230 break;
5231 } else if ((ei->type == EXP_REP) &&
5232 (ei->def->cur_depth < ei->def->max_depth)) {
5233 ei->def->cur_depth ++;
5234 ei->current = ei->def->line;
5235 ei->lineno = 0;
5236 continue;
5237 } else if ((ei->type == EXP_WHILE) &&
5238 (ei->def->cur_depth < ei->def->max_depth)) {
5239 ei->current = ei->def->line;
5240 ei->lineno = 0;
5241 tline = copy_Token(ei->current->first);
5242 j = if_condition(tline, PP_WHILE);
5243 tline = NULL;
5244 j = (((j < 0) ? COND_NEVER : j) ? COND_IF_TRUE : COND_IF_FALSE);
5245 if (j == COND_IF_TRUE) {
5246 ei->current = ei->current->next;
5247 ei->def->cur_depth ++;
5248 } else {
5249 ei->emitting = false;
5250 ei->current = NULL;
5251 ei->def->cur_depth = ei->def->max_depth;
5252 }
5253 continue;
5254 } else {
5255 istk->expansion = ei->prev;
5256 ed = ei->def;
5257 if (ed != NULL) {
5258 if ((ei->emitting == true) &&
5259 (ed->max_depth == DEADMAN_LIMIT) &&
5260 (ed->cur_depth == DEADMAN_LIMIT)
5261 ) {
5262 error(ERR_FATAL, "runaway expansion detected, aborting");
5263 }
5264 if (ed->cur_depth > 0) {
5265 ed->cur_depth --;
Keith Kanios94124652010-12-18 11:47:28 -06005266 } else if (ed->type != EXP_MMACRO) {
Keith Kanios6a7c3e92010-12-18 11:49:53 -06005267 expansions = ed->prev;
5268 free_expdef(ed);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005269 }
5270 if ((ei->type == EXP_REP) ||
5271 (ei->type == EXP_MMACRO) ||
5272 (ei->type == EXP_WHILE)) {
5273 list->downlevel(LIST_MACRO);
5274 if (ei->type == EXP_MMACRO) {
5275 istk->mmac_depth--;
5276 }
5277 }
5278 }
5279 if (ei->linnum > -1) {
5280 src_set_linnum(ei->linnum);
5281 }
5282 free_expinv(ei);
5283 continue;
5284 }
5285 }
5286
5287 /*
5288 * Read in line from input and tokenize
5289 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005290 line = read_line();
5291 if (line) { /* from the current input file */
5292 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005293 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005294 nasm_free(line);
5295 break;
5296 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005297
H. Peter Anvine2c80182005-01-15 22:15:51 +00005298 /*
5299 * The current file has ended; work down the istk
5300 */
5301 {
5302 Include *i = istk;
5303 fclose(i->fp);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005304 if (i->expansion != NULL) {
5305 error(ERR_FATAL,
5306 "end of file while still in an expansion");
5307 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005308 /* only set line and file name if there's a next node */
5309 if (i->next) {
5310 src_set_linnum(i->lineno);
Cyrill Gorcunovd34a1082011-03-07 11:23:08 +03005311 nasm_free(src_set_fname(nasm_strdup(i->fname)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005312 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005313 if ((i->next == NULL) && (finals != NULL)) {
5314 in_final = true;
5315 ei = new_ExpInv(EXP_FINAL, NULL);
5316 ei->emitting = true;
5317 ei->current = finals;
5318 istk->expansion = ei;
5319 finals = NULL;
5320 continue;
5321 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005322 istk = i->next;
5323 list->downlevel(LIST_INCLUDE);
5324 nasm_free(i);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005325 if (istk == NULL) {
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005326 if (finals != NULL)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005327 in_final = true;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005328 else
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005329 return NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005330 }
5331 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005332 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005333 }
5334
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005335 if (defining == NULL)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005336 tline = expand_mmac_params(tline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005337
H. Peter Anvine2c80182005-01-15 22:15:51 +00005338 /*
5339 * Check the line to see if it's a preprocessor directive.
5340 */
5341 if (do_directive(tline) == DIRECTIVE_FOUND) {
5342 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005343 } else if (defining != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005344 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005345 * We're defining an expansion. We emit nothing at all,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005346 * and just shove the tokenized line on to the definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005347 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005348 if (defining->ignoring == false) {
5349 Line *l = new_Line();
5350 l->first = tline;
5351 if (defining->line == NULL) {
5352 defining->line = l;
5353 defining->last = l;
5354 } else {
5355 defining->last->next = l;
5356 defining->last = l;
5357 }
5358 } else {
Keith Kanios104803d2010-12-18 11:05:46 -06005359 free_tlist(tline);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005360 }
5361 defining->linecount++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005362 continue;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005363 } else if ((istk->expansion != NULL) &&
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005364 (istk->expansion->emitting != true)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005365 /*
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005366 * We're in a non-emitting branch of an expansion.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005367 * Emit nothing at all, not even a blank line: when we
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005368 * emerge from the expansion we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005369 * directive so we keep our place correctly.
5370 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005371 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005372 continue;
5373 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005374 tline = expand_smacro(tline);
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005375 if (expand_mmacro(tline) != true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005376 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005377 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005378 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005379 line = detoken(tline, true);
5380 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005381 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005382 } else {
5383 continue;
5384 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005385 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005386 }
5387 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005388}
5389
H. Peter Anvine2c80182005-01-15 22:15:51 +00005390static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005391{
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005392 if (defining != NULL) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005393 error(ERR_NONFATAL, "end of file while still defining an expansion");
5394 while (defining != NULL) {
5395 ExpDef *ed = defining;
5396 defining = ed->prev;
5397 free_expdef(ed);
5398 }
5399 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005400 }
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005401 while (cstk != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005402 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005403 free_macros();
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005404 while (istk != NULL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005405 Include *i = istk;
5406 istk = istk->next;
5407 fclose(i->fp);
5408 nasm_free(i->fname);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005409 while (i->expansion != NULL) {
5410 ExpInv *ei = i->expansion;
5411 i->expansion = ei->prev;
5412 free_expinv(ei);
5413 }
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005414 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005415 }
5416 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005417 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07005418 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00005419 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005420 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005421 free_llist(predef);
5422 delete_Blocks();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005423 while ((i = ipath)) {
5424 ipath = i->next;
Cyrill Gorcunovb6c6ca92011-06-28 01:33:02 +04005425 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005426 nasm_free(i);
5427 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005428 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005429}
5430
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005431void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005432{
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005433 IncPath *i = nasm_zalloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005434
Cyrill Gorcunov6b271292011-02-28 08:45:52 +03005435 if (path)
5436 i->path = nasm_strdup(path);
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005437
H. Peter Anvin89cee572009-07-15 09:16:54 -04005438 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005439 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005440 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005441 j = j->next;
5442 j->next = i;
5443 } else {
5444 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005445 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005446}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005447
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005448void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005449{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005450 Token *inc, *space, *name;
5451 Line *l;
5452
H. Peter Anvin734b1882002-04-30 21:01:08 +00005453 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5454 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5455 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005456
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005457 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005458 l->next = predef;
5459 l->first = inc;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005460 predef = l;
5461}
5462
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005463void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005464{
5465 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005466 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005467 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005468
5469 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005470 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5471 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005472 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005473 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005474 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005475 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005476 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005477
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005478 l = new_Line();
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005479 l->next = predef;
5480 l->first = def;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005481 predef = l;
5482}
5483
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005484void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005485{
5486 Token *def, *space;
5487 Line *l;
5488
H. Peter Anvin734b1882002-04-30 21:01:08 +00005489 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5490 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005491 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005492
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005493 l = new_Line();
H. Peter Anvin620515a2002-04-30 20:57:38 +00005494 l->next = predef;
5495 l->first = def;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005496 predef = l;
5497}
5498
Keith Kaniosb7a89542007-04-12 02:40:54 +00005499/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005500 * This function is used to assist with "runtime" preprocessor
Keith Kaniosb307a4f2010-11-06 17:41:51 -05005501 * directives, e.g. pp_runtime("%define __BITS__ 64");
Keith Kaniosb7a89542007-04-12 02:40:54 +00005502 *
5503 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
5504 * PASS A VALID STRING TO THIS FUNCTION!!!!!
5505 */
5506
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005507void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005508{
5509 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07005510
Keith Kaniosb7a89542007-04-12 02:40:54 +00005511 def = tokenize(definition);
H. Peter Anvin89cee572009-07-15 09:16:54 -04005512 if (do_directive(def) == NO_DIRECTIVE_FOUND)
Keith Kaniosb7a89542007-04-12 02:40:54 +00005513 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07005514
Keith Kaniosb7a89542007-04-12 02:40:54 +00005515}
5516
H. Peter Anvina70547f2008-07-19 21:44:26 -07005517void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005518{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005519 extrastdmac = macros;
5520}
5521
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005522static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005523{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005524 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005525 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005526 tok->text = nasm_strdup(numbuf);
5527 tok->type = TOK_NUMBER;
5528}
5529
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +04005530struct preproc_ops nasmpp = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005531 pp_reset,
5532 pp_getline,
5533 pp_cleanup
5534};