blob: 7f1c44f00202a8fe4cf9f519245c1477bc8c4137 [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 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200256 * These states mean that we're not emitting now, and also that
257 * nothing until %endif will be emitted at all. COND_DONE is
258 * used when we've had our moment of emission
259 * and have now started seeing %elifs. COND_NEVER is used when
260 * the condition construct in question is contained within a
261 * non-emitting branch of a larger condition construct,
262 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000263 */
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200264 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000265};
266#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
267
H. Peter Anvin70653092007-10-19 14:42:29 -0700268/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000269 * These defines are used as the possible return values for do_directive
270 */
271#define NO_DIRECTIVE_FOUND 0
272#define DIRECTIVE_FOUND 1
273
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000274/*
275 * Condition codes. Note that we use c_ prefix not C_ because C_ is
276 * used in nasm.h for the "real" condition codes. At _this_ level,
277 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
278 * ones, so we need a different enum...
279 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700280static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000281 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
282 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000283 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000284};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700285enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000286 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
287 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 -0700288 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
289 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000290};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700291static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000292 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
293 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 +0000294 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000295};
296
H. Peter Anvin76690a12002-04-30 20:52:49 +0000297/*
298 * Directive names.
299 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000300/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000301static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000302{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000303 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000304}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000305
306/* For TASM compatibility we need to be able to recognise TASM compatible
307 * conditional compilation directives. Using the NASM pre-processor does
308 * not work, so we look for them specifically from the following list and
309 * then jam in the equivalent NASM directive into the input stream.
310 */
311
H. Peter Anvine2c80182005-01-15 22:15:51 +0000312enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000313 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
314 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
315};
316
H. Peter Anvin476d2862007-10-02 22:04:15 -0700317static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000318 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
319 "ifndef", "include", "local"
320};
321
322static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000323static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000324static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800325static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000326
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000327static Context *cstk;
328static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000329static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000330
H. Peter Anvine2c80182005-01-15 22:15:51 +0000331static efunc _error; /* Pointer to client-provided error reporting function */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000332static evalfunc evaluate;
333
H. Peter Anvine2c80182005-01-15 22:15:51 +0000334static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700335static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000336
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700337static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000338
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000339static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700340static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000341
342static ListGen *list;
343
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000344/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000345 * The current set of multi-line macros we have defined.
346 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700347static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000348
349/*
350 * The current set of single-line macros we have defined.
351 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700352static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000353
354/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000355 * The multi-line macro we are currently defining, or the %rep
356 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000357 */
358static MMacro *defining;
359
Charles Crayned4200be2008-07-12 16:42:33 -0700360static uint64_t nested_mac_count;
361static uint64_t nested_rep_count;
362
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000363/*
364 * The number of macro parameters to allocate space for at a time.
365 */
366#define PARAM_DELTA 16
367
368/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700369 * The standard macro set: defined in macros.c in the array nasm_stdmac.
370 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000371 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700372static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000373
374/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000375 * The extra standard macros that come from the object format, if
376 * any.
377 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700378static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700379static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000380
381/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000382 * Tokens are allocated in blocks to improve speed
383 */
384#define TOKEN_BLOCKSIZE 4096
385static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000386struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000387 Blocks *next;
388 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000389};
390
391static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000392
393/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000394 * Forward declarations.
395 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000396static Token *expand_mmac_params(Token * tline);
397static Token *expand_smacro(Token * tline);
398static Token *expand_id(Token * tline);
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -0700399static Context *get_ctx(const char *name, bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700400static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000401static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200402static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000403static void *new_Block(size_t size);
404static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700405static Token *new_Token(Token * next, enum pp_token_type type,
406 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000407static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000408
409/*
410 * Macros for safe checking of token pointers, avoid *(NULL)
411 */
412#define tok_type_(x,t) ((x) && (x)->type == (t))
413#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
414#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
415#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000416
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000417/* Handle TASM specific directives, which do not contain a % in
418 * front of them. We do it here because I could not find any other
419 * place to do it for the moment, and it is a hack (ideally it would
420 * be nice to be able to use the NASM pre-processor to do it).
421 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000422static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000423{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000424 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000425 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000426
427 /* Skip whitespace */
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700428 while (nasm_isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000429 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000430
431 /* Binary search for the directive name */
432 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000433 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000434 len = 0;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700435 while (!nasm_isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000436 len++;
437 if (len) {
438 oldchar = p[len];
439 p[len] = 0;
440 while (j - i > 1) {
441 k = (j + i) / 2;
442 m = nasm_stricmp(p, tasm_directives[k]);
443 if (m == 0) {
444 /* We have found a directive, so jam a % in front of it
445 * so that NASM will then recognise it as one if it's own.
446 */
447 p[len] = oldchar;
448 len = strlen(p);
449 oldline = line;
450 line = nasm_malloc(len + 2);
451 line[0] = '%';
452 if (k == TM_IFDIFI) {
453 /* NASM does not recognise IFDIFI, so we convert it to
454 * %ifdef BOGUS. This is not used in NASM comaptible
455 * code, but does need to parse for the TASM macro
456 * package.
457 */
458 strcpy(line + 1, "ifdef BOGUS");
459 } else {
460 memcpy(line + 1, p, len + 1);
461 }
462 nasm_free(oldline);
463 return line;
464 } else if (m < 0) {
465 j = k;
466 } else
467 i = k;
468 }
469 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000470 }
471 return line;
472}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000473
H. Peter Anvin76690a12002-04-30 20:52:49 +0000474/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000475 * The pre-preprocessing stage... This function translates line
476 * number indications as they emerge from GNU cpp (`# lineno "file"
477 * flags') into NASM preprocessor line number indications (`%line
478 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000479 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000480static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000481{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000482 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000483 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000484
H. Peter Anvine2c80182005-01-15 22:15:51 +0000485 if (line[0] == '#' && line[1] == ' ') {
486 oldline = line;
487 fname = oldline + 2;
488 lineno = atoi(fname);
489 fname += strspn(fname, "0123456789 ");
490 if (*fname == '"')
491 fname++;
492 fnlen = strcspn(fname, "\"");
493 line = nasm_malloc(20 + fnlen);
494 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
495 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000496 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000497 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000498 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000499 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000500}
501
502/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000503 * Free a linked list of tokens.
504 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000505static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000506{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000507 while (list) {
508 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000509 }
510}
511
512/*
513 * Free a linked list of lines.
514 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000515static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000516{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000517 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000518 while (list) {
519 l = list;
520 list = list->next;
521 free_tlist(l->first);
522 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000523 }
524}
525
526/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000527 * Free an MMacro
528 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000529static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000530{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000531 nasm_free(m->name);
532 free_tlist(m->dlist);
533 nasm_free(m->defaults);
534 free_llist(m->expansion);
535 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000536}
537
538/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700539 * Free all currently defined macros, and free the hash tables
540 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700541static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700542{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700543 SMacro *s;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700544 const char *key;
545 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700546
H. Peter Anvin072771e2008-05-22 13:17:51 -0700547 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700548 nasm_free((void *)key);
549 while (s) {
550 SMacro *ns = s->next;
551 nasm_free(s->name);
552 free_tlist(s->expansion);
553 nasm_free(s);
554 s = ns;
555 }
556 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700557 hash_free(smt);
558}
559
560static void free_mmacro_table(struct hash_table *mmt)
561{
562 MMacro *m;
563 const char *key;
564 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700565
566 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700567 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700568 nasm_free((void *)key);
569 while (m) {
570 MMacro *nm = m->next;
571 free_mmacro(m);
572 m = nm;
573 }
574 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700575 hash_free(mmt);
576}
577
578static void free_macros(void)
579{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700580 free_smacro_table(&smacros);
581 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700582}
583
584/*
585 * Initialize the hash tables
586 */
587static void init_macros(void)
588{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700589 hash_init(&smacros, HASH_LARGE);
590 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700591}
592
593/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000594 * Pop the context stack.
595 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000596static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000597{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000598 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000599
600 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700601 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000602 nasm_free(c->name);
603 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000604}
605
H. Peter Anvin072771e2008-05-22 13:17:51 -0700606/*
607 * Search for a key in the hash index; adding it if necessary
608 * (in which case we initialize the data pointer to NULL.)
609 */
610static void **
611hash_findi_add(struct hash_table *hash, const char *str)
612{
613 struct hash_insert hi;
614 void **r;
615 char *strx;
616
617 r = hash_findi(hash, str, &hi);
618 if (r)
619 return r;
620
621 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
622 return hash_add(&hi, strx, NULL);
623}
624
625/*
626 * Like hash_findi, but returns the data element rather than a pointer
627 * to it. Used only when not adding a new element, hence no third
628 * argument.
629 */
630static void *
631hash_findix(struct hash_table *hash, const char *str)
632{
633 void **p;
634
635 p = hash_findi(hash, str, NULL);
636 return p ? *p : NULL;
637}
638
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000639#define BUF_DELTA 512
640/*
641 * Read a line from the top file in istk, handling multiple CR/LFs
642 * at the end of the line read, and handling spurious ^Zs. Will
643 * return lines from the standard macro set if this has not already
644 * been done.
645 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000646static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000647{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000648 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000649 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000650
H. Peter Anvine2c80182005-01-15 22:15:51 +0000651 if (stdmacpos) {
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700652 unsigned char c;
H. Peter Anvin7e50d232008-06-25 14:54:14 -0700653 const unsigned char *p = stdmacpos;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700654 char *ret, *q;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700655 size_t len = 0;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700656 while ((c = *p++)) {
657 if (c >= 0x80)
658 len += pp_directives_len[c-0x80]+1;
659 else
660 len++;
661 }
662 ret = nasm_malloc(len+1);
H. Peter Anvincda81632008-06-21 15:15:40 -0700663 q = ret;
664 while ((c = *stdmacpos++)) {
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700665 if (c >= 0x80) {
666 memcpy(q, pp_directives[c-0x80], pp_directives_len[c-0x80]);
667 q += pp_directives_len[c-0x80];
668 *q++ = ' ';
669 } else {
670 *q++ = c;
671 }
672 }
H. Peter Anvincda81632008-06-21 15:15:40 -0700673 stdmacpos = p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700674 *q = '\0';
675
H. Peter Anvind2456592008-06-19 15:04:18 -0700676 if (!*stdmacpos) {
677 /* This was the last of the standard macro chain... */
678 stdmacpos = NULL;
679 if (any_extrastdmac) {
680 stdmacpos = extrastdmac;
681 any_extrastdmac = false;
682 } else if (do_predef) {
683 Line *pd, *l;
684 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000685
H. Peter Anvind2456592008-06-19 15:04:18 -0700686 /*
687 * Nasty hack: here we push the contents of
688 * `predef' on to the top-level expansion stack,
689 * since this is the most convenient way to
690 * implement the pre-include and pre-define
691 * features.
692 */
693 for (pd = predef; pd; pd = pd->next) {
694 head = NULL;
695 tail = &head;
696 for (t = pd->first; t; t = t->next) {
697 *tail = new_Token(NULL, t->type, t->text, 0);
698 tail = &(*tail)->next;
699 }
700 l = nasm_malloc(sizeof(Line));
701 l->next = istk->expansion;
702 l->first = head;
H. Peter Anvin538002d2008-06-28 18:30:27 -0700703 l->finishes = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700704 istk->expansion = l;
705 }
706 do_predef = false;
707 }
708 }
709 return ret;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000710 }
711
712 bufsize = BUF_DELTA;
713 buffer = nasm_malloc(BUF_DELTA);
714 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000715 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000716 while (1) {
717 q = fgets(p, bufsize - (p - buffer), istk->fp);
718 if (!q)
719 break;
720 p += strlen(p);
721 if (p > buffer && p[-1] == '\n') {
722 /* Convert backslash-CRLF line continuation sequences into
723 nothing at all (for DOS and Windows) */
724 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
725 p -= 3;
726 *p = 0;
727 continued_count++;
728 }
729 /* Also convert backslash-LF line continuation sequences into
730 nothing at all (for Unix) */
731 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
732 p -= 2;
733 *p = 0;
734 continued_count++;
735 } else {
736 break;
737 }
738 }
739 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000740 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000741 bufsize += BUF_DELTA;
742 buffer = nasm_realloc(buffer, bufsize);
743 p = buffer + offset; /* prevent stale-pointer problems */
744 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000745 }
746
H. Peter Anvine2c80182005-01-15 22:15:51 +0000747 if (!q && p == buffer) {
748 nasm_free(buffer);
749 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000750 }
751
H. Peter Anvine2c80182005-01-15 22:15:51 +0000752 src_set_linnum(src_get_linnum() + istk->lineinc +
753 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000754
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000755 /*
756 * Play safe: remove CRs as well as LFs, if any of either are
757 * present at the end of the line.
758 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000759 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000760 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000761
762 /*
763 * Handle spurious ^Z, which may be inserted into source files
764 * by some file transfer utilities.
765 */
766 buffer[strcspn(buffer, "\032")] = '\0';
767
H. Peter Anvin734b1882002-04-30 21:01:08 +0000768 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000769
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000770 return buffer;
771}
772
773/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000774 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000775 * don't need to parse the value out of e.g. numeric tokens: we
776 * simply split one string into many.
777 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000778static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000779{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000780 char *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000781 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000782 Token *list = NULL;
783 Token *t, **tail = &list;
784
H. Peter Anvine2c80182005-01-15 22:15:51 +0000785 while (*line) {
786 p = line;
787 if (*p == '%') {
788 p++;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700789 if (nasm_isdigit(*p) ||
790 ((*p == '-' || *p == '+') && nasm_isdigit(p[1])) ||
791 ((*p == '+') && (nasm_isspace(p[1]) || !p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000792 do {
793 p++;
794 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700795 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000796 type = TOK_PREPROC_ID;
797 } else if (*p == '{') {
798 p++;
799 while (*p && *p != '}') {
800 p[-1] = *p;
801 p++;
802 }
803 p[-1] = '\0';
804 if (*p)
805 p++;
806 type = TOK_PREPROC_ID;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700807 } else if (*p == '?') {
808 type = TOK_PREPROC_Q; /* %? */
809 p++;
810 if (*p == '?') {
811 type = TOK_PREPROC_QQ; /* %?? */
812 p++;
813 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000814 } else if (isidchar(*p) ||
815 ((*p == '!' || *p == '%' || *p == '$') &&
816 isidchar(p[1]))) {
817 do {
818 p++;
819 }
820 while (isidchar(*p));
821 type = TOK_PREPROC_ID;
822 } else {
823 type = TOK_OTHER;
824 if (*p == '%')
825 p++;
826 }
827 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
828 type = TOK_ID;
829 p++;
830 while (*p && isidchar(*p))
831 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700832 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000833 /*
834 * A string token.
835 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000836 type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700837 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000838
H. Peter Anvine2c80182005-01-15 22:15:51 +0000839 if (*p) {
840 p++;
841 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -0700842 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000843 /* Handling unterminated strings by UNV */
844 /* type = -1; */
845 }
846 } else if (isnumstart(*p)) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700847 bool is_hex = false;
848 bool is_float = false;
849 bool has_e = false;
850 char c, *r;
851
H. Peter Anvine2c80182005-01-15 22:15:51 +0000852 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700853 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000854 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700855
856 if (*p == '$') {
857 p++;
858 is_hex = true;
859 }
860
861 for (;;) {
862 c = *p++;
863
864 if (!is_hex && (c == 'e' || c == 'E')) {
865 has_e = true;
866 if (*p == '+' || *p == '-') {
867 /* e can only be followed by +/- if it is either a
868 prefixed hex number or a floating-point number */
869 p++;
870 is_float = true;
871 }
872 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
873 is_hex = true;
874 } else if (c == 'P' || c == 'p') {
875 is_float = true;
876 if (*p == '+' || *p == '-')
877 p++;
878 } else if (isnumchar(c) || c == '_')
879 ; /* just advance */
880 else if (c == '.') {
881 /* we need to deal with consequences of the legacy
882 parser, like "1.nolist" being two tokens
883 (TOK_NUMBER, TOK_ID) here; at least give it
884 a shot for now. In the future, we probably need
885 a flex-based scanner with proper pattern matching
886 to do it as well as it can be done. Nothing in
887 the world is going to help the person who wants
888 0x123.p16 interpreted as two tokens, though. */
889 r = p;
890 while (*r == '_')
891 r++;
892
H. Peter Anvinf221b9e2008-06-21 11:03:51 -0700893 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700894 (!is_hex && (*r == 'e' || *r == 'E')) ||
895 (*r == 'p' || *r == 'P')) {
896 p = r;
897 is_float = true;
898 } else
899 break; /* Terminate the token */
900 } else
901 break;
902 }
903 p--; /* Point to first character beyond number */
904
905 if (has_e && !is_hex) {
906 /* 1e13 is floating-point, but 1e13h is not */
907 is_float = true;
908 }
909
910 type = is_float ? TOK_FLOAT : TOK_NUMBER;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700911 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000912 type = TOK_WHITESPACE;
913 p++;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700914 while (*p && nasm_isspace(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000915 p++;
916 /*
917 * Whitespace just before end-of-line is discarded by
918 * pretending it's a comment; whitespace just before a
919 * comment gets lumped into the comment.
920 */
921 if (!*p || *p == ';') {
922 type = TOK_COMMENT;
923 while (*p)
924 p++;
925 }
926 } else if (*p == ';') {
927 type = TOK_COMMENT;
928 while (*p)
929 p++;
930 } else {
931 /*
932 * Anything else is an operator of some kind. We check
933 * for all the double-character operators (>>, <<, //,
934 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000935 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000936 */
937 type = TOK_OTHER;
938 if ((p[0] == '>' && p[1] == '>') ||
939 (p[0] == '<' && p[1] == '<') ||
940 (p[0] == '/' && p[1] == '/') ||
941 (p[0] == '<' && p[1] == '=') ||
942 (p[0] == '>' && p[1] == '=') ||
943 (p[0] == '=' && p[1] == '=') ||
944 (p[0] == '!' && p[1] == '=') ||
945 (p[0] == '<' && p[1] == '>') ||
946 (p[0] == '&' && p[1] == '&') ||
947 (p[0] == '|' && p[1] == '|') ||
948 (p[0] == '^' && p[1] == '^')) {
949 p++;
950 }
951 p++;
952 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000953
H. Peter Anvine2c80182005-01-15 22:15:51 +0000954 /* Handling unterminated string by UNV */
955 /*if (type == -1)
956 {
957 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
958 t->text[p-line] = *line;
959 tail = &t->next;
960 }
961 else */
962 if (type != TOK_COMMENT) {
963 *tail = t = new_Token(NULL, type, line, p - line);
964 tail = &t->next;
965 }
966 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000967 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000968 return list;
969}
970
H. Peter Anvince616072002-04-30 21:02:23 +0000971/*
972 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -0700973 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +0000974 * deleted only all at once by the delete_Blocks function.
975 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000976static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +0000977{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000978 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000979
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000980 /* first, get to the end of the linked list */
981 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000982 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000983 /* now allocate the requested chunk */
984 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000986 /* now allocate a new block for the next request */
987 b->next = nasm_malloc(sizeof(Blocks));
988 /* and initialize the contents of the new block */
989 b->next->next = NULL;
990 b->next->chunk = NULL;
991 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000992}
993
994/*
995 * this function deletes all managed blocks of memory
996 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +0000998{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001000
H. Peter Anvin70653092007-10-19 14:42:29 -07001001 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001002 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001003 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001004 * free it.
1005 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 while (b) {
1007 if (b->chunk)
1008 nasm_free(b->chunk);
1009 a = b;
1010 b = b->next;
1011 if (a != &blocks)
1012 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001013 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001015
1016/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001017 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001018 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001019 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001020 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001021static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvinc751e862008-06-09 10:18:45 -07001022 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001023{
1024 Token *t;
1025 int i;
1026
H. Peter Anvine2c80182005-01-15 22:15:51 +00001027 if (freeTokens == NULL) {
1028 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1029 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1030 freeTokens[i].next = &freeTokens[i + 1];
1031 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001032 }
1033 t = freeTokens;
1034 freeTokens = t->next;
1035 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001036 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001037 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001038 if (type == TOK_WHITESPACE || text == NULL) {
1039 t->text = NULL;
1040 } else {
1041 if (txtlen == 0)
1042 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001043 t->text = nasm_malloc(txtlen+1);
1044 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001045 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001046 }
1047 return t;
1048}
1049
H. Peter Anvine2c80182005-01-15 22:15:51 +00001050static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001051{
1052 Token *next = t->next;
1053 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001054 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001055 freeTokens = t;
1056 return next;
1057}
1058
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001059/*
1060 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001061 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1062 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001063 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001064static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001065{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001066 Token *t;
1067 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001068 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001069 const char *q;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001070
1071 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 for (t = tlist; t; t = t->next) {
1073 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001074 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001075 nasm_free(t->text);
1076 if (p)
1077 t->text = nasm_strdup(p);
1078 else
1079 t->text = NULL;
1080 }
1081 /* Expand local macros here and not during preprocessing */
1082 if (expand_locals &&
1083 t->type == TOK_PREPROC_ID && t->text &&
1084 t->text[0] == '%' && t->text[1] == '$') {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001085 Context *ctx = get_ctx(t->text, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001087 char buffer[40];
1088 char *p, *q = t->text + 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001089
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 q += strspn(q, "$");
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001091 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092 p = nasm_strcat(buffer, q);
1093 nasm_free(t->text);
1094 t->text = p;
1095 }
1096 }
1097 if (t->type == TOK_WHITESPACE) {
1098 len++;
1099 } else if (t->text) {
1100 len += strlen(t->text);
1101 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001102 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001103 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001104 for (t = tlist; t; t = t->next) {
1105 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001106 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001107 } else if (t->text) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001108 q = t->text;
1109 while (*q)
1110 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001112 }
1113 *p = '\0';
1114 return line;
1115}
1116
1117/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001118 * A scanner, suitable for use by the expression evaluator, which
1119 * operates on a line of Tokens. Expects a pointer to a pointer to
1120 * the first token in the line to be passed in as its private_data
1121 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001122 *
1123 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001124 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001125static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001126{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001127 Token **tlineptr = private_data;
1128 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001129 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001130
H. Peter Anvine2c80182005-01-15 22:15:51 +00001131 do {
1132 tline = *tlineptr;
1133 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001134 }
1135 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001136 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001137
1138 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001140
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001141 tokval->t_charptr = tline->text;
1142
H. Peter Anvin76690a12002-04-30 20:52:49 +00001143 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001145 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001146 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001147
H. Peter Anvine2c80182005-01-15 22:15:51 +00001148 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001149 p = tokval->t_charptr = tline->text;
1150 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151 tokval->t_charptr++;
1152 return tokval->t_type = TOKEN_ID;
1153 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001154
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001155 for (r = p, s = ourcopy; *r; r++) {
Philipp Thomas76ec8e72008-05-21 08:53:21 -07001156 if (r >= p+MAX_KEYWORD)
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001157 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001158 *s++ = nasm_tolower(*r);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001159 }
1160 *s = '\0';
1161 /* right, so we have an identifier sitting in temp storage. now,
1162 * is it actually a register or instruction name, or what? */
1163 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001164 }
1165
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 if (tline->type == TOK_NUMBER) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001167 bool rn_error;
1168 tokval->t_integer = readnum(tline->text, &rn_error);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001169 tokval->t_charptr = tline->text;
H. Peter Anvin11627042008-06-09 20:45:19 -07001170 if (rn_error)
1171 return tokval->t_type = TOKEN_ERRNUM;
1172 else
1173 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001174 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001175
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001176 if (tline->type == TOK_FLOAT) {
1177 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001178 }
1179
H. Peter Anvine2c80182005-01-15 22:15:51 +00001180 if (tline->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001181 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001182
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001183 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001184 tokval->t_charptr = tline->text;
1185 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001186
H. Peter Anvin11627042008-06-09 20:45:19 -07001187 if (ep[0] != bq || ep[1] != '\0')
1188 return tokval->t_type = TOKEN_ERRSTR;
1189 else
1190 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001191 }
1192
H. Peter Anvine2c80182005-01-15 22:15:51 +00001193 if (tline->type == TOK_OTHER) {
1194 if (!strcmp(tline->text, "<<"))
1195 return tokval->t_type = TOKEN_SHL;
1196 if (!strcmp(tline->text, ">>"))
1197 return tokval->t_type = TOKEN_SHR;
1198 if (!strcmp(tline->text, "//"))
1199 return tokval->t_type = TOKEN_SDIV;
1200 if (!strcmp(tline->text, "%%"))
1201 return tokval->t_type = TOKEN_SMOD;
1202 if (!strcmp(tline->text, "=="))
1203 return tokval->t_type = TOKEN_EQ;
1204 if (!strcmp(tline->text, "<>"))
1205 return tokval->t_type = TOKEN_NE;
1206 if (!strcmp(tline->text, "!="))
1207 return tokval->t_type = TOKEN_NE;
1208 if (!strcmp(tline->text, "<="))
1209 return tokval->t_type = TOKEN_LE;
1210 if (!strcmp(tline->text, ">="))
1211 return tokval->t_type = TOKEN_GE;
1212 if (!strcmp(tline->text, "&&"))
1213 return tokval->t_type = TOKEN_DBL_AND;
1214 if (!strcmp(tline->text, "^^"))
1215 return tokval->t_type = TOKEN_DBL_XOR;
1216 if (!strcmp(tline->text, "||"))
1217 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001218 }
1219
1220 /*
1221 * We have no other options: just return the first character of
1222 * the token text.
1223 */
1224 return tokval->t_type = tline->text[0];
1225}
1226
1227/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001228 * Compare a string to the name of an existing macro; this is a
1229 * simple wrapper which calls either strcmp or nasm_stricmp
1230 * depending on the value of the `casesense' parameter.
1231 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001232static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001233{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001234 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001235}
1236
1237/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001238 * Compare a string to the name of an existing macro; this is a
1239 * simple wrapper which calls either strcmp or nasm_stricmp
1240 * depending on the value of the `casesense' parameter.
1241 */
1242static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1243{
1244 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1245}
1246
1247/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001248 * Return the Context structure associated with a %$ token. Return
1249 * NULL, having _already_ reported an error condition, if the
1250 * context stack isn't deep enough for the supplied number of $
1251 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001252 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001253 * also scanned for such smacro, until it is found; if not -
1254 * only the context that directly results from the number of $'s
1255 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001256 */
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001257static Context *get_ctx(const char *name, bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001258{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001259 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001260 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001261 int i;
1262
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001263 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001264 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001265
H. Peter Anvine2c80182005-01-15 22:15:51 +00001266 if (!cstk) {
1267 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1268 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001269 }
1270
H. Peter Anvine2c80182005-01-15 22:15:51 +00001271 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1272 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001273/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001274 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001275 if (!ctx) {
1276 error(ERR_NONFATAL, "`%s': context stack is only"
1277 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1278 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001279 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001280 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001281 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001282
H. Peter Anvine2c80182005-01-15 22:15:51 +00001283 do {
1284 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001285 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001286 while (m) {
1287 if (!mstrcmp(m->name, name, m->casesense))
1288 return ctx;
1289 m = m->next;
1290 }
1291 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001292 }
1293 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001294 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001295}
1296
1297/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001298 * Check to see if a file is already in a string list
1299 */
1300static bool in_list(const StrList *list, const char *str)
1301{
1302 while (list) {
1303 if (!strcmp(list->str, str))
1304 return true;
1305 list = list->next;
1306 }
1307 return false;
1308}
1309
1310/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001311 * Open an include file. This routine must always return a valid
1312 * file pointer if it returns - it's responsible for throwing an
1313 * ERR_FATAL and bombing out completely if not. It should also try
1314 * the include path one by one until it finds the file or reaches
1315 * the end of the path.
1316 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001317static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
H. Peter Anvin418ca702008-05-30 10:42:30 -07001318 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001319{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001320 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001321 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001322 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001323 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001324 size_t prefix_len = 0;
1325 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001326
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001328 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1329 memcpy(sl->str, prefix, prefix_len);
1330 memcpy(sl->str+prefix_len, file, len+1);
1331 fp = fopen(sl->str, "r");
H. Peter Anvin418ca702008-05-30 10:42:30 -07001332 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001333 sl->next = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001334 **dtail = sl;
1335 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001336 } else {
1337 nasm_free(sl);
1338 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001339 if (fp)
1340 return fp;
H. Peter Anvin418ca702008-05-30 10:42:30 -07001341 if (!ip) {
1342 if (!missing_ok)
1343 break;
1344 prefix = NULL;
1345 } else {
1346 prefix = ip->path;
1347 ip = ip->next;
1348 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001349 if (prefix) {
1350 prefix_len = strlen(prefix);
1351 } else {
1352 /* -MG given and file not found */
H. Peter Anvin418ca702008-05-30 10:42:30 -07001353 if (dhead && !in_list(*dhead, file)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001354 sl = nasm_malloc(len+1+sizeof sl->next);
1355 sl->next = NULL;
1356 strcpy(sl->str, file);
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001357 **dtail = sl;
1358 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001359 }
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001360 return NULL;
1361 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001362 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001363
H. Peter Anvin734b1882002-04-30 21:01:08 +00001364 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001366}
1367
1368/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001369 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001370 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001371 * return true if _any_ single-line macro of that name is defined.
1372 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001373 * `nparam' or no parameters is defined.
1374 *
1375 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001376 * defined, or nparam is -1, the address of the definition structure
1377 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001378 * is NULL, no action will be taken regarding its contents, and no
1379 * error will occur.
1380 *
1381 * Note that this is also called with nparam zero to resolve
1382 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001383 *
1384 * If you already know which context macro belongs to, you can pass
1385 * the context pointer as first parameter; if you won't but name begins
1386 * with %$ the context will be automatically computed. If all_contexts
1387 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001388 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001389static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001390smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001391 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001392{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001393 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001394 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001395
H. Peter Anvin97a23472007-09-16 17:57:25 -07001396 if (ctx) {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001397 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001398 } else if (name[0] == '%' && name[1] == '$') {
1399 if (cstk)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001400 ctx = get_ctx(name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001401 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001402 return false; /* got to return _something_ */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001403 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001404 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001405 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001406 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001407 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001408
H. Peter Anvine2c80182005-01-15 22:15:51 +00001409 while (m) {
1410 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001411 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001412 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001413 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001414 *defn = m;
1415 else
1416 *defn = NULL;
1417 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001418 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001419 }
1420 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001421 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001422
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001423 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001424}
1425
1426/*
1427 * Count and mark off the parameters in a multi-line macro call.
1428 * This is called both from within the multi-line macro expansion
1429 * code, and also to mark off the default parameters when provided
1430 * in a %macro definition line.
1431 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001432static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001433{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001434 int paramsize, brace;
1435
1436 *nparam = paramsize = 0;
1437 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001438 while (t) {
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001439 /* +1: we need space for the final NULL */
1440 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001441 paramsize += PARAM_DELTA;
1442 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1443 }
1444 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001445 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001446 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001447 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001448 (*params)[(*nparam)++] = t;
1449 while (tok_isnt_(t, brace ? "}" : ","))
1450 t = t->next;
1451 if (t) { /* got a comma/brace */
1452 t = t->next;
1453 if (brace) {
1454 /*
1455 * Now we've found the closing brace, look further
1456 * for the comma.
1457 */
1458 skip_white_(t);
1459 if (tok_isnt_(t, ",")) {
1460 error(ERR_NONFATAL,
1461 "braces do not enclose all of macro parameter");
1462 while (tok_isnt_(t, ","))
1463 t = t->next;
1464 }
1465 if (t)
1466 t = t->next; /* eat the comma */
1467 }
1468 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001469 }
1470}
1471
1472/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001473 * Determine whether one of the various `if' conditions is true or
1474 * not.
1475 *
1476 * We must free the tline we get passed.
1477 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001478static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001479{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001480 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001481 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001482 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001483 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001484 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001485 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001486
1487 origline = tline;
1488
H. Peter Anvine2c80182005-01-15 22:15:51 +00001489 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001490 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001491 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001492 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001493 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001494 if (!tline)
1495 break;
1496 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001497 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001498 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001499 free_tlist(origline);
1500 return -1;
1501 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001502 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001503 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001504 tline = tline->next;
1505 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001506 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001507
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001508 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001509 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001510 while (tline) {
1511 skip_white_(tline);
1512 if (!tline || (tline->type != TOK_ID &&
1513 (tline->type != TOK_PREPROC_ID ||
1514 tline->text[1] != '$'))) {
1515 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001516 "`%s' expects macro identifiers", pp_directives[ct]);
1517 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001518 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001519 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001520 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 tline = tline->next;
1522 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001523 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001524
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001525 case PPC_IFIDN:
1526 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001527 tline = expand_smacro(tline);
1528 t = tt = tline;
1529 while (tok_isnt_(tt, ","))
1530 tt = tt->next;
1531 if (!tt) {
1532 error(ERR_NONFATAL,
1533 "`%s' expects two comma-separated arguments",
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 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001538 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001539 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1540 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1541 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001542 pp_directives[ct]);
1543 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001544 }
1545 if (t->type == TOK_WHITESPACE) {
1546 t = t->next;
1547 continue;
1548 }
1549 if (tt->type == TOK_WHITESPACE) {
1550 tt = tt->next;
1551 continue;
1552 }
1553 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001554 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001555 break;
1556 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001557 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001558 if (t->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001559 size_t l1 = nasm_unquote(t->text, NULL);
1560 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001561
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001562 if (l1 != l2) {
1563 j = false;
1564 break;
1565 }
1566 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1567 j = false;
1568 break;
1569 }
1570 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001571 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001572 break;
1573 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001574
H. Peter Anvine2c80182005-01-15 22:15:51 +00001575 t = t->next;
1576 tt = tt->next;
1577 }
1578 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001579 j = false; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001580 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001581
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001582 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001583 {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001584 bool found = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001585 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001586
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 tline = tline->next;
1588 skip_white_(tline);
1589 tline = expand_id(tline);
1590 if (!tok_type_(tline, TOK_ID)) {
1591 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001592 "`%s' expects a macro name", pp_directives[ct]);
1593 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001594 }
1595 searching.name = nasm_strdup(tline->text);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001596 searching.casesense = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001597 searching.plus = false;
1598 searching.nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001599 searching.in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001600 searching.rep_nest = NULL;
1601 searching.nparam_min = 0;
1602 searching.nparam_max = INT_MAX;
1603 tline = expand_smacro(tline->next);
1604 skip_white_(tline);
1605 if (!tline) {
1606 } else if (!tok_type_(tline, TOK_NUMBER)) {
1607 error(ERR_NONFATAL,
1608 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001609 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001610 } else {
1611 searching.nparam_min = searching.nparam_max =
1612 readnum(tline->text, &j);
1613 if (j)
1614 error(ERR_NONFATAL,
1615 "unable to parse parameter count `%s'",
1616 tline->text);
1617 }
1618 if (tline && tok_is_(tline->next, "-")) {
1619 tline = tline->next->next;
1620 if (tok_is_(tline, "*"))
1621 searching.nparam_max = INT_MAX;
1622 else if (!tok_type_(tline, TOK_NUMBER))
1623 error(ERR_NONFATAL,
1624 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001625 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001626 else {
1627 searching.nparam_max = readnum(tline->text, &j);
1628 if (j)
1629 error(ERR_NONFATAL,
1630 "unable to parse parameter count `%s'",
1631 tline->text);
1632 if (searching.nparam_min > searching.nparam_max)
1633 error(ERR_NONFATAL,
1634 "minimum parameter count exceeds maximum");
1635 }
1636 }
1637 if (tline && tok_is_(tline->next, "+")) {
1638 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001639 searching.plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001640 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001641 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001642 while (mmac) {
1643 if (!strcmp(mmac->name, searching.name) &&
1644 (mmac->nparam_min <= searching.nparam_max
1645 || searching.plus)
1646 && (searching.nparam_min <= mmac->nparam_max
1647 || mmac->plus)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001648 found = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001649 break;
1650 }
1651 mmac = mmac->next;
1652 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001653 if(tline && tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001654 error(ERR_WARNING|ERR_PASS1,
1655 "trailing garbage after %%ifmacro ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001656 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001657 j = found;
1658 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001659 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001660
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001661 case PPC_IFID:
1662 needtype = TOK_ID;
1663 goto iftype;
1664 case PPC_IFNUM:
1665 needtype = TOK_NUMBER;
1666 goto iftype;
1667 case PPC_IFSTR:
1668 needtype = TOK_STRING;
1669 goto iftype;
1670
1671 iftype:
H. Peter Anvin134b9462008-02-16 17:01:40 -08001672 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001673
H. Peter Anvin927c92b2008-02-16 13:44:52 -08001674 while (tok_type_(t, TOK_WHITESPACE) ||
1675 (needtype == TOK_NUMBER &&
1676 tok_type_(t, TOK_OTHER) &&
1677 (t->text[0] == '-' || t->text[0] == '+') &&
1678 !t->text[1]))
1679 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001680
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001681 j = tok_type_(t, needtype);
1682 break;
1683
1684 case PPC_IFTOKEN:
1685 t = tline = expand_smacro(tline);
1686 while (tok_type_(t, TOK_WHITESPACE))
1687 t = t->next;
1688
1689 j = false;
1690 if (t) {
1691 t = t->next; /* Skip the actual token */
1692 while (tok_type_(t, TOK_WHITESPACE))
1693 t = t->next;
1694 j = !t; /* Should be nothing left */
1695 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001696 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001697
H. Peter Anvin134b9462008-02-16 17:01:40 -08001698 case PPC_IFEMPTY:
1699 t = tline = expand_smacro(tline);
1700 while (tok_type_(t, TOK_WHITESPACE))
1701 t = t->next;
1702
1703 j = !t; /* Should be empty */
1704 break;
1705
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001706 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001707 t = tline = expand_smacro(tline);
1708 tptr = &t;
1709 tokval.t_type = TOKEN_INVALID;
1710 evalresult = evaluate(ppscan, tptr, &tokval,
1711 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001712 if (!evalresult)
1713 return -1;
1714 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001715 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001716 "trailing garbage after expression ignored");
1717 if (!is_simple(evalresult)) {
1718 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001719 "non-constant value given to `%s'", pp_directives[ct]);
1720 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001721 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001722 j = reloc_value(evalresult) != 0;
H. Peter Anvin90e6e812008-07-16 14:38:24 -07001723 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001724
H. Peter Anvine2c80182005-01-15 22:15:51 +00001725 default:
1726 error(ERR_FATAL,
1727 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001728 pp_directives[ct]);
1729 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001730 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001731
1732 free_tlist(origline);
1733 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001734
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001735fail:
1736 free_tlist(origline);
1737 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001738}
1739
1740/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001741 * Common code for defining an smacro
1742 */
1743static bool define_smacro(Context *ctx, char *mname, bool casesense,
1744 int nparam, Token *expansion)
1745{
1746 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001747 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001748
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001749 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1750 if (!smac) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001751 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001752 "single-line macro `%s' defined both with and"
1753 " without parameters", mname);
1754
1755 /* Some instances of the old code considered this a failure,
1756 some others didn't. What is the right thing to do here? */
1757 free_tlist(expansion);
1758 return false; /* Failure */
1759 } else {
1760 /*
1761 * We're redefining, so we have to take over an
1762 * existing SMacro structure. This means freeing
1763 * what was already in it.
1764 */
1765 nasm_free(smac->name);
1766 free_tlist(smac->expansion);
1767 }
1768 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001769 smtbl = ctx ? &ctx->localmac : &smacros;
1770 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001771 smac = nasm_malloc(sizeof(SMacro));
1772 smac->next = *smhead;
1773 *smhead = smac;
1774 }
1775 smac->name = nasm_strdup(mname);
1776 smac->casesense = casesense;
1777 smac->nparam = nparam;
1778 smac->expansion = expansion;
1779 smac->in_progress = false;
1780 return true; /* Success */
1781}
1782
1783/*
1784 * Undefine an smacro
1785 */
1786static void undef_smacro(Context *ctx, const char *mname)
1787{
1788 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001789 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001790
H. Peter Anvin166c2472008-05-28 12:28:58 -07001791 smtbl = ctx ? &ctx->localmac : &smacros;
1792 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001793
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001794 if (smhead) {
1795 /*
1796 * We now have a macro name... go hunt for it.
1797 */
1798 sp = smhead;
1799 while ((s = *sp) != NULL) {
1800 if (!mstrcmp(s->name, mname, s->casesense)) {
1801 *sp = s->next;
1802 nasm_free(s->name);
1803 free_tlist(s->expansion);
1804 nasm_free(s);
1805 } else {
1806 sp = &s->next;
1807 }
1808 }
1809 }
1810}
1811
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001812/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07001813 * Parse a mmacro specification.
1814 */
1815static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1816{
1817 bool err;
1818
1819 tline = tline->next;
1820 skip_white_(tline);
1821 tline = expand_id(tline);
1822 if (!tok_type_(tline, TOK_ID)) {
1823 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1824 return false;
1825 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001826
H. Peter Anvina26433d2008-07-16 14:40:01 -07001827 def->name = nasm_strdup(tline->text);
1828 def->plus = false;
1829 def->nolist = false;
1830 def->in_progress = 0;
1831 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001832 def->nparam_min = 0;
1833 def->nparam_max = 0;
1834
H. Peter Anvina26433d2008-07-16 14:40:01 -07001835 tline = expand_smacro(tline->next);
1836 skip_white_(tline);
1837 if (!tok_type_(tline, TOK_NUMBER)) {
1838 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001839 } else {
1840 def->nparam_min = def->nparam_max =
1841 readnum(tline->text, &err);
1842 if (err)
1843 error(ERR_NONFATAL,
1844 "unable to parse parameter count `%s'", tline->text);
1845 }
1846 if (tline && tok_is_(tline->next, "-")) {
1847 tline = tline->next->next;
1848 if (tok_is_(tline, "*")) {
1849 def->nparam_max = INT_MAX;
1850 } else if (!tok_type_(tline, TOK_NUMBER)) {
1851 error(ERR_NONFATAL,
1852 "`%s' expects a parameter count after `-'", directive);
1853 } else {
1854 def->nparam_max = readnum(tline->text, &err);
1855 if (err) {
1856 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
1857 tline->text);
1858 }
1859 if (def->nparam_min > def->nparam_max) {
1860 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
1861 }
1862 }
1863 }
1864 if (tline && tok_is_(tline->next, "+")) {
1865 tline = tline->next;
1866 def->plus = true;
1867 }
1868 if (tline && tok_type_(tline->next, TOK_ID) &&
1869 !nasm_stricmp(tline->next->text, ".nolist")) {
1870 tline = tline->next;
1871 def->nolist = true;
1872 }
1873
1874 /*
1875 * Handle default parameters.
1876 */
1877 if (tline && tline->next) {
1878 def->dlist = tline->next;
1879 tline->next = NULL;
1880 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
1881 } else {
1882 def->dlist = NULL;
1883 def->defaults = NULL;
1884 }
1885 def->expansion = NULL;
1886
Victor van den Elzen22343c22008-08-06 14:47:54 +02001887 if(def->defaults &&
1888 def->ndefs > def->nparam_max - def->nparam_min &&
1889 !def->plus)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001890 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
1891 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001892
H. Peter Anvina26433d2008-07-16 14:40:01 -07001893 return true;
1894}
1895
1896
1897/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001898 * Decode a size directive
1899 */
1900static int parse_size(const char *str) {
1901 static const char *size_names[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001902 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001903 static const int sizes[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001904 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001905
1906 return sizes[bsii(str, size_names, elements(size_names))+1];
1907}
1908
Ed Beroset3ab3f412002-06-11 03:31:49 +00001909/**
1910 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001911 * Find out if a line contains a preprocessor directive, and deal
1912 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07001913 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001914 * If a directive _is_ found, it is the responsibility of this routine
1915 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001916 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001917 * @param tline a pointer to the current tokeninzed line linked list
1918 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07001919 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001920 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001921static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001922{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001923 enum preproc_token i;
1924 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07001925 bool err;
1926 int nparam;
1927 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001928 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001929 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001930 int offset;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001931 char *p, *pp, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001932 Include *inc;
1933 Context *ctx;
1934 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001935 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001936 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1937 Line *l;
1938 struct tokenval tokval;
1939 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001940 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001941 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001942 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07001943 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001944
1945 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001946
H. Peter Anvineba20a72002-04-30 20:53:55 +00001947 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07001948 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001949 (tline->text[1] == '%' || tline->text[1] == '$'
1950 || tline->text[1] == '!'))
1951 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001952
H. Peter Anvin4169a472007-09-12 01:29:43 +00001953 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001954
1955 /*
1956 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001957 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001958 * we should ignore all directives except for condition
1959 * directives.
1960 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001961 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001962 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1963 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001964 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001965
1966 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001967 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02001968 * ignore all directives except for %macro/%imacro (which nest),
1969 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
1970 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001971 */
1972 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001973 i != PP_ENDMACRO && i != PP_ENDM &&
1974 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1975 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001976 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001977
Charles Crayned4200be2008-07-12 16:42:33 -07001978 if (defining) {
1979 if (i == PP_MACRO || i == PP_IMACRO) {
1980 nested_mac_count++;
1981 return NO_DIRECTIVE_FOUND;
1982 } else if (nested_mac_count > 0) {
1983 if (i == PP_ENDMACRO) {
1984 nested_mac_count--;
1985 return NO_DIRECTIVE_FOUND;
1986 }
1987 }
1988 if (!defining->name) {
1989 if (i == PP_REP) {
1990 nested_rep_count++;
1991 return NO_DIRECTIVE_FOUND;
1992 } else if (nested_rep_count > 0) {
1993 if (i == PP_ENDREP) {
1994 nested_rep_count--;
1995 return NO_DIRECTIVE_FOUND;
1996 }
1997 }
1998 }
1999 }
2000
H. Peter Anvin4169a472007-09-12 01:29:43 +00002001 switch (i) {
2002 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002003 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2004 tline->text);
2005 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002006
H. Peter Anvine2c80182005-01-15 22:15:51 +00002007 case PP_STACKSIZE:
2008 /* Directive to tell NASM what the default stack size is. The
2009 * default is for a 16-bit stack, and this can be overriden with
2010 * %stacksize large.
2011 * the following form:
2012 *
2013 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2014 */
2015 tline = tline->next;
2016 if (tline && tline->type == TOK_WHITESPACE)
2017 tline = tline->next;
2018 if (!tline || tline->type != TOK_ID) {
2019 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2020 free_tlist(origline);
2021 return DIRECTIVE_FOUND;
2022 }
2023 if (nasm_stricmp(tline->text, "flat") == 0) {
2024 /* All subsequent ARG directives are for a 32-bit stack */
2025 StackSize = 4;
2026 StackPointer = "ebp";
2027 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002028 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002029 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2030 /* All subsequent ARG directives are for a 64-bit stack */
2031 StackSize = 8;
2032 StackPointer = "rbp";
2033 ArgOffset = 8;
2034 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002035 } else if (nasm_stricmp(tline->text, "large") == 0) {
2036 /* All subsequent ARG directives are for a 16-bit stack,
2037 * far function call.
2038 */
2039 StackSize = 2;
2040 StackPointer = "bp";
2041 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002042 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002043 } else if (nasm_stricmp(tline->text, "small") == 0) {
2044 /* All subsequent ARG directives are for a 16-bit stack,
2045 * far function call. We don't support near functions.
2046 */
2047 StackSize = 2;
2048 StackPointer = "bp";
2049 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002050 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002051 } else {
2052 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2053 free_tlist(origline);
2054 return DIRECTIVE_FOUND;
2055 }
2056 free_tlist(origline);
2057 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002058
H. Peter Anvine2c80182005-01-15 22:15:51 +00002059 case PP_ARG:
2060 /* TASM like ARG directive to define arguments to functions, in
2061 * the following form:
2062 *
2063 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2064 */
2065 offset = ArgOffset;
2066 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002067 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002068 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002069
H. Peter Anvine2c80182005-01-15 22:15:51 +00002070 /* Find the argument name */
2071 tline = tline->next;
2072 if (tline && tline->type == TOK_WHITESPACE)
2073 tline = tline->next;
2074 if (!tline || tline->type != TOK_ID) {
2075 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2076 free_tlist(origline);
2077 return DIRECTIVE_FOUND;
2078 }
2079 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002080
H. Peter Anvine2c80182005-01-15 22:15:51 +00002081 /* Find the argument size type */
2082 tline = tline->next;
2083 if (!tline || tline->type != TOK_OTHER
2084 || tline->text[0] != ':') {
2085 error(ERR_NONFATAL,
2086 "Syntax error processing `%%arg' directive");
2087 free_tlist(origline);
2088 return DIRECTIVE_FOUND;
2089 }
2090 tline = tline->next;
2091 if (!tline || tline->type != TOK_ID) {
2092 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2093 free_tlist(origline);
2094 return DIRECTIVE_FOUND;
2095 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002096
H. Peter Anvine2c80182005-01-15 22:15:51 +00002097 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002098 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002099 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002100 size = parse_size(tt->text);
2101 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002102 error(ERR_NONFATAL,
2103 "Invalid size type for `%%arg' missing directive");
2104 free_tlist(tt);
2105 free_tlist(origline);
2106 return DIRECTIVE_FOUND;
2107 }
2108 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002109
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002110 /* Round up to even stack slots */
2111 size = (size+StackSize-1) & ~(StackSize-1);
2112
H. Peter Anvine2c80182005-01-15 22:15:51 +00002113 /* Now define the macro for the argument */
2114 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2115 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002116 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002117 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002118
H. Peter Anvine2c80182005-01-15 22:15:51 +00002119 /* Move to the next argument in the list */
2120 tline = tline->next;
2121 if (tline && tline->type == TOK_WHITESPACE)
2122 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002123 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2124 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002125 free_tlist(origline);
2126 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002127
H. Peter Anvine2c80182005-01-15 22:15:51 +00002128 case PP_LOCAL:
2129 /* TASM like LOCAL directive to define local variables for a
2130 * function, in the following form:
2131 *
2132 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2133 *
2134 * The '= LocalSize' at the end is ignored by NASM, but is
2135 * required by TASM to define the local parameter size (and used
2136 * by the TASM macro package).
2137 */
2138 offset = LocalOffset;
2139 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002140 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002141 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002142
H. Peter Anvine2c80182005-01-15 22:15:51 +00002143 /* Find the argument name */
2144 tline = tline->next;
2145 if (tline && tline->type == TOK_WHITESPACE)
2146 tline = tline->next;
2147 if (!tline || tline->type != TOK_ID) {
2148 error(ERR_NONFATAL,
2149 "`%%local' missing argument parameter");
2150 free_tlist(origline);
2151 return DIRECTIVE_FOUND;
2152 }
2153 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002154
H. Peter Anvine2c80182005-01-15 22:15:51 +00002155 /* Find the argument size type */
2156 tline = tline->next;
2157 if (!tline || tline->type != TOK_OTHER
2158 || tline->text[0] != ':') {
2159 error(ERR_NONFATAL,
2160 "Syntax error processing `%%local' directive");
2161 free_tlist(origline);
2162 return DIRECTIVE_FOUND;
2163 }
2164 tline = tline->next;
2165 if (!tline || tline->type != TOK_ID) {
2166 error(ERR_NONFATAL,
2167 "`%%local' missing size type parameter");
2168 free_tlist(origline);
2169 return DIRECTIVE_FOUND;
2170 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002171
H. Peter Anvine2c80182005-01-15 22:15:51 +00002172 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002173 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002174 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002175 size = parse_size(tt->text);
2176 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002177 error(ERR_NONFATAL,
2178 "Invalid size type for `%%local' missing directive");
2179 free_tlist(tt);
2180 free_tlist(origline);
2181 return DIRECTIVE_FOUND;
2182 }
2183 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002184
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002185 /* Round up to even stack slots */
2186 size = (size+StackSize-1) & ~(StackSize-1);
2187
2188 offset += size; /* Negative offset, increment before */
2189
2190 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002191 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2192 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002193 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002194
H. Peter Anvine2c80182005-01-15 22:15:51 +00002195 /* Now define the assign to setup the enter_c macro correctly */
2196 snprintf(directive, sizeof(directive),
2197 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002198 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002199
H. Peter Anvine2c80182005-01-15 22:15:51 +00002200 /* Move to the next argument in the list */
2201 tline = tline->next;
2202 if (tline && tline->type == TOK_WHITESPACE)
2203 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002204 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2205 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002206 free_tlist(origline);
2207 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002208
H. Peter Anvine2c80182005-01-15 22:15:51 +00002209 case PP_CLEAR:
2210 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002211 error(ERR_WARNING|ERR_PASS1,
2212 "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002213 free_macros();
2214 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002215 free_tlist(origline);
2216 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002217
H. Peter Anvin418ca702008-05-30 10:42:30 -07002218 case PP_DEPEND:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002219 t = tline->next = expand_smacro(tline->next);
2220 skip_white_(t);
2221 if (!t || (t->type != TOK_STRING &&
2222 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002223 error(ERR_NONFATAL, "`%%depend' expects a file name");
2224 free_tlist(origline);
2225 return DIRECTIVE_FOUND; /* but we did _something_ */
2226 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002227 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002228 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002229 "trailing garbage after `%%depend' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002230 p = t->text;
2231 if (t->type != TOK_INTERNAL_STRING)
2232 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002233 if (dephead && !in_list(*dephead, p)) {
2234 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2235 sl->next = NULL;
2236 strcpy(sl->str, p);
2237 *deptail = sl;
2238 deptail = &sl->next;
2239 }
2240 free_tlist(origline);
2241 return DIRECTIVE_FOUND;
2242
2243 case PP_INCLUDE:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002244 t = tline->next = expand_smacro(tline->next);
2245 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002246
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002247 if (!t || (t->type != TOK_STRING &&
2248 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002249 error(ERR_NONFATAL, "`%%include' expects a file name");
2250 free_tlist(origline);
2251 return DIRECTIVE_FOUND; /* but we did _something_ */
2252 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002253 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002254 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002255 "trailing garbage after `%%include' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002256 p = t->text;
2257 if (t->type != TOK_INTERNAL_STRING)
2258 nasm_unquote(p, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002259 inc = nasm_malloc(sizeof(Include));
2260 inc->next = istk;
2261 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002262 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002263 if (!inc->fp) {
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002264 /* -MG given but file not found */
2265 nasm_free(inc);
2266 } else {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002267 inc->fname = src_set_fname(nasm_strdup(p));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002268 inc->lineno = src_set_linnum(0);
2269 inc->lineinc = 1;
2270 inc->expansion = NULL;
2271 inc->mstk = NULL;
2272 istk = inc;
2273 list->uplevel(LIST_INCLUDE);
2274 }
2275 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002276 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002277
H. Peter Anvind2456592008-06-19 15:04:18 -07002278 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002279 {
H. Peter Anvina70547f2008-07-19 21:44:26 -07002280 static macros_t *use_pkg;
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002281 const char *pkg_macro;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002282
H. Peter Anvind2456592008-06-19 15:04:18 -07002283 t = tline->next = expand_smacro(tline->next);
2284 skip_white_(t);
2285
2286 if (!t || (t->type != TOK_STRING &&
2287 t->type != TOK_INTERNAL_STRING &&
2288 t->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002289 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002290 free_tlist(origline);
2291 return DIRECTIVE_FOUND; /* but we did _something_ */
2292 }
2293 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002294 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002295 "trailing garbage after `%%use' ignored");
H. Peter Anvind2456592008-06-19 15:04:18 -07002296 if (t->type == TOK_STRING)
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002297 nasm_unquote(t->text, NULL);
2298 use_pkg = nasm_stdmac_find_package(t->text);
2299 if (!use_pkg)
2300 error(ERR_NONFATAL, "unknown `%%use' package: %s", t->text);
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002301 /* The first string will be <%define>__USE_*__ */
H. Peter Anvin7e50d232008-06-25 14:54:14 -07002302 pkg_macro = (char *)use_pkg + 1;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002303 if (!smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2304 /* Not already included, go ahead and include it */
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002305 stdmacpos = use_pkg;
2306 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002307 free_tlist(origline);
2308 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002309 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002310 case PP_PUSH:
2311 tline = tline->next;
2312 skip_white_(tline);
2313 tline = expand_id(tline);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002314 if (tline) {
2315 if (!tok_type_(tline, TOK_ID)) {
2316 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2317 free_tlist(origline);
2318 return DIRECTIVE_FOUND; /* but we did _something_ */
2319 }
2320 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002321 error(ERR_WARNING|ERR_PASS1,
2322 "trailing garbage after `%%push' ignored");
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002323 p = nasm_strdup(tline->text);
2324 } else {
2325 p = NULL; /* Anonymous context */
2326 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002327 ctx = nasm_malloc(sizeof(Context));
2328 ctx->next = cstk;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002329 hash_init(&ctx->localmac, HASH_SMALL);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002330 ctx->name = p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002331 ctx->number = unique++;
2332 cstk = ctx;
2333 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002334 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002335
H. Peter Anvine2c80182005-01-15 22:15:51 +00002336 case PP_REPL:
2337 tline = tline->next;
2338 skip_white_(tline);
2339 tline = expand_id(tline);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002340 if (tline) {
2341 if (!tok_type_(tline, TOK_ID)) {
2342 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2343 free_tlist(origline);
2344 return DIRECTIVE_FOUND; /* but we did _something_ */
2345 }
2346 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002347 error(ERR_WARNING|ERR_PASS1,
2348 "trailing garbage after `%%repl' ignored");
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002349 p = nasm_strdup(tline->text);
2350 } else {
2351 p = NULL;
2352 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002353 if (!cstk)
2354 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2355 else {
2356 nasm_free(cstk->name);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002357 cstk->name = p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002358 }
2359 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002360 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002361
H. Peter Anvine2c80182005-01-15 22:15:51 +00002362 case PP_POP:
2363 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002364 error(ERR_WARNING|ERR_PASS1,
2365 "trailing garbage after `%%pop' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002366 if (!cstk)
2367 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2368 else
2369 ctx_pop();
2370 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002371 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002372
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002373 case PP_FATAL:
2374 severity = ERR_FATAL|ERR_NO_SEVERITY;
2375 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002376 case PP_ERROR:
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002377 severity = ERR_NONFATAL|ERR_NO_SEVERITY;
2378 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002379 case PP_WARNING:
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002380 severity = ERR_WARNING|ERR_NO_SEVERITY;
2381 goto issue_error;
2382
2383 issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002384 {
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002385 /* Only error out if this is the final pass */
2386 if (pass != 2 && i != PP_FATAL)
2387 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002388
H. Peter Anvine2c80182005-01-15 22:15:51 +00002389 tline->next = expand_smacro(tline->next);
2390 tline = tline->next;
2391 skip_white_(tline);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002392 t = tline ? tline->next : NULL;
2393 skip_white_(t);
2394 if (tok_type_(tline, TOK_STRING) && !t) {
2395 /* The line contains only a quoted string */
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002396 p = tline->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002397 nasm_unquote(p, NULL);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002398 error(severity, "%s: %s", pp_directives[i], p);
2399 } else {
2400 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002401 p = detoken(tline, false);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002402 error(severity, "%s: %s", pp_directives[i], p);
2403 nasm_free(p);
H. Peter Anvind2456592008-06-19 15:04:18 -07002404 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002405 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002406 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002407 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002408
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002409 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002410 if (istk->conds && !emitting(istk->conds->state))
2411 j = COND_NEVER;
2412 else {
2413 j = if_condition(tline->next, i);
2414 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002415 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2416 }
2417 cond = nasm_malloc(sizeof(Cond));
2418 cond->next = istk->conds;
2419 cond->state = j;
2420 istk->conds = cond;
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002421 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002422 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002423
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002424 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002425 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002426 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002427 switch(istk->conds->state) {
2428 case COND_IF_TRUE:
2429 istk->conds->state = COND_DONE;
2430 break;
2431
2432 case COND_DONE:
2433 case COND_NEVER:
2434 break;
2435
2436 case COND_ELSE_TRUE:
2437 case COND_ELSE_FALSE:
H. Peter Anvin917a3492008-09-24 09:14:49 -07002438 error_precond(ERR_WARNING|ERR_PASS1,
2439 "`%%elif' after `%%else' ignored");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002440 istk->conds->state = COND_NEVER;
2441 break;
2442
2443 case COND_IF_FALSE:
2444 /*
2445 * IMPORTANT: In the case of %if, we will already have
2446 * called expand_mmac_params(); however, if we're
2447 * processing an %elif we must have been in a
2448 * non-emitting mode, which would have inhibited
2449 * the normal invocation of expand_mmac_params(). Therefore,
2450 * we have to do it explicitly here.
2451 */
2452 j = if_condition(expand_mmac_params(tline->next), i);
2453 tline->next = NULL; /* it got freed */
2454 istk->conds->state =
2455 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2456 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002457 }
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002458 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002459 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002460
H. Peter Anvine2c80182005-01-15 22:15:51 +00002461 case PP_ELSE:
2462 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002463 error_precond(ERR_WARNING|ERR_PASS1,
2464 "trailing garbage after `%%else' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002465 if (!istk->conds)
2466 error(ERR_FATAL, "`%%else': no matching `%%if'");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002467 switch(istk->conds->state) {
2468 case COND_IF_TRUE:
2469 case COND_DONE:
2470 istk->conds->state = COND_ELSE_FALSE;
2471 break;
2472
2473 case COND_NEVER:
2474 break;
2475
2476 case COND_IF_FALSE:
2477 istk->conds->state = COND_ELSE_TRUE;
2478 break;
2479
2480 case COND_ELSE_TRUE:
2481 case COND_ELSE_FALSE:
H. Peter Anvin917a3492008-09-24 09:14:49 -07002482 error_precond(ERR_WARNING|ERR_PASS1,
2483 "`%%else' after `%%else' ignored.");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002484 istk->conds->state = COND_NEVER;
2485 break;
2486 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002487 free_tlist(origline);
2488 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002489
H. Peter Anvine2c80182005-01-15 22:15:51 +00002490 case PP_ENDIF:
2491 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002492 error_precond(ERR_WARNING|ERR_PASS1,
2493 "trailing garbage after `%%endif' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002494 if (!istk->conds)
2495 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2496 cond = istk->conds;
2497 istk->conds = cond->next;
2498 nasm_free(cond);
2499 free_tlist(origline);
2500 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002501
H. Peter Anvine2c80182005-01-15 22:15:51 +00002502 case PP_MACRO:
2503 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002504 if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002505 error(ERR_FATAL,
2506 "`%%%smacro': already defining a macro",
2507 (i == PP_IMACRO ? "i" : ""));
H. Peter Anvina26433d2008-07-16 14:40:01 -07002508 return DIRECTIVE_FOUND;
2509 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002510 defining = nasm_malloc(sizeof(MMacro));
H. Peter Anvina26433d2008-07-16 14:40:01 -07002511 defining->casesense = (i == PP_MACRO);
2512 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2513 nasm_free(defining);
2514 defining = NULL;
2515 return DIRECTIVE_FOUND;
2516 }
2517
H. Peter Anvin166c2472008-05-28 12:28:58 -07002518 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002519 while (mmac) {
2520 if (!strcmp(mmac->name, defining->name) &&
2521 (mmac->nparam_min <= defining->nparam_max
2522 || defining->plus)
2523 && (defining->nparam_min <= mmac->nparam_max
2524 || mmac->plus)) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002525 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002526 "redefining multi-line macro `%s'", defining->name);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002527 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002528 }
2529 mmac = mmac->next;
2530 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002531 free_tlist(origline);
2532 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002533
H. Peter Anvine2c80182005-01-15 22:15:51 +00002534 case PP_ENDM:
2535 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002536 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002537 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2538 return DIRECTIVE_FOUND;
2539 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002540 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002541 defining->next = *mmhead;
2542 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002543 defining = NULL;
2544 free_tlist(origline);
2545 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002546
H. Peter Anvina26433d2008-07-16 14:40:01 -07002547 case PP_UNMACRO:
2548 case PP_UNIMACRO:
2549 {
2550 MMacro **mmac_p;
2551 MMacro spec;
2552
2553 spec.casesense = (i == PP_UNMACRO);
2554 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2555 return DIRECTIVE_FOUND;
2556 }
2557 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2558 while (mmac_p && *mmac_p) {
2559 mmac = *mmac_p;
2560 if (mmac->casesense == spec.casesense &&
2561 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2562 mmac->nparam_min == spec.nparam_min &&
2563 mmac->nparam_max == spec.nparam_max &&
2564 mmac->plus == spec.plus) {
2565 *mmac_p = mmac->next;
2566 free_mmacro(mmac);
2567 } else {
2568 mmac_p = &mmac->next;
2569 }
2570 }
2571 free_tlist(origline);
2572 free_tlist(spec.dlist);
2573 return DIRECTIVE_FOUND;
2574 }
2575
H. Peter Anvine2c80182005-01-15 22:15:51 +00002576 case PP_ROTATE:
2577 if (tline->next && tline->next->type == TOK_WHITESPACE)
2578 tline = tline->next;
2579 if (tline->next == NULL) {
2580 free_tlist(origline);
2581 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2582 return DIRECTIVE_FOUND;
2583 }
2584 t = expand_smacro(tline->next);
2585 tline->next = NULL;
2586 free_tlist(origline);
2587 tline = t;
2588 tptr = &t;
2589 tokval.t_type = TOKEN_INVALID;
2590 evalresult =
2591 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2592 free_tlist(tline);
2593 if (!evalresult)
2594 return DIRECTIVE_FOUND;
2595 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002596 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002597 "trailing garbage after expression ignored");
2598 if (!is_simple(evalresult)) {
2599 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2600 return DIRECTIVE_FOUND;
2601 }
2602 mmac = istk->mstk;
2603 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2604 mmac = mmac->next_active;
2605 if (!mmac) {
2606 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2607 } else if (mmac->nparam == 0) {
2608 error(ERR_NONFATAL,
2609 "`%%rotate' invoked within macro without parameters");
2610 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002611 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002612
H. Peter Anvin25a99342007-09-22 17:45:45 -07002613 rotate %= (int)mmac->nparam;
2614 if (rotate < 0)
2615 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002616
H. Peter Anvin25a99342007-09-22 17:45:45 -07002617 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002618 }
2619 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002620
H. Peter Anvine2c80182005-01-15 22:15:51 +00002621 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002622 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002623 do {
2624 tline = tline->next;
2625 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002626
H. Peter Anvine2c80182005-01-15 22:15:51 +00002627 if (tok_type_(tline, TOK_ID) &&
2628 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002629 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002630 do {
2631 tline = tline->next;
2632 } while (tok_type_(tline, TOK_WHITESPACE));
2633 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002634
H. Peter Anvine2c80182005-01-15 22:15:51 +00002635 if (tline) {
2636 t = expand_smacro(tline);
2637 tptr = &t;
2638 tokval.t_type = TOKEN_INVALID;
2639 evalresult =
2640 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2641 if (!evalresult) {
2642 free_tlist(origline);
2643 return DIRECTIVE_FOUND;
2644 }
2645 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002646 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002647 "trailing garbage after expression ignored");
2648 if (!is_simple(evalresult)) {
2649 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2650 return DIRECTIVE_FOUND;
2651 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002652 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002653 } else {
2654 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002655 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002656 }
2657 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002658
H. Peter Anvine2c80182005-01-15 22:15:51 +00002659 tmp_defining = defining;
2660 defining = nasm_malloc(sizeof(MMacro));
2661 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002662 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002663 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002664 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002665 defining->in_progress = count;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002666 defining->nparam_min = defining->nparam_max = 0;
2667 defining->defaults = NULL;
2668 defining->dlist = NULL;
2669 defining->expansion = NULL;
2670 defining->next_active = istk->mstk;
2671 defining->rep_nest = tmp_defining;
2672 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002673
H. Peter Anvine2c80182005-01-15 22:15:51 +00002674 case PP_ENDREP:
2675 if (!defining || defining->name) {
2676 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2677 return DIRECTIVE_FOUND;
2678 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002679
H. Peter Anvine2c80182005-01-15 22:15:51 +00002680 /*
2681 * Now we have a "macro" defined - although it has no name
2682 * and we won't be entering it in the hash tables - we must
2683 * push a macro-end marker for it on to istk->expansion.
2684 * After that, it will take care of propagating itself (a
2685 * macro-end marker line for a macro which is really a %rep
2686 * block will cause the macro to be re-expanded, complete
2687 * with another macro-end marker to ensure the process
2688 * continues) until the whole expansion is forcibly removed
2689 * from istk->expansion by a %exitrep.
2690 */
2691 l = nasm_malloc(sizeof(Line));
2692 l->next = istk->expansion;
2693 l->finishes = defining;
2694 l->first = NULL;
2695 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002696
H. Peter Anvine2c80182005-01-15 22:15:51 +00002697 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002698
H. Peter Anvine2c80182005-01-15 22:15:51 +00002699 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2700 tmp_defining = defining;
2701 defining = defining->rep_nest;
2702 free_tlist(origline);
2703 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002704
H. Peter Anvine2c80182005-01-15 22:15:51 +00002705 case PP_EXITREP:
2706 /*
2707 * We must search along istk->expansion until we hit a
2708 * macro-end marker for a macro with no name. Then we set
2709 * its `in_progress' flag to 0.
2710 */
2711 for (l = istk->expansion; l; l = l->next)
2712 if (l->finishes && !l->finishes->name)
H. Peter Anvinca348b62008-07-23 10:49:26 -04002713 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002714
H. Peter Anvine2c80182005-01-15 22:15:51 +00002715 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07002716 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002717 else
2718 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2719 free_tlist(origline);
2720 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002721
H. Peter Anvine2c80182005-01-15 22:15:51 +00002722 case PP_XDEFINE:
2723 case PP_IXDEFINE:
2724 case PP_DEFINE:
2725 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002726 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002727
H. Peter Anvine2c80182005-01-15 22:15:51 +00002728 tline = tline->next;
2729 skip_white_(tline);
2730 tline = expand_id(tline);
2731 if (!tline || (tline->type != TOK_ID &&
2732 (tline->type != TOK_PREPROC_ID ||
2733 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002734 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2735 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002736 free_tlist(origline);
2737 return DIRECTIVE_FOUND;
2738 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002739
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002740 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002741
H. Peter Anvine2c80182005-01-15 22:15:51 +00002742 mname = tline->text;
2743 last = tline;
2744 param_start = tline = tline->next;
2745 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002746
H. Peter Anvine2c80182005-01-15 22:15:51 +00002747 /* Expand the macro definition now for %xdefine and %ixdefine */
2748 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2749 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002750
H. Peter Anvine2c80182005-01-15 22:15:51 +00002751 if (tok_is_(tline, "(")) {
2752 /*
2753 * This macro has parameters.
2754 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002755
H. Peter Anvine2c80182005-01-15 22:15:51 +00002756 tline = tline->next;
2757 while (1) {
2758 skip_white_(tline);
2759 if (!tline) {
2760 error(ERR_NONFATAL, "parameter identifier expected");
2761 free_tlist(origline);
2762 return DIRECTIVE_FOUND;
2763 }
2764 if (tline->type != TOK_ID) {
2765 error(ERR_NONFATAL,
2766 "`%s': parameter identifier expected",
2767 tline->text);
2768 free_tlist(origline);
2769 return DIRECTIVE_FOUND;
2770 }
2771 tline->type = TOK_SMAC_PARAM + nparam++;
2772 tline = tline->next;
2773 skip_white_(tline);
2774 if (tok_is_(tline, ",")) {
2775 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04002776 } else {
2777 if (!tok_is_(tline, ")")) {
2778 error(ERR_NONFATAL,
2779 "`)' expected to terminate macro template");
2780 free_tlist(origline);
2781 return DIRECTIVE_FOUND;
2782 }
2783 break;
2784 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002785 }
2786 last = tline;
2787 tline = tline->next;
2788 }
2789 if (tok_type_(tline, TOK_WHITESPACE))
2790 last = tline, tline = tline->next;
2791 macro_start = NULL;
2792 last->next = NULL;
2793 t = tline;
2794 while (t) {
2795 if (t->type == TOK_ID) {
2796 for (tt = param_start; tt; tt = tt->next)
2797 if (tt->type >= TOK_SMAC_PARAM &&
2798 !strcmp(tt->text, t->text))
2799 t->type = tt->type;
2800 }
2801 tt = t->next;
2802 t->next = macro_start;
2803 macro_start = t;
2804 t = tt;
2805 }
2806 /*
2807 * Good. We now have a macro name, a parameter count, and a
2808 * token list (in reverse order) for an expansion. We ought
2809 * to be OK just to create an SMacro, store it, and let
2810 * free_tlist have the rest of the line (which we have
2811 * carefully re-terminated after chopping off the expansion
2812 * from the end).
2813 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002814 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002815 free_tlist(origline);
2816 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002817
H. Peter Anvine2c80182005-01-15 22:15:51 +00002818 case PP_UNDEF:
2819 tline = tline->next;
2820 skip_white_(tline);
2821 tline = expand_id(tline);
2822 if (!tline || (tline->type != TOK_ID &&
2823 (tline->type != TOK_PREPROC_ID ||
2824 tline->text[1] != '$'))) {
2825 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2826 free_tlist(origline);
2827 return DIRECTIVE_FOUND;
2828 }
2829 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002830 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002831 "trailing garbage after macro name ignored");
2832 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002833
H. Peter Anvine2c80182005-01-15 22:15:51 +00002834 /* Find the context that symbol belongs to */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002835 ctx = get_ctx(tline->text, false);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002836 undef_smacro(ctx, tline->text);
2837 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002838 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002839
H. Peter Anvin9e200162008-06-04 17:23:14 -07002840 case PP_DEFSTR:
2841 case PP_IDEFSTR:
2842 casesense = (i == PP_DEFSTR);
2843
2844 tline = tline->next;
2845 skip_white_(tline);
2846 tline = expand_id(tline);
2847 if (!tline || (tline->type != TOK_ID &&
2848 (tline->type != TOK_PREPROC_ID ||
2849 tline->text[1] != '$'))) {
2850 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2851 pp_directives[i]);
2852 free_tlist(origline);
2853 return DIRECTIVE_FOUND;
2854 }
2855
2856 ctx = get_ctx(tline->text, false);
2857
2858 mname = tline->text;
2859 last = tline;
2860 tline = expand_smacro(tline->next);
2861 last->next = NULL;
2862
2863 while (tok_type_(tline, TOK_WHITESPACE))
2864 tline = delete_Token(tline);
2865
2866 p = detoken(tline, false);
2867 macro_start = nasm_malloc(sizeof(*macro_start));
2868 macro_start->next = NULL;
2869 macro_start->text = nasm_quote(p, strlen(p));
2870 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002871 macro_start->a.mac = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07002872 nasm_free(p);
2873
2874 /*
2875 * We now have a macro name, an implicit parameter count of
2876 * zero, and a string token to use as an expansion. Create
2877 * and store an SMacro.
2878 */
2879 define_smacro(ctx, mname, casesense, 0, macro_start);
2880 free_tlist(origline);
2881 return DIRECTIVE_FOUND;
2882
H. Peter Anvin418ca702008-05-30 10:42:30 -07002883 case PP_PATHSEARCH:
2884 {
2885 FILE *fp;
2886 StrList *xsl = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002887 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002888
2889 casesense = true;
2890
2891 tline = tline->next;
2892 skip_white_(tline);
2893 tline = expand_id(tline);
2894 if (!tline || (tline->type != TOK_ID &&
2895 (tline->type != TOK_PREPROC_ID ||
2896 tline->text[1] != '$'))) {
2897 error(ERR_NONFATAL,
2898 "`%%pathsearch' expects a macro identifier as first parameter");
2899 free_tlist(origline);
2900 return DIRECTIVE_FOUND;
2901 }
2902 ctx = get_ctx(tline->text, false);
2903
2904 mname = tline->text;
2905 last = tline;
2906 tline = expand_smacro(tline->next);
2907 last->next = NULL;
2908
2909 t = tline;
2910 while (tok_type_(t, TOK_WHITESPACE))
2911 t = t->next;
2912
2913 if (!t || (t->type != TOK_STRING &&
2914 t->type != TOK_INTERNAL_STRING)) {
2915 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
2916 free_tlist(tline);
2917 free_tlist(origline);
2918 return DIRECTIVE_FOUND; /* but we did _something_ */
2919 }
2920 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002921 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002922 "trailing garbage after `%%pathsearch' ignored");
H. Peter Anvin427cc912008-06-01 21:43:03 -07002923 p = t->text;
2924 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002925 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002926
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002927 fp = inc_fopen(p, &xsl, &xst, true);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002928 if (fp) {
2929 p = xsl->str;
2930 fclose(fp); /* Don't actually care about the file */
2931 }
2932 macro_start = nasm_malloc(sizeof(*macro_start));
2933 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002934 macro_start->text = nasm_quote(p, strlen(p));
H. Peter Anvin418ca702008-05-30 10:42:30 -07002935 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002936 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002937 if (xsl)
2938 nasm_free(xsl);
2939
2940 /*
2941 * We now have a macro name, an implicit parameter count of
2942 * zero, and a string token to use as an expansion. Create
2943 * and store an SMacro.
2944 */
2945 define_smacro(ctx, mname, casesense, 0, macro_start);
2946 free_tlist(tline);
2947 free_tlist(origline);
2948 return DIRECTIVE_FOUND;
2949 }
2950
H. Peter Anvine2c80182005-01-15 22:15:51 +00002951 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002952 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07002953
H. Peter Anvine2c80182005-01-15 22:15:51 +00002954 tline = tline->next;
2955 skip_white_(tline);
2956 tline = expand_id(tline);
2957 if (!tline || (tline->type != TOK_ID &&
2958 (tline->type != TOK_PREPROC_ID ||
2959 tline->text[1] != '$'))) {
2960 error(ERR_NONFATAL,
2961 "`%%strlen' expects a macro identifier as first parameter");
2962 free_tlist(origline);
2963 return DIRECTIVE_FOUND;
2964 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002965 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002966
H. Peter Anvine2c80182005-01-15 22:15:51 +00002967 mname = tline->text;
2968 last = tline;
2969 tline = expand_smacro(tline->next);
2970 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002971
H. Peter Anvine2c80182005-01-15 22:15:51 +00002972 t = tline;
2973 while (tok_type_(t, TOK_WHITESPACE))
2974 t = t->next;
2975 /* t should now point to the string */
2976 if (t->type != TOK_STRING) {
2977 error(ERR_NONFATAL,
2978 "`%%strlen` requires string as second parameter");
2979 free_tlist(tline);
2980 free_tlist(origline);
2981 return DIRECTIVE_FOUND;
2982 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002983
H. Peter Anvine2c80182005-01-15 22:15:51 +00002984 macro_start = nasm_malloc(sizeof(*macro_start));
2985 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002986 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002987 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002988
H. Peter Anvine2c80182005-01-15 22:15:51 +00002989 /*
2990 * We now have a macro name, an implicit parameter count of
2991 * zero, and a numeric token to use as an expansion. Create
2992 * and store an SMacro.
2993 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002994 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002995 free_tlist(tline);
2996 free_tlist(origline);
2997 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002998
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002999 case PP_STRCAT:
3000 casesense = true;
3001
3002 tline = tline->next;
3003 skip_white_(tline);
3004 tline = expand_id(tline);
3005 if (!tline || (tline->type != TOK_ID &&
3006 (tline->type != TOK_PREPROC_ID ||
3007 tline->text[1] != '$'))) {
3008 error(ERR_NONFATAL,
3009 "`%%strcat' expects a macro identifier as first parameter");
3010 free_tlist(origline);
3011 return DIRECTIVE_FOUND;
3012 }
3013 ctx = get_ctx(tline->text, false);
3014
3015 mname = tline->text;
3016 last = tline;
3017 tline = expand_smacro(tline->next);
3018 last->next = NULL;
3019
3020 len = 0;
3021 for (t = tline; t; t = t->next) {
3022 switch (t->type) {
3023 case TOK_WHITESPACE:
3024 break;
3025 case TOK_STRING:
3026 len += t->a.len = nasm_unquote(t->text, NULL);
3027 break;
H. Peter Anvinaccf4332008-07-01 21:42:08 -07003028 case TOK_OTHER:
3029 if (!strcmp(t->text, ",")) /* permit comma separators */
3030 break;
3031 /* else fall through */
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003032 default:
3033 error(ERR_NONFATAL,
3034 "non-string passed to `%%strcat' (%d)", t->type);
3035 free_tlist(tline);
3036 free_tlist(origline);
3037 return DIRECTIVE_FOUND;
3038 }
3039 }
3040
3041 p = pp = nasm_malloc(len);
3042 t = tline;
3043 for (t = tline; t; t = t->next) {
3044 if (t->type == TOK_STRING) {
3045 memcpy(p, t->text, t->a.len);
3046 p += t->a.len;
3047 }
3048 }
3049
3050 /*
3051 * We now have a macro name, an implicit parameter count of
3052 * zero, and a numeric token to use as an expansion. Create
3053 * and store an SMacro.
3054 */
3055 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3056 macro_start->text = nasm_quote(pp, len);
3057 nasm_free(pp);
3058 define_smacro(ctx, mname, casesense, 0, macro_start);
3059 free_tlist(tline);
3060 free_tlist(origline);
3061 return DIRECTIVE_FOUND;
3062
H. Peter Anvine2c80182005-01-15 22:15:51 +00003063 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003064 {
3065 int64_t a1, a2;
3066 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003067
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003068 casesense = true;
3069
H. Peter Anvine2c80182005-01-15 22:15:51 +00003070 tline = tline->next;
3071 skip_white_(tline);
3072 tline = expand_id(tline);
3073 if (!tline || (tline->type != TOK_ID &&
3074 (tline->type != TOK_PREPROC_ID ||
3075 tline->text[1] != '$'))) {
3076 error(ERR_NONFATAL,
3077 "`%%substr' expects a macro identifier as first parameter");
3078 free_tlist(origline);
3079 return DIRECTIVE_FOUND;
3080 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003081 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003082
H. Peter Anvine2c80182005-01-15 22:15:51 +00003083 mname = tline->text;
3084 last = tline;
3085 tline = expand_smacro(tline->next);
3086 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003087
H. Peter Anvine2c80182005-01-15 22:15:51 +00003088 t = tline->next;
3089 while (tok_type_(t, TOK_WHITESPACE))
3090 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003091
H. Peter Anvine2c80182005-01-15 22:15:51 +00003092 /* t should now point to the string */
3093 if (t->type != TOK_STRING) {
3094 error(ERR_NONFATAL,
3095 "`%%substr` requires string as second parameter");
3096 free_tlist(tline);
3097 free_tlist(origline);
3098 return DIRECTIVE_FOUND;
3099 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003100
H. Peter Anvine2c80182005-01-15 22:15:51 +00003101 tt = t->next;
3102 tptr = &tt;
3103 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003104 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3105 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003106 if (!evalresult) {
3107 free_tlist(tline);
3108 free_tlist(origline);
3109 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003110 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003111 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3112 free_tlist(tline);
3113 free_tlist(origline);
3114 return DIRECTIVE_FOUND;
3115 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003116 a1 = evalresult->value-1;
3117
3118 while (tok_type_(tt, TOK_WHITESPACE))
3119 tt = tt->next;
3120 if (!tt) {
3121 a2 = 1; /* Backwards compatibility: one character */
3122 } else {
3123 tokval.t_type = TOKEN_INVALID;
3124 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3125 pass, error, NULL);
3126 if (!evalresult) {
3127 free_tlist(tline);
3128 free_tlist(origline);
3129 return DIRECTIVE_FOUND;
3130 } else if (!is_simple(evalresult)) {
3131 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3132 free_tlist(tline);
3133 free_tlist(origline);
3134 return DIRECTIVE_FOUND;
3135 }
3136 a2 = evalresult->value;
3137 }
3138
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003139 len = nasm_unquote(t->text, NULL);
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003140 if (a2 < 0)
3141 a2 = a2+1+len-a1;
3142 if (a1+a2 > (int64_t)len)
3143 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003144
H. Peter Anvine2c80182005-01-15 22:15:51 +00003145 macro_start = nasm_malloc(sizeof(*macro_start));
3146 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003147 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003148 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003149 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003150
H. Peter Anvine2c80182005-01-15 22:15:51 +00003151 /*
3152 * We now have a macro name, an implicit parameter count of
3153 * zero, and a numeric token to use as an expansion. Create
3154 * and store an SMacro.
3155 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003156 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003157 free_tlist(tline);
3158 free_tlist(origline);
3159 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003160 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003161
H. Peter Anvine2c80182005-01-15 22:15:51 +00003162 case PP_ASSIGN:
3163 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003164 casesense = (i == PP_ASSIGN);
3165
H. Peter Anvine2c80182005-01-15 22:15:51 +00003166 tline = tline->next;
3167 skip_white_(tline);
3168 tline = expand_id(tline);
3169 if (!tline || (tline->type != TOK_ID &&
3170 (tline->type != TOK_PREPROC_ID ||
3171 tline->text[1] != '$'))) {
3172 error(ERR_NONFATAL,
3173 "`%%%sassign' expects a macro identifier",
3174 (i == PP_IASSIGN ? "i" : ""));
3175 free_tlist(origline);
3176 return DIRECTIVE_FOUND;
3177 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003178 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003179
H. Peter Anvine2c80182005-01-15 22:15:51 +00003180 mname = tline->text;
3181 last = tline;
3182 tline = expand_smacro(tline->next);
3183 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003184
H. Peter Anvine2c80182005-01-15 22:15:51 +00003185 t = tline;
3186 tptr = &t;
3187 tokval.t_type = TOKEN_INVALID;
3188 evalresult =
3189 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3190 free_tlist(tline);
3191 if (!evalresult) {
3192 free_tlist(origline);
3193 return DIRECTIVE_FOUND;
3194 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003195
H. Peter Anvine2c80182005-01-15 22:15:51 +00003196 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003197 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003198 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003199
H. Peter Anvine2c80182005-01-15 22:15:51 +00003200 if (!is_simple(evalresult)) {
3201 error(ERR_NONFATAL,
3202 "non-constant value given to `%%%sassign'",
3203 (i == PP_IASSIGN ? "i" : ""));
3204 free_tlist(origline);
3205 return DIRECTIVE_FOUND;
3206 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003207
H. Peter Anvine2c80182005-01-15 22:15:51 +00003208 macro_start = nasm_malloc(sizeof(*macro_start));
3209 macro_start->next = NULL;
3210 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003211 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003212
H. Peter Anvine2c80182005-01-15 22:15:51 +00003213 /*
3214 * We now have a macro name, an implicit parameter count of
3215 * zero, and a numeric token to use as an expansion. Create
3216 * and store an SMacro.
3217 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003218 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003219 free_tlist(origline);
3220 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003221
H. Peter Anvine2c80182005-01-15 22:15:51 +00003222 case PP_LINE:
3223 /*
3224 * Syntax is `%line nnn[+mmm] [filename]'
3225 */
3226 tline = tline->next;
3227 skip_white_(tline);
3228 if (!tok_type_(tline, TOK_NUMBER)) {
3229 error(ERR_NONFATAL, "`%%line' expects line number");
3230 free_tlist(origline);
3231 return DIRECTIVE_FOUND;
3232 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003233 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003234 m = 1;
3235 tline = tline->next;
3236 if (tok_is_(tline, "+")) {
3237 tline = tline->next;
3238 if (!tok_type_(tline, TOK_NUMBER)) {
3239 error(ERR_NONFATAL, "`%%line' expects line increment");
3240 free_tlist(origline);
3241 return DIRECTIVE_FOUND;
3242 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003243 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003244 tline = tline->next;
3245 }
3246 skip_white_(tline);
3247 src_set_linnum(k);
3248 istk->lineinc = m;
3249 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003250 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003251 }
3252 free_tlist(origline);
3253 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003254
H. Peter Anvine2c80182005-01-15 22:15:51 +00003255 default:
3256 error(ERR_FATAL,
3257 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003258 pp_directives[i]);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003259 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003260 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003261}
3262
3263/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003264 * Ensure that a macro parameter contains a condition code and
3265 * nothing else. Return the condition code index if so, or -1
3266 * otherwise.
3267 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003268static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003269{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003270 Token *tt;
3271 int i, j, k, m;
3272
H. Peter Anvin25a99342007-09-22 17:45:45 -07003273 if (!t)
3274 return -1; /* Probably a %+ without a space */
3275
H. Peter Anvineba20a72002-04-30 20:53:55 +00003276 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003277 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003278 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003279 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003280 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003281 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003282 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003283
3284 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00003285 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003286 while (j - i > 1) {
3287 k = (j + i) / 2;
3288 m = nasm_stricmp(t->text, conditions[k]);
3289 if (m == 0) {
3290 i = k;
3291 j = -2;
3292 break;
3293 } else if (m < 0) {
3294 j = k;
3295 } else
3296 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003297 }
3298 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003299 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003300 return i;
3301}
3302
3303/*
3304 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3305 * %-n) and MMacro-local identifiers (%%foo).
3306 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003307static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003308{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003309 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003310
3311 tail = &thead;
3312 thead = NULL;
3313
H. Peter Anvine2c80182005-01-15 22:15:51 +00003314 while (tline) {
3315 if (tline->type == TOK_PREPROC_ID &&
3316 (((tline->text[1] == '+' || tline->text[1] == '-')
3317 && tline->text[2]) || tline->text[1] == '%'
3318 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003319 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003320 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003321 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003322 unsigned int n;
3323 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003324 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003325
H. Peter Anvine2c80182005-01-15 22:15:51 +00003326 t = tline;
3327 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003328
H. Peter Anvine2c80182005-01-15 22:15:51 +00003329 mac = istk->mstk;
3330 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3331 mac = mac->next_active;
3332 if (!mac)
3333 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3334 else
3335 switch (t->text[1]) {
3336 /*
3337 * We have to make a substitution of one of the
3338 * forms %1, %-1, %+1, %%foo, %0.
3339 */
3340 case '0':
3341 type = TOK_NUMBER;
3342 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3343 text = nasm_strdup(tmpbuf);
3344 break;
3345 case '%':
3346 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003347 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00003348 mac->unique);
3349 text = nasm_strcat(tmpbuf, t->text + 2);
3350 break;
3351 case '-':
3352 n = atoi(t->text + 2) - 1;
3353 if (n >= mac->nparam)
3354 tt = NULL;
3355 else {
3356 if (mac->nparam > 1)
3357 n = (n + mac->rotate) % mac->nparam;
3358 tt = mac->params[n];
3359 }
3360 cc = find_cc(tt);
3361 if (cc == -1) {
3362 error(ERR_NONFATAL,
3363 "macro parameter %d is not a condition code",
3364 n + 1);
3365 text = NULL;
3366 } else {
3367 type = TOK_ID;
3368 if (inverse_ccs[cc] == -1) {
3369 error(ERR_NONFATAL,
3370 "condition code `%s' is not invertible",
3371 conditions[cc]);
3372 text = NULL;
3373 } else
3374 text =
3375 nasm_strdup(conditions[inverse_ccs[cc]]);
3376 }
3377 break;
3378 case '+':
3379 n = atoi(t->text + 2) - 1;
3380 if (n >= mac->nparam)
3381 tt = NULL;
3382 else {
3383 if (mac->nparam > 1)
3384 n = (n + mac->rotate) % mac->nparam;
3385 tt = mac->params[n];
3386 }
3387 cc = find_cc(tt);
3388 if (cc == -1) {
3389 error(ERR_NONFATAL,
3390 "macro parameter %d is not a condition code",
3391 n + 1);
3392 text = NULL;
3393 } else {
3394 type = TOK_ID;
3395 text = nasm_strdup(conditions[cc]);
3396 }
3397 break;
3398 default:
3399 n = atoi(t->text + 1) - 1;
3400 if (n >= mac->nparam)
3401 tt = NULL;
3402 else {
3403 if (mac->nparam > 1)
3404 n = (n + mac->rotate) % mac->nparam;
3405 tt = mac->params[n];
3406 }
3407 if (tt) {
3408 for (i = 0; i < mac->paramlen[n]; i++) {
3409 *tail = new_Token(NULL, tt->type, tt->text, 0);
3410 tail = &(*tail)->next;
3411 tt = tt->next;
3412 }
3413 }
3414 text = NULL; /* we've done it here */
3415 break;
3416 }
3417 if (!text) {
3418 delete_Token(t);
3419 } else {
3420 *tail = t;
3421 tail = &t->next;
3422 t->type = type;
3423 nasm_free(t->text);
3424 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003425 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003426 }
3427 continue;
3428 } else {
3429 t = *tail = tline;
3430 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003431 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003432 tail = &t->next;
3433 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003434 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003435 *tail = NULL;
3436 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003437 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003438 switch (t->type) {
3439 case TOK_WHITESPACE:
3440 if (tt->type == TOK_WHITESPACE) {
3441 t->next = delete_Token(tt);
3442 }
3443 break;
3444 case TOK_ID:
3445 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003446 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003447 nasm_free(t->text);
3448 t->text = tmp;
3449 t->next = delete_Token(tt);
3450 }
3451 break;
3452 case TOK_NUMBER:
3453 if (tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003454 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003455 nasm_free(t->text);
3456 t->text = tmp;
3457 t->next = delete_Token(tt);
3458 }
3459 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003460 default:
3461 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003462 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003463
H. Peter Anvin76690a12002-04-30 20:52:49 +00003464 return thead;
3465}
3466
3467/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003468 * Expand all single-line macro calls made in the given line.
3469 * Return the expanded version of the line. The original is deemed
3470 * to be destroyed in the process. (In reality we'll just move
3471 * Tokens from input to output a lot of the time, rather than
3472 * actually bothering to destroy and replicate.)
3473 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003474#define DEADMAN_LIMIT (1 << 20)
3475
H. Peter Anvine2c80182005-01-15 22:15:51 +00003476static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003477{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003478 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003479 struct hash_table *smtbl;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003480 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003481 Token **params;
3482 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003483 unsigned int nparam, sparam;
3484 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003485 Token *org_tline = tline;
3486 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003487 char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003488 int deadman = DEADMAN_LIMIT;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003489
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003490 /*
3491 * Trick: we should avoid changing the start token pointer since it can
3492 * be contained in "next" field of other token. Because of this
3493 * we allocate a copy of first token and work with it; at the end of
3494 * routine we copy it back
3495 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003496 if (org_tline) {
3497 tline =
3498 new_Token(org_tline->next, org_tline->type, org_tline->text,
3499 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003500 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003501 nasm_free(org_tline->text);
3502 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003503 }
3504
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003505again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003506 tail = &thead;
3507 thead = NULL;
3508
H. Peter Anvine2c80182005-01-15 22:15:51 +00003509 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003510 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003511 error(ERR_NONFATAL, "interminable macro recursion");
3512 break;
3513 }
3514
H. Peter Anvine2c80182005-01-15 22:15:51 +00003515 if ((mname = tline->text)) {
3516 /* if this token is a local macro, look in local context */
H. Peter Anvinb037a672008-05-29 19:08:08 -07003517 ctx = NULL;
3518 smtbl = &smacros;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003519 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003520 ctx = get_ctx(mname, true);
H. Peter Anvinb037a672008-05-29 19:08:08 -07003521 if (ctx)
3522 smtbl = &ctx->localmac;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003523 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07003524 head = (SMacro *) hash_findix(smtbl, mname);
H. Peter Anvin072771e2008-05-22 13:17:51 -07003525
H. Peter Anvine2c80182005-01-15 22:15:51 +00003526 /*
3527 * We've hit an identifier. As in is_mmacro below, we first
3528 * check whether the identifier is a single-line macro at
3529 * all, then think about checking for parameters if
3530 * necessary.
3531 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003532 for (m = head; m; m = m->next)
3533 if (!mstrcmp(m->name, mname, m->casesense))
3534 break;
3535 if (m) {
3536 mstart = tline;
3537 params = NULL;
3538 paramsize = NULL;
3539 if (m->nparam == 0) {
3540 /*
3541 * Simple case: the macro is parameterless. Discard the
3542 * one token that the macro call took, and push the
3543 * expansion back on the to-do stack.
3544 */
3545 if (!m->expansion) {
3546 if (!strcmp("__FILE__", m->name)) {
3547 int32_t num = 0;
H. Peter Anvin932de6c2008-07-31 18:46:11 -07003548 char *file = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003549 src_get(&num, &file);
3550 tline->text = nasm_quote(file, strlen(file));
H. Peter Anvin97a23472007-09-16 17:57:25 -07003551 tline->type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003552 nasm_free(file);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003553 continue;
3554 }
3555 if (!strcmp("__LINE__", m->name)) {
3556 nasm_free(tline->text);
3557 make_tok_num(tline, src_get_linnum());
3558 continue;
3559 }
3560 if (!strcmp("__BITS__", m->name)) {
3561 nasm_free(tline->text);
3562 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003563 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003564 }
3565 tline = delete_Token(tline);
3566 continue;
3567 }
3568 } else {
3569 /*
3570 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003571 * exists and takes parameters. We must find the
3572 * parameters in the call, count them, find the SMacro
3573 * that corresponds to that form of the macro call, and
3574 * substitute for the parameters when we expand. What a
3575 * pain.
3576 */
3577 /*tline = tline->next;
3578 skip_white_(tline); */
3579 do {
3580 t = tline->next;
3581 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003582 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003583 t->text = NULL;
3584 t = tline->next = delete_Token(t);
3585 }
3586 tline = t;
3587 } while (tok_type_(tline, TOK_WHITESPACE));
3588 if (!tok_is_(tline, "(")) {
3589 /*
3590 * This macro wasn't called with parameters: ignore
3591 * the call. (Behaviour borrowed from gnu cpp.)
3592 */
3593 tline = mstart;
3594 m = NULL;
3595 } else {
3596 int paren = 0;
3597 int white = 0;
3598 brackets = 0;
3599 nparam = 0;
3600 sparam = PARAM_DELTA;
3601 params = nasm_malloc(sparam * sizeof(Token *));
3602 params[0] = tline->next;
3603 paramsize = nasm_malloc(sparam * sizeof(int));
3604 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003605 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003606 /*
3607 * For some unusual expansions
3608 * which concatenates function call
3609 */
3610 t = tline->next;
3611 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003612 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003613 t->text = NULL;
3614 t = tline->next = delete_Token(t);
3615 }
3616 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003617
H. Peter Anvine2c80182005-01-15 22:15:51 +00003618 if (!tline) {
3619 error(ERR_NONFATAL,
3620 "macro call expects terminating `)'");
3621 break;
3622 }
3623 if (tline->type == TOK_WHITESPACE
3624 && brackets <= 0) {
3625 if (paramsize[nparam])
3626 white++;
3627 else
3628 params[nparam] = tline->next;
3629 continue; /* parameter loop */
3630 }
3631 if (tline->type == TOK_OTHER
3632 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003633 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003634 if (ch == ',' && !paren && brackets <= 0) {
3635 if (++nparam >= sparam) {
3636 sparam += PARAM_DELTA;
3637 params = nasm_realloc(params,
3638 sparam *
3639 sizeof(Token
3640 *));
3641 paramsize =
3642 nasm_realloc(paramsize,
3643 sparam *
3644 sizeof(int));
3645 }
3646 params[nparam] = tline->next;
3647 paramsize[nparam] = 0;
3648 white = 0;
3649 continue; /* parameter loop */
3650 }
3651 if (ch == '{' &&
3652 (brackets > 0 || (brackets == 0 &&
3653 !paramsize[nparam])))
3654 {
3655 if (!(brackets++)) {
3656 params[nparam] = tline->next;
3657 continue; /* parameter loop */
3658 }
3659 }
3660 if (ch == '}' && brackets > 0)
3661 if (--brackets == 0) {
3662 brackets = -1;
3663 continue; /* parameter loop */
3664 }
3665 if (ch == '(' && !brackets)
3666 paren++;
3667 if (ch == ')' && brackets <= 0)
3668 if (--paren < 0)
3669 break;
3670 }
3671 if (brackets < 0) {
3672 brackets = 0;
3673 error(ERR_NONFATAL, "braces do not "
3674 "enclose all of macro parameter");
3675 }
3676 paramsize[nparam] += white + 1;
3677 white = 0;
3678 } /* parameter loop */
3679 nparam++;
3680 while (m && (m->nparam != nparam ||
3681 mstrcmp(m->name, mname,
3682 m->casesense)))
3683 m = m->next;
3684 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003685 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003686 "macro `%s' exists, "
3687 "but not taking %d parameters",
3688 mstart->text, nparam);
3689 }
3690 }
3691 if (m && m->in_progress)
3692 m = NULL;
3693 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003694 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003695 * Design question: should we handle !tline, which
3696 * indicates missing ')' here, or expand those
3697 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003698 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003699 */
3700 nasm_free(params);
3701 nasm_free(paramsize);
3702 tline = mstart;
3703 } else {
3704 /*
3705 * Expand the macro: we are placed on the last token of the
3706 * call, so that we can easily split the call from the
3707 * following tokens. We also start by pushing an SMAC_END
3708 * token for the cycle removal.
3709 */
3710 t = tline;
3711 if (t) {
3712 tline = t->next;
3713 t->next = NULL;
3714 }
3715 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003716 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003717 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003718 tline = tt;
3719 for (t = m->expansion; t; t = t->next) {
3720 if (t->type >= TOK_SMAC_PARAM) {
3721 Token *pcopy = tline, **ptail = &pcopy;
3722 Token *ttt, *pt;
3723 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003724
H. Peter Anvine2c80182005-01-15 22:15:51 +00003725 ttt = params[t->type - TOK_SMAC_PARAM];
3726 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3727 --i >= 0;) {
3728 pt = *ptail =
3729 new_Token(tline, ttt->type, ttt->text,
3730 0);
3731 ptail = &pt->next;
3732 ttt = ttt->next;
3733 }
3734 tline = pcopy;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -07003735 } else if (t->type == TOK_PREPROC_Q) {
3736 tt = new_Token(tline, TOK_ID, mname, 0);
3737 tline = tt;
3738 } else if (t->type == TOK_PREPROC_QQ) {
3739 tt = new_Token(tline, TOK_ID, m->name, 0);
3740 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003741 } else {
3742 tt = new_Token(tline, t->type, t->text, 0);
3743 tline = tt;
3744 }
3745 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003746
H. Peter Anvine2c80182005-01-15 22:15:51 +00003747 /*
3748 * Having done that, get rid of the macro call, and clean
3749 * up the parameters.
3750 */
3751 nasm_free(params);
3752 nasm_free(paramsize);
3753 free_tlist(mstart);
3754 continue; /* main token loop */
3755 }
3756 }
3757 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003758
H. Peter Anvine2c80182005-01-15 22:15:51 +00003759 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003760 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003761 tline = delete_Token(tline);
3762 } else {
3763 t = *tail = tline;
3764 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003765 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003766 t->next = NULL;
3767 tail = &t->next;
3768 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003769 }
3770
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003771 /*
3772 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003773 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003774 * TOK_IDs should be concatenated.
3775 * Also we look for %+ tokens and concatenate the tokens before and after
3776 * them (without white spaces in between).
3777 */
3778 t = thead;
3779 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003780 while (t) {
3781 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3782 t = t->next;
3783 if (!t || !t->next)
3784 break;
3785 if (t->next->type == TOK_ID ||
3786 t->next->type == TOK_PREPROC_ID ||
3787 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003788 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003789 nasm_free(t->text);
3790 t->next = delete_Token(t->next);
3791 t->text = p;
3792 rescan = 1;
3793 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3794 t->next->next->type == TOK_PREPROC_ID &&
3795 strcmp(t->next->next->text, "%+") == 0) {
3796 /* free the next whitespace, the %+ token and next whitespace */
3797 int i;
3798 for (i = 1; i <= 3; i++) {
3799 if (!t->next
3800 || (i != 2 && t->next->type != TOK_WHITESPACE))
3801 break;
3802 t->next = delete_Token(t->next);
3803 } /* endfor */
3804 } else
3805 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003806 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003807 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003808 if (rescan) {
3809 tline = thead;
3810 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003811 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003812
H. Peter Anvine2c80182005-01-15 22:15:51 +00003813 if (org_tline) {
3814 if (thead) {
3815 *org_tline = *thead;
3816 /* since we just gave text to org_line, don't free it */
3817 thead->text = NULL;
3818 delete_Token(thead);
3819 } else {
3820 /* the expression expanded to empty line;
3821 we can't return NULL for some reasons
3822 we just set the line to a single WHITESPACE token. */
3823 memset(org_tline, 0, sizeof(*org_tline));
3824 org_tline->text = NULL;
3825 org_tline->type = TOK_WHITESPACE;
3826 }
3827 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003828 }
3829
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003830 return thead;
3831}
3832
3833/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003834 * Similar to expand_smacro but used exclusively with macro identifiers
3835 * right before they are fetched in. The reason is that there can be
3836 * identifiers consisting of several subparts. We consider that if there
3837 * are more than one element forming the name, user wants a expansion,
3838 * otherwise it will be left as-is. Example:
3839 *
3840 * %define %$abc cde
3841 *
3842 * the identifier %$abc will be left as-is so that the handler for %define
3843 * will suck it and define the corresponding value. Other case:
3844 *
3845 * %define _%$abc cde
3846 *
3847 * In this case user wants name to be expanded *before* %define starts
3848 * working, so we'll expand %$abc into something (if it has a value;
3849 * otherwise it will be left as-is) then concatenate all successive
3850 * PP_IDs into one.
3851 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003852static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003853{
3854 Token *cur, *oldnext = NULL;
3855
H. Peter Anvin734b1882002-04-30 21:01:08 +00003856 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003857 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003858
3859 cur = tline;
3860 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003861 (cur->next->type == TOK_ID ||
3862 cur->next->type == TOK_PREPROC_ID
3863 || cur->next->type == TOK_NUMBER))
3864 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003865
3866 /* If identifier consists of just one token, don't expand */
3867 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003868 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003869
H. Peter Anvine2c80182005-01-15 22:15:51 +00003870 if (cur) {
3871 oldnext = cur->next; /* Detach the tail past identifier */
3872 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003873 }
3874
H. Peter Anvin734b1882002-04-30 21:01:08 +00003875 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003876
H. Peter Anvine2c80182005-01-15 22:15:51 +00003877 if (cur) {
3878 /* expand_smacro possibly changhed tline; re-scan for EOL */
3879 cur = tline;
3880 while (cur && cur->next)
3881 cur = cur->next;
3882 if (cur)
3883 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003884 }
3885
3886 return tline;
3887}
3888
3889/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003890 * Determine whether the given line constitutes a multi-line macro
3891 * call, and return the MMacro structure called if so. Doesn't have
3892 * to check for an initial label - that's taken care of in
3893 * expand_mmacro - but must check numbers of parameters. Guaranteed
3894 * to be called with tline->type == TOK_ID, so the putative macro
3895 * name is easy to find.
3896 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003897static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003898{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003899 MMacro *head, *m;
3900 Token **params;
3901 int nparam;
3902
H. Peter Anvin166c2472008-05-28 12:28:58 -07003903 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003904
3905 /*
3906 * Efficiency: first we see if any macro exists with the given
3907 * name. If not, we can return NULL immediately. _Then_ we
3908 * count the parameters, and then we look further along the
3909 * list if necessary to find the proper MMacro.
3910 */
3911 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003912 if (!mstrcmp(m->name, tline->text, m->casesense))
3913 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003914 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003915 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003916
3917 /*
3918 * OK, we have a potential macro. Count and demarcate the
3919 * parameters.
3920 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003921 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003922
3923 /*
3924 * So we know how many parameters we've got. Find the MMacro
3925 * structure that handles this number.
3926 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003927 while (m) {
3928 if (m->nparam_min <= nparam
3929 && (m->plus || nparam <= m->nparam_max)) {
3930 /*
3931 * This one is right. Just check if cycle removal
3932 * prohibits us using it before we actually celebrate...
3933 */
3934 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003935#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003936 error(ERR_NONFATAL,
3937 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003938#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003939 nasm_free(params);
3940 return NULL;
3941 }
3942 /*
3943 * It's right, and we can use it. Add its default
3944 * parameters to the end of our list if necessary.
3945 */
3946 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3947 params =
3948 nasm_realloc(params,
3949 ((m->nparam_min + m->ndefs +
3950 1) * sizeof(*params)));
3951 while (nparam < m->nparam_min + m->ndefs) {
3952 params[nparam] = m->defaults[nparam - m->nparam_min];
3953 nparam++;
3954 }
3955 }
3956 /*
3957 * If we've gone over the maximum parameter count (and
3958 * we're in Plus mode), ignore parameters beyond
3959 * nparam_max.
3960 */
3961 if (m->plus && nparam > m->nparam_max)
3962 nparam = m->nparam_max;
3963 /*
3964 * Then terminate the parameter list, and leave.
3965 */
3966 if (!params) { /* need this special case */
3967 params = nasm_malloc(sizeof(*params));
3968 nparam = 0;
3969 }
3970 params[nparam] = NULL;
3971 *params_array = params;
3972 return m;
3973 }
3974 /*
3975 * This one wasn't right: look for the next one with the
3976 * same name.
3977 */
3978 for (m = m->next; m; m = m->next)
3979 if (!mstrcmp(m->name, tline->text, m->casesense))
3980 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003981 }
3982
3983 /*
3984 * After all that, we didn't find one with the right number of
3985 * parameters. Issue a warning, and fail to expand the macro.
3986 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07003987 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003988 "macro `%s' exists, but not taking %d parameters",
3989 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003990 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003991 return NULL;
3992}
3993
3994/*
3995 * Expand the multi-line macro call made by the given line, if
3996 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003997 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003998 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003999static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004000{
4001 Token *startline = tline;
4002 Token *label = NULL;
4003 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004004 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004005 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004006 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004007 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004008 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004009
4010 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004011 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004012 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004013 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004014 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004015 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004016 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004017 if (m) {
4018 mname = t->text;
4019 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004020 Token *last;
4021 /*
4022 * We have an id which isn't a macro call. We'll assume
4023 * it might be a label; we'll also check to see if a
4024 * colon follows it. Then, if there's another id after
4025 * that lot, we'll check it again for macro-hood.
4026 */
4027 label = last = t;
4028 t = t->next;
4029 if (tok_type_(t, TOK_WHITESPACE))
4030 last = t, t = t->next;
4031 if (tok_is_(t, ":")) {
4032 dont_prepend = 1;
4033 last = t, t = t->next;
4034 if (tok_type_(t, TOK_WHITESPACE))
4035 last = t, t = t->next;
4036 }
4037 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
4038 return 0;
4039 last->next = NULL;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004040 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004041 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004042 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004043
4044 /*
4045 * Fix up the parameters: this involves stripping leading and
4046 * trailing whitespace, then stripping braces if they are
4047 * present.
4048 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004049 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004050 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004051
H. Peter Anvine2c80182005-01-15 22:15:51 +00004052 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004053 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004054 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004055
H. Peter Anvine2c80182005-01-15 22:15:51 +00004056 t = params[i];
4057 skip_white_(t);
4058 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004059 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004060 params[i] = t;
4061 paramlen[i] = 0;
4062 while (t) {
4063 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4064 break; /* ... because we have hit a comma */
4065 if (comma && t->type == TOK_WHITESPACE
4066 && tok_is_(t->next, ","))
4067 break; /* ... or a space then a comma */
4068 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4069 break; /* ... or a brace */
4070 t = t->next;
4071 paramlen[i]++;
4072 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004073 }
4074
4075 /*
4076 * OK, we have a MMacro structure together with a set of
4077 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004078 * copies of each Line on to istk->expansion. Substitution of
4079 * parameter tokens and macro-local tokens doesn't get done
4080 * until the single-line macro substitution process; this is
4081 * because delaying them allows us to change the semantics
4082 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004083 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004084 * First, push an end marker on to istk->expansion, mark this
4085 * macro as in progress, and set up its invocation-specific
4086 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004087 */
4088 ll = nasm_malloc(sizeof(Line));
4089 ll->next = istk->expansion;
4090 ll->finishes = m;
4091 ll->first = NULL;
4092 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004093
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004094 m->in_progress = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004095 m->params = params;
4096 m->iline = tline;
4097 m->nparam = nparam;
4098 m->rotate = 0;
4099 m->paramlen = paramlen;
4100 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004101 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004102
4103 m->next_active = istk->mstk;
4104 istk->mstk = m;
4105
H. Peter Anvine2c80182005-01-15 22:15:51 +00004106 for (l = m->expansion; l; l = l->next) {
4107 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004108
H. Peter Anvine2c80182005-01-15 22:15:51 +00004109 ll = nasm_malloc(sizeof(Line));
4110 ll->finishes = NULL;
4111 ll->next = istk->expansion;
4112 istk->expansion = ll;
4113 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004114
H. Peter Anvine2c80182005-01-15 22:15:51 +00004115 for (t = l->first; t; t = t->next) {
4116 Token *x = t;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004117 switch (t->type) {
4118 case TOK_PREPROC_Q:
H. Peter Anvinc751e862008-06-09 10:18:45 -07004119 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004120 break;
4121 case TOK_PREPROC_QQ:
4122 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4123 break;
4124 case TOK_PREPROC_ID:
4125 if (t->text[1] == '0' && t->text[2] == '0') {
4126 dont_prepend = -1;
4127 x = label;
4128 if (!x)
4129 continue;
4130 }
4131 /* fall through */
4132 default:
4133 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4134 break;
H. Peter Anvin166c2472008-05-28 12:28:58 -07004135 }
H. Peter Anvince2233b2008-05-25 21:57:00 -07004136 tail = &tt->next;
4137 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004138 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004139 }
4140
4141 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004142 * If we had a label, push it on as the first line of
4143 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004144 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004145 if (label) {
4146 if (dont_prepend < 0)
4147 free_tlist(startline);
4148 else {
4149 ll = nasm_malloc(sizeof(Line));
4150 ll->finishes = NULL;
4151 ll->next = istk->expansion;
4152 istk->expansion = ll;
4153 ll->first = startline;
4154 if (!dont_prepend) {
4155 while (label->next)
4156 label = label->next;
4157 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4158 }
4159 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004160 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004161
H. Peter Anvin734b1882002-04-30 21:01:08 +00004162 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004163
H. Peter Anvineba20a72002-04-30 20:53:55 +00004164 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004165}
4166
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004167/* The function that actually does the error reporting */
4168static void verror(int severity, const char *fmt, va_list arg)
4169{
4170 char buff[1024];
4171
4172 vsnprintf(buff, sizeof(buff), fmt, arg);
4173
4174 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004175 _error(severity, "(%s:%d) %s", istk->mstk->name,
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004176 istk->mstk->lineno, buff);
4177 else
H. Peter Anvin917a3492008-09-24 09:14:49 -07004178 _error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004179}
4180
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004181/*
4182 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004183 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004184 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004185static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004186{
4187 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004188
4189 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004190 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004191 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004192
H. Peter Anvin734b1882002-04-30 21:01:08 +00004193 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004194 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004195 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004196}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004197
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004198/*
4199 * Because %else etc are evaluated in the state context
4200 * of the previous branch, errors might get lost with error():
4201 * %if 0 ... %else trailing garbage ... %endif
4202 * So %else etc should report errors with this function.
4203 */
4204static void error_precond(int severity, const char *fmt, ...)
4205{
4206 va_list arg;
4207
4208 /* Only ignore the error if it's really in a dead branch */
4209 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4210 return;
4211
4212 va_start(arg, fmt);
4213 verror(severity, fmt, arg);
4214 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004215}
4216
H. Peter Anvin734b1882002-04-30 21:01:08 +00004217static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004218pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004219 ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004220{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004221 Token *t;
4222
H. Peter Anvin99941bf2002-05-14 17:44:03 +00004223 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004224 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004225 istk = nasm_malloc(sizeof(Include));
4226 istk->next = NULL;
4227 istk->conds = NULL;
4228 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004229 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004230 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004231 istk->fname = NULL;
4232 src_set_fname(nasm_strdup(file));
4233 src_set_linnum(0);
4234 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004235 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004236 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004237 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004238 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004239 nested_mac_count = 0;
4240 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004241 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004242 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004243 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004244 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004245 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004246 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004247 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004248 any_extrastdmac = extrastdmac && *extrastdmac;
4249 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004250 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004251 evaluate = eval;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004252
4253 /*
4254 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4255 * The caller, however, will also pass in 3 for preprocess-only so
4256 * we can set __PASS__ accordingly.
4257 */
4258 pass = apass > 2 ? 2 : apass;
4259
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004260 dephead = deptail = deplist;
4261 if (deplist) {
4262 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4263 sl->next = NULL;
4264 strcpy(sl->str, file);
4265 *deptail = sl;
4266 deptail = &sl->next;
4267 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004268
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004269 /*
4270 * Define the __PASS__ macro. This is defined here unlike
4271 * all the other builtins, because it is special -- it varies between
4272 * passes.
4273 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004274 t = nasm_malloc(sizeof(*t));
4275 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004276 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004277 t->a.mac = NULL;
4278 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004279}
4280
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004281static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004282{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004283 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004284 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004285
H. Peter Anvine2c80182005-01-15 22:15:51 +00004286 while (1) {
4287 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004288 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004289 * buffer or from the input file.
4290 */
4291 tline = NULL;
4292 while (istk->expansion && istk->expansion->finishes) {
4293 Line *l = istk->expansion;
4294 if (!l->finishes->name && l->finishes->in_progress > 1) {
4295 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004296
H. Peter Anvine2c80182005-01-15 22:15:51 +00004297 /*
4298 * This is a macro-end marker for a macro with no
4299 * name, which means it's not really a macro at all
4300 * but a %rep block, and the `in_progress' field is
4301 * more than 1, meaning that we still need to
4302 * repeat. (1 means the natural last repetition; 0
4303 * means termination by %exitrep.) We have
4304 * therefore expanded up to the %endrep, and must
4305 * push the whole block on to the expansion buffer
4306 * again. We don't bother to remove the macro-end
4307 * marker: we'd only have to generate another one
4308 * if we did.
4309 */
4310 l->finishes->in_progress--;
4311 for (l = l->finishes->expansion; l; l = l->next) {
4312 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004313
H. Peter Anvine2c80182005-01-15 22:15:51 +00004314 ll = nasm_malloc(sizeof(Line));
4315 ll->next = istk->expansion;
4316 ll->finishes = NULL;
4317 ll->first = NULL;
4318 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004319
H. Peter Anvine2c80182005-01-15 22:15:51 +00004320 for (t = l->first; t; t = t->next) {
4321 if (t->text || t->type == TOK_WHITESPACE) {
4322 tt = *tail =
4323 new_Token(NULL, t->type, t->text, 0);
4324 tail = &tt->next;
4325 }
4326 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004327
H. Peter Anvine2c80182005-01-15 22:15:51 +00004328 istk->expansion = ll;
4329 }
4330 } else {
4331 /*
4332 * Check whether a `%rep' was started and not ended
4333 * within this macro expansion. This can happen and
4334 * should be detected. It's a fatal error because
4335 * I'm too confused to work out how to recover
4336 * sensibly from it.
4337 */
4338 if (defining) {
4339 if (defining->name)
4340 error(ERR_PANIC,
4341 "defining with name in expansion");
4342 else if (istk->mstk->name)
4343 error(ERR_FATAL,
4344 "`%%rep' without `%%endrep' within"
4345 " expansion of macro `%s'",
4346 istk->mstk->name);
4347 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004348
H. Peter Anvine2c80182005-01-15 22:15:51 +00004349 /*
4350 * FIXME: investigate the relationship at this point between
4351 * istk->mstk and l->finishes
4352 */
4353 {
4354 MMacro *m = istk->mstk;
4355 istk->mstk = m->next_active;
4356 if (m->name) {
4357 /*
4358 * This was a real macro call, not a %rep, and
4359 * therefore the parameter information needs to
4360 * be freed.
4361 */
4362 nasm_free(m->params);
4363 free_tlist(m->iline);
4364 nasm_free(m->paramlen);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004365 l->finishes->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004366 } else
4367 free_mmacro(m);
4368 }
4369 istk->expansion = l->next;
4370 nasm_free(l);
4371 list->downlevel(LIST_MACRO);
4372 }
4373 }
4374 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004375
H. Peter Anvine2c80182005-01-15 22:15:51 +00004376 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004377 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004378 Line *l = istk->expansion;
4379 if (istk->mstk)
4380 istk->mstk->lineno++;
4381 tline = l->first;
4382 istk->expansion = l->next;
4383 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004384 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004385 list->line(LIST_MACRO, p);
4386 nasm_free(p);
4387 break;
4388 }
4389 line = read_line();
4390 if (line) { /* from the current input file */
4391 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004392 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004393 nasm_free(line);
4394 break;
4395 }
4396 /*
4397 * The current file has ended; work down the istk
4398 */
4399 {
4400 Include *i = istk;
4401 fclose(i->fp);
4402 if (i->conds)
4403 error(ERR_FATAL,
4404 "expected `%%endif' before end of file");
4405 /* only set line and file name if there's a next node */
4406 if (i->next) {
4407 src_set_linnum(i->lineno);
4408 nasm_free(src_set_fname(i->fname));
H. Peter Anvin86877b22008-06-20 15:55:45 -07004409 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004410 istk = i->next;
4411 list->downlevel(LIST_INCLUDE);
4412 nasm_free(i);
4413 if (!istk)
4414 return NULL;
4415 }
4416 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004417
H. Peter Anvine2c80182005-01-15 22:15:51 +00004418 /*
4419 * We must expand MMacro parameters and MMacro-local labels
4420 * _before_ we plunge into directive processing, to cope
4421 * with things like `%define something %1' such as STRUC
4422 * uses. Unless we're _defining_ a MMacro, in which case
4423 * those tokens should be left alone to go into the
4424 * definition; and unless we're in a non-emitting
4425 * condition, in which case we don't want to meddle with
4426 * anything.
4427 */
Charles Crayned4200be2008-07-12 16:42:33 -07004428 if (!defining && !(istk->conds && !emitting(istk->conds->state))
4429 && !(istk->mstk && !istk->mstk->in_progress))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004430 tline = expand_mmac_params(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004431
H. Peter Anvine2c80182005-01-15 22:15:51 +00004432 /*
4433 * Check the line to see if it's a preprocessor directive.
4434 */
4435 if (do_directive(tline) == DIRECTIVE_FOUND) {
4436 continue;
4437 } else if (defining) {
4438 /*
4439 * We're defining a multi-line macro. We emit nothing
4440 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004441 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004442 */
4443 Line *l = nasm_malloc(sizeof(Line));
4444 l->next = defining->expansion;
4445 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004446 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004447 defining->expansion = l;
4448 continue;
4449 } else if (istk->conds && !emitting(istk->conds->state)) {
4450 /*
4451 * We're in a non-emitting branch of a condition block.
4452 * Emit nothing at all, not even a blank line: when we
4453 * emerge from the condition we'll give a line-number
4454 * directive so we keep our place correctly.
4455 */
4456 free_tlist(tline);
4457 continue;
4458 } else if (istk->mstk && !istk->mstk->in_progress) {
4459 /*
4460 * We're in a %rep block which has been terminated, so
4461 * we're walking through to the %endrep without
4462 * emitting anything. Emit nothing at all, not even a
4463 * blank line: when we emerge from the %rep block we'll
4464 * give a line-number directive so we keep our place
4465 * correctly.
4466 */
4467 free_tlist(tline);
4468 continue;
4469 } else {
4470 tline = expand_smacro(tline);
4471 if (!expand_mmacro(tline)) {
4472 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004473 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004474 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004475 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004476 free_tlist(tline);
4477 break;
4478 } else {
4479 continue; /* expand_mmacro calls free_tlist */
4480 }
4481 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004482 }
4483
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004484 return line;
4485}
4486
H. Peter Anvine2c80182005-01-15 22:15:51 +00004487static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004488{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004489 if (defining) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02004490 if(defining->name) {
4491 error(ERR_NONFATAL,
4492 "end of file while still defining macro `%s'",
4493 defining->name);
4494 } else {
4495 error(ERR_NONFATAL, "end of file while still in %%rep");
4496 }
4497
H. Peter Anvine2c80182005-01-15 22:15:51 +00004498 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004499 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004500 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004501 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004502 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004503 while (istk) {
4504 Include *i = istk;
4505 istk = istk->next;
4506 fclose(i->fp);
4507 nasm_free(i->fname);
4508 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004509 }
4510 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004511 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004512 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004513 if (pass == 0) {
H. Peter Anvin86877b22008-06-20 15:55:45 -07004514 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004515 free_llist(predef);
4516 delete_Blocks();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004517 while ((i = ipath)) {
4518 ipath = i->next;
4519 if (i->path)
4520 nasm_free(i->path);
4521 nasm_free(i);
4522 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07004523 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004524}
4525
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004526void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004527{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004528 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004529
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004530 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004531 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004532 i->next = NULL;
4533
H. Peter Anvine2c80182005-01-15 22:15:51 +00004534 if (ipath != NULL) {
4535 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004536 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004537 j = j->next;
4538 j->next = i;
4539 } else {
4540 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004541 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004542}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004543
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004544void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004545{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004546 Token *inc, *space, *name;
4547 Line *l;
4548
H. Peter Anvin734b1882002-04-30 21:01:08 +00004549 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4550 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4551 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004552
4553 l = nasm_malloc(sizeof(Line));
4554 l->next = predef;
4555 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004556 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004557 predef = l;
4558}
4559
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004560void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004561{
4562 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004563 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004564 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004565
4566 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004567 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4568 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004569 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004570 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004571 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004572 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004573 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004574
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004575 l = nasm_malloc(sizeof(Line));
4576 l->next = predef;
4577 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004578 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004579 predef = l;
4580}
4581
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004582void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004583{
4584 Token *def, *space;
4585 Line *l;
4586
H. Peter Anvin734b1882002-04-30 21:01:08 +00004587 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4588 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004589 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004590
4591 l = nasm_malloc(sizeof(Line));
4592 l->next = predef;
4593 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004594 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004595 predef = l;
4596}
4597
Keith Kaniosb7a89542007-04-12 02:40:54 +00004598/*
4599 * Added by Keith Kanios:
4600 *
4601 * This function is used to assist with "runtime" preprocessor
4602 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4603 *
4604 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4605 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4606 */
4607
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004608void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004609{
4610 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004611
Keith Kaniosb7a89542007-04-12 02:40:54 +00004612 def = tokenize(definition);
4613 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4614 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004615
Keith Kaniosb7a89542007-04-12 02:40:54 +00004616}
4617
H. Peter Anvina70547f2008-07-19 21:44:26 -07004618void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004619{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004620 extrastdmac = macros;
4621}
4622
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004623static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004624{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004625 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004626 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004627 tok->text = nasm_strdup(numbuf);
4628 tok->type = TOK_NUMBER;
4629}
4630
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004631Preproc nasmpp = {
4632 pp_reset,
4633 pp_getline,
4634 pp_cleanup
4635};