blob: ecc9506b430fef32e77c4f934a278dd7d54d48dd [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
H. Peter Anvin130736c2016-02-17 20:27:41 -08003 * Copyright 1996-2016 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000065#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000066#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000067#include <stdlib.h>
68#include <stddef.h>
69#include <string.h>
70#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000071#include <limits.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000072
73#include "nasm.h"
74#include "nasmlib.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000075#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070076#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070077#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070078#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070079#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070080#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070081#include "tables.h"
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -080082#include "listing.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000083
84typedef struct SMacro SMacro;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080085typedef struct MMacro MMacro;
86typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000087typedef struct Context Context;
88typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000089typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000090typedef struct Line Line;
91typedef struct Include Include;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080092typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000093typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000094
95/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070096 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
102 */
103
104/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 * Store the definition of a single-line macro.
106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107struct SMacro {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800108 SMacro *next;
109 char *name;
110 bool casesense;
111 bool in_progress;
112 unsigned int nparam;
113 Token *expansion;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000114};
115
116/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800117 * Store the definition of a multi-line macro. This is also used to
118 * store the interiors of `%rep...%endrep' blocks, which are
119 * effectively self-re-invoking multi-line macros which simply
120 * don't have a name or bother to appear in the hash tables. %rep
121 * blocks are signified by having a NULL `name' field.
122 *
123 * In a MMacro describing a `%rep' block, the `in_progress' field
124 * isn't merely boolean, but gives the number of repeats left to
125 * run.
126 *
127 * The `next' field is used for storing MMacros in hash tables; the
128 * `next_active' field is for stacking them on istk entries.
129 *
130 * When a MMacro is being expanded, `params', `iline', `nparam',
131 * `paramlen', `rotate' and `unique' are local to the invocation.
132 */
133struct MMacro {
134 MMacro *next;
135 MMacroInvocation *prev; /* previous invocation */
136 char *name;
137 int nparam_min, nparam_max;
138 bool casesense;
139 bool plus; /* is the last parameter greedy? */
140 bool nolist; /* is this macro listing-inhibited? */
141 int64_t in_progress; /* is this macro currently being expanded? */
142 int32_t max_depth; /* maximum number of recursive expansions allowed */
143 Token *dlist; /* All defaults as one list */
144 Token **defaults; /* Parameter default pointers */
145 int ndefs; /* number of default parameters */
146 Line *expansion;
147
148 MMacro *next_active;
149 MMacro *rep_nest; /* used for nesting %rep */
150 Token **params; /* actual parameters */
151 Token *iline; /* invocation line */
152 unsigned int nparam, rotate;
153 int *paramlen;
154 uint64_t unique;
155 int lineno; /* Current line number on expansion */
156 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700157
H. Peter Anvin274cda82016-05-10 02:56:29 -0700158 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700159 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800160};
161
162
163/* Store the definition of a multi-line macro, as defined in a
164 * previous recursive macro expansion.
165 */
166struct MMacroInvocation {
167 MMacroInvocation *prev; /* previous invocation */
168 Token **params; /* actual parameters */
169 Token *iline; /* invocation line */
170 unsigned int nparam, rotate;
171 int *paramlen;
172 uint64_t unique;
173 uint64_t condcnt;
174};
175
176
177/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000178 * The context stack is composed of a linked list of these.
179 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000180struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800181 Context *next;
182 char *name;
183 struct hash_table localmac;
184 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000185};
186
187/*
188 * This is the internal form which we break input lines up into.
189 * Typically stored in linked lists.
190 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000191 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
192 * necessarily used as-is, but is intended to denote the number of
193 * the substituted parameter. So in the definition
194 *
195 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700196 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000197 * the token representing `x' will have its type changed to
198 * TOK_SMAC_PARAM, but the one representing `y' will be
199 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000200 *
201 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
202 * which doesn't need quotes around it. Used in the pre-include
203 * mechanism as an alternative to trying to find a sensible type of
204 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000205 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000206enum pp_token_type {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800207 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
208 TOK_PREPROC_ID, TOK_STRING,
209 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700210 TOK_INTERNAL_STRING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800211 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300212 TOK_PASTE, /* %+ */
213 TOK_INDIRECT, /* %[...] */
214 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
215 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000216};
217
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400218#define PP_CONCAT_MASK(x) (1 << (x))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +0400219#define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400220
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400221struct tokseq_match {
222 int mask_head;
223 int mask_tail;
224};
225
H. Peter Anvine2c80182005-01-15 22:15:51 +0000226struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800227 Token *next;
228 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700229 union {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800230 SMacro *mac; /* associated macro for TOK_SMAC_END */
231 size_t len; /* scratch length field */
232 } a; /* Auxiliary data */
233 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000234};
235
236/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800237 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000238 * these, which is essentially a container to allow several linked
239 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700240 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000241 * Note that in this module, linked lists are treated as stacks
242 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800243 * `expansion' field in MMacro structures, so that the linked list,
244 * if walked, would give the macro lines in reverse order; this
245 * means that we can walk the list when expanding a macro, and thus
246 * push the lines on to the `expansion' field in _istk_ in reverse
247 * order (so that when popped back off they are in the right
248 * order). It may seem cockeyed, and it relies on my design having
249 * an even number of steps in, but it works...
250 *
251 * Some of these structures, rather than being actual lines, are
252 * markers delimiting the end of the expansion of a given macro.
253 * This is for use in the cycle-tracking and %rep-handling code.
254 * Such structures have `finishes' non-NULL, and `first' NULL. All
255 * others have `finishes' NULL, but `first' may still be NULL if
256 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000257 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000258struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800259 Line *next;
260 MMacro *finishes;
261 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500262};
263
264/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000265 * To handle an arbitrary level of file inclusion, we maintain a
266 * stack (ie linked list) of these things.
267 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000268struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800269 Include *next;
270 FILE *fp;
271 Cond *conds;
272 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700273 const char *fname;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800274 int lineno, lineinc;
275 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000276};
277
278/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000279 * Include search path. This is simply a list of strings which get
280 * prepended, in turn, to the name of an include file, in an
281 * attempt to find the file if it's not in the current directory.
282 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000283struct IncPath {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800284 IncPath *next;
285 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000286};
287
288/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700289 * File real name hash, so we don't have to re-search the include
290 * path for every pass (and potentially more than that if a file
291 * is used more than once.)
292 */
293struct hash_table FileHash;
294
295/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000296 * Conditional assembly: we maintain a separate stack of these for
297 * each level of file inclusion. (The only reason we keep the
298 * stacks separate is to ensure that a stray `%endif' in a file
299 * included from within the true branch of a `%if' won't terminate
300 * it and cause confusion: instead, rightly, it'll cause an error.)
301 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800302struct Cond {
303 Cond *next;
304 int state;
305};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000306enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000307 /*
308 * These states are for use just after %if or %elif: IF_TRUE
309 * means the condition has evaluated to truth so we are
310 * currently emitting, whereas IF_FALSE means we are not
311 * currently emitting but will start doing so if a %else comes
312 * up. In these states, all directives are admissible: %elif,
313 * %else and %endif. (And of course %if.)
314 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800315 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000316 /*
317 * These states come up after a %else: ELSE_TRUE means we're
318 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
319 * any %elif or %else will cause an error.
320 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800321 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000322 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200323 * These states mean that we're not emitting now, and also that
324 * nothing until %endif will be emitted at all. COND_DONE is
325 * used when we've had our moment of emission
326 * and have now started seeing %elifs. COND_NEVER is used when
327 * the condition construct in question is contained within a
328 * non-emitting branch of a larger condition construct,
329 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000330 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800331 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000332};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800333#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000334
H. Peter Anvin70653092007-10-19 14:42:29 -0700335/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000336 * These defines are used as the possible return values for do_directive
337 */
338#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300339#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000340
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800342 * This define sets the upper limit for smacro and recursive mmacro
343 * expansions
Keith Kanios852f1ee2009-07-12 00:19:55 -0500344 */
345#define DEADMAN_LIMIT (1 << 20)
346
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400347/* max reps */
348#define REP_LIMIT ((INT64_C(1) << 62))
349
Keith Kanios852f1ee2009-07-12 00:19:55 -0500350/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000351 * Condition codes. Note that we use c_ prefix not C_ because C_ is
352 * used in nasm.h for the "real" condition codes. At _this_ level,
353 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
354 * ones, so we need a different enum...
355 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700356static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000357 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
358 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000359 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000360};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700361enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000362 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
363 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
H. Peter Anvin476d2862007-10-02 22:04:15 -0700364 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
365 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000366};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700367static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000368 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
369 c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
H. Peter Anvince9be342007-09-12 00:22:29 +0000370 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000371};
372
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800373/*
374 * Directive names.
375 */
376/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
377static int is_condition(enum preproc_token arg)
378{
379 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
380}
381
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000382/* For TASM compatibility we need to be able to recognise TASM compatible
383 * conditional compilation directives. Using the NASM pre-processor does
384 * not work, so we look for them specifically from the following list and
385 * then jam in the equivalent NASM directive into the input stream.
386 */
387
H. Peter Anvine2c80182005-01-15 22:15:51 +0000388enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000389 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
390 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
391};
392
H. Peter Anvin476d2862007-10-02 22:04:15 -0700393static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000394 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
395 "ifndef", "include", "local"
396};
397
398static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700399static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000400static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800401static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000402
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000403static Context *cstk;
404static Include *istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800405static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000406
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300407static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700408static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000409
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300410static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000411
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800412static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700413static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000414
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000415/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800416 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000417 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800418static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000419
420/*
421 * The current set of single-line macros we have defined.
422 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700423static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000424
425/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800426 * The multi-line macro we are currently defining, or the %rep
427 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000428 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800429static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000430
Charles Crayned4200be2008-07-12 16:42:33 -0700431static uint64_t nested_mac_count;
432static uint64_t nested_rep_count;
433
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000434/*
435 * The number of macro parameters to allocate space for at a time.
436 */
437#define PARAM_DELTA 16
438
439/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700440 * The standard macro set: defined in macros.c in a set of arrays.
441 * This gives our position in any macro set, while we are processing it.
442 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000443 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700444static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700445static macros_t **stdmacnext;
446static macros_t *stdmacros[8];
H. Peter Anvin76690a12002-04-30 20:52:49 +0000447
448/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000449 * Tokens are allocated in blocks to improve speed
450 */
451#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800452static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000453struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000454 Blocks *next;
455 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000456};
457
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800458static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000459
460/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000461 * Forward declarations.
462 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700463static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000464static Token *expand_mmac_params(Token * tline);
465static Token *expand_smacro(Token * tline);
466static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400467static Context *get_ctx(const char *name, const char **namep);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700468static void make_tok_num(Token * tok, int64_t val);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800469static void pp_verror(int severity, const char *fmt, va_list ap);
470static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000471static void *new_Block(size_t size);
472static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700473static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300474 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000475static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000476
477/*
478 * Macros for safe checking of token pointers, avoid *(NULL)
479 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300480#define tok_type_(x,t) ((x) && (x)->type == (t))
481#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
482#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
483#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000484
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400485/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700486 * nasm_unquote with error if the string contains NUL characters.
487 * If the string contains NUL characters, issue an error and return
488 * the C len, i.e. truncate at the NUL.
489 */
490static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
491{
492 size_t len = nasm_unquote(qstr, NULL);
493 size_t clen = strlen(qstr);
494
495 if (len != clen)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800496 nasm_error(ERR_NONFATAL, "NUL character in `%s' directive",
H. Peter Anvin077fb932010-07-20 14:56:30 -0700497 pp_directives[directive]);
498
499 return clen;
500}
501
502/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700503 * In-place reverse a list of tokens.
504 */
505static Token *reverse_tokens(Token *t)
506{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800507 Token *prev = NULL;
508 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700509
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800510 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400511 next = t->next;
512 t->next = prev;
513 prev = t;
514 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800515 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700516
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800517 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700518}
519
520/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300521 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000522 * front of them. We do it here because I could not find any other
523 * place to do it for the moment, and it is a hack (ideally it would
524 * be nice to be able to use the NASM pre-processor to do it).
525 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000526static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000527{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000528 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400529 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000530
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400531 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000532
533 /* Binary search for the directive name */
534 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400535 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400536 q = nasm_skip_word(p);
537 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000538 if (len) {
539 oldchar = p[len];
540 p[len] = 0;
541 while (j - i > 1) {
542 k = (j + i) / 2;
543 m = nasm_stricmp(p, tasm_directives[k]);
544 if (m == 0) {
545 /* We have found a directive, so jam a % in front of it
546 * so that NASM will then recognise it as one if it's own.
547 */
548 p[len] = oldchar;
549 len = strlen(p);
550 oldline = line;
551 line = nasm_malloc(len + 2);
552 line[0] = '%';
553 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700554 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300555 * NASM does not recognise IFDIFI, so we convert
556 * it to %if 0. This is not used in NASM
557 * compatible code, but does need to parse for the
558 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000559 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700560 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000561 } else {
562 memcpy(line + 1, p, len + 1);
563 }
564 nasm_free(oldline);
565 return line;
566 } else if (m < 0) {
567 j = k;
568 } else
569 i = k;
570 }
571 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000572 }
573 return line;
574}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000575
H. Peter Anvin76690a12002-04-30 20:52:49 +0000576/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000577 * The pre-preprocessing stage... This function translates line
578 * number indications as they emerge from GNU cpp (`# lineno "file"
579 * flags') into NASM preprocessor line number indications (`%line
580 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000581 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000582static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000583{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000584 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000585 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000586
H. Peter Anvine2c80182005-01-15 22:15:51 +0000587 if (line[0] == '#' && line[1] == ' ') {
588 oldline = line;
589 fname = oldline + 2;
590 lineno = atoi(fname);
591 fname += strspn(fname, "0123456789 ");
592 if (*fname == '"')
593 fname++;
594 fnlen = strcspn(fname, "\"");
595 line = nasm_malloc(20 + fnlen);
596 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
597 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000598 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000599 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000600 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000601 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000602}
603
604/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000605 * Free a linked list of tokens.
606 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000607static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000608{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400609 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000610 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000611}
612
613/*
614 * Free a linked list of lines.
615 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000616static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000617{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400618 Line *l, *tmp;
619 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000620 free_tlist(l->first);
621 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000622 }
623}
624
625/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800626 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000627 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800628static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000629{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800630 nasm_free(m->name);
631 free_tlist(m->dlist);
632 nasm_free(m->defaults);
633 free_llist(m->expansion);
634 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000635}
636
637/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700638 * Free all currently defined macros, and free the hash tables
639 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700640static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700641{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400642 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700643 const char *key;
644 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700645
H. Peter Anvin072771e2008-05-22 13:17:51 -0700646 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300647 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400648 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300649 nasm_free(s->name);
650 free_tlist(s->expansion);
651 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300652 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700653 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700654 hash_free(smt);
655}
656
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800657static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700658{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800659 MMacro *m, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700660 const char *key;
661 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700662
663 it = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800664 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300665 nasm_free((void *)key);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800666 list_for_each_safe(m ,tmp, m)
667 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700668 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800669 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700670}
671
672static void free_macros(void)
673{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700674 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800675 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700676}
677
678/*
679 * Initialize the hash tables
680 */
681static void init_macros(void)
682{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700683 hash_init(&smacros, HASH_LARGE);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800684 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700685}
686
687/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000688 * Pop the context stack.
689 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000690static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000691{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000692 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000693
694 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700695 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000696 nasm_free(c->name);
697 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000698}
699
H. Peter Anvin072771e2008-05-22 13:17:51 -0700700/*
701 * Search for a key in the hash index; adding it if necessary
702 * (in which case we initialize the data pointer to NULL.)
703 */
704static void **
705hash_findi_add(struct hash_table *hash, const char *str)
706{
707 struct hash_insert hi;
708 void **r;
709 char *strx;
710
711 r = hash_findi(hash, str, &hi);
712 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300713 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700714
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300715 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700716 return hash_add(&hi, strx, NULL);
717}
718
719/*
720 * Like hash_findi, but returns the data element rather than a pointer
721 * to it. Used only when not adding a new element, hence no third
722 * argument.
723 */
724static void *
725hash_findix(struct hash_table *hash, const char *str)
726{
727 void **p;
728
729 p = hash_findi(hash, str, NULL);
730 return p ? *p : NULL;
731}
732
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400733/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800734 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400735 * if there no more left -- return NULL
736 */
737static char *line_from_stdmac(void)
738{
739 unsigned char c;
740 const unsigned char *p = stdmacpos;
741 char *line, *q;
742 size_t len = 0;
743
744 if (!stdmacpos)
745 return NULL;
746
747 while ((c = *p++)) {
748 if (c >= 0x80)
749 len += pp_directives_len[c - 0x80] + 1;
750 else
751 len++;
752 }
753
754 line = nasm_malloc(len + 1);
755 q = line;
756 while ((c = *stdmacpos++)) {
757 if (c >= 0x80) {
758 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
759 q += pp_directives_len[c - 0x80];
760 *q++ = ' ';
761 } else {
762 *q++ = c;
763 }
764 }
765 stdmacpos = p;
766 *q = '\0';
767
768 if (!*stdmacpos) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700769 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400770 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700771 if (*stdmacnext) {
772 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400773 } else if (do_predef) {
774 Line *pd, *l;
775 Token *head, **tail, *t;
776
777 /*
778 * Nasty hack: here we push the contents of
779 * `predef' on to the top-level expansion stack,
780 * since this is the most convenient way to
781 * implement the pre-include and pre-define
782 * features.
783 */
784 list_for_each(pd, predef) {
785 head = NULL;
786 tail = &head;
787 list_for_each(t, pd->first) {
788 *tail = new_Token(NULL, t->type, t->text, 0);
789 tail = &(*tail)->next;
790 }
791
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800792 l = nasm_malloc(sizeof(Line));
793 l->next = istk->expansion;
794 l->first = head;
795 l->finishes = NULL;
796
797 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400798 }
799 do_predef = false;
800 }
801 }
802
803 return line;
804}
805
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000806static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000807{
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400808 unsigned int size, c, next;
809 const unsigned int delta = 512;
810 const unsigned int pad = 8;
811 unsigned int nr_cont = 0;
812 bool cont = false;
813 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000814
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400815 /* Standart macros set (predefined) goes first */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400816 p = line_from_stdmac();
817 if (p)
818 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700819
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400820 size = delta;
821 p = buffer = nasm_malloc(size);
822
823 for (;;) {
824 c = fgetc(istk->fp);
825 if ((int)(c) == EOF) {
826 p[0] = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000827 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000828 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400829
830 switch (c) {
831 case '\r':
832 next = fgetc(istk->fp);
833 if (next != '\n')
834 ungetc(next, istk->fp);
835 if (cont) {
836 cont = false;
837 continue;
838 }
839 break;
840
841 case '\n':
842 if (cont) {
843 cont = false;
844 continue;
845 }
846 break;
847
848 case '\\':
849 next = fgetc(istk->fp);
850 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400851 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400852 cont = true;
853 nr_cont++;
854 continue;
855 }
856 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000857 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400858
859 if (c == '\r' || c == '\n') {
860 *p++ = 0;
861 break;
862 }
863
864 if (p >= (buffer + size - pad)) {
865 buffer = nasm_realloc(buffer, size + delta);
866 p = buffer + size - pad;
867 size += delta;
868 }
869
870 *p++ = (unsigned char)c;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000871 }
872
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400873 if (p == buffer) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000874 nasm_free(buffer);
875 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000876 }
877
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300878 src_set_linnum(src_get_linnum() + istk->lineinc +
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400879 (nr_cont * istk->lineinc));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000880
881 /*
882 * Handle spurious ^Z, which may be inserted into source files
883 * by some file transfer utilities.
884 */
885 buffer[strcspn(buffer, "\032")] = '\0';
886
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -0800887 lfmt->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000888
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000889 return buffer;
890}
891
892/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000893 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000894 * don't need to parse the value out of e.g. numeric tokens: we
895 * simply split one string into many.
896 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000897static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000898{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700899 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000900 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000901 Token *list = NULL;
902 Token *t, **tail = &list;
903
H. Peter Anvine2c80182005-01-15 22:15:51 +0000904 while (*line) {
905 p = line;
906 if (*p == '%') {
907 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300908 if (*p == '+' && !nasm_isdigit(p[1])) {
909 p++;
910 type = TOK_PASTE;
911 } else if (nasm_isdigit(*p) ||
912 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000913 do {
914 p++;
915 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700916 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000917 type = TOK_PREPROC_ID;
918 } else if (*p == '{') {
919 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800920 while (*p) {
921 if (*p == '}')
922 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000923 p[-1] = *p;
924 p++;
925 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800926 if (*p != '}')
H. Peter Anvin130736c2016-02-17 20:27:41 -0800927 nasm_error(ERR_WARNING | ERR_PASS1,
928 "unterminated %%{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000929 p[-1] = '\0';
930 if (*p)
931 p++;
932 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300933 } else if (*p == '[') {
934 int lvl = 1;
935 line += 2; /* Skip the leading %[ */
936 p++;
937 while (lvl && (c = *p++)) {
938 switch (c) {
939 case ']':
940 lvl--;
941 break;
942 case '%':
943 if (*p == '[')
944 lvl++;
945 break;
946 case '\'':
947 case '\"':
948 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400949 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300950 break;
951 default:
952 break;
953 }
954 }
955 p--;
956 if (*p)
957 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800958 if (lvl)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800959 nasm_error(ERR_NONFATAL|ERR_PASS1,
960 "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300961 type = TOK_INDIRECT;
962 } else if (*p == '?') {
963 type = TOK_PREPROC_Q; /* %? */
964 p++;
965 if (*p == '?') {
966 type = TOK_PREPROC_QQ; /* %?? */
967 p++;
968 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400969 } else if (*p == '!') {
970 type = TOK_PREPROC_ID;
971 p++;
972 if (isidchar(*p)) {
973 do {
974 p++;
975 }
976 while (isidchar(*p));
977 } else if (*p == '\'' || *p == '\"' || *p == '`') {
978 p = nasm_skip_string(p);
979 if (*p)
980 p++;
981 else
H. Peter Anvin130736c2016-02-17 20:27:41 -0800982 nasm_error(ERR_NONFATAL|ERR_PASS1,
983 "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400984 } else {
985 /* %! without string or identifier */
986 type = TOK_OTHER; /* Legacy behavior... */
987 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 } else if (isidchar(*p) ||
989 ((*p == '!' || *p == '%' || *p == '$') &&
990 isidchar(p[1]))) {
991 do {
992 p++;
993 }
994 while (isidchar(*p));
995 type = TOK_PREPROC_ID;
996 } else {
997 type = TOK_OTHER;
998 if (*p == '%')
999 p++;
1000 }
1001 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1002 type = TOK_ID;
1003 p++;
1004 while (*p && isidchar(*p))
1005 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001006 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 /*
1008 * A string token.
1009 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001011 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001012
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 if (*p) {
1014 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001015 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001016 nasm_error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 /* Handling unterminated strings by UNV */
1018 /* type = -1; */
1019 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001020 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001021 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001022 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001024 bool is_hex = false;
1025 bool is_float = false;
1026 bool has_e = false;
1027 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001028
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001030 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001031 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001032
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001033 if (*p == '$') {
1034 p++;
1035 is_hex = true;
1036 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001037
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001038 for (;;) {
1039 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001040
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001041 if (!is_hex && (c == 'e' || c == 'E')) {
1042 has_e = true;
1043 if (*p == '+' || *p == '-') {
1044 /*
1045 * e can only be followed by +/- if it is either a
1046 * prefixed hex number or a floating-point number
1047 */
1048 p++;
1049 is_float = true;
1050 }
1051 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1052 is_hex = true;
1053 } else if (c == 'P' || c == 'p') {
1054 is_float = true;
1055 if (*p == '+' || *p == '-')
1056 p++;
1057 } else if (isnumchar(c) || c == '_')
1058 ; /* just advance */
1059 else if (c == '.') {
1060 /*
1061 * we need to deal with consequences of the legacy
1062 * parser, like "1.nolist" being two tokens
1063 * (TOK_NUMBER, TOK_ID) here; at least give it
1064 * a shot for now. In the future, we probably need
1065 * a flex-based scanner with proper pattern matching
1066 * to do it as well as it can be done. Nothing in
1067 * the world is going to help the person who wants
1068 * 0x123.p16 interpreted as two tokens, though.
1069 */
1070 r = p;
1071 while (*r == '_')
1072 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001073
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001074 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1075 (!is_hex && (*r == 'e' || *r == 'E')) ||
1076 (*r == 'p' || *r == 'P')) {
1077 p = r;
1078 is_float = true;
1079 } else
1080 break; /* Terminate the token */
1081 } else
1082 break;
1083 }
1084 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001085
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001086 if (p == line+1 && *line == '$') {
1087 type = TOK_OTHER; /* TOKEN_HERE */
1088 } else {
1089 if (has_e && !is_hex) {
1090 /* 1e13 is floating-point, but 1e13h is not */
1091 is_float = true;
1092 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001093
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001094 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1095 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001096 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001097 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001098 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 /*
1100 * Whitespace just before end-of-line is discarded by
1101 * pretending it's a comment; whitespace just before a
1102 * comment gets lumped into the comment.
1103 */
1104 if (!*p || *p == ';') {
1105 type = TOK_COMMENT;
1106 while (*p)
1107 p++;
1108 }
1109 } else if (*p == ';') {
1110 type = TOK_COMMENT;
1111 while (*p)
1112 p++;
1113 } else {
1114 /*
1115 * Anything else is an operator of some kind. We check
1116 * for all the double-character operators (>>, <<, //,
1117 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001118 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001119 */
1120 type = TOK_OTHER;
1121 if ((p[0] == '>' && p[1] == '>') ||
1122 (p[0] == '<' && p[1] == '<') ||
1123 (p[0] == '/' && p[1] == '/') ||
1124 (p[0] == '<' && p[1] == '=') ||
1125 (p[0] == '>' && p[1] == '=') ||
1126 (p[0] == '=' && p[1] == '=') ||
1127 (p[0] == '!' && p[1] == '=') ||
1128 (p[0] == '<' && p[1] == '>') ||
1129 (p[0] == '&' && p[1] == '&') ||
1130 (p[0] == '|' && p[1] == '|') ||
1131 (p[0] == '^' && p[1] == '^')) {
1132 p++;
1133 }
1134 p++;
1135 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001136
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 /* Handling unterminated string by UNV */
1138 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001139 {
1140 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1141 t->text[p-line] = *line;
1142 tail = &t->next;
1143 }
1144 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145 if (type != TOK_COMMENT) {
1146 *tail = t = new_Token(NULL, type, line, p - line);
1147 tail = &t->next;
1148 }
1149 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001150 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001151 return list;
1152}
1153
H. Peter Anvince616072002-04-30 21:02:23 +00001154/*
1155 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001156 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001157 * deleted only all at once by the delete_Blocks function.
1158 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001160{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001161 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001163 /* first, get to the end of the linked list */
1164 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001165 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001166 /* now allocate the requested chunk */
1167 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001168
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001169 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001170 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001171 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001172}
1173
1174/*
1175 * this function deletes all managed blocks of memory
1176 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001178{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001179 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001180
H. Peter Anvin70653092007-10-19 14:42:29 -07001181 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001182 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001183 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001184 * free it.
1185 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001187 if (b->chunk)
1188 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001189 a = b;
1190 b = b->next;
1191 if (a != &blocks)
1192 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001193 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001194 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001195}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001196
1197/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001198 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001199 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001200 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001201 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001202static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001203 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001204{
1205 Token *t;
1206 int i;
1207
H. Peter Anvin89cee572009-07-15 09:16:54 -04001208 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1210 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1211 freeTokens[i].next = &freeTokens[i + 1];
1212 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001213 }
1214 t = freeTokens;
1215 freeTokens = t->next;
1216 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001217 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001218 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001219 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001220 t->text = NULL;
1221 } else {
1222 if (txtlen == 0)
1223 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001224 t->text = nasm_malloc(txtlen+1);
1225 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001226 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001227 }
1228 return t;
1229}
1230
H. Peter Anvine2c80182005-01-15 22:15:51 +00001231static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001232{
1233 Token *next = t->next;
1234 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001235 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001236 freeTokens = t;
1237 return next;
1238}
1239
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001240/*
1241 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001242 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1243 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001244 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001245static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001246{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001247 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001248 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001249 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001250 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001251
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001252 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001253 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001254 char *v;
1255 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001256
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001257 v = t->text + 2;
1258 if (*v == '\'' || *v == '\"' || *v == '`') {
1259 size_t len = nasm_unquote(v, NULL);
1260 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001261
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001262 if (len != clen) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001263 nasm_error(ERR_NONFATAL | ERR_PASS1,
1264 "NUL character in %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001265 v = NULL;
1266 }
1267 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001268
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001269 if (v) {
1270 char *p = getenv(v);
1271 if (!p) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001272 nasm_error(ERR_NONFATAL | ERR_PASS1,
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001273 "nonexistent environment variable `%s'", v);
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001274 /*
1275 * FIXME We better should investigate if accessing
1276 * ->text[1] without ->text[0] is safe enough.
1277 */
1278 t->text = nasm_zalloc(2);
1279 } else
1280 t->text = nasm_strdup(p);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001281 }
1282 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001283 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001284
H. Peter Anvine2c80182005-01-15 22:15:51 +00001285 /* Expand local macros here and not during preprocessing */
1286 if (expand_locals &&
1287 t->type == TOK_PREPROC_ID && t->text &&
1288 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001289 const char *q;
1290 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001291 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001293 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001294 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001295 p = nasm_strcat(buffer, q);
1296 nasm_free(t->text);
1297 t->text = p;
1298 }
1299 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001300 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001301 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001302 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001304 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001305
H. Peter Anvin734b1882002-04-30 21:01:08 +00001306 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001307
1308 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001309 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001310 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001312 q = t->text;
1313 while (*q)
1314 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001315 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001316 }
1317 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001318
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001319 return line;
1320}
1321
1322/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001323 * A scanner, suitable for use by the expression evaluator, which
1324 * operates on a line of Tokens. Expects a pointer to a pointer to
1325 * the first token in the line to be passed in as its private_data
1326 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001327 *
1328 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001329 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001330static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001331{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001332 Token **tlineptr = private_data;
1333 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001334 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001335
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 do {
1337 tline = *tlineptr;
1338 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001339 } while (tline && (tline->type == TOK_WHITESPACE ||
1340 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001341
1342 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001344
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001345 tokval->t_charptr = tline->text;
1346
H. Peter Anvin76690a12002-04-30 20:52:49 +00001347 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001348 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001349 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001351
H. Peter Anvine2c80182005-01-15 22:15:51 +00001352 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001353 p = tokval->t_charptr = tline->text;
1354 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001355 tokval->t_charptr++;
1356 return tokval->t_type = TOKEN_ID;
1357 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001358
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001359 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001360 if (r >= p+MAX_KEYWORD)
1361 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001362 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001363 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001364 *s = '\0';
1365 /* right, so we have an identifier sitting in temp storage. now,
1366 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001367 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001368 }
1369
H. Peter Anvine2c80182005-01-15 22:15:51 +00001370 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001371 bool rn_error;
1372 tokval->t_integer = readnum(tline->text, &rn_error);
1373 tokval->t_charptr = tline->text;
1374 if (rn_error)
1375 return tokval->t_type = TOKEN_ERRNUM;
1376 else
1377 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001378 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001379
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001380 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001381 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001382 }
1383
H. Peter Anvine2c80182005-01-15 22:15:51 +00001384 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001385 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001386
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001387 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001388 tokval->t_charptr = tline->text;
1389 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001390
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001391 if (ep[0] != bq || ep[1] != '\0')
1392 return tokval->t_type = TOKEN_ERRSTR;
1393 else
1394 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001395 }
1396
H. Peter Anvine2c80182005-01-15 22:15:51 +00001397 if (tline->type == TOK_OTHER) {
1398 if (!strcmp(tline->text, "<<"))
1399 return tokval->t_type = TOKEN_SHL;
1400 if (!strcmp(tline->text, ">>"))
1401 return tokval->t_type = TOKEN_SHR;
1402 if (!strcmp(tline->text, "//"))
1403 return tokval->t_type = TOKEN_SDIV;
1404 if (!strcmp(tline->text, "%%"))
1405 return tokval->t_type = TOKEN_SMOD;
1406 if (!strcmp(tline->text, "=="))
1407 return tokval->t_type = TOKEN_EQ;
1408 if (!strcmp(tline->text, "<>"))
1409 return tokval->t_type = TOKEN_NE;
1410 if (!strcmp(tline->text, "!="))
1411 return tokval->t_type = TOKEN_NE;
1412 if (!strcmp(tline->text, "<="))
1413 return tokval->t_type = TOKEN_LE;
1414 if (!strcmp(tline->text, ">="))
1415 return tokval->t_type = TOKEN_GE;
1416 if (!strcmp(tline->text, "&&"))
1417 return tokval->t_type = TOKEN_DBL_AND;
1418 if (!strcmp(tline->text, "^^"))
1419 return tokval->t_type = TOKEN_DBL_XOR;
1420 if (!strcmp(tline->text, "||"))
1421 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001422 }
1423
1424 /*
1425 * We have no other options: just return the first character of
1426 * the token text.
1427 */
1428 return tokval->t_type = tline->text[0];
1429}
1430
1431/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001432 * Compare a string to the name of an existing macro; this is a
1433 * simple wrapper which calls either strcmp or nasm_stricmp
1434 * depending on the value of the `casesense' parameter.
1435 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001436static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001437{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001438 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001439}
1440
1441/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001442 * Compare a string to the name of an existing macro; this is a
1443 * simple wrapper which calls either strcmp or nasm_stricmp
1444 * depending on the value of the `casesense' parameter.
1445 */
1446static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1447{
1448 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1449}
1450
1451/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001452 * Return the Context structure associated with a %$ token. Return
1453 * NULL, having _already_ reported an error condition, if the
1454 * context stack isn't deep enough for the supplied number of $
1455 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001456 *
1457 * If "namep" is non-NULL, set it to the pointer to the macro name
1458 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001459 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001460static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001461{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001462 Context *ctx;
1463 int i;
1464
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001465 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001466 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001467
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001468 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001469 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001470
H. Peter Anvine2c80182005-01-15 22:15:51 +00001471 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001472 nasm_error(ERR_NONFATAL, "`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001473 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001474 }
1475
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001476 name += 2;
1477 ctx = cstk;
1478 i = 0;
1479 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001480 name++;
1481 i++;
1482 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001483 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001484 if (!ctx) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001485 nasm_error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001486 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001487 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001488 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001489
1490 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001491 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001492
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001493 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001494}
1495
1496/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001497 * Check to see if a file is already in a string list
1498 */
1499static bool in_list(const StrList *list, const char *str)
1500{
1501 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001502 if (!strcmp(list->str, str))
1503 return true;
1504 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001505 }
1506 return false;
1507}
1508
1509/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001510 * Open an include file. This routine must always return a valid
1511 * file pointer if it returns - it's responsible for throwing an
1512 * ERR_FATAL and bombing out completely if not. It should also try
1513 * the include path one by one until it finds the file or reaches
1514 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001515 *
1516 * Note: for INC_PROBE the function returns NULL at all times;
1517 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001518 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001519enum incopen_mode {
1520 INC_NEEDED, /* File must exist */
1521 INC_OPTIONAL, /* Missing is OK */
1522 INC_PROBE /* Only an existence probe */
1523};
1524
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001525/* This is conducts a full pathname search */
1526static FILE *inc_fopen_search(const char *file, StrList **slpath,
1527 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001528{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001529 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001530 char *prefix = "";
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001531 const IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001532 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001533 size_t prefix_len = 0;
1534 StrList *sl;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001535 size_t path_len;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001536 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001537
H. Peter Anvine2c80182005-01-15 22:15:51 +00001538 while (1) {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001539 path_len = prefix_len + len + 1;
1540
1541 sl = nasm_malloc(path_len + sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001542 memcpy(sl->str, prefix, prefix_len);
1543 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001544 sl->next = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001545
H. Peter Anvind81a2352016-09-21 14:03:18 -07001546 if (omode == INC_PROBE) {
1547 fp = NULL;
1548 found = nasm_file_exists(sl->str);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001549 } else {
H. Peter Anvind81a2352016-09-21 14:03:18 -07001550 fp = nasm_open_read(sl->str, fmode);
1551 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001552 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001553 if (found) {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001554 *slpath = sl;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001555 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001556 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001557
H. Peter Anvind81a2352016-09-21 14:03:18 -07001558 nasm_free(sl);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001559
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001560 if (!ip)
1561 return NULL;
1562
1563 prefix = ip->path;
1564 prefix_len = strlen(prefix);
1565 ip = ip->next;
1566 }
1567}
1568
1569/*
1570 * Open a file, or test for the presence of one (depending on omode),
1571 * considering the include path.
1572 */
1573static FILE *inc_fopen(const char *file,
1574 StrList **dhead, StrList ***dtail,
1575 char **found_path,
1576 enum incopen_mode omode,
1577 enum file_flags fmode)
1578{
1579 StrList *sl;
1580 struct hash_insert hi;
1581 void **hp;
1582 char *path;
1583 FILE *fp = NULL;
1584
1585 hp = hash_find(&FileHash, file, &hi);
1586 if (hp) {
1587 path = *hp;
1588 } else {
1589 /* Need to do the actual path search */
1590 size_t file_len;
1591
1592 sl = NULL;
1593 fp = inc_fopen_search(file, &sl, omode, fmode);
1594
1595 file_len = strlen(file);
1596
1597 if (!sl) {
1598 /* Store negative result for this file */
1599 sl = nasm_malloc(file_len + 1 + sizeof sl->next);
1600 memcpy(sl->str, file, file_len+1);
1601 sl->next = NULL;
1602 file = sl->str;
1603 path = NULL;
1604 } else {
1605 path = sl->str;
1606 file = strchr(path, '\0') - file_len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001607 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001608
1609 hash_add(&hi, file, path); /* Positive or negative result */
1610
1611 if (path || omode != INC_NEEDED) {
1612 /*
1613 * Add file to dependency path. The in_list() is needed
1614 * in case the file was already added with %depend.
1615 */
1616 if (dhead && !in_list(*dhead, sl->str)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001617 **dtail = sl;
1618 *dtail = &sl->next;
1619 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001620 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001621 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001622
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001623 if (!path) {
1624 if (omode == INC_NEEDED)
1625 nasm_fatal(0, "unable to open include file `%s'", file);
1626
1627 if (found_path)
1628 *found_path = NULL;
1629
1630 return NULL;
1631 }
1632
1633 if (!fp && omode != INC_PROBE)
1634 fp = nasm_open_read(file, fmode);
1635
1636 if (found_path)
1637 *found_path = nasm_strdup(path);
1638
1639 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001640}
1641
1642/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001643 * Opens an include or input file. Public version, for use by modules
1644 * that get a file:lineno pair and need to look at the file again
1645 * (e.g. the CodeView debug backend). Returns NULL on failure.
1646 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001647FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001648{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001649 return inc_fopen(filename, NULL, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001650}
1651
1652/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001653 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001654 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001655 * return true if _any_ single-line macro of that name is defined.
1656 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001657 * `nparam' or no parameters is defined.
1658 *
1659 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001660 * defined, or nparam is -1, the address of the definition structure
1661 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001662 * is NULL, no action will be taken regarding its contents, and no
1663 * error will occur.
1664 *
1665 * Note that this is also called with nparam zero to resolve
1666 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001667 *
1668 * If you already know which context macro belongs to, you can pass
1669 * the context pointer as first parameter; if you won't but name begins
1670 * with %$ the context will be automatically computed. If all_contexts
1671 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001672 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001673static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001674smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001675 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001676{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001677 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001678 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001679
H. Peter Anvin97a23472007-09-16 17:57:25 -07001680 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001681 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001682 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001683 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001684 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001686 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001687 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001688 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001689 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001690 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001691 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001692
H. Peter Anvine2c80182005-01-15 22:15:51 +00001693 while (m) {
1694 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001695 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001696 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001697 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001698 *defn = m;
1699 else
1700 *defn = NULL;
1701 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001702 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001703 }
1704 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001705 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001706
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001707 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001708}
1709
1710/*
1711 * Count and mark off the parameters in a multi-line macro call.
1712 * This is called both from within the multi-line macro expansion
1713 * code, and also to mark off the default parameters when provided
1714 * in a %macro definition line.
1715 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001716static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001717{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001718 int paramsize, brace;
1719
1720 *nparam = paramsize = 0;
1721 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001722 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001723 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001724 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001725 paramsize += PARAM_DELTA;
1726 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1727 }
1728 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001729 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001730 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001731 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001732 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001733 if (brace) {
1734 while (brace && (t = t->next) != NULL) {
1735 if (tok_is_(t, "{"))
1736 brace++;
1737 else if (tok_is_(t, "}"))
1738 brace--;
1739 }
1740
1741 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 /*
1743 * Now we've found the closing brace, look further
1744 * for the comma.
1745 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001746 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001747 skip_white_(t);
1748 if (tok_isnt_(t, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001749 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001750 "braces do not enclose all of macro parameter");
1751 while (tok_isnt_(t, ","))
1752 t = t->next;
1753 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001754 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001755 } else {
1756 while (tok_isnt_(t, ","))
1757 t = t->next;
1758 }
1759 if (t) { /* got a comma/brace */
1760 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001761 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001762 }
1763}
1764
1765/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001766 * Determine whether one of the various `if' conditions is true or
1767 * not.
1768 *
1769 * We must free the tline we get passed.
1770 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001771static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001772{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001773 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001774 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001775 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001776 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001777 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001778 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001779 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001780
1781 origline = tline;
1782
H. Peter Anvine2c80182005-01-15 22:15:51 +00001783 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001784 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001785 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001786 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001787 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001788 if (!tline)
1789 break;
1790 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001791 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001792 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001793 free_tlist(origline);
1794 return -1;
1795 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001796 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001797 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001798 tline = tline->next;
1799 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001800 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001801
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001802 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001803 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001804 while (tline) {
1805 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 if (!tline || (tline->type != TOK_ID &&
1807 (tline->type != TOK_PREPROC_ID ||
1808 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001809 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001810 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001811 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001813 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001814 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001815 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001816 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001817 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001818
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001819 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001820 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001821 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001822 while (tline) {
1823 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001824 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001825 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001826 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001827 tline->text[1] != '!'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001828 nasm_error(ERR_NONFATAL,
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001829 "`%s' expects environment variable names",
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001830 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001831 goto fail;
1832 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001833 p = tline->text;
1834 if (tline->type == TOK_PREPROC_ID)
1835 p += 2; /* Skip leading %! */
1836 if (*p == '\'' || *p == '\"' || *p == '`')
1837 nasm_unquote_cstr(p, ct);
1838 if (getenv(p))
1839 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001840 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001841 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001842 break;
1843
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001844 case PPC_IFIDN:
1845 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001846 tline = expand_smacro(tline);
1847 t = tt = tline;
1848 while (tok_isnt_(tt, ","))
1849 tt = tt->next;
1850 if (!tt) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001851 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001852 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001853 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001854 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001855 }
1856 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001857 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001858 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1859 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001860 nasm_error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001861 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001862 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001863 }
1864 if (t->type == TOK_WHITESPACE) {
1865 t = t->next;
1866 continue;
1867 }
1868 if (tt->type == TOK_WHITESPACE) {
1869 tt = tt->next;
1870 continue;
1871 }
1872 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001873 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001874 break;
1875 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001876 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001877 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001878 size_t l1 = nasm_unquote(t->text, NULL);
1879 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001880
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001881 if (l1 != l2) {
1882 j = false;
1883 break;
1884 }
1885 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1886 j = false;
1887 break;
1888 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001889 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001890 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001891 break;
1892 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001893
H. Peter Anvine2c80182005-01-15 22:15:51 +00001894 t = t->next;
1895 tt = tt->next;
1896 }
1897 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001898 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001899 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001900
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001901 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001902 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001903 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001904 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001905
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001906 skip_white_(tline);
1907 tline = expand_id(tline);
1908 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001909 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001910 "`%s' expects a macro name", pp_directives[ct]);
1911 goto fail;
1912 }
1913 searching.name = nasm_strdup(tline->text);
1914 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001915 searching.plus = false;
1916 searching.nolist = false;
1917 searching.in_progress = 0;
1918 searching.max_depth = 0;
1919 searching.rep_nest = NULL;
1920 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001921 searching.nparam_max = INT_MAX;
1922 tline = expand_smacro(tline->next);
1923 skip_white_(tline);
1924 if (!tline) {
1925 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001926 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001927 "`%s' expects a parameter count or nothing",
1928 pp_directives[ct]);
1929 } else {
1930 searching.nparam_min = searching.nparam_max =
1931 readnum(tline->text, &j);
1932 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001933 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001934 "unable to parse parameter count `%s'",
1935 tline->text);
1936 }
1937 if (tline && tok_is_(tline->next, "-")) {
1938 tline = tline->next->next;
1939 if (tok_is_(tline, "*"))
1940 searching.nparam_max = INT_MAX;
1941 else if (!tok_type_(tline, TOK_NUMBER))
H. Peter Anvin130736c2016-02-17 20:27:41 -08001942 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001943 "`%s' expects a parameter count after `-'",
1944 pp_directives[ct]);
1945 else {
1946 searching.nparam_max = readnum(tline->text, &j);
1947 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001948 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001949 "unable to parse parameter count `%s'",
1950 tline->text);
1951 if (searching.nparam_min > searching.nparam_max)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001952 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001953 "minimum parameter count exceeds maximum");
1954 }
1955 }
1956 if (tline && tok_is_(tline->next, "+")) {
1957 tline = tline->next;
1958 searching.plus = true;
1959 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001960 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1961 while (mmac) {
1962 if (!strcmp(mmac->name, searching.name) &&
1963 (mmac->nparam_min <= searching.nparam_max
1964 || searching.plus)
1965 && (searching.nparam_min <= mmac->nparam_max
1966 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001967 found = true;
1968 break;
1969 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001970 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001971 }
1972 if (tline && tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001973 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001974 "trailing garbage after %%ifmacro ignored");
1975 nasm_free(searching.name);
1976 j = found;
1977 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001978 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001979
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001980 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001981 needtype = TOK_ID;
1982 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001983 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001984 needtype = TOK_NUMBER;
1985 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001986 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001987 needtype = TOK_STRING;
1988 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001989
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001990iftype:
1991 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001992
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001993 while (tok_type_(t, TOK_WHITESPACE) ||
1994 (needtype == TOK_NUMBER &&
1995 tok_type_(t, TOK_OTHER) &&
1996 (t->text[0] == '-' || t->text[0] == '+') &&
1997 !t->text[1]))
1998 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001999
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002000 j = tok_type_(t, needtype);
2001 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002002
2003 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002004 t = tline = expand_smacro(tline);
2005 while (tok_type_(t, TOK_WHITESPACE))
2006 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002007
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002008 j = false;
2009 if (t) {
2010 t = t->next; /* Skip the actual token */
2011 while (tok_type_(t, TOK_WHITESPACE))
2012 t = t->next;
2013 j = !t; /* Should be nothing left */
2014 }
2015 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002016
H. Peter Anvin134b9462008-02-16 17:01:40 -08002017 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002018 t = tline = expand_smacro(tline);
2019 while (tok_type_(t, TOK_WHITESPACE))
2020 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002021
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002022 j = !t; /* Should be empty */
2023 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002024
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002025 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002026 t = tline = expand_smacro(tline);
2027 tptr = &t;
2028 tokval.t_type = TOKEN_INVALID;
2029 evalresult = evaluate(ppscan, tptr, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -08002030 NULL, pass | CRITICAL, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002031 if (!evalresult)
2032 return -1;
2033 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002034 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002035 "trailing garbage after expression ignored");
2036 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002037 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002038 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002039 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002040 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002041 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002042 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002043
H. Peter Anvine2c80182005-01-15 22:15:51 +00002044 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002045 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002046 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002047 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002048 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002049 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002050
2051 free_tlist(origline);
2052 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002053
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002054fail:
2055 free_tlist(origline);
2056 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002057}
2058
2059/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002060 * Common code for defining an smacro
2061 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002062static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002063 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002064{
2065 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002066 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002067
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002068 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002069 if (!smac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002070 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002071 "single-line macro `%s' defined both with and"
2072 " without parameters", mname);
2073 /*
2074 * Some instances of the old code considered this a failure,
2075 * some others didn't. What is the right thing to do here?
2076 */
2077 free_tlist(expansion);
2078 return false; /* Failure */
2079 } else {
2080 /*
2081 * We're redefining, so we have to take over an
2082 * existing SMacro structure. This means freeing
2083 * what was already in it.
2084 */
2085 nasm_free(smac->name);
2086 free_tlist(smac->expansion);
2087 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002088 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002089 smtbl = ctx ? &ctx->localmac : &smacros;
2090 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002091 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002092 smac->next = *smhead;
2093 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002094 }
2095 smac->name = nasm_strdup(mname);
2096 smac->casesense = casesense;
2097 smac->nparam = nparam;
2098 smac->expansion = expansion;
2099 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002100 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002101}
2102
2103/*
2104 * Undefine an smacro
2105 */
2106static void undef_smacro(Context *ctx, const char *mname)
2107{
2108 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002109 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002110
H. Peter Anvin166c2472008-05-28 12:28:58 -07002111 smtbl = ctx ? &ctx->localmac : &smacros;
2112 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002113
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002114 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002115 /*
2116 * We now have a macro name... go hunt for it.
2117 */
2118 sp = smhead;
2119 while ((s = *sp) != NULL) {
2120 if (!mstrcmp(s->name, mname, s->casesense)) {
2121 *sp = s->next;
2122 nasm_free(s->name);
2123 free_tlist(s->expansion);
2124 nasm_free(s);
2125 } else {
2126 sp = &s->next;
2127 }
2128 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002129 }
2130}
2131
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002132/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002133 * Parse a mmacro specification.
2134 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002135static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002136{
2137 bool err;
2138
2139 tline = tline->next;
2140 skip_white_(tline);
2141 tline = expand_id(tline);
2142 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002143 nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002144 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002145 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002146
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002147 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002148 def->name = nasm_strdup(tline->text);
2149 def->plus = false;
2150 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002151 def->in_progress = 0;
2152 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002153 def->nparam_min = 0;
2154 def->nparam_max = 0;
2155
H. Peter Anvina26433d2008-07-16 14:40:01 -07002156 tline = expand_smacro(tline->next);
2157 skip_white_(tline);
2158 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002159 nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002160 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002161 def->nparam_min = def->nparam_max =
2162 readnum(tline->text, &err);
2163 if (err)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002164 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002165 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002166 }
2167 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002168 tline = tline->next->next;
2169 if (tok_is_(tline, "*")) {
2170 def->nparam_max = INT_MAX;
2171 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002172 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002173 "`%s' expects a parameter count after `-'", directive);
2174 } else {
2175 def->nparam_max = readnum(tline->text, &err);
2176 if (err) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002177 nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002178 tline->text);
2179 }
2180 if (def->nparam_min > def->nparam_max) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002181 nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002182 }
2183 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002184 }
2185 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002186 tline = tline->next;
2187 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002188 }
2189 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002190 !nasm_stricmp(tline->next->text, ".nolist")) {
2191 tline = tline->next;
2192 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002193 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002194
H. Peter Anvina26433d2008-07-16 14:40:01 -07002195 /*
2196 * Handle default parameters.
2197 */
2198 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002199 def->dlist = tline->next;
2200 tline->next = NULL;
2201 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002202 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002203 def->dlist = NULL;
2204 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002205 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002206 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002207
H. Peter Anvin89cee572009-07-15 09:16:54 -04002208 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002209 !def->plus)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002210 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002211 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002212
H. Peter Anvina26433d2008-07-16 14:40:01 -07002213 return true;
2214}
2215
2216
2217/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002218 * Decode a size directive
2219 */
2220static int parse_size(const char *str) {
2221 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002222 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002223 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002224 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002225
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002226 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002227}
2228
Ed Beroset3ab3f412002-06-11 03:31:49 +00002229/**
2230 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002231 * Find out if a line contains a preprocessor directive, and deal
2232 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002233 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002234 * If a directive _is_ found, it is the responsibility of this routine
2235 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002236 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002237 * @param tline a pointer to the current tokeninzed line linked list
2238 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002239 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002240 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002241static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002242{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002243 enum preproc_token i;
2244 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002245 bool err;
2246 int nparam;
2247 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002248 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002249 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002250 int offset;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002251 char *p, *pp, *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002252 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002253 Include *inc;
2254 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002255 Cond *cond;
2256 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002257 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002258 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002259 struct tokenval tokval;
2260 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002261 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002262 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002263 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002264 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002265
2266 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002267
H. Peter Anvineba20a72002-04-30 20:53:55 +00002268 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002269 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002270 (tline->text[1] == '%' || tline->text[1] == '$'
2271 || tline->text[1] == '!'))
2272 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002273
H. Peter Anvin4169a472007-09-12 01:29:43 +00002274 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002275
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002276 /*
2277 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2278 * since they are known to be buggy at moment, we need to fix them
2279 * in future release (2.09-2.10)
2280 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002281 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002282 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002283 tline->text);
2284 return NO_DIRECTIVE_FOUND;
2285 }
2286
2287 /*
2288 * If we're in a non-emitting branch of a condition construct,
2289 * or walking to the end of an already terminated %rep block,
2290 * we should ignore all directives except for condition
2291 * directives.
2292 */
2293 if (((istk->conds && !emitting(istk->conds->state)) ||
2294 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2295 return NO_DIRECTIVE_FOUND;
2296 }
2297
2298 /*
2299 * If we're defining a macro or reading a %rep block, we should
2300 * ignore all directives except for %macro/%imacro (which nest),
2301 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2302 * If we're in a %rep block, another %rep nests, so should be let through.
2303 */
2304 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2305 i != PP_RMACRO && i != PP_IRMACRO &&
2306 i != PP_ENDMACRO && i != PP_ENDM &&
2307 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2308 return NO_DIRECTIVE_FOUND;
2309 }
2310
2311 if (defining) {
2312 if (i == PP_MACRO || i == PP_IMACRO ||
2313 i == PP_RMACRO || i == PP_IRMACRO) {
2314 nested_mac_count++;
2315 return NO_DIRECTIVE_FOUND;
2316 } else if (nested_mac_count > 0) {
2317 if (i == PP_ENDMACRO) {
2318 nested_mac_count--;
2319 return NO_DIRECTIVE_FOUND;
2320 }
2321 }
2322 if (!defining->name) {
2323 if (i == PP_REP) {
2324 nested_rep_count++;
2325 return NO_DIRECTIVE_FOUND;
2326 } else if (nested_rep_count > 0) {
2327 if (i == PP_ENDREP) {
2328 nested_rep_count--;
2329 return NO_DIRECTIVE_FOUND;
2330 }
2331 }
2332 }
2333 }
2334
H. Peter Anvin4169a472007-09-12 01:29:43 +00002335 switch (i) {
2336 case PP_INVALID:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002337 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002338 tline->text);
2339 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002340
H. Peter Anvine2c80182005-01-15 22:15:51 +00002341 case PP_STACKSIZE:
2342 /* Directive to tell NASM what the default stack size is. The
2343 * default is for a 16-bit stack, and this can be overriden with
2344 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002345 */
2346 tline = tline->next;
2347 if (tline && tline->type == TOK_WHITESPACE)
2348 tline = tline->next;
2349 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002350 nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002351 free_tlist(origline);
2352 return DIRECTIVE_FOUND;
2353 }
2354 if (nasm_stricmp(tline->text, "flat") == 0) {
2355 /* All subsequent ARG directives are for a 32-bit stack */
2356 StackSize = 4;
2357 StackPointer = "ebp";
2358 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002359 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002360 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2361 /* All subsequent ARG directives are for a 64-bit stack */
2362 StackSize = 8;
2363 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002364 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002365 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002366 } else if (nasm_stricmp(tline->text, "large") == 0) {
2367 /* All subsequent ARG directives are for a 16-bit stack,
2368 * far function call.
2369 */
2370 StackSize = 2;
2371 StackPointer = "bp";
2372 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002373 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002374 } else if (nasm_stricmp(tline->text, "small") == 0) {
2375 /* All subsequent ARG directives are for a 16-bit stack,
2376 * far function call. We don't support near functions.
2377 */
2378 StackSize = 2;
2379 StackPointer = "bp";
2380 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002381 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002383 nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 free_tlist(origline);
2385 return DIRECTIVE_FOUND;
2386 }
2387 free_tlist(origline);
2388 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002389
H. Peter Anvine2c80182005-01-15 22:15:51 +00002390 case PP_ARG:
2391 /* TASM like ARG directive to define arguments to functions, in
2392 * the following form:
2393 *
2394 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2395 */
2396 offset = ArgOffset;
2397 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002398 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002399 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002400
H. Peter Anvine2c80182005-01-15 22:15:51 +00002401 /* Find the argument name */
2402 tline = tline->next;
2403 if (tline && tline->type == TOK_WHITESPACE)
2404 tline = tline->next;
2405 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002406 nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002407 free_tlist(origline);
2408 return DIRECTIVE_FOUND;
2409 }
2410 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002411
H. Peter Anvine2c80182005-01-15 22:15:51 +00002412 /* Find the argument size type */
2413 tline = tline->next;
2414 if (!tline || tline->type != TOK_OTHER
2415 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002416 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002417 "Syntax error processing `%%arg' directive");
2418 free_tlist(origline);
2419 return DIRECTIVE_FOUND;
2420 }
2421 tline = tline->next;
2422 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002423 nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002424 free_tlist(origline);
2425 return DIRECTIVE_FOUND;
2426 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002427
H. Peter Anvine2c80182005-01-15 22:15:51 +00002428 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002429 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002430 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002431 size = parse_size(tt->text);
2432 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002433 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002434 "Invalid size type for `%%arg' missing directive");
2435 free_tlist(tt);
2436 free_tlist(origline);
2437 return DIRECTIVE_FOUND;
2438 }
2439 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002440
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002441 /* Round up to even stack slots */
2442 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002443
H. Peter Anvine2c80182005-01-15 22:15:51 +00002444 /* Now define the macro for the argument */
2445 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2446 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002447 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002448 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002449
H. Peter Anvine2c80182005-01-15 22:15:51 +00002450 /* Move to the next argument in the list */
2451 tline = tline->next;
2452 if (tline && tline->type == TOK_WHITESPACE)
2453 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002454 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002455 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002456 free_tlist(origline);
2457 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002458
H. Peter Anvine2c80182005-01-15 22:15:51 +00002459 case PP_LOCAL:
2460 /* TASM like LOCAL directive to define local variables for a
2461 * function, in the following form:
2462 *
2463 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2464 *
2465 * The '= LocalSize' at the end is ignored by NASM, but is
2466 * required by TASM to define the local parameter size (and used
2467 * by the TASM macro package).
2468 */
2469 offset = LocalOffset;
2470 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002471 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002472 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002473
H. Peter Anvine2c80182005-01-15 22:15:51 +00002474 /* Find the argument name */
2475 tline = tline->next;
2476 if (tline && tline->type == TOK_WHITESPACE)
2477 tline = tline->next;
2478 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002479 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002480 "`%%local' missing argument parameter");
2481 free_tlist(origline);
2482 return DIRECTIVE_FOUND;
2483 }
2484 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002485
H. Peter Anvine2c80182005-01-15 22:15:51 +00002486 /* Find the argument size type */
2487 tline = tline->next;
2488 if (!tline || tline->type != TOK_OTHER
2489 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002490 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002491 "Syntax error processing `%%local' directive");
2492 free_tlist(origline);
2493 return DIRECTIVE_FOUND;
2494 }
2495 tline = tline->next;
2496 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002497 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002498 "`%%local' missing size type parameter");
2499 free_tlist(origline);
2500 return DIRECTIVE_FOUND;
2501 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002502
H. Peter Anvine2c80182005-01-15 22:15:51 +00002503 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002504 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002505 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002506 size = parse_size(tt->text);
2507 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002508 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002509 "Invalid size type for `%%local' missing directive");
2510 free_tlist(tt);
2511 free_tlist(origline);
2512 return DIRECTIVE_FOUND;
2513 }
2514 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002515
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002516 /* Round up to even stack slots */
2517 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002518
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002519 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002520
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002521 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002522 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2523 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002524 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002525
H. Peter Anvine2c80182005-01-15 22:15:51 +00002526 /* Now define the assign to setup the enter_c macro correctly */
2527 snprintf(directive, sizeof(directive),
2528 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002529 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002530
H. Peter Anvine2c80182005-01-15 22:15:51 +00002531 /* Move to the next argument in the list */
2532 tline = tline->next;
2533 if (tline && tline->type == TOK_WHITESPACE)
2534 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002535 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002536 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002537 free_tlist(origline);
2538 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002539
H. Peter Anvine2c80182005-01-15 22:15:51 +00002540 case PP_CLEAR:
2541 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002542 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002543 "trailing garbage after `%%clear' ignored");
2544 free_macros();
2545 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002546 free_tlist(origline);
2547 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002548
H. Peter Anvin418ca702008-05-30 10:42:30 -07002549 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002550 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002551 skip_white_(t);
2552 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002553 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002554 nasm_error(ERR_NONFATAL, "`%%depend' expects a file name");
H. Peter Anvin418ca702008-05-30 10:42:30 -07002555 free_tlist(origline);
2556 return DIRECTIVE_FOUND; /* but we did _something_ */
2557 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002558 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002559 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002560 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002561 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002562 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002563 nasm_unquote_cstr(p, i);
2564 if (dephead && !in_list(*dephead, p)) {
2565 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2566 sl->next = NULL;
2567 strcpy(sl->str, p);
2568 *deptail = sl;
2569 deptail = &sl->next;
2570 }
2571 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002572 return DIRECTIVE_FOUND;
2573
2574 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002575 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002576 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002577
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002578 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002579 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002580 nasm_error(ERR_NONFATAL, "`%%include' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002581 free_tlist(origline);
2582 return DIRECTIVE_FOUND; /* but we did _something_ */
2583 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002584 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002585 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002586 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002587 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002588 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002589 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002590 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002591 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002592 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002593 found_path = NULL;
H. Peter Anvind81a2352016-09-21 14:03:18 -07002594 inc->fp = inc_fopen(p, dephead, &deptail, &found_path,
2595 pass == 0 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002596 if (!inc->fp) {
2597 /* -MG given but file not found */
2598 nasm_free(inc);
2599 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002600 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002601 inc->lineno = src_set_linnum(0);
2602 inc->lineinc = 1;
2603 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002604 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002605 istk = inc;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002606 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002607 }
2608 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002609 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002610
H. Peter Anvind2456592008-06-19 15:04:18 -07002611 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002612 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002613 static macros_t *use_pkg;
2614 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002615
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002616 tline = tline->next;
2617 skip_white_(tline);
2618 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002619
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002620 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002621 tline->type != TOK_INTERNAL_STRING &&
2622 tline->type != TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002623 nasm_error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002624 free_tlist(origline);
2625 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002626 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002627 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002628 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002629 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002630 if (tline->type == TOK_STRING)
2631 nasm_unquote_cstr(tline->text, i);
2632 use_pkg = nasm_stdmac_find_package(tline->text);
2633 if (!use_pkg)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002634 nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002635 else
2636 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002637 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002638 /* Not already included, go ahead and include it */
2639 stdmacpos = use_pkg;
2640 }
2641 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002642 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002643 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002644 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002645 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002646 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002647 tline = tline->next;
2648 skip_white_(tline);
2649 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002650 if (tline) {
2651 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002652 nasm_error(ERR_NONFATAL, "`%s' expects a context identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002653 pp_directives[i]);
2654 free_tlist(origline);
2655 return DIRECTIVE_FOUND; /* but we did _something_ */
2656 }
2657 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002658 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002659 "trailing garbage after `%s' ignored",
2660 pp_directives[i]);
2661 p = nasm_strdup(tline->text);
2662 } else {
2663 p = NULL; /* Anonymous */
2664 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002665
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002666 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002667 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002668 ctx->next = cstk;
2669 hash_init(&ctx->localmac, HASH_SMALL);
2670 ctx->name = p;
2671 ctx->number = unique++;
2672 cstk = ctx;
2673 } else {
2674 /* %pop or %repl */
2675 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002676 nasm_error(ERR_NONFATAL, "`%s': context stack is empty",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002677 pp_directives[i]);
2678 } else if (i == PP_POP) {
2679 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08002680 nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002681 "expected %s",
2682 cstk->name ? cstk->name : "anonymous", p);
2683 else
2684 ctx_pop();
2685 } else {
2686 /* i == PP_REPL */
2687 nasm_free(cstk->name);
2688 cstk->name = p;
2689 p = NULL;
2690 }
2691 nasm_free(p);
2692 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002693 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002694 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002695 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002696 severity = ERR_FATAL;
2697 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002698 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002699 severity = ERR_NONFATAL;
2700 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002701 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002702 severity = ERR_WARNING|ERR_WARN_USER;
2703 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002704
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002705issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002706 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002707 /* Only error out if this is the final pass */
2708 if (pass != 2 && i != PP_FATAL)
2709 return DIRECTIVE_FOUND;
2710
2711 tline->next = expand_smacro(tline->next);
2712 tline = tline->next;
2713 skip_white_(tline);
2714 t = tline ? tline->next : NULL;
2715 skip_white_(t);
2716 if (tok_type_(tline, TOK_STRING) && !t) {
2717 /* The line contains only a quoted string */
2718 p = tline->text;
2719 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002720 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002721 } else {
2722 /* Not a quoted string, or more than a quoted string */
2723 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002724 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002725 nasm_free(p);
2726 }
2727 free_tlist(origline);
2728 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002729 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002730
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002731 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002732 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002733 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002734 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002735 j = if_condition(tline->next, i);
2736 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002737 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002738 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002739 cond = nasm_malloc(sizeof(Cond));
2740 cond->next = istk->conds;
2741 cond->state = j;
2742 istk->conds = cond;
2743 if(istk->mstk)
2744 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002745 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002746 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002747
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002748 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002749 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002750 nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002751 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002752 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002753 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002754 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002755
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002756 case COND_DONE:
2757 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002758 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002759
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002760 case COND_ELSE_TRUE:
2761 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002762 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2763 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002764 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002765 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002766
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002767 case COND_IF_FALSE:
2768 /*
2769 * IMPORTANT: In the case of %if, we will already have
2770 * called expand_mmac_params(); however, if we're
2771 * processing an %elif we must have been in a
2772 * non-emitting mode, which would have inhibited
2773 * the normal invocation of expand_mmac_params().
2774 * Therefore, we have to do it explicitly here.
2775 */
2776 j = if_condition(expand_mmac_params(tline->next), i);
2777 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002778 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002779 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2780 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002781 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002782 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002783 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002784
H. Peter Anvine2c80182005-01-15 22:15:51 +00002785 case PP_ELSE:
2786 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002787 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2788 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002789 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002790 nasm_fatal(0, "`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002791 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002792 case COND_IF_TRUE:
2793 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002794 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002795 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002796
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002797 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002798 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002799
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002800 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002801 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002802 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002803
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002804 case COND_ELSE_TRUE:
2805 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002806 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002807 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002808 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002809 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002810 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002811 free_tlist(origline);
2812 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002813
H. Peter Anvine2c80182005-01-15 22:15:51 +00002814 case PP_ENDIF:
2815 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002816 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2817 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002818 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002819 nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002820 cond = istk->conds;
2821 istk->conds = cond->next;
2822 nasm_free(cond);
2823 if(istk->mstk)
2824 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002825 free_tlist(origline);
2826 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002827
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002828 case PP_RMACRO:
2829 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002830 case PP_MACRO:
2831 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002832 if (defining) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002833 nasm_error(ERR_FATAL, "`%s': already defining a macro",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002834 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002835 return DIRECTIVE_FOUND;
2836 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002837 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002838 defining->max_depth =
2839 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2840 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2841 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2842 nasm_free(defining);
2843 defining = NULL;
2844 return DIRECTIVE_FOUND;
2845 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002846
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002847 src_get(&defining->xline, &defining->fname);
2848
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002849 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2850 while (mmac) {
2851 if (!strcmp(mmac->name, defining->name) &&
2852 (mmac->nparam_min <= defining->nparam_max
2853 || defining->plus)
2854 && (defining->nparam_min <= mmac->nparam_max
2855 || mmac->plus)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002856 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002857 "redefining multi-line macro `%s'", defining->name);
2858 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002859 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002860 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002861 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002862 free_tlist(origline);
2863 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002864
H. Peter Anvine2c80182005-01-15 22:15:51 +00002865 case PP_ENDM:
2866 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002867 if (! (defining && defining->name)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002868 nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002869 return DIRECTIVE_FOUND;
2870 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002871 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2872 defining->next = *mmhead;
2873 *mmhead = defining;
2874 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002875 free_tlist(origline);
2876 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002877
H. Peter Anvin89cee572009-07-15 09:16:54 -04002878 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002879 /*
2880 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002881 * macro-end marker for a macro with a name. Then we
2882 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002883 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002884 list_for_each(l, istk->expansion)
2885 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002886 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002887
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002888 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002889 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002890 * Remove all conditional entries relative to this
2891 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002892 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002893 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2894 cond = istk->conds;
2895 istk->conds = cond->next;
2896 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002897 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002898 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002899 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002900 nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002901 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002902 free_tlist(origline);
2903 return DIRECTIVE_FOUND;
2904
H. Peter Anvina26433d2008-07-16 14:40:01 -07002905 case PP_UNMACRO:
2906 case PP_UNIMACRO:
2907 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002908 MMacro **mmac_p;
2909 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002910
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002911 spec.casesense = (i == PP_UNMACRO);
2912 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2913 return DIRECTIVE_FOUND;
2914 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002915 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2916 while (mmac_p && *mmac_p) {
2917 mmac = *mmac_p;
2918 if (mmac->casesense == spec.casesense &&
2919 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2920 mmac->nparam_min == spec.nparam_min &&
2921 mmac->nparam_max == spec.nparam_max &&
2922 mmac->plus == spec.plus) {
2923 *mmac_p = mmac->next;
2924 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002925 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002926 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002927 }
2928 }
2929 free_tlist(origline);
2930 free_tlist(spec.dlist);
2931 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002932 }
2933
H. Peter Anvine2c80182005-01-15 22:15:51 +00002934 case PP_ROTATE:
2935 if (tline->next && tline->next->type == TOK_WHITESPACE)
2936 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002937 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002938 free_tlist(origline);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002939 nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002940 return DIRECTIVE_FOUND;
2941 }
2942 t = expand_smacro(tline->next);
2943 tline->next = NULL;
2944 free_tlist(origline);
2945 tline = t;
2946 tptr = &t;
2947 tokval.t_type = TOKEN_INVALID;
2948 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002949 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002950 free_tlist(tline);
2951 if (!evalresult)
2952 return DIRECTIVE_FOUND;
2953 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002954 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002955 "trailing garbage after expression ignored");
2956 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002957 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002958 return DIRECTIVE_FOUND;
2959 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002960 mmac = istk->mstk;
2961 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2962 mmac = mmac->next_active;
2963 if (!mmac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002964 nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002965 } else if (mmac->nparam == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002966 nasm_error(ERR_NONFATAL,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002967 "`%%rotate' invoked within macro without parameters");
2968 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002969 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002970
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002971 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002972 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002973 rotate += mmac->nparam;
2974
2975 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002976 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002977 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002978
H. Peter Anvine2c80182005-01-15 22:15:51 +00002979 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002980 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002981 do {
2982 tline = tline->next;
2983 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002984
H. Peter Anvine2c80182005-01-15 22:15:51 +00002985 if (tok_type_(tline, TOK_ID) &&
2986 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002987 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002988 do {
2989 tline = tline->next;
2990 } while (tok_type_(tline, TOK_WHITESPACE));
2991 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002992
H. Peter Anvine2c80182005-01-15 22:15:51 +00002993 if (tline) {
2994 t = expand_smacro(tline);
2995 tptr = &t;
2996 tokval.t_type = TOKEN_INVALID;
2997 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002998 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002999 if (!evalresult) {
3000 free_tlist(origline);
3001 return DIRECTIVE_FOUND;
3002 }
3003 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003004 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003005 "trailing garbage after expression ignored");
3006 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003007 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003008 return DIRECTIVE_FOUND;
3009 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003010 count = reloc_value(evalresult);
3011 if (count >= REP_LIMIT) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003012 nasm_error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003013 count = 0;
3014 } else
3015 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003016 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003017 nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003018 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003019 }
3020 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003021
3022 tmp_defining = defining;
3023 defining = nasm_malloc(sizeof(MMacro));
3024 defining->prev = NULL;
3025 defining->name = NULL; /* flags this macro as a %rep block */
3026 defining->casesense = false;
3027 defining->plus = false;
3028 defining->nolist = nolist;
3029 defining->in_progress = count;
3030 defining->max_depth = 0;
3031 defining->nparam_min = defining->nparam_max = 0;
3032 defining->defaults = NULL;
3033 defining->dlist = NULL;
3034 defining->expansion = NULL;
3035 defining->next_active = istk->mstk;
3036 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003037 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003038
H. Peter Anvine2c80182005-01-15 22:15:51 +00003039 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003040 if (!defining || defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003041 nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003042 return DIRECTIVE_FOUND;
3043 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003044
H. Peter Anvine2c80182005-01-15 22:15:51 +00003045 /*
3046 * Now we have a "macro" defined - although it has no name
3047 * and we won't be entering it in the hash tables - we must
3048 * push a macro-end marker for it on to istk->expansion.
3049 * After that, it will take care of propagating itself (a
3050 * macro-end marker line for a macro which is really a %rep
3051 * block will cause the macro to be re-expanded, complete
3052 * with another macro-end marker to ensure the process
3053 * continues) until the whole expansion is forcibly removed
3054 * from istk->expansion by a %exitrep.
3055 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003056 l = nasm_malloc(sizeof(Line));
3057 l->next = istk->expansion;
3058 l->finishes = defining;
3059 l->first = NULL;
3060 istk->expansion = l;
3061
3062 istk->mstk = defining;
3063
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08003064 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003065 tmp_defining = defining;
3066 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003067 free_tlist(origline);
3068 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003069
H. Peter Anvine2c80182005-01-15 22:15:51 +00003070 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003071 /*
3072 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003073 * macro-end marker for a macro with no name. Then we set
3074 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003075 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003076 list_for_each(l, istk->expansion)
3077 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003078 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003079
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003080 if (l)
3081 l->finishes->in_progress = 1;
3082 else
H. Peter Anvin130736c2016-02-17 20:27:41 -08003083 nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003084 free_tlist(origline);
3085 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003086
H. Peter Anvine2c80182005-01-15 22:15:51 +00003087 case PP_XDEFINE:
3088 case PP_IXDEFINE:
3089 case PP_DEFINE:
3090 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003091 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003092
H. Peter Anvine2c80182005-01-15 22:15:51 +00003093 tline = tline->next;
3094 skip_white_(tline);
3095 tline = expand_id(tline);
3096 if (!tline || (tline->type != TOK_ID &&
3097 (tline->type != TOK_PREPROC_ID ||
3098 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003099 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003100 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003101 free_tlist(origline);
3102 return DIRECTIVE_FOUND;
3103 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003104
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003105 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003106 last = tline;
3107 param_start = tline = tline->next;
3108 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003109
H. Peter Anvine2c80182005-01-15 22:15:51 +00003110 /* Expand the macro definition now for %xdefine and %ixdefine */
3111 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3112 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003113
H. Peter Anvine2c80182005-01-15 22:15:51 +00003114 if (tok_is_(tline, "(")) {
3115 /*
3116 * This macro has parameters.
3117 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003118
H. Peter Anvine2c80182005-01-15 22:15:51 +00003119 tline = tline->next;
3120 while (1) {
3121 skip_white_(tline);
3122 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003123 nasm_error(ERR_NONFATAL, "parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003124 free_tlist(origline);
3125 return DIRECTIVE_FOUND;
3126 }
3127 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003128 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003129 "`%s': parameter identifier expected",
3130 tline->text);
3131 free_tlist(origline);
3132 return DIRECTIVE_FOUND;
3133 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003134 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003135 tline = tline->next;
3136 skip_white_(tline);
3137 if (tok_is_(tline, ",")) {
3138 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003139 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003140 if (!tok_is_(tline, ")")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003141 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003142 "`)' expected to terminate macro template");
3143 free_tlist(origline);
3144 return DIRECTIVE_FOUND;
3145 }
3146 break;
3147 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003148 }
3149 last = tline;
3150 tline = tline->next;
3151 }
3152 if (tok_type_(tline, TOK_WHITESPACE))
3153 last = tline, tline = tline->next;
3154 macro_start = NULL;
3155 last->next = NULL;
3156 t = tline;
3157 while (t) {
3158 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003159 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003160 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003161 !strcmp(tt->text, t->text))
3162 t->type = tt->type;
3163 }
3164 tt = t->next;
3165 t->next = macro_start;
3166 macro_start = t;
3167 t = tt;
3168 }
3169 /*
3170 * Good. We now have a macro name, a parameter count, and a
3171 * token list (in reverse order) for an expansion. We ought
3172 * to be OK just to create an SMacro, store it, and let
3173 * free_tlist have the rest of the line (which we have
3174 * carefully re-terminated after chopping off the expansion
3175 * from the end).
3176 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003177 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003178 free_tlist(origline);
3179 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003180
H. Peter Anvine2c80182005-01-15 22:15:51 +00003181 case PP_UNDEF:
3182 tline = tline->next;
3183 skip_white_(tline);
3184 tline = expand_id(tline);
3185 if (!tline || (tline->type != TOK_ID &&
3186 (tline->type != TOK_PREPROC_ID ||
3187 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003188 nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003189 free_tlist(origline);
3190 return DIRECTIVE_FOUND;
3191 }
3192 if (tline->next) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003193 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003194 "trailing garbage after macro name ignored");
3195 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003196
H. Peter Anvine2c80182005-01-15 22:15:51 +00003197 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003198 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003199 undef_smacro(ctx, mname);
3200 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003201 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003202
H. Peter Anvin9e200162008-06-04 17:23:14 -07003203 case PP_DEFSTR:
3204 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003205 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003206
3207 tline = tline->next;
3208 skip_white_(tline);
3209 tline = expand_id(tline);
3210 if (!tline || (tline->type != TOK_ID &&
3211 (tline->type != TOK_PREPROC_ID ||
3212 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003213 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003214 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003215 free_tlist(origline);
3216 return DIRECTIVE_FOUND;
3217 }
3218
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003219 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003220 last = tline;
3221 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003222 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003223
3224 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003225 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003226
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003227 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003228 macro_start = nasm_malloc(sizeof(*macro_start));
3229 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003230 macro_start->text = nasm_quote(p, strlen(p));
3231 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003232 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003233 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003234
3235 /*
3236 * We now have a macro name, an implicit parameter count of
3237 * zero, and a string token to use as an expansion. Create
3238 * and store an SMacro.
3239 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003240 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003241 free_tlist(origline);
3242 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003243
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003244 case PP_DEFTOK:
3245 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003246 casesense = (i == PP_DEFTOK);
3247
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003248 tline = tline->next;
3249 skip_white_(tline);
3250 tline = expand_id(tline);
3251 if (!tline || (tline->type != TOK_ID &&
3252 (tline->type != TOK_PREPROC_ID ||
3253 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003254 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003255 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003256 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003257 free_tlist(origline);
3258 return DIRECTIVE_FOUND;
3259 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003260 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003261 last = tline;
3262 tline = expand_smacro(tline->next);
3263 last->next = NULL;
3264
3265 t = tline;
3266 while (tok_type_(t, TOK_WHITESPACE))
3267 t = t->next;
3268 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003269 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003270 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003271 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003272 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003273 free_tlist(tline);
3274 free_tlist(origline);
3275 return DIRECTIVE_FOUND;
3276 }
3277
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003278 /*
3279 * Convert the string to a token stream. Note that smacros
3280 * are stored with the token stream reversed, so we have to
3281 * reverse the output of tokenize().
3282 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003283 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003284 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003285
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003286 /*
3287 * We now have a macro name, an implicit parameter count of
3288 * zero, and a numeric token to use as an expansion. Create
3289 * and store an SMacro.
3290 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003291 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003292 free_tlist(tline);
3293 free_tlist(origline);
3294 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003295
H. Peter Anvin418ca702008-05-30 10:42:30 -07003296 case PP_PATHSEARCH:
3297 {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07003298 char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003299
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003300 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003301
3302 tline = tline->next;
3303 skip_white_(tline);
3304 tline = expand_id(tline);
3305 if (!tline || (tline->type != TOK_ID &&
3306 (tline->type != TOK_PREPROC_ID ||
3307 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003308 nasm_error(ERR_NONFATAL,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003309 "`%%pathsearch' expects a macro identifier as first parameter");
3310 free_tlist(origline);
3311 return DIRECTIVE_FOUND;
3312 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003313 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003314 last = tline;
3315 tline = expand_smacro(tline->next);
3316 last->next = NULL;
3317
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003318 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003319 while (tok_type_(t, TOK_WHITESPACE))
3320 t = t->next;
3321
3322 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003323 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003324 nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003325 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003326 free_tlist(origline);
3327 return DIRECTIVE_FOUND; /* but we did _something_ */
3328 }
3329 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003330 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003331 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003332 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003333 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003334 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003335
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07003336 inc_fopen(p, NULL, NULL, &found_path, INC_PROBE, NF_BINARY);
3337 if (found_path)
3338 p = found_path;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003339 macro_start = nasm_malloc(sizeof(*macro_start));
3340 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003341 macro_start->text = nasm_quote(p, strlen(p));
3342 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003343 macro_start->a.mac = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07003344 if (found_path)
3345 nasm_free(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003346
3347 /*
3348 * We now have a macro name, an implicit parameter count of
3349 * zero, and a string token to use as an expansion. Create
3350 * and store an SMacro.
3351 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003352 define_smacro(ctx, mname, casesense, 0, macro_start);
3353 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003354 free_tlist(origline);
3355 return DIRECTIVE_FOUND;
3356 }
3357
H. Peter Anvine2c80182005-01-15 22:15:51 +00003358 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003359 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003360
H. Peter Anvine2c80182005-01-15 22:15:51 +00003361 tline = tline->next;
3362 skip_white_(tline);
3363 tline = expand_id(tline);
3364 if (!tline || (tline->type != TOK_ID &&
3365 (tline->type != TOK_PREPROC_ID ||
3366 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003367 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003368 "`%%strlen' expects a macro identifier as first parameter");
3369 free_tlist(origline);
3370 return DIRECTIVE_FOUND;
3371 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003372 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003373 last = tline;
3374 tline = expand_smacro(tline->next);
3375 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003376
H. Peter Anvine2c80182005-01-15 22:15:51 +00003377 t = tline;
3378 while (tok_type_(t, TOK_WHITESPACE))
3379 t = t->next;
3380 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003381 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003382 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003383 "`%%strlen` requires string as second parameter");
3384 free_tlist(tline);
3385 free_tlist(origline);
3386 return DIRECTIVE_FOUND;
3387 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003388
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003389 macro_start = nasm_malloc(sizeof(*macro_start));
3390 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003391 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003392 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003393
H. Peter Anvine2c80182005-01-15 22:15:51 +00003394 /*
3395 * We now have a macro name, an implicit parameter count of
3396 * zero, and a numeric token to use as an expansion. Create
3397 * and store an SMacro.
3398 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003399 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003400 free_tlist(tline);
3401 free_tlist(origline);
3402 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003403
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003404 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003405 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003406
3407 tline = tline->next;
3408 skip_white_(tline);
3409 tline = expand_id(tline);
3410 if (!tline || (tline->type != TOK_ID &&
3411 (tline->type != TOK_PREPROC_ID ||
3412 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003413 nasm_error(ERR_NONFATAL,
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003414 "`%%strcat' expects a macro identifier as first parameter");
3415 free_tlist(origline);
3416 return DIRECTIVE_FOUND;
3417 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003418 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003419 last = tline;
3420 tline = expand_smacro(tline->next);
3421 last->next = NULL;
3422
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003423 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003424 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003425 switch (t->type) {
3426 case TOK_WHITESPACE:
3427 break;
3428 case TOK_STRING:
3429 len += t->a.len = nasm_unquote(t->text, NULL);
3430 break;
3431 case TOK_OTHER:
3432 if (!strcmp(t->text, ",")) /* permit comma separators */
3433 break;
3434 /* else fall through */
3435 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003436 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003437 "non-string passed to `%%strcat' (%d)", t->type);
3438 free_tlist(tline);
3439 free_tlist(origline);
3440 return DIRECTIVE_FOUND;
3441 }
3442 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003443
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003444 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003445 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003446 if (t->type == TOK_STRING) {
3447 memcpy(p, t->text, t->a.len);
3448 p += t->a.len;
3449 }
3450 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003451
3452 /*
3453 * We now have a macro name, an implicit parameter count of
3454 * zero, and a numeric token to use as an expansion. Create
3455 * and store an SMacro.
3456 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003457 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3458 macro_start->text = nasm_quote(pp, len);
3459 nasm_free(pp);
3460 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003461 free_tlist(tline);
3462 free_tlist(origline);
3463 return DIRECTIVE_FOUND;
3464
H. Peter Anvine2c80182005-01-15 22:15:51 +00003465 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003466 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003467 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003468 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003469
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003470 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003471
H. Peter Anvine2c80182005-01-15 22:15:51 +00003472 tline = tline->next;
3473 skip_white_(tline);
3474 tline = expand_id(tline);
3475 if (!tline || (tline->type != TOK_ID &&
3476 (tline->type != TOK_PREPROC_ID ||
3477 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003478 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003479 "`%%substr' expects a macro identifier as first parameter");
3480 free_tlist(origline);
3481 return DIRECTIVE_FOUND;
3482 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003483 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003484 last = tline;
3485 tline = expand_smacro(tline->next);
3486 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003487
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003488 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003489 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003490 while (tok_type_(t, TOK_WHITESPACE))
3491 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003492
H. Peter Anvine2c80182005-01-15 22:15:51 +00003493 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003494 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003495 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003496 "`%%substr` requires string as second parameter");
3497 free_tlist(tline);
3498 free_tlist(origline);
3499 return DIRECTIVE_FOUND;
3500 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003501
H. Peter Anvine2c80182005-01-15 22:15:51 +00003502 tt = t->next;
3503 tptr = &tt;
3504 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003505 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003506 if (!evalresult) {
3507 free_tlist(tline);
3508 free_tlist(origline);
3509 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003510 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003511 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003512 free_tlist(tline);
3513 free_tlist(origline);
3514 return DIRECTIVE_FOUND;
3515 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003516 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003517
3518 while (tok_type_(tt, TOK_WHITESPACE))
3519 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003520 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003521 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003522 } else {
3523 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003524 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003525 if (!evalresult) {
3526 free_tlist(tline);
3527 free_tlist(origline);
3528 return DIRECTIVE_FOUND;
3529 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003530 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003531 free_tlist(tline);
3532 free_tlist(origline);
3533 return DIRECTIVE_FOUND;
3534 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003535 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003536 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003537
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003538 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003539
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003540 /* make start and count being in range */
3541 if (start < 0)
3542 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003543 if (count < 0)
3544 count = len + count + 1 - start;
3545 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003546 count = len - start;
3547 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003548 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003549
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003550 macro_start = nasm_malloc(sizeof(*macro_start));
3551 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003552 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003553 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003554 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003555
H. Peter Anvine2c80182005-01-15 22:15:51 +00003556 /*
3557 * We now have a macro name, an implicit parameter count of
3558 * zero, and a numeric token to use as an expansion. Create
3559 * and store an SMacro.
3560 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003561 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003562 free_tlist(tline);
3563 free_tlist(origline);
3564 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003565 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003566
H. Peter Anvine2c80182005-01-15 22:15:51 +00003567 case PP_ASSIGN:
3568 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003569 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003570
H. Peter Anvine2c80182005-01-15 22:15:51 +00003571 tline = tline->next;
3572 skip_white_(tline);
3573 tline = expand_id(tline);
3574 if (!tline || (tline->type != TOK_ID &&
3575 (tline->type != TOK_PREPROC_ID ||
3576 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003577 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003578 "`%%%sassign' expects a macro identifier",
3579 (i == PP_IASSIGN ? "i" : ""));
3580 free_tlist(origline);
3581 return DIRECTIVE_FOUND;
3582 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003583 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003584 last = tline;
3585 tline = expand_smacro(tline->next);
3586 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003587
H. Peter Anvine2c80182005-01-15 22:15:51 +00003588 t = tline;
3589 tptr = &t;
3590 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003591 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003592 free_tlist(tline);
3593 if (!evalresult) {
3594 free_tlist(origline);
3595 return DIRECTIVE_FOUND;
3596 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003597
H. Peter Anvine2c80182005-01-15 22:15:51 +00003598 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003599 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003600 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003601
H. Peter Anvine2c80182005-01-15 22:15:51 +00003602 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003603 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003604 "non-constant value given to `%%%sassign'",
3605 (i == PP_IASSIGN ? "i" : ""));
3606 free_tlist(origline);
3607 return DIRECTIVE_FOUND;
3608 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003609
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003610 macro_start = nasm_malloc(sizeof(*macro_start));
3611 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003612 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003613 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003614
H. Peter Anvine2c80182005-01-15 22:15:51 +00003615 /*
3616 * We now have a macro name, an implicit parameter count of
3617 * zero, and a numeric token to use as an expansion. Create
3618 * and store an SMacro.
3619 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003620 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003621 free_tlist(origline);
3622 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003623
H. Peter Anvine2c80182005-01-15 22:15:51 +00003624 case PP_LINE:
3625 /*
3626 * Syntax is `%line nnn[+mmm] [filename]'
3627 */
3628 tline = tline->next;
3629 skip_white_(tline);
3630 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003631 nasm_error(ERR_NONFATAL, "`%%line' expects line number");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003632 free_tlist(origline);
3633 return DIRECTIVE_FOUND;
3634 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003635 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003636 m = 1;
3637 tline = tline->next;
3638 if (tok_is_(tline, "+")) {
3639 tline = tline->next;
3640 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003641 nasm_error(ERR_NONFATAL, "`%%line' expects line increment");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003642 free_tlist(origline);
3643 return DIRECTIVE_FOUND;
3644 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003645 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003646 tline = tline->next;
3647 }
3648 skip_white_(tline);
3649 src_set_linnum(k);
3650 istk->lineinc = m;
3651 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003652 char *fname = detoken(tline, false);
3653 src_set_fname(fname);
3654 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003655 }
3656 free_tlist(origline);
3657 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003658
H. Peter Anvine2c80182005-01-15 22:15:51 +00003659 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003660 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003661 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003662 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003663 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003664 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003665}
3666
3667/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003668 * Ensure that a macro parameter contains a condition code and
3669 * nothing else. Return the condition code index if so, or -1
3670 * otherwise.
3671 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003672static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003673{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003674 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003675
H. Peter Anvin25a99342007-09-22 17:45:45 -07003676 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003677 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003678
H. Peter Anvineba20a72002-04-30 20:53:55 +00003679 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003680 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003681 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003682 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003683 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003684 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003685 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003686
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003687 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003688}
3689
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003690/*
3691 * This routines walks over tokens strem and hadnles tokens
3692 * pasting, if @handle_explicit passed then explicit pasting
3693 * term is handled, otherwise -- implicit pastings only.
3694 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003695static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003696 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003697{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003698 Token *tok, *next, **prev_next, **prev_nonspace;
3699 bool pasted = false;
3700 char *buf, *p;
3701 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003702
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003703 /*
3704 * The last token before pasting. We need it
3705 * to be able to connect new handled tokens.
3706 * In other words if there were a tokens stream
3707 *
3708 * A -> B -> C -> D
3709 *
3710 * and we've joined tokens B and C, the resulting
3711 * stream should be
3712 *
3713 * A -> BC -> D
3714 */
3715 tok = *head;
3716 prev_next = NULL;
3717
3718 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3719 prev_nonspace = head;
3720 else
3721 prev_nonspace = NULL;
3722
3723 while (tok && (next = tok->next)) {
3724
3725 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003726 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003727 /* Zap redundant whitespaces */
3728 while (tok_type_(next, TOK_WHITESPACE))
3729 next = delete_Token(next);
3730 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003731 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003732
3733 case TOK_PASTE:
3734 /* Explicit pasting */
3735 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003736 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003737 next = delete_Token(tok);
3738
3739 while (tok_type_(next, TOK_WHITESPACE))
3740 next = delete_Token(next);
3741
3742 if (!pasted)
3743 pasted = true;
3744
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003745 /* Left pasting token is start of line */
3746 if (!prev_nonspace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003747 nasm_error(ERR_FATAL, "No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003748
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003749 /*
3750 * No ending token, this might happen in two
3751 * cases
3752 *
3753 * 1) There indeed no right token at all
3754 * 2) There is a bare "%define ID" statement,
3755 * and @ID does expand to whitespace.
3756 *
3757 * So technically we need to do a grammar analysis
3758 * in another stage of parsing, but for now lets don't
3759 * change the behaviour people used to. Simply allow
3760 * whitespace after paste token.
3761 */
3762 if (!next) {
3763 /*
3764 * Zap ending space tokens and that's all.
3765 */
3766 tok = (*prev_nonspace)->next;
3767 while (tok_type_(tok, TOK_WHITESPACE))
3768 tok = delete_Token(tok);
3769 tok = *prev_nonspace;
3770 tok->next = NULL;
3771 break;
3772 }
3773
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003774 tok = *prev_nonspace;
3775 while (tok_type_(tok, TOK_WHITESPACE))
3776 tok = delete_Token(tok);
3777 len = strlen(tok->text);
3778 len += strlen(next->text);
3779
3780 p = buf = nasm_malloc(len + 1);
3781 strcpy(p, tok->text);
3782 p = strchr(p, '\0');
3783 strcpy(p, next->text);
3784
3785 delete_Token(tok);
3786
3787 tok = tokenize(buf);
3788 nasm_free(buf);
3789
3790 *prev_nonspace = tok;
3791 while (tok && tok->next)
3792 tok = tok->next;
3793
3794 tok->next = delete_Token(next);
3795
3796 /* Restart from pasted tokens head */
3797 tok = *prev_nonspace;
3798 break;
3799
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003800 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003801 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003802 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003803 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3804 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003805
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003806 len = 0;
3807 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3808 len += strlen(next->text);
3809 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003810 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003811
3812 /* No match */
3813 if (tok == next)
3814 break;
3815
3816 len += strlen(tok->text);
3817 p = buf = nasm_malloc(len + 1);
3818
3819 while (tok != next) {
3820 strcpy(p, tok->text);
3821 p = strchr(p, '\0');
3822 tok = delete_Token(tok);
3823 }
3824
3825 tok = tokenize(buf);
3826 nasm_free(buf);
3827
3828 if (prev_next)
3829 *prev_next = tok;
3830 else
3831 *head = tok;
3832
3833 /*
3834 * Connect pasted into original stream,
3835 * ie A -> new-tokens -> B
3836 */
3837 while (tok && tok->next)
3838 tok = tok->next;
3839 tok->next = next;
3840
3841 if (!pasted)
3842 pasted = true;
3843
3844 /* Restart from pasted tokens head */
3845 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003846 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003847
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003848 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003849 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003850
3851 prev_next = &tok->next;
3852
3853 if (tok->next &&
3854 !tok_type_(tok->next, TOK_WHITESPACE) &&
3855 !tok_type_(tok->next, TOK_PASTE))
3856 prev_nonspace = prev_next;
3857
3858 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003859 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003860
3861 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003862}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003863
3864/*
3865 * expands to a list of tokens from %{x:y}
3866 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003867static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003868{
3869 Token *t = tline, **tt, *tm, *head;
3870 char *pos;
3871 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003872
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003873 pos = strchr(tline->text, ':');
3874 nasm_assert(pos);
3875
3876 lst = atoi(pos + 1);
3877 fst = atoi(tline->text + 1);
3878
3879 /*
3880 * only macros params are accounted so
3881 * if someone passes %0 -- we reject such
3882 * value(s)
3883 */
3884 if (lst == 0 || fst == 0)
3885 goto err;
3886
3887 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003888 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3889 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003890 goto err;
3891
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003892 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3893 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003894
3895 /* counted from zero */
3896 fst--, lst--;
3897
3898 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003899 * It will be at least one token. Note we
3900 * need to scan params until separator, otherwise
3901 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003902 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003903 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003904 head = new_Token(NULL, tm->type, tm->text, 0);
3905 tt = &head->next, tm = tm->next;
3906 while (tok_isnt_(tm, ",")) {
3907 t = new_Token(NULL, tm->type, tm->text, 0);
3908 *tt = t, tt = &t->next, tm = tm->next;
3909 }
3910
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003911 if (fst < lst) {
3912 for (i = fst + 1; i <= lst; i++) {
3913 t = new_Token(NULL, TOK_OTHER, ",", 0);
3914 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003915 j = (i + mac->rotate) % mac->nparam;
3916 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003917 while (tok_isnt_(tm, ",")) {
3918 t = new_Token(NULL, tm->type, tm->text, 0);
3919 *tt = t, tt = &t->next, tm = tm->next;
3920 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003921 }
3922 } else {
3923 for (i = fst - 1; i >= lst; i--) {
3924 t = new_Token(NULL, TOK_OTHER, ",", 0);
3925 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003926 j = (i + mac->rotate) % mac->nparam;
3927 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003928 while (tok_isnt_(tm, ",")) {
3929 t = new_Token(NULL, tm->type, tm->text, 0);
3930 *tt = t, tt = &t->next, tm = tm->next;
3931 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003932 }
3933 }
3934
3935 *last = tt;
3936 return head;
3937
3938err:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003939 nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003940 &tline->text[1]);
3941 return tline;
3942}
3943
H. Peter Anvin76690a12002-04-30 20:52:49 +00003944/*
3945 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003946 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003947 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003948 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003949static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003950{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003951 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003952 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003953 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003954
3955 tail = &thead;
3956 thead = NULL;
3957
H. Peter Anvine2c80182005-01-15 22:15:51 +00003958 while (tline) {
3959 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003960 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3961 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3962 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003963 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003964 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003965 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003966 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003967 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003968 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003969
H. Peter Anvine2c80182005-01-15 22:15:51 +00003970 t = tline;
3971 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003972
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003973 mac = istk->mstk;
3974 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3975 mac = mac->next_active;
3976 if (!mac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003977 nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003978 } else {
3979 pos = strchr(t->text, ':');
3980 if (!pos) {
3981 switch (t->text[1]) {
3982 /*
3983 * We have to make a substitution of one of the
3984 * forms %1, %-1, %+1, %%foo, %0.
3985 */
3986 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003987 type = TOK_NUMBER;
3988 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3989 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003990 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003991 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003992 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003993 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003994 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003995 text = nasm_strcat(tmpbuf, t->text + 2);
3996 break;
3997 case '-':
3998 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003999 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004000 tt = NULL;
4001 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004002 if (mac->nparam > 1)
4003 n = (n + mac->rotate) % mac->nparam;
4004 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004005 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004006 cc = find_cc(tt);
4007 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004008 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004009 "macro parameter %d is not a condition code",
4010 n + 1);
4011 text = NULL;
4012 } else {
4013 type = TOK_ID;
4014 if (inverse_ccs[cc] == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004015 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004016 "condition code `%s' is not invertible",
4017 conditions[cc]);
4018 text = NULL;
4019 } else
4020 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4021 }
4022 break;
4023 case '+':
4024 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004025 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004026 tt = NULL;
4027 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004028 if (mac->nparam > 1)
4029 n = (n + mac->rotate) % mac->nparam;
4030 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004031 }
4032 cc = find_cc(tt);
4033 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004034 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004035 "macro parameter %d is not a condition code",
4036 n + 1);
4037 text = NULL;
4038 } else {
4039 type = TOK_ID;
4040 text = nasm_strdup(conditions[cc]);
4041 }
4042 break;
4043 default:
4044 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004045 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004046 tt = NULL;
4047 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004048 if (mac->nparam > 1)
4049 n = (n + mac->rotate) % mac->nparam;
4050 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004051 }
4052 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004053 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004054 *tail = new_Token(NULL, tt->type, tt->text, 0);
4055 tail = &(*tail)->next;
4056 tt = tt->next;
4057 }
4058 }
4059 text = NULL; /* we've done it here */
4060 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004061 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004062 } else {
4063 /*
4064 * seems we have a parameters range here
4065 */
4066 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004067 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004068 if (head != t) {
4069 *tail = head;
4070 *last = tline;
4071 tline = head;
4072 text = NULL;
4073 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004074 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004075 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004076 if (!text) {
4077 delete_Token(t);
4078 } else {
4079 *tail = t;
4080 tail = &t->next;
4081 t->type = type;
4082 nasm_free(t->text);
4083 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004084 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004085 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004086 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004087 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004088 } else if (tline->type == TOK_INDIRECT) {
4089 t = tline;
4090 tline = tline->next;
4091 tt = tokenize(t->text);
4092 tt = expand_mmac_params(tt);
4093 tt = expand_smacro(tt);
4094 *tail = tt;
4095 while (tt) {
4096 tt->a.mac = NULL; /* Necessary? */
4097 tail = &tt->next;
4098 tt = tt->next;
4099 }
4100 delete_Token(t);
4101 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004102 } else {
4103 t = *tail = tline;
4104 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004105 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004106 tail = &t->next;
4107 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004108 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004109 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004110
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004111 if (changed) {
4112 const struct tokseq_match t[] = {
4113 {
4114 PP_CONCAT_MASK(TOK_ID) |
4115 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4116 PP_CONCAT_MASK(TOK_ID) |
4117 PP_CONCAT_MASK(TOK_NUMBER) |
4118 PP_CONCAT_MASK(TOK_FLOAT) |
4119 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4120 },
4121 {
4122 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4123 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4124 }
4125 };
4126 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4127 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004128
H. Peter Anvin76690a12002-04-30 20:52:49 +00004129 return thead;
4130}
4131
4132/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004133 * Expand all single-line macro calls made in the given line.
4134 * Return the expanded version of the line. The original is deemed
4135 * to be destroyed in the process. (In reality we'll just move
4136 * Tokens from input to output a lot of the time, rather than
4137 * actually bothering to destroy and replicate.)
4138 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004139
H. Peter Anvine2c80182005-01-15 22:15:51 +00004140static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004141{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004142 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004143 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004144 Token **params;
4145 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004146 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004147 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004148 Token *org_tline = tline;
4149 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004150 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004151 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004152 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004153
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004154 /*
4155 * Trick: we should avoid changing the start token pointer since it can
4156 * be contained in "next" field of other token. Because of this
4157 * we allocate a copy of first token and work with it; at the end of
4158 * routine we copy it back
4159 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004160 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004161 tline = new_Token(org_tline->next, org_tline->type,
4162 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004163 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004164 nasm_free(org_tline->text);
4165 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004166 }
4167
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004168 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004169
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004170again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004171 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004172 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004173
H. Peter Anvine2c80182005-01-15 22:15:51 +00004174 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004175 if (!--deadman) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004176 nasm_error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004177 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004178 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004179
H. Peter Anvine2c80182005-01-15 22:15:51 +00004180 if ((mname = tline->text)) {
4181 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004182 if (tline->type == TOK_ID) {
4183 head = (SMacro *)hash_findix(&smacros, mname);
4184 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004185 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004186 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4187 } else
4188 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004189
H. Peter Anvine2c80182005-01-15 22:15:51 +00004190 /*
4191 * We've hit an identifier. As in is_mmacro below, we first
4192 * check whether the identifier is a single-line macro at
4193 * all, then think about checking for parameters if
4194 * necessary.
4195 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004196 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004197 if (!mstrcmp(m->name, mname, m->casesense))
4198 break;
4199 if (m) {
4200 mstart = tline;
4201 params = NULL;
4202 paramsize = NULL;
4203 if (m->nparam == 0) {
4204 /*
4205 * Simple case: the macro is parameterless. Discard the
4206 * one token that the macro call took, and push the
4207 * expansion back on the to-do stack.
4208 */
4209 if (!m->expansion) {
4210 if (!strcmp("__FILE__", m->name)) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004211 const char *file = src_get_fname();
4212 /* nasm_free(tline->text); here? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004213 tline->text = nasm_quote(file, strlen(file));
4214 tline->type = TOK_STRING;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004215 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004216 }
4217 if (!strcmp("__LINE__", m->name)) {
4218 nasm_free(tline->text);
4219 make_tok_num(tline, src_get_linnum());
4220 continue;
4221 }
4222 if (!strcmp("__BITS__", m->name)) {
4223 nasm_free(tline->text);
4224 make_tok_num(tline, globalbits);
4225 continue;
4226 }
4227 tline = delete_Token(tline);
4228 continue;
4229 }
4230 } else {
4231 /*
4232 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004233 * exists and takes parameters. We must find the
4234 * parameters in the call, count them, find the SMacro
4235 * that corresponds to that form of the macro call, and
4236 * substitute for the parameters when we expand. What a
4237 * pain.
4238 */
4239 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004240 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004241 do {
4242 t = tline->next;
4243 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004244 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004245 t->text = NULL;
4246 t = tline->next = delete_Token(t);
4247 }
4248 tline = t;
4249 } while (tok_type_(tline, TOK_WHITESPACE));
4250 if (!tok_is_(tline, "(")) {
4251 /*
4252 * This macro wasn't called with parameters: ignore
4253 * the call. (Behaviour borrowed from gnu cpp.)
4254 */
4255 tline = mstart;
4256 m = NULL;
4257 } else {
4258 int paren = 0;
4259 int white = 0;
4260 brackets = 0;
4261 nparam = 0;
4262 sparam = PARAM_DELTA;
4263 params = nasm_malloc(sparam * sizeof(Token *));
4264 params[0] = tline->next;
4265 paramsize = nasm_malloc(sparam * sizeof(int));
4266 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004267 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004268 /*
4269 * For some unusual expansions
4270 * which concatenates function call
4271 */
4272 t = tline->next;
4273 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004274 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004275 t->text = NULL;
4276 t = tline->next = delete_Token(t);
4277 }
4278 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004279
H. Peter Anvine2c80182005-01-15 22:15:51 +00004280 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004281 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004282 "macro call expects terminating `)'");
4283 break;
4284 }
4285 if (tline->type == TOK_WHITESPACE
4286 && brackets <= 0) {
4287 if (paramsize[nparam])
4288 white++;
4289 else
4290 params[nparam] = tline->next;
4291 continue; /* parameter loop */
4292 }
4293 if (tline->type == TOK_OTHER
4294 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004295 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004296 if (ch == ',' && !paren && brackets <= 0) {
4297 if (++nparam >= sparam) {
4298 sparam += PARAM_DELTA;
4299 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004300 sparam * sizeof(Token *));
4301 paramsize = nasm_realloc(paramsize,
4302 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004303 }
4304 params[nparam] = tline->next;
4305 paramsize[nparam] = 0;
4306 white = 0;
4307 continue; /* parameter loop */
4308 }
4309 if (ch == '{' &&
4310 (brackets > 0 || (brackets == 0 &&
4311 !paramsize[nparam])))
4312 {
4313 if (!(brackets++)) {
4314 params[nparam] = tline->next;
4315 continue; /* parameter loop */
4316 }
4317 }
4318 if (ch == '}' && brackets > 0)
4319 if (--brackets == 0) {
4320 brackets = -1;
4321 continue; /* parameter loop */
4322 }
4323 if (ch == '(' && !brackets)
4324 paren++;
4325 if (ch == ')' && brackets <= 0)
4326 if (--paren < 0)
4327 break;
4328 }
4329 if (brackets < 0) {
4330 brackets = 0;
H. Peter Anvin130736c2016-02-17 20:27:41 -08004331 nasm_error(ERR_NONFATAL, "braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004332 "enclose all of macro parameter");
4333 }
4334 paramsize[nparam] += white + 1;
4335 white = 0;
4336 } /* parameter loop */
4337 nparam++;
4338 while (m && (m->nparam != nparam ||
4339 mstrcmp(m->name, mname,
4340 m->casesense)))
4341 m = m->next;
4342 if (!m)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004343 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004344 "macro `%s' exists, "
4345 "but not taking %d parameters",
4346 mstart->text, nparam);
4347 }
4348 }
4349 if (m && m->in_progress)
4350 m = NULL;
4351 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004352 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004353 * Design question: should we handle !tline, which
4354 * indicates missing ')' here, or expand those
4355 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004356 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004357 */
4358 nasm_free(params);
4359 nasm_free(paramsize);
4360 tline = mstart;
4361 } else {
4362 /*
4363 * Expand the macro: we are placed on the last token of the
4364 * call, so that we can easily split the call from the
4365 * following tokens. We also start by pushing an SMAC_END
4366 * token for the cycle removal.
4367 */
4368 t = tline;
4369 if (t) {
4370 tline = t->next;
4371 t->next = NULL;
4372 }
4373 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004374 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004375 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004376 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004377 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004378 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004379 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004380 Token *ttt, *pt;
4381 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004382
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004383 ttt = params[t->type - TOK_SMAC_PARAM];
4384 i = paramsize[t->type - TOK_SMAC_PARAM];
4385 while (--i >= 0) {
4386 pt = *ptail = new_Token(tline, ttt->type,
4387 ttt->text, 0);
4388 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004389 ttt = ttt->next;
4390 }
4391 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004392 } else if (t->type == TOK_PREPROC_Q) {
4393 tt = new_Token(tline, TOK_ID, mname, 0);
4394 tline = tt;
4395 } else if (t->type == TOK_PREPROC_QQ) {
4396 tt = new_Token(tline, TOK_ID, m->name, 0);
4397 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004398 } else {
4399 tt = new_Token(tline, t->type, t->text, 0);
4400 tline = tt;
4401 }
4402 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004403
H. Peter Anvine2c80182005-01-15 22:15:51 +00004404 /*
4405 * Having done that, get rid of the macro call, and clean
4406 * up the parameters.
4407 */
4408 nasm_free(params);
4409 nasm_free(paramsize);
4410 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004411 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004412 continue; /* main token loop */
4413 }
4414 }
4415 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004416
H. Peter Anvine2c80182005-01-15 22:15:51 +00004417 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004418 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004419 tline = delete_Token(tline);
4420 } else {
4421 t = *tail = tline;
4422 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004423 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004424 t->next = NULL;
4425 tail = &t->next;
4426 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004427 }
4428
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004429 /*
4430 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004431 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004432 * TOK_IDs should be concatenated.
4433 * Also we look for %+ tokens and concatenate the tokens before and after
4434 * them (without white spaces in between).
4435 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004436 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004437 const struct tokseq_match t[] = {
4438 {
4439 PP_CONCAT_MASK(TOK_ID) |
4440 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4441 PP_CONCAT_MASK(TOK_ID) |
4442 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4443 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4444 }
4445 };
4446 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004447 /*
4448 * If we concatenated something, *and* we had previously expanded
4449 * an actual macro, scan the lines again for macros...
4450 */
4451 tline = thead;
4452 expanded = false;
4453 goto again;
4454 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004455 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004456
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004457err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004458 if (org_tline) {
4459 if (thead) {
4460 *org_tline = *thead;
4461 /* since we just gave text to org_line, don't free it */
4462 thead->text = NULL;
4463 delete_Token(thead);
4464 } else {
4465 /* the expression expanded to empty line;
4466 we can't return NULL for some reasons
4467 we just set the line to a single WHITESPACE token. */
4468 memset(org_tline, 0, sizeof(*org_tline));
4469 org_tline->text = NULL;
4470 org_tline->type = TOK_WHITESPACE;
4471 }
4472 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004473 }
4474
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004475 return thead;
4476}
4477
4478/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004479 * Similar to expand_smacro but used exclusively with macro identifiers
4480 * right before they are fetched in. The reason is that there can be
4481 * identifiers consisting of several subparts. We consider that if there
4482 * are more than one element forming the name, user wants a expansion,
4483 * otherwise it will be left as-is. Example:
4484 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004485 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004486 *
4487 * the identifier %$abc will be left as-is so that the handler for %define
4488 * will suck it and define the corresponding value. Other case:
4489 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004490 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004491 *
4492 * In this case user wants name to be expanded *before* %define starts
4493 * working, so we'll expand %$abc into something (if it has a value;
4494 * otherwise it will be left as-is) then concatenate all successive
4495 * PP_IDs into one.
4496 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004497static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004498{
4499 Token *cur, *oldnext = NULL;
4500
H. Peter Anvin734b1882002-04-30 21:01:08 +00004501 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004502 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004503
4504 cur = tline;
4505 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004506 (cur->next->type == TOK_ID ||
4507 cur->next->type == TOK_PREPROC_ID
4508 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004509 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004510
4511 /* If identifier consists of just one token, don't expand */
4512 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004513 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004514
H. Peter Anvine2c80182005-01-15 22:15:51 +00004515 if (cur) {
4516 oldnext = cur->next; /* Detach the tail past identifier */
4517 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004518 }
4519
H. Peter Anvin734b1882002-04-30 21:01:08 +00004520 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004521
H. Peter Anvine2c80182005-01-15 22:15:51 +00004522 if (cur) {
4523 /* expand_smacro possibly changhed tline; re-scan for EOL */
4524 cur = tline;
4525 while (cur && cur->next)
4526 cur = cur->next;
4527 if (cur)
4528 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004529 }
4530
4531 return tline;
4532}
4533
4534/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004535 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004536 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004537 * to check for an initial label - that's taken care of in
4538 * expand_mmacro - but must check numbers of parameters. Guaranteed
4539 * to be called with tline->type == TOK_ID, so the putative macro
4540 * name is easy to find.
4541 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004542static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004543{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004544 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004545 Token **params;
4546 int nparam;
4547
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004548 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004549
4550 /*
4551 * Efficiency: first we see if any macro exists with the given
4552 * name. If not, we can return NULL immediately. _Then_ we
4553 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004554 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004555 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004556 list_for_each(m, head)
4557 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004558 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004559 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004560 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004561
4562 /*
4563 * OK, we have a potential macro. Count and demarcate the
4564 * parameters.
4565 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004566 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004567
4568 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004569 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004570 * structure that handles this number.
4571 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004572 while (m) {
4573 if (m->nparam_min <= nparam
4574 && (m->plus || nparam <= m->nparam_max)) {
4575 /*
4576 * This one is right. Just check if cycle removal
4577 * prohibits us using it before we actually celebrate...
4578 */
4579 if (m->in_progress > m->max_depth) {
4580 if (m->max_depth > 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004581 nasm_error(ERR_WARNING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004582 "reached maximum recursion depth of %i",
4583 m->max_depth);
4584 }
4585 nasm_free(params);
4586 return NULL;
4587 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004588 /*
4589 * It's right, and we can use it. Add its default
4590 * parameters to the end of our list if necessary.
4591 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004592 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004593 params =
4594 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004595 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004596 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004597 while (nparam < m->nparam_min + m->ndefs) {
4598 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004599 nparam++;
4600 }
4601 }
4602 /*
4603 * If we've gone over the maximum parameter count (and
4604 * we're in Plus mode), ignore parameters beyond
4605 * nparam_max.
4606 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004607 if (m->plus && nparam > m->nparam_max)
4608 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004609 /*
4610 * Then terminate the parameter list, and leave.
4611 */
4612 if (!params) { /* need this special case */
4613 params = nasm_malloc(sizeof(*params));
4614 nparam = 0;
4615 }
4616 params[nparam] = NULL;
4617 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004618 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004619 }
4620 /*
4621 * This one wasn't right: look for the next one with the
4622 * same name.
4623 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004624 list_for_each(m, m->next)
4625 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004626 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004627 }
4628
4629 /*
4630 * After all that, we didn't find one with the right number of
4631 * parameters. Issue a warning, and fail to expand the macro.
4632 */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004633 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004634 "macro `%s' exists, but not taking %d parameters",
4635 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004636 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004637 return NULL;
4638}
4639
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004640
4641/*
4642 * Save MMacro invocation specific fields in
4643 * preparation for a recursive macro expansion
4644 */
4645static void push_mmacro(MMacro *m)
4646{
4647 MMacroInvocation *i;
4648
4649 i = nasm_malloc(sizeof(MMacroInvocation));
4650 i->prev = m->prev;
4651 i->params = m->params;
4652 i->iline = m->iline;
4653 i->nparam = m->nparam;
4654 i->rotate = m->rotate;
4655 i->paramlen = m->paramlen;
4656 i->unique = m->unique;
4657 i->condcnt = m->condcnt;
4658 m->prev = i;
4659}
4660
4661
4662/*
4663 * Restore MMacro invocation specific fields that were
4664 * saved during a previous recursive macro expansion
4665 */
4666static void pop_mmacro(MMacro *m)
4667{
4668 MMacroInvocation *i;
4669
4670 if (m->prev) {
4671 i = m->prev;
4672 m->prev = i->prev;
4673 m->params = i->params;
4674 m->iline = i->iline;
4675 m->nparam = i->nparam;
4676 m->rotate = i->rotate;
4677 m->paramlen = i->paramlen;
4678 m->unique = i->unique;
4679 m->condcnt = i->condcnt;
4680 nasm_free(i);
4681 }
4682}
4683
4684
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004685/*
4686 * Expand the multi-line macro call made by the given line, if
4687 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004688 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004689 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004690static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004691{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004692 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004693 Token *label = NULL;
4694 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004695 Token **params, *t, *tt;
4696 MMacro *m;
4697 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004698 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004699 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004700
4701 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004702 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004703 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004704 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004705 return 0;
4706 m = is_mmacro(t, &params);
4707 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004708 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004709 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004710 Token *last;
4711 /*
4712 * We have an id which isn't a macro call. We'll assume
4713 * it might be a label; we'll also check to see if a
4714 * colon follows it. Then, if there's another id after
4715 * that lot, we'll check it again for macro-hood.
4716 */
4717 label = last = t;
4718 t = t->next;
4719 if (tok_type_(t, TOK_WHITESPACE))
4720 last = t, t = t->next;
4721 if (tok_is_(t, ":")) {
4722 dont_prepend = 1;
4723 last = t, t = t->next;
4724 if (tok_type_(t, TOK_WHITESPACE))
4725 last = t, t = t->next;
4726 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004727 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4728 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004729 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004730 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004731 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004732 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004733
4734 /*
4735 * Fix up the parameters: this involves stripping leading and
4736 * trailing whitespace, then stripping braces if they are
4737 * present.
4738 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004739 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004740 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004741
H. Peter Anvine2c80182005-01-15 22:15:51 +00004742 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004743 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004744 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004745
H. Peter Anvine2c80182005-01-15 22:15:51 +00004746 t = params[i];
4747 skip_white_(t);
4748 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004749 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004750 params[i] = t;
4751 paramlen[i] = 0;
4752 while (t) {
4753 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4754 break; /* ... because we have hit a comma */
4755 if (comma && t->type == TOK_WHITESPACE
4756 && tok_is_(t->next, ","))
4757 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004758 if (brace && t->type == TOK_OTHER) {
4759 if (t->text[0] == '{')
4760 brace++; /* ... or a nested opening brace */
4761 else if (t->text[0] == '}')
4762 if (!--brace)
4763 break; /* ... or a brace */
4764 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004765 t = t->next;
4766 paramlen[i]++;
4767 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004768 if (brace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004769 nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004770 }
4771
4772 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004773 * OK, we have a MMacro structure together with a set of
4774 * parameters. We must now go through the expansion and push
4775 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004776 * parameter tokens and macro-local tokens doesn't get done
4777 * until the single-line macro substitution process; this is
4778 * because delaying them allows us to change the semantics
4779 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004780 *
4781 * First, push an end marker on to istk->expansion, mark this
4782 * macro as in progress, and set up its invocation-specific
4783 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004784 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004785 ll = nasm_malloc(sizeof(Line));
4786 ll->next = istk->expansion;
4787 ll->finishes = m;
4788 ll->first = NULL;
4789 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004790
4791 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004792 * Save the previous MMacro expansion in the case of
4793 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004794 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004795 if (m->max_depth && m->in_progress)
4796 push_mmacro(m);
4797
4798 m->in_progress ++;
4799 m->params = params;
4800 m->iline = tline;
4801 m->nparam = nparam;
4802 m->rotate = 0;
4803 m->paramlen = paramlen;
4804 m->unique = unique++;
4805 m->lineno = 0;
4806 m->condcnt = 0;
4807
4808 m->next_active = istk->mstk;
4809 istk->mstk = m;
4810
4811 list_for_each(l, m->expansion) {
4812 Token **tail;
4813
4814 ll = nasm_malloc(sizeof(Line));
4815 ll->finishes = NULL;
4816 ll->next = istk->expansion;
4817 istk->expansion = ll;
4818 tail = &ll->first;
4819
4820 list_for_each(t, l->first) {
4821 Token *x = t;
4822 switch (t->type) {
4823 case TOK_PREPROC_Q:
4824 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004825 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004826 case TOK_PREPROC_QQ:
4827 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4828 break;
4829 case TOK_PREPROC_ID:
4830 if (t->text[1] == '0' && t->text[2] == '0') {
4831 dont_prepend = -1;
4832 x = label;
4833 if (!x)
4834 continue;
4835 }
4836 /* fall through */
4837 default:
4838 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4839 break;
4840 }
4841 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004842 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004843 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004844 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004845
4846 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004847 * If we had a label, push it on as the first line of
4848 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004849 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004850 if (label) {
4851 if (dont_prepend < 0)
4852 free_tlist(startline);
4853 else {
4854 ll = nasm_malloc(sizeof(Line));
4855 ll->finishes = NULL;
4856 ll->next = istk->expansion;
4857 istk->expansion = ll;
4858 ll->first = startline;
4859 if (!dont_prepend) {
4860 while (label->next)
4861 label = label->next;
4862 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004863 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004864 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004865 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004866
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004867 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004868
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004869 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004870}
4871
H. Peter Anvin130736c2016-02-17 20:27:41 -08004872/*
4873 * This function adds macro names to error messages, and suppresses
4874 * them if necessary.
4875 */
4876static void pp_verror(int severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004877{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004878 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004879 MMacro *mmac = NULL;
4880 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004881
H. Peter Anvin130736c2016-02-17 20:27:41 -08004882 /*
4883 * If we're in a dead branch of IF or something like it, ignore the error.
4884 * However, because %else etc are evaluated in the state context
4885 * of the previous branch, errors might get lost:
4886 * %if 0 ... %else trailing garbage ... %endif
4887 * So %else etc should set the ERR_PP_PRECOND flag.
4888 */
4889 if ((severity & ERR_MASK) < ERR_FATAL &&
4890 istk && istk->conds &&
4891 ((severity & ERR_PP_PRECOND) ?
4892 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004893 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004894 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004895
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004896 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004897 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004898 mmac = istk->mstk;
4899 /* but %rep blocks should be skipped */
4900 while (mmac && !mmac->name)
4901 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004902 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004903
H. Peter Anvin130736c2016-02-17 20:27:41 -08004904 if (mmac) {
4905 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004906
H. Peter Anvin130736c2016-02-17 20:27:41 -08004907 nasm_set_verror(real_verror);
4908 nasm_error(severity, "(%s:%d) %s",
4909 mmac->name, mmac->lineno - delta, buff);
4910 nasm_set_verror(pp_verror);
4911 } else {
4912 real_verror(severity, fmt, arg);
4913 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004914}
4915
H. Peter Anvin734b1882002-04-30 21:01:08 +00004916static void
H. Peter Anvin130736c2016-02-17 20:27:41 -08004917pp_reset(char *file, int apass, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004918{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004919 Token *t;
4920
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004921 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004922 istk = nasm_malloc(sizeof(Include));
4923 istk->next = NULL;
4924 istk->conds = NULL;
4925 istk->expansion = NULL;
4926 istk->mstk = NULL;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07004927 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004928 istk->fname = NULL;
H. Peter Anvin274cda82016-05-10 02:56:29 -07004929 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004930 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004931 if (!istk->fp)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004932 nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004933 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004934 nested_mac_count = 0;
4935 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004936 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004937 unique = 0;
H. Peter Anvinf7606612016-07-13 14:23:48 -07004938
4939 if (tasm_compatible_mode)
4940 pp_add_stdmac(nasm_stdmac_tasm);
4941
4942 pp_add_stdmac(nasm_stdmac_nasm);
4943 pp_add_stdmac(nasm_stdmac_version);
4944
4945 stdmacpos = stdmacros[0];
4946 stdmacnext = &stdmacros[1];
4947
H. Peter Anvind2456592008-06-19 15:04:18 -07004948 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004949
4950 /*
4951 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4952 * The caller, however, will also pass in 3 for preprocess-only so
4953 * we can set __PASS__ accordingly.
4954 */
4955 pass = apass > 2 ? 2 : apass;
4956
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004957 dephead = deptail = deplist;
4958 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004959 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4960 sl->next = NULL;
4961 strcpy(sl->str, file);
4962 *deptail = sl;
4963 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004964 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004965
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004966 /*
4967 * Define the __PASS__ macro. This is defined here unlike
4968 * all the other builtins, because it is special -- it varies between
4969 * passes.
4970 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004971 t = nasm_malloc(sizeof(*t));
4972 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004973 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004974 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004975 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004976}
4977
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07004978static void pp_init(void)
4979{
4980 hash_init(&FileHash, HASH_MEDIUM);
4981}
4982
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004983static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004984{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004985 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004986 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004987
H. Peter Anvin130736c2016-02-17 20:27:41 -08004988 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08004989
H. Peter Anvine2c80182005-01-15 22:15:51 +00004990 while (1) {
4991 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004992 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004993 * buffer or from the input file.
4994 */
4995 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004996 while (istk->expansion && istk->expansion->finishes) {
4997 Line *l = istk->expansion;
4998 if (!l->finishes->name && l->finishes->in_progress > 1) {
4999 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005000
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005001 /*
5002 * This is a macro-end marker for a macro with no
5003 * name, which means it's not really a macro at all
5004 * but a %rep block, and the `in_progress' field is
5005 * more than 1, meaning that we still need to
5006 * repeat. (1 means the natural last repetition; 0
5007 * means termination by %exitrep.) We have
5008 * therefore expanded up to the %endrep, and must
5009 * push the whole block on to the expansion buffer
5010 * again. We don't bother to remove the macro-end
5011 * marker: we'd only have to generate another one
5012 * if we did.
5013 */
5014 l->finishes->in_progress--;
5015 list_for_each(l, l->finishes->expansion) {
5016 Token *t, *tt, **tail;
5017
5018 ll = nasm_malloc(sizeof(Line));
5019 ll->next = istk->expansion;
5020 ll->finishes = NULL;
5021 ll->first = NULL;
5022 tail = &ll->first;
5023
5024 list_for_each(t, l->first) {
5025 if (t->text || t->type == TOK_WHITESPACE) {
5026 tt = *tail = new_Token(NULL, t->type, t->text, 0);
5027 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005028 }
5029 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005030
5031 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005032 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005033 } else {
5034 /*
5035 * Check whether a `%rep' was started and not ended
5036 * within this macro expansion. This can happen and
5037 * should be detected. It's a fatal error because
5038 * I'm too confused to work out how to recover
5039 * sensibly from it.
5040 */
5041 if (defining) {
5042 if (defining->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005043 nasm_panic(0, "defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005044 else if (istk->mstk->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005045 nasm_fatal(0, "`%%rep' without `%%endrep' within"
5046 " expansion of macro `%s'",
5047 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005048 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005049
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005050 /*
5051 * FIXME: investigate the relationship at this point between
5052 * istk->mstk and l->finishes
5053 */
5054 {
5055 MMacro *m = istk->mstk;
5056 istk->mstk = m->next_active;
5057 if (m->name) {
5058 /*
5059 * This was a real macro call, not a %rep, and
5060 * therefore the parameter information needs to
5061 * be freed.
5062 */
5063 if (m->prev) {
5064 pop_mmacro(m);
5065 l->finishes->in_progress --;
5066 } else {
5067 nasm_free(m->params);
5068 free_tlist(m->iline);
5069 nasm_free(m->paramlen);
5070 l->finishes->in_progress = 0;
5071 }
5072 } else
5073 free_mmacro(m);
5074 }
5075 istk->expansion = l->next;
5076 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005077 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005078 }
5079 }
5080 while (1) { /* until we get a line we can use */
5081
5082 if (istk->expansion) { /* from a macro expansion */
5083 char *p;
5084 Line *l = istk->expansion;
5085 if (istk->mstk)
5086 istk->mstk->lineno++;
5087 tline = l->first;
5088 istk->expansion = l->next;
5089 nasm_free(l);
5090 p = detoken(tline, false);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005091 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005092 nasm_free(p);
5093 break;
5094 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005095 line = read_line();
5096 if (line) { /* from the current input file */
5097 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005098 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005099 nasm_free(line);
5100 break;
5101 }
5102 /*
5103 * The current file has ended; work down the istk
5104 */
5105 {
5106 Include *i = istk;
5107 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005108 if (i->conds) {
5109 /* nasm_error can't be conditionally suppressed */
H. Peter Anvin41087062016-03-03 14:27:34 -08005110 nasm_fatal(0,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005111 "expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005112 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005113 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005114 if (i->next)
5115 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005116 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005117 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005118 nasm_free(i);
H. Peter Anvin130736c2016-02-17 20:27:41 -08005119 if (!istk) {
5120 line = NULL;
5121 goto done;
5122 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005123 if (istk->expansion && istk->expansion->finishes)
5124 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005125 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005126 }
5127
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005128 /*
5129 * We must expand MMacro parameters and MMacro-local labels
5130 * _before_ we plunge into directive processing, to cope
5131 * with things like `%define something %1' such as STRUC
5132 * uses. Unless we're _defining_ a MMacro, in which case
5133 * those tokens should be left alone to go into the
5134 * definition; and unless we're in a non-emitting
5135 * condition, in which case we don't want to meddle with
5136 * anything.
5137 */
5138 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5139 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005140 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005141 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005142
H. Peter Anvine2c80182005-01-15 22:15:51 +00005143 /*
5144 * Check the line to see if it's a preprocessor directive.
5145 */
5146 if (do_directive(tline) == DIRECTIVE_FOUND) {
5147 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005148 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005149 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005150 * We're defining a multi-line macro. We emit nothing
5151 * at all, and just
5152 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005153 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005154 Line *l = nasm_malloc(sizeof(Line));
5155 l->next = defining->expansion;
5156 l->first = tline;
5157 l->finishes = NULL;
5158 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005159 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005160 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005161 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005162 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005163 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005164 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005165 * directive so we keep our place correctly.
5166 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005167 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005168 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005169 } else if (istk->mstk && !istk->mstk->in_progress) {
5170 /*
5171 * We're in a %rep block which has been terminated, so
5172 * we're walking through to the %endrep without
5173 * emitting anything. Emit nothing at all, not even a
5174 * blank line: when we emerge from the %rep block we'll
5175 * give a line-number directive so we keep our place
5176 * correctly.
5177 */
5178 free_tlist(tline);
5179 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005180 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005181 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005182 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005183 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005184 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005185 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005186 line = detoken(tline, true);
5187 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005188 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005189 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005190 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005191 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005192 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005193 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005194
H. Peter Anvin130736c2016-02-17 20:27:41 -08005195done:
5196 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005197 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005198}
5199
H. Peter Anvine2c80182005-01-15 22:15:51 +00005200static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005201{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005202 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005203
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005204 if (defining) {
5205 if (defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005206 nasm_error(ERR_NONFATAL,
5207 "end of file while still defining macro `%s'",
5208 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005209 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005210 nasm_error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005211 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005212
5213 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005214 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005215 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005216
5217 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005218
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005219 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005220 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005221 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005222 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005223 Include *i = istk;
5224 istk = istk->next;
5225 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005226 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005227 }
5228 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005229 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005230 src_set_fname(NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005231 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005232 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005233 free_llist(predef);
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005234 predef = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005235 delete_Blocks();
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005236 freeTokens = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005237 while ((i = ipath)) {
5238 ipath = i->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005239 if (i->path)
5240 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005241 nasm_free(i);
5242 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005243 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005244}
5245
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005246static void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005247{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005248 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005249
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005250 i = nasm_malloc(sizeof(IncPath));
5251 i->path = path ? nasm_strdup(path) : NULL;
5252 i->next = NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005253
H. Peter Anvin89cee572009-07-15 09:16:54 -04005254 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005255 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005256 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005257 j = j->next;
5258 j->next = i;
5259 } else {
5260 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005261 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005262}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005263
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005264static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005265{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005266 Token *inc, *space, *name;
5267 Line *l;
5268
H. Peter Anvin734b1882002-04-30 21:01:08 +00005269 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5270 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5271 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005272
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005273 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005274 l->next = predef;
5275 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005276 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005277 predef = l;
5278}
5279
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005280static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005281{
5282 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005283 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005284 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005285
H. Peter Anvin130736c2016-02-17 20:27:41 -08005286 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005287
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005288 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005289 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5290 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005291 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005292 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005293 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005294 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005295 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005296
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005297 if (space->next->type != TOK_PREPROC_ID &&
5298 space->next->type != TOK_ID)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005299 nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005300
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005301 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005302 l->next = predef;
5303 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005304 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005305 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005306
5307 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005308}
5309
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005310static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005311{
5312 Token *def, *space;
5313 Line *l;
5314
H. Peter Anvin734b1882002-04-30 21:01:08 +00005315 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5316 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005317 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005318
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005319 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005320 l->next = predef;
5321 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005322 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005323 predef = l;
5324}
5325
H. Peter Anvinf7606612016-07-13 14:23:48 -07005326static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005327{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005328 macros_t **mp;
5329
5330 /* Find the end of the list and avoid duplicates */
5331 for (mp = stdmacros; *mp; mp++) {
5332 if (*mp == macros)
5333 return; /* Nothing to do */
5334 }
5335
5336 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5337
5338 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005339}
5340
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005341static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005342{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005343 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005344 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005345 tok->text = nasm_strdup(numbuf);
5346 tok->type = TOK_NUMBER;
5347}
5348
H. Peter Anvin37368952016-05-09 14:10:32 -07005349static void pp_list_one_macro(MMacro *m, int severity)
5350{
5351 if (!m)
5352 return;
5353
5354 /* We need to print the next_active list in reverse order */
5355 pp_list_one_macro(m->next_active, severity);
5356
5357 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005358 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvin37368952016-05-09 14:10:32 -07005359 nasm_error(severity, "... from macro `%s' defined here", m->name);
5360 }
5361}
5362
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005363static void pp_error_list_macros(int severity)
5364{
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005365 int32_t saved_line;
5366 const char *saved_fname = NULL;
5367
5368 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY;
H. Peter Anvin274cda82016-05-10 02:56:29 -07005369 src_get(&saved_line, &saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005370
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005371 if (istk)
5372 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005373
H. Peter Anvin274cda82016-05-10 02:56:29 -07005374 src_set(saved_line, saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005375}
5376
H. Peter Anvine7469712016-02-18 02:20:59 -08005377const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005378 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005379 pp_reset,
5380 pp_getline,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005381 pp_cleanup,
H. Peter Anvinf7606612016-07-13 14:23:48 -07005382 pp_add_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005383 pp_pre_define,
5384 pp_pre_undefine,
5385 pp_pre_include,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005386 pp_include_path,
5387 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005388};