blob: 71936e4c4681b530cc20d79471f4bc21ed84ab3c [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
359/*
360 * The number of macro parameters to allocate space for at a time.
361 */
362#define PARAM_DELTA 16
363
364/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700365 * The standard macro set: defined in macros.c in the array nasm_stdmac.
366 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000367 */
H. Peter Anvincda81632008-06-21 15:15:40 -0700368static const macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000369
370/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000371 * The extra standard macros that come from the object format, if
372 * any.
373 */
H. Peter Anvincda81632008-06-21 15:15:40 -0700374static const macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700375static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000376
377/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000378 * Tokens are allocated in blocks to improve speed
379 */
380#define TOKEN_BLOCKSIZE 4096
381static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000382struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000383 Blocks *next;
384 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000385};
386
387static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000388
389/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000390 * Forward declarations.
391 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000392static Token *expand_mmac_params(Token * tline);
393static Token *expand_smacro(Token * tline);
394static Token *expand_id(Token * tline);
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -0700395static Context *get_ctx(const char *name, bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700396static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000397static void error(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000398static void *new_Block(size_t size);
399static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700400static Token *new_Token(Token * next, enum pp_token_type type,
401 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000402static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000403
404/*
405 * Macros for safe checking of token pointers, avoid *(NULL)
406 */
407#define tok_type_(x,t) ((x) && (x)->type == (t))
408#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
409#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
410#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000411
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000412/* Handle TASM specific directives, which do not contain a % in
413 * front of them. We do it here because I could not find any other
414 * place to do it for the moment, and it is a hack (ideally it would
415 * be nice to be able to use the NASM pre-processor to do it).
416 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000417static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000418{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000419 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000420 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000421
422 /* Skip whitespace */
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700423 while (nasm_isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000424 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000425
426 /* Binary search for the directive name */
427 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000428 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000429 len = 0;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700430 while (!nasm_isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000431 len++;
432 if (len) {
433 oldchar = p[len];
434 p[len] = 0;
435 while (j - i > 1) {
436 k = (j + i) / 2;
437 m = nasm_stricmp(p, tasm_directives[k]);
438 if (m == 0) {
439 /* We have found a directive, so jam a % in front of it
440 * so that NASM will then recognise it as one if it's own.
441 */
442 p[len] = oldchar;
443 len = strlen(p);
444 oldline = line;
445 line = nasm_malloc(len + 2);
446 line[0] = '%';
447 if (k == TM_IFDIFI) {
448 /* NASM does not recognise IFDIFI, so we convert it to
449 * %ifdef BOGUS. This is not used in NASM comaptible
450 * code, but does need to parse for the TASM macro
451 * package.
452 */
453 strcpy(line + 1, "ifdef BOGUS");
454 } else {
455 memcpy(line + 1, p, len + 1);
456 }
457 nasm_free(oldline);
458 return line;
459 } else if (m < 0) {
460 j = k;
461 } else
462 i = k;
463 }
464 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000465 }
466 return line;
467}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000468
H. Peter Anvin76690a12002-04-30 20:52:49 +0000469/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000470 * The pre-preprocessing stage... This function translates line
471 * number indications as they emerge from GNU cpp (`# lineno "file"
472 * flags') into NASM preprocessor line number indications (`%line
473 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000474 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000475static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000476{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000477 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000478 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000479
H. Peter Anvine2c80182005-01-15 22:15:51 +0000480 if (line[0] == '#' && line[1] == ' ') {
481 oldline = line;
482 fname = oldline + 2;
483 lineno = atoi(fname);
484 fname += strspn(fname, "0123456789 ");
485 if (*fname == '"')
486 fname++;
487 fnlen = strcspn(fname, "\"");
488 line = nasm_malloc(20 + fnlen);
489 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
490 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000491 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000492 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000493 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000494 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000495}
496
497/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000498 * Free a linked list of tokens.
499 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000501{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000502 while (list) {
503 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000504 }
505}
506
507/*
508 * Free a linked list of lines.
509 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000510static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000511{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000512 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000513 while (list) {
514 l = list;
515 list = list->next;
516 free_tlist(l->first);
517 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000518 }
519}
520
521/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000522 * Free an MMacro
523 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000524static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000525{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000526 nasm_free(m->name);
527 free_tlist(m->dlist);
528 nasm_free(m->defaults);
529 free_llist(m->expansion);
530 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000531}
532
533/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700534 * Free all currently defined macros, and free the hash tables
535 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700536static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700537{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700538 SMacro *s;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700539 const char *key;
540 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700541
H. Peter Anvin072771e2008-05-22 13:17:51 -0700542 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700543 nasm_free((void *)key);
544 while (s) {
545 SMacro *ns = s->next;
546 nasm_free(s->name);
547 free_tlist(s->expansion);
548 nasm_free(s);
549 s = ns;
550 }
551 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700552 hash_free(smt);
553}
554
555static void free_mmacro_table(struct hash_table *mmt)
556{
557 MMacro *m;
558 const char *key;
559 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700560
561 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700562 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700563 nasm_free((void *)key);
564 while (m) {
565 MMacro *nm = m->next;
566 free_mmacro(m);
567 m = nm;
568 }
569 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700570 hash_free(mmt);
571}
572
573static void free_macros(void)
574{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700575 free_smacro_table(&smacros);
576 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700577}
578
579/*
580 * Initialize the hash tables
581 */
582static void init_macros(void)
583{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700584 hash_init(&smacros, HASH_LARGE);
585 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700586}
587
588/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000589 * Pop the context stack.
590 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000591static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000592{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000593 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000594
595 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700596 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000597 nasm_free(c->name);
598 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000599}
600
H. Peter Anvin072771e2008-05-22 13:17:51 -0700601/*
602 * Search for a key in the hash index; adding it if necessary
603 * (in which case we initialize the data pointer to NULL.)
604 */
605static void **
606hash_findi_add(struct hash_table *hash, const char *str)
607{
608 struct hash_insert hi;
609 void **r;
610 char *strx;
611
612 r = hash_findi(hash, str, &hi);
613 if (r)
614 return r;
615
616 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
617 return hash_add(&hi, strx, NULL);
618}
619
620/*
621 * Like hash_findi, but returns the data element rather than a pointer
622 * to it. Used only when not adding a new element, hence no third
623 * argument.
624 */
625static void *
626hash_findix(struct hash_table *hash, const char *str)
627{
628 void **p;
629
630 p = hash_findi(hash, str, NULL);
631 return p ? *p : NULL;
632}
633
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000634#define BUF_DELTA 512
635/*
636 * Read a line from the top file in istk, handling multiple CR/LFs
637 * at the end of the line read, and handling spurious ^Zs. Will
638 * return lines from the standard macro set if this has not already
639 * been done.
640 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000641static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000642{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000643 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000644 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000645
H. Peter Anvine2c80182005-01-15 22:15:51 +0000646 if (stdmacpos) {
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700647 unsigned char c;
H. Peter Anvin7e50d232008-06-25 14:54:14 -0700648 const unsigned char *p = stdmacpos;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700649 char *ret, *q;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700650 size_t len = 0;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700651 while ((c = *p++)) {
652 if (c >= 0x80)
653 len += pp_directives_len[c-0x80]+1;
654 else
655 len++;
656 }
657 ret = nasm_malloc(len+1);
H. Peter Anvincda81632008-06-21 15:15:40 -0700658 q = ret;
659 while ((c = *stdmacpos++)) {
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700660 if (c >= 0x80) {
661 memcpy(q, pp_directives[c-0x80], pp_directives_len[c-0x80]);
662 q += pp_directives_len[c-0x80];
663 *q++ = ' ';
664 } else {
665 *q++ = c;
666 }
667 }
H. Peter Anvincda81632008-06-21 15:15:40 -0700668 stdmacpos = p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700669 *q = '\0';
670
H. Peter Anvind2456592008-06-19 15:04:18 -0700671 if (!*stdmacpos) {
672 /* This was the last of the standard macro chain... */
673 stdmacpos = NULL;
674 if (any_extrastdmac) {
675 stdmacpos = extrastdmac;
676 any_extrastdmac = false;
677 } else if (do_predef) {
678 Line *pd, *l;
679 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000680
H. Peter Anvind2456592008-06-19 15:04:18 -0700681 /*
682 * Nasty hack: here we push the contents of
683 * `predef' on to the top-level expansion stack,
684 * since this is the most convenient way to
685 * implement the pre-include and pre-define
686 * features.
687 */
688 for (pd = predef; pd; pd = pd->next) {
689 head = NULL;
690 tail = &head;
691 for (t = pd->first; t; t = t->next) {
692 *tail = new_Token(NULL, t->type, t->text, 0);
693 tail = &(*tail)->next;
694 }
695 l = nasm_malloc(sizeof(Line));
696 l->next = istk->expansion;
697 l->first = head;
H. Peter Anvin538002d2008-06-28 18:30:27 -0700698 l->finishes = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700699 istk->expansion = l;
700 }
701 do_predef = false;
702 }
703 }
704 return ret;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000705 }
706
707 bufsize = BUF_DELTA;
708 buffer = nasm_malloc(BUF_DELTA);
709 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000710 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000711 while (1) {
712 q = fgets(p, bufsize - (p - buffer), istk->fp);
713 if (!q)
714 break;
715 p += strlen(p);
716 if (p > buffer && p[-1] == '\n') {
717 /* Convert backslash-CRLF line continuation sequences into
718 nothing at all (for DOS and Windows) */
719 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
720 p -= 3;
721 *p = 0;
722 continued_count++;
723 }
724 /* Also convert backslash-LF line continuation sequences into
725 nothing at all (for Unix) */
726 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
727 p -= 2;
728 *p = 0;
729 continued_count++;
730 } else {
731 break;
732 }
733 }
734 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000735 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000736 bufsize += BUF_DELTA;
737 buffer = nasm_realloc(buffer, bufsize);
738 p = buffer + offset; /* prevent stale-pointer problems */
739 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000740 }
741
H. Peter Anvine2c80182005-01-15 22:15:51 +0000742 if (!q && p == buffer) {
743 nasm_free(buffer);
744 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000745 }
746
H. Peter Anvine2c80182005-01-15 22:15:51 +0000747 src_set_linnum(src_get_linnum() + istk->lineinc +
748 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000749
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000750 /*
751 * Play safe: remove CRs as well as LFs, if any of either are
752 * present at the end of the line.
753 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000754 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000755 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000756
757 /*
758 * Handle spurious ^Z, which may be inserted into source files
759 * by some file transfer utilities.
760 */
761 buffer[strcspn(buffer, "\032")] = '\0';
762
H. Peter Anvin734b1882002-04-30 21:01:08 +0000763 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000764
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000765 return buffer;
766}
767
768/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000769 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000770 * don't need to parse the value out of e.g. numeric tokens: we
771 * simply split one string into many.
772 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000773static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000774{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000775 char *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000776 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000777 Token *list = NULL;
778 Token *t, **tail = &list;
779
H. Peter Anvine2c80182005-01-15 22:15:51 +0000780 while (*line) {
781 p = line;
782 if (*p == '%') {
783 p++;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700784 if (nasm_isdigit(*p) ||
785 ((*p == '-' || *p == '+') && nasm_isdigit(p[1])) ||
786 ((*p == '+') && (nasm_isspace(p[1]) || !p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000787 do {
788 p++;
789 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700790 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000791 type = TOK_PREPROC_ID;
792 } else if (*p == '{') {
793 p++;
794 while (*p && *p != '}') {
795 p[-1] = *p;
796 p++;
797 }
798 p[-1] = '\0';
799 if (*p)
800 p++;
801 type = TOK_PREPROC_ID;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700802 } else if (*p == '?') {
803 type = TOK_PREPROC_Q; /* %? */
804 p++;
805 if (*p == '?') {
806 type = TOK_PREPROC_QQ; /* %?? */
807 p++;
808 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000809 } else if (isidchar(*p) ||
810 ((*p == '!' || *p == '%' || *p == '$') &&
811 isidchar(p[1]))) {
812 do {
813 p++;
814 }
815 while (isidchar(*p));
816 type = TOK_PREPROC_ID;
817 } else {
818 type = TOK_OTHER;
819 if (*p == '%')
820 p++;
821 }
822 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
823 type = TOK_ID;
824 p++;
825 while (*p && isidchar(*p))
826 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700827 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000828 /*
829 * A string token.
830 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700832 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000833
H. Peter Anvine2c80182005-01-15 22:15:51 +0000834 if (*p) {
835 p++;
836 } else {
837 error(ERR_WARNING, "unterminated string");
838 /* Handling unterminated strings by UNV */
839 /* type = -1; */
840 }
841 } else if (isnumstart(*p)) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700842 bool is_hex = false;
843 bool is_float = false;
844 bool has_e = false;
845 char c, *r;
846
H. Peter Anvine2c80182005-01-15 22:15:51 +0000847 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700848 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000849 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700850
851 if (*p == '$') {
852 p++;
853 is_hex = true;
854 }
855
856 for (;;) {
857 c = *p++;
858
859 if (!is_hex && (c == 'e' || c == 'E')) {
860 has_e = true;
861 if (*p == '+' || *p == '-') {
862 /* e can only be followed by +/- if it is either a
863 prefixed hex number or a floating-point number */
864 p++;
865 is_float = true;
866 }
867 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
868 is_hex = true;
869 } else if (c == 'P' || c == 'p') {
870 is_float = true;
871 if (*p == '+' || *p == '-')
872 p++;
873 } else if (isnumchar(c) || c == '_')
874 ; /* just advance */
875 else if (c == '.') {
876 /* we need to deal with consequences of the legacy
877 parser, like "1.nolist" being two tokens
878 (TOK_NUMBER, TOK_ID) here; at least give it
879 a shot for now. In the future, we probably need
880 a flex-based scanner with proper pattern matching
881 to do it as well as it can be done. Nothing in
882 the world is going to help the person who wants
883 0x123.p16 interpreted as two tokens, though. */
884 r = p;
885 while (*r == '_')
886 r++;
887
H. Peter Anvinf221b9e2008-06-21 11:03:51 -0700888 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700889 (!is_hex && (*r == 'e' || *r == 'E')) ||
890 (*r == 'p' || *r == 'P')) {
891 p = r;
892 is_float = true;
893 } else
894 break; /* Terminate the token */
895 } else
896 break;
897 }
898 p--; /* Point to first character beyond number */
899
900 if (has_e && !is_hex) {
901 /* 1e13 is floating-point, but 1e13h is not */
902 is_float = true;
903 }
904
905 type = is_float ? TOK_FLOAT : TOK_NUMBER;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700906 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000907 type = TOK_WHITESPACE;
908 p++;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700909 while (*p && nasm_isspace(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000910 p++;
911 /*
912 * Whitespace just before end-of-line is discarded by
913 * pretending it's a comment; whitespace just before a
914 * comment gets lumped into the comment.
915 */
916 if (!*p || *p == ';') {
917 type = TOK_COMMENT;
918 while (*p)
919 p++;
920 }
921 } else if (*p == ';') {
922 type = TOK_COMMENT;
923 while (*p)
924 p++;
925 } else {
926 /*
927 * Anything else is an operator of some kind. We check
928 * for all the double-character operators (>>, <<, //,
929 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000930 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000931 */
932 type = TOK_OTHER;
933 if ((p[0] == '>' && p[1] == '>') ||
934 (p[0] == '<' && p[1] == '<') ||
935 (p[0] == '/' && p[1] == '/') ||
936 (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++;
945 }
946 p++;
947 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000948
H. Peter Anvine2c80182005-01-15 22:15:51 +0000949 /* Handling unterminated string by UNV */
950 /*if (type == -1)
951 {
952 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
953 t->text[p-line] = *line;
954 tail = &t->next;
955 }
956 else */
957 if (type != TOK_COMMENT) {
958 *tail = t = new_Token(NULL, type, line, p - line);
959 tail = &t->next;
960 }
961 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000962 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000963 return list;
964}
965
H. Peter Anvince616072002-04-30 21:02:23 +0000966/*
967 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -0700968 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +0000969 * deleted only all at once by the delete_Blocks function.
970 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +0000972{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000973 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000975 /* first, get to the end of the linked list */
976 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000977 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000978 /* now allocate the requested chunk */
979 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000980
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000981 /* now allocate a new block for the next request */
982 b->next = nasm_malloc(sizeof(Blocks));
983 /* and initialize the contents of the new block */
984 b->next->next = NULL;
985 b->next->chunk = NULL;
986 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000987}
988
989/*
990 * this function deletes all managed blocks of memory
991 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000992static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +0000993{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +0000995
H. Peter Anvin70653092007-10-19 14:42:29 -0700996 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000997 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -0700998 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000999 * free it.
1000 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 while (b) {
1002 if (b->chunk)
1003 nasm_free(b->chunk);
1004 a = b;
1005 b = b->next;
1006 if (a != &blocks)
1007 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001008 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001010
1011/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001012 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001013 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001014 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001015 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001016static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvinc751e862008-06-09 10:18:45 -07001017 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001018{
1019 Token *t;
1020 int i;
1021
H. Peter Anvine2c80182005-01-15 22:15:51 +00001022 if (freeTokens == NULL) {
1023 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1024 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1025 freeTokens[i].next = &freeTokens[i + 1];
1026 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001027 }
1028 t = freeTokens;
1029 freeTokens = t->next;
1030 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001031 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001032 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001033 if (type == TOK_WHITESPACE || text == NULL) {
1034 t->text = NULL;
1035 } else {
1036 if (txtlen == 0)
1037 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001038 t->text = nasm_malloc(txtlen+1);
1039 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001040 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001041 }
1042 return t;
1043}
1044
H. Peter Anvine2c80182005-01-15 22:15:51 +00001045static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001046{
1047 Token *next = t->next;
1048 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001049 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001050 freeTokens = t;
1051 return next;
1052}
1053
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001054/*
1055 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001056 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1057 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001058 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001059static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001060{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001061 Token *t;
1062 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001063 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001064 const char *q;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001065
1066 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001067 for (t = tlist; t; t = t->next) {
1068 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001069 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001070 nasm_free(t->text);
1071 if (p)
1072 t->text = nasm_strdup(p);
1073 else
1074 t->text = NULL;
1075 }
1076 /* Expand local macros here and not during preprocessing */
1077 if (expand_locals &&
1078 t->type == TOK_PREPROC_ID && t->text &&
1079 t->text[0] == '%' && t->text[1] == '$') {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001080 Context *ctx = get_ctx(t->text, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001082 char buffer[40];
1083 char *p, *q = t->text + 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001084
H. Peter Anvine2c80182005-01-15 22:15:51 +00001085 q += strspn(q, "$");
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001086 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001087 p = nasm_strcat(buffer, q);
1088 nasm_free(t->text);
1089 t->text = p;
1090 }
1091 }
1092 if (t->type == TOK_WHITESPACE) {
1093 len++;
1094 } else if (t->text) {
1095 len += strlen(t->text);
1096 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001097 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001098 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 for (t = tlist; t; t = t->next) {
1100 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001101 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001102 } else if (t->text) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001103 q = t->text;
1104 while (*q)
1105 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001106 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001107 }
1108 *p = '\0';
1109 return line;
1110}
1111
1112/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001113 * A scanner, suitable for use by the expression evaluator, which
1114 * operates on a line of Tokens. Expects a pointer to a pointer to
1115 * the first token in the line to be passed in as its private_data
1116 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001117 *
1118 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001119 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001120static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001121{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001122 Token **tlineptr = private_data;
1123 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001124 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001125
H. Peter Anvine2c80182005-01-15 22:15:51 +00001126 do {
1127 tline = *tlineptr;
1128 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001129 }
1130 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001131 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001132
1133 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001135
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001136 tokval->t_charptr = tline->text;
1137
H. Peter Anvin76690a12002-04-30 20:52:49 +00001138 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001140 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001141 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001142
H. Peter Anvine2c80182005-01-15 22:15:51 +00001143 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001144 p = tokval->t_charptr = tline->text;
1145 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001146 tokval->t_charptr++;
1147 return tokval->t_type = TOKEN_ID;
1148 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001149
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001150 for (r = p, s = ourcopy; *r; r++) {
Philipp Thomas76ec8e72008-05-21 08:53:21 -07001151 if (r >= p+MAX_KEYWORD)
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001152 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001153 *s++ = nasm_tolower(*r);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001154 }
1155 *s = '\0';
1156 /* right, so we have an identifier sitting in temp storage. now,
1157 * is it actually a register or instruction name, or what? */
1158 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001159 }
1160
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161 if (tline->type == TOK_NUMBER) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001162 bool rn_error;
1163 tokval->t_integer = readnum(tline->text, &rn_error);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001164 tokval->t_charptr = tline->text;
H. Peter Anvin11627042008-06-09 20:45:19 -07001165 if (rn_error)
1166 return tokval->t_type = TOKEN_ERRNUM;
1167 else
1168 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001169 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001170
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001171 if (tline->type == TOK_FLOAT) {
1172 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001173 }
1174
H. Peter Anvine2c80182005-01-15 22:15:51 +00001175 if (tline->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001176 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001177
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001178 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001179 tokval->t_charptr = tline->text;
1180 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001181
H. Peter Anvin11627042008-06-09 20:45:19 -07001182 if (ep[0] != bq || ep[1] != '\0')
1183 return tokval->t_type = TOKEN_ERRSTR;
1184 else
1185 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001186 }
1187
H. Peter Anvine2c80182005-01-15 22:15:51 +00001188 if (tline->type == TOK_OTHER) {
1189 if (!strcmp(tline->text, "<<"))
1190 return tokval->t_type = TOKEN_SHL;
1191 if (!strcmp(tline->text, ">>"))
1192 return tokval->t_type = TOKEN_SHR;
1193 if (!strcmp(tline->text, "//"))
1194 return tokval->t_type = TOKEN_SDIV;
1195 if (!strcmp(tline->text, "%%"))
1196 return tokval->t_type = TOKEN_SMOD;
1197 if (!strcmp(tline->text, "=="))
1198 return tokval->t_type = TOKEN_EQ;
1199 if (!strcmp(tline->text, "<>"))
1200 return tokval->t_type = TOKEN_NE;
1201 if (!strcmp(tline->text, "!="))
1202 return tokval->t_type = TOKEN_NE;
1203 if (!strcmp(tline->text, "<="))
1204 return tokval->t_type = TOKEN_LE;
1205 if (!strcmp(tline->text, ">="))
1206 return tokval->t_type = TOKEN_GE;
1207 if (!strcmp(tline->text, "&&"))
1208 return tokval->t_type = TOKEN_DBL_AND;
1209 if (!strcmp(tline->text, "^^"))
1210 return tokval->t_type = TOKEN_DBL_XOR;
1211 if (!strcmp(tline->text, "||"))
1212 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001213 }
1214
1215 /*
1216 * We have no other options: just return the first character of
1217 * the token text.
1218 */
1219 return tokval->t_type = tline->text[0];
1220}
1221
1222/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001223 * Compare a string to the name of an existing macro; this is a
1224 * simple wrapper which calls either strcmp or nasm_stricmp
1225 * depending on the value of the `casesense' parameter.
1226 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001227static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001228{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001229 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001230}
1231
1232/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001233 * Compare a string to the name of an existing macro; this is a
1234 * simple wrapper which calls either strcmp or nasm_stricmp
1235 * depending on the value of the `casesense' parameter.
1236 */
1237static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1238{
1239 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1240}
1241
1242/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001243 * Return the Context structure associated with a %$ token. Return
1244 * NULL, having _already_ reported an error condition, if the
1245 * context stack isn't deep enough for the supplied number of $
1246 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001247 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001248 * also scanned for such smacro, until it is found; if not -
1249 * only the context that directly results from the number of $'s
1250 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001251 */
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001252static Context *get_ctx(const char *name, bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001253{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001254 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001255 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001256 int i;
1257
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001258 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001259 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001260
H. Peter Anvine2c80182005-01-15 22:15:51 +00001261 if (!cstk) {
1262 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1263 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001264 }
1265
H. Peter Anvine2c80182005-01-15 22:15:51 +00001266 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1267 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001268/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001269 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001270 if (!ctx) {
1271 error(ERR_NONFATAL, "`%s': context stack is only"
1272 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1273 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001274 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001275 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001276 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001277
H. Peter Anvine2c80182005-01-15 22:15:51 +00001278 do {
1279 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001280 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001281 while (m) {
1282 if (!mstrcmp(m->name, name, m->casesense))
1283 return ctx;
1284 m = m->next;
1285 }
1286 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001287 }
1288 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001289 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001290}
1291
1292/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001293 * Check to see if a file is already in a string list
1294 */
1295static bool in_list(const StrList *list, const char *str)
1296{
1297 while (list) {
1298 if (!strcmp(list->str, str))
1299 return true;
1300 list = list->next;
1301 }
1302 return false;
1303}
1304
1305/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001306 * Open an include file. This routine must always return a valid
1307 * file pointer if it returns - it's responsible for throwing an
1308 * ERR_FATAL and bombing out completely if not. It should also try
1309 * the include path one by one until it finds the file or reaches
1310 * the end of the path.
1311 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001312static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
H. Peter Anvin418ca702008-05-30 10:42:30 -07001313 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001314{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001315 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001316 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001317 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001318 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001319 size_t prefix_len = 0;
1320 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001321
H. Peter Anvine2c80182005-01-15 22:15:51 +00001322 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001323 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1324 memcpy(sl->str, prefix, prefix_len);
1325 memcpy(sl->str+prefix_len, file, len+1);
1326 fp = fopen(sl->str, "r");
H. Peter Anvin418ca702008-05-30 10:42:30 -07001327 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001328 sl->next = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001329 **dtail = sl;
1330 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001331 } else {
1332 nasm_free(sl);
1333 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001334 if (fp)
1335 return fp;
H. Peter Anvin418ca702008-05-30 10:42:30 -07001336 if (!ip) {
1337 if (!missing_ok)
1338 break;
1339 prefix = NULL;
1340 } else {
1341 prefix = ip->path;
1342 ip = ip->next;
1343 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001344 if (prefix) {
1345 prefix_len = strlen(prefix);
1346 } else {
1347 /* -MG given and file not found */
H. Peter Anvin418ca702008-05-30 10:42:30 -07001348 if (dhead && !in_list(*dhead, file)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001349 sl = nasm_malloc(len+1+sizeof sl->next);
1350 sl->next = NULL;
1351 strcpy(sl->str, file);
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001352 **dtail = sl;
1353 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001354 }
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001355 return NULL;
1356 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001357 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001358
H. Peter Anvin734b1882002-04-30 21:01:08 +00001359 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001360 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001361}
1362
1363/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001364 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001365 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001366 * return true if _any_ single-line macro of that name is defined.
1367 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001368 * `nparam' or no parameters is defined.
1369 *
1370 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001371 * defined, or nparam is -1, the address of the definition structure
1372 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001373 * is NULL, no action will be taken regarding its contents, and no
1374 * error will occur.
1375 *
1376 * Note that this is also called with nparam zero to resolve
1377 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001378 *
1379 * If you already know which context macro belongs to, you can pass
1380 * the context pointer as first parameter; if you won't but name begins
1381 * with %$ the context will be automatically computed. If all_contexts
1382 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001383 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001384static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001385smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001386 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001387{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001388 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001389 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001390
H. Peter Anvin97a23472007-09-16 17:57:25 -07001391 if (ctx) {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001392 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001393 } else if (name[0] == '%' && name[1] == '$') {
1394 if (cstk)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001395 ctx = get_ctx(name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001396 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001397 return false; /* got to return _something_ */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001398 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001399 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001400 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001401 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001402 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001403
H. Peter Anvine2c80182005-01-15 22:15:51 +00001404 while (m) {
1405 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001406 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001407 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001408 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001409 *defn = m;
1410 else
1411 *defn = NULL;
1412 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001413 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001414 }
1415 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001416 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001417
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001418 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001419}
1420
1421/*
1422 * Count and mark off the parameters in a multi-line macro call.
1423 * This is called both from within the multi-line macro expansion
1424 * code, and also to mark off the default parameters when provided
1425 * in a %macro definition line.
1426 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001427static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001428{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001429 int paramsize, brace;
1430
1431 *nparam = paramsize = 0;
1432 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433 while (t) {
1434 if (*nparam >= paramsize) {
1435 paramsize += PARAM_DELTA;
1436 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1437 }
1438 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001439 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001440 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001441 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001442 (*params)[(*nparam)++] = t;
1443 while (tok_isnt_(t, brace ? "}" : ","))
1444 t = t->next;
1445 if (t) { /* got a comma/brace */
1446 t = t->next;
1447 if (brace) {
1448 /*
1449 * Now we've found the closing brace, look further
1450 * for the comma.
1451 */
1452 skip_white_(t);
1453 if (tok_isnt_(t, ",")) {
1454 error(ERR_NONFATAL,
1455 "braces do not enclose all of macro parameter");
1456 while (tok_isnt_(t, ","))
1457 t = t->next;
1458 }
1459 if (t)
1460 t = t->next; /* eat the comma */
1461 }
1462 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001463 }
1464}
1465
1466/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001467 * Determine whether one of the various `if' conditions is true or
1468 * not.
1469 *
1470 * We must free the tline we get passed.
1471 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001472static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001473{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001474 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001475 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001476 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001477 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001478 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001479 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001480
1481 origline = tline;
1482
H. Peter Anvine2c80182005-01-15 22:15:51 +00001483 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001484 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001485 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001486 while (cstk && tline) {
1487 skip_white_(tline);
1488 if (!tline || tline->type != TOK_ID) {
1489 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001490 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001491 free_tlist(origline);
1492 return -1;
1493 }
1494 if (!nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001495 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001496 tline = tline->next;
1497 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001498 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001499
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001500 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001501 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001502 while (tline) {
1503 skip_white_(tline);
1504 if (!tline || (tline->type != TOK_ID &&
1505 (tline->type != TOK_PREPROC_ID ||
1506 tline->text[1] != '$'))) {
1507 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001508 "`%s' expects macro identifiers", pp_directives[ct]);
1509 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001510 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001511 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001512 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001513 tline = tline->next;
1514 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001515 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001516
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001517 case PPC_IFIDN:
1518 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001519 tline = expand_smacro(tline);
1520 t = tt = tline;
1521 while (tok_isnt_(tt, ","))
1522 tt = tt->next;
1523 if (!tt) {
1524 error(ERR_NONFATAL,
1525 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001526 pp_directives[ct]);
1527 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001528 }
1529 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001530 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001531 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1532 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1533 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001534 pp_directives[ct]);
1535 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001536 }
1537 if (t->type == TOK_WHITESPACE) {
1538 t = t->next;
1539 continue;
1540 }
1541 if (tt->type == TOK_WHITESPACE) {
1542 tt = tt->next;
1543 continue;
1544 }
1545 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001546 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001547 break;
1548 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001549 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001550 if (t->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001551 size_t l1 = nasm_unquote(t->text, NULL);
1552 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001553
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001554 if (l1 != l2) {
1555 j = false;
1556 break;
1557 }
1558 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1559 j = false;
1560 break;
1561 }
1562 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001563 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001564 break;
1565 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001566
H. Peter Anvine2c80182005-01-15 22:15:51 +00001567 t = t->next;
1568 tt = tt->next;
1569 }
1570 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001571 j = false; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001572 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001573
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001574 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001575 {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001576 bool found = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001577 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001578
H. Peter Anvine2c80182005-01-15 22:15:51 +00001579 tline = tline->next;
1580 skip_white_(tline);
1581 tline = expand_id(tline);
1582 if (!tok_type_(tline, TOK_ID)) {
1583 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001584 "`%s' expects a macro name", pp_directives[ct]);
1585 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001586 }
1587 searching.name = nasm_strdup(tline->text);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001588 searching.casesense = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001589 searching.plus = false;
1590 searching.nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001591 searching.in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001592 searching.rep_nest = NULL;
1593 searching.nparam_min = 0;
1594 searching.nparam_max = INT_MAX;
1595 tline = expand_smacro(tline->next);
1596 skip_white_(tline);
1597 if (!tline) {
1598 } else if (!tok_type_(tline, TOK_NUMBER)) {
1599 error(ERR_NONFATAL,
1600 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001601 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001602 } else {
1603 searching.nparam_min = searching.nparam_max =
1604 readnum(tline->text, &j);
1605 if (j)
1606 error(ERR_NONFATAL,
1607 "unable to parse parameter count `%s'",
1608 tline->text);
1609 }
1610 if (tline && tok_is_(tline->next, "-")) {
1611 tline = tline->next->next;
1612 if (tok_is_(tline, "*"))
1613 searching.nparam_max = INT_MAX;
1614 else if (!tok_type_(tline, TOK_NUMBER))
1615 error(ERR_NONFATAL,
1616 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001617 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001618 else {
1619 searching.nparam_max = readnum(tline->text, &j);
1620 if (j)
1621 error(ERR_NONFATAL,
1622 "unable to parse parameter count `%s'",
1623 tline->text);
1624 if (searching.nparam_min > searching.nparam_max)
1625 error(ERR_NONFATAL,
1626 "minimum parameter count exceeds maximum");
1627 }
1628 }
1629 if (tline && tok_is_(tline->next, "+")) {
1630 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001631 searching.plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001632 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001633 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001634 while (mmac) {
1635 if (!strcmp(mmac->name, searching.name) &&
1636 (mmac->nparam_min <= searching.nparam_max
1637 || searching.plus)
1638 && (searching.nparam_min <= mmac->nparam_max
1639 || mmac->plus)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001640 found = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001641 break;
1642 }
1643 mmac = mmac->next;
1644 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001646 j = found;
1647 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001648 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001649
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001650 case PPC_IFID:
1651 needtype = TOK_ID;
1652 goto iftype;
1653 case PPC_IFNUM:
1654 needtype = TOK_NUMBER;
1655 goto iftype;
1656 case PPC_IFSTR:
1657 needtype = TOK_STRING;
1658 goto iftype;
1659
1660 iftype:
H. Peter Anvin134b9462008-02-16 17:01:40 -08001661 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001662
H. Peter Anvin927c92b2008-02-16 13:44:52 -08001663 while (tok_type_(t, TOK_WHITESPACE) ||
1664 (needtype == TOK_NUMBER &&
1665 tok_type_(t, TOK_OTHER) &&
1666 (t->text[0] == '-' || t->text[0] == '+') &&
1667 !t->text[1]))
1668 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001669
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001670 j = tok_type_(t, needtype);
1671 break;
1672
1673 case PPC_IFTOKEN:
1674 t = tline = expand_smacro(tline);
1675 while (tok_type_(t, TOK_WHITESPACE))
1676 t = t->next;
1677
1678 j = false;
1679 if (t) {
1680 t = t->next; /* Skip the actual token */
1681 while (tok_type_(t, TOK_WHITESPACE))
1682 t = t->next;
1683 j = !t; /* Should be nothing left */
1684 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001685 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001686
H. Peter Anvin134b9462008-02-16 17:01:40 -08001687 case PPC_IFEMPTY:
1688 t = tline = expand_smacro(tline);
1689 while (tok_type_(t, TOK_WHITESPACE))
1690 t = t->next;
1691
1692 j = !t; /* Should be empty */
1693 break;
1694
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001695 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001696 t = tline = expand_smacro(tline);
1697 tptr = &t;
1698 tokval.t_type = TOKEN_INVALID;
1699 evalresult = evaluate(ppscan, tptr, &tokval,
1700 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001701 if (!evalresult)
1702 return -1;
1703 if (tokval.t_type)
1704 error(ERR_WARNING,
1705 "trailing garbage after expression ignored");
1706 if (!is_simple(evalresult)) {
1707 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001708 "non-constant value given to `%s'", pp_directives[ct]);
1709 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001710 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001711 j = reloc_value(evalresult) != 0;
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001712 return j;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001713
H. Peter Anvine2c80182005-01-15 22:15:51 +00001714 default:
1715 error(ERR_FATAL,
1716 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001717 pp_directives[ct]);
1718 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001719 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001720
1721 free_tlist(origline);
1722 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001723
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001724fail:
1725 free_tlist(origline);
1726 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001727}
1728
1729/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001730 * Common code for defining an smacro
1731 */
1732static bool define_smacro(Context *ctx, char *mname, bool casesense,
1733 int nparam, Token *expansion)
1734{
1735 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001736 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001737
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001738 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1739 if (!smac) {
1740 error(ERR_WARNING,
1741 "single-line macro `%s' defined both with and"
1742 " without parameters", mname);
1743
1744 /* Some instances of the old code considered this a failure,
1745 some others didn't. What is the right thing to do here? */
1746 free_tlist(expansion);
1747 return false; /* Failure */
1748 } else {
1749 /*
1750 * We're redefining, so we have to take over an
1751 * existing SMacro structure. This means freeing
1752 * what was already in it.
1753 */
1754 nasm_free(smac->name);
1755 free_tlist(smac->expansion);
1756 }
1757 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001758 smtbl = ctx ? &ctx->localmac : &smacros;
1759 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001760 smac = nasm_malloc(sizeof(SMacro));
1761 smac->next = *smhead;
1762 *smhead = smac;
1763 }
1764 smac->name = nasm_strdup(mname);
1765 smac->casesense = casesense;
1766 smac->nparam = nparam;
1767 smac->expansion = expansion;
1768 smac->in_progress = false;
1769 return true; /* Success */
1770}
1771
1772/*
1773 * Undefine an smacro
1774 */
1775static void undef_smacro(Context *ctx, const char *mname)
1776{
1777 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001778 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001779
H. Peter Anvin166c2472008-05-28 12:28:58 -07001780 smtbl = ctx ? &ctx->localmac : &smacros;
1781 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001782
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001783 if (smhead) {
1784 /*
1785 * We now have a macro name... go hunt for it.
1786 */
1787 sp = smhead;
1788 while ((s = *sp) != NULL) {
1789 if (!mstrcmp(s->name, mname, s->casesense)) {
1790 *sp = s->next;
1791 nasm_free(s->name);
1792 free_tlist(s->expansion);
1793 nasm_free(s);
1794 } else {
1795 sp = &s->next;
1796 }
1797 }
1798 }
1799}
1800
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001801/*
1802 * Decode a size directive
1803 */
1804static int parse_size(const char *str) {
1805 static const char *size_names[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001806 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001807 static const int sizes[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001808 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001809
1810 return sizes[bsii(str, size_names, elements(size_names))+1];
1811}
1812
Ed Beroset3ab3f412002-06-11 03:31:49 +00001813/**
1814 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001815 * Find out if a line contains a preprocessor directive, and deal
1816 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07001817 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001818 * If a directive _is_ found, it is the responsibility of this routine
1819 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001820 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001821 * @param tline a pointer to the current tokeninzed line linked list
1822 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07001823 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001824 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001825static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001826{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001827 enum preproc_token i;
1828 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07001829 bool err;
1830 int nparam;
1831 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001832 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001833 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001834 int offset;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001835 char *p, *pp, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001836 Include *inc;
1837 Context *ctx;
1838 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001839 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001840 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1841 Line *l;
1842 struct tokenval tokval;
1843 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001844 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001845 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001846 size_t len;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001847
1848 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001849
H. Peter Anvineba20a72002-04-30 20:53:55 +00001850 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07001851 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001852 (tline->text[1] == '%' || tline->text[1] == '$'
1853 || tline->text[1] == '!'))
1854 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001855
H. Peter Anvin4169a472007-09-12 01:29:43 +00001856 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001857
1858 /*
1859 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001860 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001861 * we should ignore all directives except for condition
1862 * directives.
1863 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001864 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001865 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1866 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001867 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001868
1869 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001870 * If we're defining a macro or reading a %rep block, we should
1871 * ignore all directives except for %macro/%imacro (which
1872 * generate an error), %endm/%endmacro, and (only if we're in a
H. Peter Anvineba20a72002-04-30 20:53:55 +00001873 * %rep block) %endrep. If we're in a %rep block, another %rep
1874 * causes an error, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001875 */
1876 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001877 i != PP_ENDMACRO && i != PP_ENDM &&
1878 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1879 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001880 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001881
H. Peter Anvin4169a472007-09-12 01:29:43 +00001882 switch (i) {
1883 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001884 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1885 tline->text);
1886 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001887
H. Peter Anvine2c80182005-01-15 22:15:51 +00001888 case PP_STACKSIZE:
1889 /* Directive to tell NASM what the default stack size is. The
1890 * default is for a 16-bit stack, and this can be overriden with
1891 * %stacksize large.
1892 * the following form:
1893 *
1894 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1895 */
1896 tline = tline->next;
1897 if (tline && tline->type == TOK_WHITESPACE)
1898 tline = tline->next;
1899 if (!tline || tline->type != TOK_ID) {
1900 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1901 free_tlist(origline);
1902 return DIRECTIVE_FOUND;
1903 }
1904 if (nasm_stricmp(tline->text, "flat") == 0) {
1905 /* All subsequent ARG directives are for a 32-bit stack */
1906 StackSize = 4;
1907 StackPointer = "ebp";
1908 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001909 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08001910 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
1911 /* All subsequent ARG directives are for a 64-bit stack */
1912 StackSize = 8;
1913 StackPointer = "rbp";
1914 ArgOffset = 8;
1915 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001916 } else if (nasm_stricmp(tline->text, "large") == 0) {
1917 /* All subsequent ARG directives are for a 16-bit stack,
1918 * far function call.
1919 */
1920 StackSize = 2;
1921 StackPointer = "bp";
1922 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001923 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001924 } else if (nasm_stricmp(tline->text, "small") == 0) {
1925 /* All subsequent ARG directives are for a 16-bit stack,
1926 * far function call. We don't support near functions.
1927 */
1928 StackSize = 2;
1929 StackPointer = "bp";
1930 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001931 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001932 } else {
1933 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1934 free_tlist(origline);
1935 return DIRECTIVE_FOUND;
1936 }
1937 free_tlist(origline);
1938 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001939
H. Peter Anvine2c80182005-01-15 22:15:51 +00001940 case PP_ARG:
1941 /* TASM like ARG directive to define arguments to functions, in
1942 * the following form:
1943 *
1944 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1945 */
1946 offset = ArgOffset;
1947 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001948 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001949 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001950
H. Peter Anvine2c80182005-01-15 22:15:51 +00001951 /* Find the argument name */
1952 tline = tline->next;
1953 if (tline && tline->type == TOK_WHITESPACE)
1954 tline = tline->next;
1955 if (!tline || tline->type != TOK_ID) {
1956 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1957 free_tlist(origline);
1958 return DIRECTIVE_FOUND;
1959 }
1960 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001961
H. Peter Anvine2c80182005-01-15 22:15:51 +00001962 /* Find the argument size type */
1963 tline = tline->next;
1964 if (!tline || tline->type != TOK_OTHER
1965 || tline->text[0] != ':') {
1966 error(ERR_NONFATAL,
1967 "Syntax error processing `%%arg' directive");
1968 free_tlist(origline);
1969 return DIRECTIVE_FOUND;
1970 }
1971 tline = tline->next;
1972 if (!tline || tline->type != TOK_ID) {
1973 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1974 free_tlist(origline);
1975 return DIRECTIVE_FOUND;
1976 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001977
H. Peter Anvine2c80182005-01-15 22:15:51 +00001978 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001979 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001980 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001981 size = parse_size(tt->text);
1982 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001983 error(ERR_NONFATAL,
1984 "Invalid size type for `%%arg' missing directive");
1985 free_tlist(tt);
1986 free_tlist(origline);
1987 return DIRECTIVE_FOUND;
1988 }
1989 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001990
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001991 /* Round up to even stack slots */
1992 size = (size+StackSize-1) & ~(StackSize-1);
1993
H. Peter Anvine2c80182005-01-15 22:15:51 +00001994 /* Now define the macro for the argument */
1995 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1996 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001997 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001998 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001999
H. Peter Anvine2c80182005-01-15 22:15:51 +00002000 /* Move to the next argument in the list */
2001 tline = tline->next;
2002 if (tline && tline->type == TOK_WHITESPACE)
2003 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002004 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2005 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002006 free_tlist(origline);
2007 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002008
H. Peter Anvine2c80182005-01-15 22:15:51 +00002009 case PP_LOCAL:
2010 /* TASM like LOCAL directive to define local variables for a
2011 * function, in the following form:
2012 *
2013 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2014 *
2015 * The '= LocalSize' at the end is ignored by NASM, but is
2016 * required by TASM to define the local parameter size (and used
2017 * by the TASM macro package).
2018 */
2019 offset = LocalOffset;
2020 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002021 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002022 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002023
H. Peter Anvine2c80182005-01-15 22:15:51 +00002024 /* Find the argument name */
2025 tline = tline->next;
2026 if (tline && tline->type == TOK_WHITESPACE)
2027 tline = tline->next;
2028 if (!tline || tline->type != TOK_ID) {
2029 error(ERR_NONFATAL,
2030 "`%%local' missing argument parameter");
2031 free_tlist(origline);
2032 return DIRECTIVE_FOUND;
2033 }
2034 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002035
H. Peter Anvine2c80182005-01-15 22:15:51 +00002036 /* Find the argument size type */
2037 tline = tline->next;
2038 if (!tline || tline->type != TOK_OTHER
2039 || tline->text[0] != ':') {
2040 error(ERR_NONFATAL,
2041 "Syntax error processing `%%local' directive");
2042 free_tlist(origline);
2043 return DIRECTIVE_FOUND;
2044 }
2045 tline = tline->next;
2046 if (!tline || tline->type != TOK_ID) {
2047 error(ERR_NONFATAL,
2048 "`%%local' missing size type parameter");
2049 free_tlist(origline);
2050 return DIRECTIVE_FOUND;
2051 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002052
H. Peter Anvine2c80182005-01-15 22:15:51 +00002053 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002054 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002055 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002056 size = parse_size(tt->text);
2057 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002058 error(ERR_NONFATAL,
2059 "Invalid size type for `%%local' missing directive");
2060 free_tlist(tt);
2061 free_tlist(origline);
2062 return DIRECTIVE_FOUND;
2063 }
2064 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002065
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002066 /* Round up to even stack slots */
2067 size = (size+StackSize-1) & ~(StackSize-1);
2068
2069 offset += size; /* Negative offset, increment before */
2070
2071 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002072 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2073 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002074 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002075
H. Peter Anvine2c80182005-01-15 22:15:51 +00002076 /* Now define the assign to setup the enter_c macro correctly */
2077 snprintf(directive, sizeof(directive),
2078 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002079 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002080
H. Peter Anvine2c80182005-01-15 22:15:51 +00002081 /* Move to the next argument in the list */
2082 tline = tline->next;
2083 if (tline && tline->type == TOK_WHITESPACE)
2084 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002085 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2086 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002087 free_tlist(origline);
2088 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002089
H. Peter Anvine2c80182005-01-15 22:15:51 +00002090 case PP_CLEAR:
2091 if (tline->next)
2092 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002093 free_macros();
2094 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002095 free_tlist(origline);
2096 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002097
H. Peter Anvin418ca702008-05-30 10:42:30 -07002098 case PP_DEPEND:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002099 t = tline->next = expand_smacro(tline->next);
2100 skip_white_(t);
2101 if (!t || (t->type != TOK_STRING &&
2102 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002103 error(ERR_NONFATAL, "`%%depend' expects a file name");
2104 free_tlist(origline);
2105 return DIRECTIVE_FOUND; /* but we did _something_ */
2106 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002107 if (t->next)
H. Peter Anvin418ca702008-05-30 10:42:30 -07002108 error(ERR_WARNING,
2109 "trailing garbage after `%%depend' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002110 p = t->text;
2111 if (t->type != TOK_INTERNAL_STRING)
2112 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002113 if (dephead && !in_list(*dephead, p)) {
2114 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2115 sl->next = NULL;
2116 strcpy(sl->str, p);
2117 *deptail = sl;
2118 deptail = &sl->next;
2119 }
2120 free_tlist(origline);
2121 return DIRECTIVE_FOUND;
2122
2123 case PP_INCLUDE:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002124 t = tline->next = expand_smacro(tline->next);
2125 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002126
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002127 if (!t || (t->type != TOK_STRING &&
2128 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002129 error(ERR_NONFATAL, "`%%include' expects a file name");
2130 free_tlist(origline);
2131 return DIRECTIVE_FOUND; /* but we did _something_ */
2132 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002133 if (t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002134 error(ERR_WARNING,
2135 "trailing garbage after `%%include' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002136 p = t->text;
2137 if (t->type != TOK_INTERNAL_STRING)
2138 nasm_unquote(p, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002139 inc = nasm_malloc(sizeof(Include));
2140 inc->next = istk;
2141 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002142 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002143 if (!inc->fp) {
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002144 /* -MG given but file not found */
2145 nasm_free(inc);
2146 } else {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002147 inc->fname = src_set_fname(nasm_strdup(p));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002148 inc->lineno = src_set_linnum(0);
2149 inc->lineinc = 1;
2150 inc->expansion = NULL;
2151 inc->mstk = NULL;
2152 istk = inc;
2153 list->uplevel(LIST_INCLUDE);
2154 }
2155 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002156 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002157
H. Peter Anvind2456592008-06-19 15:04:18 -07002158 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002159 {
H. Peter Anvin7e50d232008-06-25 14:54:14 -07002160 static const macros_t *use_pkg;
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002161 const char *pkg_macro;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002162
H. Peter Anvind2456592008-06-19 15:04:18 -07002163 t = tline->next = expand_smacro(tline->next);
2164 skip_white_(t);
2165
2166 if (!t || (t->type != TOK_STRING &&
2167 t->type != TOK_INTERNAL_STRING &&
2168 t->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002169 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002170 free_tlist(origline);
2171 return DIRECTIVE_FOUND; /* but we did _something_ */
2172 }
2173 if (t->next)
2174 error(ERR_WARNING,
2175 "trailing garbage after `%%use' ignored");
H. Peter Anvind2456592008-06-19 15:04:18 -07002176 if (t->type == TOK_STRING)
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002177 nasm_unquote(t->text, NULL);
2178 use_pkg = nasm_stdmac_find_package(t->text);
2179 if (!use_pkg)
2180 error(ERR_NONFATAL, "unknown `%%use' package: %s", t->text);
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002181 /* The first string will be <%define>__USE_*__ */
H. Peter Anvin7e50d232008-06-25 14:54:14 -07002182 pkg_macro = (char *)use_pkg + 1;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002183 if (!smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2184 /* Not already included, go ahead and include it */
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002185 stdmacpos = use_pkg;
2186 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002187 free_tlist(origline);
2188 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002189 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002190 case PP_PUSH:
2191 tline = tline->next;
2192 skip_white_(tline);
2193 tline = expand_id(tline);
2194 if (!tok_type_(tline, TOK_ID)) {
2195 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2196 free_tlist(origline);
2197 return DIRECTIVE_FOUND; /* but we did _something_ */
2198 }
2199 if (tline->next)
2200 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2201 ctx = nasm_malloc(sizeof(Context));
2202 ctx->next = cstk;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002203 hash_init(&ctx->localmac, HASH_SMALL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002204 ctx->name = nasm_strdup(tline->text);
2205 ctx->number = unique++;
2206 cstk = ctx;
2207 free_tlist(origline);
2208 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002209
H. Peter Anvine2c80182005-01-15 22:15:51 +00002210 case PP_REPL:
2211 tline = tline->next;
2212 skip_white_(tline);
2213 tline = expand_id(tline);
2214 if (!tok_type_(tline, TOK_ID)) {
2215 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2216 free_tlist(origline);
2217 return DIRECTIVE_FOUND; /* but we did _something_ */
2218 }
2219 if (tline->next)
2220 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2221 if (!cstk)
2222 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2223 else {
2224 nasm_free(cstk->name);
2225 cstk->name = nasm_strdup(tline->text);
2226 }
2227 free_tlist(origline);
2228 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002229
H. Peter Anvine2c80182005-01-15 22:15:51 +00002230 case PP_POP:
2231 if (tline->next)
2232 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2233 if (!cstk)
2234 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2235 else
2236 ctx_pop();
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_ERROR:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002241 case PP_WARNING:
2242 {
2243 int severity = PP_ERROR ? ERR_NONFATAL|ERR_NO_SEVERITY :
2244 ERR_WARNING|ERR_NO_SEVERITY;
2245
H. Peter Anvine2c80182005-01-15 22:15:51 +00002246 tline->next = expand_smacro(tline->next);
2247 tline = tline->next;
2248 skip_white_(tline);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002249 t = tline ? tline->next : NULL;
2250 skip_white_(t);
2251 if (tok_type_(tline, TOK_STRING) && !t) {
2252 /* The line contains only a quoted string */
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002253 p = tline->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002254 nasm_unquote(p, NULL);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002255 error(severity, "%s: %s", pp_directives[i], p);
2256 } else {
2257 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002258 p = detoken(tline, false);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002259 error(severity, "%s: %s", pp_directives[i], p);
2260 nasm_free(p);
H. Peter Anvind2456592008-06-19 15:04:18 -07002261 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002262 free_tlist(origline);
2263 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002264 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002265
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002266 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002267 if (istk->conds && !emitting(istk->conds->state))
2268 j = COND_NEVER;
2269 else {
2270 j = if_condition(tline->next, i);
2271 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002272 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2273 }
2274 cond = nasm_malloc(sizeof(Cond));
2275 cond->next = istk->conds;
2276 cond->state = j;
2277 istk->conds = cond;
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002278 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002279 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002280
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002281 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002282 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002283 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002284 if (emitting(istk->conds->state)
2285 || istk->conds->state == COND_NEVER)
2286 istk->conds->state = COND_NEVER;
2287 else {
2288 /*
2289 * IMPORTANT: In the case of %if, we will already have
2290 * called expand_mmac_params(); however, if we're
2291 * processing an %elif we must have been in a
2292 * non-emitting mode, which would have inhibited
2293 * the normal invocation of expand_mmac_params(). Therefore,
2294 * we have to do it explicitly here.
2295 */
2296 j = if_condition(expand_mmac_params(tline->next), i);
2297 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002298 istk->conds->state =
2299 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2300 }
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002301 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002302 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002303
H. Peter Anvine2c80182005-01-15 22:15:51 +00002304 case PP_ELSE:
2305 if (tline->next)
2306 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2307 if (!istk->conds)
2308 error(ERR_FATAL, "`%%else': no matching `%%if'");
2309 if (emitting(istk->conds->state)
2310 || istk->conds->state == COND_NEVER)
2311 istk->conds->state = COND_ELSE_FALSE;
2312 else
2313 istk->conds->state = COND_ELSE_TRUE;
2314 free_tlist(origline);
2315 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002316
H. Peter Anvine2c80182005-01-15 22:15:51 +00002317 case PP_ENDIF:
2318 if (tline->next)
2319 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2320 if (!istk->conds)
2321 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2322 cond = istk->conds;
2323 istk->conds = cond->next;
2324 nasm_free(cond);
2325 free_tlist(origline);
2326 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002327
H. Peter Anvine2c80182005-01-15 22:15:51 +00002328 case PP_MACRO:
2329 case PP_IMACRO:
2330 if (defining)
2331 error(ERR_FATAL,
2332 "`%%%smacro': already defining a macro",
2333 (i == PP_IMACRO ? "i" : ""));
2334 tline = tline->next;
2335 skip_white_(tline);
2336 tline = expand_id(tline);
2337 if (!tok_type_(tline, TOK_ID)) {
2338 error(ERR_NONFATAL,
2339 "`%%%smacro' expects a macro name",
2340 (i == PP_IMACRO ? "i" : ""));
2341 return DIRECTIVE_FOUND;
2342 }
2343 defining = nasm_malloc(sizeof(MMacro));
2344 defining->name = nasm_strdup(tline->text);
2345 defining->casesense = (i == PP_MACRO);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002346 defining->plus = false;
2347 defining->nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002348 defining->in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002349 defining->rep_nest = NULL;
2350 tline = expand_smacro(tline->next);
2351 skip_white_(tline);
2352 if (!tok_type_(tline, TOK_NUMBER)) {
2353 error(ERR_NONFATAL,
2354 "`%%%smacro' expects a parameter count",
2355 (i == PP_IMACRO ? "i" : ""));
2356 defining->nparam_min = defining->nparam_max = 0;
2357 } else {
2358 defining->nparam_min = defining->nparam_max =
H. Peter Anvin70055962007-10-11 00:05:31 -07002359 readnum(tline->text, &err);
2360 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002361 error(ERR_NONFATAL,
2362 "unable to parse parameter count `%s'", tline->text);
2363 }
2364 if (tline && tok_is_(tline->next, "-")) {
2365 tline = tline->next->next;
2366 if (tok_is_(tline, "*"))
2367 defining->nparam_max = INT_MAX;
2368 else if (!tok_type_(tline, TOK_NUMBER))
2369 error(ERR_NONFATAL,
2370 "`%%%smacro' expects a parameter count after `-'",
2371 (i == PP_IMACRO ? "i" : ""));
2372 else {
H. Peter Anvin70055962007-10-11 00:05:31 -07002373 defining->nparam_max = readnum(tline->text, &err);
2374 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002375 error(ERR_NONFATAL,
2376 "unable to parse parameter count `%s'",
2377 tline->text);
2378 if (defining->nparam_min > defining->nparam_max)
2379 error(ERR_NONFATAL,
2380 "minimum parameter count exceeds maximum");
2381 }
2382 }
2383 if (tline && tok_is_(tline->next, "+")) {
2384 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002385 defining->plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002386 }
2387 if (tline && tok_type_(tline->next, TOK_ID) &&
2388 !nasm_stricmp(tline->next->text, ".nolist")) {
2389 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002390 defining->nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002391 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002392 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002393 while (mmac) {
2394 if (!strcmp(mmac->name, defining->name) &&
2395 (mmac->nparam_min <= defining->nparam_max
2396 || defining->plus)
2397 && (defining->nparam_min <= mmac->nparam_max
2398 || mmac->plus)) {
2399 error(ERR_WARNING,
2400 "redefining multi-line macro `%s'", defining->name);
2401 break;
2402 }
2403 mmac = mmac->next;
2404 }
2405 /*
2406 * Handle default parameters.
2407 */
2408 if (tline && tline->next) {
2409 defining->dlist = tline->next;
2410 tline->next = NULL;
2411 count_mmac_params(defining->dlist, &defining->ndefs,
2412 &defining->defaults);
2413 } else {
2414 defining->dlist = NULL;
2415 defining->defaults = NULL;
2416 }
2417 defining->expansion = NULL;
2418 free_tlist(origline);
2419 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002420
H. Peter Anvine2c80182005-01-15 22:15:51 +00002421 case PP_ENDM:
2422 case PP_ENDMACRO:
2423 if (!defining) {
2424 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2425 return DIRECTIVE_FOUND;
2426 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002427 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002428 defining->next = *mmhead;
2429 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002430 defining = NULL;
2431 free_tlist(origline);
2432 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002433
H. Peter Anvine2c80182005-01-15 22:15:51 +00002434 case PP_ROTATE:
2435 if (tline->next && tline->next->type == TOK_WHITESPACE)
2436 tline = tline->next;
2437 if (tline->next == NULL) {
2438 free_tlist(origline);
2439 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2440 return DIRECTIVE_FOUND;
2441 }
2442 t = expand_smacro(tline->next);
2443 tline->next = NULL;
2444 free_tlist(origline);
2445 tline = t;
2446 tptr = &t;
2447 tokval.t_type = TOKEN_INVALID;
2448 evalresult =
2449 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2450 free_tlist(tline);
2451 if (!evalresult)
2452 return DIRECTIVE_FOUND;
2453 if (tokval.t_type)
2454 error(ERR_WARNING,
2455 "trailing garbage after expression ignored");
2456 if (!is_simple(evalresult)) {
2457 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2458 return DIRECTIVE_FOUND;
2459 }
2460 mmac = istk->mstk;
2461 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2462 mmac = mmac->next_active;
2463 if (!mmac) {
2464 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2465 } else if (mmac->nparam == 0) {
2466 error(ERR_NONFATAL,
2467 "`%%rotate' invoked within macro without parameters");
2468 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002469 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002470
H. Peter Anvin25a99342007-09-22 17:45:45 -07002471 rotate %= (int)mmac->nparam;
2472 if (rotate < 0)
2473 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002474
H. Peter Anvin25a99342007-09-22 17:45:45 -07002475 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002476 }
2477 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002478
H. Peter Anvine2c80182005-01-15 22:15:51 +00002479 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002480 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002481 do {
2482 tline = tline->next;
2483 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002484
H. Peter Anvine2c80182005-01-15 22:15:51 +00002485 if (tok_type_(tline, TOK_ID) &&
2486 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002487 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002488 do {
2489 tline = tline->next;
2490 } while (tok_type_(tline, TOK_WHITESPACE));
2491 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002492
H. Peter Anvine2c80182005-01-15 22:15:51 +00002493 if (tline) {
2494 t = expand_smacro(tline);
2495 tptr = &t;
2496 tokval.t_type = TOKEN_INVALID;
2497 evalresult =
2498 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2499 if (!evalresult) {
2500 free_tlist(origline);
2501 return DIRECTIVE_FOUND;
2502 }
2503 if (tokval.t_type)
2504 error(ERR_WARNING,
2505 "trailing garbage after expression ignored");
2506 if (!is_simple(evalresult)) {
2507 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2508 return DIRECTIVE_FOUND;
2509 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002510 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002511 } else {
2512 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002513 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002514 }
2515 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002516
H. Peter Anvine2c80182005-01-15 22:15:51 +00002517 tmp_defining = defining;
2518 defining = nasm_malloc(sizeof(MMacro));
2519 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002520 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002521 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002522 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002523 defining->in_progress = count;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002524 defining->nparam_min = defining->nparam_max = 0;
2525 defining->defaults = NULL;
2526 defining->dlist = NULL;
2527 defining->expansion = NULL;
2528 defining->next_active = istk->mstk;
2529 defining->rep_nest = tmp_defining;
2530 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002531
H. Peter Anvine2c80182005-01-15 22:15:51 +00002532 case PP_ENDREP:
2533 if (!defining || defining->name) {
2534 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2535 return DIRECTIVE_FOUND;
2536 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002537
H. Peter Anvine2c80182005-01-15 22:15:51 +00002538 /*
2539 * Now we have a "macro" defined - although it has no name
2540 * and we won't be entering it in the hash tables - we must
2541 * push a macro-end marker for it on to istk->expansion.
2542 * After that, it will take care of propagating itself (a
2543 * macro-end marker line for a macro which is really a %rep
2544 * block will cause the macro to be re-expanded, complete
2545 * with another macro-end marker to ensure the process
2546 * continues) until the whole expansion is forcibly removed
2547 * from istk->expansion by a %exitrep.
2548 */
2549 l = nasm_malloc(sizeof(Line));
2550 l->next = istk->expansion;
2551 l->finishes = defining;
2552 l->first = NULL;
2553 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002554
H. Peter Anvine2c80182005-01-15 22:15:51 +00002555 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002556
H. Peter Anvine2c80182005-01-15 22:15:51 +00002557 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2558 tmp_defining = defining;
2559 defining = defining->rep_nest;
2560 free_tlist(origline);
2561 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002562
H. Peter Anvine2c80182005-01-15 22:15:51 +00002563 case PP_EXITREP:
2564 /*
2565 * We must search along istk->expansion until we hit a
2566 * macro-end marker for a macro with no name. Then we set
2567 * its `in_progress' flag to 0.
2568 */
2569 for (l = istk->expansion; l; l = l->next)
2570 if (l->finishes && !l->finishes->name)
2571 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002572
H. Peter Anvine2c80182005-01-15 22:15:51 +00002573 if (l)
2574 l->finishes->in_progress = 0;
2575 else
2576 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2577 free_tlist(origline);
2578 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002579
H. Peter Anvine2c80182005-01-15 22:15:51 +00002580 case PP_XDEFINE:
2581 case PP_IXDEFINE:
2582 case PP_DEFINE:
2583 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002584 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002585
H. Peter Anvine2c80182005-01-15 22:15:51 +00002586 tline = tline->next;
2587 skip_white_(tline);
2588 tline = expand_id(tline);
2589 if (!tline || (tline->type != TOK_ID &&
2590 (tline->type != TOK_PREPROC_ID ||
2591 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002592 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2593 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002594 free_tlist(origline);
2595 return DIRECTIVE_FOUND;
2596 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002597
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002598 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002599
H. Peter Anvine2c80182005-01-15 22:15:51 +00002600 mname = tline->text;
2601 last = tline;
2602 param_start = tline = tline->next;
2603 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002604
H. Peter Anvine2c80182005-01-15 22:15:51 +00002605 /* Expand the macro definition now for %xdefine and %ixdefine */
2606 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2607 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002608
H. Peter Anvine2c80182005-01-15 22:15:51 +00002609 if (tok_is_(tline, "(")) {
2610 /*
2611 * This macro has parameters.
2612 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002613
H. Peter Anvine2c80182005-01-15 22:15:51 +00002614 tline = tline->next;
2615 while (1) {
2616 skip_white_(tline);
2617 if (!tline) {
2618 error(ERR_NONFATAL, "parameter identifier expected");
2619 free_tlist(origline);
2620 return DIRECTIVE_FOUND;
2621 }
2622 if (tline->type != TOK_ID) {
2623 error(ERR_NONFATAL,
2624 "`%s': parameter identifier expected",
2625 tline->text);
2626 free_tlist(origline);
2627 return DIRECTIVE_FOUND;
2628 }
2629 tline->type = TOK_SMAC_PARAM + nparam++;
2630 tline = tline->next;
2631 skip_white_(tline);
2632 if (tok_is_(tline, ",")) {
2633 tline = tline->next;
2634 continue;
2635 }
2636 if (!tok_is_(tline, ")")) {
2637 error(ERR_NONFATAL,
2638 "`)' expected to terminate macro template");
2639 free_tlist(origline);
2640 return DIRECTIVE_FOUND;
2641 }
2642 break;
2643 }
2644 last = tline;
2645 tline = tline->next;
2646 }
2647 if (tok_type_(tline, TOK_WHITESPACE))
2648 last = tline, tline = tline->next;
2649 macro_start = NULL;
2650 last->next = NULL;
2651 t = tline;
2652 while (t) {
2653 if (t->type == TOK_ID) {
2654 for (tt = param_start; tt; tt = tt->next)
2655 if (tt->type >= TOK_SMAC_PARAM &&
2656 !strcmp(tt->text, t->text))
2657 t->type = tt->type;
2658 }
2659 tt = t->next;
2660 t->next = macro_start;
2661 macro_start = t;
2662 t = tt;
2663 }
2664 /*
2665 * Good. We now have a macro name, a parameter count, and a
2666 * token list (in reverse order) for an expansion. We ought
2667 * to be OK just to create an SMacro, store it, and let
2668 * free_tlist have the rest of the line (which we have
2669 * carefully re-terminated after chopping off the expansion
2670 * from the end).
2671 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002672 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002673 free_tlist(origline);
2674 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002675
H. Peter Anvine2c80182005-01-15 22:15:51 +00002676 case PP_UNDEF:
2677 tline = tline->next;
2678 skip_white_(tline);
2679 tline = expand_id(tline);
2680 if (!tline || (tline->type != TOK_ID &&
2681 (tline->type != TOK_PREPROC_ID ||
2682 tline->text[1] != '$'))) {
2683 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2684 free_tlist(origline);
2685 return DIRECTIVE_FOUND;
2686 }
2687 if (tline->next) {
2688 error(ERR_WARNING,
2689 "trailing garbage after macro name ignored");
2690 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002691
H. Peter Anvine2c80182005-01-15 22:15:51 +00002692 /* Find the context that symbol belongs to */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002693 ctx = get_ctx(tline->text, false);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002694 undef_smacro(ctx, tline->text);
2695 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002696 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002697
H. Peter Anvin9e200162008-06-04 17:23:14 -07002698 case PP_DEFSTR:
2699 case PP_IDEFSTR:
2700 casesense = (i == PP_DEFSTR);
2701
2702 tline = tline->next;
2703 skip_white_(tline);
2704 tline = expand_id(tline);
2705 if (!tline || (tline->type != TOK_ID &&
2706 (tline->type != TOK_PREPROC_ID ||
2707 tline->text[1] != '$'))) {
2708 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2709 pp_directives[i]);
2710 free_tlist(origline);
2711 return DIRECTIVE_FOUND;
2712 }
2713
2714 ctx = get_ctx(tline->text, false);
2715
2716 mname = tline->text;
2717 last = tline;
2718 tline = expand_smacro(tline->next);
2719 last->next = NULL;
2720
2721 while (tok_type_(tline, TOK_WHITESPACE))
2722 tline = delete_Token(tline);
2723
2724 p = detoken(tline, false);
2725 macro_start = nasm_malloc(sizeof(*macro_start));
2726 macro_start->next = NULL;
2727 macro_start->text = nasm_quote(p, strlen(p));
2728 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002729 macro_start->a.mac = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07002730 nasm_free(p);
2731
2732 /*
2733 * We now have a macro name, an implicit parameter count of
2734 * zero, and a string token to use as an expansion. Create
2735 * and store an SMacro.
2736 */
2737 define_smacro(ctx, mname, casesense, 0, macro_start);
2738 free_tlist(origline);
2739 return DIRECTIVE_FOUND;
2740
H. Peter Anvin418ca702008-05-30 10:42:30 -07002741 case PP_PATHSEARCH:
2742 {
2743 FILE *fp;
2744 StrList *xsl = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002745 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002746
2747 casesense = true;
2748
2749 tline = tline->next;
2750 skip_white_(tline);
2751 tline = expand_id(tline);
2752 if (!tline || (tline->type != TOK_ID &&
2753 (tline->type != TOK_PREPROC_ID ||
2754 tline->text[1] != '$'))) {
2755 error(ERR_NONFATAL,
2756 "`%%pathsearch' expects a macro identifier as first parameter");
2757 free_tlist(origline);
2758 return DIRECTIVE_FOUND;
2759 }
2760 ctx = get_ctx(tline->text, false);
2761
2762 mname = tline->text;
2763 last = tline;
2764 tline = expand_smacro(tline->next);
2765 last->next = NULL;
2766
2767 t = tline;
2768 while (tok_type_(t, TOK_WHITESPACE))
2769 t = t->next;
2770
2771 if (!t || (t->type != TOK_STRING &&
2772 t->type != TOK_INTERNAL_STRING)) {
2773 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
2774 free_tlist(tline);
2775 free_tlist(origline);
2776 return DIRECTIVE_FOUND; /* but we did _something_ */
2777 }
2778 if (t->next)
2779 error(ERR_WARNING,
2780 "trailing garbage after `%%pathsearch' ignored");
H. Peter Anvin427cc912008-06-01 21:43:03 -07002781 p = t->text;
2782 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002783 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002784
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002785 fp = inc_fopen(p, &xsl, &xst, true);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002786 if (fp) {
2787 p = xsl->str;
2788 fclose(fp); /* Don't actually care about the file */
2789 }
2790 macro_start = nasm_malloc(sizeof(*macro_start));
2791 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002792 macro_start->text = nasm_quote(p, strlen(p));
H. Peter Anvin418ca702008-05-30 10:42:30 -07002793 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002794 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002795 if (xsl)
2796 nasm_free(xsl);
2797
2798 /*
2799 * We now have a macro name, an implicit parameter count of
2800 * zero, and a string token to use as an expansion. Create
2801 * and store an SMacro.
2802 */
2803 define_smacro(ctx, mname, casesense, 0, macro_start);
2804 free_tlist(tline);
2805 free_tlist(origline);
2806 return DIRECTIVE_FOUND;
2807 }
2808
H. Peter Anvine2c80182005-01-15 22:15:51 +00002809 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002810 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07002811
H. Peter Anvine2c80182005-01-15 22:15:51 +00002812 tline = tline->next;
2813 skip_white_(tline);
2814 tline = expand_id(tline);
2815 if (!tline || (tline->type != TOK_ID &&
2816 (tline->type != TOK_PREPROC_ID ||
2817 tline->text[1] != '$'))) {
2818 error(ERR_NONFATAL,
2819 "`%%strlen' expects a macro identifier as first parameter");
2820 free_tlist(origline);
2821 return DIRECTIVE_FOUND;
2822 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002823 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002824
H. Peter Anvine2c80182005-01-15 22:15:51 +00002825 mname = tline->text;
2826 last = tline;
2827 tline = expand_smacro(tline->next);
2828 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002829
H. Peter Anvine2c80182005-01-15 22:15:51 +00002830 t = tline;
2831 while (tok_type_(t, TOK_WHITESPACE))
2832 t = t->next;
2833 /* t should now point to the string */
2834 if (t->type != TOK_STRING) {
2835 error(ERR_NONFATAL,
2836 "`%%strlen` requires string as second parameter");
2837 free_tlist(tline);
2838 free_tlist(origline);
2839 return DIRECTIVE_FOUND;
2840 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002841
H. Peter Anvine2c80182005-01-15 22:15:51 +00002842 macro_start = nasm_malloc(sizeof(*macro_start));
2843 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002844 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002845 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002846
H. Peter Anvine2c80182005-01-15 22:15:51 +00002847 /*
2848 * We now have a macro name, an implicit parameter count of
2849 * zero, and a numeric token to use as an expansion. Create
2850 * and store an SMacro.
2851 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002852 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002853 free_tlist(tline);
2854 free_tlist(origline);
2855 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002856
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002857 case PP_STRCAT:
2858 casesense = true;
2859
2860 tline = tline->next;
2861 skip_white_(tline);
2862 tline = expand_id(tline);
2863 if (!tline || (tline->type != TOK_ID &&
2864 (tline->type != TOK_PREPROC_ID ||
2865 tline->text[1] != '$'))) {
2866 error(ERR_NONFATAL,
2867 "`%%strcat' expects a macro identifier as first parameter");
2868 free_tlist(origline);
2869 return DIRECTIVE_FOUND;
2870 }
2871 ctx = get_ctx(tline->text, false);
2872
2873 mname = tline->text;
2874 last = tline;
2875 tline = expand_smacro(tline->next);
2876 last->next = NULL;
2877
2878 len = 0;
2879 for (t = tline; t; t = t->next) {
2880 switch (t->type) {
2881 case TOK_WHITESPACE:
2882 break;
2883 case TOK_STRING:
2884 len += t->a.len = nasm_unquote(t->text, NULL);
2885 break;
2886 default:
2887 error(ERR_NONFATAL,
2888 "non-string passed to `%%strcat' (%d)", t->type);
2889 free_tlist(tline);
2890 free_tlist(origline);
2891 return DIRECTIVE_FOUND;
2892 }
2893 }
2894
2895 p = pp = nasm_malloc(len);
2896 t = tline;
2897 for (t = tline; t; t = t->next) {
2898 if (t->type == TOK_STRING) {
2899 memcpy(p, t->text, t->a.len);
2900 p += t->a.len;
2901 }
2902 }
2903
2904 /*
2905 * We now have a macro name, an implicit parameter count of
2906 * zero, and a numeric token to use as an expansion. Create
2907 * and store an SMacro.
2908 */
2909 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
2910 macro_start->text = nasm_quote(pp, len);
2911 nasm_free(pp);
2912 define_smacro(ctx, mname, casesense, 0, macro_start);
2913 free_tlist(tline);
2914 free_tlist(origline);
2915 return DIRECTIVE_FOUND;
2916
H. Peter Anvine2c80182005-01-15 22:15:51 +00002917 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002918 {
2919 int64_t a1, a2;
2920 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07002921
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002922 casesense = true;
2923
H. Peter Anvine2c80182005-01-15 22:15:51 +00002924 tline = tline->next;
2925 skip_white_(tline);
2926 tline = expand_id(tline);
2927 if (!tline || (tline->type != TOK_ID &&
2928 (tline->type != TOK_PREPROC_ID ||
2929 tline->text[1] != '$'))) {
2930 error(ERR_NONFATAL,
2931 "`%%substr' expects a macro identifier as first parameter");
2932 free_tlist(origline);
2933 return DIRECTIVE_FOUND;
2934 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002935 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002936
H. Peter Anvine2c80182005-01-15 22:15:51 +00002937 mname = tline->text;
2938 last = tline;
2939 tline = expand_smacro(tline->next);
2940 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002941
H. Peter Anvine2c80182005-01-15 22:15:51 +00002942 t = tline->next;
2943 while (tok_type_(t, TOK_WHITESPACE))
2944 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002945
H. Peter Anvine2c80182005-01-15 22:15:51 +00002946 /* t should now point to the string */
2947 if (t->type != TOK_STRING) {
2948 error(ERR_NONFATAL,
2949 "`%%substr` requires string as second parameter");
2950 free_tlist(tline);
2951 free_tlist(origline);
2952 return DIRECTIVE_FOUND;
2953 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002954
H. Peter Anvine2c80182005-01-15 22:15:51 +00002955 tt = t->next;
2956 tptr = &tt;
2957 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002958 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
2959 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002960 if (!evalresult) {
2961 free_tlist(tline);
2962 free_tlist(origline);
2963 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002964 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002965 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2966 free_tlist(tline);
2967 free_tlist(origline);
2968 return DIRECTIVE_FOUND;
2969 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002970 a1 = evalresult->value-1;
2971
2972 while (tok_type_(tt, TOK_WHITESPACE))
2973 tt = tt->next;
2974 if (!tt) {
2975 a2 = 1; /* Backwards compatibility: one character */
2976 } else {
2977 tokval.t_type = TOKEN_INVALID;
2978 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
2979 pass, error, NULL);
2980 if (!evalresult) {
2981 free_tlist(tline);
2982 free_tlist(origline);
2983 return DIRECTIVE_FOUND;
2984 } else if (!is_simple(evalresult)) {
2985 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2986 free_tlist(tline);
2987 free_tlist(origline);
2988 return DIRECTIVE_FOUND;
2989 }
2990 a2 = evalresult->value;
2991 }
2992
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002993 len = nasm_unquote(t->text, NULL);
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002994 if (a2 < 0)
2995 a2 = a2+1+len-a1;
2996 if (a1+a2 > (int64_t)len)
2997 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002998
H. Peter Anvine2c80182005-01-15 22:15:51 +00002999 macro_start = nasm_malloc(sizeof(*macro_start));
3000 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003001 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003002 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003003 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003004
H. Peter Anvine2c80182005-01-15 22:15:51 +00003005 /*
3006 * We now have a macro name, an implicit parameter count of
3007 * zero, and a numeric token to use as an expansion. Create
3008 * and store an SMacro.
3009 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003010 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003011 free_tlist(tline);
3012 free_tlist(origline);
3013 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003014 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003015
H. Peter Anvine2c80182005-01-15 22:15:51 +00003016 case PP_ASSIGN:
3017 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003018 casesense = (i == PP_ASSIGN);
3019
H. Peter Anvine2c80182005-01-15 22:15:51 +00003020 tline = tline->next;
3021 skip_white_(tline);
3022 tline = expand_id(tline);
3023 if (!tline || (tline->type != TOK_ID &&
3024 (tline->type != TOK_PREPROC_ID ||
3025 tline->text[1] != '$'))) {
3026 error(ERR_NONFATAL,
3027 "`%%%sassign' expects a macro identifier",
3028 (i == PP_IASSIGN ? "i" : ""));
3029 free_tlist(origline);
3030 return DIRECTIVE_FOUND;
3031 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003032 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003033
H. Peter Anvine2c80182005-01-15 22:15:51 +00003034 mname = tline->text;
3035 last = tline;
3036 tline = expand_smacro(tline->next);
3037 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003038
H. Peter Anvine2c80182005-01-15 22:15:51 +00003039 t = tline;
3040 tptr = &t;
3041 tokval.t_type = TOKEN_INVALID;
3042 evalresult =
3043 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3044 free_tlist(tline);
3045 if (!evalresult) {
3046 free_tlist(origline);
3047 return DIRECTIVE_FOUND;
3048 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003049
H. Peter Anvine2c80182005-01-15 22:15:51 +00003050 if (tokval.t_type)
3051 error(ERR_WARNING,
3052 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003053
H. Peter Anvine2c80182005-01-15 22:15:51 +00003054 if (!is_simple(evalresult)) {
3055 error(ERR_NONFATAL,
3056 "non-constant value given to `%%%sassign'",
3057 (i == PP_IASSIGN ? "i" : ""));
3058 free_tlist(origline);
3059 return DIRECTIVE_FOUND;
3060 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003061
H. Peter Anvine2c80182005-01-15 22:15:51 +00003062 macro_start = nasm_malloc(sizeof(*macro_start));
3063 macro_start->next = NULL;
3064 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003065 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003066
H. Peter Anvine2c80182005-01-15 22:15:51 +00003067 /*
3068 * We now have a macro name, an implicit parameter count of
3069 * zero, and a numeric token to use as an expansion. Create
3070 * and store an SMacro.
3071 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003072 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003073 free_tlist(origline);
3074 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003075
H. Peter Anvine2c80182005-01-15 22:15:51 +00003076 case PP_LINE:
3077 /*
3078 * Syntax is `%line nnn[+mmm] [filename]'
3079 */
3080 tline = tline->next;
3081 skip_white_(tline);
3082 if (!tok_type_(tline, TOK_NUMBER)) {
3083 error(ERR_NONFATAL, "`%%line' expects line number");
3084 free_tlist(origline);
3085 return DIRECTIVE_FOUND;
3086 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003087 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003088 m = 1;
3089 tline = tline->next;
3090 if (tok_is_(tline, "+")) {
3091 tline = tline->next;
3092 if (!tok_type_(tline, TOK_NUMBER)) {
3093 error(ERR_NONFATAL, "`%%line' expects line increment");
3094 free_tlist(origline);
3095 return DIRECTIVE_FOUND;
3096 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003097 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003098 tline = tline->next;
3099 }
3100 skip_white_(tline);
3101 src_set_linnum(k);
3102 istk->lineinc = m;
3103 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003104 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003105 }
3106 free_tlist(origline);
3107 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003108
H. Peter Anvine2c80182005-01-15 22:15:51 +00003109 default:
3110 error(ERR_FATAL,
3111 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003112 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003113 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003114 }
Ed Beroset3ab3f412002-06-11 03:31:49 +00003115 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003116}
3117
3118/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003119 * Ensure that a macro parameter contains a condition code and
3120 * nothing else. Return the condition code index if so, or -1
3121 * otherwise.
3122 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003123static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003124{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003125 Token *tt;
3126 int i, j, k, m;
3127
H. Peter Anvin25a99342007-09-22 17:45:45 -07003128 if (!t)
3129 return -1; /* Probably a %+ without a space */
3130
H. Peter Anvineba20a72002-04-30 20:53:55 +00003131 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003132 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003133 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003134 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003135 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003136 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003137 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003138
3139 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00003140 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003141 while (j - i > 1) {
3142 k = (j + i) / 2;
3143 m = nasm_stricmp(t->text, conditions[k]);
3144 if (m == 0) {
3145 i = k;
3146 j = -2;
3147 break;
3148 } else if (m < 0) {
3149 j = k;
3150 } else
3151 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003152 }
3153 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003154 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003155 return i;
3156}
3157
3158/*
3159 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3160 * %-n) and MMacro-local identifiers (%%foo).
3161 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003162static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003163{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003164 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003165
3166 tail = &thead;
3167 thead = NULL;
3168
H. Peter Anvine2c80182005-01-15 22:15:51 +00003169 while (tline) {
3170 if (tline->type == TOK_PREPROC_ID &&
3171 (((tline->text[1] == '+' || tline->text[1] == '-')
3172 && tline->text[2]) || tline->text[1] == '%'
3173 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003174 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003175 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003176 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003177 unsigned int n;
3178 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003179 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003180
H. Peter Anvine2c80182005-01-15 22:15:51 +00003181 t = tline;
3182 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003183
H. Peter Anvine2c80182005-01-15 22:15:51 +00003184 mac = istk->mstk;
3185 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3186 mac = mac->next_active;
3187 if (!mac)
3188 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3189 else
3190 switch (t->text[1]) {
3191 /*
3192 * We have to make a substitution of one of the
3193 * forms %1, %-1, %+1, %%foo, %0.
3194 */
3195 case '0':
3196 type = TOK_NUMBER;
3197 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3198 text = nasm_strdup(tmpbuf);
3199 break;
3200 case '%':
3201 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003202 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00003203 mac->unique);
3204 text = nasm_strcat(tmpbuf, t->text + 2);
3205 break;
3206 case '-':
3207 n = atoi(t->text + 2) - 1;
3208 if (n >= mac->nparam)
3209 tt = NULL;
3210 else {
3211 if (mac->nparam > 1)
3212 n = (n + mac->rotate) % mac->nparam;
3213 tt = mac->params[n];
3214 }
3215 cc = find_cc(tt);
3216 if (cc == -1) {
3217 error(ERR_NONFATAL,
3218 "macro parameter %d is not a condition code",
3219 n + 1);
3220 text = NULL;
3221 } else {
3222 type = TOK_ID;
3223 if (inverse_ccs[cc] == -1) {
3224 error(ERR_NONFATAL,
3225 "condition code `%s' is not invertible",
3226 conditions[cc]);
3227 text = NULL;
3228 } else
3229 text =
3230 nasm_strdup(conditions[inverse_ccs[cc]]);
3231 }
3232 break;
3233 case '+':
3234 n = atoi(t->text + 2) - 1;
3235 if (n >= mac->nparam)
3236 tt = NULL;
3237 else {
3238 if (mac->nparam > 1)
3239 n = (n + mac->rotate) % mac->nparam;
3240 tt = mac->params[n];
3241 }
3242 cc = find_cc(tt);
3243 if (cc == -1) {
3244 error(ERR_NONFATAL,
3245 "macro parameter %d is not a condition code",
3246 n + 1);
3247 text = NULL;
3248 } else {
3249 type = TOK_ID;
3250 text = nasm_strdup(conditions[cc]);
3251 }
3252 break;
3253 default:
3254 n = atoi(t->text + 1) - 1;
3255 if (n >= mac->nparam)
3256 tt = NULL;
3257 else {
3258 if (mac->nparam > 1)
3259 n = (n + mac->rotate) % mac->nparam;
3260 tt = mac->params[n];
3261 }
3262 if (tt) {
3263 for (i = 0; i < mac->paramlen[n]; i++) {
3264 *tail = new_Token(NULL, tt->type, tt->text, 0);
3265 tail = &(*tail)->next;
3266 tt = tt->next;
3267 }
3268 }
3269 text = NULL; /* we've done it here */
3270 break;
3271 }
3272 if (!text) {
3273 delete_Token(t);
3274 } else {
3275 *tail = t;
3276 tail = &t->next;
3277 t->type = type;
3278 nasm_free(t->text);
3279 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003280 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003281 }
3282 continue;
3283 } else {
3284 t = *tail = tline;
3285 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003286 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003287 tail = &t->next;
3288 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003289 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003290 *tail = NULL;
3291 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003292 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003293 switch (t->type) {
3294 case TOK_WHITESPACE:
3295 if (tt->type == TOK_WHITESPACE) {
3296 t->next = delete_Token(tt);
3297 }
3298 break;
3299 case TOK_ID:
3300 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003301 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003302 nasm_free(t->text);
3303 t->text = tmp;
3304 t->next = delete_Token(tt);
3305 }
3306 break;
3307 case TOK_NUMBER:
3308 if (tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003309 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003310 nasm_free(t->text);
3311 t->text = tmp;
3312 t->next = delete_Token(tt);
3313 }
3314 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003315 default:
3316 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003317 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003318
H. Peter Anvin76690a12002-04-30 20:52:49 +00003319 return thead;
3320}
3321
3322/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003323 * Expand all single-line macro calls made in the given line.
3324 * Return the expanded version of the line. The original is deemed
3325 * to be destroyed in the process. (In reality we'll just move
3326 * Tokens from input to output a lot of the time, rather than
3327 * actually bothering to destroy and replicate.)
3328 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003329#define DEADMAN_LIMIT (1 << 20)
3330
H. Peter Anvine2c80182005-01-15 22:15:51 +00003331static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003332{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003333 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003334 struct hash_table *smtbl;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003335 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003336 Token **params;
3337 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003338 unsigned int nparam, sparam;
3339 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003340 Token *org_tline = tline;
3341 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003342 char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003343 int deadman = DEADMAN_LIMIT;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003344
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003345 /*
3346 * Trick: we should avoid changing the start token pointer since it can
3347 * be contained in "next" field of other token. Because of this
3348 * we allocate a copy of first token and work with it; at the end of
3349 * routine we copy it back
3350 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003351 if (org_tline) {
3352 tline =
3353 new_Token(org_tline->next, org_tline->type, org_tline->text,
3354 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003355 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003356 nasm_free(org_tline->text);
3357 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003358 }
3359
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003360again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003361 tail = &thead;
3362 thead = NULL;
3363
H. Peter Anvine2c80182005-01-15 22:15:51 +00003364 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003365 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003366 error(ERR_NONFATAL, "interminable macro recursion");
3367 break;
3368 }
3369
H. Peter Anvine2c80182005-01-15 22:15:51 +00003370 if ((mname = tline->text)) {
3371 /* if this token is a local macro, look in local context */
H. Peter Anvinb037a672008-05-29 19:08:08 -07003372 ctx = NULL;
3373 smtbl = &smacros;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003374 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003375 ctx = get_ctx(mname, true);
H. Peter Anvinb037a672008-05-29 19:08:08 -07003376 if (ctx)
3377 smtbl = &ctx->localmac;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003378 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07003379 head = (SMacro *) hash_findix(smtbl, mname);
H. Peter Anvin072771e2008-05-22 13:17:51 -07003380
H. Peter Anvine2c80182005-01-15 22:15:51 +00003381 /*
3382 * We've hit an identifier. As in is_mmacro below, we first
3383 * check whether the identifier is a single-line macro at
3384 * all, then think about checking for parameters if
3385 * necessary.
3386 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003387 for (m = head; m; m = m->next)
3388 if (!mstrcmp(m->name, mname, m->casesense))
3389 break;
3390 if (m) {
3391 mstart = tline;
3392 params = NULL;
3393 paramsize = NULL;
3394 if (m->nparam == 0) {
3395 /*
3396 * Simple case: the macro is parameterless. Discard the
3397 * one token that the macro call took, and push the
3398 * expansion back on the to-do stack.
3399 */
3400 if (!m->expansion) {
3401 if (!strcmp("__FILE__", m->name)) {
3402 int32_t num = 0;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003403 char *file;
3404 src_get(&num, &file);
3405 tline->text = nasm_quote(file, strlen(file));
H. Peter Anvin97a23472007-09-16 17:57:25 -07003406 tline->type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003407 nasm_free(file);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003408 continue;
3409 }
3410 if (!strcmp("__LINE__", m->name)) {
3411 nasm_free(tline->text);
3412 make_tok_num(tline, src_get_linnum());
3413 continue;
3414 }
3415 if (!strcmp("__BITS__", m->name)) {
3416 nasm_free(tline->text);
3417 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003418 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003419 }
3420 tline = delete_Token(tline);
3421 continue;
3422 }
3423 } else {
3424 /*
3425 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003426 * exists and takes parameters. We must find the
3427 * parameters in the call, count them, find the SMacro
3428 * that corresponds to that form of the macro call, and
3429 * substitute for the parameters when we expand. What a
3430 * pain.
3431 */
3432 /*tline = tline->next;
3433 skip_white_(tline); */
3434 do {
3435 t = tline->next;
3436 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003437 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003438 t->text = NULL;
3439 t = tline->next = delete_Token(t);
3440 }
3441 tline = t;
3442 } while (tok_type_(tline, TOK_WHITESPACE));
3443 if (!tok_is_(tline, "(")) {
3444 /*
3445 * This macro wasn't called with parameters: ignore
3446 * the call. (Behaviour borrowed from gnu cpp.)
3447 */
3448 tline = mstart;
3449 m = NULL;
3450 } else {
3451 int paren = 0;
3452 int white = 0;
3453 brackets = 0;
3454 nparam = 0;
3455 sparam = PARAM_DELTA;
3456 params = nasm_malloc(sparam * sizeof(Token *));
3457 params[0] = tline->next;
3458 paramsize = nasm_malloc(sparam * sizeof(int));
3459 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003460 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003461 /*
3462 * For some unusual expansions
3463 * which concatenates function call
3464 */
3465 t = tline->next;
3466 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003467 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003468 t->text = NULL;
3469 t = tline->next = delete_Token(t);
3470 }
3471 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003472
H. Peter Anvine2c80182005-01-15 22:15:51 +00003473 if (!tline) {
3474 error(ERR_NONFATAL,
3475 "macro call expects terminating `)'");
3476 break;
3477 }
3478 if (tline->type == TOK_WHITESPACE
3479 && brackets <= 0) {
3480 if (paramsize[nparam])
3481 white++;
3482 else
3483 params[nparam] = tline->next;
3484 continue; /* parameter loop */
3485 }
3486 if (tline->type == TOK_OTHER
3487 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003488 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003489 if (ch == ',' && !paren && brackets <= 0) {
3490 if (++nparam >= sparam) {
3491 sparam += PARAM_DELTA;
3492 params = nasm_realloc(params,
3493 sparam *
3494 sizeof(Token
3495 *));
3496 paramsize =
3497 nasm_realloc(paramsize,
3498 sparam *
3499 sizeof(int));
3500 }
3501 params[nparam] = tline->next;
3502 paramsize[nparam] = 0;
3503 white = 0;
3504 continue; /* parameter loop */
3505 }
3506 if (ch == '{' &&
3507 (brackets > 0 || (brackets == 0 &&
3508 !paramsize[nparam])))
3509 {
3510 if (!(brackets++)) {
3511 params[nparam] = tline->next;
3512 continue; /* parameter loop */
3513 }
3514 }
3515 if (ch == '}' && brackets > 0)
3516 if (--brackets == 0) {
3517 brackets = -1;
3518 continue; /* parameter loop */
3519 }
3520 if (ch == '(' && !brackets)
3521 paren++;
3522 if (ch == ')' && brackets <= 0)
3523 if (--paren < 0)
3524 break;
3525 }
3526 if (brackets < 0) {
3527 brackets = 0;
3528 error(ERR_NONFATAL, "braces do not "
3529 "enclose all of macro parameter");
3530 }
3531 paramsize[nparam] += white + 1;
3532 white = 0;
3533 } /* parameter loop */
3534 nparam++;
3535 while (m && (m->nparam != nparam ||
3536 mstrcmp(m->name, mname,
3537 m->casesense)))
3538 m = m->next;
3539 if (!m)
3540 error(ERR_WARNING | ERR_WARN_MNP,
3541 "macro `%s' exists, "
3542 "but not taking %d parameters",
3543 mstart->text, nparam);
3544 }
3545 }
3546 if (m && m->in_progress)
3547 m = NULL;
3548 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003549 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003550 * Design question: should we handle !tline, which
3551 * indicates missing ')' here, or expand those
3552 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003553 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003554 */
3555 nasm_free(params);
3556 nasm_free(paramsize);
3557 tline = mstart;
3558 } else {
3559 /*
3560 * Expand the macro: we are placed on the last token of the
3561 * call, so that we can easily split the call from the
3562 * following tokens. We also start by pushing an SMAC_END
3563 * token for the cycle removal.
3564 */
3565 t = tline;
3566 if (t) {
3567 tline = t->next;
3568 t->next = NULL;
3569 }
3570 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003571 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003572 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003573 tline = tt;
3574 for (t = m->expansion; t; t = t->next) {
3575 if (t->type >= TOK_SMAC_PARAM) {
3576 Token *pcopy = tline, **ptail = &pcopy;
3577 Token *ttt, *pt;
3578 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003579
H. Peter Anvine2c80182005-01-15 22:15:51 +00003580 ttt = params[t->type - TOK_SMAC_PARAM];
3581 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3582 --i >= 0;) {
3583 pt = *ptail =
3584 new_Token(tline, ttt->type, ttt->text,
3585 0);
3586 ptail = &pt->next;
3587 ttt = ttt->next;
3588 }
3589 tline = pcopy;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -07003590 } else if (t->type == TOK_PREPROC_Q) {
3591 tt = new_Token(tline, TOK_ID, mname, 0);
3592 tline = tt;
3593 } else if (t->type == TOK_PREPROC_QQ) {
3594 tt = new_Token(tline, TOK_ID, m->name, 0);
3595 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003596 } else {
3597 tt = new_Token(tline, t->type, t->text, 0);
3598 tline = tt;
3599 }
3600 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003601
H. Peter Anvine2c80182005-01-15 22:15:51 +00003602 /*
3603 * Having done that, get rid of the macro call, and clean
3604 * up the parameters.
3605 */
3606 nasm_free(params);
3607 nasm_free(paramsize);
3608 free_tlist(mstart);
3609 continue; /* main token loop */
3610 }
3611 }
3612 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003613
H. Peter Anvine2c80182005-01-15 22:15:51 +00003614 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003615 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003616 tline = delete_Token(tline);
3617 } else {
3618 t = *tail = tline;
3619 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003620 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003621 t->next = NULL;
3622 tail = &t->next;
3623 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003624 }
3625
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003626 /*
3627 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003628 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003629 * TOK_IDs should be concatenated.
3630 * Also we look for %+ tokens and concatenate the tokens before and after
3631 * them (without white spaces in between).
3632 */
3633 t = thead;
3634 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003635 while (t) {
3636 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3637 t = t->next;
3638 if (!t || !t->next)
3639 break;
3640 if (t->next->type == TOK_ID ||
3641 t->next->type == TOK_PREPROC_ID ||
3642 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003643 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003644 nasm_free(t->text);
3645 t->next = delete_Token(t->next);
3646 t->text = p;
3647 rescan = 1;
3648 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3649 t->next->next->type == TOK_PREPROC_ID &&
3650 strcmp(t->next->next->text, "%+") == 0) {
3651 /* free the next whitespace, the %+ token and next whitespace */
3652 int i;
3653 for (i = 1; i <= 3; i++) {
3654 if (!t->next
3655 || (i != 2 && t->next->type != TOK_WHITESPACE))
3656 break;
3657 t->next = delete_Token(t->next);
3658 } /* endfor */
3659 } else
3660 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003661 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003662 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003663 if (rescan) {
3664 tline = thead;
3665 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003666 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003667
H. Peter Anvine2c80182005-01-15 22:15:51 +00003668 if (org_tline) {
3669 if (thead) {
3670 *org_tline = *thead;
3671 /* since we just gave text to org_line, don't free it */
3672 thead->text = NULL;
3673 delete_Token(thead);
3674 } else {
3675 /* the expression expanded to empty line;
3676 we can't return NULL for some reasons
3677 we just set the line to a single WHITESPACE token. */
3678 memset(org_tline, 0, sizeof(*org_tline));
3679 org_tline->text = NULL;
3680 org_tline->type = TOK_WHITESPACE;
3681 }
3682 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003683 }
3684
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003685 return thead;
3686}
3687
3688/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003689 * Similar to expand_smacro but used exclusively with macro identifiers
3690 * right before they are fetched in. The reason is that there can be
3691 * identifiers consisting of several subparts. We consider that if there
3692 * are more than one element forming the name, user wants a expansion,
3693 * otherwise it will be left as-is. Example:
3694 *
3695 * %define %$abc cde
3696 *
3697 * the identifier %$abc will be left as-is so that the handler for %define
3698 * will suck it and define the corresponding value. Other case:
3699 *
3700 * %define _%$abc cde
3701 *
3702 * In this case user wants name to be expanded *before* %define starts
3703 * working, so we'll expand %$abc into something (if it has a value;
3704 * otherwise it will be left as-is) then concatenate all successive
3705 * PP_IDs into one.
3706 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003707static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003708{
3709 Token *cur, *oldnext = NULL;
3710
H. Peter Anvin734b1882002-04-30 21:01:08 +00003711 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003712 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003713
3714 cur = tline;
3715 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003716 (cur->next->type == TOK_ID ||
3717 cur->next->type == TOK_PREPROC_ID
3718 || cur->next->type == TOK_NUMBER))
3719 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003720
3721 /* If identifier consists of just one token, don't expand */
3722 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003723 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003724
H. Peter Anvine2c80182005-01-15 22:15:51 +00003725 if (cur) {
3726 oldnext = cur->next; /* Detach the tail past identifier */
3727 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003728 }
3729
H. Peter Anvin734b1882002-04-30 21:01:08 +00003730 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003731
H. Peter Anvine2c80182005-01-15 22:15:51 +00003732 if (cur) {
3733 /* expand_smacro possibly changhed tline; re-scan for EOL */
3734 cur = tline;
3735 while (cur && cur->next)
3736 cur = cur->next;
3737 if (cur)
3738 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003739 }
3740
3741 return tline;
3742}
3743
3744/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003745 * Determine whether the given line constitutes a multi-line macro
3746 * call, and return the MMacro structure called if so. Doesn't have
3747 * to check for an initial label - that's taken care of in
3748 * expand_mmacro - but must check numbers of parameters. Guaranteed
3749 * to be called with tline->type == TOK_ID, so the putative macro
3750 * name is easy to find.
3751 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003752static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003753{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003754 MMacro *head, *m;
3755 Token **params;
3756 int nparam;
3757
H. Peter Anvin166c2472008-05-28 12:28:58 -07003758 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003759
3760 /*
3761 * Efficiency: first we see if any macro exists with the given
3762 * name. If not, we can return NULL immediately. _Then_ we
3763 * count the parameters, and then we look further along the
3764 * list if necessary to find the proper MMacro.
3765 */
3766 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003767 if (!mstrcmp(m->name, tline->text, m->casesense))
3768 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003769 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003770 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003771
3772 /*
3773 * OK, we have a potential macro. Count and demarcate the
3774 * parameters.
3775 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003776 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003777
3778 /*
3779 * So we know how many parameters we've got. Find the MMacro
3780 * structure that handles this number.
3781 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003782 while (m) {
3783 if (m->nparam_min <= nparam
3784 && (m->plus || nparam <= m->nparam_max)) {
3785 /*
3786 * This one is right. Just check if cycle removal
3787 * prohibits us using it before we actually celebrate...
3788 */
3789 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003790#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003791 error(ERR_NONFATAL,
3792 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003793#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003794 nasm_free(params);
3795 return NULL;
3796 }
3797 /*
3798 * It's right, and we can use it. Add its default
3799 * parameters to the end of our list if necessary.
3800 */
3801 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3802 params =
3803 nasm_realloc(params,
3804 ((m->nparam_min + m->ndefs +
3805 1) * sizeof(*params)));
3806 while (nparam < m->nparam_min + m->ndefs) {
3807 params[nparam] = m->defaults[nparam - m->nparam_min];
3808 nparam++;
3809 }
3810 }
3811 /*
3812 * If we've gone over the maximum parameter count (and
3813 * we're in Plus mode), ignore parameters beyond
3814 * nparam_max.
3815 */
3816 if (m->plus && nparam > m->nparam_max)
3817 nparam = m->nparam_max;
3818 /*
3819 * Then terminate the parameter list, and leave.
3820 */
3821 if (!params) { /* need this special case */
3822 params = nasm_malloc(sizeof(*params));
3823 nparam = 0;
3824 }
3825 params[nparam] = NULL;
3826 *params_array = params;
3827 return m;
3828 }
3829 /*
3830 * This one wasn't right: look for the next one with the
3831 * same name.
3832 */
3833 for (m = m->next; m; m = m->next)
3834 if (!mstrcmp(m->name, tline->text, m->casesense))
3835 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003836 }
3837
3838 /*
3839 * After all that, we didn't find one with the right number of
3840 * parameters. Issue a warning, and fail to expand the macro.
3841 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003842 error(ERR_WARNING | ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003843 "macro `%s' exists, but not taking %d parameters",
3844 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003845 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003846 return NULL;
3847}
3848
3849/*
3850 * Expand the multi-line macro call made by the given line, if
3851 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003852 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003853 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003854static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003855{
3856 Token *startline = tline;
3857 Token *label = NULL;
3858 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003859 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003860 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003861 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003862 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07003863 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003864
3865 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003866 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07003867 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003868 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003869 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003870 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003871 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07003872 if (m) {
3873 mname = t->text;
3874 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003875 Token *last;
3876 /*
3877 * We have an id which isn't a macro call. We'll assume
3878 * it might be a label; we'll also check to see if a
3879 * colon follows it. Then, if there's another id after
3880 * that lot, we'll check it again for macro-hood.
3881 */
3882 label = last = t;
3883 t = t->next;
3884 if (tok_type_(t, TOK_WHITESPACE))
3885 last = t, t = t->next;
3886 if (tok_is_(t, ":")) {
3887 dont_prepend = 1;
3888 last = t, t = t->next;
3889 if (tok_type_(t, TOK_WHITESPACE))
3890 last = t, t = t->next;
3891 }
3892 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3893 return 0;
3894 last->next = NULL;
H. Peter Anvinc751e862008-06-09 10:18:45 -07003895 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003896 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003897 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003898
3899 /*
3900 * Fix up the parameters: this involves stripping leading and
3901 * trailing whitespace, then stripping braces if they are
3902 * present.
3903 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003904 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003905 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003906
H. Peter Anvine2c80182005-01-15 22:15:51 +00003907 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003908 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003909 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003910
H. Peter Anvine2c80182005-01-15 22:15:51 +00003911 t = params[i];
3912 skip_white_(t);
3913 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003914 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003915 params[i] = t;
3916 paramlen[i] = 0;
3917 while (t) {
3918 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3919 break; /* ... because we have hit a comma */
3920 if (comma && t->type == TOK_WHITESPACE
3921 && tok_is_(t->next, ","))
3922 break; /* ... or a space then a comma */
3923 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3924 break; /* ... or a brace */
3925 t = t->next;
3926 paramlen[i]++;
3927 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003928 }
3929
3930 /*
3931 * OK, we have a MMacro structure together with a set of
3932 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003933 * copies of each Line on to istk->expansion. Substitution of
3934 * parameter tokens and macro-local tokens doesn't get done
3935 * until the single-line macro substitution process; this is
3936 * because delaying them allows us to change the semantics
3937 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003938 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003939 * First, push an end marker on to istk->expansion, mark this
3940 * macro as in progress, and set up its invocation-specific
3941 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003942 */
3943 ll = nasm_malloc(sizeof(Line));
3944 ll->next = istk->expansion;
3945 ll->finishes = m;
3946 ll->first = NULL;
3947 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003948
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003949 m->in_progress = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003950 m->params = params;
3951 m->iline = tline;
3952 m->nparam = nparam;
3953 m->rotate = 0;
3954 m->paramlen = paramlen;
3955 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003956 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003957
3958 m->next_active = istk->mstk;
3959 istk->mstk = m;
3960
H. Peter Anvine2c80182005-01-15 22:15:51 +00003961 for (l = m->expansion; l; l = l->next) {
3962 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003963
H. Peter Anvine2c80182005-01-15 22:15:51 +00003964 ll = nasm_malloc(sizeof(Line));
3965 ll->finishes = NULL;
3966 ll->next = istk->expansion;
3967 istk->expansion = ll;
3968 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003969
H. Peter Anvine2c80182005-01-15 22:15:51 +00003970 for (t = l->first; t; t = t->next) {
3971 Token *x = t;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003972 switch (t->type) {
3973 case TOK_PREPROC_Q:
H. Peter Anvinc751e862008-06-09 10:18:45 -07003974 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
H. Peter Anvince2233b2008-05-25 21:57:00 -07003975 break;
3976 case TOK_PREPROC_QQ:
3977 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
3978 break;
3979 case TOK_PREPROC_ID:
3980 if (t->text[1] == '0' && t->text[2] == '0') {
3981 dont_prepend = -1;
3982 x = label;
3983 if (!x)
3984 continue;
3985 }
3986 /* fall through */
3987 default:
3988 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3989 break;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003990 }
H. Peter Anvince2233b2008-05-25 21:57:00 -07003991 tail = &tt->next;
3992 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003993 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003994 }
3995
3996 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00003997 * If we had a label, push it on as the first line of
3998 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003999 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004000 if (label) {
4001 if (dont_prepend < 0)
4002 free_tlist(startline);
4003 else {
4004 ll = nasm_malloc(sizeof(Line));
4005 ll->finishes = NULL;
4006 ll->next = istk->expansion;
4007 istk->expansion = ll;
4008 ll->first = startline;
4009 if (!dont_prepend) {
4010 while (label->next)
4011 label = label->next;
4012 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4013 }
4014 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004015 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004016
H. Peter Anvin734b1882002-04-30 21:01:08 +00004017 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004018
H. Peter Anvineba20a72002-04-30 20:53:55 +00004019 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004020}
4021
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004022/*
4023 * Since preprocessor always operate only on the line that didn't
4024 * arrived yet, we should always use ERR_OFFBY1. Also since user
4025 * won't want to see same error twice (preprocessing is done once
4026 * per pass) we will want to show errors only during pass one.
4027 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004028static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004029{
4030 va_list arg;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004031 char buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004032
4033 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004034 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004035 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004036
H. Peter Anvin734b1882002-04-30 21:01:08 +00004037 va_start(arg, fmt);
Ed Beroset19f927a2004-12-15 17:07:03 +00004038 vsnprintf(buff, sizeof(buff), fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004039 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004040
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004041 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004042 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
4043 istk->mstk->lineno, buff);
4044 else
4045 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004046}
4047
H. Peter Anvin734b1882002-04-30 21:01:08 +00004048static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004049pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004050 ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004051{
H. Peter Anvin99941bf2002-05-14 17:44:03 +00004052 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004053 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004054 istk = nasm_malloc(sizeof(Include));
4055 istk->next = NULL;
4056 istk->conds = NULL;
4057 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004058 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004059 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004060 istk->fname = NULL;
4061 src_set_fname(nasm_strdup(file));
4062 src_set_linnum(0);
4063 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004064 if (!istk->fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004065 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
4066 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004067 defining = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004068 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004069 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004070 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004071 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004072 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004073 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004074 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004075 any_extrastdmac = extrastdmac && *extrastdmac;
4076 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004077 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004078 evaluate = eval;
4079 pass = apass;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004080 dephead = deptail = deplist;
4081 if (deplist) {
4082 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4083 sl->next = NULL;
4084 strcpy(sl->str, file);
4085 *deptail = sl;
4086 deptail = &sl->next;
4087 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004088}
4089
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004090static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004091{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004092 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004093 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004094
H. Peter Anvine2c80182005-01-15 22:15:51 +00004095 while (1) {
4096 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004097 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004098 * buffer or from the input file.
4099 */
4100 tline = NULL;
4101 while (istk->expansion && istk->expansion->finishes) {
4102 Line *l = istk->expansion;
4103 if (!l->finishes->name && l->finishes->in_progress > 1) {
4104 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004105
H. Peter Anvine2c80182005-01-15 22:15:51 +00004106 /*
4107 * This is a macro-end marker for a macro with no
4108 * name, which means it's not really a macro at all
4109 * but a %rep block, and the `in_progress' field is
4110 * more than 1, meaning that we still need to
4111 * repeat. (1 means the natural last repetition; 0
4112 * means termination by %exitrep.) We have
4113 * therefore expanded up to the %endrep, and must
4114 * push the whole block on to the expansion buffer
4115 * again. We don't bother to remove the macro-end
4116 * marker: we'd only have to generate another one
4117 * if we did.
4118 */
4119 l->finishes->in_progress--;
4120 for (l = l->finishes->expansion; l; l = l->next) {
4121 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004122
H. Peter Anvine2c80182005-01-15 22:15:51 +00004123 ll = nasm_malloc(sizeof(Line));
4124 ll->next = istk->expansion;
4125 ll->finishes = NULL;
4126 ll->first = NULL;
4127 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004128
H. Peter Anvine2c80182005-01-15 22:15:51 +00004129 for (t = l->first; t; t = t->next) {
4130 if (t->text || t->type == TOK_WHITESPACE) {
4131 tt = *tail =
4132 new_Token(NULL, t->type, t->text, 0);
4133 tail = &tt->next;
4134 }
4135 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004136
H. Peter Anvine2c80182005-01-15 22:15:51 +00004137 istk->expansion = ll;
4138 }
4139 } else {
4140 /*
4141 * Check whether a `%rep' was started and not ended
4142 * within this macro expansion. This can happen and
4143 * should be detected. It's a fatal error because
4144 * I'm too confused to work out how to recover
4145 * sensibly from it.
4146 */
4147 if (defining) {
4148 if (defining->name)
4149 error(ERR_PANIC,
4150 "defining with name in expansion");
4151 else if (istk->mstk->name)
4152 error(ERR_FATAL,
4153 "`%%rep' without `%%endrep' within"
4154 " expansion of macro `%s'",
4155 istk->mstk->name);
4156 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004157
H. Peter Anvine2c80182005-01-15 22:15:51 +00004158 /*
4159 * FIXME: investigate the relationship at this point between
4160 * istk->mstk and l->finishes
4161 */
4162 {
4163 MMacro *m = istk->mstk;
4164 istk->mstk = m->next_active;
4165 if (m->name) {
4166 /*
4167 * This was a real macro call, not a %rep, and
4168 * therefore the parameter information needs to
4169 * be freed.
4170 */
4171 nasm_free(m->params);
4172 free_tlist(m->iline);
4173 nasm_free(m->paramlen);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004174 l->finishes->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004175 } else
4176 free_mmacro(m);
4177 }
4178 istk->expansion = l->next;
4179 nasm_free(l);
4180 list->downlevel(LIST_MACRO);
4181 }
4182 }
4183 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004184
H. Peter Anvine2c80182005-01-15 22:15:51 +00004185 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004186 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004187 Line *l = istk->expansion;
4188 if (istk->mstk)
4189 istk->mstk->lineno++;
4190 tline = l->first;
4191 istk->expansion = l->next;
4192 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004193 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004194 list->line(LIST_MACRO, p);
4195 nasm_free(p);
4196 break;
4197 }
4198 line = read_line();
4199 if (line) { /* from the current input file */
4200 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004201 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004202 nasm_free(line);
4203 break;
4204 }
4205 /*
4206 * The current file has ended; work down the istk
4207 */
4208 {
4209 Include *i = istk;
4210 fclose(i->fp);
4211 if (i->conds)
4212 error(ERR_FATAL,
4213 "expected `%%endif' before end of file");
4214 /* only set line and file name if there's a next node */
4215 if (i->next) {
4216 src_set_linnum(i->lineno);
4217 nasm_free(src_set_fname(i->fname));
H. Peter Anvin86877b22008-06-20 15:55:45 -07004218 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004219 istk = i->next;
4220 list->downlevel(LIST_INCLUDE);
4221 nasm_free(i);
4222 if (!istk)
4223 return NULL;
4224 }
4225 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004226
H. Peter Anvine2c80182005-01-15 22:15:51 +00004227 /*
4228 * We must expand MMacro parameters and MMacro-local labels
4229 * _before_ we plunge into directive processing, to cope
4230 * with things like `%define something %1' such as STRUC
4231 * uses. Unless we're _defining_ a MMacro, in which case
4232 * those tokens should be left alone to go into the
4233 * definition; and unless we're in a non-emitting
4234 * condition, in which case we don't want to meddle with
4235 * anything.
4236 */
4237 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
4238 tline = expand_mmac_params(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004239
H. Peter Anvine2c80182005-01-15 22:15:51 +00004240 /*
4241 * Check the line to see if it's a preprocessor directive.
4242 */
4243 if (do_directive(tline) == DIRECTIVE_FOUND) {
4244 continue;
4245 } else if (defining) {
4246 /*
4247 * We're defining a multi-line macro. We emit nothing
4248 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004249 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004250 */
4251 Line *l = nasm_malloc(sizeof(Line));
4252 l->next = defining->expansion;
4253 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004254 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004255 defining->expansion = l;
4256 continue;
4257 } else if (istk->conds && !emitting(istk->conds->state)) {
4258 /*
4259 * We're in a non-emitting branch of a condition block.
4260 * Emit nothing at all, not even a blank line: when we
4261 * emerge from the condition we'll give a line-number
4262 * directive so we keep our place correctly.
4263 */
4264 free_tlist(tline);
4265 continue;
4266 } else if (istk->mstk && !istk->mstk->in_progress) {
4267 /*
4268 * We're in a %rep block which has been terminated, so
4269 * we're walking through to the %endrep without
4270 * emitting anything. Emit nothing at all, not even a
4271 * blank line: when we emerge from the %rep block we'll
4272 * give a line-number directive so we keep our place
4273 * correctly.
4274 */
4275 free_tlist(tline);
4276 continue;
4277 } else {
4278 tline = expand_smacro(tline);
4279 if (!expand_mmacro(tline)) {
4280 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004281 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004282 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004283 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004284 free_tlist(tline);
4285 break;
4286 } else {
4287 continue; /* expand_mmacro calls free_tlist */
4288 }
4289 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004290 }
4291
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004292 return line;
4293}
4294
H. Peter Anvine2c80182005-01-15 22:15:51 +00004295static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004296{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004297 if (defining) {
4298 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
4299 defining->name);
4300 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004301 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004302 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004303 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004304 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004305 while (istk) {
4306 Include *i = istk;
4307 istk = istk->next;
4308 fclose(i->fp);
4309 nasm_free(i->fname);
4310 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004311 }
4312 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004313 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004314 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004315 if (pass == 0) {
H. Peter Anvin86877b22008-06-20 15:55:45 -07004316 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004317 free_llist(predef);
4318 delete_Blocks();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004319 while ((i = ipath)) {
4320 ipath = i->next;
4321 if (i->path)
4322 nasm_free(i->path);
4323 nasm_free(i);
4324 }
4325 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004326}
4327
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004328void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004329{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004330 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004331
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004332 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004333 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004334 i->next = NULL;
4335
H. Peter Anvine2c80182005-01-15 22:15:51 +00004336 if (ipath != NULL) {
4337 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004338 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004339 j = j->next;
4340 j->next = i;
4341 } else {
4342 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004343 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004344}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004345
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004346void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004347{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004348 Token *inc, *space, *name;
4349 Line *l;
4350
H. Peter Anvin734b1882002-04-30 21:01:08 +00004351 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4352 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4353 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004354
4355 l = nasm_malloc(sizeof(Line));
4356 l->next = predef;
4357 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004358 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004359 predef = l;
4360}
4361
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004362void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004363{
4364 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004365 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004366 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004367
4368 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004369 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4370 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004371 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004372 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004373 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004374 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004375 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004376
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004377 l = nasm_malloc(sizeof(Line));
4378 l->next = predef;
4379 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004380 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004381 predef = l;
4382}
4383
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004384void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004385{
4386 Token *def, *space;
4387 Line *l;
4388
H. Peter Anvin734b1882002-04-30 21:01:08 +00004389 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4390 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004391 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004392
4393 l = nasm_malloc(sizeof(Line));
4394 l->next = predef;
4395 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004396 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004397 predef = l;
4398}
4399
Keith Kaniosb7a89542007-04-12 02:40:54 +00004400/*
4401 * Added by Keith Kanios:
4402 *
4403 * This function is used to assist with "runtime" preprocessor
4404 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4405 *
4406 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4407 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4408 */
4409
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004410void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004411{
4412 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004413
Keith Kaniosb7a89542007-04-12 02:40:54 +00004414 def = tokenize(definition);
4415 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4416 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004417
Keith Kaniosb7a89542007-04-12 02:40:54 +00004418}
4419
H. Peter Anvincfb71762008-06-20 15:20:16 -07004420void pp_extra_stdmac(const macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004421{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004422 extrastdmac = macros;
4423}
4424
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004425static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004426{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004427 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004428 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004429 tok->text = nasm_strdup(numbuf);
4430 tok->type = TOK_NUMBER;
4431}
4432
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004433Preproc nasmpp = {
4434 pp_reset,
4435 pp_getline,
4436 pp_cleanup
4437};