blob: b0fbaadb76020076a66c018089104686d5cbfea6 [file] [log] [blame]
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001/* preproc.c macro preprocessor for the Netwide Assembler
2 *
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
Beroset095e6a22007-12-29 09:44:23 -05005 * redistributable under the license given in the file "LICENSE"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 * distributed in the NASM archive.
7 *
8 * initial version 18/iii/97 by Simon Tatham
9 */
10
H. Peter Anvin4836e332002-04-30 20:56:43 +000011/* Typical flow of text through preproc
12 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000013 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000014 *
15 * from a macro expansion
16 *
17 * or
18 * {
19 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000020 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000021 * }
22 *
23 * expand_mmac_params is used to expand %1 etc., unless a macro is being
24 * defined or a false conditional is being processed
25 * (%0, %1, %+1, %-1, %%foo
26 *
27 * do_directive checks for directives
28 *
29 * expand_smacro is used to expand single line macros
30 *
31 * expand_mmacro is used to expand multi-line macros
32 *
33 * detoken is used to convert the line back to text
34 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000035
H. Peter Anvinfe501952007-10-02 21:53:51 -070036#include "compiler.h"
37
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000038#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000039#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000040#include <stdlib.h>
41#include <stddef.h>
42#include <string.h>
43#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000044#include <limits.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000045#include <inttypes.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000046
47#include "nasm.h"
48#include "nasmlib.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000049#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070050#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070051#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070052#include "stdscan.h"
53#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070054#include "tables.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000055
56typedef struct SMacro SMacro;
57typedef struct MMacro MMacro;
58typedef struct Context Context;
59typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000060typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000061typedef struct Line Line;
62typedef struct Include Include;
63typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000064typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000065
66/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070067 * Note on the storage of both SMacro and MMacros: the hash table
68 * indexes them case-insensitively, and we then have to go through a
69 * linked list of potential case aliases (and, for MMacros, parameter
70 * ranges); this is to preserve the matching semantics of the earlier
71 * code. If the number of case aliases for a specific macro is a
72 * performance issue, you may want to reconsider your coding style.
73 */
74
75/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000076 * Store the definition of a single-line macro.
77 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000078struct SMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000079 SMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +000080 char *name;
H. Peter Anvin70055962007-10-11 00:05:31 -070081 bool casesense;
H. Peter Anvin16ed4382007-10-11 10:06:19 -070082 bool in_progress;
H. Peter Anvin70055962007-10-11 00:05:31 -070083 unsigned int nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000084 Token *expansion;
85};
86
87/*
H. Peter Anvin76690a12002-04-30 20:52:49 +000088 * Store the definition of a multi-line macro. This is also used to
89 * store the interiors of `%rep...%endrep' blocks, which are
90 * effectively self-re-invoking multi-line macros which simply
91 * don't have a name or bother to appear in the hash tables. %rep
92 * blocks are signified by having a NULL `name' field.
93 *
94 * In a MMacro describing a `%rep' block, the `in_progress' field
95 * isn't merely boolean, but gives the number of repeats left to
96 * run.
97 *
98 * The `next' field is used for storing MMacros in hash tables; the
99 * `next_active' field is for stacking them on istk entries.
100 *
101 * When a MMacro is being expanded, `params', `iline', `nparam',
102 * `paramlen', `rotate' and `unique' are local to the invocation.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000103 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000104struct MMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 MMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000106 char *name;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -0700107 int nparam_min, nparam_max;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700108 bool casesense;
109 bool plus; /* is the last parameter greedy? */
110 bool nolist; /* is this macro listing-inhibited? */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700111 int64_t in_progress;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000112 Token *dlist; /* All defaults as one list */
113 Token **defaults; /* Parameter default pointers */
114 int ndefs; /* number of default parameters */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000115 Line *expansion;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000116
117 MMacro *next_active;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000118 MMacro *rep_nest; /* used for nesting %rep */
119 Token **params; /* actual parameters */
120 Token *iline; /* invocation line */
H. Peter Anvin25a99342007-09-22 17:45:45 -0700121 unsigned int nparam, rotate;
122 int *paramlen;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700123 uint64_t unique;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000124 int lineno; /* Current line number on expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000125};
126
127/*
128 * The context stack is composed of a linked list of these.
129 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000130struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000131 Context *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000132 char *name;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700133 struct hash_table localmac;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000134 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000135};
136
137/*
138 * This is the internal form which we break input lines up into.
139 * Typically stored in linked lists.
140 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000141 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
142 * necessarily used as-is, but is intended to denote the number of
143 * the substituted parameter. So in the definition
144 *
145 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700146 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000147 * the token representing `x' will have its type changed to
148 * TOK_SMAC_PARAM, but the one representing `y' will be
149 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000150 *
151 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
152 * which doesn't need quotes around it. Used in the pre-include
153 * mechanism as an alternative to trying to find a sensible type of
154 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000155 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000156enum pp_token_type {
157 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
158 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700159 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
160 TOK_INTERNAL_STRING,
161 TOK_PREPROC_Q, TOK_PREPROC_QQ,
H. Peter Anvin5b76fa22008-05-26 11:14:38 -0700162 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
163 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000164};
165
H. Peter Anvine2c80182005-01-15 22:15:51 +0000166struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000167 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000168 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700169 union {
170 SMacro *mac; /* associated macro for TOK_SMAC_END */
171 size_t len; /* scratch length field */
172 } a; /* Auxiliary data */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000173 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000174};
175
176/*
177 * Multi-line macro definitions are stored as a linked list of
178 * these, which is essentially a container to allow several linked
179 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700180 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000181 * Note that in this module, linked lists are treated as stacks
182 * wherever possible. For this reason, Lines are _pushed_ on to the
183 * `expansion' field in MMacro structures, so that the linked list,
184 * if walked, would give the macro lines in reverse order; this
185 * means that we can walk the list when expanding a macro, and thus
186 * push the lines on to the `expansion' field in _istk_ in reverse
187 * order (so that when popped back off they are in the right
188 * order). It may seem cockeyed, and it relies on my design having
189 * an even number of steps in, but it works...
190 *
191 * Some of these structures, rather than being actual lines, are
192 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000193 * This is for use in the cycle-tracking and %rep-handling code.
194 * Such structures have `finishes' non-NULL, and `first' NULL. All
195 * others have `finishes' NULL, but `first' may still be NULL if
196 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000197 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000198struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000199 Line *next;
200 MMacro *finishes;
201 Token *first;
202};
203
204/*
205 * To handle an arbitrary level of file inclusion, we maintain a
206 * stack (ie linked list) of these things.
207 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000208struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000209 Include *next;
210 FILE *fp;
211 Cond *conds;
212 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000213 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000214 int lineno, lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000215 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000216};
217
218/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000219 * Include search path. This is simply a list of strings which get
220 * prepended, in turn, to the name of an include file, in an
221 * attempt to find the file if it's not in the current directory.
222 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000223struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000224 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000225 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000226};
227
228/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000229 * Conditional assembly: we maintain a separate stack of these for
230 * each level of file inclusion. (The only reason we keep the
231 * stacks separate is to ensure that a stray `%endif' in a file
232 * included from within the true branch of a `%if' won't terminate
233 * it and cause confusion: instead, rightly, it'll cause an error.)
234 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000235struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000236 Cond *next;
237 int state;
238};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000239enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000240 /*
241 * These states are for use just after %if or %elif: IF_TRUE
242 * means the condition has evaluated to truth so we are
243 * currently emitting, whereas IF_FALSE means we are not
244 * currently emitting but will start doing so if a %else comes
245 * up. In these states, all directives are admissible: %elif,
246 * %else and %endif. (And of course %if.)
247 */
248 COND_IF_TRUE, COND_IF_FALSE,
249 /*
250 * These states come up after a %else: ELSE_TRUE means we're
251 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
252 * any %elif or %else will cause an error.
253 */
254 COND_ELSE_TRUE, COND_ELSE_FALSE,
255 /*
256 * This state means that we're not emitting now, and also that
257 * nothing until %endif will be emitted at all. It's for use in
258 * two circumstances: (i) when we've had our moment of emission
259 * and have now started seeing %elifs, and (ii) when the
260 * condition construct in question is contained within a
261 * non-emitting branch of a larger condition construct.
262 */
263 COND_NEVER
264};
265#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
266
H. Peter Anvin70653092007-10-19 14:42:29 -0700267/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000268 * These defines are used as the possible return values for do_directive
269 */
270#define NO_DIRECTIVE_FOUND 0
271#define DIRECTIVE_FOUND 1
272
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000273/*
274 * Condition codes. Note that we use c_ prefix not C_ because C_ is
275 * used in nasm.h for the "real" condition codes. At _this_ level,
276 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
277 * ones, so we need a different enum...
278 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700279static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000280 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
281 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000282 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000283};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700284enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000285 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
286 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 -0700287 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
288 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000289};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700290static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000291 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
292 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 +0000293 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000294};
295
H. Peter Anvin76690a12002-04-30 20:52:49 +0000296/*
297 * Directive names.
298 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000299/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000300static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000301{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000302 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000303}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000304
305/* For TASM compatibility we need to be able to recognise TASM compatible
306 * conditional compilation directives. Using the NASM pre-processor does
307 * not work, so we look for them specifically from the following list and
308 * then jam in the equivalent NASM directive into the input stream.
309 */
310
H. Peter Anvine2c80182005-01-15 22:15:51 +0000311enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000312 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
313 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
314};
315
H. Peter Anvin476d2862007-10-02 22:04:15 -0700316static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000317 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
318 "ifndef", "include", "local"
319};
320
321static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000322static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000323static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800324static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000325
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000326static Context *cstk;
327static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000328static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000329
H. Peter Anvine2c80182005-01-15 22:15:51 +0000330static efunc _error; /* Pointer to client-provided error reporting function */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000331static evalfunc evaluate;
332
H. Peter Anvine2c80182005-01-15 22:15:51 +0000333static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700334static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000335
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700336static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000337
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000338static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700339static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000340
341static ListGen *list;
342
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000343/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000344 * The current set of multi-line macros we have defined.
345 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700346static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000347
348/*
349 * The current set of single-line macros we have defined.
350 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700351static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000352
353/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000354 * The multi-line macro we are currently defining, or the %rep
355 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000356 */
357static MMacro *defining;
358
Charles Crayned4200be2008-07-12 16:42:33 -0700359static uint64_t nested_mac_count;
360static uint64_t nested_rep_count;
361
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000362/*
363 * The number of macro parameters to allocate space for at a time.
364 */
365#define PARAM_DELTA 16
366
367/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700368 * The standard macro set: defined in macros.c in the array nasm_stdmac.
369 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000370 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700371static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000372
373/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000374 * The extra standard macros that come from the object format, if
375 * any.
376 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700377static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700378static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000379
380/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000381 * Tokens are allocated in blocks to improve speed
382 */
383#define TOKEN_BLOCKSIZE 4096
384static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000385struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000386 Blocks *next;
387 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000388};
389
390static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000391
392/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000393 * Forward declarations.
394 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000395static Token *expand_mmac_params(Token * tline);
396static Token *expand_smacro(Token * tline);
397static Token *expand_id(Token * tline);
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -0700398static Context *get_ctx(const char *name, bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700399static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000400static void error(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000401static void *new_Block(size_t size);
402static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700403static Token *new_Token(Token * next, enum pp_token_type type,
404 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000405static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000406
407/*
408 * Macros for safe checking of token pointers, avoid *(NULL)
409 */
410#define tok_type_(x,t) ((x) && (x)->type == (t))
411#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
412#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
413#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000414
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000415/* Handle TASM specific directives, which do not contain a % in
416 * front of them. We do it here because I could not find any other
417 * place to do it for the moment, and it is a hack (ideally it would
418 * be nice to be able to use the NASM pre-processor to do it).
419 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000420static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000421{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000422 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000423 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000424
425 /* Skip whitespace */
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700426 while (nasm_isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000427 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000428
429 /* Binary search for the directive name */
430 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000431 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000432 len = 0;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700433 while (!nasm_isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000434 len++;
435 if (len) {
436 oldchar = p[len];
437 p[len] = 0;
438 while (j - i > 1) {
439 k = (j + i) / 2;
440 m = nasm_stricmp(p, tasm_directives[k]);
441 if (m == 0) {
442 /* We have found a directive, so jam a % in front of it
443 * so that NASM will then recognise it as one if it's own.
444 */
445 p[len] = oldchar;
446 len = strlen(p);
447 oldline = line;
448 line = nasm_malloc(len + 2);
449 line[0] = '%';
450 if (k == TM_IFDIFI) {
451 /* NASM does not recognise IFDIFI, so we convert it to
452 * %ifdef BOGUS. This is not used in NASM comaptible
453 * code, but does need to parse for the TASM macro
454 * package.
455 */
456 strcpy(line + 1, "ifdef BOGUS");
457 } else {
458 memcpy(line + 1, p, len + 1);
459 }
460 nasm_free(oldline);
461 return line;
462 } else if (m < 0) {
463 j = k;
464 } else
465 i = k;
466 }
467 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000468 }
469 return line;
470}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000471
H. Peter Anvin76690a12002-04-30 20:52:49 +0000472/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000473 * The pre-preprocessing stage... This function translates line
474 * number indications as they emerge from GNU cpp (`# lineno "file"
475 * flags') into NASM preprocessor line number indications (`%line
476 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000477 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000478static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000479{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000480 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000481 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000482
H. Peter Anvine2c80182005-01-15 22:15:51 +0000483 if (line[0] == '#' && line[1] == ' ') {
484 oldline = line;
485 fname = oldline + 2;
486 lineno = atoi(fname);
487 fname += strspn(fname, "0123456789 ");
488 if (*fname == '"')
489 fname++;
490 fnlen = strcspn(fname, "\"");
491 line = nasm_malloc(20 + fnlen);
492 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
493 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000494 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000495 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000496 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000497 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000498}
499
500/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000501 * Free a linked list of tokens.
502 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000504{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000505 while (list) {
506 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000507 }
508}
509
510/*
511 * Free a linked list of lines.
512 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000513static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000514{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000515 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000516 while (list) {
517 l = list;
518 list = list->next;
519 free_tlist(l->first);
520 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000521 }
522}
523
524/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000525 * Free an MMacro
526 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000527static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000528{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000529 nasm_free(m->name);
530 free_tlist(m->dlist);
531 nasm_free(m->defaults);
532 free_llist(m->expansion);
533 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000534}
535
536/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700537 * Free all currently defined macros, and free the hash tables
538 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700539static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700540{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700541 SMacro *s;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700542 const char *key;
543 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700544
H. Peter Anvin072771e2008-05-22 13:17:51 -0700545 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700546 nasm_free((void *)key);
547 while (s) {
548 SMacro *ns = s->next;
549 nasm_free(s->name);
550 free_tlist(s->expansion);
551 nasm_free(s);
552 s = ns;
553 }
554 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700555 hash_free(smt);
556}
557
558static void free_mmacro_table(struct hash_table *mmt)
559{
560 MMacro *m;
561 const char *key;
562 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700563
564 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700565 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700566 nasm_free((void *)key);
567 while (m) {
568 MMacro *nm = m->next;
569 free_mmacro(m);
570 m = nm;
571 }
572 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700573 hash_free(mmt);
574}
575
576static void free_macros(void)
577{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700578 free_smacro_table(&smacros);
579 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700580}
581
582/*
583 * Initialize the hash tables
584 */
585static void init_macros(void)
586{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700587 hash_init(&smacros, HASH_LARGE);
588 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700589}
590
591/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000592 * Pop the context stack.
593 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000594static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000595{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000596 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000597
598 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700599 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000600 nasm_free(c->name);
601 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000602}
603
H. Peter Anvin072771e2008-05-22 13:17:51 -0700604/*
605 * Search for a key in the hash index; adding it if necessary
606 * (in which case we initialize the data pointer to NULL.)
607 */
608static void **
609hash_findi_add(struct hash_table *hash, const char *str)
610{
611 struct hash_insert hi;
612 void **r;
613 char *strx;
614
615 r = hash_findi(hash, str, &hi);
616 if (r)
617 return r;
618
619 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
620 return hash_add(&hi, strx, NULL);
621}
622
623/*
624 * Like hash_findi, but returns the data element rather than a pointer
625 * to it. Used only when not adding a new element, hence no third
626 * argument.
627 */
628static void *
629hash_findix(struct hash_table *hash, const char *str)
630{
631 void **p;
632
633 p = hash_findi(hash, str, NULL);
634 return p ? *p : NULL;
635}
636
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000637#define BUF_DELTA 512
638/*
639 * Read a line from the top file in istk, handling multiple CR/LFs
640 * at the end of the line read, and handling spurious ^Zs. Will
641 * return lines from the standard macro set if this has not already
642 * been done.
643 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000644static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000645{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000646 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000647 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000648
H. Peter Anvine2c80182005-01-15 22:15:51 +0000649 if (stdmacpos) {
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700650 unsigned char c;
H. Peter Anvin7e50d232008-06-25 14:54:14 -0700651 const unsigned char *p = stdmacpos;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700652 char *ret, *q;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700653 size_t len = 0;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700654 while ((c = *p++)) {
655 if (c >= 0x80)
656 len += pp_directives_len[c-0x80]+1;
657 else
658 len++;
659 }
660 ret = nasm_malloc(len+1);
H. Peter Anvincda81632008-06-21 15:15:40 -0700661 q = ret;
662 while ((c = *stdmacpos++)) {
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700663 if (c >= 0x80) {
664 memcpy(q, pp_directives[c-0x80], pp_directives_len[c-0x80]);
665 q += pp_directives_len[c-0x80];
666 *q++ = ' ';
667 } else {
668 *q++ = c;
669 }
670 }
H. Peter Anvincda81632008-06-21 15:15:40 -0700671 stdmacpos = p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700672 *q = '\0';
673
H. Peter Anvind2456592008-06-19 15:04:18 -0700674 if (!*stdmacpos) {
675 /* This was the last of the standard macro chain... */
676 stdmacpos = NULL;
677 if (any_extrastdmac) {
678 stdmacpos = extrastdmac;
679 any_extrastdmac = false;
680 } else if (do_predef) {
681 Line *pd, *l;
682 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000683
H. Peter Anvind2456592008-06-19 15:04:18 -0700684 /*
685 * Nasty hack: here we push the contents of
686 * `predef' on to the top-level expansion stack,
687 * since this is the most convenient way to
688 * implement the pre-include and pre-define
689 * features.
690 */
691 for (pd = predef; pd; pd = pd->next) {
692 head = NULL;
693 tail = &head;
694 for (t = pd->first; t; t = t->next) {
695 *tail = new_Token(NULL, t->type, t->text, 0);
696 tail = &(*tail)->next;
697 }
698 l = nasm_malloc(sizeof(Line));
699 l->next = istk->expansion;
700 l->first = head;
H. Peter Anvin538002d2008-06-28 18:30:27 -0700701 l->finishes = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700702 istk->expansion = l;
703 }
704 do_predef = false;
705 }
706 }
707 return ret;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000708 }
709
710 bufsize = BUF_DELTA;
711 buffer = nasm_malloc(BUF_DELTA);
712 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000713 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000714 while (1) {
715 q = fgets(p, bufsize - (p - buffer), istk->fp);
716 if (!q)
717 break;
718 p += strlen(p);
719 if (p > buffer && p[-1] == '\n') {
720 /* Convert backslash-CRLF line continuation sequences into
721 nothing at all (for DOS and Windows) */
722 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
723 p -= 3;
724 *p = 0;
725 continued_count++;
726 }
727 /* Also convert backslash-LF line continuation sequences into
728 nothing at all (for Unix) */
729 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
730 p -= 2;
731 *p = 0;
732 continued_count++;
733 } else {
734 break;
735 }
736 }
737 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000738 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000739 bufsize += BUF_DELTA;
740 buffer = nasm_realloc(buffer, bufsize);
741 p = buffer + offset; /* prevent stale-pointer problems */
742 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000743 }
744
H. Peter Anvine2c80182005-01-15 22:15:51 +0000745 if (!q && p == buffer) {
746 nasm_free(buffer);
747 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000748 }
749
H. Peter Anvine2c80182005-01-15 22:15:51 +0000750 src_set_linnum(src_get_linnum() + istk->lineinc +
751 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000752
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000753 /*
754 * Play safe: remove CRs as well as LFs, if any of either are
755 * present at the end of the line.
756 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000757 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000758 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000759
760 /*
761 * Handle spurious ^Z, which may be inserted into source files
762 * by some file transfer utilities.
763 */
764 buffer[strcspn(buffer, "\032")] = '\0';
765
H. Peter Anvin734b1882002-04-30 21:01:08 +0000766 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000767
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000768 return buffer;
769}
770
771/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000772 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000773 * don't need to parse the value out of e.g. numeric tokens: we
774 * simply split one string into many.
775 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000776static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000777{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000778 char *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000779 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000780 Token *list = NULL;
781 Token *t, **tail = &list;
782
H. Peter Anvine2c80182005-01-15 22:15:51 +0000783 while (*line) {
784 p = line;
785 if (*p == '%') {
786 p++;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700787 if (nasm_isdigit(*p) ||
788 ((*p == '-' || *p == '+') && nasm_isdigit(p[1])) ||
789 ((*p == '+') && (nasm_isspace(p[1]) || !p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000790 do {
791 p++;
792 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700793 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000794 type = TOK_PREPROC_ID;
795 } else if (*p == '{') {
796 p++;
797 while (*p && *p != '}') {
798 p[-1] = *p;
799 p++;
800 }
801 p[-1] = '\0';
802 if (*p)
803 p++;
804 type = TOK_PREPROC_ID;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700805 } else if (*p == '?') {
806 type = TOK_PREPROC_Q; /* %? */
807 p++;
808 if (*p == '?') {
809 type = TOK_PREPROC_QQ; /* %?? */
810 p++;
811 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000812 } else if (isidchar(*p) ||
813 ((*p == '!' || *p == '%' || *p == '$') &&
814 isidchar(p[1]))) {
815 do {
816 p++;
817 }
818 while (isidchar(*p));
819 type = TOK_PREPROC_ID;
820 } else {
821 type = TOK_OTHER;
822 if (*p == '%')
823 p++;
824 }
825 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
826 type = TOK_ID;
827 p++;
828 while (*p && isidchar(*p))
829 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700830 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 /*
832 * A string token.
833 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000834 type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700835 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000836
H. Peter Anvine2c80182005-01-15 22:15:51 +0000837 if (*p) {
838 p++;
839 } else {
840 error(ERR_WARNING, "unterminated string");
841 /* Handling unterminated strings by UNV */
842 /* type = -1; */
843 }
844 } else if (isnumstart(*p)) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700845 bool is_hex = false;
846 bool is_float = false;
847 bool has_e = false;
848 char c, *r;
849
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700851 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000852 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700853
854 if (*p == '$') {
855 p++;
856 is_hex = true;
857 }
858
859 for (;;) {
860 c = *p++;
861
862 if (!is_hex && (c == 'e' || c == 'E')) {
863 has_e = true;
864 if (*p == '+' || *p == '-') {
865 /* e can only be followed by +/- if it is either a
866 prefixed hex number or a floating-point number */
867 p++;
868 is_float = true;
869 }
870 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
871 is_hex = true;
872 } else if (c == 'P' || c == 'p') {
873 is_float = true;
874 if (*p == '+' || *p == '-')
875 p++;
876 } else if (isnumchar(c) || c == '_')
877 ; /* just advance */
878 else if (c == '.') {
879 /* we need to deal with consequences of the legacy
880 parser, like "1.nolist" being two tokens
881 (TOK_NUMBER, TOK_ID) here; at least give it
882 a shot for now. In the future, we probably need
883 a flex-based scanner with proper pattern matching
884 to do it as well as it can be done. Nothing in
885 the world is going to help the person who wants
886 0x123.p16 interpreted as two tokens, though. */
887 r = p;
888 while (*r == '_')
889 r++;
890
H. Peter Anvinf221b9e2008-06-21 11:03:51 -0700891 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700892 (!is_hex && (*r == 'e' || *r == 'E')) ||
893 (*r == 'p' || *r == 'P')) {
894 p = r;
895 is_float = true;
896 } else
897 break; /* Terminate the token */
898 } else
899 break;
900 }
901 p--; /* Point to first character beyond number */
902
903 if (has_e && !is_hex) {
904 /* 1e13 is floating-point, but 1e13h is not */
905 is_float = true;
906 }
907
908 type = is_float ? TOK_FLOAT : TOK_NUMBER;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700909 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000910 type = TOK_WHITESPACE;
911 p++;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700912 while (*p && nasm_isspace(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000913 p++;
914 /*
915 * Whitespace just before end-of-line is discarded by
916 * pretending it's a comment; whitespace just before a
917 * comment gets lumped into the comment.
918 */
919 if (!*p || *p == ';') {
920 type = TOK_COMMENT;
921 while (*p)
922 p++;
923 }
924 } else if (*p == ';') {
925 type = TOK_COMMENT;
926 while (*p)
927 p++;
928 } else {
929 /*
930 * Anything else is an operator of some kind. We check
931 * for all the double-character operators (>>, <<, //,
932 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000933 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 */
935 type = TOK_OTHER;
936 if ((p[0] == '>' && p[1] == '>') ||
937 (p[0] == '<' && p[1] == '<') ||
938 (p[0] == '/' && p[1] == '/') ||
939 (p[0] == '<' && p[1] == '=') ||
940 (p[0] == '>' && p[1] == '=') ||
941 (p[0] == '=' && p[1] == '=') ||
942 (p[0] == '!' && p[1] == '=') ||
943 (p[0] == '<' && p[1] == '>') ||
944 (p[0] == '&' && p[1] == '&') ||
945 (p[0] == '|' && p[1] == '|') ||
946 (p[0] == '^' && p[1] == '^')) {
947 p++;
948 }
949 p++;
950 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000951
H. Peter Anvine2c80182005-01-15 22:15:51 +0000952 /* Handling unterminated string by UNV */
953 /*if (type == -1)
954 {
955 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
956 t->text[p-line] = *line;
957 tail = &t->next;
958 }
959 else */
960 if (type != TOK_COMMENT) {
961 *tail = t = new_Token(NULL, type, line, p - line);
962 tail = &t->next;
963 }
964 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000965 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000966 return list;
967}
968
H. Peter Anvince616072002-04-30 21:02:23 +0000969/*
970 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -0700971 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +0000972 * deleted only all at once by the delete_Blocks function.
973 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +0000975{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000976 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000977
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000978 /* first, get to the end of the linked list */
979 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000980 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000981 /* now allocate the requested chunk */
982 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000983
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000984 /* now allocate a new block for the next request */
985 b->next = nasm_malloc(sizeof(Blocks));
986 /* and initialize the contents of the new block */
987 b->next->next = NULL;
988 b->next->chunk = NULL;
989 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000990}
991
992/*
993 * this function deletes all managed blocks of memory
994 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +0000996{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +0000998
H. Peter Anvin70653092007-10-19 14:42:29 -0700999 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001000 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001001 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001002 * free it.
1003 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 while (b) {
1005 if (b->chunk)
1006 nasm_free(b->chunk);
1007 a = b;
1008 b = b->next;
1009 if (a != &blocks)
1010 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001011 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001013
1014/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001015 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001016 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001017 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001018 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001019static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvinc751e862008-06-09 10:18:45 -07001020 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001021{
1022 Token *t;
1023 int i;
1024
H. Peter Anvine2c80182005-01-15 22:15:51 +00001025 if (freeTokens == NULL) {
1026 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1027 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1028 freeTokens[i].next = &freeTokens[i + 1];
1029 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001030 }
1031 t = freeTokens;
1032 freeTokens = t->next;
1033 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001034 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001035 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001036 if (type == TOK_WHITESPACE || text == NULL) {
1037 t->text = NULL;
1038 } else {
1039 if (txtlen == 0)
1040 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001041 t->text = nasm_malloc(txtlen+1);
1042 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001043 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001044 }
1045 return t;
1046}
1047
H. Peter Anvine2c80182005-01-15 22:15:51 +00001048static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001049{
1050 Token *next = t->next;
1051 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001052 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001053 freeTokens = t;
1054 return next;
1055}
1056
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001057/*
1058 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001059 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1060 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001061 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001062static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001063{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001064 Token *t;
1065 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001066 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001067 const char *q;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001068
1069 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001070 for (t = tlist; t; t = t->next) {
1071 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001072 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001073 nasm_free(t->text);
1074 if (p)
1075 t->text = nasm_strdup(p);
1076 else
1077 t->text = NULL;
1078 }
1079 /* Expand local macros here and not during preprocessing */
1080 if (expand_locals &&
1081 t->type == TOK_PREPROC_ID && t->text &&
1082 t->text[0] == '%' && t->text[1] == '$') {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001083 Context *ctx = get_ctx(t->text, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001084 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001085 char buffer[40];
1086 char *p, *q = t->text + 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001087
H. Peter Anvine2c80182005-01-15 22:15:51 +00001088 q += strspn(q, "$");
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001089 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 p = nasm_strcat(buffer, q);
1091 nasm_free(t->text);
1092 t->text = p;
1093 }
1094 }
1095 if (t->type == TOK_WHITESPACE) {
1096 len++;
1097 } else if (t->text) {
1098 len += strlen(t->text);
1099 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001100 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001101 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001102 for (t = tlist; t; t = t->next) {
1103 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001104 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001105 } else if (t->text) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001106 q = t->text;
1107 while (*q)
1108 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001109 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001110 }
1111 *p = '\0';
1112 return line;
1113}
1114
1115/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001116 * A scanner, suitable for use by the expression evaluator, which
1117 * operates on a line of Tokens. Expects a pointer to a pointer to
1118 * the first token in the line to be passed in as its private_data
1119 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001120 *
1121 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001122 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001123static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001124{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001125 Token **tlineptr = private_data;
1126 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001127 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001128
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 do {
1130 tline = *tlineptr;
1131 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001132 }
1133 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001135
1136 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001138
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001139 tokval->t_charptr = tline->text;
1140
H. Peter Anvin76690a12002-04-30 20:52:49 +00001141 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001143 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001145
H. Peter Anvine2c80182005-01-15 22:15:51 +00001146 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001147 p = tokval->t_charptr = tline->text;
1148 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 tokval->t_charptr++;
1150 return tokval->t_type = TOKEN_ID;
1151 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001152
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001153 for (r = p, s = ourcopy; *r; r++) {
Philipp Thomas76ec8e72008-05-21 08:53:21 -07001154 if (r >= p+MAX_KEYWORD)
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001155 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001156 *s++ = nasm_tolower(*r);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001157 }
1158 *s = '\0';
1159 /* right, so we have an identifier sitting in temp storage. now,
1160 * is it actually a register or instruction name, or what? */
1161 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001162 }
1163
H. Peter Anvine2c80182005-01-15 22:15:51 +00001164 if (tline->type == TOK_NUMBER) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001165 bool rn_error;
1166 tokval->t_integer = readnum(tline->text, &rn_error);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001167 tokval->t_charptr = tline->text;
H. Peter Anvin11627042008-06-09 20:45:19 -07001168 if (rn_error)
1169 return tokval->t_type = TOKEN_ERRNUM;
1170 else
1171 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001172 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001173
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001174 if (tline->type == TOK_FLOAT) {
1175 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001176 }
1177
H. Peter Anvine2c80182005-01-15 22:15:51 +00001178 if (tline->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001179 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001180
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001181 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001182 tokval->t_charptr = tline->text;
1183 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001184
H. Peter Anvin11627042008-06-09 20:45:19 -07001185 if (ep[0] != bq || ep[1] != '\0')
1186 return tokval->t_type = TOKEN_ERRSTR;
1187 else
1188 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001189 }
1190
H. Peter Anvine2c80182005-01-15 22:15:51 +00001191 if (tline->type == TOK_OTHER) {
1192 if (!strcmp(tline->text, "<<"))
1193 return tokval->t_type = TOKEN_SHL;
1194 if (!strcmp(tline->text, ">>"))
1195 return tokval->t_type = TOKEN_SHR;
1196 if (!strcmp(tline->text, "//"))
1197 return tokval->t_type = TOKEN_SDIV;
1198 if (!strcmp(tline->text, "%%"))
1199 return tokval->t_type = TOKEN_SMOD;
1200 if (!strcmp(tline->text, "=="))
1201 return tokval->t_type = TOKEN_EQ;
1202 if (!strcmp(tline->text, "<>"))
1203 return tokval->t_type = TOKEN_NE;
1204 if (!strcmp(tline->text, "!="))
1205 return tokval->t_type = TOKEN_NE;
1206 if (!strcmp(tline->text, "<="))
1207 return tokval->t_type = TOKEN_LE;
1208 if (!strcmp(tline->text, ">="))
1209 return tokval->t_type = TOKEN_GE;
1210 if (!strcmp(tline->text, "&&"))
1211 return tokval->t_type = TOKEN_DBL_AND;
1212 if (!strcmp(tline->text, "^^"))
1213 return tokval->t_type = TOKEN_DBL_XOR;
1214 if (!strcmp(tline->text, "||"))
1215 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001216 }
1217
1218 /*
1219 * We have no other options: just return the first character of
1220 * the token text.
1221 */
1222 return tokval->t_type = tline->text[0];
1223}
1224
1225/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001226 * Compare a string to the name of an existing macro; this is a
1227 * simple wrapper which calls either strcmp or nasm_stricmp
1228 * depending on the value of the `casesense' parameter.
1229 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001230static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001231{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001232 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001233}
1234
1235/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001236 * Compare a string to the name of an existing macro; this is a
1237 * simple wrapper which calls either strcmp or nasm_stricmp
1238 * depending on the value of the `casesense' parameter.
1239 */
1240static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1241{
1242 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1243}
1244
1245/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001246 * Return the Context structure associated with a %$ token. Return
1247 * NULL, having _already_ reported an error condition, if the
1248 * context stack isn't deep enough for the supplied number of $
1249 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001250 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001251 * also scanned for such smacro, until it is found; if not -
1252 * only the context that directly results from the number of $'s
1253 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001254 */
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001255static Context *get_ctx(const char *name, bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001256{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001257 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001258 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001259 int i;
1260
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001261 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001262 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001263
H. Peter Anvine2c80182005-01-15 22:15:51 +00001264 if (!cstk) {
1265 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1266 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001267 }
1268
H. Peter Anvine2c80182005-01-15 22:15:51 +00001269 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1270 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001271/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001272 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001273 if (!ctx) {
1274 error(ERR_NONFATAL, "`%s': context stack is only"
1275 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1276 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001277 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001278 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001279 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001280
H. Peter Anvine2c80182005-01-15 22:15:51 +00001281 do {
1282 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001283 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001284 while (m) {
1285 if (!mstrcmp(m->name, name, m->casesense))
1286 return ctx;
1287 m = m->next;
1288 }
1289 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001290 }
1291 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001292 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001293}
1294
1295/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001296 * Check to see if a file is already in a string list
1297 */
1298static bool in_list(const StrList *list, const char *str)
1299{
1300 while (list) {
1301 if (!strcmp(list->str, str))
1302 return true;
1303 list = list->next;
1304 }
1305 return false;
1306}
1307
1308/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001309 * Open an include file. This routine must always return a valid
1310 * file pointer if it returns - it's responsible for throwing an
1311 * ERR_FATAL and bombing out completely if not. It should also try
1312 * the include path one by one until it finds the file or reaches
1313 * the end of the path.
1314 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001315static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
H. Peter Anvin418ca702008-05-30 10:42:30 -07001316 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001317{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001318 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001319 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001320 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001321 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001322 size_t prefix_len = 0;
1323 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001324
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001326 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1327 memcpy(sl->str, prefix, prefix_len);
1328 memcpy(sl->str+prefix_len, file, len+1);
1329 fp = fopen(sl->str, "r");
H. Peter Anvin418ca702008-05-30 10:42:30 -07001330 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001331 sl->next = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001332 **dtail = sl;
1333 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001334 } else {
1335 nasm_free(sl);
1336 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001337 if (fp)
1338 return fp;
H. Peter Anvin418ca702008-05-30 10:42:30 -07001339 if (!ip) {
1340 if (!missing_ok)
1341 break;
1342 prefix = NULL;
1343 } else {
1344 prefix = ip->path;
1345 ip = ip->next;
1346 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001347 if (prefix) {
1348 prefix_len = strlen(prefix);
1349 } else {
1350 /* -MG given and file not found */
H. Peter Anvin418ca702008-05-30 10:42:30 -07001351 if (dhead && !in_list(*dhead, file)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001352 sl = nasm_malloc(len+1+sizeof sl->next);
1353 sl->next = NULL;
1354 strcpy(sl->str, file);
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001355 **dtail = sl;
1356 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001357 }
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001358 return NULL;
1359 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001360 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001361
H. Peter Anvin734b1882002-04-30 21:01:08 +00001362 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001363 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001364}
1365
1366/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001367 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001368 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001369 * return true if _any_ single-line macro of that name is defined.
1370 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001371 * `nparam' or no parameters is defined.
1372 *
1373 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001374 * defined, or nparam is -1, the address of the definition structure
1375 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001376 * is NULL, no action will be taken regarding its contents, and no
1377 * error will occur.
1378 *
1379 * Note that this is also called with nparam zero to resolve
1380 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001381 *
1382 * If you already know which context macro belongs to, you can pass
1383 * the context pointer as first parameter; if you won't but name begins
1384 * with %$ the context will be automatically computed. If all_contexts
1385 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001386 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001387static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001388smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001389 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001390{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001391 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001392 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001393
H. Peter Anvin97a23472007-09-16 17:57:25 -07001394 if (ctx) {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001395 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001396 } else if (name[0] == '%' && name[1] == '$') {
1397 if (cstk)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001398 ctx = get_ctx(name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001399 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001400 return false; /* got to return _something_ */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001401 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001402 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001403 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001404 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001405 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001406
H. Peter Anvine2c80182005-01-15 22:15:51 +00001407 while (m) {
1408 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001409 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001410 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001411 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001412 *defn = m;
1413 else
1414 *defn = NULL;
1415 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001416 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001417 }
1418 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001419 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001420
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001421 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001422}
1423
1424/*
1425 * Count and mark off the parameters in a multi-line macro call.
1426 * This is called both from within the multi-line macro expansion
1427 * code, and also to mark off the default parameters when provided
1428 * in a %macro definition line.
1429 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001431{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001432 int paramsize, brace;
1433
1434 *nparam = paramsize = 0;
1435 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001436 while (t) {
1437 if (*nparam >= paramsize) {
1438 paramsize += PARAM_DELTA;
1439 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1440 }
1441 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001442 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001443 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001444 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001445 (*params)[(*nparam)++] = t;
1446 while (tok_isnt_(t, brace ? "}" : ","))
1447 t = t->next;
1448 if (t) { /* got a comma/brace */
1449 t = t->next;
1450 if (brace) {
1451 /*
1452 * Now we've found the closing brace, look further
1453 * for the comma.
1454 */
1455 skip_white_(t);
1456 if (tok_isnt_(t, ",")) {
1457 error(ERR_NONFATAL,
1458 "braces do not enclose all of macro parameter");
1459 while (tok_isnt_(t, ","))
1460 t = t->next;
1461 }
1462 if (t)
1463 t = t->next; /* eat the comma */
1464 }
1465 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001466 }
1467}
1468
1469/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001470 * Determine whether one of the various `if' conditions is true or
1471 * not.
1472 *
1473 * We must free the tline we get passed.
1474 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001475static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001476{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001477 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001478 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001479 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001480 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001481 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001482 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001483
1484 origline = tline;
1485
H. Peter Anvine2c80182005-01-15 22:15:51 +00001486 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001487 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001488 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001489 while (cstk && tline) {
1490 skip_white_(tline);
1491 if (!tline || tline->type != TOK_ID) {
1492 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001493 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001494 free_tlist(origline);
1495 return -1;
1496 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07001497 if (cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001498 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001499 tline = tline->next;
1500 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001501 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001502
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001503 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001504 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001505 while (tline) {
1506 skip_white_(tline);
1507 if (!tline || (tline->type != TOK_ID &&
1508 (tline->type != TOK_PREPROC_ID ||
1509 tline->text[1] != '$'))) {
1510 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001511 "`%s' expects macro identifiers", pp_directives[ct]);
1512 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001513 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001514 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001515 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001516 tline = tline->next;
1517 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001518 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001519
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001520 case PPC_IFIDN:
1521 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001522 tline = expand_smacro(tline);
1523 t = tt = tline;
1524 while (tok_isnt_(tt, ","))
1525 tt = tt->next;
1526 if (!tt) {
1527 error(ERR_NONFATAL,
1528 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001529 pp_directives[ct]);
1530 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001531 }
1532 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001533 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001534 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1535 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1536 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001537 pp_directives[ct]);
1538 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001539 }
1540 if (t->type == TOK_WHITESPACE) {
1541 t = t->next;
1542 continue;
1543 }
1544 if (tt->type == TOK_WHITESPACE) {
1545 tt = tt->next;
1546 continue;
1547 }
1548 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001549 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001550 break;
1551 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001552 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001553 if (t->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001554 size_t l1 = nasm_unquote(t->text, NULL);
1555 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001556
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001557 if (l1 != l2) {
1558 j = false;
1559 break;
1560 }
1561 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1562 j = false;
1563 break;
1564 }
1565 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001566 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001567 break;
1568 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001569
H. Peter Anvine2c80182005-01-15 22:15:51 +00001570 t = t->next;
1571 tt = tt->next;
1572 }
1573 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001574 j = false; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001575 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001576
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001577 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001578 {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001579 bool found = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001580 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001581
H. Peter Anvine2c80182005-01-15 22:15:51 +00001582 tline = tline->next;
1583 skip_white_(tline);
1584 tline = expand_id(tline);
1585 if (!tok_type_(tline, TOK_ID)) {
1586 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001587 "`%s' expects a macro name", pp_directives[ct]);
1588 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001589 }
1590 searching.name = nasm_strdup(tline->text);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001591 searching.casesense = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001592 searching.plus = false;
1593 searching.nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001594 searching.in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001595 searching.rep_nest = NULL;
1596 searching.nparam_min = 0;
1597 searching.nparam_max = INT_MAX;
1598 tline = expand_smacro(tline->next);
1599 skip_white_(tline);
1600 if (!tline) {
1601 } else if (!tok_type_(tline, TOK_NUMBER)) {
1602 error(ERR_NONFATAL,
1603 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001604 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001605 } else {
1606 searching.nparam_min = searching.nparam_max =
1607 readnum(tline->text, &j);
1608 if (j)
1609 error(ERR_NONFATAL,
1610 "unable to parse parameter count `%s'",
1611 tline->text);
1612 }
1613 if (tline && tok_is_(tline->next, "-")) {
1614 tline = tline->next->next;
1615 if (tok_is_(tline, "*"))
1616 searching.nparam_max = INT_MAX;
1617 else if (!tok_type_(tline, TOK_NUMBER))
1618 error(ERR_NONFATAL,
1619 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001620 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001621 else {
1622 searching.nparam_max = readnum(tline->text, &j);
1623 if (j)
1624 error(ERR_NONFATAL,
1625 "unable to parse parameter count `%s'",
1626 tline->text);
1627 if (searching.nparam_min > searching.nparam_max)
1628 error(ERR_NONFATAL,
1629 "minimum parameter count exceeds maximum");
1630 }
1631 }
1632 if (tline && tok_is_(tline->next, "+")) {
1633 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001634 searching.plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001636 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001637 while (mmac) {
1638 if (!strcmp(mmac->name, searching.name) &&
1639 (mmac->nparam_min <= searching.nparam_max
1640 || searching.plus)
1641 && (searching.nparam_min <= mmac->nparam_max
1642 || mmac->plus)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001643 found = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001644 break;
1645 }
1646 mmac = mmac->next;
1647 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001648 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001649 j = found;
1650 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001651 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001652
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001653 case PPC_IFID:
1654 needtype = TOK_ID;
1655 goto iftype;
1656 case PPC_IFNUM:
1657 needtype = TOK_NUMBER;
1658 goto iftype;
1659 case PPC_IFSTR:
1660 needtype = TOK_STRING;
1661 goto iftype;
1662
1663 iftype:
H. Peter Anvin134b9462008-02-16 17:01:40 -08001664 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001665
H. Peter Anvin927c92b2008-02-16 13:44:52 -08001666 while (tok_type_(t, TOK_WHITESPACE) ||
1667 (needtype == TOK_NUMBER &&
1668 tok_type_(t, TOK_OTHER) &&
1669 (t->text[0] == '-' || t->text[0] == '+') &&
1670 !t->text[1]))
1671 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001672
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001673 j = tok_type_(t, needtype);
1674 break;
1675
1676 case PPC_IFTOKEN:
1677 t = tline = expand_smacro(tline);
1678 while (tok_type_(t, TOK_WHITESPACE))
1679 t = t->next;
1680
1681 j = false;
1682 if (t) {
1683 t = t->next; /* Skip the actual token */
1684 while (tok_type_(t, TOK_WHITESPACE))
1685 t = t->next;
1686 j = !t; /* Should be nothing left */
1687 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001688 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001689
H. Peter Anvin134b9462008-02-16 17:01:40 -08001690 case PPC_IFEMPTY:
1691 t = tline = expand_smacro(tline);
1692 while (tok_type_(t, TOK_WHITESPACE))
1693 t = t->next;
1694
1695 j = !t; /* Should be empty */
1696 break;
1697
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001698 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001699 t = tline = expand_smacro(tline);
1700 tptr = &t;
1701 tokval.t_type = TOKEN_INVALID;
1702 evalresult = evaluate(ppscan, tptr, &tokval,
1703 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001704 if (!evalresult)
1705 return -1;
1706 if (tokval.t_type)
1707 error(ERR_WARNING,
1708 "trailing garbage after expression ignored");
1709 if (!is_simple(evalresult)) {
1710 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001711 "non-constant value given to `%s'", pp_directives[ct]);
1712 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001713 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001714 j = reloc_value(evalresult) != 0;
H. Peter Anvin90e6e812008-07-16 14:38:24 -07001715 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001716
H. Peter Anvine2c80182005-01-15 22:15:51 +00001717 default:
1718 error(ERR_FATAL,
1719 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001720 pp_directives[ct]);
1721 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001722 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001723
1724 free_tlist(origline);
1725 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001726
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001727fail:
1728 free_tlist(origline);
1729 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001730}
1731
1732/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001733 * Common code for defining an smacro
1734 */
1735static bool define_smacro(Context *ctx, char *mname, bool casesense,
1736 int nparam, Token *expansion)
1737{
1738 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001739 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001740
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001741 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1742 if (!smac) {
1743 error(ERR_WARNING,
1744 "single-line macro `%s' defined both with and"
1745 " without parameters", mname);
1746
1747 /* Some instances of the old code considered this a failure,
1748 some others didn't. What is the right thing to do here? */
1749 free_tlist(expansion);
1750 return false; /* Failure */
1751 } else {
1752 /*
1753 * We're redefining, so we have to take over an
1754 * existing SMacro structure. This means freeing
1755 * what was already in it.
1756 */
1757 nasm_free(smac->name);
1758 free_tlist(smac->expansion);
1759 }
1760 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001761 smtbl = ctx ? &ctx->localmac : &smacros;
1762 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001763 smac = nasm_malloc(sizeof(SMacro));
1764 smac->next = *smhead;
1765 *smhead = smac;
1766 }
1767 smac->name = nasm_strdup(mname);
1768 smac->casesense = casesense;
1769 smac->nparam = nparam;
1770 smac->expansion = expansion;
1771 smac->in_progress = false;
1772 return true; /* Success */
1773}
1774
1775/*
1776 * Undefine an smacro
1777 */
1778static void undef_smacro(Context *ctx, const char *mname)
1779{
1780 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001781 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001782
H. Peter Anvin166c2472008-05-28 12:28:58 -07001783 smtbl = ctx ? &ctx->localmac : &smacros;
1784 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001785
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001786 if (smhead) {
1787 /*
1788 * We now have a macro name... go hunt for it.
1789 */
1790 sp = smhead;
1791 while ((s = *sp) != NULL) {
1792 if (!mstrcmp(s->name, mname, s->casesense)) {
1793 *sp = s->next;
1794 nasm_free(s->name);
1795 free_tlist(s->expansion);
1796 nasm_free(s);
1797 } else {
1798 sp = &s->next;
1799 }
1800 }
1801 }
1802}
1803
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001804/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07001805 * Parse a mmacro specification.
1806 */
1807static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1808{
1809 bool err;
1810
1811 tline = tline->next;
1812 skip_white_(tline);
1813 tline = expand_id(tline);
1814 if (!tok_type_(tline, TOK_ID)) {
1815 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1816 return false;
1817 }
1818 def->name = nasm_strdup(tline->text);
1819 def->plus = false;
1820 def->nolist = false;
1821 def->in_progress = 0;
1822 def->rep_nest = NULL;
1823 tline = expand_smacro(tline->next);
1824 skip_white_(tline);
1825 if (!tok_type_(tline, TOK_NUMBER)) {
1826 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
1827 def->nparam_min = def->nparam_max = 0;
1828 } else {
1829 def->nparam_min = def->nparam_max =
1830 readnum(tline->text, &err);
1831 if (err)
1832 error(ERR_NONFATAL,
1833 "unable to parse parameter count `%s'", tline->text);
1834 }
1835 if (tline && tok_is_(tline->next, "-")) {
1836 tline = tline->next->next;
1837 if (tok_is_(tline, "*")) {
1838 def->nparam_max = INT_MAX;
1839 } else if (!tok_type_(tline, TOK_NUMBER)) {
1840 error(ERR_NONFATAL,
1841 "`%s' expects a parameter count after `-'", directive);
1842 } else {
1843 def->nparam_max = readnum(tline->text, &err);
1844 if (err) {
1845 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
1846 tline->text);
1847 }
1848 if (def->nparam_min > def->nparam_max) {
1849 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
1850 }
1851 }
1852 }
1853 if (tline && tok_is_(tline->next, "+")) {
1854 tline = tline->next;
1855 def->plus = true;
1856 }
1857 if (tline && tok_type_(tline->next, TOK_ID) &&
1858 !nasm_stricmp(tline->next->text, ".nolist")) {
1859 tline = tline->next;
1860 def->nolist = true;
1861 }
1862
1863 /*
1864 * Handle default parameters.
1865 */
1866 if (tline && tline->next) {
1867 def->dlist = tline->next;
1868 tline->next = NULL;
1869 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
1870 } else {
1871 def->dlist = NULL;
1872 def->defaults = NULL;
1873 }
1874 def->expansion = NULL;
1875
1876 return true;
1877}
1878
1879
1880/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001881 * Decode a size directive
1882 */
1883static int parse_size(const char *str) {
1884 static const char *size_names[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001885 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001886 static const int sizes[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001887 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001888
1889 return sizes[bsii(str, size_names, elements(size_names))+1];
1890}
1891
Ed Beroset3ab3f412002-06-11 03:31:49 +00001892/**
1893 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001894 * Find out if a line contains a preprocessor directive, and deal
1895 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07001896 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001897 * If a directive _is_ found, it is the responsibility of this routine
1898 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001899 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001900 * @param tline a pointer to the current tokeninzed line linked list
1901 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07001902 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001903 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001904static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001905{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001906 enum preproc_token i;
1907 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07001908 bool err;
1909 int nparam;
1910 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001911 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001912 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001913 int offset;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001914 char *p, *pp, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001915 Include *inc;
1916 Context *ctx;
1917 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001918 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001919 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1920 Line *l;
1921 struct tokenval tokval;
1922 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001923 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001924 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001925 size_t len;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001926
1927 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001928
H. Peter Anvineba20a72002-04-30 20:53:55 +00001929 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07001930 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001931 (tline->text[1] == '%' || tline->text[1] == '$'
1932 || tline->text[1] == '!'))
1933 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001934
H. Peter Anvin4169a472007-09-12 01:29:43 +00001935 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001936
1937 /*
1938 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001939 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001940 * we should ignore all directives except for condition
1941 * directives.
1942 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001943 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001944 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1945 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001946 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001947
1948 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001949 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02001950 * ignore all directives except for %macro/%imacro (which nest),
1951 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
1952 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001953 */
1954 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001955 i != PP_ENDMACRO && i != PP_ENDM &&
1956 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1957 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001958 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001959
Charles Crayned4200be2008-07-12 16:42:33 -07001960 if (defining) {
1961 if (i == PP_MACRO || i == PP_IMACRO) {
1962 nested_mac_count++;
1963 return NO_DIRECTIVE_FOUND;
1964 } else if (nested_mac_count > 0) {
1965 if (i == PP_ENDMACRO) {
1966 nested_mac_count--;
1967 return NO_DIRECTIVE_FOUND;
1968 }
1969 }
1970 if (!defining->name) {
1971 if (i == PP_REP) {
1972 nested_rep_count++;
1973 return NO_DIRECTIVE_FOUND;
1974 } else if (nested_rep_count > 0) {
1975 if (i == PP_ENDREP) {
1976 nested_rep_count--;
1977 return NO_DIRECTIVE_FOUND;
1978 }
1979 }
1980 }
1981 }
1982
H. Peter Anvin4169a472007-09-12 01:29:43 +00001983 switch (i) {
1984 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001985 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1986 tline->text);
1987 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001988
H. Peter Anvine2c80182005-01-15 22:15:51 +00001989 case PP_STACKSIZE:
1990 /* Directive to tell NASM what the default stack size is. The
1991 * default is for a 16-bit stack, and this can be overriden with
1992 * %stacksize large.
1993 * the following form:
1994 *
1995 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1996 */
1997 tline = tline->next;
1998 if (tline && tline->type == TOK_WHITESPACE)
1999 tline = tline->next;
2000 if (!tline || tline->type != TOK_ID) {
2001 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2002 free_tlist(origline);
2003 return DIRECTIVE_FOUND;
2004 }
2005 if (nasm_stricmp(tline->text, "flat") == 0) {
2006 /* All subsequent ARG directives are for a 32-bit stack */
2007 StackSize = 4;
2008 StackPointer = "ebp";
2009 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002010 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002011 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2012 /* All subsequent ARG directives are for a 64-bit stack */
2013 StackSize = 8;
2014 StackPointer = "rbp";
2015 ArgOffset = 8;
2016 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002017 } else if (nasm_stricmp(tline->text, "large") == 0) {
2018 /* All subsequent ARG directives are for a 16-bit stack,
2019 * far function call.
2020 */
2021 StackSize = 2;
2022 StackPointer = "bp";
2023 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002024 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002025 } else if (nasm_stricmp(tline->text, "small") == 0) {
2026 /* All subsequent ARG directives are for a 16-bit stack,
2027 * far function call. We don't support near functions.
2028 */
2029 StackSize = 2;
2030 StackPointer = "bp";
2031 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002032 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002033 } else {
2034 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2035 free_tlist(origline);
2036 return DIRECTIVE_FOUND;
2037 }
2038 free_tlist(origline);
2039 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002040
H. Peter Anvine2c80182005-01-15 22:15:51 +00002041 case PP_ARG:
2042 /* TASM like ARG directive to define arguments to functions, in
2043 * the following form:
2044 *
2045 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2046 */
2047 offset = ArgOffset;
2048 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002049 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002050 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002051
H. Peter Anvine2c80182005-01-15 22:15:51 +00002052 /* Find the argument name */
2053 tline = tline->next;
2054 if (tline && tline->type == TOK_WHITESPACE)
2055 tline = tline->next;
2056 if (!tline || tline->type != TOK_ID) {
2057 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2058 free_tlist(origline);
2059 return DIRECTIVE_FOUND;
2060 }
2061 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002062
H. Peter Anvine2c80182005-01-15 22:15:51 +00002063 /* Find the argument size type */
2064 tline = tline->next;
2065 if (!tline || tline->type != TOK_OTHER
2066 || tline->text[0] != ':') {
2067 error(ERR_NONFATAL,
2068 "Syntax error processing `%%arg' directive");
2069 free_tlist(origline);
2070 return DIRECTIVE_FOUND;
2071 }
2072 tline = tline->next;
2073 if (!tline || tline->type != TOK_ID) {
2074 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2075 free_tlist(origline);
2076 return DIRECTIVE_FOUND;
2077 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002078
H. Peter Anvine2c80182005-01-15 22:15:51 +00002079 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002080 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002081 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002082 size = parse_size(tt->text);
2083 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002084 error(ERR_NONFATAL,
2085 "Invalid size type for `%%arg' missing directive");
2086 free_tlist(tt);
2087 free_tlist(origline);
2088 return DIRECTIVE_FOUND;
2089 }
2090 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002091
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002092 /* Round up to even stack slots */
2093 size = (size+StackSize-1) & ~(StackSize-1);
2094
H. Peter Anvine2c80182005-01-15 22:15:51 +00002095 /* Now define the macro for the argument */
2096 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2097 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002098 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002099 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002100
H. Peter Anvine2c80182005-01-15 22:15:51 +00002101 /* Move to the next argument in the list */
2102 tline = tline->next;
2103 if (tline && tline->type == TOK_WHITESPACE)
2104 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002105 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2106 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002107 free_tlist(origline);
2108 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002109
H. Peter Anvine2c80182005-01-15 22:15:51 +00002110 case PP_LOCAL:
2111 /* TASM like LOCAL directive to define local variables for a
2112 * function, in the following form:
2113 *
2114 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2115 *
2116 * The '= LocalSize' at the end is ignored by NASM, but is
2117 * required by TASM to define the local parameter size (and used
2118 * by the TASM macro package).
2119 */
2120 offset = LocalOffset;
2121 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002122 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002123 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002124
H. Peter Anvine2c80182005-01-15 22:15:51 +00002125 /* Find the argument name */
2126 tline = tline->next;
2127 if (tline && tline->type == TOK_WHITESPACE)
2128 tline = tline->next;
2129 if (!tline || tline->type != TOK_ID) {
2130 error(ERR_NONFATAL,
2131 "`%%local' missing argument parameter");
2132 free_tlist(origline);
2133 return DIRECTIVE_FOUND;
2134 }
2135 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002136
H. Peter Anvine2c80182005-01-15 22:15:51 +00002137 /* Find the argument size type */
2138 tline = tline->next;
2139 if (!tline || tline->type != TOK_OTHER
2140 || tline->text[0] != ':') {
2141 error(ERR_NONFATAL,
2142 "Syntax error processing `%%local' directive");
2143 free_tlist(origline);
2144 return DIRECTIVE_FOUND;
2145 }
2146 tline = tline->next;
2147 if (!tline || tline->type != TOK_ID) {
2148 error(ERR_NONFATAL,
2149 "`%%local' missing size type parameter");
2150 free_tlist(origline);
2151 return DIRECTIVE_FOUND;
2152 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002153
H. Peter Anvine2c80182005-01-15 22:15:51 +00002154 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002155 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002156 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002157 size = parse_size(tt->text);
2158 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002159 error(ERR_NONFATAL,
2160 "Invalid size type for `%%local' missing directive");
2161 free_tlist(tt);
2162 free_tlist(origline);
2163 return DIRECTIVE_FOUND;
2164 }
2165 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002166
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002167 /* Round up to even stack slots */
2168 size = (size+StackSize-1) & ~(StackSize-1);
2169
2170 offset += size; /* Negative offset, increment before */
2171
2172 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002173 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2174 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002175 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002176
H. Peter Anvine2c80182005-01-15 22:15:51 +00002177 /* Now define the assign to setup the enter_c macro correctly */
2178 snprintf(directive, sizeof(directive),
2179 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002180 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002181
H. Peter Anvine2c80182005-01-15 22:15:51 +00002182 /* Move to the next argument in the list */
2183 tline = tline->next;
2184 if (tline && tline->type == TOK_WHITESPACE)
2185 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002186 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2187 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002188 free_tlist(origline);
2189 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002190
H. Peter Anvine2c80182005-01-15 22:15:51 +00002191 case PP_CLEAR:
2192 if (tline->next)
2193 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002194 free_macros();
2195 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002196 free_tlist(origline);
2197 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002198
H. Peter Anvin418ca702008-05-30 10:42:30 -07002199 case PP_DEPEND:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002200 t = tline->next = expand_smacro(tline->next);
2201 skip_white_(t);
2202 if (!t || (t->type != TOK_STRING &&
2203 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002204 error(ERR_NONFATAL, "`%%depend' expects a file name");
2205 free_tlist(origline);
2206 return DIRECTIVE_FOUND; /* but we did _something_ */
2207 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002208 if (t->next)
H. Peter Anvin418ca702008-05-30 10:42:30 -07002209 error(ERR_WARNING,
2210 "trailing garbage after `%%depend' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002211 p = t->text;
2212 if (t->type != TOK_INTERNAL_STRING)
2213 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002214 if (dephead && !in_list(*dephead, p)) {
2215 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2216 sl->next = NULL;
2217 strcpy(sl->str, p);
2218 *deptail = sl;
2219 deptail = &sl->next;
2220 }
2221 free_tlist(origline);
2222 return DIRECTIVE_FOUND;
2223
2224 case PP_INCLUDE:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002225 t = tline->next = expand_smacro(tline->next);
2226 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002227
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002228 if (!t || (t->type != TOK_STRING &&
2229 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002230 error(ERR_NONFATAL, "`%%include' expects a file name");
2231 free_tlist(origline);
2232 return DIRECTIVE_FOUND; /* but we did _something_ */
2233 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002234 if (t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002235 error(ERR_WARNING,
2236 "trailing garbage after `%%include' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002237 p = t->text;
2238 if (t->type != TOK_INTERNAL_STRING)
2239 nasm_unquote(p, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002240 inc = nasm_malloc(sizeof(Include));
2241 inc->next = istk;
2242 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002243 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002244 if (!inc->fp) {
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002245 /* -MG given but file not found */
2246 nasm_free(inc);
2247 } else {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002248 inc->fname = src_set_fname(nasm_strdup(p));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002249 inc->lineno = src_set_linnum(0);
2250 inc->lineinc = 1;
2251 inc->expansion = NULL;
2252 inc->mstk = NULL;
2253 istk = inc;
2254 list->uplevel(LIST_INCLUDE);
2255 }
2256 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002257 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002258
H. Peter Anvind2456592008-06-19 15:04:18 -07002259 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002260 {
H. Peter Anvina70547f2008-07-19 21:44:26 -07002261 static macros_t *use_pkg;
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002262 const char *pkg_macro;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002263
H. Peter Anvind2456592008-06-19 15:04:18 -07002264 t = tline->next = expand_smacro(tline->next);
2265 skip_white_(t);
2266
2267 if (!t || (t->type != TOK_STRING &&
2268 t->type != TOK_INTERNAL_STRING &&
2269 t->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002270 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002271 free_tlist(origline);
2272 return DIRECTIVE_FOUND; /* but we did _something_ */
2273 }
2274 if (t->next)
2275 error(ERR_WARNING,
2276 "trailing garbage after `%%use' ignored");
H. Peter Anvind2456592008-06-19 15:04:18 -07002277 if (t->type == TOK_STRING)
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002278 nasm_unquote(t->text, NULL);
2279 use_pkg = nasm_stdmac_find_package(t->text);
2280 if (!use_pkg)
2281 error(ERR_NONFATAL, "unknown `%%use' package: %s", t->text);
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002282 /* The first string will be <%define>__USE_*__ */
H. Peter Anvin7e50d232008-06-25 14:54:14 -07002283 pkg_macro = (char *)use_pkg + 1;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002284 if (!smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2285 /* Not already included, go ahead and include it */
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002286 stdmacpos = use_pkg;
2287 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002288 free_tlist(origline);
2289 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002290 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002291 case PP_PUSH:
2292 tline = tline->next;
2293 skip_white_(tline);
2294 tline = expand_id(tline);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002295 if (tline) {
2296 if (!tok_type_(tline, TOK_ID)) {
2297 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2298 free_tlist(origline);
2299 return DIRECTIVE_FOUND; /* but we did _something_ */
2300 }
2301 if (tline->next)
2302 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2303 p = nasm_strdup(tline->text);
2304 } else {
2305 p = NULL; /* Anonymous context */
2306 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002307 ctx = nasm_malloc(sizeof(Context));
2308 ctx->next = cstk;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002309 hash_init(&ctx->localmac, HASH_SMALL);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002310 ctx->name = p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002311 ctx->number = unique++;
2312 cstk = ctx;
2313 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002314 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002315
H. Peter Anvine2c80182005-01-15 22:15:51 +00002316 case PP_REPL:
2317 tline = tline->next;
2318 skip_white_(tline);
2319 tline = expand_id(tline);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002320 if (tline) {
2321 if (!tok_type_(tline, TOK_ID)) {
2322 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2323 free_tlist(origline);
2324 return DIRECTIVE_FOUND; /* but we did _something_ */
2325 }
2326 if (tline->next)
2327 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2328 p = nasm_strdup(tline->text);
2329 } else {
2330 p = NULL;
2331 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002332 if (!cstk)
2333 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2334 else {
2335 nasm_free(cstk->name);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002336 cstk->name = p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002337 }
2338 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002339 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002340
H. Peter Anvine2c80182005-01-15 22:15:51 +00002341 case PP_POP:
2342 if (tline->next)
2343 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2344 if (!cstk)
2345 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2346 else
2347 ctx_pop();
2348 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002349 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002350
H. Peter Anvine2c80182005-01-15 22:15:51 +00002351 case PP_ERROR:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002352 case PP_WARNING:
2353 {
H. Peter Anvin97b6d5b2008-07-13 15:05:53 -07002354 int severity = (i == PP_ERROR)
2355 ? ERR_NONFATAL|ERR_NO_SEVERITY
2356 : ERR_WARNING|ERR_NO_SEVERITY;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002357
H. Peter Anvine2c80182005-01-15 22:15:51 +00002358 tline->next = expand_smacro(tline->next);
2359 tline = tline->next;
2360 skip_white_(tline);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002361 t = tline ? tline->next : NULL;
2362 skip_white_(t);
2363 if (tok_type_(tline, TOK_STRING) && !t) {
2364 /* The line contains only a quoted string */
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002365 p = tline->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002366 nasm_unquote(p, NULL);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002367 error(severity, "%s: %s", pp_directives[i], p);
2368 } else {
2369 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002370 p = detoken(tline, false);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002371 error(severity, "%s: %s", pp_directives[i], p);
2372 nasm_free(p);
H. Peter Anvind2456592008-06-19 15:04:18 -07002373 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002374 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002375 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002376 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002377
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002378 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002379 if (istk->conds && !emitting(istk->conds->state))
2380 j = COND_NEVER;
2381 else {
2382 j = if_condition(tline->next, i);
2383 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2385 }
2386 cond = nasm_malloc(sizeof(Cond));
2387 cond->next = istk->conds;
2388 cond->state = j;
2389 istk->conds = cond;
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002390 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002391 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002392
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002393 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002394 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002395 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002396 if (emitting(istk->conds->state)
2397 || istk->conds->state == COND_NEVER)
2398 istk->conds->state = COND_NEVER;
2399 else {
2400 /*
2401 * IMPORTANT: In the case of %if, we will already have
2402 * called expand_mmac_params(); however, if we're
2403 * processing an %elif we must have been in a
2404 * non-emitting mode, which would have inhibited
2405 * the normal invocation of expand_mmac_params(). Therefore,
2406 * we have to do it explicitly here.
2407 */
2408 j = if_condition(expand_mmac_params(tline->next), i);
2409 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002410 istk->conds->state =
2411 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2412 }
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002413 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002414 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002415
H. Peter Anvine2c80182005-01-15 22:15:51 +00002416 case PP_ELSE:
2417 if (tline->next)
2418 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2419 if (!istk->conds)
2420 error(ERR_FATAL, "`%%else': no matching `%%if'");
2421 if (emitting(istk->conds->state)
2422 || istk->conds->state == COND_NEVER)
2423 istk->conds->state = COND_ELSE_FALSE;
2424 else
2425 istk->conds->state = COND_ELSE_TRUE;
2426 free_tlist(origline);
2427 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002428
H. Peter Anvine2c80182005-01-15 22:15:51 +00002429 case PP_ENDIF:
2430 if (tline->next)
2431 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2432 if (!istk->conds)
2433 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2434 cond = istk->conds;
2435 istk->conds = cond->next;
2436 nasm_free(cond);
2437 free_tlist(origline);
2438 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002439
H. Peter Anvine2c80182005-01-15 22:15:51 +00002440 case PP_MACRO:
2441 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002442 if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002443 error(ERR_FATAL,
2444 "`%%%smacro': already defining a macro",
2445 (i == PP_IMACRO ? "i" : ""));
H. Peter Anvina26433d2008-07-16 14:40:01 -07002446 return DIRECTIVE_FOUND;
2447 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002448 defining = nasm_malloc(sizeof(MMacro));
H. Peter Anvina26433d2008-07-16 14:40:01 -07002449 defining->casesense = (i == PP_MACRO);
2450 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2451 nasm_free(defining);
2452 defining = NULL;
2453 return DIRECTIVE_FOUND;
2454 }
2455
H. Peter Anvin166c2472008-05-28 12:28:58 -07002456 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002457 while (mmac) {
2458 if (!strcmp(mmac->name, defining->name) &&
2459 (mmac->nparam_min <= defining->nparam_max
2460 || defining->plus)
2461 && (defining->nparam_min <= mmac->nparam_max
2462 || mmac->plus)) {
2463 error(ERR_WARNING,
2464 "redefining multi-line macro `%s'", defining->name);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002465 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002466 }
2467 mmac = mmac->next;
2468 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002469 free_tlist(origline);
2470 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002471
H. Peter Anvine2c80182005-01-15 22:15:51 +00002472 case PP_ENDM:
2473 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002474 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002475 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2476 return DIRECTIVE_FOUND;
2477 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002478 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002479 defining->next = *mmhead;
2480 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002481 defining = NULL;
2482 free_tlist(origline);
2483 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002484
H. Peter Anvina26433d2008-07-16 14:40:01 -07002485 case PP_UNMACRO:
2486 case PP_UNIMACRO:
2487 {
2488 MMacro **mmac_p;
2489 MMacro spec;
2490
2491 spec.casesense = (i == PP_UNMACRO);
2492 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2493 return DIRECTIVE_FOUND;
2494 }
2495 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2496 while (mmac_p && *mmac_p) {
2497 mmac = *mmac_p;
2498 if (mmac->casesense == spec.casesense &&
2499 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2500 mmac->nparam_min == spec.nparam_min &&
2501 mmac->nparam_max == spec.nparam_max &&
2502 mmac->plus == spec.plus) {
2503 *mmac_p = mmac->next;
2504 free_mmacro(mmac);
2505 } else {
2506 mmac_p = &mmac->next;
2507 }
2508 }
2509 free_tlist(origline);
2510 free_tlist(spec.dlist);
2511 return DIRECTIVE_FOUND;
2512 }
2513
H. Peter Anvine2c80182005-01-15 22:15:51 +00002514 case PP_ROTATE:
2515 if (tline->next && tline->next->type == TOK_WHITESPACE)
2516 tline = tline->next;
2517 if (tline->next == NULL) {
2518 free_tlist(origline);
2519 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2520 return DIRECTIVE_FOUND;
2521 }
2522 t = expand_smacro(tline->next);
2523 tline->next = NULL;
2524 free_tlist(origline);
2525 tline = t;
2526 tptr = &t;
2527 tokval.t_type = TOKEN_INVALID;
2528 evalresult =
2529 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2530 free_tlist(tline);
2531 if (!evalresult)
2532 return DIRECTIVE_FOUND;
2533 if (tokval.t_type)
2534 error(ERR_WARNING,
2535 "trailing garbage after expression ignored");
2536 if (!is_simple(evalresult)) {
2537 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2538 return DIRECTIVE_FOUND;
2539 }
2540 mmac = istk->mstk;
2541 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2542 mmac = mmac->next_active;
2543 if (!mmac) {
2544 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2545 } else if (mmac->nparam == 0) {
2546 error(ERR_NONFATAL,
2547 "`%%rotate' invoked within macro without parameters");
2548 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002549 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002550
H. Peter Anvin25a99342007-09-22 17:45:45 -07002551 rotate %= (int)mmac->nparam;
2552 if (rotate < 0)
2553 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002554
H. Peter Anvin25a99342007-09-22 17:45:45 -07002555 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002556 }
2557 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002558
H. Peter Anvine2c80182005-01-15 22:15:51 +00002559 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002560 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002561 do {
2562 tline = tline->next;
2563 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002564
H. Peter Anvine2c80182005-01-15 22:15:51 +00002565 if (tok_type_(tline, TOK_ID) &&
2566 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002567 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002568 do {
2569 tline = tline->next;
2570 } while (tok_type_(tline, TOK_WHITESPACE));
2571 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002572
H. Peter Anvine2c80182005-01-15 22:15:51 +00002573 if (tline) {
2574 t = expand_smacro(tline);
2575 tptr = &t;
2576 tokval.t_type = TOKEN_INVALID;
2577 evalresult =
2578 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2579 if (!evalresult) {
2580 free_tlist(origline);
2581 return DIRECTIVE_FOUND;
2582 }
2583 if (tokval.t_type)
2584 error(ERR_WARNING,
2585 "trailing garbage after expression ignored");
2586 if (!is_simple(evalresult)) {
2587 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2588 return DIRECTIVE_FOUND;
2589 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002590 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002591 } else {
2592 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002593 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002594 }
2595 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002596
H. Peter Anvine2c80182005-01-15 22:15:51 +00002597 tmp_defining = defining;
2598 defining = nasm_malloc(sizeof(MMacro));
2599 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002600 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002601 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002602 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002603 defining->in_progress = count;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002604 defining->nparam_min = defining->nparam_max = 0;
2605 defining->defaults = NULL;
2606 defining->dlist = NULL;
2607 defining->expansion = NULL;
2608 defining->next_active = istk->mstk;
2609 defining->rep_nest = tmp_defining;
2610 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002611
H. Peter Anvine2c80182005-01-15 22:15:51 +00002612 case PP_ENDREP:
2613 if (!defining || defining->name) {
2614 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2615 return DIRECTIVE_FOUND;
2616 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002617
H. Peter Anvine2c80182005-01-15 22:15:51 +00002618 /*
2619 * Now we have a "macro" defined - although it has no name
2620 * and we won't be entering it in the hash tables - we must
2621 * push a macro-end marker for it on to istk->expansion.
2622 * After that, it will take care of propagating itself (a
2623 * macro-end marker line for a macro which is really a %rep
2624 * block will cause the macro to be re-expanded, complete
2625 * with another macro-end marker to ensure the process
2626 * continues) until the whole expansion is forcibly removed
2627 * from istk->expansion by a %exitrep.
2628 */
2629 l = nasm_malloc(sizeof(Line));
2630 l->next = istk->expansion;
2631 l->finishes = defining;
2632 l->first = NULL;
2633 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002634
H. Peter Anvine2c80182005-01-15 22:15:51 +00002635 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002636
H. Peter Anvine2c80182005-01-15 22:15:51 +00002637 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2638 tmp_defining = defining;
2639 defining = defining->rep_nest;
2640 free_tlist(origline);
2641 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002642
H. Peter Anvine2c80182005-01-15 22:15:51 +00002643 case PP_EXITREP:
2644 /*
2645 * We must search along istk->expansion until we hit a
2646 * macro-end marker for a macro with no name. Then we set
2647 * its `in_progress' flag to 0.
2648 */
2649 for (l = istk->expansion; l; l = l->next)
2650 if (l->finishes && !l->finishes->name)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002651 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002652
H. Peter Anvine2c80182005-01-15 22:15:51 +00002653 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07002654 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002655 else
2656 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2657 free_tlist(origline);
2658 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002659
H. Peter Anvine2c80182005-01-15 22:15:51 +00002660 case PP_XDEFINE:
2661 case PP_IXDEFINE:
2662 case PP_DEFINE:
2663 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002664 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002665
H. Peter Anvine2c80182005-01-15 22:15:51 +00002666 tline = tline->next;
2667 skip_white_(tline);
2668 tline = expand_id(tline);
2669 if (!tline || (tline->type != TOK_ID &&
2670 (tline->type != TOK_PREPROC_ID ||
2671 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002672 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2673 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002674 free_tlist(origline);
2675 return DIRECTIVE_FOUND;
2676 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002677
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002678 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002679
H. Peter Anvine2c80182005-01-15 22:15:51 +00002680 mname = tline->text;
2681 last = tline;
2682 param_start = tline = tline->next;
2683 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002684
H. Peter Anvine2c80182005-01-15 22:15:51 +00002685 /* Expand the macro definition now for %xdefine and %ixdefine */
2686 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2687 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002688
H. Peter Anvine2c80182005-01-15 22:15:51 +00002689 if (tok_is_(tline, "(")) {
2690 /*
2691 * This macro has parameters.
2692 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002693
H. Peter Anvine2c80182005-01-15 22:15:51 +00002694 tline = tline->next;
2695 while (1) {
2696 skip_white_(tline);
2697 if (!tline) {
2698 error(ERR_NONFATAL, "parameter identifier expected");
2699 free_tlist(origline);
2700 return DIRECTIVE_FOUND;
2701 }
2702 if (tline->type != TOK_ID) {
2703 error(ERR_NONFATAL,
2704 "`%s': parameter identifier expected",
2705 tline->text);
2706 free_tlist(origline);
2707 return DIRECTIVE_FOUND;
2708 }
2709 tline->type = TOK_SMAC_PARAM + nparam++;
2710 tline = tline->next;
2711 skip_white_(tline);
2712 if (tok_is_(tline, ",")) {
2713 tline = tline->next;
2714 continue;
2715 }
2716 if (!tok_is_(tline, ")")) {
2717 error(ERR_NONFATAL,
2718 "`)' expected to terminate macro template");
2719 free_tlist(origline);
2720 return DIRECTIVE_FOUND;
2721 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002722 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002723 }
2724 last = tline;
2725 tline = tline->next;
2726 }
2727 if (tok_type_(tline, TOK_WHITESPACE))
2728 last = tline, tline = tline->next;
2729 macro_start = NULL;
2730 last->next = NULL;
2731 t = tline;
2732 while (t) {
2733 if (t->type == TOK_ID) {
2734 for (tt = param_start; tt; tt = tt->next)
2735 if (tt->type >= TOK_SMAC_PARAM &&
2736 !strcmp(tt->text, t->text))
2737 t->type = tt->type;
2738 }
2739 tt = t->next;
2740 t->next = macro_start;
2741 macro_start = t;
2742 t = tt;
2743 }
2744 /*
2745 * Good. We now have a macro name, a parameter count, and a
2746 * token list (in reverse order) for an expansion. We ought
2747 * to be OK just to create an SMacro, store it, and let
2748 * free_tlist have the rest of the line (which we have
2749 * carefully re-terminated after chopping off the expansion
2750 * from the end).
2751 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002752 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002753 free_tlist(origline);
2754 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002755
H. Peter Anvine2c80182005-01-15 22:15:51 +00002756 case PP_UNDEF:
2757 tline = tline->next;
2758 skip_white_(tline);
2759 tline = expand_id(tline);
2760 if (!tline || (tline->type != TOK_ID &&
2761 (tline->type != TOK_PREPROC_ID ||
2762 tline->text[1] != '$'))) {
2763 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2764 free_tlist(origline);
2765 return DIRECTIVE_FOUND;
2766 }
2767 if (tline->next) {
2768 error(ERR_WARNING,
2769 "trailing garbage after macro name ignored");
2770 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002771
H. Peter Anvine2c80182005-01-15 22:15:51 +00002772 /* Find the context that symbol belongs to */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002773 ctx = get_ctx(tline->text, false);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002774 undef_smacro(ctx, tline->text);
2775 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002776 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002777
H. Peter Anvin9e200162008-06-04 17:23:14 -07002778 case PP_DEFSTR:
2779 case PP_IDEFSTR:
2780 casesense = (i == PP_DEFSTR);
2781
2782 tline = tline->next;
2783 skip_white_(tline);
2784 tline = expand_id(tline);
2785 if (!tline || (tline->type != TOK_ID &&
2786 (tline->type != TOK_PREPROC_ID ||
2787 tline->text[1] != '$'))) {
2788 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2789 pp_directives[i]);
2790 free_tlist(origline);
2791 return DIRECTIVE_FOUND;
2792 }
2793
2794 ctx = get_ctx(tline->text, false);
2795
2796 mname = tline->text;
2797 last = tline;
2798 tline = expand_smacro(tline->next);
2799 last->next = NULL;
2800
2801 while (tok_type_(tline, TOK_WHITESPACE))
2802 tline = delete_Token(tline);
2803
2804 p = detoken(tline, false);
2805 macro_start = nasm_malloc(sizeof(*macro_start));
2806 macro_start->next = NULL;
2807 macro_start->text = nasm_quote(p, strlen(p));
2808 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002809 macro_start->a.mac = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07002810 nasm_free(p);
2811
2812 /*
2813 * We now have a macro name, an implicit parameter count of
2814 * zero, and a string token to use as an expansion. Create
2815 * and store an SMacro.
2816 */
2817 define_smacro(ctx, mname, casesense, 0, macro_start);
2818 free_tlist(origline);
2819 return DIRECTIVE_FOUND;
2820
H. Peter Anvin418ca702008-05-30 10:42:30 -07002821 case PP_PATHSEARCH:
2822 {
2823 FILE *fp;
2824 StrList *xsl = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002825 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002826
2827 casesense = true;
2828
2829 tline = tline->next;
2830 skip_white_(tline);
2831 tline = expand_id(tline);
2832 if (!tline || (tline->type != TOK_ID &&
2833 (tline->type != TOK_PREPROC_ID ||
2834 tline->text[1] != '$'))) {
2835 error(ERR_NONFATAL,
2836 "`%%pathsearch' expects a macro identifier as first parameter");
2837 free_tlist(origline);
2838 return DIRECTIVE_FOUND;
2839 }
2840 ctx = get_ctx(tline->text, false);
2841
2842 mname = tline->text;
2843 last = tline;
2844 tline = expand_smacro(tline->next);
2845 last->next = NULL;
2846
2847 t = tline;
2848 while (tok_type_(t, TOK_WHITESPACE))
2849 t = t->next;
2850
2851 if (!t || (t->type != TOK_STRING &&
2852 t->type != TOK_INTERNAL_STRING)) {
2853 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
2854 free_tlist(tline);
2855 free_tlist(origline);
2856 return DIRECTIVE_FOUND; /* but we did _something_ */
2857 }
2858 if (t->next)
2859 error(ERR_WARNING,
2860 "trailing garbage after `%%pathsearch' ignored");
H. Peter Anvin427cc912008-06-01 21:43:03 -07002861 p = t->text;
2862 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002863 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002864
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002865 fp = inc_fopen(p, &xsl, &xst, true);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002866 if (fp) {
2867 p = xsl->str;
2868 fclose(fp); /* Don't actually care about the file */
2869 }
2870 macro_start = nasm_malloc(sizeof(*macro_start));
2871 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002872 macro_start->text = nasm_quote(p, strlen(p));
H. Peter Anvin418ca702008-05-30 10:42:30 -07002873 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002874 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002875 if (xsl)
2876 nasm_free(xsl);
2877
2878 /*
2879 * We now have a macro name, an implicit parameter count of
2880 * zero, and a string token to use as an expansion. Create
2881 * and store an SMacro.
2882 */
2883 define_smacro(ctx, mname, casesense, 0, macro_start);
2884 free_tlist(tline);
2885 free_tlist(origline);
2886 return DIRECTIVE_FOUND;
2887 }
2888
H. Peter Anvine2c80182005-01-15 22:15:51 +00002889 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002890 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07002891
H. Peter Anvine2c80182005-01-15 22:15:51 +00002892 tline = tline->next;
2893 skip_white_(tline);
2894 tline = expand_id(tline);
2895 if (!tline || (tline->type != TOK_ID &&
2896 (tline->type != TOK_PREPROC_ID ||
2897 tline->text[1] != '$'))) {
2898 error(ERR_NONFATAL,
2899 "`%%strlen' expects a macro identifier as first parameter");
2900 free_tlist(origline);
2901 return DIRECTIVE_FOUND;
2902 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002903 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002904
H. Peter Anvine2c80182005-01-15 22:15:51 +00002905 mname = tline->text;
2906 last = tline;
2907 tline = expand_smacro(tline->next);
2908 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002909
H. Peter Anvine2c80182005-01-15 22:15:51 +00002910 t = tline;
2911 while (tok_type_(t, TOK_WHITESPACE))
2912 t = t->next;
2913 /* t should now point to the string */
2914 if (t->type != TOK_STRING) {
2915 error(ERR_NONFATAL,
2916 "`%%strlen` requires string as second parameter");
2917 free_tlist(tline);
2918 free_tlist(origline);
2919 return DIRECTIVE_FOUND;
2920 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002921
H. Peter Anvine2c80182005-01-15 22:15:51 +00002922 macro_start = nasm_malloc(sizeof(*macro_start));
2923 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002924 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002925 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002926
H. Peter Anvine2c80182005-01-15 22:15:51 +00002927 /*
2928 * We now have a macro name, an implicit parameter count of
2929 * zero, and a numeric token to use as an expansion. Create
2930 * and store an SMacro.
2931 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002932 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002933 free_tlist(tline);
2934 free_tlist(origline);
2935 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002936
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002937 case PP_STRCAT:
2938 casesense = true;
2939
2940 tline = tline->next;
2941 skip_white_(tline);
2942 tline = expand_id(tline);
2943 if (!tline || (tline->type != TOK_ID &&
2944 (tline->type != TOK_PREPROC_ID ||
2945 tline->text[1] != '$'))) {
2946 error(ERR_NONFATAL,
2947 "`%%strcat' expects a macro identifier as first parameter");
2948 free_tlist(origline);
2949 return DIRECTIVE_FOUND;
2950 }
2951 ctx = get_ctx(tline->text, false);
2952
2953 mname = tline->text;
2954 last = tline;
2955 tline = expand_smacro(tline->next);
2956 last->next = NULL;
2957
2958 len = 0;
2959 for (t = tline; t; t = t->next) {
2960 switch (t->type) {
2961 case TOK_WHITESPACE:
2962 break;
2963 case TOK_STRING:
2964 len += t->a.len = nasm_unquote(t->text, NULL);
2965 break;
H. Peter Anvinaccf4332008-07-01 21:42:08 -07002966 case TOK_OTHER:
2967 if (!strcmp(t->text, ",")) /* permit comma separators */
2968 break;
2969 /* else fall through */
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002970 default:
2971 error(ERR_NONFATAL,
2972 "non-string passed to `%%strcat' (%d)", t->type);
2973 free_tlist(tline);
2974 free_tlist(origline);
2975 return DIRECTIVE_FOUND;
2976 }
2977 }
2978
2979 p = pp = nasm_malloc(len);
2980 t = tline;
2981 for (t = tline; t; t = t->next) {
2982 if (t->type == TOK_STRING) {
2983 memcpy(p, t->text, t->a.len);
2984 p += t->a.len;
2985 }
2986 }
2987
2988 /*
2989 * We now have a macro name, an implicit parameter count of
2990 * zero, and a numeric token to use as an expansion. Create
2991 * and store an SMacro.
2992 */
2993 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
2994 macro_start->text = nasm_quote(pp, len);
2995 nasm_free(pp);
2996 define_smacro(ctx, mname, casesense, 0, macro_start);
2997 free_tlist(tline);
2998 free_tlist(origline);
2999 return DIRECTIVE_FOUND;
3000
H. Peter Anvine2c80182005-01-15 22:15:51 +00003001 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003002 {
3003 int64_t a1, a2;
3004 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003005
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003006 casesense = true;
3007
H. Peter Anvine2c80182005-01-15 22:15:51 +00003008 tline = tline->next;
3009 skip_white_(tline);
3010 tline = expand_id(tline);
3011 if (!tline || (tline->type != TOK_ID &&
3012 (tline->type != TOK_PREPROC_ID ||
3013 tline->text[1] != '$'))) {
3014 error(ERR_NONFATAL,
3015 "`%%substr' expects a macro identifier as first parameter");
3016 free_tlist(origline);
3017 return DIRECTIVE_FOUND;
3018 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003019 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003020
H. Peter Anvine2c80182005-01-15 22:15:51 +00003021 mname = tline->text;
3022 last = tline;
3023 tline = expand_smacro(tline->next);
3024 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003025
H. Peter Anvine2c80182005-01-15 22:15:51 +00003026 t = tline->next;
3027 while (tok_type_(t, TOK_WHITESPACE))
3028 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003029
H. Peter Anvine2c80182005-01-15 22:15:51 +00003030 /* t should now point to the string */
3031 if (t->type != TOK_STRING) {
3032 error(ERR_NONFATAL,
3033 "`%%substr` requires string as second parameter");
3034 free_tlist(tline);
3035 free_tlist(origline);
3036 return DIRECTIVE_FOUND;
3037 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003038
H. Peter Anvine2c80182005-01-15 22:15:51 +00003039 tt = t->next;
3040 tptr = &tt;
3041 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003042 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3043 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003044 if (!evalresult) {
3045 free_tlist(tline);
3046 free_tlist(origline);
3047 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003048 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003049 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3050 free_tlist(tline);
3051 free_tlist(origline);
3052 return DIRECTIVE_FOUND;
3053 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003054 a1 = evalresult->value-1;
3055
3056 while (tok_type_(tt, TOK_WHITESPACE))
3057 tt = tt->next;
3058 if (!tt) {
3059 a2 = 1; /* Backwards compatibility: one character */
3060 } else {
3061 tokval.t_type = TOKEN_INVALID;
3062 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3063 pass, error, NULL);
3064 if (!evalresult) {
3065 free_tlist(tline);
3066 free_tlist(origline);
3067 return DIRECTIVE_FOUND;
3068 } else if (!is_simple(evalresult)) {
3069 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3070 free_tlist(tline);
3071 free_tlist(origline);
3072 return DIRECTIVE_FOUND;
3073 }
3074 a2 = evalresult->value;
3075 }
3076
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003077 len = nasm_unquote(t->text, NULL);
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003078 if (a2 < 0)
3079 a2 = a2+1+len-a1;
3080 if (a1+a2 > (int64_t)len)
3081 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003082
H. Peter Anvine2c80182005-01-15 22:15:51 +00003083 macro_start = nasm_malloc(sizeof(*macro_start));
3084 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003085 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003086 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003087 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003088
H. Peter Anvine2c80182005-01-15 22:15:51 +00003089 /*
3090 * We now have a macro name, an implicit parameter count of
3091 * zero, and a numeric token to use as an expansion. Create
3092 * and store an SMacro.
3093 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003094 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003095 free_tlist(tline);
3096 free_tlist(origline);
3097 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003098 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003099
H. Peter Anvine2c80182005-01-15 22:15:51 +00003100 case PP_ASSIGN:
3101 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003102 casesense = (i == PP_ASSIGN);
3103
H. Peter Anvine2c80182005-01-15 22:15:51 +00003104 tline = tline->next;
3105 skip_white_(tline);
3106 tline = expand_id(tline);
3107 if (!tline || (tline->type != TOK_ID &&
3108 (tline->type != TOK_PREPROC_ID ||
3109 tline->text[1] != '$'))) {
3110 error(ERR_NONFATAL,
3111 "`%%%sassign' expects a macro identifier",
3112 (i == PP_IASSIGN ? "i" : ""));
3113 free_tlist(origline);
3114 return DIRECTIVE_FOUND;
3115 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003116 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003117
H. Peter Anvine2c80182005-01-15 22:15:51 +00003118 mname = tline->text;
3119 last = tline;
3120 tline = expand_smacro(tline->next);
3121 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003122
H. Peter Anvine2c80182005-01-15 22:15:51 +00003123 t = tline;
3124 tptr = &t;
3125 tokval.t_type = TOKEN_INVALID;
3126 evalresult =
3127 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3128 free_tlist(tline);
3129 if (!evalresult) {
3130 free_tlist(origline);
3131 return DIRECTIVE_FOUND;
3132 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003133
H. Peter Anvine2c80182005-01-15 22:15:51 +00003134 if (tokval.t_type)
3135 error(ERR_WARNING,
3136 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003137
H. Peter Anvine2c80182005-01-15 22:15:51 +00003138 if (!is_simple(evalresult)) {
3139 error(ERR_NONFATAL,
3140 "non-constant value given to `%%%sassign'",
3141 (i == PP_IASSIGN ? "i" : ""));
3142 free_tlist(origline);
3143 return DIRECTIVE_FOUND;
3144 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003145
H. Peter Anvine2c80182005-01-15 22:15:51 +00003146 macro_start = nasm_malloc(sizeof(*macro_start));
3147 macro_start->next = NULL;
3148 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003149 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003150
H. Peter Anvine2c80182005-01-15 22:15:51 +00003151 /*
3152 * We now have a macro name, an implicit parameter count of
3153 * zero, and a numeric token to use as an expansion. Create
3154 * and store an SMacro.
3155 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003156 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003157 free_tlist(origline);
3158 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003159
H. Peter Anvine2c80182005-01-15 22:15:51 +00003160 case PP_LINE:
3161 /*
3162 * Syntax is `%line nnn[+mmm] [filename]'
3163 */
3164 tline = tline->next;
3165 skip_white_(tline);
3166 if (!tok_type_(tline, TOK_NUMBER)) {
3167 error(ERR_NONFATAL, "`%%line' expects line number");
3168 free_tlist(origline);
3169 return DIRECTIVE_FOUND;
3170 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003171 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003172 m = 1;
3173 tline = tline->next;
3174 if (tok_is_(tline, "+")) {
3175 tline = tline->next;
3176 if (!tok_type_(tline, TOK_NUMBER)) {
3177 error(ERR_NONFATAL, "`%%line' expects line increment");
3178 free_tlist(origline);
3179 return DIRECTIVE_FOUND;
3180 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003181 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003182 tline = tline->next;
3183 }
3184 skip_white_(tline);
3185 src_set_linnum(k);
3186 istk->lineinc = m;
3187 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003188 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003189 }
3190 free_tlist(origline);
3191 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003192
H. Peter Anvine2c80182005-01-15 22:15:51 +00003193 default:
3194 error(ERR_FATAL,
3195 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003196 pp_directives[i]);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003197 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003198 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003199}
3200
3201/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003202 * Ensure that a macro parameter contains a condition code and
3203 * nothing else. Return the condition code index if so, or -1
3204 * otherwise.
3205 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003206static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003207{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003208 Token *tt;
3209 int i, j, k, m;
3210
H. Peter Anvin25a99342007-09-22 17:45:45 -07003211 if (!t)
3212 return -1; /* Probably a %+ without a space */
3213
H. Peter Anvineba20a72002-04-30 20:53:55 +00003214 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003215 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003216 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003217 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003218 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003219 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003220 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003221
3222 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00003223 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003224 while (j - i > 1) {
3225 k = (j + i) / 2;
3226 m = nasm_stricmp(t->text, conditions[k]);
3227 if (m == 0) {
3228 i = k;
3229 j = -2;
3230 break;
3231 } else if (m < 0) {
3232 j = k;
3233 } else
3234 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003235 }
3236 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003237 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003238 return i;
3239}
3240
3241/*
3242 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3243 * %-n) and MMacro-local identifiers (%%foo).
3244 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003245static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003246{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003247 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003248
3249 tail = &thead;
3250 thead = NULL;
3251
H. Peter Anvine2c80182005-01-15 22:15:51 +00003252 while (tline) {
3253 if (tline->type == TOK_PREPROC_ID &&
3254 (((tline->text[1] == '+' || tline->text[1] == '-')
3255 && tline->text[2]) || tline->text[1] == '%'
3256 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003257 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003258 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003259 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003260 unsigned int n;
3261 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003262 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003263
H. Peter Anvine2c80182005-01-15 22:15:51 +00003264 t = tline;
3265 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003266
H. Peter Anvine2c80182005-01-15 22:15:51 +00003267 mac = istk->mstk;
3268 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3269 mac = mac->next_active;
3270 if (!mac)
3271 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3272 else
3273 switch (t->text[1]) {
3274 /*
3275 * We have to make a substitution of one of the
3276 * forms %1, %-1, %+1, %%foo, %0.
3277 */
3278 case '0':
3279 type = TOK_NUMBER;
3280 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3281 text = nasm_strdup(tmpbuf);
3282 break;
3283 case '%':
3284 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003285 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00003286 mac->unique);
3287 text = nasm_strcat(tmpbuf, t->text + 2);
3288 break;
3289 case '-':
3290 n = atoi(t->text + 2) - 1;
3291 if (n >= mac->nparam)
3292 tt = NULL;
3293 else {
3294 if (mac->nparam > 1)
3295 n = (n + mac->rotate) % mac->nparam;
3296 tt = mac->params[n];
3297 }
3298 cc = find_cc(tt);
3299 if (cc == -1) {
3300 error(ERR_NONFATAL,
3301 "macro parameter %d is not a condition code",
3302 n + 1);
3303 text = NULL;
3304 } else {
3305 type = TOK_ID;
3306 if (inverse_ccs[cc] == -1) {
3307 error(ERR_NONFATAL,
3308 "condition code `%s' is not invertible",
3309 conditions[cc]);
3310 text = NULL;
3311 } else
3312 text =
3313 nasm_strdup(conditions[inverse_ccs[cc]]);
3314 }
3315 break;
3316 case '+':
3317 n = atoi(t->text + 2) - 1;
3318 if (n >= mac->nparam)
3319 tt = NULL;
3320 else {
3321 if (mac->nparam > 1)
3322 n = (n + mac->rotate) % mac->nparam;
3323 tt = mac->params[n];
3324 }
3325 cc = find_cc(tt);
3326 if (cc == -1) {
3327 error(ERR_NONFATAL,
3328 "macro parameter %d is not a condition code",
3329 n + 1);
3330 text = NULL;
3331 } else {
3332 type = TOK_ID;
3333 text = nasm_strdup(conditions[cc]);
3334 }
3335 break;
3336 default:
3337 n = atoi(t->text + 1) - 1;
3338 if (n >= mac->nparam)
3339 tt = NULL;
3340 else {
3341 if (mac->nparam > 1)
3342 n = (n + mac->rotate) % mac->nparam;
3343 tt = mac->params[n];
3344 }
3345 if (tt) {
3346 for (i = 0; i < mac->paramlen[n]; i++) {
3347 *tail = new_Token(NULL, tt->type, tt->text, 0);
3348 tail = &(*tail)->next;
3349 tt = tt->next;
3350 }
3351 }
3352 text = NULL; /* we've done it here */
3353 break;
3354 }
3355 if (!text) {
3356 delete_Token(t);
3357 } else {
3358 *tail = t;
3359 tail = &t->next;
3360 t->type = type;
3361 nasm_free(t->text);
3362 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003363 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003364 }
3365 continue;
3366 } else {
3367 t = *tail = tline;
3368 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003369 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003370 tail = &t->next;
3371 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003372 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003373 *tail = NULL;
3374 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003375 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003376 switch (t->type) {
3377 case TOK_WHITESPACE:
3378 if (tt->type == TOK_WHITESPACE) {
3379 t->next = delete_Token(tt);
3380 }
3381 break;
3382 case TOK_ID:
3383 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003384 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003385 nasm_free(t->text);
3386 t->text = tmp;
3387 t->next = delete_Token(tt);
3388 }
3389 break;
3390 case TOK_NUMBER:
3391 if (tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003392 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003393 nasm_free(t->text);
3394 t->text = tmp;
3395 t->next = delete_Token(tt);
3396 }
3397 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003398 default:
3399 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003400 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003401
H. Peter Anvin76690a12002-04-30 20:52:49 +00003402 return thead;
3403}
3404
3405/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003406 * Expand all single-line macro calls made in the given line.
3407 * Return the expanded version of the line. The original is deemed
3408 * to be destroyed in the process. (In reality we'll just move
3409 * Tokens from input to output a lot of the time, rather than
3410 * actually bothering to destroy and replicate.)
3411 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003412#define DEADMAN_LIMIT (1 << 20)
3413
H. Peter Anvine2c80182005-01-15 22:15:51 +00003414static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003415{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003416 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003417 struct hash_table *smtbl;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003418 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003419 Token **params;
3420 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003421 unsigned int nparam, sparam;
3422 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003423 Token *org_tline = tline;
3424 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003425 char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003426 int deadman = DEADMAN_LIMIT;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003427
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003428 /*
3429 * Trick: we should avoid changing the start token pointer since it can
3430 * be contained in "next" field of other token. Because of this
3431 * we allocate a copy of first token and work with it; at the end of
3432 * routine we copy it back
3433 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003434 if (org_tline) {
3435 tline =
3436 new_Token(org_tline->next, org_tline->type, org_tline->text,
3437 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003438 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003439 nasm_free(org_tline->text);
3440 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003441 }
3442
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003443again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003444 tail = &thead;
3445 thead = NULL;
3446
H. Peter Anvine2c80182005-01-15 22:15:51 +00003447 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003448 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003449 error(ERR_NONFATAL, "interminable macro recursion");
3450 break;
3451 }
3452
H. Peter Anvine2c80182005-01-15 22:15:51 +00003453 if ((mname = tline->text)) {
3454 /* if this token is a local macro, look in local context */
H. Peter Anvinb037a672008-05-29 19:08:08 -07003455 ctx = NULL;
3456 smtbl = &smacros;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003457 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003458 ctx = get_ctx(mname, true);
H. Peter Anvinb037a672008-05-29 19:08:08 -07003459 if (ctx)
3460 smtbl = &ctx->localmac;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003461 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07003462 head = (SMacro *) hash_findix(smtbl, mname);
H. Peter Anvin072771e2008-05-22 13:17:51 -07003463
H. Peter Anvine2c80182005-01-15 22:15:51 +00003464 /*
3465 * We've hit an identifier. As in is_mmacro below, we first
3466 * check whether the identifier is a single-line macro at
3467 * all, then think about checking for parameters if
3468 * necessary.
3469 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003470 for (m = head; m; m = m->next)
3471 if (!mstrcmp(m->name, mname, m->casesense))
3472 break;
3473 if (m) {
3474 mstart = tline;
3475 params = NULL;
3476 paramsize = NULL;
3477 if (m->nparam == 0) {
3478 /*
3479 * Simple case: the macro is parameterless. Discard the
3480 * one token that the macro call took, and push the
3481 * expansion back on the to-do stack.
3482 */
3483 if (!m->expansion) {
3484 if (!strcmp("__FILE__", m->name)) {
3485 int32_t num = 0;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003486 char *file;
3487 src_get(&num, &file);
3488 tline->text = nasm_quote(file, strlen(file));
H. Peter Anvin97a23472007-09-16 17:57:25 -07003489 tline->type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003490 nasm_free(file);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003491 continue;
3492 }
3493 if (!strcmp("__LINE__", m->name)) {
3494 nasm_free(tline->text);
3495 make_tok_num(tline, src_get_linnum());
3496 continue;
3497 }
3498 if (!strcmp("__BITS__", m->name)) {
3499 nasm_free(tline->text);
3500 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003501 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003502 }
3503 tline = delete_Token(tline);
3504 continue;
3505 }
3506 } else {
3507 /*
3508 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003509 * exists and takes parameters. We must find the
3510 * parameters in the call, count them, find the SMacro
3511 * that corresponds to that form of the macro call, and
3512 * substitute for the parameters when we expand. What a
3513 * pain.
3514 */
3515 /*tline = tline->next;
3516 skip_white_(tline); */
3517 do {
3518 t = tline->next;
3519 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003520 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003521 t->text = NULL;
3522 t = tline->next = delete_Token(t);
3523 }
3524 tline = t;
3525 } while (tok_type_(tline, TOK_WHITESPACE));
3526 if (!tok_is_(tline, "(")) {
3527 /*
3528 * This macro wasn't called with parameters: ignore
3529 * the call. (Behaviour borrowed from gnu cpp.)
3530 */
3531 tline = mstart;
3532 m = NULL;
3533 } else {
3534 int paren = 0;
3535 int white = 0;
3536 brackets = 0;
3537 nparam = 0;
3538 sparam = PARAM_DELTA;
3539 params = nasm_malloc(sparam * sizeof(Token *));
3540 params[0] = tline->next;
3541 paramsize = nasm_malloc(sparam * sizeof(int));
3542 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003543 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003544 /*
3545 * For some unusual expansions
3546 * which concatenates function call
3547 */
3548 t = tline->next;
3549 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003550 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003551 t->text = NULL;
3552 t = tline->next = delete_Token(t);
3553 }
3554 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003555
H. Peter Anvine2c80182005-01-15 22:15:51 +00003556 if (!tline) {
3557 error(ERR_NONFATAL,
3558 "macro call expects terminating `)'");
3559 break;
3560 }
3561 if (tline->type == TOK_WHITESPACE
3562 && brackets <= 0) {
3563 if (paramsize[nparam])
3564 white++;
3565 else
3566 params[nparam] = tline->next;
3567 continue; /* parameter loop */
3568 }
3569 if (tline->type == TOK_OTHER
3570 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003571 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003572 if (ch == ',' && !paren && brackets <= 0) {
3573 if (++nparam >= sparam) {
3574 sparam += PARAM_DELTA;
3575 params = nasm_realloc(params,
3576 sparam *
3577 sizeof(Token
3578 *));
3579 paramsize =
3580 nasm_realloc(paramsize,
3581 sparam *
3582 sizeof(int));
3583 }
3584 params[nparam] = tline->next;
3585 paramsize[nparam] = 0;
3586 white = 0;
3587 continue; /* parameter loop */
3588 }
3589 if (ch == '{' &&
3590 (brackets > 0 || (brackets == 0 &&
3591 !paramsize[nparam])))
3592 {
3593 if (!(brackets++)) {
3594 params[nparam] = tline->next;
3595 continue; /* parameter loop */
3596 }
3597 }
3598 if (ch == '}' && brackets > 0)
3599 if (--brackets == 0) {
3600 brackets = -1;
3601 continue; /* parameter loop */
3602 }
3603 if (ch == '(' && !brackets)
3604 paren++;
3605 if (ch == ')' && brackets <= 0)
3606 if (--paren < 0)
3607 break;
3608 }
3609 if (brackets < 0) {
3610 brackets = 0;
3611 error(ERR_NONFATAL, "braces do not "
3612 "enclose all of macro parameter");
3613 }
3614 paramsize[nparam] += white + 1;
3615 white = 0;
3616 } /* parameter loop */
3617 nparam++;
3618 while (m && (m->nparam != nparam ||
3619 mstrcmp(m->name, mname,
3620 m->casesense)))
3621 m = m->next;
3622 if (!m)
3623 error(ERR_WARNING | ERR_WARN_MNP,
3624 "macro `%s' exists, "
3625 "but not taking %d parameters",
3626 mstart->text, nparam);
3627 }
3628 }
3629 if (m && m->in_progress)
3630 m = NULL;
3631 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003632 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003633 * Design question: should we handle !tline, which
3634 * indicates missing ')' here, or expand those
3635 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003636 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003637 */
3638 nasm_free(params);
3639 nasm_free(paramsize);
3640 tline = mstart;
3641 } else {
3642 /*
3643 * Expand the macro: we are placed on the last token of the
3644 * call, so that we can easily split the call from the
3645 * following tokens. We also start by pushing an SMAC_END
3646 * token for the cycle removal.
3647 */
3648 t = tline;
3649 if (t) {
3650 tline = t->next;
3651 t->next = NULL;
3652 }
3653 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003654 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003655 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003656 tline = tt;
3657 for (t = m->expansion; t; t = t->next) {
3658 if (t->type >= TOK_SMAC_PARAM) {
3659 Token *pcopy = tline, **ptail = &pcopy;
3660 Token *ttt, *pt;
3661 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003662
H. Peter Anvine2c80182005-01-15 22:15:51 +00003663 ttt = params[t->type - TOK_SMAC_PARAM];
3664 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3665 --i >= 0;) {
3666 pt = *ptail =
3667 new_Token(tline, ttt->type, ttt->text,
3668 0);
3669 ptail = &pt->next;
3670 ttt = ttt->next;
3671 }
3672 tline = pcopy;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -07003673 } else if (t->type == TOK_PREPROC_Q) {
3674 tt = new_Token(tline, TOK_ID, mname, 0);
3675 tline = tt;
3676 } else if (t->type == TOK_PREPROC_QQ) {
3677 tt = new_Token(tline, TOK_ID, m->name, 0);
3678 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003679 } else {
3680 tt = new_Token(tline, t->type, t->text, 0);
3681 tline = tt;
3682 }
3683 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003684
H. Peter Anvine2c80182005-01-15 22:15:51 +00003685 /*
3686 * Having done that, get rid of the macro call, and clean
3687 * up the parameters.
3688 */
3689 nasm_free(params);
3690 nasm_free(paramsize);
3691 free_tlist(mstart);
3692 continue; /* main token loop */
3693 }
3694 }
3695 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003696
H. Peter Anvine2c80182005-01-15 22:15:51 +00003697 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003698 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003699 tline = delete_Token(tline);
3700 } else {
3701 t = *tail = tline;
3702 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003703 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003704 t->next = NULL;
3705 tail = &t->next;
3706 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003707 }
3708
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003709 /*
3710 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003711 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003712 * TOK_IDs should be concatenated.
3713 * Also we look for %+ tokens and concatenate the tokens before and after
3714 * them (without white spaces in between).
3715 */
3716 t = thead;
3717 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003718 while (t) {
3719 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3720 t = t->next;
3721 if (!t || !t->next)
3722 break;
3723 if (t->next->type == TOK_ID ||
3724 t->next->type == TOK_PREPROC_ID ||
3725 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003726 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003727 nasm_free(t->text);
3728 t->next = delete_Token(t->next);
3729 t->text = p;
3730 rescan = 1;
3731 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3732 t->next->next->type == TOK_PREPROC_ID &&
3733 strcmp(t->next->next->text, "%+") == 0) {
3734 /* free the next whitespace, the %+ token and next whitespace */
3735 int i;
3736 for (i = 1; i <= 3; i++) {
3737 if (!t->next
3738 || (i != 2 && t->next->type != TOK_WHITESPACE))
3739 break;
3740 t->next = delete_Token(t->next);
3741 } /* endfor */
3742 } else
3743 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003744 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003745 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003746 if (rescan) {
3747 tline = thead;
3748 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003749 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003750
H. Peter Anvine2c80182005-01-15 22:15:51 +00003751 if (org_tline) {
3752 if (thead) {
3753 *org_tline = *thead;
3754 /* since we just gave text to org_line, don't free it */
3755 thead->text = NULL;
3756 delete_Token(thead);
3757 } else {
3758 /* the expression expanded to empty line;
3759 we can't return NULL for some reasons
3760 we just set the line to a single WHITESPACE token. */
3761 memset(org_tline, 0, sizeof(*org_tline));
3762 org_tline->text = NULL;
3763 org_tline->type = TOK_WHITESPACE;
3764 }
3765 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003766 }
3767
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003768 return thead;
3769}
3770
3771/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003772 * Similar to expand_smacro but used exclusively with macro identifiers
3773 * right before they are fetched in. The reason is that there can be
3774 * identifiers consisting of several subparts. We consider that if there
3775 * are more than one element forming the name, user wants a expansion,
3776 * otherwise it will be left as-is. Example:
3777 *
3778 * %define %$abc cde
3779 *
3780 * the identifier %$abc will be left as-is so that the handler for %define
3781 * will suck it and define the corresponding value. Other case:
3782 *
3783 * %define _%$abc cde
3784 *
3785 * In this case user wants name to be expanded *before* %define starts
3786 * working, so we'll expand %$abc into something (if it has a value;
3787 * otherwise it will be left as-is) then concatenate all successive
3788 * PP_IDs into one.
3789 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003790static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003791{
3792 Token *cur, *oldnext = NULL;
3793
H. Peter Anvin734b1882002-04-30 21:01:08 +00003794 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003795 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003796
3797 cur = tline;
3798 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003799 (cur->next->type == TOK_ID ||
3800 cur->next->type == TOK_PREPROC_ID
3801 || cur->next->type == TOK_NUMBER))
3802 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003803
3804 /* If identifier consists of just one token, don't expand */
3805 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003806 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003807
H. Peter Anvine2c80182005-01-15 22:15:51 +00003808 if (cur) {
3809 oldnext = cur->next; /* Detach the tail past identifier */
3810 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003811 }
3812
H. Peter Anvin734b1882002-04-30 21:01:08 +00003813 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003814
H. Peter Anvine2c80182005-01-15 22:15:51 +00003815 if (cur) {
3816 /* expand_smacro possibly changhed tline; re-scan for EOL */
3817 cur = tline;
3818 while (cur && cur->next)
3819 cur = cur->next;
3820 if (cur)
3821 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003822 }
3823
3824 return tline;
3825}
3826
3827/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003828 * Determine whether the given line constitutes a multi-line macro
3829 * call, and return the MMacro structure called if so. Doesn't have
3830 * to check for an initial label - that's taken care of in
3831 * expand_mmacro - but must check numbers of parameters. Guaranteed
3832 * to be called with tline->type == TOK_ID, so the putative macro
3833 * name is easy to find.
3834 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003835static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003836{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003837 MMacro *head, *m;
3838 Token **params;
3839 int nparam;
3840
H. Peter Anvin166c2472008-05-28 12:28:58 -07003841 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003842
3843 /*
3844 * Efficiency: first we see if any macro exists with the given
3845 * name. If not, we can return NULL immediately. _Then_ we
3846 * count the parameters, and then we look further along the
3847 * list if necessary to find the proper MMacro.
3848 */
3849 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003850 if (!mstrcmp(m->name, tline->text, m->casesense))
3851 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003852 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003853 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003854
3855 /*
3856 * OK, we have a potential macro. Count and demarcate the
3857 * parameters.
3858 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003859 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003860
3861 /*
3862 * So we know how many parameters we've got. Find the MMacro
3863 * structure that handles this number.
3864 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003865 while (m) {
3866 if (m->nparam_min <= nparam
3867 && (m->plus || nparam <= m->nparam_max)) {
3868 /*
3869 * This one is right. Just check if cycle removal
3870 * prohibits us using it before we actually celebrate...
3871 */
3872 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003873#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003874 error(ERR_NONFATAL,
3875 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003876#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003877 nasm_free(params);
3878 return NULL;
3879 }
3880 /*
3881 * It's right, and we can use it. Add its default
3882 * parameters to the end of our list if necessary.
3883 */
3884 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3885 params =
3886 nasm_realloc(params,
3887 ((m->nparam_min + m->ndefs +
3888 1) * sizeof(*params)));
3889 while (nparam < m->nparam_min + m->ndefs) {
3890 params[nparam] = m->defaults[nparam - m->nparam_min];
3891 nparam++;
3892 }
3893 }
3894 /*
3895 * If we've gone over the maximum parameter count (and
3896 * we're in Plus mode), ignore parameters beyond
3897 * nparam_max.
3898 */
3899 if (m->plus && nparam > m->nparam_max)
3900 nparam = m->nparam_max;
3901 /*
3902 * Then terminate the parameter list, and leave.
3903 */
3904 if (!params) { /* need this special case */
3905 params = nasm_malloc(sizeof(*params));
3906 nparam = 0;
3907 }
3908 params[nparam] = NULL;
3909 *params_array = params;
3910 return m;
3911 }
3912 /*
3913 * This one wasn't right: look for the next one with the
3914 * same name.
3915 */
3916 for (m = m->next; m; m = m->next)
3917 if (!mstrcmp(m->name, tline->text, m->casesense))
3918 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003919 }
3920
3921 /*
3922 * After all that, we didn't find one with the right number of
3923 * parameters. Issue a warning, and fail to expand the macro.
3924 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003925 error(ERR_WARNING | ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003926 "macro `%s' exists, but not taking %d parameters",
3927 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003928 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003929 return NULL;
3930}
3931
3932/*
3933 * Expand the multi-line macro call made by the given line, if
3934 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003935 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003936 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003937static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003938{
3939 Token *startline = tline;
3940 Token *label = NULL;
3941 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003942 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003943 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003944 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003945 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07003946 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003947
3948 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003949 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07003950 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003951 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003952 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003953 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003954 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07003955 if (m) {
3956 mname = t->text;
3957 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003958 Token *last;
3959 /*
3960 * We have an id which isn't a macro call. We'll assume
3961 * it might be a label; we'll also check to see if a
3962 * colon follows it. Then, if there's another id after
3963 * that lot, we'll check it again for macro-hood.
3964 */
3965 label = last = t;
3966 t = t->next;
3967 if (tok_type_(t, TOK_WHITESPACE))
3968 last = t, t = t->next;
3969 if (tok_is_(t, ":")) {
3970 dont_prepend = 1;
3971 last = t, t = t->next;
3972 if (tok_type_(t, TOK_WHITESPACE))
3973 last = t, t = t->next;
3974 }
3975 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3976 return 0;
3977 last->next = NULL;
H. Peter Anvinc751e862008-06-09 10:18:45 -07003978 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003979 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003980 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003981
3982 /*
3983 * Fix up the parameters: this involves stripping leading and
3984 * trailing whitespace, then stripping braces if they are
3985 * present.
3986 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003987 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003988 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003989
H. Peter Anvine2c80182005-01-15 22:15:51 +00003990 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003991 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003992 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003993
H. Peter Anvine2c80182005-01-15 22:15:51 +00003994 t = params[i];
3995 skip_white_(t);
3996 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003997 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003998 params[i] = t;
3999 paramlen[i] = 0;
4000 while (t) {
4001 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4002 break; /* ... because we have hit a comma */
4003 if (comma && t->type == TOK_WHITESPACE
4004 && tok_is_(t->next, ","))
4005 break; /* ... or a space then a comma */
4006 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4007 break; /* ... or a brace */
4008 t = t->next;
4009 paramlen[i]++;
4010 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004011 }
4012
4013 /*
4014 * OK, we have a MMacro structure together with a set of
4015 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004016 * copies of each Line on to istk->expansion. Substitution of
4017 * parameter tokens and macro-local tokens doesn't get done
4018 * until the single-line macro substitution process; this is
4019 * because delaying them allows us to change the semantics
4020 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004021 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004022 * First, push an end marker on to istk->expansion, mark this
4023 * macro as in progress, and set up its invocation-specific
4024 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004025 */
4026 ll = nasm_malloc(sizeof(Line));
4027 ll->next = istk->expansion;
4028 ll->finishes = m;
4029 ll->first = NULL;
4030 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004031
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004032 m->in_progress = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004033 m->params = params;
4034 m->iline = tline;
4035 m->nparam = nparam;
4036 m->rotate = 0;
4037 m->paramlen = paramlen;
4038 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004039 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004040
4041 m->next_active = istk->mstk;
4042 istk->mstk = m;
4043
H. Peter Anvine2c80182005-01-15 22:15:51 +00004044 for (l = m->expansion; l; l = l->next) {
4045 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004046
H. Peter Anvine2c80182005-01-15 22:15:51 +00004047 ll = nasm_malloc(sizeof(Line));
4048 ll->finishes = NULL;
4049 ll->next = istk->expansion;
4050 istk->expansion = ll;
4051 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004052
H. Peter Anvine2c80182005-01-15 22:15:51 +00004053 for (t = l->first; t; t = t->next) {
4054 Token *x = t;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004055 switch (t->type) {
4056 case TOK_PREPROC_Q:
H. Peter Anvinc751e862008-06-09 10:18:45 -07004057 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004058 break;
4059 case TOK_PREPROC_QQ:
4060 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4061 break;
4062 case TOK_PREPROC_ID:
4063 if (t->text[1] == '0' && t->text[2] == '0') {
4064 dont_prepend = -1;
4065 x = label;
4066 if (!x)
4067 continue;
4068 }
4069 /* fall through */
4070 default:
4071 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4072 break;
H. Peter Anvin166c2472008-05-28 12:28:58 -07004073 }
H. Peter Anvince2233b2008-05-25 21:57:00 -07004074 tail = &tt->next;
4075 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004076 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004077 }
4078
4079 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004080 * If we had a label, push it on as the first line of
4081 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004082 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004083 if (label) {
4084 if (dont_prepend < 0)
4085 free_tlist(startline);
4086 else {
4087 ll = nasm_malloc(sizeof(Line));
4088 ll->finishes = NULL;
4089 ll->next = istk->expansion;
4090 istk->expansion = ll;
4091 ll->first = startline;
4092 if (!dont_prepend) {
4093 while (label->next)
4094 label = label->next;
4095 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4096 }
4097 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004098 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004099
H. Peter Anvin734b1882002-04-30 21:01:08 +00004100 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004101
H. Peter Anvineba20a72002-04-30 20:53:55 +00004102 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004103}
4104
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004105/*
4106 * Since preprocessor always operate only on the line that didn't
4107 * arrived yet, we should always use ERR_OFFBY1. Also since user
4108 * won't want to see same error twice (preprocessing is done once
4109 * per pass) we will want to show errors only during pass one.
4110 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004111static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004112{
4113 va_list arg;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004114 char buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004115
4116 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004117 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004118 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004119
H. Peter Anvin734b1882002-04-30 21:01:08 +00004120 va_start(arg, fmt);
Ed Beroset19f927a2004-12-15 17:07:03 +00004121 vsnprintf(buff, sizeof(buff), fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004122 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004123
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004124 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004125 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
4126 istk->mstk->lineno, buff);
4127 else
4128 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004129}
4130
H. Peter Anvin734b1882002-04-30 21:01:08 +00004131static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004132pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004133 ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004134{
H. Peter Anvin99941bf2002-05-14 17:44:03 +00004135 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004136 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004137 istk = nasm_malloc(sizeof(Include));
4138 istk->next = NULL;
4139 istk->conds = NULL;
4140 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004141 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004142 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004143 istk->fname = NULL;
4144 src_set_fname(nasm_strdup(file));
4145 src_set_linnum(0);
4146 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004147 if (!istk->fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004148 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
4149 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004150 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004151 nested_mac_count = 0;
4152 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004153 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004154 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004155 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004156 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004157 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004158 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004159 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004160 any_extrastdmac = extrastdmac && *extrastdmac;
4161 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004162 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004163 evaluate = eval;
4164 pass = apass;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004165 dephead = deptail = deplist;
4166 if (deplist) {
4167 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4168 sl->next = NULL;
4169 strcpy(sl->str, file);
4170 *deptail = sl;
4171 deptail = &sl->next;
4172 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004173}
4174
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004175static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004176{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004177 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004178 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004179
H. Peter Anvine2c80182005-01-15 22:15:51 +00004180 while (1) {
4181 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004182 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004183 * buffer or from the input file.
4184 */
4185 tline = NULL;
4186 while (istk->expansion && istk->expansion->finishes) {
4187 Line *l = istk->expansion;
4188 if (!l->finishes->name && l->finishes->in_progress > 1) {
4189 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004190
H. Peter Anvine2c80182005-01-15 22:15:51 +00004191 /*
4192 * This is a macro-end marker for a macro with no
4193 * name, which means it's not really a macro at all
4194 * but a %rep block, and the `in_progress' field is
4195 * more than 1, meaning that we still need to
4196 * repeat. (1 means the natural last repetition; 0
4197 * means termination by %exitrep.) We have
4198 * therefore expanded up to the %endrep, and must
4199 * push the whole block on to the expansion buffer
4200 * again. We don't bother to remove the macro-end
4201 * marker: we'd only have to generate another one
4202 * if we did.
4203 */
4204 l->finishes->in_progress--;
4205 for (l = l->finishes->expansion; l; l = l->next) {
4206 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004207
H. Peter Anvine2c80182005-01-15 22:15:51 +00004208 ll = nasm_malloc(sizeof(Line));
4209 ll->next = istk->expansion;
4210 ll->finishes = NULL;
4211 ll->first = NULL;
4212 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004213
H. Peter Anvine2c80182005-01-15 22:15:51 +00004214 for (t = l->first; t; t = t->next) {
4215 if (t->text || t->type == TOK_WHITESPACE) {
4216 tt = *tail =
4217 new_Token(NULL, t->type, t->text, 0);
4218 tail = &tt->next;
4219 }
4220 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004221
H. Peter Anvine2c80182005-01-15 22:15:51 +00004222 istk->expansion = ll;
4223 }
4224 } else {
4225 /*
4226 * Check whether a `%rep' was started and not ended
4227 * within this macro expansion. This can happen and
4228 * should be detected. It's a fatal error because
4229 * I'm too confused to work out how to recover
4230 * sensibly from it.
4231 */
4232 if (defining) {
4233 if (defining->name)
4234 error(ERR_PANIC,
4235 "defining with name in expansion");
4236 else if (istk->mstk->name)
4237 error(ERR_FATAL,
4238 "`%%rep' without `%%endrep' within"
4239 " expansion of macro `%s'",
4240 istk->mstk->name);
4241 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004242
H. Peter Anvine2c80182005-01-15 22:15:51 +00004243 /*
4244 * FIXME: investigate the relationship at this point between
4245 * istk->mstk and l->finishes
4246 */
4247 {
4248 MMacro *m = istk->mstk;
4249 istk->mstk = m->next_active;
4250 if (m->name) {
4251 /*
4252 * This was a real macro call, not a %rep, and
4253 * therefore the parameter information needs to
4254 * be freed.
4255 */
4256 nasm_free(m->params);
4257 free_tlist(m->iline);
4258 nasm_free(m->paramlen);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004259 l->finishes->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004260 } else
4261 free_mmacro(m);
4262 }
4263 istk->expansion = l->next;
4264 nasm_free(l);
4265 list->downlevel(LIST_MACRO);
4266 }
4267 }
4268 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004269
H. Peter Anvine2c80182005-01-15 22:15:51 +00004270 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004271 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004272 Line *l = istk->expansion;
4273 if (istk->mstk)
4274 istk->mstk->lineno++;
4275 tline = l->first;
4276 istk->expansion = l->next;
4277 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004278 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004279 list->line(LIST_MACRO, p);
4280 nasm_free(p);
4281 break;
4282 }
4283 line = read_line();
4284 if (line) { /* from the current input file */
4285 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004286 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004287 nasm_free(line);
4288 break;
4289 }
4290 /*
4291 * The current file has ended; work down the istk
4292 */
4293 {
4294 Include *i = istk;
4295 fclose(i->fp);
4296 if (i->conds)
4297 error(ERR_FATAL,
4298 "expected `%%endif' before end of file");
4299 /* only set line and file name if there's a next node */
4300 if (i->next) {
4301 src_set_linnum(i->lineno);
4302 nasm_free(src_set_fname(i->fname));
H. Peter Anvin86877b22008-06-20 15:55:45 -07004303 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004304 istk = i->next;
4305 list->downlevel(LIST_INCLUDE);
4306 nasm_free(i);
4307 if (!istk)
4308 return NULL;
4309 }
4310 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004311
H. Peter Anvine2c80182005-01-15 22:15:51 +00004312 /*
4313 * We must expand MMacro parameters and MMacro-local labels
4314 * _before_ we plunge into directive processing, to cope
4315 * with things like `%define something %1' such as STRUC
4316 * uses. Unless we're _defining_ a MMacro, in which case
4317 * those tokens should be left alone to go into the
4318 * definition; and unless we're in a non-emitting
4319 * condition, in which case we don't want to meddle with
4320 * anything.
4321 */
Charles Crayned4200be2008-07-12 16:42:33 -07004322 if (!defining && !(istk->conds && !emitting(istk->conds->state))
4323 && !(istk->mstk && !istk->mstk->in_progress))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004324 tline = expand_mmac_params(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004325
H. Peter Anvine2c80182005-01-15 22:15:51 +00004326 /*
4327 * Check the line to see if it's a preprocessor directive.
4328 */
4329 if (do_directive(tline) == DIRECTIVE_FOUND) {
4330 continue;
4331 } else if (defining) {
4332 /*
4333 * We're defining a multi-line macro. We emit nothing
4334 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004335 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004336 */
4337 Line *l = nasm_malloc(sizeof(Line));
4338 l->next = defining->expansion;
4339 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004340 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004341 defining->expansion = l;
4342 continue;
4343 } else if (istk->conds && !emitting(istk->conds->state)) {
4344 /*
4345 * We're in a non-emitting branch of a condition block.
4346 * Emit nothing at all, not even a blank line: when we
4347 * emerge from the condition we'll give a line-number
4348 * directive so we keep our place correctly.
4349 */
4350 free_tlist(tline);
4351 continue;
4352 } else if (istk->mstk && !istk->mstk->in_progress) {
4353 /*
4354 * We're in a %rep block which has been terminated, so
4355 * we're walking through to the %endrep without
4356 * emitting anything. Emit nothing at all, not even a
4357 * blank line: when we emerge from the %rep block we'll
4358 * give a line-number directive so we keep our place
4359 * correctly.
4360 */
4361 free_tlist(tline);
4362 continue;
4363 } else {
4364 tline = expand_smacro(tline);
4365 if (!expand_mmacro(tline)) {
4366 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004367 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004368 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004369 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004370 free_tlist(tline);
4371 break;
4372 } else {
4373 continue; /* expand_mmacro calls free_tlist */
4374 }
4375 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004376 }
4377
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004378 return line;
4379}
4380
H. Peter Anvine2c80182005-01-15 22:15:51 +00004381static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004382{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004383 if (defining) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02004384 if(defining->name) {
4385 error(ERR_NONFATAL,
4386 "end of file while still defining macro `%s'",
4387 defining->name);
4388 } else {
4389 error(ERR_NONFATAL, "end of file while still in %%rep");
4390 }
4391
H. Peter Anvine2c80182005-01-15 22:15:51 +00004392 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004393 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004394 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004395 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004396 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004397 while (istk) {
4398 Include *i = istk;
4399 istk = istk->next;
4400 fclose(i->fp);
4401 nasm_free(i->fname);
4402 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004403 }
4404 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004405 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004406 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004407 if (pass == 0) {
H. Peter Anvin86877b22008-06-20 15:55:45 -07004408 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004409 free_llist(predef);
4410 delete_Blocks();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004411 while ((i = ipath)) {
4412 ipath = i->next;
4413 if (i->path)
4414 nasm_free(i->path);
4415 nasm_free(i);
4416 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07004417 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004418}
4419
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004420void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004421{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004422 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004423
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004424 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004425 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004426 i->next = NULL;
4427
H. Peter Anvine2c80182005-01-15 22:15:51 +00004428 if (ipath != NULL) {
4429 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004430 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004431 j = j->next;
4432 j->next = i;
4433 } else {
4434 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004435 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004436}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004437
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004438void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004439{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004440 Token *inc, *space, *name;
4441 Line *l;
4442
H. Peter Anvin734b1882002-04-30 21:01:08 +00004443 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4444 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4445 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004446
4447 l = nasm_malloc(sizeof(Line));
4448 l->next = predef;
4449 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004450 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004451 predef = l;
4452}
4453
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004454void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004455{
4456 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004457 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004458 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004459
4460 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004461 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4462 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004463 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004464 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004465 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004466 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004467 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004468
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004469 l = nasm_malloc(sizeof(Line));
4470 l->next = predef;
4471 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004472 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004473 predef = l;
4474}
4475
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004476void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004477{
4478 Token *def, *space;
4479 Line *l;
4480
H. Peter Anvin734b1882002-04-30 21:01:08 +00004481 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4482 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004483 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004484
4485 l = nasm_malloc(sizeof(Line));
4486 l->next = predef;
4487 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004488 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004489 predef = l;
4490}
4491
Keith Kaniosb7a89542007-04-12 02:40:54 +00004492/*
4493 * Added by Keith Kanios:
4494 *
4495 * This function is used to assist with "runtime" preprocessor
4496 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4497 *
4498 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4499 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4500 */
4501
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004502void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004503{
4504 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004505
Keith Kaniosb7a89542007-04-12 02:40:54 +00004506 def = tokenize(definition);
4507 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4508 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004509
Keith Kaniosb7a89542007-04-12 02:40:54 +00004510}
4511
H. Peter Anvina70547f2008-07-19 21:44:26 -07004512void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004513{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004514 extrastdmac = macros;
4515}
4516
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004517static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004518{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004519 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004520 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004521 tok->text = nasm_strdup(numbuf);
4522 tok->type = TOK_NUMBER;
4523}
4524
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004525Preproc nasmpp = {
4526 pp_reset,
4527 pp_getline,
4528 pp_cleanup
4529};