blob: f6956bfc4e640418c059a32a838b8e7436c9ffc6 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
H. Peter Anvin130736c2016-02-17 20:27:41 -08003 * Copyright 1996-2016 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>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000072
73#include "nasm.h"
74#include "nasmlib.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000075#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070076#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070077#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070078#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070079#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070080#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070081#include "tables.h"
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -080082#include "listing.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000083
84typedef struct SMacro SMacro;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080085typedef struct MMacro MMacro;
86typedef struct MMacroInvocation MMacroInvocation;
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 Anvin36206cd2012-03-03 16:14:51 -080092typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000093typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000094
95/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070096 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
102 */
103
104/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 * Store the definition of a single-line macro.
106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107struct SMacro {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800108 SMacro *next;
109 char *name;
110 bool casesense;
111 bool in_progress;
112 unsigned int nparam;
113 Token *expansion;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000114};
115
116/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800117 * Store the definition of a multi-line macro. This is also used to
118 * store the interiors of `%rep...%endrep' blocks, which are
119 * effectively self-re-invoking multi-line macros which simply
120 * don't have a name or bother to appear in the hash tables. %rep
121 * blocks are signified by having a NULL `name' field.
122 *
123 * In a MMacro describing a `%rep' block, the `in_progress' field
124 * isn't merely boolean, but gives the number of repeats left to
125 * run.
126 *
127 * The `next' field is used for storing MMacros in hash tables; the
128 * `next_active' field is for stacking them on istk entries.
129 *
130 * When a MMacro is being expanded, `params', `iline', `nparam',
131 * `paramlen', `rotate' and `unique' are local to the invocation.
132 */
133struct MMacro {
134 MMacro *next;
135 MMacroInvocation *prev; /* previous invocation */
136 char *name;
137 int nparam_min, nparam_max;
138 bool casesense;
139 bool plus; /* is the last parameter greedy? */
140 bool nolist; /* is this macro listing-inhibited? */
141 int64_t in_progress; /* is this macro currently being expanded? */
142 int32_t max_depth; /* maximum number of recursive expansions allowed */
143 Token *dlist; /* All defaults as one list */
144 Token **defaults; /* Parameter default pointers */
145 int ndefs; /* number of default parameters */
146 Line *expansion;
147
148 MMacro *next_active;
149 MMacro *rep_nest; /* used for nesting %rep */
150 Token **params; /* actual parameters */
151 Token *iline; /* invocation line */
152 unsigned int nparam, rotate;
153 int *paramlen;
154 uint64_t unique;
155 int lineno; /* Current line number on expansion */
156 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700157
H. Peter Anvin274cda82016-05-10 02:56:29 -0700158 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700159 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800160};
161
162
163/* Store the definition of a multi-line macro, as defined in a
164 * previous recursive macro expansion.
165 */
166struct MMacroInvocation {
167 MMacroInvocation *prev; /* previous invocation */
168 Token **params; /* actual parameters */
169 Token *iline; /* invocation line */
170 unsigned int nparam, rotate;
171 int *paramlen;
172 uint64_t unique;
173 uint64_t condcnt;
174};
175
176
177/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000178 * The context stack is composed of a linked list of these.
179 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000180struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800181 Context *next;
182 char *name;
183 struct hash_table localmac;
184 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000185};
186
187/*
188 * This is the internal form which we break input lines up into.
189 * Typically stored in linked lists.
190 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000191 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
192 * necessarily used as-is, but is intended to denote the number of
193 * the substituted parameter. So in the definition
194 *
195 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700196 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000197 * the token representing `x' will have its type changed to
198 * TOK_SMAC_PARAM, but the one representing `y' will be
199 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000200 *
201 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
202 * which doesn't need quotes around it. Used in the pre-include
203 * mechanism as an alternative to trying to find a sensible type of
204 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000205 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000206enum pp_token_type {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800207 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
208 TOK_PREPROC_ID, TOK_STRING,
209 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700210 TOK_INTERNAL_STRING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800211 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300212 TOK_PASTE, /* %+ */
213 TOK_INDIRECT, /* %[...] */
214 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
215 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000216};
217
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400218#define PP_CONCAT_MASK(x) (1 << (x))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +0400219#define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400220
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400221struct tokseq_match {
222 int mask_head;
223 int mask_tail;
224};
225
H. Peter Anvine2c80182005-01-15 22:15:51 +0000226struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800227 Token *next;
228 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700229 union {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800230 SMacro *mac; /* associated macro for TOK_SMAC_END */
231 size_t len; /* scratch length field */
232 } a; /* Auxiliary data */
233 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000234};
235
236/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800237 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000238 * these, which is essentially a container to allow several linked
239 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700240 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000241 * Note that in this module, linked lists are treated as stacks
242 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800243 * `expansion' field in MMacro structures, so that the linked list,
244 * if walked, would give the macro lines in reverse order; this
245 * means that we can walk the list when expanding a macro, and thus
246 * push the lines on to the `expansion' field in _istk_ in reverse
247 * order (so that when popped back off they are in the right
248 * order). It may seem cockeyed, and it relies on my design having
249 * an even number of steps in, but it works...
250 *
251 * Some of these structures, rather than being actual lines, are
252 * markers delimiting the end of the expansion of a given macro.
253 * This is for use in the cycle-tracking and %rep-handling code.
254 * Such structures have `finishes' non-NULL, and `first' NULL. All
255 * others have `finishes' NULL, but `first' may still be NULL if
256 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000257 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000258struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800259 Line *next;
260 MMacro *finishes;
261 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500262};
263
264/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000265 * To handle an arbitrary level of file inclusion, we maintain a
266 * stack (ie linked list) of these things.
267 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000268struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800269 Include *next;
270 FILE *fp;
271 Cond *conds;
272 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700273 const char *fname;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800274 int lineno, lineinc;
275 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000276};
277
278/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000279 * Include search path. This is simply a list of strings which get
280 * prepended, in turn, to the name of an include file, in an
281 * attempt to find the file if it's not in the current directory.
282 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000283struct IncPath {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800284 IncPath *next;
285 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000286};
287
288/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700289 * File real name hash, so we don't have to re-search the include
290 * path for every pass (and potentially more than that if a file
291 * is used more than once.)
292 */
293struct hash_table FileHash;
294
295/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000296 * Conditional assembly: we maintain a separate stack of these for
297 * each level of file inclusion. (The only reason we keep the
298 * stacks separate is to ensure that a stray `%endif' in a file
299 * included from within the true branch of a `%if' won't terminate
300 * it and cause confusion: instead, rightly, it'll cause an error.)
301 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800302struct Cond {
303 Cond *next;
304 int state;
305};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000306enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000307 /*
308 * These states are for use just after %if or %elif: IF_TRUE
309 * means the condition has evaluated to truth so we are
310 * currently emitting, whereas IF_FALSE means we are not
311 * currently emitting but will start doing so if a %else comes
312 * up. In these states, all directives are admissible: %elif,
313 * %else and %endif. (And of course %if.)
314 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800315 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000316 /*
317 * These states come up after a %else: ELSE_TRUE means we're
318 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
319 * any %elif or %else will cause an error.
320 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800321 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000322 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200323 * These states mean that we're not emitting now, and also that
324 * nothing until %endif will be emitted at all. COND_DONE is
325 * used when we've had our moment of emission
326 * and have now started seeing %elifs. COND_NEVER is used when
327 * the condition construct in question is contained within a
328 * non-emitting branch of a larger condition construct,
329 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000330 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800331 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000332};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800333#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000334
H. Peter Anvin70653092007-10-19 14:42:29 -0700335/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000336 * These defines are used as the possible return values for do_directive
337 */
338#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300339#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000340
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800342 * This define sets the upper limit for smacro and recursive mmacro
343 * expansions
Keith Kanios852f1ee2009-07-12 00:19:55 -0500344 */
345#define DEADMAN_LIMIT (1 << 20)
346
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400347/* max reps */
348#define REP_LIMIT ((INT64_C(1) << 62))
349
Keith Kanios852f1ee2009-07-12 00:19:55 -0500350/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000351 * Condition codes. Note that we use c_ prefix not C_ because C_ is
352 * used in nasm.h for the "real" condition codes. At _this_ level,
353 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
354 * ones, so we need a different enum...
355 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700356static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000357 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
358 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000359 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000360};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700361enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000362 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
363 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 -0700364 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
365 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000366};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700367static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000368 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
369 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 +0000370 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000371};
372
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800373/*
374 * Directive names.
375 */
376/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
377static int is_condition(enum preproc_token arg)
378{
379 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
380}
381
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000382/* For TASM compatibility we need to be able to recognise TASM compatible
383 * conditional compilation directives. Using the NASM pre-processor does
384 * not work, so we look for them specifically from the following list and
385 * then jam in the equivalent NASM directive into the input stream.
386 */
387
H. Peter Anvine2c80182005-01-15 22:15:51 +0000388enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000389 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
390 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
391};
392
H. Peter Anvin476d2862007-10-02 22:04:15 -0700393static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000394 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
395 "ifndef", "include", "local"
396};
397
398static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700399static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000400static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800401static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000402
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000403static Context *cstk;
404static Include *istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800405static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000406
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300407static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700408static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000409
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300410static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000411
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800412static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700413static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000414
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000415/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800416 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000417 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800418static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000419
420/*
421 * The current set of single-line macros we have defined.
422 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700423static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000424
425/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800426 * The multi-line macro we are currently defining, or the %rep
427 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000428 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800429static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000430
Charles Crayned4200be2008-07-12 16:42:33 -0700431static uint64_t nested_mac_count;
432static uint64_t nested_rep_count;
433
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000434/*
435 * The number of macro parameters to allocate space for at a time.
436 */
437#define PARAM_DELTA 16
438
439/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700440 * The standard macro set: defined in macros.c in a set of arrays.
441 * This gives our position in any macro set, while we are processing it.
442 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000443 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700444static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700445static macros_t **stdmacnext;
446static macros_t *stdmacros[8];
H. Peter Anvin76690a12002-04-30 20:52:49 +0000447
448/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000449 * Tokens are allocated in blocks to improve speed
450 */
451#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800452static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000453struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000454 Blocks *next;
455 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000456};
457
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800458static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000459
460/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000461 * Forward declarations.
462 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700463static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000464static Token *expand_mmac_params(Token * tline);
465static Token *expand_smacro(Token * tline);
466static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400467static Context *get_ctx(const char *name, const char **namep);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700468static void make_tok_num(Token * tok, int64_t val);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800469static void pp_verror(int severity, const char *fmt, va_list ap);
470static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000471static void *new_Block(size_t size);
472static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700473static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300474 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000475static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000476
477/*
478 * Macros for safe checking of token pointers, avoid *(NULL)
479 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300480#define tok_type_(x,t) ((x) && (x)->type == (t))
481#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
482#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
483#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000484
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400485/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700486 * nasm_unquote with error if the string contains NUL characters.
487 * If the string contains NUL characters, issue an error and return
488 * the C len, i.e. truncate at the NUL.
489 */
490static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
491{
492 size_t len = nasm_unquote(qstr, NULL);
493 size_t clen = strlen(qstr);
494
495 if (len != clen)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800496 nasm_error(ERR_NONFATAL, "NUL character in `%s' directive",
H. Peter Anvin077fb932010-07-20 14:56:30 -0700497 pp_directives[directive]);
498
499 return clen;
500}
501
502/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700503 * In-place reverse a list of tokens.
504 */
505static Token *reverse_tokens(Token *t)
506{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800507 Token *prev = NULL;
508 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700509
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800510 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400511 next = t->next;
512 t->next = prev;
513 prev = t;
514 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800515 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700516
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800517 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700518}
519
520/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300521 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000522 * front of them. We do it here because I could not find any other
523 * place to do it for the moment, and it is a hack (ideally it would
524 * be nice to be able to use the NASM pre-processor to do it).
525 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000526static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000527{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000528 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400529 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000530
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400531 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000532
533 /* Binary search for the directive name */
534 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400535 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400536 q = nasm_skip_word(p);
537 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000538 if (len) {
539 oldchar = p[len];
540 p[len] = 0;
541 while (j - i > 1) {
542 k = (j + i) / 2;
543 m = nasm_stricmp(p, tasm_directives[k]);
544 if (m == 0) {
545 /* We have found a directive, so jam a % in front of it
546 * so that NASM will then recognise it as one if it's own.
547 */
548 p[len] = oldchar;
549 len = strlen(p);
550 oldline = line;
551 line = nasm_malloc(len + 2);
552 line[0] = '%';
553 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700554 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300555 * NASM does not recognise IFDIFI, so we convert
556 * it to %if 0. This is not used in NASM
557 * compatible code, but does need to parse for the
558 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000559 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700560 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000561 } else {
562 memcpy(line + 1, p, len + 1);
563 }
564 nasm_free(oldline);
565 return line;
566 } else if (m < 0) {
567 j = k;
568 } else
569 i = k;
570 }
571 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000572 }
573 return line;
574}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000575
H. Peter Anvin76690a12002-04-30 20:52:49 +0000576/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000577 * The pre-preprocessing stage... This function translates line
578 * number indications as they emerge from GNU cpp (`# lineno "file"
579 * flags') into NASM preprocessor line number indications (`%line
580 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000581 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000582static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000583{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000584 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000585 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000586
H. Peter Anvine2c80182005-01-15 22:15:51 +0000587 if (line[0] == '#' && line[1] == ' ') {
588 oldline = line;
589 fname = oldline + 2;
590 lineno = atoi(fname);
591 fname += strspn(fname, "0123456789 ");
592 if (*fname == '"')
593 fname++;
594 fnlen = strcspn(fname, "\"");
595 line = nasm_malloc(20 + fnlen);
596 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
597 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000598 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000599 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000600 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000601 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000602}
603
604/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000605 * Free a linked list of tokens.
606 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000607static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000608{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400609 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000610 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000611}
612
613/*
614 * Free a linked list of lines.
615 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000616static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000617{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400618 Line *l, *tmp;
619 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000620 free_tlist(l->first);
621 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000622 }
623}
624
625/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800626 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000627 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800628static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000629{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800630 nasm_free(m->name);
631 free_tlist(m->dlist);
632 nasm_free(m->defaults);
633 free_llist(m->expansion);
634 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000635}
636
637/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700638 * Free all currently defined macros, and free the hash tables
639 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700640static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700641{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400642 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700643 const char *key;
644 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700645
H. Peter Anvin072771e2008-05-22 13:17:51 -0700646 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300647 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400648 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300649 nasm_free(s->name);
650 free_tlist(s->expansion);
651 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300652 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700653 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700654 hash_free(smt);
655}
656
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800657static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700658{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800659 MMacro *m, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700660 const char *key;
661 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700662
663 it = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800664 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300665 nasm_free((void *)key);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800666 list_for_each_safe(m ,tmp, m)
667 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700668 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800669 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700670}
671
672static void free_macros(void)
673{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700674 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800675 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700676}
677
678/*
679 * Initialize the hash tables
680 */
681static void init_macros(void)
682{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700683 hash_init(&smacros, HASH_LARGE);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800684 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700685}
686
687/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000688 * Pop the context stack.
689 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000690static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000691{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000692 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000693
694 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700695 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000696 nasm_free(c->name);
697 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000698}
699
H. Peter Anvin072771e2008-05-22 13:17:51 -0700700/*
701 * Search for a key in the hash index; adding it if necessary
702 * (in which case we initialize the data pointer to NULL.)
703 */
704static void **
705hash_findi_add(struct hash_table *hash, const char *str)
706{
707 struct hash_insert hi;
708 void **r;
709 char *strx;
710
711 r = hash_findi(hash, str, &hi);
712 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300713 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700714
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300715 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700716 return hash_add(&hi, strx, NULL);
717}
718
719/*
720 * Like hash_findi, but returns the data element rather than a pointer
721 * to it. Used only when not adding a new element, hence no third
722 * argument.
723 */
724static void *
725hash_findix(struct hash_table *hash, const char *str)
726{
727 void **p;
728
729 p = hash_findi(hash, str, NULL);
730 return p ? *p : NULL;
731}
732
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400733/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800734 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400735 * if there no more left -- return NULL
736 */
737static char *line_from_stdmac(void)
738{
739 unsigned char c;
740 const unsigned char *p = stdmacpos;
741 char *line, *q;
742 size_t len = 0;
743
744 if (!stdmacpos)
745 return NULL;
746
747 while ((c = *p++)) {
748 if (c >= 0x80)
749 len += pp_directives_len[c - 0x80] + 1;
750 else
751 len++;
752 }
753
754 line = nasm_malloc(len + 1);
755 q = line;
756 while ((c = *stdmacpos++)) {
757 if (c >= 0x80) {
758 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
759 q += pp_directives_len[c - 0x80];
760 *q++ = ' ';
761 } else {
762 *q++ = c;
763 }
764 }
765 stdmacpos = p;
766 *q = '\0';
767
768 if (!*stdmacpos) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700769 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400770 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700771 if (*stdmacnext) {
772 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400773 } else if (do_predef) {
774 Line *pd, *l;
775 Token *head, **tail, *t;
776
777 /*
778 * Nasty hack: here we push the contents of
779 * `predef' on to the top-level expansion stack,
780 * since this is the most convenient way to
781 * implement the pre-include and pre-define
782 * features.
783 */
784 list_for_each(pd, predef) {
785 head = NULL;
786 tail = &head;
787 list_for_each(t, pd->first) {
788 *tail = new_Token(NULL, t->type, t->text, 0);
789 tail = &(*tail)->next;
790 }
791
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800792 l = nasm_malloc(sizeof(Line));
793 l->next = istk->expansion;
794 l->first = head;
795 l->finishes = NULL;
796
797 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400798 }
799 do_predef = false;
800 }
801 }
802
803 return line;
804}
805
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000806static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000807{
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400808 unsigned int size, c, next;
809 const unsigned int delta = 512;
810 const unsigned int pad = 8;
811 unsigned int nr_cont = 0;
812 bool cont = false;
813 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000814
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400815 /* Standart macros set (predefined) goes first */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400816 p = line_from_stdmac();
817 if (p)
818 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700819
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400820 size = delta;
821 p = buffer = nasm_malloc(size);
822
823 for (;;) {
824 c = fgetc(istk->fp);
825 if ((int)(c) == EOF) {
826 p[0] = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000827 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000828 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400829
830 switch (c) {
831 case '\r':
832 next = fgetc(istk->fp);
833 if (next != '\n')
834 ungetc(next, istk->fp);
835 if (cont) {
836 cont = false;
837 continue;
838 }
839 break;
840
841 case '\n':
842 if (cont) {
843 cont = false;
844 continue;
845 }
846 break;
847
848 case '\\':
849 next = fgetc(istk->fp);
850 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400851 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400852 cont = true;
853 nr_cont++;
854 continue;
855 }
856 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000857 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400858
859 if (c == '\r' || c == '\n') {
860 *p++ = 0;
861 break;
862 }
863
864 if (p >= (buffer + size - pad)) {
865 buffer = nasm_realloc(buffer, size + delta);
866 p = buffer + size - pad;
867 size += delta;
868 }
869
870 *p++ = (unsigned char)c;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000871 }
872
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400873 if (p == buffer) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000874 nasm_free(buffer);
875 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000876 }
877
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300878 src_set_linnum(src_get_linnum() + istk->lineinc +
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400879 (nr_cont * istk->lineinc));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000880
881 /*
882 * Handle spurious ^Z, which may be inserted into source files
883 * by some file transfer utilities.
884 */
885 buffer[strcspn(buffer, "\032")] = '\0';
886
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -0800887 lfmt->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000888
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000889 return buffer;
890}
891
892/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000893 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000894 * don't need to parse the value out of e.g. numeric tokens: we
895 * simply split one string into many.
896 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000897static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000898{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700899 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000900 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000901 Token *list = NULL;
902 Token *t, **tail = &list;
903
H. Peter Anvine2c80182005-01-15 22:15:51 +0000904 while (*line) {
905 p = line;
906 if (*p == '%') {
907 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300908 if (*p == '+' && !nasm_isdigit(p[1])) {
909 p++;
910 type = TOK_PASTE;
911 } else if (nasm_isdigit(*p) ||
912 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000913 do {
914 p++;
915 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700916 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000917 type = TOK_PREPROC_ID;
918 } else if (*p == '{') {
919 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800920 while (*p) {
921 if (*p == '}')
922 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000923 p[-1] = *p;
924 p++;
925 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800926 if (*p != '}')
H. Peter Anvin130736c2016-02-17 20:27:41 -0800927 nasm_error(ERR_WARNING | ERR_PASS1,
928 "unterminated %%{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000929 p[-1] = '\0';
930 if (*p)
931 p++;
932 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300933 } else if (*p == '[') {
934 int lvl = 1;
935 line += 2; /* Skip the leading %[ */
936 p++;
937 while (lvl && (c = *p++)) {
938 switch (c) {
939 case ']':
940 lvl--;
941 break;
942 case '%':
943 if (*p == '[')
944 lvl++;
945 break;
946 case '\'':
947 case '\"':
948 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400949 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300950 break;
951 default:
952 break;
953 }
954 }
955 p--;
956 if (*p)
957 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800958 if (lvl)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800959 nasm_error(ERR_NONFATAL|ERR_PASS1,
960 "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300961 type = TOK_INDIRECT;
962 } else if (*p == '?') {
963 type = TOK_PREPROC_Q; /* %? */
964 p++;
965 if (*p == '?') {
966 type = TOK_PREPROC_QQ; /* %?? */
967 p++;
968 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400969 } else if (*p == '!') {
970 type = TOK_PREPROC_ID;
971 p++;
972 if (isidchar(*p)) {
973 do {
974 p++;
975 }
976 while (isidchar(*p));
977 } else if (*p == '\'' || *p == '\"' || *p == '`') {
978 p = nasm_skip_string(p);
979 if (*p)
980 p++;
981 else
H. Peter Anvin130736c2016-02-17 20:27:41 -0800982 nasm_error(ERR_NONFATAL|ERR_PASS1,
983 "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400984 } else {
985 /* %! without string or identifier */
986 type = TOK_OTHER; /* Legacy behavior... */
987 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 } else if (isidchar(*p) ||
989 ((*p == '!' || *p == '%' || *p == '$') &&
990 isidchar(p[1]))) {
991 do {
992 p++;
993 }
994 while (isidchar(*p));
995 type = TOK_PREPROC_ID;
996 } else {
997 type = TOK_OTHER;
998 if (*p == '%')
999 p++;
1000 }
1001 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1002 type = TOK_ID;
1003 p++;
1004 while (*p && isidchar(*p))
1005 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001006 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 /*
1008 * A string token.
1009 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001011 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001012
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 if (*p) {
1014 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001015 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001016 nasm_error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 /* Handling unterminated strings by UNV */
1018 /* type = -1; */
1019 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001020 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001021 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001022 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001024 bool is_hex = false;
1025 bool is_float = false;
1026 bool has_e = false;
1027 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001028
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001030 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001031 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001032
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001033 if (*p == '$') {
1034 p++;
1035 is_hex = true;
1036 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001037
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001038 for (;;) {
1039 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001040
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001041 if (!is_hex && (c == 'e' || c == 'E')) {
1042 has_e = true;
1043 if (*p == '+' || *p == '-') {
1044 /*
1045 * e can only be followed by +/- if it is either a
1046 * prefixed hex number or a floating-point number
1047 */
1048 p++;
1049 is_float = true;
1050 }
1051 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1052 is_hex = true;
1053 } else if (c == 'P' || c == 'p') {
1054 is_float = true;
1055 if (*p == '+' || *p == '-')
1056 p++;
1057 } else if (isnumchar(c) || c == '_')
1058 ; /* just advance */
1059 else if (c == '.') {
1060 /*
1061 * we need to deal with consequences of the legacy
1062 * parser, like "1.nolist" being two tokens
1063 * (TOK_NUMBER, TOK_ID) here; at least give it
1064 * a shot for now. In the future, we probably need
1065 * a flex-based scanner with proper pattern matching
1066 * to do it as well as it can be done. Nothing in
1067 * the world is going to help the person who wants
1068 * 0x123.p16 interpreted as two tokens, though.
1069 */
1070 r = p;
1071 while (*r == '_')
1072 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001073
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001074 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1075 (!is_hex && (*r == 'e' || *r == 'E')) ||
1076 (*r == 'p' || *r == 'P')) {
1077 p = r;
1078 is_float = true;
1079 } else
1080 break; /* Terminate the token */
1081 } else
1082 break;
1083 }
1084 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001085
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001086 if (p == line+1 && *line == '$') {
1087 type = TOK_OTHER; /* TOKEN_HERE */
1088 } else {
1089 if (has_e && !is_hex) {
1090 /* 1e13 is floating-point, but 1e13h is not */
1091 is_float = true;
1092 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001093
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001094 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1095 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001096 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001097 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001098 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 /*
1100 * Whitespace just before end-of-line is discarded by
1101 * pretending it's a comment; whitespace just before a
1102 * comment gets lumped into the comment.
1103 */
1104 if (!*p || *p == ';') {
1105 type = TOK_COMMENT;
1106 while (*p)
1107 p++;
1108 }
1109 } else if (*p == ';') {
1110 type = TOK_COMMENT;
1111 while (*p)
1112 p++;
1113 } else {
1114 /*
1115 * Anything else is an operator of some kind. We check
1116 * for all the double-character operators (>>, <<, //,
1117 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001118 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001119 */
1120 type = TOK_OTHER;
1121 if ((p[0] == '>' && p[1] == '>') ||
1122 (p[0] == '<' && p[1] == '<') ||
1123 (p[0] == '/' && p[1] == '/') ||
1124 (p[0] == '<' && p[1] == '=') ||
1125 (p[0] == '>' && p[1] == '=') ||
1126 (p[0] == '=' && p[1] == '=') ||
1127 (p[0] == '!' && p[1] == '=') ||
1128 (p[0] == '<' && p[1] == '>') ||
1129 (p[0] == '&' && p[1] == '&') ||
1130 (p[0] == '|' && p[1] == '|') ||
1131 (p[0] == '^' && p[1] == '^')) {
1132 p++;
1133 }
1134 p++;
1135 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001136
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 /* Handling unterminated string by UNV */
1138 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001139 {
1140 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1141 t->text[p-line] = *line;
1142 tail = &t->next;
1143 }
1144 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145 if (type != TOK_COMMENT) {
1146 *tail = t = new_Token(NULL, type, line, p - line);
1147 tail = &t->next;
1148 }
1149 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001150 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001151 return list;
1152}
1153
H. Peter Anvince616072002-04-30 21:02:23 +00001154/*
1155 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001156 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001157 * deleted only all at once by the delete_Blocks function.
1158 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001160{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001161 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001163 /* first, get to the end of the linked list */
1164 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001165 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001166 /* now allocate the requested chunk */
1167 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001168
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001169 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001170 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001171 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001172}
1173
1174/*
1175 * this function deletes all managed blocks of memory
1176 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001178{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001179 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001180
H. Peter Anvin70653092007-10-19 14:42:29 -07001181 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001182 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001183 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001184 * free it.
1185 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001187 if (b->chunk)
1188 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001189 a = b;
1190 b = b->next;
1191 if (a != &blocks)
1192 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001193 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001194 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001195}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001196
1197/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001198 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001199 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001200 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001201 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001202static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001203 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001204{
1205 Token *t;
1206 int i;
1207
H. Peter Anvin89cee572009-07-15 09:16:54 -04001208 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1210 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1211 freeTokens[i].next = &freeTokens[i + 1];
1212 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001213 }
1214 t = freeTokens;
1215 freeTokens = t->next;
1216 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001217 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001218 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001219 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001220 t->text = NULL;
1221 } else {
1222 if (txtlen == 0)
1223 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001224 t->text = nasm_malloc(txtlen+1);
1225 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001226 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001227 }
1228 return t;
1229}
1230
H. Peter Anvine2c80182005-01-15 22:15:51 +00001231static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001232{
1233 Token *next = t->next;
1234 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001235 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001236 freeTokens = t;
1237 return next;
1238}
1239
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001240/*
1241 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001242 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1243 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001244 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001245static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001246{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001247 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001248 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001249 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001250 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001251
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001252 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001253 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001254 char *v;
1255 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001256
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001257 v = t->text + 2;
1258 if (*v == '\'' || *v == '\"' || *v == '`') {
1259 size_t len = nasm_unquote(v, NULL);
1260 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001261
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001262 if (len != clen) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001263 nasm_error(ERR_NONFATAL | ERR_PASS1,
1264 "NUL character in %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001265 v = NULL;
1266 }
1267 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001268
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001269 if (v) {
1270 char *p = getenv(v);
1271 if (!p) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001272 nasm_error(ERR_NONFATAL | ERR_PASS1,
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001273 "nonexistent environment variable `%s'", v);
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001274 /*
1275 * FIXME We better should investigate if accessing
1276 * ->text[1] without ->text[0] is safe enough.
1277 */
1278 t->text = nasm_zalloc(2);
1279 } else
1280 t->text = nasm_strdup(p);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001281 }
1282 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001283 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001284
H. Peter Anvine2c80182005-01-15 22:15:51 +00001285 /* Expand local macros here and not during preprocessing */
1286 if (expand_locals &&
1287 t->type == TOK_PREPROC_ID && t->text &&
1288 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001289 const char *q;
1290 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001291 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001293 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001294 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001295 p = nasm_strcat(buffer, q);
1296 nasm_free(t->text);
1297 t->text = p;
1298 }
1299 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001300 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001301 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001302 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001304 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001305
H. Peter Anvin734b1882002-04-30 21:01:08 +00001306 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001307
1308 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001309 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001310 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001312 q = t->text;
1313 while (*q)
1314 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001315 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001316 }
1317 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001318
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001319 return line;
1320}
1321
1322/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001323 * A scanner, suitable for use by the expression evaluator, which
1324 * operates on a line of Tokens. Expects a pointer to a pointer to
1325 * the first token in the line to be passed in as its private_data
1326 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001327 *
1328 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001329 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001330static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001331{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001332 Token **tlineptr = private_data;
1333 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001334 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001335
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 do {
1337 tline = *tlineptr;
1338 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001339 } while (tline && (tline->type == TOK_WHITESPACE ||
1340 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001341
1342 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001344
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001345 tokval->t_charptr = tline->text;
1346
H. Peter Anvin76690a12002-04-30 20:52:49 +00001347 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001348 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001349 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001351
H. Peter Anvine2c80182005-01-15 22:15:51 +00001352 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001353 p = tokval->t_charptr = tline->text;
1354 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001355 tokval->t_charptr++;
1356 return tokval->t_type = TOKEN_ID;
1357 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001358
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001359 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001360 if (r >= p+MAX_KEYWORD)
1361 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001362 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001363 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001364 *s = '\0';
1365 /* right, so we have an identifier sitting in temp storage. now,
1366 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001367 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001368 }
1369
H. Peter Anvine2c80182005-01-15 22:15:51 +00001370 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001371 bool rn_error;
1372 tokval->t_integer = readnum(tline->text, &rn_error);
1373 tokval->t_charptr = tline->text;
1374 if (rn_error)
1375 return tokval->t_type = TOKEN_ERRNUM;
1376 else
1377 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001378 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001379
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001380 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001381 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001382 }
1383
H. Peter Anvine2c80182005-01-15 22:15:51 +00001384 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001385 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001386
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001387 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001388 tokval->t_charptr = tline->text;
1389 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001390
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001391 if (ep[0] != bq || ep[1] != '\0')
1392 return tokval->t_type = TOKEN_ERRSTR;
1393 else
1394 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001395 }
1396
H. Peter Anvine2c80182005-01-15 22:15:51 +00001397 if (tline->type == TOK_OTHER) {
1398 if (!strcmp(tline->text, "<<"))
1399 return tokval->t_type = TOKEN_SHL;
1400 if (!strcmp(tline->text, ">>"))
1401 return tokval->t_type = TOKEN_SHR;
1402 if (!strcmp(tline->text, "//"))
1403 return tokval->t_type = TOKEN_SDIV;
1404 if (!strcmp(tline->text, "%%"))
1405 return tokval->t_type = TOKEN_SMOD;
1406 if (!strcmp(tline->text, "=="))
1407 return tokval->t_type = TOKEN_EQ;
1408 if (!strcmp(tline->text, "<>"))
1409 return tokval->t_type = TOKEN_NE;
1410 if (!strcmp(tline->text, "!="))
1411 return tokval->t_type = TOKEN_NE;
1412 if (!strcmp(tline->text, "<="))
1413 return tokval->t_type = TOKEN_LE;
1414 if (!strcmp(tline->text, ">="))
1415 return tokval->t_type = TOKEN_GE;
1416 if (!strcmp(tline->text, "&&"))
1417 return tokval->t_type = TOKEN_DBL_AND;
1418 if (!strcmp(tline->text, "^^"))
1419 return tokval->t_type = TOKEN_DBL_XOR;
1420 if (!strcmp(tline->text, "||"))
1421 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001422 }
1423
1424 /*
1425 * We have no other options: just return the first character of
1426 * the token text.
1427 */
1428 return tokval->t_type = tline->text[0];
1429}
1430
1431/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001432 * Compare a string to the name of an existing macro; this is a
1433 * simple wrapper which calls either strcmp or nasm_stricmp
1434 * depending on the value of the `casesense' parameter.
1435 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001436static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001437{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001438 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001439}
1440
1441/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001442 * Compare a string to the name of an existing macro; this is a
1443 * simple wrapper which calls either strcmp or nasm_stricmp
1444 * depending on the value of the `casesense' parameter.
1445 */
1446static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1447{
1448 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1449}
1450
1451/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001452 * Return the Context structure associated with a %$ token. Return
1453 * NULL, having _already_ reported an error condition, if the
1454 * context stack isn't deep enough for the supplied number of $
1455 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001456 *
1457 * If "namep" is non-NULL, set it to the pointer to the macro name
1458 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001459 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001460static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001461{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001462 Context *ctx;
1463 int i;
1464
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001465 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001466 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001467
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001468 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001469 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001470
H. Peter Anvine2c80182005-01-15 22:15:51 +00001471 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001472 nasm_error(ERR_NONFATAL, "`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001473 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001474 }
1475
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001476 name += 2;
1477 ctx = cstk;
1478 i = 0;
1479 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001480 name++;
1481 i++;
1482 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001483 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001484 if (!ctx) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001485 nasm_error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001486 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001487 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001488 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001489
1490 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001491 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001492
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001493 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001494}
1495
1496/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001497 * Check to see if a file is already in a string list
1498 */
1499static bool in_list(const StrList *list, const char *str)
1500{
1501 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001502 if (!strcmp(list->str, str))
1503 return true;
1504 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001505 }
1506 return false;
1507}
1508
1509/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001510 * Open an include file. This routine must always return a valid
1511 * file pointer if it returns - it's responsible for throwing an
1512 * ERR_FATAL and bombing out completely if not. It should also try
1513 * the include path one by one until it finds the file or reaches
1514 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001515 *
1516 * Note: for INC_PROBE the function returns NULL at all times;
1517 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001518 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001519enum incopen_mode {
1520 INC_NEEDED, /* File must exist */
1521 INC_OPTIONAL, /* Missing is OK */
1522 INC_PROBE /* Only an existence probe */
1523};
1524
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001525/* This is conducts a full pathname search */
1526static FILE *inc_fopen_search(const char *file, StrList **slpath,
1527 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001528{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001529 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001530 char *prefix = "";
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001531 const IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001532 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001533 size_t prefix_len = 0;
1534 StrList *sl;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001535 size_t path_len;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001536 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001537
H. Peter Anvine2c80182005-01-15 22:15:51 +00001538 while (1) {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001539 path_len = prefix_len + len + 1;
1540
1541 sl = nasm_malloc(path_len + sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001542 memcpy(sl->str, prefix, prefix_len);
1543 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001544 sl->next = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001545
H. Peter Anvind81a2352016-09-21 14:03:18 -07001546 if (omode == INC_PROBE) {
1547 fp = NULL;
1548 found = nasm_file_exists(sl->str);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001549 } else {
H. Peter Anvind81a2352016-09-21 14:03:18 -07001550 fp = nasm_open_read(sl->str, fmode);
1551 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001552 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001553 if (found) {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001554 *slpath = sl;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001555 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001556 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001557
H. Peter Anvind81a2352016-09-21 14:03:18 -07001558 nasm_free(sl);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001559
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001560 if (!ip)
1561 return NULL;
1562
1563 prefix = ip->path;
1564 prefix_len = strlen(prefix);
1565 ip = ip->next;
1566 }
1567}
1568
1569/*
1570 * Open a file, or test for the presence of one (depending on omode),
1571 * considering the include path.
1572 */
1573static FILE *inc_fopen(const char *file,
1574 StrList **dhead, StrList ***dtail,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001575 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001576 enum incopen_mode omode,
1577 enum file_flags fmode)
1578{
1579 StrList *sl;
1580 struct hash_insert hi;
1581 void **hp;
1582 char *path;
1583 FILE *fp = NULL;
1584
1585 hp = hash_find(&FileHash, file, &hi);
1586 if (hp) {
1587 path = *hp;
1588 } else {
1589 /* Need to do the actual path search */
1590 size_t file_len;
1591
1592 sl = NULL;
1593 fp = inc_fopen_search(file, &sl, omode, fmode);
1594
1595 file_len = strlen(file);
1596
1597 if (!sl) {
1598 /* Store negative result for this file */
1599 sl = nasm_malloc(file_len + 1 + sizeof sl->next);
1600 memcpy(sl->str, file, file_len+1);
1601 sl->next = NULL;
1602 file = sl->str;
1603 path = NULL;
1604 } else {
1605 path = sl->str;
1606 file = strchr(path, '\0') - file_len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001607 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001608
1609 hash_add(&hi, file, path); /* Positive or negative result */
1610
1611 if (path || omode != INC_NEEDED) {
1612 /*
1613 * Add file to dependency path. The in_list() is needed
1614 * in case the file was already added with %depend.
1615 */
1616 if (dhead && !in_list(*dhead, sl->str)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001617 **dtail = sl;
1618 *dtail = &sl->next;
1619 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001620 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001621 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001622
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001623 if (!path) {
1624 if (omode == INC_NEEDED)
1625 nasm_fatal(0, "unable to open include file `%s'", file);
1626
1627 if (found_path)
1628 *found_path = NULL;
1629
1630 return NULL;
1631 }
1632
1633 if (!fp && omode != INC_PROBE)
1634 fp = nasm_open_read(file, fmode);
1635
1636 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001637 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001638
1639 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001640}
1641
1642/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001643 * Opens an include or input file. Public version, for use by modules
1644 * that get a file:lineno pair and need to look at the file again
1645 * (e.g. the CodeView debug backend). Returns NULL on failure.
1646 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001647FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001648{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001649 return inc_fopen(filename, NULL, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001650}
1651
1652/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001653 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001654 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001655 * return true if _any_ single-line macro of that name is defined.
1656 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001657 * `nparam' or no parameters is defined.
1658 *
1659 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001660 * defined, or nparam is -1, the address of the definition structure
1661 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001662 * is NULL, no action will be taken regarding its contents, and no
1663 * error will occur.
1664 *
1665 * Note that this is also called with nparam zero to resolve
1666 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001667 *
1668 * If you already know which context macro belongs to, you can pass
1669 * the context pointer as first parameter; if you won't but name begins
1670 * with %$ the context will be automatically computed. If all_contexts
1671 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001672 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001673static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001674smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001675 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001676{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001677 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001678 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001679
H. Peter Anvin97a23472007-09-16 17:57:25 -07001680 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001681 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001682 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001683 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001684 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001686 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001687 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001688 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001689 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001690 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001691 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001692
H. Peter Anvine2c80182005-01-15 22:15:51 +00001693 while (m) {
1694 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001695 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001696 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001697 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001698 *defn = m;
1699 else
1700 *defn = NULL;
1701 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001702 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001703 }
1704 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001705 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001706
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001707 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001708}
1709
1710/*
1711 * Count and mark off the parameters in a multi-line macro call.
1712 * This is called both from within the multi-line macro expansion
1713 * code, and also to mark off the default parameters when provided
1714 * in a %macro definition line.
1715 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001716static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001717{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001718 int paramsize, brace;
1719
1720 *nparam = paramsize = 0;
1721 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001722 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001723 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001724 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001725 paramsize += PARAM_DELTA;
1726 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1727 }
1728 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001729 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001730 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001731 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001732 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001733 if (brace) {
1734 while (brace && (t = t->next) != NULL) {
1735 if (tok_is_(t, "{"))
1736 brace++;
1737 else if (tok_is_(t, "}"))
1738 brace--;
1739 }
1740
1741 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 /*
1743 * Now we've found the closing brace, look further
1744 * for the comma.
1745 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001746 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001747 skip_white_(t);
1748 if (tok_isnt_(t, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001749 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001750 "braces do not enclose all of macro parameter");
1751 while (tok_isnt_(t, ","))
1752 t = t->next;
1753 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001754 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001755 } else {
1756 while (tok_isnt_(t, ","))
1757 t = t->next;
1758 }
1759 if (t) { /* got a comma/brace */
1760 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001761 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001762 }
1763}
1764
1765/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001766 * Determine whether one of the various `if' conditions is true or
1767 * not.
1768 *
1769 * We must free the tline we get passed.
1770 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001771static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001772{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001773 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001774 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001775 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001776 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001777 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001778 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001779 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001780
1781 origline = tline;
1782
H. Peter Anvine2c80182005-01-15 22:15:51 +00001783 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001784 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001785 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001786 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001787 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001788 if (!tline)
1789 break;
1790 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001791 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001792 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001793 free_tlist(origline);
1794 return -1;
1795 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001796 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001797 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001798 tline = tline->next;
1799 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001800 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001801
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001802 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001803 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001804 while (tline) {
1805 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 if (!tline || (tline->type != TOK_ID &&
1807 (tline->type != TOK_PREPROC_ID ||
1808 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001809 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001810 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001811 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001813 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001814 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001815 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001816 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001817 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001818
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001819 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001820 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001821 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001822 while (tline) {
1823 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001824 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001825 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001826 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001827 tline->text[1] != '!'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001828 nasm_error(ERR_NONFATAL,
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001829 "`%s' expects environment variable names",
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001830 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001831 goto fail;
1832 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001833 p = tline->text;
1834 if (tline->type == TOK_PREPROC_ID)
1835 p += 2; /* Skip leading %! */
1836 if (*p == '\'' || *p == '\"' || *p == '`')
1837 nasm_unquote_cstr(p, ct);
1838 if (getenv(p))
1839 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001840 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001841 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001842 break;
1843
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001844 case PPC_IFIDN:
1845 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001846 tline = expand_smacro(tline);
1847 t = tt = tline;
1848 while (tok_isnt_(tt, ","))
1849 tt = tt->next;
1850 if (!tt) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001851 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001852 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001853 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001854 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001855 }
1856 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001857 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001858 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1859 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001860 nasm_error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001861 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001862 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001863 }
1864 if (t->type == TOK_WHITESPACE) {
1865 t = t->next;
1866 continue;
1867 }
1868 if (tt->type == TOK_WHITESPACE) {
1869 tt = tt->next;
1870 continue;
1871 }
1872 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001873 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001874 break;
1875 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001876 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001877 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001878 size_t l1 = nasm_unquote(t->text, NULL);
1879 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001880
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001881 if (l1 != l2) {
1882 j = false;
1883 break;
1884 }
1885 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1886 j = false;
1887 break;
1888 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001889 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001890 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001891 break;
1892 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001893
H. Peter Anvine2c80182005-01-15 22:15:51 +00001894 t = t->next;
1895 tt = tt->next;
1896 }
1897 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001898 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001899 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001900
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001901 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001902 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001903 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001904 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001905
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001906 skip_white_(tline);
1907 tline = expand_id(tline);
1908 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001909 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001910 "`%s' expects a macro name", pp_directives[ct]);
1911 goto fail;
1912 }
1913 searching.name = nasm_strdup(tline->text);
1914 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001915 searching.plus = false;
1916 searching.nolist = false;
1917 searching.in_progress = 0;
1918 searching.max_depth = 0;
1919 searching.rep_nest = NULL;
1920 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001921 searching.nparam_max = INT_MAX;
1922 tline = expand_smacro(tline->next);
1923 skip_white_(tline);
1924 if (!tline) {
1925 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001926 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001927 "`%s' expects a parameter count or nothing",
1928 pp_directives[ct]);
1929 } else {
1930 searching.nparam_min = searching.nparam_max =
1931 readnum(tline->text, &j);
1932 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001933 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001934 "unable to parse parameter count `%s'",
1935 tline->text);
1936 }
1937 if (tline && tok_is_(tline->next, "-")) {
1938 tline = tline->next->next;
1939 if (tok_is_(tline, "*"))
1940 searching.nparam_max = INT_MAX;
1941 else if (!tok_type_(tline, TOK_NUMBER))
H. Peter Anvin130736c2016-02-17 20:27:41 -08001942 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001943 "`%s' expects a parameter count after `-'",
1944 pp_directives[ct]);
1945 else {
1946 searching.nparam_max = readnum(tline->text, &j);
1947 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001948 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001949 "unable to parse parameter count `%s'",
1950 tline->text);
1951 if (searching.nparam_min > searching.nparam_max)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001952 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001953 "minimum parameter count exceeds maximum");
1954 }
1955 }
1956 if (tline && tok_is_(tline->next, "+")) {
1957 tline = tline->next;
1958 searching.plus = true;
1959 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001960 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1961 while (mmac) {
1962 if (!strcmp(mmac->name, searching.name) &&
1963 (mmac->nparam_min <= searching.nparam_max
1964 || searching.plus)
1965 && (searching.nparam_min <= mmac->nparam_max
1966 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001967 found = true;
1968 break;
1969 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001970 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001971 }
1972 if (tline && tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001973 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001974 "trailing garbage after %%ifmacro ignored");
1975 nasm_free(searching.name);
1976 j = found;
1977 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001978 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001979
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001980 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001981 needtype = TOK_ID;
1982 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001983 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001984 needtype = TOK_NUMBER;
1985 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001986 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001987 needtype = TOK_STRING;
1988 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001989
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001990iftype:
1991 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001992
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001993 while (tok_type_(t, TOK_WHITESPACE) ||
1994 (needtype == TOK_NUMBER &&
1995 tok_type_(t, TOK_OTHER) &&
1996 (t->text[0] == '-' || t->text[0] == '+') &&
1997 !t->text[1]))
1998 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001999
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002000 j = tok_type_(t, needtype);
2001 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002002
2003 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002004 t = tline = expand_smacro(tline);
2005 while (tok_type_(t, TOK_WHITESPACE))
2006 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002007
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002008 j = false;
2009 if (t) {
2010 t = t->next; /* Skip the actual token */
2011 while (tok_type_(t, TOK_WHITESPACE))
2012 t = t->next;
2013 j = !t; /* Should be nothing left */
2014 }
2015 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002016
H. Peter Anvin134b9462008-02-16 17:01:40 -08002017 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002018 t = tline = expand_smacro(tline);
2019 while (tok_type_(t, TOK_WHITESPACE))
2020 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002021
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002022 j = !t; /* Should be empty */
2023 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002024
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002025 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002026 t = tline = expand_smacro(tline);
2027 tptr = &t;
2028 tokval.t_type = TOKEN_INVALID;
2029 evalresult = evaluate(ppscan, tptr, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -08002030 NULL, pass | CRITICAL, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002031 if (!evalresult)
2032 return -1;
2033 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002034 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002035 "trailing garbage after expression ignored");
2036 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002037 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002038 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002039 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002040 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002041 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002042 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002043
H. Peter Anvine2c80182005-01-15 22:15:51 +00002044 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002045 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002046 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002047 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002048 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002049 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002050
2051 free_tlist(origline);
2052 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002053
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002054fail:
2055 free_tlist(origline);
2056 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002057}
2058
2059/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002060 * Common code for defining an smacro
2061 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002062static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002063 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002064{
2065 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002066 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002067
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002068 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002069 if (!smac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002070 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002071 "single-line macro `%s' defined both with and"
2072 " without parameters", mname);
2073 /*
2074 * Some instances of the old code considered this a failure,
2075 * some others didn't. What is the right thing to do here?
2076 */
2077 free_tlist(expansion);
2078 return false; /* Failure */
2079 } else {
2080 /*
2081 * We're redefining, so we have to take over an
2082 * existing SMacro structure. This means freeing
2083 * what was already in it.
2084 */
2085 nasm_free(smac->name);
2086 free_tlist(smac->expansion);
2087 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002088 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002089 smtbl = ctx ? &ctx->localmac : &smacros;
2090 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002091 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002092 smac->next = *smhead;
2093 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002094 }
2095 smac->name = nasm_strdup(mname);
2096 smac->casesense = casesense;
2097 smac->nparam = nparam;
2098 smac->expansion = expansion;
2099 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002100 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002101}
2102
2103/*
2104 * Undefine an smacro
2105 */
2106static void undef_smacro(Context *ctx, const char *mname)
2107{
2108 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002109 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002110
H. Peter Anvin166c2472008-05-28 12:28:58 -07002111 smtbl = ctx ? &ctx->localmac : &smacros;
2112 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002113
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002114 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002115 /*
2116 * We now have a macro name... go hunt for it.
2117 */
2118 sp = smhead;
2119 while ((s = *sp) != NULL) {
2120 if (!mstrcmp(s->name, mname, s->casesense)) {
2121 *sp = s->next;
2122 nasm_free(s->name);
2123 free_tlist(s->expansion);
2124 nasm_free(s);
2125 } else {
2126 sp = &s->next;
2127 }
2128 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002129 }
2130}
2131
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002132/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002133 * Parse a mmacro specification.
2134 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002135static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002136{
2137 bool err;
2138
2139 tline = tline->next;
2140 skip_white_(tline);
2141 tline = expand_id(tline);
2142 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002143 nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002144 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002145 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002146
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002147 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002148 def->name = nasm_strdup(tline->text);
2149 def->plus = false;
2150 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002151 def->in_progress = 0;
2152 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002153 def->nparam_min = 0;
2154 def->nparam_max = 0;
2155
H. Peter Anvina26433d2008-07-16 14:40:01 -07002156 tline = expand_smacro(tline->next);
2157 skip_white_(tline);
2158 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002159 nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002160 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002161 def->nparam_min = def->nparam_max =
2162 readnum(tline->text, &err);
2163 if (err)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002164 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002165 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002166 }
2167 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002168 tline = tline->next->next;
2169 if (tok_is_(tline, "*")) {
2170 def->nparam_max = INT_MAX;
2171 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002172 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002173 "`%s' expects a parameter count after `-'", directive);
2174 } else {
2175 def->nparam_max = readnum(tline->text, &err);
2176 if (err) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002177 nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002178 tline->text);
2179 }
2180 if (def->nparam_min > def->nparam_max) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002181 nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002182 }
2183 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002184 }
2185 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002186 tline = tline->next;
2187 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002188 }
2189 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002190 !nasm_stricmp(tline->next->text, ".nolist")) {
2191 tline = tline->next;
2192 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002193 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002194
H. Peter Anvina26433d2008-07-16 14:40:01 -07002195 /*
2196 * Handle default parameters.
2197 */
2198 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002199 def->dlist = tline->next;
2200 tline->next = NULL;
2201 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002202 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002203 def->dlist = NULL;
2204 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002205 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002206 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002207
H. Peter Anvin89cee572009-07-15 09:16:54 -04002208 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002209 !def->plus)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002210 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002211 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002212
H. Peter Anvina26433d2008-07-16 14:40:01 -07002213 return true;
2214}
2215
2216
2217/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002218 * Decode a size directive
2219 */
2220static int parse_size(const char *str) {
2221 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002222 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002223 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002224 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002225
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002226 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002227}
2228
Ed Beroset3ab3f412002-06-11 03:31:49 +00002229/**
2230 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002231 * Find out if a line contains a preprocessor directive, and deal
2232 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002233 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002234 * If a directive _is_ found, it is the responsibility of this routine
2235 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002236 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002237 * @param tline a pointer to the current tokeninzed line linked list
2238 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002239 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002240 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002241static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002242{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002243 enum preproc_token i;
2244 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002245 bool err;
2246 int nparam;
2247 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002248 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002249 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002250 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002251 char *p, *pp;
2252 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002253 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002254 Include *inc;
2255 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002256 Cond *cond;
2257 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002258 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002259 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002260 struct tokenval tokval;
2261 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002262 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002263 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002264 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002265 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002266
2267 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002268
H. Peter Anvineba20a72002-04-30 20:53:55 +00002269 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002270 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002271 (tline->text[1] == '%' || tline->text[1] == '$'
2272 || tline->text[1] == '!'))
2273 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002274
H. Peter Anvin4169a472007-09-12 01:29:43 +00002275 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002276
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002277 /*
2278 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2279 * since they are known to be buggy at moment, we need to fix them
2280 * in future release (2.09-2.10)
2281 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002282 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002283 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002284 tline->text);
2285 return NO_DIRECTIVE_FOUND;
2286 }
2287
2288 /*
2289 * If we're in a non-emitting branch of a condition construct,
2290 * or walking to the end of an already terminated %rep block,
2291 * we should ignore all directives except for condition
2292 * directives.
2293 */
2294 if (((istk->conds && !emitting(istk->conds->state)) ||
2295 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2296 return NO_DIRECTIVE_FOUND;
2297 }
2298
2299 /*
2300 * If we're defining a macro or reading a %rep block, we should
2301 * ignore all directives except for %macro/%imacro (which nest),
2302 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2303 * If we're in a %rep block, another %rep nests, so should be let through.
2304 */
2305 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2306 i != PP_RMACRO && i != PP_IRMACRO &&
2307 i != PP_ENDMACRO && i != PP_ENDM &&
2308 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2309 return NO_DIRECTIVE_FOUND;
2310 }
2311
2312 if (defining) {
2313 if (i == PP_MACRO || i == PP_IMACRO ||
2314 i == PP_RMACRO || i == PP_IRMACRO) {
2315 nested_mac_count++;
2316 return NO_DIRECTIVE_FOUND;
2317 } else if (nested_mac_count > 0) {
2318 if (i == PP_ENDMACRO) {
2319 nested_mac_count--;
2320 return NO_DIRECTIVE_FOUND;
2321 }
2322 }
2323 if (!defining->name) {
2324 if (i == PP_REP) {
2325 nested_rep_count++;
2326 return NO_DIRECTIVE_FOUND;
2327 } else if (nested_rep_count > 0) {
2328 if (i == PP_ENDREP) {
2329 nested_rep_count--;
2330 return NO_DIRECTIVE_FOUND;
2331 }
2332 }
2333 }
2334 }
2335
H. Peter Anvin4169a472007-09-12 01:29:43 +00002336 switch (i) {
2337 case PP_INVALID:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002338 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002339 tline->text);
2340 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002341
H. Peter Anvine2c80182005-01-15 22:15:51 +00002342 case PP_STACKSIZE:
2343 /* Directive to tell NASM what the default stack size is. The
2344 * default is for a 16-bit stack, and this can be overriden with
2345 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002346 */
2347 tline = tline->next;
2348 if (tline && tline->type == TOK_WHITESPACE)
2349 tline = tline->next;
2350 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002351 nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002352 free_tlist(origline);
2353 return DIRECTIVE_FOUND;
2354 }
2355 if (nasm_stricmp(tline->text, "flat") == 0) {
2356 /* All subsequent ARG directives are for a 32-bit stack */
2357 StackSize = 4;
2358 StackPointer = "ebp";
2359 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002360 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002361 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2362 /* All subsequent ARG directives are for a 64-bit stack */
2363 StackSize = 8;
2364 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002365 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002366 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002367 } else if (nasm_stricmp(tline->text, "large") == 0) {
2368 /* All subsequent ARG directives are for a 16-bit stack,
2369 * far function call.
2370 */
2371 StackSize = 2;
2372 StackPointer = "bp";
2373 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002374 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002375 } else if (nasm_stricmp(tline->text, "small") == 0) {
2376 /* All subsequent ARG directives are for a 16-bit stack,
2377 * far function call. We don't support near functions.
2378 */
2379 StackSize = 2;
2380 StackPointer = "bp";
2381 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002382 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002383 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002384 nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002385 free_tlist(origline);
2386 return DIRECTIVE_FOUND;
2387 }
2388 free_tlist(origline);
2389 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002390
H. Peter Anvine2c80182005-01-15 22:15:51 +00002391 case PP_ARG:
2392 /* TASM like ARG directive to define arguments to functions, in
2393 * the following form:
2394 *
2395 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2396 */
2397 offset = ArgOffset;
2398 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002399 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002400 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002401
H. Peter Anvine2c80182005-01-15 22:15:51 +00002402 /* Find the argument name */
2403 tline = tline->next;
2404 if (tline && tline->type == TOK_WHITESPACE)
2405 tline = tline->next;
2406 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002407 nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002408 free_tlist(origline);
2409 return DIRECTIVE_FOUND;
2410 }
2411 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002412
H. Peter Anvine2c80182005-01-15 22:15:51 +00002413 /* Find the argument size type */
2414 tline = tline->next;
2415 if (!tline || tline->type != TOK_OTHER
2416 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002417 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002418 "Syntax error processing `%%arg' directive");
2419 free_tlist(origline);
2420 return DIRECTIVE_FOUND;
2421 }
2422 tline = tline->next;
2423 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002424 nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002425 free_tlist(origline);
2426 return DIRECTIVE_FOUND;
2427 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002428
H. Peter Anvine2c80182005-01-15 22:15:51 +00002429 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002430 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002431 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002432 size = parse_size(tt->text);
2433 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002434 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002435 "Invalid size type for `%%arg' missing directive");
2436 free_tlist(tt);
2437 free_tlist(origline);
2438 return DIRECTIVE_FOUND;
2439 }
2440 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002441
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002442 /* Round up to even stack slots */
2443 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002444
H. Peter Anvine2c80182005-01-15 22:15:51 +00002445 /* Now define the macro for the argument */
2446 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2447 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002448 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002449 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002450
H. Peter Anvine2c80182005-01-15 22:15:51 +00002451 /* Move to the next argument in the list */
2452 tline = tline->next;
2453 if (tline && tline->type == TOK_WHITESPACE)
2454 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002455 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002456 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002457 free_tlist(origline);
2458 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002459
H. Peter Anvine2c80182005-01-15 22:15:51 +00002460 case PP_LOCAL:
2461 /* TASM like LOCAL directive to define local variables for a
2462 * function, in the following form:
2463 *
2464 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2465 *
2466 * The '= LocalSize' at the end is ignored by NASM, but is
2467 * required by TASM to define the local parameter size (and used
2468 * by the TASM macro package).
2469 */
2470 offset = LocalOffset;
2471 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002472 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002473 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002474
H. Peter Anvine2c80182005-01-15 22:15:51 +00002475 /* Find the argument name */
2476 tline = tline->next;
2477 if (tline && tline->type == TOK_WHITESPACE)
2478 tline = tline->next;
2479 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002480 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002481 "`%%local' missing argument parameter");
2482 free_tlist(origline);
2483 return DIRECTIVE_FOUND;
2484 }
2485 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002486
H. Peter Anvine2c80182005-01-15 22:15:51 +00002487 /* Find the argument size type */
2488 tline = tline->next;
2489 if (!tline || tline->type != TOK_OTHER
2490 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002491 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002492 "Syntax error processing `%%local' directive");
2493 free_tlist(origline);
2494 return DIRECTIVE_FOUND;
2495 }
2496 tline = tline->next;
2497 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002498 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002499 "`%%local' missing size type parameter");
2500 free_tlist(origline);
2501 return DIRECTIVE_FOUND;
2502 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002503
H. Peter Anvine2c80182005-01-15 22:15:51 +00002504 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002505 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002506 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002507 size = parse_size(tt->text);
2508 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002509 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002510 "Invalid size type for `%%local' missing directive");
2511 free_tlist(tt);
2512 free_tlist(origline);
2513 return DIRECTIVE_FOUND;
2514 }
2515 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002516
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002517 /* Round up to even stack slots */
2518 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002519
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002520 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002521
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002522 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002523 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2524 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002525 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002526
H. Peter Anvine2c80182005-01-15 22:15:51 +00002527 /* Now define the assign to setup the enter_c macro correctly */
2528 snprintf(directive, sizeof(directive),
2529 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002530 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002531
H. Peter Anvine2c80182005-01-15 22:15:51 +00002532 /* Move to the next argument in the list */
2533 tline = tline->next;
2534 if (tline && tline->type == TOK_WHITESPACE)
2535 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002536 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002537 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002538 free_tlist(origline);
2539 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002540
H. Peter Anvine2c80182005-01-15 22:15:51 +00002541 case PP_CLEAR:
2542 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002543 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002544 "trailing garbage after `%%clear' ignored");
2545 free_macros();
2546 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002547 free_tlist(origline);
2548 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002549
H. Peter Anvin418ca702008-05-30 10:42:30 -07002550 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002551 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002552 skip_white_(t);
2553 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002554 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002555 nasm_error(ERR_NONFATAL, "`%%depend' expects a file name");
H. Peter Anvin418ca702008-05-30 10:42:30 -07002556 free_tlist(origline);
2557 return DIRECTIVE_FOUND; /* but we did _something_ */
2558 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002559 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002560 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002561 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002562 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002563 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002564 nasm_unquote_cstr(p, i);
2565 if (dephead && !in_list(*dephead, p)) {
2566 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2567 sl->next = NULL;
2568 strcpy(sl->str, p);
2569 *deptail = sl;
2570 deptail = &sl->next;
2571 }
2572 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002573 return DIRECTIVE_FOUND;
2574
2575 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002576 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002577 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002578
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002579 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002580 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002581 nasm_error(ERR_NONFATAL, "`%%include' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002582 free_tlist(origline);
2583 return DIRECTIVE_FOUND; /* but we did _something_ */
2584 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002585 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002586 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002587 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002588 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002589 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002590 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002591 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002592 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002593 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002594 found_path = NULL;
H. Peter Anvind81a2352016-09-21 14:03:18 -07002595 inc->fp = inc_fopen(p, dephead, &deptail, &found_path,
2596 pass == 0 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002597 if (!inc->fp) {
2598 /* -MG given but file not found */
2599 nasm_free(inc);
2600 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002601 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002602 inc->lineno = src_set_linnum(0);
2603 inc->lineinc = 1;
2604 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002605 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002606 istk = inc;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002607 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002608 }
2609 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002610 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002611
H. Peter Anvind2456592008-06-19 15:04:18 -07002612 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002613 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002614 static macros_t *use_pkg;
2615 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002616
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002617 tline = tline->next;
2618 skip_white_(tline);
2619 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002620
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002621 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002622 tline->type != TOK_INTERNAL_STRING &&
2623 tline->type != TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002624 nasm_error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002625 free_tlist(origline);
2626 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002627 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002628 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002629 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002630 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002631 if (tline->type == TOK_STRING)
2632 nasm_unquote_cstr(tline->text, i);
2633 use_pkg = nasm_stdmac_find_package(tline->text);
2634 if (!use_pkg)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002635 nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002636 else
2637 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002638 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002639 /* Not already included, go ahead and include it */
2640 stdmacpos = use_pkg;
2641 }
2642 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002643 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002644 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002645 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002646 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002647 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002648 tline = tline->next;
2649 skip_white_(tline);
2650 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002651 if (tline) {
2652 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002653 nasm_error(ERR_NONFATAL, "`%s' expects a context identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002654 pp_directives[i]);
2655 free_tlist(origline);
2656 return DIRECTIVE_FOUND; /* but we did _something_ */
2657 }
2658 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002659 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002660 "trailing garbage after `%s' ignored",
2661 pp_directives[i]);
2662 p = nasm_strdup(tline->text);
2663 } else {
2664 p = NULL; /* Anonymous */
2665 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002666
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002667 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002668 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002669 ctx->next = cstk;
2670 hash_init(&ctx->localmac, HASH_SMALL);
2671 ctx->name = p;
2672 ctx->number = unique++;
2673 cstk = ctx;
2674 } else {
2675 /* %pop or %repl */
2676 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002677 nasm_error(ERR_NONFATAL, "`%s': context stack is empty",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002678 pp_directives[i]);
2679 } else if (i == PP_POP) {
2680 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08002681 nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002682 "expected %s",
2683 cstk->name ? cstk->name : "anonymous", p);
2684 else
2685 ctx_pop();
2686 } else {
2687 /* i == PP_REPL */
2688 nasm_free(cstk->name);
2689 cstk->name = p;
2690 p = NULL;
2691 }
2692 nasm_free(p);
2693 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002694 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002695 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002696 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002697 severity = ERR_FATAL;
2698 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002699 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002700 severity = ERR_NONFATAL;
2701 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002702 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002703 severity = ERR_WARNING|ERR_WARN_USER;
2704 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002705
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002706issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002707 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002708 /* Only error out if this is the final pass */
2709 if (pass != 2 && i != PP_FATAL)
2710 return DIRECTIVE_FOUND;
2711
2712 tline->next = expand_smacro(tline->next);
2713 tline = tline->next;
2714 skip_white_(tline);
2715 t = tline ? tline->next : NULL;
2716 skip_white_(t);
2717 if (tok_type_(tline, TOK_STRING) && !t) {
2718 /* The line contains only a quoted string */
2719 p = tline->text;
2720 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002721 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002722 } else {
2723 /* Not a quoted string, or more than a quoted string */
2724 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002725 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002726 nasm_free(p);
2727 }
2728 free_tlist(origline);
2729 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002730 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002731
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002732 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002733 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002734 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002735 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002736 j = if_condition(tline->next, i);
2737 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002738 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002739 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002740 cond = nasm_malloc(sizeof(Cond));
2741 cond->next = istk->conds;
2742 cond->state = j;
2743 istk->conds = cond;
2744 if(istk->mstk)
2745 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002746 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002747 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002748
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002749 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002750 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002751 nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002752 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002753 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002754 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002755 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002756
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002757 case COND_DONE:
2758 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002759 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002760
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002761 case COND_ELSE_TRUE:
2762 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002763 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2764 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002765 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002766 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002767
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002768 case COND_IF_FALSE:
2769 /*
2770 * IMPORTANT: In the case of %if, we will already have
2771 * called expand_mmac_params(); however, if we're
2772 * processing an %elif we must have been in a
2773 * non-emitting mode, which would have inhibited
2774 * the normal invocation of expand_mmac_params().
2775 * Therefore, we have to do it explicitly here.
2776 */
2777 j = if_condition(expand_mmac_params(tline->next), i);
2778 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002779 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002780 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2781 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002782 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002783 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002784 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002785
H. Peter Anvine2c80182005-01-15 22:15:51 +00002786 case PP_ELSE:
2787 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002788 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2789 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002790 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002791 nasm_fatal(0, "`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002792 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002793 case COND_IF_TRUE:
2794 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002795 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002796 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002797
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002798 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002799 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002800
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002801 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002802 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002803 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002804
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002805 case COND_ELSE_TRUE:
2806 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002807 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002808 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002809 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002810 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002811 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002812 free_tlist(origline);
2813 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002814
H. Peter Anvine2c80182005-01-15 22:15:51 +00002815 case PP_ENDIF:
2816 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002817 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2818 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002819 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002820 nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002821 cond = istk->conds;
2822 istk->conds = cond->next;
2823 nasm_free(cond);
2824 if(istk->mstk)
2825 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002826 free_tlist(origline);
2827 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002828
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002829 case PP_RMACRO:
2830 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002831 case PP_MACRO:
2832 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002833 if (defining) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002834 nasm_error(ERR_FATAL, "`%s': already defining a macro",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002835 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002836 return DIRECTIVE_FOUND;
2837 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002838 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002839 defining->max_depth =
2840 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2841 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2842 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2843 nasm_free(defining);
2844 defining = NULL;
2845 return DIRECTIVE_FOUND;
2846 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002847
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002848 src_get(&defining->xline, &defining->fname);
2849
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002850 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2851 while (mmac) {
2852 if (!strcmp(mmac->name, defining->name) &&
2853 (mmac->nparam_min <= defining->nparam_max
2854 || defining->plus)
2855 && (defining->nparam_min <= mmac->nparam_max
2856 || mmac->plus)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002857 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002858 "redefining multi-line macro `%s'", defining->name);
2859 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002860 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002861 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002862 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002863 free_tlist(origline);
2864 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002865
H. Peter Anvine2c80182005-01-15 22:15:51 +00002866 case PP_ENDM:
2867 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002868 if (! (defining && defining->name)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002869 nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002870 return DIRECTIVE_FOUND;
2871 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002872 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2873 defining->next = *mmhead;
2874 *mmhead = defining;
2875 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002876 free_tlist(origline);
2877 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002878
H. Peter Anvin89cee572009-07-15 09:16:54 -04002879 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002880 /*
2881 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002882 * macro-end marker for a macro with a name. Then we
2883 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002884 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002885 list_for_each(l, istk->expansion)
2886 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002887 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002888
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002889 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002890 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002891 * Remove all conditional entries relative to this
2892 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002893 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002894 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2895 cond = istk->conds;
2896 istk->conds = cond->next;
2897 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002898 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002899 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002900 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002901 nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002902 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002903 free_tlist(origline);
2904 return DIRECTIVE_FOUND;
2905
H. Peter Anvina26433d2008-07-16 14:40:01 -07002906 case PP_UNMACRO:
2907 case PP_UNIMACRO:
2908 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002909 MMacro **mmac_p;
2910 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002911
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002912 spec.casesense = (i == PP_UNMACRO);
2913 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2914 return DIRECTIVE_FOUND;
2915 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002916 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2917 while (mmac_p && *mmac_p) {
2918 mmac = *mmac_p;
2919 if (mmac->casesense == spec.casesense &&
2920 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2921 mmac->nparam_min == spec.nparam_min &&
2922 mmac->nparam_max == spec.nparam_max &&
2923 mmac->plus == spec.plus) {
2924 *mmac_p = mmac->next;
2925 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002926 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002927 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002928 }
2929 }
2930 free_tlist(origline);
2931 free_tlist(spec.dlist);
2932 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002933 }
2934
H. Peter Anvine2c80182005-01-15 22:15:51 +00002935 case PP_ROTATE:
2936 if (tline->next && tline->next->type == TOK_WHITESPACE)
2937 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002938 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002939 free_tlist(origline);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002940 nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002941 return DIRECTIVE_FOUND;
2942 }
2943 t = expand_smacro(tline->next);
2944 tline->next = NULL;
2945 free_tlist(origline);
2946 tline = t;
2947 tptr = &t;
2948 tokval.t_type = TOKEN_INVALID;
2949 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002950 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002951 free_tlist(tline);
2952 if (!evalresult)
2953 return DIRECTIVE_FOUND;
2954 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002955 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002956 "trailing garbage after expression ignored");
2957 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002958 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002959 return DIRECTIVE_FOUND;
2960 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002961 mmac = istk->mstk;
2962 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2963 mmac = mmac->next_active;
2964 if (!mmac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002965 nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002966 } else if (mmac->nparam == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002967 nasm_error(ERR_NONFATAL,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002968 "`%%rotate' invoked within macro without parameters");
2969 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002970 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002971
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002972 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002973 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002974 rotate += mmac->nparam;
2975
2976 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002977 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002978 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002979
H. Peter Anvine2c80182005-01-15 22:15:51 +00002980 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002981 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002982 do {
2983 tline = tline->next;
2984 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002985
H. Peter Anvine2c80182005-01-15 22:15:51 +00002986 if (tok_type_(tline, TOK_ID) &&
2987 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002988 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002989 do {
2990 tline = tline->next;
2991 } while (tok_type_(tline, TOK_WHITESPACE));
2992 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002993
H. Peter Anvine2c80182005-01-15 22:15:51 +00002994 if (tline) {
2995 t = expand_smacro(tline);
2996 tptr = &t;
2997 tokval.t_type = TOKEN_INVALID;
2998 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002999 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003000 if (!evalresult) {
3001 free_tlist(origline);
3002 return DIRECTIVE_FOUND;
3003 }
3004 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003005 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003006 "trailing garbage after expression ignored");
3007 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003008 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003009 return DIRECTIVE_FOUND;
3010 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003011 count = reloc_value(evalresult);
3012 if (count >= REP_LIMIT) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003013 nasm_error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003014 count = 0;
3015 } else
3016 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003017 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003018 nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003019 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003020 }
3021 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003022
3023 tmp_defining = defining;
3024 defining = nasm_malloc(sizeof(MMacro));
3025 defining->prev = NULL;
3026 defining->name = NULL; /* flags this macro as a %rep block */
3027 defining->casesense = false;
3028 defining->plus = false;
3029 defining->nolist = nolist;
3030 defining->in_progress = count;
3031 defining->max_depth = 0;
3032 defining->nparam_min = defining->nparam_max = 0;
3033 defining->defaults = NULL;
3034 defining->dlist = NULL;
3035 defining->expansion = NULL;
3036 defining->next_active = istk->mstk;
3037 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003038 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003039
H. Peter Anvine2c80182005-01-15 22:15:51 +00003040 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003041 if (!defining || defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003042 nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003043 return DIRECTIVE_FOUND;
3044 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003045
H. Peter Anvine2c80182005-01-15 22:15:51 +00003046 /*
3047 * Now we have a "macro" defined - although it has no name
3048 * and we won't be entering it in the hash tables - we must
3049 * push a macro-end marker for it on to istk->expansion.
3050 * After that, it will take care of propagating itself (a
3051 * macro-end marker line for a macro which is really a %rep
3052 * block will cause the macro to be re-expanded, complete
3053 * with another macro-end marker to ensure the process
3054 * continues) until the whole expansion is forcibly removed
3055 * from istk->expansion by a %exitrep.
3056 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003057 l = nasm_malloc(sizeof(Line));
3058 l->next = istk->expansion;
3059 l->finishes = defining;
3060 l->first = NULL;
3061 istk->expansion = l;
3062
3063 istk->mstk = defining;
3064
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08003065 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003066 tmp_defining = defining;
3067 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003068 free_tlist(origline);
3069 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003070
H. Peter Anvine2c80182005-01-15 22:15:51 +00003071 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003072 /*
3073 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003074 * macro-end marker for a macro with no name. Then we set
3075 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003076 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003077 list_for_each(l, istk->expansion)
3078 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003079 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003080
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003081 if (l)
3082 l->finishes->in_progress = 1;
3083 else
H. Peter Anvin130736c2016-02-17 20:27:41 -08003084 nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003085 free_tlist(origline);
3086 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003087
H. Peter Anvine2c80182005-01-15 22:15:51 +00003088 case PP_XDEFINE:
3089 case PP_IXDEFINE:
3090 case PP_DEFINE:
3091 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003092 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003093
H. Peter Anvine2c80182005-01-15 22:15:51 +00003094 tline = tline->next;
3095 skip_white_(tline);
3096 tline = expand_id(tline);
3097 if (!tline || (tline->type != TOK_ID &&
3098 (tline->type != TOK_PREPROC_ID ||
3099 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003100 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003101 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003102 free_tlist(origline);
3103 return DIRECTIVE_FOUND;
3104 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003105
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003106 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003107 last = tline;
3108 param_start = tline = tline->next;
3109 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003110
H. Peter Anvine2c80182005-01-15 22:15:51 +00003111 /* Expand the macro definition now for %xdefine and %ixdefine */
3112 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3113 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003114
H. Peter Anvine2c80182005-01-15 22:15:51 +00003115 if (tok_is_(tline, "(")) {
3116 /*
3117 * This macro has parameters.
3118 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003119
H. Peter Anvine2c80182005-01-15 22:15:51 +00003120 tline = tline->next;
3121 while (1) {
3122 skip_white_(tline);
3123 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003124 nasm_error(ERR_NONFATAL, "parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003125 free_tlist(origline);
3126 return DIRECTIVE_FOUND;
3127 }
3128 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003129 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003130 "`%s': parameter identifier expected",
3131 tline->text);
3132 free_tlist(origline);
3133 return DIRECTIVE_FOUND;
3134 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003135 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003136 tline = tline->next;
3137 skip_white_(tline);
3138 if (tok_is_(tline, ",")) {
3139 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003140 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003141 if (!tok_is_(tline, ")")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003142 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003143 "`)' expected to terminate macro template");
3144 free_tlist(origline);
3145 return DIRECTIVE_FOUND;
3146 }
3147 break;
3148 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003149 }
3150 last = tline;
3151 tline = tline->next;
3152 }
3153 if (tok_type_(tline, TOK_WHITESPACE))
3154 last = tline, tline = tline->next;
3155 macro_start = NULL;
3156 last->next = NULL;
3157 t = tline;
3158 while (t) {
3159 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003160 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003161 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003162 !strcmp(tt->text, t->text))
3163 t->type = tt->type;
3164 }
3165 tt = t->next;
3166 t->next = macro_start;
3167 macro_start = t;
3168 t = tt;
3169 }
3170 /*
3171 * Good. We now have a macro name, a parameter count, and a
3172 * token list (in reverse order) for an expansion. We ought
3173 * to be OK just to create an SMacro, store it, and let
3174 * free_tlist have the rest of the line (which we have
3175 * carefully re-terminated after chopping off the expansion
3176 * from the end).
3177 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003178 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003179 free_tlist(origline);
3180 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003181
H. Peter Anvine2c80182005-01-15 22:15:51 +00003182 case PP_UNDEF:
3183 tline = tline->next;
3184 skip_white_(tline);
3185 tline = expand_id(tline);
3186 if (!tline || (tline->type != TOK_ID &&
3187 (tline->type != TOK_PREPROC_ID ||
3188 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003189 nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003190 free_tlist(origline);
3191 return DIRECTIVE_FOUND;
3192 }
3193 if (tline->next) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003194 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003195 "trailing garbage after macro name ignored");
3196 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003197
H. Peter Anvine2c80182005-01-15 22:15:51 +00003198 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003199 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003200 undef_smacro(ctx, mname);
3201 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003202 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003203
H. Peter Anvin9e200162008-06-04 17:23:14 -07003204 case PP_DEFSTR:
3205 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003206 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003207
3208 tline = tline->next;
3209 skip_white_(tline);
3210 tline = expand_id(tline);
3211 if (!tline || (tline->type != TOK_ID &&
3212 (tline->type != TOK_PREPROC_ID ||
3213 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003214 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003215 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003216 free_tlist(origline);
3217 return DIRECTIVE_FOUND;
3218 }
3219
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003220 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003221 last = tline;
3222 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003223 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003224
3225 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003226 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003227
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003228 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003229 macro_start = nasm_malloc(sizeof(*macro_start));
3230 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003231 macro_start->text = nasm_quote(p, strlen(p));
3232 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003233 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003234 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003235
3236 /*
3237 * We now have a macro name, an implicit parameter count of
3238 * zero, and a string token to use as an expansion. Create
3239 * and store an SMacro.
3240 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003241 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003242 free_tlist(origline);
3243 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003244
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003245 case PP_DEFTOK:
3246 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003247 casesense = (i == PP_DEFTOK);
3248
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003249 tline = tline->next;
3250 skip_white_(tline);
3251 tline = expand_id(tline);
3252 if (!tline || (tline->type != TOK_ID &&
3253 (tline->type != TOK_PREPROC_ID ||
3254 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003255 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003256 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003257 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003258 free_tlist(origline);
3259 return DIRECTIVE_FOUND;
3260 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003261 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003262 last = tline;
3263 tline = expand_smacro(tline->next);
3264 last->next = NULL;
3265
3266 t = tline;
3267 while (tok_type_(t, TOK_WHITESPACE))
3268 t = t->next;
3269 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003270 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003271 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003272 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003273 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003274 free_tlist(tline);
3275 free_tlist(origline);
3276 return DIRECTIVE_FOUND;
3277 }
3278
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003279 /*
3280 * Convert the string to a token stream. Note that smacros
3281 * are stored with the token stream reversed, so we have to
3282 * reverse the output of tokenize().
3283 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003284 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003285 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003286
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003287 /*
3288 * We now have a macro name, an implicit parameter count of
3289 * zero, and a numeric token to use as an expansion. Create
3290 * and store an SMacro.
3291 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003292 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003293 free_tlist(tline);
3294 free_tlist(origline);
3295 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003296
H. Peter Anvin418ca702008-05-30 10:42:30 -07003297 case PP_PATHSEARCH:
3298 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003299 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003300
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003301 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003302
3303 tline = tline->next;
3304 skip_white_(tline);
3305 tline = expand_id(tline);
3306 if (!tline || (tline->type != TOK_ID &&
3307 (tline->type != TOK_PREPROC_ID ||
3308 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003309 nasm_error(ERR_NONFATAL,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003310 "`%%pathsearch' expects a macro identifier as first parameter");
3311 free_tlist(origline);
3312 return DIRECTIVE_FOUND;
3313 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003314 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003315 last = tline;
3316 tline = expand_smacro(tline->next);
3317 last->next = NULL;
3318
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003319 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003320 while (tok_type_(t, TOK_WHITESPACE))
3321 t = t->next;
3322
3323 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003324 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003325 nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003326 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003327 free_tlist(origline);
3328 return DIRECTIVE_FOUND; /* but we did _something_ */
3329 }
3330 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003331 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003332 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003333 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003334 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003335 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003336
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07003337 inc_fopen(p, NULL, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003338 if (!found_path)
3339 found_path = p;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003340 macro_start = nasm_malloc(sizeof(*macro_start));
3341 macro_start->next = NULL;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003342 macro_start->text = nasm_quote(found_path, strlen(found_path));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003343 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003344 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003345
3346 /*
3347 * We now have a macro name, an implicit parameter count of
3348 * zero, and a string token to use as an expansion. Create
3349 * and store an SMacro.
3350 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003351 define_smacro(ctx, mname, casesense, 0, macro_start);
3352 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003353 free_tlist(origline);
3354 return DIRECTIVE_FOUND;
3355 }
3356
H. Peter Anvine2c80182005-01-15 22:15:51 +00003357 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003358 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003359
H. Peter Anvine2c80182005-01-15 22:15:51 +00003360 tline = tline->next;
3361 skip_white_(tline);
3362 tline = expand_id(tline);
3363 if (!tline || (tline->type != TOK_ID &&
3364 (tline->type != TOK_PREPROC_ID ||
3365 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003366 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003367 "`%%strlen' expects a macro identifier as first parameter");
3368 free_tlist(origline);
3369 return DIRECTIVE_FOUND;
3370 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003371 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003372 last = tline;
3373 tline = expand_smacro(tline->next);
3374 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003375
H. Peter Anvine2c80182005-01-15 22:15:51 +00003376 t = tline;
3377 while (tok_type_(t, TOK_WHITESPACE))
3378 t = t->next;
3379 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003380 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003381 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003382 "`%%strlen` requires string as second parameter");
3383 free_tlist(tline);
3384 free_tlist(origline);
3385 return DIRECTIVE_FOUND;
3386 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003387
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003388 macro_start = nasm_malloc(sizeof(*macro_start));
3389 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003390 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003391 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003392
H. Peter Anvine2c80182005-01-15 22:15:51 +00003393 /*
3394 * We now have a macro name, an implicit parameter count of
3395 * zero, and a numeric token to use as an expansion. Create
3396 * and store an SMacro.
3397 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003398 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003399 free_tlist(tline);
3400 free_tlist(origline);
3401 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003402
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003403 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003404 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003405
3406 tline = tline->next;
3407 skip_white_(tline);
3408 tline = expand_id(tline);
3409 if (!tline || (tline->type != TOK_ID &&
3410 (tline->type != TOK_PREPROC_ID ||
3411 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003412 nasm_error(ERR_NONFATAL,
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003413 "`%%strcat' expects a macro identifier as first parameter");
3414 free_tlist(origline);
3415 return DIRECTIVE_FOUND;
3416 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003417 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003418 last = tline;
3419 tline = expand_smacro(tline->next);
3420 last->next = NULL;
3421
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003422 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003423 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003424 switch (t->type) {
3425 case TOK_WHITESPACE:
3426 break;
3427 case TOK_STRING:
3428 len += t->a.len = nasm_unquote(t->text, NULL);
3429 break;
3430 case TOK_OTHER:
3431 if (!strcmp(t->text, ",")) /* permit comma separators */
3432 break;
3433 /* else fall through */
3434 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003435 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003436 "non-string passed to `%%strcat' (%d)", t->type);
3437 free_tlist(tline);
3438 free_tlist(origline);
3439 return DIRECTIVE_FOUND;
3440 }
3441 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003442
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003443 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003444 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003445 if (t->type == TOK_STRING) {
3446 memcpy(p, t->text, t->a.len);
3447 p += t->a.len;
3448 }
3449 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003450
3451 /*
3452 * We now have a macro name, an implicit parameter count of
3453 * zero, and a numeric token to use as an expansion. Create
3454 * and store an SMacro.
3455 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003456 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3457 macro_start->text = nasm_quote(pp, len);
3458 nasm_free(pp);
3459 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003460 free_tlist(tline);
3461 free_tlist(origline);
3462 return DIRECTIVE_FOUND;
3463
H. Peter Anvine2c80182005-01-15 22:15:51 +00003464 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003465 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003466 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003467 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003468
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003469 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003470
H. Peter Anvine2c80182005-01-15 22:15:51 +00003471 tline = tline->next;
3472 skip_white_(tline);
3473 tline = expand_id(tline);
3474 if (!tline || (tline->type != TOK_ID &&
3475 (tline->type != TOK_PREPROC_ID ||
3476 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003477 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003478 "`%%substr' expects a macro identifier as first parameter");
3479 free_tlist(origline);
3480 return DIRECTIVE_FOUND;
3481 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003482 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003483 last = tline;
3484 tline = expand_smacro(tline->next);
3485 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003486
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003487 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003488 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003489 while (tok_type_(t, TOK_WHITESPACE))
3490 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003491
H. Peter Anvine2c80182005-01-15 22:15:51 +00003492 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003493 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003494 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003495 "`%%substr` requires string as second parameter");
3496 free_tlist(tline);
3497 free_tlist(origline);
3498 return DIRECTIVE_FOUND;
3499 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003500
H. Peter Anvine2c80182005-01-15 22:15:51 +00003501 tt = t->next;
3502 tptr = &tt;
3503 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003504 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003505 if (!evalresult) {
3506 free_tlist(tline);
3507 free_tlist(origline);
3508 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003509 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003510 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003511 free_tlist(tline);
3512 free_tlist(origline);
3513 return DIRECTIVE_FOUND;
3514 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003515 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003516
3517 while (tok_type_(tt, TOK_WHITESPACE))
3518 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003519 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003520 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003521 } else {
3522 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003523 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003524 if (!evalresult) {
3525 free_tlist(tline);
3526 free_tlist(origline);
3527 return DIRECTIVE_FOUND;
3528 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003529 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003530 free_tlist(tline);
3531 free_tlist(origline);
3532 return DIRECTIVE_FOUND;
3533 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003534 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003535 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003536
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003537 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003538
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003539 /* make start and count being in range */
3540 if (start < 0)
3541 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003542 if (count < 0)
3543 count = len + count + 1 - start;
3544 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003545 count = len - start;
3546 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003547 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003548
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003549 macro_start = nasm_malloc(sizeof(*macro_start));
3550 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003551 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003552 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003553 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003554
H. Peter Anvine2c80182005-01-15 22:15:51 +00003555 /*
3556 * We now have a macro name, an implicit parameter count of
3557 * zero, and a numeric token to use as an expansion. Create
3558 * and store an SMacro.
3559 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003560 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003561 free_tlist(tline);
3562 free_tlist(origline);
3563 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003564 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003565
H. Peter Anvine2c80182005-01-15 22:15:51 +00003566 case PP_ASSIGN:
3567 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003568 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003569
H. Peter Anvine2c80182005-01-15 22:15:51 +00003570 tline = tline->next;
3571 skip_white_(tline);
3572 tline = expand_id(tline);
3573 if (!tline || (tline->type != TOK_ID &&
3574 (tline->type != TOK_PREPROC_ID ||
3575 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003576 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003577 "`%%%sassign' expects a macro identifier",
3578 (i == PP_IASSIGN ? "i" : ""));
3579 free_tlist(origline);
3580 return DIRECTIVE_FOUND;
3581 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003582 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003583 last = tline;
3584 tline = expand_smacro(tline->next);
3585 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003586
H. Peter Anvine2c80182005-01-15 22:15:51 +00003587 t = tline;
3588 tptr = &t;
3589 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003590 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003591 free_tlist(tline);
3592 if (!evalresult) {
3593 free_tlist(origline);
3594 return DIRECTIVE_FOUND;
3595 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003596
H. Peter Anvine2c80182005-01-15 22:15:51 +00003597 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003598 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003599 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003600
H. Peter Anvine2c80182005-01-15 22:15:51 +00003601 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003602 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003603 "non-constant value given to `%%%sassign'",
3604 (i == PP_IASSIGN ? "i" : ""));
3605 free_tlist(origline);
3606 return DIRECTIVE_FOUND;
3607 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003608
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003609 macro_start = nasm_malloc(sizeof(*macro_start));
3610 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003611 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003612 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003613
H. Peter Anvine2c80182005-01-15 22:15:51 +00003614 /*
3615 * We now have a macro name, an implicit parameter count of
3616 * zero, and a numeric token to use as an expansion. Create
3617 * and store an SMacro.
3618 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003619 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003620 free_tlist(origline);
3621 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003622
H. Peter Anvine2c80182005-01-15 22:15:51 +00003623 case PP_LINE:
3624 /*
3625 * Syntax is `%line nnn[+mmm] [filename]'
3626 */
3627 tline = tline->next;
3628 skip_white_(tline);
3629 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003630 nasm_error(ERR_NONFATAL, "`%%line' expects line number");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003631 free_tlist(origline);
3632 return DIRECTIVE_FOUND;
3633 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003634 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003635 m = 1;
3636 tline = tline->next;
3637 if (tok_is_(tline, "+")) {
3638 tline = tline->next;
3639 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003640 nasm_error(ERR_NONFATAL, "`%%line' expects line increment");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003641 free_tlist(origline);
3642 return DIRECTIVE_FOUND;
3643 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003644 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003645 tline = tline->next;
3646 }
3647 skip_white_(tline);
3648 src_set_linnum(k);
3649 istk->lineinc = m;
3650 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003651 char *fname = detoken(tline, false);
3652 src_set_fname(fname);
3653 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003654 }
3655 free_tlist(origline);
3656 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003657
H. Peter Anvine2c80182005-01-15 22:15:51 +00003658 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003659 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003660 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003661 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003662 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003663 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003664}
3665
3666/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003667 * Ensure that a macro parameter contains a condition code and
3668 * nothing else. Return the condition code index if so, or -1
3669 * otherwise.
3670 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003671static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003672{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003673 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003674
H. Peter Anvin25a99342007-09-22 17:45:45 -07003675 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003676 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003677
H. Peter Anvineba20a72002-04-30 20:53:55 +00003678 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003679 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003680 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003681 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003682 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003683 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003684 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003685
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003686 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003687}
3688
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003689/*
3690 * This routines walks over tokens strem and hadnles tokens
3691 * pasting, if @handle_explicit passed then explicit pasting
3692 * term is handled, otherwise -- implicit pastings only.
3693 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003694static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003695 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003696{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003697 Token *tok, *next, **prev_next, **prev_nonspace;
3698 bool pasted = false;
3699 char *buf, *p;
3700 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003701
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003702 /*
3703 * The last token before pasting. We need it
3704 * to be able to connect new handled tokens.
3705 * In other words if there were a tokens stream
3706 *
3707 * A -> B -> C -> D
3708 *
3709 * and we've joined tokens B and C, the resulting
3710 * stream should be
3711 *
3712 * A -> BC -> D
3713 */
3714 tok = *head;
3715 prev_next = NULL;
3716
3717 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3718 prev_nonspace = head;
3719 else
3720 prev_nonspace = NULL;
3721
3722 while (tok && (next = tok->next)) {
3723
3724 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003725 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003726 /* Zap redundant whitespaces */
3727 while (tok_type_(next, TOK_WHITESPACE))
3728 next = delete_Token(next);
3729 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003730 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003731
3732 case TOK_PASTE:
3733 /* Explicit pasting */
3734 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003735 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003736 next = delete_Token(tok);
3737
3738 while (tok_type_(next, TOK_WHITESPACE))
3739 next = delete_Token(next);
3740
3741 if (!pasted)
3742 pasted = true;
3743
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003744 /* Left pasting token is start of line */
3745 if (!prev_nonspace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003746 nasm_error(ERR_FATAL, "No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003747
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003748 /*
3749 * No ending token, this might happen in two
3750 * cases
3751 *
3752 * 1) There indeed no right token at all
3753 * 2) There is a bare "%define ID" statement,
3754 * and @ID does expand to whitespace.
3755 *
3756 * So technically we need to do a grammar analysis
3757 * in another stage of parsing, but for now lets don't
3758 * change the behaviour people used to. Simply allow
3759 * whitespace after paste token.
3760 */
3761 if (!next) {
3762 /*
3763 * Zap ending space tokens and that's all.
3764 */
3765 tok = (*prev_nonspace)->next;
3766 while (tok_type_(tok, TOK_WHITESPACE))
3767 tok = delete_Token(tok);
3768 tok = *prev_nonspace;
3769 tok->next = NULL;
3770 break;
3771 }
3772
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003773 tok = *prev_nonspace;
3774 while (tok_type_(tok, TOK_WHITESPACE))
3775 tok = delete_Token(tok);
3776 len = strlen(tok->text);
3777 len += strlen(next->text);
3778
3779 p = buf = nasm_malloc(len + 1);
3780 strcpy(p, tok->text);
3781 p = strchr(p, '\0');
3782 strcpy(p, next->text);
3783
3784 delete_Token(tok);
3785
3786 tok = tokenize(buf);
3787 nasm_free(buf);
3788
3789 *prev_nonspace = tok;
3790 while (tok && tok->next)
3791 tok = tok->next;
3792
3793 tok->next = delete_Token(next);
3794
3795 /* Restart from pasted tokens head */
3796 tok = *prev_nonspace;
3797 break;
3798
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003799 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003800 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003801 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003802 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3803 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003804
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003805 len = 0;
3806 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3807 len += strlen(next->text);
3808 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003809 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003810
3811 /* No match */
3812 if (tok == next)
3813 break;
3814
3815 len += strlen(tok->text);
3816 p = buf = nasm_malloc(len + 1);
3817
3818 while (tok != next) {
3819 strcpy(p, tok->text);
3820 p = strchr(p, '\0');
3821 tok = delete_Token(tok);
3822 }
3823
3824 tok = tokenize(buf);
3825 nasm_free(buf);
3826
3827 if (prev_next)
3828 *prev_next = tok;
3829 else
3830 *head = tok;
3831
3832 /*
3833 * Connect pasted into original stream,
3834 * ie A -> new-tokens -> B
3835 */
3836 while (tok && tok->next)
3837 tok = tok->next;
3838 tok->next = next;
3839
3840 if (!pasted)
3841 pasted = true;
3842
3843 /* Restart from pasted tokens head */
3844 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003845 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003846
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003847 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003848 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003849
3850 prev_next = &tok->next;
3851
3852 if (tok->next &&
3853 !tok_type_(tok->next, TOK_WHITESPACE) &&
3854 !tok_type_(tok->next, TOK_PASTE))
3855 prev_nonspace = prev_next;
3856
3857 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003858 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003859
3860 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003861}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003862
3863/*
3864 * expands to a list of tokens from %{x:y}
3865 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003866static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003867{
3868 Token *t = tline, **tt, *tm, *head;
3869 char *pos;
3870 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003871
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003872 pos = strchr(tline->text, ':');
3873 nasm_assert(pos);
3874
3875 lst = atoi(pos + 1);
3876 fst = atoi(tline->text + 1);
3877
3878 /*
3879 * only macros params are accounted so
3880 * if someone passes %0 -- we reject such
3881 * value(s)
3882 */
3883 if (lst == 0 || fst == 0)
3884 goto err;
3885
3886 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003887 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3888 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003889 goto err;
3890
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003891 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3892 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003893
3894 /* counted from zero */
3895 fst--, lst--;
3896
3897 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003898 * It will be at least one token. Note we
3899 * need to scan params until separator, otherwise
3900 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003901 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003902 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003903 head = new_Token(NULL, tm->type, tm->text, 0);
3904 tt = &head->next, tm = tm->next;
3905 while (tok_isnt_(tm, ",")) {
3906 t = new_Token(NULL, tm->type, tm->text, 0);
3907 *tt = t, tt = &t->next, tm = tm->next;
3908 }
3909
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003910 if (fst < lst) {
3911 for (i = fst + 1; i <= lst; i++) {
3912 t = new_Token(NULL, TOK_OTHER, ",", 0);
3913 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003914 j = (i + mac->rotate) % mac->nparam;
3915 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003916 while (tok_isnt_(tm, ",")) {
3917 t = new_Token(NULL, tm->type, tm->text, 0);
3918 *tt = t, tt = &t->next, tm = tm->next;
3919 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003920 }
3921 } else {
3922 for (i = fst - 1; i >= lst; i--) {
3923 t = new_Token(NULL, TOK_OTHER, ",", 0);
3924 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003925 j = (i + mac->rotate) % mac->nparam;
3926 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003927 while (tok_isnt_(tm, ",")) {
3928 t = new_Token(NULL, tm->type, tm->text, 0);
3929 *tt = t, tt = &t->next, tm = tm->next;
3930 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003931 }
3932 }
3933
3934 *last = tt;
3935 return head;
3936
3937err:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003938 nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003939 &tline->text[1]);
3940 return tline;
3941}
3942
H. Peter Anvin76690a12002-04-30 20:52:49 +00003943/*
3944 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003945 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003946 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003947 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003948static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003949{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003950 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003951 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003952 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003953
3954 tail = &thead;
3955 thead = NULL;
3956
H. Peter Anvine2c80182005-01-15 22:15:51 +00003957 while (tline) {
3958 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003959 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3960 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3961 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003962 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003963 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003964 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003965 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003966 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003967 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003968
H. Peter Anvine2c80182005-01-15 22:15:51 +00003969 t = tline;
3970 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003971
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003972 mac = istk->mstk;
3973 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3974 mac = mac->next_active;
3975 if (!mac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003976 nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003977 } else {
3978 pos = strchr(t->text, ':');
3979 if (!pos) {
3980 switch (t->text[1]) {
3981 /*
3982 * We have to make a substitution of one of the
3983 * forms %1, %-1, %+1, %%foo, %0.
3984 */
3985 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003986 type = TOK_NUMBER;
3987 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3988 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003989 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003990 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003991 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003992 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003993 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003994 text = nasm_strcat(tmpbuf, t->text + 2);
3995 break;
3996 case '-':
3997 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003998 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003999 tt = NULL;
4000 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004001 if (mac->nparam > 1)
4002 n = (n + mac->rotate) % mac->nparam;
4003 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004004 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004005 cc = find_cc(tt);
4006 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004007 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004008 "macro parameter %d is not a condition code",
4009 n + 1);
4010 text = NULL;
4011 } else {
4012 type = TOK_ID;
4013 if (inverse_ccs[cc] == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004014 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004015 "condition code `%s' is not invertible",
4016 conditions[cc]);
4017 text = NULL;
4018 } else
4019 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4020 }
4021 break;
4022 case '+':
4023 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004024 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004025 tt = NULL;
4026 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004027 if (mac->nparam > 1)
4028 n = (n + mac->rotate) % mac->nparam;
4029 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004030 }
4031 cc = find_cc(tt);
4032 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004033 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004034 "macro parameter %d is not a condition code",
4035 n + 1);
4036 text = NULL;
4037 } else {
4038 type = TOK_ID;
4039 text = nasm_strdup(conditions[cc]);
4040 }
4041 break;
4042 default:
4043 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004044 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004045 tt = NULL;
4046 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004047 if (mac->nparam > 1)
4048 n = (n + mac->rotate) % mac->nparam;
4049 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004050 }
4051 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004052 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004053 *tail = new_Token(NULL, tt->type, tt->text, 0);
4054 tail = &(*tail)->next;
4055 tt = tt->next;
4056 }
4057 }
4058 text = NULL; /* we've done it here */
4059 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004060 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004061 } else {
4062 /*
4063 * seems we have a parameters range here
4064 */
4065 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004066 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004067 if (head != t) {
4068 *tail = head;
4069 *last = tline;
4070 tline = head;
4071 text = NULL;
4072 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004073 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004074 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004075 if (!text) {
4076 delete_Token(t);
4077 } else {
4078 *tail = t;
4079 tail = &t->next;
4080 t->type = type;
4081 nasm_free(t->text);
4082 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004083 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004084 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004085 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004086 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004087 } else if (tline->type == TOK_INDIRECT) {
4088 t = tline;
4089 tline = tline->next;
4090 tt = tokenize(t->text);
4091 tt = expand_mmac_params(tt);
4092 tt = expand_smacro(tt);
4093 *tail = tt;
4094 while (tt) {
4095 tt->a.mac = NULL; /* Necessary? */
4096 tail = &tt->next;
4097 tt = tt->next;
4098 }
4099 delete_Token(t);
4100 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004101 } else {
4102 t = *tail = tline;
4103 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004104 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004105 tail = &t->next;
4106 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004107 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004108 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004109
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004110 if (changed) {
4111 const struct tokseq_match t[] = {
4112 {
4113 PP_CONCAT_MASK(TOK_ID) |
4114 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4115 PP_CONCAT_MASK(TOK_ID) |
4116 PP_CONCAT_MASK(TOK_NUMBER) |
4117 PP_CONCAT_MASK(TOK_FLOAT) |
4118 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4119 },
4120 {
4121 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4122 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4123 }
4124 };
4125 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4126 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004127
H. Peter Anvin76690a12002-04-30 20:52:49 +00004128 return thead;
4129}
4130
4131/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004132 * Expand all single-line macro calls made in the given line.
4133 * Return the expanded version of the line. The original is deemed
4134 * to be destroyed in the process. (In reality we'll just move
4135 * Tokens from input to output a lot of the time, rather than
4136 * actually bothering to destroy and replicate.)
4137 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004138
H. Peter Anvine2c80182005-01-15 22:15:51 +00004139static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004140{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004141 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004142 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004143 Token **params;
4144 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004145 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004146 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004147 Token *org_tline = tline;
4148 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004149 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004150 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004151 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004152
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004153 /*
4154 * Trick: we should avoid changing the start token pointer since it can
4155 * be contained in "next" field of other token. Because of this
4156 * we allocate a copy of first token and work with it; at the end of
4157 * routine we copy it back
4158 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004159 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004160 tline = new_Token(org_tline->next, org_tline->type,
4161 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004162 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004163 nasm_free(org_tline->text);
4164 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004165 }
4166
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004167 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004168
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004169again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004170 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004171 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004172
H. Peter Anvine2c80182005-01-15 22:15:51 +00004173 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004174 if (!--deadman) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004175 nasm_error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004176 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004177 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004178
H. Peter Anvine2c80182005-01-15 22:15:51 +00004179 if ((mname = tline->text)) {
4180 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004181 if (tline->type == TOK_ID) {
4182 head = (SMacro *)hash_findix(&smacros, mname);
4183 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004184 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004185 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4186 } else
4187 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004188
H. Peter Anvine2c80182005-01-15 22:15:51 +00004189 /*
4190 * We've hit an identifier. As in is_mmacro below, we first
4191 * check whether the identifier is a single-line macro at
4192 * all, then think about checking for parameters if
4193 * necessary.
4194 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004195 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004196 if (!mstrcmp(m->name, mname, m->casesense))
4197 break;
4198 if (m) {
4199 mstart = tline;
4200 params = NULL;
4201 paramsize = NULL;
4202 if (m->nparam == 0) {
4203 /*
4204 * Simple case: the macro is parameterless. Discard the
4205 * one token that the macro call took, and push the
4206 * expansion back on the to-do stack.
4207 */
4208 if (!m->expansion) {
4209 if (!strcmp("__FILE__", m->name)) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004210 const char *file = src_get_fname();
4211 /* nasm_free(tline->text); here? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004212 tline->text = nasm_quote(file, strlen(file));
4213 tline->type = TOK_STRING;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004214 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004215 }
4216 if (!strcmp("__LINE__", m->name)) {
4217 nasm_free(tline->text);
4218 make_tok_num(tline, src_get_linnum());
4219 continue;
4220 }
4221 if (!strcmp("__BITS__", m->name)) {
4222 nasm_free(tline->text);
4223 make_tok_num(tline, globalbits);
4224 continue;
4225 }
4226 tline = delete_Token(tline);
4227 continue;
4228 }
4229 } else {
4230 /*
4231 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004232 * exists and takes parameters. We must find the
4233 * parameters in the call, count them, find the SMacro
4234 * that corresponds to that form of the macro call, and
4235 * substitute for the parameters when we expand. What a
4236 * pain.
4237 */
4238 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004239 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004240 do {
4241 t = tline->next;
4242 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004243 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004244 t->text = NULL;
4245 t = tline->next = delete_Token(t);
4246 }
4247 tline = t;
4248 } while (tok_type_(tline, TOK_WHITESPACE));
4249 if (!tok_is_(tline, "(")) {
4250 /*
4251 * This macro wasn't called with parameters: ignore
4252 * the call. (Behaviour borrowed from gnu cpp.)
4253 */
4254 tline = mstart;
4255 m = NULL;
4256 } else {
4257 int paren = 0;
4258 int white = 0;
4259 brackets = 0;
4260 nparam = 0;
4261 sparam = PARAM_DELTA;
4262 params = nasm_malloc(sparam * sizeof(Token *));
4263 params[0] = tline->next;
4264 paramsize = nasm_malloc(sparam * sizeof(int));
4265 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004266 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004267 /*
4268 * For some unusual expansions
4269 * which concatenates function call
4270 */
4271 t = tline->next;
4272 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004273 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004274 t->text = NULL;
4275 t = tline->next = delete_Token(t);
4276 }
4277 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004278
H. Peter Anvine2c80182005-01-15 22:15:51 +00004279 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004280 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004281 "macro call expects terminating `)'");
4282 break;
4283 }
4284 if (tline->type == TOK_WHITESPACE
4285 && brackets <= 0) {
4286 if (paramsize[nparam])
4287 white++;
4288 else
4289 params[nparam] = tline->next;
4290 continue; /* parameter loop */
4291 }
4292 if (tline->type == TOK_OTHER
4293 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004294 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004295 if (ch == ',' && !paren && brackets <= 0) {
4296 if (++nparam >= sparam) {
4297 sparam += PARAM_DELTA;
4298 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004299 sparam * sizeof(Token *));
4300 paramsize = nasm_realloc(paramsize,
4301 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004302 }
4303 params[nparam] = tline->next;
4304 paramsize[nparam] = 0;
4305 white = 0;
4306 continue; /* parameter loop */
4307 }
4308 if (ch == '{' &&
4309 (brackets > 0 || (brackets == 0 &&
4310 !paramsize[nparam])))
4311 {
4312 if (!(brackets++)) {
4313 params[nparam] = tline->next;
4314 continue; /* parameter loop */
4315 }
4316 }
4317 if (ch == '}' && brackets > 0)
4318 if (--brackets == 0) {
4319 brackets = -1;
4320 continue; /* parameter loop */
4321 }
4322 if (ch == '(' && !brackets)
4323 paren++;
4324 if (ch == ')' && brackets <= 0)
4325 if (--paren < 0)
4326 break;
4327 }
4328 if (brackets < 0) {
4329 brackets = 0;
H. Peter Anvin130736c2016-02-17 20:27:41 -08004330 nasm_error(ERR_NONFATAL, "braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004331 "enclose all of macro parameter");
4332 }
4333 paramsize[nparam] += white + 1;
4334 white = 0;
4335 } /* parameter loop */
4336 nparam++;
4337 while (m && (m->nparam != nparam ||
4338 mstrcmp(m->name, mname,
4339 m->casesense)))
4340 m = m->next;
4341 if (!m)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004342 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004343 "macro `%s' exists, "
4344 "but not taking %d parameters",
4345 mstart->text, nparam);
4346 }
4347 }
4348 if (m && m->in_progress)
4349 m = NULL;
4350 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004351 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004352 * Design question: should we handle !tline, which
4353 * indicates missing ')' here, or expand those
4354 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004355 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004356 */
4357 nasm_free(params);
4358 nasm_free(paramsize);
4359 tline = mstart;
4360 } else {
4361 /*
4362 * Expand the macro: we are placed on the last token of the
4363 * call, so that we can easily split the call from the
4364 * following tokens. We also start by pushing an SMAC_END
4365 * token for the cycle removal.
4366 */
4367 t = tline;
4368 if (t) {
4369 tline = t->next;
4370 t->next = NULL;
4371 }
4372 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004373 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004374 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004375 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004376 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004377 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004378 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004379 Token *ttt, *pt;
4380 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004381
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004382 ttt = params[t->type - TOK_SMAC_PARAM];
4383 i = paramsize[t->type - TOK_SMAC_PARAM];
4384 while (--i >= 0) {
4385 pt = *ptail = new_Token(tline, ttt->type,
4386 ttt->text, 0);
4387 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004388 ttt = ttt->next;
4389 }
4390 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004391 } else if (t->type == TOK_PREPROC_Q) {
4392 tt = new_Token(tline, TOK_ID, mname, 0);
4393 tline = tt;
4394 } else if (t->type == TOK_PREPROC_QQ) {
4395 tt = new_Token(tline, TOK_ID, m->name, 0);
4396 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004397 } else {
4398 tt = new_Token(tline, t->type, t->text, 0);
4399 tline = tt;
4400 }
4401 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004402
H. Peter Anvine2c80182005-01-15 22:15:51 +00004403 /*
4404 * Having done that, get rid of the macro call, and clean
4405 * up the parameters.
4406 */
4407 nasm_free(params);
4408 nasm_free(paramsize);
4409 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004410 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004411 continue; /* main token loop */
4412 }
4413 }
4414 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004415
H. Peter Anvine2c80182005-01-15 22:15:51 +00004416 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004417 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004418 tline = delete_Token(tline);
4419 } else {
4420 t = *tail = tline;
4421 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004422 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004423 t->next = NULL;
4424 tail = &t->next;
4425 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004426 }
4427
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004428 /*
4429 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004430 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004431 * TOK_IDs should be concatenated.
4432 * Also we look for %+ tokens and concatenate the tokens before and after
4433 * them (without white spaces in between).
4434 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004435 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004436 const struct tokseq_match t[] = {
4437 {
4438 PP_CONCAT_MASK(TOK_ID) |
4439 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4440 PP_CONCAT_MASK(TOK_ID) |
4441 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4442 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4443 }
4444 };
4445 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004446 /*
4447 * If we concatenated something, *and* we had previously expanded
4448 * an actual macro, scan the lines again for macros...
4449 */
4450 tline = thead;
4451 expanded = false;
4452 goto again;
4453 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004454 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004455
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004456err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004457 if (org_tline) {
4458 if (thead) {
4459 *org_tline = *thead;
4460 /* since we just gave text to org_line, don't free it */
4461 thead->text = NULL;
4462 delete_Token(thead);
4463 } else {
4464 /* the expression expanded to empty line;
4465 we can't return NULL for some reasons
4466 we just set the line to a single WHITESPACE token. */
4467 memset(org_tline, 0, sizeof(*org_tline));
4468 org_tline->text = NULL;
4469 org_tline->type = TOK_WHITESPACE;
4470 }
4471 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004472 }
4473
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004474 return thead;
4475}
4476
4477/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004478 * Similar to expand_smacro but used exclusively with macro identifiers
4479 * right before they are fetched in. The reason is that there can be
4480 * identifiers consisting of several subparts. We consider that if there
4481 * are more than one element forming the name, user wants a expansion,
4482 * otherwise it will be left as-is. Example:
4483 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004484 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004485 *
4486 * the identifier %$abc will be left as-is so that the handler for %define
4487 * will suck it and define the corresponding value. Other case:
4488 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004489 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004490 *
4491 * In this case user wants name to be expanded *before* %define starts
4492 * working, so we'll expand %$abc into something (if it has a value;
4493 * otherwise it will be left as-is) then concatenate all successive
4494 * PP_IDs into one.
4495 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004496static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004497{
4498 Token *cur, *oldnext = NULL;
4499
H. Peter Anvin734b1882002-04-30 21:01:08 +00004500 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004501 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004502
4503 cur = tline;
4504 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004505 (cur->next->type == TOK_ID ||
4506 cur->next->type == TOK_PREPROC_ID
4507 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004508 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004509
4510 /* If identifier consists of just one token, don't expand */
4511 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004512 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004513
H. Peter Anvine2c80182005-01-15 22:15:51 +00004514 if (cur) {
4515 oldnext = cur->next; /* Detach the tail past identifier */
4516 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004517 }
4518
H. Peter Anvin734b1882002-04-30 21:01:08 +00004519 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004520
H. Peter Anvine2c80182005-01-15 22:15:51 +00004521 if (cur) {
4522 /* expand_smacro possibly changhed tline; re-scan for EOL */
4523 cur = tline;
4524 while (cur && cur->next)
4525 cur = cur->next;
4526 if (cur)
4527 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004528 }
4529
4530 return tline;
4531}
4532
4533/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004534 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004535 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004536 * to check for an initial label - that's taken care of in
4537 * expand_mmacro - but must check numbers of parameters. Guaranteed
4538 * to be called with tline->type == TOK_ID, so the putative macro
4539 * name is easy to find.
4540 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004541static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004542{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004543 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004544 Token **params;
4545 int nparam;
4546
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004547 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004548
4549 /*
4550 * Efficiency: first we see if any macro exists with the given
4551 * name. If not, we can return NULL immediately. _Then_ we
4552 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004553 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004554 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004555 list_for_each(m, head)
4556 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004557 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004558 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004559 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004560
4561 /*
4562 * OK, we have a potential macro. Count and demarcate the
4563 * parameters.
4564 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004565 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004566
4567 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004568 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004569 * structure that handles this number.
4570 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004571 while (m) {
4572 if (m->nparam_min <= nparam
4573 && (m->plus || nparam <= m->nparam_max)) {
4574 /*
4575 * This one is right. Just check if cycle removal
4576 * prohibits us using it before we actually celebrate...
4577 */
4578 if (m->in_progress > m->max_depth) {
4579 if (m->max_depth > 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004580 nasm_error(ERR_WARNING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004581 "reached maximum recursion depth of %i",
4582 m->max_depth);
4583 }
4584 nasm_free(params);
4585 return NULL;
4586 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004587 /*
4588 * It's right, and we can use it. Add its default
4589 * parameters to the end of our list if necessary.
4590 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004591 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004592 params =
4593 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004594 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004595 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004596 while (nparam < m->nparam_min + m->ndefs) {
4597 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004598 nparam++;
4599 }
4600 }
4601 /*
4602 * If we've gone over the maximum parameter count (and
4603 * we're in Plus mode), ignore parameters beyond
4604 * nparam_max.
4605 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004606 if (m->plus && nparam > m->nparam_max)
4607 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004608 /*
4609 * Then terminate the parameter list, and leave.
4610 */
4611 if (!params) { /* need this special case */
4612 params = nasm_malloc(sizeof(*params));
4613 nparam = 0;
4614 }
4615 params[nparam] = NULL;
4616 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004617 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004618 }
4619 /*
4620 * This one wasn't right: look for the next one with the
4621 * same name.
4622 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004623 list_for_each(m, m->next)
4624 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004625 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004626 }
4627
4628 /*
4629 * After all that, we didn't find one with the right number of
4630 * parameters. Issue a warning, and fail to expand the macro.
4631 */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004632 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004633 "macro `%s' exists, but not taking %d parameters",
4634 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004635 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004636 return NULL;
4637}
4638
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004639
4640/*
4641 * Save MMacro invocation specific fields in
4642 * preparation for a recursive macro expansion
4643 */
4644static void push_mmacro(MMacro *m)
4645{
4646 MMacroInvocation *i;
4647
4648 i = nasm_malloc(sizeof(MMacroInvocation));
4649 i->prev = m->prev;
4650 i->params = m->params;
4651 i->iline = m->iline;
4652 i->nparam = m->nparam;
4653 i->rotate = m->rotate;
4654 i->paramlen = m->paramlen;
4655 i->unique = m->unique;
4656 i->condcnt = m->condcnt;
4657 m->prev = i;
4658}
4659
4660
4661/*
4662 * Restore MMacro invocation specific fields that were
4663 * saved during a previous recursive macro expansion
4664 */
4665static void pop_mmacro(MMacro *m)
4666{
4667 MMacroInvocation *i;
4668
4669 if (m->prev) {
4670 i = m->prev;
4671 m->prev = i->prev;
4672 m->params = i->params;
4673 m->iline = i->iline;
4674 m->nparam = i->nparam;
4675 m->rotate = i->rotate;
4676 m->paramlen = i->paramlen;
4677 m->unique = i->unique;
4678 m->condcnt = i->condcnt;
4679 nasm_free(i);
4680 }
4681}
4682
4683
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004684/*
4685 * Expand the multi-line macro call made by the given line, if
4686 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004687 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004688 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004689static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004690{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004691 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004692 Token *label = NULL;
4693 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004694 Token **params, *t, *tt;
4695 MMacro *m;
4696 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004697 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004698 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004699
4700 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004701 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004702 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004703 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004704 return 0;
4705 m = is_mmacro(t, &params);
4706 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004707 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004708 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004709 Token *last;
4710 /*
4711 * We have an id which isn't a macro call. We'll assume
4712 * it might be a label; we'll also check to see if a
4713 * colon follows it. Then, if there's another id after
4714 * that lot, we'll check it again for macro-hood.
4715 */
4716 label = last = t;
4717 t = t->next;
4718 if (tok_type_(t, TOK_WHITESPACE))
4719 last = t, t = t->next;
4720 if (tok_is_(t, ":")) {
4721 dont_prepend = 1;
4722 last = t, t = t->next;
4723 if (tok_type_(t, TOK_WHITESPACE))
4724 last = t, t = t->next;
4725 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004726 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4727 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004728 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004729 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004730 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004731 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004732
4733 /*
4734 * Fix up the parameters: this involves stripping leading and
4735 * trailing whitespace, then stripping braces if they are
4736 * present.
4737 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004738 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004739 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004740
H. Peter Anvine2c80182005-01-15 22:15:51 +00004741 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004742 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004743 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004744
H. Peter Anvine2c80182005-01-15 22:15:51 +00004745 t = params[i];
4746 skip_white_(t);
4747 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004748 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004749 params[i] = t;
4750 paramlen[i] = 0;
4751 while (t) {
4752 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4753 break; /* ... because we have hit a comma */
4754 if (comma && t->type == TOK_WHITESPACE
4755 && tok_is_(t->next, ","))
4756 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004757 if (brace && t->type == TOK_OTHER) {
4758 if (t->text[0] == '{')
4759 brace++; /* ... or a nested opening brace */
4760 else if (t->text[0] == '}')
4761 if (!--brace)
4762 break; /* ... or a brace */
4763 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004764 t = t->next;
4765 paramlen[i]++;
4766 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004767 if (brace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004768 nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004769 }
4770
4771 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004772 * OK, we have a MMacro structure together with a set of
4773 * parameters. We must now go through the expansion and push
4774 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004775 * parameter tokens and macro-local tokens doesn't get done
4776 * until the single-line macro substitution process; this is
4777 * because delaying them allows us to change the semantics
4778 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004779 *
4780 * First, push an end marker on to istk->expansion, mark this
4781 * macro as in progress, and set up its invocation-specific
4782 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004783 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004784 ll = nasm_malloc(sizeof(Line));
4785 ll->next = istk->expansion;
4786 ll->finishes = m;
4787 ll->first = NULL;
4788 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004789
4790 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004791 * Save the previous MMacro expansion in the case of
4792 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004793 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004794 if (m->max_depth && m->in_progress)
4795 push_mmacro(m);
4796
4797 m->in_progress ++;
4798 m->params = params;
4799 m->iline = tline;
4800 m->nparam = nparam;
4801 m->rotate = 0;
4802 m->paramlen = paramlen;
4803 m->unique = unique++;
4804 m->lineno = 0;
4805 m->condcnt = 0;
4806
4807 m->next_active = istk->mstk;
4808 istk->mstk = m;
4809
4810 list_for_each(l, m->expansion) {
4811 Token **tail;
4812
4813 ll = nasm_malloc(sizeof(Line));
4814 ll->finishes = NULL;
4815 ll->next = istk->expansion;
4816 istk->expansion = ll;
4817 tail = &ll->first;
4818
4819 list_for_each(t, l->first) {
4820 Token *x = t;
4821 switch (t->type) {
4822 case TOK_PREPROC_Q:
4823 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004824 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004825 case TOK_PREPROC_QQ:
4826 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4827 break;
4828 case TOK_PREPROC_ID:
4829 if (t->text[1] == '0' && t->text[2] == '0') {
4830 dont_prepend = -1;
4831 x = label;
4832 if (!x)
4833 continue;
4834 }
4835 /* fall through */
4836 default:
4837 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4838 break;
4839 }
4840 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004841 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004842 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004843 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004844
4845 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004846 * If we had a label, push it on as the first line of
4847 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004848 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004849 if (label) {
4850 if (dont_prepend < 0)
4851 free_tlist(startline);
4852 else {
4853 ll = nasm_malloc(sizeof(Line));
4854 ll->finishes = NULL;
4855 ll->next = istk->expansion;
4856 istk->expansion = ll;
4857 ll->first = startline;
4858 if (!dont_prepend) {
4859 while (label->next)
4860 label = label->next;
4861 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004862 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004863 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004864 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004865
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004866 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004867
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004868 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004869}
4870
H. Peter Anvin130736c2016-02-17 20:27:41 -08004871/*
4872 * This function adds macro names to error messages, and suppresses
4873 * them if necessary.
4874 */
4875static void pp_verror(int severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004876{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004877 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004878 MMacro *mmac = NULL;
4879 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004880
H. Peter Anvin130736c2016-02-17 20:27:41 -08004881 /*
4882 * If we're in a dead branch of IF or something like it, ignore the error.
4883 * However, because %else etc are evaluated in the state context
4884 * of the previous branch, errors might get lost:
4885 * %if 0 ... %else trailing garbage ... %endif
4886 * So %else etc should set the ERR_PP_PRECOND flag.
4887 */
4888 if ((severity & ERR_MASK) < ERR_FATAL &&
4889 istk && istk->conds &&
4890 ((severity & ERR_PP_PRECOND) ?
4891 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004892 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004893 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004894
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004895 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004896 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004897 mmac = istk->mstk;
4898 /* but %rep blocks should be skipped */
4899 while (mmac && !mmac->name)
4900 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004901 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004902
H. Peter Anvin130736c2016-02-17 20:27:41 -08004903 if (mmac) {
4904 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004905
H. Peter Anvin130736c2016-02-17 20:27:41 -08004906 nasm_set_verror(real_verror);
4907 nasm_error(severity, "(%s:%d) %s",
4908 mmac->name, mmac->lineno - delta, buff);
4909 nasm_set_verror(pp_verror);
4910 } else {
4911 real_verror(severity, fmt, arg);
4912 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004913}
4914
H. Peter Anvin734b1882002-04-30 21:01:08 +00004915static void
H. Peter Anvin130736c2016-02-17 20:27:41 -08004916pp_reset(char *file, int apass, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004917{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004918 Token *t;
4919
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004920 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004921 istk = nasm_malloc(sizeof(Include));
4922 istk->next = NULL;
4923 istk->conds = NULL;
4924 istk->expansion = NULL;
4925 istk->mstk = NULL;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07004926 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004927 istk->fname = NULL;
H. Peter Anvin274cda82016-05-10 02:56:29 -07004928 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004929 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004930 if (!istk->fp)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004931 nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004932 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004933 nested_mac_count = 0;
4934 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004935 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004936 unique = 0;
H. Peter Anvinf7606612016-07-13 14:23:48 -07004937
4938 if (tasm_compatible_mode)
4939 pp_add_stdmac(nasm_stdmac_tasm);
4940
4941 pp_add_stdmac(nasm_stdmac_nasm);
4942 pp_add_stdmac(nasm_stdmac_version);
4943
4944 stdmacpos = stdmacros[0];
4945 stdmacnext = &stdmacros[1];
4946
H. Peter Anvind2456592008-06-19 15:04:18 -07004947 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004948
4949 /*
4950 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4951 * The caller, however, will also pass in 3 for preprocess-only so
4952 * we can set __PASS__ accordingly.
4953 */
4954 pass = apass > 2 ? 2 : apass;
4955
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004956 dephead = deptail = deplist;
4957 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004958 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4959 sl->next = NULL;
4960 strcpy(sl->str, file);
4961 *deptail = sl;
4962 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004963 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004964
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004965 /*
4966 * Define the __PASS__ macro. This is defined here unlike
4967 * all the other builtins, because it is special -- it varies between
4968 * passes.
4969 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004970 t = nasm_malloc(sizeof(*t));
4971 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004972 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004973 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004974 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004975}
4976
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07004977static void pp_init(void)
4978{
4979 hash_init(&FileHash, HASH_MEDIUM);
4980}
4981
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004982static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004983{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004984 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004985 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004986
H. Peter Anvin130736c2016-02-17 20:27:41 -08004987 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08004988
H. Peter Anvine2c80182005-01-15 22:15:51 +00004989 while (1) {
4990 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004991 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004992 * buffer or from the input file.
4993 */
4994 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004995 while (istk->expansion && istk->expansion->finishes) {
4996 Line *l = istk->expansion;
4997 if (!l->finishes->name && l->finishes->in_progress > 1) {
4998 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004999
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005000 /*
5001 * This is a macro-end marker for a macro with no
5002 * name, which means it's not really a macro at all
5003 * but a %rep block, and the `in_progress' field is
5004 * more than 1, meaning that we still need to
5005 * repeat. (1 means the natural last repetition; 0
5006 * means termination by %exitrep.) We have
5007 * therefore expanded up to the %endrep, and must
5008 * push the whole block on to the expansion buffer
5009 * again. We don't bother to remove the macro-end
5010 * marker: we'd only have to generate another one
5011 * if we did.
5012 */
5013 l->finishes->in_progress--;
5014 list_for_each(l, l->finishes->expansion) {
5015 Token *t, *tt, **tail;
5016
5017 ll = nasm_malloc(sizeof(Line));
5018 ll->next = istk->expansion;
5019 ll->finishes = NULL;
5020 ll->first = NULL;
5021 tail = &ll->first;
5022
5023 list_for_each(t, l->first) {
5024 if (t->text || t->type == TOK_WHITESPACE) {
5025 tt = *tail = new_Token(NULL, t->type, t->text, 0);
5026 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005027 }
5028 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005029
5030 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005031 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005032 } else {
5033 /*
5034 * Check whether a `%rep' was started and not ended
5035 * within this macro expansion. This can happen and
5036 * should be detected. It's a fatal error because
5037 * I'm too confused to work out how to recover
5038 * sensibly from it.
5039 */
5040 if (defining) {
5041 if (defining->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005042 nasm_panic(0, "defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005043 else if (istk->mstk->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005044 nasm_fatal(0, "`%%rep' without `%%endrep' within"
5045 " expansion of macro `%s'",
5046 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005047 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005048
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005049 /*
5050 * FIXME: investigate the relationship at this point between
5051 * istk->mstk and l->finishes
5052 */
5053 {
5054 MMacro *m = istk->mstk;
5055 istk->mstk = m->next_active;
5056 if (m->name) {
5057 /*
5058 * This was a real macro call, not a %rep, and
5059 * therefore the parameter information needs to
5060 * be freed.
5061 */
5062 if (m->prev) {
5063 pop_mmacro(m);
5064 l->finishes->in_progress --;
5065 } else {
5066 nasm_free(m->params);
5067 free_tlist(m->iline);
5068 nasm_free(m->paramlen);
5069 l->finishes->in_progress = 0;
5070 }
5071 } else
5072 free_mmacro(m);
5073 }
5074 istk->expansion = l->next;
5075 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005076 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005077 }
5078 }
5079 while (1) { /* until we get a line we can use */
5080
5081 if (istk->expansion) { /* from a macro expansion */
5082 char *p;
5083 Line *l = istk->expansion;
5084 if (istk->mstk)
5085 istk->mstk->lineno++;
5086 tline = l->first;
5087 istk->expansion = l->next;
5088 nasm_free(l);
5089 p = detoken(tline, false);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005090 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005091 nasm_free(p);
5092 break;
5093 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005094 line = read_line();
5095 if (line) { /* from the current input file */
5096 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005097 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005098 nasm_free(line);
5099 break;
5100 }
5101 /*
5102 * The current file has ended; work down the istk
5103 */
5104 {
5105 Include *i = istk;
5106 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005107 if (i->conds) {
5108 /* nasm_error can't be conditionally suppressed */
H. Peter Anvin41087062016-03-03 14:27:34 -08005109 nasm_fatal(0,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005110 "expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005111 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005112 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005113 if (i->next)
5114 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005115 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005116 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005117 nasm_free(i);
H. Peter Anvin130736c2016-02-17 20:27:41 -08005118 if (!istk) {
5119 line = NULL;
5120 goto done;
5121 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005122 if (istk->expansion && istk->expansion->finishes)
5123 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005124 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005125 }
5126
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005127 /*
5128 * We must expand MMacro parameters and MMacro-local labels
5129 * _before_ we plunge into directive processing, to cope
5130 * with things like `%define something %1' such as STRUC
5131 * uses. Unless we're _defining_ a MMacro, in which case
5132 * those tokens should be left alone to go into the
5133 * definition; and unless we're in a non-emitting
5134 * condition, in which case we don't want to meddle with
5135 * anything.
5136 */
5137 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5138 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005139 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005140 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005141
H. Peter Anvine2c80182005-01-15 22:15:51 +00005142 /*
5143 * Check the line to see if it's a preprocessor directive.
5144 */
5145 if (do_directive(tline) == DIRECTIVE_FOUND) {
5146 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005147 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005148 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005149 * We're defining a multi-line macro. We emit nothing
5150 * at all, and just
5151 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005152 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005153 Line *l = nasm_malloc(sizeof(Line));
5154 l->next = defining->expansion;
5155 l->first = tline;
5156 l->finishes = NULL;
5157 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005158 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005159 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005160 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005161 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005162 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005163 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005164 * directive so we keep our place correctly.
5165 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005166 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005167 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005168 } else if (istk->mstk && !istk->mstk->in_progress) {
5169 /*
5170 * We're in a %rep block which has been terminated, so
5171 * we're walking through to the %endrep without
5172 * emitting anything. Emit nothing at all, not even a
5173 * blank line: when we emerge from the %rep block we'll
5174 * give a line-number directive so we keep our place
5175 * correctly.
5176 */
5177 free_tlist(tline);
5178 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005179 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005180 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005181 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005182 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005183 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005184 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005185 line = detoken(tline, true);
5186 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005187 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005188 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005189 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005190 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005191 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005192 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005193
H. Peter Anvin130736c2016-02-17 20:27:41 -08005194done:
5195 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005196 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005197}
5198
H. Peter Anvine2c80182005-01-15 22:15:51 +00005199static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005200{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005201 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005202
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005203 if (defining) {
5204 if (defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005205 nasm_error(ERR_NONFATAL,
5206 "end of file while still defining macro `%s'",
5207 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005208 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005209 nasm_error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005210 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005211
5212 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005213 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005214 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005215
5216 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005217
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005218 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005219 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005220 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005221 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005222 Include *i = istk;
5223 istk = istk->next;
5224 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005225 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005226 }
5227 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005228 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005229 src_set_fname(NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005230 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005231 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005232 free_llist(predef);
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005233 predef = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005234 delete_Blocks();
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005235 freeTokens = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005236 while ((i = ipath)) {
5237 ipath = i->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005238 if (i->path)
5239 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005240 nasm_free(i);
5241 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005242 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005243}
5244
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005245static void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005246{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005247 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005248
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005249 i = nasm_malloc(sizeof(IncPath));
5250 i->path = path ? nasm_strdup(path) : NULL;
5251 i->next = NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005252
H. Peter Anvin89cee572009-07-15 09:16:54 -04005253 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005254 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005255 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005256 j = j->next;
5257 j->next = i;
5258 } else {
5259 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005260 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005261}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005262
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005263static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005264{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005265 Token *inc, *space, *name;
5266 Line *l;
5267
H. Peter Anvin734b1882002-04-30 21:01:08 +00005268 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5269 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5270 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005271
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005272 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005273 l->next = predef;
5274 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005275 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005276 predef = l;
5277}
5278
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005279static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005280{
5281 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005282 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005283 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005284
H. Peter Anvin130736c2016-02-17 20:27:41 -08005285 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005286
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005287 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005288 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5289 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005290 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005291 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005292 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005293 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005294 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005295
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005296 if (space->next->type != TOK_PREPROC_ID &&
5297 space->next->type != TOK_ID)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005298 nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005299
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005300 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005301 l->next = predef;
5302 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005303 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005304 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005305
5306 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005307}
5308
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005309static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005310{
5311 Token *def, *space;
5312 Line *l;
5313
H. Peter Anvin734b1882002-04-30 21:01:08 +00005314 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5315 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005316 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005317
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005318 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005319 l->next = predef;
5320 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005321 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005322 predef = l;
5323}
5324
H. Peter Anvinf7606612016-07-13 14:23:48 -07005325static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005326{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005327 macros_t **mp;
5328
5329 /* Find the end of the list and avoid duplicates */
5330 for (mp = stdmacros; *mp; mp++) {
5331 if (*mp == macros)
5332 return; /* Nothing to do */
5333 }
5334
5335 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5336
5337 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005338}
5339
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005340static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005341{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005342 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005343 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005344 tok->text = nasm_strdup(numbuf);
5345 tok->type = TOK_NUMBER;
5346}
5347
H. Peter Anvin37368952016-05-09 14:10:32 -07005348static void pp_list_one_macro(MMacro *m, int severity)
5349{
5350 if (!m)
5351 return;
5352
5353 /* We need to print the next_active list in reverse order */
5354 pp_list_one_macro(m->next_active, severity);
5355
5356 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005357 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvin37368952016-05-09 14:10:32 -07005358 nasm_error(severity, "... from macro `%s' defined here", m->name);
5359 }
5360}
5361
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005362static void pp_error_list_macros(int severity)
5363{
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005364 int32_t saved_line;
5365 const char *saved_fname = NULL;
5366
5367 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY;
H. Peter Anvin274cda82016-05-10 02:56:29 -07005368 src_get(&saved_line, &saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005369
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005370 if (istk)
5371 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005372
H. Peter Anvin274cda82016-05-10 02:56:29 -07005373 src_set(saved_line, saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005374}
5375
H. Peter Anvine7469712016-02-18 02:20:59 -08005376const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005377 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005378 pp_reset,
5379 pp_getline,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005380 pp_cleanup,
H. Peter Anvinf7606612016-07-13 14:23:48 -07005381 pp_add_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005382 pp_pre_define,
5383 pp_pre_undefine,
5384 pp_pre_include,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005385 pp_include_path,
5386 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005387};