blob: a7d8252b72f434cff54c3efdc022ece29c1d1b87 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
H. Peter Anvin130736c2016-02-17 20:27:41 -08003 * Copyright 1996-2016 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000065#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000066#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000067#include <stdlib.h>
68#include <stddef.h>
69#include <string.h>
70#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000071#include <limits.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000072
73#include "nasm.h"
74#include "nasmlib.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000075#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070076#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070077#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070078#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070079#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070080#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070081#include "tables.h"
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -080082#include "listing.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000083
84typedef struct SMacro SMacro;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080085typedef struct MMacro MMacro;
86typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000087typedef struct Context Context;
88typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000089typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000090typedef struct Line Line;
91typedef struct Include Include;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080092typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000093typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000094
95/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070096 * Note on the storage of both SMacro and MMacros: the hash table
97 * indexes them case-insensitively, and we then have to go through a
98 * linked list of potential case aliases (and, for MMacros, parameter
99 * ranges); this is to preserve the matching semantics of the earlier
100 * code. If the number of case aliases for a specific macro is a
101 * performance issue, you may want to reconsider your coding style.
102 */
103
104/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 * Store the definition of a single-line macro.
106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107struct SMacro {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800108 SMacro *next;
109 char *name;
110 bool casesense;
111 bool in_progress;
112 unsigned int nparam;
113 Token *expansion;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000114};
115
116/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800117 * Store the definition of a multi-line macro. This is also used to
118 * store the interiors of `%rep...%endrep' blocks, which are
119 * effectively self-re-invoking multi-line macros which simply
120 * don't have a name or bother to appear in the hash tables. %rep
121 * blocks are signified by having a NULL `name' field.
122 *
123 * In a MMacro describing a `%rep' block, the `in_progress' field
124 * isn't merely boolean, but gives the number of repeats left to
125 * run.
126 *
127 * The `next' field is used for storing MMacros in hash tables; the
128 * `next_active' field is for stacking them on istk entries.
129 *
130 * When a MMacro is being expanded, `params', `iline', `nparam',
131 * `paramlen', `rotate' and `unique' are local to the invocation.
132 */
133struct MMacro {
134 MMacro *next;
135 MMacroInvocation *prev; /* previous invocation */
136 char *name;
137 int nparam_min, nparam_max;
138 bool casesense;
139 bool plus; /* is the last parameter greedy? */
140 bool nolist; /* is this macro listing-inhibited? */
141 int64_t in_progress; /* is this macro currently being expanded? */
142 int32_t max_depth; /* maximum number of recursive expansions allowed */
143 Token *dlist; /* All defaults as one list */
144 Token **defaults; /* Parameter default pointers */
145 int ndefs; /* number of default parameters */
146 Line *expansion;
147
148 MMacro *next_active;
149 MMacro *rep_nest; /* used for nesting %rep */
150 Token **params; /* actual parameters */
151 Token *iline; /* invocation line */
152 unsigned int nparam, rotate;
153 int *paramlen;
154 uint64_t unique;
155 int lineno; /* Current line number on expansion */
156 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700157
H. Peter Anvin274cda82016-05-10 02:56:29 -0700158 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700159 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800160};
161
162
163/* Store the definition of a multi-line macro, as defined in a
164 * previous recursive macro expansion.
165 */
166struct MMacroInvocation {
167 MMacroInvocation *prev; /* previous invocation */
168 Token **params; /* actual parameters */
169 Token *iline; /* invocation line */
170 unsigned int nparam, rotate;
171 int *paramlen;
172 uint64_t unique;
173 uint64_t condcnt;
174};
175
176
177/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000178 * The context stack is composed of a linked list of these.
179 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000180struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800181 Context *next;
182 char *name;
183 struct hash_table localmac;
184 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000185};
186
187/*
188 * This is the internal form which we break input lines up into.
189 * Typically stored in linked lists.
190 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000191 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
192 * necessarily used as-is, but is intended to denote the number of
193 * the substituted parameter. So in the definition
194 *
195 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700196 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000197 * the token representing `x' will have its type changed to
198 * TOK_SMAC_PARAM, but the one representing `y' will be
199 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000200 *
201 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
202 * which doesn't need quotes around it. Used in the pre-include
203 * mechanism as an alternative to trying to find a sensible type of
204 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000205 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000206enum pp_token_type {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800207 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
208 TOK_PREPROC_ID, TOK_STRING,
209 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700210 TOK_INTERNAL_STRING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800211 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300212 TOK_PASTE, /* %+ */
213 TOK_INDIRECT, /* %[...] */
214 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
215 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000216};
217
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400218#define PP_CONCAT_MASK(x) (1 << (x))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +0400219#define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400220
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400221struct tokseq_match {
222 int mask_head;
223 int mask_tail;
224};
225
H. Peter Anvine2c80182005-01-15 22:15:51 +0000226struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800227 Token *next;
228 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700229 union {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800230 SMacro *mac; /* associated macro for TOK_SMAC_END */
231 size_t len; /* scratch length field */
232 } a; /* Auxiliary data */
233 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000234};
235
236/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800237 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000238 * these, which is essentially a container to allow several linked
239 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700240 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000241 * Note that in this module, linked lists are treated as stacks
242 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800243 * `expansion' field in MMacro structures, so that the linked list,
244 * if walked, would give the macro lines in reverse order; this
245 * means that we can walk the list when expanding a macro, and thus
246 * push the lines on to the `expansion' field in _istk_ in reverse
247 * order (so that when popped back off they are in the right
248 * order). It may seem cockeyed, and it relies on my design having
249 * an even number of steps in, but it works...
250 *
251 * Some of these structures, rather than being actual lines, are
252 * markers delimiting the end of the expansion of a given macro.
253 * This is for use in the cycle-tracking and %rep-handling code.
254 * Such structures have `finishes' non-NULL, and `first' NULL. All
255 * others have `finishes' NULL, but `first' may still be NULL if
256 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000257 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000258struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800259 Line *next;
260 MMacro *finishes;
261 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500262};
263
264/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000265 * To handle an arbitrary level of file inclusion, we maintain a
266 * stack (ie linked list) of these things.
267 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000268struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800269 Include *next;
270 FILE *fp;
271 Cond *conds;
272 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700273 const char *fname;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800274 int lineno, lineinc;
275 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000276};
277
278/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000279 * Include search path. This is simply a list of strings which get
280 * prepended, in turn, to the name of an include file, in an
281 * attempt to find the file if it's not in the current directory.
282 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000283struct IncPath {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800284 IncPath *next;
285 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000286};
287
288/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700289 * File real name hash, so we don't have to re-search the include
290 * path for every pass (and potentially more than that if a file
291 * is used more than once.)
292 */
293struct hash_table FileHash;
294
295/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000296 * Conditional assembly: we maintain a separate stack of these for
297 * each level of file inclusion. (The only reason we keep the
298 * stacks separate is to ensure that a stray `%endif' in a file
299 * included from within the true branch of a `%if' won't terminate
300 * it and cause confusion: instead, rightly, it'll cause an error.)
301 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800302struct Cond {
303 Cond *next;
304 int state;
305};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000306enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000307 /*
308 * These states are for use just after %if or %elif: IF_TRUE
309 * means the condition has evaluated to truth so we are
310 * currently emitting, whereas IF_FALSE means we are not
311 * currently emitting but will start doing so if a %else comes
312 * up. In these states, all directives are admissible: %elif,
313 * %else and %endif. (And of course %if.)
314 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800315 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000316 /*
317 * These states come up after a %else: ELSE_TRUE means we're
318 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
319 * any %elif or %else will cause an error.
320 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800321 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000322 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200323 * These states mean that we're not emitting now, and also that
324 * nothing until %endif will be emitted at all. COND_DONE is
325 * used when we've had our moment of emission
326 * and have now started seeing %elifs. COND_NEVER is used when
327 * the condition construct in question is contained within a
328 * non-emitting branch of a larger condition construct,
329 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000330 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800331 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000332};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800333#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000334
H. Peter Anvin70653092007-10-19 14:42:29 -0700335/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000336 * These defines are used as the possible return values for do_directive
337 */
338#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300339#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000340
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800342 * This define sets the upper limit for smacro and recursive mmacro
343 * expansions
Keith Kanios852f1ee2009-07-12 00:19:55 -0500344 */
345#define DEADMAN_LIMIT (1 << 20)
346
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400347/* max reps */
348#define REP_LIMIT ((INT64_C(1) << 62))
349
Keith Kanios852f1ee2009-07-12 00:19:55 -0500350/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000351 * Condition codes. Note that we use c_ prefix not C_ because C_ is
352 * used in nasm.h for the "real" condition codes. At _this_ level,
353 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
354 * ones, so we need a different enum...
355 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700356static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000357 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
358 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000359 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000360};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700361enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000362 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
363 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
H. Peter Anvin476d2862007-10-02 22:04:15 -0700364 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
365 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000366};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700367static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000368 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
369 c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
H. Peter Anvince9be342007-09-12 00:22:29 +0000370 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000371};
372
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800373/*
374 * Directive names.
375 */
376/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
377static int is_condition(enum preproc_token arg)
378{
379 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
380}
381
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000382/* For TASM compatibility we need to be able to recognise TASM compatible
383 * conditional compilation directives. Using the NASM pre-processor does
384 * not work, so we look for them specifically from the following list and
385 * then jam in the equivalent NASM directive into the input stream.
386 */
387
H. Peter Anvine2c80182005-01-15 22:15:51 +0000388enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000389 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
390 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
391};
392
H. Peter Anvin476d2862007-10-02 22:04:15 -0700393static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000394 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
395 "ifndef", "include", "local"
396};
397
398static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700399static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000400static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800401static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000402
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000403static Context *cstk;
404static Include *istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800405static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000406
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300407static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9924d1e2016-10-04 00:59:39 -0700408static StrList **dephead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000409
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300410static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000411
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800412static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700413static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000414
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000415/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800416 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000417 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800418static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000419
420/*
421 * The current set of single-line macros we have defined.
422 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700423static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000424
425/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800426 * The multi-line macro we are currently defining, or the %rep
427 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000428 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800429static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000430
Charles Crayned4200be2008-07-12 16:42:33 -0700431static uint64_t nested_mac_count;
432static uint64_t nested_rep_count;
433
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000434/*
435 * The number of macro parameters to allocate space for at a time.
436 */
437#define PARAM_DELTA 16
438
439/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700440 * The standard macro set: defined in macros.c in a set of arrays.
441 * This gives our position in any macro set, while we are processing it.
442 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000443 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700444static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700445static macros_t **stdmacnext;
446static macros_t *stdmacros[8];
H. Peter Anvin76690a12002-04-30 20:52:49 +0000447
448/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000449 * Tokens are allocated in blocks to improve speed
450 */
451#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800452static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000453struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000454 Blocks *next;
455 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000456};
457
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800458static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000459
460/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000461 * Forward declarations.
462 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700463static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000464static Token *expand_mmac_params(Token * tline);
465static Token *expand_smacro(Token * tline);
466static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400467static Context *get_ctx(const char *name, const char **namep);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700468static void make_tok_num(Token * tok, int64_t val);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800469static void pp_verror(int severity, const char *fmt, va_list ap);
470static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000471static void *new_Block(size_t size);
472static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700473static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300474 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000475static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000476
477/*
478 * Macros for safe checking of token pointers, avoid *(NULL)
479 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300480#define tok_type_(x,t) ((x) && (x)->type == (t))
481#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
482#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
483#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000484
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400485/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700486 * nasm_unquote with error if the string contains NUL characters.
487 * If the string contains NUL characters, issue an error and return
488 * the C len, i.e. truncate at the NUL.
489 */
490static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
491{
492 size_t len = nasm_unquote(qstr, NULL);
493 size_t clen = strlen(qstr);
494
495 if (len != clen)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800496 nasm_error(ERR_NONFATAL, "NUL character in `%s' directive",
H. Peter Anvin077fb932010-07-20 14:56:30 -0700497 pp_directives[directive]);
498
499 return clen;
500}
501
502/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700503 * In-place reverse a list of tokens.
504 */
505static Token *reverse_tokens(Token *t)
506{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800507 Token *prev = NULL;
508 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700509
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800510 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400511 next = t->next;
512 t->next = prev;
513 prev = t;
514 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800515 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700516
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800517 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700518}
519
520/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300521 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000522 * front of them. We do it here because I could not find any other
523 * place to do it for the moment, and it is a hack (ideally it would
524 * be nice to be able to use the NASM pre-processor to do it).
525 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000526static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000527{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000528 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400529 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000530
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400531 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000532
533 /* Binary search for the directive name */
534 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400535 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400536 q = nasm_skip_word(p);
537 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000538 if (len) {
539 oldchar = p[len];
540 p[len] = 0;
541 while (j - i > 1) {
542 k = (j + i) / 2;
543 m = nasm_stricmp(p, tasm_directives[k]);
544 if (m == 0) {
545 /* We have found a directive, so jam a % in front of it
546 * so that NASM will then recognise it as one if it's own.
547 */
548 p[len] = oldchar;
549 len = strlen(p);
550 oldline = line;
551 line = nasm_malloc(len + 2);
552 line[0] = '%';
553 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700554 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300555 * NASM does not recognise IFDIFI, so we convert
556 * it to %if 0. This is not used in NASM
557 * compatible code, but does need to parse for the
558 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000559 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700560 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000561 } else {
562 memcpy(line + 1, p, len + 1);
563 }
564 nasm_free(oldline);
565 return line;
566 } else if (m < 0) {
567 j = k;
568 } else
569 i = k;
570 }
571 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000572 }
573 return line;
574}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000575
H. Peter Anvin76690a12002-04-30 20:52:49 +0000576/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000577 * The pre-preprocessing stage... This function translates line
578 * number indications as they emerge from GNU cpp (`# lineno "file"
579 * flags') into NASM preprocessor line number indications (`%line
580 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000581 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000582static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000583{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000584 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000585 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000586
H. Peter Anvine2c80182005-01-15 22:15:51 +0000587 if (line[0] == '#' && line[1] == ' ') {
588 oldline = line;
589 fname = oldline + 2;
590 lineno = atoi(fname);
591 fname += strspn(fname, "0123456789 ");
592 if (*fname == '"')
593 fname++;
594 fnlen = strcspn(fname, "\"");
595 line = nasm_malloc(20 + fnlen);
596 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
597 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000598 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000599 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000600 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000601 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000602}
603
604/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000605 * Free a linked list of tokens.
606 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000607static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000608{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400609 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000610 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000611}
612
613/*
614 * Free a linked list of lines.
615 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000616static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000617{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400618 Line *l, *tmp;
619 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000620 free_tlist(l->first);
621 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000622 }
623}
624
625/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800626 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000627 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800628static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000629{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800630 nasm_free(m->name);
631 free_tlist(m->dlist);
632 nasm_free(m->defaults);
633 free_llist(m->expansion);
634 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000635}
636
637/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700638 * Free all currently defined macros, and free the hash tables
639 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700640static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700641{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400642 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700643 const char *key;
644 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700645
H. Peter Anvin072771e2008-05-22 13:17:51 -0700646 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300647 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400648 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300649 nasm_free(s->name);
650 free_tlist(s->expansion);
651 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300652 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700653 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700654 hash_free(smt);
655}
656
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800657static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700658{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800659 MMacro *m, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700660 const char *key;
661 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700662
663 it = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800664 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300665 nasm_free((void *)key);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800666 list_for_each_safe(m ,tmp, m)
667 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700668 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800669 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700670}
671
672static void free_macros(void)
673{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700674 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800675 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700676}
677
678/*
679 * Initialize the hash tables
680 */
681static void init_macros(void)
682{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700683 hash_init(&smacros, HASH_LARGE);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800684 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700685}
686
687/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000688 * Pop the context stack.
689 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000690static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000691{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000692 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000693
694 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700695 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000696 nasm_free(c->name);
697 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000698}
699
H. Peter Anvin072771e2008-05-22 13:17:51 -0700700/*
701 * Search for a key in the hash index; adding it if necessary
702 * (in which case we initialize the data pointer to NULL.)
703 */
704static void **
705hash_findi_add(struct hash_table *hash, const char *str)
706{
707 struct hash_insert hi;
708 void **r;
709 char *strx;
710
711 r = hash_findi(hash, str, &hi);
712 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300713 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700714
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300715 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700716 return hash_add(&hi, strx, NULL);
717}
718
719/*
720 * Like hash_findi, but returns the data element rather than a pointer
721 * to it. Used only when not adding a new element, hence no third
722 * argument.
723 */
724static void *
725hash_findix(struct hash_table *hash, const char *str)
726{
727 void **p;
728
729 p = hash_findi(hash, str, NULL);
730 return p ? *p : NULL;
731}
732
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400733/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800734 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400735 * if there no more left -- return NULL
736 */
737static char *line_from_stdmac(void)
738{
739 unsigned char c;
740 const unsigned char *p = stdmacpos;
741 char *line, *q;
742 size_t len = 0;
743
744 if (!stdmacpos)
745 return NULL;
746
747 while ((c = *p++)) {
748 if (c >= 0x80)
749 len += pp_directives_len[c - 0x80] + 1;
750 else
751 len++;
752 }
753
754 line = nasm_malloc(len + 1);
755 q = line;
756 while ((c = *stdmacpos++)) {
757 if (c >= 0x80) {
758 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
759 q += pp_directives_len[c - 0x80];
760 *q++ = ' ';
761 } else {
762 *q++ = c;
763 }
764 }
765 stdmacpos = p;
766 *q = '\0';
767
768 if (!*stdmacpos) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700769 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400770 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700771 if (*stdmacnext) {
772 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400773 } else if (do_predef) {
774 Line *pd, *l;
775 Token *head, **tail, *t;
776
777 /*
778 * Nasty hack: here we push the contents of
779 * `predef' on to the top-level expansion stack,
780 * since this is the most convenient way to
781 * implement the pre-include and pre-define
782 * features.
783 */
784 list_for_each(pd, predef) {
785 head = NULL;
786 tail = &head;
787 list_for_each(t, pd->first) {
788 *tail = new_Token(NULL, t->type, t->text, 0);
789 tail = &(*tail)->next;
790 }
791
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800792 l = nasm_malloc(sizeof(Line));
793 l->next = istk->expansion;
794 l->first = head;
795 l->finishes = NULL;
796
797 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400798 }
799 do_predef = false;
800 }
801 }
802
803 return line;
804}
805
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000806static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000807{
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400808 unsigned int size, c, next;
809 const unsigned int delta = 512;
810 const unsigned int pad = 8;
811 unsigned int nr_cont = 0;
812 bool cont = false;
813 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000814
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400815 /* Standart macros set (predefined) goes first */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400816 p = line_from_stdmac();
817 if (p)
818 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700819
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400820 size = delta;
821 p = buffer = nasm_malloc(size);
822
823 for (;;) {
824 c = fgetc(istk->fp);
825 if ((int)(c) == EOF) {
826 p[0] = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000827 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000828 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400829
830 switch (c) {
831 case '\r':
832 next = fgetc(istk->fp);
833 if (next != '\n')
834 ungetc(next, istk->fp);
835 if (cont) {
836 cont = false;
837 continue;
838 }
839 break;
840
841 case '\n':
842 if (cont) {
843 cont = false;
844 continue;
845 }
846 break;
847
848 case '\\':
849 next = fgetc(istk->fp);
850 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400851 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400852 cont = true;
853 nr_cont++;
854 continue;
855 }
856 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000857 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400858
859 if (c == '\r' || c == '\n') {
860 *p++ = 0;
861 break;
862 }
863
864 if (p >= (buffer + size - pad)) {
865 buffer = nasm_realloc(buffer, size + delta);
866 p = buffer + size - pad;
867 size += delta;
868 }
869
870 *p++ = (unsigned char)c;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000871 }
872
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400873 if (p == buffer) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000874 nasm_free(buffer);
875 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000876 }
877
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300878 src_set_linnum(src_get_linnum() + istk->lineinc +
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400879 (nr_cont * istk->lineinc));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000880
881 /*
882 * Handle spurious ^Z, which may be inserted into source files
883 * by some file transfer utilities.
884 */
885 buffer[strcspn(buffer, "\032")] = '\0';
886
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -0800887 lfmt->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000888
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000889 return buffer;
890}
891
892/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000893 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000894 * don't need to parse the value out of e.g. numeric tokens: we
895 * simply split one string into many.
896 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000897static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000898{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700899 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000900 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000901 Token *list = NULL;
902 Token *t, **tail = &list;
903
H. Peter Anvine2c80182005-01-15 22:15:51 +0000904 while (*line) {
905 p = line;
906 if (*p == '%') {
907 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300908 if (*p == '+' && !nasm_isdigit(p[1])) {
909 p++;
910 type = TOK_PASTE;
911 } else if (nasm_isdigit(*p) ||
912 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000913 do {
914 p++;
915 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700916 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000917 type = TOK_PREPROC_ID;
918 } else if (*p == '{') {
919 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800920 while (*p) {
921 if (*p == '}')
922 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000923 p[-1] = *p;
924 p++;
925 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800926 if (*p != '}')
H. Peter Anvin130736c2016-02-17 20:27:41 -0800927 nasm_error(ERR_WARNING | ERR_PASS1,
928 "unterminated %%{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000929 p[-1] = '\0';
930 if (*p)
931 p++;
932 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300933 } else if (*p == '[') {
934 int lvl = 1;
935 line += 2; /* Skip the leading %[ */
936 p++;
937 while (lvl && (c = *p++)) {
938 switch (c) {
939 case ']':
940 lvl--;
941 break;
942 case '%':
943 if (*p == '[')
944 lvl++;
945 break;
946 case '\'':
947 case '\"':
948 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400949 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300950 break;
951 default:
952 break;
953 }
954 }
955 p--;
956 if (*p)
957 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800958 if (lvl)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800959 nasm_error(ERR_NONFATAL|ERR_PASS1,
960 "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300961 type = TOK_INDIRECT;
962 } else if (*p == '?') {
963 type = TOK_PREPROC_Q; /* %? */
964 p++;
965 if (*p == '?') {
966 type = TOK_PREPROC_QQ; /* %?? */
967 p++;
968 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400969 } else if (*p == '!') {
970 type = TOK_PREPROC_ID;
971 p++;
972 if (isidchar(*p)) {
973 do {
974 p++;
975 }
976 while (isidchar(*p));
977 } else if (*p == '\'' || *p == '\"' || *p == '`') {
978 p = nasm_skip_string(p);
979 if (*p)
980 p++;
981 else
H. Peter Anvin130736c2016-02-17 20:27:41 -0800982 nasm_error(ERR_NONFATAL|ERR_PASS1,
983 "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400984 } else {
985 /* %! without string or identifier */
986 type = TOK_OTHER; /* Legacy behavior... */
987 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 } else if (isidchar(*p) ||
989 ((*p == '!' || *p == '%' || *p == '$') &&
990 isidchar(p[1]))) {
991 do {
992 p++;
993 }
994 while (isidchar(*p));
995 type = TOK_PREPROC_ID;
996 } else {
997 type = TOK_OTHER;
998 if (*p == '%')
999 p++;
1000 }
1001 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1002 type = TOK_ID;
1003 p++;
1004 while (*p && isidchar(*p))
1005 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001006 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 /*
1008 * A string token.
1009 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001011 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001012
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 if (*p) {
1014 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001015 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001016 nasm_error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 /* Handling unterminated strings by UNV */
1018 /* type = -1; */
1019 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001020 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001021 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001022 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001024 bool is_hex = false;
1025 bool is_float = false;
1026 bool has_e = false;
1027 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001028
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001030 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001031 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001032
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001033 if (*p == '$') {
1034 p++;
1035 is_hex = true;
1036 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001037
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001038 for (;;) {
1039 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001040
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001041 if (!is_hex && (c == 'e' || c == 'E')) {
1042 has_e = true;
1043 if (*p == '+' || *p == '-') {
1044 /*
1045 * e can only be followed by +/- if it is either a
1046 * prefixed hex number or a floating-point number
1047 */
1048 p++;
1049 is_float = true;
1050 }
1051 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1052 is_hex = true;
1053 } else if (c == 'P' || c == 'p') {
1054 is_float = true;
1055 if (*p == '+' || *p == '-')
1056 p++;
1057 } else if (isnumchar(c) || c == '_')
1058 ; /* just advance */
1059 else if (c == '.') {
1060 /*
1061 * we need to deal with consequences of the legacy
1062 * parser, like "1.nolist" being two tokens
1063 * (TOK_NUMBER, TOK_ID) here; at least give it
1064 * a shot for now. In the future, we probably need
1065 * a flex-based scanner with proper pattern matching
1066 * to do it as well as it can be done. Nothing in
1067 * the world is going to help the person who wants
1068 * 0x123.p16 interpreted as two tokens, though.
1069 */
1070 r = p;
1071 while (*r == '_')
1072 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001073
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001074 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1075 (!is_hex && (*r == 'e' || *r == 'E')) ||
1076 (*r == 'p' || *r == 'P')) {
1077 p = r;
1078 is_float = true;
1079 } else
1080 break; /* Terminate the token */
1081 } else
1082 break;
1083 }
1084 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001085
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001086 if (p == line+1 && *line == '$') {
1087 type = TOK_OTHER; /* TOKEN_HERE */
1088 } else {
1089 if (has_e && !is_hex) {
1090 /* 1e13 is floating-point, but 1e13h is not */
1091 is_float = true;
1092 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001093
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001094 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1095 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001096 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001097 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001098 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 /*
1100 * Whitespace just before end-of-line is discarded by
1101 * pretending it's a comment; whitespace just before a
1102 * comment gets lumped into the comment.
1103 */
1104 if (!*p || *p == ';') {
1105 type = TOK_COMMENT;
1106 while (*p)
1107 p++;
1108 }
1109 } else if (*p == ';') {
1110 type = TOK_COMMENT;
1111 while (*p)
1112 p++;
1113 } else {
1114 /*
1115 * Anything else is an operator of some kind. We check
1116 * for all the double-character operators (>>, <<, //,
1117 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001118 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001119 */
1120 type = TOK_OTHER;
1121 if ((p[0] == '>' && p[1] == '>') ||
1122 (p[0] == '<' && p[1] == '<') ||
1123 (p[0] == '/' && p[1] == '/') ||
1124 (p[0] == '<' && p[1] == '=') ||
1125 (p[0] == '>' && p[1] == '=') ||
1126 (p[0] == '=' && p[1] == '=') ||
1127 (p[0] == '!' && p[1] == '=') ||
1128 (p[0] == '<' && p[1] == '>') ||
1129 (p[0] == '&' && p[1] == '&') ||
1130 (p[0] == '|' && p[1] == '|') ||
1131 (p[0] == '^' && p[1] == '^')) {
1132 p++;
1133 }
1134 p++;
1135 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001136
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 /* Handling unterminated string by UNV */
1138 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001139 {
1140 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1141 t->text[p-line] = *line;
1142 tail = &t->next;
1143 }
1144 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145 if (type != TOK_COMMENT) {
1146 *tail = t = new_Token(NULL, type, line, p - line);
1147 tail = &t->next;
1148 }
1149 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001150 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001151 return list;
1152}
1153
H. Peter Anvince616072002-04-30 21:02:23 +00001154/*
1155 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001156 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001157 * deleted only all at once by the delete_Blocks function.
1158 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001160{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001161 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001163 /* first, get to the end of the linked list */
1164 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001165 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001166 /* now allocate the requested chunk */
1167 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001168
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001169 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001170 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001171 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001172}
1173
1174/*
1175 * this function deletes all managed blocks of memory
1176 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001178{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001179 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001180
H. Peter Anvin70653092007-10-19 14:42:29 -07001181 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001182 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001183 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001184 * free it.
1185 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001187 if (b->chunk)
1188 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001189 a = b;
1190 b = b->next;
1191 if (a != &blocks)
1192 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001193 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001194 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001195}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001196
1197/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001198 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001199 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001200 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001201 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001202static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001203 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001204{
1205 Token *t;
1206 int i;
1207
H. Peter Anvin89cee572009-07-15 09:16:54 -04001208 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1210 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1211 freeTokens[i].next = &freeTokens[i + 1];
1212 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001213 }
1214 t = freeTokens;
1215 freeTokens = t->next;
1216 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001217 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001218 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001219 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001220 t->text = NULL;
1221 } else {
1222 if (txtlen == 0)
1223 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001224 t->text = nasm_malloc(txtlen+1);
1225 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001226 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001227 }
1228 return t;
1229}
1230
H. Peter Anvine2c80182005-01-15 22:15:51 +00001231static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001232{
1233 Token *next = t->next;
1234 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001235 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001236 freeTokens = t;
1237 return next;
1238}
1239
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001240/*
1241 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001242 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1243 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001244 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001245static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001246{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001247 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001248 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001249 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001250 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001251
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001252 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001253 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001254 char *v;
1255 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001256
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001257 v = t->text + 2;
1258 if (*v == '\'' || *v == '\"' || *v == '`') {
1259 size_t len = nasm_unquote(v, NULL);
1260 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001261
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001262 if (len != clen) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001263 nasm_error(ERR_NONFATAL | ERR_PASS1,
1264 "NUL character in %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001265 v = NULL;
1266 }
1267 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001268
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001269 if (v) {
1270 char *p = getenv(v);
1271 if (!p) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001272 nasm_error(ERR_NONFATAL | ERR_PASS1,
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001273 "nonexistent environment variable `%s'", v);
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001274 /*
1275 * FIXME We better should investigate if accessing
1276 * ->text[1] without ->text[0] is safe enough.
1277 */
1278 t->text = nasm_zalloc(2);
1279 } else
1280 t->text = nasm_strdup(p);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001281 }
1282 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001283 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001284
H. Peter Anvine2c80182005-01-15 22:15:51 +00001285 /* Expand local macros here and not during preprocessing */
1286 if (expand_locals &&
1287 t->type == TOK_PREPROC_ID && t->text &&
1288 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001289 const char *q;
1290 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001291 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001293 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001294 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001295 p = nasm_strcat(buffer, q);
1296 nasm_free(t->text);
1297 t->text = p;
1298 }
1299 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001300 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001301 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001302 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001304 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001305
H. Peter Anvin734b1882002-04-30 21:01:08 +00001306 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001307
1308 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001309 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001310 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001312 q = t->text;
1313 while (*q)
1314 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001315 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001316 }
1317 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001318
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001319 return line;
1320}
1321
1322/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001323 * A scanner, suitable for use by the expression evaluator, which
1324 * operates on a line of Tokens. Expects a pointer to a pointer to
1325 * the first token in the line to be passed in as its private_data
1326 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001327 *
1328 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001329 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001330static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001331{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001332 Token **tlineptr = private_data;
1333 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001334 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001335
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 do {
1337 tline = *tlineptr;
1338 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001339 } while (tline && (tline->type == TOK_WHITESPACE ||
1340 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001341
1342 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001344
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001345 tokval->t_charptr = tline->text;
1346
H. Peter Anvin76690a12002-04-30 20:52:49 +00001347 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001348 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001349 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001351
H. Peter Anvine2c80182005-01-15 22:15:51 +00001352 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001353 p = tokval->t_charptr = tline->text;
1354 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001355 tokval->t_charptr++;
1356 return tokval->t_type = TOKEN_ID;
1357 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001358
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001359 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001360 if (r >= p+MAX_KEYWORD)
1361 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001362 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001363 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001364 *s = '\0';
1365 /* right, so we have an identifier sitting in temp storage. now,
1366 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001367 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001368 }
1369
H. Peter Anvine2c80182005-01-15 22:15:51 +00001370 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001371 bool rn_error;
1372 tokval->t_integer = readnum(tline->text, &rn_error);
1373 tokval->t_charptr = tline->text;
1374 if (rn_error)
1375 return tokval->t_type = TOKEN_ERRNUM;
1376 else
1377 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001378 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001379
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001380 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001381 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001382 }
1383
H. Peter Anvine2c80182005-01-15 22:15:51 +00001384 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001385 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001386
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001387 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001388 tokval->t_charptr = tline->text;
1389 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001390
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001391 if (ep[0] != bq || ep[1] != '\0')
1392 return tokval->t_type = TOKEN_ERRSTR;
1393 else
1394 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001395 }
1396
H. Peter Anvine2c80182005-01-15 22:15:51 +00001397 if (tline->type == TOK_OTHER) {
1398 if (!strcmp(tline->text, "<<"))
1399 return tokval->t_type = TOKEN_SHL;
1400 if (!strcmp(tline->text, ">>"))
1401 return tokval->t_type = TOKEN_SHR;
1402 if (!strcmp(tline->text, "//"))
1403 return tokval->t_type = TOKEN_SDIV;
1404 if (!strcmp(tline->text, "%%"))
1405 return tokval->t_type = TOKEN_SMOD;
1406 if (!strcmp(tline->text, "=="))
1407 return tokval->t_type = TOKEN_EQ;
1408 if (!strcmp(tline->text, "<>"))
1409 return tokval->t_type = TOKEN_NE;
1410 if (!strcmp(tline->text, "!="))
1411 return tokval->t_type = TOKEN_NE;
1412 if (!strcmp(tline->text, "<="))
1413 return tokval->t_type = TOKEN_LE;
1414 if (!strcmp(tline->text, ">="))
1415 return tokval->t_type = TOKEN_GE;
1416 if (!strcmp(tline->text, "&&"))
1417 return tokval->t_type = TOKEN_DBL_AND;
1418 if (!strcmp(tline->text, "^^"))
1419 return tokval->t_type = TOKEN_DBL_XOR;
1420 if (!strcmp(tline->text, "||"))
1421 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001422 }
1423
1424 /*
1425 * We have no other options: just return the first character of
1426 * the token text.
1427 */
1428 return tokval->t_type = tline->text[0];
1429}
1430
1431/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001432 * Compare a string to the name of an existing macro; this is a
1433 * simple wrapper which calls either strcmp or nasm_stricmp
1434 * depending on the value of the `casesense' parameter.
1435 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001436static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001437{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001438 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001439}
1440
1441/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001442 * Compare a string to the name of an existing macro; this is a
1443 * simple wrapper which calls either strcmp or nasm_stricmp
1444 * depending on the value of the `casesense' parameter.
1445 */
1446static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1447{
1448 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1449}
1450
1451/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001452 * Return the Context structure associated with a %$ token. Return
1453 * NULL, having _already_ reported an error condition, if the
1454 * context stack isn't deep enough for the supplied number of $
1455 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001456 *
1457 * If "namep" is non-NULL, set it to the pointer to the macro name
1458 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001459 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001460static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001461{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001462 Context *ctx;
1463 int i;
1464
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001465 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001466 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001467
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001468 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001469 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001470
H. Peter Anvine2c80182005-01-15 22:15:51 +00001471 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001472 nasm_error(ERR_NONFATAL, "`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001473 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001474 }
1475
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001476 name += 2;
1477 ctx = cstk;
1478 i = 0;
1479 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001480 name++;
1481 i++;
1482 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001483 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001484 if (!ctx) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001485 nasm_error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001486 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001487 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001488 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001489
1490 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001491 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001492
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001493 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001494}
1495
1496/*
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001497 * Append a string list entry to a string list if and only if it isn't
1498 * already there. Return true if it was added.
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001499 */
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001500static bool add_to_list(StrList **head, StrList *entry)
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001501{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001502 StrList *list;
1503
1504 if (!head)
1505 return false;
1506
1507 list = *head;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001508 while (list) {
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001509 if (!strcmp(list->str, entry->str))
1510 return false;
1511 head = &list->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001512 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001513 }
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001514
1515 *head = entry;
1516 entry->next = NULL;
1517 return true;
1518}
1519
1520/*
1521 * Append a string to a string list if and only if it isn't
1522 * already there. Return true if it was added.
1523 */
1524static bool add_string_to_list(StrList **head, const char *str)
1525{
1526 StrList *list;
1527
1528 if (!head)
1529 return false;
1530
1531 list = *head;
1532 while (list) {
1533 if (!strcmp(list->str, str))
1534 return false;
1535 head = &list->next;
1536 list = list->next;
1537 }
1538
1539 *head = nasm_str_to_strlist(str);
1540 return true;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001541}
1542
1543/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001544 * Open an include file. This routine must always return a valid
1545 * file pointer if it returns - it's responsible for throwing an
1546 * ERR_FATAL and bombing out completely if not. It should also try
1547 * the include path one by one until it finds the file or reaches
1548 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001549 *
1550 * Note: for INC_PROBE the function returns NULL at all times;
1551 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001552 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001553enum incopen_mode {
1554 INC_NEEDED, /* File must exist */
1555 INC_OPTIONAL, /* Missing is OK */
1556 INC_PROBE /* Only an existence probe */
1557};
1558
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001559/* This is conducts a full pathname search */
1560static FILE *inc_fopen_search(const char *file, StrList **slpath,
1561 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001562{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001563 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001564 char *prefix = "";
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001565 const IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001566 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001567 size_t prefix_len = 0;
1568 StrList *sl;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001569 size_t path_len;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001570 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001571
H. Peter Anvine2c80182005-01-15 22:15:51 +00001572 while (1) {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001573 path_len = prefix_len + len + 1;
1574
1575 sl = nasm_malloc(path_len + sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001576 memcpy(sl->str, prefix, prefix_len);
1577 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001578 sl->next = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001579
H. Peter Anvind81a2352016-09-21 14:03:18 -07001580 if (omode == INC_PROBE) {
1581 fp = NULL;
1582 found = nasm_file_exists(sl->str);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001583 } else {
H. Peter Anvind81a2352016-09-21 14:03:18 -07001584 fp = nasm_open_read(sl->str, fmode);
1585 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001586 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001587 if (found) {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001588 *slpath = sl;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001589 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001590 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001591
H. Peter Anvind81a2352016-09-21 14:03:18 -07001592 nasm_free(sl);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001593
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001594 if (!ip)
1595 return NULL;
1596
1597 prefix = ip->path;
1598 prefix_len = strlen(prefix);
1599 ip = ip->next;
1600 }
1601}
1602
1603/*
1604 * Open a file, or test for the presence of one (depending on omode),
1605 * considering the include path.
1606 */
1607static FILE *inc_fopen(const char *file,
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001608 StrList **dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001609 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001610 enum incopen_mode omode,
1611 enum file_flags fmode)
1612{
1613 StrList *sl;
1614 struct hash_insert hi;
1615 void **hp;
1616 char *path;
1617 FILE *fp = NULL;
1618
1619 hp = hash_find(&FileHash, file, &hi);
1620 if (hp) {
1621 path = *hp;
1622 } else {
1623 /* Need to do the actual path search */
1624 size_t file_len;
1625
1626 sl = NULL;
1627 fp = inc_fopen_search(file, &sl, omode, fmode);
1628
1629 file_len = strlen(file);
1630
1631 if (!sl) {
1632 /* Store negative result for this file */
1633 sl = nasm_malloc(file_len + 1 + sizeof sl->next);
1634 memcpy(sl->str, file, file_len+1);
1635 sl->next = NULL;
1636 file = sl->str;
1637 path = NULL;
1638 } else {
1639 path = sl->str;
1640 file = strchr(path, '\0') - file_len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001641 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001642
1643 hash_add(&hi, file, path); /* Positive or negative result */
1644
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001645 /*
1646 * Add file to dependency path. The in_list() is needed
1647 * in case the file was already added with %depend.
1648 */
1649 if (path || omode != INC_NEEDED)
1650 add_to_list(dhead, sl);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001651 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001652
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001653 if (!path) {
1654 if (omode == INC_NEEDED)
1655 nasm_fatal(0, "unable to open include file `%s'", file);
1656
1657 if (found_path)
1658 *found_path = NULL;
1659
1660 return NULL;
1661 }
1662
1663 if (!fp && omode != INC_PROBE)
1664 fp = nasm_open_read(file, fmode);
1665
1666 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001667 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001668
1669 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001670}
1671
1672/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001673 * Opens an include or input file. Public version, for use by modules
1674 * that get a file:lineno pair and need to look at the file again
1675 * (e.g. the CodeView debug backend). Returns NULL on failure.
1676 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001677FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001678{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001679 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001680}
1681
1682/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001683 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001684 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001685 * return true if _any_ single-line macro of that name is defined.
1686 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001687 * `nparam' or no parameters is defined.
1688 *
1689 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001690 * defined, or nparam is -1, the address of the definition structure
1691 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001692 * is NULL, no action will be taken regarding its contents, and no
1693 * error will occur.
1694 *
1695 * Note that this is also called with nparam zero to resolve
1696 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001697 *
1698 * If you already know which context macro belongs to, you can pass
1699 * the context pointer as first parameter; if you won't but name begins
1700 * with %$ the context will be automatically computed. If all_contexts
1701 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001702 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001703static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001704smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001705 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001706{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001707 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001708 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001709
H. Peter Anvin97a23472007-09-16 17:57:25 -07001710 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001711 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001712 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001713 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001714 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001715 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001716 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001717 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001718 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001719 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001720 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001721 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001722
H. Peter Anvine2c80182005-01-15 22:15:51 +00001723 while (m) {
1724 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001725 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001726 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001727 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001728 *defn = m;
1729 else
1730 *defn = NULL;
1731 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001732 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001733 }
1734 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001735 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001736
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001737 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001738}
1739
1740/*
1741 * Count and mark off the parameters in a multi-line macro call.
1742 * This is called both from within the multi-line macro expansion
1743 * code, and also to mark off the default parameters when provided
1744 * in a %macro definition line.
1745 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001746static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001747{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001748 int paramsize, brace;
1749
1750 *nparam = paramsize = 0;
1751 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001752 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001753 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001754 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001755 paramsize += PARAM_DELTA;
1756 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1757 }
1758 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001759 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001760 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001761 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001762 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001763 if (brace) {
1764 while (brace && (t = t->next) != NULL) {
1765 if (tok_is_(t, "{"))
1766 brace++;
1767 else if (tok_is_(t, "}"))
1768 brace--;
1769 }
1770
1771 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001772 /*
1773 * Now we've found the closing brace, look further
1774 * for the comma.
1775 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001776 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001777 skip_white_(t);
1778 if (tok_isnt_(t, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001779 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001780 "braces do not enclose all of macro parameter");
1781 while (tok_isnt_(t, ","))
1782 t = t->next;
1783 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001784 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001785 } else {
1786 while (tok_isnt_(t, ","))
1787 t = t->next;
1788 }
1789 if (t) { /* got a comma/brace */
1790 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001791 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001792 }
1793}
1794
1795/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001796 * Determine whether one of the various `if' conditions is true or
1797 * not.
1798 *
1799 * We must free the tline we get passed.
1800 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001801static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001802{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001803 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001804 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001805 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001806 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001807 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001808 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001809 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001810
1811 origline = tline;
1812
H. Peter Anvine2c80182005-01-15 22:15:51 +00001813 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001814 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001815 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001816 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001818 if (!tline)
1819 break;
1820 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001821 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001822 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823 free_tlist(origline);
1824 return -1;
1825 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001826 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001827 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001828 tline = tline->next;
1829 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001830 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001831
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001832 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001833 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001834 while (tline) {
1835 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001836 if (!tline || (tline->type != TOK_ID &&
1837 (tline->type != TOK_PREPROC_ID ||
1838 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001839 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001840 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001841 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001842 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001843 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001844 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001845 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001846 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001847 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001848
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001849 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001850 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001851 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001852 while (tline) {
1853 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001854 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001855 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001856 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001857 tline->text[1] != '!'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001858 nasm_error(ERR_NONFATAL,
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001859 "`%s' expects environment variable names",
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001860 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001861 goto fail;
1862 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001863 p = tline->text;
1864 if (tline->type == TOK_PREPROC_ID)
1865 p += 2; /* Skip leading %! */
1866 if (*p == '\'' || *p == '\"' || *p == '`')
1867 nasm_unquote_cstr(p, ct);
1868 if (getenv(p))
1869 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001870 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001871 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001872 break;
1873
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001874 case PPC_IFIDN:
1875 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001876 tline = expand_smacro(tline);
1877 t = tt = tline;
1878 while (tok_isnt_(tt, ","))
1879 tt = tt->next;
1880 if (!tt) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001881 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001882 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001883 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001884 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001885 }
1886 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001887 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001888 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1889 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001890 nasm_error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001891 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001892 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001893 }
1894 if (t->type == TOK_WHITESPACE) {
1895 t = t->next;
1896 continue;
1897 }
1898 if (tt->type == TOK_WHITESPACE) {
1899 tt = tt->next;
1900 continue;
1901 }
1902 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001903 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001904 break;
1905 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001906 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001907 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001908 size_t l1 = nasm_unquote(t->text, NULL);
1909 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001910
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001911 if (l1 != l2) {
1912 j = false;
1913 break;
1914 }
1915 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1916 j = false;
1917 break;
1918 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001919 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001920 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001921 break;
1922 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001923
H. Peter Anvine2c80182005-01-15 22:15:51 +00001924 t = t->next;
1925 tt = tt->next;
1926 }
1927 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001928 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001929 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001930
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001931 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001932 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001933 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001934 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001935
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001936 skip_white_(tline);
1937 tline = expand_id(tline);
1938 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001939 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001940 "`%s' expects a macro name", pp_directives[ct]);
1941 goto fail;
1942 }
1943 searching.name = nasm_strdup(tline->text);
1944 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001945 searching.plus = false;
1946 searching.nolist = false;
1947 searching.in_progress = 0;
1948 searching.max_depth = 0;
1949 searching.rep_nest = NULL;
1950 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001951 searching.nparam_max = INT_MAX;
1952 tline = expand_smacro(tline->next);
1953 skip_white_(tline);
1954 if (!tline) {
1955 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001956 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001957 "`%s' expects a parameter count or nothing",
1958 pp_directives[ct]);
1959 } else {
1960 searching.nparam_min = searching.nparam_max =
1961 readnum(tline->text, &j);
1962 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001963 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001964 "unable to parse parameter count `%s'",
1965 tline->text);
1966 }
1967 if (tline && tok_is_(tline->next, "-")) {
1968 tline = tline->next->next;
1969 if (tok_is_(tline, "*"))
1970 searching.nparam_max = INT_MAX;
1971 else if (!tok_type_(tline, TOK_NUMBER))
H. Peter Anvin130736c2016-02-17 20:27:41 -08001972 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001973 "`%s' expects a parameter count after `-'",
1974 pp_directives[ct]);
1975 else {
1976 searching.nparam_max = readnum(tline->text, &j);
1977 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001978 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001979 "unable to parse parameter count `%s'",
1980 tline->text);
1981 if (searching.nparam_min > searching.nparam_max)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001982 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001983 "minimum parameter count exceeds maximum");
1984 }
1985 }
1986 if (tline && tok_is_(tline->next, "+")) {
1987 tline = tline->next;
1988 searching.plus = true;
1989 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001990 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1991 while (mmac) {
1992 if (!strcmp(mmac->name, searching.name) &&
1993 (mmac->nparam_min <= searching.nparam_max
1994 || searching.plus)
1995 && (searching.nparam_min <= mmac->nparam_max
1996 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001997 found = true;
1998 break;
1999 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002000 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002001 }
2002 if (tline && tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002003 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002004 "trailing garbage after %%ifmacro ignored");
2005 nasm_free(searching.name);
2006 j = found;
2007 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002008 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002009
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002010 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002011 needtype = TOK_ID;
2012 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002013 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002014 needtype = TOK_NUMBER;
2015 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002016 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002017 needtype = TOK_STRING;
2018 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002019
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002020iftype:
2021 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002022
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002023 while (tok_type_(t, TOK_WHITESPACE) ||
2024 (needtype == TOK_NUMBER &&
2025 tok_type_(t, TOK_OTHER) &&
2026 (t->text[0] == '-' || t->text[0] == '+') &&
2027 !t->text[1]))
2028 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002029
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002030 j = tok_type_(t, needtype);
2031 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002032
2033 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002034 t = tline = expand_smacro(tline);
2035 while (tok_type_(t, TOK_WHITESPACE))
2036 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002037
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002038 j = false;
2039 if (t) {
2040 t = t->next; /* Skip the actual token */
2041 while (tok_type_(t, TOK_WHITESPACE))
2042 t = t->next;
2043 j = !t; /* Should be nothing left */
2044 }
2045 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002046
H. Peter Anvin134b9462008-02-16 17:01:40 -08002047 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002048 t = tline = expand_smacro(tline);
2049 while (tok_type_(t, TOK_WHITESPACE))
2050 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002051
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002052 j = !t; /* Should be empty */
2053 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002054
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002055 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002056 t = tline = expand_smacro(tline);
2057 tptr = &t;
2058 tokval.t_type = TOKEN_INVALID;
2059 evalresult = evaluate(ppscan, tptr, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -08002060 NULL, pass | CRITICAL, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002061 if (!evalresult)
2062 return -1;
2063 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002064 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002065 "trailing garbage after expression ignored");
2066 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002067 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002068 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002069 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002070 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002071 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002072 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002073
H. Peter Anvine2c80182005-01-15 22:15:51 +00002074 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002075 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002076 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002077 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002078 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002079 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002080
2081 free_tlist(origline);
2082 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002083
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002084fail:
2085 free_tlist(origline);
2086 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002087}
2088
2089/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002090 * Common code for defining an smacro
2091 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002092static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002093 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002094{
2095 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002096 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002097
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002098 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002099 if (!smac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002100 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002101 "single-line macro `%s' defined both with and"
2102 " without parameters", mname);
2103 /*
2104 * Some instances of the old code considered this a failure,
2105 * some others didn't. What is the right thing to do here?
2106 */
2107 free_tlist(expansion);
2108 return false; /* Failure */
2109 } else {
2110 /*
2111 * We're redefining, so we have to take over an
2112 * existing SMacro structure. This means freeing
2113 * what was already in it.
2114 */
2115 nasm_free(smac->name);
2116 free_tlist(smac->expansion);
2117 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002118 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002119 smtbl = ctx ? &ctx->localmac : &smacros;
2120 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002121 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002122 smac->next = *smhead;
2123 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002124 }
2125 smac->name = nasm_strdup(mname);
2126 smac->casesense = casesense;
2127 smac->nparam = nparam;
2128 smac->expansion = expansion;
2129 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002130 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002131}
2132
2133/*
2134 * Undefine an smacro
2135 */
2136static void undef_smacro(Context *ctx, const char *mname)
2137{
2138 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002139 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002140
H. Peter Anvin166c2472008-05-28 12:28:58 -07002141 smtbl = ctx ? &ctx->localmac : &smacros;
2142 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002143
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002144 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002145 /*
2146 * We now have a macro name... go hunt for it.
2147 */
2148 sp = smhead;
2149 while ((s = *sp) != NULL) {
2150 if (!mstrcmp(s->name, mname, s->casesense)) {
2151 *sp = s->next;
2152 nasm_free(s->name);
2153 free_tlist(s->expansion);
2154 nasm_free(s);
2155 } else {
2156 sp = &s->next;
2157 }
2158 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002159 }
2160}
2161
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002162/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002163 * Parse a mmacro specification.
2164 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002165static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002166{
2167 bool err;
2168
2169 tline = tline->next;
2170 skip_white_(tline);
2171 tline = expand_id(tline);
2172 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002173 nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002174 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002175 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002176
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002177 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002178 def->name = nasm_strdup(tline->text);
2179 def->plus = false;
2180 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002181 def->in_progress = 0;
2182 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002183 def->nparam_min = 0;
2184 def->nparam_max = 0;
2185
H. Peter Anvina26433d2008-07-16 14:40:01 -07002186 tline = expand_smacro(tline->next);
2187 skip_white_(tline);
2188 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002189 nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002190 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002191 def->nparam_min = def->nparam_max =
2192 readnum(tline->text, &err);
2193 if (err)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002194 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002195 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002196 }
2197 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002198 tline = tline->next->next;
2199 if (tok_is_(tline, "*")) {
2200 def->nparam_max = INT_MAX;
2201 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002202 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002203 "`%s' expects a parameter count after `-'", directive);
2204 } else {
2205 def->nparam_max = readnum(tline->text, &err);
2206 if (err) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002207 nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002208 tline->text);
2209 }
2210 if (def->nparam_min > def->nparam_max) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002211 nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002212 }
2213 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002214 }
2215 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002216 tline = tline->next;
2217 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002218 }
2219 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002220 !nasm_stricmp(tline->next->text, ".nolist")) {
2221 tline = tline->next;
2222 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002223 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002224
H. Peter Anvina26433d2008-07-16 14:40:01 -07002225 /*
2226 * Handle default parameters.
2227 */
2228 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002229 def->dlist = tline->next;
2230 tline->next = NULL;
2231 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002232 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002233 def->dlist = NULL;
2234 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002235 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002236 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002237
H. Peter Anvin89cee572009-07-15 09:16:54 -04002238 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002239 !def->plus)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002240 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002241 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002242
H. Peter Anvina26433d2008-07-16 14:40:01 -07002243 return true;
2244}
2245
2246
2247/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002248 * Decode a size directive
2249 */
2250static int parse_size(const char *str) {
2251 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002252 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002253 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002254 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002255
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002256 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002257}
2258
Ed Beroset3ab3f412002-06-11 03:31:49 +00002259/**
2260 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002261 * Find out if a line contains a preprocessor directive, and deal
2262 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002263 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002264 * If a directive _is_ found, it is the responsibility of this routine
2265 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002266 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002267 * @param tline a pointer to the current tokeninzed line linked list
2268 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002269 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002270 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002271static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002272{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002273 enum preproc_token i;
2274 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002275 bool err;
2276 int nparam;
2277 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002278 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002279 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002280 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002281 char *p, *pp;
2282 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002283 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002284 Include *inc;
2285 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002286 Cond *cond;
2287 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002288 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002289 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002290 struct tokenval tokval;
2291 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002292 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002293 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002294 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002295 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002296
2297 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002298
H. Peter Anvineba20a72002-04-30 20:53:55 +00002299 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002300 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002301 (tline->text[1] == '%' || tline->text[1] == '$'
2302 || tline->text[1] == '!'))
2303 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002304
H. Peter Anvin4169a472007-09-12 01:29:43 +00002305 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002306
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002307 /*
2308 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2309 * since they are known to be buggy at moment, we need to fix them
2310 * in future release (2.09-2.10)
2311 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002312 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002313 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002314 tline->text);
2315 return NO_DIRECTIVE_FOUND;
2316 }
2317
2318 /*
2319 * If we're in a non-emitting branch of a condition construct,
2320 * or walking to the end of an already terminated %rep block,
2321 * we should ignore all directives except for condition
2322 * directives.
2323 */
2324 if (((istk->conds && !emitting(istk->conds->state)) ||
2325 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2326 return NO_DIRECTIVE_FOUND;
2327 }
2328
2329 /*
2330 * If we're defining a macro or reading a %rep block, we should
2331 * ignore all directives except for %macro/%imacro (which nest),
2332 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2333 * If we're in a %rep block, another %rep nests, so should be let through.
2334 */
2335 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2336 i != PP_RMACRO && i != PP_IRMACRO &&
2337 i != PP_ENDMACRO && i != PP_ENDM &&
2338 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2339 return NO_DIRECTIVE_FOUND;
2340 }
2341
2342 if (defining) {
2343 if (i == PP_MACRO || i == PP_IMACRO ||
2344 i == PP_RMACRO || i == PP_IRMACRO) {
2345 nested_mac_count++;
2346 return NO_DIRECTIVE_FOUND;
2347 } else if (nested_mac_count > 0) {
2348 if (i == PP_ENDMACRO) {
2349 nested_mac_count--;
2350 return NO_DIRECTIVE_FOUND;
2351 }
2352 }
2353 if (!defining->name) {
2354 if (i == PP_REP) {
2355 nested_rep_count++;
2356 return NO_DIRECTIVE_FOUND;
2357 } else if (nested_rep_count > 0) {
2358 if (i == PP_ENDREP) {
2359 nested_rep_count--;
2360 return NO_DIRECTIVE_FOUND;
2361 }
2362 }
2363 }
2364 }
2365
H. Peter Anvin4169a472007-09-12 01:29:43 +00002366 switch (i) {
2367 case PP_INVALID:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002368 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002369 tline->text);
2370 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002371
H. Peter Anvine2c80182005-01-15 22:15:51 +00002372 case PP_STACKSIZE:
2373 /* Directive to tell NASM what the default stack size is. The
2374 * default is for a 16-bit stack, and this can be overriden with
2375 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002376 */
2377 tline = tline->next;
2378 if (tline && tline->type == TOK_WHITESPACE)
2379 tline = tline->next;
2380 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002381 nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 free_tlist(origline);
2383 return DIRECTIVE_FOUND;
2384 }
2385 if (nasm_stricmp(tline->text, "flat") == 0) {
2386 /* All subsequent ARG directives are for a 32-bit stack */
2387 StackSize = 4;
2388 StackPointer = "ebp";
2389 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002390 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002391 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2392 /* All subsequent ARG directives are for a 64-bit stack */
2393 StackSize = 8;
2394 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002395 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002396 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002397 } else if (nasm_stricmp(tline->text, "large") == 0) {
2398 /* All subsequent ARG directives are for a 16-bit stack,
2399 * far function call.
2400 */
2401 StackSize = 2;
2402 StackPointer = "bp";
2403 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002404 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002405 } else if (nasm_stricmp(tline->text, "small") == 0) {
2406 /* All subsequent ARG directives are for a 16-bit stack,
2407 * far function call. We don't support near functions.
2408 */
2409 StackSize = 2;
2410 StackPointer = "bp";
2411 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002412 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002413 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002414 nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002415 free_tlist(origline);
2416 return DIRECTIVE_FOUND;
2417 }
2418 free_tlist(origline);
2419 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002420
H. Peter Anvine2c80182005-01-15 22:15:51 +00002421 case PP_ARG:
2422 /* TASM like ARG directive to define arguments to functions, in
2423 * the following form:
2424 *
2425 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2426 */
2427 offset = ArgOffset;
2428 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002429 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002430 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002431
H. Peter Anvine2c80182005-01-15 22:15:51 +00002432 /* Find the argument name */
2433 tline = tline->next;
2434 if (tline && tline->type == TOK_WHITESPACE)
2435 tline = tline->next;
2436 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002437 nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002438 free_tlist(origline);
2439 return DIRECTIVE_FOUND;
2440 }
2441 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002442
H. Peter Anvine2c80182005-01-15 22:15:51 +00002443 /* Find the argument size type */
2444 tline = tline->next;
2445 if (!tline || tline->type != TOK_OTHER
2446 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002447 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002448 "Syntax error processing `%%arg' directive");
2449 free_tlist(origline);
2450 return DIRECTIVE_FOUND;
2451 }
2452 tline = tline->next;
2453 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002454 nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002455 free_tlist(origline);
2456 return DIRECTIVE_FOUND;
2457 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002458
H. Peter Anvine2c80182005-01-15 22:15:51 +00002459 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002460 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002461 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002462 size = parse_size(tt->text);
2463 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002464 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002465 "Invalid size type for `%%arg' missing directive");
2466 free_tlist(tt);
2467 free_tlist(origline);
2468 return DIRECTIVE_FOUND;
2469 }
2470 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002471
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002472 /* Round up to even stack slots */
2473 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002474
H. Peter Anvine2c80182005-01-15 22:15:51 +00002475 /* Now define the macro for the argument */
2476 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2477 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002478 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002479 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002480
H. Peter Anvine2c80182005-01-15 22:15:51 +00002481 /* Move to the next argument in the list */
2482 tline = tline->next;
2483 if (tline && tline->type == TOK_WHITESPACE)
2484 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002485 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002486 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002487 free_tlist(origline);
2488 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002489
H. Peter Anvine2c80182005-01-15 22:15:51 +00002490 case PP_LOCAL:
2491 /* TASM like LOCAL directive to define local variables for a
2492 * function, in the following form:
2493 *
2494 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2495 *
2496 * The '= LocalSize' at the end is ignored by NASM, but is
2497 * required by TASM to define the local parameter size (and used
2498 * by the TASM macro package).
2499 */
2500 offset = LocalOffset;
2501 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002502 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002503 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002504
H. Peter Anvine2c80182005-01-15 22:15:51 +00002505 /* Find the argument name */
2506 tline = tline->next;
2507 if (tline && tline->type == TOK_WHITESPACE)
2508 tline = tline->next;
2509 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002510 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002511 "`%%local' missing argument parameter");
2512 free_tlist(origline);
2513 return DIRECTIVE_FOUND;
2514 }
2515 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002516
H. Peter Anvine2c80182005-01-15 22:15:51 +00002517 /* Find the argument size type */
2518 tline = tline->next;
2519 if (!tline || tline->type != TOK_OTHER
2520 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002521 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002522 "Syntax error processing `%%local' directive");
2523 free_tlist(origline);
2524 return DIRECTIVE_FOUND;
2525 }
2526 tline = tline->next;
2527 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002528 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002529 "`%%local' missing size type parameter");
2530 free_tlist(origline);
2531 return DIRECTIVE_FOUND;
2532 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002533
H. Peter Anvine2c80182005-01-15 22:15:51 +00002534 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002535 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002536 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002537 size = parse_size(tt->text);
2538 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002539 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002540 "Invalid size type for `%%local' missing directive");
2541 free_tlist(tt);
2542 free_tlist(origline);
2543 return DIRECTIVE_FOUND;
2544 }
2545 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002546
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002547 /* Round up to even stack slots */
2548 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002549
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002550 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002551
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002552 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002553 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2554 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002555 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002556
H. Peter Anvine2c80182005-01-15 22:15:51 +00002557 /* Now define the assign to setup the enter_c macro correctly */
2558 snprintf(directive, sizeof(directive),
2559 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002560 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002561
H. Peter Anvine2c80182005-01-15 22:15:51 +00002562 /* Move to the next argument in the list */
2563 tline = tline->next;
2564 if (tline && tline->type == TOK_WHITESPACE)
2565 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002566 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002567 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002568 free_tlist(origline);
2569 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002570
H. Peter Anvine2c80182005-01-15 22:15:51 +00002571 case PP_CLEAR:
2572 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002573 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002574 "trailing garbage after `%%clear' ignored");
2575 free_macros();
2576 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002577 free_tlist(origline);
2578 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002579
H. Peter Anvin418ca702008-05-30 10:42:30 -07002580 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002581 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002582 skip_white_(t);
2583 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002584 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002585 nasm_error(ERR_NONFATAL, "`%%depend' expects a file name");
H. Peter Anvin418ca702008-05-30 10:42:30 -07002586 free_tlist(origline);
2587 return DIRECTIVE_FOUND; /* but we did _something_ */
2588 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002589 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002590 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002591 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002592 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002593 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002594 nasm_unquote_cstr(p, i);
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002595 add_string_to_list(dephead, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002596 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002597 return DIRECTIVE_FOUND;
2598
2599 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002600 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002601 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002602
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002603 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002604 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002605 nasm_error(ERR_NONFATAL, "`%%include' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002606 free_tlist(origline);
2607 return DIRECTIVE_FOUND; /* but we did _something_ */
2608 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002609 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002610 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002611 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002612 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002613 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002614 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002615 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002616 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002617 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002618 found_path = NULL;
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002619 inc->fp = inc_fopen(p, dephead, &found_path,
H. Peter Anvind81a2352016-09-21 14:03:18 -07002620 pass == 0 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002621 if (!inc->fp) {
2622 /* -MG given but file not found */
2623 nasm_free(inc);
2624 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002625 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002626 inc->lineno = src_set_linnum(0);
2627 inc->lineinc = 1;
2628 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002629 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002630 istk = inc;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002631 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002632 }
2633 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002634 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002635
H. Peter Anvind2456592008-06-19 15:04:18 -07002636 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002637 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002638 static macros_t *use_pkg;
2639 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002640
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002641 tline = tline->next;
2642 skip_white_(tline);
2643 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002644
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002645 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002646 tline->type != TOK_INTERNAL_STRING &&
2647 tline->type != TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002648 nasm_error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002649 free_tlist(origline);
2650 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002651 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002652 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002653 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002654 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002655 if (tline->type == TOK_STRING)
2656 nasm_unquote_cstr(tline->text, i);
2657 use_pkg = nasm_stdmac_find_package(tline->text);
2658 if (!use_pkg)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002659 nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002660 else
2661 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002662 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002663 /* Not already included, go ahead and include it */
2664 stdmacpos = use_pkg;
2665 }
2666 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002667 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002668 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002669 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002670 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002671 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002672 tline = tline->next;
2673 skip_white_(tline);
2674 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002675 if (tline) {
2676 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002677 nasm_error(ERR_NONFATAL, "`%s' expects a context identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002678 pp_directives[i]);
2679 free_tlist(origline);
2680 return DIRECTIVE_FOUND; /* but we did _something_ */
2681 }
2682 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002683 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002684 "trailing garbage after `%s' ignored",
2685 pp_directives[i]);
2686 p = nasm_strdup(tline->text);
2687 } else {
2688 p = NULL; /* Anonymous */
2689 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002690
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002691 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002692 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002693 ctx->next = cstk;
2694 hash_init(&ctx->localmac, HASH_SMALL);
2695 ctx->name = p;
2696 ctx->number = unique++;
2697 cstk = ctx;
2698 } else {
2699 /* %pop or %repl */
2700 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002701 nasm_error(ERR_NONFATAL, "`%s': context stack is empty",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002702 pp_directives[i]);
2703 } else if (i == PP_POP) {
2704 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08002705 nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002706 "expected %s",
2707 cstk->name ? cstk->name : "anonymous", p);
2708 else
2709 ctx_pop();
2710 } else {
2711 /* i == PP_REPL */
2712 nasm_free(cstk->name);
2713 cstk->name = p;
2714 p = NULL;
2715 }
2716 nasm_free(p);
2717 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002718 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002719 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002720 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002721 severity = ERR_FATAL;
2722 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002723 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002724 severity = ERR_NONFATAL;
2725 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002726 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002727 severity = ERR_WARNING|ERR_WARN_USER;
2728 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002729
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002730issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002731 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002732 /* Only error out if this is the final pass */
2733 if (pass != 2 && i != PP_FATAL)
2734 return DIRECTIVE_FOUND;
2735
2736 tline->next = expand_smacro(tline->next);
2737 tline = tline->next;
2738 skip_white_(tline);
2739 t = tline ? tline->next : NULL;
2740 skip_white_(t);
2741 if (tok_type_(tline, TOK_STRING) && !t) {
2742 /* The line contains only a quoted string */
2743 p = tline->text;
2744 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002745 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002746 } else {
2747 /* Not a quoted string, or more than a quoted string */
2748 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002749 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002750 nasm_free(p);
2751 }
2752 free_tlist(origline);
2753 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002754 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002755
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002756 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002757 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002758 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002759 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002760 j = if_condition(tline->next, i);
2761 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002762 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002763 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002764 cond = nasm_malloc(sizeof(Cond));
2765 cond->next = istk->conds;
2766 cond->state = j;
2767 istk->conds = cond;
2768 if(istk->mstk)
2769 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002770 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002771 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002772
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002773 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002774 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002775 nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002776 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002777 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002778 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002779 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002780
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002781 case COND_DONE:
2782 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002783 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002784
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002785 case COND_ELSE_TRUE:
2786 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002787 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2788 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002789 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002790 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002791
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002792 case COND_IF_FALSE:
2793 /*
2794 * IMPORTANT: In the case of %if, we will already have
2795 * called expand_mmac_params(); however, if we're
2796 * processing an %elif we must have been in a
2797 * non-emitting mode, which would have inhibited
2798 * the normal invocation of expand_mmac_params().
2799 * Therefore, we have to do it explicitly here.
2800 */
2801 j = if_condition(expand_mmac_params(tline->next), i);
2802 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002803 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002804 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2805 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002806 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002807 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002808 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002809
H. Peter Anvine2c80182005-01-15 22:15:51 +00002810 case PP_ELSE:
2811 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002812 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2813 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002814 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002815 nasm_fatal(0, "`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002816 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002817 case COND_IF_TRUE:
2818 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002819 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002820 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002821
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002822 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002823 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002824
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002825 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002826 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002827 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002828
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002829 case COND_ELSE_TRUE:
2830 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002831 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002832 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002833 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002834 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002835 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002836 free_tlist(origline);
2837 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002838
H. Peter Anvine2c80182005-01-15 22:15:51 +00002839 case PP_ENDIF:
2840 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002841 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2842 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002843 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002844 nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002845 cond = istk->conds;
2846 istk->conds = cond->next;
2847 nasm_free(cond);
2848 if(istk->mstk)
2849 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002850 free_tlist(origline);
2851 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002852
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002853 case PP_RMACRO:
2854 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002855 case PP_MACRO:
2856 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002857 if (defining) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002858 nasm_error(ERR_FATAL, "`%s': already defining a macro",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002859 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002860 return DIRECTIVE_FOUND;
2861 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002862 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002863 defining->max_depth =
2864 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2865 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2866 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2867 nasm_free(defining);
2868 defining = NULL;
2869 return DIRECTIVE_FOUND;
2870 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002871
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002872 src_get(&defining->xline, &defining->fname);
2873
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002874 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2875 while (mmac) {
2876 if (!strcmp(mmac->name, defining->name) &&
2877 (mmac->nparam_min <= defining->nparam_max
2878 || defining->plus)
2879 && (defining->nparam_min <= mmac->nparam_max
2880 || mmac->plus)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002881 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002882 "redefining multi-line macro `%s'", defining->name);
2883 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002884 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002885 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002886 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002887 free_tlist(origline);
2888 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002889
H. Peter Anvine2c80182005-01-15 22:15:51 +00002890 case PP_ENDM:
2891 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002892 if (! (defining && defining->name)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002893 nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002894 return DIRECTIVE_FOUND;
2895 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002896 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2897 defining->next = *mmhead;
2898 *mmhead = defining;
2899 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002900 free_tlist(origline);
2901 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002902
H. Peter Anvin89cee572009-07-15 09:16:54 -04002903 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002904 /*
2905 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002906 * macro-end marker for a macro with a name. Then we
2907 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002908 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002909 list_for_each(l, istk->expansion)
2910 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002911 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002912
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002913 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002914 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002915 * Remove all conditional entries relative to this
2916 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002917 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002918 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2919 cond = istk->conds;
2920 istk->conds = cond->next;
2921 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002922 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002923 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002924 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002925 nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002926 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002927 free_tlist(origline);
2928 return DIRECTIVE_FOUND;
2929
H. Peter Anvina26433d2008-07-16 14:40:01 -07002930 case PP_UNMACRO:
2931 case PP_UNIMACRO:
2932 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002933 MMacro **mmac_p;
2934 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002935
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002936 spec.casesense = (i == PP_UNMACRO);
2937 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2938 return DIRECTIVE_FOUND;
2939 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002940 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2941 while (mmac_p && *mmac_p) {
2942 mmac = *mmac_p;
2943 if (mmac->casesense == spec.casesense &&
2944 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2945 mmac->nparam_min == spec.nparam_min &&
2946 mmac->nparam_max == spec.nparam_max &&
2947 mmac->plus == spec.plus) {
2948 *mmac_p = mmac->next;
2949 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002950 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002951 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002952 }
2953 }
2954 free_tlist(origline);
2955 free_tlist(spec.dlist);
2956 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002957 }
2958
H. Peter Anvine2c80182005-01-15 22:15:51 +00002959 case PP_ROTATE:
2960 if (tline->next && tline->next->type == TOK_WHITESPACE)
2961 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002962 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002963 free_tlist(origline);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002964 nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002965 return DIRECTIVE_FOUND;
2966 }
2967 t = expand_smacro(tline->next);
2968 tline->next = NULL;
2969 free_tlist(origline);
2970 tline = t;
2971 tptr = &t;
2972 tokval.t_type = TOKEN_INVALID;
2973 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002974 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002975 free_tlist(tline);
2976 if (!evalresult)
2977 return DIRECTIVE_FOUND;
2978 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002979 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002980 "trailing garbage after expression ignored");
2981 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002982 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002983 return DIRECTIVE_FOUND;
2984 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002985 mmac = istk->mstk;
2986 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2987 mmac = mmac->next_active;
2988 if (!mmac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002989 nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002990 } else if (mmac->nparam == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002991 nasm_error(ERR_NONFATAL,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002992 "`%%rotate' invoked within macro without parameters");
2993 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002994 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002995
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002996 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002997 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002998 rotate += mmac->nparam;
2999
3000 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003001 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003002 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003003
H. Peter Anvine2c80182005-01-15 22:15:51 +00003004 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003005 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003006 do {
3007 tline = tline->next;
3008 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003009
H. Peter Anvine2c80182005-01-15 22:15:51 +00003010 if (tok_type_(tline, TOK_ID) &&
3011 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003012 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003013 do {
3014 tline = tline->next;
3015 } while (tok_type_(tline, TOK_WHITESPACE));
3016 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003017
H. Peter Anvine2c80182005-01-15 22:15:51 +00003018 if (tline) {
3019 t = expand_smacro(tline);
3020 tptr = &t;
3021 tokval.t_type = TOKEN_INVALID;
3022 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08003023 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003024 if (!evalresult) {
3025 free_tlist(origline);
3026 return DIRECTIVE_FOUND;
3027 }
3028 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003029 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003030 "trailing garbage after expression ignored");
3031 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003032 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003033 return DIRECTIVE_FOUND;
3034 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003035 count = reloc_value(evalresult);
3036 if (count >= REP_LIMIT) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003037 nasm_error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003038 count = 0;
3039 } else
3040 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003041 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003042 nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003043 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003044 }
3045 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003046
3047 tmp_defining = defining;
3048 defining = nasm_malloc(sizeof(MMacro));
3049 defining->prev = NULL;
3050 defining->name = NULL; /* flags this macro as a %rep block */
3051 defining->casesense = false;
3052 defining->plus = false;
3053 defining->nolist = nolist;
3054 defining->in_progress = count;
3055 defining->max_depth = 0;
3056 defining->nparam_min = defining->nparam_max = 0;
3057 defining->defaults = NULL;
3058 defining->dlist = NULL;
3059 defining->expansion = NULL;
3060 defining->next_active = istk->mstk;
3061 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003062 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003063
H. Peter Anvine2c80182005-01-15 22:15:51 +00003064 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003065 if (!defining || defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003066 nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003067 return DIRECTIVE_FOUND;
3068 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003069
H. Peter Anvine2c80182005-01-15 22:15:51 +00003070 /*
3071 * Now we have a "macro" defined - although it has no name
3072 * and we won't be entering it in the hash tables - we must
3073 * push a macro-end marker for it on to istk->expansion.
3074 * After that, it will take care of propagating itself (a
3075 * macro-end marker line for a macro which is really a %rep
3076 * block will cause the macro to be re-expanded, complete
3077 * with another macro-end marker to ensure the process
3078 * continues) until the whole expansion is forcibly removed
3079 * from istk->expansion by a %exitrep.
3080 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003081 l = nasm_malloc(sizeof(Line));
3082 l->next = istk->expansion;
3083 l->finishes = defining;
3084 l->first = NULL;
3085 istk->expansion = l;
3086
3087 istk->mstk = defining;
3088
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08003089 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003090 tmp_defining = defining;
3091 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003092 free_tlist(origline);
3093 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003094
H. Peter Anvine2c80182005-01-15 22:15:51 +00003095 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003096 /*
3097 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003098 * macro-end marker for a macro with no name. Then we set
3099 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003100 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003101 list_for_each(l, istk->expansion)
3102 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003103 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003104
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003105 if (l)
3106 l->finishes->in_progress = 1;
3107 else
H. Peter Anvin130736c2016-02-17 20:27:41 -08003108 nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003109 free_tlist(origline);
3110 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003111
H. Peter Anvine2c80182005-01-15 22:15:51 +00003112 case PP_XDEFINE:
3113 case PP_IXDEFINE:
3114 case PP_DEFINE:
3115 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003116 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003117
H. Peter Anvine2c80182005-01-15 22:15:51 +00003118 tline = tline->next;
3119 skip_white_(tline);
3120 tline = expand_id(tline);
3121 if (!tline || (tline->type != TOK_ID &&
3122 (tline->type != TOK_PREPROC_ID ||
3123 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003124 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003125 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003126 free_tlist(origline);
3127 return DIRECTIVE_FOUND;
3128 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003129
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003130 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003131 last = tline;
3132 param_start = tline = tline->next;
3133 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003134
H. Peter Anvine2c80182005-01-15 22:15:51 +00003135 /* Expand the macro definition now for %xdefine and %ixdefine */
3136 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3137 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003138
H. Peter Anvine2c80182005-01-15 22:15:51 +00003139 if (tok_is_(tline, "(")) {
3140 /*
3141 * This macro has parameters.
3142 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003143
H. Peter Anvine2c80182005-01-15 22:15:51 +00003144 tline = tline->next;
3145 while (1) {
3146 skip_white_(tline);
3147 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003148 nasm_error(ERR_NONFATAL, "parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003149 free_tlist(origline);
3150 return DIRECTIVE_FOUND;
3151 }
3152 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003153 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003154 "`%s': parameter identifier expected",
3155 tline->text);
3156 free_tlist(origline);
3157 return DIRECTIVE_FOUND;
3158 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003159 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003160 tline = tline->next;
3161 skip_white_(tline);
3162 if (tok_is_(tline, ",")) {
3163 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003164 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003165 if (!tok_is_(tline, ")")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003166 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003167 "`)' expected to terminate macro template");
3168 free_tlist(origline);
3169 return DIRECTIVE_FOUND;
3170 }
3171 break;
3172 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003173 }
3174 last = tline;
3175 tline = tline->next;
3176 }
3177 if (tok_type_(tline, TOK_WHITESPACE))
3178 last = tline, tline = tline->next;
3179 macro_start = NULL;
3180 last->next = NULL;
3181 t = tline;
3182 while (t) {
3183 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003184 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003185 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003186 !strcmp(tt->text, t->text))
3187 t->type = tt->type;
3188 }
3189 tt = t->next;
3190 t->next = macro_start;
3191 macro_start = t;
3192 t = tt;
3193 }
3194 /*
3195 * Good. We now have a macro name, a parameter count, and a
3196 * token list (in reverse order) for an expansion. We ought
3197 * to be OK just to create an SMacro, store it, and let
3198 * free_tlist have the rest of the line (which we have
3199 * carefully re-terminated after chopping off the expansion
3200 * from the end).
3201 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003202 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003203 free_tlist(origline);
3204 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003205
H. Peter Anvine2c80182005-01-15 22:15:51 +00003206 case PP_UNDEF:
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, "`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003214 free_tlist(origline);
3215 return DIRECTIVE_FOUND;
3216 }
3217 if (tline->next) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003218 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003219 "trailing garbage after macro name ignored");
3220 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003221
H. Peter Anvine2c80182005-01-15 22:15:51 +00003222 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003223 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003224 undef_smacro(ctx, mname);
3225 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003226 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003227
H. Peter Anvin9e200162008-06-04 17:23:14 -07003228 case PP_DEFSTR:
3229 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003230 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003231
3232 tline = tline->next;
3233 skip_white_(tline);
3234 tline = expand_id(tline);
3235 if (!tline || (tline->type != TOK_ID &&
3236 (tline->type != TOK_PREPROC_ID ||
3237 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003238 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003239 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003240 free_tlist(origline);
3241 return DIRECTIVE_FOUND;
3242 }
3243
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003244 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003245 last = tline;
3246 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003247 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003248
3249 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003250 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003251
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003252 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003253 macro_start = nasm_malloc(sizeof(*macro_start));
3254 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003255 macro_start->text = nasm_quote(p, strlen(p));
3256 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003257 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003258 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003259
3260 /*
3261 * We now have a macro name, an implicit parameter count of
3262 * zero, and a string token to use as an expansion. Create
3263 * and store an SMacro.
3264 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003265 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003266 free_tlist(origline);
3267 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003268
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003269 case PP_DEFTOK:
3270 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003271 casesense = (i == PP_DEFTOK);
3272
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003273 tline = tline->next;
3274 skip_white_(tline);
3275 tline = expand_id(tline);
3276 if (!tline || (tline->type != TOK_ID &&
3277 (tline->type != TOK_PREPROC_ID ||
3278 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003279 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003280 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003281 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003282 free_tlist(origline);
3283 return DIRECTIVE_FOUND;
3284 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003285 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003286 last = tline;
3287 tline = expand_smacro(tline->next);
3288 last->next = NULL;
3289
3290 t = tline;
3291 while (tok_type_(t, TOK_WHITESPACE))
3292 t = t->next;
3293 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003294 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003295 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003296 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003297 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003298 free_tlist(tline);
3299 free_tlist(origline);
3300 return DIRECTIVE_FOUND;
3301 }
3302
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003303 /*
3304 * Convert the string to a token stream. Note that smacros
3305 * are stored with the token stream reversed, so we have to
3306 * reverse the output of tokenize().
3307 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003308 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003309 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003310
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003311 /*
3312 * We now have a macro name, an implicit parameter count of
3313 * zero, and a numeric token to use as an expansion. Create
3314 * and store an SMacro.
3315 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003316 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003317 free_tlist(tline);
3318 free_tlist(origline);
3319 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003320
H. Peter Anvin418ca702008-05-30 10:42:30 -07003321 case PP_PATHSEARCH:
3322 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003323 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003324
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003325 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003326
3327 tline = tline->next;
3328 skip_white_(tline);
3329 tline = expand_id(tline);
3330 if (!tline || (tline->type != TOK_ID &&
3331 (tline->type != TOK_PREPROC_ID ||
3332 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003333 nasm_error(ERR_NONFATAL,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003334 "`%%pathsearch' expects a macro identifier as first parameter");
3335 free_tlist(origline);
3336 return DIRECTIVE_FOUND;
3337 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003338 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003339 last = tline;
3340 tline = expand_smacro(tline->next);
3341 last->next = NULL;
3342
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003343 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003344 while (tok_type_(t, TOK_WHITESPACE))
3345 t = t->next;
3346
3347 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003348 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003349 nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003350 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003351 free_tlist(origline);
3352 return DIRECTIVE_FOUND; /* but we did _something_ */
3353 }
3354 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003355 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003356 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003357 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003358 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003359 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003360
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003361 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003362 if (!found_path)
3363 found_path = p;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003364 macro_start = nasm_malloc(sizeof(*macro_start));
3365 macro_start->next = NULL;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003366 macro_start->text = nasm_quote(found_path, strlen(found_path));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003367 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003368 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003369
3370 /*
3371 * We now have a macro name, an implicit parameter count of
3372 * zero, and a string token to use as an expansion. Create
3373 * and store an SMacro.
3374 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003375 define_smacro(ctx, mname, casesense, 0, macro_start);
3376 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003377 free_tlist(origline);
3378 return DIRECTIVE_FOUND;
3379 }
3380
H. Peter Anvine2c80182005-01-15 22:15:51 +00003381 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003382 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003383
H. Peter Anvine2c80182005-01-15 22:15:51 +00003384 tline = tline->next;
3385 skip_white_(tline);
3386 tline = expand_id(tline);
3387 if (!tline || (tline->type != TOK_ID &&
3388 (tline->type != TOK_PREPROC_ID ||
3389 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003390 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003391 "`%%strlen' expects a macro identifier as first parameter");
3392 free_tlist(origline);
3393 return DIRECTIVE_FOUND;
3394 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003395 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003396 last = tline;
3397 tline = expand_smacro(tline->next);
3398 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003399
H. Peter Anvine2c80182005-01-15 22:15:51 +00003400 t = tline;
3401 while (tok_type_(t, TOK_WHITESPACE))
3402 t = t->next;
3403 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003404 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003405 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003406 "`%%strlen` requires string as second parameter");
3407 free_tlist(tline);
3408 free_tlist(origline);
3409 return DIRECTIVE_FOUND;
3410 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003411
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003412 macro_start = nasm_malloc(sizeof(*macro_start));
3413 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003414 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003415 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003416
H. Peter Anvine2c80182005-01-15 22:15:51 +00003417 /*
3418 * We now have a macro name, an implicit parameter count of
3419 * zero, and a numeric token to use as an expansion. Create
3420 * and store an SMacro.
3421 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003422 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003423 free_tlist(tline);
3424 free_tlist(origline);
3425 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003426
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003427 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003428 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003429
3430 tline = tline->next;
3431 skip_white_(tline);
3432 tline = expand_id(tline);
3433 if (!tline || (tline->type != TOK_ID &&
3434 (tline->type != TOK_PREPROC_ID ||
3435 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003436 nasm_error(ERR_NONFATAL,
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003437 "`%%strcat' expects a macro identifier as first parameter");
3438 free_tlist(origline);
3439 return DIRECTIVE_FOUND;
3440 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003441 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003442 last = tline;
3443 tline = expand_smacro(tline->next);
3444 last->next = NULL;
3445
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003446 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003447 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003448 switch (t->type) {
3449 case TOK_WHITESPACE:
3450 break;
3451 case TOK_STRING:
3452 len += t->a.len = nasm_unquote(t->text, NULL);
3453 break;
3454 case TOK_OTHER:
3455 if (!strcmp(t->text, ",")) /* permit comma separators */
3456 break;
3457 /* else fall through */
3458 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003459 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003460 "non-string passed to `%%strcat' (%d)", t->type);
3461 free_tlist(tline);
3462 free_tlist(origline);
3463 return DIRECTIVE_FOUND;
3464 }
3465 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003466
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003467 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003468 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003469 if (t->type == TOK_STRING) {
3470 memcpy(p, t->text, t->a.len);
3471 p += t->a.len;
3472 }
3473 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003474
3475 /*
3476 * We now have a macro name, an implicit parameter count of
3477 * zero, and a numeric token to use as an expansion. Create
3478 * and store an SMacro.
3479 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003480 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3481 macro_start->text = nasm_quote(pp, len);
3482 nasm_free(pp);
3483 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003484 free_tlist(tline);
3485 free_tlist(origline);
3486 return DIRECTIVE_FOUND;
3487
H. Peter Anvine2c80182005-01-15 22:15:51 +00003488 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003489 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003490 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003491 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003492
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003493 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003494
H. Peter Anvine2c80182005-01-15 22:15:51 +00003495 tline = tline->next;
3496 skip_white_(tline);
3497 tline = expand_id(tline);
3498 if (!tline || (tline->type != TOK_ID &&
3499 (tline->type != TOK_PREPROC_ID ||
3500 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003501 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003502 "`%%substr' expects a macro identifier as first parameter");
3503 free_tlist(origline);
3504 return DIRECTIVE_FOUND;
3505 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003506 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003507 last = tline;
3508 tline = expand_smacro(tline->next);
3509 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003510
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003511 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003512 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003513 while (tok_type_(t, TOK_WHITESPACE))
3514 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003515
H. Peter Anvine2c80182005-01-15 22:15:51 +00003516 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003517 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003518 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003519 "`%%substr` requires string as second parameter");
3520 free_tlist(tline);
3521 free_tlist(origline);
3522 return DIRECTIVE_FOUND;
3523 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003524
H. Peter Anvine2c80182005-01-15 22:15:51 +00003525 tt = t->next;
3526 tptr = &tt;
3527 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003528 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003529 if (!evalresult) {
3530 free_tlist(tline);
3531 free_tlist(origline);
3532 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003533 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003534 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003535 free_tlist(tline);
3536 free_tlist(origline);
3537 return DIRECTIVE_FOUND;
3538 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003539 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003540
3541 while (tok_type_(tt, TOK_WHITESPACE))
3542 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003543 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003544 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003545 } else {
3546 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003547 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003548 if (!evalresult) {
3549 free_tlist(tline);
3550 free_tlist(origline);
3551 return DIRECTIVE_FOUND;
3552 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003553 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003554 free_tlist(tline);
3555 free_tlist(origline);
3556 return DIRECTIVE_FOUND;
3557 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003558 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003559 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003560
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003561 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003562
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003563 /* make start and count being in range */
3564 if (start < 0)
3565 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003566 if (count < 0)
3567 count = len + count + 1 - start;
3568 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003569 count = len - start;
3570 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003571 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003572
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003573 macro_start = nasm_malloc(sizeof(*macro_start));
3574 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003575 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003576 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003577 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003578
H. Peter Anvine2c80182005-01-15 22:15:51 +00003579 /*
3580 * We now have a macro name, an implicit parameter count of
3581 * zero, and a numeric token to use as an expansion. Create
3582 * and store an SMacro.
3583 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003584 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003585 free_tlist(tline);
3586 free_tlist(origline);
3587 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003588 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003589
H. Peter Anvine2c80182005-01-15 22:15:51 +00003590 case PP_ASSIGN:
3591 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003592 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003593
H. Peter Anvine2c80182005-01-15 22:15:51 +00003594 tline = tline->next;
3595 skip_white_(tline);
3596 tline = expand_id(tline);
3597 if (!tline || (tline->type != TOK_ID &&
3598 (tline->type != TOK_PREPROC_ID ||
3599 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003600 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003601 "`%%%sassign' expects a macro identifier",
3602 (i == PP_IASSIGN ? "i" : ""));
3603 free_tlist(origline);
3604 return DIRECTIVE_FOUND;
3605 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003606 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003607 last = tline;
3608 tline = expand_smacro(tline->next);
3609 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003610
H. Peter Anvine2c80182005-01-15 22:15:51 +00003611 t = tline;
3612 tptr = &t;
3613 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003614 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003615 free_tlist(tline);
3616 if (!evalresult) {
3617 free_tlist(origline);
3618 return DIRECTIVE_FOUND;
3619 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003620
H. Peter Anvine2c80182005-01-15 22:15:51 +00003621 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003622 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003623 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003624
H. Peter Anvine2c80182005-01-15 22:15:51 +00003625 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003626 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003627 "non-constant value given to `%%%sassign'",
3628 (i == PP_IASSIGN ? "i" : ""));
3629 free_tlist(origline);
3630 return DIRECTIVE_FOUND;
3631 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003632
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003633 macro_start = nasm_malloc(sizeof(*macro_start));
3634 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003635 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003636 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003637
H. Peter Anvine2c80182005-01-15 22:15:51 +00003638 /*
3639 * We now have a macro name, an implicit parameter count of
3640 * zero, and a numeric token to use as an expansion. Create
3641 * and store an SMacro.
3642 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003643 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003644 free_tlist(origline);
3645 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003646
H. Peter Anvine2c80182005-01-15 22:15:51 +00003647 case PP_LINE:
3648 /*
3649 * Syntax is `%line nnn[+mmm] [filename]'
3650 */
3651 tline = tline->next;
3652 skip_white_(tline);
3653 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003654 nasm_error(ERR_NONFATAL, "`%%line' expects line number");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003655 free_tlist(origline);
3656 return DIRECTIVE_FOUND;
3657 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003658 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003659 m = 1;
3660 tline = tline->next;
3661 if (tok_is_(tline, "+")) {
3662 tline = tline->next;
3663 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003664 nasm_error(ERR_NONFATAL, "`%%line' expects line increment");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003665 free_tlist(origline);
3666 return DIRECTIVE_FOUND;
3667 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003668 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003669 tline = tline->next;
3670 }
3671 skip_white_(tline);
3672 src_set_linnum(k);
3673 istk->lineinc = m;
3674 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003675 char *fname = detoken(tline, false);
3676 src_set_fname(fname);
3677 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003678 }
3679 free_tlist(origline);
3680 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003681
H. Peter Anvine2c80182005-01-15 22:15:51 +00003682 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003683 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003684 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003685 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003686 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003687 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003688}
3689
3690/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003691 * Ensure that a macro parameter contains a condition code and
3692 * nothing else. Return the condition code index if so, or -1
3693 * otherwise.
3694 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003695static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003696{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003697 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003698
H. Peter Anvin25a99342007-09-22 17:45:45 -07003699 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003700 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003701
H. Peter Anvineba20a72002-04-30 20:53:55 +00003702 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003703 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003704 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003705 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003706 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003707 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003708 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003709
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003710 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003711}
3712
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003713/*
3714 * This routines walks over tokens strem and hadnles tokens
3715 * pasting, if @handle_explicit passed then explicit pasting
3716 * term is handled, otherwise -- implicit pastings only.
3717 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003718static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003719 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003720{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003721 Token *tok, *next, **prev_next, **prev_nonspace;
3722 bool pasted = false;
3723 char *buf, *p;
3724 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003725
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003726 /*
3727 * The last token before pasting. We need it
3728 * to be able to connect new handled tokens.
3729 * In other words if there were a tokens stream
3730 *
3731 * A -> B -> C -> D
3732 *
3733 * and we've joined tokens B and C, the resulting
3734 * stream should be
3735 *
3736 * A -> BC -> D
3737 */
3738 tok = *head;
3739 prev_next = NULL;
3740
3741 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3742 prev_nonspace = head;
3743 else
3744 prev_nonspace = NULL;
3745
3746 while (tok && (next = tok->next)) {
3747
3748 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003749 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003750 /* Zap redundant whitespaces */
3751 while (tok_type_(next, TOK_WHITESPACE))
3752 next = delete_Token(next);
3753 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003754 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003755
3756 case TOK_PASTE:
3757 /* Explicit pasting */
3758 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003759 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003760 next = delete_Token(tok);
3761
3762 while (tok_type_(next, TOK_WHITESPACE))
3763 next = delete_Token(next);
3764
3765 if (!pasted)
3766 pasted = true;
3767
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003768 /* Left pasting token is start of line */
3769 if (!prev_nonspace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003770 nasm_error(ERR_FATAL, "No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003771
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003772 /*
3773 * No ending token, this might happen in two
3774 * cases
3775 *
3776 * 1) There indeed no right token at all
3777 * 2) There is a bare "%define ID" statement,
3778 * and @ID does expand to whitespace.
3779 *
3780 * So technically we need to do a grammar analysis
3781 * in another stage of parsing, but for now lets don't
3782 * change the behaviour people used to. Simply allow
3783 * whitespace after paste token.
3784 */
3785 if (!next) {
3786 /*
3787 * Zap ending space tokens and that's all.
3788 */
3789 tok = (*prev_nonspace)->next;
3790 while (tok_type_(tok, TOK_WHITESPACE))
3791 tok = delete_Token(tok);
3792 tok = *prev_nonspace;
3793 tok->next = NULL;
3794 break;
3795 }
3796
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003797 tok = *prev_nonspace;
3798 while (tok_type_(tok, TOK_WHITESPACE))
3799 tok = delete_Token(tok);
3800 len = strlen(tok->text);
3801 len += strlen(next->text);
3802
3803 p = buf = nasm_malloc(len + 1);
3804 strcpy(p, tok->text);
3805 p = strchr(p, '\0');
3806 strcpy(p, next->text);
3807
3808 delete_Token(tok);
3809
3810 tok = tokenize(buf);
3811 nasm_free(buf);
3812
3813 *prev_nonspace = tok;
3814 while (tok && tok->next)
3815 tok = tok->next;
3816
3817 tok->next = delete_Token(next);
3818
3819 /* Restart from pasted tokens head */
3820 tok = *prev_nonspace;
3821 break;
3822
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003823 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003824 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003825 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003826 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3827 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003828
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003829 len = 0;
3830 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3831 len += strlen(next->text);
3832 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003833 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003834
3835 /* No match */
3836 if (tok == next)
3837 break;
3838
3839 len += strlen(tok->text);
3840 p = buf = nasm_malloc(len + 1);
3841
3842 while (tok != next) {
3843 strcpy(p, tok->text);
3844 p = strchr(p, '\0');
3845 tok = delete_Token(tok);
3846 }
3847
3848 tok = tokenize(buf);
3849 nasm_free(buf);
3850
3851 if (prev_next)
3852 *prev_next = tok;
3853 else
3854 *head = tok;
3855
3856 /*
3857 * Connect pasted into original stream,
3858 * ie A -> new-tokens -> B
3859 */
3860 while (tok && tok->next)
3861 tok = tok->next;
3862 tok->next = next;
3863
3864 if (!pasted)
3865 pasted = true;
3866
3867 /* Restart from pasted tokens head */
3868 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003869 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003870
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003871 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003872 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003873
3874 prev_next = &tok->next;
3875
3876 if (tok->next &&
3877 !tok_type_(tok->next, TOK_WHITESPACE) &&
3878 !tok_type_(tok->next, TOK_PASTE))
3879 prev_nonspace = prev_next;
3880
3881 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003882 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003883
3884 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003885}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003886
3887/*
3888 * expands to a list of tokens from %{x:y}
3889 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003890static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003891{
3892 Token *t = tline, **tt, *tm, *head;
3893 char *pos;
3894 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003895
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003896 pos = strchr(tline->text, ':');
3897 nasm_assert(pos);
3898
3899 lst = atoi(pos + 1);
3900 fst = atoi(tline->text + 1);
3901
3902 /*
3903 * only macros params are accounted so
3904 * if someone passes %0 -- we reject such
3905 * value(s)
3906 */
3907 if (lst == 0 || fst == 0)
3908 goto err;
3909
3910 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003911 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3912 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003913 goto err;
3914
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003915 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3916 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003917
3918 /* counted from zero */
3919 fst--, lst--;
3920
3921 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003922 * It will be at least one token. Note we
3923 * need to scan params until separator, otherwise
3924 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003925 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003926 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003927 head = new_Token(NULL, tm->type, tm->text, 0);
3928 tt = &head->next, tm = tm->next;
3929 while (tok_isnt_(tm, ",")) {
3930 t = new_Token(NULL, tm->type, tm->text, 0);
3931 *tt = t, tt = &t->next, tm = tm->next;
3932 }
3933
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003934 if (fst < lst) {
3935 for (i = fst + 1; i <= lst; i++) {
3936 t = new_Token(NULL, TOK_OTHER, ",", 0);
3937 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003938 j = (i + mac->rotate) % mac->nparam;
3939 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003940 while (tok_isnt_(tm, ",")) {
3941 t = new_Token(NULL, tm->type, tm->text, 0);
3942 *tt = t, tt = &t->next, tm = tm->next;
3943 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003944 }
3945 } else {
3946 for (i = fst - 1; i >= lst; i--) {
3947 t = new_Token(NULL, TOK_OTHER, ",", 0);
3948 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003949 j = (i + mac->rotate) % mac->nparam;
3950 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003951 while (tok_isnt_(tm, ",")) {
3952 t = new_Token(NULL, tm->type, tm->text, 0);
3953 *tt = t, tt = &t->next, tm = tm->next;
3954 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003955 }
3956 }
3957
3958 *last = tt;
3959 return head;
3960
3961err:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003962 nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003963 &tline->text[1]);
3964 return tline;
3965}
3966
H. Peter Anvin76690a12002-04-30 20:52:49 +00003967/*
3968 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003969 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003970 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003971 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003972static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003973{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003974 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003975 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003976 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003977
3978 tail = &thead;
3979 thead = NULL;
3980
H. Peter Anvine2c80182005-01-15 22:15:51 +00003981 while (tline) {
3982 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003983 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3984 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3985 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003986 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003987 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003988 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003989 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003990 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003991 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003992
H. Peter Anvine2c80182005-01-15 22:15:51 +00003993 t = tline;
3994 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003995
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003996 mac = istk->mstk;
3997 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3998 mac = mac->next_active;
3999 if (!mac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004000 nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004001 } else {
4002 pos = strchr(t->text, ':');
4003 if (!pos) {
4004 switch (t->text[1]) {
4005 /*
4006 * We have to make a substitution of one of the
4007 * forms %1, %-1, %+1, %%foo, %0.
4008 */
4009 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004010 type = TOK_NUMBER;
4011 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
4012 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004013 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004014 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004015 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004016 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004017 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004018 text = nasm_strcat(tmpbuf, t->text + 2);
4019 break;
4020 case '-':
4021 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004022 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004023 tt = NULL;
4024 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004025 if (mac->nparam > 1)
4026 n = (n + mac->rotate) % mac->nparam;
4027 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004028 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004029 cc = find_cc(tt);
4030 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004031 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004032 "macro parameter %d is not a condition code",
4033 n + 1);
4034 text = NULL;
4035 } else {
4036 type = TOK_ID;
4037 if (inverse_ccs[cc] == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004038 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004039 "condition code `%s' is not invertible",
4040 conditions[cc]);
4041 text = NULL;
4042 } else
4043 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4044 }
4045 break;
4046 case '+':
4047 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004048 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004049 tt = NULL;
4050 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004051 if (mac->nparam > 1)
4052 n = (n + mac->rotate) % mac->nparam;
4053 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004054 }
4055 cc = find_cc(tt);
4056 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004057 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004058 "macro parameter %d is not a condition code",
4059 n + 1);
4060 text = NULL;
4061 } else {
4062 type = TOK_ID;
4063 text = nasm_strdup(conditions[cc]);
4064 }
4065 break;
4066 default:
4067 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004068 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004069 tt = NULL;
4070 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004071 if (mac->nparam > 1)
4072 n = (n + mac->rotate) % mac->nparam;
4073 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004074 }
4075 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004076 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004077 *tail = new_Token(NULL, tt->type, tt->text, 0);
4078 tail = &(*tail)->next;
4079 tt = tt->next;
4080 }
4081 }
4082 text = NULL; /* we've done it here */
4083 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004084 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004085 } else {
4086 /*
4087 * seems we have a parameters range here
4088 */
4089 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004090 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004091 if (head != t) {
4092 *tail = head;
4093 *last = tline;
4094 tline = head;
4095 text = NULL;
4096 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004097 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004098 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004099 if (!text) {
4100 delete_Token(t);
4101 } else {
4102 *tail = t;
4103 tail = &t->next;
4104 t->type = type;
4105 nasm_free(t->text);
4106 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004107 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004108 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004109 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004110 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004111 } else if (tline->type == TOK_INDIRECT) {
4112 t = tline;
4113 tline = tline->next;
4114 tt = tokenize(t->text);
4115 tt = expand_mmac_params(tt);
4116 tt = expand_smacro(tt);
4117 *tail = tt;
4118 while (tt) {
4119 tt->a.mac = NULL; /* Necessary? */
4120 tail = &tt->next;
4121 tt = tt->next;
4122 }
4123 delete_Token(t);
4124 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004125 } else {
4126 t = *tail = tline;
4127 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004128 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004129 tail = &t->next;
4130 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004131 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004132 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004133
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004134 if (changed) {
4135 const struct tokseq_match t[] = {
4136 {
4137 PP_CONCAT_MASK(TOK_ID) |
4138 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4139 PP_CONCAT_MASK(TOK_ID) |
4140 PP_CONCAT_MASK(TOK_NUMBER) |
4141 PP_CONCAT_MASK(TOK_FLOAT) |
4142 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4143 },
4144 {
4145 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4146 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4147 }
4148 };
4149 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4150 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004151
H. Peter Anvin76690a12002-04-30 20:52:49 +00004152 return thead;
4153}
4154
4155/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004156 * Expand all single-line macro calls made in the given line.
4157 * Return the expanded version of the line. The original is deemed
4158 * to be destroyed in the process. (In reality we'll just move
4159 * Tokens from input to output a lot of the time, rather than
4160 * actually bothering to destroy and replicate.)
4161 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004162
H. Peter Anvine2c80182005-01-15 22:15:51 +00004163static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004164{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004165 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004166 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004167 Token **params;
4168 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004169 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004170 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004171 Token *org_tline = tline;
4172 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004173 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004174 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004175 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004176
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004177 /*
4178 * Trick: we should avoid changing the start token pointer since it can
4179 * be contained in "next" field of other token. Because of this
4180 * we allocate a copy of first token and work with it; at the end of
4181 * routine we copy it back
4182 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004183 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004184 tline = new_Token(org_tline->next, org_tline->type,
4185 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004186 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004187 nasm_free(org_tline->text);
4188 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004189 }
4190
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004191 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004192
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004193again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004194 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004195 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004196
H. Peter Anvine2c80182005-01-15 22:15:51 +00004197 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004198 if (!--deadman) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004199 nasm_error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004200 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004201 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004202
H. Peter Anvine2c80182005-01-15 22:15:51 +00004203 if ((mname = tline->text)) {
4204 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004205 if (tline->type == TOK_ID) {
4206 head = (SMacro *)hash_findix(&smacros, mname);
4207 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004208 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004209 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4210 } else
4211 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004212
H. Peter Anvine2c80182005-01-15 22:15:51 +00004213 /*
4214 * We've hit an identifier. As in is_mmacro below, we first
4215 * check whether the identifier is a single-line macro at
4216 * all, then think about checking for parameters if
4217 * necessary.
4218 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004219 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004220 if (!mstrcmp(m->name, mname, m->casesense))
4221 break;
4222 if (m) {
4223 mstart = tline;
4224 params = NULL;
4225 paramsize = NULL;
4226 if (m->nparam == 0) {
4227 /*
4228 * Simple case: the macro is parameterless. Discard the
4229 * one token that the macro call took, and push the
4230 * expansion back on the to-do stack.
4231 */
4232 if (!m->expansion) {
4233 if (!strcmp("__FILE__", m->name)) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004234 const char *file = src_get_fname();
4235 /* nasm_free(tline->text); here? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004236 tline->text = nasm_quote(file, strlen(file));
4237 tline->type = TOK_STRING;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004238 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004239 }
4240 if (!strcmp("__LINE__", m->name)) {
4241 nasm_free(tline->text);
4242 make_tok_num(tline, src_get_linnum());
4243 continue;
4244 }
4245 if (!strcmp("__BITS__", m->name)) {
4246 nasm_free(tline->text);
4247 make_tok_num(tline, globalbits);
4248 continue;
4249 }
4250 tline = delete_Token(tline);
4251 continue;
4252 }
4253 } else {
4254 /*
4255 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004256 * exists and takes parameters. We must find the
4257 * parameters in the call, count them, find the SMacro
4258 * that corresponds to that form of the macro call, and
4259 * substitute for the parameters when we expand. What a
4260 * pain.
4261 */
4262 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004263 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004264 do {
4265 t = tline->next;
4266 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004267 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004268 t->text = NULL;
4269 t = tline->next = delete_Token(t);
4270 }
4271 tline = t;
4272 } while (tok_type_(tline, TOK_WHITESPACE));
4273 if (!tok_is_(tline, "(")) {
4274 /*
4275 * This macro wasn't called with parameters: ignore
4276 * the call. (Behaviour borrowed from gnu cpp.)
4277 */
4278 tline = mstart;
4279 m = NULL;
4280 } else {
4281 int paren = 0;
4282 int white = 0;
4283 brackets = 0;
4284 nparam = 0;
4285 sparam = PARAM_DELTA;
4286 params = nasm_malloc(sparam * sizeof(Token *));
4287 params[0] = tline->next;
4288 paramsize = nasm_malloc(sparam * sizeof(int));
4289 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004290 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004291 /*
4292 * For some unusual expansions
4293 * which concatenates function call
4294 */
4295 t = tline->next;
4296 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004297 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004298 t->text = NULL;
4299 t = tline->next = delete_Token(t);
4300 }
4301 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004302
H. Peter Anvine2c80182005-01-15 22:15:51 +00004303 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004304 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004305 "macro call expects terminating `)'");
4306 break;
4307 }
4308 if (tline->type == TOK_WHITESPACE
4309 && brackets <= 0) {
4310 if (paramsize[nparam])
4311 white++;
4312 else
4313 params[nparam] = tline->next;
4314 continue; /* parameter loop */
4315 }
4316 if (tline->type == TOK_OTHER
4317 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004318 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004319 if (ch == ',' && !paren && brackets <= 0) {
4320 if (++nparam >= sparam) {
4321 sparam += PARAM_DELTA;
4322 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004323 sparam * sizeof(Token *));
4324 paramsize = nasm_realloc(paramsize,
4325 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004326 }
4327 params[nparam] = tline->next;
4328 paramsize[nparam] = 0;
4329 white = 0;
4330 continue; /* parameter loop */
4331 }
4332 if (ch == '{' &&
4333 (brackets > 0 || (brackets == 0 &&
4334 !paramsize[nparam])))
4335 {
4336 if (!(brackets++)) {
4337 params[nparam] = tline->next;
4338 continue; /* parameter loop */
4339 }
4340 }
4341 if (ch == '}' && brackets > 0)
4342 if (--brackets == 0) {
4343 brackets = -1;
4344 continue; /* parameter loop */
4345 }
4346 if (ch == '(' && !brackets)
4347 paren++;
4348 if (ch == ')' && brackets <= 0)
4349 if (--paren < 0)
4350 break;
4351 }
4352 if (brackets < 0) {
4353 brackets = 0;
H. Peter Anvin130736c2016-02-17 20:27:41 -08004354 nasm_error(ERR_NONFATAL, "braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004355 "enclose all of macro parameter");
4356 }
4357 paramsize[nparam] += white + 1;
4358 white = 0;
4359 } /* parameter loop */
4360 nparam++;
4361 while (m && (m->nparam != nparam ||
4362 mstrcmp(m->name, mname,
4363 m->casesense)))
4364 m = m->next;
4365 if (!m)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004366 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004367 "macro `%s' exists, "
4368 "but not taking %d parameters",
4369 mstart->text, nparam);
4370 }
4371 }
4372 if (m && m->in_progress)
4373 m = NULL;
4374 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004375 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004376 * Design question: should we handle !tline, which
4377 * indicates missing ')' here, or expand those
4378 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004379 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004380 */
4381 nasm_free(params);
4382 nasm_free(paramsize);
4383 tline = mstart;
4384 } else {
4385 /*
4386 * Expand the macro: we are placed on the last token of the
4387 * call, so that we can easily split the call from the
4388 * following tokens. We also start by pushing an SMAC_END
4389 * token for the cycle removal.
4390 */
4391 t = tline;
4392 if (t) {
4393 tline = t->next;
4394 t->next = NULL;
4395 }
4396 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004397 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004398 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004399 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004400 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004401 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004402 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004403 Token *ttt, *pt;
4404 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004405
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004406 ttt = params[t->type - TOK_SMAC_PARAM];
4407 i = paramsize[t->type - TOK_SMAC_PARAM];
4408 while (--i >= 0) {
4409 pt = *ptail = new_Token(tline, ttt->type,
4410 ttt->text, 0);
4411 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004412 ttt = ttt->next;
4413 }
4414 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004415 } else if (t->type == TOK_PREPROC_Q) {
4416 tt = new_Token(tline, TOK_ID, mname, 0);
4417 tline = tt;
4418 } else if (t->type == TOK_PREPROC_QQ) {
4419 tt = new_Token(tline, TOK_ID, m->name, 0);
4420 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004421 } else {
4422 tt = new_Token(tline, t->type, t->text, 0);
4423 tline = tt;
4424 }
4425 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004426
H. Peter Anvine2c80182005-01-15 22:15:51 +00004427 /*
4428 * Having done that, get rid of the macro call, and clean
4429 * up the parameters.
4430 */
4431 nasm_free(params);
4432 nasm_free(paramsize);
4433 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004434 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004435 continue; /* main token loop */
4436 }
4437 }
4438 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004439
H. Peter Anvine2c80182005-01-15 22:15:51 +00004440 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004441 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004442 tline = delete_Token(tline);
4443 } else {
4444 t = *tail = tline;
4445 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004446 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004447 t->next = NULL;
4448 tail = &t->next;
4449 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004450 }
4451
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004452 /*
4453 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004454 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004455 * TOK_IDs should be concatenated.
4456 * Also we look for %+ tokens and concatenate the tokens before and after
4457 * them (without white spaces in between).
4458 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004459 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004460 const struct tokseq_match t[] = {
4461 {
4462 PP_CONCAT_MASK(TOK_ID) |
4463 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4464 PP_CONCAT_MASK(TOK_ID) |
4465 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4466 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4467 }
4468 };
4469 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004470 /*
4471 * If we concatenated something, *and* we had previously expanded
4472 * an actual macro, scan the lines again for macros...
4473 */
4474 tline = thead;
4475 expanded = false;
4476 goto again;
4477 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004478 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004479
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004480err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004481 if (org_tline) {
4482 if (thead) {
4483 *org_tline = *thead;
4484 /* since we just gave text to org_line, don't free it */
4485 thead->text = NULL;
4486 delete_Token(thead);
4487 } else {
4488 /* the expression expanded to empty line;
4489 we can't return NULL for some reasons
4490 we just set the line to a single WHITESPACE token. */
4491 memset(org_tline, 0, sizeof(*org_tline));
4492 org_tline->text = NULL;
4493 org_tline->type = TOK_WHITESPACE;
4494 }
4495 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004496 }
4497
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004498 return thead;
4499}
4500
4501/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004502 * Similar to expand_smacro but used exclusively with macro identifiers
4503 * right before they are fetched in. The reason is that there can be
4504 * identifiers consisting of several subparts. We consider that if there
4505 * are more than one element forming the name, user wants a expansion,
4506 * otherwise it will be left as-is. Example:
4507 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004508 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004509 *
4510 * the identifier %$abc will be left as-is so that the handler for %define
4511 * will suck it and define the corresponding value. Other case:
4512 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004513 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004514 *
4515 * In this case user wants name to be expanded *before* %define starts
4516 * working, so we'll expand %$abc into something (if it has a value;
4517 * otherwise it will be left as-is) then concatenate all successive
4518 * PP_IDs into one.
4519 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004520static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004521{
4522 Token *cur, *oldnext = NULL;
4523
H. Peter Anvin734b1882002-04-30 21:01:08 +00004524 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004525 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004526
4527 cur = tline;
4528 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004529 (cur->next->type == TOK_ID ||
4530 cur->next->type == TOK_PREPROC_ID
4531 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004532 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004533
4534 /* If identifier consists of just one token, don't expand */
4535 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004536 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004537
H. Peter Anvine2c80182005-01-15 22:15:51 +00004538 if (cur) {
4539 oldnext = cur->next; /* Detach the tail past identifier */
4540 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004541 }
4542
H. Peter Anvin734b1882002-04-30 21:01:08 +00004543 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004544
H. Peter Anvine2c80182005-01-15 22:15:51 +00004545 if (cur) {
4546 /* expand_smacro possibly changhed tline; re-scan for EOL */
4547 cur = tline;
4548 while (cur && cur->next)
4549 cur = cur->next;
4550 if (cur)
4551 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004552 }
4553
4554 return tline;
4555}
4556
4557/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004558 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004559 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004560 * to check for an initial label - that's taken care of in
4561 * expand_mmacro - but must check numbers of parameters. Guaranteed
4562 * to be called with tline->type == TOK_ID, so the putative macro
4563 * name is easy to find.
4564 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004565static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004566{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004567 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004568 Token **params;
4569 int nparam;
4570
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004571 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004572
4573 /*
4574 * Efficiency: first we see if any macro exists with the given
4575 * name. If not, we can return NULL immediately. _Then_ we
4576 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004577 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004578 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004579 list_for_each(m, head)
4580 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004581 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004582 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004583 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004584
4585 /*
4586 * OK, we have a potential macro. Count and demarcate the
4587 * parameters.
4588 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004589 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004590
4591 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004592 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004593 * structure that handles this number.
4594 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004595 while (m) {
4596 if (m->nparam_min <= nparam
4597 && (m->plus || nparam <= m->nparam_max)) {
4598 /*
4599 * This one is right. Just check if cycle removal
4600 * prohibits us using it before we actually celebrate...
4601 */
4602 if (m->in_progress > m->max_depth) {
4603 if (m->max_depth > 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004604 nasm_error(ERR_WARNING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004605 "reached maximum recursion depth of %i",
4606 m->max_depth);
4607 }
4608 nasm_free(params);
4609 return NULL;
4610 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004611 /*
4612 * It's right, and we can use it. Add its default
4613 * parameters to the end of our list if necessary.
4614 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004615 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004616 params =
4617 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004618 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004619 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004620 while (nparam < m->nparam_min + m->ndefs) {
4621 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004622 nparam++;
4623 }
4624 }
4625 /*
4626 * If we've gone over the maximum parameter count (and
4627 * we're in Plus mode), ignore parameters beyond
4628 * nparam_max.
4629 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004630 if (m->plus && nparam > m->nparam_max)
4631 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004632 /*
4633 * Then terminate the parameter list, and leave.
4634 */
4635 if (!params) { /* need this special case */
4636 params = nasm_malloc(sizeof(*params));
4637 nparam = 0;
4638 }
4639 params[nparam] = NULL;
4640 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004641 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004642 }
4643 /*
4644 * This one wasn't right: look for the next one with the
4645 * same name.
4646 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004647 list_for_each(m, m->next)
4648 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004649 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004650 }
4651
4652 /*
4653 * After all that, we didn't find one with the right number of
4654 * parameters. Issue a warning, and fail to expand the macro.
4655 */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004656 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004657 "macro `%s' exists, but not taking %d parameters",
4658 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004659 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004660 return NULL;
4661}
4662
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004663
4664/*
4665 * Save MMacro invocation specific fields in
4666 * preparation for a recursive macro expansion
4667 */
4668static void push_mmacro(MMacro *m)
4669{
4670 MMacroInvocation *i;
4671
4672 i = nasm_malloc(sizeof(MMacroInvocation));
4673 i->prev = m->prev;
4674 i->params = m->params;
4675 i->iline = m->iline;
4676 i->nparam = m->nparam;
4677 i->rotate = m->rotate;
4678 i->paramlen = m->paramlen;
4679 i->unique = m->unique;
4680 i->condcnt = m->condcnt;
4681 m->prev = i;
4682}
4683
4684
4685/*
4686 * Restore MMacro invocation specific fields that were
4687 * saved during a previous recursive macro expansion
4688 */
4689static void pop_mmacro(MMacro *m)
4690{
4691 MMacroInvocation *i;
4692
4693 if (m->prev) {
4694 i = m->prev;
4695 m->prev = i->prev;
4696 m->params = i->params;
4697 m->iline = i->iline;
4698 m->nparam = i->nparam;
4699 m->rotate = i->rotate;
4700 m->paramlen = i->paramlen;
4701 m->unique = i->unique;
4702 m->condcnt = i->condcnt;
4703 nasm_free(i);
4704 }
4705}
4706
4707
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004708/*
4709 * Expand the multi-line macro call made by the given line, if
4710 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004711 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004712 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004713static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004714{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004715 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004716 Token *label = NULL;
4717 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004718 Token **params, *t, *tt;
4719 MMacro *m;
4720 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004721 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004722 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004723
4724 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004725 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004726 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004727 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004728 return 0;
4729 m = is_mmacro(t, &params);
4730 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004731 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004732 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004733 Token *last;
4734 /*
4735 * We have an id which isn't a macro call. We'll assume
4736 * it might be a label; we'll also check to see if a
4737 * colon follows it. Then, if there's another id after
4738 * that lot, we'll check it again for macro-hood.
4739 */
4740 label = last = t;
4741 t = t->next;
4742 if (tok_type_(t, TOK_WHITESPACE))
4743 last = t, t = t->next;
4744 if (tok_is_(t, ":")) {
4745 dont_prepend = 1;
4746 last = t, t = t->next;
4747 if (tok_type_(t, TOK_WHITESPACE))
4748 last = t, t = t->next;
4749 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004750 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4751 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004752 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004753 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004754 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004755 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004756
4757 /*
4758 * Fix up the parameters: this involves stripping leading and
4759 * trailing whitespace, then stripping braces if they are
4760 * present.
4761 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004762 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004763 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004764
H. Peter Anvine2c80182005-01-15 22:15:51 +00004765 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004766 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004767 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004768
H. Peter Anvine2c80182005-01-15 22:15:51 +00004769 t = params[i];
4770 skip_white_(t);
4771 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004772 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004773 params[i] = t;
4774 paramlen[i] = 0;
4775 while (t) {
4776 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4777 break; /* ... because we have hit a comma */
4778 if (comma && t->type == TOK_WHITESPACE
4779 && tok_is_(t->next, ","))
4780 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004781 if (brace && t->type == TOK_OTHER) {
4782 if (t->text[0] == '{')
4783 brace++; /* ... or a nested opening brace */
4784 else if (t->text[0] == '}')
4785 if (!--brace)
4786 break; /* ... or a brace */
4787 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004788 t = t->next;
4789 paramlen[i]++;
4790 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004791 if (brace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004792 nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004793 }
4794
4795 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004796 * OK, we have a MMacro structure together with a set of
4797 * parameters. We must now go through the expansion and push
4798 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004799 * parameter tokens and macro-local tokens doesn't get done
4800 * until the single-line macro substitution process; this is
4801 * because delaying them allows us to change the semantics
4802 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004803 *
4804 * First, push an end marker on to istk->expansion, mark this
4805 * macro as in progress, and set up its invocation-specific
4806 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004807 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004808 ll = nasm_malloc(sizeof(Line));
4809 ll->next = istk->expansion;
4810 ll->finishes = m;
4811 ll->first = NULL;
4812 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004813
4814 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004815 * Save the previous MMacro expansion in the case of
4816 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004817 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004818 if (m->max_depth && m->in_progress)
4819 push_mmacro(m);
4820
4821 m->in_progress ++;
4822 m->params = params;
4823 m->iline = tline;
4824 m->nparam = nparam;
4825 m->rotate = 0;
4826 m->paramlen = paramlen;
4827 m->unique = unique++;
4828 m->lineno = 0;
4829 m->condcnt = 0;
4830
4831 m->next_active = istk->mstk;
4832 istk->mstk = m;
4833
4834 list_for_each(l, m->expansion) {
4835 Token **tail;
4836
4837 ll = nasm_malloc(sizeof(Line));
4838 ll->finishes = NULL;
4839 ll->next = istk->expansion;
4840 istk->expansion = ll;
4841 tail = &ll->first;
4842
4843 list_for_each(t, l->first) {
4844 Token *x = t;
4845 switch (t->type) {
4846 case TOK_PREPROC_Q:
4847 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004848 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004849 case TOK_PREPROC_QQ:
4850 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4851 break;
4852 case TOK_PREPROC_ID:
4853 if (t->text[1] == '0' && t->text[2] == '0') {
4854 dont_prepend = -1;
4855 x = label;
4856 if (!x)
4857 continue;
4858 }
4859 /* fall through */
4860 default:
4861 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4862 break;
4863 }
4864 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004865 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004866 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004867 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004868
4869 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004870 * If we had a label, push it on as the first line of
4871 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004872 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004873 if (label) {
4874 if (dont_prepend < 0)
4875 free_tlist(startline);
4876 else {
4877 ll = nasm_malloc(sizeof(Line));
4878 ll->finishes = NULL;
4879 ll->next = istk->expansion;
4880 istk->expansion = ll;
4881 ll->first = startline;
4882 if (!dont_prepend) {
4883 while (label->next)
4884 label = label->next;
4885 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004886 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004887 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004888 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004889
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004890 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004891
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004892 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004893}
4894
H. Peter Anvin130736c2016-02-17 20:27:41 -08004895/*
4896 * This function adds macro names to error messages, and suppresses
4897 * them if necessary.
4898 */
4899static void pp_verror(int severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004900{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004901 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004902 MMacro *mmac = NULL;
4903 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004904
H. Peter Anvin130736c2016-02-17 20:27:41 -08004905 /*
4906 * If we're in a dead branch of IF or something like it, ignore the error.
4907 * However, because %else etc are evaluated in the state context
4908 * of the previous branch, errors might get lost:
4909 * %if 0 ... %else trailing garbage ... %endif
4910 * So %else etc should set the ERR_PP_PRECOND flag.
4911 */
4912 if ((severity & ERR_MASK) < ERR_FATAL &&
4913 istk && istk->conds &&
4914 ((severity & ERR_PP_PRECOND) ?
4915 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004916 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004917 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004918
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004919 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004920 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004921 mmac = istk->mstk;
4922 /* but %rep blocks should be skipped */
4923 while (mmac && !mmac->name)
4924 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004925 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004926
H. Peter Anvin130736c2016-02-17 20:27:41 -08004927 if (mmac) {
4928 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004929
H. Peter Anvin130736c2016-02-17 20:27:41 -08004930 nasm_set_verror(real_verror);
4931 nasm_error(severity, "(%s:%d) %s",
4932 mmac->name, mmac->lineno - delta, buff);
4933 nasm_set_verror(pp_verror);
4934 } else {
4935 real_verror(severity, fmt, arg);
4936 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004937}
4938
H. Peter Anvin734b1882002-04-30 21:01:08 +00004939static void
H. Peter Anvin130736c2016-02-17 20:27:41 -08004940pp_reset(char *file, int apass, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004941{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004942 Token *t;
4943
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004944 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004945 istk = nasm_malloc(sizeof(Include));
4946 istk->next = NULL;
4947 istk->conds = NULL;
4948 istk->expansion = NULL;
4949 istk->mstk = NULL;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07004950 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004951 istk->fname = NULL;
H. Peter Anvin274cda82016-05-10 02:56:29 -07004952 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004953 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004954 if (!istk->fp)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004955 nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004956 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004957 nested_mac_count = 0;
4958 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004959 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004960 unique = 0;
H. Peter Anvinf7606612016-07-13 14:23:48 -07004961
4962 if (tasm_compatible_mode)
4963 pp_add_stdmac(nasm_stdmac_tasm);
4964
4965 pp_add_stdmac(nasm_stdmac_nasm);
4966 pp_add_stdmac(nasm_stdmac_version);
4967
4968 stdmacpos = stdmacros[0];
4969 stdmacnext = &stdmacros[1];
4970
H. Peter Anvind2456592008-06-19 15:04:18 -07004971 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004972
4973 /*
4974 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4975 * The caller, however, will also pass in 3 for preprocess-only so
4976 * we can set __PASS__ accordingly.
4977 */
4978 pass = apass > 2 ? 2 : apass;
4979
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004980 dephead = deplist;
4981 add_string_to_list(dephead, file);
4982
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004983 /*
4984 * Define the __PASS__ macro. This is defined here unlike
4985 * all the other builtins, because it is special -- it varies between
4986 * passes.
4987 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004988 t = nasm_malloc(sizeof(*t));
4989 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004990 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004991 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004992 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004993}
4994
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07004995static void pp_init(void)
4996{
4997 hash_init(&FileHash, HASH_MEDIUM);
4998}
4999
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005000static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005001{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005002 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005003 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005004
H. Peter Anvin130736c2016-02-17 20:27:41 -08005005 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005006
H. Peter Anvine2c80182005-01-15 22:15:51 +00005007 while (1) {
5008 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005009 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005010 * buffer or from the input file.
5011 */
5012 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005013 while (istk->expansion && istk->expansion->finishes) {
5014 Line *l = istk->expansion;
5015 if (!l->finishes->name && l->finishes->in_progress > 1) {
5016 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005017
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005018 /*
5019 * This is a macro-end marker for a macro with no
5020 * name, which means it's not really a macro at all
5021 * but a %rep block, and the `in_progress' field is
5022 * more than 1, meaning that we still need to
5023 * repeat. (1 means the natural last repetition; 0
5024 * means termination by %exitrep.) We have
5025 * therefore expanded up to the %endrep, and must
5026 * push the whole block on to the expansion buffer
5027 * again. We don't bother to remove the macro-end
5028 * marker: we'd only have to generate another one
5029 * if we did.
5030 */
5031 l->finishes->in_progress--;
5032 list_for_each(l, l->finishes->expansion) {
5033 Token *t, *tt, **tail;
5034
5035 ll = nasm_malloc(sizeof(Line));
5036 ll->next = istk->expansion;
5037 ll->finishes = NULL;
5038 ll->first = NULL;
5039 tail = &ll->first;
5040
5041 list_for_each(t, l->first) {
5042 if (t->text || t->type == TOK_WHITESPACE) {
5043 tt = *tail = new_Token(NULL, t->type, t->text, 0);
5044 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005045 }
5046 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005047
5048 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005049 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005050 } else {
5051 /*
5052 * Check whether a `%rep' was started and not ended
5053 * within this macro expansion. This can happen and
5054 * should be detected. It's a fatal error because
5055 * I'm too confused to work out how to recover
5056 * sensibly from it.
5057 */
5058 if (defining) {
5059 if (defining->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005060 nasm_panic(0, "defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005061 else if (istk->mstk->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005062 nasm_fatal(0, "`%%rep' without `%%endrep' within"
5063 " expansion of macro `%s'",
5064 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005065 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005066
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005067 /*
5068 * FIXME: investigate the relationship at this point between
5069 * istk->mstk and l->finishes
5070 */
5071 {
5072 MMacro *m = istk->mstk;
5073 istk->mstk = m->next_active;
5074 if (m->name) {
5075 /*
5076 * This was a real macro call, not a %rep, and
5077 * therefore the parameter information needs to
5078 * be freed.
5079 */
5080 if (m->prev) {
5081 pop_mmacro(m);
5082 l->finishes->in_progress --;
5083 } else {
5084 nasm_free(m->params);
5085 free_tlist(m->iline);
5086 nasm_free(m->paramlen);
5087 l->finishes->in_progress = 0;
5088 }
5089 } else
5090 free_mmacro(m);
5091 }
5092 istk->expansion = l->next;
5093 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005094 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005095 }
5096 }
5097 while (1) { /* until we get a line we can use */
5098
5099 if (istk->expansion) { /* from a macro expansion */
5100 char *p;
5101 Line *l = istk->expansion;
5102 if (istk->mstk)
5103 istk->mstk->lineno++;
5104 tline = l->first;
5105 istk->expansion = l->next;
5106 nasm_free(l);
5107 p = detoken(tline, false);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005108 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005109 nasm_free(p);
5110 break;
5111 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005112 line = read_line();
5113 if (line) { /* from the current input file */
5114 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005115 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005116 nasm_free(line);
5117 break;
5118 }
5119 /*
5120 * The current file has ended; work down the istk
5121 */
5122 {
5123 Include *i = istk;
5124 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005125 if (i->conds) {
5126 /* nasm_error can't be conditionally suppressed */
H. Peter Anvin41087062016-03-03 14:27:34 -08005127 nasm_fatal(0,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005128 "expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005129 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005130 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005131 if (i->next)
5132 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005133 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005134 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005135 nasm_free(i);
H. Peter Anvin130736c2016-02-17 20:27:41 -08005136 if (!istk) {
5137 line = NULL;
5138 goto done;
5139 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005140 if (istk->expansion && istk->expansion->finishes)
5141 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005142 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005143 }
5144
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005145 /*
5146 * We must expand MMacro parameters and MMacro-local labels
5147 * _before_ we plunge into directive processing, to cope
5148 * with things like `%define something %1' such as STRUC
5149 * uses. Unless we're _defining_ a MMacro, in which case
5150 * those tokens should be left alone to go into the
5151 * definition; and unless we're in a non-emitting
5152 * condition, in which case we don't want to meddle with
5153 * anything.
5154 */
5155 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5156 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005157 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005158 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005159
H. Peter Anvine2c80182005-01-15 22:15:51 +00005160 /*
5161 * Check the line to see if it's a preprocessor directive.
5162 */
5163 if (do_directive(tline) == DIRECTIVE_FOUND) {
5164 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005165 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005166 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005167 * We're defining a multi-line macro. We emit nothing
5168 * at all, and just
5169 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005170 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005171 Line *l = nasm_malloc(sizeof(Line));
5172 l->next = defining->expansion;
5173 l->first = tline;
5174 l->finishes = NULL;
5175 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005176 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005177 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005178 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005179 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005180 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005181 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005182 * directive so we keep our place correctly.
5183 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005184 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005185 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005186 } else if (istk->mstk && !istk->mstk->in_progress) {
5187 /*
5188 * We're in a %rep block which has been terminated, so
5189 * we're walking through to the %endrep without
5190 * emitting anything. Emit nothing at all, not even a
5191 * blank line: when we emerge from the %rep block we'll
5192 * give a line-number directive so we keep our place
5193 * correctly.
5194 */
5195 free_tlist(tline);
5196 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005197 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005198 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005199 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005200 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005201 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005202 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005203 line = detoken(tline, true);
5204 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005205 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005206 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005207 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005208 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005209 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005210 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005211
H. Peter Anvin130736c2016-02-17 20:27:41 -08005212done:
5213 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005214 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005215}
5216
H. Peter Anvine2c80182005-01-15 22:15:51 +00005217static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005218{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005219 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005220
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005221 if (defining) {
5222 if (defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005223 nasm_error(ERR_NONFATAL,
5224 "end of file while still defining macro `%s'",
5225 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005226 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005227 nasm_error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005228 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005229
5230 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005231 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005232 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005233
5234 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005235
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005236 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005237 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005238 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005239 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005240 Include *i = istk;
5241 istk = istk->next;
5242 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005243 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005244 }
5245 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005246 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005247 src_set_fname(NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005248 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005249 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005250 free_llist(predef);
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005251 predef = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005252 delete_Blocks();
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005253 freeTokens = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005254 while ((i = ipath)) {
5255 ipath = i->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005256 if (i->path)
5257 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005258 nasm_free(i);
5259 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005260 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005261}
5262
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005263static void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005264{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005265 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005266
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005267 i = nasm_malloc(sizeof(IncPath));
5268 i->path = path ? nasm_strdup(path) : NULL;
5269 i->next = NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005270
H. Peter Anvin89cee572009-07-15 09:16:54 -04005271 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005272 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005273 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005274 j = j->next;
5275 j->next = i;
5276 } else {
5277 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005278 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005279}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005280
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005281static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005282{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005283 Token *inc, *space, *name;
5284 Line *l;
5285
H. Peter Anvin734b1882002-04-30 21:01:08 +00005286 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5287 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5288 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005289
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005290 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005291 l->next = predef;
5292 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005293 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005294 predef = l;
5295}
5296
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005297static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005298{
5299 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005300 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005301 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005302
H. Peter Anvin130736c2016-02-17 20:27:41 -08005303 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005304
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005305 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005306 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5307 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005308 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005309 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005310 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005311 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005312 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005313
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005314 if (space->next->type != TOK_PREPROC_ID &&
5315 space->next->type != TOK_ID)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005316 nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005317
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005318 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005319 l->next = predef;
5320 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005321 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005322 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005323
5324 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005325}
5326
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005327static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005328{
5329 Token *def, *space;
5330 Line *l;
5331
H. Peter Anvin734b1882002-04-30 21:01:08 +00005332 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5333 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005334 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005335
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005336 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005337 l->next = predef;
5338 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005339 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005340 predef = l;
5341}
5342
H. Peter Anvinf7606612016-07-13 14:23:48 -07005343static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005344{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005345 macros_t **mp;
5346
5347 /* Find the end of the list and avoid duplicates */
5348 for (mp = stdmacros; *mp; mp++) {
5349 if (*mp == macros)
5350 return; /* Nothing to do */
5351 }
5352
5353 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5354
5355 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005356}
5357
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005358static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005359{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005360 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005361 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005362 tok->text = nasm_strdup(numbuf);
5363 tok->type = TOK_NUMBER;
5364}
5365
H. Peter Anvin37368952016-05-09 14:10:32 -07005366static void pp_list_one_macro(MMacro *m, int severity)
5367{
5368 if (!m)
5369 return;
5370
5371 /* We need to print the next_active list in reverse order */
5372 pp_list_one_macro(m->next_active, severity);
5373
5374 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005375 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvin37368952016-05-09 14:10:32 -07005376 nasm_error(severity, "... from macro `%s' defined here", m->name);
5377 }
5378}
5379
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005380static void pp_error_list_macros(int severity)
5381{
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005382 int32_t saved_line;
5383 const char *saved_fname = NULL;
5384
5385 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY;
H. Peter Anvin274cda82016-05-10 02:56:29 -07005386 src_get(&saved_line, &saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005387
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005388 if (istk)
5389 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005390
H. Peter Anvin274cda82016-05-10 02:56:29 -07005391 src_set(saved_line, saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005392}
5393
H. Peter Anvine7469712016-02-18 02:20:59 -08005394const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005395 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005396 pp_reset,
5397 pp_getline,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005398 pp_cleanup,
H. Peter Anvinf7606612016-07-13 14:23:48 -07005399 pp_add_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005400 pp_pre_define,
5401 pp_pre_undefine,
5402 pp_pre_include,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005403 pp_include_path,
5404 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005405};