blob: ee91d15beb77f60856a06b04de39324729037754 [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];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300447static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000448
449/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000450 * Tokens are allocated in blocks to improve speed
451 */
452#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800453static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000454struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000455 Blocks *next;
456 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000457};
458
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800459static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000460
461/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000462 * Forward declarations.
463 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700464static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000465static Token *expand_mmac_params(Token * tline);
466static Token *expand_smacro(Token * tline);
467static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400468static Context *get_ctx(const char *name, const char **namep);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700469static void make_tok_num(Token * tok, int64_t val);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800470static void pp_verror(int severity, const char *fmt, va_list ap);
471static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000472static void *new_Block(size_t size);
473static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700474static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300475 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000476static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000477
478/*
479 * Macros for safe checking of token pointers, avoid *(NULL)
480 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300481#define tok_type_(x,t) ((x) && (x)->type == (t))
482#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
483#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
484#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000485
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400486/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700487 * nasm_unquote with error if the string contains NUL characters.
488 * If the string contains NUL characters, issue an error and return
489 * the C len, i.e. truncate at the NUL.
490 */
491static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
492{
493 size_t len = nasm_unquote(qstr, NULL);
494 size_t clen = strlen(qstr);
495
496 if (len != clen)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800497 nasm_error(ERR_NONFATAL, "NUL character in `%s' directive",
H. Peter Anvin077fb932010-07-20 14:56:30 -0700498 pp_directives[directive]);
499
500 return clen;
501}
502
503/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700504 * In-place reverse a list of tokens.
505 */
506static Token *reverse_tokens(Token *t)
507{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800508 Token *prev = NULL;
509 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700510
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800511 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400512 next = t->next;
513 t->next = prev;
514 prev = t;
515 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800516 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700517
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800518 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700519}
520
521/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300522 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000523 * front of them. We do it here because I could not find any other
524 * place to do it for the moment, and it is a hack (ideally it would
525 * be nice to be able to use the NASM pre-processor to do it).
526 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000527static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000528{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000529 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400530 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000531
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400532 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000533
534 /* Binary search for the directive name */
535 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400536 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400537 q = nasm_skip_word(p);
538 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000539 if (len) {
540 oldchar = p[len];
541 p[len] = 0;
542 while (j - i > 1) {
543 k = (j + i) / 2;
544 m = nasm_stricmp(p, tasm_directives[k]);
545 if (m == 0) {
546 /* We have found a directive, so jam a % in front of it
547 * so that NASM will then recognise it as one if it's own.
548 */
549 p[len] = oldchar;
550 len = strlen(p);
551 oldline = line;
552 line = nasm_malloc(len + 2);
553 line[0] = '%';
554 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700555 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300556 * NASM does not recognise IFDIFI, so we convert
557 * it to %if 0. This is not used in NASM
558 * compatible code, but does need to parse for the
559 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000560 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700561 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000562 } else {
563 memcpy(line + 1, p, len + 1);
564 }
565 nasm_free(oldline);
566 return line;
567 } else if (m < 0) {
568 j = k;
569 } else
570 i = k;
571 }
572 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000573 }
574 return line;
575}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000576
H. Peter Anvin76690a12002-04-30 20:52:49 +0000577/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000578 * The pre-preprocessing stage... This function translates line
579 * number indications as they emerge from GNU cpp (`# lineno "file"
580 * flags') into NASM preprocessor line number indications (`%line
581 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000582 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000583static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000584{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000585 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000586 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000587
H. Peter Anvine2c80182005-01-15 22:15:51 +0000588 if (line[0] == '#' && line[1] == ' ') {
589 oldline = line;
590 fname = oldline + 2;
591 lineno = atoi(fname);
592 fname += strspn(fname, "0123456789 ");
593 if (*fname == '"')
594 fname++;
595 fnlen = strcspn(fname, "\"");
596 line = nasm_malloc(20 + fnlen);
597 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
598 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000599 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000600 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000601 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000602 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000603}
604
605/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000606 * Free a linked list of tokens.
607 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000608static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000609{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400610 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000611 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000612}
613
614/*
615 * Free a linked list of lines.
616 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000617static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000618{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400619 Line *l, *tmp;
620 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000621 free_tlist(l->first);
622 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000623 }
624}
625
626/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800627 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000628 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800629static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000630{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800631 nasm_free(m->name);
632 free_tlist(m->dlist);
633 nasm_free(m->defaults);
634 free_llist(m->expansion);
635 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000636}
637
638/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700639 * Free all currently defined macros, and free the hash tables
640 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700641static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700642{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400643 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700644 const char *key;
645 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700646
H. Peter Anvin072771e2008-05-22 13:17:51 -0700647 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300648 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400649 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300650 nasm_free(s->name);
651 free_tlist(s->expansion);
652 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300653 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700654 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700655 hash_free(smt);
656}
657
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800658static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700659{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800660 MMacro *m, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700661 const char *key;
662 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700663
664 it = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800665 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300666 nasm_free((void *)key);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800667 list_for_each_safe(m ,tmp, m)
668 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700669 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800670 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700671}
672
673static void free_macros(void)
674{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700675 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800676 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700677}
678
679/*
680 * Initialize the hash tables
681 */
682static void init_macros(void)
683{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700684 hash_init(&smacros, HASH_LARGE);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800685 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700686}
687
688/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000689 * Pop the context stack.
690 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000691static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000692{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000693 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000694
695 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700696 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000697 nasm_free(c->name);
698 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000699}
700
H. Peter Anvin072771e2008-05-22 13:17:51 -0700701/*
702 * Search for a key in the hash index; adding it if necessary
703 * (in which case we initialize the data pointer to NULL.)
704 */
705static void **
706hash_findi_add(struct hash_table *hash, const char *str)
707{
708 struct hash_insert hi;
709 void **r;
710 char *strx;
711
712 r = hash_findi(hash, str, &hi);
713 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300714 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700715
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300716 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700717 return hash_add(&hi, strx, NULL);
718}
719
720/*
721 * Like hash_findi, but returns the data element rather than a pointer
722 * to it. Used only when not adding a new element, hence no third
723 * argument.
724 */
725static void *
726hash_findix(struct hash_table *hash, const char *str)
727{
728 void **p;
729
730 p = hash_findi(hash, str, NULL);
731 return p ? *p : NULL;
732}
733
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400734/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800735 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400736 * if there no more left -- return NULL
737 */
738static char *line_from_stdmac(void)
739{
740 unsigned char c;
741 const unsigned char *p = stdmacpos;
742 char *line, *q;
743 size_t len = 0;
744
745 if (!stdmacpos)
746 return NULL;
747
748 while ((c = *p++)) {
749 if (c >= 0x80)
750 len += pp_directives_len[c - 0x80] + 1;
751 else
752 len++;
753 }
754
755 line = nasm_malloc(len + 1);
756 q = line;
757 while ((c = *stdmacpos++)) {
758 if (c >= 0x80) {
759 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
760 q += pp_directives_len[c - 0x80];
761 *q++ = ' ';
762 } else {
763 *q++ = c;
764 }
765 }
766 stdmacpos = p;
767 *q = '\0';
768
769 if (!*stdmacpos) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700770 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400771 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700772 if (*stdmacnext) {
773 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400774 } else if (do_predef) {
775 Line *pd, *l;
776 Token *head, **tail, *t;
777
778 /*
779 * Nasty hack: here we push the contents of
780 * `predef' on to the top-level expansion stack,
781 * since this is the most convenient way to
782 * implement the pre-include and pre-define
783 * features.
784 */
785 list_for_each(pd, predef) {
786 head = NULL;
787 tail = &head;
788 list_for_each(t, pd->first) {
789 *tail = new_Token(NULL, t->type, t->text, 0);
790 tail = &(*tail)->next;
791 }
792
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800793 l = nasm_malloc(sizeof(Line));
794 l->next = istk->expansion;
795 l->first = head;
796 l->finishes = NULL;
797
798 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400799 }
800 do_predef = false;
801 }
802 }
803
804 return line;
805}
806
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000807static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000808{
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400809 unsigned int size, c, next;
810 const unsigned int delta = 512;
811 const unsigned int pad = 8;
812 unsigned int nr_cont = 0;
813 bool cont = false;
814 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000815
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400816 /* Standart macros set (predefined) goes first */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400817 p = line_from_stdmac();
818 if (p)
819 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700820
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400821 size = delta;
822 p = buffer = nasm_malloc(size);
823
824 for (;;) {
825 c = fgetc(istk->fp);
826 if ((int)(c) == EOF) {
827 p[0] = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000828 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000829 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400830
831 switch (c) {
832 case '\r':
833 next = fgetc(istk->fp);
834 if (next != '\n')
835 ungetc(next, istk->fp);
836 if (cont) {
837 cont = false;
838 continue;
839 }
840 break;
841
842 case '\n':
843 if (cont) {
844 cont = false;
845 continue;
846 }
847 break;
848
849 case '\\':
850 next = fgetc(istk->fp);
851 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400852 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400853 cont = true;
854 nr_cont++;
855 continue;
856 }
857 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000858 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400859
860 if (c == '\r' || c == '\n') {
861 *p++ = 0;
862 break;
863 }
864
865 if (p >= (buffer + size - pad)) {
866 buffer = nasm_realloc(buffer, size + delta);
867 p = buffer + size - pad;
868 size += delta;
869 }
870
871 *p++ = (unsigned char)c;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000872 }
873
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400874 if (p == buffer) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000875 nasm_free(buffer);
876 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000877 }
878
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300879 src_set_linnum(src_get_linnum() + istk->lineinc +
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400880 (nr_cont * istk->lineinc));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000881
882 /*
883 * Handle spurious ^Z, which may be inserted into source files
884 * by some file transfer utilities.
885 */
886 buffer[strcspn(buffer, "\032")] = '\0';
887
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -0800888 lfmt->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000889
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000890 return buffer;
891}
892
893/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000894 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000895 * don't need to parse the value out of e.g. numeric tokens: we
896 * simply split one string into many.
897 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000898static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000899{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700900 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000901 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000902 Token *list = NULL;
903 Token *t, **tail = &list;
904
H. Peter Anvine2c80182005-01-15 22:15:51 +0000905 while (*line) {
906 p = line;
907 if (*p == '%') {
908 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300909 if (*p == '+' && !nasm_isdigit(p[1])) {
910 p++;
911 type = TOK_PASTE;
912 } else if (nasm_isdigit(*p) ||
913 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000914 do {
915 p++;
916 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700917 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000918 type = TOK_PREPROC_ID;
919 } else if (*p == '{') {
920 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800921 while (*p) {
922 if (*p == '}')
923 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000924 p[-1] = *p;
925 p++;
926 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800927 if (*p != '}')
H. Peter Anvin130736c2016-02-17 20:27:41 -0800928 nasm_error(ERR_WARNING | ERR_PASS1,
929 "unterminated %%{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000930 p[-1] = '\0';
931 if (*p)
932 p++;
933 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300934 } else if (*p == '[') {
935 int lvl = 1;
936 line += 2; /* Skip the leading %[ */
937 p++;
938 while (lvl && (c = *p++)) {
939 switch (c) {
940 case ']':
941 lvl--;
942 break;
943 case '%':
944 if (*p == '[')
945 lvl++;
946 break;
947 case '\'':
948 case '\"':
949 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400950 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300951 break;
952 default:
953 break;
954 }
955 }
956 p--;
957 if (*p)
958 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800959 if (lvl)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800960 nasm_error(ERR_NONFATAL|ERR_PASS1,
961 "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300962 type = TOK_INDIRECT;
963 } else if (*p == '?') {
964 type = TOK_PREPROC_Q; /* %? */
965 p++;
966 if (*p == '?') {
967 type = TOK_PREPROC_QQ; /* %?? */
968 p++;
969 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400970 } else if (*p == '!') {
971 type = TOK_PREPROC_ID;
972 p++;
973 if (isidchar(*p)) {
974 do {
975 p++;
976 }
977 while (isidchar(*p));
978 } else if (*p == '\'' || *p == '\"' || *p == '`') {
979 p = nasm_skip_string(p);
980 if (*p)
981 p++;
982 else
H. Peter Anvin130736c2016-02-17 20:27:41 -0800983 nasm_error(ERR_NONFATAL|ERR_PASS1,
984 "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400985 } else {
986 /* %! without string or identifier */
987 type = TOK_OTHER; /* Legacy behavior... */
988 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000989 } else if (isidchar(*p) ||
990 ((*p == '!' || *p == '%' || *p == '$') &&
991 isidchar(p[1]))) {
992 do {
993 p++;
994 }
995 while (isidchar(*p));
996 type = TOK_PREPROC_ID;
997 } else {
998 type = TOK_OTHER;
999 if (*p == '%')
1000 p++;
1001 }
1002 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
1003 type = TOK_ID;
1004 p++;
1005 while (*p && isidchar(*p))
1006 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001007 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001008 /*
1009 * A string token.
1010 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001012 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001013
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014 if (*p) {
1015 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001016 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001017 nasm_error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001018 /* Handling unterminated strings by UNV */
1019 /* type = -1; */
1020 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001021 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001022 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001023 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001024 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001025 bool is_hex = false;
1026 bool is_float = false;
1027 bool has_e = false;
1028 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001029
H. Peter Anvine2c80182005-01-15 22:15:51 +00001030 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001031 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001032 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001033
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001034 if (*p == '$') {
1035 p++;
1036 is_hex = true;
1037 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001038
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001039 for (;;) {
1040 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001041
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001042 if (!is_hex && (c == 'e' || c == 'E')) {
1043 has_e = true;
1044 if (*p == '+' || *p == '-') {
1045 /*
1046 * e can only be followed by +/- if it is either a
1047 * prefixed hex number or a floating-point number
1048 */
1049 p++;
1050 is_float = true;
1051 }
1052 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1053 is_hex = true;
1054 } else if (c == 'P' || c == 'p') {
1055 is_float = true;
1056 if (*p == '+' || *p == '-')
1057 p++;
Martin Lindheb150e382016-11-16 15:36:53 +01001058 } else if (isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001059 ; /* just advance */
1060 else if (c == '.') {
1061 /*
1062 * we need to deal with consequences of the legacy
1063 * parser, like "1.nolist" being two tokens
1064 * (TOK_NUMBER, TOK_ID) here; at least give it
1065 * a shot for now. In the future, we probably need
1066 * a flex-based scanner with proper pattern matching
1067 * to do it as well as it can be done. Nothing in
1068 * the world is going to help the person who wants
1069 * 0x123.p16 interpreted as two tokens, though.
1070 */
1071 r = p;
1072 while (*r == '_')
1073 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001074
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001075 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1076 (!is_hex && (*r == 'e' || *r == 'E')) ||
1077 (*r == 'p' || *r == 'P')) {
1078 p = r;
1079 is_float = true;
1080 } else
1081 break; /* Terminate the token */
1082 } else
1083 break;
1084 }
1085 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001086
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001087 if (p == line+1 && *line == '$') {
1088 type = TOK_OTHER; /* TOKEN_HERE */
1089 } else {
1090 if (has_e && !is_hex) {
1091 /* 1e13 is floating-point, but 1e13h is not */
1092 is_float = true;
1093 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001094
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001095 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1096 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001097 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001098 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001099 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001100 /*
1101 * Whitespace just before end-of-line is discarded by
1102 * pretending it's a comment; whitespace just before a
1103 * comment gets lumped into the comment.
1104 */
1105 if (!*p || *p == ';') {
1106 type = TOK_COMMENT;
1107 while (*p)
1108 p++;
1109 }
1110 } else if (*p == ';') {
1111 type = TOK_COMMENT;
1112 while (*p)
1113 p++;
1114 } else {
1115 /*
1116 * Anything else is an operator of some kind. We check
1117 * for all the double-character operators (>>, <<, //,
1118 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001119 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001120 */
1121 type = TOK_OTHER;
1122 if ((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[0] == '^' && p[1] == '^')) {
1133 p++;
1134 }
1135 p++;
1136 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001137
H. Peter Anvine2c80182005-01-15 22:15:51 +00001138 /* Handling unterminated string by UNV */
1139 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001140 {
1141 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1142 t->text[p-line] = *line;
1143 tail = &t->next;
1144 }
1145 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001146 if (type != TOK_COMMENT) {
1147 *tail = t = new_Token(NULL, type, line, p - line);
1148 tail = &t->next;
1149 }
1150 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001151 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001152 return list;
1153}
1154
H. Peter Anvince616072002-04-30 21:02:23 +00001155/*
1156 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001157 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001158 * deleted only all at once by the delete_Blocks function.
1159 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001161{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001162 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001164 /* first, get to the end of the linked list */
1165 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001167 /* now allocate the requested chunk */
1168 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001169
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001170 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001171 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001172 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001173}
1174
1175/*
1176 * this function deletes all managed blocks of memory
1177 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001178static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001179{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001180 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001181
H. Peter Anvin70653092007-10-19 14:42:29 -07001182 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001183 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001184 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001185 * free it.
1186 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001187 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001188 if (b->chunk)
1189 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001190 a = b;
1191 b = b->next;
1192 if (a != &blocks)
1193 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001194 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001195 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001196}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001197
1198/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001199 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001200 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001201 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001202 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001203static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001204 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001205{
1206 Token *t;
1207 int i;
1208
H. Peter Anvin89cee572009-07-15 09:16:54 -04001209 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001210 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1211 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1212 freeTokens[i].next = &freeTokens[i + 1];
1213 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001214 }
1215 t = freeTokens;
1216 freeTokens = t->next;
1217 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001218 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001219 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001220 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001221 t->text = NULL;
1222 } else {
1223 if (txtlen == 0)
1224 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001225 t->text = nasm_malloc(txtlen+1);
1226 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001227 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001228 }
1229 return t;
1230}
1231
H. Peter Anvine2c80182005-01-15 22:15:51 +00001232static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001233{
1234 Token *next = t->next;
1235 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001236 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001237 freeTokens = t;
1238 return next;
1239}
1240
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001241/*
1242 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001243 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1244 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001245 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001246static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001247{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001248 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001249 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001250 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001251 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001252
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001253 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001254 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001255 char *v;
1256 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001257
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001258 v = t->text + 2;
1259 if (*v == '\'' || *v == '\"' || *v == '`') {
1260 size_t len = nasm_unquote(v, NULL);
1261 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001262
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001263 if (len != clen) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001264 nasm_error(ERR_NONFATAL | ERR_PASS1,
1265 "NUL character in %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001266 v = NULL;
1267 }
1268 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001269
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001270 if (v) {
1271 char *p = getenv(v);
1272 if (!p) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001273 nasm_error(ERR_NONFATAL | ERR_PASS1,
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001274 "nonexistent environment variable `%s'", v);
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001275 /*
1276 * FIXME We better should investigate if accessing
1277 * ->text[1] without ->text[0] is safe enough.
1278 */
1279 t->text = nasm_zalloc(2);
1280 } else
1281 t->text = nasm_strdup(p);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001282 }
1283 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001284 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001285
H. Peter Anvine2c80182005-01-15 22:15:51 +00001286 /* Expand local macros here and not during preprocessing */
1287 if (expand_locals &&
1288 t->type == TOK_PREPROC_ID && t->text &&
1289 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001290 const char *q;
1291 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001292 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001293 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001294 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001295 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001296 p = nasm_strcat(buffer, q);
1297 nasm_free(t->text);
1298 t->text = p;
1299 }
1300 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001301 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001302 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001303 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001304 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001305 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001306
H. Peter Anvin734b1882002-04-30 21:01:08 +00001307 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001308
1309 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001311 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001312 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001313 q = t->text;
1314 while (*q)
1315 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001316 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001317 }
1318 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001319
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001320 return line;
1321}
1322
1323/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001324 * A scanner, suitable for use by the expression evaluator, which
1325 * operates on a line of Tokens. Expects a pointer to a pointer to
1326 * the first token in the line to be passed in as its private_data
1327 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001328 *
1329 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001330 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001331static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001332{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001333 Token **tlineptr = private_data;
1334 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001335 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001336
H. Peter Anvine2c80182005-01-15 22:15:51 +00001337 do {
1338 tline = *tlineptr;
1339 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001340 } while (tline && (tline->type == TOK_WHITESPACE ||
1341 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001342
1343 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001344 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001345
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001346 tokval->t_charptr = tline->text;
1347
H. Peter Anvin76690a12002-04-30 20:52:49 +00001348 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001349 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001350 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001351 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001352
H. Peter Anvine2c80182005-01-15 22:15:51 +00001353 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001354 p = tokval->t_charptr = tline->text;
1355 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001356 tokval->t_charptr++;
1357 return tokval->t_type = TOKEN_ID;
1358 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001359
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001360 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001361 if (r >= p+MAX_KEYWORD)
1362 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001363 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001364 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001365 *s = '\0';
1366 /* right, so we have an identifier sitting in temp storage. now,
1367 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001368 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001369 }
1370
H. Peter Anvine2c80182005-01-15 22:15:51 +00001371 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001372 bool rn_error;
1373 tokval->t_integer = readnum(tline->text, &rn_error);
1374 tokval->t_charptr = tline->text;
1375 if (rn_error)
1376 return tokval->t_type = TOKEN_ERRNUM;
1377 else
1378 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001379 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001380
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001381 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001382 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001383 }
1384
H. Peter Anvine2c80182005-01-15 22:15:51 +00001385 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001386 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001387
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001388 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001389 tokval->t_charptr = tline->text;
1390 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001391
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001392 if (ep[0] != bq || ep[1] != '\0')
1393 return tokval->t_type = TOKEN_ERRSTR;
1394 else
1395 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001396 }
1397
H. Peter Anvine2c80182005-01-15 22:15:51 +00001398 if (tline->type == TOK_OTHER) {
1399 if (!strcmp(tline->text, "<<"))
1400 return tokval->t_type = TOKEN_SHL;
1401 if (!strcmp(tline->text, ">>"))
1402 return tokval->t_type = TOKEN_SHR;
1403 if (!strcmp(tline->text, "//"))
1404 return tokval->t_type = TOKEN_SDIV;
1405 if (!strcmp(tline->text, "%%"))
1406 return tokval->t_type = TOKEN_SMOD;
1407 if (!strcmp(tline->text, "=="))
1408 return tokval->t_type = TOKEN_EQ;
1409 if (!strcmp(tline->text, "<>"))
1410 return tokval->t_type = TOKEN_NE;
1411 if (!strcmp(tline->text, "!="))
1412 return tokval->t_type = TOKEN_NE;
1413 if (!strcmp(tline->text, "<="))
1414 return tokval->t_type = TOKEN_LE;
1415 if (!strcmp(tline->text, ">="))
1416 return tokval->t_type = TOKEN_GE;
1417 if (!strcmp(tline->text, "&&"))
1418 return tokval->t_type = TOKEN_DBL_AND;
1419 if (!strcmp(tline->text, "^^"))
1420 return tokval->t_type = TOKEN_DBL_XOR;
1421 if (!strcmp(tline->text, "||"))
1422 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001423 }
1424
1425 /*
1426 * We have no other options: just return the first character of
1427 * the token text.
1428 */
1429 return tokval->t_type = tline->text[0];
1430}
1431
1432/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001433 * Compare a string to the name of an existing macro; this is a
1434 * simple wrapper which calls either strcmp or nasm_stricmp
1435 * depending on the value of the `casesense' parameter.
1436 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001437static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001438{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001439 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001440}
1441
1442/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001443 * Compare a string to the name of an existing macro; this is a
1444 * simple wrapper which calls either strcmp or nasm_stricmp
1445 * depending on the value of the `casesense' parameter.
1446 */
1447static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1448{
1449 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1450}
1451
1452/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001453 * Return the Context structure associated with a %$ token. Return
1454 * NULL, having _already_ reported an error condition, if the
1455 * context stack isn't deep enough for the supplied number of $
1456 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001457 *
1458 * If "namep" is non-NULL, set it to the pointer to the macro name
1459 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001460 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001461static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001462{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001463 Context *ctx;
1464 int i;
1465
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001466 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001467 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001468
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001469 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001470 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001471
H. Peter Anvine2c80182005-01-15 22:15:51 +00001472 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001473 nasm_error(ERR_NONFATAL, "`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001474 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001475 }
1476
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001477 name += 2;
1478 ctx = cstk;
1479 i = 0;
1480 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001481 name++;
1482 i++;
1483 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001484 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001485 if (!ctx) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001486 nasm_error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001487 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001488 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001489 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001490
1491 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001492 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001493
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001494 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001495}
1496
1497/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001498 * Open an include file. This routine must always return a valid
1499 * file pointer if it returns - it's responsible for throwing an
1500 * ERR_FATAL and bombing out completely if not. It should also try
1501 * the include path one by one until it finds the file or reaches
1502 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001503 *
1504 * Note: for INC_PROBE the function returns NULL at all times;
1505 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001506 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001507enum incopen_mode {
1508 INC_NEEDED, /* File must exist */
1509 INC_OPTIONAL, /* Missing is OK */
1510 INC_PROBE /* Only an existence probe */
1511};
1512
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001513/* This is conducts a full pathname search */
1514static FILE *inc_fopen_search(const char *file, StrList **slpath,
1515 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001516{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001517 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001518 char *prefix = "";
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001519 const IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001520 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001521 size_t prefix_len = 0;
1522 StrList *sl;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001523 size_t path_len;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001524 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001525
H. Peter Anvine2c80182005-01-15 22:15:51 +00001526 while (1) {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001527 path_len = prefix_len + len + 1;
1528
1529 sl = nasm_malloc(path_len + sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001530 memcpy(sl->str, prefix, prefix_len);
1531 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001532 sl->next = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001533
H. Peter Anvind81a2352016-09-21 14:03:18 -07001534 if (omode == INC_PROBE) {
1535 fp = NULL;
1536 found = nasm_file_exists(sl->str);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001537 } else {
H. Peter Anvind81a2352016-09-21 14:03:18 -07001538 fp = nasm_open_read(sl->str, fmode);
1539 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001540 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001541 if (found) {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001542 *slpath = sl;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001543 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001544 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001545
H. Peter Anvind81a2352016-09-21 14:03:18 -07001546 nasm_free(sl);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001547
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001548 if (!ip)
1549 return NULL;
1550
1551 prefix = ip->path;
1552 prefix_len = strlen(prefix);
1553 ip = ip->next;
1554 }
1555}
1556
1557/*
1558 * Open a file, or test for the presence of one (depending on omode),
1559 * considering the include path.
1560 */
1561static FILE *inc_fopen(const char *file,
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001562 StrList **dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001563 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001564 enum incopen_mode omode,
1565 enum file_flags fmode)
1566{
1567 StrList *sl;
1568 struct hash_insert hi;
1569 void **hp;
1570 char *path;
1571 FILE *fp = NULL;
1572
1573 hp = hash_find(&FileHash, file, &hi);
1574 if (hp) {
1575 path = *hp;
1576 } else {
1577 /* Need to do the actual path search */
1578 size_t file_len;
1579
1580 sl = NULL;
1581 fp = inc_fopen_search(file, &sl, omode, fmode);
1582
1583 file_len = strlen(file);
1584
1585 if (!sl) {
1586 /* Store negative result for this file */
1587 sl = nasm_malloc(file_len + 1 + sizeof sl->next);
1588 memcpy(sl->str, file, file_len+1);
1589 sl->next = NULL;
1590 file = sl->str;
1591 path = NULL;
1592 } else {
1593 path = sl->str;
1594 file = strchr(path, '\0') - file_len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001595 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001596
1597 hash_add(&hi, file, path); /* Positive or negative result */
1598
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001599 /*
1600 * Add file to dependency path. The in_list() is needed
1601 * in case the file was already added with %depend.
1602 */
1603 if (path || omode != INC_NEEDED)
H. Peter Anvin436e3672016-10-04 01:12:28 -07001604 nasm_add_to_strlist(dhead, sl);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001605 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001606
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001607 if (!path) {
1608 if (omode == INC_NEEDED)
1609 nasm_fatal(0, "unable to open include file `%s'", file);
1610
1611 if (found_path)
1612 *found_path = NULL;
1613
1614 return NULL;
1615 }
1616
1617 if (!fp && omode != INC_PROBE)
Cyrill Gorcunov4ff8c632017-01-06 00:36:23 +03001618 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001619
1620 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001621 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001622
1623 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001624}
1625
1626/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001627 * Opens an include or input file. Public version, for use by modules
1628 * that get a file:lineno pair and need to look at the file again
1629 * (e.g. the CodeView debug backend). Returns NULL on failure.
1630 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001631FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001632{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001633 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001634}
1635
1636/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001637 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001638 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001639 * return true if _any_ single-line macro of that name is defined.
1640 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001641 * `nparam' or no parameters is defined.
1642 *
1643 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001644 * defined, or nparam is -1, the address of the definition structure
1645 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001646 * is NULL, no action will be taken regarding its contents, and no
1647 * error will occur.
1648 *
1649 * Note that this is also called with nparam zero to resolve
1650 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001651 *
1652 * If you already know which context macro belongs to, you can pass
1653 * the context pointer as first parameter; if you won't but name begins
1654 * with %$ the context will be automatically computed. If all_contexts
1655 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001656 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001657static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001658smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001659 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001660{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001661 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001662 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001663
H. Peter Anvin97a23472007-09-16 17:57:25 -07001664 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001665 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001666 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001667 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001668 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001669 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001670 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001671 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001672 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001673 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001674 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001675 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001676
H. Peter Anvine2c80182005-01-15 22:15:51 +00001677 while (m) {
1678 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001679 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001680 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001681 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001682 *defn = m;
1683 else
1684 *defn = NULL;
1685 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001686 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001687 }
1688 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001689 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001690
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001691 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001692}
1693
1694/*
1695 * Count and mark off the parameters in a multi-line macro call.
1696 * This is called both from within the multi-line macro expansion
1697 * code, and also to mark off the default parameters when provided
1698 * in a %macro definition line.
1699 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001700static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001701{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001702 int paramsize, brace;
1703
1704 *nparam = paramsize = 0;
1705 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001706 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001707 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001708 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001709 paramsize += PARAM_DELTA;
1710 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1711 }
1712 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001713 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001714 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001715 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001716 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001717 if (brace) {
1718 while (brace && (t = t->next) != NULL) {
1719 if (tok_is_(t, "{"))
1720 brace++;
1721 else if (tok_is_(t, "}"))
1722 brace--;
1723 }
1724
1725 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001726 /*
1727 * Now we've found the closing brace, look further
1728 * for the comma.
1729 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001730 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001731 skip_white_(t);
1732 if (tok_isnt_(t, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001733 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001734 "braces do not enclose all of macro parameter");
1735 while (tok_isnt_(t, ","))
1736 t = t->next;
1737 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001738 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001739 } else {
1740 while (tok_isnt_(t, ","))
1741 t = t->next;
1742 }
1743 if (t) { /* got a comma/brace */
1744 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001746 }
1747}
1748
1749/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001750 * Determine whether one of the various `if' conditions is true or
1751 * not.
1752 *
1753 * We must free the tline we get passed.
1754 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001755static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001756{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001757 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001758 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001759 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001760 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001761 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001762 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001763 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001764
1765 origline = tline;
1766
H. Peter Anvine2c80182005-01-15 22:15:51 +00001767 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001768 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001769 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001770 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001771 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001772 if (!tline)
1773 break;
1774 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001775 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001776 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001777 free_tlist(origline);
1778 return -1;
1779 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001780 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001781 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782 tline = tline->next;
1783 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001784 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001785
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001786 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001787 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001788 while (tline) {
1789 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001790 if (!tline || (tline->type != TOK_ID &&
1791 (tline->type != TOK_PREPROC_ID ||
1792 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001793 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001794 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001795 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001796 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001797 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001798 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001799 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001800 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001801 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001802
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001803 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001804 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001805 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001806 while (tline) {
1807 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001808 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001809 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001810 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001811 tline->text[1] != '!'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001812 nasm_error(ERR_NONFATAL,
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001813 "`%s' expects environment variable names",
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001814 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001815 goto fail;
1816 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001817 p = tline->text;
1818 if (tline->type == TOK_PREPROC_ID)
1819 p += 2; /* Skip leading %! */
1820 if (*p == '\'' || *p == '\"' || *p == '`')
1821 nasm_unquote_cstr(p, ct);
1822 if (getenv(p))
1823 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001824 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001825 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001826 break;
1827
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001828 case PPC_IFIDN:
1829 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001830 tline = expand_smacro(tline);
1831 t = tt = tline;
1832 while (tok_isnt_(tt, ","))
1833 tt = tt->next;
1834 if (!tt) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001835 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001836 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001837 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001838 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001839 }
1840 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001841 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001842 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1843 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001844 nasm_error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001845 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001846 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001847 }
1848 if (t->type == TOK_WHITESPACE) {
1849 t = t->next;
1850 continue;
1851 }
1852 if (tt->type == TOK_WHITESPACE) {
1853 tt = tt->next;
1854 continue;
1855 }
1856 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001857 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001858 break;
1859 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001860 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001861 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001862 size_t l1 = nasm_unquote(t->text, NULL);
1863 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001864
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001865 if (l1 != l2) {
1866 j = false;
1867 break;
1868 }
1869 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1870 j = false;
1871 break;
1872 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001873 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001874 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001875 break;
1876 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001877
H. Peter Anvine2c80182005-01-15 22:15:51 +00001878 t = t->next;
1879 tt = tt->next;
1880 }
1881 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001882 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001883 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001884
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001885 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001886 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001887 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001888 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001889
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001890 skip_white_(tline);
1891 tline = expand_id(tline);
1892 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001893 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001894 "`%s' expects a macro name", pp_directives[ct]);
1895 goto fail;
1896 }
1897 searching.name = nasm_strdup(tline->text);
1898 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001899 searching.plus = false;
1900 searching.nolist = false;
1901 searching.in_progress = 0;
1902 searching.max_depth = 0;
1903 searching.rep_nest = NULL;
1904 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001905 searching.nparam_max = INT_MAX;
1906 tline = expand_smacro(tline->next);
1907 skip_white_(tline);
1908 if (!tline) {
1909 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001910 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001911 "`%s' expects a parameter count or nothing",
1912 pp_directives[ct]);
1913 } else {
1914 searching.nparam_min = searching.nparam_max =
1915 readnum(tline->text, &j);
1916 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001917 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001918 "unable to parse parameter count `%s'",
1919 tline->text);
1920 }
1921 if (tline && tok_is_(tline->next, "-")) {
1922 tline = tline->next->next;
1923 if (tok_is_(tline, "*"))
1924 searching.nparam_max = INT_MAX;
1925 else if (!tok_type_(tline, TOK_NUMBER))
H. Peter Anvin130736c2016-02-17 20:27:41 -08001926 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001927 "`%s' expects a parameter count after `-'",
1928 pp_directives[ct]);
1929 else {
1930 searching.nparam_max = readnum(tline->text, &j);
1931 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001932 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001933 "unable to parse parameter count `%s'",
1934 tline->text);
1935 if (searching.nparam_min > searching.nparam_max)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001936 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001937 "minimum parameter count exceeds maximum");
1938 }
1939 }
1940 if (tline && tok_is_(tline->next, "+")) {
1941 tline = tline->next;
1942 searching.plus = true;
1943 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001944 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1945 while (mmac) {
1946 if (!strcmp(mmac->name, searching.name) &&
1947 (mmac->nparam_min <= searching.nparam_max
1948 || searching.plus)
1949 && (searching.nparam_min <= mmac->nparam_max
1950 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001951 found = true;
1952 break;
1953 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001954 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001955 }
1956 if (tline && tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001957 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001958 "trailing garbage after %%ifmacro ignored");
1959 nasm_free(searching.name);
1960 j = found;
1961 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001962 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001963
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001964 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001965 needtype = TOK_ID;
1966 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001967 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001968 needtype = TOK_NUMBER;
1969 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001970 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001971 needtype = TOK_STRING;
1972 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001973
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001974iftype:
1975 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001976
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001977 while (tok_type_(t, TOK_WHITESPACE) ||
1978 (needtype == TOK_NUMBER &&
1979 tok_type_(t, TOK_OTHER) &&
1980 (t->text[0] == '-' || t->text[0] == '+') &&
1981 !t->text[1]))
1982 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001983
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001984 j = tok_type_(t, needtype);
1985 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001986
1987 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001988 t = tline = expand_smacro(tline);
1989 while (tok_type_(t, TOK_WHITESPACE))
1990 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001991
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001992 j = false;
1993 if (t) {
1994 t = t->next; /* Skip the actual token */
1995 while (tok_type_(t, TOK_WHITESPACE))
1996 t = t->next;
1997 j = !t; /* Should be nothing left */
1998 }
1999 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002000
H. Peter Anvin134b9462008-02-16 17:01:40 -08002001 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002002 t = tline = expand_smacro(tline);
2003 while (tok_type_(t, TOK_WHITESPACE))
2004 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002005
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002006 j = !t; /* Should be empty */
2007 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002008
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002009 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002010 t = tline = expand_smacro(tline);
2011 tptr = &t;
2012 tokval.t_type = TOKEN_INVALID;
2013 evalresult = evaluate(ppscan, tptr, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -08002014 NULL, pass | CRITICAL, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002015 if (!evalresult)
2016 return -1;
2017 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002018 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002019 "trailing garbage after expression ignored");
2020 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002021 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002022 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002023 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002024 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002025 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002026 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002027
H. Peter Anvine2c80182005-01-15 22:15:51 +00002028 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002029 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002030 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002031 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002032 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002033 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002034
2035 free_tlist(origline);
2036 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002037
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002038fail:
2039 free_tlist(origline);
2040 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002041}
2042
2043/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002044 * Common code for defining an smacro
2045 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002046static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002047 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002048{
2049 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002050 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002051
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002052 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002053 if (!smac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002054 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002055 "single-line macro `%s' defined both with and"
2056 " without parameters", mname);
2057 /*
2058 * Some instances of the old code considered this a failure,
2059 * some others didn't. What is the right thing to do here?
2060 */
2061 free_tlist(expansion);
2062 return false; /* Failure */
2063 } else {
2064 /*
2065 * We're redefining, so we have to take over an
2066 * existing SMacro structure. This means freeing
2067 * what was already in it.
2068 */
2069 nasm_free(smac->name);
2070 free_tlist(smac->expansion);
2071 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002072 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002073 smtbl = ctx ? &ctx->localmac : &smacros;
2074 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002075 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002076 smac->next = *smhead;
2077 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002078 }
2079 smac->name = nasm_strdup(mname);
2080 smac->casesense = casesense;
2081 smac->nparam = nparam;
2082 smac->expansion = expansion;
2083 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002084 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002085}
2086
2087/*
2088 * Undefine an smacro
2089 */
2090static void undef_smacro(Context *ctx, const char *mname)
2091{
2092 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002093 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002094
H. Peter Anvin166c2472008-05-28 12:28:58 -07002095 smtbl = ctx ? &ctx->localmac : &smacros;
2096 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002097
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002098 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002099 /*
2100 * We now have a macro name... go hunt for it.
2101 */
2102 sp = smhead;
2103 while ((s = *sp) != NULL) {
2104 if (!mstrcmp(s->name, mname, s->casesense)) {
2105 *sp = s->next;
2106 nasm_free(s->name);
2107 free_tlist(s->expansion);
2108 nasm_free(s);
2109 } else {
2110 sp = &s->next;
2111 }
2112 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002113 }
2114}
2115
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002116/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002117 * Parse a mmacro specification.
2118 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002119static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002120{
2121 bool err;
2122
2123 tline = tline->next;
2124 skip_white_(tline);
2125 tline = expand_id(tline);
2126 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002127 nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002128 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002129 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002130
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002131 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002132 def->name = nasm_strdup(tline->text);
2133 def->plus = false;
2134 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002135 def->in_progress = 0;
2136 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002137 def->nparam_min = 0;
2138 def->nparam_max = 0;
2139
H. Peter Anvina26433d2008-07-16 14:40:01 -07002140 tline = expand_smacro(tline->next);
2141 skip_white_(tline);
2142 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002143 nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002144 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002145 def->nparam_min = def->nparam_max =
2146 readnum(tline->text, &err);
2147 if (err)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002148 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002149 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002150 }
2151 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002152 tline = tline->next->next;
2153 if (tok_is_(tline, "*")) {
2154 def->nparam_max = INT_MAX;
2155 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002156 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002157 "`%s' expects a parameter count after `-'", directive);
2158 } else {
2159 def->nparam_max = readnum(tline->text, &err);
2160 if (err) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002161 nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002162 tline->text);
2163 }
2164 if (def->nparam_min > def->nparam_max) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002165 nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002166 }
2167 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002168 }
2169 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002170 tline = tline->next;
2171 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002172 }
2173 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002174 !nasm_stricmp(tline->next->text, ".nolist")) {
2175 tline = tline->next;
2176 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002177 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002178
H. Peter Anvina26433d2008-07-16 14:40:01 -07002179 /*
2180 * Handle default parameters.
2181 */
2182 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002183 def->dlist = tline->next;
2184 tline->next = NULL;
2185 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002186 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002187 def->dlist = NULL;
2188 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002189 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002190 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002191
H. Peter Anvin89cee572009-07-15 09:16:54 -04002192 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002193 !def->plus)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002194 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002195 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002196
H. Peter Anvina26433d2008-07-16 14:40:01 -07002197 return true;
2198}
2199
2200
2201/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002202 * Decode a size directive
2203 */
2204static int parse_size(const char *str) {
2205 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002206 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002207 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002208 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002209
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002210 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002211}
2212
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002213/*
2214 * Process a preprocessor %pragma directive. Currently there are none.
2215 * Gets passed the token list starting with the "preproc" token from
2216 * "%pragma preproc".
2217 */
2218static void do_pragma_preproc(Token *tline)
2219{
2220 /* Skip to the real stuff */
2221 tline = tline->next;
2222 skip_white_(tline);
2223 if (!tline)
2224 return;
2225
2226 (void)tline; /* Nothing else to do at present */
2227}
2228
Ed Beroset3ab3f412002-06-11 03:31:49 +00002229/**
2230 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002231 * Find out if a line contains a preprocessor directive, and deal
2232 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002233 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002234 * If a directive _is_ found, it is the responsibility of this routine
2235 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002236 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002237 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002238 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002239 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002240 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002241 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002242static int do_directive(Token *tline, char **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002243{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002244 enum preproc_token i;
2245 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002246 bool err;
2247 int nparam;
2248 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002249 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002250 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002251 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002252 char *p, *pp;
2253 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002254 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002255 Include *inc;
2256 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002257 Cond *cond;
2258 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002259 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002260 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002261 struct tokenval tokval;
2262 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002263 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002264 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002265 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002266 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002267
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002268 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002269 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002270
H. Peter Anvineba20a72002-04-30 20:53:55 +00002271 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002272 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002273 (tline->text[1] == '%' || tline->text[1] == '$'
2274 || tline->text[1] == '!'))
2275 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002276
H. Peter Anvin4169a472007-09-12 01:29:43 +00002277 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002278
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002279 /*
2280 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2281 * since they are known to be buggy at moment, we need to fix them
2282 * in future release (2.09-2.10)
2283 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002284 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002285 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002286 tline->text);
2287 return NO_DIRECTIVE_FOUND;
2288 }
2289
2290 /*
2291 * If we're in a non-emitting branch of a condition construct,
2292 * or walking to the end of an already terminated %rep block,
2293 * we should ignore all directives except for condition
2294 * directives.
2295 */
2296 if (((istk->conds && !emitting(istk->conds->state)) ||
2297 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2298 return NO_DIRECTIVE_FOUND;
2299 }
2300
2301 /*
2302 * If we're defining a macro or reading a %rep block, we should
2303 * ignore all directives except for %macro/%imacro (which nest),
2304 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2305 * If we're in a %rep block, another %rep nests, so should be let through.
2306 */
2307 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2308 i != PP_RMACRO && i != PP_IRMACRO &&
2309 i != PP_ENDMACRO && i != PP_ENDM &&
2310 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2311 return NO_DIRECTIVE_FOUND;
2312 }
2313
2314 if (defining) {
2315 if (i == PP_MACRO || i == PP_IMACRO ||
2316 i == PP_RMACRO || i == PP_IRMACRO) {
2317 nested_mac_count++;
2318 return NO_DIRECTIVE_FOUND;
2319 } else if (nested_mac_count > 0) {
2320 if (i == PP_ENDMACRO) {
2321 nested_mac_count--;
2322 return NO_DIRECTIVE_FOUND;
2323 }
2324 }
2325 if (!defining->name) {
2326 if (i == PP_REP) {
2327 nested_rep_count++;
2328 return NO_DIRECTIVE_FOUND;
2329 } else if (nested_rep_count > 0) {
2330 if (i == PP_ENDREP) {
2331 nested_rep_count--;
2332 return NO_DIRECTIVE_FOUND;
2333 }
2334 }
2335 }
2336 }
2337
H. Peter Anvin4169a472007-09-12 01:29:43 +00002338 switch (i) {
2339 case PP_INVALID:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002340 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002341 tline->text);
2342 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002343
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002344 case PP_PRAGMA:
2345 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002346 * %pragma namespace options...
2347 *
2348 * The namespace "preproc" is reserved for the preprocessor;
2349 * all other namespaces generate a [pragma] assembly directive.
2350 *
2351 * Invalid %pragmas are ignored and may have different
2352 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002353 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002354 tline = tline->next;
2355 skip_white_(tline);
2356 tline = expand_smacro(tline);
2357 if (tok_type_(tline, TOK_ID)) {
2358 if (!nasm_stricmp(tline->text, "preproc")) {
2359 /* Preprocessor pragma */
2360 do_pragma_preproc(tline);
2361 } else {
2362 /* Build the assembler directive */
2363 t = new_Token(NULL, TOK_OTHER, "[", 1);
2364 t->next = new_Token(NULL, TOK_ID, "pragma", 6);
2365 t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2366 tline = t;
2367 for (t = tline; t->next; t = t->next)
2368 ;
2369 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
2370 /* true here can be revisited in the future */
2371 *output = detoken(tline, true);
2372 }
2373 }
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002374 free_tlist(origline);
2375 return DIRECTIVE_FOUND;
2376
H. Peter Anvine2c80182005-01-15 22:15:51 +00002377 case PP_STACKSIZE:
2378 /* Directive to tell NASM what the default stack size is. The
2379 * default is for a 16-bit stack, and this can be overriden with
2380 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002381 */
2382 tline = tline->next;
2383 if (tline && tline->type == TOK_WHITESPACE)
2384 tline = tline->next;
2385 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002386 nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002387 free_tlist(origline);
2388 return DIRECTIVE_FOUND;
2389 }
2390 if (nasm_stricmp(tline->text, "flat") == 0) {
2391 /* All subsequent ARG directives are for a 32-bit stack */
2392 StackSize = 4;
2393 StackPointer = "ebp";
2394 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002395 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002396 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2397 /* All subsequent ARG directives are for a 64-bit stack */
2398 StackSize = 8;
2399 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002400 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002401 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002402 } else if (nasm_stricmp(tline->text, "large") == 0) {
2403 /* All subsequent ARG directives are for a 16-bit stack,
2404 * far function call.
2405 */
2406 StackSize = 2;
2407 StackPointer = "bp";
2408 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002409 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002410 } else if (nasm_stricmp(tline->text, "small") == 0) {
2411 /* All subsequent ARG directives are for a 16-bit stack,
2412 * far function call. We don't support near functions.
2413 */
2414 StackSize = 2;
2415 StackPointer = "bp";
2416 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002417 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002418 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002419 nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002420 free_tlist(origline);
2421 return DIRECTIVE_FOUND;
2422 }
2423 free_tlist(origline);
2424 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002425
H. Peter Anvine2c80182005-01-15 22:15:51 +00002426 case PP_ARG:
2427 /* TASM like ARG directive to define arguments to functions, in
2428 * the following form:
2429 *
2430 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2431 */
2432 offset = ArgOffset;
2433 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002434 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002435 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002436
H. Peter Anvine2c80182005-01-15 22:15:51 +00002437 /* Find the argument name */
2438 tline = tline->next;
2439 if (tline && tline->type == TOK_WHITESPACE)
2440 tline = tline->next;
2441 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002442 nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002443 free_tlist(origline);
2444 return DIRECTIVE_FOUND;
2445 }
2446 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002447
H. Peter Anvine2c80182005-01-15 22:15:51 +00002448 /* Find the argument size type */
2449 tline = tline->next;
2450 if (!tline || tline->type != TOK_OTHER
2451 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002452 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002453 "Syntax error processing `%%arg' directive");
2454 free_tlist(origline);
2455 return DIRECTIVE_FOUND;
2456 }
2457 tline = tline->next;
2458 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002459 nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002460 free_tlist(origline);
2461 return DIRECTIVE_FOUND;
2462 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002463
H. Peter Anvine2c80182005-01-15 22:15:51 +00002464 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002465 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002466 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002467 size = parse_size(tt->text);
2468 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002469 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002470 "Invalid size type for `%%arg' missing directive");
2471 free_tlist(tt);
2472 free_tlist(origline);
2473 return DIRECTIVE_FOUND;
2474 }
2475 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002476
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002477 /* Round up to even stack slots */
2478 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002479
H. Peter Anvine2c80182005-01-15 22:15:51 +00002480 /* Now define the macro for the argument */
2481 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2482 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002483 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002484 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002485
H. Peter Anvine2c80182005-01-15 22:15:51 +00002486 /* Move to the next argument in the list */
2487 tline = tline->next;
2488 if (tline && tline->type == TOK_WHITESPACE)
2489 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002490 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002491 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002492 free_tlist(origline);
2493 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002494
H. Peter Anvine2c80182005-01-15 22:15:51 +00002495 case PP_LOCAL:
2496 /* TASM like LOCAL directive to define local variables for a
2497 * function, in the following form:
2498 *
2499 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2500 *
2501 * The '= LocalSize' at the end is ignored by NASM, but is
2502 * required by TASM to define the local parameter size (and used
2503 * by the TASM macro package).
2504 */
2505 offset = LocalOffset;
2506 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002507 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002508 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002509
H. Peter Anvine2c80182005-01-15 22:15:51 +00002510 /* Find the argument name */
2511 tline = tline->next;
2512 if (tline && tline->type == TOK_WHITESPACE)
2513 tline = tline->next;
2514 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002515 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002516 "`%%local' missing argument parameter");
2517 free_tlist(origline);
2518 return DIRECTIVE_FOUND;
2519 }
2520 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002521
H. Peter Anvine2c80182005-01-15 22:15:51 +00002522 /* Find the argument size type */
2523 tline = tline->next;
2524 if (!tline || tline->type != TOK_OTHER
2525 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002526 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002527 "Syntax error processing `%%local' directive");
2528 free_tlist(origline);
2529 return DIRECTIVE_FOUND;
2530 }
2531 tline = tline->next;
2532 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002533 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002534 "`%%local' missing size type parameter");
2535 free_tlist(origline);
2536 return DIRECTIVE_FOUND;
2537 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002538
H. Peter Anvine2c80182005-01-15 22:15:51 +00002539 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002540 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002541 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002542 size = parse_size(tt->text);
2543 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002544 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002545 "Invalid size type for `%%local' missing directive");
2546 free_tlist(tt);
2547 free_tlist(origline);
2548 return DIRECTIVE_FOUND;
2549 }
2550 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002551
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002552 /* Round up to even stack slots */
2553 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002554
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002555 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002556
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002557 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002558 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2559 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002560 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002561
H. Peter Anvine2c80182005-01-15 22:15:51 +00002562 /* Now define the assign to setup the enter_c macro correctly */
2563 snprintf(directive, sizeof(directive),
2564 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002565 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002566
H. Peter Anvine2c80182005-01-15 22:15:51 +00002567 /* Move to the next argument in the list */
2568 tline = tline->next;
2569 if (tline && tline->type == TOK_WHITESPACE)
2570 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002571 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002572 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002573 free_tlist(origline);
2574 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002575
H. Peter Anvine2c80182005-01-15 22:15:51 +00002576 case PP_CLEAR:
2577 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002578 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002579 "trailing garbage after `%%clear' ignored");
2580 free_macros();
2581 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002582 free_tlist(origline);
2583 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002584
H. Peter Anvin418ca702008-05-30 10:42:30 -07002585 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002586 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002587 skip_white_(t);
2588 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002589 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002590 nasm_error(ERR_NONFATAL, "`%%depend' expects a file name");
H. Peter Anvin418ca702008-05-30 10:42:30 -07002591 free_tlist(origline);
2592 return DIRECTIVE_FOUND; /* but we did _something_ */
2593 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002594 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002595 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002596 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002597 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002598 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002599 nasm_unquote_cstr(p, i);
H. Peter Anvin436e3672016-10-04 01:12:28 -07002600 nasm_add_string_to_strlist(dephead, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002601 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002602 return DIRECTIVE_FOUND;
2603
2604 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002605 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002606 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002607
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002608 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002609 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002610 nasm_error(ERR_NONFATAL, "`%%include' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002611 free_tlist(origline);
2612 return DIRECTIVE_FOUND; /* but we did _something_ */
2613 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002614 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002615 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002616 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002617 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002618 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002619 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002620 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002621 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002622 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002623 found_path = NULL;
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002624 inc->fp = inc_fopen(p, dephead, &found_path,
H. Peter Anvind81a2352016-09-21 14:03:18 -07002625 pass == 0 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002626 if (!inc->fp) {
2627 /* -MG given but file not found */
2628 nasm_free(inc);
2629 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002630 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002631 inc->lineno = src_set_linnum(0);
2632 inc->lineinc = 1;
2633 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002634 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002635 istk = inc;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002636 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002637 }
2638 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002639 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002640
H. Peter Anvind2456592008-06-19 15:04:18 -07002641 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002642 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002643 static macros_t *use_pkg;
2644 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002645
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002646 tline = tline->next;
2647 skip_white_(tline);
2648 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002649
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002650 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002651 tline->type != TOK_INTERNAL_STRING &&
2652 tline->type != TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002653 nasm_error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002654 free_tlist(origline);
2655 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002656 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002657 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002658 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002659 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002660 if (tline->type == TOK_STRING)
2661 nasm_unquote_cstr(tline->text, i);
2662 use_pkg = nasm_stdmac_find_package(tline->text);
2663 if (!use_pkg)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002664 nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002665 else
2666 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002667 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002668 /* Not already included, go ahead and include it */
2669 stdmacpos = use_pkg;
2670 }
2671 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002672 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002673 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002674 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002675 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002676 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002677 tline = tline->next;
2678 skip_white_(tline);
2679 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002680 if (tline) {
2681 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002682 nasm_error(ERR_NONFATAL, "`%s' expects a context identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002683 pp_directives[i]);
2684 free_tlist(origline);
2685 return DIRECTIVE_FOUND; /* but we did _something_ */
2686 }
2687 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002688 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002689 "trailing garbage after `%s' ignored",
2690 pp_directives[i]);
2691 p = nasm_strdup(tline->text);
2692 } else {
2693 p = NULL; /* Anonymous */
2694 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002695
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002696 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002697 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002698 ctx->next = cstk;
2699 hash_init(&ctx->localmac, HASH_SMALL);
2700 ctx->name = p;
2701 ctx->number = unique++;
2702 cstk = ctx;
2703 } else {
2704 /* %pop or %repl */
2705 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002706 nasm_error(ERR_NONFATAL, "`%s': context stack is empty",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002707 pp_directives[i]);
2708 } else if (i == PP_POP) {
2709 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08002710 nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002711 "expected %s",
2712 cstk->name ? cstk->name : "anonymous", p);
2713 else
2714 ctx_pop();
2715 } else {
2716 /* i == PP_REPL */
2717 nasm_free(cstk->name);
2718 cstk->name = p;
2719 p = NULL;
2720 }
2721 nasm_free(p);
2722 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002723 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002724 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002725 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002726 severity = ERR_FATAL;
2727 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002728 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002729 severity = ERR_NONFATAL;
2730 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002731 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002732 severity = ERR_WARNING|ERR_WARN_USER;
2733 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002734
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002735issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002736 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002737 /* Only error out if this is the final pass */
2738 if (pass != 2 && i != PP_FATAL)
2739 return DIRECTIVE_FOUND;
2740
2741 tline->next = expand_smacro(tline->next);
2742 tline = tline->next;
2743 skip_white_(tline);
2744 t = tline ? tline->next : NULL;
2745 skip_white_(t);
2746 if (tok_type_(tline, TOK_STRING) && !t) {
2747 /* The line contains only a quoted string */
2748 p = tline->text;
2749 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002750 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002751 } else {
2752 /* Not a quoted string, or more than a quoted string */
2753 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002754 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002755 nasm_free(p);
2756 }
2757 free_tlist(origline);
2758 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002759 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002760
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002761 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002762 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002763 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002764 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002765 j = if_condition(tline->next, i);
2766 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002767 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002768 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002769 cond = nasm_malloc(sizeof(Cond));
2770 cond->next = istk->conds;
2771 cond->state = j;
2772 istk->conds = cond;
2773 if(istk->mstk)
2774 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002775 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002776 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002777
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002778 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002779 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002780 nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002781 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002782 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002783 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002784 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002785
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002786 case COND_DONE:
2787 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002788 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002789
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002790 case COND_ELSE_TRUE:
2791 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002792 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2793 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002794 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002795 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002796
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002797 case COND_IF_FALSE:
2798 /*
2799 * IMPORTANT: In the case of %if, we will already have
2800 * called expand_mmac_params(); however, if we're
2801 * processing an %elif we must have been in a
2802 * non-emitting mode, which would have inhibited
2803 * the normal invocation of expand_mmac_params().
2804 * Therefore, we have to do it explicitly here.
2805 */
2806 j = if_condition(expand_mmac_params(tline->next), i);
2807 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002808 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002809 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2810 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002811 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002812 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002813 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002814
H. Peter Anvine2c80182005-01-15 22:15:51 +00002815 case PP_ELSE:
2816 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002817 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2818 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002819 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002820 nasm_fatal(0, "`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002821 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002822 case COND_IF_TRUE:
2823 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002824 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002825 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002826
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002827 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002828 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002829
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002830 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002831 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002832 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002833
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002834 case COND_ELSE_TRUE:
2835 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002836 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002837 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002838 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002839 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002840 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002841 free_tlist(origline);
2842 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002843
H. Peter Anvine2c80182005-01-15 22:15:51 +00002844 case PP_ENDIF:
2845 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002846 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2847 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002848 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002849 nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002850 cond = istk->conds;
2851 istk->conds = cond->next;
2852 nasm_free(cond);
2853 if(istk->mstk)
2854 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002855 free_tlist(origline);
2856 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002857
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002858 case PP_RMACRO:
2859 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002860 case PP_MACRO:
2861 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002862 if (defining) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002863 nasm_error(ERR_FATAL, "`%s': already defining a macro",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002864 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002865 return DIRECTIVE_FOUND;
2866 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002867 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002868 defining->max_depth =
2869 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2870 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2871 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2872 nasm_free(defining);
2873 defining = NULL;
2874 return DIRECTIVE_FOUND;
2875 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002876
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002877 src_get(&defining->xline, &defining->fname);
2878
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002879 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2880 while (mmac) {
2881 if (!strcmp(mmac->name, defining->name) &&
2882 (mmac->nparam_min <= defining->nparam_max
2883 || defining->plus)
2884 && (defining->nparam_min <= mmac->nparam_max
2885 || mmac->plus)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002886 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002887 "redefining multi-line macro `%s'", defining->name);
2888 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002889 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002890 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002891 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002892 free_tlist(origline);
2893 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002894
H. Peter Anvine2c80182005-01-15 22:15:51 +00002895 case PP_ENDM:
2896 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002897 if (! (defining && defining->name)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002898 nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002899 return DIRECTIVE_FOUND;
2900 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002901 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2902 defining->next = *mmhead;
2903 *mmhead = defining;
2904 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002905 free_tlist(origline);
2906 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002907
H. Peter Anvin89cee572009-07-15 09:16:54 -04002908 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002909 /*
2910 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002911 * macro-end marker for a macro with a name. Then we
2912 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002913 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002914 list_for_each(l, istk->expansion)
2915 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002916 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002917
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002918 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002919 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002920 * Remove all conditional entries relative to this
2921 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002922 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002923 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2924 cond = istk->conds;
2925 istk->conds = cond->next;
2926 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002927 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002928 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002929 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002930 nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002931 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002932 free_tlist(origline);
2933 return DIRECTIVE_FOUND;
2934
H. Peter Anvina26433d2008-07-16 14:40:01 -07002935 case PP_UNMACRO:
2936 case PP_UNIMACRO:
2937 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002938 MMacro **mmac_p;
2939 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002940
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002941 spec.casesense = (i == PP_UNMACRO);
2942 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2943 return DIRECTIVE_FOUND;
2944 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002945 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2946 while (mmac_p && *mmac_p) {
2947 mmac = *mmac_p;
2948 if (mmac->casesense == spec.casesense &&
2949 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2950 mmac->nparam_min == spec.nparam_min &&
2951 mmac->nparam_max == spec.nparam_max &&
2952 mmac->plus == spec.plus) {
2953 *mmac_p = mmac->next;
2954 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002955 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002956 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002957 }
2958 }
2959 free_tlist(origline);
2960 free_tlist(spec.dlist);
2961 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002962 }
2963
H. Peter Anvine2c80182005-01-15 22:15:51 +00002964 case PP_ROTATE:
2965 if (tline->next && tline->next->type == TOK_WHITESPACE)
2966 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002967 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002968 free_tlist(origline);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002969 nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002970 return DIRECTIVE_FOUND;
2971 }
2972 t = expand_smacro(tline->next);
2973 tline->next = NULL;
2974 free_tlist(origline);
2975 tline = t;
2976 tptr = &t;
2977 tokval.t_type = TOKEN_INVALID;
2978 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002979 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002980 free_tlist(tline);
2981 if (!evalresult)
2982 return DIRECTIVE_FOUND;
2983 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002984 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002985 "trailing garbage after expression ignored");
2986 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002987 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002988 return DIRECTIVE_FOUND;
2989 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002990 mmac = istk->mstk;
2991 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2992 mmac = mmac->next_active;
2993 if (!mmac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002994 nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002995 } else if (mmac->nparam == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002996 nasm_error(ERR_NONFATAL,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002997 "`%%rotate' invoked within macro without parameters");
2998 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002999 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003000
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003001 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003002 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003003 rotate += mmac->nparam;
3004
3005 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003006 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003007 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003008
H. Peter Anvine2c80182005-01-15 22:15:51 +00003009 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003010 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003011 do {
3012 tline = tline->next;
3013 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003014
H. Peter Anvine2c80182005-01-15 22:15:51 +00003015 if (tok_type_(tline, TOK_ID) &&
3016 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003017 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003018 do {
3019 tline = tline->next;
3020 } while (tok_type_(tline, TOK_WHITESPACE));
3021 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003022
H. Peter Anvine2c80182005-01-15 22:15:51 +00003023 if (tline) {
3024 t = expand_smacro(tline);
3025 tptr = &t;
3026 tokval.t_type = TOKEN_INVALID;
3027 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08003028 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003029 if (!evalresult) {
3030 free_tlist(origline);
3031 return DIRECTIVE_FOUND;
3032 }
3033 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003034 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003035 "trailing garbage after expression ignored");
3036 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003037 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003038 return DIRECTIVE_FOUND;
3039 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003040 count = reloc_value(evalresult);
3041 if (count >= REP_LIMIT) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003042 nasm_error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003043 count = 0;
3044 } else
3045 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003046 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003047 nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003048 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003049 }
3050 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003051
3052 tmp_defining = defining;
3053 defining = nasm_malloc(sizeof(MMacro));
3054 defining->prev = NULL;
3055 defining->name = NULL; /* flags this macro as a %rep block */
3056 defining->casesense = false;
3057 defining->plus = false;
3058 defining->nolist = nolist;
3059 defining->in_progress = count;
3060 defining->max_depth = 0;
3061 defining->nparam_min = defining->nparam_max = 0;
3062 defining->defaults = NULL;
3063 defining->dlist = NULL;
3064 defining->expansion = NULL;
3065 defining->next_active = istk->mstk;
3066 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003067 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003068
H. Peter Anvine2c80182005-01-15 22:15:51 +00003069 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003070 if (!defining || defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003071 nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003072 return DIRECTIVE_FOUND;
3073 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003074
H. Peter Anvine2c80182005-01-15 22:15:51 +00003075 /*
3076 * Now we have a "macro" defined - although it has no name
3077 * and we won't be entering it in the hash tables - we must
3078 * push a macro-end marker for it on to istk->expansion.
3079 * After that, it will take care of propagating itself (a
3080 * macro-end marker line for a macro which is really a %rep
3081 * block will cause the macro to be re-expanded, complete
3082 * with another macro-end marker to ensure the process
3083 * continues) until the whole expansion is forcibly removed
3084 * from istk->expansion by a %exitrep.
3085 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003086 l = nasm_malloc(sizeof(Line));
3087 l->next = istk->expansion;
3088 l->finishes = defining;
3089 l->first = NULL;
3090 istk->expansion = l;
3091
3092 istk->mstk = defining;
3093
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08003094 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003095 tmp_defining = defining;
3096 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003097 free_tlist(origline);
3098 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003099
H. Peter Anvine2c80182005-01-15 22:15:51 +00003100 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003101 /*
3102 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003103 * macro-end marker for a macro with no name. Then we set
3104 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003105 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003106 list_for_each(l, istk->expansion)
3107 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003108 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003109
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003110 if (l)
3111 l->finishes->in_progress = 1;
3112 else
H. Peter Anvin130736c2016-02-17 20:27:41 -08003113 nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003114 free_tlist(origline);
3115 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003116
H. Peter Anvine2c80182005-01-15 22:15:51 +00003117 case PP_XDEFINE:
3118 case PP_IXDEFINE:
3119 case PP_DEFINE:
3120 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003121 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003122
H. Peter Anvine2c80182005-01-15 22:15:51 +00003123 tline = tline->next;
3124 skip_white_(tline);
3125 tline = expand_id(tline);
3126 if (!tline || (tline->type != TOK_ID &&
3127 (tline->type != TOK_PREPROC_ID ||
3128 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003129 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003130 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003131 free_tlist(origline);
3132 return DIRECTIVE_FOUND;
3133 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003134
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003135 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003136 last = tline;
3137 param_start = tline = tline->next;
3138 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003139
H. Peter Anvine2c80182005-01-15 22:15:51 +00003140 /* Expand the macro definition now for %xdefine and %ixdefine */
3141 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3142 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003143
H. Peter Anvine2c80182005-01-15 22:15:51 +00003144 if (tok_is_(tline, "(")) {
3145 /*
3146 * This macro has parameters.
3147 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003148
H. Peter Anvine2c80182005-01-15 22:15:51 +00003149 tline = tline->next;
3150 while (1) {
3151 skip_white_(tline);
3152 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003153 nasm_error(ERR_NONFATAL, "parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003154 free_tlist(origline);
3155 return DIRECTIVE_FOUND;
3156 }
3157 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003158 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003159 "`%s': parameter identifier expected",
3160 tline->text);
3161 free_tlist(origline);
3162 return DIRECTIVE_FOUND;
3163 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003164 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003165 tline = tline->next;
3166 skip_white_(tline);
3167 if (tok_is_(tline, ",")) {
3168 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003169 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003170 if (!tok_is_(tline, ")")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003171 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003172 "`)' expected to terminate macro template");
3173 free_tlist(origline);
3174 return DIRECTIVE_FOUND;
3175 }
3176 break;
3177 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003178 }
3179 last = tline;
3180 tline = tline->next;
3181 }
3182 if (tok_type_(tline, TOK_WHITESPACE))
3183 last = tline, tline = tline->next;
3184 macro_start = NULL;
3185 last->next = NULL;
3186 t = tline;
3187 while (t) {
3188 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003189 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003190 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003191 !strcmp(tt->text, t->text))
3192 t->type = tt->type;
3193 }
3194 tt = t->next;
3195 t->next = macro_start;
3196 macro_start = t;
3197 t = tt;
3198 }
3199 /*
3200 * Good. We now have a macro name, a parameter count, and a
3201 * token list (in reverse order) for an expansion. We ought
3202 * to be OK just to create an SMacro, store it, and let
3203 * free_tlist have the rest of the line (which we have
3204 * carefully re-terminated after chopping off the expansion
3205 * from the end).
3206 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003207 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003208 free_tlist(origline);
3209 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003210
H. Peter Anvine2c80182005-01-15 22:15:51 +00003211 case PP_UNDEF:
3212 tline = tline->next;
3213 skip_white_(tline);
3214 tline = expand_id(tline);
3215 if (!tline || (tline->type != TOK_ID &&
3216 (tline->type != TOK_PREPROC_ID ||
3217 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003218 nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003219 free_tlist(origline);
3220 return DIRECTIVE_FOUND;
3221 }
3222 if (tline->next) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003223 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003224 "trailing garbage after macro name ignored");
3225 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003226
H. Peter Anvine2c80182005-01-15 22:15:51 +00003227 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003228 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003229 undef_smacro(ctx, mname);
3230 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003231 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003232
H. Peter Anvin9e200162008-06-04 17:23:14 -07003233 case PP_DEFSTR:
3234 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003235 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003236
3237 tline = tline->next;
3238 skip_white_(tline);
3239 tline = expand_id(tline);
3240 if (!tline || (tline->type != TOK_ID &&
3241 (tline->type != TOK_PREPROC_ID ||
3242 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003243 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003244 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003245 free_tlist(origline);
3246 return DIRECTIVE_FOUND;
3247 }
3248
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003249 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003250 last = tline;
3251 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003252 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003253
3254 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003255 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003256
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003257 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003258 macro_start = nasm_malloc(sizeof(*macro_start));
3259 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003260 macro_start->text = nasm_quote(p, strlen(p));
3261 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003262 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003263 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003264
3265 /*
3266 * We now have a macro name, an implicit parameter count of
3267 * zero, and a string token to use as an expansion. Create
3268 * and store an SMacro.
3269 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003270 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003271 free_tlist(origline);
3272 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003273
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003274 case PP_DEFTOK:
3275 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003276 casesense = (i == PP_DEFTOK);
3277
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003278 tline = tline->next;
3279 skip_white_(tline);
3280 tline = expand_id(tline);
3281 if (!tline || (tline->type != TOK_ID &&
3282 (tline->type != TOK_PREPROC_ID ||
3283 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003284 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003285 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003286 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003287 free_tlist(origline);
3288 return DIRECTIVE_FOUND;
3289 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003290 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003291 last = tline;
3292 tline = expand_smacro(tline->next);
3293 last->next = NULL;
3294
3295 t = tline;
3296 while (tok_type_(t, TOK_WHITESPACE))
3297 t = t->next;
3298 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003299 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003300 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003301 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003302 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003303 free_tlist(tline);
3304 free_tlist(origline);
3305 return DIRECTIVE_FOUND;
3306 }
3307
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003308 /*
3309 * Convert the string to a token stream. Note that smacros
3310 * are stored with the token stream reversed, so we have to
3311 * reverse the output of tokenize().
3312 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003313 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003314 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003315
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003316 /*
3317 * We now have a macro name, an implicit parameter count of
3318 * zero, and a numeric token to use as an expansion. Create
3319 * and store an SMacro.
3320 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003321 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003322 free_tlist(tline);
3323 free_tlist(origline);
3324 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003325
H. Peter Anvin418ca702008-05-30 10:42:30 -07003326 case PP_PATHSEARCH:
3327 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003328 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003329
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003330 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003331
3332 tline = tline->next;
3333 skip_white_(tline);
3334 tline = expand_id(tline);
3335 if (!tline || (tline->type != TOK_ID &&
3336 (tline->type != TOK_PREPROC_ID ||
3337 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003338 nasm_error(ERR_NONFATAL,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003339 "`%%pathsearch' expects a macro identifier as first parameter");
3340 free_tlist(origline);
3341 return DIRECTIVE_FOUND;
3342 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003343 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003344 last = tline;
3345 tline = expand_smacro(tline->next);
3346 last->next = NULL;
3347
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003348 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003349 while (tok_type_(t, TOK_WHITESPACE))
3350 t = t->next;
3351
3352 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003353 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003354 nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003355 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003356 free_tlist(origline);
3357 return DIRECTIVE_FOUND; /* but we did _something_ */
3358 }
3359 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003360 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003361 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003362 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003363 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003364 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003365
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003366 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003367 if (!found_path)
3368 found_path = p;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003369 macro_start = nasm_malloc(sizeof(*macro_start));
3370 macro_start->next = NULL;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003371 macro_start->text = nasm_quote(found_path, strlen(found_path));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003372 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003373 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003374
3375 /*
3376 * We now have a macro name, an implicit parameter count of
3377 * zero, and a string token to use as an expansion. Create
3378 * and store an SMacro.
3379 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003380 define_smacro(ctx, mname, casesense, 0, macro_start);
3381 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003382 free_tlist(origline);
3383 return DIRECTIVE_FOUND;
3384 }
3385
H. Peter Anvine2c80182005-01-15 22:15:51 +00003386 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003387 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003388
H. Peter Anvine2c80182005-01-15 22:15:51 +00003389 tline = tline->next;
3390 skip_white_(tline);
3391 tline = expand_id(tline);
3392 if (!tline || (tline->type != TOK_ID &&
3393 (tline->type != TOK_PREPROC_ID ||
3394 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003395 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003396 "`%%strlen' expects a macro identifier as first parameter");
3397 free_tlist(origline);
3398 return DIRECTIVE_FOUND;
3399 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003400 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003401 last = tline;
3402 tline = expand_smacro(tline->next);
3403 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003404
H. Peter Anvine2c80182005-01-15 22:15:51 +00003405 t = tline;
3406 while (tok_type_(t, TOK_WHITESPACE))
3407 t = t->next;
3408 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003409 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003410 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003411 "`%%strlen` requires string as second parameter");
3412 free_tlist(tline);
3413 free_tlist(origline);
3414 return DIRECTIVE_FOUND;
3415 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003416
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003417 macro_start = nasm_malloc(sizeof(*macro_start));
3418 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003419 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003420 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003421
H. Peter Anvine2c80182005-01-15 22:15:51 +00003422 /*
3423 * We now have a macro name, an implicit parameter count of
3424 * zero, and a numeric token to use as an expansion. Create
3425 * and store an SMacro.
3426 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003427 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003428 free_tlist(tline);
3429 free_tlist(origline);
3430 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003431
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003432 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003433 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003434
3435 tline = tline->next;
3436 skip_white_(tline);
3437 tline = expand_id(tline);
3438 if (!tline || (tline->type != TOK_ID &&
3439 (tline->type != TOK_PREPROC_ID ||
3440 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003441 nasm_error(ERR_NONFATAL,
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003442 "`%%strcat' expects a macro identifier as first parameter");
3443 free_tlist(origline);
3444 return DIRECTIVE_FOUND;
3445 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003446 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003447 last = tline;
3448 tline = expand_smacro(tline->next);
3449 last->next = NULL;
3450
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003451 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003452 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003453 switch (t->type) {
3454 case TOK_WHITESPACE:
3455 break;
3456 case TOK_STRING:
3457 len += t->a.len = nasm_unquote(t->text, NULL);
3458 break;
3459 case TOK_OTHER:
3460 if (!strcmp(t->text, ",")) /* permit comma separators */
3461 break;
3462 /* else fall through */
3463 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003464 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003465 "non-string passed to `%%strcat' (%d)", t->type);
3466 free_tlist(tline);
3467 free_tlist(origline);
3468 return DIRECTIVE_FOUND;
3469 }
3470 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003471
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003472 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003473 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003474 if (t->type == TOK_STRING) {
3475 memcpy(p, t->text, t->a.len);
3476 p += t->a.len;
3477 }
3478 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003479
3480 /*
3481 * We now have a macro name, an implicit parameter count of
3482 * zero, and a numeric token to use as an expansion. Create
3483 * and store an SMacro.
3484 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003485 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3486 macro_start->text = nasm_quote(pp, len);
3487 nasm_free(pp);
3488 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003489 free_tlist(tline);
3490 free_tlist(origline);
3491 return DIRECTIVE_FOUND;
3492
H. Peter Anvine2c80182005-01-15 22:15:51 +00003493 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003494 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003495 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003496 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003497
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003498 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003499
H. Peter Anvine2c80182005-01-15 22:15:51 +00003500 tline = tline->next;
3501 skip_white_(tline);
3502 tline = expand_id(tline);
3503 if (!tline || (tline->type != TOK_ID &&
3504 (tline->type != TOK_PREPROC_ID ||
3505 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003506 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003507 "`%%substr' expects a macro identifier as first parameter");
3508 free_tlist(origline);
3509 return DIRECTIVE_FOUND;
3510 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003511 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003512 last = tline;
3513 tline = expand_smacro(tline->next);
3514 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003515
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003516 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003517 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003518 while (tok_type_(t, TOK_WHITESPACE))
3519 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003520
H. Peter Anvine2c80182005-01-15 22:15:51 +00003521 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003522 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003523 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003524 "`%%substr` requires string as second parameter");
3525 free_tlist(tline);
3526 free_tlist(origline);
3527 return DIRECTIVE_FOUND;
3528 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003529
H. Peter Anvine2c80182005-01-15 22:15:51 +00003530 tt = t->next;
3531 tptr = &tt;
3532 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003533 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003534 if (!evalresult) {
3535 free_tlist(tline);
3536 free_tlist(origline);
3537 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003538 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003539 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003540 free_tlist(tline);
3541 free_tlist(origline);
3542 return DIRECTIVE_FOUND;
3543 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003544 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003545
3546 while (tok_type_(tt, TOK_WHITESPACE))
3547 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003548 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003549 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003550 } else {
3551 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003552 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003553 if (!evalresult) {
3554 free_tlist(tline);
3555 free_tlist(origline);
3556 return DIRECTIVE_FOUND;
3557 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003558 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003559 free_tlist(tline);
3560 free_tlist(origline);
3561 return DIRECTIVE_FOUND;
3562 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003563 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003564 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003565
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003566 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003567
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003568 /* make start and count being in range */
3569 if (start < 0)
3570 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003571 if (count < 0)
3572 count = len + count + 1 - start;
3573 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003574 count = len - start;
3575 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003576 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003577
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003578 macro_start = nasm_malloc(sizeof(*macro_start));
3579 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003580 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003581 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003582 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003583
H. Peter Anvine2c80182005-01-15 22:15:51 +00003584 /*
3585 * We now have a macro name, an implicit parameter count of
3586 * zero, and a numeric token to use as an expansion. Create
3587 * and store an SMacro.
3588 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003589 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003590 free_tlist(tline);
3591 free_tlist(origline);
3592 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003593 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003594
H. Peter Anvine2c80182005-01-15 22:15:51 +00003595 case PP_ASSIGN:
3596 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003597 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003598
H. Peter Anvine2c80182005-01-15 22:15:51 +00003599 tline = tline->next;
3600 skip_white_(tline);
3601 tline = expand_id(tline);
3602 if (!tline || (tline->type != TOK_ID &&
3603 (tline->type != TOK_PREPROC_ID ||
3604 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003605 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003606 "`%%%sassign' expects a macro identifier",
3607 (i == PP_IASSIGN ? "i" : ""));
3608 free_tlist(origline);
3609 return DIRECTIVE_FOUND;
3610 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003611 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003612 last = tline;
3613 tline = expand_smacro(tline->next);
3614 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003615
H. Peter Anvine2c80182005-01-15 22:15:51 +00003616 t = tline;
3617 tptr = &t;
3618 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003619 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003620 free_tlist(tline);
3621 if (!evalresult) {
3622 free_tlist(origline);
3623 return DIRECTIVE_FOUND;
3624 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003625
H. Peter Anvine2c80182005-01-15 22:15:51 +00003626 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003627 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003628 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003629
H. Peter Anvine2c80182005-01-15 22:15:51 +00003630 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003631 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003632 "non-constant value given to `%%%sassign'",
3633 (i == PP_IASSIGN ? "i" : ""));
3634 free_tlist(origline);
3635 return DIRECTIVE_FOUND;
3636 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003637
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003638 macro_start = nasm_malloc(sizeof(*macro_start));
3639 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003640 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003641 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003642
H. Peter Anvine2c80182005-01-15 22:15:51 +00003643 /*
3644 * We now have a macro name, an implicit parameter count of
3645 * zero, and a numeric token to use as an expansion. Create
3646 * and store an SMacro.
3647 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003648 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003649 free_tlist(origline);
3650 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003651
H. Peter Anvine2c80182005-01-15 22:15:51 +00003652 case PP_LINE:
3653 /*
3654 * Syntax is `%line nnn[+mmm] [filename]'
3655 */
3656 tline = tline->next;
3657 skip_white_(tline);
3658 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003659 nasm_error(ERR_NONFATAL, "`%%line' expects line number");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003660 free_tlist(origline);
3661 return DIRECTIVE_FOUND;
3662 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003663 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003664 m = 1;
3665 tline = tline->next;
3666 if (tok_is_(tline, "+")) {
3667 tline = tline->next;
3668 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003669 nasm_error(ERR_NONFATAL, "`%%line' expects line increment");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003670 free_tlist(origline);
3671 return DIRECTIVE_FOUND;
3672 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003673 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003674 tline = tline->next;
3675 }
3676 skip_white_(tline);
3677 src_set_linnum(k);
3678 istk->lineinc = m;
3679 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003680 char *fname = detoken(tline, false);
3681 src_set_fname(fname);
3682 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003683 }
3684 free_tlist(origline);
3685 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003686
H. Peter Anvine2c80182005-01-15 22:15:51 +00003687 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003688 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003689 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003690 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003691 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003692 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003693}
3694
3695/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003696 * Ensure that a macro parameter contains a condition code and
3697 * nothing else. Return the condition code index if so, or -1
3698 * otherwise.
3699 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003700static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003701{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003702 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003703
H. Peter Anvin25a99342007-09-22 17:45:45 -07003704 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003705 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003706
H. Peter Anvineba20a72002-04-30 20:53:55 +00003707 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003708 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003709 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003710 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003711 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003712 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003713 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003714
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003715 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003716}
3717
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003718/*
3719 * This routines walks over tokens strem and hadnles tokens
3720 * pasting, if @handle_explicit passed then explicit pasting
3721 * term is handled, otherwise -- implicit pastings only.
3722 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003723static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003724 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003725{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003726 Token *tok, *next, **prev_next, **prev_nonspace;
3727 bool pasted = false;
3728 char *buf, *p;
3729 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003730
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003731 /*
3732 * The last token before pasting. We need it
3733 * to be able to connect new handled tokens.
3734 * In other words if there were a tokens stream
3735 *
3736 * A -> B -> C -> D
3737 *
3738 * and we've joined tokens B and C, the resulting
3739 * stream should be
3740 *
3741 * A -> BC -> D
3742 */
3743 tok = *head;
3744 prev_next = NULL;
3745
3746 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3747 prev_nonspace = head;
3748 else
3749 prev_nonspace = NULL;
3750
3751 while (tok && (next = tok->next)) {
3752
3753 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003754 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003755 /* Zap redundant whitespaces */
3756 while (tok_type_(next, TOK_WHITESPACE))
3757 next = delete_Token(next);
3758 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003759 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003760
3761 case TOK_PASTE:
3762 /* Explicit pasting */
3763 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003764 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003765 next = delete_Token(tok);
3766
3767 while (tok_type_(next, TOK_WHITESPACE))
3768 next = delete_Token(next);
3769
3770 if (!pasted)
3771 pasted = true;
3772
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003773 /* Left pasting token is start of line */
3774 if (!prev_nonspace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003775 nasm_error(ERR_FATAL, "No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003776
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003777 /*
3778 * No ending token, this might happen in two
3779 * cases
3780 *
3781 * 1) There indeed no right token at all
3782 * 2) There is a bare "%define ID" statement,
3783 * and @ID does expand to whitespace.
3784 *
3785 * So technically we need to do a grammar analysis
3786 * in another stage of parsing, but for now lets don't
3787 * change the behaviour people used to. Simply allow
3788 * whitespace after paste token.
3789 */
3790 if (!next) {
3791 /*
3792 * Zap ending space tokens and that's all.
3793 */
3794 tok = (*prev_nonspace)->next;
3795 while (tok_type_(tok, TOK_WHITESPACE))
3796 tok = delete_Token(tok);
3797 tok = *prev_nonspace;
3798 tok->next = NULL;
3799 break;
3800 }
3801
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003802 tok = *prev_nonspace;
3803 while (tok_type_(tok, TOK_WHITESPACE))
3804 tok = delete_Token(tok);
3805 len = strlen(tok->text);
3806 len += strlen(next->text);
3807
3808 p = buf = nasm_malloc(len + 1);
3809 strcpy(p, tok->text);
3810 p = strchr(p, '\0');
3811 strcpy(p, next->text);
3812
3813 delete_Token(tok);
3814
3815 tok = tokenize(buf);
3816 nasm_free(buf);
3817
3818 *prev_nonspace = tok;
3819 while (tok && tok->next)
3820 tok = tok->next;
3821
3822 tok->next = delete_Token(next);
3823
3824 /* Restart from pasted tokens head */
3825 tok = *prev_nonspace;
3826 break;
3827
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003828 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003829 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003830 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003831 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3832 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003833
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003834 len = 0;
3835 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3836 len += strlen(next->text);
3837 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003838 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003839
3840 /* No match */
3841 if (tok == next)
3842 break;
3843
3844 len += strlen(tok->text);
3845 p = buf = nasm_malloc(len + 1);
3846
3847 while (tok != next) {
3848 strcpy(p, tok->text);
3849 p = strchr(p, '\0');
3850 tok = delete_Token(tok);
3851 }
3852
3853 tok = tokenize(buf);
3854 nasm_free(buf);
3855
3856 if (prev_next)
3857 *prev_next = tok;
3858 else
3859 *head = tok;
3860
3861 /*
3862 * Connect pasted into original stream,
3863 * ie A -> new-tokens -> B
3864 */
3865 while (tok && tok->next)
3866 tok = tok->next;
3867 tok->next = next;
3868
3869 if (!pasted)
3870 pasted = true;
3871
3872 /* Restart from pasted tokens head */
3873 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003874 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003875
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003876 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003877 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003878
3879 prev_next = &tok->next;
3880
3881 if (tok->next &&
3882 !tok_type_(tok->next, TOK_WHITESPACE) &&
3883 !tok_type_(tok->next, TOK_PASTE))
3884 prev_nonspace = prev_next;
3885
3886 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003887 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003888
3889 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003890}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003891
3892/*
3893 * expands to a list of tokens from %{x:y}
3894 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003895static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003896{
3897 Token *t = tline, **tt, *tm, *head;
3898 char *pos;
3899 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003900
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003901 pos = strchr(tline->text, ':');
3902 nasm_assert(pos);
3903
3904 lst = atoi(pos + 1);
3905 fst = atoi(tline->text + 1);
3906
3907 /*
3908 * only macros params are accounted so
3909 * if someone passes %0 -- we reject such
3910 * value(s)
3911 */
3912 if (lst == 0 || fst == 0)
3913 goto err;
3914
3915 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003916 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3917 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003918 goto err;
3919
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003920 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3921 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003922
3923 /* counted from zero */
3924 fst--, lst--;
3925
3926 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003927 * It will be at least one token. Note we
3928 * need to scan params until separator, otherwise
3929 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003930 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003931 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003932 head = new_Token(NULL, tm->type, tm->text, 0);
3933 tt = &head->next, tm = tm->next;
3934 while (tok_isnt_(tm, ",")) {
3935 t = new_Token(NULL, tm->type, tm->text, 0);
3936 *tt = t, tt = &t->next, tm = tm->next;
3937 }
3938
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003939 if (fst < lst) {
3940 for (i = fst + 1; i <= lst; i++) {
3941 t = new_Token(NULL, TOK_OTHER, ",", 0);
3942 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003943 j = (i + mac->rotate) % mac->nparam;
3944 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003945 while (tok_isnt_(tm, ",")) {
3946 t = new_Token(NULL, tm->type, tm->text, 0);
3947 *tt = t, tt = &t->next, tm = tm->next;
3948 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003949 }
3950 } else {
3951 for (i = fst - 1; i >= lst; i--) {
3952 t = new_Token(NULL, TOK_OTHER, ",", 0);
3953 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003954 j = (i + mac->rotate) % mac->nparam;
3955 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003956 while (tok_isnt_(tm, ",")) {
3957 t = new_Token(NULL, tm->type, tm->text, 0);
3958 *tt = t, tt = &t->next, tm = tm->next;
3959 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003960 }
3961 }
3962
3963 *last = tt;
3964 return head;
3965
3966err:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003967 nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003968 &tline->text[1]);
3969 return tline;
3970}
3971
H. Peter Anvin76690a12002-04-30 20:52:49 +00003972/*
3973 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003974 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003975 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003976 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003977static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003978{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003979 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003980 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003981 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003982
3983 tail = &thead;
3984 thead = NULL;
3985
H. Peter Anvine2c80182005-01-15 22:15:51 +00003986 while (tline) {
3987 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003988 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3989 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3990 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003991 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003992 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003993 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003994 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003995 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003996 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003997
H. Peter Anvine2c80182005-01-15 22:15:51 +00003998 t = tline;
3999 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004000
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004001 mac = istk->mstk;
4002 while (mac && !mac->name) /* avoid mistaking %reps for macros */
4003 mac = mac->next_active;
4004 if (!mac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004005 nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004006 } else {
4007 pos = strchr(t->text, ':');
4008 if (!pos) {
4009 switch (t->text[1]) {
4010 /*
4011 * We have to make a substitution of one of the
4012 * forms %1, %-1, %+1, %%foo, %0.
4013 */
4014 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004015 type = TOK_NUMBER;
4016 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
4017 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004018 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004019 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004020 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004021 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004022 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004023 text = nasm_strcat(tmpbuf, t->text + 2);
4024 break;
4025 case '-':
4026 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004027 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004028 tt = NULL;
4029 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004030 if (mac->nparam > 1)
4031 n = (n + mac->rotate) % mac->nparam;
4032 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004033 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004034 cc = find_cc(tt);
4035 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004036 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004037 "macro parameter %d is not a condition code",
4038 n + 1);
4039 text = NULL;
4040 } else {
4041 type = TOK_ID;
4042 if (inverse_ccs[cc] == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004043 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004044 "condition code `%s' is not invertible",
4045 conditions[cc]);
4046 text = NULL;
4047 } else
4048 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4049 }
4050 break;
4051 case '+':
4052 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004053 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004054 tt = NULL;
4055 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004056 if (mac->nparam > 1)
4057 n = (n + mac->rotate) % mac->nparam;
4058 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004059 }
4060 cc = find_cc(tt);
4061 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004062 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004063 "macro parameter %d is not a condition code",
4064 n + 1);
4065 text = NULL;
4066 } else {
4067 type = TOK_ID;
4068 text = nasm_strdup(conditions[cc]);
4069 }
4070 break;
4071 default:
4072 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004073 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004074 tt = NULL;
4075 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004076 if (mac->nparam > 1)
4077 n = (n + mac->rotate) % mac->nparam;
4078 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004079 }
4080 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004081 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004082 *tail = new_Token(NULL, tt->type, tt->text, 0);
4083 tail = &(*tail)->next;
4084 tt = tt->next;
4085 }
4086 }
4087 text = NULL; /* we've done it here */
4088 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004089 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004090 } else {
4091 /*
4092 * seems we have a parameters range here
4093 */
4094 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004095 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004096 if (head != t) {
4097 *tail = head;
4098 *last = tline;
4099 tline = head;
4100 text = NULL;
4101 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004102 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004103 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004104 if (!text) {
4105 delete_Token(t);
4106 } else {
4107 *tail = t;
4108 tail = &t->next;
4109 t->type = type;
4110 nasm_free(t->text);
4111 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004112 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004113 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004114 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004115 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004116 } else if (tline->type == TOK_INDIRECT) {
4117 t = tline;
4118 tline = tline->next;
4119 tt = tokenize(t->text);
4120 tt = expand_mmac_params(tt);
4121 tt = expand_smacro(tt);
4122 *tail = tt;
4123 while (tt) {
4124 tt->a.mac = NULL; /* Necessary? */
4125 tail = &tt->next;
4126 tt = tt->next;
4127 }
4128 delete_Token(t);
4129 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004130 } else {
4131 t = *tail = tline;
4132 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004133 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004134 tail = &t->next;
4135 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004136 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004137 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004138
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004139 if (changed) {
4140 const struct tokseq_match t[] = {
4141 {
4142 PP_CONCAT_MASK(TOK_ID) |
4143 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4144 PP_CONCAT_MASK(TOK_ID) |
4145 PP_CONCAT_MASK(TOK_NUMBER) |
4146 PP_CONCAT_MASK(TOK_FLOAT) |
4147 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4148 },
4149 {
4150 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4151 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4152 }
4153 };
4154 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4155 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004156
H. Peter Anvin76690a12002-04-30 20:52:49 +00004157 return thead;
4158}
4159
4160/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004161 * Expand all single-line macro calls made in the given line.
4162 * Return the expanded version of the line. The original is deemed
4163 * to be destroyed in the process. (In reality we'll just move
4164 * Tokens from input to output a lot of the time, rather than
4165 * actually bothering to destroy and replicate.)
4166 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004167
H. Peter Anvine2c80182005-01-15 22:15:51 +00004168static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004169{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004170 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004171 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004172 Token **params;
4173 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004174 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004175 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004176 Token *org_tline = tline;
4177 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004178 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004179 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004180 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004181
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004182 /*
4183 * Trick: we should avoid changing the start token pointer since it can
4184 * be contained in "next" field of other token. Because of this
4185 * we allocate a copy of first token and work with it; at the end of
4186 * routine we copy it back
4187 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004188 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004189 tline = new_Token(org_tline->next, org_tline->type,
4190 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004191 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004192 nasm_free(org_tline->text);
4193 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004194 }
4195
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004196 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004197
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004198again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004199 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004200 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004201
H. Peter Anvine2c80182005-01-15 22:15:51 +00004202 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004203 if (!--deadman) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004204 nasm_error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004205 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004206 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004207
H. Peter Anvine2c80182005-01-15 22:15:51 +00004208 if ((mname = tline->text)) {
4209 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004210 if (tline->type == TOK_ID) {
4211 head = (SMacro *)hash_findix(&smacros, mname);
4212 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004213 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004214 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4215 } else
4216 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004217
H. Peter Anvine2c80182005-01-15 22:15:51 +00004218 /*
4219 * We've hit an identifier. As in is_mmacro below, we first
4220 * check whether the identifier is a single-line macro at
4221 * all, then think about checking for parameters if
4222 * necessary.
4223 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004224 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004225 if (!mstrcmp(m->name, mname, m->casesense))
4226 break;
4227 if (m) {
4228 mstart = tline;
4229 params = NULL;
4230 paramsize = NULL;
4231 if (m->nparam == 0) {
4232 /*
4233 * Simple case: the macro is parameterless. Discard the
4234 * one token that the macro call took, and push the
4235 * expansion back on the to-do stack.
4236 */
4237 if (!m->expansion) {
4238 if (!strcmp("__FILE__", m->name)) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004239 const char *file = src_get_fname();
4240 /* nasm_free(tline->text); here? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004241 tline->text = nasm_quote(file, strlen(file));
4242 tline->type = TOK_STRING;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004243 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004244 }
4245 if (!strcmp("__LINE__", m->name)) {
4246 nasm_free(tline->text);
4247 make_tok_num(tline, src_get_linnum());
4248 continue;
4249 }
4250 if (!strcmp("__BITS__", m->name)) {
4251 nasm_free(tline->text);
4252 make_tok_num(tline, globalbits);
4253 continue;
4254 }
4255 tline = delete_Token(tline);
4256 continue;
4257 }
4258 } else {
4259 /*
4260 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004261 * exists and takes parameters. We must find the
4262 * parameters in the call, count them, find the SMacro
4263 * that corresponds to that form of the macro call, and
4264 * substitute for the parameters when we expand. What a
4265 * pain.
4266 */
4267 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004268 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004269 do {
4270 t = tline->next;
4271 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004272 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004273 t->text = NULL;
4274 t = tline->next = delete_Token(t);
4275 }
4276 tline = t;
4277 } while (tok_type_(tline, TOK_WHITESPACE));
4278 if (!tok_is_(tline, "(")) {
4279 /*
4280 * This macro wasn't called with parameters: ignore
4281 * the call. (Behaviour borrowed from gnu cpp.)
4282 */
4283 tline = mstart;
4284 m = NULL;
4285 } else {
4286 int paren = 0;
4287 int white = 0;
4288 brackets = 0;
4289 nparam = 0;
4290 sparam = PARAM_DELTA;
4291 params = nasm_malloc(sparam * sizeof(Token *));
4292 params[0] = tline->next;
4293 paramsize = nasm_malloc(sparam * sizeof(int));
4294 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004295 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004296 /*
4297 * For some unusual expansions
4298 * which concatenates function call
4299 */
4300 t = tline->next;
4301 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004302 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004303 t->text = NULL;
4304 t = tline->next = delete_Token(t);
4305 }
4306 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004307
H. Peter Anvine2c80182005-01-15 22:15:51 +00004308 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004309 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004310 "macro call expects terminating `)'");
4311 break;
4312 }
4313 if (tline->type == TOK_WHITESPACE
4314 && brackets <= 0) {
4315 if (paramsize[nparam])
4316 white++;
4317 else
4318 params[nparam] = tline->next;
4319 continue; /* parameter loop */
4320 }
4321 if (tline->type == TOK_OTHER
4322 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004323 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004324 if (ch == ',' && !paren && brackets <= 0) {
4325 if (++nparam >= sparam) {
4326 sparam += PARAM_DELTA;
4327 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004328 sparam * sizeof(Token *));
4329 paramsize = nasm_realloc(paramsize,
4330 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004331 }
4332 params[nparam] = tline->next;
4333 paramsize[nparam] = 0;
4334 white = 0;
4335 continue; /* parameter loop */
4336 }
4337 if (ch == '{' &&
4338 (brackets > 0 || (brackets == 0 &&
4339 !paramsize[nparam])))
4340 {
4341 if (!(brackets++)) {
4342 params[nparam] = tline->next;
4343 continue; /* parameter loop */
4344 }
4345 }
4346 if (ch == '}' && brackets > 0)
4347 if (--brackets == 0) {
4348 brackets = -1;
4349 continue; /* parameter loop */
4350 }
4351 if (ch == '(' && !brackets)
4352 paren++;
4353 if (ch == ')' && brackets <= 0)
4354 if (--paren < 0)
4355 break;
4356 }
4357 if (brackets < 0) {
4358 brackets = 0;
H. Peter Anvin130736c2016-02-17 20:27:41 -08004359 nasm_error(ERR_NONFATAL, "braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004360 "enclose all of macro parameter");
4361 }
4362 paramsize[nparam] += white + 1;
4363 white = 0;
4364 } /* parameter loop */
4365 nparam++;
4366 while (m && (m->nparam != nparam ||
4367 mstrcmp(m->name, mname,
4368 m->casesense)))
4369 m = m->next;
4370 if (!m)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004371 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004372 "macro `%s' exists, "
4373 "but not taking %d parameters",
4374 mstart->text, nparam);
4375 }
4376 }
4377 if (m && m->in_progress)
4378 m = NULL;
4379 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004380 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004381 * Design question: should we handle !tline, which
4382 * indicates missing ')' here, or expand those
4383 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004384 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004385 */
4386 nasm_free(params);
4387 nasm_free(paramsize);
4388 tline = mstart;
4389 } else {
4390 /*
4391 * Expand the macro: we are placed on the last token of the
4392 * call, so that we can easily split the call from the
4393 * following tokens. We also start by pushing an SMAC_END
4394 * token for the cycle removal.
4395 */
4396 t = tline;
4397 if (t) {
4398 tline = t->next;
4399 t->next = NULL;
4400 }
4401 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004402 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004403 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004404 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004405 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004406 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004407 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004408 Token *ttt, *pt;
4409 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004410
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004411 ttt = params[t->type - TOK_SMAC_PARAM];
4412 i = paramsize[t->type - TOK_SMAC_PARAM];
4413 while (--i >= 0) {
4414 pt = *ptail = new_Token(tline, ttt->type,
4415 ttt->text, 0);
4416 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004417 ttt = ttt->next;
4418 }
4419 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004420 } else if (t->type == TOK_PREPROC_Q) {
4421 tt = new_Token(tline, TOK_ID, mname, 0);
4422 tline = tt;
4423 } else if (t->type == TOK_PREPROC_QQ) {
4424 tt = new_Token(tline, TOK_ID, m->name, 0);
4425 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004426 } else {
4427 tt = new_Token(tline, t->type, t->text, 0);
4428 tline = tt;
4429 }
4430 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004431
H. Peter Anvine2c80182005-01-15 22:15:51 +00004432 /*
4433 * Having done that, get rid of the macro call, and clean
4434 * up the parameters.
4435 */
4436 nasm_free(params);
4437 nasm_free(paramsize);
4438 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004439 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004440 continue; /* main token loop */
4441 }
4442 }
4443 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004444
H. Peter Anvine2c80182005-01-15 22:15:51 +00004445 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004446 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004447 tline = delete_Token(tline);
4448 } else {
4449 t = *tail = tline;
4450 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004451 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004452 t->next = NULL;
4453 tail = &t->next;
4454 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004455 }
4456
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004457 /*
4458 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004459 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004460 * TOK_IDs should be concatenated.
4461 * Also we look for %+ tokens and concatenate the tokens before and after
4462 * them (without white spaces in between).
4463 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004464 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004465 const struct tokseq_match t[] = {
4466 {
4467 PP_CONCAT_MASK(TOK_ID) |
4468 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4469 PP_CONCAT_MASK(TOK_ID) |
4470 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4471 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4472 }
4473 };
4474 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004475 /*
4476 * If we concatenated something, *and* we had previously expanded
4477 * an actual macro, scan the lines again for macros...
4478 */
4479 tline = thead;
4480 expanded = false;
4481 goto again;
4482 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004483 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004484
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004485err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004486 if (org_tline) {
4487 if (thead) {
4488 *org_tline = *thead;
4489 /* since we just gave text to org_line, don't free it */
4490 thead->text = NULL;
4491 delete_Token(thead);
4492 } else {
4493 /* the expression expanded to empty line;
4494 we can't return NULL for some reasons
4495 we just set the line to a single WHITESPACE token. */
4496 memset(org_tline, 0, sizeof(*org_tline));
4497 org_tline->text = NULL;
4498 org_tline->type = TOK_WHITESPACE;
4499 }
4500 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004501 }
4502
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004503 return thead;
4504}
4505
4506/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004507 * Similar to expand_smacro but used exclusively with macro identifiers
4508 * right before they are fetched in. The reason is that there can be
4509 * identifiers consisting of several subparts. We consider that if there
4510 * are more than one element forming the name, user wants a expansion,
4511 * otherwise it will be left as-is. Example:
4512 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004513 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004514 *
4515 * the identifier %$abc will be left as-is so that the handler for %define
4516 * will suck it and define the corresponding value. Other case:
4517 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004518 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004519 *
4520 * In this case user wants name to be expanded *before* %define starts
4521 * working, so we'll expand %$abc into something (if it has a value;
4522 * otherwise it will be left as-is) then concatenate all successive
4523 * PP_IDs into one.
4524 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004525static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004526{
4527 Token *cur, *oldnext = NULL;
4528
H. Peter Anvin734b1882002-04-30 21:01:08 +00004529 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004530 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004531
4532 cur = tline;
4533 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004534 (cur->next->type == TOK_ID ||
4535 cur->next->type == TOK_PREPROC_ID
4536 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004537 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004538
4539 /* If identifier consists of just one token, don't expand */
4540 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004541 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004542
H. Peter Anvine2c80182005-01-15 22:15:51 +00004543 if (cur) {
4544 oldnext = cur->next; /* Detach the tail past identifier */
4545 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004546 }
4547
H. Peter Anvin734b1882002-04-30 21:01:08 +00004548 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004549
H. Peter Anvine2c80182005-01-15 22:15:51 +00004550 if (cur) {
4551 /* expand_smacro possibly changhed tline; re-scan for EOL */
4552 cur = tline;
4553 while (cur && cur->next)
4554 cur = cur->next;
4555 if (cur)
4556 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004557 }
4558
4559 return tline;
4560}
4561
4562/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004563 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004564 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004565 * to check for an initial label - that's taken care of in
4566 * expand_mmacro - but must check numbers of parameters. Guaranteed
4567 * to be called with tline->type == TOK_ID, so the putative macro
4568 * name is easy to find.
4569 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004570static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004571{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004572 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004573 Token **params;
4574 int nparam;
4575
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004576 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004577
4578 /*
4579 * Efficiency: first we see if any macro exists with the given
4580 * name. If not, we can return NULL immediately. _Then_ we
4581 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004582 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004583 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004584 list_for_each(m, head)
4585 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004586 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004587 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004588 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004589
4590 /*
4591 * OK, we have a potential macro. Count and demarcate the
4592 * parameters.
4593 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004594 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004595
4596 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004597 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004598 * structure that handles this number.
4599 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004600 while (m) {
4601 if (m->nparam_min <= nparam
4602 && (m->plus || nparam <= m->nparam_max)) {
4603 /*
4604 * This one is right. Just check if cycle removal
4605 * prohibits us using it before we actually celebrate...
4606 */
4607 if (m->in_progress > m->max_depth) {
4608 if (m->max_depth > 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004609 nasm_error(ERR_WARNING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004610 "reached maximum recursion depth of %i",
4611 m->max_depth);
4612 }
4613 nasm_free(params);
4614 return NULL;
4615 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004616 /*
4617 * It's right, and we can use it. Add its default
4618 * parameters to the end of our list if necessary.
4619 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004620 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004621 params =
4622 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004623 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004624 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004625 while (nparam < m->nparam_min + m->ndefs) {
4626 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004627 nparam++;
4628 }
4629 }
4630 /*
4631 * If we've gone over the maximum parameter count (and
4632 * we're in Plus mode), ignore parameters beyond
4633 * nparam_max.
4634 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004635 if (m->plus && nparam > m->nparam_max)
4636 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004637 /*
4638 * Then terminate the parameter list, and leave.
4639 */
4640 if (!params) { /* need this special case */
4641 params = nasm_malloc(sizeof(*params));
4642 nparam = 0;
4643 }
4644 params[nparam] = NULL;
4645 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004646 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004647 }
4648 /*
4649 * This one wasn't right: look for the next one with the
4650 * same name.
4651 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004652 list_for_each(m, m->next)
4653 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004654 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004655 }
4656
4657 /*
4658 * After all that, we didn't find one with the right number of
4659 * parameters. Issue a warning, and fail to expand the macro.
4660 */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004661 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004662 "macro `%s' exists, but not taking %d parameters",
4663 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004664 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004665 return NULL;
4666}
4667
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004668
4669/*
4670 * Save MMacro invocation specific fields in
4671 * preparation for a recursive macro expansion
4672 */
4673static void push_mmacro(MMacro *m)
4674{
4675 MMacroInvocation *i;
4676
4677 i = nasm_malloc(sizeof(MMacroInvocation));
4678 i->prev = m->prev;
4679 i->params = m->params;
4680 i->iline = m->iline;
4681 i->nparam = m->nparam;
4682 i->rotate = m->rotate;
4683 i->paramlen = m->paramlen;
4684 i->unique = m->unique;
4685 i->condcnt = m->condcnt;
4686 m->prev = i;
4687}
4688
4689
4690/*
4691 * Restore MMacro invocation specific fields that were
4692 * saved during a previous recursive macro expansion
4693 */
4694static void pop_mmacro(MMacro *m)
4695{
4696 MMacroInvocation *i;
4697
4698 if (m->prev) {
4699 i = m->prev;
4700 m->prev = i->prev;
4701 m->params = i->params;
4702 m->iline = i->iline;
4703 m->nparam = i->nparam;
4704 m->rotate = i->rotate;
4705 m->paramlen = i->paramlen;
4706 m->unique = i->unique;
4707 m->condcnt = i->condcnt;
4708 nasm_free(i);
4709 }
4710}
4711
4712
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004713/*
4714 * Expand the multi-line macro call made by the given line, if
4715 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004716 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004717 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004718static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004719{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004720 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004721 Token *label = NULL;
4722 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004723 Token **params, *t, *tt;
4724 MMacro *m;
4725 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004726 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004727 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004728
4729 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004730 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004731 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004732 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004733 return 0;
4734 m = is_mmacro(t, &params);
4735 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004736 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004737 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004738 Token *last;
4739 /*
4740 * We have an id which isn't a macro call. We'll assume
4741 * it might be a label; we'll also check to see if a
4742 * colon follows it. Then, if there's another id after
4743 * that lot, we'll check it again for macro-hood.
4744 */
4745 label = last = t;
4746 t = t->next;
4747 if (tok_type_(t, TOK_WHITESPACE))
4748 last = t, t = t->next;
4749 if (tok_is_(t, ":")) {
4750 dont_prepend = 1;
4751 last = t, t = t->next;
4752 if (tok_type_(t, TOK_WHITESPACE))
4753 last = t, t = t->next;
4754 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004755 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4756 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004757 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004758 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004759 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004760 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004761
4762 /*
4763 * Fix up the parameters: this involves stripping leading and
4764 * trailing whitespace, then stripping braces if they are
4765 * present.
4766 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004767 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004768 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004769
H. Peter Anvine2c80182005-01-15 22:15:51 +00004770 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004771 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004772 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004773
H. Peter Anvine2c80182005-01-15 22:15:51 +00004774 t = params[i];
4775 skip_white_(t);
4776 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004777 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004778 params[i] = t;
4779 paramlen[i] = 0;
4780 while (t) {
4781 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4782 break; /* ... because we have hit a comma */
4783 if (comma && t->type == TOK_WHITESPACE
4784 && tok_is_(t->next, ","))
4785 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004786 if (brace && t->type == TOK_OTHER) {
4787 if (t->text[0] == '{')
4788 brace++; /* ... or a nested opening brace */
4789 else if (t->text[0] == '}')
4790 if (!--brace)
4791 break; /* ... or a brace */
4792 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004793 t = t->next;
4794 paramlen[i]++;
4795 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004796 if (brace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004797 nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004798 }
4799
4800 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004801 * OK, we have a MMacro structure together with a set of
4802 * parameters. We must now go through the expansion and push
4803 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004804 * parameter tokens and macro-local tokens doesn't get done
4805 * until the single-line macro substitution process; this is
4806 * because delaying them allows us to change the semantics
4807 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004808 *
4809 * First, push an end marker on to istk->expansion, mark this
4810 * macro as in progress, and set up its invocation-specific
4811 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004812 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004813 ll = nasm_malloc(sizeof(Line));
4814 ll->next = istk->expansion;
4815 ll->finishes = m;
4816 ll->first = NULL;
4817 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004818
4819 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004820 * Save the previous MMacro expansion in the case of
4821 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004822 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004823 if (m->max_depth && m->in_progress)
4824 push_mmacro(m);
4825
4826 m->in_progress ++;
4827 m->params = params;
4828 m->iline = tline;
4829 m->nparam = nparam;
4830 m->rotate = 0;
4831 m->paramlen = paramlen;
4832 m->unique = unique++;
4833 m->lineno = 0;
4834 m->condcnt = 0;
4835
4836 m->next_active = istk->mstk;
4837 istk->mstk = m;
4838
4839 list_for_each(l, m->expansion) {
4840 Token **tail;
4841
4842 ll = nasm_malloc(sizeof(Line));
4843 ll->finishes = NULL;
4844 ll->next = istk->expansion;
4845 istk->expansion = ll;
4846 tail = &ll->first;
4847
4848 list_for_each(t, l->first) {
4849 Token *x = t;
4850 switch (t->type) {
4851 case TOK_PREPROC_Q:
4852 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004853 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004854 case TOK_PREPROC_QQ:
4855 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4856 break;
4857 case TOK_PREPROC_ID:
4858 if (t->text[1] == '0' && t->text[2] == '0') {
4859 dont_prepend = -1;
4860 x = label;
4861 if (!x)
4862 continue;
4863 }
4864 /* fall through */
4865 default:
4866 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4867 break;
4868 }
4869 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004870 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004871 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004872 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004873
4874 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004875 * If we had a label, push it on as the first line of
4876 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004877 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004878 if (label) {
4879 if (dont_prepend < 0)
4880 free_tlist(startline);
4881 else {
4882 ll = nasm_malloc(sizeof(Line));
4883 ll->finishes = NULL;
4884 ll->next = istk->expansion;
4885 istk->expansion = ll;
4886 ll->first = startline;
4887 if (!dont_prepend) {
4888 while (label->next)
4889 label = label->next;
4890 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004891 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004892 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004893 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004894
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004895 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004896
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004897 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004898}
4899
H. Peter Anvin130736c2016-02-17 20:27:41 -08004900/*
4901 * This function adds macro names to error messages, and suppresses
4902 * them if necessary.
4903 */
4904static void pp_verror(int severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004905{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004906 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004907 MMacro *mmac = NULL;
4908 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004909
H. Peter Anvin130736c2016-02-17 20:27:41 -08004910 /*
4911 * If we're in a dead branch of IF or something like it, ignore the error.
4912 * However, because %else etc are evaluated in the state context
4913 * of the previous branch, errors might get lost:
4914 * %if 0 ... %else trailing garbage ... %endif
4915 * So %else etc should set the ERR_PP_PRECOND flag.
4916 */
4917 if ((severity & ERR_MASK) < ERR_FATAL &&
4918 istk && istk->conds &&
4919 ((severity & ERR_PP_PRECOND) ?
4920 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004921 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004922 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004923
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004924 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004925 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004926 mmac = istk->mstk;
4927 /* but %rep blocks should be skipped */
4928 while (mmac && !mmac->name)
4929 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004930 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004931
H. Peter Anvin130736c2016-02-17 20:27:41 -08004932 if (mmac) {
4933 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004934
H. Peter Anvin130736c2016-02-17 20:27:41 -08004935 nasm_set_verror(real_verror);
4936 nasm_error(severity, "(%s:%d) %s",
4937 mmac->name, mmac->lineno - delta, buff);
4938 nasm_set_verror(pp_verror);
4939 } else {
4940 real_verror(severity, fmt, arg);
4941 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004942}
4943
H. Peter Anvin734b1882002-04-30 21:01:08 +00004944static void
H. Peter Anvin130736c2016-02-17 20:27:41 -08004945pp_reset(char *file, int apass, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004946{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004947 Token *t;
4948
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004949 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004950 istk = nasm_malloc(sizeof(Include));
4951 istk->next = NULL;
4952 istk->conds = NULL;
4953 istk->expansion = NULL;
4954 istk->mstk = NULL;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07004955 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004956 istk->fname = NULL;
H. Peter Anvin274cda82016-05-10 02:56:29 -07004957 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004958 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004959 if (!istk->fp)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004960 nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004961 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004962 nested_mac_count = 0;
4963 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004964 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004965 unique = 0;
H. Peter Anvinf7606612016-07-13 14:23:48 -07004966
4967 if (tasm_compatible_mode)
4968 pp_add_stdmac(nasm_stdmac_tasm);
4969
4970 pp_add_stdmac(nasm_stdmac_nasm);
4971 pp_add_stdmac(nasm_stdmac_version);
4972
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03004973 if (extrastdmac)
4974 pp_add_stdmac(extrastdmac);
4975
H. Peter Anvinf7606612016-07-13 14:23:48 -07004976 stdmacpos = stdmacros[0];
4977 stdmacnext = &stdmacros[1];
4978
H. Peter Anvind2456592008-06-19 15:04:18 -07004979 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004980
4981 /*
4982 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4983 * The caller, however, will also pass in 3 for preprocess-only so
4984 * we can set __PASS__ accordingly.
4985 */
4986 pass = apass > 2 ? 2 : apass;
4987
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004988 dephead = deplist;
H. Peter Anvin436e3672016-10-04 01:12:28 -07004989 nasm_add_string_to_strlist(dephead, file);
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004990
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004991 /*
4992 * Define the __PASS__ macro. This is defined here unlike
4993 * all the other builtins, because it is special -- it varies between
4994 * passes.
4995 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004996 t = nasm_malloc(sizeof(*t));
4997 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004998 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004999 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005000 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005001}
5002
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005003static void pp_init(void)
5004{
5005 hash_init(&FileHash, HASH_MEDIUM);
5006}
5007
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005008static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005009{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005010 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005011 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005012
H. Peter Anvin130736c2016-02-17 20:27:41 -08005013 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005014
H. Peter Anvine2c80182005-01-15 22:15:51 +00005015 while (1) {
5016 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005017 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005018 * buffer or from the input file.
5019 */
5020 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005021 while (istk->expansion && istk->expansion->finishes) {
5022 Line *l = istk->expansion;
5023 if (!l->finishes->name && l->finishes->in_progress > 1) {
5024 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005025
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005026 /*
5027 * This is a macro-end marker for a macro with no
5028 * name, which means it's not really a macro at all
5029 * but a %rep block, and the `in_progress' field is
5030 * more than 1, meaning that we still need to
5031 * repeat. (1 means the natural last repetition; 0
5032 * means termination by %exitrep.) We have
5033 * therefore expanded up to the %endrep, and must
5034 * push the whole block on to the expansion buffer
5035 * again. We don't bother to remove the macro-end
5036 * marker: we'd only have to generate another one
5037 * if we did.
5038 */
5039 l->finishes->in_progress--;
5040 list_for_each(l, l->finishes->expansion) {
5041 Token *t, *tt, **tail;
5042
5043 ll = nasm_malloc(sizeof(Line));
5044 ll->next = istk->expansion;
5045 ll->finishes = NULL;
5046 ll->first = NULL;
5047 tail = &ll->first;
5048
5049 list_for_each(t, l->first) {
5050 if (t->text || t->type == TOK_WHITESPACE) {
5051 tt = *tail = new_Token(NULL, t->type, t->text, 0);
5052 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005053 }
5054 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005055
5056 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005057 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005058 } else {
5059 /*
5060 * Check whether a `%rep' was started and not ended
5061 * within this macro expansion. This can happen and
5062 * should be detected. It's a fatal error because
5063 * I'm too confused to work out how to recover
5064 * sensibly from it.
5065 */
5066 if (defining) {
5067 if (defining->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005068 nasm_panic(0, "defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005069 else if (istk->mstk->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005070 nasm_fatal(0, "`%%rep' without `%%endrep' within"
5071 " expansion of macro `%s'",
5072 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005073 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005074
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005075 /*
5076 * FIXME: investigate the relationship at this point between
5077 * istk->mstk and l->finishes
5078 */
5079 {
5080 MMacro *m = istk->mstk;
5081 istk->mstk = m->next_active;
5082 if (m->name) {
5083 /*
5084 * This was a real macro call, not a %rep, and
5085 * therefore the parameter information needs to
5086 * be freed.
5087 */
5088 if (m->prev) {
5089 pop_mmacro(m);
5090 l->finishes->in_progress --;
5091 } else {
5092 nasm_free(m->params);
5093 free_tlist(m->iline);
5094 nasm_free(m->paramlen);
5095 l->finishes->in_progress = 0;
5096 }
5097 } else
5098 free_mmacro(m);
5099 }
5100 istk->expansion = l->next;
5101 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005102 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005103 }
5104 }
5105 while (1) { /* until we get a line we can use */
5106
5107 if (istk->expansion) { /* from a macro expansion */
5108 char *p;
5109 Line *l = istk->expansion;
5110 if (istk->mstk)
5111 istk->mstk->lineno++;
5112 tline = l->first;
5113 istk->expansion = l->next;
5114 nasm_free(l);
5115 p = detoken(tline, false);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005116 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005117 nasm_free(p);
5118 break;
5119 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005120 line = read_line();
5121 if (line) { /* from the current input file */
5122 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005123 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005124 nasm_free(line);
5125 break;
5126 }
5127 /*
5128 * The current file has ended; work down the istk
5129 */
5130 {
5131 Include *i = istk;
5132 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005133 if (i->conds) {
5134 /* nasm_error can't be conditionally suppressed */
H. Peter Anvin41087062016-03-03 14:27:34 -08005135 nasm_fatal(0,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005136 "expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005137 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005138 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005139 if (i->next)
5140 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005141 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005142 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005143 nasm_free(i);
H. Peter Anvin130736c2016-02-17 20:27:41 -08005144 if (!istk) {
5145 line = NULL;
5146 goto done;
5147 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005148 if (istk->expansion && istk->expansion->finishes)
5149 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005150 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005151 }
5152
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005153 /*
5154 * We must expand MMacro parameters and MMacro-local labels
5155 * _before_ we plunge into directive processing, to cope
5156 * with things like `%define something %1' such as STRUC
5157 * uses. Unless we're _defining_ a MMacro, in which case
5158 * those tokens should be left alone to go into the
5159 * definition; and unless we're in a non-emitting
5160 * condition, in which case we don't want to meddle with
5161 * anything.
5162 */
5163 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5164 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005165 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005166 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005167
H. Peter Anvine2c80182005-01-15 22:15:51 +00005168 /*
5169 * Check the line to see if it's a preprocessor directive.
5170 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07005171 if (do_directive(tline, &line) == DIRECTIVE_FOUND) {
5172 if (line)
5173 break; /* Directive generated output */
5174 else
5175 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005176 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005177 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005178 * We're defining a multi-line macro. We emit nothing
5179 * at all, and just
5180 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005181 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005182 Line *l = nasm_malloc(sizeof(Line));
5183 l->next = defining->expansion;
5184 l->first = tline;
5185 l->finishes = NULL;
5186 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005187 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005188 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005189 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005190 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005191 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005192 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005193 * directive so we keep our place correctly.
5194 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005195 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005196 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005197 } else if (istk->mstk && !istk->mstk->in_progress) {
5198 /*
5199 * We're in a %rep block which has been terminated, so
5200 * we're walking through to the %endrep without
5201 * emitting anything. Emit nothing at all, not even a
5202 * blank line: when we emerge from the %rep block we'll
5203 * give a line-number directive so we keep our place
5204 * correctly.
5205 */
5206 free_tlist(tline);
5207 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005208 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005209 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005210 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005211 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005212 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005213 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005214 line = detoken(tline, true);
5215 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005216 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005217 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005218 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005219 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005220 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005221 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005222
H. Peter Anvin130736c2016-02-17 20:27:41 -08005223done:
5224 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005225 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005226}
5227
H. Peter Anvine2c80182005-01-15 22:15:51 +00005228static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005229{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005230 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005231
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005232 if (defining) {
5233 if (defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005234 nasm_error(ERR_NONFATAL,
5235 "end of file while still defining macro `%s'",
5236 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005237 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005238 nasm_error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005239 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005240
5241 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005242 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005243 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005244
5245 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005246
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005247 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005248 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005249 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005250 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005251 Include *i = istk;
5252 istk = istk->next;
5253 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005254 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005255 }
5256 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005257 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005258 src_set_fname(NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005259 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005260 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005261 free_llist(predef);
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005262 predef = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005263 delete_Blocks();
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005264 freeTokens = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005265 while ((i = ipath)) {
5266 ipath = i->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005267 if (i->path)
5268 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005269 nasm_free(i);
5270 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005271 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005272}
5273
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005274static void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005275{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005276 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005277
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005278 i = nasm_malloc(sizeof(IncPath));
5279 i->path = path ? nasm_strdup(path) : NULL;
5280 i->next = NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005281
H. Peter Anvin89cee572009-07-15 09:16:54 -04005282 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005283 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005284 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005285 j = j->next;
5286 j->next = i;
5287 } else {
5288 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005289 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005290}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005291
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005292static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005293{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005294 Token *inc, *space, *name;
5295 Line *l;
5296
H. Peter Anvin734b1882002-04-30 21:01:08 +00005297 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5298 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5299 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005300
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005301 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005302 l->next = predef;
5303 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005304 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005305 predef = l;
5306}
5307
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005308static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005309{
5310 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005311 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005312 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005313
H. Peter Anvin130736c2016-02-17 20:27:41 -08005314 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005315
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005316 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005317 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5318 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005319 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005320 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005321 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005322 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005323 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005324
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005325 if (space->next->type != TOK_PREPROC_ID &&
5326 space->next->type != TOK_ID)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005327 nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005328
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005329 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005330 l->next = predef;
5331 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005332 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005333 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005334
5335 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005336}
5337
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005338static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005339{
5340 Token *def, *space;
5341 Line *l;
5342
H. Peter Anvin734b1882002-04-30 21:01:08 +00005343 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5344 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005345 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005346
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005347 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005348 l->next = predef;
5349 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005350 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005351 predef = l;
5352}
5353
H. Peter Anvinf7606612016-07-13 14:23:48 -07005354static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005355{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005356 macros_t **mp;
5357
5358 /* Find the end of the list and avoid duplicates */
5359 for (mp = stdmacros; *mp; mp++) {
5360 if (*mp == macros)
5361 return; /* Nothing to do */
5362 }
5363
5364 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5365
5366 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005367}
5368
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005369static void pp_extra_stdmac(macros_t *macros)
5370{
5371 extrastdmac = macros;
5372}
5373
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005374static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005375{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005376 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005377 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005378 tok->text = nasm_strdup(numbuf);
5379 tok->type = TOK_NUMBER;
5380}
5381
H. Peter Anvin37368952016-05-09 14:10:32 -07005382static void pp_list_one_macro(MMacro *m, int severity)
5383{
5384 if (!m)
5385 return;
5386
5387 /* We need to print the next_active list in reverse order */
5388 pp_list_one_macro(m->next_active, severity);
5389
5390 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005391 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvin37368952016-05-09 14:10:32 -07005392 nasm_error(severity, "... from macro `%s' defined here", m->name);
5393 }
5394}
5395
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005396static void pp_error_list_macros(int severity)
5397{
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005398 int32_t saved_line;
5399 const char *saved_fname = NULL;
5400
5401 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY;
H. Peter Anvin274cda82016-05-10 02:56:29 -07005402 src_get(&saved_line, &saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005403
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005404 if (istk)
5405 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005406
H. Peter Anvin274cda82016-05-10 02:56:29 -07005407 src_set(saved_line, saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005408}
5409
H. Peter Anvine7469712016-02-18 02:20:59 -08005410const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005411 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005412 pp_reset,
5413 pp_getline,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005414 pp_cleanup,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005415 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005416 pp_pre_define,
5417 pp_pre_undefine,
5418 pp_pre_include,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005419 pp_include_path,
5420 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005421};