blob: 0853bed1e3873f89cd8a7164c4c93cc21ad6b3ad [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 Anvine2c80182005-01-15 22:15:51 +00002325 case PP_STACKSIZE:
2326 /* Directive to tell NASM what the default stack size is. The
2327 * default is for a 16-bit stack, and this can be overriden with
2328 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002329 */
2330 tline = tline->next;
2331 if (tline && tline->type == TOK_WHITESPACE)
2332 tline = tline->next;
2333 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002334 nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002335 free_tlist(origline);
2336 return DIRECTIVE_FOUND;
2337 }
2338 if (nasm_stricmp(tline->text, "flat") == 0) {
2339 /* All subsequent ARG directives are for a 32-bit stack */
2340 StackSize = 4;
2341 StackPointer = "ebp";
2342 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002343 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002344 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2345 /* All subsequent ARG directives are for a 64-bit stack */
2346 StackSize = 8;
2347 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002348 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002349 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002350 } else if (nasm_stricmp(tline->text, "large") == 0) {
2351 /* All subsequent ARG directives are for a 16-bit stack,
2352 * far function call.
2353 */
2354 StackSize = 2;
2355 StackPointer = "bp";
2356 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002357 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002358 } else if (nasm_stricmp(tline->text, "small") == 0) {
2359 /* All subsequent ARG directives are for a 16-bit stack,
2360 * far function call. We don't support near functions.
2361 */
2362 StackSize = 2;
2363 StackPointer = "bp";
2364 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002365 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002366 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002367 nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002368 free_tlist(origline);
2369 return DIRECTIVE_FOUND;
2370 }
2371 free_tlist(origline);
2372 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002373
H. Peter Anvine2c80182005-01-15 22:15:51 +00002374 case PP_ARG:
2375 /* TASM like ARG directive to define arguments to functions, in
2376 * the following form:
2377 *
2378 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2379 */
2380 offset = ArgOffset;
2381 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002382 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002383 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002384
H. Peter Anvine2c80182005-01-15 22:15:51 +00002385 /* Find the argument name */
2386 tline = tline->next;
2387 if (tline && tline->type == TOK_WHITESPACE)
2388 tline = tline->next;
2389 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002390 nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002391 free_tlist(origline);
2392 return DIRECTIVE_FOUND;
2393 }
2394 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002395
H. Peter Anvine2c80182005-01-15 22:15:51 +00002396 /* Find the argument size type */
2397 tline = tline->next;
2398 if (!tline || tline->type != TOK_OTHER
2399 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002400 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002401 "Syntax error processing `%%arg' directive");
2402 free_tlist(origline);
2403 return DIRECTIVE_FOUND;
2404 }
2405 tline = tline->next;
2406 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002407 nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002408 free_tlist(origline);
2409 return DIRECTIVE_FOUND;
2410 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002411
H. Peter Anvine2c80182005-01-15 22:15:51 +00002412 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002413 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002414 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002415 size = parse_size(tt->text);
2416 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002417 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002418 "Invalid size type for `%%arg' missing directive");
2419 free_tlist(tt);
2420 free_tlist(origline);
2421 return DIRECTIVE_FOUND;
2422 }
2423 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002424
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002425 /* Round up to even stack slots */
2426 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002427
H. Peter Anvine2c80182005-01-15 22:15:51 +00002428 /* Now define the macro for the argument */
2429 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2430 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002431 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002432 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002433
H. Peter Anvine2c80182005-01-15 22:15:51 +00002434 /* Move to the next argument in the list */
2435 tline = tline->next;
2436 if (tline && tline->type == TOK_WHITESPACE)
2437 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002438 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002439 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002440 free_tlist(origline);
2441 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002442
H. Peter Anvine2c80182005-01-15 22:15:51 +00002443 case PP_LOCAL:
2444 /* TASM like LOCAL directive to define local variables for a
2445 * function, in the following form:
2446 *
2447 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2448 *
2449 * The '= LocalSize' at the end is ignored by NASM, but is
2450 * required by TASM to define the local parameter size (and used
2451 * by the TASM macro package).
2452 */
2453 offset = LocalOffset;
2454 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002455 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002456 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002457
H. Peter Anvine2c80182005-01-15 22:15:51 +00002458 /* Find the argument name */
2459 tline = tline->next;
2460 if (tline && tline->type == TOK_WHITESPACE)
2461 tline = tline->next;
2462 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002463 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002464 "`%%local' missing argument parameter");
2465 free_tlist(origline);
2466 return DIRECTIVE_FOUND;
2467 }
2468 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002469
H. Peter Anvine2c80182005-01-15 22:15:51 +00002470 /* Find the argument size type */
2471 tline = tline->next;
2472 if (!tline || tline->type != TOK_OTHER
2473 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002474 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002475 "Syntax error processing `%%local' directive");
2476 free_tlist(origline);
2477 return DIRECTIVE_FOUND;
2478 }
2479 tline = tline->next;
2480 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002481 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002482 "`%%local' missing size type parameter");
2483 free_tlist(origline);
2484 return DIRECTIVE_FOUND;
2485 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002486
H. Peter Anvine2c80182005-01-15 22:15:51 +00002487 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002488 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002489 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002490 size = parse_size(tt->text);
2491 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002492 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002493 "Invalid size type for `%%local' missing directive");
2494 free_tlist(tt);
2495 free_tlist(origline);
2496 return DIRECTIVE_FOUND;
2497 }
2498 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002499
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002500 /* Round up to even stack slots */
2501 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002502
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002503 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002504
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002505 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002506 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2507 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002508 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002509
H. Peter Anvine2c80182005-01-15 22:15:51 +00002510 /* Now define the assign to setup the enter_c macro correctly */
2511 snprintf(directive, sizeof(directive),
2512 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002513 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002514
H. Peter Anvine2c80182005-01-15 22:15:51 +00002515 /* Move to the next argument in the list */
2516 tline = tline->next;
2517 if (tline && tline->type == TOK_WHITESPACE)
2518 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002519 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002520 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002521 free_tlist(origline);
2522 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002523
H. Peter Anvine2c80182005-01-15 22:15:51 +00002524 case PP_CLEAR:
2525 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002526 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002527 "trailing garbage after `%%clear' ignored");
2528 free_macros();
2529 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002530 free_tlist(origline);
2531 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002532
H. Peter Anvin418ca702008-05-30 10:42:30 -07002533 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002534 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002535 skip_white_(t);
2536 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002537 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002538 nasm_error(ERR_NONFATAL, "`%%depend' expects a file name");
H. Peter Anvin418ca702008-05-30 10:42:30 -07002539 free_tlist(origline);
2540 return DIRECTIVE_FOUND; /* but we did _something_ */
2541 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002542 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002543 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002544 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002545 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002546 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002547 nasm_unquote_cstr(p, i);
H. Peter Anvin436e3672016-10-04 01:12:28 -07002548 nasm_add_string_to_strlist(dephead, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002549 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002550 return DIRECTIVE_FOUND;
2551
2552 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002553 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002554 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002555
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002556 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002557 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002558 nasm_error(ERR_NONFATAL, "`%%include' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002559 free_tlist(origline);
2560 return DIRECTIVE_FOUND; /* but we did _something_ */
2561 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002562 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002563 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002564 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002565 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002566 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002567 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002568 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002569 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002570 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002571 found_path = NULL;
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002572 inc->fp = inc_fopen(p, dephead, &found_path,
H. Peter Anvind81a2352016-09-21 14:03:18 -07002573 pass == 0 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002574 if (!inc->fp) {
2575 /* -MG given but file not found */
2576 nasm_free(inc);
2577 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002578 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002579 inc->lineno = src_set_linnum(0);
2580 inc->lineinc = 1;
2581 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002582 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002583 istk = inc;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002584 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002585 }
2586 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002587 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002588
H. Peter Anvind2456592008-06-19 15:04:18 -07002589 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002590 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002591 static macros_t *use_pkg;
2592 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002593
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002594 tline = tline->next;
2595 skip_white_(tline);
2596 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002597
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002598 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002599 tline->type != TOK_INTERNAL_STRING &&
2600 tline->type != TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002601 nasm_error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002602 free_tlist(origline);
2603 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002604 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002605 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002606 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002607 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002608 if (tline->type == TOK_STRING)
2609 nasm_unquote_cstr(tline->text, i);
2610 use_pkg = nasm_stdmac_find_package(tline->text);
2611 if (!use_pkg)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002612 nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002613 else
2614 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002615 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002616 /* Not already included, go ahead and include it */
2617 stdmacpos = use_pkg;
2618 }
2619 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002620 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002621 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002622 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002623 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002624 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002625 tline = tline->next;
2626 skip_white_(tline);
2627 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002628 if (tline) {
2629 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002630 nasm_error(ERR_NONFATAL, "`%s' expects a context identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002631 pp_directives[i]);
2632 free_tlist(origline);
2633 return DIRECTIVE_FOUND; /* but we did _something_ */
2634 }
2635 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002636 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002637 "trailing garbage after `%s' ignored",
2638 pp_directives[i]);
2639 p = nasm_strdup(tline->text);
2640 } else {
2641 p = NULL; /* Anonymous */
2642 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002643
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002644 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002645 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002646 ctx->next = cstk;
2647 hash_init(&ctx->localmac, HASH_SMALL);
2648 ctx->name = p;
2649 ctx->number = unique++;
2650 cstk = ctx;
2651 } else {
2652 /* %pop or %repl */
2653 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002654 nasm_error(ERR_NONFATAL, "`%s': context stack is empty",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002655 pp_directives[i]);
2656 } else if (i == PP_POP) {
2657 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08002658 nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002659 "expected %s",
2660 cstk->name ? cstk->name : "anonymous", p);
2661 else
2662 ctx_pop();
2663 } else {
2664 /* i == PP_REPL */
2665 nasm_free(cstk->name);
2666 cstk->name = p;
2667 p = NULL;
2668 }
2669 nasm_free(p);
2670 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002671 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002672 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002673 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002674 severity = ERR_FATAL;
2675 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002676 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002677 severity = ERR_NONFATAL;
2678 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002679 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002680 severity = ERR_WARNING|ERR_WARN_USER;
2681 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002682
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002683issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002684 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002685 /* Only error out if this is the final pass */
2686 if (pass != 2 && i != PP_FATAL)
2687 return DIRECTIVE_FOUND;
2688
2689 tline->next = expand_smacro(tline->next);
2690 tline = tline->next;
2691 skip_white_(tline);
2692 t = tline ? tline->next : NULL;
2693 skip_white_(t);
2694 if (tok_type_(tline, TOK_STRING) && !t) {
2695 /* The line contains only a quoted string */
2696 p = tline->text;
2697 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002698 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002699 } else {
2700 /* Not a quoted string, or more than a quoted string */
2701 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002702 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002703 nasm_free(p);
2704 }
2705 free_tlist(origline);
2706 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002707 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002708
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002709 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002710 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002711 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002712 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002713 j = if_condition(tline->next, i);
2714 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002715 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002716 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002717 cond = nasm_malloc(sizeof(Cond));
2718 cond->next = istk->conds;
2719 cond->state = j;
2720 istk->conds = cond;
2721 if(istk->mstk)
2722 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002723 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002724 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002725
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002726 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002727 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002728 nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002729 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002730 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002731 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002732 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002733
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002734 case COND_DONE:
2735 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002736 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002737
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002738 case COND_ELSE_TRUE:
2739 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002740 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2741 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002742 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002743 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002744
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002745 case COND_IF_FALSE:
2746 /*
2747 * IMPORTANT: In the case of %if, we will already have
2748 * called expand_mmac_params(); however, if we're
2749 * processing an %elif we must have been in a
2750 * non-emitting mode, which would have inhibited
2751 * the normal invocation of expand_mmac_params().
2752 * Therefore, we have to do it explicitly here.
2753 */
2754 j = if_condition(expand_mmac_params(tline->next), i);
2755 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002756 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002757 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2758 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002759 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002760 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002761 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002762
H. Peter Anvine2c80182005-01-15 22:15:51 +00002763 case PP_ELSE:
2764 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002765 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2766 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002767 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002768 nasm_fatal(0, "`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002769 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002770 case COND_IF_TRUE:
2771 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002772 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002773 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002774
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002775 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002776 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002777
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002778 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002779 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002780 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002781
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002782 case COND_ELSE_TRUE:
2783 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002784 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002785 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002786 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002787 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002788 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002789 free_tlist(origline);
2790 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002791
H. Peter Anvine2c80182005-01-15 22:15:51 +00002792 case PP_ENDIF:
2793 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002794 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2795 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002796 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002797 nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002798 cond = istk->conds;
2799 istk->conds = cond->next;
2800 nasm_free(cond);
2801 if(istk->mstk)
2802 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002803 free_tlist(origline);
2804 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002805
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002806 case PP_RMACRO:
2807 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002808 case PP_MACRO:
2809 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002810 if (defining) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002811 nasm_error(ERR_FATAL, "`%s': already defining a macro",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002812 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002813 return DIRECTIVE_FOUND;
2814 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002815 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002816 defining->max_depth =
2817 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2818 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2819 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2820 nasm_free(defining);
2821 defining = NULL;
2822 return DIRECTIVE_FOUND;
2823 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002824
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002825 src_get(&defining->xline, &defining->fname);
2826
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002827 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2828 while (mmac) {
2829 if (!strcmp(mmac->name, defining->name) &&
2830 (mmac->nparam_min <= defining->nparam_max
2831 || defining->plus)
2832 && (defining->nparam_min <= mmac->nparam_max
2833 || mmac->plus)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002834 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002835 "redefining multi-line macro `%s'", defining->name);
2836 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002837 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002838 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002839 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002840 free_tlist(origline);
2841 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002842
H. Peter Anvine2c80182005-01-15 22:15:51 +00002843 case PP_ENDM:
2844 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002845 if (! (defining && defining->name)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002846 nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002847 return DIRECTIVE_FOUND;
2848 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002849 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2850 defining->next = *mmhead;
2851 *mmhead = defining;
2852 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002853 free_tlist(origline);
2854 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002855
H. Peter Anvin89cee572009-07-15 09:16:54 -04002856 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002857 /*
2858 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002859 * macro-end marker for a macro with a name. Then we
2860 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002861 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002862 list_for_each(l, istk->expansion)
2863 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002864 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002865
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002866 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002867 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002868 * Remove all conditional entries relative to this
2869 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002870 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002871 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2872 cond = istk->conds;
2873 istk->conds = cond->next;
2874 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002875 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002876 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002877 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002878 nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002879 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002880 free_tlist(origline);
2881 return DIRECTIVE_FOUND;
2882
H. Peter Anvina26433d2008-07-16 14:40:01 -07002883 case PP_UNMACRO:
2884 case PP_UNIMACRO:
2885 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002886 MMacro **mmac_p;
2887 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002888
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002889 spec.casesense = (i == PP_UNMACRO);
2890 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2891 return DIRECTIVE_FOUND;
2892 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002893 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2894 while (mmac_p && *mmac_p) {
2895 mmac = *mmac_p;
2896 if (mmac->casesense == spec.casesense &&
2897 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2898 mmac->nparam_min == spec.nparam_min &&
2899 mmac->nparam_max == spec.nparam_max &&
2900 mmac->plus == spec.plus) {
2901 *mmac_p = mmac->next;
2902 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002903 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002904 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002905 }
2906 }
2907 free_tlist(origline);
2908 free_tlist(spec.dlist);
2909 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002910 }
2911
H. Peter Anvine2c80182005-01-15 22:15:51 +00002912 case PP_ROTATE:
2913 if (tline->next && tline->next->type == TOK_WHITESPACE)
2914 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002915 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002916 free_tlist(origline);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002917 nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002918 return DIRECTIVE_FOUND;
2919 }
2920 t = expand_smacro(tline->next);
2921 tline->next = NULL;
2922 free_tlist(origline);
2923 tline = t;
2924 tptr = &t;
2925 tokval.t_type = TOKEN_INVALID;
2926 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002927 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002928 free_tlist(tline);
2929 if (!evalresult)
2930 return DIRECTIVE_FOUND;
2931 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002932 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002933 "trailing garbage after expression ignored");
2934 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002935 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002936 return DIRECTIVE_FOUND;
2937 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002938 mmac = istk->mstk;
2939 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2940 mmac = mmac->next_active;
2941 if (!mmac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002942 nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002943 } else if (mmac->nparam == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002944 nasm_error(ERR_NONFATAL,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002945 "`%%rotate' invoked within macro without parameters");
2946 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002947 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002948
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002949 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002950 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002951 rotate += mmac->nparam;
2952
2953 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002954 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002955 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002956
H. Peter Anvine2c80182005-01-15 22:15:51 +00002957 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002958 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002959 do {
2960 tline = tline->next;
2961 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002962
H. Peter Anvine2c80182005-01-15 22:15:51 +00002963 if (tok_type_(tline, TOK_ID) &&
2964 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002965 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002966 do {
2967 tline = tline->next;
2968 } while (tok_type_(tline, TOK_WHITESPACE));
2969 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002970
H. Peter Anvine2c80182005-01-15 22:15:51 +00002971 if (tline) {
2972 t = expand_smacro(tline);
2973 tptr = &t;
2974 tokval.t_type = TOKEN_INVALID;
2975 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002976 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002977 if (!evalresult) {
2978 free_tlist(origline);
2979 return DIRECTIVE_FOUND;
2980 }
2981 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002982 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002983 "trailing garbage after expression ignored");
2984 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002985 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002986 return DIRECTIVE_FOUND;
2987 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002988 count = reloc_value(evalresult);
2989 if (count >= REP_LIMIT) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002990 nasm_error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002991 count = 0;
2992 } else
2993 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002994 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002995 nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002996 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002997 }
2998 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002999
3000 tmp_defining = defining;
3001 defining = nasm_malloc(sizeof(MMacro));
3002 defining->prev = NULL;
3003 defining->name = NULL; /* flags this macro as a %rep block */
3004 defining->casesense = false;
3005 defining->plus = false;
3006 defining->nolist = nolist;
3007 defining->in_progress = count;
3008 defining->max_depth = 0;
3009 defining->nparam_min = defining->nparam_max = 0;
3010 defining->defaults = NULL;
3011 defining->dlist = NULL;
3012 defining->expansion = NULL;
3013 defining->next_active = istk->mstk;
3014 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003015 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003016
H. Peter Anvine2c80182005-01-15 22:15:51 +00003017 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003018 if (!defining || defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003019 nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003020 return DIRECTIVE_FOUND;
3021 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003022
H. Peter Anvine2c80182005-01-15 22:15:51 +00003023 /*
3024 * Now we have a "macro" defined - although it has no name
3025 * and we won't be entering it in the hash tables - we must
3026 * push a macro-end marker for it on to istk->expansion.
3027 * After that, it will take care of propagating itself (a
3028 * macro-end marker line for a macro which is really a %rep
3029 * block will cause the macro to be re-expanded, complete
3030 * with another macro-end marker to ensure the process
3031 * continues) until the whole expansion is forcibly removed
3032 * from istk->expansion by a %exitrep.
3033 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003034 l = nasm_malloc(sizeof(Line));
3035 l->next = istk->expansion;
3036 l->finishes = defining;
3037 l->first = NULL;
3038 istk->expansion = l;
3039
3040 istk->mstk = defining;
3041
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08003042 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003043 tmp_defining = defining;
3044 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003045 free_tlist(origline);
3046 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003047
H. Peter Anvine2c80182005-01-15 22:15:51 +00003048 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003049 /*
3050 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003051 * macro-end marker for a macro with no name. Then we set
3052 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003053 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003054 list_for_each(l, istk->expansion)
3055 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003056 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003057
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003058 if (l)
3059 l->finishes->in_progress = 1;
3060 else
H. Peter Anvin130736c2016-02-17 20:27:41 -08003061 nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003062 free_tlist(origline);
3063 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003064
H. Peter Anvine2c80182005-01-15 22:15:51 +00003065 case PP_XDEFINE:
3066 case PP_IXDEFINE:
3067 case PP_DEFINE:
3068 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003069 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003070
H. Peter Anvine2c80182005-01-15 22:15:51 +00003071 tline = tline->next;
3072 skip_white_(tline);
3073 tline = expand_id(tline);
3074 if (!tline || (tline->type != TOK_ID &&
3075 (tline->type != TOK_PREPROC_ID ||
3076 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003077 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003078 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003079 free_tlist(origline);
3080 return DIRECTIVE_FOUND;
3081 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003082
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003083 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003084 last = tline;
3085 param_start = tline = tline->next;
3086 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003087
H. Peter Anvine2c80182005-01-15 22:15:51 +00003088 /* Expand the macro definition now for %xdefine and %ixdefine */
3089 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3090 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003091
H. Peter Anvine2c80182005-01-15 22:15:51 +00003092 if (tok_is_(tline, "(")) {
3093 /*
3094 * This macro has parameters.
3095 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003096
H. Peter Anvine2c80182005-01-15 22:15:51 +00003097 tline = tline->next;
3098 while (1) {
3099 skip_white_(tline);
3100 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003101 nasm_error(ERR_NONFATAL, "parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003102 free_tlist(origline);
3103 return DIRECTIVE_FOUND;
3104 }
3105 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003106 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003107 "`%s': parameter identifier expected",
3108 tline->text);
3109 free_tlist(origline);
3110 return DIRECTIVE_FOUND;
3111 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003112 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003113 tline = tline->next;
3114 skip_white_(tline);
3115 if (tok_is_(tline, ",")) {
3116 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003117 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003118 if (!tok_is_(tline, ")")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003119 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003120 "`)' expected to terminate macro template");
3121 free_tlist(origline);
3122 return DIRECTIVE_FOUND;
3123 }
3124 break;
3125 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003126 }
3127 last = tline;
3128 tline = tline->next;
3129 }
3130 if (tok_type_(tline, TOK_WHITESPACE))
3131 last = tline, tline = tline->next;
3132 macro_start = NULL;
3133 last->next = NULL;
3134 t = tline;
3135 while (t) {
3136 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003137 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003138 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003139 !strcmp(tt->text, t->text))
3140 t->type = tt->type;
3141 }
3142 tt = t->next;
3143 t->next = macro_start;
3144 macro_start = t;
3145 t = tt;
3146 }
3147 /*
3148 * Good. We now have a macro name, a parameter count, and a
3149 * token list (in reverse order) for an expansion. We ought
3150 * to be OK just to create an SMacro, store it, and let
3151 * free_tlist have the rest of the line (which we have
3152 * carefully re-terminated after chopping off the expansion
3153 * from the end).
3154 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003155 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003156 free_tlist(origline);
3157 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003158
H. Peter Anvine2c80182005-01-15 22:15:51 +00003159 case PP_UNDEF:
3160 tline = tline->next;
3161 skip_white_(tline);
3162 tline = expand_id(tline);
3163 if (!tline || (tline->type != TOK_ID &&
3164 (tline->type != TOK_PREPROC_ID ||
3165 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003166 nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003167 free_tlist(origline);
3168 return DIRECTIVE_FOUND;
3169 }
3170 if (tline->next) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003171 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003172 "trailing garbage after macro name ignored");
3173 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003174
H. Peter Anvine2c80182005-01-15 22:15:51 +00003175 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003176 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003177 undef_smacro(ctx, mname);
3178 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003179 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003180
H. Peter Anvin9e200162008-06-04 17:23:14 -07003181 case PP_DEFSTR:
3182 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003183 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003184
3185 tline = tline->next;
3186 skip_white_(tline);
3187 tline = expand_id(tline);
3188 if (!tline || (tline->type != TOK_ID &&
3189 (tline->type != TOK_PREPROC_ID ||
3190 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003191 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003192 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003193 free_tlist(origline);
3194 return DIRECTIVE_FOUND;
3195 }
3196
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003197 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003198 last = tline;
3199 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003200 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003201
3202 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003203 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003204
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003205 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003206 macro_start = nasm_malloc(sizeof(*macro_start));
3207 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003208 macro_start->text = nasm_quote(p, strlen(p));
3209 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003210 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003211 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003212
3213 /*
3214 * We now have a macro name, an implicit parameter count of
3215 * zero, and a string token to use as an expansion. Create
3216 * and store an SMacro.
3217 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003218 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003219 free_tlist(origline);
3220 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003221
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003222 case PP_DEFTOK:
3223 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003224 casesense = (i == PP_DEFTOK);
3225
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003226 tline = tline->next;
3227 skip_white_(tline);
3228 tline = expand_id(tline);
3229 if (!tline || (tline->type != TOK_ID &&
3230 (tline->type != TOK_PREPROC_ID ||
3231 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003232 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003233 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003234 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003235 free_tlist(origline);
3236 return DIRECTIVE_FOUND;
3237 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003238 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003239 last = tline;
3240 tline = expand_smacro(tline->next);
3241 last->next = NULL;
3242
3243 t = tline;
3244 while (tok_type_(t, TOK_WHITESPACE))
3245 t = t->next;
3246 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003247 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003248 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003249 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003250 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003251 free_tlist(tline);
3252 free_tlist(origline);
3253 return DIRECTIVE_FOUND;
3254 }
3255
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003256 /*
3257 * Convert the string to a token stream. Note that smacros
3258 * are stored with the token stream reversed, so we have to
3259 * reverse the output of tokenize().
3260 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003261 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003262 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003263
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003264 /*
3265 * We now have a macro name, an implicit parameter count of
3266 * zero, and a numeric token to use as an expansion. Create
3267 * and store an SMacro.
3268 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003269 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003270 free_tlist(tline);
3271 free_tlist(origline);
3272 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003273
H. Peter Anvin418ca702008-05-30 10:42:30 -07003274 case PP_PATHSEARCH:
3275 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003276 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003277
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003278 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003279
3280 tline = tline->next;
3281 skip_white_(tline);
3282 tline = expand_id(tline);
3283 if (!tline || (tline->type != TOK_ID &&
3284 (tline->type != TOK_PREPROC_ID ||
3285 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003286 nasm_error(ERR_NONFATAL,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003287 "`%%pathsearch' expects a macro identifier as first parameter");
3288 free_tlist(origline);
3289 return DIRECTIVE_FOUND;
3290 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003291 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003292 last = tline;
3293 tline = expand_smacro(tline->next);
3294 last->next = NULL;
3295
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003296 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003297 while (tok_type_(t, TOK_WHITESPACE))
3298 t = t->next;
3299
3300 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003301 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003302 nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003303 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003304 free_tlist(origline);
3305 return DIRECTIVE_FOUND; /* but we did _something_ */
3306 }
3307 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003308 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003309 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003310 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003311 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003312 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003313
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003314 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003315 if (!found_path)
3316 found_path = p;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003317 macro_start = nasm_malloc(sizeof(*macro_start));
3318 macro_start->next = NULL;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003319 macro_start->text = nasm_quote(found_path, strlen(found_path));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003320 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003321 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003322
3323 /*
3324 * We now have a macro name, an implicit parameter count of
3325 * zero, and a string token to use as an expansion. Create
3326 * and store an SMacro.
3327 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003328 define_smacro(ctx, mname, casesense, 0, macro_start);
3329 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003330 free_tlist(origline);
3331 return DIRECTIVE_FOUND;
3332 }
3333
H. Peter Anvine2c80182005-01-15 22:15:51 +00003334 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003335 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003336
H. Peter Anvine2c80182005-01-15 22:15:51 +00003337 tline = tline->next;
3338 skip_white_(tline);
3339 tline = expand_id(tline);
3340 if (!tline || (tline->type != TOK_ID &&
3341 (tline->type != TOK_PREPROC_ID ||
3342 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003343 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003344 "`%%strlen' expects a macro identifier as first parameter");
3345 free_tlist(origline);
3346 return DIRECTIVE_FOUND;
3347 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003348 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003349 last = tline;
3350 tline = expand_smacro(tline->next);
3351 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003352
H. Peter Anvine2c80182005-01-15 22:15:51 +00003353 t = tline;
3354 while (tok_type_(t, TOK_WHITESPACE))
3355 t = t->next;
3356 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003357 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003358 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003359 "`%%strlen` requires string as second parameter");
3360 free_tlist(tline);
3361 free_tlist(origline);
3362 return DIRECTIVE_FOUND;
3363 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003364
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003365 macro_start = nasm_malloc(sizeof(*macro_start));
3366 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003367 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003368 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003369
H. Peter Anvine2c80182005-01-15 22:15:51 +00003370 /*
3371 * We now have a macro name, an implicit parameter count of
3372 * zero, and a numeric token to use as an expansion. Create
3373 * and store an SMacro.
3374 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003375 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003376 free_tlist(tline);
3377 free_tlist(origline);
3378 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003379
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003380 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003381 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003382
3383 tline = tline->next;
3384 skip_white_(tline);
3385 tline = expand_id(tline);
3386 if (!tline || (tline->type != TOK_ID &&
3387 (tline->type != TOK_PREPROC_ID ||
3388 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003389 nasm_error(ERR_NONFATAL,
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003390 "`%%strcat' expects a macro identifier as first parameter");
3391 free_tlist(origline);
3392 return DIRECTIVE_FOUND;
3393 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003394 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003395 last = tline;
3396 tline = expand_smacro(tline->next);
3397 last->next = NULL;
3398
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003399 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003400 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003401 switch (t->type) {
3402 case TOK_WHITESPACE:
3403 break;
3404 case TOK_STRING:
3405 len += t->a.len = nasm_unquote(t->text, NULL);
3406 break;
3407 case TOK_OTHER:
3408 if (!strcmp(t->text, ",")) /* permit comma separators */
3409 break;
3410 /* else fall through */
3411 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003412 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003413 "non-string passed to `%%strcat' (%d)", t->type);
3414 free_tlist(tline);
3415 free_tlist(origline);
3416 return DIRECTIVE_FOUND;
3417 }
3418 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003419
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003420 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003421 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003422 if (t->type == TOK_STRING) {
3423 memcpy(p, t->text, t->a.len);
3424 p += t->a.len;
3425 }
3426 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003427
3428 /*
3429 * We now have a macro name, an implicit parameter count of
3430 * zero, and a numeric token to use as an expansion. Create
3431 * and store an SMacro.
3432 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003433 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3434 macro_start->text = nasm_quote(pp, len);
3435 nasm_free(pp);
3436 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003437 free_tlist(tline);
3438 free_tlist(origline);
3439 return DIRECTIVE_FOUND;
3440
H. Peter Anvine2c80182005-01-15 22:15:51 +00003441 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003442 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003443 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003444 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003445
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003446 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003447
H. Peter Anvine2c80182005-01-15 22:15:51 +00003448 tline = tline->next;
3449 skip_white_(tline);
3450 tline = expand_id(tline);
3451 if (!tline || (tline->type != TOK_ID &&
3452 (tline->type != TOK_PREPROC_ID ||
3453 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003454 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003455 "`%%substr' expects a macro identifier as first parameter");
3456 free_tlist(origline);
3457 return DIRECTIVE_FOUND;
3458 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003459 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003460 last = tline;
3461 tline = expand_smacro(tline->next);
3462 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003463
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003464 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003465 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003466 while (tok_type_(t, TOK_WHITESPACE))
3467 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003468
H. Peter Anvine2c80182005-01-15 22:15:51 +00003469 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003470 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003471 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003472 "`%%substr` requires string as second parameter");
3473 free_tlist(tline);
3474 free_tlist(origline);
3475 return DIRECTIVE_FOUND;
3476 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003477
H. Peter Anvine2c80182005-01-15 22:15:51 +00003478 tt = t->next;
3479 tptr = &tt;
3480 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003481 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003482 if (!evalresult) {
3483 free_tlist(tline);
3484 free_tlist(origline);
3485 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003486 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003487 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003488 free_tlist(tline);
3489 free_tlist(origline);
3490 return DIRECTIVE_FOUND;
3491 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003492 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003493
3494 while (tok_type_(tt, TOK_WHITESPACE))
3495 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003496 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003497 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003498 } else {
3499 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003500 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003501 if (!evalresult) {
3502 free_tlist(tline);
3503 free_tlist(origline);
3504 return DIRECTIVE_FOUND;
3505 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003506 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003507 free_tlist(tline);
3508 free_tlist(origline);
3509 return DIRECTIVE_FOUND;
3510 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003511 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003512 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003513
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003514 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003515
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003516 /* make start and count being in range */
3517 if (start < 0)
3518 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003519 if (count < 0)
3520 count = len + count + 1 - start;
3521 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003522 count = len - start;
3523 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003524 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003525
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003526 macro_start = nasm_malloc(sizeof(*macro_start));
3527 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003528 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003529 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003530 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003531
H. Peter Anvine2c80182005-01-15 22:15:51 +00003532 /*
3533 * We now have a macro name, an implicit parameter count of
3534 * zero, and a numeric token to use as an expansion. Create
3535 * and store an SMacro.
3536 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003537 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003538 free_tlist(tline);
3539 free_tlist(origline);
3540 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003541 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003542
H. Peter Anvine2c80182005-01-15 22:15:51 +00003543 case PP_ASSIGN:
3544 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003545 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003546
H. Peter Anvine2c80182005-01-15 22:15:51 +00003547 tline = tline->next;
3548 skip_white_(tline);
3549 tline = expand_id(tline);
3550 if (!tline || (tline->type != TOK_ID &&
3551 (tline->type != TOK_PREPROC_ID ||
3552 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003553 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003554 "`%%%sassign' expects a macro identifier",
3555 (i == PP_IASSIGN ? "i" : ""));
3556 free_tlist(origline);
3557 return DIRECTIVE_FOUND;
3558 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003559 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003560 last = tline;
3561 tline = expand_smacro(tline->next);
3562 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003563
H. Peter Anvine2c80182005-01-15 22:15:51 +00003564 t = tline;
3565 tptr = &t;
3566 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003567 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003568 free_tlist(tline);
3569 if (!evalresult) {
3570 free_tlist(origline);
3571 return DIRECTIVE_FOUND;
3572 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003573
H. Peter Anvine2c80182005-01-15 22:15:51 +00003574 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003575 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003576 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003577
H. Peter Anvine2c80182005-01-15 22:15:51 +00003578 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003579 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003580 "non-constant value given to `%%%sassign'",
3581 (i == PP_IASSIGN ? "i" : ""));
3582 free_tlist(origline);
3583 return DIRECTIVE_FOUND;
3584 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003585
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003586 macro_start = nasm_malloc(sizeof(*macro_start));
3587 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003588 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003589 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003590
H. Peter Anvine2c80182005-01-15 22:15:51 +00003591 /*
3592 * We now have a macro name, an implicit parameter count of
3593 * zero, and a numeric token to use as an expansion. Create
3594 * and store an SMacro.
3595 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003596 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003597 free_tlist(origline);
3598 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003599
H. Peter Anvine2c80182005-01-15 22:15:51 +00003600 case PP_LINE:
3601 /*
3602 * Syntax is `%line nnn[+mmm] [filename]'
3603 */
3604 tline = tline->next;
3605 skip_white_(tline);
3606 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003607 nasm_error(ERR_NONFATAL, "`%%line' expects line number");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003608 free_tlist(origline);
3609 return DIRECTIVE_FOUND;
3610 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003611 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003612 m = 1;
3613 tline = tline->next;
3614 if (tok_is_(tline, "+")) {
3615 tline = tline->next;
3616 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003617 nasm_error(ERR_NONFATAL, "`%%line' expects line increment");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003618 free_tlist(origline);
3619 return DIRECTIVE_FOUND;
3620 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003621 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003622 tline = tline->next;
3623 }
3624 skip_white_(tline);
3625 src_set_linnum(k);
3626 istk->lineinc = m;
3627 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003628 char *fname = detoken(tline, false);
3629 src_set_fname(fname);
3630 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003631 }
3632 free_tlist(origline);
3633 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003634
H. Peter Anvine2c80182005-01-15 22:15:51 +00003635 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003636 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003637 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003638 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003639 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003640 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003641}
3642
3643/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003644 * Ensure that a macro parameter contains a condition code and
3645 * nothing else. Return the condition code index if so, or -1
3646 * otherwise.
3647 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003648static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003649{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003650 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003651
H. Peter Anvin25a99342007-09-22 17:45:45 -07003652 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003653 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003654
H. Peter Anvineba20a72002-04-30 20:53:55 +00003655 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003656 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003657 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003658 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003659 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003660 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003661 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003662
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003663 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003664}
3665
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003666/*
3667 * This routines walks over tokens strem and hadnles tokens
3668 * pasting, if @handle_explicit passed then explicit pasting
3669 * term is handled, otherwise -- implicit pastings only.
3670 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003671static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003672 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003673{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003674 Token *tok, *next, **prev_next, **prev_nonspace;
3675 bool pasted = false;
3676 char *buf, *p;
3677 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003678
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003679 /*
3680 * The last token before pasting. We need it
3681 * to be able to connect new handled tokens.
3682 * In other words if there were a tokens stream
3683 *
3684 * A -> B -> C -> D
3685 *
3686 * and we've joined tokens B and C, the resulting
3687 * stream should be
3688 *
3689 * A -> BC -> D
3690 */
3691 tok = *head;
3692 prev_next = NULL;
3693
3694 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3695 prev_nonspace = head;
3696 else
3697 prev_nonspace = NULL;
3698
3699 while (tok && (next = tok->next)) {
3700
3701 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003702 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003703 /* Zap redundant whitespaces */
3704 while (tok_type_(next, TOK_WHITESPACE))
3705 next = delete_Token(next);
3706 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003707 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003708
3709 case TOK_PASTE:
3710 /* Explicit pasting */
3711 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003712 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003713 next = delete_Token(tok);
3714
3715 while (tok_type_(next, TOK_WHITESPACE))
3716 next = delete_Token(next);
3717
3718 if (!pasted)
3719 pasted = true;
3720
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003721 /* Left pasting token is start of line */
3722 if (!prev_nonspace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003723 nasm_error(ERR_FATAL, "No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003724
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003725 /*
3726 * No ending token, this might happen in two
3727 * cases
3728 *
3729 * 1) There indeed no right token at all
3730 * 2) There is a bare "%define ID" statement,
3731 * and @ID does expand to whitespace.
3732 *
3733 * So technically we need to do a grammar analysis
3734 * in another stage of parsing, but for now lets don't
3735 * change the behaviour people used to. Simply allow
3736 * whitespace after paste token.
3737 */
3738 if (!next) {
3739 /*
3740 * Zap ending space tokens and that's all.
3741 */
3742 tok = (*prev_nonspace)->next;
3743 while (tok_type_(tok, TOK_WHITESPACE))
3744 tok = delete_Token(tok);
3745 tok = *prev_nonspace;
3746 tok->next = NULL;
3747 break;
3748 }
3749
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003750 tok = *prev_nonspace;
3751 while (tok_type_(tok, TOK_WHITESPACE))
3752 tok = delete_Token(tok);
3753 len = strlen(tok->text);
3754 len += strlen(next->text);
3755
3756 p = buf = nasm_malloc(len + 1);
3757 strcpy(p, tok->text);
3758 p = strchr(p, '\0');
3759 strcpy(p, next->text);
3760
3761 delete_Token(tok);
3762
3763 tok = tokenize(buf);
3764 nasm_free(buf);
3765
3766 *prev_nonspace = tok;
3767 while (tok && tok->next)
3768 tok = tok->next;
3769
3770 tok->next = delete_Token(next);
3771
3772 /* Restart from pasted tokens head */
3773 tok = *prev_nonspace;
3774 break;
3775
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003776 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003777 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003778 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003779 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3780 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003781
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003782 len = 0;
3783 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3784 len += strlen(next->text);
3785 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003786 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003787
3788 /* No match */
3789 if (tok == next)
3790 break;
3791
3792 len += strlen(tok->text);
3793 p = buf = nasm_malloc(len + 1);
3794
3795 while (tok != next) {
3796 strcpy(p, tok->text);
3797 p = strchr(p, '\0');
3798 tok = delete_Token(tok);
3799 }
3800
3801 tok = tokenize(buf);
3802 nasm_free(buf);
3803
3804 if (prev_next)
3805 *prev_next = tok;
3806 else
3807 *head = tok;
3808
3809 /*
3810 * Connect pasted into original stream,
3811 * ie A -> new-tokens -> B
3812 */
3813 while (tok && tok->next)
3814 tok = tok->next;
3815 tok->next = next;
3816
3817 if (!pasted)
3818 pasted = true;
3819
3820 /* Restart from pasted tokens head */
3821 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003822 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003823
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003824 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003825 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003826
3827 prev_next = &tok->next;
3828
3829 if (tok->next &&
3830 !tok_type_(tok->next, TOK_WHITESPACE) &&
3831 !tok_type_(tok->next, TOK_PASTE))
3832 prev_nonspace = prev_next;
3833
3834 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003835 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003836
3837 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003838}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003839
3840/*
3841 * expands to a list of tokens from %{x:y}
3842 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003843static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003844{
3845 Token *t = tline, **tt, *tm, *head;
3846 char *pos;
3847 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003848
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003849 pos = strchr(tline->text, ':');
3850 nasm_assert(pos);
3851
3852 lst = atoi(pos + 1);
3853 fst = atoi(tline->text + 1);
3854
3855 /*
3856 * only macros params are accounted so
3857 * if someone passes %0 -- we reject such
3858 * value(s)
3859 */
3860 if (lst == 0 || fst == 0)
3861 goto err;
3862
3863 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003864 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3865 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003866 goto err;
3867
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003868 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3869 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003870
3871 /* counted from zero */
3872 fst--, lst--;
3873
3874 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003875 * It will be at least one token. Note we
3876 * need to scan params until separator, otherwise
3877 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003878 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003879 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003880 head = new_Token(NULL, tm->type, tm->text, 0);
3881 tt = &head->next, tm = tm->next;
3882 while (tok_isnt_(tm, ",")) {
3883 t = new_Token(NULL, tm->type, tm->text, 0);
3884 *tt = t, tt = &t->next, tm = tm->next;
3885 }
3886
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003887 if (fst < lst) {
3888 for (i = fst + 1; i <= lst; i++) {
3889 t = new_Token(NULL, TOK_OTHER, ",", 0);
3890 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003891 j = (i + mac->rotate) % mac->nparam;
3892 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003893 while (tok_isnt_(tm, ",")) {
3894 t = new_Token(NULL, tm->type, tm->text, 0);
3895 *tt = t, tt = &t->next, tm = tm->next;
3896 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003897 }
3898 } else {
3899 for (i = fst - 1; i >= lst; i--) {
3900 t = new_Token(NULL, TOK_OTHER, ",", 0);
3901 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003902 j = (i + mac->rotate) % mac->nparam;
3903 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003904 while (tok_isnt_(tm, ",")) {
3905 t = new_Token(NULL, tm->type, tm->text, 0);
3906 *tt = t, tt = &t->next, tm = tm->next;
3907 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003908 }
3909 }
3910
3911 *last = tt;
3912 return head;
3913
3914err:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003915 nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003916 &tline->text[1]);
3917 return tline;
3918}
3919
H. Peter Anvin76690a12002-04-30 20:52:49 +00003920/*
3921 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003922 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003923 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003924 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003925static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003926{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003927 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003928 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003929 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003930
3931 tail = &thead;
3932 thead = NULL;
3933
H. Peter Anvine2c80182005-01-15 22:15:51 +00003934 while (tline) {
3935 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003936 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3937 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3938 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003939 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003940 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003941 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003942 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003943 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003944 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003945
H. Peter Anvine2c80182005-01-15 22:15:51 +00003946 t = tline;
3947 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003948
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003949 mac = istk->mstk;
3950 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3951 mac = mac->next_active;
3952 if (!mac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003953 nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003954 } else {
3955 pos = strchr(t->text, ':');
3956 if (!pos) {
3957 switch (t->text[1]) {
3958 /*
3959 * We have to make a substitution of one of the
3960 * forms %1, %-1, %+1, %%foo, %0.
3961 */
3962 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003963 type = TOK_NUMBER;
3964 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3965 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003966 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003967 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003968 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003969 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003970 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003971 text = nasm_strcat(tmpbuf, t->text + 2);
3972 break;
3973 case '-':
3974 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003975 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003976 tt = NULL;
3977 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003978 if (mac->nparam > 1)
3979 n = (n + mac->rotate) % mac->nparam;
3980 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003981 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003982 cc = find_cc(tt);
3983 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003984 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003985 "macro parameter %d is not a condition code",
3986 n + 1);
3987 text = NULL;
3988 } else {
3989 type = TOK_ID;
3990 if (inverse_ccs[cc] == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003991 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003992 "condition code `%s' is not invertible",
3993 conditions[cc]);
3994 text = NULL;
3995 } else
3996 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3997 }
3998 break;
3999 case '+':
4000 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004001 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004002 tt = NULL;
4003 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004004 if (mac->nparam > 1)
4005 n = (n + mac->rotate) % mac->nparam;
4006 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004007 }
4008 cc = find_cc(tt);
4009 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004010 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004011 "macro parameter %d is not a condition code",
4012 n + 1);
4013 text = NULL;
4014 } else {
4015 type = TOK_ID;
4016 text = nasm_strdup(conditions[cc]);
4017 }
4018 break;
4019 default:
4020 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004021 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004022 tt = NULL;
4023 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004024 if (mac->nparam > 1)
4025 n = (n + mac->rotate) % mac->nparam;
4026 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004027 }
4028 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004029 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004030 *tail = new_Token(NULL, tt->type, tt->text, 0);
4031 tail = &(*tail)->next;
4032 tt = tt->next;
4033 }
4034 }
4035 text = NULL; /* we've done it here */
4036 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004037 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004038 } else {
4039 /*
4040 * seems we have a parameters range here
4041 */
4042 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004043 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004044 if (head != t) {
4045 *tail = head;
4046 *last = tline;
4047 tline = head;
4048 text = NULL;
4049 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004050 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004051 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004052 if (!text) {
4053 delete_Token(t);
4054 } else {
4055 *tail = t;
4056 tail = &t->next;
4057 t->type = type;
4058 nasm_free(t->text);
4059 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004060 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004061 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004062 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004063 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004064 } else if (tline->type == TOK_INDIRECT) {
4065 t = tline;
4066 tline = tline->next;
4067 tt = tokenize(t->text);
4068 tt = expand_mmac_params(tt);
4069 tt = expand_smacro(tt);
4070 *tail = tt;
4071 while (tt) {
4072 tt->a.mac = NULL; /* Necessary? */
4073 tail = &tt->next;
4074 tt = tt->next;
4075 }
4076 delete_Token(t);
4077 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004078 } else {
4079 t = *tail = tline;
4080 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004081 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004082 tail = &t->next;
4083 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004084 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004085 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004086
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004087 if (changed) {
4088 const struct tokseq_match t[] = {
4089 {
4090 PP_CONCAT_MASK(TOK_ID) |
4091 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4092 PP_CONCAT_MASK(TOK_ID) |
4093 PP_CONCAT_MASK(TOK_NUMBER) |
4094 PP_CONCAT_MASK(TOK_FLOAT) |
4095 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4096 },
4097 {
4098 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4099 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4100 }
4101 };
4102 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4103 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004104
H. Peter Anvin76690a12002-04-30 20:52:49 +00004105 return thead;
4106}
4107
4108/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004109 * Expand all single-line macro calls made in the given line.
4110 * Return the expanded version of the line. The original is deemed
4111 * to be destroyed in the process. (In reality we'll just move
4112 * Tokens from input to output a lot of the time, rather than
4113 * actually bothering to destroy and replicate.)
4114 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004115
H. Peter Anvine2c80182005-01-15 22:15:51 +00004116static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004117{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004118 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004119 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004120 Token **params;
4121 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004122 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004123 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004124 Token *org_tline = tline;
4125 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004126 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004127 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004128 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004129
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004130 /*
4131 * Trick: we should avoid changing the start token pointer since it can
4132 * be contained in "next" field of other token. Because of this
4133 * we allocate a copy of first token and work with it; at the end of
4134 * routine we copy it back
4135 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004136 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004137 tline = new_Token(org_tline->next, org_tline->type,
4138 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004139 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004140 nasm_free(org_tline->text);
4141 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004142 }
4143
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004144 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004145
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004146again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004147 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004148 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004149
H. Peter Anvine2c80182005-01-15 22:15:51 +00004150 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004151 if (!--deadman) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004152 nasm_error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004153 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004154 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004155
H. Peter Anvine2c80182005-01-15 22:15:51 +00004156 if ((mname = tline->text)) {
4157 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004158 if (tline->type == TOK_ID) {
4159 head = (SMacro *)hash_findix(&smacros, mname);
4160 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004161 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004162 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4163 } else
4164 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004165
H. Peter Anvine2c80182005-01-15 22:15:51 +00004166 /*
4167 * We've hit an identifier. As in is_mmacro below, we first
4168 * check whether the identifier is a single-line macro at
4169 * all, then think about checking for parameters if
4170 * necessary.
4171 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004172 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004173 if (!mstrcmp(m->name, mname, m->casesense))
4174 break;
4175 if (m) {
4176 mstart = tline;
4177 params = NULL;
4178 paramsize = NULL;
4179 if (m->nparam == 0) {
4180 /*
4181 * Simple case: the macro is parameterless. Discard the
4182 * one token that the macro call took, and push the
4183 * expansion back on the to-do stack.
4184 */
4185 if (!m->expansion) {
4186 if (!strcmp("__FILE__", m->name)) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004187 const char *file = src_get_fname();
4188 /* nasm_free(tline->text); here? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004189 tline->text = nasm_quote(file, strlen(file));
4190 tline->type = TOK_STRING;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004191 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004192 }
4193 if (!strcmp("__LINE__", m->name)) {
4194 nasm_free(tline->text);
4195 make_tok_num(tline, src_get_linnum());
4196 continue;
4197 }
4198 if (!strcmp("__BITS__", m->name)) {
4199 nasm_free(tline->text);
4200 make_tok_num(tline, globalbits);
4201 continue;
4202 }
4203 tline = delete_Token(tline);
4204 continue;
4205 }
4206 } else {
4207 /*
4208 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004209 * exists and takes parameters. We must find the
4210 * parameters in the call, count them, find the SMacro
4211 * that corresponds to that form of the macro call, and
4212 * substitute for the parameters when we expand. What a
4213 * pain.
4214 */
4215 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004216 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004217 do {
4218 t = tline->next;
4219 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004220 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004221 t->text = NULL;
4222 t = tline->next = delete_Token(t);
4223 }
4224 tline = t;
4225 } while (tok_type_(tline, TOK_WHITESPACE));
4226 if (!tok_is_(tline, "(")) {
4227 /*
4228 * This macro wasn't called with parameters: ignore
4229 * the call. (Behaviour borrowed from gnu cpp.)
4230 */
4231 tline = mstart;
4232 m = NULL;
4233 } else {
4234 int paren = 0;
4235 int white = 0;
4236 brackets = 0;
4237 nparam = 0;
4238 sparam = PARAM_DELTA;
4239 params = nasm_malloc(sparam * sizeof(Token *));
4240 params[0] = tline->next;
4241 paramsize = nasm_malloc(sparam * sizeof(int));
4242 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004243 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004244 /*
4245 * For some unusual expansions
4246 * which concatenates function call
4247 */
4248 t = tline->next;
4249 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004250 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004251 t->text = NULL;
4252 t = tline->next = delete_Token(t);
4253 }
4254 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004255
H. Peter Anvine2c80182005-01-15 22:15:51 +00004256 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004257 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004258 "macro call expects terminating `)'");
4259 break;
4260 }
4261 if (tline->type == TOK_WHITESPACE
4262 && brackets <= 0) {
4263 if (paramsize[nparam])
4264 white++;
4265 else
4266 params[nparam] = tline->next;
4267 continue; /* parameter loop */
4268 }
4269 if (tline->type == TOK_OTHER
4270 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004271 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004272 if (ch == ',' && !paren && brackets <= 0) {
4273 if (++nparam >= sparam) {
4274 sparam += PARAM_DELTA;
4275 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004276 sparam * sizeof(Token *));
4277 paramsize = nasm_realloc(paramsize,
4278 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004279 }
4280 params[nparam] = tline->next;
4281 paramsize[nparam] = 0;
4282 white = 0;
4283 continue; /* parameter loop */
4284 }
4285 if (ch == '{' &&
4286 (brackets > 0 || (brackets == 0 &&
4287 !paramsize[nparam])))
4288 {
4289 if (!(brackets++)) {
4290 params[nparam] = tline->next;
4291 continue; /* parameter loop */
4292 }
4293 }
4294 if (ch == '}' && brackets > 0)
4295 if (--brackets == 0) {
4296 brackets = -1;
4297 continue; /* parameter loop */
4298 }
4299 if (ch == '(' && !brackets)
4300 paren++;
4301 if (ch == ')' && brackets <= 0)
4302 if (--paren < 0)
4303 break;
4304 }
4305 if (brackets < 0) {
4306 brackets = 0;
H. Peter Anvin130736c2016-02-17 20:27:41 -08004307 nasm_error(ERR_NONFATAL, "braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004308 "enclose all of macro parameter");
4309 }
4310 paramsize[nparam] += white + 1;
4311 white = 0;
4312 } /* parameter loop */
4313 nparam++;
4314 while (m && (m->nparam != nparam ||
4315 mstrcmp(m->name, mname,
4316 m->casesense)))
4317 m = m->next;
4318 if (!m)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004319 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004320 "macro `%s' exists, "
4321 "but not taking %d parameters",
4322 mstart->text, nparam);
4323 }
4324 }
4325 if (m && m->in_progress)
4326 m = NULL;
4327 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004328 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004329 * Design question: should we handle !tline, which
4330 * indicates missing ')' here, or expand those
4331 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004332 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004333 */
4334 nasm_free(params);
4335 nasm_free(paramsize);
4336 tline = mstart;
4337 } else {
4338 /*
4339 * Expand the macro: we are placed on the last token of the
4340 * call, so that we can easily split the call from the
4341 * following tokens. We also start by pushing an SMAC_END
4342 * token for the cycle removal.
4343 */
4344 t = tline;
4345 if (t) {
4346 tline = t->next;
4347 t->next = NULL;
4348 }
4349 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004350 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004351 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004352 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004353 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004354 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004355 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004356 Token *ttt, *pt;
4357 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004358
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004359 ttt = params[t->type - TOK_SMAC_PARAM];
4360 i = paramsize[t->type - TOK_SMAC_PARAM];
4361 while (--i >= 0) {
4362 pt = *ptail = new_Token(tline, ttt->type,
4363 ttt->text, 0);
4364 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004365 ttt = ttt->next;
4366 }
4367 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004368 } else if (t->type == TOK_PREPROC_Q) {
4369 tt = new_Token(tline, TOK_ID, mname, 0);
4370 tline = tt;
4371 } else if (t->type == TOK_PREPROC_QQ) {
4372 tt = new_Token(tline, TOK_ID, m->name, 0);
4373 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004374 } else {
4375 tt = new_Token(tline, t->type, t->text, 0);
4376 tline = tt;
4377 }
4378 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004379
H. Peter Anvine2c80182005-01-15 22:15:51 +00004380 /*
4381 * Having done that, get rid of the macro call, and clean
4382 * up the parameters.
4383 */
4384 nasm_free(params);
4385 nasm_free(paramsize);
4386 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004387 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004388 continue; /* main token loop */
4389 }
4390 }
4391 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004392
H. Peter Anvine2c80182005-01-15 22:15:51 +00004393 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004394 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004395 tline = delete_Token(tline);
4396 } else {
4397 t = *tail = tline;
4398 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004399 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004400 t->next = NULL;
4401 tail = &t->next;
4402 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004403 }
4404
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004405 /*
4406 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004407 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004408 * TOK_IDs should be concatenated.
4409 * Also we look for %+ tokens and concatenate the tokens before and after
4410 * them (without white spaces in between).
4411 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004412 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004413 const struct tokseq_match t[] = {
4414 {
4415 PP_CONCAT_MASK(TOK_ID) |
4416 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4417 PP_CONCAT_MASK(TOK_ID) |
4418 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4419 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4420 }
4421 };
4422 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004423 /*
4424 * If we concatenated something, *and* we had previously expanded
4425 * an actual macro, scan the lines again for macros...
4426 */
4427 tline = thead;
4428 expanded = false;
4429 goto again;
4430 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004431 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004432
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004433err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004434 if (org_tline) {
4435 if (thead) {
4436 *org_tline = *thead;
4437 /* since we just gave text to org_line, don't free it */
4438 thead->text = NULL;
4439 delete_Token(thead);
4440 } else {
4441 /* the expression expanded to empty line;
4442 we can't return NULL for some reasons
4443 we just set the line to a single WHITESPACE token. */
4444 memset(org_tline, 0, sizeof(*org_tline));
4445 org_tline->text = NULL;
4446 org_tline->type = TOK_WHITESPACE;
4447 }
4448 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004449 }
4450
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004451 return thead;
4452}
4453
4454/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004455 * Similar to expand_smacro but used exclusively with macro identifiers
4456 * right before they are fetched in. The reason is that there can be
4457 * identifiers consisting of several subparts. We consider that if there
4458 * are more than one element forming the name, user wants a expansion,
4459 * otherwise it will be left as-is. Example:
4460 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004461 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004462 *
4463 * the identifier %$abc will be left as-is so that the handler for %define
4464 * will suck it and define the corresponding value. Other case:
4465 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004466 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004467 *
4468 * In this case user wants name to be expanded *before* %define starts
4469 * working, so we'll expand %$abc into something (if it has a value;
4470 * otherwise it will be left as-is) then concatenate all successive
4471 * PP_IDs into one.
4472 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004473static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004474{
4475 Token *cur, *oldnext = NULL;
4476
H. Peter Anvin734b1882002-04-30 21:01:08 +00004477 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004478 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004479
4480 cur = tline;
4481 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004482 (cur->next->type == TOK_ID ||
4483 cur->next->type == TOK_PREPROC_ID
4484 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004485 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004486
4487 /* If identifier consists of just one token, don't expand */
4488 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004489 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004490
H. Peter Anvine2c80182005-01-15 22:15:51 +00004491 if (cur) {
4492 oldnext = cur->next; /* Detach the tail past identifier */
4493 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004494 }
4495
H. Peter Anvin734b1882002-04-30 21:01:08 +00004496 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004497
H. Peter Anvine2c80182005-01-15 22:15:51 +00004498 if (cur) {
4499 /* expand_smacro possibly changhed tline; re-scan for EOL */
4500 cur = tline;
4501 while (cur && cur->next)
4502 cur = cur->next;
4503 if (cur)
4504 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004505 }
4506
4507 return tline;
4508}
4509
4510/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004511 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004512 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004513 * to check for an initial label - that's taken care of in
4514 * expand_mmacro - but must check numbers of parameters. Guaranteed
4515 * to be called with tline->type == TOK_ID, so the putative macro
4516 * name is easy to find.
4517 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004518static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004519{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004520 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004521 Token **params;
4522 int nparam;
4523
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004524 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004525
4526 /*
4527 * Efficiency: first we see if any macro exists with the given
4528 * name. If not, we can return NULL immediately. _Then_ we
4529 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004530 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004531 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004532 list_for_each(m, head)
4533 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004534 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004535 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004536 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004537
4538 /*
4539 * OK, we have a potential macro. Count and demarcate the
4540 * parameters.
4541 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004542 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004543
4544 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004545 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004546 * structure that handles this number.
4547 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004548 while (m) {
4549 if (m->nparam_min <= nparam
4550 && (m->plus || nparam <= m->nparam_max)) {
4551 /*
4552 * This one is right. Just check if cycle removal
4553 * prohibits us using it before we actually celebrate...
4554 */
4555 if (m->in_progress > m->max_depth) {
4556 if (m->max_depth > 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004557 nasm_error(ERR_WARNING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004558 "reached maximum recursion depth of %i",
4559 m->max_depth);
4560 }
4561 nasm_free(params);
4562 return NULL;
4563 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004564 /*
4565 * It's right, and we can use it. Add its default
4566 * parameters to the end of our list if necessary.
4567 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004568 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004569 params =
4570 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004571 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004572 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004573 while (nparam < m->nparam_min + m->ndefs) {
4574 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004575 nparam++;
4576 }
4577 }
4578 /*
4579 * If we've gone over the maximum parameter count (and
4580 * we're in Plus mode), ignore parameters beyond
4581 * nparam_max.
4582 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004583 if (m->plus && nparam > m->nparam_max)
4584 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004585 /*
4586 * Then terminate the parameter list, and leave.
4587 */
4588 if (!params) { /* need this special case */
4589 params = nasm_malloc(sizeof(*params));
4590 nparam = 0;
4591 }
4592 params[nparam] = NULL;
4593 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004594 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004595 }
4596 /*
4597 * This one wasn't right: look for the next one with the
4598 * same name.
4599 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004600 list_for_each(m, m->next)
4601 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004602 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004603 }
4604
4605 /*
4606 * After all that, we didn't find one with the right number of
4607 * parameters. Issue a warning, and fail to expand the macro.
4608 */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004609 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004610 "macro `%s' exists, but not taking %d parameters",
4611 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004612 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004613 return NULL;
4614}
4615
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004616
4617/*
4618 * Save MMacro invocation specific fields in
4619 * preparation for a recursive macro expansion
4620 */
4621static void push_mmacro(MMacro *m)
4622{
4623 MMacroInvocation *i;
4624
4625 i = nasm_malloc(sizeof(MMacroInvocation));
4626 i->prev = m->prev;
4627 i->params = m->params;
4628 i->iline = m->iline;
4629 i->nparam = m->nparam;
4630 i->rotate = m->rotate;
4631 i->paramlen = m->paramlen;
4632 i->unique = m->unique;
4633 i->condcnt = m->condcnt;
4634 m->prev = i;
4635}
4636
4637
4638/*
4639 * Restore MMacro invocation specific fields that were
4640 * saved during a previous recursive macro expansion
4641 */
4642static void pop_mmacro(MMacro *m)
4643{
4644 MMacroInvocation *i;
4645
4646 if (m->prev) {
4647 i = m->prev;
4648 m->prev = i->prev;
4649 m->params = i->params;
4650 m->iline = i->iline;
4651 m->nparam = i->nparam;
4652 m->rotate = i->rotate;
4653 m->paramlen = i->paramlen;
4654 m->unique = i->unique;
4655 m->condcnt = i->condcnt;
4656 nasm_free(i);
4657 }
4658}
4659
4660
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004661/*
4662 * Expand the multi-line macro call made by the given line, if
4663 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004664 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004665 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004666static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004667{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004668 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004669 Token *label = NULL;
4670 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004671 Token **params, *t, *tt;
4672 MMacro *m;
4673 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004674 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004675 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004676
4677 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004678 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004679 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004680 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004681 return 0;
4682 m = is_mmacro(t, &params);
4683 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004684 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004685 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004686 Token *last;
4687 /*
4688 * We have an id which isn't a macro call. We'll assume
4689 * it might be a label; we'll also check to see if a
4690 * colon follows it. Then, if there's another id after
4691 * that lot, we'll check it again for macro-hood.
4692 */
4693 label = last = t;
4694 t = t->next;
4695 if (tok_type_(t, TOK_WHITESPACE))
4696 last = t, t = t->next;
4697 if (tok_is_(t, ":")) {
4698 dont_prepend = 1;
4699 last = t, t = t->next;
4700 if (tok_type_(t, TOK_WHITESPACE))
4701 last = t, t = t->next;
4702 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004703 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4704 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004705 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004706 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004707 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004708 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004709
4710 /*
4711 * Fix up the parameters: this involves stripping leading and
4712 * trailing whitespace, then stripping braces if they are
4713 * present.
4714 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004715 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004716 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004717
H. Peter Anvine2c80182005-01-15 22:15:51 +00004718 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004719 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004720 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004721
H. Peter Anvine2c80182005-01-15 22:15:51 +00004722 t = params[i];
4723 skip_white_(t);
4724 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004725 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004726 params[i] = t;
4727 paramlen[i] = 0;
4728 while (t) {
4729 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4730 break; /* ... because we have hit a comma */
4731 if (comma && t->type == TOK_WHITESPACE
4732 && tok_is_(t->next, ","))
4733 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004734 if (brace && t->type == TOK_OTHER) {
4735 if (t->text[0] == '{')
4736 brace++; /* ... or a nested opening brace */
4737 else if (t->text[0] == '}')
4738 if (!--brace)
4739 break; /* ... or a brace */
4740 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004741 t = t->next;
4742 paramlen[i]++;
4743 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004744 if (brace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004745 nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004746 }
4747
4748 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004749 * OK, we have a MMacro structure together with a set of
4750 * parameters. We must now go through the expansion and push
4751 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004752 * parameter tokens and macro-local tokens doesn't get done
4753 * until the single-line macro substitution process; this is
4754 * because delaying them allows us to change the semantics
4755 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004756 *
4757 * First, push an end marker on to istk->expansion, mark this
4758 * macro as in progress, and set up its invocation-specific
4759 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004760 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004761 ll = nasm_malloc(sizeof(Line));
4762 ll->next = istk->expansion;
4763 ll->finishes = m;
4764 ll->first = NULL;
4765 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004766
4767 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004768 * Save the previous MMacro expansion in the case of
4769 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004770 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004771 if (m->max_depth && m->in_progress)
4772 push_mmacro(m);
4773
4774 m->in_progress ++;
4775 m->params = params;
4776 m->iline = tline;
4777 m->nparam = nparam;
4778 m->rotate = 0;
4779 m->paramlen = paramlen;
4780 m->unique = unique++;
4781 m->lineno = 0;
4782 m->condcnt = 0;
4783
4784 m->next_active = istk->mstk;
4785 istk->mstk = m;
4786
4787 list_for_each(l, m->expansion) {
4788 Token **tail;
4789
4790 ll = nasm_malloc(sizeof(Line));
4791 ll->finishes = NULL;
4792 ll->next = istk->expansion;
4793 istk->expansion = ll;
4794 tail = &ll->first;
4795
4796 list_for_each(t, l->first) {
4797 Token *x = t;
4798 switch (t->type) {
4799 case TOK_PREPROC_Q:
4800 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004801 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004802 case TOK_PREPROC_QQ:
4803 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4804 break;
4805 case TOK_PREPROC_ID:
4806 if (t->text[1] == '0' && t->text[2] == '0') {
4807 dont_prepend = -1;
4808 x = label;
4809 if (!x)
4810 continue;
4811 }
4812 /* fall through */
4813 default:
4814 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4815 break;
4816 }
4817 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004818 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004819 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004820 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004821
4822 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004823 * If we had a label, push it on as the first line of
4824 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004825 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004826 if (label) {
4827 if (dont_prepend < 0)
4828 free_tlist(startline);
4829 else {
4830 ll = nasm_malloc(sizeof(Line));
4831 ll->finishes = NULL;
4832 ll->next = istk->expansion;
4833 istk->expansion = ll;
4834 ll->first = startline;
4835 if (!dont_prepend) {
4836 while (label->next)
4837 label = label->next;
4838 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004839 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004840 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004841 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004842
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004843 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004844
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004845 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004846}
4847
H. Peter Anvin130736c2016-02-17 20:27:41 -08004848/*
4849 * This function adds macro names to error messages, and suppresses
4850 * them if necessary.
4851 */
4852static void pp_verror(int severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004853{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004854 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004855 MMacro *mmac = NULL;
4856 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004857
H. Peter Anvin130736c2016-02-17 20:27:41 -08004858 /*
4859 * If we're in a dead branch of IF or something like it, ignore the error.
4860 * However, because %else etc are evaluated in the state context
4861 * of the previous branch, errors might get lost:
4862 * %if 0 ... %else trailing garbage ... %endif
4863 * So %else etc should set the ERR_PP_PRECOND flag.
4864 */
4865 if ((severity & ERR_MASK) < ERR_FATAL &&
4866 istk && istk->conds &&
4867 ((severity & ERR_PP_PRECOND) ?
4868 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004869 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004870 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004871
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004872 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004873 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004874 mmac = istk->mstk;
4875 /* but %rep blocks should be skipped */
4876 while (mmac && !mmac->name)
4877 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004878 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004879
H. Peter Anvin130736c2016-02-17 20:27:41 -08004880 if (mmac) {
4881 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004882
H. Peter Anvin130736c2016-02-17 20:27:41 -08004883 nasm_set_verror(real_verror);
4884 nasm_error(severity, "(%s:%d) %s",
4885 mmac->name, mmac->lineno - delta, buff);
4886 nasm_set_verror(pp_verror);
4887 } else {
4888 real_verror(severity, fmt, arg);
4889 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004890}
4891
H. Peter Anvin734b1882002-04-30 21:01:08 +00004892static void
H. Peter Anvin130736c2016-02-17 20:27:41 -08004893pp_reset(char *file, int apass, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004894{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004895 Token *t;
4896
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004897 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004898 istk = nasm_malloc(sizeof(Include));
4899 istk->next = NULL;
4900 istk->conds = NULL;
4901 istk->expansion = NULL;
4902 istk->mstk = NULL;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07004903 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004904 istk->fname = NULL;
H. Peter Anvin274cda82016-05-10 02:56:29 -07004905 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004906 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004907 if (!istk->fp)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004908 nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004909 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004910 nested_mac_count = 0;
4911 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004912 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004913 unique = 0;
H. Peter Anvinf7606612016-07-13 14:23:48 -07004914
4915 if (tasm_compatible_mode)
4916 pp_add_stdmac(nasm_stdmac_tasm);
4917
4918 pp_add_stdmac(nasm_stdmac_nasm);
4919 pp_add_stdmac(nasm_stdmac_version);
4920
4921 stdmacpos = stdmacros[0];
4922 stdmacnext = &stdmacros[1];
4923
H. Peter Anvind2456592008-06-19 15:04:18 -07004924 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004925
4926 /*
4927 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4928 * The caller, however, will also pass in 3 for preprocess-only so
4929 * we can set __PASS__ accordingly.
4930 */
4931 pass = apass > 2 ? 2 : apass;
4932
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004933 dephead = deplist;
H. Peter Anvin436e3672016-10-04 01:12:28 -07004934 nasm_add_string_to_strlist(dephead, file);
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004935
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004936 /*
4937 * Define the __PASS__ macro. This is defined here unlike
4938 * all the other builtins, because it is special -- it varies between
4939 * passes.
4940 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004941 t = nasm_malloc(sizeof(*t));
4942 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004943 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004944 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004945 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004946}
4947
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07004948static void pp_init(void)
4949{
4950 hash_init(&FileHash, HASH_MEDIUM);
4951}
4952
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004953static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004954{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004955 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004956 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004957
H. Peter Anvin130736c2016-02-17 20:27:41 -08004958 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08004959
H. Peter Anvine2c80182005-01-15 22:15:51 +00004960 while (1) {
4961 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004962 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004963 * buffer or from the input file.
4964 */
4965 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004966 while (istk->expansion && istk->expansion->finishes) {
4967 Line *l = istk->expansion;
4968 if (!l->finishes->name && l->finishes->in_progress > 1) {
4969 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004970
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004971 /*
4972 * This is a macro-end marker for a macro with no
4973 * name, which means it's not really a macro at all
4974 * but a %rep block, and the `in_progress' field is
4975 * more than 1, meaning that we still need to
4976 * repeat. (1 means the natural last repetition; 0
4977 * means termination by %exitrep.) We have
4978 * therefore expanded up to the %endrep, and must
4979 * push the whole block on to the expansion buffer
4980 * again. We don't bother to remove the macro-end
4981 * marker: we'd only have to generate another one
4982 * if we did.
4983 */
4984 l->finishes->in_progress--;
4985 list_for_each(l, l->finishes->expansion) {
4986 Token *t, *tt, **tail;
4987
4988 ll = nasm_malloc(sizeof(Line));
4989 ll->next = istk->expansion;
4990 ll->finishes = NULL;
4991 ll->first = NULL;
4992 tail = &ll->first;
4993
4994 list_for_each(t, l->first) {
4995 if (t->text || t->type == TOK_WHITESPACE) {
4996 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4997 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004998 }
4999 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005000
5001 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005002 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005003 } else {
5004 /*
5005 * Check whether a `%rep' was started and not ended
5006 * within this macro expansion. This can happen and
5007 * should be detected. It's a fatal error because
5008 * I'm too confused to work out how to recover
5009 * sensibly from it.
5010 */
5011 if (defining) {
5012 if (defining->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005013 nasm_panic(0, "defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005014 else if (istk->mstk->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005015 nasm_fatal(0, "`%%rep' without `%%endrep' within"
5016 " expansion of macro `%s'",
5017 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005018 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005019
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005020 /*
5021 * FIXME: investigate the relationship at this point between
5022 * istk->mstk and l->finishes
5023 */
5024 {
5025 MMacro *m = istk->mstk;
5026 istk->mstk = m->next_active;
5027 if (m->name) {
5028 /*
5029 * This was a real macro call, not a %rep, and
5030 * therefore the parameter information needs to
5031 * be freed.
5032 */
5033 if (m->prev) {
5034 pop_mmacro(m);
5035 l->finishes->in_progress --;
5036 } else {
5037 nasm_free(m->params);
5038 free_tlist(m->iline);
5039 nasm_free(m->paramlen);
5040 l->finishes->in_progress = 0;
5041 }
5042 } else
5043 free_mmacro(m);
5044 }
5045 istk->expansion = l->next;
5046 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005047 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005048 }
5049 }
5050 while (1) { /* until we get a line we can use */
5051
5052 if (istk->expansion) { /* from a macro expansion */
5053 char *p;
5054 Line *l = istk->expansion;
5055 if (istk->mstk)
5056 istk->mstk->lineno++;
5057 tline = l->first;
5058 istk->expansion = l->next;
5059 nasm_free(l);
5060 p = detoken(tline, false);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005061 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005062 nasm_free(p);
5063 break;
5064 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005065 line = read_line();
5066 if (line) { /* from the current input file */
5067 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005068 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005069 nasm_free(line);
5070 break;
5071 }
5072 /*
5073 * The current file has ended; work down the istk
5074 */
5075 {
5076 Include *i = istk;
5077 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005078 if (i->conds) {
5079 /* nasm_error can't be conditionally suppressed */
H. Peter Anvin41087062016-03-03 14:27:34 -08005080 nasm_fatal(0,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005081 "expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005082 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005083 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005084 if (i->next)
5085 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005086 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005087 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005088 nasm_free(i);
H. Peter Anvin130736c2016-02-17 20:27:41 -08005089 if (!istk) {
5090 line = NULL;
5091 goto done;
5092 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005093 if (istk->expansion && istk->expansion->finishes)
5094 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005095 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005096 }
5097
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005098 /*
5099 * We must expand MMacro parameters and MMacro-local labels
5100 * _before_ we plunge into directive processing, to cope
5101 * with things like `%define something %1' such as STRUC
5102 * uses. Unless we're _defining_ a MMacro, in which case
5103 * those tokens should be left alone to go into the
5104 * definition; and unless we're in a non-emitting
5105 * condition, in which case we don't want to meddle with
5106 * anything.
5107 */
5108 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5109 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005110 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005111 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005112
H. Peter Anvine2c80182005-01-15 22:15:51 +00005113 /*
5114 * Check the line to see if it's a preprocessor directive.
5115 */
5116 if (do_directive(tline) == DIRECTIVE_FOUND) {
5117 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005118 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005119 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005120 * We're defining a multi-line macro. We emit nothing
5121 * at all, and just
5122 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005123 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005124 Line *l = nasm_malloc(sizeof(Line));
5125 l->next = defining->expansion;
5126 l->first = tline;
5127 l->finishes = NULL;
5128 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005129 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005130 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005131 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005132 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005133 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005134 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005135 * directive so we keep our place correctly.
5136 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005137 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005138 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005139 } else if (istk->mstk && !istk->mstk->in_progress) {
5140 /*
5141 * We're in a %rep block which has been terminated, so
5142 * we're walking through to the %endrep without
5143 * emitting anything. Emit nothing at all, not even a
5144 * blank line: when we emerge from the %rep block we'll
5145 * give a line-number directive so we keep our place
5146 * correctly.
5147 */
5148 free_tlist(tline);
5149 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005150 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005151 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005152 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005153 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005154 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005155 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005156 line = detoken(tline, true);
5157 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005158 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005159 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005160 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005161 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005162 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005163 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005164
H. Peter Anvin130736c2016-02-17 20:27:41 -08005165done:
5166 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005167 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005168}
5169
H. Peter Anvine2c80182005-01-15 22:15:51 +00005170static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005171{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005172 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005173
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005174 if (defining) {
5175 if (defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005176 nasm_error(ERR_NONFATAL,
5177 "end of file while still defining macro `%s'",
5178 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005179 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005180 nasm_error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005181 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005182
5183 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005184 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005185 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005186
5187 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005188
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005189 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005190 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005191 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005192 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005193 Include *i = istk;
5194 istk = istk->next;
5195 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005196 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005197 }
5198 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005199 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005200 src_set_fname(NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005201 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005202 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005203 free_llist(predef);
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005204 predef = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005205 delete_Blocks();
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005206 freeTokens = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005207 while ((i = ipath)) {
5208 ipath = i->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005209 if (i->path)
5210 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005211 nasm_free(i);
5212 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005213 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005214}
5215
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005216static void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005217{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005218 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005219
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005220 i = nasm_malloc(sizeof(IncPath));
5221 i->path = path ? nasm_strdup(path) : NULL;
5222 i->next = NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005223
H. Peter Anvin89cee572009-07-15 09:16:54 -04005224 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005225 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005226 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005227 j = j->next;
5228 j->next = i;
5229 } else {
5230 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005231 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005232}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005233
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005234static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005235{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005236 Token *inc, *space, *name;
5237 Line *l;
5238
H. Peter Anvin734b1882002-04-30 21:01:08 +00005239 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5240 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5241 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005242
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005243 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005244 l->next = predef;
5245 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005246 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005247 predef = l;
5248}
5249
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005250static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005251{
5252 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005253 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005254 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005255
H. Peter Anvin130736c2016-02-17 20:27:41 -08005256 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005257
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005258 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005259 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5260 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005261 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005262 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005263 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005264 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005265 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005266
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005267 if (space->next->type != TOK_PREPROC_ID &&
5268 space->next->type != TOK_ID)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005269 nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005270
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005271 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005272 l->next = predef;
5273 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005274 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005275 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005276
5277 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005278}
5279
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005280static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005281{
5282 Token *def, *space;
5283 Line *l;
5284
H. Peter Anvin734b1882002-04-30 21:01:08 +00005285 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5286 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005287 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005288
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005289 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005290 l->next = predef;
5291 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005292 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005293 predef = l;
5294}
5295
H. Peter Anvinf7606612016-07-13 14:23:48 -07005296static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005297{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005298 macros_t **mp;
5299
5300 /* Find the end of the list and avoid duplicates */
5301 for (mp = stdmacros; *mp; mp++) {
5302 if (*mp == macros)
5303 return; /* Nothing to do */
5304 }
5305
5306 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5307
5308 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005309}
5310
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005311static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005312{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005313 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005314 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005315 tok->text = nasm_strdup(numbuf);
5316 tok->type = TOK_NUMBER;
5317}
5318
H. Peter Anvin37368952016-05-09 14:10:32 -07005319static void pp_list_one_macro(MMacro *m, int severity)
5320{
5321 if (!m)
5322 return;
5323
5324 /* We need to print the next_active list in reverse order */
5325 pp_list_one_macro(m->next_active, severity);
5326
5327 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005328 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvin37368952016-05-09 14:10:32 -07005329 nasm_error(severity, "... from macro `%s' defined here", m->name);
5330 }
5331}
5332
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005333static void pp_error_list_macros(int severity)
5334{
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005335 int32_t saved_line;
5336 const char *saved_fname = NULL;
5337
5338 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY;
H. Peter Anvin274cda82016-05-10 02:56:29 -07005339 src_get(&saved_line, &saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005340
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005341 if (istk)
5342 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005343
H. Peter Anvin274cda82016-05-10 02:56:29 -07005344 src_set(saved_line, saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005345}
5346
H. Peter Anvine7469712016-02-18 02:20:59 -08005347const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005348 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005349 pp_reset,
5350 pp_getline,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005351 pp_cleanup,
H. Peter Anvinf7606612016-07-13 14:23:48 -07005352 pp_add_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005353 pp_pre_define,
5354 pp_pre_undefine,
5355 pp_pre_include,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005356 pp_include_path,
5357 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005358};