blob: b4c90aad0fe0aa7ca4652073e83cf7b89b7ad671 [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++;
Martin Lindheb150e382016-11-16 15:36:53 +01001057 } else if (isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001058 ; /* just advance */
1059 else if (c == '.') {
1060 /*
1061 * we need to deal with consequences of the legacy
1062 * parser, like "1.nolist" being two tokens
1063 * (TOK_NUMBER, TOK_ID) here; at least give it
1064 * a shot for now. In the future, we probably need
1065 * a flex-based scanner with proper pattern matching
1066 * to do it as well as it can be done. Nothing in
1067 * the world is going to help the person who wants
1068 * 0x123.p16 interpreted as two tokens, though.
1069 */
1070 r = p;
1071 while (*r == '_')
1072 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001073
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001074 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1075 (!is_hex && (*r == 'e' || *r == 'E')) ||
1076 (*r == 'p' || *r == 'P')) {
1077 p = r;
1078 is_float = true;
1079 } else
1080 break; /* Terminate the token */
1081 } else
1082 break;
1083 }
1084 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001085
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001086 if (p == line+1 && *line == '$') {
1087 type = TOK_OTHER; /* TOKEN_HERE */
1088 } else {
1089 if (has_e && !is_hex) {
1090 /* 1e13 is floating-point, but 1e13h is not */
1091 is_float = true;
1092 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001093
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001094 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1095 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001096 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001097 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001098 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 /*
1100 * Whitespace just before end-of-line is discarded by
1101 * pretending it's a comment; whitespace just before a
1102 * comment gets lumped into the comment.
1103 */
1104 if (!*p || *p == ';') {
1105 type = TOK_COMMENT;
1106 while (*p)
1107 p++;
1108 }
1109 } else if (*p == ';') {
1110 type = TOK_COMMENT;
1111 while (*p)
1112 p++;
1113 } else {
1114 /*
1115 * Anything else is an operator of some kind. We check
1116 * for all the double-character operators (>>, <<, //,
1117 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001118 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001119 */
1120 type = TOK_OTHER;
1121 if ((p[0] == '>' && p[1] == '>') ||
1122 (p[0] == '<' && p[1] == '<') ||
1123 (p[0] == '/' && p[1] == '/') ||
1124 (p[0] == '<' && p[1] == '=') ||
1125 (p[0] == '>' && p[1] == '=') ||
1126 (p[0] == '=' && p[1] == '=') ||
1127 (p[0] == '!' && p[1] == '=') ||
1128 (p[0] == '<' && p[1] == '>') ||
1129 (p[0] == '&' && p[1] == '&') ||
1130 (p[0] == '|' && p[1] == '|') ||
1131 (p[0] == '^' && p[1] == '^')) {
1132 p++;
1133 }
1134 p++;
1135 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001136
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 /* Handling unterminated string by UNV */
1138 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001139 {
1140 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1141 t->text[p-line] = *line;
1142 tail = &t->next;
1143 }
1144 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145 if (type != TOK_COMMENT) {
1146 *tail = t = new_Token(NULL, type, line, p - line);
1147 tail = &t->next;
1148 }
1149 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001150 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001151 return list;
1152}
1153
H. Peter Anvince616072002-04-30 21:02:23 +00001154/*
1155 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001156 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001157 * deleted only all at once by the delete_Blocks function.
1158 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001160{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001161 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001163 /* first, get to the end of the linked list */
1164 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001165 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001166 /* now allocate the requested chunk */
1167 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001168
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001169 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001170 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001171 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001172}
1173
1174/*
1175 * this function deletes all managed blocks of memory
1176 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001178{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001179 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001180
H. Peter Anvin70653092007-10-19 14:42:29 -07001181 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001182 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001183 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001184 * free it.
1185 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001187 if (b->chunk)
1188 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001189 a = b;
1190 b = b->next;
1191 if (a != &blocks)
1192 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001193 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001194 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001195}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001196
1197/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001198 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001199 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001200 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001201 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001202static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001203 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001204{
1205 Token *t;
1206 int i;
1207
H. Peter Anvin89cee572009-07-15 09:16:54 -04001208 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1210 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1211 freeTokens[i].next = &freeTokens[i + 1];
1212 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001213 }
1214 t = freeTokens;
1215 freeTokens = t->next;
1216 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001217 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001218 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001219 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001220 t->text = NULL;
1221 } else {
1222 if (txtlen == 0)
1223 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001224 t->text = nasm_malloc(txtlen+1);
1225 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001226 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001227 }
1228 return t;
1229}
1230
H. Peter Anvine2c80182005-01-15 22:15:51 +00001231static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001232{
1233 Token *next = t->next;
1234 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001235 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001236 freeTokens = t;
1237 return next;
1238}
1239
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001240/*
1241 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001242 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1243 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001244 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001245static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001246{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001247 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001248 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001249 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001250 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001251
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001252 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001253 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001254 char *v;
1255 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001256
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001257 v = t->text + 2;
1258 if (*v == '\'' || *v == '\"' || *v == '`') {
1259 size_t len = nasm_unquote(v, NULL);
1260 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001261
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001262 if (len != clen) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001263 nasm_error(ERR_NONFATAL | ERR_PASS1,
1264 "NUL character in %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001265 v = NULL;
1266 }
1267 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001268
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001269 if (v) {
1270 char *p = getenv(v);
1271 if (!p) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001272 nasm_error(ERR_NONFATAL | ERR_PASS1,
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001273 "nonexistent environment variable `%s'", v);
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001274 /*
1275 * FIXME We better should investigate if accessing
1276 * ->text[1] without ->text[0] is safe enough.
1277 */
1278 t->text = nasm_zalloc(2);
1279 } else
1280 t->text = nasm_strdup(p);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001281 }
1282 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001283 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001284
H. Peter Anvine2c80182005-01-15 22:15:51 +00001285 /* Expand local macros here and not during preprocessing */
1286 if (expand_locals &&
1287 t->type == TOK_PREPROC_ID && t->text &&
1288 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001289 const char *q;
1290 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001291 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001293 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001294 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001295 p = nasm_strcat(buffer, q);
1296 nasm_free(t->text);
1297 t->text = p;
1298 }
1299 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001300 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001301 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001302 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001304 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001305
H. Peter Anvin734b1882002-04-30 21:01:08 +00001306 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001307
1308 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001309 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001310 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001312 q = t->text;
1313 while (*q)
1314 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001315 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001316 }
1317 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001318
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001319 return line;
1320}
1321
1322/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001323 * A scanner, suitable for use by the expression evaluator, which
1324 * operates on a line of Tokens. Expects a pointer to a pointer to
1325 * the first token in the line to be passed in as its private_data
1326 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001327 *
1328 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001329 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001330static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001331{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001332 Token **tlineptr = private_data;
1333 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001334 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001335
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 do {
1337 tline = *tlineptr;
1338 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001339 } while (tline && (tline->type == TOK_WHITESPACE ||
1340 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001341
1342 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001344
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001345 tokval->t_charptr = tline->text;
1346
H. Peter Anvin76690a12002-04-30 20:52:49 +00001347 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001348 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001349 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001351
H. Peter Anvine2c80182005-01-15 22:15:51 +00001352 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001353 p = tokval->t_charptr = tline->text;
1354 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001355 tokval->t_charptr++;
1356 return tokval->t_type = TOKEN_ID;
1357 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001358
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001359 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001360 if (r >= p+MAX_KEYWORD)
1361 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001362 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001363 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001364 *s = '\0';
1365 /* right, so we have an identifier sitting in temp storage. now,
1366 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001367 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001368 }
1369
H. Peter Anvine2c80182005-01-15 22:15:51 +00001370 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001371 bool rn_error;
1372 tokval->t_integer = readnum(tline->text, &rn_error);
1373 tokval->t_charptr = tline->text;
1374 if (rn_error)
1375 return tokval->t_type = TOKEN_ERRNUM;
1376 else
1377 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001378 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001379
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001380 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001381 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001382 }
1383
H. Peter Anvine2c80182005-01-15 22:15:51 +00001384 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001385 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001386
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001387 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001388 tokval->t_charptr = tline->text;
1389 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001390
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001391 if (ep[0] != bq || ep[1] != '\0')
1392 return tokval->t_type = TOKEN_ERRSTR;
1393 else
1394 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001395 }
1396
H. Peter Anvine2c80182005-01-15 22:15:51 +00001397 if (tline->type == TOK_OTHER) {
1398 if (!strcmp(tline->text, "<<"))
1399 return tokval->t_type = TOKEN_SHL;
1400 if (!strcmp(tline->text, ">>"))
1401 return tokval->t_type = TOKEN_SHR;
1402 if (!strcmp(tline->text, "//"))
1403 return tokval->t_type = TOKEN_SDIV;
1404 if (!strcmp(tline->text, "%%"))
1405 return tokval->t_type = TOKEN_SMOD;
1406 if (!strcmp(tline->text, "=="))
1407 return tokval->t_type = TOKEN_EQ;
1408 if (!strcmp(tline->text, "<>"))
1409 return tokval->t_type = TOKEN_NE;
1410 if (!strcmp(tline->text, "!="))
1411 return tokval->t_type = TOKEN_NE;
1412 if (!strcmp(tline->text, "<="))
1413 return tokval->t_type = TOKEN_LE;
1414 if (!strcmp(tline->text, ">="))
1415 return tokval->t_type = TOKEN_GE;
1416 if (!strcmp(tline->text, "&&"))
1417 return tokval->t_type = TOKEN_DBL_AND;
1418 if (!strcmp(tline->text, "^^"))
1419 return tokval->t_type = TOKEN_DBL_XOR;
1420 if (!strcmp(tline->text, "||"))
1421 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001422 }
1423
1424 /*
1425 * We have no other options: just return the first character of
1426 * the token text.
1427 */
1428 return tokval->t_type = tline->text[0];
1429}
1430
1431/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001432 * Compare a string to the name of an existing macro; this is a
1433 * simple wrapper which calls either strcmp or nasm_stricmp
1434 * depending on the value of the `casesense' parameter.
1435 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001436static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001437{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001438 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001439}
1440
1441/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001442 * Compare a string to the name of an existing macro; this is a
1443 * simple wrapper which calls either strcmp or nasm_stricmp
1444 * depending on the value of the `casesense' parameter.
1445 */
1446static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1447{
1448 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1449}
1450
1451/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001452 * Return the Context structure associated with a %$ token. Return
1453 * NULL, having _already_ reported an error condition, if the
1454 * context stack isn't deep enough for the supplied number of $
1455 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001456 *
1457 * If "namep" is non-NULL, set it to the pointer to the macro name
1458 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001459 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001460static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001461{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001462 Context *ctx;
1463 int i;
1464
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001465 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001466 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001467
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001468 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001469 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001470
H. Peter Anvine2c80182005-01-15 22:15:51 +00001471 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001472 nasm_error(ERR_NONFATAL, "`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001473 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001474 }
1475
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001476 name += 2;
1477 ctx = cstk;
1478 i = 0;
1479 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001480 name++;
1481 i++;
1482 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001483 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001484 if (!ctx) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001485 nasm_error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001486 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001487 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001488 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001489
1490 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001491 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001492
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001493 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001494}
1495
1496/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001497 * Open an include file. This routine must always return a valid
1498 * file pointer if it returns - it's responsible for throwing an
1499 * ERR_FATAL and bombing out completely if not. It should also try
1500 * the include path one by one until it finds the file or reaches
1501 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001502 *
1503 * Note: for INC_PROBE the function returns NULL at all times;
1504 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001505 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001506enum incopen_mode {
1507 INC_NEEDED, /* File must exist */
1508 INC_OPTIONAL, /* Missing is OK */
1509 INC_PROBE /* Only an existence probe */
1510};
1511
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001512/* This is conducts a full pathname search */
1513static FILE *inc_fopen_search(const char *file, StrList **slpath,
1514 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001515{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001516 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001517 char *prefix = "";
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001518 const IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001519 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001520 size_t prefix_len = 0;
1521 StrList *sl;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001522 size_t path_len;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001523 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001524
H. Peter Anvine2c80182005-01-15 22:15:51 +00001525 while (1) {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001526 path_len = prefix_len + len + 1;
1527
1528 sl = nasm_malloc(path_len + sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001529 memcpy(sl->str, prefix, prefix_len);
1530 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001531 sl->next = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001532
H. Peter Anvind81a2352016-09-21 14:03:18 -07001533 if (omode == INC_PROBE) {
1534 fp = NULL;
1535 found = nasm_file_exists(sl->str);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001536 } else {
H. Peter Anvind81a2352016-09-21 14:03:18 -07001537 fp = nasm_open_read(sl->str, fmode);
1538 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001539 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001540 if (found) {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001541 *slpath = sl;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001542 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001543 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001544
H. Peter Anvind81a2352016-09-21 14:03:18 -07001545 nasm_free(sl);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001546
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001547 if (!ip)
1548 return NULL;
1549
1550 prefix = ip->path;
1551 prefix_len = strlen(prefix);
1552 ip = ip->next;
1553 }
1554}
1555
1556/*
1557 * Open a file, or test for the presence of one (depending on omode),
1558 * considering the include path.
1559 */
1560static FILE *inc_fopen(const char *file,
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001561 StrList **dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001562 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001563 enum incopen_mode omode,
1564 enum file_flags fmode)
1565{
1566 StrList *sl;
1567 struct hash_insert hi;
1568 void **hp;
1569 char *path;
1570 FILE *fp = NULL;
1571
1572 hp = hash_find(&FileHash, file, &hi);
1573 if (hp) {
1574 path = *hp;
1575 } else {
1576 /* Need to do the actual path search */
1577 size_t file_len;
1578
1579 sl = NULL;
1580 fp = inc_fopen_search(file, &sl, omode, fmode);
1581
1582 file_len = strlen(file);
1583
1584 if (!sl) {
1585 /* Store negative result for this file */
1586 sl = nasm_malloc(file_len + 1 + sizeof sl->next);
1587 memcpy(sl->str, file, file_len+1);
1588 sl->next = NULL;
1589 file = sl->str;
1590 path = NULL;
1591 } else {
1592 path = sl->str;
1593 file = strchr(path, '\0') - file_len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001594 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001595
1596 hash_add(&hi, file, path); /* Positive or negative result */
1597
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001598 /*
1599 * Add file to dependency path. The in_list() is needed
1600 * in case the file was already added with %depend.
1601 */
1602 if (path || omode != INC_NEEDED)
H. Peter Anvin436e3672016-10-04 01:12:28 -07001603 nasm_add_to_strlist(dhead, sl);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001604 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001605
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001606 if (!path) {
1607 if (omode == INC_NEEDED)
1608 nasm_fatal(0, "unable to open include file `%s'", file);
1609
1610 if (found_path)
1611 *found_path = NULL;
1612
1613 return NULL;
1614 }
1615
1616 if (!fp && omode != INC_PROBE)
1617 fp = nasm_open_read(file, fmode);
1618
1619 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001620 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001621
1622 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001623}
1624
1625/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001626 * Opens an include or input file. Public version, for use by modules
1627 * that get a file:lineno pair and need to look at the file again
1628 * (e.g. the CodeView debug backend). Returns NULL on failure.
1629 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001630FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001631{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001632 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001633}
1634
1635/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001636 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001637 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001638 * return true if _any_ single-line macro of that name is defined.
1639 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001640 * `nparam' or no parameters is defined.
1641 *
1642 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001643 * defined, or nparam is -1, the address of the definition structure
1644 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001645 * is NULL, no action will be taken regarding its contents, and no
1646 * error will occur.
1647 *
1648 * Note that this is also called with nparam zero to resolve
1649 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001650 *
1651 * If you already know which context macro belongs to, you can pass
1652 * the context pointer as first parameter; if you won't but name begins
1653 * with %$ the context will be automatically computed. If all_contexts
1654 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001655 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001656static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001657smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001658 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001659{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001660 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001661 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001662
H. Peter Anvin97a23472007-09-16 17:57:25 -07001663 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001664 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001665 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001666 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001667 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001668 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001669 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001670 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001671 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001672 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001673 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001674 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001675
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676 while (m) {
1677 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001678 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001679 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001680 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001681 *defn = m;
1682 else
1683 *defn = NULL;
1684 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001685 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 }
1687 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001688 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001689
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001690 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001691}
1692
1693/*
1694 * Count and mark off the parameters in a multi-line macro call.
1695 * This is called both from within the multi-line macro expansion
1696 * code, and also to mark off the default parameters when provided
1697 * in a %macro definition line.
1698 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001699static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001700{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001701 int paramsize, brace;
1702
1703 *nparam = paramsize = 0;
1704 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001705 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001706 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001707 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001708 paramsize += PARAM_DELTA;
1709 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1710 }
1711 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001712 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001713 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001714 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001715 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001716 if (brace) {
1717 while (brace && (t = t->next) != NULL) {
1718 if (tok_is_(t, "{"))
1719 brace++;
1720 else if (tok_is_(t, "}"))
1721 brace--;
1722 }
1723
1724 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001725 /*
1726 * Now we've found the closing brace, look further
1727 * for the comma.
1728 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001729 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001730 skip_white_(t);
1731 if (tok_isnt_(t, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001732 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001733 "braces do not enclose all of macro parameter");
1734 while (tok_isnt_(t, ","))
1735 t = t->next;
1736 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001737 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001738 } else {
1739 while (tok_isnt_(t, ","))
1740 t = t->next;
1741 }
1742 if (t) { /* got a comma/brace */
1743 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001744 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001745 }
1746}
1747
1748/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001749 * Determine whether one of the various `if' conditions is true or
1750 * not.
1751 *
1752 * We must free the tline we get passed.
1753 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001754static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001755{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001756 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001757 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001758 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001759 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001760 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001761 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001762 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001763
1764 origline = tline;
1765
H. Peter Anvine2c80182005-01-15 22:15:51 +00001766 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001767 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001768 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001769 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001770 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001771 if (!tline)
1772 break;
1773 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001774 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001775 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001776 free_tlist(origline);
1777 return -1;
1778 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001779 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001780 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001781 tline = tline->next;
1782 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001783 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001784
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001785 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001786 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001787 while (tline) {
1788 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001789 if (!tline || (tline->type != TOK_ID &&
1790 (tline->type != TOK_PREPROC_ID ||
1791 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001792 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001793 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001794 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001795 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001796 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001797 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001798 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001799 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001800 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001801
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001802 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001803 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001804 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001805 while (tline) {
1806 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001807 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001808 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001809 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001810 tline->text[1] != '!'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001811 nasm_error(ERR_NONFATAL,
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001812 "`%s' expects environment variable names",
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001813 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001814 goto fail;
1815 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001816 p = tline->text;
1817 if (tline->type == TOK_PREPROC_ID)
1818 p += 2; /* Skip leading %! */
1819 if (*p == '\'' || *p == '\"' || *p == '`')
1820 nasm_unquote_cstr(p, ct);
1821 if (getenv(p))
1822 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001823 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001824 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001825 break;
1826
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001827 case PPC_IFIDN:
1828 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001829 tline = expand_smacro(tline);
1830 t = tt = tline;
1831 while (tok_isnt_(tt, ","))
1832 tt = tt->next;
1833 if (!tt) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001834 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001835 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001836 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001837 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001838 }
1839 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001840 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001841 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1842 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001843 nasm_error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001844 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001845 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001846 }
1847 if (t->type == TOK_WHITESPACE) {
1848 t = t->next;
1849 continue;
1850 }
1851 if (tt->type == TOK_WHITESPACE) {
1852 tt = tt->next;
1853 continue;
1854 }
1855 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001856 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001857 break;
1858 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001859 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001860 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001861 size_t l1 = nasm_unquote(t->text, NULL);
1862 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001863
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001864 if (l1 != l2) {
1865 j = false;
1866 break;
1867 }
1868 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1869 j = false;
1870 break;
1871 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001872 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001873 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001874 break;
1875 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001876
H. Peter Anvine2c80182005-01-15 22:15:51 +00001877 t = t->next;
1878 tt = tt->next;
1879 }
1880 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001881 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001882 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001883
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001884 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001885 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001886 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001887 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001888
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001889 skip_white_(tline);
1890 tline = expand_id(tline);
1891 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001892 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001893 "`%s' expects a macro name", pp_directives[ct]);
1894 goto fail;
1895 }
1896 searching.name = nasm_strdup(tline->text);
1897 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001898 searching.plus = false;
1899 searching.nolist = false;
1900 searching.in_progress = 0;
1901 searching.max_depth = 0;
1902 searching.rep_nest = NULL;
1903 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001904 searching.nparam_max = INT_MAX;
1905 tline = expand_smacro(tline->next);
1906 skip_white_(tline);
1907 if (!tline) {
1908 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001909 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001910 "`%s' expects a parameter count or nothing",
1911 pp_directives[ct]);
1912 } else {
1913 searching.nparam_min = searching.nparam_max =
1914 readnum(tline->text, &j);
1915 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001916 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001917 "unable to parse parameter count `%s'",
1918 tline->text);
1919 }
1920 if (tline && tok_is_(tline->next, "-")) {
1921 tline = tline->next->next;
1922 if (tok_is_(tline, "*"))
1923 searching.nparam_max = INT_MAX;
1924 else if (!tok_type_(tline, TOK_NUMBER))
H. Peter Anvin130736c2016-02-17 20:27:41 -08001925 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001926 "`%s' expects a parameter count after `-'",
1927 pp_directives[ct]);
1928 else {
1929 searching.nparam_max = readnum(tline->text, &j);
1930 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001931 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001932 "unable to parse parameter count `%s'",
1933 tline->text);
1934 if (searching.nparam_min > searching.nparam_max)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001935 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001936 "minimum parameter count exceeds maximum");
1937 }
1938 }
1939 if (tline && tok_is_(tline->next, "+")) {
1940 tline = tline->next;
1941 searching.plus = true;
1942 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001943 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1944 while (mmac) {
1945 if (!strcmp(mmac->name, searching.name) &&
1946 (mmac->nparam_min <= searching.nparam_max
1947 || searching.plus)
1948 && (searching.nparam_min <= mmac->nparam_max
1949 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001950 found = true;
1951 break;
1952 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001953 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001954 }
1955 if (tline && tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001956 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001957 "trailing garbage after %%ifmacro ignored");
1958 nasm_free(searching.name);
1959 j = found;
1960 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001961 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001962
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001963 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001964 needtype = TOK_ID;
1965 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001966 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001967 needtype = TOK_NUMBER;
1968 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001969 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001970 needtype = TOK_STRING;
1971 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001972
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001973iftype:
1974 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001975
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001976 while (tok_type_(t, TOK_WHITESPACE) ||
1977 (needtype == TOK_NUMBER &&
1978 tok_type_(t, TOK_OTHER) &&
1979 (t->text[0] == '-' || t->text[0] == '+') &&
1980 !t->text[1]))
1981 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001982
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001983 j = tok_type_(t, needtype);
1984 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001985
1986 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001987 t = tline = expand_smacro(tline);
1988 while (tok_type_(t, TOK_WHITESPACE))
1989 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001990
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001991 j = false;
1992 if (t) {
1993 t = t->next; /* Skip the actual token */
1994 while (tok_type_(t, TOK_WHITESPACE))
1995 t = t->next;
1996 j = !t; /* Should be nothing left */
1997 }
1998 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001999
H. Peter Anvin134b9462008-02-16 17:01:40 -08002000 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002001 t = tline = expand_smacro(tline);
2002 while (tok_type_(t, TOK_WHITESPACE))
2003 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002004
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002005 j = !t; /* Should be empty */
2006 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002007
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002008 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002009 t = tline = expand_smacro(tline);
2010 tptr = &t;
2011 tokval.t_type = TOKEN_INVALID;
2012 evalresult = evaluate(ppscan, tptr, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -08002013 NULL, pass | CRITICAL, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002014 if (!evalresult)
2015 return -1;
2016 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002017 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002018 "trailing garbage after expression ignored");
2019 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002020 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002021 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002022 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002023 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002024 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002025 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002026
H. Peter Anvine2c80182005-01-15 22:15:51 +00002027 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002028 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002029 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002030 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002031 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002032 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002033
2034 free_tlist(origline);
2035 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002036
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002037fail:
2038 free_tlist(origline);
2039 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002040}
2041
2042/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002043 * Common code for defining an smacro
2044 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002045static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002046 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002047{
2048 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002049 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002050
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002051 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002052 if (!smac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002053 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002054 "single-line macro `%s' defined both with and"
2055 " without parameters", mname);
2056 /*
2057 * Some instances of the old code considered this a failure,
2058 * some others didn't. What is the right thing to do here?
2059 */
2060 free_tlist(expansion);
2061 return false; /* Failure */
2062 } else {
2063 /*
2064 * We're redefining, so we have to take over an
2065 * existing SMacro structure. This means freeing
2066 * what was already in it.
2067 */
2068 nasm_free(smac->name);
2069 free_tlist(smac->expansion);
2070 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002071 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002072 smtbl = ctx ? &ctx->localmac : &smacros;
2073 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002074 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002075 smac->next = *smhead;
2076 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002077 }
2078 smac->name = nasm_strdup(mname);
2079 smac->casesense = casesense;
2080 smac->nparam = nparam;
2081 smac->expansion = expansion;
2082 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002083 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002084}
2085
2086/*
2087 * Undefine an smacro
2088 */
2089static void undef_smacro(Context *ctx, const char *mname)
2090{
2091 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002092 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002093
H. Peter Anvin166c2472008-05-28 12:28:58 -07002094 smtbl = ctx ? &ctx->localmac : &smacros;
2095 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002096
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002097 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002098 /*
2099 * We now have a macro name... go hunt for it.
2100 */
2101 sp = smhead;
2102 while ((s = *sp) != NULL) {
2103 if (!mstrcmp(s->name, mname, s->casesense)) {
2104 *sp = s->next;
2105 nasm_free(s->name);
2106 free_tlist(s->expansion);
2107 nasm_free(s);
2108 } else {
2109 sp = &s->next;
2110 }
2111 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002112 }
2113}
2114
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002115/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002116 * Parse a mmacro specification.
2117 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002118static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002119{
2120 bool err;
2121
2122 tline = tline->next;
2123 skip_white_(tline);
2124 tline = expand_id(tline);
2125 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002126 nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002127 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002128 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002129
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002130 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002131 def->name = nasm_strdup(tline->text);
2132 def->plus = false;
2133 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002134 def->in_progress = 0;
2135 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002136 def->nparam_min = 0;
2137 def->nparam_max = 0;
2138
H. Peter Anvina26433d2008-07-16 14:40:01 -07002139 tline = expand_smacro(tline->next);
2140 skip_white_(tline);
2141 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002142 nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002143 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002144 def->nparam_min = def->nparam_max =
2145 readnum(tline->text, &err);
2146 if (err)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002147 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002148 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002149 }
2150 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002151 tline = tline->next->next;
2152 if (tok_is_(tline, "*")) {
2153 def->nparam_max = INT_MAX;
2154 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002155 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002156 "`%s' expects a parameter count after `-'", directive);
2157 } else {
2158 def->nparam_max = readnum(tline->text, &err);
2159 if (err) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002160 nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002161 tline->text);
2162 }
2163 if (def->nparam_min > def->nparam_max) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002164 nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002165 }
2166 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002167 }
2168 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002169 tline = tline->next;
2170 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002171 }
2172 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002173 !nasm_stricmp(tline->next->text, ".nolist")) {
2174 tline = tline->next;
2175 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002176 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002177
H. Peter Anvina26433d2008-07-16 14:40:01 -07002178 /*
2179 * Handle default parameters.
2180 */
2181 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002182 def->dlist = tline->next;
2183 tline->next = NULL;
2184 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002185 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002186 def->dlist = NULL;
2187 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002188 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002189 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002190
H. Peter Anvin89cee572009-07-15 09:16:54 -04002191 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002192 !def->plus)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002193 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002194 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002195
H. Peter Anvina26433d2008-07-16 14:40:01 -07002196 return true;
2197}
2198
2199
2200/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002201 * Decode a size directive
2202 */
2203static int parse_size(const char *str) {
2204 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002205 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002206 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002207 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002208
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002209 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002210}
2211
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002212/*
2213 * Process a preprocessor %pragma directive. Currently there are none.
2214 * Gets passed the token list starting with the "preproc" token from
2215 * "%pragma preproc".
2216 */
2217static void do_pragma_preproc(Token *tline)
2218{
2219 /* Skip to the real stuff */
2220 tline = tline->next;
2221 skip_white_(tline);
2222 if (!tline)
2223 return;
2224
2225 (void)tline; /* Nothing else to do at present */
2226}
2227
Ed Beroset3ab3f412002-06-11 03:31:49 +00002228/**
2229 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002230 * Find out if a line contains a preprocessor directive, and deal
2231 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002232 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002233 * If a directive _is_ found, it is the responsibility of this routine
2234 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002235 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002236 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002237 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002238 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002239 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002240 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002241static int do_directive(Token *tline, char **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002242{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002243 enum preproc_token i;
2244 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002245 bool err;
2246 int nparam;
2247 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002248 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002249 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002250 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002251 char *p, *pp;
2252 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002253 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002254 Include *inc;
2255 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002256 Cond *cond;
2257 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002258 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002259 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002260 struct tokenval tokval;
2261 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002262 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002263 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002264 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002265 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002266
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002267 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002268 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002269
H. Peter Anvineba20a72002-04-30 20:53:55 +00002270 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002271 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002272 (tline->text[1] == '%' || tline->text[1] == '$'
2273 || tline->text[1] == '!'))
2274 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002275
H. Peter Anvin4169a472007-09-12 01:29:43 +00002276 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002277
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002278 /*
2279 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2280 * since they are known to be buggy at moment, we need to fix them
2281 * in future release (2.09-2.10)
2282 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002283 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002284 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002285 tline->text);
2286 return NO_DIRECTIVE_FOUND;
2287 }
2288
2289 /*
2290 * If we're in a non-emitting branch of a condition construct,
2291 * or walking to the end of an already terminated %rep block,
2292 * we should ignore all directives except for condition
2293 * directives.
2294 */
2295 if (((istk->conds && !emitting(istk->conds->state)) ||
2296 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2297 return NO_DIRECTIVE_FOUND;
2298 }
2299
2300 /*
2301 * If we're defining a macro or reading a %rep block, we should
2302 * ignore all directives except for %macro/%imacro (which nest),
2303 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2304 * If we're in a %rep block, another %rep nests, so should be let through.
2305 */
2306 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2307 i != PP_RMACRO && i != PP_IRMACRO &&
2308 i != PP_ENDMACRO && i != PP_ENDM &&
2309 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2310 return NO_DIRECTIVE_FOUND;
2311 }
2312
2313 if (defining) {
2314 if (i == PP_MACRO || i == PP_IMACRO ||
2315 i == PP_RMACRO || i == PP_IRMACRO) {
2316 nested_mac_count++;
2317 return NO_DIRECTIVE_FOUND;
2318 } else if (nested_mac_count > 0) {
2319 if (i == PP_ENDMACRO) {
2320 nested_mac_count--;
2321 return NO_DIRECTIVE_FOUND;
2322 }
2323 }
2324 if (!defining->name) {
2325 if (i == PP_REP) {
2326 nested_rep_count++;
2327 return NO_DIRECTIVE_FOUND;
2328 } else if (nested_rep_count > 0) {
2329 if (i == PP_ENDREP) {
2330 nested_rep_count--;
2331 return NO_DIRECTIVE_FOUND;
2332 }
2333 }
2334 }
2335 }
2336
H. Peter Anvin4169a472007-09-12 01:29:43 +00002337 switch (i) {
2338 case PP_INVALID:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002339 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002340 tline->text);
2341 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002342
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002343 case PP_PRAGMA:
2344 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002345 * %pragma namespace options...
2346 *
2347 * The namespace "preproc" is reserved for the preprocessor;
2348 * all other namespaces generate a [pragma] assembly directive.
2349 *
2350 * Invalid %pragmas are ignored and may have different
2351 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002352 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002353 tline = tline->next;
2354 skip_white_(tline);
2355 tline = expand_smacro(tline);
2356 if (tok_type_(tline, TOK_ID)) {
2357 if (!nasm_stricmp(tline->text, "preproc")) {
2358 /* Preprocessor pragma */
2359 do_pragma_preproc(tline);
2360 } else {
2361 /* Build the assembler directive */
2362 t = new_Token(NULL, TOK_OTHER, "[", 1);
2363 t->next = new_Token(NULL, TOK_ID, "pragma", 6);
2364 t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2365 tline = t;
2366 for (t = tline; t->next; t = t->next)
2367 ;
2368 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
2369 /* true here can be revisited in the future */
2370 *output = detoken(tline, true);
2371 }
2372 }
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002373 free_tlist(origline);
2374 return DIRECTIVE_FOUND;
2375
H. Peter Anvine2c80182005-01-15 22:15:51 +00002376 case PP_STACKSIZE:
2377 /* Directive to tell NASM what the default stack size is. The
2378 * default is for a 16-bit stack, and this can be overriden with
2379 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002380 */
2381 tline = tline->next;
2382 if (tline && tline->type == TOK_WHITESPACE)
2383 tline = tline->next;
2384 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002385 nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002386 free_tlist(origline);
2387 return DIRECTIVE_FOUND;
2388 }
2389 if (nasm_stricmp(tline->text, "flat") == 0) {
2390 /* All subsequent ARG directives are for a 32-bit stack */
2391 StackSize = 4;
2392 StackPointer = "ebp";
2393 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002394 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002395 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2396 /* All subsequent ARG directives are for a 64-bit stack */
2397 StackSize = 8;
2398 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002399 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002400 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002401 } else if (nasm_stricmp(tline->text, "large") == 0) {
2402 /* All subsequent ARG directives are for a 16-bit stack,
2403 * far function call.
2404 */
2405 StackSize = 2;
2406 StackPointer = "bp";
2407 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002408 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002409 } else if (nasm_stricmp(tline->text, "small") == 0) {
2410 /* All subsequent ARG directives are for a 16-bit stack,
2411 * far function call. We don't support near functions.
2412 */
2413 StackSize = 2;
2414 StackPointer = "bp";
2415 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002416 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002417 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002418 nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002419 free_tlist(origline);
2420 return DIRECTIVE_FOUND;
2421 }
2422 free_tlist(origline);
2423 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002424
H. Peter Anvine2c80182005-01-15 22:15:51 +00002425 case PP_ARG:
2426 /* TASM like ARG directive to define arguments to functions, in
2427 * the following form:
2428 *
2429 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2430 */
2431 offset = ArgOffset;
2432 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002433 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002434 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002435
H. Peter Anvine2c80182005-01-15 22:15:51 +00002436 /* Find the argument name */
2437 tline = tline->next;
2438 if (tline && tline->type == TOK_WHITESPACE)
2439 tline = tline->next;
2440 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002441 nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002442 free_tlist(origline);
2443 return DIRECTIVE_FOUND;
2444 }
2445 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002446
H. Peter Anvine2c80182005-01-15 22:15:51 +00002447 /* Find the argument size type */
2448 tline = tline->next;
2449 if (!tline || tline->type != TOK_OTHER
2450 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002451 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002452 "Syntax error processing `%%arg' directive");
2453 free_tlist(origline);
2454 return DIRECTIVE_FOUND;
2455 }
2456 tline = tline->next;
2457 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002458 nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002459 free_tlist(origline);
2460 return DIRECTIVE_FOUND;
2461 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002462
H. Peter Anvine2c80182005-01-15 22:15:51 +00002463 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002464 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002465 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002466 size = parse_size(tt->text);
2467 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002468 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002469 "Invalid size type for `%%arg' missing directive");
2470 free_tlist(tt);
2471 free_tlist(origline);
2472 return DIRECTIVE_FOUND;
2473 }
2474 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002475
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002476 /* Round up to even stack slots */
2477 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002478
H. Peter Anvine2c80182005-01-15 22:15:51 +00002479 /* Now define the macro for the argument */
2480 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2481 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002482 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002483 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002484
H. Peter Anvine2c80182005-01-15 22:15:51 +00002485 /* Move to the next argument in the list */
2486 tline = tline->next;
2487 if (tline && tline->type == TOK_WHITESPACE)
2488 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002489 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002490 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002491 free_tlist(origline);
2492 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002493
H. Peter Anvine2c80182005-01-15 22:15:51 +00002494 case PP_LOCAL:
2495 /* TASM like LOCAL directive to define local variables for a
2496 * function, in the following form:
2497 *
2498 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2499 *
2500 * The '= LocalSize' at the end is ignored by NASM, but is
2501 * required by TASM to define the local parameter size (and used
2502 * by the TASM macro package).
2503 */
2504 offset = LocalOffset;
2505 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002506 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002507 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002508
H. Peter Anvine2c80182005-01-15 22:15:51 +00002509 /* Find the argument name */
2510 tline = tline->next;
2511 if (tline && tline->type == TOK_WHITESPACE)
2512 tline = tline->next;
2513 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002514 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002515 "`%%local' missing argument parameter");
2516 free_tlist(origline);
2517 return DIRECTIVE_FOUND;
2518 }
2519 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002520
H. Peter Anvine2c80182005-01-15 22:15:51 +00002521 /* Find the argument size type */
2522 tline = tline->next;
2523 if (!tline || tline->type != TOK_OTHER
2524 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002525 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002526 "Syntax error processing `%%local' directive");
2527 free_tlist(origline);
2528 return DIRECTIVE_FOUND;
2529 }
2530 tline = tline->next;
2531 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002532 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002533 "`%%local' missing size type parameter");
2534 free_tlist(origline);
2535 return DIRECTIVE_FOUND;
2536 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002537
H. Peter Anvine2c80182005-01-15 22:15:51 +00002538 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002539 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002540 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002541 size = parse_size(tt->text);
2542 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002543 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002544 "Invalid size type for `%%local' missing directive");
2545 free_tlist(tt);
2546 free_tlist(origline);
2547 return DIRECTIVE_FOUND;
2548 }
2549 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002550
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002551 /* Round up to even stack slots */
2552 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002553
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002554 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002555
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002556 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002557 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2558 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002559 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002560
H. Peter Anvine2c80182005-01-15 22:15:51 +00002561 /* Now define the assign to setup the enter_c macro correctly */
2562 snprintf(directive, sizeof(directive),
2563 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002564 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002565
H. Peter Anvine2c80182005-01-15 22:15:51 +00002566 /* Move to the next argument in the list */
2567 tline = tline->next;
2568 if (tline && tline->type == TOK_WHITESPACE)
2569 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002570 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002571 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002572 free_tlist(origline);
2573 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002574
H. Peter Anvine2c80182005-01-15 22:15:51 +00002575 case PP_CLEAR:
2576 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002577 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002578 "trailing garbage after `%%clear' ignored");
2579 free_macros();
2580 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002581 free_tlist(origline);
2582 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002583
H. Peter Anvin418ca702008-05-30 10:42:30 -07002584 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002585 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002586 skip_white_(t);
2587 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002588 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002589 nasm_error(ERR_NONFATAL, "`%%depend' expects a file name");
H. Peter Anvin418ca702008-05-30 10:42:30 -07002590 free_tlist(origline);
2591 return DIRECTIVE_FOUND; /* but we did _something_ */
2592 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002593 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002594 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002595 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002596 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002597 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002598 nasm_unquote_cstr(p, i);
H. Peter Anvin436e3672016-10-04 01:12:28 -07002599 nasm_add_string_to_strlist(dephead, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002600 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002601 return DIRECTIVE_FOUND;
2602
2603 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002604 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002605 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002606
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002607 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002608 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002609 nasm_error(ERR_NONFATAL, "`%%include' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002610 free_tlist(origline);
2611 return DIRECTIVE_FOUND; /* but we did _something_ */
2612 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002613 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002614 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002615 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002616 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002617 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002618 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002619 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002620 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002621 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002622 found_path = NULL;
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002623 inc->fp = inc_fopen(p, dephead, &found_path,
H. Peter Anvind81a2352016-09-21 14:03:18 -07002624 pass == 0 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002625 if (!inc->fp) {
2626 /* -MG given but file not found */
2627 nasm_free(inc);
2628 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002629 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002630 inc->lineno = src_set_linnum(0);
2631 inc->lineinc = 1;
2632 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002633 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002634 istk = inc;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002635 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002636 }
2637 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002638 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002639
H. Peter Anvind2456592008-06-19 15:04:18 -07002640 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002641 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002642 static macros_t *use_pkg;
2643 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002644
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002645 tline = tline->next;
2646 skip_white_(tline);
2647 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002648
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002649 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002650 tline->type != TOK_INTERNAL_STRING &&
2651 tline->type != TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002652 nasm_error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002653 free_tlist(origline);
2654 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002655 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002656 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002657 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002658 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002659 if (tline->type == TOK_STRING)
2660 nasm_unquote_cstr(tline->text, i);
2661 use_pkg = nasm_stdmac_find_package(tline->text);
2662 if (!use_pkg)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002663 nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002664 else
2665 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002666 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002667 /* Not already included, go ahead and include it */
2668 stdmacpos = use_pkg;
2669 }
2670 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002671 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002672 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002673 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002674 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002675 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002676 tline = tline->next;
2677 skip_white_(tline);
2678 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002679 if (tline) {
2680 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002681 nasm_error(ERR_NONFATAL, "`%s' expects a context identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002682 pp_directives[i]);
2683 free_tlist(origline);
2684 return DIRECTIVE_FOUND; /* but we did _something_ */
2685 }
2686 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002687 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002688 "trailing garbage after `%s' ignored",
2689 pp_directives[i]);
2690 p = nasm_strdup(tline->text);
2691 } else {
2692 p = NULL; /* Anonymous */
2693 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002694
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002695 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002696 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002697 ctx->next = cstk;
2698 hash_init(&ctx->localmac, HASH_SMALL);
2699 ctx->name = p;
2700 ctx->number = unique++;
2701 cstk = ctx;
2702 } else {
2703 /* %pop or %repl */
2704 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002705 nasm_error(ERR_NONFATAL, "`%s': context stack is empty",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002706 pp_directives[i]);
2707 } else if (i == PP_POP) {
2708 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08002709 nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002710 "expected %s",
2711 cstk->name ? cstk->name : "anonymous", p);
2712 else
2713 ctx_pop();
2714 } else {
2715 /* i == PP_REPL */
2716 nasm_free(cstk->name);
2717 cstk->name = p;
2718 p = NULL;
2719 }
2720 nasm_free(p);
2721 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002722 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002723 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002724 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002725 severity = ERR_FATAL;
2726 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002727 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002728 severity = ERR_NONFATAL;
2729 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002730 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002731 severity = ERR_WARNING|ERR_WARN_USER;
2732 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002733
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002734issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002735 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002736 /* Only error out if this is the final pass */
2737 if (pass != 2 && i != PP_FATAL)
2738 return DIRECTIVE_FOUND;
2739
2740 tline->next = expand_smacro(tline->next);
2741 tline = tline->next;
2742 skip_white_(tline);
2743 t = tline ? tline->next : NULL;
2744 skip_white_(t);
2745 if (tok_type_(tline, TOK_STRING) && !t) {
2746 /* The line contains only a quoted string */
2747 p = tline->text;
2748 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002749 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002750 } else {
2751 /* Not a quoted string, or more than a quoted string */
2752 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002753 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002754 nasm_free(p);
2755 }
2756 free_tlist(origline);
2757 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002758 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002759
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002760 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002761 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002762 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002763 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002764 j = if_condition(tline->next, i);
2765 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002766 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002767 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002768 cond = nasm_malloc(sizeof(Cond));
2769 cond->next = istk->conds;
2770 cond->state = j;
2771 istk->conds = cond;
2772 if(istk->mstk)
2773 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002774 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002775 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002776
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002777 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002778 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002779 nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002780 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002781 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002782 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002783 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002784
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002785 case COND_DONE:
2786 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002787 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002788
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002789 case COND_ELSE_TRUE:
2790 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002791 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2792 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002793 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002794 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002795
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002796 case COND_IF_FALSE:
2797 /*
2798 * IMPORTANT: In the case of %if, we will already have
2799 * called expand_mmac_params(); however, if we're
2800 * processing an %elif we must have been in a
2801 * non-emitting mode, which would have inhibited
2802 * the normal invocation of expand_mmac_params().
2803 * Therefore, we have to do it explicitly here.
2804 */
2805 j = if_condition(expand_mmac_params(tline->next), i);
2806 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002807 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002808 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2809 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002810 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002811 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002812 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002813
H. Peter Anvine2c80182005-01-15 22:15:51 +00002814 case PP_ELSE:
2815 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002816 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2817 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002818 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002819 nasm_fatal(0, "`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002820 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002821 case COND_IF_TRUE:
2822 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002823 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002824 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002825
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002826 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002827 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002828
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002829 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002830 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002831 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002832
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002833 case COND_ELSE_TRUE:
2834 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002835 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002836 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002837 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002838 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002839 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002840 free_tlist(origline);
2841 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002842
H. Peter Anvine2c80182005-01-15 22:15:51 +00002843 case PP_ENDIF:
2844 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002845 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2846 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002847 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002848 nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002849 cond = istk->conds;
2850 istk->conds = cond->next;
2851 nasm_free(cond);
2852 if(istk->mstk)
2853 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002854 free_tlist(origline);
2855 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002856
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002857 case PP_RMACRO:
2858 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002859 case PP_MACRO:
2860 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002861 if (defining) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002862 nasm_error(ERR_FATAL, "`%s': already defining a macro",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002863 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002864 return DIRECTIVE_FOUND;
2865 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002866 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002867 defining->max_depth =
2868 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2869 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2870 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2871 nasm_free(defining);
2872 defining = NULL;
2873 return DIRECTIVE_FOUND;
2874 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002875
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002876 src_get(&defining->xline, &defining->fname);
2877
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002878 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2879 while (mmac) {
2880 if (!strcmp(mmac->name, defining->name) &&
2881 (mmac->nparam_min <= defining->nparam_max
2882 || defining->plus)
2883 && (defining->nparam_min <= mmac->nparam_max
2884 || mmac->plus)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002885 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002886 "redefining multi-line macro `%s'", defining->name);
2887 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002888 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002889 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002890 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002891 free_tlist(origline);
2892 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002893
H. Peter Anvine2c80182005-01-15 22:15:51 +00002894 case PP_ENDM:
2895 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002896 if (! (defining && defining->name)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002897 nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002898 return DIRECTIVE_FOUND;
2899 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002900 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2901 defining->next = *mmhead;
2902 *mmhead = defining;
2903 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002904 free_tlist(origline);
2905 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002906
H. Peter Anvin89cee572009-07-15 09:16:54 -04002907 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002908 /*
2909 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002910 * macro-end marker for a macro with a name. Then we
2911 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002912 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002913 list_for_each(l, istk->expansion)
2914 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002915 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002916
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002917 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002918 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002919 * Remove all conditional entries relative to this
2920 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002921 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002922 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2923 cond = istk->conds;
2924 istk->conds = cond->next;
2925 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002926 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002927 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002928 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002929 nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002930 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002931 free_tlist(origline);
2932 return DIRECTIVE_FOUND;
2933
H. Peter Anvina26433d2008-07-16 14:40:01 -07002934 case PP_UNMACRO:
2935 case PP_UNIMACRO:
2936 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002937 MMacro **mmac_p;
2938 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002939
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002940 spec.casesense = (i == PP_UNMACRO);
2941 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2942 return DIRECTIVE_FOUND;
2943 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002944 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2945 while (mmac_p && *mmac_p) {
2946 mmac = *mmac_p;
2947 if (mmac->casesense == spec.casesense &&
2948 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2949 mmac->nparam_min == spec.nparam_min &&
2950 mmac->nparam_max == spec.nparam_max &&
2951 mmac->plus == spec.plus) {
2952 *mmac_p = mmac->next;
2953 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002954 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002955 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002956 }
2957 }
2958 free_tlist(origline);
2959 free_tlist(spec.dlist);
2960 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002961 }
2962
H. Peter Anvine2c80182005-01-15 22:15:51 +00002963 case PP_ROTATE:
2964 if (tline->next && tline->next->type == TOK_WHITESPACE)
2965 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002966 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002967 free_tlist(origline);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002968 nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002969 return DIRECTIVE_FOUND;
2970 }
2971 t = expand_smacro(tline->next);
2972 tline->next = NULL;
2973 free_tlist(origline);
2974 tline = t;
2975 tptr = &t;
2976 tokval.t_type = TOKEN_INVALID;
2977 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002978 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002979 free_tlist(tline);
2980 if (!evalresult)
2981 return DIRECTIVE_FOUND;
2982 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002983 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002984 "trailing garbage after expression ignored");
2985 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002986 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002987 return DIRECTIVE_FOUND;
2988 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002989 mmac = istk->mstk;
2990 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2991 mmac = mmac->next_active;
2992 if (!mmac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002993 nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002994 } else if (mmac->nparam == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002995 nasm_error(ERR_NONFATAL,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002996 "`%%rotate' invoked within macro without parameters");
2997 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002998 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002999
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003000 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003001 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003002 rotate += mmac->nparam;
3003
3004 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003005 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003006 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003007
H. Peter Anvine2c80182005-01-15 22:15:51 +00003008 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003009 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003010 do {
3011 tline = tline->next;
3012 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003013
H. Peter Anvine2c80182005-01-15 22:15:51 +00003014 if (tok_type_(tline, TOK_ID) &&
3015 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003016 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003017 do {
3018 tline = tline->next;
3019 } while (tok_type_(tline, TOK_WHITESPACE));
3020 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003021
H. Peter Anvine2c80182005-01-15 22:15:51 +00003022 if (tline) {
3023 t = expand_smacro(tline);
3024 tptr = &t;
3025 tokval.t_type = TOKEN_INVALID;
3026 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08003027 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003028 if (!evalresult) {
3029 free_tlist(origline);
3030 return DIRECTIVE_FOUND;
3031 }
3032 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003033 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003034 "trailing garbage after expression ignored");
3035 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003036 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003037 return DIRECTIVE_FOUND;
3038 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003039 count = reloc_value(evalresult);
3040 if (count >= REP_LIMIT) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003041 nasm_error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003042 count = 0;
3043 } else
3044 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003045 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003046 nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003047 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003048 }
3049 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003050
3051 tmp_defining = defining;
3052 defining = nasm_malloc(sizeof(MMacro));
3053 defining->prev = NULL;
3054 defining->name = NULL; /* flags this macro as a %rep block */
3055 defining->casesense = false;
3056 defining->plus = false;
3057 defining->nolist = nolist;
3058 defining->in_progress = count;
3059 defining->max_depth = 0;
3060 defining->nparam_min = defining->nparam_max = 0;
3061 defining->defaults = NULL;
3062 defining->dlist = NULL;
3063 defining->expansion = NULL;
3064 defining->next_active = istk->mstk;
3065 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003066 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003067
H. Peter Anvine2c80182005-01-15 22:15:51 +00003068 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003069 if (!defining || defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003070 nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003071 return DIRECTIVE_FOUND;
3072 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003073
H. Peter Anvine2c80182005-01-15 22:15:51 +00003074 /*
3075 * Now we have a "macro" defined - although it has no name
3076 * and we won't be entering it in the hash tables - we must
3077 * push a macro-end marker for it on to istk->expansion.
3078 * After that, it will take care of propagating itself (a
3079 * macro-end marker line for a macro which is really a %rep
3080 * block will cause the macro to be re-expanded, complete
3081 * with another macro-end marker to ensure the process
3082 * continues) until the whole expansion is forcibly removed
3083 * from istk->expansion by a %exitrep.
3084 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003085 l = nasm_malloc(sizeof(Line));
3086 l->next = istk->expansion;
3087 l->finishes = defining;
3088 l->first = NULL;
3089 istk->expansion = l;
3090
3091 istk->mstk = defining;
3092
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08003093 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003094 tmp_defining = defining;
3095 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003096 free_tlist(origline);
3097 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003098
H. Peter Anvine2c80182005-01-15 22:15:51 +00003099 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003100 /*
3101 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003102 * macro-end marker for a macro with no name. Then we set
3103 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003104 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003105 list_for_each(l, istk->expansion)
3106 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003107 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003108
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003109 if (l)
3110 l->finishes->in_progress = 1;
3111 else
H. Peter Anvin130736c2016-02-17 20:27:41 -08003112 nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003113 free_tlist(origline);
3114 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003115
H. Peter Anvine2c80182005-01-15 22:15:51 +00003116 case PP_XDEFINE:
3117 case PP_IXDEFINE:
3118 case PP_DEFINE:
3119 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003120 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003121
H. Peter Anvine2c80182005-01-15 22:15:51 +00003122 tline = tline->next;
3123 skip_white_(tline);
3124 tline = expand_id(tline);
3125 if (!tline || (tline->type != TOK_ID &&
3126 (tline->type != TOK_PREPROC_ID ||
3127 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003128 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003129 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003130 free_tlist(origline);
3131 return DIRECTIVE_FOUND;
3132 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003133
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003134 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003135 last = tline;
3136 param_start = tline = tline->next;
3137 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003138
H. Peter Anvine2c80182005-01-15 22:15:51 +00003139 /* Expand the macro definition now for %xdefine and %ixdefine */
3140 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3141 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003142
H. Peter Anvine2c80182005-01-15 22:15:51 +00003143 if (tok_is_(tline, "(")) {
3144 /*
3145 * This macro has parameters.
3146 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003147
H. Peter Anvine2c80182005-01-15 22:15:51 +00003148 tline = tline->next;
3149 while (1) {
3150 skip_white_(tline);
3151 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003152 nasm_error(ERR_NONFATAL, "parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003153 free_tlist(origline);
3154 return DIRECTIVE_FOUND;
3155 }
3156 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003157 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003158 "`%s': parameter identifier expected",
3159 tline->text);
3160 free_tlist(origline);
3161 return DIRECTIVE_FOUND;
3162 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003163 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003164 tline = tline->next;
3165 skip_white_(tline);
3166 if (tok_is_(tline, ",")) {
3167 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003168 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003169 if (!tok_is_(tline, ")")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003170 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003171 "`)' expected to terminate macro template");
3172 free_tlist(origline);
3173 return DIRECTIVE_FOUND;
3174 }
3175 break;
3176 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003177 }
3178 last = tline;
3179 tline = tline->next;
3180 }
3181 if (tok_type_(tline, TOK_WHITESPACE))
3182 last = tline, tline = tline->next;
3183 macro_start = NULL;
3184 last->next = NULL;
3185 t = tline;
3186 while (t) {
3187 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003188 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003189 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003190 !strcmp(tt->text, t->text))
3191 t->type = tt->type;
3192 }
3193 tt = t->next;
3194 t->next = macro_start;
3195 macro_start = t;
3196 t = tt;
3197 }
3198 /*
3199 * Good. We now have a macro name, a parameter count, and a
3200 * token list (in reverse order) for an expansion. We ought
3201 * to be OK just to create an SMacro, store it, and let
3202 * free_tlist have the rest of the line (which we have
3203 * carefully re-terminated after chopping off the expansion
3204 * from the end).
3205 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003206 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003207 free_tlist(origline);
3208 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003209
H. Peter Anvine2c80182005-01-15 22:15:51 +00003210 case PP_UNDEF:
3211 tline = tline->next;
3212 skip_white_(tline);
3213 tline = expand_id(tline);
3214 if (!tline || (tline->type != TOK_ID &&
3215 (tline->type != TOK_PREPROC_ID ||
3216 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003217 nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003218 free_tlist(origline);
3219 return DIRECTIVE_FOUND;
3220 }
3221 if (tline->next) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003222 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003223 "trailing garbage after macro name ignored");
3224 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003225
H. Peter Anvine2c80182005-01-15 22:15:51 +00003226 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003227 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003228 undef_smacro(ctx, mname);
3229 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003230 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003231
H. Peter Anvin9e200162008-06-04 17:23:14 -07003232 case PP_DEFSTR:
3233 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003234 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003235
3236 tline = tline->next;
3237 skip_white_(tline);
3238 tline = expand_id(tline);
3239 if (!tline || (tline->type != TOK_ID &&
3240 (tline->type != TOK_PREPROC_ID ||
3241 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003242 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003243 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003244 free_tlist(origline);
3245 return DIRECTIVE_FOUND;
3246 }
3247
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003248 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003249 last = tline;
3250 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003251 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003252
3253 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003254 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003255
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003256 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003257 macro_start = nasm_malloc(sizeof(*macro_start));
3258 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003259 macro_start->text = nasm_quote(p, strlen(p));
3260 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003261 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003262 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003263
3264 /*
3265 * We now have a macro name, an implicit parameter count of
3266 * zero, and a string token to use as an expansion. Create
3267 * and store an SMacro.
3268 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003269 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003270 free_tlist(origline);
3271 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003272
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003273 case PP_DEFTOK:
3274 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003275 casesense = (i == PP_DEFTOK);
3276
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003277 tline = tline->next;
3278 skip_white_(tline);
3279 tline = expand_id(tline);
3280 if (!tline || (tline->type != TOK_ID &&
3281 (tline->type != TOK_PREPROC_ID ||
3282 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003283 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003284 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003285 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003286 free_tlist(origline);
3287 return DIRECTIVE_FOUND;
3288 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003289 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003290 last = tline;
3291 tline = expand_smacro(tline->next);
3292 last->next = NULL;
3293
3294 t = tline;
3295 while (tok_type_(t, TOK_WHITESPACE))
3296 t = t->next;
3297 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003298 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003299 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003300 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003301 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003302 free_tlist(tline);
3303 free_tlist(origline);
3304 return DIRECTIVE_FOUND;
3305 }
3306
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003307 /*
3308 * Convert the string to a token stream. Note that smacros
3309 * are stored with the token stream reversed, so we have to
3310 * reverse the output of tokenize().
3311 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003312 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003313 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003314
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003315 /*
3316 * We now have a macro name, an implicit parameter count of
3317 * zero, and a numeric token to use as an expansion. Create
3318 * and store an SMacro.
3319 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003320 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003321 free_tlist(tline);
3322 free_tlist(origline);
3323 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003324
H. Peter Anvin418ca702008-05-30 10:42:30 -07003325 case PP_PATHSEARCH:
3326 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003327 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003328
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003329 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003330
3331 tline = tline->next;
3332 skip_white_(tline);
3333 tline = expand_id(tline);
3334 if (!tline || (tline->type != TOK_ID &&
3335 (tline->type != TOK_PREPROC_ID ||
3336 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003337 nasm_error(ERR_NONFATAL,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003338 "`%%pathsearch' expects a macro identifier as first parameter");
3339 free_tlist(origline);
3340 return DIRECTIVE_FOUND;
3341 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003342 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003343 last = tline;
3344 tline = expand_smacro(tline->next);
3345 last->next = NULL;
3346
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003347 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003348 while (tok_type_(t, TOK_WHITESPACE))
3349 t = t->next;
3350
3351 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003352 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003353 nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003354 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003355 free_tlist(origline);
3356 return DIRECTIVE_FOUND; /* but we did _something_ */
3357 }
3358 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003359 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003360 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003361 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003362 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003363 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003364
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003365 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003366 if (!found_path)
3367 found_path = p;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003368 macro_start = nasm_malloc(sizeof(*macro_start));
3369 macro_start->next = NULL;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003370 macro_start->text = nasm_quote(found_path, strlen(found_path));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003371 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003372 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003373
3374 /*
3375 * We now have a macro name, an implicit parameter count of
3376 * zero, and a string token to use as an expansion. Create
3377 * and store an SMacro.
3378 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003379 define_smacro(ctx, mname, casesense, 0, macro_start);
3380 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003381 free_tlist(origline);
3382 return DIRECTIVE_FOUND;
3383 }
3384
H. Peter Anvine2c80182005-01-15 22:15:51 +00003385 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003386 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003387
H. Peter Anvine2c80182005-01-15 22:15:51 +00003388 tline = tline->next;
3389 skip_white_(tline);
3390 tline = expand_id(tline);
3391 if (!tline || (tline->type != TOK_ID &&
3392 (tline->type != TOK_PREPROC_ID ||
3393 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003394 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003395 "`%%strlen' expects a macro identifier as first parameter");
3396 free_tlist(origline);
3397 return DIRECTIVE_FOUND;
3398 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003399 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003400 last = tline;
3401 tline = expand_smacro(tline->next);
3402 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003403
H. Peter Anvine2c80182005-01-15 22:15:51 +00003404 t = tline;
3405 while (tok_type_(t, TOK_WHITESPACE))
3406 t = t->next;
3407 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003408 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003409 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003410 "`%%strlen` requires string as second parameter");
3411 free_tlist(tline);
3412 free_tlist(origline);
3413 return DIRECTIVE_FOUND;
3414 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003415
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003416 macro_start = nasm_malloc(sizeof(*macro_start));
3417 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003418 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003419 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003420
H. Peter Anvine2c80182005-01-15 22:15:51 +00003421 /*
3422 * We now have a macro name, an implicit parameter count of
3423 * zero, and a numeric token to use as an expansion. Create
3424 * and store an SMacro.
3425 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003426 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003427 free_tlist(tline);
3428 free_tlist(origline);
3429 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003430
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003431 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003432 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003433
3434 tline = tline->next;
3435 skip_white_(tline);
3436 tline = expand_id(tline);
3437 if (!tline || (tline->type != TOK_ID &&
3438 (tline->type != TOK_PREPROC_ID ||
3439 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003440 nasm_error(ERR_NONFATAL,
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003441 "`%%strcat' expects a macro identifier as first parameter");
3442 free_tlist(origline);
3443 return DIRECTIVE_FOUND;
3444 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003445 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003446 last = tline;
3447 tline = expand_smacro(tline->next);
3448 last->next = NULL;
3449
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003450 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003451 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003452 switch (t->type) {
3453 case TOK_WHITESPACE:
3454 break;
3455 case TOK_STRING:
3456 len += t->a.len = nasm_unquote(t->text, NULL);
3457 break;
3458 case TOK_OTHER:
3459 if (!strcmp(t->text, ",")) /* permit comma separators */
3460 break;
3461 /* else fall through */
3462 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003463 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003464 "non-string passed to `%%strcat' (%d)", t->type);
3465 free_tlist(tline);
3466 free_tlist(origline);
3467 return DIRECTIVE_FOUND;
3468 }
3469 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003470
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003471 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003472 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003473 if (t->type == TOK_STRING) {
3474 memcpy(p, t->text, t->a.len);
3475 p += t->a.len;
3476 }
3477 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003478
3479 /*
3480 * We now have a macro name, an implicit parameter count of
3481 * zero, and a numeric token to use as an expansion. Create
3482 * and store an SMacro.
3483 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003484 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3485 macro_start->text = nasm_quote(pp, len);
3486 nasm_free(pp);
3487 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003488 free_tlist(tline);
3489 free_tlist(origline);
3490 return DIRECTIVE_FOUND;
3491
H. Peter Anvine2c80182005-01-15 22:15:51 +00003492 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003493 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003494 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003495 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003496
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003497 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003498
H. Peter Anvine2c80182005-01-15 22:15:51 +00003499 tline = tline->next;
3500 skip_white_(tline);
3501 tline = expand_id(tline);
3502 if (!tline || (tline->type != TOK_ID &&
3503 (tline->type != TOK_PREPROC_ID ||
3504 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003505 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003506 "`%%substr' expects a macro identifier as first parameter");
3507 free_tlist(origline);
3508 return DIRECTIVE_FOUND;
3509 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003510 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003511 last = tline;
3512 tline = expand_smacro(tline->next);
3513 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003514
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003515 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003516 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003517 while (tok_type_(t, TOK_WHITESPACE))
3518 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003519
H. Peter Anvine2c80182005-01-15 22:15:51 +00003520 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003521 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003522 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003523 "`%%substr` requires string as second parameter");
3524 free_tlist(tline);
3525 free_tlist(origline);
3526 return DIRECTIVE_FOUND;
3527 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003528
H. Peter Anvine2c80182005-01-15 22:15:51 +00003529 tt = t->next;
3530 tptr = &tt;
3531 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003532 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003533 if (!evalresult) {
3534 free_tlist(tline);
3535 free_tlist(origline);
3536 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003537 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003538 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003539 free_tlist(tline);
3540 free_tlist(origline);
3541 return DIRECTIVE_FOUND;
3542 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003543 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003544
3545 while (tok_type_(tt, TOK_WHITESPACE))
3546 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003547 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003548 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003549 } else {
3550 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003551 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003552 if (!evalresult) {
3553 free_tlist(tline);
3554 free_tlist(origline);
3555 return DIRECTIVE_FOUND;
3556 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003557 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003558 free_tlist(tline);
3559 free_tlist(origline);
3560 return DIRECTIVE_FOUND;
3561 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003562 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003563 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003564
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003565 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003566
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003567 /* make start and count being in range */
3568 if (start < 0)
3569 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003570 if (count < 0)
3571 count = len + count + 1 - start;
3572 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003573 count = len - start;
3574 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003575 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003576
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003577 macro_start = nasm_malloc(sizeof(*macro_start));
3578 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003579 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003580 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003581 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003582
H. Peter Anvine2c80182005-01-15 22:15:51 +00003583 /*
3584 * We now have a macro name, an implicit parameter count of
3585 * zero, and a numeric token to use as an expansion. Create
3586 * and store an SMacro.
3587 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003588 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003589 free_tlist(tline);
3590 free_tlist(origline);
3591 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003592 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003593
H. Peter Anvine2c80182005-01-15 22:15:51 +00003594 case PP_ASSIGN:
3595 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003596 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003597
H. Peter Anvine2c80182005-01-15 22:15:51 +00003598 tline = tline->next;
3599 skip_white_(tline);
3600 tline = expand_id(tline);
3601 if (!tline || (tline->type != TOK_ID &&
3602 (tline->type != TOK_PREPROC_ID ||
3603 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003604 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003605 "`%%%sassign' expects a macro identifier",
3606 (i == PP_IASSIGN ? "i" : ""));
3607 free_tlist(origline);
3608 return DIRECTIVE_FOUND;
3609 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003610 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003611 last = tline;
3612 tline = expand_smacro(tline->next);
3613 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003614
H. Peter Anvine2c80182005-01-15 22:15:51 +00003615 t = tline;
3616 tptr = &t;
3617 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003618 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003619 free_tlist(tline);
3620 if (!evalresult) {
3621 free_tlist(origline);
3622 return DIRECTIVE_FOUND;
3623 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003624
H. Peter Anvine2c80182005-01-15 22:15:51 +00003625 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003626 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003627 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003628
H. Peter Anvine2c80182005-01-15 22:15:51 +00003629 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003630 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003631 "non-constant value given to `%%%sassign'",
3632 (i == PP_IASSIGN ? "i" : ""));
3633 free_tlist(origline);
3634 return DIRECTIVE_FOUND;
3635 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003636
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003637 macro_start = nasm_malloc(sizeof(*macro_start));
3638 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003639 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003640 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003641
H. Peter Anvine2c80182005-01-15 22:15:51 +00003642 /*
3643 * We now have a macro name, an implicit parameter count of
3644 * zero, and a numeric token to use as an expansion. Create
3645 * and store an SMacro.
3646 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003647 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003648 free_tlist(origline);
3649 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003650
H. Peter Anvine2c80182005-01-15 22:15:51 +00003651 case PP_LINE:
3652 /*
3653 * Syntax is `%line nnn[+mmm] [filename]'
3654 */
3655 tline = tline->next;
3656 skip_white_(tline);
3657 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003658 nasm_error(ERR_NONFATAL, "`%%line' expects line number");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003659 free_tlist(origline);
3660 return DIRECTIVE_FOUND;
3661 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003662 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003663 m = 1;
3664 tline = tline->next;
3665 if (tok_is_(tline, "+")) {
3666 tline = tline->next;
3667 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003668 nasm_error(ERR_NONFATAL, "`%%line' expects line increment");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003669 free_tlist(origline);
3670 return DIRECTIVE_FOUND;
3671 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003672 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003673 tline = tline->next;
3674 }
3675 skip_white_(tline);
3676 src_set_linnum(k);
3677 istk->lineinc = m;
3678 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003679 char *fname = detoken(tline, false);
3680 src_set_fname(fname);
3681 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003682 }
3683 free_tlist(origline);
3684 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003685
H. Peter Anvine2c80182005-01-15 22:15:51 +00003686 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003687 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003688 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003689 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003690 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003691 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003692}
3693
3694/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003695 * Ensure that a macro parameter contains a condition code and
3696 * nothing else. Return the condition code index if so, or -1
3697 * otherwise.
3698 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003699static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003700{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003701 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003702
H. Peter Anvin25a99342007-09-22 17:45:45 -07003703 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003704 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003705
H. Peter Anvineba20a72002-04-30 20:53:55 +00003706 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003707 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003708 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003709 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003710 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003711 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003712 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003713
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003714 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003715}
3716
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003717/*
3718 * This routines walks over tokens strem and hadnles tokens
3719 * pasting, if @handle_explicit passed then explicit pasting
3720 * term is handled, otherwise -- implicit pastings only.
3721 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003722static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003723 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003724{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003725 Token *tok, *next, **prev_next, **prev_nonspace;
3726 bool pasted = false;
3727 char *buf, *p;
3728 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003729
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003730 /*
3731 * The last token before pasting. We need it
3732 * to be able to connect new handled tokens.
3733 * In other words if there were a tokens stream
3734 *
3735 * A -> B -> C -> D
3736 *
3737 * and we've joined tokens B and C, the resulting
3738 * stream should be
3739 *
3740 * A -> BC -> D
3741 */
3742 tok = *head;
3743 prev_next = NULL;
3744
3745 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3746 prev_nonspace = head;
3747 else
3748 prev_nonspace = NULL;
3749
3750 while (tok && (next = tok->next)) {
3751
3752 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003753 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003754 /* Zap redundant whitespaces */
3755 while (tok_type_(next, TOK_WHITESPACE))
3756 next = delete_Token(next);
3757 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003758 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003759
3760 case TOK_PASTE:
3761 /* Explicit pasting */
3762 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003763 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003764 next = delete_Token(tok);
3765
3766 while (tok_type_(next, TOK_WHITESPACE))
3767 next = delete_Token(next);
3768
3769 if (!pasted)
3770 pasted = true;
3771
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003772 /* Left pasting token is start of line */
3773 if (!prev_nonspace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003774 nasm_error(ERR_FATAL, "No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003775
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003776 /*
3777 * No ending token, this might happen in two
3778 * cases
3779 *
3780 * 1) There indeed no right token at all
3781 * 2) There is a bare "%define ID" statement,
3782 * and @ID does expand to whitespace.
3783 *
3784 * So technically we need to do a grammar analysis
3785 * in another stage of parsing, but for now lets don't
3786 * change the behaviour people used to. Simply allow
3787 * whitespace after paste token.
3788 */
3789 if (!next) {
3790 /*
3791 * Zap ending space tokens and that's all.
3792 */
3793 tok = (*prev_nonspace)->next;
3794 while (tok_type_(tok, TOK_WHITESPACE))
3795 tok = delete_Token(tok);
3796 tok = *prev_nonspace;
3797 tok->next = NULL;
3798 break;
3799 }
3800
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003801 tok = *prev_nonspace;
3802 while (tok_type_(tok, TOK_WHITESPACE))
3803 tok = delete_Token(tok);
3804 len = strlen(tok->text);
3805 len += strlen(next->text);
3806
3807 p = buf = nasm_malloc(len + 1);
3808 strcpy(p, tok->text);
3809 p = strchr(p, '\0');
3810 strcpy(p, next->text);
3811
3812 delete_Token(tok);
3813
3814 tok = tokenize(buf);
3815 nasm_free(buf);
3816
3817 *prev_nonspace = tok;
3818 while (tok && tok->next)
3819 tok = tok->next;
3820
3821 tok->next = delete_Token(next);
3822
3823 /* Restart from pasted tokens head */
3824 tok = *prev_nonspace;
3825 break;
3826
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003827 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003828 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003829 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003830 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3831 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003832
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003833 len = 0;
3834 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3835 len += strlen(next->text);
3836 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003837 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003838
3839 /* No match */
3840 if (tok == next)
3841 break;
3842
3843 len += strlen(tok->text);
3844 p = buf = nasm_malloc(len + 1);
3845
3846 while (tok != next) {
3847 strcpy(p, tok->text);
3848 p = strchr(p, '\0');
3849 tok = delete_Token(tok);
3850 }
3851
3852 tok = tokenize(buf);
3853 nasm_free(buf);
3854
3855 if (prev_next)
3856 *prev_next = tok;
3857 else
3858 *head = tok;
3859
3860 /*
3861 * Connect pasted into original stream,
3862 * ie A -> new-tokens -> B
3863 */
3864 while (tok && tok->next)
3865 tok = tok->next;
3866 tok->next = next;
3867
3868 if (!pasted)
3869 pasted = true;
3870
3871 /* Restart from pasted tokens head */
3872 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003873 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003874
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003875 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003876 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003877
3878 prev_next = &tok->next;
3879
3880 if (tok->next &&
3881 !tok_type_(tok->next, TOK_WHITESPACE) &&
3882 !tok_type_(tok->next, TOK_PASTE))
3883 prev_nonspace = prev_next;
3884
3885 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003886 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003887
3888 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003889}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003890
3891/*
3892 * expands to a list of tokens from %{x:y}
3893 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003894static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003895{
3896 Token *t = tline, **tt, *tm, *head;
3897 char *pos;
3898 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003899
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003900 pos = strchr(tline->text, ':');
3901 nasm_assert(pos);
3902
3903 lst = atoi(pos + 1);
3904 fst = atoi(tline->text + 1);
3905
3906 /*
3907 * only macros params are accounted so
3908 * if someone passes %0 -- we reject such
3909 * value(s)
3910 */
3911 if (lst == 0 || fst == 0)
3912 goto err;
3913
3914 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003915 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3916 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003917 goto err;
3918
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003919 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3920 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003921
3922 /* counted from zero */
3923 fst--, lst--;
3924
3925 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003926 * It will be at least one token. Note we
3927 * need to scan params until separator, otherwise
3928 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003929 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003930 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003931 head = new_Token(NULL, tm->type, tm->text, 0);
3932 tt = &head->next, tm = tm->next;
3933 while (tok_isnt_(tm, ",")) {
3934 t = new_Token(NULL, tm->type, tm->text, 0);
3935 *tt = t, tt = &t->next, tm = tm->next;
3936 }
3937
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003938 if (fst < lst) {
3939 for (i = fst + 1; i <= lst; i++) {
3940 t = new_Token(NULL, TOK_OTHER, ",", 0);
3941 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003942 j = (i + mac->rotate) % mac->nparam;
3943 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003944 while (tok_isnt_(tm, ",")) {
3945 t = new_Token(NULL, tm->type, tm->text, 0);
3946 *tt = t, tt = &t->next, tm = tm->next;
3947 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003948 }
3949 } else {
3950 for (i = fst - 1; i >= lst; i--) {
3951 t = new_Token(NULL, TOK_OTHER, ",", 0);
3952 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003953 j = (i + mac->rotate) % mac->nparam;
3954 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003955 while (tok_isnt_(tm, ",")) {
3956 t = new_Token(NULL, tm->type, tm->text, 0);
3957 *tt = t, tt = &t->next, tm = tm->next;
3958 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003959 }
3960 }
3961
3962 *last = tt;
3963 return head;
3964
3965err:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003966 nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003967 &tline->text[1]);
3968 return tline;
3969}
3970
H. Peter Anvin76690a12002-04-30 20:52:49 +00003971/*
3972 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003973 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003974 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003975 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003976static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003977{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003978 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003979 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003980 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003981
3982 tail = &thead;
3983 thead = NULL;
3984
H. Peter Anvine2c80182005-01-15 22:15:51 +00003985 while (tline) {
3986 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003987 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3988 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3989 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003990 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003991 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003992 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003993 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003994 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003995 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003996
H. Peter Anvine2c80182005-01-15 22:15:51 +00003997 t = tline;
3998 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003999
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004000 mac = istk->mstk;
4001 while (mac && !mac->name) /* avoid mistaking %reps for macros */
4002 mac = mac->next_active;
4003 if (!mac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004004 nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004005 } else {
4006 pos = strchr(t->text, ':');
4007 if (!pos) {
4008 switch (t->text[1]) {
4009 /*
4010 * We have to make a substitution of one of the
4011 * forms %1, %-1, %+1, %%foo, %0.
4012 */
4013 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004014 type = TOK_NUMBER;
4015 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
4016 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004017 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004018 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004019 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004020 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004021 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004022 text = nasm_strcat(tmpbuf, t->text + 2);
4023 break;
4024 case '-':
4025 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004026 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004027 tt = NULL;
4028 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004029 if (mac->nparam > 1)
4030 n = (n + mac->rotate) % mac->nparam;
4031 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004032 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004033 cc = find_cc(tt);
4034 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004035 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004036 "macro parameter %d is not a condition code",
4037 n + 1);
4038 text = NULL;
4039 } else {
4040 type = TOK_ID;
4041 if (inverse_ccs[cc] == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004042 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004043 "condition code `%s' is not invertible",
4044 conditions[cc]);
4045 text = NULL;
4046 } else
4047 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4048 }
4049 break;
4050 case '+':
4051 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004052 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004053 tt = NULL;
4054 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004055 if (mac->nparam > 1)
4056 n = (n + mac->rotate) % mac->nparam;
4057 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004058 }
4059 cc = find_cc(tt);
4060 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004061 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004062 "macro parameter %d is not a condition code",
4063 n + 1);
4064 text = NULL;
4065 } else {
4066 type = TOK_ID;
4067 text = nasm_strdup(conditions[cc]);
4068 }
4069 break;
4070 default:
4071 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004072 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004073 tt = NULL;
4074 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004075 if (mac->nparam > 1)
4076 n = (n + mac->rotate) % mac->nparam;
4077 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004078 }
4079 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004080 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004081 *tail = new_Token(NULL, tt->type, tt->text, 0);
4082 tail = &(*tail)->next;
4083 tt = tt->next;
4084 }
4085 }
4086 text = NULL; /* we've done it here */
4087 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004088 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004089 } else {
4090 /*
4091 * seems we have a parameters range here
4092 */
4093 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004094 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004095 if (head != t) {
4096 *tail = head;
4097 *last = tline;
4098 tline = head;
4099 text = NULL;
4100 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004101 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004102 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004103 if (!text) {
4104 delete_Token(t);
4105 } else {
4106 *tail = t;
4107 tail = &t->next;
4108 t->type = type;
4109 nasm_free(t->text);
4110 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004111 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004112 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004113 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004114 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004115 } else if (tline->type == TOK_INDIRECT) {
4116 t = tline;
4117 tline = tline->next;
4118 tt = tokenize(t->text);
4119 tt = expand_mmac_params(tt);
4120 tt = expand_smacro(tt);
4121 *tail = tt;
4122 while (tt) {
4123 tt->a.mac = NULL; /* Necessary? */
4124 tail = &tt->next;
4125 tt = tt->next;
4126 }
4127 delete_Token(t);
4128 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004129 } else {
4130 t = *tail = tline;
4131 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004132 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004133 tail = &t->next;
4134 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004135 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004136 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004137
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004138 if (changed) {
4139 const struct tokseq_match t[] = {
4140 {
4141 PP_CONCAT_MASK(TOK_ID) |
4142 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4143 PP_CONCAT_MASK(TOK_ID) |
4144 PP_CONCAT_MASK(TOK_NUMBER) |
4145 PP_CONCAT_MASK(TOK_FLOAT) |
4146 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4147 },
4148 {
4149 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4150 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4151 }
4152 };
4153 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4154 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004155
H. Peter Anvin76690a12002-04-30 20:52:49 +00004156 return thead;
4157}
4158
4159/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004160 * Expand all single-line macro calls made in the given line.
4161 * Return the expanded version of the line. The original is deemed
4162 * to be destroyed in the process. (In reality we'll just move
4163 * Tokens from input to output a lot of the time, rather than
4164 * actually bothering to destroy and replicate.)
4165 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004166
H. Peter Anvine2c80182005-01-15 22:15:51 +00004167static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004168{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004169 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004170 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004171 Token **params;
4172 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004173 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004174 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004175 Token *org_tline = tline;
4176 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004177 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004178 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004179 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004180
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004181 /*
4182 * Trick: we should avoid changing the start token pointer since it can
4183 * be contained in "next" field of other token. Because of this
4184 * we allocate a copy of first token and work with it; at the end of
4185 * routine we copy it back
4186 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004187 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004188 tline = new_Token(org_tline->next, org_tline->type,
4189 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004190 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004191 nasm_free(org_tline->text);
4192 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004193 }
4194
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004195 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004196
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004197again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004198 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004199 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004200
H. Peter Anvine2c80182005-01-15 22:15:51 +00004201 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004202 if (!--deadman) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004203 nasm_error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004204 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004205 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004206
H. Peter Anvine2c80182005-01-15 22:15:51 +00004207 if ((mname = tline->text)) {
4208 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004209 if (tline->type == TOK_ID) {
4210 head = (SMacro *)hash_findix(&smacros, mname);
4211 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004212 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004213 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4214 } else
4215 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004216
H. Peter Anvine2c80182005-01-15 22:15:51 +00004217 /*
4218 * We've hit an identifier. As in is_mmacro below, we first
4219 * check whether the identifier is a single-line macro at
4220 * all, then think about checking for parameters if
4221 * necessary.
4222 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004223 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004224 if (!mstrcmp(m->name, mname, m->casesense))
4225 break;
4226 if (m) {
4227 mstart = tline;
4228 params = NULL;
4229 paramsize = NULL;
4230 if (m->nparam == 0) {
4231 /*
4232 * Simple case: the macro is parameterless. Discard the
4233 * one token that the macro call took, and push the
4234 * expansion back on the to-do stack.
4235 */
4236 if (!m->expansion) {
4237 if (!strcmp("__FILE__", m->name)) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004238 const char *file = src_get_fname();
4239 /* nasm_free(tline->text); here? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004240 tline->text = nasm_quote(file, strlen(file));
4241 tline->type = TOK_STRING;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004242 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004243 }
4244 if (!strcmp("__LINE__", m->name)) {
4245 nasm_free(tline->text);
4246 make_tok_num(tline, src_get_linnum());
4247 continue;
4248 }
4249 if (!strcmp("__BITS__", m->name)) {
4250 nasm_free(tline->text);
4251 make_tok_num(tline, globalbits);
4252 continue;
4253 }
4254 tline = delete_Token(tline);
4255 continue;
4256 }
4257 } else {
4258 /*
4259 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004260 * exists and takes parameters. We must find the
4261 * parameters in the call, count them, find the SMacro
4262 * that corresponds to that form of the macro call, and
4263 * substitute for the parameters when we expand. What a
4264 * pain.
4265 */
4266 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004267 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004268 do {
4269 t = tline->next;
4270 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004271 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004272 t->text = NULL;
4273 t = tline->next = delete_Token(t);
4274 }
4275 tline = t;
4276 } while (tok_type_(tline, TOK_WHITESPACE));
4277 if (!tok_is_(tline, "(")) {
4278 /*
4279 * This macro wasn't called with parameters: ignore
4280 * the call. (Behaviour borrowed from gnu cpp.)
4281 */
4282 tline = mstart;
4283 m = NULL;
4284 } else {
4285 int paren = 0;
4286 int white = 0;
4287 brackets = 0;
4288 nparam = 0;
4289 sparam = PARAM_DELTA;
4290 params = nasm_malloc(sparam * sizeof(Token *));
4291 params[0] = tline->next;
4292 paramsize = nasm_malloc(sparam * sizeof(int));
4293 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004294 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004295 /*
4296 * For some unusual expansions
4297 * which concatenates function call
4298 */
4299 t = tline->next;
4300 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004301 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004302 t->text = NULL;
4303 t = tline->next = delete_Token(t);
4304 }
4305 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004306
H. Peter Anvine2c80182005-01-15 22:15:51 +00004307 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004308 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004309 "macro call expects terminating `)'");
4310 break;
4311 }
4312 if (tline->type == TOK_WHITESPACE
4313 && brackets <= 0) {
4314 if (paramsize[nparam])
4315 white++;
4316 else
4317 params[nparam] = tline->next;
4318 continue; /* parameter loop */
4319 }
4320 if (tline->type == TOK_OTHER
4321 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004322 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004323 if (ch == ',' && !paren && brackets <= 0) {
4324 if (++nparam >= sparam) {
4325 sparam += PARAM_DELTA;
4326 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004327 sparam * sizeof(Token *));
4328 paramsize = nasm_realloc(paramsize,
4329 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004330 }
4331 params[nparam] = tline->next;
4332 paramsize[nparam] = 0;
4333 white = 0;
4334 continue; /* parameter loop */
4335 }
4336 if (ch == '{' &&
4337 (brackets > 0 || (brackets == 0 &&
4338 !paramsize[nparam])))
4339 {
4340 if (!(brackets++)) {
4341 params[nparam] = tline->next;
4342 continue; /* parameter loop */
4343 }
4344 }
4345 if (ch == '}' && brackets > 0)
4346 if (--brackets == 0) {
4347 brackets = -1;
4348 continue; /* parameter loop */
4349 }
4350 if (ch == '(' && !brackets)
4351 paren++;
4352 if (ch == ')' && brackets <= 0)
4353 if (--paren < 0)
4354 break;
4355 }
4356 if (brackets < 0) {
4357 brackets = 0;
H. Peter Anvin130736c2016-02-17 20:27:41 -08004358 nasm_error(ERR_NONFATAL, "braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004359 "enclose all of macro parameter");
4360 }
4361 paramsize[nparam] += white + 1;
4362 white = 0;
4363 } /* parameter loop */
4364 nparam++;
4365 while (m && (m->nparam != nparam ||
4366 mstrcmp(m->name, mname,
4367 m->casesense)))
4368 m = m->next;
4369 if (!m)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004370 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004371 "macro `%s' exists, "
4372 "but not taking %d parameters",
4373 mstart->text, nparam);
4374 }
4375 }
4376 if (m && m->in_progress)
4377 m = NULL;
4378 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004379 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004380 * Design question: should we handle !tline, which
4381 * indicates missing ')' here, or expand those
4382 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004383 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004384 */
4385 nasm_free(params);
4386 nasm_free(paramsize);
4387 tline = mstart;
4388 } else {
4389 /*
4390 * Expand the macro: we are placed on the last token of the
4391 * call, so that we can easily split the call from the
4392 * following tokens. We also start by pushing an SMAC_END
4393 * token for the cycle removal.
4394 */
4395 t = tline;
4396 if (t) {
4397 tline = t->next;
4398 t->next = NULL;
4399 }
4400 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004401 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004402 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004403 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004404 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004405 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004406 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004407 Token *ttt, *pt;
4408 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004409
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004410 ttt = params[t->type - TOK_SMAC_PARAM];
4411 i = paramsize[t->type - TOK_SMAC_PARAM];
4412 while (--i >= 0) {
4413 pt = *ptail = new_Token(tline, ttt->type,
4414 ttt->text, 0);
4415 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004416 ttt = ttt->next;
4417 }
4418 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004419 } else if (t->type == TOK_PREPROC_Q) {
4420 tt = new_Token(tline, TOK_ID, mname, 0);
4421 tline = tt;
4422 } else if (t->type == TOK_PREPROC_QQ) {
4423 tt = new_Token(tline, TOK_ID, m->name, 0);
4424 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004425 } else {
4426 tt = new_Token(tline, t->type, t->text, 0);
4427 tline = tt;
4428 }
4429 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004430
H. Peter Anvine2c80182005-01-15 22:15:51 +00004431 /*
4432 * Having done that, get rid of the macro call, and clean
4433 * up the parameters.
4434 */
4435 nasm_free(params);
4436 nasm_free(paramsize);
4437 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004438 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004439 continue; /* main token loop */
4440 }
4441 }
4442 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004443
H. Peter Anvine2c80182005-01-15 22:15:51 +00004444 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004445 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004446 tline = delete_Token(tline);
4447 } else {
4448 t = *tail = tline;
4449 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004450 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004451 t->next = NULL;
4452 tail = &t->next;
4453 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004454 }
4455
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004456 /*
4457 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004458 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004459 * TOK_IDs should be concatenated.
4460 * Also we look for %+ tokens and concatenate the tokens before and after
4461 * them (without white spaces in between).
4462 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004463 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004464 const struct tokseq_match t[] = {
4465 {
4466 PP_CONCAT_MASK(TOK_ID) |
4467 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4468 PP_CONCAT_MASK(TOK_ID) |
4469 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4470 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4471 }
4472 };
4473 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004474 /*
4475 * If we concatenated something, *and* we had previously expanded
4476 * an actual macro, scan the lines again for macros...
4477 */
4478 tline = thead;
4479 expanded = false;
4480 goto again;
4481 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004482 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004483
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004484err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004485 if (org_tline) {
4486 if (thead) {
4487 *org_tline = *thead;
4488 /* since we just gave text to org_line, don't free it */
4489 thead->text = NULL;
4490 delete_Token(thead);
4491 } else {
4492 /* the expression expanded to empty line;
4493 we can't return NULL for some reasons
4494 we just set the line to a single WHITESPACE token. */
4495 memset(org_tline, 0, sizeof(*org_tline));
4496 org_tline->text = NULL;
4497 org_tline->type = TOK_WHITESPACE;
4498 }
4499 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004500 }
4501
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004502 return thead;
4503}
4504
4505/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004506 * Similar to expand_smacro but used exclusively with macro identifiers
4507 * right before they are fetched in. The reason is that there can be
4508 * identifiers consisting of several subparts. We consider that if there
4509 * are more than one element forming the name, user wants a expansion,
4510 * otherwise it will be left as-is. Example:
4511 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004512 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004513 *
4514 * the identifier %$abc will be left as-is so that the handler for %define
4515 * will suck it and define the corresponding value. Other case:
4516 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004517 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004518 *
4519 * In this case user wants name to be expanded *before* %define starts
4520 * working, so we'll expand %$abc into something (if it has a value;
4521 * otherwise it will be left as-is) then concatenate all successive
4522 * PP_IDs into one.
4523 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004524static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004525{
4526 Token *cur, *oldnext = NULL;
4527
H. Peter Anvin734b1882002-04-30 21:01:08 +00004528 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004529 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004530
4531 cur = tline;
4532 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004533 (cur->next->type == TOK_ID ||
4534 cur->next->type == TOK_PREPROC_ID
4535 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004536 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004537
4538 /* If identifier consists of just one token, don't expand */
4539 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004540 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004541
H. Peter Anvine2c80182005-01-15 22:15:51 +00004542 if (cur) {
4543 oldnext = cur->next; /* Detach the tail past identifier */
4544 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004545 }
4546
H. Peter Anvin734b1882002-04-30 21:01:08 +00004547 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004548
H. Peter Anvine2c80182005-01-15 22:15:51 +00004549 if (cur) {
4550 /* expand_smacro possibly changhed tline; re-scan for EOL */
4551 cur = tline;
4552 while (cur && cur->next)
4553 cur = cur->next;
4554 if (cur)
4555 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004556 }
4557
4558 return tline;
4559}
4560
4561/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004562 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004563 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004564 * to check for an initial label - that's taken care of in
4565 * expand_mmacro - but must check numbers of parameters. Guaranteed
4566 * to be called with tline->type == TOK_ID, so the putative macro
4567 * name is easy to find.
4568 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004569static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004570{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004571 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004572 Token **params;
4573 int nparam;
4574
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004575 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004576
4577 /*
4578 * Efficiency: first we see if any macro exists with the given
4579 * name. If not, we can return NULL immediately. _Then_ we
4580 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004581 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004582 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004583 list_for_each(m, head)
4584 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004585 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004586 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004587 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004588
4589 /*
4590 * OK, we have a potential macro. Count and demarcate the
4591 * parameters.
4592 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004593 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004594
4595 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004596 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004597 * structure that handles this number.
4598 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004599 while (m) {
4600 if (m->nparam_min <= nparam
4601 && (m->plus || nparam <= m->nparam_max)) {
4602 /*
4603 * This one is right. Just check if cycle removal
4604 * prohibits us using it before we actually celebrate...
4605 */
4606 if (m->in_progress > m->max_depth) {
4607 if (m->max_depth > 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004608 nasm_error(ERR_WARNING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004609 "reached maximum recursion depth of %i",
4610 m->max_depth);
4611 }
4612 nasm_free(params);
4613 return NULL;
4614 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004615 /*
4616 * It's right, and we can use it. Add its default
4617 * parameters to the end of our list if necessary.
4618 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004619 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004620 params =
4621 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004622 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004623 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004624 while (nparam < m->nparam_min + m->ndefs) {
4625 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004626 nparam++;
4627 }
4628 }
4629 /*
4630 * If we've gone over the maximum parameter count (and
4631 * we're in Plus mode), ignore parameters beyond
4632 * nparam_max.
4633 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004634 if (m->plus && nparam > m->nparam_max)
4635 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004636 /*
4637 * Then terminate the parameter list, and leave.
4638 */
4639 if (!params) { /* need this special case */
4640 params = nasm_malloc(sizeof(*params));
4641 nparam = 0;
4642 }
4643 params[nparam] = NULL;
4644 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004645 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004646 }
4647 /*
4648 * This one wasn't right: look for the next one with the
4649 * same name.
4650 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004651 list_for_each(m, m->next)
4652 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004653 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004654 }
4655
4656 /*
4657 * After all that, we didn't find one with the right number of
4658 * parameters. Issue a warning, and fail to expand the macro.
4659 */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004660 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004661 "macro `%s' exists, but not taking %d parameters",
4662 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004663 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004664 return NULL;
4665}
4666
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004667
4668/*
4669 * Save MMacro invocation specific fields in
4670 * preparation for a recursive macro expansion
4671 */
4672static void push_mmacro(MMacro *m)
4673{
4674 MMacroInvocation *i;
4675
4676 i = nasm_malloc(sizeof(MMacroInvocation));
4677 i->prev = m->prev;
4678 i->params = m->params;
4679 i->iline = m->iline;
4680 i->nparam = m->nparam;
4681 i->rotate = m->rotate;
4682 i->paramlen = m->paramlen;
4683 i->unique = m->unique;
4684 i->condcnt = m->condcnt;
4685 m->prev = i;
4686}
4687
4688
4689/*
4690 * Restore MMacro invocation specific fields that were
4691 * saved during a previous recursive macro expansion
4692 */
4693static void pop_mmacro(MMacro *m)
4694{
4695 MMacroInvocation *i;
4696
4697 if (m->prev) {
4698 i = m->prev;
4699 m->prev = i->prev;
4700 m->params = i->params;
4701 m->iline = i->iline;
4702 m->nparam = i->nparam;
4703 m->rotate = i->rotate;
4704 m->paramlen = i->paramlen;
4705 m->unique = i->unique;
4706 m->condcnt = i->condcnt;
4707 nasm_free(i);
4708 }
4709}
4710
4711
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004712/*
4713 * Expand the multi-line macro call made by the given line, if
4714 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004715 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004716 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004717static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004718{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004719 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004720 Token *label = NULL;
4721 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004722 Token **params, *t, *tt;
4723 MMacro *m;
4724 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004725 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004726 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004727
4728 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004729 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004730 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004731 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004732 return 0;
4733 m = is_mmacro(t, &params);
4734 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004735 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004736 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004737 Token *last;
4738 /*
4739 * We have an id which isn't a macro call. We'll assume
4740 * it might be a label; we'll also check to see if a
4741 * colon follows it. Then, if there's another id after
4742 * that lot, we'll check it again for macro-hood.
4743 */
4744 label = last = t;
4745 t = t->next;
4746 if (tok_type_(t, TOK_WHITESPACE))
4747 last = t, t = t->next;
4748 if (tok_is_(t, ":")) {
4749 dont_prepend = 1;
4750 last = t, t = t->next;
4751 if (tok_type_(t, TOK_WHITESPACE))
4752 last = t, t = t->next;
4753 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004754 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4755 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004756 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004757 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004758 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004759 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004760
4761 /*
4762 * Fix up the parameters: this involves stripping leading and
4763 * trailing whitespace, then stripping braces if they are
4764 * present.
4765 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004766 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004767 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004768
H. Peter Anvine2c80182005-01-15 22:15:51 +00004769 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004770 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004771 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004772
H. Peter Anvine2c80182005-01-15 22:15:51 +00004773 t = params[i];
4774 skip_white_(t);
4775 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004776 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004777 params[i] = t;
4778 paramlen[i] = 0;
4779 while (t) {
4780 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4781 break; /* ... because we have hit a comma */
4782 if (comma && t->type == TOK_WHITESPACE
4783 && tok_is_(t->next, ","))
4784 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004785 if (brace && t->type == TOK_OTHER) {
4786 if (t->text[0] == '{')
4787 brace++; /* ... or a nested opening brace */
4788 else if (t->text[0] == '}')
4789 if (!--brace)
4790 break; /* ... or a brace */
4791 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004792 t = t->next;
4793 paramlen[i]++;
4794 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004795 if (brace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004796 nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004797 }
4798
4799 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004800 * OK, we have a MMacro structure together with a set of
4801 * parameters. We must now go through the expansion and push
4802 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004803 * parameter tokens and macro-local tokens doesn't get done
4804 * until the single-line macro substitution process; this is
4805 * because delaying them allows us to change the semantics
4806 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004807 *
4808 * First, push an end marker on to istk->expansion, mark this
4809 * macro as in progress, and set up its invocation-specific
4810 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004811 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004812 ll = nasm_malloc(sizeof(Line));
4813 ll->next = istk->expansion;
4814 ll->finishes = m;
4815 ll->first = NULL;
4816 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004817
4818 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004819 * Save the previous MMacro expansion in the case of
4820 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004821 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004822 if (m->max_depth && m->in_progress)
4823 push_mmacro(m);
4824
4825 m->in_progress ++;
4826 m->params = params;
4827 m->iline = tline;
4828 m->nparam = nparam;
4829 m->rotate = 0;
4830 m->paramlen = paramlen;
4831 m->unique = unique++;
4832 m->lineno = 0;
4833 m->condcnt = 0;
4834
4835 m->next_active = istk->mstk;
4836 istk->mstk = m;
4837
4838 list_for_each(l, m->expansion) {
4839 Token **tail;
4840
4841 ll = nasm_malloc(sizeof(Line));
4842 ll->finishes = NULL;
4843 ll->next = istk->expansion;
4844 istk->expansion = ll;
4845 tail = &ll->first;
4846
4847 list_for_each(t, l->first) {
4848 Token *x = t;
4849 switch (t->type) {
4850 case TOK_PREPROC_Q:
4851 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004852 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004853 case TOK_PREPROC_QQ:
4854 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4855 break;
4856 case TOK_PREPROC_ID:
4857 if (t->text[1] == '0' && t->text[2] == '0') {
4858 dont_prepend = -1;
4859 x = label;
4860 if (!x)
4861 continue;
4862 }
4863 /* fall through */
4864 default:
4865 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4866 break;
4867 }
4868 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004869 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004870 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004871 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004872
4873 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004874 * If we had a label, push it on as the first line of
4875 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004876 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004877 if (label) {
4878 if (dont_prepend < 0)
4879 free_tlist(startline);
4880 else {
4881 ll = nasm_malloc(sizeof(Line));
4882 ll->finishes = NULL;
4883 ll->next = istk->expansion;
4884 istk->expansion = ll;
4885 ll->first = startline;
4886 if (!dont_prepend) {
4887 while (label->next)
4888 label = label->next;
4889 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004890 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004891 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004892 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004893
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004894 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004895
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004896 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004897}
4898
H. Peter Anvin130736c2016-02-17 20:27:41 -08004899/*
4900 * This function adds macro names to error messages, and suppresses
4901 * them if necessary.
4902 */
4903static void pp_verror(int severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004904{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004905 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004906 MMacro *mmac = NULL;
4907 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004908
H. Peter Anvin130736c2016-02-17 20:27:41 -08004909 /*
4910 * If we're in a dead branch of IF or something like it, ignore the error.
4911 * However, because %else etc are evaluated in the state context
4912 * of the previous branch, errors might get lost:
4913 * %if 0 ... %else trailing garbage ... %endif
4914 * So %else etc should set the ERR_PP_PRECOND flag.
4915 */
4916 if ((severity & ERR_MASK) < ERR_FATAL &&
4917 istk && istk->conds &&
4918 ((severity & ERR_PP_PRECOND) ?
4919 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004920 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004921 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004922
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004923 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004924 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004925 mmac = istk->mstk;
4926 /* but %rep blocks should be skipped */
4927 while (mmac && !mmac->name)
4928 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004929 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004930
H. Peter Anvin130736c2016-02-17 20:27:41 -08004931 if (mmac) {
4932 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004933
H. Peter Anvin130736c2016-02-17 20:27:41 -08004934 nasm_set_verror(real_verror);
4935 nasm_error(severity, "(%s:%d) %s",
4936 mmac->name, mmac->lineno - delta, buff);
4937 nasm_set_verror(pp_verror);
4938 } else {
4939 real_verror(severity, fmt, arg);
4940 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004941}
4942
H. Peter Anvin734b1882002-04-30 21:01:08 +00004943static void
H. Peter Anvin130736c2016-02-17 20:27:41 -08004944pp_reset(char *file, int apass, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004945{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004946 Token *t;
4947
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004948 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004949 istk = nasm_malloc(sizeof(Include));
4950 istk->next = NULL;
4951 istk->conds = NULL;
4952 istk->expansion = NULL;
4953 istk->mstk = NULL;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07004954 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004955 istk->fname = NULL;
H. Peter Anvin274cda82016-05-10 02:56:29 -07004956 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004957 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004958 if (!istk->fp)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004959 nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004960 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004961 nested_mac_count = 0;
4962 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004963 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004964 unique = 0;
H. Peter Anvinf7606612016-07-13 14:23:48 -07004965
4966 if (tasm_compatible_mode)
4967 pp_add_stdmac(nasm_stdmac_tasm);
4968
4969 pp_add_stdmac(nasm_stdmac_nasm);
4970 pp_add_stdmac(nasm_stdmac_version);
4971
4972 stdmacpos = stdmacros[0];
4973 stdmacnext = &stdmacros[1];
4974
H. Peter Anvind2456592008-06-19 15:04:18 -07004975 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004976
4977 /*
4978 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4979 * The caller, however, will also pass in 3 for preprocess-only so
4980 * we can set __PASS__ accordingly.
4981 */
4982 pass = apass > 2 ? 2 : apass;
4983
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004984 dephead = deplist;
H. Peter Anvin436e3672016-10-04 01:12:28 -07004985 nasm_add_string_to_strlist(dephead, file);
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004986
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004987 /*
4988 * Define the __PASS__ macro. This is defined here unlike
4989 * all the other builtins, because it is special -- it varies between
4990 * passes.
4991 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004992 t = nasm_malloc(sizeof(*t));
4993 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004994 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004995 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004996 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004997}
4998
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07004999static void pp_init(void)
5000{
5001 hash_init(&FileHash, HASH_MEDIUM);
5002}
5003
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005004static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005005{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005006 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005007 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005008
H. Peter Anvin130736c2016-02-17 20:27:41 -08005009 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005010
H. Peter Anvine2c80182005-01-15 22:15:51 +00005011 while (1) {
5012 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005013 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005014 * buffer or from the input file.
5015 */
5016 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005017 while (istk->expansion && istk->expansion->finishes) {
5018 Line *l = istk->expansion;
5019 if (!l->finishes->name && l->finishes->in_progress > 1) {
5020 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005021
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005022 /*
5023 * This is a macro-end marker for a macro with no
5024 * name, which means it's not really a macro at all
5025 * but a %rep block, and the `in_progress' field is
5026 * more than 1, meaning that we still need to
5027 * repeat. (1 means the natural last repetition; 0
5028 * means termination by %exitrep.) We have
5029 * therefore expanded up to the %endrep, and must
5030 * push the whole block on to the expansion buffer
5031 * again. We don't bother to remove the macro-end
5032 * marker: we'd only have to generate another one
5033 * if we did.
5034 */
5035 l->finishes->in_progress--;
5036 list_for_each(l, l->finishes->expansion) {
5037 Token *t, *tt, **tail;
5038
5039 ll = nasm_malloc(sizeof(Line));
5040 ll->next = istk->expansion;
5041 ll->finishes = NULL;
5042 ll->first = NULL;
5043 tail = &ll->first;
5044
5045 list_for_each(t, l->first) {
5046 if (t->text || t->type == TOK_WHITESPACE) {
5047 tt = *tail = new_Token(NULL, t->type, t->text, 0);
5048 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005049 }
5050 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005051
5052 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005053 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005054 } else {
5055 /*
5056 * Check whether a `%rep' was started and not ended
5057 * within this macro expansion. This can happen and
5058 * should be detected. It's a fatal error because
5059 * I'm too confused to work out how to recover
5060 * sensibly from it.
5061 */
5062 if (defining) {
5063 if (defining->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005064 nasm_panic(0, "defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005065 else if (istk->mstk->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005066 nasm_fatal(0, "`%%rep' without `%%endrep' within"
5067 " expansion of macro `%s'",
5068 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005069 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005070
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005071 /*
5072 * FIXME: investigate the relationship at this point between
5073 * istk->mstk and l->finishes
5074 */
5075 {
5076 MMacro *m = istk->mstk;
5077 istk->mstk = m->next_active;
5078 if (m->name) {
5079 /*
5080 * This was a real macro call, not a %rep, and
5081 * therefore the parameter information needs to
5082 * be freed.
5083 */
5084 if (m->prev) {
5085 pop_mmacro(m);
5086 l->finishes->in_progress --;
5087 } else {
5088 nasm_free(m->params);
5089 free_tlist(m->iline);
5090 nasm_free(m->paramlen);
5091 l->finishes->in_progress = 0;
5092 }
5093 } else
5094 free_mmacro(m);
5095 }
5096 istk->expansion = l->next;
5097 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005098 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005099 }
5100 }
5101 while (1) { /* until we get a line we can use */
5102
5103 if (istk->expansion) { /* from a macro expansion */
5104 char *p;
5105 Line *l = istk->expansion;
5106 if (istk->mstk)
5107 istk->mstk->lineno++;
5108 tline = l->first;
5109 istk->expansion = l->next;
5110 nasm_free(l);
5111 p = detoken(tline, false);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005112 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005113 nasm_free(p);
5114 break;
5115 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005116 line = read_line();
5117 if (line) { /* from the current input file */
5118 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005119 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005120 nasm_free(line);
5121 break;
5122 }
5123 /*
5124 * The current file has ended; work down the istk
5125 */
5126 {
5127 Include *i = istk;
5128 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005129 if (i->conds) {
5130 /* nasm_error can't be conditionally suppressed */
H. Peter Anvin41087062016-03-03 14:27:34 -08005131 nasm_fatal(0,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005132 "expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005133 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005134 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005135 if (i->next)
5136 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005137 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005138 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005139 nasm_free(i);
H. Peter Anvin130736c2016-02-17 20:27:41 -08005140 if (!istk) {
5141 line = NULL;
5142 goto done;
5143 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005144 if (istk->expansion && istk->expansion->finishes)
5145 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005146 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005147 }
5148
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005149 /*
5150 * We must expand MMacro parameters and MMacro-local labels
5151 * _before_ we plunge into directive processing, to cope
5152 * with things like `%define something %1' such as STRUC
5153 * uses. Unless we're _defining_ a MMacro, in which case
5154 * those tokens should be left alone to go into the
5155 * definition; and unless we're in a non-emitting
5156 * condition, in which case we don't want to meddle with
5157 * anything.
5158 */
5159 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5160 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005161 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005162 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005163
H. Peter Anvine2c80182005-01-15 22:15:51 +00005164 /*
5165 * Check the line to see if it's a preprocessor directive.
5166 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07005167 if (do_directive(tline, &line) == DIRECTIVE_FOUND) {
5168 if (line)
5169 break; /* Directive generated output */
5170 else
5171 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005172 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005173 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005174 * We're defining a multi-line macro. We emit nothing
5175 * at all, and just
5176 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005177 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005178 Line *l = nasm_malloc(sizeof(Line));
5179 l->next = defining->expansion;
5180 l->first = tline;
5181 l->finishes = NULL;
5182 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005183 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005184 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005185 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005186 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005187 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005188 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005189 * directive so we keep our place correctly.
5190 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005191 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005192 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005193 } else if (istk->mstk && !istk->mstk->in_progress) {
5194 /*
5195 * We're in a %rep block which has been terminated, so
5196 * we're walking through to the %endrep without
5197 * emitting anything. Emit nothing at all, not even a
5198 * blank line: when we emerge from the %rep block we'll
5199 * give a line-number directive so we keep our place
5200 * correctly.
5201 */
5202 free_tlist(tline);
5203 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005204 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005205 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005206 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005207 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005208 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005209 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005210 line = detoken(tline, true);
5211 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005212 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005213 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005214 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005215 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005216 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005217 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005218
H. Peter Anvin130736c2016-02-17 20:27:41 -08005219done:
5220 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005221 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005222}
5223
H. Peter Anvine2c80182005-01-15 22:15:51 +00005224static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005225{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005226 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005227
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005228 if (defining) {
5229 if (defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005230 nasm_error(ERR_NONFATAL,
5231 "end of file while still defining macro `%s'",
5232 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005233 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005234 nasm_error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005235 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005236
5237 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005238 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005239 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005240
5241 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005242
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005243 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005244 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005245 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005246 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005247 Include *i = istk;
5248 istk = istk->next;
5249 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005250 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005251 }
5252 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005253 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005254 src_set_fname(NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005255 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005256 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005257 free_llist(predef);
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005258 predef = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005259 delete_Blocks();
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005260 freeTokens = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005261 while ((i = ipath)) {
5262 ipath = i->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005263 if (i->path)
5264 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005265 nasm_free(i);
5266 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005267 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005268}
5269
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005270static void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005271{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005272 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005273
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005274 i = nasm_malloc(sizeof(IncPath));
5275 i->path = path ? nasm_strdup(path) : NULL;
5276 i->next = NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005277
H. Peter Anvin89cee572009-07-15 09:16:54 -04005278 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005279 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005280 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005281 j = j->next;
5282 j->next = i;
5283 } else {
5284 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005285 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005286}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005287
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005288static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005289{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005290 Token *inc, *space, *name;
5291 Line *l;
5292
H. Peter Anvin734b1882002-04-30 21:01:08 +00005293 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5294 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5295 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005296
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005297 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005298 l->next = predef;
5299 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005300 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005301 predef = l;
5302}
5303
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005304static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005305{
5306 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005307 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005308 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005309
H. Peter Anvin130736c2016-02-17 20:27:41 -08005310 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005311
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005312 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005313 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5314 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005315 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005316 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005317 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005318 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005319 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005320
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005321 if (space->next->type != TOK_PREPROC_ID &&
5322 space->next->type != TOK_ID)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005323 nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005324
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005325 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005326 l->next = predef;
5327 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005328 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005329 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005330
5331 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005332}
5333
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005334static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005335{
5336 Token *def, *space;
5337 Line *l;
5338
H. Peter Anvin734b1882002-04-30 21:01:08 +00005339 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5340 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005341 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005342
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005343 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005344 l->next = predef;
5345 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005346 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005347 predef = l;
5348}
5349
H. Peter Anvinf7606612016-07-13 14:23:48 -07005350static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005351{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005352 macros_t **mp;
5353
5354 /* Find the end of the list and avoid duplicates */
5355 for (mp = stdmacros; *mp; mp++) {
5356 if (*mp == macros)
5357 return; /* Nothing to do */
5358 }
5359
5360 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5361
5362 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005363}
5364
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005365static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005366{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005367 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005368 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005369 tok->text = nasm_strdup(numbuf);
5370 tok->type = TOK_NUMBER;
5371}
5372
H. Peter Anvin37368952016-05-09 14:10:32 -07005373static void pp_list_one_macro(MMacro *m, int severity)
5374{
5375 if (!m)
5376 return;
5377
5378 /* We need to print the next_active list in reverse order */
5379 pp_list_one_macro(m->next_active, severity);
5380
5381 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005382 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvin37368952016-05-09 14:10:32 -07005383 nasm_error(severity, "... from macro `%s' defined here", m->name);
5384 }
5385}
5386
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005387static void pp_error_list_macros(int severity)
5388{
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005389 int32_t saved_line;
5390 const char *saved_fname = NULL;
5391
5392 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY;
H. Peter Anvin274cda82016-05-10 02:56:29 -07005393 src_get(&saved_line, &saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005394
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005395 if (istk)
5396 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005397
H. Peter Anvin274cda82016-05-10 02:56:29 -07005398 src_set(saved_line, saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005399}
5400
H. Peter Anvine7469712016-02-18 02:20:59 -08005401const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005402 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005403 pp_reset,
5404 pp_getline,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005405 pp_cleanup,
H. Peter Anvinf7606612016-07-13 14:23:48 -07005406 pp_add_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005407 pp_pre_define,
5408 pp_pre_undefine,
5409 pp_pre_include,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005410 pp_include_path,
5411 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005412};