blob: e36ae9c9b3f64936b0f58db8c1d9c77b6138bc46 [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 Anvin3e83cec2016-05-25 04:28:46 -07001510 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;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001518
H. Peter Anvine2c80182005-01-15 22:15:51 +00001519 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001520 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001521 memcpy(sl->str, prefix, prefix_len);
1522 memcpy(sl->str+prefix_len, file, len+1);
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001523 fp = nasm_open_read(sl->str, mode);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001524 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001525 sl->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001526 **dtail = sl;
1527 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001528 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001529 nasm_free(sl);
1530 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001531 if (fp)
1532 return fp;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001533 if (!ip) {
1534 if (!missing_ok)
1535 break;
1536 prefix = NULL;
1537 } else {
1538 prefix = ip->path;
1539 ip = ip->next;
1540 }
1541 if (prefix) {
1542 prefix_len = strlen(prefix);
1543 } else {
1544 /* -MG given and file not found */
1545 if (dhead && !in_list(*dhead, file)) {
1546 sl = nasm_malloc(len+1+sizeof sl->next);
1547 sl->next = NULL;
1548 strcpy(sl->str, file);
1549 **dtail = sl;
1550 *dtail = &sl->next;
1551 }
1552 return NULL;
1553 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001554 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001555
H. Peter Anvin130736c2016-02-17 20:27:41 -08001556 nasm_error(ERR_FATAL, "unable to open include file `%s'", file);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001557 return NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001558}
1559
1560/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001561 * Opens an include or input file. Public version, for use by modules
1562 * that get a file:lineno pair and need to look at the file again
1563 * (e.g. the CodeView debug backend). Returns NULL on failure.
1564 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001565FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001566{
1567 FILE *fp;
1568 StrList *xsl = NULL;
1569 StrList **xst = &xsl;
1570
Fabian Giesen5b838ef2016-04-28 13:48:15 -07001571 fp = inc_fopen(filename, &xsl, &xst, true, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001572 if (xsl)
1573 nasm_free(xsl);
1574 return fp;
1575}
1576
1577/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001578 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001579 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001580 * return true if _any_ single-line macro of that name is defined.
1581 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001582 * `nparam' or no parameters is defined.
1583 *
1584 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001585 * defined, or nparam is -1, the address of the definition structure
1586 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001587 * is NULL, no action will be taken regarding its contents, and no
1588 * error will occur.
1589 *
1590 * Note that this is also called with nparam zero to resolve
1591 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001592 *
1593 * If you already know which context macro belongs to, you can pass
1594 * the context pointer as first parameter; if you won't but name begins
1595 * with %$ the context will be automatically computed. If all_contexts
1596 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001597 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001598static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001599smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001600 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001601{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001602 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001603 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001604
H. Peter Anvin97a23472007-09-16 17:57:25 -07001605 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001606 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001607 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001608 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001609 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001610 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001611 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001612 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001613 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001614 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001615 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001616 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001617
H. Peter Anvine2c80182005-01-15 22:15:51 +00001618 while (m) {
1619 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001620 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001621 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001622 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001623 *defn = m;
1624 else
1625 *defn = NULL;
1626 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001627 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 }
1629 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001630 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001631
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001632 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001633}
1634
1635/*
1636 * Count and mark off the parameters in a multi-line macro call.
1637 * This is called both from within the multi-line macro expansion
1638 * code, and also to mark off the default parameters when provided
1639 * in a %macro definition line.
1640 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001641static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001642{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001643 int paramsize, brace;
1644
1645 *nparam = paramsize = 0;
1646 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001647 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001648 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001649 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001650 paramsize += PARAM_DELTA;
1651 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1652 }
1653 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001654 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001655 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001656 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001657 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001658 if (brace) {
1659 while (brace && (t = t->next) != NULL) {
1660 if (tok_is_(t, "{"))
1661 brace++;
1662 else if (tok_is_(t, "}"))
1663 brace--;
1664 }
1665
1666 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001667 /*
1668 * Now we've found the closing brace, look further
1669 * for the comma.
1670 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001671 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001672 skip_white_(t);
1673 if (tok_isnt_(t, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001674 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001675 "braces do not enclose all of macro parameter");
1676 while (tok_isnt_(t, ","))
1677 t = t->next;
1678 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001679 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001680 } else {
1681 while (tok_isnt_(t, ","))
1682 t = t->next;
1683 }
1684 if (t) { /* got a comma/brace */
1685 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001687 }
1688}
1689
1690/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001691 * Determine whether one of the various `if' conditions is true or
1692 * not.
1693 *
1694 * We must free the tline we get passed.
1695 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001696static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001697{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001698 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001699 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001700 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001701 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001702 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001703 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001704 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001705
1706 origline = tline;
1707
H. Peter Anvine2c80182005-01-15 22:15:51 +00001708 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001709 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001710 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001711 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001712 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001713 if (!tline)
1714 break;
1715 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001716 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001717 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001718 free_tlist(origline);
1719 return -1;
1720 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001721 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001722 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001723 tline = tline->next;
1724 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001725 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001726
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001727 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001728 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001729 while (tline) {
1730 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001731 if (!tline || (tline->type != TOK_ID &&
1732 (tline->type != TOK_PREPROC_ID ||
1733 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001734 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001735 "`%s' expects macro identifiers", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001736 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001737 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001738 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001739 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001740 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001741 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001742 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001743
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001744 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001745 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001746 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001747 while (tline) {
1748 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001749 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001750 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001751 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001752 tline->text[1] != '!'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001753 nasm_error(ERR_NONFATAL,
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001754 "`%s' expects environment variable names",
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001755 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001756 goto fail;
1757 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001758 p = tline->text;
1759 if (tline->type == TOK_PREPROC_ID)
1760 p += 2; /* Skip leading %! */
1761 if (*p == '\'' || *p == '\"' || *p == '`')
1762 nasm_unquote_cstr(p, ct);
1763 if (getenv(p))
1764 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001765 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001766 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001767 break;
1768
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001769 case PPC_IFIDN:
1770 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001771 tline = expand_smacro(tline);
1772 t = tt = tline;
1773 while (tok_isnt_(tt, ","))
1774 tt = tt->next;
1775 if (!tt) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001776 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001777 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001778 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001779 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001780 }
1781 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001782 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001783 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1784 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001785 nasm_error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001786 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001787 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001788 }
1789 if (t->type == TOK_WHITESPACE) {
1790 t = t->next;
1791 continue;
1792 }
1793 if (tt->type == TOK_WHITESPACE) {
1794 tt = tt->next;
1795 continue;
1796 }
1797 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001798 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001799 break;
1800 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001801 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001803 size_t l1 = nasm_unquote(t->text, NULL);
1804 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001805
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001806 if (l1 != l2) {
1807 j = false;
1808 break;
1809 }
1810 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1811 j = false;
1812 break;
1813 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001814 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001815 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001816 break;
1817 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001818
H. Peter Anvine2c80182005-01-15 22:15:51 +00001819 t = t->next;
1820 tt = tt->next;
1821 }
1822 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001823 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001824 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001825
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001826 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001827 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001828 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001829 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001830
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001831 skip_white_(tline);
1832 tline = expand_id(tline);
1833 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001834 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001835 "`%s' expects a macro name", pp_directives[ct]);
1836 goto fail;
1837 }
1838 searching.name = nasm_strdup(tline->text);
1839 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001840 searching.plus = false;
1841 searching.nolist = false;
1842 searching.in_progress = 0;
1843 searching.max_depth = 0;
1844 searching.rep_nest = NULL;
1845 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001846 searching.nparam_max = INT_MAX;
1847 tline = expand_smacro(tline->next);
1848 skip_white_(tline);
1849 if (!tline) {
1850 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001851 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001852 "`%s' expects a parameter count or nothing",
1853 pp_directives[ct]);
1854 } else {
1855 searching.nparam_min = searching.nparam_max =
1856 readnum(tline->text, &j);
1857 if (j)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001858 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001859 "unable to parse parameter count `%s'",
1860 tline->text);
1861 }
1862 if (tline && tok_is_(tline->next, "-")) {
1863 tline = tline->next->next;
1864 if (tok_is_(tline, "*"))
1865 searching.nparam_max = INT_MAX;
1866 else if (!tok_type_(tline, TOK_NUMBER))
H. Peter Anvin130736c2016-02-17 20:27:41 -08001867 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001868 "`%s' expects a parameter count after `-'",
1869 pp_directives[ct]);
1870 else {
1871 searching.nparam_max = 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 if (searching.nparam_min > searching.nparam_max)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001877 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001878 "minimum parameter count exceeds maximum");
1879 }
1880 }
1881 if (tline && tok_is_(tline->next, "+")) {
1882 tline = tline->next;
1883 searching.plus = true;
1884 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001885 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1886 while (mmac) {
1887 if (!strcmp(mmac->name, searching.name) &&
1888 (mmac->nparam_min <= searching.nparam_max
1889 || searching.plus)
1890 && (searching.nparam_min <= mmac->nparam_max
1891 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001892 found = true;
1893 break;
1894 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001895 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001896 }
1897 if (tline && tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001898 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001899 "trailing garbage after %%ifmacro ignored");
1900 nasm_free(searching.name);
1901 j = found;
1902 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001903 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001904
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001905 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001906 needtype = TOK_ID;
1907 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001908 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001909 needtype = TOK_NUMBER;
1910 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001911 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001912 needtype = TOK_STRING;
1913 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001914
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001915iftype:
1916 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001917
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001918 while (tok_type_(t, TOK_WHITESPACE) ||
1919 (needtype == TOK_NUMBER &&
1920 tok_type_(t, TOK_OTHER) &&
1921 (t->text[0] == '-' || t->text[0] == '+') &&
1922 !t->text[1]))
1923 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001924
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001925 j = tok_type_(t, needtype);
1926 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001927
1928 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001929 t = tline = expand_smacro(tline);
1930 while (tok_type_(t, TOK_WHITESPACE))
1931 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001932
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001933 j = false;
1934 if (t) {
1935 t = t->next; /* Skip the actual token */
1936 while (tok_type_(t, TOK_WHITESPACE))
1937 t = t->next;
1938 j = !t; /* Should be nothing left */
1939 }
1940 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001941
H. Peter Anvin134b9462008-02-16 17:01:40 -08001942 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001943 t = tline = expand_smacro(tline);
1944 while (tok_type_(t, TOK_WHITESPACE))
1945 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001946
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001947 j = !t; /* Should be empty */
1948 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001949
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001950 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001951 t = tline = expand_smacro(tline);
1952 tptr = &t;
1953 tokval.t_type = TOKEN_INVALID;
1954 evalresult = evaluate(ppscan, tptr, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -08001955 NULL, pass | CRITICAL, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001956 if (!evalresult)
1957 return -1;
1958 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001959 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001960 "trailing garbage after expression ignored");
1961 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001962 nasm_error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001963 "non-constant value given to `%s'", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001964 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001965 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001966 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001967 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001968
H. Peter Anvine2c80182005-01-15 22:15:51 +00001969 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08001970 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001971 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001972 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001973 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001974 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001975
1976 free_tlist(origline);
1977 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001978
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001979fail:
1980 free_tlist(origline);
1981 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001982}
1983
1984/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001985 * Common code for defining an smacro
1986 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001987static bool define_smacro(Context *ctx, const char *mname, bool casesense,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001988 int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001989{
1990 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001991 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001992
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001993 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001994 if (!smac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001995 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001996 "single-line macro `%s' defined both with and"
1997 " without parameters", mname);
1998 /*
1999 * Some instances of the old code considered this a failure,
2000 * some others didn't. What is the right thing to do here?
2001 */
2002 free_tlist(expansion);
2003 return false; /* Failure */
2004 } else {
2005 /*
2006 * We're redefining, so we have to take over an
2007 * existing SMacro structure. This means freeing
2008 * what was already in it.
2009 */
2010 nasm_free(smac->name);
2011 free_tlist(smac->expansion);
2012 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002013 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002014 smtbl = ctx ? &ctx->localmac : &smacros;
2015 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002016 smac = nasm_malloc(sizeof(SMacro));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002017 smac->next = *smhead;
2018 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002019 }
2020 smac->name = nasm_strdup(mname);
2021 smac->casesense = casesense;
2022 smac->nparam = nparam;
2023 smac->expansion = expansion;
2024 smac->in_progress = false;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002025 return true; /* Success */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002026}
2027
2028/*
2029 * Undefine an smacro
2030 */
2031static void undef_smacro(Context *ctx, const char *mname)
2032{
2033 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002034 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002035
H. Peter Anvin166c2472008-05-28 12:28:58 -07002036 smtbl = ctx ? &ctx->localmac : &smacros;
2037 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002038
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002039 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002040 /*
2041 * We now have a macro name... go hunt for it.
2042 */
2043 sp = smhead;
2044 while ((s = *sp) != NULL) {
2045 if (!mstrcmp(s->name, mname, s->casesense)) {
2046 *sp = s->next;
2047 nasm_free(s->name);
2048 free_tlist(s->expansion);
2049 nasm_free(s);
2050 } else {
2051 sp = &s->next;
2052 }
2053 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002054 }
2055}
2056
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002057/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002058 * Parse a mmacro specification.
2059 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002060static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002061{
2062 bool err;
2063
2064 tline = tline->next;
2065 skip_white_(tline);
2066 tline = expand_id(tline);
2067 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002068 nasm_error(ERR_NONFATAL, "`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002069 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002070 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002071
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002072 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002073 def->name = nasm_strdup(tline->text);
2074 def->plus = false;
2075 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002076 def->in_progress = 0;
2077 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002078 def->nparam_min = 0;
2079 def->nparam_max = 0;
2080
H. Peter Anvina26433d2008-07-16 14:40:01 -07002081 tline = expand_smacro(tline->next);
2082 skip_white_(tline);
2083 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002084 nasm_error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002085 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002086 def->nparam_min = def->nparam_max =
2087 readnum(tline->text, &err);
2088 if (err)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002089 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002090 "unable to parse parameter count `%s'", tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002091 }
2092 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002093 tline = tline->next->next;
2094 if (tok_is_(tline, "*")) {
2095 def->nparam_max = INT_MAX;
2096 } else if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002097 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002098 "`%s' expects a parameter count after `-'", directive);
2099 } else {
2100 def->nparam_max = readnum(tline->text, &err);
2101 if (err) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002102 nasm_error(ERR_NONFATAL, "unable to parse parameter count `%s'",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002103 tline->text);
2104 }
2105 if (def->nparam_min > def->nparam_max) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002106 nasm_error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002107 }
2108 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002109 }
2110 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002111 tline = tline->next;
2112 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002113 }
2114 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002115 !nasm_stricmp(tline->next->text, ".nolist")) {
2116 tline = tline->next;
2117 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002118 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002119
H. Peter Anvina26433d2008-07-16 14:40:01 -07002120 /*
2121 * Handle default parameters.
2122 */
2123 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002124 def->dlist = tline->next;
2125 tline->next = NULL;
2126 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002127 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002128 def->dlist = NULL;
2129 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002130 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002131 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002132
H. Peter Anvin89cee572009-07-15 09:16:54 -04002133 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002134 !def->plus)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002135 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002136 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002137
H. Peter Anvina26433d2008-07-16 14:40:01 -07002138 return true;
2139}
2140
2141
2142/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002143 * Decode a size directive
2144 */
2145static int parse_size(const char *str) {
2146 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002147 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002148 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002149 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002150
Cyrill Gorcunova7319242010-06-03 22:04:36 +04002151 return sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1];
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002152}
2153
Ed Beroset3ab3f412002-06-11 03:31:49 +00002154/**
2155 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002156 * Find out if a line contains a preprocessor directive, and deal
2157 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002158 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002159 * If a directive _is_ found, it is the responsibility of this routine
2160 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002161 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002162 * @param tline a pointer to the current tokeninzed line linked list
2163 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002164 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002165 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002166static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002167{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002168 enum preproc_token i;
2169 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002170 bool err;
2171 int nparam;
2172 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002173 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002174 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002175 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002176 char *p, *pp;
2177 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002178 Include *inc;
2179 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002180 Cond *cond;
2181 MMacro *mmac, **mmhead;
Jin Kyu Songc9486b92013-10-28 17:07:57 -07002182 Token *t = NULL, *tt, *param_start, *macro_start, *last, **tptr, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002183 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002184 struct tokenval tokval;
2185 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002186 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002187 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002188 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002189 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002190
2191 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002192
H. Peter Anvineba20a72002-04-30 20:53:55 +00002193 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002194 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002195 (tline->text[1] == '%' || tline->text[1] == '$'
2196 || tline->text[1] == '!'))
2197 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002198
H. Peter Anvin4169a472007-09-12 01:29:43 +00002199 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002200
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002201 /*
2202 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2203 * since they are known to be buggy at moment, we need to fix them
2204 * in future release (2.09-2.10)
2205 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002206 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002207 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002208 tline->text);
2209 return NO_DIRECTIVE_FOUND;
2210 }
2211
2212 /*
2213 * If we're in a non-emitting branch of a condition construct,
2214 * or walking to the end of an already terminated %rep block,
2215 * we should ignore all directives except for condition
2216 * directives.
2217 */
2218 if (((istk->conds && !emitting(istk->conds->state)) ||
2219 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2220 return NO_DIRECTIVE_FOUND;
2221 }
2222
2223 /*
2224 * If we're defining a macro or reading a %rep block, we should
2225 * ignore all directives except for %macro/%imacro (which nest),
2226 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2227 * If we're in a %rep block, another %rep nests, so should be let through.
2228 */
2229 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2230 i != PP_RMACRO && i != PP_IRMACRO &&
2231 i != PP_ENDMACRO && i != PP_ENDM &&
2232 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2233 return NO_DIRECTIVE_FOUND;
2234 }
2235
2236 if (defining) {
2237 if (i == PP_MACRO || i == PP_IMACRO ||
2238 i == PP_RMACRO || i == PP_IRMACRO) {
2239 nested_mac_count++;
2240 return NO_DIRECTIVE_FOUND;
2241 } else if (nested_mac_count > 0) {
2242 if (i == PP_ENDMACRO) {
2243 nested_mac_count--;
2244 return NO_DIRECTIVE_FOUND;
2245 }
2246 }
2247 if (!defining->name) {
2248 if (i == PP_REP) {
2249 nested_rep_count++;
2250 return NO_DIRECTIVE_FOUND;
2251 } else if (nested_rep_count > 0) {
2252 if (i == PP_ENDREP) {
2253 nested_rep_count--;
2254 return NO_DIRECTIVE_FOUND;
2255 }
2256 }
2257 }
2258 }
2259
H. Peter Anvin4169a472007-09-12 01:29:43 +00002260 switch (i) {
2261 case PP_INVALID:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002262 nasm_error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002263 tline->text);
2264 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002265
H. Peter Anvine2c80182005-01-15 22:15:51 +00002266 case PP_STACKSIZE:
2267 /* Directive to tell NASM what the default stack size is. The
2268 * default is for a 16-bit stack, and this can be overriden with
2269 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002270 */
2271 tline = tline->next;
2272 if (tline && tline->type == TOK_WHITESPACE)
2273 tline = tline->next;
2274 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002275 nasm_error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002276 free_tlist(origline);
2277 return DIRECTIVE_FOUND;
2278 }
2279 if (nasm_stricmp(tline->text, "flat") == 0) {
2280 /* All subsequent ARG directives are for a 32-bit stack */
2281 StackSize = 4;
2282 StackPointer = "ebp";
2283 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002284 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002285 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2286 /* All subsequent ARG directives are for a 64-bit stack */
2287 StackSize = 8;
2288 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002289 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002290 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002291 } else if (nasm_stricmp(tline->text, "large") == 0) {
2292 /* All subsequent ARG directives are for a 16-bit stack,
2293 * far function call.
2294 */
2295 StackSize = 2;
2296 StackPointer = "bp";
2297 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002298 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002299 } else if (nasm_stricmp(tline->text, "small") == 0) {
2300 /* All subsequent ARG directives are for a 16-bit stack,
2301 * far function call. We don't support near functions.
2302 */
2303 StackSize = 2;
2304 StackPointer = "bp";
2305 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002306 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002307 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002308 nasm_error(ERR_NONFATAL, "`%%stacksize' invalid size type");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002309 free_tlist(origline);
2310 return DIRECTIVE_FOUND;
2311 }
2312 free_tlist(origline);
2313 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002314
H. Peter Anvine2c80182005-01-15 22:15:51 +00002315 case PP_ARG:
2316 /* TASM like ARG directive to define arguments to functions, in
2317 * the following form:
2318 *
2319 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2320 */
2321 offset = ArgOffset;
2322 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002323 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002324 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002325
H. Peter Anvine2c80182005-01-15 22:15:51 +00002326 /* Find the argument name */
2327 tline = tline->next;
2328 if (tline && tline->type == TOK_WHITESPACE)
2329 tline = tline->next;
2330 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002331 nasm_error(ERR_NONFATAL, "`%%arg' missing argument parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002332 free_tlist(origline);
2333 return DIRECTIVE_FOUND;
2334 }
2335 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002336
H. Peter Anvine2c80182005-01-15 22:15:51 +00002337 /* Find the argument size type */
2338 tline = tline->next;
2339 if (!tline || tline->type != TOK_OTHER
2340 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002341 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002342 "Syntax error processing `%%arg' directive");
2343 free_tlist(origline);
2344 return DIRECTIVE_FOUND;
2345 }
2346 tline = tline->next;
2347 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002348 nasm_error(ERR_NONFATAL, "`%%arg' missing size type parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002349 free_tlist(origline);
2350 return DIRECTIVE_FOUND;
2351 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002352
H. Peter Anvine2c80182005-01-15 22:15:51 +00002353 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002354 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002355 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002356 size = parse_size(tt->text);
2357 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002358 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002359 "Invalid size type for `%%arg' missing directive");
2360 free_tlist(tt);
2361 free_tlist(origline);
2362 return DIRECTIVE_FOUND;
2363 }
2364 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002365
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002366 /* Round up to even stack slots */
2367 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002368
H. Peter Anvine2c80182005-01-15 22:15:51 +00002369 /* Now define the macro for the argument */
2370 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2371 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002372 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002373 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002374
H. Peter Anvine2c80182005-01-15 22:15:51 +00002375 /* Move to the next argument in the list */
2376 tline = tline->next;
2377 if (tline && tline->type == TOK_WHITESPACE)
2378 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002379 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002380 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002381 free_tlist(origline);
2382 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002383
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 case PP_LOCAL:
2385 /* TASM like LOCAL directive to define local variables for a
2386 * function, in the following form:
2387 *
2388 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2389 *
2390 * The '= LocalSize' at the end is ignored by NASM, but is
2391 * required by TASM to define the local parameter size (and used
2392 * by the TASM macro package).
2393 */
2394 offset = LocalOffset;
2395 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002396 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002397 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002398
H. Peter Anvine2c80182005-01-15 22:15:51 +00002399 /* Find the argument name */
2400 tline = tline->next;
2401 if (tline && tline->type == TOK_WHITESPACE)
2402 tline = tline->next;
2403 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002404 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002405 "`%%local' missing argument parameter");
2406 free_tlist(origline);
2407 return DIRECTIVE_FOUND;
2408 }
2409 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002410
H. Peter Anvine2c80182005-01-15 22:15:51 +00002411 /* Find the argument size type */
2412 tline = tline->next;
2413 if (!tline || tline->type != TOK_OTHER
2414 || tline->text[0] != ':') {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002415 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002416 "Syntax error processing `%%local' directive");
2417 free_tlist(origline);
2418 return DIRECTIVE_FOUND;
2419 }
2420 tline = tline->next;
2421 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002422 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002423 "`%%local' missing size type parameter");
2424 free_tlist(origline);
2425 return DIRECTIVE_FOUND;
2426 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002427
H. Peter Anvine2c80182005-01-15 22:15:51 +00002428 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002429 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002430 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002431 size = parse_size(tt->text);
2432 if (!size) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002433 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002434 "Invalid size type for `%%local' missing directive");
2435 free_tlist(tt);
2436 free_tlist(origline);
2437 return DIRECTIVE_FOUND;
2438 }
2439 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002440
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002441 /* Round up to even stack slots */
2442 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002443
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002444 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002445
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002446 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002447 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2448 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002449 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002450
H. Peter Anvine2c80182005-01-15 22:15:51 +00002451 /* Now define the assign to setup the enter_c macro correctly */
2452 snprintf(directive, sizeof(directive),
2453 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002454 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002455
H. Peter Anvine2c80182005-01-15 22:15:51 +00002456 /* Move to the next argument in the list */
2457 tline = tline->next;
2458 if (tline && tline->type == TOK_WHITESPACE)
2459 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002460 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002461 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002462 free_tlist(origline);
2463 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002464
H. Peter Anvine2c80182005-01-15 22:15:51 +00002465 case PP_CLEAR:
2466 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002467 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002468 "trailing garbage after `%%clear' ignored");
2469 free_macros();
2470 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002471 free_tlist(origline);
2472 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002473
H. Peter Anvin418ca702008-05-30 10:42:30 -07002474 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002475 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002476 skip_white_(t);
2477 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002478 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002479 nasm_error(ERR_NONFATAL, "`%%depend' expects a file name");
H. Peter Anvin418ca702008-05-30 10:42:30 -07002480 free_tlist(origline);
2481 return DIRECTIVE_FOUND; /* but we did _something_ */
2482 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002483 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002484 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002485 "trailing garbage after `%%depend' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002486 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002487 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002488 nasm_unquote_cstr(p, i);
2489 if (dephead && !in_list(*dephead, p)) {
2490 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2491 sl->next = NULL;
2492 strcpy(sl->str, p);
2493 *deptail = sl;
2494 deptail = &sl->next;
2495 }
2496 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002497 return DIRECTIVE_FOUND;
2498
2499 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002500 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002501 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002502
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002503 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002504 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002505 nasm_error(ERR_NONFATAL, "`%%include' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002506 free_tlist(origline);
2507 return DIRECTIVE_FOUND; /* but we did _something_ */
2508 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002509 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002510 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002511 "trailing garbage after `%%include' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002512 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002513 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002514 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002515 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002516 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002517 inc->conds = NULL;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07002518 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002519 if (!inc->fp) {
2520 /* -MG given but file not found */
2521 nasm_free(inc);
2522 } else {
H. Peter Anvin274cda82016-05-10 02:56:29 -07002523 inc->fname = src_set_fname(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002524 inc->lineno = src_set_linnum(0);
2525 inc->lineinc = 1;
2526 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002527 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002528 istk = inc;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002529 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002530 }
2531 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002532 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002533
H. Peter Anvind2456592008-06-19 15:04:18 -07002534 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002535 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002536 static macros_t *use_pkg;
2537 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002538
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002539 tline = tline->next;
2540 skip_white_(tline);
2541 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002542
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002543 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002544 tline->type != TOK_INTERNAL_STRING &&
2545 tline->type != TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002546 nasm_error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002547 free_tlist(origline);
2548 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002549 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002550 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002551 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002552 "trailing garbage after `%%use' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002553 if (tline->type == TOK_STRING)
2554 nasm_unquote_cstr(tline->text, i);
2555 use_pkg = nasm_stdmac_find_package(tline->text);
2556 if (!use_pkg)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002557 nasm_error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002558 else
2559 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002560 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002561 /* Not already included, go ahead and include it */
2562 stdmacpos = use_pkg;
2563 }
2564 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002565 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002566 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002567 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002568 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002569 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002570 tline = tline->next;
2571 skip_white_(tline);
2572 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002573 if (tline) {
2574 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002575 nasm_error(ERR_NONFATAL, "`%s' expects a context identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002576 pp_directives[i]);
2577 free_tlist(origline);
2578 return DIRECTIVE_FOUND; /* but we did _something_ */
2579 }
2580 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002581 nasm_error(ERR_WARNING|ERR_PASS1,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002582 "trailing garbage after `%s' ignored",
2583 pp_directives[i]);
2584 p = nasm_strdup(tline->text);
2585 } else {
2586 p = NULL; /* Anonymous */
2587 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002588
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002589 if (i == PP_PUSH) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002590 ctx = nasm_malloc(sizeof(Context));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002591 ctx->next = cstk;
2592 hash_init(&ctx->localmac, HASH_SMALL);
2593 ctx->name = p;
2594 ctx->number = unique++;
2595 cstk = ctx;
2596 } else {
2597 /* %pop or %repl */
2598 if (!cstk) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002599 nasm_error(ERR_NONFATAL, "`%s': context stack is empty",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002600 pp_directives[i]);
2601 } else if (i == PP_POP) {
2602 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08002603 nasm_error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002604 "expected %s",
2605 cstk->name ? cstk->name : "anonymous", p);
2606 else
2607 ctx_pop();
2608 } else {
2609 /* i == PP_REPL */
2610 nasm_free(cstk->name);
2611 cstk->name = p;
2612 p = NULL;
2613 }
2614 nasm_free(p);
2615 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002616 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002617 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002618 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002619 severity = ERR_FATAL;
2620 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002621 case PP_ERROR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002622 severity = ERR_NONFATAL;
2623 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002624 case PP_WARNING:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002625 severity = ERR_WARNING|ERR_WARN_USER;
2626 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002627
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002628issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002629 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002630 /* Only error out if this is the final pass */
2631 if (pass != 2 && i != PP_FATAL)
2632 return DIRECTIVE_FOUND;
2633
2634 tline->next = expand_smacro(tline->next);
2635 tline = tline->next;
2636 skip_white_(tline);
2637 t = tline ? tline->next : NULL;
2638 skip_white_(t);
2639 if (tok_type_(tline, TOK_STRING) && !t) {
2640 /* The line contains only a quoted string */
2641 p = tline->text;
2642 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002643 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002644 } else {
2645 /* Not a quoted string, or more than a quoted string */
2646 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002647 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002648 nasm_free(p);
2649 }
2650 free_tlist(origline);
2651 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002652 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002653
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002654 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002655 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002656 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002657 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002658 j = if_condition(tline->next, i);
2659 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002660 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002661 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002662 cond = nasm_malloc(sizeof(Cond));
2663 cond->next = istk->conds;
2664 cond->state = j;
2665 istk->conds = cond;
2666 if(istk->mstk)
2667 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002668 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002669 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002670
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002671 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002672 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002673 nasm_error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002674 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002675 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002676 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002677 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002678
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002679 case COND_DONE:
2680 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002681 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002682
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002683 case COND_ELSE_TRUE:
2684 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002685 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2686 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002687 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002688 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002689
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002690 case COND_IF_FALSE:
2691 /*
2692 * IMPORTANT: In the case of %if, we will already have
2693 * called expand_mmac_params(); however, if we're
2694 * processing an %elif we must have been in a
2695 * non-emitting mode, which would have inhibited
2696 * the normal invocation of expand_mmac_params().
2697 * Therefore, we have to do it explicitly here.
2698 */
2699 j = if_condition(expand_mmac_params(tline->next), i);
2700 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002701 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002702 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2703 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002704 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002705 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002706 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002707
H. Peter Anvine2c80182005-01-15 22:15:51 +00002708 case PP_ELSE:
2709 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002710 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2711 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002712 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002713 nasm_fatal(0, "`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002714 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002715 case COND_IF_TRUE:
2716 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002717 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002718 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002719
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002720 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002721 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002722
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002723 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002724 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002725 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002726
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002727 case COND_ELSE_TRUE:
2728 case COND_ELSE_FALSE:
H. Peter Anvin130736c2016-02-17 20:27:41 -08002729 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002730 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002731 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002732 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002733 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002734 free_tlist(origline);
2735 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002736
H. Peter Anvine2c80182005-01-15 22:15:51 +00002737 case PP_ENDIF:
2738 if (tline->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002739 nasm_error(ERR_WARNING|ERR_PASS1|ERR_PP_PRECOND,
2740 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002741 if (!istk->conds)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002742 nasm_error(ERR_FATAL, "`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002743 cond = istk->conds;
2744 istk->conds = cond->next;
2745 nasm_free(cond);
2746 if(istk->mstk)
2747 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002748 free_tlist(origline);
2749 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002750
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002751 case PP_RMACRO:
2752 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002753 case PP_MACRO:
2754 case PP_IMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002755 if (defining) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002756 nasm_error(ERR_FATAL, "`%s': already defining a macro",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002757 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002758 return DIRECTIVE_FOUND;
2759 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002760 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002761 defining->max_depth =
2762 (i == PP_RMACRO) || (i == PP_IRMACRO) ? DEADMAN_LIMIT : 0;
2763 defining->casesense = (i == PP_MACRO) || (i == PP_RMACRO);
2764 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2765 nasm_free(defining);
2766 defining = NULL;
2767 return DIRECTIVE_FOUND;
2768 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002769
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002770 src_get(&defining->xline, &defining->fname);
2771
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002772 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2773 while (mmac) {
2774 if (!strcmp(mmac->name, defining->name) &&
2775 (mmac->nparam_min <= defining->nparam_max
2776 || defining->plus)
2777 && (defining->nparam_min <= mmac->nparam_max
2778 || mmac->plus)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002779 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002780 "redefining multi-line macro `%s'", defining->name);
2781 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002782 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002783 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002784 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002785 free_tlist(origline);
2786 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002787
H. Peter Anvine2c80182005-01-15 22:15:51 +00002788 case PP_ENDM:
2789 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002790 if (! (defining && defining->name)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002791 nasm_error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002792 return DIRECTIVE_FOUND;
2793 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002794 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2795 defining->next = *mmhead;
2796 *mmhead = defining;
2797 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002798 free_tlist(origline);
2799 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002800
H. Peter Anvin89cee572009-07-15 09:16:54 -04002801 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002802 /*
2803 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002804 * macro-end marker for a macro with a name. Then we
2805 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002806 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002807 list_for_each(l, istk->expansion)
2808 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002809 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002810
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002811 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002812 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002813 * Remove all conditional entries relative to this
2814 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002815 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002816 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2817 cond = istk->conds;
2818 istk->conds = cond->next;
2819 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002820 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002821 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002822 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002823 nasm_error(ERR_NONFATAL, "`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002824 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002825 free_tlist(origline);
2826 return DIRECTIVE_FOUND;
2827
H. Peter Anvina26433d2008-07-16 14:40:01 -07002828 case PP_UNMACRO:
2829 case PP_UNIMACRO:
2830 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002831 MMacro **mmac_p;
2832 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002833
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002834 spec.casesense = (i == PP_UNMACRO);
2835 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2836 return DIRECTIVE_FOUND;
2837 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002838 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2839 while (mmac_p && *mmac_p) {
2840 mmac = *mmac_p;
2841 if (mmac->casesense == spec.casesense &&
2842 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2843 mmac->nparam_min == spec.nparam_min &&
2844 mmac->nparam_max == spec.nparam_max &&
2845 mmac->plus == spec.plus) {
2846 *mmac_p = mmac->next;
2847 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002848 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002849 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002850 }
2851 }
2852 free_tlist(origline);
2853 free_tlist(spec.dlist);
2854 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002855 }
2856
H. Peter Anvine2c80182005-01-15 22:15:51 +00002857 case PP_ROTATE:
2858 if (tline->next && tline->next->type == TOK_WHITESPACE)
2859 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002860 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002861 free_tlist(origline);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002862 nasm_error(ERR_NONFATAL, "`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002863 return DIRECTIVE_FOUND;
2864 }
2865 t = expand_smacro(tline->next);
2866 tline->next = NULL;
2867 free_tlist(origline);
2868 tline = t;
2869 tptr = &t;
2870 tokval.t_type = TOKEN_INVALID;
2871 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002872 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002873 free_tlist(tline);
2874 if (!evalresult)
2875 return DIRECTIVE_FOUND;
2876 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002877 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002878 "trailing garbage after expression ignored");
2879 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002880 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002881 return DIRECTIVE_FOUND;
2882 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002883 mmac = istk->mstk;
2884 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2885 mmac = mmac->next_active;
2886 if (!mmac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002887 nasm_error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002888 } else if (mmac->nparam == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002889 nasm_error(ERR_NONFATAL,
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002890 "`%%rotate' invoked within macro without parameters");
2891 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002892 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002893
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002894 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002895 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002896 rotate += mmac->nparam;
2897
2898 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002899 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002900 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002901
H. Peter Anvine2c80182005-01-15 22:15:51 +00002902 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002903 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002904 do {
2905 tline = tline->next;
2906 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002907
H. Peter Anvine2c80182005-01-15 22:15:51 +00002908 if (tok_type_(tline, TOK_ID) &&
2909 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002910 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002911 do {
2912 tline = tline->next;
2913 } while (tok_type_(tline, TOK_WHITESPACE));
2914 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002915
H. Peter Anvine2c80182005-01-15 22:15:51 +00002916 if (tline) {
2917 t = expand_smacro(tline);
2918 tptr = &t;
2919 tokval.t_type = TOKEN_INVALID;
2920 evalresult =
H. Peter Anvin130736c2016-02-17 20:27:41 -08002921 evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002922 if (!evalresult) {
2923 free_tlist(origline);
2924 return DIRECTIVE_FOUND;
2925 }
2926 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08002927 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002928 "trailing garbage after expression ignored");
2929 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002930 nasm_error(ERR_NONFATAL, "non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002931 return DIRECTIVE_FOUND;
2932 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002933 count = reloc_value(evalresult);
2934 if (count >= REP_LIMIT) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002935 nasm_error(ERR_NONFATAL, "`%%rep' value exceeds limit");
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04002936 count = 0;
2937 } else
2938 count++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002939 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002940 nasm_error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002941 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002942 }
2943 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002944
2945 tmp_defining = defining;
2946 defining = nasm_malloc(sizeof(MMacro));
2947 defining->prev = NULL;
2948 defining->name = NULL; /* flags this macro as a %rep block */
2949 defining->casesense = false;
2950 defining->plus = false;
2951 defining->nolist = nolist;
2952 defining->in_progress = count;
2953 defining->max_depth = 0;
2954 defining->nparam_min = defining->nparam_max = 0;
2955 defining->defaults = NULL;
2956 defining->dlist = NULL;
2957 defining->expansion = NULL;
2958 defining->next_active = istk->mstk;
2959 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002960 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002961
H. Peter Anvine2c80182005-01-15 22:15:51 +00002962 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002963 if (!defining || defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08002964 nasm_error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002965 return DIRECTIVE_FOUND;
2966 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002967
H. Peter Anvine2c80182005-01-15 22:15:51 +00002968 /*
2969 * Now we have a "macro" defined - although it has no name
2970 * and we won't be entering it in the hash tables - we must
2971 * push a macro-end marker for it on to istk->expansion.
2972 * After that, it will take care of propagating itself (a
2973 * macro-end marker line for a macro which is really a %rep
2974 * block will cause the macro to be re-expanded, complete
2975 * with another macro-end marker to ensure the process
2976 * continues) until the whole expansion is forcibly removed
2977 * from istk->expansion by a %exitrep.
2978 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002979 l = nasm_malloc(sizeof(Line));
2980 l->next = istk->expansion;
2981 l->finishes = defining;
2982 l->first = NULL;
2983 istk->expansion = l;
2984
2985 istk->mstk = defining;
2986
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002987 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002988 tmp_defining = defining;
2989 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002990 free_tlist(origline);
2991 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002992
H. Peter Anvine2c80182005-01-15 22:15:51 +00002993 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002994 /*
2995 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002996 * macro-end marker for a macro with no name. Then we set
2997 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002998 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002999 list_for_each(l, istk->expansion)
3000 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003001 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003002
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003003 if (l)
3004 l->finishes->in_progress = 1;
3005 else
H. Peter Anvin130736c2016-02-17 20:27:41 -08003006 nasm_error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003007 free_tlist(origline);
3008 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003009
H. Peter Anvine2c80182005-01-15 22:15:51 +00003010 case PP_XDEFINE:
3011 case PP_IXDEFINE:
3012 case PP_DEFINE:
3013 case PP_IDEFINE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003014 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003015
H. Peter Anvine2c80182005-01-15 22:15:51 +00003016 tline = tline->next;
3017 skip_white_(tline);
3018 tline = expand_id(tline);
3019 if (!tline || (tline->type != TOK_ID &&
3020 (tline->type != TOK_PREPROC_ID ||
3021 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003022 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003023 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003024 free_tlist(origline);
3025 return DIRECTIVE_FOUND;
3026 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003027
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003028 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003029 last = tline;
3030 param_start = tline = tline->next;
3031 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003032
H. Peter Anvine2c80182005-01-15 22:15:51 +00003033 /* Expand the macro definition now for %xdefine and %ixdefine */
3034 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3035 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003036
H. Peter Anvine2c80182005-01-15 22:15:51 +00003037 if (tok_is_(tline, "(")) {
3038 /*
3039 * This macro has parameters.
3040 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003041
H. Peter Anvine2c80182005-01-15 22:15:51 +00003042 tline = tline->next;
3043 while (1) {
3044 skip_white_(tline);
3045 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003046 nasm_error(ERR_NONFATAL, "parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003047 free_tlist(origline);
3048 return DIRECTIVE_FOUND;
3049 }
3050 if (tline->type != TOK_ID) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003051 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003052 "`%s': parameter identifier expected",
3053 tline->text);
3054 free_tlist(origline);
3055 return DIRECTIVE_FOUND;
3056 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003057 tline->type = TOK_SMAC_PARAM + nparam++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003058 tline = tline->next;
3059 skip_white_(tline);
3060 if (tok_is_(tline, ",")) {
3061 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003062 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003063 if (!tok_is_(tline, ")")) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003064 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003065 "`)' expected to terminate macro template");
3066 free_tlist(origline);
3067 return DIRECTIVE_FOUND;
3068 }
3069 break;
3070 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003071 }
3072 last = tline;
3073 tline = tline->next;
3074 }
3075 if (tok_type_(tline, TOK_WHITESPACE))
3076 last = tline, tline = tline->next;
3077 macro_start = NULL;
3078 last->next = NULL;
3079 t = tline;
3080 while (t) {
3081 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003082 list_for_each(tt, param_start)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003083 if (tt->type >= TOK_SMAC_PARAM &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003084 !strcmp(tt->text, t->text))
3085 t->type = tt->type;
3086 }
3087 tt = t->next;
3088 t->next = macro_start;
3089 macro_start = t;
3090 t = tt;
3091 }
3092 /*
3093 * Good. We now have a macro name, a parameter count, and a
3094 * token list (in reverse order) for an expansion. We ought
3095 * to be OK just to create an SMacro, store it, and let
3096 * free_tlist have the rest of the line (which we have
3097 * carefully re-terminated after chopping off the expansion
3098 * from the end).
3099 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003100 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003101 free_tlist(origline);
3102 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003103
H. Peter Anvine2c80182005-01-15 22:15:51 +00003104 case PP_UNDEF:
3105 tline = tline->next;
3106 skip_white_(tline);
3107 tline = expand_id(tline);
3108 if (!tline || (tline->type != TOK_ID &&
3109 (tline->type != TOK_PREPROC_ID ||
3110 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003111 nasm_error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003112 free_tlist(origline);
3113 return DIRECTIVE_FOUND;
3114 }
3115 if (tline->next) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003116 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003117 "trailing garbage after macro name ignored");
3118 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003119
H. Peter Anvine2c80182005-01-15 22:15:51 +00003120 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003121 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003122 undef_smacro(ctx, mname);
3123 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003124 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003125
H. Peter Anvin9e200162008-06-04 17:23:14 -07003126 case PP_DEFSTR:
3127 case PP_IDEFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003128 casesense = (i == PP_DEFSTR);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003129
3130 tline = tline->next;
3131 skip_white_(tline);
3132 tline = expand_id(tline);
3133 if (!tline || (tline->type != TOK_ID &&
3134 (tline->type != TOK_PREPROC_ID ||
3135 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003136 nasm_error(ERR_NONFATAL, "`%s' expects a macro identifier",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003137 pp_directives[i]);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003138 free_tlist(origline);
3139 return DIRECTIVE_FOUND;
3140 }
3141
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003142 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003143 last = tline;
3144 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003145 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003146
3147 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003148 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003149
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003150 p = detoken(tline, false);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003151 macro_start = nasm_malloc(sizeof(*macro_start));
3152 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003153 macro_start->text = nasm_quote(p, strlen(p));
3154 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003155 macro_start->a.mac = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003156 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003157
3158 /*
3159 * We now have a macro name, an implicit parameter count of
3160 * zero, and a string token to use as an expansion. Create
3161 * and store an SMacro.
3162 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003163 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003164 free_tlist(origline);
3165 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003166
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003167 case PP_DEFTOK:
3168 case PP_IDEFTOK:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003169 casesense = (i == PP_DEFTOK);
3170
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003171 tline = tline->next;
3172 skip_white_(tline);
3173 tline = expand_id(tline);
3174 if (!tline || (tline->type != TOK_ID &&
3175 (tline->type != TOK_PREPROC_ID ||
3176 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003177 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003178 "`%s' expects a macro identifier as first parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003179 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003180 free_tlist(origline);
3181 return DIRECTIVE_FOUND;
3182 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003183 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003184 last = tline;
3185 tline = expand_smacro(tline->next);
3186 last->next = NULL;
3187
3188 t = tline;
3189 while (tok_type_(t, TOK_WHITESPACE))
3190 t = t->next;
3191 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003192 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003193 nasm_error(ERR_NONFATAL,
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003194 "`%s` requires string as second parameter",
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003195 pp_directives[i]);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003196 free_tlist(tline);
3197 free_tlist(origline);
3198 return DIRECTIVE_FOUND;
3199 }
3200
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003201 /*
3202 * Convert the string to a token stream. Note that smacros
3203 * are stored with the token stream reversed, so we have to
3204 * reverse the output of tokenize().
3205 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003206 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003207 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003208
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003209 /*
3210 * We now have a macro name, an implicit parameter count of
3211 * zero, and a numeric token to use as an expansion. Create
3212 * and store an SMacro.
3213 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003214 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003215 free_tlist(tline);
3216 free_tlist(origline);
3217 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003218
H. Peter Anvin418ca702008-05-30 10:42:30 -07003219 case PP_PATHSEARCH:
3220 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003221 FILE *fp;
3222 StrList *xsl = NULL;
3223 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003224
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003225 casesense = true;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003226
3227 tline = tline->next;
3228 skip_white_(tline);
3229 tline = expand_id(tline);
3230 if (!tline || (tline->type != TOK_ID &&
3231 (tline->type != TOK_PREPROC_ID ||
3232 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003233 nasm_error(ERR_NONFATAL,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003234 "`%%pathsearch' expects a macro identifier as first parameter");
3235 free_tlist(origline);
3236 return DIRECTIVE_FOUND;
3237 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003238 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003239 last = tline;
3240 tline = expand_smacro(tline->next);
3241 last->next = NULL;
3242
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003243 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003244 while (tok_type_(t, TOK_WHITESPACE))
3245 t = t->next;
3246
3247 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003248 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003249 nasm_error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003250 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003251 free_tlist(origline);
3252 return DIRECTIVE_FOUND; /* but we did _something_ */
3253 }
3254 if (t->next)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003255 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07003256 "trailing garbage after `%%pathsearch' ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003257 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003258 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003259 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003260
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07003261 fp = inc_fopen(p, &xsl, &xst, true, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003262 if (fp) {
3263 p = xsl->str;
3264 fclose(fp); /* Don't actually care about the file */
3265 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003266 macro_start = nasm_malloc(sizeof(*macro_start));
3267 macro_start->next = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003268 macro_start->text = nasm_quote(p, strlen(p));
3269 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003270 macro_start->a.mac = NULL;
3271 if (xsl)
3272 nasm_free(xsl);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003273
3274 /*
3275 * We now have a macro name, an implicit parameter count of
3276 * zero, and a string token to use as an expansion. Create
3277 * and store an SMacro.
3278 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003279 define_smacro(ctx, mname, casesense, 0, macro_start);
3280 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003281 free_tlist(origline);
3282 return DIRECTIVE_FOUND;
3283 }
3284
H. Peter Anvine2c80182005-01-15 22:15:51 +00003285 case PP_STRLEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003286 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07003287
H. Peter Anvine2c80182005-01-15 22:15:51 +00003288 tline = tline->next;
3289 skip_white_(tline);
3290 tline = expand_id(tline);
3291 if (!tline || (tline->type != TOK_ID &&
3292 (tline->type != TOK_PREPROC_ID ||
3293 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003294 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003295 "`%%strlen' expects a macro identifier as first parameter");
3296 free_tlist(origline);
3297 return DIRECTIVE_FOUND;
3298 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003299 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003300 last = tline;
3301 tline = expand_smacro(tline->next);
3302 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003303
H. Peter Anvine2c80182005-01-15 22:15:51 +00003304 t = tline;
3305 while (tok_type_(t, TOK_WHITESPACE))
3306 t = t->next;
3307 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003308 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003309 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003310 "`%%strlen` requires string as second parameter");
3311 free_tlist(tline);
3312 free_tlist(origline);
3313 return DIRECTIVE_FOUND;
3314 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003315
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003316 macro_start = nasm_malloc(sizeof(*macro_start));
3317 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003318 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003319 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003320
H. Peter Anvine2c80182005-01-15 22:15:51 +00003321 /*
3322 * We now have a macro name, an implicit parameter count of
3323 * zero, and a numeric token to use as an expansion. Create
3324 * and store an SMacro.
3325 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003326 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003327 free_tlist(tline);
3328 free_tlist(origline);
3329 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003330
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003331 case PP_STRCAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003332 casesense = true;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003333
3334 tline = tline->next;
3335 skip_white_(tline);
3336 tline = expand_id(tline);
3337 if (!tline || (tline->type != TOK_ID &&
3338 (tline->type != TOK_PREPROC_ID ||
3339 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003340 nasm_error(ERR_NONFATAL,
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003341 "`%%strcat' expects a macro identifier as first parameter");
3342 free_tlist(origline);
3343 return DIRECTIVE_FOUND;
3344 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003345 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003346 last = tline;
3347 tline = expand_smacro(tline->next);
3348 last->next = NULL;
3349
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003350 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003351 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003352 switch (t->type) {
3353 case TOK_WHITESPACE:
3354 break;
3355 case TOK_STRING:
3356 len += t->a.len = nasm_unquote(t->text, NULL);
3357 break;
3358 case TOK_OTHER:
3359 if (!strcmp(t->text, ",")) /* permit comma separators */
3360 break;
3361 /* else fall through */
3362 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003363 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003364 "non-string passed to `%%strcat' (%d)", t->type);
3365 free_tlist(tline);
3366 free_tlist(origline);
3367 return DIRECTIVE_FOUND;
3368 }
3369 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003370
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003371 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003372 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003373 if (t->type == TOK_STRING) {
3374 memcpy(p, t->text, t->a.len);
3375 p += t->a.len;
3376 }
3377 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003378
3379 /*
3380 * We now have a macro name, an implicit parameter count of
3381 * zero, and a numeric token to use as an expansion. Create
3382 * and store an SMacro.
3383 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003384 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3385 macro_start->text = nasm_quote(pp, len);
3386 nasm_free(pp);
3387 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003388 free_tlist(tline);
3389 free_tlist(origline);
3390 return DIRECTIVE_FOUND;
3391
H. Peter Anvine2c80182005-01-15 22:15:51 +00003392 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003393 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003394 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003395 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003396
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003397 casesense = true;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003398
H. Peter Anvine2c80182005-01-15 22:15:51 +00003399 tline = tline->next;
3400 skip_white_(tline);
3401 tline = expand_id(tline);
3402 if (!tline || (tline->type != TOK_ID &&
3403 (tline->type != TOK_PREPROC_ID ||
3404 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003405 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003406 "`%%substr' expects a macro identifier as first parameter");
3407 free_tlist(origline);
3408 return DIRECTIVE_FOUND;
3409 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003410 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003411 last = tline;
3412 tline = expand_smacro(tline->next);
3413 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003414
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003415 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003416 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003417 while (tok_type_(t, TOK_WHITESPACE))
3418 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003419
H. Peter Anvine2c80182005-01-15 22:15:51 +00003420 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003421 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003422 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003423 "`%%substr` requires string as second parameter");
3424 free_tlist(tline);
3425 free_tlist(origline);
3426 return DIRECTIVE_FOUND;
3427 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003428
H. Peter Anvine2c80182005-01-15 22:15:51 +00003429 tt = t->next;
3430 tptr = &tt;
3431 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003432 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003433 if (!evalresult) {
3434 free_tlist(tline);
3435 free_tlist(origline);
3436 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003437 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003438 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003439 free_tlist(tline);
3440 free_tlist(origline);
3441 return DIRECTIVE_FOUND;
3442 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003443 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003444
3445 while (tok_type_(tt, TOK_WHITESPACE))
3446 tt = tt->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003447 if (!tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003448 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003449 } else {
3450 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003451 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003452 if (!evalresult) {
3453 free_tlist(tline);
3454 free_tlist(origline);
3455 return DIRECTIVE_FOUND;
3456 } else if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003457 nasm_error(ERR_NONFATAL, "non-constant value given to `%%substr`");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003458 free_tlist(tline);
3459 free_tlist(origline);
3460 return DIRECTIVE_FOUND;
3461 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003462 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003463 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003464
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003465 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003466
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003467 /* make start and count being in range */
3468 if (start < 0)
3469 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003470 if (count < 0)
3471 count = len + count + 1 - start;
3472 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003473 count = len - start;
3474 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003475 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003476
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003477 macro_start = nasm_malloc(sizeof(*macro_start));
3478 macro_start->next = NULL;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003479 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start, count);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003480 macro_start->type = TOK_STRING;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003481 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003482
H. Peter Anvine2c80182005-01-15 22:15:51 +00003483 /*
3484 * We now have a macro name, an implicit parameter count of
3485 * zero, and a numeric token to use as an expansion. Create
3486 * and store an SMacro.
3487 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003488 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003489 free_tlist(tline);
3490 free_tlist(origline);
3491 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003492 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003493
H. Peter Anvine2c80182005-01-15 22:15:51 +00003494 case PP_ASSIGN:
3495 case PP_IASSIGN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003496 casesense = (i == PP_ASSIGN);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003497
H. Peter Anvine2c80182005-01-15 22:15:51 +00003498 tline = tline->next;
3499 skip_white_(tline);
3500 tline = expand_id(tline);
3501 if (!tline || (tline->type != TOK_ID &&
3502 (tline->type != TOK_PREPROC_ID ||
3503 tline->text[1] != '$'))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003504 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003505 "`%%%sassign' expects a macro identifier",
3506 (i == PP_IASSIGN ? "i" : ""));
3507 free_tlist(origline);
3508 return DIRECTIVE_FOUND;
3509 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003510 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003511 last = tline;
3512 tline = expand_smacro(tline->next);
3513 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003514
H. Peter Anvine2c80182005-01-15 22:15:51 +00003515 t = tline;
3516 tptr = &t;
3517 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08003518 evalresult = evaluate(ppscan, tptr, &tokval, NULL, pass, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003519 free_tlist(tline);
3520 if (!evalresult) {
3521 free_tlist(origline);
3522 return DIRECTIVE_FOUND;
3523 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003524
H. Peter Anvine2c80182005-01-15 22:15:51 +00003525 if (tokval.t_type)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003526 nasm_error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003527 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003528
H. Peter Anvine2c80182005-01-15 22:15:51 +00003529 if (!is_simple(evalresult)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003530 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003531 "non-constant value given to `%%%sassign'",
3532 (i == PP_IASSIGN ? "i" : ""));
3533 free_tlist(origline);
3534 return DIRECTIVE_FOUND;
3535 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003536
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003537 macro_start = nasm_malloc(sizeof(*macro_start));
3538 macro_start->next = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003539 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003540 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003541
H. Peter Anvine2c80182005-01-15 22:15:51 +00003542 /*
3543 * We now have a macro name, an implicit parameter count of
3544 * zero, and a numeric token to use as an expansion. Create
3545 * and store an SMacro.
3546 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003547 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003548 free_tlist(origline);
3549 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003550
H. Peter Anvine2c80182005-01-15 22:15:51 +00003551 case PP_LINE:
3552 /*
3553 * Syntax is `%line nnn[+mmm] [filename]'
3554 */
3555 tline = tline->next;
3556 skip_white_(tline);
3557 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003558 nasm_error(ERR_NONFATAL, "`%%line' expects line number");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003559 free_tlist(origline);
3560 return DIRECTIVE_FOUND;
3561 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003562 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003563 m = 1;
3564 tline = tline->next;
3565 if (tok_is_(tline, "+")) {
3566 tline = tline->next;
3567 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003568 nasm_error(ERR_NONFATAL, "`%%line' expects line increment");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003569 free_tlist(origline);
3570 return DIRECTIVE_FOUND;
3571 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003572 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003573 tline = tline->next;
3574 }
3575 skip_white_(tline);
3576 src_set_linnum(k);
3577 istk->lineinc = m;
3578 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003579 char *fname = detoken(tline, false);
3580 src_set_fname(fname);
3581 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003582 }
3583 free_tlist(origline);
3584 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003585
H. Peter Anvine2c80182005-01-15 22:15:51 +00003586 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003587 nasm_error(ERR_FATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003588 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003589 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003590 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003591 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003592}
3593
3594/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003595 * Ensure that a macro parameter contains a condition code and
3596 * nothing else. Return the condition code index if so, or -1
3597 * otherwise.
3598 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003599static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003600{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003601 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003602
H. Peter Anvin25a99342007-09-22 17:45:45 -07003603 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003604 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003605
H. Peter Anvineba20a72002-04-30 20:53:55 +00003606 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003607 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003608 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003609 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003610 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003611 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003612 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003613
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003614 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003615}
3616
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003617/*
3618 * This routines walks over tokens strem and hadnles tokens
3619 * pasting, if @handle_explicit passed then explicit pasting
3620 * term is handled, otherwise -- implicit pastings only.
3621 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003622static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003623 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003624{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003625 Token *tok, *next, **prev_next, **prev_nonspace;
3626 bool pasted = false;
3627 char *buf, *p;
3628 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003629
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003630 /*
3631 * The last token before pasting. We need it
3632 * to be able to connect new handled tokens.
3633 * In other words if there were a tokens stream
3634 *
3635 * A -> B -> C -> D
3636 *
3637 * and we've joined tokens B and C, the resulting
3638 * stream should be
3639 *
3640 * A -> BC -> D
3641 */
3642 tok = *head;
3643 prev_next = NULL;
3644
3645 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3646 prev_nonspace = head;
3647 else
3648 prev_nonspace = NULL;
3649
3650 while (tok && (next = tok->next)) {
3651
3652 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003653 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003654 /* Zap redundant whitespaces */
3655 while (tok_type_(next, TOK_WHITESPACE))
3656 next = delete_Token(next);
3657 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003658 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003659
3660 case TOK_PASTE:
3661 /* Explicit pasting */
3662 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003663 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003664 next = delete_Token(tok);
3665
3666 while (tok_type_(next, TOK_WHITESPACE))
3667 next = delete_Token(next);
3668
3669 if (!pasted)
3670 pasted = true;
3671
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003672 /* Left pasting token is start of line */
3673 if (!prev_nonspace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08003674 nasm_error(ERR_FATAL, "No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003675
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003676 /*
3677 * No ending token, this might happen in two
3678 * cases
3679 *
3680 * 1) There indeed no right token at all
3681 * 2) There is a bare "%define ID" statement,
3682 * and @ID does expand to whitespace.
3683 *
3684 * So technically we need to do a grammar analysis
3685 * in another stage of parsing, but for now lets don't
3686 * change the behaviour people used to. Simply allow
3687 * whitespace after paste token.
3688 */
3689 if (!next) {
3690 /*
3691 * Zap ending space tokens and that's all.
3692 */
3693 tok = (*prev_nonspace)->next;
3694 while (tok_type_(tok, TOK_WHITESPACE))
3695 tok = delete_Token(tok);
3696 tok = *prev_nonspace;
3697 tok->next = NULL;
3698 break;
3699 }
3700
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003701 tok = *prev_nonspace;
3702 while (tok_type_(tok, TOK_WHITESPACE))
3703 tok = delete_Token(tok);
3704 len = strlen(tok->text);
3705 len += strlen(next->text);
3706
3707 p = buf = nasm_malloc(len + 1);
3708 strcpy(p, tok->text);
3709 p = strchr(p, '\0');
3710 strcpy(p, next->text);
3711
3712 delete_Token(tok);
3713
3714 tok = tokenize(buf);
3715 nasm_free(buf);
3716
3717 *prev_nonspace = tok;
3718 while (tok && tok->next)
3719 tok = tok->next;
3720
3721 tok->next = delete_Token(next);
3722
3723 /* Restart from pasted tokens head */
3724 tok = *prev_nonspace;
3725 break;
3726
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003727 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003728 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003729 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003730 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3731 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003732
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003733 len = 0;
3734 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3735 len += strlen(next->text);
3736 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003737 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003738
3739 /* No match */
3740 if (tok == next)
3741 break;
3742
3743 len += strlen(tok->text);
3744 p = buf = nasm_malloc(len + 1);
3745
3746 while (tok != next) {
3747 strcpy(p, tok->text);
3748 p = strchr(p, '\0');
3749 tok = delete_Token(tok);
3750 }
3751
3752 tok = tokenize(buf);
3753 nasm_free(buf);
3754
3755 if (prev_next)
3756 *prev_next = tok;
3757 else
3758 *head = tok;
3759
3760 /*
3761 * Connect pasted into original stream,
3762 * ie A -> new-tokens -> B
3763 */
3764 while (tok && tok->next)
3765 tok = tok->next;
3766 tok->next = next;
3767
3768 if (!pasted)
3769 pasted = true;
3770
3771 /* Restart from pasted tokens head */
3772 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003773 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003774
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003775 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003776 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003777
3778 prev_next = &tok->next;
3779
3780 if (tok->next &&
3781 !tok_type_(tok->next, TOK_WHITESPACE) &&
3782 !tok_type_(tok->next, TOK_PASTE))
3783 prev_nonspace = prev_next;
3784
3785 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003786 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003787
3788 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003789}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003790
3791/*
3792 * expands to a list of tokens from %{x:y}
3793 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003794static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003795{
3796 Token *t = tline, **tt, *tm, *head;
3797 char *pos;
3798 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003799
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003800 pos = strchr(tline->text, ':');
3801 nasm_assert(pos);
3802
3803 lst = atoi(pos + 1);
3804 fst = atoi(tline->text + 1);
3805
3806 /*
3807 * only macros params are accounted so
3808 * if someone passes %0 -- we reject such
3809 * value(s)
3810 */
3811 if (lst == 0 || fst == 0)
3812 goto err;
3813
3814 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003815 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3816 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003817 goto err;
3818
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003819 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3820 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003821
3822 /* counted from zero */
3823 fst--, lst--;
3824
3825 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003826 * It will be at least one token. Note we
3827 * need to scan params until separator, otherwise
3828 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003829 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003830 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003831 head = new_Token(NULL, tm->type, tm->text, 0);
3832 tt = &head->next, tm = tm->next;
3833 while (tok_isnt_(tm, ",")) {
3834 t = new_Token(NULL, tm->type, tm->text, 0);
3835 *tt = t, tt = &t->next, tm = tm->next;
3836 }
3837
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003838 if (fst < lst) {
3839 for (i = fst + 1; i <= lst; i++) {
3840 t = new_Token(NULL, TOK_OTHER, ",", 0);
3841 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003842 j = (i + mac->rotate) % mac->nparam;
3843 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003844 while (tok_isnt_(tm, ",")) {
3845 t = new_Token(NULL, tm->type, tm->text, 0);
3846 *tt = t, tt = &t->next, tm = tm->next;
3847 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003848 }
3849 } else {
3850 for (i = fst - 1; i >= lst; i--) {
3851 t = new_Token(NULL, TOK_OTHER, ",", 0);
3852 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003853 j = (i + mac->rotate) % mac->nparam;
3854 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003855 while (tok_isnt_(tm, ",")) {
3856 t = new_Token(NULL, tm->type, tm->text, 0);
3857 *tt = t, tt = &t->next, tm = tm->next;
3858 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003859 }
3860 }
3861
3862 *last = tt;
3863 return head;
3864
3865err:
H. Peter Anvin130736c2016-02-17 20:27:41 -08003866 nasm_error(ERR_NONFATAL, "`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003867 &tline->text[1]);
3868 return tline;
3869}
3870
H. Peter Anvin76690a12002-04-30 20:52:49 +00003871/*
3872 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003873 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003874 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003875 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003876static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003877{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003878 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003879 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003880 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003881
3882 tail = &thead;
3883 thead = NULL;
3884
H. Peter Anvine2c80182005-01-15 22:15:51 +00003885 while (tline) {
3886 if (tline->type == TOK_PREPROC_ID &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003887 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3888 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3889 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003890 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003891 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003892 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003893 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003894 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003895 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003896
H. Peter Anvine2c80182005-01-15 22:15:51 +00003897 t = tline;
3898 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003899
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003900 mac = istk->mstk;
3901 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3902 mac = mac->next_active;
3903 if (!mac) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003904 nasm_error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003905 } else {
3906 pos = strchr(t->text, ':');
3907 if (!pos) {
3908 switch (t->text[1]) {
3909 /*
3910 * We have to make a substitution of one of the
3911 * forms %1, %-1, %+1, %%foo, %0.
3912 */
3913 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003914 type = TOK_NUMBER;
3915 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3916 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003917 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003918 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003919 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003920 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003921 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003922 text = nasm_strcat(tmpbuf, t->text + 2);
3923 break;
3924 case '-':
3925 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003926 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003927 tt = NULL;
3928 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003929 if (mac->nparam > 1)
3930 n = (n + mac->rotate) % mac->nparam;
3931 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003932 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003933 cc = find_cc(tt);
3934 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003935 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003936 "macro parameter %d is not a condition code",
3937 n + 1);
3938 text = NULL;
3939 } else {
3940 type = TOK_ID;
3941 if (inverse_ccs[cc] == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003942 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003943 "condition code `%s' is not invertible",
3944 conditions[cc]);
3945 text = NULL;
3946 } else
3947 text = nasm_strdup(conditions[inverse_ccs[cc]]);
3948 }
3949 break;
3950 case '+':
3951 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003952 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003953 tt = NULL;
3954 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003955 if (mac->nparam > 1)
3956 n = (n + mac->rotate) % mac->nparam;
3957 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003958 }
3959 cc = find_cc(tt);
3960 if (cc == -1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08003961 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003962 "macro parameter %d is not a condition code",
3963 n + 1);
3964 text = NULL;
3965 } else {
3966 type = TOK_ID;
3967 text = nasm_strdup(conditions[cc]);
3968 }
3969 break;
3970 default:
3971 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003972 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003973 tt = NULL;
3974 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003975 if (mac->nparam > 1)
3976 n = (n + mac->rotate) % mac->nparam;
3977 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003978 }
3979 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003980 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003981 *tail = new_Token(NULL, tt->type, tt->text, 0);
3982 tail = &(*tail)->next;
3983 tt = tt->next;
3984 }
3985 }
3986 text = NULL; /* we've done it here */
3987 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003988 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003989 } else {
3990 /*
3991 * seems we have a parameters range here
3992 */
3993 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003994 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003995 if (head != t) {
3996 *tail = head;
3997 *last = tline;
3998 tline = head;
3999 text = NULL;
4000 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004001 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004002 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004003 if (!text) {
4004 delete_Token(t);
4005 } else {
4006 *tail = t;
4007 tail = &t->next;
4008 t->type = type;
4009 nasm_free(t->text);
4010 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004011 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004012 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004013 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004014 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004015 } else if (tline->type == TOK_INDIRECT) {
4016 t = tline;
4017 tline = tline->next;
4018 tt = tokenize(t->text);
4019 tt = expand_mmac_params(tt);
4020 tt = expand_smacro(tt);
4021 *tail = tt;
4022 while (tt) {
4023 tt->a.mac = NULL; /* Necessary? */
4024 tail = &tt->next;
4025 tt = tt->next;
4026 }
4027 delete_Token(t);
4028 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004029 } else {
4030 t = *tail = tline;
4031 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004032 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004033 tail = &t->next;
4034 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004035 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004036 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004037
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004038 if (changed) {
4039 const struct tokseq_match t[] = {
4040 {
4041 PP_CONCAT_MASK(TOK_ID) |
4042 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4043 PP_CONCAT_MASK(TOK_ID) |
4044 PP_CONCAT_MASK(TOK_NUMBER) |
4045 PP_CONCAT_MASK(TOK_FLOAT) |
4046 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4047 },
4048 {
4049 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4050 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4051 }
4052 };
4053 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4054 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004055
H. Peter Anvin76690a12002-04-30 20:52:49 +00004056 return thead;
4057}
4058
4059/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004060 * Expand all single-line macro calls made in the given line.
4061 * Return the expanded version of the line. The original is deemed
4062 * to be destroyed in the process. (In reality we'll just move
4063 * Tokens from input to output a lot of the time, rather than
4064 * actually bothering to destroy and replicate.)
4065 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004066
H. Peter Anvine2c80182005-01-15 22:15:51 +00004067static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004068{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004069 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004070 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004071 Token **params;
4072 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07004073 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07004074 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004075 Token *org_tline = tline;
4076 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08004077 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08004078 int deadman = DEADMAN_LIMIT;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004079 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004080
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004081 /*
4082 * Trick: we should avoid changing the start token pointer since it can
4083 * be contained in "next" field of other token. Because of this
4084 * we allocate a copy of first token and work with it; at the end of
4085 * routine we copy it back
4086 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004087 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004088 tline = new_Token(org_tline->next, org_tline->type,
4089 org_tline->text, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004090 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004091 nasm_free(org_tline->text);
4092 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004093 }
4094
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004095 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004096
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004097again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004098 thead = NULL;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004099 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004100
H. Peter Anvine2c80182005-01-15 22:15:51 +00004101 while (tline) { /* main token loop */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004102 if (!--deadman) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004103 nasm_error(ERR_NONFATAL, "interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004104 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004105 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004106
H. Peter Anvine2c80182005-01-15 22:15:51 +00004107 if ((mname = tline->text)) {
4108 /* if this token is a local macro, look in local context */
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004109 if (tline->type == TOK_ID) {
4110 head = (SMacro *)hash_findix(&smacros, mname);
4111 } else if (tline->type == TOK_PREPROC_ID) {
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04004112 ctx = get_ctx(mname, &mname);
Cyrill Gorcunovc56d9ad2010-02-11 15:12:19 +03004113 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4114 } else
4115 head = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07004116
H. Peter Anvine2c80182005-01-15 22:15:51 +00004117 /*
4118 * We've hit an identifier. As in is_mmacro below, we first
4119 * check whether the identifier is a single-line macro at
4120 * all, then think about checking for parameters if
4121 * necessary.
4122 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004123 list_for_each(m, head)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004124 if (!mstrcmp(m->name, mname, m->casesense))
4125 break;
4126 if (m) {
4127 mstart = tline;
4128 params = NULL;
4129 paramsize = NULL;
4130 if (m->nparam == 0) {
4131 /*
4132 * Simple case: the macro is parameterless. Discard the
4133 * one token that the macro call took, and push the
4134 * expansion back on the to-do stack.
4135 */
4136 if (!m->expansion) {
4137 if (!strcmp("__FILE__", m->name)) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004138 const char *file = src_get_fname();
4139 /* nasm_free(tline->text); here? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004140 tline->text = nasm_quote(file, strlen(file));
4141 tline->type = TOK_STRING;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004142 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004143 }
4144 if (!strcmp("__LINE__", m->name)) {
4145 nasm_free(tline->text);
4146 make_tok_num(tline, src_get_linnum());
4147 continue;
4148 }
4149 if (!strcmp("__BITS__", m->name)) {
4150 nasm_free(tline->text);
4151 make_tok_num(tline, globalbits);
4152 continue;
4153 }
4154 tline = delete_Token(tline);
4155 continue;
4156 }
4157 } else {
4158 /*
4159 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00004160 * exists and takes parameters. We must find the
4161 * parameters in the call, count them, find the SMacro
4162 * that corresponds to that form of the macro call, and
4163 * substitute for the parameters when we expand. What a
4164 * pain.
4165 */
4166 /*tline = tline->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004167 skip_white_(tline); */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004168 do {
4169 t = tline->next;
4170 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004171 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004172 t->text = NULL;
4173 t = tline->next = delete_Token(t);
4174 }
4175 tline = t;
4176 } while (tok_type_(tline, TOK_WHITESPACE));
4177 if (!tok_is_(tline, "(")) {
4178 /*
4179 * This macro wasn't called with parameters: ignore
4180 * the call. (Behaviour borrowed from gnu cpp.)
4181 */
4182 tline = mstart;
4183 m = NULL;
4184 } else {
4185 int paren = 0;
4186 int white = 0;
4187 brackets = 0;
4188 nparam = 0;
4189 sparam = PARAM_DELTA;
4190 params = nasm_malloc(sparam * sizeof(Token *));
4191 params[0] = tline->next;
4192 paramsize = nasm_malloc(sparam * sizeof(int));
4193 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004194 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004195 /*
4196 * For some unusual expansions
4197 * which concatenates function call
4198 */
4199 t = tline->next;
4200 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004201 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004202 t->text = NULL;
4203 t = tline->next = delete_Token(t);
4204 }
4205 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004206
H. Peter Anvine2c80182005-01-15 22:15:51 +00004207 if (!tline) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004208 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004209 "macro call expects terminating `)'");
4210 break;
4211 }
4212 if (tline->type == TOK_WHITESPACE
4213 && brackets <= 0) {
4214 if (paramsize[nparam])
4215 white++;
4216 else
4217 params[nparam] = tline->next;
4218 continue; /* parameter loop */
4219 }
4220 if (tline->type == TOK_OTHER
4221 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004222 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004223 if (ch == ',' && !paren && brackets <= 0) {
4224 if (++nparam >= sparam) {
4225 sparam += PARAM_DELTA;
4226 params = nasm_realloc(params,
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004227 sparam * sizeof(Token *));
4228 paramsize = nasm_realloc(paramsize,
4229 sparam * sizeof(int));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004230 }
4231 params[nparam] = tline->next;
4232 paramsize[nparam] = 0;
4233 white = 0;
4234 continue; /* parameter loop */
4235 }
4236 if (ch == '{' &&
4237 (brackets > 0 || (brackets == 0 &&
4238 !paramsize[nparam])))
4239 {
4240 if (!(brackets++)) {
4241 params[nparam] = tline->next;
4242 continue; /* parameter loop */
4243 }
4244 }
4245 if (ch == '}' && brackets > 0)
4246 if (--brackets == 0) {
4247 brackets = -1;
4248 continue; /* parameter loop */
4249 }
4250 if (ch == '(' && !brackets)
4251 paren++;
4252 if (ch == ')' && brackets <= 0)
4253 if (--paren < 0)
4254 break;
4255 }
4256 if (brackets < 0) {
4257 brackets = 0;
H. Peter Anvin130736c2016-02-17 20:27:41 -08004258 nasm_error(ERR_NONFATAL, "braces do not "
H. Peter Anvine2c80182005-01-15 22:15:51 +00004259 "enclose all of macro parameter");
4260 }
4261 paramsize[nparam] += white + 1;
4262 white = 0;
4263 } /* parameter loop */
4264 nparam++;
4265 while (m && (m->nparam != nparam ||
4266 mstrcmp(m->name, mname,
4267 m->casesense)))
4268 m = m->next;
4269 if (!m)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004270 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004271 "macro `%s' exists, "
4272 "but not taking %d parameters",
4273 mstart->text, nparam);
4274 }
4275 }
4276 if (m && m->in_progress)
4277 m = NULL;
4278 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07004279 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00004280 * Design question: should we handle !tline, which
4281 * indicates missing ')' here, or expand those
4282 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07004283 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00004284 */
4285 nasm_free(params);
4286 nasm_free(paramsize);
4287 tline = mstart;
4288 } else {
4289 /*
4290 * Expand the macro: we are placed on the last token of the
4291 * call, so that we can easily split the call from the
4292 * following tokens. We also start by pushing an SMAC_END
4293 * token for the cycle removal.
4294 */
4295 t = tline;
4296 if (t) {
4297 tline = t->next;
4298 t->next = NULL;
4299 }
4300 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004301 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004302 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004303 tline = tt;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004304 list_for_each(t, m->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004305 if (t->type >= TOK_SMAC_PARAM) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004306 Token *pcopy = tline, **ptail = &pcopy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004307 Token *ttt, *pt;
4308 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004309
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004310 ttt = params[t->type - TOK_SMAC_PARAM];
4311 i = paramsize[t->type - TOK_SMAC_PARAM];
4312 while (--i >= 0) {
4313 pt = *ptail = new_Token(tline, ttt->type,
4314 ttt->text, 0);
4315 ptail = &pt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004316 ttt = ttt->next;
4317 }
4318 tline = pcopy;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004319 } else if (t->type == TOK_PREPROC_Q) {
4320 tt = new_Token(tline, TOK_ID, mname, 0);
4321 tline = tt;
4322 } else if (t->type == TOK_PREPROC_QQ) {
4323 tt = new_Token(tline, TOK_ID, m->name, 0);
4324 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004325 } else {
4326 tt = new_Token(tline, t->type, t->text, 0);
4327 tline = tt;
4328 }
4329 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004330
H. Peter Anvine2c80182005-01-15 22:15:51 +00004331 /*
4332 * Having done that, get rid of the macro call, and clean
4333 * up the parameters.
4334 */
4335 nasm_free(params);
4336 nasm_free(paramsize);
4337 free_tlist(mstart);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004338 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004339 continue; /* main token loop */
4340 }
4341 }
4342 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004343
H. Peter Anvine2c80182005-01-15 22:15:51 +00004344 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004345 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004346 tline = delete_Token(tline);
4347 } else {
4348 t = *tail = tline;
4349 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004350 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004351 t->next = NULL;
4352 tail = &t->next;
4353 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004354 }
4355
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004356 /*
4357 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004358 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004359 * TOK_IDs should be concatenated.
4360 * Also we look for %+ tokens and concatenate the tokens before and after
4361 * them (without white spaces in between).
4362 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004363 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004364 const struct tokseq_match t[] = {
4365 {
4366 PP_CONCAT_MASK(TOK_ID) |
4367 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4368 PP_CONCAT_MASK(TOK_ID) |
4369 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4370 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4371 }
4372 };
4373 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004374 /*
4375 * If we concatenated something, *and* we had previously expanded
4376 * an actual macro, scan the lines again for macros...
4377 */
4378 tline = thead;
4379 expanded = false;
4380 goto again;
4381 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004382 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004383
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004384err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004385 if (org_tline) {
4386 if (thead) {
4387 *org_tline = *thead;
4388 /* since we just gave text to org_line, don't free it */
4389 thead->text = NULL;
4390 delete_Token(thead);
4391 } else {
4392 /* the expression expanded to empty line;
4393 we can't return NULL for some reasons
4394 we just set the line to a single WHITESPACE token. */
4395 memset(org_tline, 0, sizeof(*org_tline));
4396 org_tline->text = NULL;
4397 org_tline->type = TOK_WHITESPACE;
4398 }
4399 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004400 }
4401
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004402 return thead;
4403}
4404
4405/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004406 * Similar to expand_smacro but used exclusively with macro identifiers
4407 * right before they are fetched in. The reason is that there can be
4408 * identifiers consisting of several subparts. We consider that if there
4409 * are more than one element forming the name, user wants a expansion,
4410 * otherwise it will be left as-is. Example:
4411 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004412 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004413 *
4414 * the identifier %$abc will be left as-is so that the handler for %define
4415 * will suck it and define the corresponding value. Other case:
4416 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004417 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004418 *
4419 * In this case user wants name to be expanded *before* %define starts
4420 * working, so we'll expand %$abc into something (if it has a value;
4421 * otherwise it will be left as-is) then concatenate all successive
4422 * PP_IDs into one.
4423 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004424static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004425{
4426 Token *cur, *oldnext = NULL;
4427
H. Peter Anvin734b1882002-04-30 21:01:08 +00004428 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004429 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004430
4431 cur = tline;
4432 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004433 (cur->next->type == TOK_ID ||
4434 cur->next->type == TOK_PREPROC_ID
4435 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004436 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004437
4438 /* If identifier consists of just one token, don't expand */
4439 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004440 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004441
H. Peter Anvine2c80182005-01-15 22:15:51 +00004442 if (cur) {
4443 oldnext = cur->next; /* Detach the tail past identifier */
4444 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004445 }
4446
H. Peter Anvin734b1882002-04-30 21:01:08 +00004447 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004448
H. Peter Anvine2c80182005-01-15 22:15:51 +00004449 if (cur) {
4450 /* expand_smacro possibly changhed tline; re-scan for EOL */
4451 cur = tline;
4452 while (cur && cur->next)
4453 cur = cur->next;
4454 if (cur)
4455 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004456 }
4457
4458 return tline;
4459}
4460
4461/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004462 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004463 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004464 * to check for an initial label - that's taken care of in
4465 * expand_mmacro - but must check numbers of parameters. Guaranteed
4466 * to be called with tline->type == TOK_ID, so the putative macro
4467 * name is easy to find.
4468 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004469static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004470{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004471 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004472 Token **params;
4473 int nparam;
4474
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004475 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004476
4477 /*
4478 * Efficiency: first we see if any macro exists with the given
4479 * name. If not, we can return NULL immediately. _Then_ we
4480 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004481 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004482 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004483 list_for_each(m, head)
4484 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004485 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004486 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004487 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004488
4489 /*
4490 * OK, we have a potential macro. Count and demarcate the
4491 * parameters.
4492 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004493 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004494
4495 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004496 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004497 * structure that handles this number.
4498 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004499 while (m) {
4500 if (m->nparam_min <= nparam
4501 && (m->plus || nparam <= m->nparam_max)) {
4502 /*
4503 * This one is right. Just check if cycle removal
4504 * prohibits us using it before we actually celebrate...
4505 */
4506 if (m->in_progress > m->max_depth) {
4507 if (m->max_depth > 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08004508 nasm_error(ERR_WARNING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004509 "reached maximum recursion depth of %i",
4510 m->max_depth);
4511 }
4512 nasm_free(params);
4513 return NULL;
4514 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004515 /*
4516 * It's right, and we can use it. Add its default
4517 * parameters to the end of our list if necessary.
4518 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004519 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004520 params =
4521 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004522 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004523 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004524 while (nparam < m->nparam_min + m->ndefs) {
4525 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004526 nparam++;
4527 }
4528 }
4529 /*
4530 * If we've gone over the maximum parameter count (and
4531 * we're in Plus mode), ignore parameters beyond
4532 * nparam_max.
4533 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004534 if (m->plus && nparam > m->nparam_max)
4535 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004536 /*
4537 * Then terminate the parameter list, and leave.
4538 */
4539 if (!params) { /* need this special case */
4540 params = nasm_malloc(sizeof(*params));
4541 nparam = 0;
4542 }
4543 params[nparam] = NULL;
4544 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004545 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004546 }
4547 /*
4548 * This one wasn't right: look for the next one with the
4549 * same name.
4550 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004551 list_for_each(m, m->next)
4552 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004553 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004554 }
4555
4556 /*
4557 * After all that, we didn't find one with the right number of
4558 * parameters. Issue a warning, and fail to expand the macro.
4559 */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004560 nasm_error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004561 "macro `%s' exists, but not taking %d parameters",
4562 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004563 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004564 return NULL;
4565}
4566
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004567
4568/*
4569 * Save MMacro invocation specific fields in
4570 * preparation for a recursive macro expansion
4571 */
4572static void push_mmacro(MMacro *m)
4573{
4574 MMacroInvocation *i;
4575
4576 i = nasm_malloc(sizeof(MMacroInvocation));
4577 i->prev = m->prev;
4578 i->params = m->params;
4579 i->iline = m->iline;
4580 i->nparam = m->nparam;
4581 i->rotate = m->rotate;
4582 i->paramlen = m->paramlen;
4583 i->unique = m->unique;
4584 i->condcnt = m->condcnt;
4585 m->prev = i;
4586}
4587
4588
4589/*
4590 * Restore MMacro invocation specific fields that were
4591 * saved during a previous recursive macro expansion
4592 */
4593static void pop_mmacro(MMacro *m)
4594{
4595 MMacroInvocation *i;
4596
4597 if (m->prev) {
4598 i = m->prev;
4599 m->prev = i->prev;
4600 m->params = i->params;
4601 m->iline = i->iline;
4602 m->nparam = i->nparam;
4603 m->rotate = i->rotate;
4604 m->paramlen = i->paramlen;
4605 m->unique = i->unique;
4606 m->condcnt = i->condcnt;
4607 nasm_free(i);
4608 }
4609}
4610
4611
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004612/*
4613 * Expand the multi-line macro call made by the given line, if
4614 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004615 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004616 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004617static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004618{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004619 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004620 Token *label = NULL;
4621 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004622 Token **params, *t, *tt;
4623 MMacro *m;
4624 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004625 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004626 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004627
4628 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004629 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004630 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004631 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004632 return 0;
4633 m = is_mmacro(t, &params);
4634 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004635 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004636 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004637 Token *last;
4638 /*
4639 * We have an id which isn't a macro call. We'll assume
4640 * it might be a label; we'll also check to see if a
4641 * colon follows it. Then, if there's another id after
4642 * that lot, we'll check it again for macro-hood.
4643 */
4644 label = last = t;
4645 t = t->next;
4646 if (tok_type_(t, TOK_WHITESPACE))
4647 last = t, t = t->next;
4648 if (tok_is_(t, ":")) {
4649 dont_prepend = 1;
4650 last = t, t = t->next;
4651 if (tok_type_(t, TOK_WHITESPACE))
4652 last = t, t = t->next;
4653 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004654 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4655 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004656 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004657 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004658 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004659 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004660
4661 /*
4662 * Fix up the parameters: this involves stripping leading and
4663 * trailing whitespace, then stripping braces if they are
4664 * present.
4665 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004666 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004667 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004668
H. Peter Anvine2c80182005-01-15 22:15:51 +00004669 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004670 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004671 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004672
H. Peter Anvine2c80182005-01-15 22:15:51 +00004673 t = params[i];
4674 skip_white_(t);
4675 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004676 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004677 params[i] = t;
4678 paramlen[i] = 0;
4679 while (t) {
4680 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4681 break; /* ... because we have hit a comma */
4682 if (comma && t->type == TOK_WHITESPACE
4683 && tok_is_(t->next, ","))
4684 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004685 if (brace && t->type == TOK_OTHER) {
4686 if (t->text[0] == '{')
4687 brace++; /* ... or a nested opening brace */
4688 else if (t->text[0] == '}')
4689 if (!--brace)
4690 break; /* ... or a brace */
4691 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004692 t = t->next;
4693 paramlen[i]++;
4694 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004695 if (brace)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004696 nasm_error(ERR_NONFATAL, "macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004697 }
4698
4699 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004700 * OK, we have a MMacro structure together with a set of
4701 * parameters. We must now go through the expansion and push
4702 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004703 * parameter tokens and macro-local tokens doesn't get done
4704 * until the single-line macro substitution process; this is
4705 * because delaying them allows us to change the semantics
4706 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004707 *
4708 * First, push an end marker on to istk->expansion, mark this
4709 * macro as in progress, and set up its invocation-specific
4710 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004711 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004712 ll = nasm_malloc(sizeof(Line));
4713 ll->next = istk->expansion;
4714 ll->finishes = m;
4715 ll->first = NULL;
4716 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004717
4718 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004719 * Save the previous MMacro expansion in the case of
4720 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004721 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004722 if (m->max_depth && m->in_progress)
4723 push_mmacro(m);
4724
4725 m->in_progress ++;
4726 m->params = params;
4727 m->iline = tline;
4728 m->nparam = nparam;
4729 m->rotate = 0;
4730 m->paramlen = paramlen;
4731 m->unique = unique++;
4732 m->lineno = 0;
4733 m->condcnt = 0;
4734
4735 m->next_active = istk->mstk;
4736 istk->mstk = m;
4737
4738 list_for_each(l, m->expansion) {
4739 Token **tail;
4740
4741 ll = nasm_malloc(sizeof(Line));
4742 ll->finishes = NULL;
4743 ll->next = istk->expansion;
4744 istk->expansion = ll;
4745 tail = &ll->first;
4746
4747 list_for_each(t, l->first) {
4748 Token *x = t;
4749 switch (t->type) {
4750 case TOK_PREPROC_Q:
4751 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004752 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004753 case TOK_PREPROC_QQ:
4754 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4755 break;
4756 case TOK_PREPROC_ID:
4757 if (t->text[1] == '0' && t->text[2] == '0') {
4758 dont_prepend = -1;
4759 x = label;
4760 if (!x)
4761 continue;
4762 }
4763 /* fall through */
4764 default:
4765 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4766 break;
4767 }
4768 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004769 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004770 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004771 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004772
4773 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004774 * If we had a label, push it on as the first line of
4775 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004776 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004777 if (label) {
4778 if (dont_prepend < 0)
4779 free_tlist(startline);
4780 else {
4781 ll = nasm_malloc(sizeof(Line));
4782 ll->finishes = NULL;
4783 ll->next = istk->expansion;
4784 istk->expansion = ll;
4785 ll->first = startline;
4786 if (!dont_prepend) {
4787 while (label->next)
4788 label = label->next;
4789 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004790 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004791 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004792 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004793
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004794 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004795
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004796 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004797}
4798
H. Peter Anvin130736c2016-02-17 20:27:41 -08004799/*
4800 * This function adds macro names to error messages, and suppresses
4801 * them if necessary.
4802 */
4803static void pp_verror(int severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004804{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004805 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004806 MMacro *mmac = NULL;
4807 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004808
H. Peter Anvin130736c2016-02-17 20:27:41 -08004809 /*
4810 * If we're in a dead branch of IF or something like it, ignore the error.
4811 * However, because %else etc are evaluated in the state context
4812 * of the previous branch, errors might get lost:
4813 * %if 0 ... %else trailing garbage ... %endif
4814 * So %else etc should set the ERR_PP_PRECOND flag.
4815 */
4816 if ((severity & ERR_MASK) < ERR_FATAL &&
4817 istk && istk->conds &&
4818 ((severity & ERR_PP_PRECOND) ?
4819 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004820 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004821 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004822
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004823 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004824 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004825 mmac = istk->mstk;
4826 /* but %rep blocks should be skipped */
4827 while (mmac && !mmac->name)
4828 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004829 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004830
H. Peter Anvin130736c2016-02-17 20:27:41 -08004831 if (mmac) {
4832 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004833
H. Peter Anvin130736c2016-02-17 20:27:41 -08004834 nasm_set_verror(real_verror);
4835 nasm_error(severity, "(%s:%d) %s",
4836 mmac->name, mmac->lineno - delta, buff);
4837 nasm_set_verror(pp_verror);
4838 } else {
4839 real_verror(severity, fmt, arg);
4840 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004841}
4842
H. Peter Anvin734b1882002-04-30 21:01:08 +00004843static void
H. Peter Anvin130736c2016-02-17 20:27:41 -08004844pp_reset(char *file, int apass, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004845{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004846 Token *t;
4847
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004848 cstk = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004849 istk = nasm_malloc(sizeof(Include));
4850 istk->next = NULL;
4851 istk->conds = NULL;
4852 istk->expansion = NULL;
4853 istk->mstk = NULL;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07004854 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004855 istk->fname = NULL;
H. Peter Anvin274cda82016-05-10 02:56:29 -07004856 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004857 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004858 if (!istk->fp)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004859 nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004860 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004861 nested_mac_count = 0;
4862 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004863 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004864 unique = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004865 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004866 stdmacpos = nasm_stdmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004867 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004868 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004869 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004870 any_extrastdmac = extrastdmac && *extrastdmac;
4871 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004872
4873 /*
4874 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4875 * The caller, however, will also pass in 3 for preprocess-only so
4876 * we can set __PASS__ accordingly.
4877 */
4878 pass = apass > 2 ? 2 : apass;
4879
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004880 dephead = deptail = deplist;
4881 if (deplist) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004882 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4883 sl->next = NULL;
4884 strcpy(sl->str, file);
4885 *deptail = sl;
4886 deptail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004887 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004888
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004889 /*
4890 * Define the __PASS__ macro. This is defined here unlike
4891 * all the other builtins, because it is special -- it varies between
4892 * passes.
4893 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004894 t = nasm_malloc(sizeof(*t));
4895 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004896 make_tok_num(t, apass);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004897 t->a.mac = NULL;
H. Peter Anvin7383b402008-09-24 10:20:40 -07004898 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004899}
4900
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004901static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004902{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004903 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004904 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004905
H. Peter Anvin130736c2016-02-17 20:27:41 -08004906 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08004907
H. Peter Anvine2c80182005-01-15 22:15:51 +00004908 while (1) {
4909 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004910 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004911 * buffer or from the input file.
4912 */
4913 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004914 while (istk->expansion && istk->expansion->finishes) {
4915 Line *l = istk->expansion;
4916 if (!l->finishes->name && l->finishes->in_progress > 1) {
4917 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004918
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004919 /*
4920 * This is a macro-end marker for a macro with no
4921 * name, which means it's not really a macro at all
4922 * but a %rep block, and the `in_progress' field is
4923 * more than 1, meaning that we still need to
4924 * repeat. (1 means the natural last repetition; 0
4925 * means termination by %exitrep.) We have
4926 * therefore expanded up to the %endrep, and must
4927 * push the whole block on to the expansion buffer
4928 * again. We don't bother to remove the macro-end
4929 * marker: we'd only have to generate another one
4930 * if we did.
4931 */
4932 l->finishes->in_progress--;
4933 list_for_each(l, l->finishes->expansion) {
4934 Token *t, *tt, **tail;
4935
4936 ll = nasm_malloc(sizeof(Line));
4937 ll->next = istk->expansion;
4938 ll->finishes = NULL;
4939 ll->first = NULL;
4940 tail = &ll->first;
4941
4942 list_for_each(t, l->first) {
4943 if (t->text || t->type == TOK_WHITESPACE) {
4944 tt = *tail = new_Token(NULL, t->type, t->text, 0);
4945 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004946 }
4947 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004948
4949 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004950 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004951 } else {
4952 /*
4953 * Check whether a `%rep' was started and not ended
4954 * within this macro expansion. This can happen and
4955 * should be detected. It's a fatal error because
4956 * I'm too confused to work out how to recover
4957 * sensibly from it.
4958 */
4959 if (defining) {
4960 if (defining->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004961 nasm_panic(0, "defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004962 else if (istk->mstk->name)
H. Peter Anvin130736c2016-02-17 20:27:41 -08004963 nasm_fatal(0, "`%%rep' without `%%endrep' within"
4964 " expansion of macro `%s'",
4965 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004966 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004967
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004968 /*
4969 * FIXME: investigate the relationship at this point between
4970 * istk->mstk and l->finishes
4971 */
4972 {
4973 MMacro *m = istk->mstk;
4974 istk->mstk = m->next_active;
4975 if (m->name) {
4976 /*
4977 * This was a real macro call, not a %rep, and
4978 * therefore the parameter information needs to
4979 * be freed.
4980 */
4981 if (m->prev) {
4982 pop_mmacro(m);
4983 l->finishes->in_progress --;
4984 } else {
4985 nasm_free(m->params);
4986 free_tlist(m->iline);
4987 nasm_free(m->paramlen);
4988 l->finishes->in_progress = 0;
4989 }
4990 } else
4991 free_mmacro(m);
4992 }
4993 istk->expansion = l->next;
4994 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004995 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004996 }
4997 }
4998 while (1) { /* until we get a line we can use */
4999
5000 if (istk->expansion) { /* from a macro expansion */
5001 char *p;
5002 Line *l = istk->expansion;
5003 if (istk->mstk)
5004 istk->mstk->lineno++;
5005 tline = l->first;
5006 istk->expansion = l->next;
5007 nasm_free(l);
5008 p = detoken(tline, false);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005009 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005010 nasm_free(p);
5011 break;
5012 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005013 line = read_line();
5014 if (line) { /* from the current input file */
5015 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005016 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005017 nasm_free(line);
5018 break;
5019 }
5020 /*
5021 * The current file has ended; work down the istk
5022 */
5023 {
5024 Include *i = istk;
5025 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005026 if (i->conds) {
5027 /* nasm_error can't be conditionally suppressed */
H. Peter Anvin41087062016-03-03 14:27:34 -08005028 nasm_fatal(0,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005029 "expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005030 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005031 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005032 if (i->next)
5033 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005034 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005035 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005036 nasm_free(i);
H. Peter Anvin130736c2016-02-17 20:27:41 -08005037 if (!istk) {
5038 line = NULL;
5039 goto done;
5040 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005041 if (istk->expansion && istk->expansion->finishes)
5042 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005043 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005044 }
5045
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005046 /*
5047 * We must expand MMacro parameters and MMacro-local labels
5048 * _before_ we plunge into directive processing, to cope
5049 * with things like `%define something %1' such as STRUC
5050 * uses. Unless we're _defining_ a MMacro, in which case
5051 * those tokens should be left alone to go into the
5052 * definition; and unless we're in a non-emitting
5053 * condition, in which case we don't want to meddle with
5054 * anything.
5055 */
5056 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5057 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005058 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005059 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005060
H. Peter Anvine2c80182005-01-15 22:15:51 +00005061 /*
5062 * Check the line to see if it's a preprocessor directive.
5063 */
5064 if (do_directive(tline) == DIRECTIVE_FOUND) {
5065 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005066 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005067 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005068 * We're defining a multi-line macro. We emit nothing
5069 * at all, and just
5070 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005071 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005072 Line *l = nasm_malloc(sizeof(Line));
5073 l->next = defining->expansion;
5074 l->first = tline;
5075 l->finishes = NULL;
5076 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005077 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005078 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005079 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005080 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005081 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005082 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005083 * directive so we keep our place correctly.
5084 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005085 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005086 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005087 } else if (istk->mstk && !istk->mstk->in_progress) {
5088 /*
5089 * We're in a %rep block which has been terminated, so
5090 * we're walking through to the %endrep without
5091 * emitting anything. Emit nothing at all, not even a
5092 * blank line: when we emerge from the %rep block we'll
5093 * give a line-number directive so we keep our place
5094 * correctly.
5095 */
5096 free_tlist(tline);
5097 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005098 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005099 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005100 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005101 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005102 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005103 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005104 line = detoken(tline, true);
5105 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005106 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005107 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005108 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005109 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005110 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005111 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005112
H. Peter Anvin130736c2016-02-17 20:27:41 -08005113done:
5114 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005115 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005116}
5117
H. Peter Anvine2c80182005-01-15 22:15:51 +00005118static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005119{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005120 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005121
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005122 if (defining) {
5123 if (defining->name) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005124 nasm_error(ERR_NONFATAL,
5125 "end of file while still defining macro `%s'",
5126 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005127 } else {
H. Peter Anvin130736c2016-02-17 20:27:41 -08005128 nasm_error(ERR_NONFATAL, "end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005129 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005130
5131 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005132 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005133 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005134
5135 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005136
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005137 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005138 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005139 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005140 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005141 Include *i = istk;
5142 istk = istk->next;
5143 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005144 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005145 }
5146 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005147 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005148 src_set_fname(NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005149 if (pass == 0) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005150 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005151 free_llist(predef);
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005152 predef = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005153 delete_Blocks();
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04005154 freeTokens = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005155 while ((i = ipath)) {
5156 ipath = i->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005157 if (i->path)
5158 nasm_free(i->path);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005159 nasm_free(i);
5160 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07005161 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005162}
5163
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005164static void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005165{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005166 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07005167
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005168 i = nasm_malloc(sizeof(IncPath));
5169 i->path = path ? nasm_strdup(path) : NULL;
5170 i->next = NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005171
H. Peter Anvin89cee572009-07-15 09:16:54 -04005172 if (ipath) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005173 IncPath *j = ipath;
H. Peter Anvin89cee572009-07-15 09:16:54 -04005174 while (j->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005175 j = j->next;
5176 j->next = i;
5177 } else {
5178 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005179 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005180}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005181
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005182static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005183{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005184 Token *inc, *space, *name;
5185 Line *l;
5186
H. Peter Anvin734b1882002-04-30 21:01:08 +00005187 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5188 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5189 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005190
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005191 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005192 l->next = predef;
5193 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005194 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005195 predef = l;
5196}
5197
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005198static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005199{
5200 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005201 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005202 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005203
H. Peter Anvin130736c2016-02-17 20:27:41 -08005204 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005205
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005206 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005207 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5208 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005209 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005210 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005211 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005212 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005213 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005214
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005215 if (space->next->type != TOK_PREPROC_ID &&
5216 space->next->type != TOK_ID)
H. Peter Anvin130736c2016-02-17 20:27:41 -08005217 nasm_error(ERR_WARNING, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005218
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005219 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005220 l->next = predef;
5221 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005222 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005223 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005224
5225 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005226}
5227
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005228static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005229{
5230 Token *def, *space;
5231 Line *l;
5232
H. Peter Anvin734b1882002-04-30 21:01:08 +00005233 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5234 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005235 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005236
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005237 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005238 l->next = predef;
5239 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005240 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005241 predef = l;
5242}
5243
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005244static void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005245{
H. Peter Anvin76690a12002-04-30 20:52:49 +00005246 extrastdmac = macros;
5247}
5248
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005249static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005250{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005251 char numbuf[32];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07005252 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005253 tok->text = nasm_strdup(numbuf);
5254 tok->type = TOK_NUMBER;
5255}
5256
H. Peter Anvin37368952016-05-09 14:10:32 -07005257static void pp_list_one_macro(MMacro *m, int severity)
5258{
5259 if (!m)
5260 return;
5261
5262 /* We need to print the next_active list in reverse order */
5263 pp_list_one_macro(m->next_active, severity);
5264
5265 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005266 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvin37368952016-05-09 14:10:32 -07005267 nasm_error(severity, "... from macro `%s' defined here", m->name);
5268 }
5269}
5270
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005271static void pp_error_list_macros(int severity)
5272{
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005273 int32_t saved_line;
5274 const char *saved_fname = NULL;
5275
5276 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY;
H. Peter Anvin274cda82016-05-10 02:56:29 -07005277 src_get(&saved_line, &saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005278
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005279 if (istk)
5280 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005281
H. Peter Anvin274cda82016-05-10 02:56:29 -07005282 src_set(saved_line, saved_fname);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005283}
5284
H. Peter Anvine7469712016-02-18 02:20:59 -08005285const struct preproc_ops nasmpp = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005286 pp_reset,
5287 pp_getline,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005288 pp_cleanup,
5289 pp_extra_stdmac,
5290 pp_pre_define,
5291 pp_pre_undefine,
5292 pp_pre_include,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005293 pp_include_path,
5294 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005295};