blob: d8a96eb21328e587c4167328358abd67fd733a85 [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 Anvincda81632008-06-21 15:15:40 -0700371static const 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 Anvincda81632008-06-21 15:15:40 -0700377static const 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;
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001715 return j;
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/*
1805 * Decode a size directive
1806 */
1807static int parse_size(const char *str) {
1808 static const char *size_names[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001809 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001810 static const int sizes[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001811 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001812
1813 return sizes[bsii(str, size_names, elements(size_names))+1];
1814}
1815
Ed Beroset3ab3f412002-06-11 03:31:49 +00001816/**
1817 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001818 * Find out if a line contains a preprocessor directive, and deal
1819 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07001820 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001821 * If a directive _is_ found, it is the responsibility of this routine
1822 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001823 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001824 * @param tline a pointer to the current tokeninzed line linked list
1825 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07001826 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001827 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001828static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001829{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001830 enum preproc_token i;
1831 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07001832 bool err;
1833 int nparam;
1834 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001835 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001836 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001837 int offset;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001838 char *p, *pp, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001839 Include *inc;
1840 Context *ctx;
1841 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001842 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001843 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1844 Line *l;
1845 struct tokenval tokval;
1846 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001847 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001848 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001849 size_t len;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001850
1851 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001852
H. Peter Anvineba20a72002-04-30 20:53:55 +00001853 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07001854 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001855 (tline->text[1] == '%' || tline->text[1] == '$'
1856 || tline->text[1] == '!'))
1857 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001858
H. Peter Anvin4169a472007-09-12 01:29:43 +00001859 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001860
1861 /*
1862 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001863 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001864 * we should ignore all directives except for condition
1865 * directives.
1866 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001867 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001868 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1869 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001870 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001871
1872 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001873 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02001874 * ignore all directives except for %macro/%imacro (which nest),
1875 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
1876 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001877 */
1878 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001879 i != PP_ENDMACRO && i != PP_ENDM &&
1880 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1881 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001882 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001883
Charles Crayned4200be2008-07-12 16:42:33 -07001884 if (defining) {
1885 if (i == PP_MACRO || i == PP_IMACRO) {
1886 nested_mac_count++;
1887 return NO_DIRECTIVE_FOUND;
1888 } else if (nested_mac_count > 0) {
1889 if (i == PP_ENDMACRO) {
1890 nested_mac_count--;
1891 return NO_DIRECTIVE_FOUND;
1892 }
1893 }
1894 if (!defining->name) {
1895 if (i == PP_REP) {
1896 nested_rep_count++;
1897 return NO_DIRECTIVE_FOUND;
1898 } else if (nested_rep_count > 0) {
1899 if (i == PP_ENDREP) {
1900 nested_rep_count--;
1901 return NO_DIRECTIVE_FOUND;
1902 }
1903 }
1904 }
1905 }
1906
H. Peter Anvin4169a472007-09-12 01:29:43 +00001907 switch (i) {
1908 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001909 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1910 tline->text);
1911 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001912
H. Peter Anvine2c80182005-01-15 22:15:51 +00001913 case PP_STACKSIZE:
1914 /* Directive to tell NASM what the default stack size is. The
1915 * default is for a 16-bit stack, and this can be overriden with
1916 * %stacksize large.
1917 * the following form:
1918 *
1919 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1920 */
1921 tline = tline->next;
1922 if (tline && tline->type == TOK_WHITESPACE)
1923 tline = tline->next;
1924 if (!tline || tline->type != TOK_ID) {
1925 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1926 free_tlist(origline);
1927 return DIRECTIVE_FOUND;
1928 }
1929 if (nasm_stricmp(tline->text, "flat") == 0) {
1930 /* All subsequent ARG directives are for a 32-bit stack */
1931 StackSize = 4;
1932 StackPointer = "ebp";
1933 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001934 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08001935 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
1936 /* All subsequent ARG directives are for a 64-bit stack */
1937 StackSize = 8;
1938 StackPointer = "rbp";
1939 ArgOffset = 8;
1940 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001941 } else if (nasm_stricmp(tline->text, "large") == 0) {
1942 /* All subsequent ARG directives are for a 16-bit stack,
1943 * far function call.
1944 */
1945 StackSize = 2;
1946 StackPointer = "bp";
1947 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001948 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001949 } else if (nasm_stricmp(tline->text, "small") == 0) {
1950 /* All subsequent ARG directives are for a 16-bit stack,
1951 * far function call. We don't support near functions.
1952 */
1953 StackSize = 2;
1954 StackPointer = "bp";
1955 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001956 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001957 } else {
1958 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1959 free_tlist(origline);
1960 return DIRECTIVE_FOUND;
1961 }
1962 free_tlist(origline);
1963 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001964
H. Peter Anvine2c80182005-01-15 22:15:51 +00001965 case PP_ARG:
1966 /* TASM like ARG directive to define arguments to functions, in
1967 * the following form:
1968 *
1969 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1970 */
1971 offset = ArgOffset;
1972 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001973 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001974 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001975
H. Peter Anvine2c80182005-01-15 22:15:51 +00001976 /* Find the argument name */
1977 tline = tline->next;
1978 if (tline && tline->type == TOK_WHITESPACE)
1979 tline = tline->next;
1980 if (!tline || tline->type != TOK_ID) {
1981 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1982 free_tlist(origline);
1983 return DIRECTIVE_FOUND;
1984 }
1985 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001986
H. Peter Anvine2c80182005-01-15 22:15:51 +00001987 /* Find the argument size type */
1988 tline = tline->next;
1989 if (!tline || tline->type != TOK_OTHER
1990 || tline->text[0] != ':') {
1991 error(ERR_NONFATAL,
1992 "Syntax error processing `%%arg' directive");
1993 free_tlist(origline);
1994 return DIRECTIVE_FOUND;
1995 }
1996 tline = tline->next;
1997 if (!tline || tline->type != TOK_ID) {
1998 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1999 free_tlist(origline);
2000 return DIRECTIVE_FOUND;
2001 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002002
H. Peter Anvine2c80182005-01-15 22:15:51 +00002003 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002004 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002005 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002006 size = parse_size(tt->text);
2007 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002008 error(ERR_NONFATAL,
2009 "Invalid size type for `%%arg' missing directive");
2010 free_tlist(tt);
2011 free_tlist(origline);
2012 return DIRECTIVE_FOUND;
2013 }
2014 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002015
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002016 /* Round up to even stack slots */
2017 size = (size+StackSize-1) & ~(StackSize-1);
2018
H. Peter Anvine2c80182005-01-15 22:15:51 +00002019 /* Now define the macro for the argument */
2020 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2021 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002022 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002023 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002024
H. Peter Anvine2c80182005-01-15 22:15:51 +00002025 /* Move to the next argument in the list */
2026 tline = tline->next;
2027 if (tline && tline->type == TOK_WHITESPACE)
2028 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002029 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2030 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002031 free_tlist(origline);
2032 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002033
H. Peter Anvine2c80182005-01-15 22:15:51 +00002034 case PP_LOCAL:
2035 /* TASM like LOCAL directive to define local variables for a
2036 * function, in the following form:
2037 *
2038 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2039 *
2040 * The '= LocalSize' at the end is ignored by NASM, but is
2041 * required by TASM to define the local parameter size (and used
2042 * by the TASM macro package).
2043 */
2044 offset = LocalOffset;
2045 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002046 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002047 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002048
H. Peter Anvine2c80182005-01-15 22:15:51 +00002049 /* Find the argument name */
2050 tline = tline->next;
2051 if (tline && tline->type == TOK_WHITESPACE)
2052 tline = tline->next;
2053 if (!tline || tline->type != TOK_ID) {
2054 error(ERR_NONFATAL,
2055 "`%%local' missing argument parameter");
2056 free_tlist(origline);
2057 return DIRECTIVE_FOUND;
2058 }
2059 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002060
H. Peter Anvine2c80182005-01-15 22:15:51 +00002061 /* Find the argument size type */
2062 tline = tline->next;
2063 if (!tline || tline->type != TOK_OTHER
2064 || tline->text[0] != ':') {
2065 error(ERR_NONFATAL,
2066 "Syntax error processing `%%local' directive");
2067 free_tlist(origline);
2068 return DIRECTIVE_FOUND;
2069 }
2070 tline = tline->next;
2071 if (!tline || tline->type != TOK_ID) {
2072 error(ERR_NONFATAL,
2073 "`%%local' missing size type parameter");
2074 free_tlist(origline);
2075 return DIRECTIVE_FOUND;
2076 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002077
H. Peter Anvine2c80182005-01-15 22:15:51 +00002078 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002079 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002080 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002081 size = parse_size(tt->text);
2082 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002083 error(ERR_NONFATAL,
2084 "Invalid size type for `%%local' missing directive");
2085 free_tlist(tt);
2086 free_tlist(origline);
2087 return DIRECTIVE_FOUND;
2088 }
2089 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002090
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002091 /* Round up to even stack slots */
2092 size = (size+StackSize-1) & ~(StackSize-1);
2093
2094 offset += size; /* Negative offset, increment before */
2095
2096 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002097 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2098 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002099 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002100
H. Peter Anvine2c80182005-01-15 22:15:51 +00002101 /* Now define the assign to setup the enter_c macro correctly */
2102 snprintf(directive, sizeof(directive),
2103 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002104 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002105
H. Peter Anvine2c80182005-01-15 22:15:51 +00002106 /* Move to the next argument in the list */
2107 tline = tline->next;
2108 if (tline && tline->type == TOK_WHITESPACE)
2109 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002110 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2111 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002112 free_tlist(origline);
2113 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002114
H. Peter Anvine2c80182005-01-15 22:15:51 +00002115 case PP_CLEAR:
2116 if (tline->next)
2117 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002118 free_macros();
2119 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002120 free_tlist(origline);
2121 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002122
H. Peter Anvin418ca702008-05-30 10:42:30 -07002123 case PP_DEPEND:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002124 t = tline->next = expand_smacro(tline->next);
2125 skip_white_(t);
2126 if (!t || (t->type != TOK_STRING &&
2127 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002128 error(ERR_NONFATAL, "`%%depend' expects a file name");
2129 free_tlist(origline);
2130 return DIRECTIVE_FOUND; /* but we did _something_ */
2131 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002132 if (t->next)
H. Peter Anvin418ca702008-05-30 10:42:30 -07002133 error(ERR_WARNING,
2134 "trailing garbage after `%%depend' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002135 p = t->text;
2136 if (t->type != TOK_INTERNAL_STRING)
2137 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002138 if (dephead && !in_list(*dephead, p)) {
2139 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2140 sl->next = NULL;
2141 strcpy(sl->str, p);
2142 *deptail = sl;
2143 deptail = &sl->next;
2144 }
2145 free_tlist(origline);
2146 return DIRECTIVE_FOUND;
2147
2148 case PP_INCLUDE:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002149 t = tline->next = expand_smacro(tline->next);
2150 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002151
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002152 if (!t || (t->type != TOK_STRING &&
2153 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002154 error(ERR_NONFATAL, "`%%include' expects a file name");
2155 free_tlist(origline);
2156 return DIRECTIVE_FOUND; /* but we did _something_ */
2157 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002158 if (t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002159 error(ERR_WARNING,
2160 "trailing garbage after `%%include' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002161 p = t->text;
2162 if (t->type != TOK_INTERNAL_STRING)
2163 nasm_unquote(p, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002164 inc = nasm_malloc(sizeof(Include));
2165 inc->next = istk;
2166 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002167 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002168 if (!inc->fp) {
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002169 /* -MG given but file not found */
2170 nasm_free(inc);
2171 } else {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002172 inc->fname = src_set_fname(nasm_strdup(p));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002173 inc->lineno = src_set_linnum(0);
2174 inc->lineinc = 1;
2175 inc->expansion = NULL;
2176 inc->mstk = NULL;
2177 istk = inc;
2178 list->uplevel(LIST_INCLUDE);
2179 }
2180 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002181 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002182
H. Peter Anvind2456592008-06-19 15:04:18 -07002183 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002184 {
H. Peter Anvin7e50d232008-06-25 14:54:14 -07002185 static const macros_t *use_pkg;
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002186 const char *pkg_macro;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002187
H. Peter Anvind2456592008-06-19 15:04:18 -07002188 t = tline->next = expand_smacro(tline->next);
2189 skip_white_(t);
2190
2191 if (!t || (t->type != TOK_STRING &&
2192 t->type != TOK_INTERNAL_STRING &&
2193 t->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002194 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002195 free_tlist(origline);
2196 return DIRECTIVE_FOUND; /* but we did _something_ */
2197 }
2198 if (t->next)
2199 error(ERR_WARNING,
2200 "trailing garbage after `%%use' ignored");
H. Peter Anvind2456592008-06-19 15:04:18 -07002201 if (t->type == TOK_STRING)
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002202 nasm_unquote(t->text, NULL);
2203 use_pkg = nasm_stdmac_find_package(t->text);
2204 if (!use_pkg)
2205 error(ERR_NONFATAL, "unknown `%%use' package: %s", t->text);
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002206 /* The first string will be <%define>__USE_*__ */
H. Peter Anvin7e50d232008-06-25 14:54:14 -07002207 pkg_macro = (char *)use_pkg + 1;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002208 if (!smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2209 /* Not already included, go ahead and include it */
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002210 stdmacpos = use_pkg;
2211 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002212 free_tlist(origline);
2213 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002214 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002215 case PP_PUSH:
2216 tline = tline->next;
2217 skip_white_(tline);
2218 tline = expand_id(tline);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002219 if (tline) {
2220 if (!tok_type_(tline, TOK_ID)) {
2221 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2222 free_tlist(origline);
2223 return DIRECTIVE_FOUND; /* but we did _something_ */
2224 }
2225 if (tline->next)
2226 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2227 p = nasm_strdup(tline->text);
2228 } else {
2229 p = NULL; /* Anonymous context */
2230 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002231 ctx = nasm_malloc(sizeof(Context));
2232 ctx->next = cstk;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002233 hash_init(&ctx->localmac, HASH_SMALL);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002234 ctx->name = p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002235 ctx->number = unique++;
2236 cstk = ctx;
2237 free_tlist(origline);
2238 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002239
H. Peter Anvine2c80182005-01-15 22:15:51 +00002240 case PP_REPL:
2241 tline = tline->next;
2242 skip_white_(tline);
2243 tline = expand_id(tline);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002244 if (tline) {
2245 if (!tok_type_(tline, TOK_ID)) {
2246 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2247 free_tlist(origline);
2248 return DIRECTIVE_FOUND; /* but we did _something_ */
2249 }
2250 if (tline->next)
2251 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2252 p = nasm_strdup(tline->text);
2253 } else {
2254 p = NULL;
2255 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002256 if (!cstk)
2257 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2258 else {
2259 nasm_free(cstk->name);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002260 cstk->name = p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002261 }
2262 free_tlist(origline);
2263 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002264
H. Peter Anvine2c80182005-01-15 22:15:51 +00002265 case PP_POP:
2266 if (tline->next)
2267 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2268 if (!cstk)
2269 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2270 else
2271 ctx_pop();
2272 free_tlist(origline);
2273 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002274
H. Peter Anvine2c80182005-01-15 22:15:51 +00002275 case PP_ERROR:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002276 case PP_WARNING:
2277 {
H. Peter Anvin97b6d5b2008-07-13 15:05:53 -07002278 int severity = (i == PP_ERROR)
2279 ? ERR_NONFATAL|ERR_NO_SEVERITY
2280 : ERR_WARNING|ERR_NO_SEVERITY;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002281
H. Peter Anvine2c80182005-01-15 22:15:51 +00002282 tline->next = expand_smacro(tline->next);
2283 tline = tline->next;
2284 skip_white_(tline);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002285 t = tline ? tline->next : NULL;
2286 skip_white_(t);
2287 if (tok_type_(tline, TOK_STRING) && !t) {
2288 /* The line contains only a quoted string */
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002289 p = tline->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002290 nasm_unquote(p, NULL);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002291 error(severity, "%s: %s", pp_directives[i], p);
2292 } else {
2293 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002294 p = detoken(tline, false);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002295 error(severity, "%s: %s", pp_directives[i], p);
2296 nasm_free(p);
H. Peter Anvind2456592008-06-19 15:04:18 -07002297 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002298 free_tlist(origline);
2299 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002300 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002301
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002302 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002303 if (istk->conds && !emitting(istk->conds->state))
2304 j = COND_NEVER;
2305 else {
2306 j = if_condition(tline->next, i);
2307 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002308 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2309 }
2310 cond = nasm_malloc(sizeof(Cond));
2311 cond->next = istk->conds;
2312 cond->state = j;
2313 istk->conds = cond;
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002314 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002315 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002316
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002317 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002318 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002319 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002320 if (emitting(istk->conds->state)
2321 || istk->conds->state == COND_NEVER)
2322 istk->conds->state = COND_NEVER;
2323 else {
2324 /*
2325 * IMPORTANT: In the case of %if, we will already have
2326 * called expand_mmac_params(); however, if we're
2327 * processing an %elif we must have been in a
2328 * non-emitting mode, which would have inhibited
2329 * the normal invocation of expand_mmac_params(). Therefore,
2330 * we have to do it explicitly here.
2331 */
2332 j = if_condition(expand_mmac_params(tline->next), i);
2333 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002334 istk->conds->state =
2335 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2336 }
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002337 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002338 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002339
H. Peter Anvine2c80182005-01-15 22:15:51 +00002340 case PP_ELSE:
2341 if (tline->next)
2342 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2343 if (!istk->conds)
2344 error(ERR_FATAL, "`%%else': no matching `%%if'");
2345 if (emitting(istk->conds->state)
2346 || istk->conds->state == COND_NEVER)
2347 istk->conds->state = COND_ELSE_FALSE;
2348 else
2349 istk->conds->state = COND_ELSE_TRUE;
2350 free_tlist(origline);
2351 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002352
H. Peter Anvine2c80182005-01-15 22:15:51 +00002353 case PP_ENDIF:
2354 if (tline->next)
2355 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2356 if (!istk->conds)
2357 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2358 cond = istk->conds;
2359 istk->conds = cond->next;
2360 nasm_free(cond);
2361 free_tlist(origline);
2362 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002363
H. Peter Anvine2c80182005-01-15 22:15:51 +00002364 case PP_MACRO:
2365 case PP_IMACRO:
2366 if (defining)
2367 error(ERR_FATAL,
2368 "`%%%smacro': already defining a macro",
2369 (i == PP_IMACRO ? "i" : ""));
2370 tline = tline->next;
2371 skip_white_(tline);
2372 tline = expand_id(tline);
2373 if (!tok_type_(tline, TOK_ID)) {
2374 error(ERR_NONFATAL,
2375 "`%%%smacro' expects a macro name",
2376 (i == PP_IMACRO ? "i" : ""));
2377 return DIRECTIVE_FOUND;
2378 }
2379 defining = nasm_malloc(sizeof(MMacro));
2380 defining->name = nasm_strdup(tline->text);
2381 defining->casesense = (i == PP_MACRO);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002382 defining->plus = false;
2383 defining->nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002384 defining->in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002385 defining->rep_nest = NULL;
2386 tline = expand_smacro(tline->next);
2387 skip_white_(tline);
2388 if (!tok_type_(tline, TOK_NUMBER)) {
2389 error(ERR_NONFATAL,
2390 "`%%%smacro' expects a parameter count",
2391 (i == PP_IMACRO ? "i" : ""));
2392 defining->nparam_min = defining->nparam_max = 0;
2393 } else {
2394 defining->nparam_min = defining->nparam_max =
H. Peter Anvin70055962007-10-11 00:05:31 -07002395 readnum(tline->text, &err);
2396 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002397 error(ERR_NONFATAL,
2398 "unable to parse parameter count `%s'", tline->text);
2399 }
2400 if (tline && tok_is_(tline->next, "-")) {
2401 tline = tline->next->next;
2402 if (tok_is_(tline, "*"))
2403 defining->nparam_max = INT_MAX;
2404 else if (!tok_type_(tline, TOK_NUMBER))
2405 error(ERR_NONFATAL,
2406 "`%%%smacro' expects a parameter count after `-'",
2407 (i == PP_IMACRO ? "i" : ""));
2408 else {
H. Peter Anvin70055962007-10-11 00:05:31 -07002409 defining->nparam_max = readnum(tline->text, &err);
2410 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002411 error(ERR_NONFATAL,
2412 "unable to parse parameter count `%s'",
2413 tline->text);
2414 if (defining->nparam_min > defining->nparam_max)
2415 error(ERR_NONFATAL,
2416 "minimum parameter count exceeds maximum");
2417 }
2418 }
2419 if (tline && tok_is_(tline->next, "+")) {
2420 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002421 defining->plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002422 }
2423 if (tline && tok_type_(tline->next, TOK_ID) &&
2424 !nasm_stricmp(tline->next->text, ".nolist")) {
2425 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002426 defining->nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002427 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002428 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002429 while (mmac) {
2430 if (!strcmp(mmac->name, defining->name) &&
2431 (mmac->nparam_min <= defining->nparam_max
2432 || defining->plus)
2433 && (defining->nparam_min <= mmac->nparam_max
2434 || mmac->plus)) {
2435 error(ERR_WARNING,
2436 "redefining multi-line macro `%s'", defining->name);
2437 break;
2438 }
2439 mmac = mmac->next;
2440 }
2441 /*
2442 * Handle default parameters.
2443 */
2444 if (tline && tline->next) {
2445 defining->dlist = tline->next;
2446 tline->next = NULL;
2447 count_mmac_params(defining->dlist, &defining->ndefs,
2448 &defining->defaults);
2449 } else {
2450 defining->dlist = NULL;
2451 defining->defaults = NULL;
2452 }
2453 defining->expansion = NULL;
2454 free_tlist(origline);
2455 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002456
H. Peter Anvine2c80182005-01-15 22:15:51 +00002457 case PP_ENDM:
2458 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002459 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002460 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2461 return DIRECTIVE_FOUND;
2462 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002463 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002464 defining->next = *mmhead;
2465 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002466 defining = NULL;
2467 free_tlist(origline);
2468 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002469
H. Peter Anvine2c80182005-01-15 22:15:51 +00002470 case PP_ROTATE:
2471 if (tline->next && tline->next->type == TOK_WHITESPACE)
2472 tline = tline->next;
2473 if (tline->next == NULL) {
2474 free_tlist(origline);
2475 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2476 return DIRECTIVE_FOUND;
2477 }
2478 t = expand_smacro(tline->next);
2479 tline->next = NULL;
2480 free_tlist(origline);
2481 tline = t;
2482 tptr = &t;
2483 tokval.t_type = TOKEN_INVALID;
2484 evalresult =
2485 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2486 free_tlist(tline);
2487 if (!evalresult)
2488 return DIRECTIVE_FOUND;
2489 if (tokval.t_type)
2490 error(ERR_WARNING,
2491 "trailing garbage after expression ignored");
2492 if (!is_simple(evalresult)) {
2493 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2494 return DIRECTIVE_FOUND;
2495 }
2496 mmac = istk->mstk;
2497 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2498 mmac = mmac->next_active;
2499 if (!mmac) {
2500 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2501 } else if (mmac->nparam == 0) {
2502 error(ERR_NONFATAL,
2503 "`%%rotate' invoked within macro without parameters");
2504 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002505 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002506
H. Peter Anvin25a99342007-09-22 17:45:45 -07002507 rotate %= (int)mmac->nparam;
2508 if (rotate < 0)
2509 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002510
H. Peter Anvin25a99342007-09-22 17:45:45 -07002511 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002512 }
2513 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002514
H. Peter Anvine2c80182005-01-15 22:15:51 +00002515 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002516 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002517 do {
2518 tline = tline->next;
2519 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002520
H. Peter Anvine2c80182005-01-15 22:15:51 +00002521 if (tok_type_(tline, TOK_ID) &&
2522 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002523 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002524 do {
2525 tline = tline->next;
2526 } while (tok_type_(tline, TOK_WHITESPACE));
2527 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002528
H. Peter Anvine2c80182005-01-15 22:15:51 +00002529 if (tline) {
2530 t = expand_smacro(tline);
2531 tptr = &t;
2532 tokval.t_type = TOKEN_INVALID;
2533 evalresult =
2534 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2535 if (!evalresult) {
2536 free_tlist(origline);
2537 return DIRECTIVE_FOUND;
2538 }
2539 if (tokval.t_type)
2540 error(ERR_WARNING,
2541 "trailing garbage after expression ignored");
2542 if (!is_simple(evalresult)) {
2543 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2544 return DIRECTIVE_FOUND;
2545 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002546 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002547 } else {
2548 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002549 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002550 }
2551 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002552
H. Peter Anvine2c80182005-01-15 22:15:51 +00002553 tmp_defining = defining;
2554 defining = nasm_malloc(sizeof(MMacro));
2555 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002556 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002557 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002558 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002559 defining->in_progress = count;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002560 defining->nparam_min = defining->nparam_max = 0;
2561 defining->defaults = NULL;
2562 defining->dlist = NULL;
2563 defining->expansion = NULL;
2564 defining->next_active = istk->mstk;
2565 defining->rep_nest = tmp_defining;
2566 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002567
H. Peter Anvine2c80182005-01-15 22:15:51 +00002568 case PP_ENDREP:
2569 if (!defining || defining->name) {
2570 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2571 return DIRECTIVE_FOUND;
2572 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002573
H. Peter Anvine2c80182005-01-15 22:15:51 +00002574 /*
2575 * Now we have a "macro" defined - although it has no name
2576 * and we won't be entering it in the hash tables - we must
2577 * push a macro-end marker for it on to istk->expansion.
2578 * After that, it will take care of propagating itself (a
2579 * macro-end marker line for a macro which is really a %rep
2580 * block will cause the macro to be re-expanded, complete
2581 * with another macro-end marker to ensure the process
2582 * continues) until the whole expansion is forcibly removed
2583 * from istk->expansion by a %exitrep.
2584 */
2585 l = nasm_malloc(sizeof(Line));
2586 l->next = istk->expansion;
2587 l->finishes = defining;
2588 l->first = NULL;
2589 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002590
H. Peter Anvine2c80182005-01-15 22:15:51 +00002591 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002592
H. Peter Anvine2c80182005-01-15 22:15:51 +00002593 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2594 tmp_defining = defining;
2595 defining = defining->rep_nest;
2596 free_tlist(origline);
2597 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002598
H. Peter Anvine2c80182005-01-15 22:15:51 +00002599 case PP_EXITREP:
2600 /*
2601 * We must search along istk->expansion until we hit a
2602 * macro-end marker for a macro with no name. Then we set
2603 * its `in_progress' flag to 0.
2604 */
2605 for (l = istk->expansion; l; l = l->next)
2606 if (l->finishes && !l->finishes->name)
2607 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002608
H. Peter Anvine2c80182005-01-15 22:15:51 +00002609 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07002610 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002611 else
2612 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2613 free_tlist(origline);
2614 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002615
H. Peter Anvine2c80182005-01-15 22:15:51 +00002616 case PP_XDEFINE:
2617 case PP_IXDEFINE:
2618 case PP_DEFINE:
2619 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002620 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002621
H. Peter Anvine2c80182005-01-15 22:15:51 +00002622 tline = tline->next;
2623 skip_white_(tline);
2624 tline = expand_id(tline);
2625 if (!tline || (tline->type != TOK_ID &&
2626 (tline->type != TOK_PREPROC_ID ||
2627 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002628 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2629 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002630 free_tlist(origline);
2631 return DIRECTIVE_FOUND;
2632 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002633
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002634 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002635
H. Peter Anvine2c80182005-01-15 22:15:51 +00002636 mname = tline->text;
2637 last = tline;
2638 param_start = tline = tline->next;
2639 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002640
H. Peter Anvine2c80182005-01-15 22:15:51 +00002641 /* Expand the macro definition now for %xdefine and %ixdefine */
2642 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2643 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002644
H. Peter Anvine2c80182005-01-15 22:15:51 +00002645 if (tok_is_(tline, "(")) {
2646 /*
2647 * This macro has parameters.
2648 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002649
H. Peter Anvine2c80182005-01-15 22:15:51 +00002650 tline = tline->next;
2651 while (1) {
2652 skip_white_(tline);
2653 if (!tline) {
2654 error(ERR_NONFATAL, "parameter identifier expected");
2655 free_tlist(origline);
2656 return DIRECTIVE_FOUND;
2657 }
2658 if (tline->type != TOK_ID) {
2659 error(ERR_NONFATAL,
2660 "`%s': parameter identifier expected",
2661 tline->text);
2662 free_tlist(origline);
2663 return DIRECTIVE_FOUND;
2664 }
2665 tline->type = TOK_SMAC_PARAM + nparam++;
2666 tline = tline->next;
2667 skip_white_(tline);
2668 if (tok_is_(tline, ",")) {
2669 tline = tline->next;
2670 continue;
2671 }
2672 if (!tok_is_(tline, ")")) {
2673 error(ERR_NONFATAL,
2674 "`)' expected to terminate macro template");
2675 free_tlist(origline);
2676 return DIRECTIVE_FOUND;
2677 }
2678 break;
2679 }
2680 last = tline;
2681 tline = tline->next;
2682 }
2683 if (tok_type_(tline, TOK_WHITESPACE))
2684 last = tline, tline = tline->next;
2685 macro_start = NULL;
2686 last->next = NULL;
2687 t = tline;
2688 while (t) {
2689 if (t->type == TOK_ID) {
2690 for (tt = param_start; tt; tt = tt->next)
2691 if (tt->type >= TOK_SMAC_PARAM &&
2692 !strcmp(tt->text, t->text))
2693 t->type = tt->type;
2694 }
2695 tt = t->next;
2696 t->next = macro_start;
2697 macro_start = t;
2698 t = tt;
2699 }
2700 /*
2701 * Good. We now have a macro name, a parameter count, and a
2702 * token list (in reverse order) for an expansion. We ought
2703 * to be OK just to create an SMacro, store it, and let
2704 * free_tlist have the rest of the line (which we have
2705 * carefully re-terminated after chopping off the expansion
2706 * from the end).
2707 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002708 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002709 free_tlist(origline);
2710 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002711
H. Peter Anvine2c80182005-01-15 22:15:51 +00002712 case PP_UNDEF:
2713 tline = tline->next;
2714 skip_white_(tline);
2715 tline = expand_id(tline);
2716 if (!tline || (tline->type != TOK_ID &&
2717 (tline->type != TOK_PREPROC_ID ||
2718 tline->text[1] != '$'))) {
2719 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2720 free_tlist(origline);
2721 return DIRECTIVE_FOUND;
2722 }
2723 if (tline->next) {
2724 error(ERR_WARNING,
2725 "trailing garbage after macro name ignored");
2726 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002727
H. Peter Anvine2c80182005-01-15 22:15:51 +00002728 /* Find the context that symbol belongs to */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002729 ctx = get_ctx(tline->text, false);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002730 undef_smacro(ctx, tline->text);
2731 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002732 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002733
H. Peter Anvin9e200162008-06-04 17:23:14 -07002734 case PP_DEFSTR:
2735 case PP_IDEFSTR:
2736 casesense = (i == PP_DEFSTR);
2737
2738 tline = tline->next;
2739 skip_white_(tline);
2740 tline = expand_id(tline);
2741 if (!tline || (tline->type != TOK_ID &&
2742 (tline->type != TOK_PREPROC_ID ||
2743 tline->text[1] != '$'))) {
2744 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2745 pp_directives[i]);
2746 free_tlist(origline);
2747 return DIRECTIVE_FOUND;
2748 }
2749
2750 ctx = get_ctx(tline->text, false);
2751
2752 mname = tline->text;
2753 last = tline;
2754 tline = expand_smacro(tline->next);
2755 last->next = NULL;
2756
2757 while (tok_type_(tline, TOK_WHITESPACE))
2758 tline = delete_Token(tline);
2759
2760 p = detoken(tline, false);
2761 macro_start = nasm_malloc(sizeof(*macro_start));
2762 macro_start->next = NULL;
2763 macro_start->text = nasm_quote(p, strlen(p));
2764 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002765 macro_start->a.mac = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07002766 nasm_free(p);
2767
2768 /*
2769 * We now have a macro name, an implicit parameter count of
2770 * zero, and a string token to use as an expansion. Create
2771 * and store an SMacro.
2772 */
2773 define_smacro(ctx, mname, casesense, 0, macro_start);
2774 free_tlist(origline);
2775 return DIRECTIVE_FOUND;
2776
H. Peter Anvin418ca702008-05-30 10:42:30 -07002777 case PP_PATHSEARCH:
2778 {
2779 FILE *fp;
2780 StrList *xsl = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002781 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002782
2783 casesense = true;
2784
2785 tline = tline->next;
2786 skip_white_(tline);
2787 tline = expand_id(tline);
2788 if (!tline || (tline->type != TOK_ID &&
2789 (tline->type != TOK_PREPROC_ID ||
2790 tline->text[1] != '$'))) {
2791 error(ERR_NONFATAL,
2792 "`%%pathsearch' expects a macro identifier as first parameter");
2793 free_tlist(origline);
2794 return DIRECTIVE_FOUND;
2795 }
2796 ctx = get_ctx(tline->text, false);
2797
2798 mname = tline->text;
2799 last = tline;
2800 tline = expand_smacro(tline->next);
2801 last->next = NULL;
2802
2803 t = tline;
2804 while (tok_type_(t, TOK_WHITESPACE))
2805 t = t->next;
2806
2807 if (!t || (t->type != TOK_STRING &&
2808 t->type != TOK_INTERNAL_STRING)) {
2809 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
2810 free_tlist(tline);
2811 free_tlist(origline);
2812 return DIRECTIVE_FOUND; /* but we did _something_ */
2813 }
2814 if (t->next)
2815 error(ERR_WARNING,
2816 "trailing garbage after `%%pathsearch' ignored");
H. Peter Anvin427cc912008-06-01 21:43:03 -07002817 p = t->text;
2818 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002819 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002820
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002821 fp = inc_fopen(p, &xsl, &xst, true);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002822 if (fp) {
2823 p = xsl->str;
2824 fclose(fp); /* Don't actually care about the file */
2825 }
2826 macro_start = nasm_malloc(sizeof(*macro_start));
2827 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002828 macro_start->text = nasm_quote(p, strlen(p));
H. Peter Anvin418ca702008-05-30 10:42:30 -07002829 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002830 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002831 if (xsl)
2832 nasm_free(xsl);
2833
2834 /*
2835 * We now have a macro name, an implicit parameter count of
2836 * zero, and a string token to use as an expansion. Create
2837 * and store an SMacro.
2838 */
2839 define_smacro(ctx, mname, casesense, 0, macro_start);
2840 free_tlist(tline);
2841 free_tlist(origline);
2842 return DIRECTIVE_FOUND;
2843 }
2844
H. Peter Anvine2c80182005-01-15 22:15:51 +00002845 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002846 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07002847
H. Peter Anvine2c80182005-01-15 22:15:51 +00002848 tline = tline->next;
2849 skip_white_(tline);
2850 tline = expand_id(tline);
2851 if (!tline || (tline->type != TOK_ID &&
2852 (tline->type != TOK_PREPROC_ID ||
2853 tline->text[1] != '$'))) {
2854 error(ERR_NONFATAL,
2855 "`%%strlen' expects a macro identifier as first parameter");
2856 free_tlist(origline);
2857 return DIRECTIVE_FOUND;
2858 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002859 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002860
H. Peter Anvine2c80182005-01-15 22:15:51 +00002861 mname = tline->text;
2862 last = tline;
2863 tline = expand_smacro(tline->next);
2864 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002865
H. Peter Anvine2c80182005-01-15 22:15:51 +00002866 t = tline;
2867 while (tok_type_(t, TOK_WHITESPACE))
2868 t = t->next;
2869 /* t should now point to the string */
2870 if (t->type != TOK_STRING) {
2871 error(ERR_NONFATAL,
2872 "`%%strlen` requires string as second parameter");
2873 free_tlist(tline);
2874 free_tlist(origline);
2875 return DIRECTIVE_FOUND;
2876 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002877
H. Peter Anvine2c80182005-01-15 22:15:51 +00002878 macro_start = nasm_malloc(sizeof(*macro_start));
2879 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002880 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002881 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002882
H. Peter Anvine2c80182005-01-15 22:15:51 +00002883 /*
2884 * We now have a macro name, an implicit parameter count of
2885 * zero, and a numeric token to use as an expansion. Create
2886 * and store an SMacro.
2887 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002888 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002889 free_tlist(tline);
2890 free_tlist(origline);
2891 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002892
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002893 case PP_STRCAT:
2894 casesense = true;
2895
2896 tline = tline->next;
2897 skip_white_(tline);
2898 tline = expand_id(tline);
2899 if (!tline || (tline->type != TOK_ID &&
2900 (tline->type != TOK_PREPROC_ID ||
2901 tline->text[1] != '$'))) {
2902 error(ERR_NONFATAL,
2903 "`%%strcat' expects a macro identifier as first parameter");
2904 free_tlist(origline);
2905 return DIRECTIVE_FOUND;
2906 }
2907 ctx = get_ctx(tline->text, false);
2908
2909 mname = tline->text;
2910 last = tline;
2911 tline = expand_smacro(tline->next);
2912 last->next = NULL;
2913
2914 len = 0;
2915 for (t = tline; t; t = t->next) {
2916 switch (t->type) {
2917 case TOK_WHITESPACE:
2918 break;
2919 case TOK_STRING:
2920 len += t->a.len = nasm_unquote(t->text, NULL);
2921 break;
H. Peter Anvinaccf4332008-07-01 21:42:08 -07002922 case TOK_OTHER:
2923 if (!strcmp(t->text, ",")) /* permit comma separators */
2924 break;
2925 /* else fall through */
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002926 default:
2927 error(ERR_NONFATAL,
2928 "non-string passed to `%%strcat' (%d)", t->type);
2929 free_tlist(tline);
2930 free_tlist(origline);
2931 return DIRECTIVE_FOUND;
2932 }
2933 }
2934
2935 p = pp = nasm_malloc(len);
2936 t = tline;
2937 for (t = tline; t; t = t->next) {
2938 if (t->type == TOK_STRING) {
2939 memcpy(p, t->text, t->a.len);
2940 p += t->a.len;
2941 }
2942 }
2943
2944 /*
2945 * We now have a macro name, an implicit parameter count of
2946 * zero, and a numeric token to use as an expansion. Create
2947 * and store an SMacro.
2948 */
2949 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
2950 macro_start->text = nasm_quote(pp, len);
2951 nasm_free(pp);
2952 define_smacro(ctx, mname, casesense, 0, macro_start);
2953 free_tlist(tline);
2954 free_tlist(origline);
2955 return DIRECTIVE_FOUND;
2956
H. Peter Anvine2c80182005-01-15 22:15:51 +00002957 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002958 {
2959 int64_t a1, a2;
2960 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07002961
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002962 casesense = true;
2963
H. Peter Anvine2c80182005-01-15 22:15:51 +00002964 tline = tline->next;
2965 skip_white_(tline);
2966 tline = expand_id(tline);
2967 if (!tline || (tline->type != TOK_ID &&
2968 (tline->type != TOK_PREPROC_ID ||
2969 tline->text[1] != '$'))) {
2970 error(ERR_NONFATAL,
2971 "`%%substr' expects a macro identifier as first parameter");
2972 free_tlist(origline);
2973 return DIRECTIVE_FOUND;
2974 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002975 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002976
H. Peter Anvine2c80182005-01-15 22:15:51 +00002977 mname = tline->text;
2978 last = tline;
2979 tline = expand_smacro(tline->next);
2980 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002981
H. Peter Anvine2c80182005-01-15 22:15:51 +00002982 t = tline->next;
2983 while (tok_type_(t, TOK_WHITESPACE))
2984 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002985
H. Peter Anvine2c80182005-01-15 22:15:51 +00002986 /* t should now point to the string */
2987 if (t->type != TOK_STRING) {
2988 error(ERR_NONFATAL,
2989 "`%%substr` requires string as second parameter");
2990 free_tlist(tline);
2991 free_tlist(origline);
2992 return DIRECTIVE_FOUND;
2993 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002994
H. Peter Anvine2c80182005-01-15 22:15:51 +00002995 tt = t->next;
2996 tptr = &tt;
2997 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002998 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
2999 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003000 if (!evalresult) {
3001 free_tlist(tline);
3002 free_tlist(origline);
3003 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003004 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003005 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3006 free_tlist(tline);
3007 free_tlist(origline);
3008 return DIRECTIVE_FOUND;
3009 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003010 a1 = evalresult->value-1;
3011
3012 while (tok_type_(tt, TOK_WHITESPACE))
3013 tt = tt->next;
3014 if (!tt) {
3015 a2 = 1; /* Backwards compatibility: one character */
3016 } else {
3017 tokval.t_type = TOKEN_INVALID;
3018 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3019 pass, error, NULL);
3020 if (!evalresult) {
3021 free_tlist(tline);
3022 free_tlist(origline);
3023 return DIRECTIVE_FOUND;
3024 } else if (!is_simple(evalresult)) {
3025 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3026 free_tlist(tline);
3027 free_tlist(origline);
3028 return DIRECTIVE_FOUND;
3029 }
3030 a2 = evalresult->value;
3031 }
3032
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003033 len = nasm_unquote(t->text, NULL);
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003034 if (a2 < 0)
3035 a2 = a2+1+len-a1;
3036 if (a1+a2 > (int64_t)len)
3037 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003038
H. Peter Anvine2c80182005-01-15 22:15:51 +00003039 macro_start = nasm_malloc(sizeof(*macro_start));
3040 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003041 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003042 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003043 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003044
H. Peter Anvine2c80182005-01-15 22:15:51 +00003045 /*
3046 * We now have a macro name, an implicit parameter count of
3047 * zero, and a numeric token to use as an expansion. Create
3048 * and store an SMacro.
3049 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003050 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003051 free_tlist(tline);
3052 free_tlist(origline);
3053 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003054 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003055
H. Peter Anvine2c80182005-01-15 22:15:51 +00003056 case PP_ASSIGN:
3057 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003058 casesense = (i == PP_ASSIGN);
3059
H. Peter Anvine2c80182005-01-15 22:15:51 +00003060 tline = tline->next;
3061 skip_white_(tline);
3062 tline = expand_id(tline);
3063 if (!tline || (tline->type != TOK_ID &&
3064 (tline->type != TOK_PREPROC_ID ||
3065 tline->text[1] != '$'))) {
3066 error(ERR_NONFATAL,
3067 "`%%%sassign' expects a macro identifier",
3068 (i == PP_IASSIGN ? "i" : ""));
3069 free_tlist(origline);
3070 return DIRECTIVE_FOUND;
3071 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003072 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003073
H. Peter Anvine2c80182005-01-15 22:15:51 +00003074 mname = tline->text;
3075 last = tline;
3076 tline = expand_smacro(tline->next);
3077 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003078
H. Peter Anvine2c80182005-01-15 22:15:51 +00003079 t = tline;
3080 tptr = &t;
3081 tokval.t_type = TOKEN_INVALID;
3082 evalresult =
3083 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3084 free_tlist(tline);
3085 if (!evalresult) {
3086 free_tlist(origline);
3087 return DIRECTIVE_FOUND;
3088 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003089
H. Peter Anvine2c80182005-01-15 22:15:51 +00003090 if (tokval.t_type)
3091 error(ERR_WARNING,
3092 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003093
H. Peter Anvine2c80182005-01-15 22:15:51 +00003094 if (!is_simple(evalresult)) {
3095 error(ERR_NONFATAL,
3096 "non-constant value given to `%%%sassign'",
3097 (i == PP_IASSIGN ? "i" : ""));
3098 free_tlist(origline);
3099 return DIRECTIVE_FOUND;
3100 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003101
H. Peter Anvine2c80182005-01-15 22:15:51 +00003102 macro_start = nasm_malloc(sizeof(*macro_start));
3103 macro_start->next = NULL;
3104 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003105 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003106
H. Peter Anvine2c80182005-01-15 22:15:51 +00003107 /*
3108 * We now have a macro name, an implicit parameter count of
3109 * zero, and a numeric token to use as an expansion. Create
3110 * and store an SMacro.
3111 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003112 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003113 free_tlist(origline);
3114 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003115
H. Peter Anvine2c80182005-01-15 22:15:51 +00003116 case PP_LINE:
3117 /*
3118 * Syntax is `%line nnn[+mmm] [filename]'
3119 */
3120 tline = tline->next;
3121 skip_white_(tline);
3122 if (!tok_type_(tline, TOK_NUMBER)) {
3123 error(ERR_NONFATAL, "`%%line' expects line number");
3124 free_tlist(origline);
3125 return DIRECTIVE_FOUND;
3126 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003127 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003128 m = 1;
3129 tline = tline->next;
3130 if (tok_is_(tline, "+")) {
3131 tline = tline->next;
3132 if (!tok_type_(tline, TOK_NUMBER)) {
3133 error(ERR_NONFATAL, "`%%line' expects line increment");
3134 free_tlist(origline);
3135 return DIRECTIVE_FOUND;
3136 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003137 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003138 tline = tline->next;
3139 }
3140 skip_white_(tline);
3141 src_set_linnum(k);
3142 istk->lineinc = m;
3143 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003144 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003145 }
3146 free_tlist(origline);
3147 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003148
H. Peter Anvine2c80182005-01-15 22:15:51 +00003149 default:
3150 error(ERR_FATAL,
3151 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003152 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003153 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003154 }
Ed Beroset3ab3f412002-06-11 03:31:49 +00003155 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003156}
3157
3158/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003159 * Ensure that a macro parameter contains a condition code and
3160 * nothing else. Return the condition code index if so, or -1
3161 * otherwise.
3162 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003163static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003164{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003165 Token *tt;
3166 int i, j, k, m;
3167
H. Peter Anvin25a99342007-09-22 17:45:45 -07003168 if (!t)
3169 return -1; /* Probably a %+ without a space */
3170
H. Peter Anvineba20a72002-04-30 20:53:55 +00003171 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003172 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003173 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003174 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003175 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003176 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003177 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003178
3179 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00003180 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003181 while (j - i > 1) {
3182 k = (j + i) / 2;
3183 m = nasm_stricmp(t->text, conditions[k]);
3184 if (m == 0) {
3185 i = k;
3186 j = -2;
3187 break;
3188 } else if (m < 0) {
3189 j = k;
3190 } else
3191 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003192 }
3193 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003194 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003195 return i;
3196}
3197
3198/*
3199 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3200 * %-n) and MMacro-local identifiers (%%foo).
3201 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003202static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003203{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003204 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003205
3206 tail = &thead;
3207 thead = NULL;
3208
H. Peter Anvine2c80182005-01-15 22:15:51 +00003209 while (tline) {
3210 if (tline->type == TOK_PREPROC_ID &&
3211 (((tline->text[1] == '+' || tline->text[1] == '-')
3212 && tline->text[2]) || tline->text[1] == '%'
3213 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003214 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003215 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003216 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003217 unsigned int n;
3218 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003219 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003220
H. Peter Anvine2c80182005-01-15 22:15:51 +00003221 t = tline;
3222 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003223
H. Peter Anvine2c80182005-01-15 22:15:51 +00003224 mac = istk->mstk;
3225 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3226 mac = mac->next_active;
3227 if (!mac)
3228 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3229 else
3230 switch (t->text[1]) {
3231 /*
3232 * We have to make a substitution of one of the
3233 * forms %1, %-1, %+1, %%foo, %0.
3234 */
3235 case '0':
3236 type = TOK_NUMBER;
3237 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3238 text = nasm_strdup(tmpbuf);
3239 break;
3240 case '%':
3241 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003242 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00003243 mac->unique);
3244 text = nasm_strcat(tmpbuf, t->text + 2);
3245 break;
3246 case '-':
3247 n = atoi(t->text + 2) - 1;
3248 if (n >= mac->nparam)
3249 tt = NULL;
3250 else {
3251 if (mac->nparam > 1)
3252 n = (n + mac->rotate) % mac->nparam;
3253 tt = mac->params[n];
3254 }
3255 cc = find_cc(tt);
3256 if (cc == -1) {
3257 error(ERR_NONFATAL,
3258 "macro parameter %d is not a condition code",
3259 n + 1);
3260 text = NULL;
3261 } else {
3262 type = TOK_ID;
3263 if (inverse_ccs[cc] == -1) {
3264 error(ERR_NONFATAL,
3265 "condition code `%s' is not invertible",
3266 conditions[cc]);
3267 text = NULL;
3268 } else
3269 text =
3270 nasm_strdup(conditions[inverse_ccs[cc]]);
3271 }
3272 break;
3273 case '+':
3274 n = atoi(t->text + 2) - 1;
3275 if (n >= mac->nparam)
3276 tt = NULL;
3277 else {
3278 if (mac->nparam > 1)
3279 n = (n + mac->rotate) % mac->nparam;
3280 tt = mac->params[n];
3281 }
3282 cc = find_cc(tt);
3283 if (cc == -1) {
3284 error(ERR_NONFATAL,
3285 "macro parameter %d is not a condition code",
3286 n + 1);
3287 text = NULL;
3288 } else {
3289 type = TOK_ID;
3290 text = nasm_strdup(conditions[cc]);
3291 }
3292 break;
3293 default:
3294 n = atoi(t->text + 1) - 1;
3295 if (n >= mac->nparam)
3296 tt = NULL;
3297 else {
3298 if (mac->nparam > 1)
3299 n = (n + mac->rotate) % mac->nparam;
3300 tt = mac->params[n];
3301 }
3302 if (tt) {
3303 for (i = 0; i < mac->paramlen[n]; i++) {
3304 *tail = new_Token(NULL, tt->type, tt->text, 0);
3305 tail = &(*tail)->next;
3306 tt = tt->next;
3307 }
3308 }
3309 text = NULL; /* we've done it here */
3310 break;
3311 }
3312 if (!text) {
3313 delete_Token(t);
3314 } else {
3315 *tail = t;
3316 tail = &t->next;
3317 t->type = type;
3318 nasm_free(t->text);
3319 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003320 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003321 }
3322 continue;
3323 } else {
3324 t = *tail = tline;
3325 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003326 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003327 tail = &t->next;
3328 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003329 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003330 *tail = NULL;
3331 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003332 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003333 switch (t->type) {
3334 case TOK_WHITESPACE:
3335 if (tt->type == TOK_WHITESPACE) {
3336 t->next = delete_Token(tt);
3337 }
3338 break;
3339 case TOK_ID:
3340 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003341 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003342 nasm_free(t->text);
3343 t->text = tmp;
3344 t->next = delete_Token(tt);
3345 }
3346 break;
3347 case TOK_NUMBER:
3348 if (tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003349 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003350 nasm_free(t->text);
3351 t->text = tmp;
3352 t->next = delete_Token(tt);
3353 }
3354 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003355 default:
3356 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003357 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003358
H. Peter Anvin76690a12002-04-30 20:52:49 +00003359 return thead;
3360}
3361
3362/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003363 * Expand all single-line macro calls made in the given line.
3364 * Return the expanded version of the line. The original is deemed
3365 * to be destroyed in the process. (In reality we'll just move
3366 * Tokens from input to output a lot of the time, rather than
3367 * actually bothering to destroy and replicate.)
3368 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003369#define DEADMAN_LIMIT (1 << 20)
3370
H. Peter Anvine2c80182005-01-15 22:15:51 +00003371static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003372{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003373 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003374 struct hash_table *smtbl;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003375 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003376 Token **params;
3377 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003378 unsigned int nparam, sparam;
3379 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003380 Token *org_tline = tline;
3381 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003382 char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003383 int deadman = DEADMAN_LIMIT;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003384
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003385 /*
3386 * Trick: we should avoid changing the start token pointer since it can
3387 * be contained in "next" field of other token. Because of this
3388 * we allocate a copy of first token and work with it; at the end of
3389 * routine we copy it back
3390 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003391 if (org_tline) {
3392 tline =
3393 new_Token(org_tline->next, org_tline->type, org_tline->text,
3394 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003395 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003396 nasm_free(org_tline->text);
3397 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003398 }
3399
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003400again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003401 tail = &thead;
3402 thead = NULL;
3403
H. Peter Anvine2c80182005-01-15 22:15:51 +00003404 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003405 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003406 error(ERR_NONFATAL, "interminable macro recursion");
3407 break;
3408 }
3409
H. Peter Anvine2c80182005-01-15 22:15:51 +00003410 if ((mname = tline->text)) {
3411 /* if this token is a local macro, look in local context */
H. Peter Anvinb037a672008-05-29 19:08:08 -07003412 ctx = NULL;
3413 smtbl = &smacros;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003414 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003415 ctx = get_ctx(mname, true);
H. Peter Anvinb037a672008-05-29 19:08:08 -07003416 if (ctx)
3417 smtbl = &ctx->localmac;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003418 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07003419 head = (SMacro *) hash_findix(smtbl, mname);
H. Peter Anvin072771e2008-05-22 13:17:51 -07003420
H. Peter Anvine2c80182005-01-15 22:15:51 +00003421 /*
3422 * We've hit an identifier. As in is_mmacro below, we first
3423 * check whether the identifier is a single-line macro at
3424 * all, then think about checking for parameters if
3425 * necessary.
3426 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003427 for (m = head; m; m = m->next)
3428 if (!mstrcmp(m->name, mname, m->casesense))
3429 break;
3430 if (m) {
3431 mstart = tline;
3432 params = NULL;
3433 paramsize = NULL;
3434 if (m->nparam == 0) {
3435 /*
3436 * Simple case: the macro is parameterless. Discard the
3437 * one token that the macro call took, and push the
3438 * expansion back on the to-do stack.
3439 */
3440 if (!m->expansion) {
3441 if (!strcmp("__FILE__", m->name)) {
3442 int32_t num = 0;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003443 char *file;
3444 src_get(&num, &file);
3445 tline->text = nasm_quote(file, strlen(file));
H. Peter Anvin97a23472007-09-16 17:57:25 -07003446 tline->type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003447 nasm_free(file);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003448 continue;
3449 }
3450 if (!strcmp("__LINE__", m->name)) {
3451 nasm_free(tline->text);
3452 make_tok_num(tline, src_get_linnum());
3453 continue;
3454 }
3455 if (!strcmp("__BITS__", m->name)) {
3456 nasm_free(tline->text);
3457 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003458 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003459 }
3460 tline = delete_Token(tline);
3461 continue;
3462 }
3463 } else {
3464 /*
3465 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003466 * exists and takes parameters. We must find the
3467 * parameters in the call, count them, find the SMacro
3468 * that corresponds to that form of the macro call, and
3469 * substitute for the parameters when we expand. What a
3470 * pain.
3471 */
3472 /*tline = tline->next;
3473 skip_white_(tline); */
3474 do {
3475 t = tline->next;
3476 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003477 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003478 t->text = NULL;
3479 t = tline->next = delete_Token(t);
3480 }
3481 tline = t;
3482 } while (tok_type_(tline, TOK_WHITESPACE));
3483 if (!tok_is_(tline, "(")) {
3484 /*
3485 * This macro wasn't called with parameters: ignore
3486 * the call. (Behaviour borrowed from gnu cpp.)
3487 */
3488 tline = mstart;
3489 m = NULL;
3490 } else {
3491 int paren = 0;
3492 int white = 0;
3493 brackets = 0;
3494 nparam = 0;
3495 sparam = PARAM_DELTA;
3496 params = nasm_malloc(sparam * sizeof(Token *));
3497 params[0] = tline->next;
3498 paramsize = nasm_malloc(sparam * sizeof(int));
3499 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003500 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003501 /*
3502 * For some unusual expansions
3503 * which concatenates function call
3504 */
3505 t = tline->next;
3506 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003507 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003508 t->text = NULL;
3509 t = tline->next = delete_Token(t);
3510 }
3511 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003512
H. Peter Anvine2c80182005-01-15 22:15:51 +00003513 if (!tline) {
3514 error(ERR_NONFATAL,
3515 "macro call expects terminating `)'");
3516 break;
3517 }
3518 if (tline->type == TOK_WHITESPACE
3519 && brackets <= 0) {
3520 if (paramsize[nparam])
3521 white++;
3522 else
3523 params[nparam] = tline->next;
3524 continue; /* parameter loop */
3525 }
3526 if (tline->type == TOK_OTHER
3527 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003528 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003529 if (ch == ',' && !paren && brackets <= 0) {
3530 if (++nparam >= sparam) {
3531 sparam += PARAM_DELTA;
3532 params = nasm_realloc(params,
3533 sparam *
3534 sizeof(Token
3535 *));
3536 paramsize =
3537 nasm_realloc(paramsize,
3538 sparam *
3539 sizeof(int));
3540 }
3541 params[nparam] = tline->next;
3542 paramsize[nparam] = 0;
3543 white = 0;
3544 continue; /* parameter loop */
3545 }
3546 if (ch == '{' &&
3547 (brackets > 0 || (brackets == 0 &&
3548 !paramsize[nparam])))
3549 {
3550 if (!(brackets++)) {
3551 params[nparam] = tline->next;
3552 continue; /* parameter loop */
3553 }
3554 }
3555 if (ch == '}' && brackets > 0)
3556 if (--brackets == 0) {
3557 brackets = -1;
3558 continue; /* parameter loop */
3559 }
3560 if (ch == '(' && !brackets)
3561 paren++;
3562 if (ch == ')' && brackets <= 0)
3563 if (--paren < 0)
3564 break;
3565 }
3566 if (brackets < 0) {
3567 brackets = 0;
3568 error(ERR_NONFATAL, "braces do not "
3569 "enclose all of macro parameter");
3570 }
3571 paramsize[nparam] += white + 1;
3572 white = 0;
3573 } /* parameter loop */
3574 nparam++;
3575 while (m && (m->nparam != nparam ||
3576 mstrcmp(m->name, mname,
3577 m->casesense)))
3578 m = m->next;
3579 if (!m)
3580 error(ERR_WARNING | ERR_WARN_MNP,
3581 "macro `%s' exists, "
3582 "but not taking %d parameters",
3583 mstart->text, nparam);
3584 }
3585 }
3586 if (m && m->in_progress)
3587 m = NULL;
3588 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003589 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003590 * Design question: should we handle !tline, which
3591 * indicates missing ')' here, or expand those
3592 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003593 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003594 */
3595 nasm_free(params);
3596 nasm_free(paramsize);
3597 tline = mstart;
3598 } else {
3599 /*
3600 * Expand the macro: we are placed on the last token of the
3601 * call, so that we can easily split the call from the
3602 * following tokens. We also start by pushing an SMAC_END
3603 * token for the cycle removal.
3604 */
3605 t = tline;
3606 if (t) {
3607 tline = t->next;
3608 t->next = NULL;
3609 }
3610 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003611 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003612 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003613 tline = tt;
3614 for (t = m->expansion; t; t = t->next) {
3615 if (t->type >= TOK_SMAC_PARAM) {
3616 Token *pcopy = tline, **ptail = &pcopy;
3617 Token *ttt, *pt;
3618 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003619
H. Peter Anvine2c80182005-01-15 22:15:51 +00003620 ttt = params[t->type - TOK_SMAC_PARAM];
3621 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3622 --i >= 0;) {
3623 pt = *ptail =
3624 new_Token(tline, ttt->type, ttt->text,
3625 0);
3626 ptail = &pt->next;
3627 ttt = ttt->next;
3628 }
3629 tline = pcopy;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -07003630 } else if (t->type == TOK_PREPROC_Q) {
3631 tt = new_Token(tline, TOK_ID, mname, 0);
3632 tline = tt;
3633 } else if (t->type == TOK_PREPROC_QQ) {
3634 tt = new_Token(tline, TOK_ID, m->name, 0);
3635 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003636 } else {
3637 tt = new_Token(tline, t->type, t->text, 0);
3638 tline = tt;
3639 }
3640 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003641
H. Peter Anvine2c80182005-01-15 22:15:51 +00003642 /*
3643 * Having done that, get rid of the macro call, and clean
3644 * up the parameters.
3645 */
3646 nasm_free(params);
3647 nasm_free(paramsize);
3648 free_tlist(mstart);
3649 continue; /* main token loop */
3650 }
3651 }
3652 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003653
H. Peter Anvine2c80182005-01-15 22:15:51 +00003654 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003655 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003656 tline = delete_Token(tline);
3657 } else {
3658 t = *tail = tline;
3659 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003660 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003661 t->next = NULL;
3662 tail = &t->next;
3663 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003664 }
3665
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003666 /*
3667 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003668 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003669 * TOK_IDs should be concatenated.
3670 * Also we look for %+ tokens and concatenate the tokens before and after
3671 * them (without white spaces in between).
3672 */
3673 t = thead;
3674 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003675 while (t) {
3676 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3677 t = t->next;
3678 if (!t || !t->next)
3679 break;
3680 if (t->next->type == TOK_ID ||
3681 t->next->type == TOK_PREPROC_ID ||
3682 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003683 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003684 nasm_free(t->text);
3685 t->next = delete_Token(t->next);
3686 t->text = p;
3687 rescan = 1;
3688 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3689 t->next->next->type == TOK_PREPROC_ID &&
3690 strcmp(t->next->next->text, "%+") == 0) {
3691 /* free the next whitespace, the %+ token and next whitespace */
3692 int i;
3693 for (i = 1; i <= 3; i++) {
3694 if (!t->next
3695 || (i != 2 && t->next->type != TOK_WHITESPACE))
3696 break;
3697 t->next = delete_Token(t->next);
3698 } /* endfor */
3699 } else
3700 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003701 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003702 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003703 if (rescan) {
3704 tline = thead;
3705 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003706 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003707
H. Peter Anvine2c80182005-01-15 22:15:51 +00003708 if (org_tline) {
3709 if (thead) {
3710 *org_tline = *thead;
3711 /* since we just gave text to org_line, don't free it */
3712 thead->text = NULL;
3713 delete_Token(thead);
3714 } else {
3715 /* the expression expanded to empty line;
3716 we can't return NULL for some reasons
3717 we just set the line to a single WHITESPACE token. */
3718 memset(org_tline, 0, sizeof(*org_tline));
3719 org_tline->text = NULL;
3720 org_tline->type = TOK_WHITESPACE;
3721 }
3722 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003723 }
3724
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003725 return thead;
3726}
3727
3728/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003729 * Similar to expand_smacro but used exclusively with macro identifiers
3730 * right before they are fetched in. The reason is that there can be
3731 * identifiers consisting of several subparts. We consider that if there
3732 * are more than one element forming the name, user wants a expansion,
3733 * otherwise it will be left as-is. Example:
3734 *
3735 * %define %$abc cde
3736 *
3737 * the identifier %$abc will be left as-is so that the handler for %define
3738 * will suck it and define the corresponding value. Other case:
3739 *
3740 * %define _%$abc cde
3741 *
3742 * In this case user wants name to be expanded *before* %define starts
3743 * working, so we'll expand %$abc into something (if it has a value;
3744 * otherwise it will be left as-is) then concatenate all successive
3745 * PP_IDs into one.
3746 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003747static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003748{
3749 Token *cur, *oldnext = NULL;
3750
H. Peter Anvin734b1882002-04-30 21:01:08 +00003751 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003752 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003753
3754 cur = tline;
3755 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003756 (cur->next->type == TOK_ID ||
3757 cur->next->type == TOK_PREPROC_ID
3758 || cur->next->type == TOK_NUMBER))
3759 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003760
3761 /* If identifier consists of just one token, don't expand */
3762 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003763 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003764
H. Peter Anvine2c80182005-01-15 22:15:51 +00003765 if (cur) {
3766 oldnext = cur->next; /* Detach the tail past identifier */
3767 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003768 }
3769
H. Peter Anvin734b1882002-04-30 21:01:08 +00003770 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003771
H. Peter Anvine2c80182005-01-15 22:15:51 +00003772 if (cur) {
3773 /* expand_smacro possibly changhed tline; re-scan for EOL */
3774 cur = tline;
3775 while (cur && cur->next)
3776 cur = cur->next;
3777 if (cur)
3778 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003779 }
3780
3781 return tline;
3782}
3783
3784/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003785 * Determine whether the given line constitutes a multi-line macro
3786 * call, and return the MMacro structure called if so. Doesn't have
3787 * to check for an initial label - that's taken care of in
3788 * expand_mmacro - but must check numbers of parameters. Guaranteed
3789 * to be called with tline->type == TOK_ID, so the putative macro
3790 * name is easy to find.
3791 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003792static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003793{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003794 MMacro *head, *m;
3795 Token **params;
3796 int nparam;
3797
H. Peter Anvin166c2472008-05-28 12:28:58 -07003798 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003799
3800 /*
3801 * Efficiency: first we see if any macro exists with the given
3802 * name. If not, we can return NULL immediately. _Then_ we
3803 * count the parameters, and then we look further along the
3804 * list if necessary to find the proper MMacro.
3805 */
3806 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003807 if (!mstrcmp(m->name, tline->text, m->casesense))
3808 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003809 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003810 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003811
3812 /*
3813 * OK, we have a potential macro. Count and demarcate the
3814 * parameters.
3815 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003816 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003817
3818 /*
3819 * So we know how many parameters we've got. Find the MMacro
3820 * structure that handles this number.
3821 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003822 while (m) {
3823 if (m->nparam_min <= nparam
3824 && (m->plus || nparam <= m->nparam_max)) {
3825 /*
3826 * This one is right. Just check if cycle removal
3827 * prohibits us using it before we actually celebrate...
3828 */
3829 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003830#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003831 error(ERR_NONFATAL,
3832 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003833#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003834 nasm_free(params);
3835 return NULL;
3836 }
3837 /*
3838 * It's right, and we can use it. Add its default
3839 * parameters to the end of our list if necessary.
3840 */
3841 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3842 params =
3843 nasm_realloc(params,
3844 ((m->nparam_min + m->ndefs +
3845 1) * sizeof(*params)));
3846 while (nparam < m->nparam_min + m->ndefs) {
3847 params[nparam] = m->defaults[nparam - m->nparam_min];
3848 nparam++;
3849 }
3850 }
3851 /*
3852 * If we've gone over the maximum parameter count (and
3853 * we're in Plus mode), ignore parameters beyond
3854 * nparam_max.
3855 */
3856 if (m->plus && nparam > m->nparam_max)
3857 nparam = m->nparam_max;
3858 /*
3859 * Then terminate the parameter list, and leave.
3860 */
3861 if (!params) { /* need this special case */
3862 params = nasm_malloc(sizeof(*params));
3863 nparam = 0;
3864 }
3865 params[nparam] = NULL;
3866 *params_array = params;
3867 return m;
3868 }
3869 /*
3870 * This one wasn't right: look for the next one with the
3871 * same name.
3872 */
3873 for (m = m->next; m; m = m->next)
3874 if (!mstrcmp(m->name, tline->text, m->casesense))
3875 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003876 }
3877
3878 /*
3879 * After all that, we didn't find one with the right number of
3880 * parameters. Issue a warning, and fail to expand the macro.
3881 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003882 error(ERR_WARNING | ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003883 "macro `%s' exists, but not taking %d parameters",
3884 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003885 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003886 return NULL;
3887}
3888
3889/*
3890 * Expand the multi-line macro call made by the given line, if
3891 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003892 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003893 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003894static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003895{
3896 Token *startline = tline;
3897 Token *label = NULL;
3898 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003899 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003900 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003901 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003902 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07003903 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003904
3905 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003906 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07003907 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003908 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003909 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003910 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003911 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07003912 if (m) {
3913 mname = t->text;
3914 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003915 Token *last;
3916 /*
3917 * We have an id which isn't a macro call. We'll assume
3918 * it might be a label; we'll also check to see if a
3919 * colon follows it. Then, if there's another id after
3920 * that lot, we'll check it again for macro-hood.
3921 */
3922 label = last = t;
3923 t = t->next;
3924 if (tok_type_(t, TOK_WHITESPACE))
3925 last = t, t = t->next;
3926 if (tok_is_(t, ":")) {
3927 dont_prepend = 1;
3928 last = t, t = t->next;
3929 if (tok_type_(t, TOK_WHITESPACE))
3930 last = t, t = t->next;
3931 }
3932 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3933 return 0;
3934 last->next = NULL;
H. Peter Anvinc751e862008-06-09 10:18:45 -07003935 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003936 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003937 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003938
3939 /*
3940 * Fix up the parameters: this involves stripping leading and
3941 * trailing whitespace, then stripping braces if they are
3942 * present.
3943 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003944 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003945 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003946
H. Peter Anvine2c80182005-01-15 22:15:51 +00003947 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003948 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003949 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003950
H. Peter Anvine2c80182005-01-15 22:15:51 +00003951 t = params[i];
3952 skip_white_(t);
3953 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003954 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003955 params[i] = t;
3956 paramlen[i] = 0;
3957 while (t) {
3958 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3959 break; /* ... because we have hit a comma */
3960 if (comma && t->type == TOK_WHITESPACE
3961 && tok_is_(t->next, ","))
3962 break; /* ... or a space then a comma */
3963 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3964 break; /* ... or a brace */
3965 t = t->next;
3966 paramlen[i]++;
3967 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003968 }
3969
3970 /*
3971 * OK, we have a MMacro structure together with a set of
3972 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003973 * copies of each Line on to istk->expansion. Substitution of
3974 * parameter tokens and macro-local tokens doesn't get done
3975 * until the single-line macro substitution process; this is
3976 * because delaying them allows us to change the semantics
3977 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003978 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003979 * First, push an end marker on to istk->expansion, mark this
3980 * macro as in progress, and set up its invocation-specific
3981 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003982 */
3983 ll = nasm_malloc(sizeof(Line));
3984 ll->next = istk->expansion;
3985 ll->finishes = m;
3986 ll->first = NULL;
3987 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003988
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003989 m->in_progress = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003990 m->params = params;
3991 m->iline = tline;
3992 m->nparam = nparam;
3993 m->rotate = 0;
3994 m->paramlen = paramlen;
3995 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003996 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003997
3998 m->next_active = istk->mstk;
3999 istk->mstk = m;
4000
H. Peter Anvine2c80182005-01-15 22:15:51 +00004001 for (l = m->expansion; l; l = l->next) {
4002 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004003
H. Peter Anvine2c80182005-01-15 22:15:51 +00004004 ll = nasm_malloc(sizeof(Line));
4005 ll->finishes = NULL;
4006 ll->next = istk->expansion;
4007 istk->expansion = ll;
4008 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004009
H. Peter Anvine2c80182005-01-15 22:15:51 +00004010 for (t = l->first; t; t = t->next) {
4011 Token *x = t;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004012 switch (t->type) {
4013 case TOK_PREPROC_Q:
H. Peter Anvinc751e862008-06-09 10:18:45 -07004014 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004015 break;
4016 case TOK_PREPROC_QQ:
4017 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4018 break;
4019 case TOK_PREPROC_ID:
4020 if (t->text[1] == '0' && t->text[2] == '0') {
4021 dont_prepend = -1;
4022 x = label;
4023 if (!x)
4024 continue;
4025 }
4026 /* fall through */
4027 default:
4028 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4029 break;
H. Peter Anvin166c2472008-05-28 12:28:58 -07004030 }
H. Peter Anvince2233b2008-05-25 21:57:00 -07004031 tail = &tt->next;
4032 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004033 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004034 }
4035
4036 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004037 * If we had a label, push it on as the first line of
4038 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004039 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004040 if (label) {
4041 if (dont_prepend < 0)
4042 free_tlist(startline);
4043 else {
4044 ll = nasm_malloc(sizeof(Line));
4045 ll->finishes = NULL;
4046 ll->next = istk->expansion;
4047 istk->expansion = ll;
4048 ll->first = startline;
4049 if (!dont_prepend) {
4050 while (label->next)
4051 label = label->next;
4052 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4053 }
4054 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004055 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004056
H. Peter Anvin734b1882002-04-30 21:01:08 +00004057 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004058
H. Peter Anvineba20a72002-04-30 20:53:55 +00004059 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004060}
4061
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004062/*
4063 * Since preprocessor always operate only on the line that didn't
4064 * arrived yet, we should always use ERR_OFFBY1. Also since user
4065 * won't want to see same error twice (preprocessing is done once
4066 * per pass) we will want to show errors only during pass one.
4067 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004068static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004069{
4070 va_list arg;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004071 char buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004072
4073 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004074 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004075 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004076
H. Peter Anvin734b1882002-04-30 21:01:08 +00004077 va_start(arg, fmt);
Ed Beroset19f927a2004-12-15 17:07:03 +00004078 vsnprintf(buff, sizeof(buff), fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004079 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004080
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004081 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004082 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
4083 istk->mstk->lineno, buff);
4084 else
4085 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004086}
4087
H. Peter Anvin734b1882002-04-30 21:01:08 +00004088static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004089pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004090 ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004091{
H. Peter Anvin99941bf2002-05-14 17:44:03 +00004092 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004093 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004094 istk = nasm_malloc(sizeof(Include));
4095 istk->next = NULL;
4096 istk->conds = NULL;
4097 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004098 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004099 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004100 istk->fname = NULL;
4101 src_set_fname(nasm_strdup(file));
4102 src_set_linnum(0);
4103 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004104 if (!istk->fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004105 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
4106 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004107 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004108 nested_mac_count = 0;
4109 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004110 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004111 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004112 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004113 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004114 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004115 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004116 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004117 any_extrastdmac = extrastdmac && *extrastdmac;
4118 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004119 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004120 evaluate = eval;
4121 pass = apass;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004122 dephead = deptail = deplist;
4123 if (deplist) {
4124 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4125 sl->next = NULL;
4126 strcpy(sl->str, file);
4127 *deptail = sl;
4128 deptail = &sl->next;
4129 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004130}
4131
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004132static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004133{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004134 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004135 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004136
H. Peter Anvine2c80182005-01-15 22:15:51 +00004137 while (1) {
4138 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004139 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004140 * buffer or from the input file.
4141 */
4142 tline = NULL;
4143 while (istk->expansion && istk->expansion->finishes) {
4144 Line *l = istk->expansion;
4145 if (!l->finishes->name && l->finishes->in_progress > 1) {
4146 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004147
H. Peter Anvine2c80182005-01-15 22:15:51 +00004148 /*
4149 * This is a macro-end marker for a macro with no
4150 * name, which means it's not really a macro at all
4151 * but a %rep block, and the `in_progress' field is
4152 * more than 1, meaning that we still need to
4153 * repeat. (1 means the natural last repetition; 0
4154 * means termination by %exitrep.) We have
4155 * therefore expanded up to the %endrep, and must
4156 * push the whole block on to the expansion buffer
4157 * again. We don't bother to remove the macro-end
4158 * marker: we'd only have to generate another one
4159 * if we did.
4160 */
4161 l->finishes->in_progress--;
4162 for (l = l->finishes->expansion; l; l = l->next) {
4163 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004164
H. Peter Anvine2c80182005-01-15 22:15:51 +00004165 ll = nasm_malloc(sizeof(Line));
4166 ll->next = istk->expansion;
4167 ll->finishes = NULL;
4168 ll->first = NULL;
4169 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004170
H. Peter Anvine2c80182005-01-15 22:15:51 +00004171 for (t = l->first; t; t = t->next) {
4172 if (t->text || t->type == TOK_WHITESPACE) {
4173 tt = *tail =
4174 new_Token(NULL, t->type, t->text, 0);
4175 tail = &tt->next;
4176 }
4177 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004178
H. Peter Anvine2c80182005-01-15 22:15:51 +00004179 istk->expansion = ll;
4180 }
4181 } else {
4182 /*
4183 * Check whether a `%rep' was started and not ended
4184 * within this macro expansion. This can happen and
4185 * should be detected. It's a fatal error because
4186 * I'm too confused to work out how to recover
4187 * sensibly from it.
4188 */
4189 if (defining) {
4190 if (defining->name)
4191 error(ERR_PANIC,
4192 "defining with name in expansion");
4193 else if (istk->mstk->name)
4194 error(ERR_FATAL,
4195 "`%%rep' without `%%endrep' within"
4196 " expansion of macro `%s'",
4197 istk->mstk->name);
4198 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004199
H. Peter Anvine2c80182005-01-15 22:15:51 +00004200 /*
4201 * FIXME: investigate the relationship at this point between
4202 * istk->mstk and l->finishes
4203 */
4204 {
4205 MMacro *m = istk->mstk;
4206 istk->mstk = m->next_active;
4207 if (m->name) {
4208 /*
4209 * This was a real macro call, not a %rep, and
4210 * therefore the parameter information needs to
4211 * be freed.
4212 */
4213 nasm_free(m->params);
4214 free_tlist(m->iline);
4215 nasm_free(m->paramlen);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004216 l->finishes->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004217 } else
4218 free_mmacro(m);
4219 }
4220 istk->expansion = l->next;
4221 nasm_free(l);
4222 list->downlevel(LIST_MACRO);
4223 }
4224 }
4225 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004226
H. Peter Anvine2c80182005-01-15 22:15:51 +00004227 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004228 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004229 Line *l = istk->expansion;
4230 if (istk->mstk)
4231 istk->mstk->lineno++;
4232 tline = l->first;
4233 istk->expansion = l->next;
4234 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004235 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004236 list->line(LIST_MACRO, p);
4237 nasm_free(p);
4238 break;
4239 }
4240 line = read_line();
4241 if (line) { /* from the current input file */
4242 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004243 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004244 nasm_free(line);
4245 break;
4246 }
4247 /*
4248 * The current file has ended; work down the istk
4249 */
4250 {
4251 Include *i = istk;
4252 fclose(i->fp);
4253 if (i->conds)
4254 error(ERR_FATAL,
4255 "expected `%%endif' before end of file");
4256 /* only set line and file name if there's a next node */
4257 if (i->next) {
4258 src_set_linnum(i->lineno);
4259 nasm_free(src_set_fname(i->fname));
H. Peter Anvin86877b22008-06-20 15:55:45 -07004260 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004261 istk = i->next;
4262 list->downlevel(LIST_INCLUDE);
4263 nasm_free(i);
4264 if (!istk)
4265 return NULL;
4266 }
4267 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004268
H. Peter Anvine2c80182005-01-15 22:15:51 +00004269 /*
4270 * We must expand MMacro parameters and MMacro-local labels
4271 * _before_ we plunge into directive processing, to cope
4272 * with things like `%define something %1' such as STRUC
4273 * uses. Unless we're _defining_ a MMacro, in which case
4274 * those tokens should be left alone to go into the
4275 * definition; and unless we're in a non-emitting
4276 * condition, in which case we don't want to meddle with
4277 * anything.
4278 */
Charles Crayned4200be2008-07-12 16:42:33 -07004279 if (!defining && !(istk->conds && !emitting(istk->conds->state))
4280 && !(istk->mstk && !istk->mstk->in_progress))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004281 tline = expand_mmac_params(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004282
H. Peter Anvine2c80182005-01-15 22:15:51 +00004283 /*
4284 * Check the line to see if it's a preprocessor directive.
4285 */
4286 if (do_directive(tline) == DIRECTIVE_FOUND) {
4287 continue;
4288 } else if (defining) {
4289 /*
4290 * We're defining a multi-line macro. We emit nothing
4291 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004292 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004293 */
4294 Line *l = nasm_malloc(sizeof(Line));
4295 l->next = defining->expansion;
4296 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004297 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004298 defining->expansion = l;
4299 continue;
4300 } else if (istk->conds && !emitting(istk->conds->state)) {
4301 /*
4302 * We're in a non-emitting branch of a condition block.
4303 * Emit nothing at all, not even a blank line: when we
4304 * emerge from the condition we'll give a line-number
4305 * directive so we keep our place correctly.
4306 */
4307 free_tlist(tline);
4308 continue;
4309 } else if (istk->mstk && !istk->mstk->in_progress) {
4310 /*
4311 * We're in a %rep block which has been terminated, so
4312 * we're walking through to the %endrep without
4313 * emitting anything. Emit nothing at all, not even a
4314 * blank line: when we emerge from the %rep block we'll
4315 * give a line-number directive so we keep our place
4316 * correctly.
4317 */
4318 free_tlist(tline);
4319 continue;
4320 } else {
4321 tline = expand_smacro(tline);
4322 if (!expand_mmacro(tline)) {
4323 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004324 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004325 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004326 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004327 free_tlist(tline);
4328 break;
4329 } else {
4330 continue; /* expand_mmacro calls free_tlist */
4331 }
4332 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004333 }
4334
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004335 return line;
4336}
4337
H. Peter Anvine2c80182005-01-15 22:15:51 +00004338static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004339{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004340 if (defining) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02004341 if(defining->name) {
4342 error(ERR_NONFATAL,
4343 "end of file while still defining macro `%s'",
4344 defining->name);
4345 } else {
4346 error(ERR_NONFATAL, "end of file while still in %%rep");
4347 }
4348
H. Peter Anvine2c80182005-01-15 22:15:51 +00004349 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004350 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004351 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004352 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004353 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004354 while (istk) {
4355 Include *i = istk;
4356 istk = istk->next;
4357 fclose(i->fp);
4358 nasm_free(i->fname);
4359 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004360 }
4361 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004362 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004363 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004364 if (pass == 0) {
H. Peter Anvin86877b22008-06-20 15:55:45 -07004365 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004366 free_llist(predef);
4367 delete_Blocks();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004368 while ((i = ipath)) {
4369 ipath = i->next;
4370 if (i->path)
4371 nasm_free(i->path);
4372 nasm_free(i);
4373 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07004374 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004375}
4376
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004377void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004378{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004379 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004380
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004381 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004382 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004383 i->next = NULL;
4384
H. Peter Anvine2c80182005-01-15 22:15:51 +00004385 if (ipath != NULL) {
4386 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004387 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004388 j = j->next;
4389 j->next = i;
4390 } else {
4391 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004392 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004393}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004394
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004395void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004396{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004397 Token *inc, *space, *name;
4398 Line *l;
4399
H. Peter Anvin734b1882002-04-30 21:01:08 +00004400 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4401 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4402 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004403
4404 l = nasm_malloc(sizeof(Line));
4405 l->next = predef;
4406 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004407 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004408 predef = l;
4409}
4410
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004411void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004412{
4413 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004414 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004415 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004416
4417 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004418 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4419 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004420 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004421 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004422 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004423 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004424 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004425
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004426 l = nasm_malloc(sizeof(Line));
4427 l->next = predef;
4428 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004429 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004430 predef = l;
4431}
4432
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004433void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004434{
4435 Token *def, *space;
4436 Line *l;
4437
H. Peter Anvin734b1882002-04-30 21:01:08 +00004438 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4439 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004440 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004441
4442 l = nasm_malloc(sizeof(Line));
4443 l->next = predef;
4444 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004445 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004446 predef = l;
4447}
4448
Keith Kaniosb7a89542007-04-12 02:40:54 +00004449/*
4450 * Added by Keith Kanios:
4451 *
4452 * This function is used to assist with "runtime" preprocessor
4453 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4454 *
4455 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4456 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4457 */
4458
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004459void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004460{
4461 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004462
Keith Kaniosb7a89542007-04-12 02:40:54 +00004463 def = tokenize(definition);
4464 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4465 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004466
Keith Kaniosb7a89542007-04-12 02:40:54 +00004467}
4468
H. Peter Anvincfb71762008-06-20 15:20:16 -07004469void pp_extra_stdmac(const macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004470{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004471 extrastdmac = macros;
4472}
4473
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004474static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004475{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004476 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004477 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004478 tok->text = nasm_strdup(numbuf);
4479 tok->type = TOK_NUMBER;
4480}
4481
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004482Preproc nasmpp = {
4483 pp_reset,
4484 pp_getline,
4485 pp_cleanup
4486};