blob: 388f4058eab151da37cfbc764b10442bcc5b2ab6 [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 Anvind7ed89e2002-04-30 20:52:08 +0000289 * Conditional assembly: we maintain a separate stack of these for
290 * each level of file inclusion. (The only reason we keep the
291 * stacks separate is to ensure that a stray `%endif' in a file
292 * included from within the true branch of a `%if' won't terminate
293 * it and cause confusion: instead, rightly, it'll cause an error.)
294 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800295struct Cond {
296 Cond *next;
297 int state;
298};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000299enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000300 /*
301 * These states are for use just after %if or %elif: IF_TRUE
302 * means the condition has evaluated to truth so we are
303 * currently emitting, whereas IF_FALSE means we are not
304 * currently emitting but will start doing so if a %else comes
305 * up. In these states, all directives are admissible: %elif,
306 * %else and %endif. (And of course %if.)
307 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800308 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000309 /*
310 * These states come up after a %else: ELSE_TRUE means we're
311 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
312 * any %elif or %else will cause an error.
313 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800314 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000315 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200316 * These states mean that we're not emitting now, and also that
317 * nothing until %endif will be emitted at all. COND_DONE is
318 * used when we've had our moment of emission
319 * and have now started seeing %elifs. COND_NEVER is used when
320 * the condition construct in question is contained within a
321 * non-emitting branch of a larger condition construct,
322 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000323 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800324 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000325};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800326#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000327
H. Peter Anvin70653092007-10-19 14:42:29 -0700328/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000329 * These defines are used as the possible return values for do_directive
330 */
331#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300332#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000333
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000334/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800335 * This define sets the upper limit for smacro and recursive mmacro
336 * expansions
Keith Kanios852f1ee2009-07-12 00:19:55 -0500337 */
338#define DEADMAN_LIMIT (1 << 20)
339
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400340/* max reps */
341#define REP_LIMIT ((INT64_C(1) << 62))
342
Keith Kanios852f1ee2009-07-12 00:19:55 -0500343/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000344 * Condition codes. Note that we use c_ prefix not C_ because C_ is
345 * used in nasm.h for the "real" condition codes. At _this_ level,
346 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
347 * ones, so we need a different enum...
348 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700349static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000350 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
351 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000352 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000353};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700354enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000355 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
356 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 -0700357 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
358 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000359};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700360static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000361 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
362 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 +0000363 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000364};
365
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800366/*
367 * Directive names.
368 */
369/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
370static int is_condition(enum preproc_token arg)
371{
372 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
373}
374
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000375/* For TASM compatibility we need to be able to recognise TASM compatible
376 * conditional compilation directives. Using the NASM pre-processor does
377 * not work, so we look for them specifically from the following list and
378 * then jam in the equivalent NASM directive into the input stream.
379 */
380
H. Peter Anvine2c80182005-01-15 22:15:51 +0000381enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000382 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
383 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
384};
385
H. Peter Anvin476d2862007-10-02 22:04:15 -0700386static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000387 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
388 "ifndef", "include", "local"
389};
390
391static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700392static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000393static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800394static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000395
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000396static Context *cstk;
397static Include *istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800398static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000399
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300400static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700401static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000402
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300403static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000404
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800405static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700406static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000407
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000408/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800409 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000410 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800411static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000412
413/*
414 * The current set of single-line macros we have defined.
415 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700416static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000417
418/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800419 * The multi-line macro we are currently defining, or the %rep
420 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000421 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800422static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000423
Charles Crayned4200be2008-07-12 16:42:33 -0700424static uint64_t nested_mac_count;
425static uint64_t nested_rep_count;
426
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000427/*
428 * The number of macro parameters to allocate space for at a time.
429 */
430#define PARAM_DELTA 16
431
432/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700433 * The standard macro set: defined in macros.c in the array nasm_stdmac.
434 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000435 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700436static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000437
438/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000439 * The extra standard macros that come from the object format, if
440 * any.
441 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800442static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700443static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000444
445/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000446 * Tokens are allocated in blocks to improve speed
447 */
448#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800449static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000450struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000451 Blocks *next;
452 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000453};
454
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800455static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000456
457/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000458 * Forward declarations.
459 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000460static Token *expand_mmac_params(Token * tline);
461static Token *expand_smacro(Token * tline);
462static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400463static Context *get_ctx(const char *name, const char **namep);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700464static void make_tok_num(Token * tok, int64_t val);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800465static void pp_verror(int severity, const char *fmt, va_list ap);
466static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000467static void *new_Block(size_t size);
468static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700469static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300470 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000471static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000472
473/*
474 * Macros for safe checking of token pointers, avoid *(NULL)
475 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300476#define tok_type_(x,t) ((x) && (x)->type == (t))
477#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
478#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
479#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000480
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400481/*
H. Peter Anvin077fb932010-07-20 14:56:30 -0700482 * nasm_unquote with error if the string contains NUL characters.
483 * If the string contains NUL characters, issue an error and return
484 * the C len, i.e. truncate at the NUL.
485 */
486static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
487{
488 size_t len = nasm_unquote(qstr, NULL);
489 size_t clen = strlen(qstr);
490
491 if (len != clen)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800492 nasm_error(ERR_NONFATAL, "NUL character in `%s' directive",
H. Peter Anvin077fb932010-07-20 14:56:30 -0700493 pp_directives[directive]);
494
495 return clen;
496}
497
498/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700499 * In-place reverse a list of tokens.
500 */
501static Token *reverse_tokens(Token *t)
502{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800503 Token *prev = NULL;
504 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700505
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800506 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400507 next = t->next;
508 t->next = prev;
509 prev = t;
510 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800511 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700512
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800513 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700514}
515
516/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300517 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000518 * front of them. We do it here because I could not find any other
519 * place to do it for the moment, and it is a hack (ideally it would
520 * be nice to be able to use the NASM pre-processor to do it).
521 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000522static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000523{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000524 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400525 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000526
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400527 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000528
529 /* Binary search for the directive name */
530 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400531 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400532 q = nasm_skip_word(p);
533 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000534 if (len) {
535 oldchar = p[len];
536 p[len] = 0;
537 while (j - i > 1) {
538 k = (j + i) / 2;
539 m = nasm_stricmp(p, tasm_directives[k]);
540 if (m == 0) {
541 /* We have found a directive, so jam a % in front of it
542 * so that NASM will then recognise it as one if it's own.
543 */
544 p[len] = oldchar;
545 len = strlen(p);
546 oldline = line;
547 line = nasm_malloc(len + 2);
548 line[0] = '%';
549 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700550 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300551 * NASM does not recognise IFDIFI, so we convert
552 * it to %if 0. This is not used in NASM
553 * compatible code, but does need to parse for the
554 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000555 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700556 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000557 } else {
558 memcpy(line + 1, p, len + 1);
559 }
560 nasm_free(oldline);
561 return line;
562 } else if (m < 0) {
563 j = k;
564 } else
565 i = k;
566 }
567 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000568 }
569 return line;
570}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000571
H. Peter Anvin76690a12002-04-30 20:52:49 +0000572/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000573 * The pre-preprocessing stage... This function translates line
574 * number indications as they emerge from GNU cpp (`# lineno "file"
575 * flags') into NASM preprocessor line number indications (`%line
576 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000577 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000578static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000579{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000580 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000581 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000582
H. Peter Anvine2c80182005-01-15 22:15:51 +0000583 if (line[0] == '#' && line[1] == ' ') {
584 oldline = line;
585 fname = oldline + 2;
586 lineno = atoi(fname);
587 fname += strspn(fname, "0123456789 ");
588 if (*fname == '"')
589 fname++;
590 fnlen = strcspn(fname, "\"");
591 line = nasm_malloc(20 + fnlen);
592 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
593 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000594 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000595 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000596 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000597 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000598}
599
600/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000601 * Free a linked list of tokens.
602 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000603static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000604{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400605 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000606 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000607}
608
609/*
610 * Free a linked list of lines.
611 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000612static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000613{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400614 Line *l, *tmp;
615 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000616 free_tlist(l->first);
617 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000618 }
619}
620
621/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800622 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000623 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800624static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000625{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800626 nasm_free(m->name);
627 free_tlist(m->dlist);
628 nasm_free(m->defaults);
629 free_llist(m->expansion);
630 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000631}
632
633/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700634 * Free all currently defined macros, and free the hash tables
635 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700636static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700637{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400638 SMacro *s, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700639 const char *key;
640 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700641
H. Peter Anvin072771e2008-05-22 13:17:51 -0700642 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300643 nasm_free((void *)key);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400644 list_for_each_safe(s, tmp, s) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300645 nasm_free(s->name);
646 free_tlist(s->expansion);
647 nasm_free(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300648 }
H. Peter Anvin97a23472007-09-16 17:57:25 -0700649 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700650 hash_free(smt);
651}
652
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800653static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700654{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800655 MMacro *m, *tmp;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700656 const char *key;
657 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700658
659 it = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800660 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300661 nasm_free((void *)key);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800662 list_for_each_safe(m ,tmp, m)
663 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700664 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800665 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700666}
667
668static void free_macros(void)
669{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700670 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800671 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700672}
673
674/*
675 * Initialize the hash tables
676 */
677static void init_macros(void)
678{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700679 hash_init(&smacros, HASH_LARGE);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800680 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700681}
682
683/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000684 * Pop the context stack.
685 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000686static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000687{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000688 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000689
690 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700691 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000692 nasm_free(c->name);
693 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000694}
695
H. Peter Anvin072771e2008-05-22 13:17:51 -0700696/*
697 * Search for a key in the hash index; adding it if necessary
698 * (in which case we initialize the data pointer to NULL.)
699 */
700static void **
701hash_findi_add(struct hash_table *hash, const char *str)
702{
703 struct hash_insert hi;
704 void **r;
705 char *strx;
706
707 r = hash_findi(hash, str, &hi);
708 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300709 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700710
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300711 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700712 return hash_add(&hi, strx, NULL);
713}
714
715/*
716 * Like hash_findi, but returns the data element rather than a pointer
717 * to it. Used only when not adding a new element, hence no third
718 * argument.
719 */
720static void *
721hash_findix(struct hash_table *hash, const char *str)
722{
723 void **p;
724
725 p = hash_findi(hash, str, NULL);
726 return p ? *p : NULL;
727}
728
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400729/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800730 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400731 * if there no more left -- return NULL
732 */
733static char *line_from_stdmac(void)
734{
735 unsigned char c;
736 const unsigned char *p = stdmacpos;
737 char *line, *q;
738 size_t len = 0;
739
740 if (!stdmacpos)
741 return NULL;
742
743 while ((c = *p++)) {
744 if (c >= 0x80)
745 len += pp_directives_len[c - 0x80] + 1;
746 else
747 len++;
748 }
749
750 line = nasm_malloc(len + 1);
751 q = line;
752 while ((c = *stdmacpos++)) {
753 if (c >= 0x80) {
754 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
755 q += pp_directives_len[c - 0x80];
756 *q++ = ' ';
757 } else {
758 *q++ = c;
759 }
760 }
761 stdmacpos = p;
762 *q = '\0';
763
764 if (!*stdmacpos) {
765 /* This was the last of the standard macro chain... */
766 stdmacpos = NULL;
767 if (any_extrastdmac) {
768 stdmacpos = extrastdmac;
769 any_extrastdmac = false;
770 } else if (do_predef) {
771 Line *pd, *l;
772 Token *head, **tail, *t;
773
774 /*
775 * Nasty hack: here we push the contents of
776 * `predef' on to the top-level expansion stack,
777 * since this is the most convenient way to
778 * implement the pre-include and pre-define
779 * features.
780 */
781 list_for_each(pd, predef) {
782 head = NULL;
783 tail = &head;
784 list_for_each(t, pd->first) {
785 *tail = new_Token(NULL, t->type, t->text, 0);
786 tail = &(*tail)->next;
787 }
788
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800789 l = nasm_malloc(sizeof(Line));
790 l->next = istk->expansion;
791 l->first = head;
792 l->finishes = NULL;
793
794 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400795 }
796 do_predef = false;
797 }
798 }
799
800 return line;
801}
802
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000803static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000804{
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400805 unsigned int size, c, next;
806 const unsigned int delta = 512;
807 const unsigned int pad = 8;
808 unsigned int nr_cont = 0;
809 bool cont = false;
810 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000811
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400812 /* Standart macros set (predefined) goes first */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400813 p = line_from_stdmac();
814 if (p)
815 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700816
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400817 size = delta;
818 p = buffer = nasm_malloc(size);
819
820 for (;;) {
821 c = fgetc(istk->fp);
822 if ((int)(c) == EOF) {
823 p[0] = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000824 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000825 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400826
827 switch (c) {
828 case '\r':
829 next = fgetc(istk->fp);
830 if (next != '\n')
831 ungetc(next, istk->fp);
832 if (cont) {
833 cont = false;
834 continue;
835 }
836 break;
837
838 case '\n':
839 if (cont) {
840 cont = false;
841 continue;
842 }
843 break;
844
845 case '\\':
846 next = fgetc(istk->fp);
847 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400848 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400849 cont = true;
850 nr_cont++;
851 continue;
852 }
853 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000854 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400855
856 if (c == '\r' || c == '\n') {
857 *p++ = 0;
858 break;
859 }
860
861 if (p >= (buffer + size - pad)) {
862 buffer = nasm_realloc(buffer, size + delta);
863 p = buffer + size - pad;
864 size += delta;
865 }
866
867 *p++ = (unsigned char)c;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000868 }
869
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400870 if (p == buffer) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000871 nasm_free(buffer);
872 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000873 }
874
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300875 src_set_linnum(src_get_linnum() + istk->lineinc +
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400876 (nr_cont * istk->lineinc));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000877
878 /*
879 * Handle spurious ^Z, which may be inserted into source files
880 * by some file transfer utilities.
881 */
882 buffer[strcspn(buffer, "\032")] = '\0';
883
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -0800884 lfmt->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000885
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000886 return buffer;
887}
888
889/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000890 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000891 * don't need to parse the value out of e.g. numeric tokens: we
892 * simply split one string into many.
893 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000894static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000895{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700896 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000897 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000898 Token *list = NULL;
899 Token *t, **tail = &list;
900
H. Peter Anvine2c80182005-01-15 22:15:51 +0000901 while (*line) {
902 p = line;
903 if (*p == '%') {
904 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300905 if (*p == '+' && !nasm_isdigit(p[1])) {
906 p++;
907 type = TOK_PASTE;
908 } else if (nasm_isdigit(*p) ||
909 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000910 do {
911 p++;
912 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700913 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000914 type = TOK_PREPROC_ID;
915 } else if (*p == '{') {
916 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800917 while (*p) {
918 if (*p == '}')
919 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000920 p[-1] = *p;
921 p++;
922 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800923 if (*p != '}')
H. Peter Anvin130736c2016-02-17 20:27:41 -0800924 nasm_error(ERR_WARNING | ERR_PASS1,
925 "unterminated %%{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000926 p[-1] = '\0';
927 if (*p)
928 p++;
929 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300930 } else if (*p == '[') {
931 int lvl = 1;
932 line += 2; /* Skip the leading %[ */
933 p++;
934 while (lvl && (c = *p++)) {
935 switch (c) {
936 case ']':
937 lvl--;
938 break;
939 case '%':
940 if (*p == '[')
941 lvl++;
942 break;
943 case '\'':
944 case '\"':
945 case '`':
Cyrill Gorcunovc6360a72010-07-13 13:32:19 +0400946 p = nasm_skip_string(p - 1) + 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300947 break;
948 default:
949 break;
950 }
951 }
952 p--;
953 if (*p)
954 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800955 if (lvl)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800956 nasm_error(ERR_NONFATAL|ERR_PASS1,
957 "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300958 type = TOK_INDIRECT;
959 } else if (*p == '?') {
960 type = TOK_PREPROC_Q; /* %? */
961 p++;
962 if (*p == '?') {
963 type = TOK_PREPROC_QQ; /* %?? */
964 p++;
965 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400966 } else if (*p == '!') {
967 type = TOK_PREPROC_ID;
968 p++;
969 if (isidchar(*p)) {
970 do {
971 p++;
972 }
973 while (isidchar(*p));
974 } else if (*p == '\'' || *p == '\"' || *p == '`') {
975 p = nasm_skip_string(p);
976 if (*p)
977 p++;
978 else
H. Peter Anvin130736c2016-02-17 20:27:41 -0800979 nasm_error(ERR_NONFATAL|ERR_PASS1,
980 "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400981 } else {
982 /* %! without string or identifier */
983 type = TOK_OTHER; /* Legacy behavior... */
984 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 } else if (isidchar(*p) ||
986 ((*p == '!' || *p == '%' || *p == '$') &&
987 isidchar(p[1]))) {
988 do {
989 p++;
990 }
991 while (isidchar(*p));
992 type = TOK_PREPROC_ID;
993 } else {
994 type = TOK_OTHER;
995 if (*p == '%')
996 p++;
997 }
998 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
999 type = TOK_ID;
1000 p++;
1001 while (*p && isidchar(*p))
1002 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001003 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 /*
1005 * A string token.
1006 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001008 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001009
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 if (*p) {
1011 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001012 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001013 nasm_error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014 /* Handling unterminated strings by UNV */
1015 /* type = -1; */
1016 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001017 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001018 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001019 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001020 } else if (isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001021 bool is_hex = false;
1022 bool is_float = false;
1023 bool has_e = false;
1024 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001025
H. Peter Anvine2c80182005-01-15 22:15:51 +00001026 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001027 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001028 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001029
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001030 if (*p == '$') {
1031 p++;
1032 is_hex = true;
1033 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001034
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001035 for (;;) {
1036 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001037
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001038 if (!is_hex && (c == 'e' || c == 'E')) {
1039 has_e = true;
1040 if (*p == '+' || *p == '-') {
1041 /*
1042 * e can only be followed by +/- if it is either a
1043 * prefixed hex number or a floating-point number
1044 */
1045 p++;
1046 is_float = true;
1047 }
1048 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1049 is_hex = true;
1050 } else if (c == 'P' || c == 'p') {
1051 is_float = true;
1052 if (*p == '+' || *p == '-')
1053 p++;
1054 } else if (isnumchar(c) || c == '_')
1055 ; /* just advance */
1056 else if (c == '.') {
1057 /*
1058 * we need to deal with consequences of the legacy
1059 * parser, like "1.nolist" being two tokens
1060 * (TOK_NUMBER, TOK_ID) here; at least give it
1061 * a shot for now. In the future, we probably need
1062 * a flex-based scanner with proper pattern matching
1063 * to do it as well as it can be done. Nothing in
1064 * the world is going to help the person who wants
1065 * 0x123.p16 interpreted as two tokens, though.
1066 */
1067 r = p;
1068 while (*r == '_')
1069 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001070
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001071 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1072 (!is_hex && (*r == 'e' || *r == 'E')) ||
1073 (*r == 'p' || *r == 'P')) {
1074 p = r;
1075 is_float = true;
1076 } else
1077 break; /* Terminate the token */
1078 } else
1079 break;
1080 }
1081 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001082
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001083 if (p == line+1 && *line == '$') {
1084 type = TOK_OTHER; /* TOKEN_HERE */
1085 } else {
1086 if (has_e && !is_hex) {
1087 /* 1e13 is floating-point, but 1e13h is not */
1088 is_float = true;
1089 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001090
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001091 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1092 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001093 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001094 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001095 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001096 /*
1097 * Whitespace just before end-of-line is discarded by
1098 * pretending it's a comment; whitespace just before a
1099 * comment gets lumped into the comment.
1100 */
1101 if (!*p || *p == ';') {
1102 type = TOK_COMMENT;
1103 while (*p)
1104 p++;
1105 }
1106 } else if (*p == ';') {
1107 type = TOK_COMMENT;
1108 while (*p)
1109 p++;
1110 } else {
1111 /*
1112 * Anything else is an operator of some kind. We check
1113 * for all the double-character operators (>>, <<, //,
1114 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001115 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001116 */
1117 type = TOK_OTHER;
1118 if ((p[0] == '>' && p[1] == '>') ||
1119 (p[0] == '<' && p[1] == '<') ||
1120 (p[0] == '/' && p[1] == '/') ||
1121 (p[0] == '<' && p[1] == '=') ||
1122 (p[0] == '>' && p[1] == '=') ||
1123 (p[0] == '=' && p[1] == '=') ||
1124 (p[0] == '!' && p[1] == '=') ||
1125 (p[0] == '<' && p[1] == '>') ||
1126 (p[0] == '&' && p[1] == '&') ||
1127 (p[0] == '|' && p[1] == '|') ||
1128 (p[0] == '^' && p[1] == '^')) {
1129 p++;
1130 }
1131 p++;
1132 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001133
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 /* Handling unterminated string by UNV */
1135 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001136 {
1137 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1138 t->text[p-line] = *line;
1139 tail = &t->next;
1140 }
1141 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142 if (type != TOK_COMMENT) {
1143 *tail = t = new_Token(NULL, type, line, p - line);
1144 tail = &t->next;
1145 }
1146 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001147 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001148 return list;
1149}
1150
H. Peter Anvince616072002-04-30 21:02:23 +00001151/*
1152 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001153 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001154 * deleted only all at once by the delete_Blocks function.
1155 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001156static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001157{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001158 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001160 /* first, get to the end of the linked list */
1161 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001163 /* now allocate the requested chunk */
1164 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001165
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001166 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001167 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001168 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001169}
1170
1171/*
1172 * this function deletes all managed blocks of memory
1173 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001174static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001175{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001176 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001177
H. Peter Anvin70653092007-10-19 14:42:29 -07001178 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001179 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001180 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001181 * free it.
1182 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001183 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001184 if (b->chunk)
1185 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 a = b;
1187 b = b->next;
1188 if (a != &blocks)
1189 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001190 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001191 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001192}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001193
1194/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001195 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001196 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001197 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001198 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001199static Token *new_Token(Token * next, enum pp_token_type type,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001200 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001201{
1202 Token *t;
1203 int i;
1204
H. Peter Anvin89cee572009-07-15 09:16:54 -04001205 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001206 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1207 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1208 freeTokens[i].next = &freeTokens[i + 1];
1209 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001210 }
1211 t = freeTokens;
1212 freeTokens = t->next;
1213 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001214 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001215 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001216 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001217 t->text = NULL;
1218 } else {
1219 if (txtlen == 0)
1220 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001221 t->text = nasm_malloc(txtlen+1);
1222 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001223 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001224 }
1225 return t;
1226}
1227
H. Peter Anvine2c80182005-01-15 22:15:51 +00001228static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001229{
1230 Token *next = t->next;
1231 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001232 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001233 freeTokens = t;
1234 return next;
1235}
1236
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001237/*
1238 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001239 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1240 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001241 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001242static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001243{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001244 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001245 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001246 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001247 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001248
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001249 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001250 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001251 char *v;
1252 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001253
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001254 v = t->text + 2;
1255 if (*v == '\'' || *v == '\"' || *v == '`') {
1256 size_t len = nasm_unquote(v, NULL);
1257 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001258
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001259 if (len != clen) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001260 nasm_error(ERR_NONFATAL | ERR_PASS1,
1261 "NUL character in %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001262 v = NULL;
1263 }
1264 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001265
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001266 if (v) {
1267 char *p = getenv(v);
1268 if (!p) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001269 nasm_error(ERR_NONFATAL | ERR_PASS1,
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001270 "nonexistent environment variable `%s'", v);
1271 p = "";
1272 }
1273 t->text = nasm_strdup(p);
1274 }
1275 nasm_free(q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001276 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001277
H. Peter Anvine2c80182005-01-15 22:15:51 +00001278 /* Expand local macros here and not during preprocessing */
1279 if (expand_locals &&
1280 t->type == TOK_PREPROC_ID && t->text &&
1281 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001282 const char *q;
1283 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001284 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001285 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001286 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001287 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001288 p = nasm_strcat(buffer, q);
1289 nasm_free(t->text);
1290 t->text = p;
1291 }
1292 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001293 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001294 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001295 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001296 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001297 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001298
H. Peter Anvin734b1882002-04-30 21:01:08 +00001299 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001300
1301 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001302 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001303 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001304 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001305 q = t->text;
1306 while (*q)
1307 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001308 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001309 }
1310 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001311
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001312 return line;
1313}
1314
1315/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001316 * A scanner, suitable for use by the expression evaluator, which
1317 * operates on a line of Tokens. Expects a pointer to a pointer to
1318 * the first token in the line to be passed in as its private_data
1319 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001320 *
1321 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001322 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001323static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001324{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001325 Token **tlineptr = private_data;
1326 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001327 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001328
H. Peter Anvine2c80182005-01-15 22:15:51 +00001329 do {
1330 tline = *tlineptr;
1331 *tlineptr = tline ? tline->next : NULL;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04001332 } while (tline && (tline->type == TOK_WHITESPACE ||
1333 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001334
1335 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001337
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001338 tokval->t_charptr = tline->text;
1339
H. Peter Anvin76690a12002-04-30 20:52:49 +00001340 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001342 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001344
H. Peter Anvine2c80182005-01-15 22:15:51 +00001345 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001346 p = tokval->t_charptr = tline->text;
1347 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001348 tokval->t_charptr++;
1349 return tokval->t_type = TOKEN_ID;
1350 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001351
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001352 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001353 if (r >= p+MAX_KEYWORD)
1354 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001355 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001356 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001357 *s = '\0';
1358 /* right, so we have an identifier sitting in temp storage. now,
1359 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001360 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001361 }
1362
H. Peter Anvine2c80182005-01-15 22:15:51 +00001363 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001364 bool rn_error;
1365 tokval->t_integer = readnum(tline->text, &rn_error);
1366 tokval->t_charptr = tline->text;
1367 if (rn_error)
1368 return tokval->t_type = TOKEN_ERRNUM;
1369 else
1370 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001371 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001372
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001373 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001374 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001375 }
1376
H. Peter Anvine2c80182005-01-15 22:15:51 +00001377 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001378 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001379
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001380 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001381 tokval->t_charptr = tline->text;
1382 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001383
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001384 if (ep[0] != bq || ep[1] != '\0')
1385 return tokval->t_type = TOKEN_ERRSTR;
1386 else
1387 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001388 }
1389
H. Peter Anvine2c80182005-01-15 22:15:51 +00001390 if (tline->type == TOK_OTHER) {
1391 if (!strcmp(tline->text, "<<"))
1392 return tokval->t_type = TOKEN_SHL;
1393 if (!strcmp(tline->text, ">>"))
1394 return tokval->t_type = TOKEN_SHR;
1395 if (!strcmp(tline->text, "//"))
1396 return tokval->t_type = TOKEN_SDIV;
1397 if (!strcmp(tline->text, "%%"))
1398 return tokval->t_type = TOKEN_SMOD;
1399 if (!strcmp(tline->text, "=="))
1400 return tokval->t_type = TOKEN_EQ;
1401 if (!strcmp(tline->text, "<>"))
1402 return tokval->t_type = TOKEN_NE;
1403 if (!strcmp(tline->text, "!="))
1404 return tokval->t_type = TOKEN_NE;
1405 if (!strcmp(tline->text, "<="))
1406 return tokval->t_type = TOKEN_LE;
1407 if (!strcmp(tline->text, ">="))
1408 return tokval->t_type = TOKEN_GE;
1409 if (!strcmp(tline->text, "&&"))
1410 return tokval->t_type = TOKEN_DBL_AND;
1411 if (!strcmp(tline->text, "^^"))
1412 return tokval->t_type = TOKEN_DBL_XOR;
1413 if (!strcmp(tline->text, "||"))
1414 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001415 }
1416
1417 /*
1418 * We have no other options: just return the first character of
1419 * the token text.
1420 */
1421 return tokval->t_type = tline->text[0];
1422}
1423
1424/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001425 * Compare a string to the name of an existing macro; this is a
1426 * simple wrapper which calls either strcmp or nasm_stricmp
1427 * depending on the value of the `casesense' parameter.
1428 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001429static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001430{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001431 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001432}
1433
1434/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001435 * Compare a string to the name of an existing macro; this is a
1436 * simple wrapper which calls either strcmp or nasm_stricmp
1437 * depending on the value of the `casesense' parameter.
1438 */
1439static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1440{
1441 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1442}
1443
1444/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001445 * Return the Context structure associated with a %$ token. Return
1446 * NULL, having _already_ reported an error condition, if the
1447 * context stack isn't deep enough for the supplied number of $
1448 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001449 *
1450 * If "namep" is non-NULL, set it to the pointer to the macro name
1451 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001452 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001453static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001454{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001455 Context *ctx;
1456 int i;
1457
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001458 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001459 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001460
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001461 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001463
H. Peter Anvine2c80182005-01-15 22:15:51 +00001464 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001465 nasm_error(ERR_NONFATAL, "`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001466 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001467 }
1468
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001469 name += 2;
1470 ctx = cstk;
1471 i = 0;
1472 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001473 name++;
1474 i++;
1475 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001476 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001477 if (!ctx) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001478 nasm_error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001479 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001480 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001481 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001482
1483 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001484 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001485
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001486 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001487}
1488
1489/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001490 * Check to see if a file is already in a string list
1491 */
1492static bool in_list(const StrList *list, const char *str)
1493{
1494 while (list) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001495 if (!strcmp(list->str, str))
1496 return true;
1497 list = list->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001498 }
1499 return false;
1500}
1501
1502/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001503 * Open an include file. This routine must always return a valid
1504 * file pointer if it returns - it's responsible for throwing an
1505 * ERR_FATAL and bombing out completely if not. It should also try
1506 * the include path one by one until it finds the file or reaches
1507 * the end of the path.
1508 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001509static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
H. Peter Anvin66d561f2016-06-16 15:47:46 -07001510 char **found_path, bool missing_ok, enum file_flags mode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001511{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001512 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001513 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001514 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001515 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001516 size_t prefix_len = 0;
1517 StrList *sl;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001518 size_t path_len;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001519
H. Peter Anvine2c80182005-01-15 22:15:51 +00001520 while (1) {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001521 path_len = prefix_len + len + 1;
1522
1523 sl = nasm_malloc(path_len + sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001524 memcpy(sl->str, prefix, prefix_len);
1525 memcpy(sl->str+prefix_len, file, len+1);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001526
1527 if (found_path != NULL) {
1528 *found_path = nasm_malloc(path_len);
1529 memcpy(*found_path, sl->str, path_len);
1530 }
1531
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001532 fp = nasm_open_read(sl->str, mode);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001533 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001534 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001535 **dtail = sl;
1536 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001537 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001538 nasm_free(sl);
1539 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001540 if (fp)
1541 return fp;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001542
1543 if (found_path != NULL && *found_path != NULL) {
1544 nasm_free(*found_path);
H. Peter Anvinf500d832016-06-16 15:28:09 -07001545 *found_path = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001546 }
1547
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001548 if (!ip) {
1549 if (!missing_ok)
1550 break;
1551 prefix = NULL;
1552 } else {
1553 prefix = ip->path;
1554 ip = ip->next;
1555 }
1556 if (prefix) {
1557 prefix_len = strlen(prefix);
1558 } else {
1559 /* -MG given and file not found */
1560 if (dhead && !in_list(*dhead, file)) {
1561 sl = nasm_malloc(len+1+sizeof sl->next);
1562 sl->next = NULL;
1563 strcpy(sl->str, file);
1564 **dtail = sl;
1565 *dtail = &sl->next;
1566 }
1567 return NULL;
1568 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001569 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001570
H. Peter Anvin130736c2016-02-17 20:27:41 -08001571 nasm_error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001572 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001573}
1574
1575/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001576 * Opens an include or input file. Public version, for use by modules
1577 * that get a file:lineno pair and need to look at the file again
1578 * (e.g. the CodeView debug backend). Returns NULL on failure.
1579 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001580FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001581{
1582 FILE *fp;
1583 StrList *xsl = NULL;
1584 StrList **xst = &xsl;
1585
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001586 fp = inc_fopen(filename, &xsl, &xst, NULL, true, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001587 if (xsl)
1588 nasm_free(xsl);
1589 return fp;
1590}
1591
1592/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001593 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001594 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001595 * return true if _any_ single-line macro of that name is defined.
1596 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001597 * `nparam' or no parameters is defined.
1598 *
1599 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001600 * defined, or nparam is -1, the address of the definition structure
1601 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001602 * is NULL, no action will be taken regarding its contents, and no
1603 * error will occur.
1604 *
1605 * Note that this is also called with nparam zero to resolve
1606 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001607 *
1608 * If you already know which context macro belongs to, you can pass
1609 * the context pointer as first parameter; if you won't but name begins
1610 * with %$ the context will be automatically computed. If all_contexts
1611 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001612 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001613static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001614smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001615 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001616{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001617 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001618 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001619
H. Peter Anvin97a23472007-09-16 17:57:25 -07001620 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001621 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001622 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001623 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001624 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001625 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001626 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001627 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001628 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001629 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001630 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001631 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001632
H. Peter Anvine2c80182005-01-15 22:15:51 +00001633 while (m) {
1634 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001635 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001636 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001637 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001638 *defn = m;
1639 else
1640 *defn = NULL;
1641 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001642 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001643 }
1644 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001645 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001646
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001647 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001648}
1649
1650/*
1651 * Count and mark off the parameters in a multi-line macro call.
1652 * This is called both from within the multi-line macro expansion
1653 * code, and also to mark off the default parameters when provided
1654 * in a %macro definition line.
1655 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001656static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001657{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001658 int paramsize, brace;
1659
1660 *nparam = paramsize = 0;
1661 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001662 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001663 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001664 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001665 paramsize += PARAM_DELTA;
1666 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1667 }
1668 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001669 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001670 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001671 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001672 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001673 if (brace) {
1674 while (brace && (t = t->next) != NULL) {
1675 if (tok_is_(t, "{"))
1676 brace++;
1677 else if (tok_is_(t, "}"))
1678 brace--;
1679 }
1680
1681 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001682 /*
1683 * Now we've found the closing brace, look further
1684 * for the comma.
1685 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001686 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001687 skip_white_(t);
1688 if (tok_isnt_(t, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001689 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 "braces do not enclose all of macro parameter");
1691 while (tok_isnt_(t, ","))
1692 t = t->next;
1693 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001694 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001695 } else {
1696 while (tok_isnt_(t, ","))
1697 t = t->next;
1698 }
1699 if (t) { /* got a comma/brace */
1700 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001701 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001702 }
1703}
1704
1705/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001706 * Determine whether one of the various `if' conditions is true or
1707 * not.
1708 *
1709 * We must free the tline we get passed.
1710 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001711static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001712{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001713 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001714 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001715 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001716 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001717 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001718 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001719 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001720
1721 origline = tline;
1722
H. Peter Anvine2c80182005-01-15 22:15:51 +00001723 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001724 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001725 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001726 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001727 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001728 if (!tline)
1729 break;
1730 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001731 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001732 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001733 free_tlist(origline);
1734 return -1;
1735 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001736 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001737 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001738 tline = tline->next;
1739 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001740 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001741
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001742 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001743 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001744 while (tline) {
1745 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001746 if (!tline || (tline->type != TOK_ID &&
1747 (tline->type != TOK_PREPROC_ID ||
1748 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001749 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001750 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001751 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001752 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001753 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001754 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001755 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001756 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001757 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001758
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001759 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001760 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001761 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001762 while (tline) {
1763 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001764 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001765 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001766 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001767 tline->text[1] != '!'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001768 nasm_error(ERR_NONFATAL,
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001769 "`%s' expects environment variable names",
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001770 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001771 goto fail;
1772 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001773 p = tline->text;
1774 if (tline->type == TOK_PREPROC_ID)
1775 p += 2; /* Skip leading %! */
1776 if (*p == '\'' || *p == '\"' || *p == '`')
1777 nasm_unquote_cstr(p, ct);
1778 if (getenv(p))
1779 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001780 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001781 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001782 break;
1783
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001784 case PPC_IFIDN:
1785 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001786 tline = expand_smacro(tline);
1787 t = tt = tline;
1788 while (tok_isnt_(tt, ","))
1789 tt = tt->next;
1790 if (!tt) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001791 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001792 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001793 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001794 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001795 }
1796 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001797 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001798 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1799 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001800 nasm_error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001801 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001802 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001803 }
1804 if (t->type == TOK_WHITESPACE) {
1805 t = t->next;
1806 continue;
1807 }
1808 if (tt->type == TOK_WHITESPACE) {
1809 tt = tt->next;
1810 continue;
1811 }
1812 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001813 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001814 break;
1815 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001816 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001818 size_t l1 = nasm_unquote(t->text, NULL);
1819 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001820
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001821 if (l1 != l2) {
1822 j = false;
1823 break;
1824 }
1825 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1826 j = false;
1827 break;
1828 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001829 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001830 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831 break;
1832 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001833
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 t = t->next;
1835 tt = tt->next;
1836 }
1837 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001838 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001839 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001840
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001841 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001842 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001843 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001844 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001845
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001846 skip_white_(tline);
1847 tline = expand_id(tline);
1848 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001849 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001850 "`%s' expects a macro name", pp_directives[ct]);
1851 goto fail;
1852 }
1853 searching.name = nasm_strdup(tline->text);
1854 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001855 searching.plus = false;
1856 searching.nolist = false;
1857 searching.in_progress = 0;
1858 searching.max_depth = 0;
1859 searching.rep_nest = NULL;
1860 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001861 searching.nparam_max = INT_MAX;
1862 tline = expand_smacro(tline->next);
1863 skip_white_(tline);
1864 if (!tline) {
1865 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001866 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001867 "`%s' expects a parameter count or nothing",
1868 pp_directives[ct]);
1869 } else {
1870 searching.nparam_min = searching.nparam_max =
1871 readnum(tline->text, &j);
1872 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001873 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001874 "unable to parse parameter count `%s'",
1875 tline->text);
1876 }
1877 if (tline && tok_is_(tline->next, "-")) {
1878 tline = tline->next->next;
1879 if (tok_is_(tline, "*"))
1880 searching.nparam_max = INT_MAX;
1881 else if (!tok_type_(tline, TOK_NUMBER))
H. Peter Anvin130736c2016-02-17 20:27:41 -08001882 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001883 "`%s' expects a parameter count after `-'",
1884 pp_directives[ct]);
1885 else {
1886 searching.nparam_max = readnum(tline->text, &j);
1887 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001888 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001889 "unable to parse parameter count `%s'",
1890 tline->text);
1891 if (searching.nparam_min > searching.nparam_max)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001892 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001893 "minimum parameter count exceeds maximum");
1894 }
1895 }
1896 if (tline && tok_is_(tline->next, "+")) {
1897 tline = tline->next;
1898 searching.plus = true;
1899 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001900 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1901 while (mmac) {
1902 if (!strcmp(mmac->name, searching.name) &&
1903 (mmac->nparam_min <= searching.nparam_max
1904 || searching.plus)
1905 && (searching.nparam_min <= mmac->nparam_max
1906 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001907 found = true;
1908 break;
1909 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001910 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001911 }
1912 if (tline && tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001913 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001914 "trailing garbage after %%ifmacro ignored");
1915 nasm_free(searching.name);
1916 j = found;
1917 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001918 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001919
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001920 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001921 needtype = TOK_ID;
1922 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001923 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001924 needtype = TOK_NUMBER;
1925 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001926 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001927 needtype = TOK_STRING;
1928 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001929
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001930iftype:
1931 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001932
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001933 while (tok_type_(t, TOK_WHITESPACE) ||
1934 (needtype == TOK_NUMBER &&
1935 tok_type_(t, TOK_OTHER) &&
1936 (t->text[0] == '-' || t->text[0] == '+') &&
1937 !t->text[1]))
1938 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001939
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001940 j = tok_type_(t, needtype);
1941 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001942
1943 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001944 t = tline = expand_smacro(tline);
1945 while (tok_type_(t, TOK_WHITESPACE))
1946 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001947
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001948 j = false;
1949 if (t) {
1950 t = t->next; /* Skip the actual token */
1951 while (tok_type_(t, TOK_WHITESPACE))
1952 t = t->next;
1953 j = !t; /* Should be nothing left */
1954 }
1955 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001956
H. Peter Anvin134b9462008-02-16 17:01:40 -08001957 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001958 t = tline = expand_smacro(tline);
1959 while (tok_type_(t, TOK_WHITESPACE))
1960 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001961
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001962 j = !t; /* Should be empty */
1963 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001964
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001965 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001966 t = tline = expand_smacro(tline);
1967 tptr = &t;
1968 tokval.t_type = TOKEN_INVALID;
1969 evalresult = evaluate(ppscan, tptr, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -08001970 NULL, pass | CRITICAL, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001971 if (!evalresult)
1972 return -1;
1973 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001974 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001975 "trailing garbage after expression ignored");
1976 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001977 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001978 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001979 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001980 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001981 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001982 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001983
H. Peter Anvine2c80182005-01-15 22:15:51 +00001984 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08001985 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001986 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001987 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001988 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001989 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001990
1991 free_tlist(origline);
1992 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001993
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001994fail:
1995 free_tlist(origline);
1996 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001997}
1998
1999/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002000 * Common code for defining an smacro
2001 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002002static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002003 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002004{
2005 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002006 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002007
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002008 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002009 if (!smac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002010 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002011 "single-line macro `%s' defined both with and"
2012 " without parameters", mname);
2013 /*
2014 * Some instances of the old code considered this a failure,
2015 * some others didn't. What is the right thing to do here?
2016 */
2017 free_tlist(expansion);
2018 return false; /* Failure */
2019 } else {
2020 /*
2021 * We're redefining, so we have to take over an
2022 * existing SMacro structure. This means freeing
2023 * what was already in it.
2024 */
2025 nasm_free(smac->name);
2026 free_tlist(smac->expansion);
2027 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002028 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002029 smtbl = ctx ? &ctx->localmac : &smacros;
2030 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002031 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002032 smac->next = *smhead;
2033 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002034 }
2035 smac->name = nasm_strdup(mname);
2036 smac->casesense = casesense;
2037 smac->nparam = nparam;
2038 smac->expansion = expansion;
2039 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002040 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002041}
2042
2043/*
2044 * Undefine an smacro
2045 */
2046static void undef_smacro(Context *ctx, const char *mname)
2047{
2048 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002049 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002050
H. Peter Anvin166c2472008-05-28 12:28:58 -07002051 smtbl = ctx ? &ctx->localmac : &smacros;
2052 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002053
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002054 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002055 /*
2056 * We now have a macro name... go hunt for it.
2057 */
2058 sp = smhead;
2059 while ((s = *sp) != NULL) {
2060 if (!mstrcmp(s->name, mname, s->casesense)) {
2061 *sp = s->next;
2062 nasm_free(s->name);
2063 free_tlist(s->expansion);
2064 nasm_free(s);
2065 } else {
2066 sp = &s->next;
2067 }
2068 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002069 }
2070}
2071
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002072/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002073 * Parse a mmacro specification.
2074 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002075static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002076{
2077 bool err;
2078
2079 tline = tline->next;
2080 skip_white_(tline);
2081 tline = expand_id(tline);
2082 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002083 nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002084 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002085 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002086
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002087 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002088 def->name = nasm_strdup(tline->text);
2089 def->plus = false;
2090 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002091 def->in_progress = 0;
2092 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002093 def->nparam_min = 0;
2094 def->nparam_max = 0;
2095
H. Peter Anvina26433d2008-07-16 14:40:01 -07002096 tline = expand_smacro(tline->next);
2097 skip_white_(tline);
2098 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002099 nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002100 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002101 def->nparam_min = def->nparam_max =
2102 readnum(tline->text, &err);
2103 if (err)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002104 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002105 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002106 }
2107 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002108 tline = tline->next->next;
2109 if (tok_is_(tline, "*")) {
2110 def->nparam_max = INT_MAX;
2111 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002112 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002113 "`%s' expects a parameter count after `-'", directive);
2114 } else {
2115 def->nparam_max = readnum(tline->text, &err);
2116 if (err) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002117 nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002118 tline->text);
2119 }
2120 if (def->nparam_min > def->nparam_max) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002121 nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002122 }
2123 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002124 }
2125 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002126 tline = tline->next;
2127 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002128 }
2129 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002130 !nasm_stricmp(tline->next->text, ".nolist")) {
2131 tline = tline->next;
2132 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002133 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002134
H. Peter Anvina26433d2008-07-16 14:40:01 -07002135 /*
2136 * Handle default parameters.
2137 */
2138 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002139 def->dlist = tline->next;
2140 tline->next = NULL;
2141 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002142 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002143 def->dlist = NULL;
2144 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002145 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002146 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002147
H. Peter Anvin89cee572009-07-15 09:16:54 -04002148 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002149 !def->plus)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002150 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002151 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002152
H. Peter Anvina26433d2008-07-16 14:40:01 -07002153 return true;
2154}
2155
2156
2157/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002158 * Decode a size directive
2159 */
2160static int parse_size(const char *str) {
2161 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002162 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002163 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002164 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002165
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002166 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002167}
2168
Ed Beroset3ab3f412002-06-11 03:31:49 +00002169/**
2170 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002171 * Find out if a line contains a preprocessor directive, and deal
2172 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002173 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002174 * If a directive _is_ found, it is the responsibility of this routine
2175 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002176 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002177 * @param tline a pointer to the current tokeninzed line linked list
2178 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002179 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002180 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002181static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002182{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002183 enum preproc_token i;
2184 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002185 bool err;
2186 int nparam;
2187 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002188 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002189 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002190 int offset;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002191 char *p, *pp, *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002192 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002193 Include *inc;
2194 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002195 Cond *cond;
2196 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002197 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002198 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002199 struct tokenval tokval;
2200 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002201 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002202 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002203 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002204 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002205
2206 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002207
H. Peter Anvineba20a72002-04-30 20:53:55 +00002208 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002209 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002210 (tline->text[1] == '%' || tline->text[1] == '$'
2211 || tline->text[1] == '!'))
2212 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002213
H. Peter Anvin4169a472007-09-12 01:29:43 +00002214 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002215
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002216 /*
2217 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2218 * since they are known to be buggy at moment, we need to fix them
2219 * in future release (2.09-2.10)
2220 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002221 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002222 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002223 tline->text);
2224 return NO_DIRECTIVE_FOUND;
2225 }
2226
2227 /*
2228 * If we're in a non-emitting branch of a condition construct,
2229 * or walking to the end of an already terminated %rep block,
2230 * we should ignore all directives except for condition
2231 * directives.
2232 */
2233 if (((istk->conds && !emitting(istk->conds->state)) ||
2234 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2235 return NO_DIRECTIVE_FOUND;
2236 }
2237
2238 /*
2239 * If we're defining a macro or reading a %rep block, we should
2240 * ignore all directives except for %macro/%imacro (which nest),
2241 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2242 * If we're in a %rep block, another %rep nests, so should be let through.
2243 */
2244 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2245 i != PP_RMACRO && i != PP_IRMACRO &&
2246 i != PP_ENDMACRO && i != PP_ENDM &&
2247 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2248 return NO_DIRECTIVE_FOUND;
2249 }
2250
2251 if (defining) {
2252 if (i == PP_MACRO || i == PP_IMACRO ||
2253 i == PP_RMACRO || i == PP_IRMACRO) {
2254 nested_mac_count++;
2255 return NO_DIRECTIVE_FOUND;
2256 } else if (nested_mac_count > 0) {
2257 if (i == PP_ENDMACRO) {
2258 nested_mac_count--;
2259 return NO_DIRECTIVE_FOUND;
2260 }
2261 }
2262 if (!defining->name) {
2263 if (i == PP_REP) {
2264 nested_rep_count++;
2265 return NO_DIRECTIVE_FOUND;
2266 } else if (nested_rep_count > 0) {
2267 if (i == PP_ENDREP) {
2268 nested_rep_count--;
2269 return NO_DIRECTIVE_FOUND;
2270 }
2271 }
2272 }
2273 }
2274
H. Peter Anvin4169a472007-09-12 01:29:43 +00002275 switch (i) {
2276 case PP_INVALID:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002277 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002278 tline->text);
2279 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002280
H. Peter Anvine2c80182005-01-15 22:15:51 +00002281 case PP_STACKSIZE:
2282 /* Directive to tell NASM what the default stack size is. The
2283 * default is for a 16-bit stack, and this can be overriden with
2284 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002285 */
2286 tline = tline->next;
2287 if (tline && tline->type == TOK_WHITESPACE)
2288 tline = tline->next;
2289 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002290 nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002291 free_tlist(origline);
2292 return DIRECTIVE_FOUND;
2293 }
2294 if (nasm_stricmp(tline->text, "flat") == 0) {
2295 /* All subsequent ARG directives are for a 32-bit stack */
2296 StackSize = 4;
2297 StackPointer = "ebp";
2298 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002299 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002300 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2301 /* All subsequent ARG directives are for a 64-bit stack */
2302 StackSize = 8;
2303 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002304 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002305 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002306 } else if (nasm_stricmp(tline->text, "large") == 0) {
2307 /* All subsequent ARG directives are for a 16-bit stack,
2308 * far function call.
2309 */
2310 StackSize = 2;
2311 StackPointer = "bp";
2312 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002313 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002314 } else if (nasm_stricmp(tline->text, "small") == 0) {
2315 /* All subsequent ARG directives are for a 16-bit stack,
2316 * far function call. We don't support near functions.
2317 */
2318 StackSize = 2;
2319 StackPointer = "bp";
2320 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002321 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002322 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002323 nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002324 free_tlist(origline);
2325 return DIRECTIVE_FOUND;
2326 }
2327 free_tlist(origline);
2328 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002329
H. Peter Anvine2c80182005-01-15 22:15:51 +00002330 case PP_ARG:
2331 /* TASM like ARG directive to define arguments to functions, in
2332 * the following form:
2333 *
2334 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2335 */
2336 offset = ArgOffset;
2337 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002338 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002339 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002340
H. Peter Anvine2c80182005-01-15 22:15:51 +00002341 /* Find the argument name */
2342 tline = tline->next;
2343 if (tline && tline->type == TOK_WHITESPACE)
2344 tline = tline->next;
2345 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002346 nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002347 free_tlist(origline);
2348 return DIRECTIVE_FOUND;
2349 }
2350 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002351
H. Peter Anvine2c80182005-01-15 22:15:51 +00002352 /* Find the argument size type */
2353 tline = tline->next;
2354 if (!tline || tline->type != TOK_OTHER
2355 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002356 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002357 "Syntax error processing `%%arg' directive");
2358 free_tlist(origline);
2359 return DIRECTIVE_FOUND;
2360 }
2361 tline = tline->next;
2362 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002363 nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002364 free_tlist(origline);
2365 return DIRECTIVE_FOUND;
2366 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002367
H. Peter Anvine2c80182005-01-15 22:15:51 +00002368 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002369 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002370 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002371 size = parse_size(tt->text);
2372 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002373 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002374 "Invalid size type for `%%arg' missing directive");
2375 free_tlist(tt);
2376 free_tlist(origline);
2377 return DIRECTIVE_FOUND;
2378 }
2379 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002380
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002381 /* Round up to even stack slots */
2382 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002383
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 /* Now define the macro for the argument */
2385 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2386 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002387 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002388 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002389
H. Peter Anvine2c80182005-01-15 22:15:51 +00002390 /* Move to the next argument in the list */
2391 tline = tline->next;
2392 if (tline && tline->type == TOK_WHITESPACE)
2393 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002394 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002395 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002396 free_tlist(origline);
2397 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002398
H. Peter Anvine2c80182005-01-15 22:15:51 +00002399 case PP_LOCAL:
2400 /* TASM like LOCAL directive to define local variables for a
2401 * function, in the following form:
2402 *
2403 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2404 *
2405 * The '= LocalSize' at the end is ignored by NASM, but is
2406 * required by TASM to define the local parameter size (and used
2407 * by the TASM macro package).
2408 */
2409 offset = LocalOffset;
2410 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002411 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002412 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002413
H. Peter Anvine2c80182005-01-15 22:15:51 +00002414 /* Find the argument name */
2415 tline = tline->next;
2416 if (tline && tline->type == TOK_WHITESPACE)
2417 tline = tline->next;
2418 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002419 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002420 "`%%local' missing argument parameter");
2421 free_tlist(origline);
2422 return DIRECTIVE_FOUND;
2423 }
2424 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002425
H. Peter Anvine2c80182005-01-15 22:15:51 +00002426 /* Find the argument size type */
2427 tline = tline->next;
2428 if (!tline || tline->type != TOK_OTHER
2429 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002430 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002431 "Syntax error processing `%%local' directive");
2432 free_tlist(origline);
2433 return DIRECTIVE_FOUND;
2434 }
2435 tline = tline->next;
2436 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002437 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002438 "`%%local' missing size type parameter");
2439 free_tlist(origline);
2440 return DIRECTIVE_FOUND;
2441 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002442
H. Peter Anvine2c80182005-01-15 22:15:51 +00002443 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002444 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002445 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002446 size = parse_size(tt->text);
2447 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002448 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002449 "Invalid size type for `%%local' missing directive");
2450 free_tlist(tt);
2451 free_tlist(origline);
2452 return DIRECTIVE_FOUND;
2453 }
2454 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002455
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002456 /* Round up to even stack slots */
2457 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002458
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002459 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002460
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002461 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002462 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2463 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002464 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002465
H. Peter Anvine2c80182005-01-15 22:15:51 +00002466 /* Now define the assign to setup the enter_c macro correctly */
2467 snprintf(directive, sizeof(directive),
2468 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002469 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002470
H. Peter Anvine2c80182005-01-15 22:15:51 +00002471 /* Move to the next argument in the list */
2472 tline = tline->next;
2473 if (tline && tline->type == TOK_WHITESPACE)
2474 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002475 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002476 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002477 free_tlist(origline);
2478 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002479
H. Peter Anvine2c80182005-01-15 22:15:51 +00002480 case PP_CLEAR:
2481 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002482 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002483 "trailing garbage after `%%clear' ignored");
2484 free_macros();
2485 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002486 free_tlist(origline);
2487 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002488
H. Peter Anvin418ca702008-05-30 10:42:30 -07002489 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002490 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002491 skip_white_(t);
2492 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002493 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002494 nasm_error(ERR_NONFATAL, "`%%depend' expects a file name");
H. Peter Anvin418ca702008-05-30 10:42:30 -07002495 free_tlist(origline);
2496 return DIRECTIVE_FOUND; /* but we did _something_ */
2497 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002498 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002499 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002500 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002501 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002502 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002503 nasm_unquote_cstr(p, i);
2504 if (dephead && !in_list(*dephead, p)) {
2505 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2506 sl->next = NULL;
2507 strcpy(sl->str, p);
2508 *deptail = sl;
2509 deptail = &sl->next;
2510 }
2511 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002512 return DIRECTIVE_FOUND;
2513
2514 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002515 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002516 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002517
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002518 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002519 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002520 nasm_error(ERR_NONFATAL, "`%%include' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002521 free_tlist(origline);
2522 return DIRECTIVE_FOUND; /* but we did _something_ */
2523 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002524 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002525 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002526 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002527 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002528 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002529 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002530 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002531 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002532 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002533 found_path = NULL;
H. Peter Anvin66d561f2016-06-16 15:47:46 -07002534 inc->fp = inc_fopen(p, dephead, &deptail, &found_path, pass == 0, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002535 if (!inc->fp) {
2536 /* -MG given but file not found */
2537 nasm_free(inc);
2538 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002539 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002540 inc->lineno = src_set_linnum(0);
2541 inc->lineinc = 1;
2542 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002543 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002544 istk = inc;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002545 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002546 }
2547 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002548 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002549
H. Peter Anvind2456592008-06-19 15:04:18 -07002550 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002551 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002552 static macros_t *use_pkg;
2553 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002554
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002555 tline = tline->next;
2556 skip_white_(tline);
2557 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002558
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002559 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002560 tline->type != TOK_INTERNAL_STRING &&
2561 tline->type != TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002562 nasm_error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002563 free_tlist(origline);
2564 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002565 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002566 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002567 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002568 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002569 if (tline->type == TOK_STRING)
2570 nasm_unquote_cstr(tline->text, i);
2571 use_pkg = nasm_stdmac_find_package(tline->text);
2572 if (!use_pkg)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002573 nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002574 else
2575 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002576 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002577 /* Not already included, go ahead and include it */
2578 stdmacpos = use_pkg;
2579 }
2580 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002581 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002582 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002583 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002584 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002585 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002586 tline = tline->next;
2587 skip_white_(tline);
2588 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002589 if (tline) {
2590 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002591 nasm_error(ERR_NONFATAL, "`%s' expects a context identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002592 pp_directives[i]);
2593 free_tlist(origline);
2594 return DIRECTIVE_FOUND; /* but we did _something_ */
2595 }
2596 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002597 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002598 "trailing garbage after `%s' ignored",
2599 pp_directives[i]);
2600 p = nasm_strdup(tline->text);
2601 } else {
2602 p = NULL; /* Anonymous */
2603 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002604
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002605 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002606 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002607 ctx->next = cstk;
2608 hash_init(&ctx->localmac, HASH_SMALL);
2609 ctx->name = p;
2610 ctx->number = unique++;
2611 cstk = ctx;
2612 } else {
2613 /* %pop or %repl */
2614 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002615 nasm_error(ERR_NONFATAL, "`%s': context stack is empty",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002616 pp_directives[i]);
2617 } else if (i == PP_POP) {
2618 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08002619 nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002620 "expected %s",
2621 cstk->name ? cstk->name : "anonymous", p);
2622 else
2623 ctx_pop();
2624 } else {
2625 /* i == PP_REPL */
2626 nasm_free(cstk->name);
2627 cstk->name = p;
2628 p = NULL;
2629 }
2630 nasm_free(p);
2631 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002632 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002633 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002634 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002635 severity = ERR_FATAL;
2636 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002637 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002638 severity = ERR_NONFATAL;
2639 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002640 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002641 severity = ERR_WARNING|ERR_WARN_USER;
2642 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002643
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002644issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002645 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002646 /* Only error out if this is the final pass */
2647 if (pass != 2 && i != PP_FATAL)
2648 return DIRECTIVE_FOUND;
2649
2650 tline->next = expand_smacro(tline->next);
2651 tline = tline->next;
2652 skip_white_(tline);
2653 t = tline ? tline->next : NULL;
2654 skip_white_(t);
2655 if (tok_type_(tline, TOK_STRING) && !t) {
2656 /* The line contains only a quoted string */
2657 p = tline->text;
2658 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002659 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002660 } else {
2661 /* Not a quoted string, or more than a quoted string */
2662 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002663 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002664 nasm_free(p);
2665 }
2666 free_tlist(origline);
2667 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002668 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002669
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002670 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002671 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002672 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002673 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002674 j = if_condition(tline->next, i);
2675 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002676 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002677 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002678 cond = nasm_malloc(sizeof(Cond));
2679 cond->next = istk->conds;
2680 cond->state = j;
2681 istk->conds = cond;
2682 if(istk->mstk)
2683 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002684 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002685 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002686
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002687 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002688 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002689 nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002690 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002691 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002692 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002693 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002694
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002695 case COND_DONE:
2696 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002697 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002698
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002699 case COND_ELSE_TRUE:
2700 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002701 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2702 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002703 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002704 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002705
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002706 case COND_IF_FALSE:
2707 /*
2708 * IMPORTANT: In the case of %if, we will already have
2709 * called expand_mmac_params(); however, if we're
2710 * processing an %elif we must have been in a
2711 * non-emitting mode, which would have inhibited
2712 * the normal invocation of expand_mmac_params().
2713 * Therefore, we have to do it explicitly here.
2714 */
2715 j = if_condition(expand_mmac_params(tline->next), i);
2716 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002717 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002718 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2719 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002720 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002721 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002722 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002723
H. Peter Anvine2c80182005-01-15 22:15:51 +00002724 case PP_ELSE:
2725 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002726 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2727 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002728 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002729 nasm_fatal(0, "`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002730 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002731 case COND_IF_TRUE:
2732 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002733 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002734 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002735
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002736 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002737 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002738
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002739 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002740 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002741 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002742
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002743 case COND_ELSE_TRUE:
2744 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002745 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002746 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002747 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002748 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002749 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002750 free_tlist(origline);
2751 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002752
H. Peter Anvine2c80182005-01-15 22:15:51 +00002753 case PP_ENDIF:
2754 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002755 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2756 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002757 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002758 nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002759 cond = istk->conds;
2760 istk->conds = cond->next;
2761 nasm_free(cond);
2762 if(istk->mstk)
2763 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002764 free_tlist(origline);
2765 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002766
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002767 case PP_RMACRO:
2768 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002769 case PP_MACRO:
2770 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002771 if (defining) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002772 nasm_error(ERR_FATAL, "`%s': already defining a macro",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002773 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002774 return DIRECTIVE_FOUND;
2775 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002776 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002777 defining->max_depth =
2778 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2779 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2780 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2781 nasm_free(defining);
2782 defining = NULL;
2783 return DIRECTIVE_FOUND;
2784 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002785
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002786 src_get(&defining->xline, &defining->fname);
2787
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002788 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2789 while (mmac) {
2790 if (!strcmp(mmac->name, defining->name) &&
2791 (mmac->nparam_min <= defining->nparam_max
2792 || defining->plus)
2793 && (defining->nparam_min <= mmac->nparam_max
2794 || mmac->plus)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002795 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002796 "redefining multi-line macro `%s'", defining->name);
2797 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002798 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002799 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002800 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002801 free_tlist(origline);
2802 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002803
H. Peter Anvine2c80182005-01-15 22:15:51 +00002804 case PP_ENDM:
2805 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002806 if (! (defining && defining->name)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002807 nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002808 return DIRECTIVE_FOUND;
2809 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002810 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2811 defining->next = *mmhead;
2812 *mmhead = defining;
2813 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002814 free_tlist(origline);
2815 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002816
H. Peter Anvin89cee572009-07-15 09:16:54 -04002817 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002818 /*
2819 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002820 * macro-end marker for a macro with a name. Then we
2821 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002822 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002823 list_for_each(l, istk->expansion)
2824 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002825 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002826
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002827 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002828 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002829 * Remove all conditional entries relative to this
2830 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002831 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002832 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2833 cond = istk->conds;
2834 istk->conds = cond->next;
2835 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002836 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002837 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002838 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002839 nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002840 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002841 free_tlist(origline);
2842 return DIRECTIVE_FOUND;
2843
H. Peter Anvina26433d2008-07-16 14:40:01 -07002844 case PP_UNMACRO:
2845 case PP_UNIMACRO:
2846 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002847 MMacro **mmac_p;
2848 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002849
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002850 spec.casesense = (i == PP_UNMACRO);
2851 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2852 return DIRECTIVE_FOUND;
2853 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002854 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2855 while (mmac_p && *mmac_p) {
2856 mmac = *mmac_p;
2857 if (mmac->casesense == spec.casesense &&
2858 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2859 mmac->nparam_min == spec.nparam_min &&
2860 mmac->nparam_max == spec.nparam_max &&
2861 mmac->plus == spec.plus) {
2862 *mmac_p = mmac->next;
2863 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002864 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002865 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002866 }
2867 }
2868 free_tlist(origline);
2869 free_tlist(spec.dlist);
2870 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002871 }
2872
H. Peter Anvine2c80182005-01-15 22:15:51 +00002873 case PP_ROTATE:
2874 if (tline->next && tline->next->type == TOK_WHITESPACE)
2875 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002876 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002877 free_tlist(origline);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002878 nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002879 return DIRECTIVE_FOUND;
2880 }
2881 t = expand_smacro(tline->next);
2882 tline->next = NULL;
2883 free_tlist(origline);
2884 tline = t;
2885 tptr = &t;
2886 tokval.t_type = TOKEN_INVALID;
2887 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002888 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002889 free_tlist(tline);
2890 if (!evalresult)
2891 return DIRECTIVE_FOUND;
2892 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002893 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002894 "trailing garbage after expression ignored");
2895 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002896 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002897 return DIRECTIVE_FOUND;
2898 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002899 mmac = istk->mstk;
2900 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2901 mmac = mmac->next_active;
2902 if (!mmac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002903 nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002904 } else if (mmac->nparam == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002905 nasm_error(ERR_NONFATAL,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002906 "`%%rotate' invoked within macro without parameters");
2907 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002908 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002909
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002910 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002911 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002912 rotate += mmac->nparam;
2913
2914 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002915 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002916 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002917
H. Peter Anvine2c80182005-01-15 22:15:51 +00002918 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002919 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002920 do {
2921 tline = tline->next;
2922 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002923
H. Peter Anvine2c80182005-01-15 22:15:51 +00002924 if (tok_type_(tline, TOK_ID) &&
2925 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002926 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002927 do {
2928 tline = tline->next;
2929 } while (tok_type_(tline, TOK_WHITESPACE));
2930 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002931
H. Peter Anvine2c80182005-01-15 22:15:51 +00002932 if (tline) {
2933 t = expand_smacro(tline);
2934 tptr = &t;
2935 tokval.t_type = TOKEN_INVALID;
2936 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002937 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002938 if (!evalresult) {
2939 free_tlist(origline);
2940 return DIRECTIVE_FOUND;
2941 }
2942 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002943 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002944 "trailing garbage after expression ignored");
2945 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002946 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002947 return DIRECTIVE_FOUND;
2948 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002949 count = reloc_value(evalresult);
2950 if (count >= REP_LIMIT) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002951 nasm_error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002952 count = 0;
2953 } else
2954 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002955 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002956 nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002957 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002958 }
2959 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002960
2961 tmp_defining = defining;
2962 defining = nasm_malloc(sizeof(MMacro));
2963 defining->prev = NULL;
2964 defining->name = NULL; /* flags this macro as a %rep block */
2965 defining->casesense = false;
2966 defining->plus = false;
2967 defining->nolist = nolist;
2968 defining->in_progress = count;
2969 defining->max_depth = 0;
2970 defining->nparam_min = defining->nparam_max = 0;
2971 defining->defaults = NULL;
2972 defining->dlist = NULL;
2973 defining->expansion = NULL;
2974 defining->next_active = istk->mstk;
2975 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002976 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002977
H. Peter Anvine2c80182005-01-15 22:15:51 +00002978 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002979 if (!defining || defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002980 nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002981 return DIRECTIVE_FOUND;
2982 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002983
H. Peter Anvine2c80182005-01-15 22:15:51 +00002984 /*
2985 * Now we have a "macro" defined - although it has no name
2986 * and we won't be entering it in the hash tables - we must
2987 * push a macro-end marker for it on to istk->expansion.
2988 * After that, it will take care of propagating itself (a
2989 * macro-end marker line for a macro which is really a %rep
2990 * block will cause the macro to be re-expanded, complete
2991 * with another macro-end marker to ensure the process
2992 * continues) until the whole expansion is forcibly removed
2993 * from istk->expansion by a %exitrep.
2994 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002995 l = nasm_malloc(sizeof(Line));
2996 l->next = istk->expansion;
2997 l->finishes = defining;
2998 l->first = NULL;
2999 istk->expansion = l;
3000
3001 istk->mstk = defining;
3002
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08003003 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003004 tmp_defining = defining;
3005 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003006 free_tlist(origline);
3007 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003008
H. Peter Anvine2c80182005-01-15 22:15:51 +00003009 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003010 /*
3011 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003012 * macro-end marker for a macro with no name. Then we set
3013 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003014 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003015 list_for_each(l, istk->expansion)
3016 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003017 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003018
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003019 if (l)
3020 l->finishes->in_progress = 1;
3021 else
H. Peter Anvin130736c2016-02-17 20:27:41 -08003022 nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003023 free_tlist(origline);
3024 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003025
H. Peter Anvine2c80182005-01-15 22:15:51 +00003026 case PP_XDEFINE:
3027 case PP_IXDEFINE:
3028 case PP_DEFINE:
3029 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003030 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003031
H. Peter Anvine2c80182005-01-15 22:15:51 +00003032 tline = tline->next;
3033 skip_white_(tline);
3034 tline = expand_id(tline);
3035 if (!tline || (tline->type != TOK_ID &&
3036 (tline->type != TOK_PREPROC_ID ||
3037 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003038 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003039 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003040 free_tlist(origline);
3041 return DIRECTIVE_FOUND;
3042 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003043
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003044 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003045 last = tline;
3046 param_start = tline = tline->next;
3047 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003048
H. Peter Anvine2c80182005-01-15 22:15:51 +00003049 /* Expand the macro definition now for %xdefine and %ixdefine */
3050 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3051 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003052
H. Peter Anvine2c80182005-01-15 22:15:51 +00003053 if (tok_is_(tline, "(")) {
3054 /*
3055 * This macro has parameters.
3056 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003057
H. Peter Anvine2c80182005-01-15 22:15:51 +00003058 tline = tline->next;
3059 while (1) {
3060 skip_white_(tline);
3061 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003062 nasm_error(ERR_NONFATAL, "parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003063 free_tlist(origline);
3064 return DIRECTIVE_FOUND;
3065 }
3066 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003067 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003068 "`%s': parameter identifier expected",
3069 tline->text);
3070 free_tlist(origline);
3071 return DIRECTIVE_FOUND;
3072 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003073 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003074 tline = tline->next;
3075 skip_white_(tline);
3076 if (tok_is_(tline, ",")) {
3077 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003078 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003079 if (!tok_is_(tline, ")")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003080 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003081 "`)' expected to terminate macro template");
3082 free_tlist(origline);
3083 return DIRECTIVE_FOUND;
3084 }
3085 break;
3086 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003087 }
3088 last = tline;
3089 tline = tline->next;
3090 }
3091 if (tok_type_(tline, TOK_WHITESPACE))
3092 last = tline, tline = tline->next;
3093 macro_start = NULL;
3094 last->next = NULL;
3095 t = tline;
3096 while (t) {
3097 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003098 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003099 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003100 !strcmp(tt->text, t->text))
3101 t->type = tt->type;
3102 }
3103 tt = t->next;
3104 t->next = macro_start;
3105 macro_start = t;
3106 t = tt;
3107 }
3108 /*
3109 * Good. We now have a macro name, a parameter count, and a
3110 * token list (in reverse order) for an expansion. We ought
3111 * to be OK just to create an SMacro, store it, and let
3112 * free_tlist have the rest of the line (which we have
3113 * carefully re-terminated after chopping off the expansion
3114 * from the end).
3115 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003116 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003117 free_tlist(origline);
3118 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003119
H. Peter Anvine2c80182005-01-15 22:15:51 +00003120 case PP_UNDEF:
3121 tline = tline->next;
3122 skip_white_(tline);
3123 tline = expand_id(tline);
3124 if (!tline || (tline->type != TOK_ID &&
3125 (tline->type != TOK_PREPROC_ID ||
3126 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003127 nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003128 free_tlist(origline);
3129 return DIRECTIVE_FOUND;
3130 }
3131 if (tline->next) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003132 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003133 "trailing garbage after macro name ignored");
3134 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003135
H. Peter Anvine2c80182005-01-15 22:15:51 +00003136 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003137 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003138 undef_smacro(ctx, mname);
3139 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003140 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003141
H. Peter Anvin9e200162008-06-04 17:23:14 -07003142 case PP_DEFSTR:
3143 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003144 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003145
3146 tline = tline->next;
3147 skip_white_(tline);
3148 tline = expand_id(tline);
3149 if (!tline || (tline->type != TOK_ID &&
3150 (tline->type != TOK_PREPROC_ID ||
3151 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003152 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003153 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003154 free_tlist(origline);
3155 return DIRECTIVE_FOUND;
3156 }
3157
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003158 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003159 last = tline;
3160 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003161 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003162
3163 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003164 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003165
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003166 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003167 macro_start = nasm_malloc(sizeof(*macro_start));
3168 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003169 macro_start->text = nasm_quote(p, strlen(p));
3170 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003171 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003172 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003173
3174 /*
3175 * We now have a macro name, an implicit parameter count of
3176 * zero, and a string token to use as an expansion. Create
3177 * and store an SMacro.
3178 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003179 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003180 free_tlist(origline);
3181 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003182
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003183 case PP_DEFTOK:
3184 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003185 casesense = (i == PP_DEFTOK);
3186
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003187 tline = tline->next;
3188 skip_white_(tline);
3189 tline = expand_id(tline);
3190 if (!tline || (tline->type != TOK_ID &&
3191 (tline->type != TOK_PREPROC_ID ||
3192 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003193 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003194 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003195 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003196 free_tlist(origline);
3197 return DIRECTIVE_FOUND;
3198 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003199 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003200 last = tline;
3201 tline = expand_smacro(tline->next);
3202 last->next = NULL;
3203
3204 t = tline;
3205 while (tok_type_(t, TOK_WHITESPACE))
3206 t = t->next;
3207 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003208 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003209 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003210 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003211 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003212 free_tlist(tline);
3213 free_tlist(origline);
3214 return DIRECTIVE_FOUND;
3215 }
3216
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003217 /*
3218 * Convert the string to a token stream. Note that smacros
3219 * are stored with the token stream reversed, so we have to
3220 * reverse the output of tokenize().
3221 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003222 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003223 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003224
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003225 /*
3226 * We now have a macro name, an implicit parameter count of
3227 * zero, and a numeric token to use as an expansion. Create
3228 * and store an SMacro.
3229 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003230 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003231 free_tlist(tline);
3232 free_tlist(origline);
3233 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003234
H. Peter Anvin418ca702008-05-30 10:42:30 -07003235 case PP_PATHSEARCH:
3236 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003237 FILE *fp;
3238 StrList *xsl = NULL;
3239 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003240
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003241 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003242
3243 tline = tline->next;
3244 skip_white_(tline);
3245 tline = expand_id(tline);
3246 if (!tline || (tline->type != TOK_ID &&
3247 (tline->type != TOK_PREPROC_ID ||
3248 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003249 nasm_error(ERR_NONFATAL,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003250 "`%%pathsearch' expects a macro identifier as first parameter");
3251 free_tlist(origline);
3252 return DIRECTIVE_FOUND;
3253 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003254 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003255 last = tline;
3256 tline = expand_smacro(tline->next);
3257 last->next = NULL;
3258
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003259 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003260 while (tok_type_(t, TOK_WHITESPACE))
3261 t = t->next;
3262
3263 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003264 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003265 nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003266 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003267 free_tlist(origline);
3268 return DIRECTIVE_FOUND; /* but we did _something_ */
3269 }
3270 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003271 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003272 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003273 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003274 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003275 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003276
H. Peter Anvin66d561f2016-06-16 15:47:46 -07003277 fp = inc_fopen(p, &xsl, &xst, NULL, true, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003278 if (fp) {
3279 p = xsl->str;
3280 fclose(fp); /* Don't actually care about the file */
3281 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003282 macro_start = nasm_malloc(sizeof(*macro_start));
3283 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003284 macro_start->text = nasm_quote(p, strlen(p));
3285 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003286 macro_start->a.mac = NULL;
3287 if (xsl)
3288 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003289
3290 /*
3291 * We now have a macro name, an implicit parameter count of
3292 * zero, and a string token to use as an expansion. Create
3293 * and store an SMacro.
3294 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003295 define_smacro(ctx, mname, casesense, 0, macro_start);
3296 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003297 free_tlist(origline);
3298 return DIRECTIVE_FOUND;
3299 }
3300
H. Peter Anvine2c80182005-01-15 22:15:51 +00003301 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003302 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003303
H. Peter Anvine2c80182005-01-15 22:15:51 +00003304 tline = tline->next;
3305 skip_white_(tline);
3306 tline = expand_id(tline);
3307 if (!tline || (tline->type != TOK_ID &&
3308 (tline->type != TOK_PREPROC_ID ||
3309 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003310 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003311 "`%%strlen' expects a macro identifier as first parameter");
3312 free_tlist(origline);
3313 return DIRECTIVE_FOUND;
3314 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003315 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003316 last = tline;
3317 tline = expand_smacro(tline->next);
3318 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003319
H. Peter Anvine2c80182005-01-15 22:15:51 +00003320 t = tline;
3321 while (tok_type_(t, TOK_WHITESPACE))
3322 t = t->next;
3323 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003324 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003325 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003326 "`%%strlen` requires string as second parameter");
3327 free_tlist(tline);
3328 free_tlist(origline);
3329 return DIRECTIVE_FOUND;
3330 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003331
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003332 macro_start = nasm_malloc(sizeof(*macro_start));
3333 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003334 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003335 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003336
H. Peter Anvine2c80182005-01-15 22:15:51 +00003337 /*
3338 * We now have a macro name, an implicit parameter count of
3339 * zero, and a numeric token to use as an expansion. Create
3340 * and store an SMacro.
3341 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003342 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003343 free_tlist(tline);
3344 free_tlist(origline);
3345 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003346
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003347 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003348 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003349
3350 tline = tline->next;
3351 skip_white_(tline);
3352 tline = expand_id(tline);
3353 if (!tline || (tline->type != TOK_ID &&
3354 (tline->type != TOK_PREPROC_ID ||
3355 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003356 nasm_error(ERR_NONFATAL,
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003357 "`%%strcat' expects a macro identifier as first parameter");
3358 free_tlist(origline);
3359 return DIRECTIVE_FOUND;
3360 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003361 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003362 last = tline;
3363 tline = expand_smacro(tline->next);
3364 last->next = NULL;
3365
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003366 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003367 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003368 switch (t->type) {
3369 case TOK_WHITESPACE:
3370 break;
3371 case TOK_STRING:
3372 len += t->a.len = nasm_unquote(t->text, NULL);
3373 break;
3374 case TOK_OTHER:
3375 if (!strcmp(t->text, ",")) /* permit comma separators */
3376 break;
3377 /* else fall through */
3378 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003379 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003380 "non-string passed to `%%strcat' (%d)", t->type);
3381 free_tlist(tline);
3382 free_tlist(origline);
3383 return DIRECTIVE_FOUND;
3384 }
3385 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003386
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003387 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003388 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003389 if (t->type == TOK_STRING) {
3390 memcpy(p, t->text, t->a.len);
3391 p += t->a.len;
3392 }
3393 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003394
3395 /*
3396 * We now have a macro name, an implicit parameter count of
3397 * zero, and a numeric token to use as an expansion. Create
3398 * and store an SMacro.
3399 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003400 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3401 macro_start->text = nasm_quote(pp, len);
3402 nasm_free(pp);
3403 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003404 free_tlist(tline);
3405 free_tlist(origline);
3406 return DIRECTIVE_FOUND;
3407
H. Peter Anvine2c80182005-01-15 22:15:51 +00003408 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003409 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003410 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003411 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003412
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003413 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003414
H. Peter Anvine2c80182005-01-15 22:15:51 +00003415 tline = tline->next;
3416 skip_white_(tline);
3417 tline = expand_id(tline);
3418 if (!tline || (tline->type != TOK_ID &&
3419 (tline->type != TOK_PREPROC_ID ||
3420 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003421 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003422 "`%%substr' expects a macro identifier as first parameter");
3423 free_tlist(origline);
3424 return DIRECTIVE_FOUND;
3425 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003426 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003427 last = tline;
3428 tline = expand_smacro(tline->next);
3429 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003430
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003431 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003432 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003433 while (tok_type_(t, TOK_WHITESPACE))
3434 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003435
H. Peter Anvine2c80182005-01-15 22:15:51 +00003436 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003437 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003438 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003439 "`%%substr` requires string as second parameter");
3440 free_tlist(tline);
3441 free_tlist(origline);
3442 return DIRECTIVE_FOUND;
3443 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003444
H. Peter Anvine2c80182005-01-15 22:15:51 +00003445 tt = t->next;
3446 tptr = &tt;
3447 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003448 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003449 if (!evalresult) {
3450 free_tlist(tline);
3451 free_tlist(origline);
3452 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003453 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003454 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003455 free_tlist(tline);
3456 free_tlist(origline);
3457 return DIRECTIVE_FOUND;
3458 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003459 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003460
3461 while (tok_type_(tt, TOK_WHITESPACE))
3462 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003463 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003464 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003465 } else {
3466 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003467 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003468 if (!evalresult) {
3469 free_tlist(tline);
3470 free_tlist(origline);
3471 return DIRECTIVE_FOUND;
3472 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003473 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003474 free_tlist(tline);
3475 free_tlist(origline);
3476 return DIRECTIVE_FOUND;
3477 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003478 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003479 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003480
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003481 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003482
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003483 /* make start and count being in range */
3484 if (start < 0)
3485 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003486 if (count < 0)
3487 count = len + count + 1 - start;
3488 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003489 count = len - start;
3490 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003491 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003492
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003493 macro_start = nasm_malloc(sizeof(*macro_start));
3494 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003495 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003496 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003497 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003498
H. Peter Anvine2c80182005-01-15 22:15:51 +00003499 /*
3500 * We now have a macro name, an implicit parameter count of
3501 * zero, and a numeric token to use as an expansion. Create
3502 * and store an SMacro.
3503 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003504 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003505 free_tlist(tline);
3506 free_tlist(origline);
3507 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003508 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003509
H. Peter Anvine2c80182005-01-15 22:15:51 +00003510 case PP_ASSIGN:
3511 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003512 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003513
H. Peter Anvine2c80182005-01-15 22:15:51 +00003514 tline = tline->next;
3515 skip_white_(tline);
3516 tline = expand_id(tline);
3517 if (!tline || (tline->type != TOK_ID &&
3518 (tline->type != TOK_PREPROC_ID ||
3519 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003520 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003521 "`%%%sassign' expects a macro identifier",
3522 (i == PP_IASSIGN ? "i" : ""));
3523 free_tlist(origline);
3524 return DIRECTIVE_FOUND;
3525 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003526 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003527 last = tline;
3528 tline = expand_smacro(tline->next);
3529 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003530
H. Peter Anvine2c80182005-01-15 22:15:51 +00003531 t = tline;
3532 tptr = &t;
3533 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003534 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003535 free_tlist(tline);
3536 if (!evalresult) {
3537 free_tlist(origline);
3538 return DIRECTIVE_FOUND;
3539 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003540
H. Peter Anvine2c80182005-01-15 22:15:51 +00003541 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003542 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003543 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003544
H. Peter Anvine2c80182005-01-15 22:15:51 +00003545 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003546 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003547 "non-constant value given to `%%%sassign'",
3548 (i == PP_IASSIGN ? "i" : ""));
3549 free_tlist(origline);
3550 return DIRECTIVE_FOUND;
3551 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003552
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003553 macro_start = nasm_malloc(sizeof(*macro_start));
3554 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003555 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003556 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003557
H. Peter Anvine2c80182005-01-15 22:15:51 +00003558 /*
3559 * We now have a macro name, an implicit parameter count of
3560 * zero, and a numeric token to use as an expansion. Create
3561 * and store an SMacro.
3562 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003563 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003564 free_tlist(origline);
3565 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003566
H. Peter Anvine2c80182005-01-15 22:15:51 +00003567 case PP_LINE:
3568 /*
3569 * Syntax is `%line nnn[+mmm] [filename]'
3570 */
3571 tline = tline->next;
3572 skip_white_(tline);
3573 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003574 nasm_error(ERR_NONFATAL, "`%%line' expects line number");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003575 free_tlist(origline);
3576 return DIRECTIVE_FOUND;
3577 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003578 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003579 m = 1;
3580 tline = tline->next;
3581 if (tok_is_(tline, "+")) {
3582 tline = tline->next;
3583 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003584 nasm_error(ERR_NONFATAL, "`%%line' expects line increment");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003585 free_tlist(origline);
3586 return DIRECTIVE_FOUND;
3587 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003588 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003589 tline = tline->next;
3590 }
3591 skip_white_(tline);
3592 src_set_linnum(k);
3593 istk->lineinc = m;
3594 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003595 char *fname = detoken(tline, false);
3596 src_set_fname(fname);
3597 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003598 }
3599 free_tlist(origline);
3600 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003601
H. Peter Anvine2c80182005-01-15 22:15:51 +00003602 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003603 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003604 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003605 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003606 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003607 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003608}
3609
3610/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003611 * Ensure that a macro parameter contains a condition code and
3612 * nothing else. Return the condition code index if so, or -1
3613 * otherwise.
3614 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003615static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003616{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003617 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003618
H. Peter Anvin25a99342007-09-22 17:45:45 -07003619 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003620 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003621
H. Peter Anvineba20a72002-04-30 20:53:55 +00003622 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003623 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003624 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003625 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003626 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003627 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003628 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003629
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003630 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003631}
3632
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003633/*
3634 * This routines walks over tokens strem and hadnles tokens
3635 * pasting, if @handle_explicit passed then explicit pasting
3636 * term is handled, otherwise -- implicit pastings only.
3637 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003638static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003639 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003640{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003641 Token *tok, *next, **prev_next, **prev_nonspace;
3642 bool pasted = false;
3643 char *buf, *p;
3644 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003645
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003646 /*
3647 * The last token before pasting. We need it
3648 * to be able to connect new handled tokens.
3649 * In other words if there were a tokens stream
3650 *
3651 * A -> B -> C -> D
3652 *
3653 * and we've joined tokens B and C, the resulting
3654 * stream should be
3655 *
3656 * A -> BC -> D
3657 */
3658 tok = *head;
3659 prev_next = NULL;
3660
3661 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3662 prev_nonspace = head;
3663 else
3664 prev_nonspace = NULL;
3665
3666 while (tok && (next = tok->next)) {
3667
3668 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003669 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003670 /* Zap redundant whitespaces */
3671 while (tok_type_(next, TOK_WHITESPACE))
3672 next = delete_Token(next);
3673 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003674 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003675
3676 case TOK_PASTE:
3677 /* Explicit pasting */
3678 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003679 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003680 next = delete_Token(tok);
3681
3682 while (tok_type_(next, TOK_WHITESPACE))
3683 next = delete_Token(next);
3684
3685 if (!pasted)
3686 pasted = true;
3687
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003688 /* Left pasting token is start of line */
3689 if (!prev_nonspace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003690 nasm_error(ERR_FATAL, "No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003691
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003692 /*
3693 * No ending token, this might happen in two
3694 * cases
3695 *
3696 * 1) There indeed no right token at all
3697 * 2) There is a bare "%define ID" statement,
3698 * and @ID does expand to whitespace.
3699 *
3700 * So technically we need to do a grammar analysis
3701 * in another stage of parsing, but for now lets don't
3702 * change the behaviour people used to. Simply allow
3703 * whitespace after paste token.
3704 */
3705 if (!next) {
3706 /*
3707 * Zap ending space tokens and that's all.
3708 */
3709 tok = (*prev_nonspace)->next;
3710 while (tok_type_(tok, TOK_WHITESPACE))
3711 tok = delete_Token(tok);
3712 tok = *prev_nonspace;
3713 tok->next = NULL;
3714 break;
3715 }
3716
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003717 tok = *prev_nonspace;
3718 while (tok_type_(tok, TOK_WHITESPACE))
3719 tok = delete_Token(tok);
3720 len = strlen(tok->text);
3721 len += strlen(next->text);
3722
3723 p = buf = nasm_malloc(len + 1);
3724 strcpy(p, tok->text);
3725 p = strchr(p, '\0');
3726 strcpy(p, next->text);
3727
3728 delete_Token(tok);
3729
3730 tok = tokenize(buf);
3731 nasm_free(buf);
3732
3733 *prev_nonspace = tok;
3734 while (tok && tok->next)
3735 tok = tok->next;
3736
3737 tok->next = delete_Token(next);
3738
3739 /* Restart from pasted tokens head */
3740 tok = *prev_nonspace;
3741 break;
3742
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003743 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003744 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003745 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003746 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3747 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003748
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003749 len = 0;
3750 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3751 len += strlen(next->text);
3752 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003753 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003754
3755 /* No match */
3756 if (tok == next)
3757 break;
3758
3759 len += strlen(tok->text);
3760 p = buf = nasm_malloc(len + 1);
3761
3762 while (tok != next) {
3763 strcpy(p, tok->text);
3764 p = strchr(p, '\0');
3765 tok = delete_Token(tok);
3766 }
3767
3768 tok = tokenize(buf);
3769 nasm_free(buf);
3770
3771 if (prev_next)
3772 *prev_next = tok;
3773 else
3774 *head = tok;
3775
3776 /*
3777 * Connect pasted into original stream,
3778 * ie A -> new-tokens -> B
3779 */
3780 while (tok && tok->next)
3781 tok = tok->next;
3782 tok->next = next;
3783
3784 if (!pasted)
3785 pasted = true;
3786
3787 /* Restart from pasted tokens head */
3788 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003789 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003790
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003791 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003792 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003793
3794 prev_next = &tok->next;
3795
3796 if (tok->next &&
3797 !tok_type_(tok->next, TOK_WHITESPACE) &&
3798 !tok_type_(tok->next, TOK_PASTE))
3799 prev_nonspace = prev_next;
3800
3801 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003802 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003803
3804 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003805}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003806
3807/*
3808 * expands to a list of tokens from %{x:y}
3809 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003810static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003811{
3812 Token *t = tline, **tt, *tm, *head;
3813 char *pos;
3814 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003815
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003816 pos = strchr(tline->text, ':');
3817 nasm_assert(pos);
3818
3819 lst = atoi(pos + 1);
3820 fst = atoi(tline->text + 1);
3821
3822 /*
3823 * only macros params are accounted so
3824 * if someone passes %0 -- we reject such
3825 * value(s)
3826 */
3827 if (lst == 0 || fst == 0)
3828 goto err;
3829
3830 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003831 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3832 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003833 goto err;
3834
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003835 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3836 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003837
3838 /* counted from zero */
3839 fst--, lst--;
3840
3841 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003842 * It will be at least one token. Note we
3843 * need to scan params until separator, otherwise
3844 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003845 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003846 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003847 head = new_Token(NULL, tm->type, tm->text, 0);
3848 tt = &head->next, tm = tm->next;
3849 while (tok_isnt_(tm, ",")) {
3850 t = new_Token(NULL, tm->type, tm->text, 0);
3851 *tt = t, tt = &t->next, tm = tm->next;
3852 }
3853
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003854 if (fst < lst) {
3855 for (i = fst + 1; i <= lst; i++) {
3856 t = new_Token(NULL, TOK_OTHER, ",", 0);
3857 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003858 j = (i + mac->rotate) % mac->nparam;
3859 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003860 while (tok_isnt_(tm, ",")) {
3861 t = new_Token(NULL, tm->type, tm->text, 0);
3862 *tt = t, tt = &t->next, tm = tm->next;
3863 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003864 }
3865 } else {
3866 for (i = fst - 1; i >= lst; i--) {
3867 t = new_Token(NULL, TOK_OTHER, ",", 0);
3868 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003869 j = (i + mac->rotate) % mac->nparam;
3870 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003871 while (tok_isnt_(tm, ",")) {
3872 t = new_Token(NULL, tm->type, tm->text, 0);
3873 *tt = t, tt = &t->next, tm = tm->next;
3874 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003875 }
3876 }
3877
3878 *last = tt;
3879 return head;
3880
3881err:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003882 nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003883 &tline->text[1]);
3884 return tline;
3885}
3886
H. Peter Anvin76690a12002-04-30 20:52:49 +00003887/*
3888 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003889 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003890 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003891 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003892static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003893{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003894 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003895 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003896 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003897
3898 tail = &thead;
3899 thead = NULL;
3900
H. Peter Anvine2c80182005-01-15 22:15:51 +00003901 while (tline) {
3902 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003903 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3904 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3905 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003906 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003907 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003908 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003909 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003910 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003911 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003912
H. Peter Anvine2c80182005-01-15 22:15:51 +00003913 t = tline;
3914 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003915
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003916 mac = istk->mstk;
3917 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3918 mac = mac->next_active;
3919 if (!mac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003920 nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003921 } else {
3922 pos = strchr(t->text, ':');
3923 if (!pos) {
3924 switch (t->text[1]) {
3925 /*
3926 * We have to make a substitution of one of the
3927 * forms %1, %-1, %+1, %%foo, %0.
3928 */
3929 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003930 type = TOK_NUMBER;
3931 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3932 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003933 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003934 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003935 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003936 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003937 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003938 text = nasm_strcat(tmpbuf, t->text + 2);
3939 break;
3940 case '-':
3941 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003942 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003943 tt = NULL;
3944 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003945 if (mac->nparam > 1)
3946 n = (n + mac->rotate) % mac->nparam;
3947 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003948 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003949 cc = find_cc(tt);
3950 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003951 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003952 "macro parameter %d is not a condition code",
3953 n + 1);
3954 text = NULL;
3955 } else {
3956 type = TOK_ID;
3957 if (inverse_ccs[cc] == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003958 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003959 "condition code `%s' is not invertible",
3960 conditions[cc]);
3961 text = NULL;
3962 } else
3963 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3964 }
3965 break;
3966 case '+':
3967 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003968 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003969 tt = NULL;
3970 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003971 if (mac->nparam > 1)
3972 n = (n + mac->rotate) % mac->nparam;
3973 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003974 }
3975 cc = find_cc(tt);
3976 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003977 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003978 "macro parameter %d is not a condition code",
3979 n + 1);
3980 text = NULL;
3981 } else {
3982 type = TOK_ID;
3983 text = nasm_strdup(conditions[cc]);
3984 }
3985 break;
3986 default:
3987 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003988 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003989 tt = NULL;
3990 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003991 if (mac->nparam > 1)
3992 n = (n + mac->rotate) % mac->nparam;
3993 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003994 }
3995 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003996 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003997 *tail = new_Token(NULL, tt->type, tt->text, 0);
3998 tail = &(*tail)->next;
3999 tt = tt->next;
4000 }
4001 }
4002 text = NULL; /* we've done it here */
4003 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004004 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004005 } else {
4006 /*
4007 * seems we have a parameters range here
4008 */
4009 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004010 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004011 if (head != t) {
4012 *tail = head;
4013 *last = tline;
4014 tline = head;
4015 text = NULL;
4016 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004017 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004018 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004019 if (!text) {
4020 delete_Token(t);
4021 } else {
4022 *tail = t;
4023 tail = &t->next;
4024 t->type = type;
4025 nasm_free(t->text);
4026 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004027 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004028 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004029 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004030 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004031 } else if (tline->type == TOK_INDIRECT) {
4032 t = tline;
4033 tline = tline->next;
4034 tt = tokenize(t->text);
4035 tt = expand_mmac_params(tt);
4036 tt = expand_smacro(tt);
4037 *tail = tt;
4038 while (tt) {
4039 tt->a.mac = NULL; /* Necessary? */
4040 tail = &tt->next;
4041 tt = tt->next;
4042 }
4043 delete_Token(t);
4044 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004045 } else {
4046 t = *tail = tline;
4047 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004048 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004049 tail = &t->next;
4050 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004051 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004052 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004053
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004054 if (changed) {
4055 const struct tokseq_match t[] = {
4056 {
4057 PP_CONCAT_MASK(TOK_ID) |
4058 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4059 PP_CONCAT_MASK(TOK_ID) |
4060 PP_CONCAT_MASK(TOK_NUMBER) |
4061 PP_CONCAT_MASK(TOK_FLOAT) |
4062 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4063 },
4064 {
4065 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4066 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4067 }
4068 };
4069 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4070 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004071
H. Peter Anvin76690a12002-04-30 20:52:49 +00004072 return thead;
4073}
4074
4075/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004076 * Expand all single-line macro calls made in the given line.
4077 * Return the expanded version of the line. The original is deemed
4078 * to be destroyed in the process. (In reality we'll just move
4079 * Tokens from input to output a lot of the time, rather than
4080 * actually bothering to destroy and replicate.)
4081 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004082
H. Peter Anvine2c80182005-01-15 22:15:51 +00004083static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004084{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004085 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004086 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004087 Token **params;
4088 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004089 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004090 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004091 Token *org_tline = tline;
4092 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004093 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004094 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004095 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004096
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004097 /*
4098 * Trick: we should avoid changing the start token pointer since it can
4099 * be contained in "next" field of other token. Because of this
4100 * we allocate a copy of first token and work with it; at the end of
4101 * routine we copy it back
4102 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004103 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004104 tline = new_Token(org_tline->next, org_tline->type,
4105 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004106 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004107 nasm_free(org_tline->text);
4108 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004109 }
4110
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004111 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004112
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004113again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004114 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004115 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004116
H. Peter Anvine2c80182005-01-15 22:15:51 +00004117 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004118 if (!--deadman) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004119 nasm_error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004120 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004121 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004122
H. Peter Anvine2c80182005-01-15 22:15:51 +00004123 if ((mname = tline->text)) {
4124 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004125 if (tline->type == TOK_ID) {
4126 head = (SMacro *)hash_findix(&smacros, mname);
4127 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004128 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004129 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4130 } else
4131 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004132
H. Peter Anvine2c80182005-01-15 22:15:51 +00004133 /*
4134 * We've hit an identifier. As in is_mmacro below, we first
4135 * check whether the identifier is a single-line macro at
4136 * all, then think about checking for parameters if
4137 * necessary.
4138 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004139 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004140 if (!mstrcmp(m->name, mname, m->casesense))
4141 break;
4142 if (m) {
4143 mstart = tline;
4144 params = NULL;
4145 paramsize = NULL;
4146 if (m->nparam == 0) {
4147 /*
4148 * Simple case: the macro is parameterless. Discard the
4149 * one token that the macro call took, and push the
4150 * expansion back on the to-do stack.
4151 */
4152 if (!m->expansion) {
4153 if (!strcmp("__FILE__", m->name)) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004154 const char *file = src_get_fname();
4155 /* nasm_free(tline->text); here? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004156 tline->text = nasm_quote(file, strlen(file));
4157 tline->type = TOK_STRING;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004158 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004159 }
4160 if (!strcmp("__LINE__", m->name)) {
4161 nasm_free(tline->text);
4162 make_tok_num(tline, src_get_linnum());
4163 continue;
4164 }
4165 if (!strcmp("__BITS__", m->name)) {
4166 nasm_free(tline->text);
4167 make_tok_num(tline, globalbits);
4168 continue;
4169 }
4170 tline = delete_Token(tline);
4171 continue;
4172 }
4173 } else {
4174 /*
4175 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004176 * exists and takes parameters. We must find the
4177 * parameters in the call, count them, find the SMacro
4178 * that corresponds to that form of the macro call, and
4179 * substitute for the parameters when we expand. What a
4180 * pain.
4181 */
4182 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004183 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004184 do {
4185 t = tline->next;
4186 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004187 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004188 t->text = NULL;
4189 t = tline->next = delete_Token(t);
4190 }
4191 tline = t;
4192 } while (tok_type_(tline, TOK_WHITESPACE));
4193 if (!tok_is_(tline, "(")) {
4194 /*
4195 * This macro wasn't called with parameters: ignore
4196 * the call. (Behaviour borrowed from gnu cpp.)
4197 */
4198 tline = mstart;
4199 m = NULL;
4200 } else {
4201 int paren = 0;
4202 int white = 0;
4203 brackets = 0;
4204 nparam = 0;
4205 sparam = PARAM_DELTA;
4206 params = nasm_malloc(sparam * sizeof(Token *));
4207 params[0] = tline->next;
4208 paramsize = nasm_malloc(sparam * sizeof(int));
4209 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004210 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004211 /*
4212 * For some unusual expansions
4213 * which concatenates function call
4214 */
4215 t = tline->next;
4216 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004217 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004218 t->text = NULL;
4219 t = tline->next = delete_Token(t);
4220 }
4221 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004222
H. Peter Anvine2c80182005-01-15 22:15:51 +00004223 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004224 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004225 "macro call expects terminating `)'");
4226 break;
4227 }
4228 if (tline->type == TOK_WHITESPACE
4229 && brackets <= 0) {
4230 if (paramsize[nparam])
4231 white++;
4232 else
4233 params[nparam] = tline->next;
4234 continue; /* parameter loop */
4235 }
4236 if (tline->type == TOK_OTHER
4237 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004238 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004239 if (ch == ',' && !paren && brackets <= 0) {
4240 if (++nparam >= sparam) {
4241 sparam += PARAM_DELTA;
4242 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004243 sparam * sizeof(Token *));
4244 paramsize = nasm_realloc(paramsize,
4245 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004246 }
4247 params[nparam] = tline->next;
4248 paramsize[nparam] = 0;
4249 white = 0;
4250 continue; /* parameter loop */
4251 }
4252 if (ch == '{' &&
4253 (brackets > 0 || (brackets == 0 &&
4254 !paramsize[nparam])))
4255 {
4256 if (!(brackets++)) {
4257 params[nparam] = tline->next;
4258 continue; /* parameter loop */
4259 }
4260 }
4261 if (ch == '}' && brackets > 0)
4262 if (--brackets == 0) {
4263 brackets = -1;
4264 continue; /* parameter loop */
4265 }
4266 if (ch == '(' && !brackets)
4267 paren++;
4268 if (ch == ')' && brackets <= 0)
4269 if (--paren < 0)
4270 break;
4271 }
4272 if (brackets < 0) {
4273 brackets = 0;
H. Peter Anvin130736c2016-02-17 20:27:41 -08004274 nasm_error(ERR_NONFATAL, "braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004275 "enclose all of macro parameter");
4276 }
4277 paramsize[nparam] += white + 1;
4278 white = 0;
4279 } /* parameter loop */
4280 nparam++;
4281 while (m && (m->nparam != nparam ||
4282 mstrcmp(m->name, mname,
4283 m->casesense)))
4284 m = m->next;
4285 if (!m)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004286 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004287 "macro `%s' exists, "
4288 "but not taking %d parameters",
4289 mstart->text, nparam);
4290 }
4291 }
4292 if (m && m->in_progress)
4293 m = NULL;
4294 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004295 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004296 * Design question: should we handle !tline, which
4297 * indicates missing ')' here, or expand those
4298 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004299 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004300 */
4301 nasm_free(params);
4302 nasm_free(paramsize);
4303 tline = mstart;
4304 } else {
4305 /*
4306 * Expand the macro: we are placed on the last token of the
4307 * call, so that we can easily split the call from the
4308 * following tokens. We also start by pushing an SMAC_END
4309 * token for the cycle removal.
4310 */
4311 t = tline;
4312 if (t) {
4313 tline = t->next;
4314 t->next = NULL;
4315 }
4316 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004317 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004318 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004319 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004320 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004321 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004322 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004323 Token *ttt, *pt;
4324 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004325
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004326 ttt = params[t->type - TOK_SMAC_PARAM];
4327 i = paramsize[t->type - TOK_SMAC_PARAM];
4328 while (--i >= 0) {
4329 pt = *ptail = new_Token(tline, ttt->type,
4330 ttt->text, 0);
4331 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004332 ttt = ttt->next;
4333 }
4334 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004335 } else if (t->type == TOK_PREPROC_Q) {
4336 tt = new_Token(tline, TOK_ID, mname, 0);
4337 tline = tt;
4338 } else if (t->type == TOK_PREPROC_QQ) {
4339 tt = new_Token(tline, TOK_ID, m->name, 0);
4340 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004341 } else {
4342 tt = new_Token(tline, t->type, t->text, 0);
4343 tline = tt;
4344 }
4345 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004346
H. Peter Anvine2c80182005-01-15 22:15:51 +00004347 /*
4348 * Having done that, get rid of the macro call, and clean
4349 * up the parameters.
4350 */
4351 nasm_free(params);
4352 nasm_free(paramsize);
4353 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004354 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004355 continue; /* main token loop */
4356 }
4357 }
4358 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004359
H. Peter Anvine2c80182005-01-15 22:15:51 +00004360 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004361 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004362 tline = delete_Token(tline);
4363 } else {
4364 t = *tail = tline;
4365 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004366 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004367 t->next = NULL;
4368 tail = &t->next;
4369 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004370 }
4371
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004372 /*
4373 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004374 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004375 * TOK_IDs should be concatenated.
4376 * Also we look for %+ tokens and concatenate the tokens before and after
4377 * them (without white spaces in between).
4378 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004379 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004380 const struct tokseq_match t[] = {
4381 {
4382 PP_CONCAT_MASK(TOK_ID) |
4383 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4384 PP_CONCAT_MASK(TOK_ID) |
4385 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4386 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4387 }
4388 };
4389 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004390 /*
4391 * If we concatenated something, *and* we had previously expanded
4392 * an actual macro, scan the lines again for macros...
4393 */
4394 tline = thead;
4395 expanded = false;
4396 goto again;
4397 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004398 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004399
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004400err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004401 if (org_tline) {
4402 if (thead) {
4403 *org_tline = *thead;
4404 /* since we just gave text to org_line, don't free it */
4405 thead->text = NULL;
4406 delete_Token(thead);
4407 } else {
4408 /* the expression expanded to empty line;
4409 we can't return NULL for some reasons
4410 we just set the line to a single WHITESPACE token. */
4411 memset(org_tline, 0, sizeof(*org_tline));
4412 org_tline->text = NULL;
4413 org_tline->type = TOK_WHITESPACE;
4414 }
4415 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004416 }
4417
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004418 return thead;
4419}
4420
4421/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004422 * Similar to expand_smacro but used exclusively with macro identifiers
4423 * right before they are fetched in. The reason is that there can be
4424 * identifiers consisting of several subparts. We consider that if there
4425 * are more than one element forming the name, user wants a expansion,
4426 * otherwise it will be left as-is. Example:
4427 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004428 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004429 *
4430 * the identifier %$abc will be left as-is so that the handler for %define
4431 * will suck it and define the corresponding value. Other case:
4432 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004433 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004434 *
4435 * In this case user wants name to be expanded *before* %define starts
4436 * working, so we'll expand %$abc into something (if it has a value;
4437 * otherwise it will be left as-is) then concatenate all successive
4438 * PP_IDs into one.
4439 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004440static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004441{
4442 Token *cur, *oldnext = NULL;
4443
H. Peter Anvin734b1882002-04-30 21:01:08 +00004444 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004445 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004446
4447 cur = tline;
4448 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004449 (cur->next->type == TOK_ID ||
4450 cur->next->type == TOK_PREPROC_ID
4451 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004452 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004453
4454 /* If identifier consists of just one token, don't expand */
4455 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004456 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004457
H. Peter Anvine2c80182005-01-15 22:15:51 +00004458 if (cur) {
4459 oldnext = cur->next; /* Detach the tail past identifier */
4460 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004461 }
4462
H. Peter Anvin734b1882002-04-30 21:01:08 +00004463 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004464
H. Peter Anvine2c80182005-01-15 22:15:51 +00004465 if (cur) {
4466 /* expand_smacro possibly changhed tline; re-scan for EOL */
4467 cur = tline;
4468 while (cur && cur->next)
4469 cur = cur->next;
4470 if (cur)
4471 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004472 }
4473
4474 return tline;
4475}
4476
4477/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004478 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004479 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004480 * to check for an initial label - that's taken care of in
4481 * expand_mmacro - but must check numbers of parameters. Guaranteed
4482 * to be called with tline->type == TOK_ID, so the putative macro
4483 * name is easy to find.
4484 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004485static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004486{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004487 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004488 Token **params;
4489 int nparam;
4490
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004491 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004492
4493 /*
4494 * Efficiency: first we see if any macro exists with the given
4495 * name. If not, we can return NULL immediately. _Then_ we
4496 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004497 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004498 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004499 list_for_each(m, head)
4500 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004501 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004502 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004503 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004504
4505 /*
4506 * OK, we have a potential macro. Count and demarcate the
4507 * parameters.
4508 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004509 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004510
4511 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004512 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004513 * structure that handles this number.
4514 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004515 while (m) {
4516 if (m->nparam_min <= nparam
4517 && (m->plus || nparam <= m->nparam_max)) {
4518 /*
4519 * This one is right. Just check if cycle removal
4520 * prohibits us using it before we actually celebrate...
4521 */
4522 if (m->in_progress > m->max_depth) {
4523 if (m->max_depth > 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004524 nasm_error(ERR_WARNING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004525 "reached maximum recursion depth of %i",
4526 m->max_depth);
4527 }
4528 nasm_free(params);
4529 return NULL;
4530 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004531 /*
4532 * It's right, and we can use it. Add its default
4533 * parameters to the end of our list if necessary.
4534 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004535 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004536 params =
4537 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004538 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004539 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004540 while (nparam < m->nparam_min + m->ndefs) {
4541 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004542 nparam++;
4543 }
4544 }
4545 /*
4546 * If we've gone over the maximum parameter count (and
4547 * we're in Plus mode), ignore parameters beyond
4548 * nparam_max.
4549 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004550 if (m->plus && nparam > m->nparam_max)
4551 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004552 /*
4553 * Then terminate the parameter list, and leave.
4554 */
4555 if (!params) { /* need this special case */
4556 params = nasm_malloc(sizeof(*params));
4557 nparam = 0;
4558 }
4559 params[nparam] = NULL;
4560 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004561 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004562 }
4563 /*
4564 * This one wasn't right: look for the next one with the
4565 * same name.
4566 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004567 list_for_each(m, m->next)
4568 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004569 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004570 }
4571
4572 /*
4573 * After all that, we didn't find one with the right number of
4574 * parameters. Issue a warning, and fail to expand the macro.
4575 */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004576 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004577 "macro `%s' exists, but not taking %d parameters",
4578 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004579 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004580 return NULL;
4581}
4582
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004583
4584/*
4585 * Save MMacro invocation specific fields in
4586 * preparation for a recursive macro expansion
4587 */
4588static void push_mmacro(MMacro *m)
4589{
4590 MMacroInvocation *i;
4591
4592 i = nasm_malloc(sizeof(MMacroInvocation));
4593 i->prev = m->prev;
4594 i->params = m->params;
4595 i->iline = m->iline;
4596 i->nparam = m->nparam;
4597 i->rotate = m->rotate;
4598 i->paramlen = m->paramlen;
4599 i->unique = m->unique;
4600 i->condcnt = m->condcnt;
4601 m->prev = i;
4602}
4603
4604
4605/*
4606 * Restore MMacro invocation specific fields that were
4607 * saved during a previous recursive macro expansion
4608 */
4609static void pop_mmacro(MMacro *m)
4610{
4611 MMacroInvocation *i;
4612
4613 if (m->prev) {
4614 i = m->prev;
4615 m->prev = i->prev;
4616 m->params = i->params;
4617 m->iline = i->iline;
4618 m->nparam = i->nparam;
4619 m->rotate = i->rotate;
4620 m->paramlen = i->paramlen;
4621 m->unique = i->unique;
4622 m->condcnt = i->condcnt;
4623 nasm_free(i);
4624 }
4625}
4626
4627
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004628/*
4629 * Expand the multi-line macro call made by the given line, if
4630 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004631 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004632 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004633static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004634{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004635 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004636 Token *label = NULL;
4637 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004638 Token **params, *t, *tt;
4639 MMacro *m;
4640 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004641 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004642 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004643
4644 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004645 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004646 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004647 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004648 return 0;
4649 m = is_mmacro(t, &params);
4650 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004651 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004652 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004653 Token *last;
4654 /*
4655 * We have an id which isn't a macro call. We'll assume
4656 * it might be a label; we'll also check to see if a
4657 * colon follows it. Then, if there's another id after
4658 * that lot, we'll check it again for macro-hood.
4659 */
4660 label = last = t;
4661 t = t->next;
4662 if (tok_type_(t, TOK_WHITESPACE))
4663 last = t, t = t->next;
4664 if (tok_is_(t, ":")) {
4665 dont_prepend = 1;
4666 last = t, t = t->next;
4667 if (tok_type_(t, TOK_WHITESPACE))
4668 last = t, t = t->next;
4669 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004670 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4671 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004672 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004673 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004674 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004675 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004676
4677 /*
4678 * Fix up the parameters: this involves stripping leading and
4679 * trailing whitespace, then stripping braces if they are
4680 * present.
4681 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004682 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004683 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004684
H. Peter Anvine2c80182005-01-15 22:15:51 +00004685 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004686 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004687 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004688
H. Peter Anvine2c80182005-01-15 22:15:51 +00004689 t = params[i];
4690 skip_white_(t);
4691 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004692 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004693 params[i] = t;
4694 paramlen[i] = 0;
4695 while (t) {
4696 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4697 break; /* ... because we have hit a comma */
4698 if (comma && t->type == TOK_WHITESPACE
4699 && tok_is_(t->next, ","))
4700 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004701 if (brace && t->type == TOK_OTHER) {
4702 if (t->text[0] == '{')
4703 brace++; /* ... or a nested opening brace */
4704 else if (t->text[0] == '}')
4705 if (!--brace)
4706 break; /* ... or a brace */
4707 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004708 t = t->next;
4709 paramlen[i]++;
4710 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004711 if (brace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004712 nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004713 }
4714
4715 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004716 * OK, we have a MMacro structure together with a set of
4717 * parameters. We must now go through the expansion and push
4718 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004719 * parameter tokens and macro-local tokens doesn't get done
4720 * until the single-line macro substitution process; this is
4721 * because delaying them allows us to change the semantics
4722 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004723 *
4724 * First, push an end marker on to istk->expansion, mark this
4725 * macro as in progress, and set up its invocation-specific
4726 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004727 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004728 ll = nasm_malloc(sizeof(Line));
4729 ll->next = istk->expansion;
4730 ll->finishes = m;
4731 ll->first = NULL;
4732 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004733
4734 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004735 * Save the previous MMacro expansion in the case of
4736 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004737 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004738 if (m->max_depth && m->in_progress)
4739 push_mmacro(m);
4740
4741 m->in_progress ++;
4742 m->params = params;
4743 m->iline = tline;
4744 m->nparam = nparam;
4745 m->rotate = 0;
4746 m->paramlen = paramlen;
4747 m->unique = unique++;
4748 m->lineno = 0;
4749 m->condcnt = 0;
4750
4751 m->next_active = istk->mstk;
4752 istk->mstk = m;
4753
4754 list_for_each(l, m->expansion) {
4755 Token **tail;
4756
4757 ll = nasm_malloc(sizeof(Line));
4758 ll->finishes = NULL;
4759 ll->next = istk->expansion;
4760 istk->expansion = ll;
4761 tail = &ll->first;
4762
4763 list_for_each(t, l->first) {
4764 Token *x = t;
4765 switch (t->type) {
4766 case TOK_PREPROC_Q:
4767 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004768 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004769 case TOK_PREPROC_QQ:
4770 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4771 break;
4772 case TOK_PREPROC_ID:
4773 if (t->text[1] == '0' && t->text[2] == '0') {
4774 dont_prepend = -1;
4775 x = label;
4776 if (!x)
4777 continue;
4778 }
4779 /* fall through */
4780 default:
4781 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4782 break;
4783 }
4784 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004785 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004786 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004787 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004788
4789 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004790 * If we had a label, push it on as the first line of
4791 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004792 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004793 if (label) {
4794 if (dont_prepend < 0)
4795 free_tlist(startline);
4796 else {
4797 ll = nasm_malloc(sizeof(Line));
4798 ll->finishes = NULL;
4799 ll->next = istk->expansion;
4800 istk->expansion = ll;
4801 ll->first = startline;
4802 if (!dont_prepend) {
4803 while (label->next)
4804 label = label->next;
4805 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004806 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004807 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004808 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004809
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004810 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004811
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004812 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004813}
4814
H. Peter Anvin130736c2016-02-17 20:27:41 -08004815/*
4816 * This function adds macro names to error messages, and suppresses
4817 * them if necessary.
4818 */
4819static void pp_verror(int severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004820{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004821 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004822 MMacro *mmac = NULL;
4823 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004824
H. Peter Anvin130736c2016-02-17 20:27:41 -08004825 /*
4826 * If we're in a dead branch of IF or something like it, ignore the error.
4827 * However, because %else etc are evaluated in the state context
4828 * of the previous branch, errors might get lost:
4829 * %if 0 ... %else trailing garbage ... %endif
4830 * So %else etc should set the ERR_PP_PRECOND flag.
4831 */
4832 if ((severity & ERR_MASK) < ERR_FATAL &&
4833 istk && istk->conds &&
4834 ((severity & ERR_PP_PRECOND) ?
4835 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004836 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004837 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004838
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004839 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004840 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004841 mmac = istk->mstk;
4842 /* but %rep blocks should be skipped */
4843 while (mmac && !mmac->name)
4844 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004845 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004846
H. Peter Anvin130736c2016-02-17 20:27:41 -08004847 if (mmac) {
4848 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004849
H. Peter Anvin130736c2016-02-17 20:27:41 -08004850 nasm_set_verror(real_verror);
4851 nasm_error(severity, "(%s:%d) %s",
4852 mmac->name, mmac->lineno - delta, buff);
4853 nasm_set_verror(pp_verror);
4854 } else {
4855 real_verror(severity, fmt, arg);
4856 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004857}
4858
H. Peter Anvin734b1882002-04-30 21:01:08 +00004859static void
H. Peter Anvin130736c2016-02-17 20:27:41 -08004860pp_reset(char *file, int apass, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004861{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004862 Token *t;
4863
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004864 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004865 istk = nasm_malloc(sizeof(Include));
4866 istk->next = NULL;
4867 istk->conds = NULL;
4868 istk->expansion = NULL;
4869 istk->mstk = NULL;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07004870 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004871 istk->fname = NULL;
H. Peter Anvin274cda82016-05-10 02:56:29 -07004872 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004873 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004874 if (!istk->fp)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004875 nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004876 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004877 nested_mac_count = 0;
4878 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004879 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004880 unique = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004881 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004882 stdmacpos = nasm_stdmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004883 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004884 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004885 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004886 any_extrastdmac = extrastdmac && *extrastdmac;
4887 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004888
4889 /*
4890 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4891 * The caller, however, will also pass in 3 for preprocess-only so
4892 * we can set __PASS__ accordingly.
4893 */
4894 pass = apass > 2 ? 2 : apass;
4895
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004896 dephead = deptail = deplist;
4897 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004898 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4899 sl->next = NULL;
4900 strcpy(sl->str, file);
4901 *deptail = sl;
4902 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004903 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004904
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004905 /*
4906 * Define the __PASS__ macro. This is defined here unlike
4907 * all the other builtins, because it is special -- it varies between
4908 * passes.
4909 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004910 t = nasm_malloc(sizeof(*t));
4911 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004912 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004913 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004914 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004915}
4916
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004917static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004918{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004919 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004920 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004921
H. Peter Anvin130736c2016-02-17 20:27:41 -08004922 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08004923
H. Peter Anvine2c80182005-01-15 22:15:51 +00004924 while (1) {
4925 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004926 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004927 * buffer or from the input file.
4928 */
4929 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004930 while (istk->expansion && istk->expansion->finishes) {
4931 Line *l = istk->expansion;
4932 if (!l->finishes->name && l->finishes->in_progress > 1) {
4933 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004934
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004935 /*
4936 * This is a macro-end marker for a macro with no
4937 * name, which means it's not really a macro at all
4938 * but a %rep block, and the `in_progress' field is
4939 * more than 1, meaning that we still need to
4940 * repeat. (1 means the natural last repetition; 0
4941 * means termination by %exitrep.) We have
4942 * therefore expanded up to the %endrep, and must
4943 * push the whole block on to the expansion buffer
4944 * again. We don't bother to remove the macro-end
4945 * marker: we'd only have to generate another one
4946 * if we did.
4947 */
4948 l->finishes->in_progress--;
4949 list_for_each(l, l->finishes->expansion) {
4950 Token *t, *tt, **tail;
4951
4952 ll = nasm_malloc(sizeof(Line));
4953 ll->next = istk->expansion;
4954 ll->finishes = NULL;
4955 ll->first = NULL;
4956 tail = &ll->first;
4957
4958 list_for_each(t, l->first) {
4959 if (t->text || t->type == TOK_WHITESPACE) {
4960 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4961 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004962 }
4963 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004964
4965 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004966 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004967 } else {
4968 /*
4969 * Check whether a `%rep' was started and not ended
4970 * within this macro expansion. This can happen and
4971 * should be detected. It's a fatal error because
4972 * I'm too confused to work out how to recover
4973 * sensibly from it.
4974 */
4975 if (defining) {
4976 if (defining->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004977 nasm_panic(0, "defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004978 else if (istk->mstk->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004979 nasm_fatal(0, "`%%rep' without `%%endrep' within"
4980 " expansion of macro `%s'",
4981 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004982 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004983
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004984 /*
4985 * FIXME: investigate the relationship at this point between
4986 * istk->mstk and l->finishes
4987 */
4988 {
4989 MMacro *m = istk->mstk;
4990 istk->mstk = m->next_active;
4991 if (m->name) {
4992 /*
4993 * This was a real macro call, not a %rep, and
4994 * therefore the parameter information needs to
4995 * be freed.
4996 */
4997 if (m->prev) {
4998 pop_mmacro(m);
4999 l->finishes->in_progress --;
5000 } else {
5001 nasm_free(m->params);
5002 free_tlist(m->iline);
5003 nasm_free(m->paramlen);
5004 l->finishes->in_progress = 0;
5005 }
5006 } else
5007 free_mmacro(m);
5008 }
5009 istk->expansion = l->next;
5010 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005011 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005012 }
5013 }
5014 while (1) { /* until we get a line we can use */
5015
5016 if (istk->expansion) { /* from a macro expansion */
5017 char *p;
5018 Line *l = istk->expansion;
5019 if (istk->mstk)
5020 istk->mstk->lineno++;
5021 tline = l->first;
5022 istk->expansion = l->next;
5023 nasm_free(l);
5024 p = detoken(tline, false);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005025 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005026 nasm_free(p);
5027 break;
5028 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005029 line = read_line();
5030 if (line) { /* from the current input file */
5031 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005032 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005033 nasm_free(line);
5034 break;
5035 }
5036 /*
5037 * The current file has ended; work down the istk
5038 */
5039 {
5040 Include *i = istk;
5041 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005042 if (i->conds) {
5043 /* nasm_error can't be conditionally suppressed */
H. Peter Anvin41087062016-03-03 14:27:34 -08005044 nasm_fatal(0,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005045 "expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005046 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005047 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005048 if (i->next)
5049 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005050 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005051 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005052 nasm_free(i);
H. Peter Anvin130736c2016-02-17 20:27:41 -08005053 if (!istk) {
5054 line = NULL;
5055 goto done;
5056 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005057 if (istk->expansion && istk->expansion->finishes)
5058 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005059 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005060 }
5061
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005062 /*
5063 * We must expand MMacro parameters and MMacro-local labels
5064 * _before_ we plunge into directive processing, to cope
5065 * with things like `%define something %1' such as STRUC
5066 * uses. Unless we're _defining_ a MMacro, in which case
5067 * those tokens should be left alone to go into the
5068 * definition; and unless we're in a non-emitting
5069 * condition, in which case we don't want to meddle with
5070 * anything.
5071 */
5072 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5073 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005074 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005075 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005076
H. Peter Anvine2c80182005-01-15 22:15:51 +00005077 /*
5078 * Check the line to see if it's a preprocessor directive.
5079 */
5080 if (do_directive(tline) == DIRECTIVE_FOUND) {
5081 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005082 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005083 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005084 * We're defining a multi-line macro. We emit nothing
5085 * at all, and just
5086 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005087 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005088 Line *l = nasm_malloc(sizeof(Line));
5089 l->next = defining->expansion;
5090 l->first = tline;
5091 l->finishes = NULL;
5092 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005093 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005094 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005095 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005096 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005097 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005098 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005099 * directive so we keep our place correctly.
5100 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005101 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005102 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005103 } else if (istk->mstk && !istk->mstk->in_progress) {
5104 /*
5105 * We're in a %rep block which has been terminated, so
5106 * we're walking through to the %endrep without
5107 * emitting anything. Emit nothing at all, not even a
5108 * blank line: when we emerge from the %rep block we'll
5109 * give a line-number directive so we keep our place
5110 * correctly.
5111 */
5112 free_tlist(tline);
5113 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005114 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005115 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005116 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005117 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005118 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005119 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005120 line = detoken(tline, true);
5121 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005122 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005123 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005124 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005125 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005126 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005127 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005128
H. Peter Anvin130736c2016-02-17 20:27:41 -08005129done:
5130 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005131 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005132}
5133
H. Peter Anvine2c80182005-01-15 22:15:51 +00005134static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005135{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005136 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005137
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005138 if (defining) {
5139 if (defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005140 nasm_error(ERR_NONFATAL,
5141 "end of file while still defining macro `%s'",
5142 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005143 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005144 nasm_error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005145 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005146
5147 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005148 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005149 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005150
5151 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005152
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005153 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005154 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005155 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005156 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005157 Include *i = istk;
5158 istk = istk->next;
5159 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005160 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005161 }
5162 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005163 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005164 src_set_fname(NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005165 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005166 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005167 free_llist(predef);
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005168 predef = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005169 delete_Blocks();
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005170 freeTokens = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005171 while ((i = ipath)) {
5172 ipath = i->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005173 if (i->path)
5174 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005175 nasm_free(i);
5176 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005177 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005178}
5179
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005180static void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005181{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005182 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005183
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005184 i = nasm_malloc(sizeof(IncPath));
5185 i->path = path ? nasm_strdup(path) : NULL;
5186 i->next = NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005187
H. Peter Anvin89cee572009-07-15 09:16:54 -04005188 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005189 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005190 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005191 j = j->next;
5192 j->next = i;
5193 } else {
5194 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005195 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005196}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005197
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005198static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005199{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005200 Token *inc, *space, *name;
5201 Line *l;
5202
H. Peter Anvin734b1882002-04-30 21:01:08 +00005203 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5204 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5205 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005206
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005207 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005208 l->next = predef;
5209 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005210 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005211 predef = l;
5212}
5213
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005214static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005215{
5216 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005217 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005218 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005219
H. Peter Anvin130736c2016-02-17 20:27:41 -08005220 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005221
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005222 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005223 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5224 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005225 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005226 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005227 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005228 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005229 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005230
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005231 if (space->next->type != TOK_PREPROC_ID &&
5232 space->next->type != TOK_ID)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005233 nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005234
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005235 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005236 l->next = predef;
5237 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005238 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005239 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005240
5241 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005242}
5243
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005244static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005245{
5246 Token *def, *space;
5247 Line *l;
5248
H. Peter Anvin734b1882002-04-30 21:01:08 +00005249 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5250 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005251 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005252
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005253 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005254 l->next = predef;
5255 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005256 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005257 predef = l;
5258}
5259
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005260static void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005261{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005262 extrastdmac = macros;
5263}
5264
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005265static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005266{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005267 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005268 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005269 tok->text = nasm_strdup(numbuf);
5270 tok->type = TOK_NUMBER;
5271}
5272
H. Peter Anvin37368952016-05-09 14:10:32 -07005273static void pp_list_one_macro(MMacro *m, int severity)
5274{
5275 if (!m)
5276 return;
5277
5278 /* We need to print the next_active list in reverse order */
5279 pp_list_one_macro(m->next_active, severity);
5280
5281 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005282 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvin37368952016-05-09 14:10:32 -07005283 nasm_error(severity, "... from macro `%s' defined here", m->name);
5284 }
5285}
5286
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005287static void pp_error_list_macros(int severity)
5288{
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005289 int32_t saved_line;
5290 const char *saved_fname = NULL;
5291
5292 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY;
H. Peter Anvin274cda82016-05-10 02:56:29 -07005293 src_get(&saved_line, &saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005294
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005295 if (istk)
5296 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005297
H. Peter Anvin274cda82016-05-10 02:56:29 -07005298 src_set(saved_line, saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005299}
5300
H. Peter Anvine7469712016-02-18 02:20:59 -08005301const struct preproc_ops nasmpp = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005302 pp_reset,
5303 pp_getline,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005304 pp_cleanup,
5305 pp_extra_stdmac,
5306 pp_pre_define,
5307 pp_pre_undefine,
5308 pp_pre_include,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005309 pp_include_path,
5310 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005311};