blob: ec38653f418ca15ebe3732317f3437572e40689b [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 Anvin9924d1e2016-10-04 00:59:39 -0700408static StrList **dephead;
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 Anvin6768eb72002-04-30 20:52:26 +00001497 * Open an include file. This routine must always return a valid
1498 * file pointer if it returns - it's responsible for throwing an
1499 * ERR_FATAL and bombing out completely if not. It should also try
1500 * the include path one by one until it finds the file or reaches
1501 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001502 *
1503 * Note: for INC_PROBE the function returns NULL at all times;
1504 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001505 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001506enum incopen_mode {
1507 INC_NEEDED, /* File must exist */
1508 INC_OPTIONAL, /* Missing is OK */
1509 INC_PROBE /* Only an existence probe */
1510};
1511
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001512/* This is conducts a full pathname search */
1513static FILE *inc_fopen_search(const char *file, StrList **slpath,
1514 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001515{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001516 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001517 char *prefix = "";
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001518 const IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001519 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001520 size_t prefix_len = 0;
1521 StrList *sl;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001522 size_t path_len;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001523 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001524
H. Peter Anvine2c80182005-01-15 22:15:51 +00001525 while (1) {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001526 path_len = prefix_len + len + 1;
1527
1528 sl = nasm_malloc(path_len + sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001529 memcpy(sl->str, prefix, prefix_len);
1530 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001531 sl->next = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001532
H. Peter Anvind81a2352016-09-21 14:03:18 -07001533 if (omode == INC_PROBE) {
1534 fp = NULL;
1535 found = nasm_file_exists(sl->str);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001536 } else {
H. Peter Anvind81a2352016-09-21 14:03:18 -07001537 fp = nasm_open_read(sl->str, fmode);
1538 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001539 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001540 if (found) {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001541 *slpath = sl;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001542 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001543 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001544
H. Peter Anvind81a2352016-09-21 14:03:18 -07001545 nasm_free(sl);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001546
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001547 if (!ip)
1548 return NULL;
1549
1550 prefix = ip->path;
1551 prefix_len = strlen(prefix);
1552 ip = ip->next;
1553 }
1554}
1555
1556/*
1557 * Open a file, or test for the presence of one (depending on omode),
1558 * considering the include path.
1559 */
1560static FILE *inc_fopen(const char *file,
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001561 StrList **dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001562 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001563 enum incopen_mode omode,
1564 enum file_flags fmode)
1565{
1566 StrList *sl;
1567 struct hash_insert hi;
1568 void **hp;
1569 char *path;
1570 FILE *fp = NULL;
1571
1572 hp = hash_find(&FileHash, file, &hi);
1573 if (hp) {
1574 path = *hp;
1575 } else {
1576 /* Need to do the actual path search */
1577 size_t file_len;
1578
1579 sl = NULL;
1580 fp = inc_fopen_search(file, &sl, omode, fmode);
1581
1582 file_len = strlen(file);
1583
1584 if (!sl) {
1585 /* Store negative result for this file */
1586 sl = nasm_malloc(file_len + 1 + sizeof sl->next);
1587 memcpy(sl->str, file, file_len+1);
1588 sl->next = NULL;
1589 file = sl->str;
1590 path = NULL;
1591 } else {
1592 path = sl->str;
1593 file = strchr(path, '\0') - file_len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001594 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001595
1596 hash_add(&hi, file, path); /* Positive or negative result */
1597
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001598 /*
1599 * Add file to dependency path. The in_list() is needed
1600 * in case the file was already added with %depend.
1601 */
1602 if (path || omode != INC_NEEDED)
H. Peter Anvin436e3672016-10-04 01:12:28 -07001603 nasm_add_to_strlist(dhead, sl);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001604 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001605
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001606 if (!path) {
1607 if (omode == INC_NEEDED)
1608 nasm_fatal(0, "unable to open include file `%s'", file);
1609
1610 if (found_path)
1611 *found_path = NULL;
1612
1613 return NULL;
1614 }
1615
1616 if (!fp && omode != INC_PROBE)
1617 fp = nasm_open_read(file, fmode);
1618
1619 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001620 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001621
1622 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001623}
1624
1625/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001626 * Opens an include or input file. Public version, for use by modules
1627 * that get a file:lineno pair and need to look at the file again
1628 * (e.g. the CodeView debug backend). Returns NULL on failure.
1629 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001630FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001631{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001632 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001633}
1634
1635/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001636 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001637 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001638 * return true if _any_ single-line macro of that name is defined.
1639 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001640 * `nparam' or no parameters is defined.
1641 *
1642 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001643 * defined, or nparam is -1, the address of the definition structure
1644 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001645 * is NULL, no action will be taken regarding its contents, and no
1646 * error will occur.
1647 *
1648 * Note that this is also called with nparam zero to resolve
1649 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001650 *
1651 * If you already know which context macro belongs to, you can pass
1652 * the context pointer as first parameter; if you won't but name begins
1653 * with %$ the context will be automatically computed. If all_contexts
1654 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001655 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001656static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001657smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001658 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001659{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001660 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001661 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001662
H. Peter Anvin97a23472007-09-16 17:57:25 -07001663 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001664 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001665 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001666 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001667 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001668 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001669 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001670 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001671 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001672 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001673 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001674 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001675
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676 while (m) {
1677 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001678 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001679 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001680 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001681 *defn = m;
1682 else
1683 *defn = NULL;
1684 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001685 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 }
1687 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001688 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001689
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001690 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001691}
1692
1693/*
1694 * Count and mark off the parameters in a multi-line macro call.
1695 * This is called both from within the multi-line macro expansion
1696 * code, and also to mark off the default parameters when provided
1697 * in a %macro definition line.
1698 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001699static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001700{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001701 int paramsize, brace;
1702
1703 *nparam = paramsize = 0;
1704 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001705 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001706 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001707 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001708 paramsize += PARAM_DELTA;
1709 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1710 }
1711 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001712 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001713 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001714 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001715 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001716 if (brace) {
1717 while (brace && (t = t->next) != NULL) {
1718 if (tok_is_(t, "{"))
1719 brace++;
1720 else if (tok_is_(t, "}"))
1721 brace--;
1722 }
1723
1724 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001725 /*
1726 * Now we've found the closing brace, look further
1727 * for the comma.
1728 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001729 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001730 skip_white_(t);
1731 if (tok_isnt_(t, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001732 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001733 "braces do not enclose all of macro parameter");
1734 while (tok_isnt_(t, ","))
1735 t = t->next;
1736 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001737 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001738 } else {
1739 while (tok_isnt_(t, ","))
1740 t = t->next;
1741 }
1742 if (t) { /* got a comma/brace */
1743 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001744 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001745 }
1746}
1747
1748/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001749 * Determine whether one of the various `if' conditions is true or
1750 * not.
1751 *
1752 * We must free the tline we get passed.
1753 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001754static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001755{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001756 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001757 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001758 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001759 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001760 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001761 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001762 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001763
1764 origline = tline;
1765
H. Peter Anvine2c80182005-01-15 22:15:51 +00001766 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001767 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001768 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001769 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001770 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001771 if (!tline)
1772 break;
1773 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001774 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001775 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001776 free_tlist(origline);
1777 return -1;
1778 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001779 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001780 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001781 tline = tline->next;
1782 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001783 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001784
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001785 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001786 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001787 while (tline) {
1788 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001789 if (!tline || (tline->type != TOK_ID &&
1790 (tline->type != TOK_PREPROC_ID ||
1791 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001792 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001793 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001794 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001795 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001796 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001797 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001798 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001799 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001800 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001801
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001802 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001803 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001804 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001805 while (tline) {
1806 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001807 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001808 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001809 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001810 tline->text[1] != '!'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001811 nasm_error(ERR_NONFATAL,
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001812 "`%s' expects environment variable names",
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001813 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001814 goto fail;
1815 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001816 p = tline->text;
1817 if (tline->type == TOK_PREPROC_ID)
1818 p += 2; /* Skip leading %! */
1819 if (*p == '\'' || *p == '\"' || *p == '`')
1820 nasm_unquote_cstr(p, ct);
1821 if (getenv(p))
1822 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001823 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001824 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001825 break;
1826
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001827 case PPC_IFIDN:
1828 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001829 tline = expand_smacro(tline);
1830 t = tt = tline;
1831 while (tok_isnt_(tt, ","))
1832 tt = tt->next;
1833 if (!tt) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001834 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001835 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001836 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001837 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001838 }
1839 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001840 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001841 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1842 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001843 nasm_error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001844 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001845 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001846 }
1847 if (t->type == TOK_WHITESPACE) {
1848 t = t->next;
1849 continue;
1850 }
1851 if (tt->type == TOK_WHITESPACE) {
1852 tt = tt->next;
1853 continue;
1854 }
1855 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001856 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001857 break;
1858 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001859 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001860 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001861 size_t l1 = nasm_unquote(t->text, NULL);
1862 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001863
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001864 if (l1 != l2) {
1865 j = false;
1866 break;
1867 }
1868 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1869 j = false;
1870 break;
1871 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001872 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
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 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001876
H. Peter Anvine2c80182005-01-15 22:15:51 +00001877 t = t->next;
1878 tt = tt->next;
1879 }
1880 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001881 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001882 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001883
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001884 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001885 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001886 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001887 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001888
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001889 skip_white_(tline);
1890 tline = expand_id(tline);
1891 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001892 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001893 "`%s' expects a macro name", pp_directives[ct]);
1894 goto fail;
1895 }
1896 searching.name = nasm_strdup(tline->text);
1897 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001898 searching.plus = false;
1899 searching.nolist = false;
1900 searching.in_progress = 0;
1901 searching.max_depth = 0;
1902 searching.rep_nest = NULL;
1903 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001904 searching.nparam_max = INT_MAX;
1905 tline = expand_smacro(tline->next);
1906 skip_white_(tline);
1907 if (!tline) {
1908 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001909 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001910 "`%s' expects a parameter count or nothing",
1911 pp_directives[ct]);
1912 } else {
1913 searching.nparam_min = searching.nparam_max =
1914 readnum(tline->text, &j);
1915 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001916 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001917 "unable to parse parameter count `%s'",
1918 tline->text);
1919 }
1920 if (tline && tok_is_(tline->next, "-")) {
1921 tline = tline->next->next;
1922 if (tok_is_(tline, "*"))
1923 searching.nparam_max = INT_MAX;
1924 else if (!tok_type_(tline, TOK_NUMBER))
H. Peter Anvin130736c2016-02-17 20:27:41 -08001925 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001926 "`%s' expects a parameter count after `-'",
1927 pp_directives[ct]);
1928 else {
1929 searching.nparam_max = readnum(tline->text, &j);
1930 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001931 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001932 "unable to parse parameter count `%s'",
1933 tline->text);
1934 if (searching.nparam_min > searching.nparam_max)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001935 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001936 "minimum parameter count exceeds maximum");
1937 }
1938 }
1939 if (tline && tok_is_(tline->next, "+")) {
1940 tline = tline->next;
1941 searching.plus = true;
1942 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001943 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1944 while (mmac) {
1945 if (!strcmp(mmac->name, searching.name) &&
1946 (mmac->nparam_min <= searching.nparam_max
1947 || searching.plus)
1948 && (searching.nparam_min <= mmac->nparam_max
1949 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001950 found = true;
1951 break;
1952 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001953 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001954 }
1955 if (tline && tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001956 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001957 "trailing garbage after %%ifmacro ignored");
1958 nasm_free(searching.name);
1959 j = found;
1960 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001961 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001962
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001963 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001964 needtype = TOK_ID;
1965 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001966 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001967 needtype = TOK_NUMBER;
1968 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001969 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001970 needtype = TOK_STRING;
1971 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001972
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001973iftype:
1974 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001975
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001976 while (tok_type_(t, TOK_WHITESPACE) ||
1977 (needtype == TOK_NUMBER &&
1978 tok_type_(t, TOK_OTHER) &&
1979 (t->text[0] == '-' || t->text[0] == '+') &&
1980 !t->text[1]))
1981 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001982
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001983 j = tok_type_(t, needtype);
1984 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001985
1986 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001987 t = tline = expand_smacro(tline);
1988 while (tok_type_(t, TOK_WHITESPACE))
1989 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001990
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001991 j = false;
1992 if (t) {
1993 t = t->next; /* Skip the actual token */
1994 while (tok_type_(t, TOK_WHITESPACE))
1995 t = t->next;
1996 j = !t; /* Should be nothing left */
1997 }
1998 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001999
H. Peter Anvin134b9462008-02-16 17:01:40 -08002000 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002001 t = tline = expand_smacro(tline);
2002 while (tok_type_(t, TOK_WHITESPACE))
2003 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002004
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002005 j = !t; /* Should be empty */
2006 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002007
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002008 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002009 t = tline = expand_smacro(tline);
2010 tptr = &t;
2011 tokval.t_type = TOKEN_INVALID;
2012 evalresult = evaluate(ppscan, tptr, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -08002013 NULL, pass | CRITICAL, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002014 if (!evalresult)
2015 return -1;
2016 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002017 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002018 "trailing garbage after expression ignored");
2019 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002020 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002021 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002022 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002023 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002024 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002025 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002026
H. Peter Anvine2c80182005-01-15 22:15:51 +00002027 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002028 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002029 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002030 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002031 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002032 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002033
2034 free_tlist(origline);
2035 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002036
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002037fail:
2038 free_tlist(origline);
2039 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002040}
2041
2042/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002043 * Common code for defining an smacro
2044 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002045static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002046 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002047{
2048 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002049 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002050
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002051 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002052 if (!smac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002053 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002054 "single-line macro `%s' defined both with and"
2055 " without parameters", mname);
2056 /*
2057 * Some instances of the old code considered this a failure,
2058 * some others didn't. What is the right thing to do here?
2059 */
2060 free_tlist(expansion);
2061 return false; /* Failure */
2062 } else {
2063 /*
2064 * We're redefining, so we have to take over an
2065 * existing SMacro structure. This means freeing
2066 * what was already in it.
2067 */
2068 nasm_free(smac->name);
2069 free_tlist(smac->expansion);
2070 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002071 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002072 smtbl = ctx ? &ctx->localmac : &smacros;
2073 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002074 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002075 smac->next = *smhead;
2076 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002077 }
2078 smac->name = nasm_strdup(mname);
2079 smac->casesense = casesense;
2080 smac->nparam = nparam;
2081 smac->expansion = expansion;
2082 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002083 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002084}
2085
2086/*
2087 * Undefine an smacro
2088 */
2089static void undef_smacro(Context *ctx, const char *mname)
2090{
2091 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002092 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002093
H. Peter Anvin166c2472008-05-28 12:28:58 -07002094 smtbl = ctx ? &ctx->localmac : &smacros;
2095 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002096
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002097 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002098 /*
2099 * We now have a macro name... go hunt for it.
2100 */
2101 sp = smhead;
2102 while ((s = *sp) != NULL) {
2103 if (!mstrcmp(s->name, mname, s->casesense)) {
2104 *sp = s->next;
2105 nasm_free(s->name);
2106 free_tlist(s->expansion);
2107 nasm_free(s);
2108 } else {
2109 sp = &s->next;
2110 }
2111 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002112 }
2113}
2114
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002115/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002116 * Parse a mmacro specification.
2117 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002118static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002119{
2120 bool err;
2121
2122 tline = tline->next;
2123 skip_white_(tline);
2124 tline = expand_id(tline);
2125 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002126 nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002127 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002128 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002129
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002130 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002131 def->name = nasm_strdup(tline->text);
2132 def->plus = false;
2133 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002134 def->in_progress = 0;
2135 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002136 def->nparam_min = 0;
2137 def->nparam_max = 0;
2138
H. Peter Anvina26433d2008-07-16 14:40:01 -07002139 tline = expand_smacro(tline->next);
2140 skip_white_(tline);
2141 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002142 nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002143 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002144 def->nparam_min = def->nparam_max =
2145 readnum(tline->text, &err);
2146 if (err)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002147 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002148 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002149 }
2150 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002151 tline = tline->next->next;
2152 if (tok_is_(tline, "*")) {
2153 def->nparam_max = INT_MAX;
2154 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002155 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002156 "`%s' expects a parameter count after `-'", directive);
2157 } else {
2158 def->nparam_max = readnum(tline->text, &err);
2159 if (err) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002160 nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002161 tline->text);
2162 }
2163 if (def->nparam_min > def->nparam_max) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002164 nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002165 }
2166 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002167 }
2168 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002169 tline = tline->next;
2170 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002171 }
2172 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002173 !nasm_stricmp(tline->next->text, ".nolist")) {
2174 tline = tline->next;
2175 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002176 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002177
H. Peter Anvina26433d2008-07-16 14:40:01 -07002178 /*
2179 * Handle default parameters.
2180 */
2181 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002182 def->dlist = tline->next;
2183 tline->next = NULL;
2184 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002185 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002186 def->dlist = NULL;
2187 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002188 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002189 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002190
H. Peter Anvin89cee572009-07-15 09:16:54 -04002191 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002192 !def->plus)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002193 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002194 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002195
H. Peter Anvina26433d2008-07-16 14:40:01 -07002196 return true;
2197}
2198
2199
2200/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002201 * Decode a size directive
2202 */
2203static int parse_size(const char *str) {
2204 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002205 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002206 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002207 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002208
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002209 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002210}
2211
Ed Beroset3ab3f412002-06-11 03:31:49 +00002212/**
2213 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002214 * Find out if a line contains a preprocessor directive, and deal
2215 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002216 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002217 * If a directive _is_ found, it is the responsibility of this routine
2218 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002219 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002220 * @param tline a pointer to the current tokeninzed line linked list
2221 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002222 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002223 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002224static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002225{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002226 enum preproc_token i;
2227 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002228 bool err;
2229 int nparam;
2230 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002231 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002232 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002233 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002234 char *p, *pp;
2235 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002236 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002237 Include *inc;
2238 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002239 Cond *cond;
2240 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002241 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002242 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002243 struct tokenval tokval;
2244 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002245 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002246 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002247 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002248 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002249
2250 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002251
H. Peter Anvineba20a72002-04-30 20:53:55 +00002252 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002253 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002254 (tline->text[1] == '%' || tline->text[1] == '$'
2255 || tline->text[1] == '!'))
2256 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002257
H. Peter Anvin4169a472007-09-12 01:29:43 +00002258 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002259
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002260 /*
2261 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2262 * since they are known to be buggy at moment, we need to fix them
2263 * in future release (2.09-2.10)
2264 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002265 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002266 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002267 tline->text);
2268 return NO_DIRECTIVE_FOUND;
2269 }
2270
2271 /*
2272 * If we're in a non-emitting branch of a condition construct,
2273 * or walking to the end of an already terminated %rep block,
2274 * we should ignore all directives except for condition
2275 * directives.
2276 */
2277 if (((istk->conds && !emitting(istk->conds->state)) ||
2278 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2279 return NO_DIRECTIVE_FOUND;
2280 }
2281
2282 /*
2283 * If we're defining a macro or reading a %rep block, we should
2284 * ignore all directives except for %macro/%imacro (which nest),
2285 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2286 * If we're in a %rep block, another %rep nests, so should be let through.
2287 */
2288 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2289 i != PP_RMACRO && i != PP_IRMACRO &&
2290 i != PP_ENDMACRO && i != PP_ENDM &&
2291 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2292 return NO_DIRECTIVE_FOUND;
2293 }
2294
2295 if (defining) {
2296 if (i == PP_MACRO || i == PP_IMACRO ||
2297 i == PP_RMACRO || i == PP_IRMACRO) {
2298 nested_mac_count++;
2299 return NO_DIRECTIVE_FOUND;
2300 } else if (nested_mac_count > 0) {
2301 if (i == PP_ENDMACRO) {
2302 nested_mac_count--;
2303 return NO_DIRECTIVE_FOUND;
2304 }
2305 }
2306 if (!defining->name) {
2307 if (i == PP_REP) {
2308 nested_rep_count++;
2309 return NO_DIRECTIVE_FOUND;
2310 } else if (nested_rep_count > 0) {
2311 if (i == PP_ENDREP) {
2312 nested_rep_count--;
2313 return NO_DIRECTIVE_FOUND;
2314 }
2315 }
2316 }
2317 }
2318
H. Peter Anvin4169a472007-09-12 01:29:43 +00002319 switch (i) {
2320 case PP_INVALID:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002321 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002322 tline->text);
2323 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002324
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002325 case PP_PRAGMA:
2326 /*
2327 * Currently %pragma doesn't do anything; it is here for
2328 * forward compatibility with future versions of NASM.
2329 */
2330 free_tlist(origline);
2331 return DIRECTIVE_FOUND;
2332
H. Peter Anvine2c80182005-01-15 22:15:51 +00002333 case PP_STACKSIZE:
2334 /* Directive to tell NASM what the default stack size is. The
2335 * default is for a 16-bit stack, and this can be overriden with
2336 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002337 */
2338 tline = tline->next;
2339 if (tline && tline->type == TOK_WHITESPACE)
2340 tline = tline->next;
2341 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002342 nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002343 free_tlist(origline);
2344 return DIRECTIVE_FOUND;
2345 }
2346 if (nasm_stricmp(tline->text, "flat") == 0) {
2347 /* All subsequent ARG directives are for a 32-bit stack */
2348 StackSize = 4;
2349 StackPointer = "ebp";
2350 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002351 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002352 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2353 /* All subsequent ARG directives are for a 64-bit stack */
2354 StackSize = 8;
2355 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002356 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002357 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002358 } else if (nasm_stricmp(tline->text, "large") == 0) {
2359 /* All subsequent ARG directives are for a 16-bit stack,
2360 * far function call.
2361 */
2362 StackSize = 2;
2363 StackPointer = "bp";
2364 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002365 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002366 } else if (nasm_stricmp(tline->text, "small") == 0) {
2367 /* All subsequent ARG directives are for a 16-bit stack,
2368 * far function call. We don't support near functions.
2369 */
2370 StackSize = 2;
2371 StackPointer = "bp";
2372 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002373 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002374 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002375 nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002376 free_tlist(origline);
2377 return DIRECTIVE_FOUND;
2378 }
2379 free_tlist(origline);
2380 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002381
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 case PP_ARG:
2383 /* TASM like ARG directive to define arguments to functions, in
2384 * the following form:
2385 *
2386 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2387 */
2388 offset = ArgOffset;
2389 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002390 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002391 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002392
H. Peter Anvine2c80182005-01-15 22:15:51 +00002393 /* Find the argument name */
2394 tline = tline->next;
2395 if (tline && tline->type == TOK_WHITESPACE)
2396 tline = tline->next;
2397 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002398 nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002399 free_tlist(origline);
2400 return DIRECTIVE_FOUND;
2401 }
2402 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002403
H. Peter Anvine2c80182005-01-15 22:15:51 +00002404 /* Find the argument size type */
2405 tline = tline->next;
2406 if (!tline || tline->type != TOK_OTHER
2407 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002408 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002409 "Syntax error processing `%%arg' directive");
2410 free_tlist(origline);
2411 return DIRECTIVE_FOUND;
2412 }
2413 tline = tline->next;
2414 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002415 nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002416 free_tlist(origline);
2417 return DIRECTIVE_FOUND;
2418 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002419
H. Peter Anvine2c80182005-01-15 22:15:51 +00002420 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002421 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002422 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002423 size = parse_size(tt->text);
2424 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002425 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002426 "Invalid size type for `%%arg' missing directive");
2427 free_tlist(tt);
2428 free_tlist(origline);
2429 return DIRECTIVE_FOUND;
2430 }
2431 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002432
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002433 /* Round up to even stack slots */
2434 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002435
H. Peter Anvine2c80182005-01-15 22:15:51 +00002436 /* Now define the macro for the argument */
2437 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2438 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002439 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002440 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002441
H. Peter Anvine2c80182005-01-15 22:15:51 +00002442 /* Move to the next argument in the list */
2443 tline = tline->next;
2444 if (tline && tline->type == TOK_WHITESPACE)
2445 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002446 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002447 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002448 free_tlist(origline);
2449 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002450
H. Peter Anvine2c80182005-01-15 22:15:51 +00002451 case PP_LOCAL:
2452 /* TASM like LOCAL directive to define local variables for a
2453 * function, in the following form:
2454 *
2455 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2456 *
2457 * The '= LocalSize' at the end is ignored by NASM, but is
2458 * required by TASM to define the local parameter size (and used
2459 * by the TASM macro package).
2460 */
2461 offset = LocalOffset;
2462 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002463 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002464 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002465
H. Peter Anvine2c80182005-01-15 22:15:51 +00002466 /* Find the argument name */
2467 tline = tline->next;
2468 if (tline && tline->type == TOK_WHITESPACE)
2469 tline = tline->next;
2470 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002471 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002472 "`%%local' missing argument parameter");
2473 free_tlist(origline);
2474 return DIRECTIVE_FOUND;
2475 }
2476 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002477
H. Peter Anvine2c80182005-01-15 22:15:51 +00002478 /* Find the argument size type */
2479 tline = tline->next;
2480 if (!tline || tline->type != TOK_OTHER
2481 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002482 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002483 "Syntax error processing `%%local' directive");
2484 free_tlist(origline);
2485 return DIRECTIVE_FOUND;
2486 }
2487 tline = tline->next;
2488 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002489 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002490 "`%%local' missing size type parameter");
2491 free_tlist(origline);
2492 return DIRECTIVE_FOUND;
2493 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002494
H. Peter Anvine2c80182005-01-15 22:15:51 +00002495 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002496 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002497 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002498 size = parse_size(tt->text);
2499 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002500 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002501 "Invalid size type for `%%local' missing directive");
2502 free_tlist(tt);
2503 free_tlist(origline);
2504 return DIRECTIVE_FOUND;
2505 }
2506 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002507
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002508 /* Round up to even stack slots */
2509 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002510
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002511 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002512
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002513 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002514 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2515 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002516 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002517
H. Peter Anvine2c80182005-01-15 22:15:51 +00002518 /* Now define the assign to setup the enter_c macro correctly */
2519 snprintf(directive, sizeof(directive),
2520 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002521 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002522
H. Peter Anvine2c80182005-01-15 22:15:51 +00002523 /* Move to the next argument in the list */
2524 tline = tline->next;
2525 if (tline && tline->type == TOK_WHITESPACE)
2526 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002527 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002528 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002529 free_tlist(origline);
2530 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002531
H. Peter Anvine2c80182005-01-15 22:15:51 +00002532 case PP_CLEAR:
2533 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002534 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002535 "trailing garbage after `%%clear' ignored");
2536 free_macros();
2537 init_macros();
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 Anvin418ca702008-05-30 10:42:30 -07002541 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002542 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002543 skip_white_(t);
2544 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002545 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002546 nasm_error(ERR_NONFATAL, "`%%depend' expects a file name");
H. Peter Anvin418ca702008-05-30 10:42:30 -07002547 free_tlist(origline);
2548 return DIRECTIVE_FOUND; /* but we did _something_ */
2549 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002550 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002551 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002552 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002553 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002554 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002555 nasm_unquote_cstr(p, i);
H. Peter Anvin436e3672016-10-04 01:12:28 -07002556 nasm_add_string_to_strlist(dephead, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002557 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002558 return DIRECTIVE_FOUND;
2559
2560 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002561 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002562 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002563
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002564 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002565 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002566 nasm_error(ERR_NONFATAL, "`%%include' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002567 free_tlist(origline);
2568 return DIRECTIVE_FOUND; /* but we did _something_ */
2569 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002570 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002571 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002572 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002573 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002574 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002575 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002576 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002577 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002578 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002579 found_path = NULL;
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002580 inc->fp = inc_fopen(p, dephead, &found_path,
H. Peter Anvind81a2352016-09-21 14:03:18 -07002581 pass == 0 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002582 if (!inc->fp) {
2583 /* -MG given but file not found */
2584 nasm_free(inc);
2585 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002586 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002587 inc->lineno = src_set_linnum(0);
2588 inc->lineinc = 1;
2589 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002590 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002591 istk = inc;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002592 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002593 }
2594 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002595 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002596
H. Peter Anvind2456592008-06-19 15:04:18 -07002597 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002598 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002599 static macros_t *use_pkg;
2600 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002601
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002602 tline = tline->next;
2603 skip_white_(tline);
2604 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002605
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002606 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002607 tline->type != TOK_INTERNAL_STRING &&
2608 tline->type != TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002609 nasm_error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002610 free_tlist(origline);
2611 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002612 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002613 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002614 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002615 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002616 if (tline->type == TOK_STRING)
2617 nasm_unquote_cstr(tline->text, i);
2618 use_pkg = nasm_stdmac_find_package(tline->text);
2619 if (!use_pkg)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002620 nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002621 else
2622 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002623 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002624 /* Not already included, go ahead and include it */
2625 stdmacpos = use_pkg;
2626 }
2627 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002628 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002629 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002630 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002631 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002632 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002633 tline = tline->next;
2634 skip_white_(tline);
2635 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002636 if (tline) {
2637 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002638 nasm_error(ERR_NONFATAL, "`%s' expects a context identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002639 pp_directives[i]);
2640 free_tlist(origline);
2641 return DIRECTIVE_FOUND; /* but we did _something_ */
2642 }
2643 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002644 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002645 "trailing garbage after `%s' ignored",
2646 pp_directives[i]);
2647 p = nasm_strdup(tline->text);
2648 } else {
2649 p = NULL; /* Anonymous */
2650 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002651
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002652 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002653 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002654 ctx->next = cstk;
2655 hash_init(&ctx->localmac, HASH_SMALL);
2656 ctx->name = p;
2657 ctx->number = unique++;
2658 cstk = ctx;
2659 } else {
2660 /* %pop or %repl */
2661 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002662 nasm_error(ERR_NONFATAL, "`%s': context stack is empty",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002663 pp_directives[i]);
2664 } else if (i == PP_POP) {
2665 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08002666 nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002667 "expected %s",
2668 cstk->name ? cstk->name : "anonymous", p);
2669 else
2670 ctx_pop();
2671 } else {
2672 /* i == PP_REPL */
2673 nasm_free(cstk->name);
2674 cstk->name = p;
2675 p = NULL;
2676 }
2677 nasm_free(p);
2678 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002679 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002680 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002681 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002682 severity = ERR_FATAL;
2683 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002684 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002685 severity = ERR_NONFATAL;
2686 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002687 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002688 severity = ERR_WARNING|ERR_WARN_USER;
2689 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002690
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002691issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002692 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002693 /* Only error out if this is the final pass */
2694 if (pass != 2 && i != PP_FATAL)
2695 return DIRECTIVE_FOUND;
2696
2697 tline->next = expand_smacro(tline->next);
2698 tline = tline->next;
2699 skip_white_(tline);
2700 t = tline ? tline->next : NULL;
2701 skip_white_(t);
2702 if (tok_type_(tline, TOK_STRING) && !t) {
2703 /* The line contains only a quoted string */
2704 p = tline->text;
2705 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002706 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002707 } else {
2708 /* Not a quoted string, or more than a quoted string */
2709 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002710 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002711 nasm_free(p);
2712 }
2713 free_tlist(origline);
2714 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002715 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002716
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002717 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002718 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002719 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002720 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002721 j = if_condition(tline->next, i);
2722 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002723 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002724 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002725 cond = nasm_malloc(sizeof(Cond));
2726 cond->next = istk->conds;
2727 cond->state = j;
2728 istk->conds = cond;
2729 if(istk->mstk)
2730 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002731 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002732 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002733
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002734 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002735 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002736 nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002737 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002738 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002739 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002740 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002741
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002742 case COND_DONE:
2743 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002744 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002745
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002746 case COND_ELSE_TRUE:
2747 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002748 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2749 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002750 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002751 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002752
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002753 case COND_IF_FALSE:
2754 /*
2755 * IMPORTANT: In the case of %if, we will already have
2756 * called expand_mmac_params(); however, if we're
2757 * processing an %elif we must have been in a
2758 * non-emitting mode, which would have inhibited
2759 * the normal invocation of expand_mmac_params().
2760 * Therefore, we have to do it explicitly here.
2761 */
2762 j = if_condition(expand_mmac_params(tline->next), i);
2763 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002764 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002765 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2766 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002767 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002768 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002769 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002770
H. Peter Anvine2c80182005-01-15 22:15:51 +00002771 case PP_ELSE:
2772 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002773 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2774 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002775 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002776 nasm_fatal(0, "`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002777 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002778 case COND_IF_TRUE:
2779 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002780 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002781 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002782
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002783 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002784 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002785
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002786 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002787 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002788 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002789
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002790 case COND_ELSE_TRUE:
2791 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002792 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002793 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002794 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002795 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002796 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002797 free_tlist(origline);
2798 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002799
H. Peter Anvine2c80182005-01-15 22:15:51 +00002800 case PP_ENDIF:
2801 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002802 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2803 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002804 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002805 nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002806 cond = istk->conds;
2807 istk->conds = cond->next;
2808 nasm_free(cond);
2809 if(istk->mstk)
2810 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002811 free_tlist(origline);
2812 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002813
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002814 case PP_RMACRO:
2815 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002816 case PP_MACRO:
2817 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002818 if (defining) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002819 nasm_error(ERR_FATAL, "`%s': already defining a macro",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002820 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002821 return DIRECTIVE_FOUND;
2822 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002823 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002824 defining->max_depth =
2825 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2826 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2827 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2828 nasm_free(defining);
2829 defining = NULL;
2830 return DIRECTIVE_FOUND;
2831 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002832
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002833 src_get(&defining->xline, &defining->fname);
2834
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002835 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2836 while (mmac) {
2837 if (!strcmp(mmac->name, defining->name) &&
2838 (mmac->nparam_min <= defining->nparam_max
2839 || defining->plus)
2840 && (defining->nparam_min <= mmac->nparam_max
2841 || mmac->plus)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002842 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002843 "redefining multi-line macro `%s'", defining->name);
2844 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002845 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002846 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002847 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002848 free_tlist(origline);
2849 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002850
H. Peter Anvine2c80182005-01-15 22:15:51 +00002851 case PP_ENDM:
2852 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002853 if (! (defining && defining->name)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002854 nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002855 return DIRECTIVE_FOUND;
2856 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002857 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2858 defining->next = *mmhead;
2859 *mmhead = defining;
2860 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002861 free_tlist(origline);
2862 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002863
H. Peter Anvin89cee572009-07-15 09:16:54 -04002864 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002865 /*
2866 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002867 * macro-end marker for a macro with a name. Then we
2868 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002869 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002870 list_for_each(l, istk->expansion)
2871 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002872 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002873
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002874 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002875 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002876 * Remove all conditional entries relative to this
2877 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002878 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002879 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2880 cond = istk->conds;
2881 istk->conds = cond->next;
2882 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002883 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002884 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002885 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002886 nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002887 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002888 free_tlist(origline);
2889 return DIRECTIVE_FOUND;
2890
H. Peter Anvina26433d2008-07-16 14:40:01 -07002891 case PP_UNMACRO:
2892 case PP_UNIMACRO:
2893 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002894 MMacro **mmac_p;
2895 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002896
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002897 spec.casesense = (i == PP_UNMACRO);
2898 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2899 return DIRECTIVE_FOUND;
2900 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002901 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2902 while (mmac_p && *mmac_p) {
2903 mmac = *mmac_p;
2904 if (mmac->casesense == spec.casesense &&
2905 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2906 mmac->nparam_min == spec.nparam_min &&
2907 mmac->nparam_max == spec.nparam_max &&
2908 mmac->plus == spec.plus) {
2909 *mmac_p = mmac->next;
2910 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002911 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002912 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002913 }
2914 }
2915 free_tlist(origline);
2916 free_tlist(spec.dlist);
2917 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002918 }
2919
H. Peter Anvine2c80182005-01-15 22:15:51 +00002920 case PP_ROTATE:
2921 if (tline->next && tline->next->type == TOK_WHITESPACE)
2922 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002923 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002924 free_tlist(origline);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002925 nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002926 return DIRECTIVE_FOUND;
2927 }
2928 t = expand_smacro(tline->next);
2929 tline->next = NULL;
2930 free_tlist(origline);
2931 tline = t;
2932 tptr = &t;
2933 tokval.t_type = TOKEN_INVALID;
2934 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002935 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002936 free_tlist(tline);
2937 if (!evalresult)
2938 return DIRECTIVE_FOUND;
2939 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002940 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002941 "trailing garbage after expression ignored");
2942 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002943 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002944 return DIRECTIVE_FOUND;
2945 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002946 mmac = istk->mstk;
2947 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2948 mmac = mmac->next_active;
2949 if (!mmac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002950 nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002951 } else if (mmac->nparam == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002952 nasm_error(ERR_NONFATAL,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002953 "`%%rotate' invoked within macro without parameters");
2954 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002955 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002956
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002957 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002958 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002959 rotate += mmac->nparam;
2960
2961 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002962 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002963 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002964
H. Peter Anvine2c80182005-01-15 22:15:51 +00002965 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002966 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002967 do {
2968 tline = tline->next;
2969 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002970
H. Peter Anvine2c80182005-01-15 22:15:51 +00002971 if (tok_type_(tline, TOK_ID) &&
2972 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002973 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002974 do {
2975 tline = tline->next;
2976 } while (tok_type_(tline, TOK_WHITESPACE));
2977 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002978
H. Peter Anvine2c80182005-01-15 22:15:51 +00002979 if (tline) {
2980 t = expand_smacro(tline);
2981 tptr = &t;
2982 tokval.t_type = TOKEN_INVALID;
2983 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002984 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002985 if (!evalresult) {
2986 free_tlist(origline);
2987 return DIRECTIVE_FOUND;
2988 }
2989 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002990 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002991 "trailing garbage after expression ignored");
2992 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002993 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002994 return DIRECTIVE_FOUND;
2995 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002996 count = reloc_value(evalresult);
2997 if (count >= REP_LIMIT) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002998 nasm_error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002999 count = 0;
3000 } else
3001 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003002 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003003 nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003004 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003005 }
3006 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003007
3008 tmp_defining = defining;
3009 defining = nasm_malloc(sizeof(MMacro));
3010 defining->prev = NULL;
3011 defining->name = NULL; /* flags this macro as a %rep block */
3012 defining->casesense = false;
3013 defining->plus = false;
3014 defining->nolist = nolist;
3015 defining->in_progress = count;
3016 defining->max_depth = 0;
3017 defining->nparam_min = defining->nparam_max = 0;
3018 defining->defaults = NULL;
3019 defining->dlist = NULL;
3020 defining->expansion = NULL;
3021 defining->next_active = istk->mstk;
3022 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003023 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003024
H. Peter Anvine2c80182005-01-15 22:15:51 +00003025 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003026 if (!defining || defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003027 nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003028 return DIRECTIVE_FOUND;
3029 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003030
H. Peter Anvine2c80182005-01-15 22:15:51 +00003031 /*
3032 * Now we have a "macro" defined - although it has no name
3033 * and we won't be entering it in the hash tables - we must
3034 * push a macro-end marker for it on to istk->expansion.
3035 * After that, it will take care of propagating itself (a
3036 * macro-end marker line for a macro which is really a %rep
3037 * block will cause the macro to be re-expanded, complete
3038 * with another macro-end marker to ensure the process
3039 * continues) until the whole expansion is forcibly removed
3040 * from istk->expansion by a %exitrep.
3041 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003042 l = nasm_malloc(sizeof(Line));
3043 l->next = istk->expansion;
3044 l->finishes = defining;
3045 l->first = NULL;
3046 istk->expansion = l;
3047
3048 istk->mstk = defining;
3049
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08003050 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003051 tmp_defining = defining;
3052 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003053 free_tlist(origline);
3054 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003055
H. Peter Anvine2c80182005-01-15 22:15:51 +00003056 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003057 /*
3058 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003059 * macro-end marker for a macro with no name. Then we set
3060 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003061 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003062 list_for_each(l, istk->expansion)
3063 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003064 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003065
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003066 if (l)
3067 l->finishes->in_progress = 1;
3068 else
H. Peter Anvin130736c2016-02-17 20:27:41 -08003069 nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003070 free_tlist(origline);
3071 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003072
H. Peter Anvine2c80182005-01-15 22:15:51 +00003073 case PP_XDEFINE:
3074 case PP_IXDEFINE:
3075 case PP_DEFINE:
3076 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003077 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003078
H. Peter Anvine2c80182005-01-15 22:15:51 +00003079 tline = tline->next;
3080 skip_white_(tline);
3081 tline = expand_id(tline);
3082 if (!tline || (tline->type != TOK_ID &&
3083 (tline->type != TOK_PREPROC_ID ||
3084 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003085 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003086 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003087 free_tlist(origline);
3088 return DIRECTIVE_FOUND;
3089 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003090
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003091 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003092 last = tline;
3093 param_start = tline = tline->next;
3094 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003095
H. Peter Anvine2c80182005-01-15 22:15:51 +00003096 /* Expand the macro definition now for %xdefine and %ixdefine */
3097 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3098 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003099
H. Peter Anvine2c80182005-01-15 22:15:51 +00003100 if (tok_is_(tline, "(")) {
3101 /*
3102 * This macro has parameters.
3103 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003104
H. Peter Anvine2c80182005-01-15 22:15:51 +00003105 tline = tline->next;
3106 while (1) {
3107 skip_white_(tline);
3108 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003109 nasm_error(ERR_NONFATAL, "parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003110 free_tlist(origline);
3111 return DIRECTIVE_FOUND;
3112 }
3113 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003114 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003115 "`%s': parameter identifier expected",
3116 tline->text);
3117 free_tlist(origline);
3118 return DIRECTIVE_FOUND;
3119 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003120 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003121 tline = tline->next;
3122 skip_white_(tline);
3123 if (tok_is_(tline, ",")) {
3124 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003125 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003126 if (!tok_is_(tline, ")")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003127 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003128 "`)' expected to terminate macro template");
3129 free_tlist(origline);
3130 return DIRECTIVE_FOUND;
3131 }
3132 break;
3133 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003134 }
3135 last = tline;
3136 tline = tline->next;
3137 }
3138 if (tok_type_(tline, TOK_WHITESPACE))
3139 last = tline, tline = tline->next;
3140 macro_start = NULL;
3141 last->next = NULL;
3142 t = tline;
3143 while (t) {
3144 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003145 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003146 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003147 !strcmp(tt->text, t->text))
3148 t->type = tt->type;
3149 }
3150 tt = t->next;
3151 t->next = macro_start;
3152 macro_start = t;
3153 t = tt;
3154 }
3155 /*
3156 * Good. We now have a macro name, a parameter count, and a
3157 * token list (in reverse order) for an expansion. We ought
3158 * to be OK just to create an SMacro, store it, and let
3159 * free_tlist have the rest of the line (which we have
3160 * carefully re-terminated after chopping off the expansion
3161 * from the end).
3162 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003163 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003164 free_tlist(origline);
3165 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003166
H. Peter Anvine2c80182005-01-15 22:15:51 +00003167 case PP_UNDEF:
3168 tline = tline->next;
3169 skip_white_(tline);
3170 tline = expand_id(tline);
3171 if (!tline || (tline->type != TOK_ID &&
3172 (tline->type != TOK_PREPROC_ID ||
3173 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003174 nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003175 free_tlist(origline);
3176 return DIRECTIVE_FOUND;
3177 }
3178 if (tline->next) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003179 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003180 "trailing garbage after macro name ignored");
3181 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003182
H. Peter Anvine2c80182005-01-15 22:15:51 +00003183 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003184 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003185 undef_smacro(ctx, mname);
3186 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003187 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003188
H. Peter Anvin9e200162008-06-04 17:23:14 -07003189 case PP_DEFSTR:
3190 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003191 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003192
3193 tline = tline->next;
3194 skip_white_(tline);
3195 tline = expand_id(tline);
3196 if (!tline || (tline->type != TOK_ID &&
3197 (tline->type != TOK_PREPROC_ID ||
3198 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003199 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003200 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003201 free_tlist(origline);
3202 return DIRECTIVE_FOUND;
3203 }
3204
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003205 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003206 last = tline;
3207 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003208 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003209
3210 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003211 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003212
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003213 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003214 macro_start = nasm_malloc(sizeof(*macro_start));
3215 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003216 macro_start->text = nasm_quote(p, strlen(p));
3217 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003218 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003219 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003220
3221 /*
3222 * We now have a macro name, an implicit parameter count of
3223 * zero, and a string token to use as an expansion. Create
3224 * and store an SMacro.
3225 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003226 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003227 free_tlist(origline);
3228 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003229
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003230 case PP_DEFTOK:
3231 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003232 casesense = (i == PP_DEFTOK);
3233
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003234 tline = tline->next;
3235 skip_white_(tline);
3236 tline = expand_id(tline);
3237 if (!tline || (tline->type != TOK_ID &&
3238 (tline->type != TOK_PREPROC_ID ||
3239 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003240 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003241 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003242 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003243 free_tlist(origline);
3244 return DIRECTIVE_FOUND;
3245 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003246 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003247 last = tline;
3248 tline = expand_smacro(tline->next);
3249 last->next = NULL;
3250
3251 t = tline;
3252 while (tok_type_(t, TOK_WHITESPACE))
3253 t = t->next;
3254 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003255 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003256 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003257 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003258 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003259 free_tlist(tline);
3260 free_tlist(origline);
3261 return DIRECTIVE_FOUND;
3262 }
3263
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003264 /*
3265 * Convert the string to a token stream. Note that smacros
3266 * are stored with the token stream reversed, so we have to
3267 * reverse the output of tokenize().
3268 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003269 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003270 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003271
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003272 /*
3273 * We now have a macro name, an implicit parameter count of
3274 * zero, and a numeric token to use as an expansion. Create
3275 * and store an SMacro.
3276 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003277 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003278 free_tlist(tline);
3279 free_tlist(origline);
3280 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003281
H. Peter Anvin418ca702008-05-30 10:42:30 -07003282 case PP_PATHSEARCH:
3283 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003284 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003285
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003286 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003287
3288 tline = tline->next;
3289 skip_white_(tline);
3290 tline = expand_id(tline);
3291 if (!tline || (tline->type != TOK_ID &&
3292 (tline->type != TOK_PREPROC_ID ||
3293 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003294 nasm_error(ERR_NONFATAL,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003295 "`%%pathsearch' expects a macro identifier as first parameter");
3296 free_tlist(origline);
3297 return DIRECTIVE_FOUND;
3298 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003299 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003300 last = tline;
3301 tline = expand_smacro(tline->next);
3302 last->next = NULL;
3303
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003304 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003305 while (tok_type_(t, TOK_WHITESPACE))
3306 t = t->next;
3307
3308 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003309 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003310 nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003311 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003312 free_tlist(origline);
3313 return DIRECTIVE_FOUND; /* but we did _something_ */
3314 }
3315 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003316 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003317 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003318 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003319 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003320 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003321
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003322 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003323 if (!found_path)
3324 found_path = p;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003325 macro_start = nasm_malloc(sizeof(*macro_start));
3326 macro_start->next = NULL;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003327 macro_start->text = nasm_quote(found_path, strlen(found_path));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003328 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003329 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003330
3331 /*
3332 * We now have a macro name, an implicit parameter count of
3333 * zero, and a string token to use as an expansion. Create
3334 * and store an SMacro.
3335 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003336 define_smacro(ctx, mname, casesense, 0, macro_start);
3337 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003338 free_tlist(origline);
3339 return DIRECTIVE_FOUND;
3340 }
3341
H. Peter Anvine2c80182005-01-15 22:15:51 +00003342 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003343 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003344
H. Peter Anvine2c80182005-01-15 22:15:51 +00003345 tline = tline->next;
3346 skip_white_(tline);
3347 tline = expand_id(tline);
3348 if (!tline || (tline->type != TOK_ID &&
3349 (tline->type != TOK_PREPROC_ID ||
3350 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003351 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003352 "`%%strlen' expects a macro identifier as first parameter");
3353 free_tlist(origline);
3354 return DIRECTIVE_FOUND;
3355 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003356 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003357 last = tline;
3358 tline = expand_smacro(tline->next);
3359 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003360
H. Peter Anvine2c80182005-01-15 22:15:51 +00003361 t = tline;
3362 while (tok_type_(t, TOK_WHITESPACE))
3363 t = t->next;
3364 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003365 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003366 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003367 "`%%strlen` requires string as second parameter");
3368 free_tlist(tline);
3369 free_tlist(origline);
3370 return DIRECTIVE_FOUND;
3371 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003372
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003373 macro_start = nasm_malloc(sizeof(*macro_start));
3374 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003375 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003376 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003377
H. Peter Anvine2c80182005-01-15 22:15:51 +00003378 /*
3379 * We now have a macro name, an implicit parameter count of
3380 * zero, and a numeric token to use as an expansion. Create
3381 * and store an SMacro.
3382 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003383 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003384 free_tlist(tline);
3385 free_tlist(origline);
3386 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003387
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003388 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003389 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003390
3391 tline = tline->next;
3392 skip_white_(tline);
3393 tline = expand_id(tline);
3394 if (!tline || (tline->type != TOK_ID &&
3395 (tline->type != TOK_PREPROC_ID ||
3396 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003397 nasm_error(ERR_NONFATAL,
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003398 "`%%strcat' expects a macro identifier as first parameter");
3399 free_tlist(origline);
3400 return DIRECTIVE_FOUND;
3401 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003402 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003403 last = tline;
3404 tline = expand_smacro(tline->next);
3405 last->next = NULL;
3406
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003407 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003408 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003409 switch (t->type) {
3410 case TOK_WHITESPACE:
3411 break;
3412 case TOK_STRING:
3413 len += t->a.len = nasm_unquote(t->text, NULL);
3414 break;
3415 case TOK_OTHER:
3416 if (!strcmp(t->text, ",")) /* permit comma separators */
3417 break;
3418 /* else fall through */
3419 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003420 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003421 "non-string passed to `%%strcat' (%d)", t->type);
3422 free_tlist(tline);
3423 free_tlist(origline);
3424 return DIRECTIVE_FOUND;
3425 }
3426 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003427
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003428 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003429 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003430 if (t->type == TOK_STRING) {
3431 memcpy(p, t->text, t->a.len);
3432 p += t->a.len;
3433 }
3434 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003435
3436 /*
3437 * We now have a macro name, an implicit parameter count of
3438 * zero, and a numeric token to use as an expansion. Create
3439 * and store an SMacro.
3440 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003441 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3442 macro_start->text = nasm_quote(pp, len);
3443 nasm_free(pp);
3444 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003445 free_tlist(tline);
3446 free_tlist(origline);
3447 return DIRECTIVE_FOUND;
3448
H. Peter Anvine2c80182005-01-15 22:15:51 +00003449 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003450 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003451 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003452 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003453
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003454 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003455
H. Peter Anvine2c80182005-01-15 22:15:51 +00003456 tline = tline->next;
3457 skip_white_(tline);
3458 tline = expand_id(tline);
3459 if (!tline || (tline->type != TOK_ID &&
3460 (tline->type != TOK_PREPROC_ID ||
3461 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003462 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003463 "`%%substr' expects a macro identifier as first parameter");
3464 free_tlist(origline);
3465 return DIRECTIVE_FOUND;
3466 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003467 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003468 last = tline;
3469 tline = expand_smacro(tline->next);
3470 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003471
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003472 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003473 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003474 while (tok_type_(t, TOK_WHITESPACE))
3475 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003476
H. Peter Anvine2c80182005-01-15 22:15:51 +00003477 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003478 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003479 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003480 "`%%substr` requires string as second parameter");
3481 free_tlist(tline);
3482 free_tlist(origline);
3483 return DIRECTIVE_FOUND;
3484 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003485
H. Peter Anvine2c80182005-01-15 22:15:51 +00003486 tt = t->next;
3487 tptr = &tt;
3488 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003489 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003490 if (!evalresult) {
3491 free_tlist(tline);
3492 free_tlist(origline);
3493 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003494 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003495 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003496 free_tlist(tline);
3497 free_tlist(origline);
3498 return DIRECTIVE_FOUND;
3499 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003500 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003501
3502 while (tok_type_(tt, TOK_WHITESPACE))
3503 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003504 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003505 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003506 } else {
3507 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003508 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003509 if (!evalresult) {
3510 free_tlist(tline);
3511 free_tlist(origline);
3512 return DIRECTIVE_FOUND;
3513 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003514 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003515 free_tlist(tline);
3516 free_tlist(origline);
3517 return DIRECTIVE_FOUND;
3518 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003519 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003520 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003521
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003522 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003523
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003524 /* make start and count being in range */
3525 if (start < 0)
3526 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003527 if (count < 0)
3528 count = len + count + 1 - start;
3529 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003530 count = len - start;
3531 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003532 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003533
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003534 macro_start = nasm_malloc(sizeof(*macro_start));
3535 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003536 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003537 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003538 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003539
H. Peter Anvine2c80182005-01-15 22:15:51 +00003540 /*
3541 * We now have a macro name, an implicit parameter count of
3542 * zero, and a numeric token to use as an expansion. Create
3543 * and store an SMacro.
3544 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003545 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003546 free_tlist(tline);
3547 free_tlist(origline);
3548 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003549 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003550
H. Peter Anvine2c80182005-01-15 22:15:51 +00003551 case PP_ASSIGN:
3552 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003553 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003554
H. Peter Anvine2c80182005-01-15 22:15:51 +00003555 tline = tline->next;
3556 skip_white_(tline);
3557 tline = expand_id(tline);
3558 if (!tline || (tline->type != TOK_ID &&
3559 (tline->type != TOK_PREPROC_ID ||
3560 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003561 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003562 "`%%%sassign' expects a macro identifier",
3563 (i == PP_IASSIGN ? "i" : ""));
3564 free_tlist(origline);
3565 return DIRECTIVE_FOUND;
3566 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003567 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003568 last = tline;
3569 tline = expand_smacro(tline->next);
3570 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003571
H. Peter Anvine2c80182005-01-15 22:15:51 +00003572 t = tline;
3573 tptr = &t;
3574 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003575 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003576 free_tlist(tline);
3577 if (!evalresult) {
3578 free_tlist(origline);
3579 return DIRECTIVE_FOUND;
3580 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003581
H. Peter Anvine2c80182005-01-15 22:15:51 +00003582 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003583 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003584 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003585
H. Peter Anvine2c80182005-01-15 22:15:51 +00003586 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003587 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003588 "non-constant value given to `%%%sassign'",
3589 (i == PP_IASSIGN ? "i" : ""));
3590 free_tlist(origline);
3591 return DIRECTIVE_FOUND;
3592 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003593
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003594 macro_start = nasm_malloc(sizeof(*macro_start));
3595 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003596 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003597 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003598
H. Peter Anvine2c80182005-01-15 22:15:51 +00003599 /*
3600 * We now have a macro name, an implicit parameter count of
3601 * zero, and a numeric token to use as an expansion. Create
3602 * and store an SMacro.
3603 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003604 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003605 free_tlist(origline);
3606 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003607
H. Peter Anvine2c80182005-01-15 22:15:51 +00003608 case PP_LINE:
3609 /*
3610 * Syntax is `%line nnn[+mmm] [filename]'
3611 */
3612 tline = tline->next;
3613 skip_white_(tline);
3614 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003615 nasm_error(ERR_NONFATAL, "`%%line' expects line number");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003616 free_tlist(origline);
3617 return DIRECTIVE_FOUND;
3618 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003619 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003620 m = 1;
3621 tline = tline->next;
3622 if (tok_is_(tline, "+")) {
3623 tline = tline->next;
3624 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003625 nasm_error(ERR_NONFATAL, "`%%line' expects line increment");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003626 free_tlist(origline);
3627 return DIRECTIVE_FOUND;
3628 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003629 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003630 tline = tline->next;
3631 }
3632 skip_white_(tline);
3633 src_set_linnum(k);
3634 istk->lineinc = m;
3635 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003636 char *fname = detoken(tline, false);
3637 src_set_fname(fname);
3638 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003639 }
3640 free_tlist(origline);
3641 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003642
H. Peter Anvine2c80182005-01-15 22:15:51 +00003643 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003644 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003645 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003646 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003647 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003648 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003649}
3650
3651/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003652 * Ensure that a macro parameter contains a condition code and
3653 * nothing else. Return the condition code index if so, or -1
3654 * otherwise.
3655 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003656static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003657{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003658 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003659
H. Peter Anvin25a99342007-09-22 17:45:45 -07003660 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003661 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003662
H. Peter Anvineba20a72002-04-30 20:53:55 +00003663 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003664 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003665 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003666 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003667 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003668 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003669 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003670
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003671 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003672}
3673
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003674/*
3675 * This routines walks over tokens strem and hadnles tokens
3676 * pasting, if @handle_explicit passed then explicit pasting
3677 * term is handled, otherwise -- implicit pastings only.
3678 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003679static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003680 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003681{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003682 Token *tok, *next, **prev_next, **prev_nonspace;
3683 bool pasted = false;
3684 char *buf, *p;
3685 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003686
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003687 /*
3688 * The last token before pasting. We need it
3689 * to be able to connect new handled tokens.
3690 * In other words if there were a tokens stream
3691 *
3692 * A -> B -> C -> D
3693 *
3694 * and we've joined tokens B and C, the resulting
3695 * stream should be
3696 *
3697 * A -> BC -> D
3698 */
3699 tok = *head;
3700 prev_next = NULL;
3701
3702 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3703 prev_nonspace = head;
3704 else
3705 prev_nonspace = NULL;
3706
3707 while (tok && (next = tok->next)) {
3708
3709 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003710 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003711 /* Zap redundant whitespaces */
3712 while (tok_type_(next, TOK_WHITESPACE))
3713 next = delete_Token(next);
3714 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003715 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003716
3717 case TOK_PASTE:
3718 /* Explicit pasting */
3719 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003720 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003721 next = delete_Token(tok);
3722
3723 while (tok_type_(next, TOK_WHITESPACE))
3724 next = delete_Token(next);
3725
3726 if (!pasted)
3727 pasted = true;
3728
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003729 /* Left pasting token is start of line */
3730 if (!prev_nonspace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003731 nasm_error(ERR_FATAL, "No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003732
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003733 /*
3734 * No ending token, this might happen in two
3735 * cases
3736 *
3737 * 1) There indeed no right token at all
3738 * 2) There is a bare "%define ID" statement,
3739 * and @ID does expand to whitespace.
3740 *
3741 * So technically we need to do a grammar analysis
3742 * in another stage of parsing, but for now lets don't
3743 * change the behaviour people used to. Simply allow
3744 * whitespace after paste token.
3745 */
3746 if (!next) {
3747 /*
3748 * Zap ending space tokens and that's all.
3749 */
3750 tok = (*prev_nonspace)->next;
3751 while (tok_type_(tok, TOK_WHITESPACE))
3752 tok = delete_Token(tok);
3753 tok = *prev_nonspace;
3754 tok->next = NULL;
3755 break;
3756 }
3757
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003758 tok = *prev_nonspace;
3759 while (tok_type_(tok, TOK_WHITESPACE))
3760 tok = delete_Token(tok);
3761 len = strlen(tok->text);
3762 len += strlen(next->text);
3763
3764 p = buf = nasm_malloc(len + 1);
3765 strcpy(p, tok->text);
3766 p = strchr(p, '\0');
3767 strcpy(p, next->text);
3768
3769 delete_Token(tok);
3770
3771 tok = tokenize(buf);
3772 nasm_free(buf);
3773
3774 *prev_nonspace = tok;
3775 while (tok && tok->next)
3776 tok = tok->next;
3777
3778 tok->next = delete_Token(next);
3779
3780 /* Restart from pasted tokens head */
3781 tok = *prev_nonspace;
3782 break;
3783
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003784 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003785 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003786 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003787 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3788 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003789
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003790 len = 0;
3791 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3792 len += strlen(next->text);
3793 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003794 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003795
3796 /* No match */
3797 if (tok == next)
3798 break;
3799
3800 len += strlen(tok->text);
3801 p = buf = nasm_malloc(len + 1);
3802
3803 while (tok != next) {
3804 strcpy(p, tok->text);
3805 p = strchr(p, '\0');
3806 tok = delete_Token(tok);
3807 }
3808
3809 tok = tokenize(buf);
3810 nasm_free(buf);
3811
3812 if (prev_next)
3813 *prev_next = tok;
3814 else
3815 *head = tok;
3816
3817 /*
3818 * Connect pasted into original stream,
3819 * ie A -> new-tokens -> B
3820 */
3821 while (tok && tok->next)
3822 tok = tok->next;
3823 tok->next = next;
3824
3825 if (!pasted)
3826 pasted = true;
3827
3828 /* Restart from pasted tokens head */
3829 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003830 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003831
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003832 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003833 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003834
3835 prev_next = &tok->next;
3836
3837 if (tok->next &&
3838 !tok_type_(tok->next, TOK_WHITESPACE) &&
3839 !tok_type_(tok->next, TOK_PASTE))
3840 prev_nonspace = prev_next;
3841
3842 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003843 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003844
3845 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003846}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003847
3848/*
3849 * expands to a list of tokens from %{x:y}
3850 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003851static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003852{
3853 Token *t = tline, **tt, *tm, *head;
3854 char *pos;
3855 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003856
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003857 pos = strchr(tline->text, ':');
3858 nasm_assert(pos);
3859
3860 lst = atoi(pos + 1);
3861 fst = atoi(tline->text + 1);
3862
3863 /*
3864 * only macros params are accounted so
3865 * if someone passes %0 -- we reject such
3866 * value(s)
3867 */
3868 if (lst == 0 || fst == 0)
3869 goto err;
3870
3871 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003872 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3873 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003874 goto err;
3875
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003876 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3877 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003878
3879 /* counted from zero */
3880 fst--, lst--;
3881
3882 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003883 * It will be at least one token. Note we
3884 * need to scan params until separator, otherwise
3885 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003886 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003887 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003888 head = new_Token(NULL, tm->type, tm->text, 0);
3889 tt = &head->next, tm = tm->next;
3890 while (tok_isnt_(tm, ",")) {
3891 t = new_Token(NULL, tm->type, tm->text, 0);
3892 *tt = t, tt = &t->next, tm = tm->next;
3893 }
3894
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003895 if (fst < lst) {
3896 for (i = fst + 1; i <= lst; i++) {
3897 t = new_Token(NULL, TOK_OTHER, ",", 0);
3898 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003899 j = (i + mac->rotate) % mac->nparam;
3900 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003901 while (tok_isnt_(tm, ",")) {
3902 t = new_Token(NULL, tm->type, tm->text, 0);
3903 *tt = t, tt = &t->next, tm = tm->next;
3904 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003905 }
3906 } else {
3907 for (i = fst - 1; i >= lst; i--) {
3908 t = new_Token(NULL, TOK_OTHER, ",", 0);
3909 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003910 j = (i + mac->rotate) % mac->nparam;
3911 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003912 while (tok_isnt_(tm, ",")) {
3913 t = new_Token(NULL, tm->type, tm->text, 0);
3914 *tt = t, tt = &t->next, tm = tm->next;
3915 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003916 }
3917 }
3918
3919 *last = tt;
3920 return head;
3921
3922err:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003923 nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003924 &tline->text[1]);
3925 return tline;
3926}
3927
H. Peter Anvin76690a12002-04-30 20:52:49 +00003928/*
3929 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003930 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003931 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003932 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003933static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003934{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003935 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003936 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003937 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003938
3939 tail = &thead;
3940 thead = NULL;
3941
H. Peter Anvine2c80182005-01-15 22:15:51 +00003942 while (tline) {
3943 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003944 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3945 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3946 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003947 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003948 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003949 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003950 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003951 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003952 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003953
H. Peter Anvine2c80182005-01-15 22:15:51 +00003954 t = tline;
3955 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003956
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003957 mac = istk->mstk;
3958 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3959 mac = mac->next_active;
3960 if (!mac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003961 nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003962 } else {
3963 pos = strchr(t->text, ':');
3964 if (!pos) {
3965 switch (t->text[1]) {
3966 /*
3967 * We have to make a substitution of one of the
3968 * forms %1, %-1, %+1, %%foo, %0.
3969 */
3970 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003971 type = TOK_NUMBER;
3972 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3973 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003974 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003975 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003976 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003977 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003978 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003979 text = nasm_strcat(tmpbuf, t->text + 2);
3980 break;
3981 case '-':
3982 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003983 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003984 tt = NULL;
3985 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003986 if (mac->nparam > 1)
3987 n = (n + mac->rotate) % mac->nparam;
3988 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003989 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003990 cc = find_cc(tt);
3991 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003992 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003993 "macro parameter %d is not a condition code",
3994 n + 1);
3995 text = NULL;
3996 } else {
3997 type = TOK_ID;
3998 if (inverse_ccs[cc] == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003999 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004000 "condition code `%s' is not invertible",
4001 conditions[cc]);
4002 text = NULL;
4003 } else
4004 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4005 }
4006 break;
4007 case '+':
4008 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004009 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004010 tt = NULL;
4011 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004012 if (mac->nparam > 1)
4013 n = (n + mac->rotate) % mac->nparam;
4014 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004015 }
4016 cc = find_cc(tt);
4017 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004018 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004019 "macro parameter %d is not a condition code",
4020 n + 1);
4021 text = NULL;
4022 } else {
4023 type = TOK_ID;
4024 text = nasm_strdup(conditions[cc]);
4025 }
4026 break;
4027 default:
4028 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004029 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004030 tt = NULL;
4031 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004032 if (mac->nparam > 1)
4033 n = (n + mac->rotate) % mac->nparam;
4034 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004035 }
4036 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004037 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004038 *tail = new_Token(NULL, tt->type, tt->text, 0);
4039 tail = &(*tail)->next;
4040 tt = tt->next;
4041 }
4042 }
4043 text = NULL; /* we've done it here */
4044 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004045 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004046 } else {
4047 /*
4048 * seems we have a parameters range here
4049 */
4050 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004051 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004052 if (head != t) {
4053 *tail = head;
4054 *last = tline;
4055 tline = head;
4056 text = NULL;
4057 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004058 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004059 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004060 if (!text) {
4061 delete_Token(t);
4062 } else {
4063 *tail = t;
4064 tail = &t->next;
4065 t->type = type;
4066 nasm_free(t->text);
4067 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004068 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004069 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004070 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004071 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004072 } else if (tline->type == TOK_INDIRECT) {
4073 t = tline;
4074 tline = tline->next;
4075 tt = tokenize(t->text);
4076 tt = expand_mmac_params(tt);
4077 tt = expand_smacro(tt);
4078 *tail = tt;
4079 while (tt) {
4080 tt->a.mac = NULL; /* Necessary? */
4081 tail = &tt->next;
4082 tt = tt->next;
4083 }
4084 delete_Token(t);
4085 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004086 } else {
4087 t = *tail = tline;
4088 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004089 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004090 tail = &t->next;
4091 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004092 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004093 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004094
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004095 if (changed) {
4096 const struct tokseq_match t[] = {
4097 {
4098 PP_CONCAT_MASK(TOK_ID) |
4099 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4100 PP_CONCAT_MASK(TOK_ID) |
4101 PP_CONCAT_MASK(TOK_NUMBER) |
4102 PP_CONCAT_MASK(TOK_FLOAT) |
4103 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4104 },
4105 {
4106 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4107 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4108 }
4109 };
4110 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4111 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004112
H. Peter Anvin76690a12002-04-30 20:52:49 +00004113 return thead;
4114}
4115
4116/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004117 * Expand all single-line macro calls made in the given line.
4118 * Return the expanded version of the line. The original is deemed
4119 * to be destroyed in the process. (In reality we'll just move
4120 * Tokens from input to output a lot of the time, rather than
4121 * actually bothering to destroy and replicate.)
4122 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004123
H. Peter Anvine2c80182005-01-15 22:15:51 +00004124static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004125{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004126 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004127 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004128 Token **params;
4129 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004130 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004131 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004132 Token *org_tline = tline;
4133 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004134 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004135 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004136 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004137
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004138 /*
4139 * Trick: we should avoid changing the start token pointer since it can
4140 * be contained in "next" field of other token. Because of this
4141 * we allocate a copy of first token and work with it; at the end of
4142 * routine we copy it back
4143 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004144 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004145 tline = new_Token(org_tline->next, org_tline->type,
4146 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004147 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004148 nasm_free(org_tline->text);
4149 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004150 }
4151
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004152 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004153
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004154again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004155 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004156 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004157
H. Peter Anvine2c80182005-01-15 22:15:51 +00004158 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004159 if (!--deadman) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004160 nasm_error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004161 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004162 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004163
H. Peter Anvine2c80182005-01-15 22:15:51 +00004164 if ((mname = tline->text)) {
4165 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004166 if (tline->type == TOK_ID) {
4167 head = (SMacro *)hash_findix(&smacros, mname);
4168 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004169 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004170 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4171 } else
4172 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004173
H. Peter Anvine2c80182005-01-15 22:15:51 +00004174 /*
4175 * We've hit an identifier. As in is_mmacro below, we first
4176 * check whether the identifier is a single-line macro at
4177 * all, then think about checking for parameters if
4178 * necessary.
4179 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004180 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004181 if (!mstrcmp(m->name, mname, m->casesense))
4182 break;
4183 if (m) {
4184 mstart = tline;
4185 params = NULL;
4186 paramsize = NULL;
4187 if (m->nparam == 0) {
4188 /*
4189 * Simple case: the macro is parameterless. Discard the
4190 * one token that the macro call took, and push the
4191 * expansion back on the to-do stack.
4192 */
4193 if (!m->expansion) {
4194 if (!strcmp("__FILE__", m->name)) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004195 const char *file = src_get_fname();
4196 /* nasm_free(tline->text); here? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004197 tline->text = nasm_quote(file, strlen(file));
4198 tline->type = TOK_STRING;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004199 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004200 }
4201 if (!strcmp("__LINE__", m->name)) {
4202 nasm_free(tline->text);
4203 make_tok_num(tline, src_get_linnum());
4204 continue;
4205 }
4206 if (!strcmp("__BITS__", m->name)) {
4207 nasm_free(tline->text);
4208 make_tok_num(tline, globalbits);
4209 continue;
4210 }
4211 tline = delete_Token(tline);
4212 continue;
4213 }
4214 } else {
4215 /*
4216 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004217 * exists and takes parameters. We must find the
4218 * parameters in the call, count them, find the SMacro
4219 * that corresponds to that form of the macro call, and
4220 * substitute for the parameters when we expand. What a
4221 * pain.
4222 */
4223 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004224 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004225 do {
4226 t = tline->next;
4227 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004228 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004229 t->text = NULL;
4230 t = tline->next = delete_Token(t);
4231 }
4232 tline = t;
4233 } while (tok_type_(tline, TOK_WHITESPACE));
4234 if (!tok_is_(tline, "(")) {
4235 /*
4236 * This macro wasn't called with parameters: ignore
4237 * the call. (Behaviour borrowed from gnu cpp.)
4238 */
4239 tline = mstart;
4240 m = NULL;
4241 } else {
4242 int paren = 0;
4243 int white = 0;
4244 brackets = 0;
4245 nparam = 0;
4246 sparam = PARAM_DELTA;
4247 params = nasm_malloc(sparam * sizeof(Token *));
4248 params[0] = tline->next;
4249 paramsize = nasm_malloc(sparam * sizeof(int));
4250 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004251 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004252 /*
4253 * For some unusual expansions
4254 * which concatenates function call
4255 */
4256 t = tline->next;
4257 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004258 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004259 t->text = NULL;
4260 t = tline->next = delete_Token(t);
4261 }
4262 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004263
H. Peter Anvine2c80182005-01-15 22:15:51 +00004264 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004265 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004266 "macro call expects terminating `)'");
4267 break;
4268 }
4269 if (tline->type == TOK_WHITESPACE
4270 && brackets <= 0) {
4271 if (paramsize[nparam])
4272 white++;
4273 else
4274 params[nparam] = tline->next;
4275 continue; /* parameter loop */
4276 }
4277 if (tline->type == TOK_OTHER
4278 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004279 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004280 if (ch == ',' && !paren && brackets <= 0) {
4281 if (++nparam >= sparam) {
4282 sparam += PARAM_DELTA;
4283 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004284 sparam * sizeof(Token *));
4285 paramsize = nasm_realloc(paramsize,
4286 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004287 }
4288 params[nparam] = tline->next;
4289 paramsize[nparam] = 0;
4290 white = 0;
4291 continue; /* parameter loop */
4292 }
4293 if (ch == '{' &&
4294 (brackets > 0 || (brackets == 0 &&
4295 !paramsize[nparam])))
4296 {
4297 if (!(brackets++)) {
4298 params[nparam] = tline->next;
4299 continue; /* parameter loop */
4300 }
4301 }
4302 if (ch == '}' && brackets > 0)
4303 if (--brackets == 0) {
4304 brackets = -1;
4305 continue; /* parameter loop */
4306 }
4307 if (ch == '(' && !brackets)
4308 paren++;
4309 if (ch == ')' && brackets <= 0)
4310 if (--paren < 0)
4311 break;
4312 }
4313 if (brackets < 0) {
4314 brackets = 0;
H. Peter Anvin130736c2016-02-17 20:27:41 -08004315 nasm_error(ERR_NONFATAL, "braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004316 "enclose all of macro parameter");
4317 }
4318 paramsize[nparam] += white + 1;
4319 white = 0;
4320 } /* parameter loop */
4321 nparam++;
4322 while (m && (m->nparam != nparam ||
4323 mstrcmp(m->name, mname,
4324 m->casesense)))
4325 m = m->next;
4326 if (!m)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004327 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004328 "macro `%s' exists, "
4329 "but not taking %d parameters",
4330 mstart->text, nparam);
4331 }
4332 }
4333 if (m && m->in_progress)
4334 m = NULL;
4335 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004336 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004337 * Design question: should we handle !tline, which
4338 * indicates missing ')' here, or expand those
4339 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004340 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004341 */
4342 nasm_free(params);
4343 nasm_free(paramsize);
4344 tline = mstart;
4345 } else {
4346 /*
4347 * Expand the macro: we are placed on the last token of the
4348 * call, so that we can easily split the call from the
4349 * following tokens. We also start by pushing an SMAC_END
4350 * token for the cycle removal.
4351 */
4352 t = tline;
4353 if (t) {
4354 tline = t->next;
4355 t->next = NULL;
4356 }
4357 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004358 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004359 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004360 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004361 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004362 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004363 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004364 Token *ttt, *pt;
4365 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004366
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004367 ttt = params[t->type - TOK_SMAC_PARAM];
4368 i = paramsize[t->type - TOK_SMAC_PARAM];
4369 while (--i >= 0) {
4370 pt = *ptail = new_Token(tline, ttt->type,
4371 ttt->text, 0);
4372 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004373 ttt = ttt->next;
4374 }
4375 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004376 } else if (t->type == TOK_PREPROC_Q) {
4377 tt = new_Token(tline, TOK_ID, mname, 0);
4378 tline = tt;
4379 } else if (t->type == TOK_PREPROC_QQ) {
4380 tt = new_Token(tline, TOK_ID, m->name, 0);
4381 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004382 } else {
4383 tt = new_Token(tline, t->type, t->text, 0);
4384 tline = tt;
4385 }
4386 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004387
H. Peter Anvine2c80182005-01-15 22:15:51 +00004388 /*
4389 * Having done that, get rid of the macro call, and clean
4390 * up the parameters.
4391 */
4392 nasm_free(params);
4393 nasm_free(paramsize);
4394 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004395 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004396 continue; /* main token loop */
4397 }
4398 }
4399 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004400
H. Peter Anvine2c80182005-01-15 22:15:51 +00004401 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004402 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004403 tline = delete_Token(tline);
4404 } else {
4405 t = *tail = tline;
4406 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004407 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004408 t->next = NULL;
4409 tail = &t->next;
4410 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004411 }
4412
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004413 /*
4414 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004415 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004416 * TOK_IDs should be concatenated.
4417 * Also we look for %+ tokens and concatenate the tokens before and after
4418 * them (without white spaces in between).
4419 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004420 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004421 const struct tokseq_match t[] = {
4422 {
4423 PP_CONCAT_MASK(TOK_ID) |
4424 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4425 PP_CONCAT_MASK(TOK_ID) |
4426 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4427 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4428 }
4429 };
4430 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004431 /*
4432 * If we concatenated something, *and* we had previously expanded
4433 * an actual macro, scan the lines again for macros...
4434 */
4435 tline = thead;
4436 expanded = false;
4437 goto again;
4438 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004439 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004440
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004441err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004442 if (org_tline) {
4443 if (thead) {
4444 *org_tline = *thead;
4445 /* since we just gave text to org_line, don't free it */
4446 thead->text = NULL;
4447 delete_Token(thead);
4448 } else {
4449 /* the expression expanded to empty line;
4450 we can't return NULL for some reasons
4451 we just set the line to a single WHITESPACE token. */
4452 memset(org_tline, 0, sizeof(*org_tline));
4453 org_tline->text = NULL;
4454 org_tline->type = TOK_WHITESPACE;
4455 }
4456 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004457 }
4458
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004459 return thead;
4460}
4461
4462/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004463 * Similar to expand_smacro but used exclusively with macro identifiers
4464 * right before they are fetched in. The reason is that there can be
4465 * identifiers consisting of several subparts. We consider that if there
4466 * are more than one element forming the name, user wants a expansion,
4467 * otherwise it will be left as-is. Example:
4468 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004469 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004470 *
4471 * the identifier %$abc will be left as-is so that the handler for %define
4472 * will suck it and define the corresponding value. Other case:
4473 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004474 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004475 *
4476 * In this case user wants name to be expanded *before* %define starts
4477 * working, so we'll expand %$abc into something (if it has a value;
4478 * otherwise it will be left as-is) then concatenate all successive
4479 * PP_IDs into one.
4480 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004481static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004482{
4483 Token *cur, *oldnext = NULL;
4484
H. Peter Anvin734b1882002-04-30 21:01:08 +00004485 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004486 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004487
4488 cur = tline;
4489 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004490 (cur->next->type == TOK_ID ||
4491 cur->next->type == TOK_PREPROC_ID
4492 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004493 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004494
4495 /* If identifier consists of just one token, don't expand */
4496 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004497 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004498
H. Peter Anvine2c80182005-01-15 22:15:51 +00004499 if (cur) {
4500 oldnext = cur->next; /* Detach the tail past identifier */
4501 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004502 }
4503
H. Peter Anvin734b1882002-04-30 21:01:08 +00004504 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004505
H. Peter Anvine2c80182005-01-15 22:15:51 +00004506 if (cur) {
4507 /* expand_smacro possibly changhed tline; re-scan for EOL */
4508 cur = tline;
4509 while (cur && cur->next)
4510 cur = cur->next;
4511 if (cur)
4512 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004513 }
4514
4515 return tline;
4516}
4517
4518/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004519 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004520 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004521 * to check for an initial label - that's taken care of in
4522 * expand_mmacro - but must check numbers of parameters. Guaranteed
4523 * to be called with tline->type == TOK_ID, so the putative macro
4524 * name is easy to find.
4525 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004526static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004527{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004528 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004529 Token **params;
4530 int nparam;
4531
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004532 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004533
4534 /*
4535 * Efficiency: first we see if any macro exists with the given
4536 * name. If not, we can return NULL immediately. _Then_ we
4537 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004538 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004539 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004540 list_for_each(m, head)
4541 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004542 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004543 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004544 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004545
4546 /*
4547 * OK, we have a potential macro. Count and demarcate the
4548 * parameters.
4549 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004550 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004551
4552 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004553 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004554 * structure that handles this number.
4555 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004556 while (m) {
4557 if (m->nparam_min <= nparam
4558 && (m->plus || nparam <= m->nparam_max)) {
4559 /*
4560 * This one is right. Just check if cycle removal
4561 * prohibits us using it before we actually celebrate...
4562 */
4563 if (m->in_progress > m->max_depth) {
4564 if (m->max_depth > 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004565 nasm_error(ERR_WARNING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004566 "reached maximum recursion depth of %i",
4567 m->max_depth);
4568 }
4569 nasm_free(params);
4570 return NULL;
4571 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004572 /*
4573 * It's right, and we can use it. Add its default
4574 * parameters to the end of our list if necessary.
4575 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004576 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004577 params =
4578 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004579 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004580 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004581 while (nparam < m->nparam_min + m->ndefs) {
4582 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004583 nparam++;
4584 }
4585 }
4586 /*
4587 * If we've gone over the maximum parameter count (and
4588 * we're in Plus mode), ignore parameters beyond
4589 * nparam_max.
4590 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004591 if (m->plus && nparam > m->nparam_max)
4592 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004593 /*
4594 * Then terminate the parameter list, and leave.
4595 */
4596 if (!params) { /* need this special case */
4597 params = nasm_malloc(sizeof(*params));
4598 nparam = 0;
4599 }
4600 params[nparam] = NULL;
4601 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004602 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004603 }
4604 /*
4605 * This one wasn't right: look for the next one with the
4606 * same name.
4607 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004608 list_for_each(m, m->next)
4609 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004610 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004611 }
4612
4613 /*
4614 * After all that, we didn't find one with the right number of
4615 * parameters. Issue a warning, and fail to expand the macro.
4616 */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004617 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004618 "macro `%s' exists, but not taking %d parameters",
4619 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004620 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004621 return NULL;
4622}
4623
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004624
4625/*
4626 * Save MMacro invocation specific fields in
4627 * preparation for a recursive macro expansion
4628 */
4629static void push_mmacro(MMacro *m)
4630{
4631 MMacroInvocation *i;
4632
4633 i = nasm_malloc(sizeof(MMacroInvocation));
4634 i->prev = m->prev;
4635 i->params = m->params;
4636 i->iline = m->iline;
4637 i->nparam = m->nparam;
4638 i->rotate = m->rotate;
4639 i->paramlen = m->paramlen;
4640 i->unique = m->unique;
4641 i->condcnt = m->condcnt;
4642 m->prev = i;
4643}
4644
4645
4646/*
4647 * Restore MMacro invocation specific fields that were
4648 * saved during a previous recursive macro expansion
4649 */
4650static void pop_mmacro(MMacro *m)
4651{
4652 MMacroInvocation *i;
4653
4654 if (m->prev) {
4655 i = m->prev;
4656 m->prev = i->prev;
4657 m->params = i->params;
4658 m->iline = i->iline;
4659 m->nparam = i->nparam;
4660 m->rotate = i->rotate;
4661 m->paramlen = i->paramlen;
4662 m->unique = i->unique;
4663 m->condcnt = i->condcnt;
4664 nasm_free(i);
4665 }
4666}
4667
4668
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004669/*
4670 * Expand the multi-line macro call made by the given line, if
4671 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004672 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004673 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004674static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004675{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004676 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004677 Token *label = NULL;
4678 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004679 Token **params, *t, *tt;
4680 MMacro *m;
4681 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004682 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004683 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004684
4685 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004686 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004687 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004688 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004689 return 0;
4690 m = is_mmacro(t, &params);
4691 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004692 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004693 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004694 Token *last;
4695 /*
4696 * We have an id which isn't a macro call. We'll assume
4697 * it might be a label; we'll also check to see if a
4698 * colon follows it. Then, if there's another id after
4699 * that lot, we'll check it again for macro-hood.
4700 */
4701 label = last = t;
4702 t = t->next;
4703 if (tok_type_(t, TOK_WHITESPACE))
4704 last = t, t = t->next;
4705 if (tok_is_(t, ":")) {
4706 dont_prepend = 1;
4707 last = t, t = t->next;
4708 if (tok_type_(t, TOK_WHITESPACE))
4709 last = t, t = t->next;
4710 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004711 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4712 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004713 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004714 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004715 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004716 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004717
4718 /*
4719 * Fix up the parameters: this involves stripping leading and
4720 * trailing whitespace, then stripping braces if they are
4721 * present.
4722 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004723 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004724 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004725
H. Peter Anvine2c80182005-01-15 22:15:51 +00004726 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004727 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004728 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004729
H. Peter Anvine2c80182005-01-15 22:15:51 +00004730 t = params[i];
4731 skip_white_(t);
4732 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004733 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004734 params[i] = t;
4735 paramlen[i] = 0;
4736 while (t) {
4737 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4738 break; /* ... because we have hit a comma */
4739 if (comma && t->type == TOK_WHITESPACE
4740 && tok_is_(t->next, ","))
4741 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004742 if (brace && t->type == TOK_OTHER) {
4743 if (t->text[0] == '{')
4744 brace++; /* ... or a nested opening brace */
4745 else if (t->text[0] == '}')
4746 if (!--brace)
4747 break; /* ... or a brace */
4748 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004749 t = t->next;
4750 paramlen[i]++;
4751 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004752 if (brace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004753 nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004754 }
4755
4756 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004757 * OK, we have a MMacro structure together with a set of
4758 * parameters. We must now go through the expansion and push
4759 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004760 * parameter tokens and macro-local tokens doesn't get done
4761 * until the single-line macro substitution process; this is
4762 * because delaying them allows us to change the semantics
4763 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004764 *
4765 * First, push an end marker on to istk->expansion, mark this
4766 * macro as in progress, and set up its invocation-specific
4767 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004768 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004769 ll = nasm_malloc(sizeof(Line));
4770 ll->next = istk->expansion;
4771 ll->finishes = m;
4772 ll->first = NULL;
4773 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004774
4775 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004776 * Save the previous MMacro expansion in the case of
4777 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004778 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004779 if (m->max_depth && m->in_progress)
4780 push_mmacro(m);
4781
4782 m->in_progress ++;
4783 m->params = params;
4784 m->iline = tline;
4785 m->nparam = nparam;
4786 m->rotate = 0;
4787 m->paramlen = paramlen;
4788 m->unique = unique++;
4789 m->lineno = 0;
4790 m->condcnt = 0;
4791
4792 m->next_active = istk->mstk;
4793 istk->mstk = m;
4794
4795 list_for_each(l, m->expansion) {
4796 Token **tail;
4797
4798 ll = nasm_malloc(sizeof(Line));
4799 ll->finishes = NULL;
4800 ll->next = istk->expansion;
4801 istk->expansion = ll;
4802 tail = &ll->first;
4803
4804 list_for_each(t, l->first) {
4805 Token *x = t;
4806 switch (t->type) {
4807 case TOK_PREPROC_Q:
4808 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004809 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004810 case TOK_PREPROC_QQ:
4811 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4812 break;
4813 case TOK_PREPROC_ID:
4814 if (t->text[1] == '0' && t->text[2] == '0') {
4815 dont_prepend = -1;
4816 x = label;
4817 if (!x)
4818 continue;
4819 }
4820 /* fall through */
4821 default:
4822 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4823 break;
4824 }
4825 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004826 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004827 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004828 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004829
4830 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004831 * If we had a label, push it on as the first line of
4832 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004833 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004834 if (label) {
4835 if (dont_prepend < 0)
4836 free_tlist(startline);
4837 else {
4838 ll = nasm_malloc(sizeof(Line));
4839 ll->finishes = NULL;
4840 ll->next = istk->expansion;
4841 istk->expansion = ll;
4842 ll->first = startline;
4843 if (!dont_prepend) {
4844 while (label->next)
4845 label = label->next;
4846 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004847 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004848 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004849 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004850
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004851 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004852
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004853 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004854}
4855
H. Peter Anvin130736c2016-02-17 20:27:41 -08004856/*
4857 * This function adds macro names to error messages, and suppresses
4858 * them if necessary.
4859 */
4860static void pp_verror(int severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004861{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004862 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004863 MMacro *mmac = NULL;
4864 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004865
H. Peter Anvin130736c2016-02-17 20:27:41 -08004866 /*
4867 * If we're in a dead branch of IF or something like it, ignore the error.
4868 * However, because %else etc are evaluated in the state context
4869 * of the previous branch, errors might get lost:
4870 * %if 0 ... %else trailing garbage ... %endif
4871 * So %else etc should set the ERR_PP_PRECOND flag.
4872 */
4873 if ((severity & ERR_MASK) < ERR_FATAL &&
4874 istk && istk->conds &&
4875 ((severity & ERR_PP_PRECOND) ?
4876 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004877 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004878 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004879
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004880 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004881 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004882 mmac = istk->mstk;
4883 /* but %rep blocks should be skipped */
4884 while (mmac && !mmac->name)
4885 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004886 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004887
H. Peter Anvin130736c2016-02-17 20:27:41 -08004888 if (mmac) {
4889 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004890
H. Peter Anvin130736c2016-02-17 20:27:41 -08004891 nasm_set_verror(real_verror);
4892 nasm_error(severity, "(%s:%d) %s",
4893 mmac->name, mmac->lineno - delta, buff);
4894 nasm_set_verror(pp_verror);
4895 } else {
4896 real_verror(severity, fmt, arg);
4897 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004898}
4899
H. Peter Anvin734b1882002-04-30 21:01:08 +00004900static void
H. Peter Anvin130736c2016-02-17 20:27:41 -08004901pp_reset(char *file, int apass, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004902{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004903 Token *t;
4904
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004905 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004906 istk = nasm_malloc(sizeof(Include));
4907 istk->next = NULL;
4908 istk->conds = NULL;
4909 istk->expansion = NULL;
4910 istk->mstk = NULL;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07004911 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004912 istk->fname = NULL;
H. Peter Anvin274cda82016-05-10 02:56:29 -07004913 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004914 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004915 if (!istk->fp)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004916 nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004917 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004918 nested_mac_count = 0;
4919 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004920 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004921 unique = 0;
H. Peter Anvinf7606612016-07-13 14:23:48 -07004922
4923 if (tasm_compatible_mode)
4924 pp_add_stdmac(nasm_stdmac_tasm);
4925
4926 pp_add_stdmac(nasm_stdmac_nasm);
4927 pp_add_stdmac(nasm_stdmac_version);
4928
4929 stdmacpos = stdmacros[0];
4930 stdmacnext = &stdmacros[1];
4931
H. Peter Anvind2456592008-06-19 15:04:18 -07004932 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004933
4934 /*
4935 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4936 * The caller, however, will also pass in 3 for preprocess-only so
4937 * we can set __PASS__ accordingly.
4938 */
4939 pass = apass > 2 ? 2 : apass;
4940
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004941 dephead = deplist;
H. Peter Anvin436e3672016-10-04 01:12:28 -07004942 nasm_add_string_to_strlist(dephead, file);
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004943
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004944 /*
4945 * Define the __PASS__ macro. This is defined here unlike
4946 * all the other builtins, because it is special -- it varies between
4947 * passes.
4948 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004949 t = nasm_malloc(sizeof(*t));
4950 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004951 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004952 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004953 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004954}
4955
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07004956static void pp_init(void)
4957{
4958 hash_init(&FileHash, HASH_MEDIUM);
4959}
4960
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004961static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004962{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004963 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004964 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004965
H. Peter Anvin130736c2016-02-17 20:27:41 -08004966 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08004967
H. Peter Anvine2c80182005-01-15 22:15:51 +00004968 while (1) {
4969 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004970 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004971 * buffer or from the input file.
4972 */
4973 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004974 while (istk->expansion && istk->expansion->finishes) {
4975 Line *l = istk->expansion;
4976 if (!l->finishes->name && l->finishes->in_progress > 1) {
4977 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004978
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004979 /*
4980 * This is a macro-end marker for a macro with no
4981 * name, which means it's not really a macro at all
4982 * but a %rep block, and the `in_progress' field is
4983 * more than 1, meaning that we still need to
4984 * repeat. (1 means the natural last repetition; 0
4985 * means termination by %exitrep.) We have
4986 * therefore expanded up to the %endrep, and must
4987 * push the whole block on to the expansion buffer
4988 * again. We don't bother to remove the macro-end
4989 * marker: we'd only have to generate another one
4990 * if we did.
4991 */
4992 l->finishes->in_progress--;
4993 list_for_each(l, l->finishes->expansion) {
4994 Token *t, *tt, **tail;
4995
4996 ll = nasm_malloc(sizeof(Line));
4997 ll->next = istk->expansion;
4998 ll->finishes = NULL;
4999 ll->first = NULL;
5000 tail = &ll->first;
5001
5002 list_for_each(t, l->first) {
5003 if (t->text || t->type == TOK_WHITESPACE) {
5004 tt = *tail = new_Token(NULL, t->type, t->text, 0);
5005 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005006 }
5007 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005008
5009 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005010 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005011 } else {
5012 /*
5013 * Check whether a `%rep' was started and not ended
5014 * within this macro expansion. This can happen and
5015 * should be detected. It's a fatal error because
5016 * I'm too confused to work out how to recover
5017 * sensibly from it.
5018 */
5019 if (defining) {
5020 if (defining->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005021 nasm_panic(0, "defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005022 else if (istk->mstk->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005023 nasm_fatal(0, "`%%rep' without `%%endrep' within"
5024 " expansion of macro `%s'",
5025 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005026 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005027
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005028 /*
5029 * FIXME: investigate the relationship at this point between
5030 * istk->mstk and l->finishes
5031 */
5032 {
5033 MMacro *m = istk->mstk;
5034 istk->mstk = m->next_active;
5035 if (m->name) {
5036 /*
5037 * This was a real macro call, not a %rep, and
5038 * therefore the parameter information needs to
5039 * be freed.
5040 */
5041 if (m->prev) {
5042 pop_mmacro(m);
5043 l->finishes->in_progress --;
5044 } else {
5045 nasm_free(m->params);
5046 free_tlist(m->iline);
5047 nasm_free(m->paramlen);
5048 l->finishes->in_progress = 0;
5049 }
5050 } else
5051 free_mmacro(m);
5052 }
5053 istk->expansion = l->next;
5054 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005055 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005056 }
5057 }
5058 while (1) { /* until we get a line we can use */
5059
5060 if (istk->expansion) { /* from a macro expansion */
5061 char *p;
5062 Line *l = istk->expansion;
5063 if (istk->mstk)
5064 istk->mstk->lineno++;
5065 tline = l->first;
5066 istk->expansion = l->next;
5067 nasm_free(l);
5068 p = detoken(tline, false);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005069 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005070 nasm_free(p);
5071 break;
5072 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005073 line = read_line();
5074 if (line) { /* from the current input file */
5075 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005076 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005077 nasm_free(line);
5078 break;
5079 }
5080 /*
5081 * The current file has ended; work down the istk
5082 */
5083 {
5084 Include *i = istk;
5085 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005086 if (i->conds) {
5087 /* nasm_error can't be conditionally suppressed */
H. Peter Anvin41087062016-03-03 14:27:34 -08005088 nasm_fatal(0,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005089 "expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005090 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005091 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005092 if (i->next)
5093 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005094 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005095 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005096 nasm_free(i);
H. Peter Anvin130736c2016-02-17 20:27:41 -08005097 if (!istk) {
5098 line = NULL;
5099 goto done;
5100 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005101 if (istk->expansion && istk->expansion->finishes)
5102 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005103 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005104 }
5105
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005106 /*
5107 * We must expand MMacro parameters and MMacro-local labels
5108 * _before_ we plunge into directive processing, to cope
5109 * with things like `%define something %1' such as STRUC
5110 * uses. Unless we're _defining_ a MMacro, in which case
5111 * those tokens should be left alone to go into the
5112 * definition; and unless we're in a non-emitting
5113 * condition, in which case we don't want to meddle with
5114 * anything.
5115 */
5116 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5117 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005118 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005119 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005120
H. Peter Anvine2c80182005-01-15 22:15:51 +00005121 /*
5122 * Check the line to see if it's a preprocessor directive.
5123 */
5124 if (do_directive(tline) == DIRECTIVE_FOUND) {
5125 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005126 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005127 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005128 * We're defining a multi-line macro. We emit nothing
5129 * at all, and just
5130 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005131 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005132 Line *l = nasm_malloc(sizeof(Line));
5133 l->next = defining->expansion;
5134 l->first = tline;
5135 l->finishes = NULL;
5136 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005137 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005138 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005139 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005140 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005141 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005142 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005143 * directive so we keep our place correctly.
5144 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005145 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005146 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005147 } else if (istk->mstk && !istk->mstk->in_progress) {
5148 /*
5149 * We're in a %rep block which has been terminated, so
5150 * we're walking through to the %endrep without
5151 * emitting anything. Emit nothing at all, not even a
5152 * blank line: when we emerge from the %rep block we'll
5153 * give a line-number directive so we keep our place
5154 * correctly.
5155 */
5156 free_tlist(tline);
5157 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005158 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005159 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005160 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005161 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005162 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005163 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005164 line = detoken(tline, true);
5165 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005166 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005167 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005168 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005169 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005170 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005171 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005172
H. Peter Anvin130736c2016-02-17 20:27:41 -08005173done:
5174 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005175 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005176}
5177
H. Peter Anvine2c80182005-01-15 22:15:51 +00005178static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005179{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005180 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005181
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005182 if (defining) {
5183 if (defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005184 nasm_error(ERR_NONFATAL,
5185 "end of file while still defining macro `%s'",
5186 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005187 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005188 nasm_error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005189 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005190
5191 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005192 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005193 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005194
5195 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005196
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005197 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005198 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005199 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005200 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005201 Include *i = istk;
5202 istk = istk->next;
5203 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005204 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005205 }
5206 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005207 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005208 src_set_fname(NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005209 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005210 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005211 free_llist(predef);
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005212 predef = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005213 delete_Blocks();
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005214 freeTokens = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005215 while ((i = ipath)) {
5216 ipath = i->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005217 if (i->path)
5218 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005219 nasm_free(i);
5220 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005221 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005222}
5223
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005224static void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005225{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005226 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005227
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005228 i = nasm_malloc(sizeof(IncPath));
5229 i->path = path ? nasm_strdup(path) : NULL;
5230 i->next = NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005231
H. Peter Anvin89cee572009-07-15 09:16:54 -04005232 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005233 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005234 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005235 j = j->next;
5236 j->next = i;
5237 } else {
5238 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005239 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005240}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005241
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005242static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005243{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005244 Token *inc, *space, *name;
5245 Line *l;
5246
H. Peter Anvin734b1882002-04-30 21:01:08 +00005247 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5248 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5249 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005250
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005251 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005252 l->next = predef;
5253 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005254 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005255 predef = l;
5256}
5257
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005258static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005259{
5260 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005261 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005262 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005263
H. Peter Anvin130736c2016-02-17 20:27:41 -08005264 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005265
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005266 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005267 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5268 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005269 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005270 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005271 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005272 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005273 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005274
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005275 if (space->next->type != TOK_PREPROC_ID &&
5276 space->next->type != TOK_ID)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005277 nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005278
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005279 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005280 l->next = predef;
5281 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005282 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005283 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005284
5285 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005286}
5287
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005288static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005289{
5290 Token *def, *space;
5291 Line *l;
5292
H. Peter Anvin734b1882002-04-30 21:01:08 +00005293 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5294 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005295 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005296
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005297 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005298 l->next = predef;
5299 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005300 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005301 predef = l;
5302}
5303
H. Peter Anvinf7606612016-07-13 14:23:48 -07005304static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005305{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005306 macros_t **mp;
5307
5308 /* Find the end of the list and avoid duplicates */
5309 for (mp = stdmacros; *mp; mp++) {
5310 if (*mp == macros)
5311 return; /* Nothing to do */
5312 }
5313
5314 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5315
5316 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005317}
5318
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005319static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005320{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005321 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005322 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005323 tok->text = nasm_strdup(numbuf);
5324 tok->type = TOK_NUMBER;
5325}
5326
H. Peter Anvin37368952016-05-09 14:10:32 -07005327static void pp_list_one_macro(MMacro *m, int severity)
5328{
5329 if (!m)
5330 return;
5331
5332 /* We need to print the next_active list in reverse order */
5333 pp_list_one_macro(m->next_active, severity);
5334
5335 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005336 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvin37368952016-05-09 14:10:32 -07005337 nasm_error(severity, "... from macro `%s' defined here", m->name);
5338 }
5339}
5340
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005341static void pp_error_list_macros(int severity)
5342{
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005343 int32_t saved_line;
5344 const char *saved_fname = NULL;
5345
5346 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY;
H. Peter Anvin274cda82016-05-10 02:56:29 -07005347 src_get(&saved_line, &saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005348
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005349 if (istk)
5350 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005351
H. Peter Anvin274cda82016-05-10 02:56:29 -07005352 src_set(saved_line, saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005353}
5354
H. Peter Anvine7469712016-02-18 02:20:59 -08005355const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005356 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005357 pp_reset,
5358 pp_getline,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005359 pp_cleanup,
H. Peter Anvinf7606612016-07-13 14:23:48 -07005360 pp_add_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005361 pp_pre_define,
5362 pp_pre_undefine,
5363 pp_pre_include,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005364 pp_include_path,
5365 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005366};