blob: c6e0fb60f95925b380d44e20c93eae464fa24efc [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 Anvin9bb46df2009-04-07 21:59:24 -0700162 TOK_INDIRECT, /* %[...] */
H. Peter Anvin5b76fa22008-05-26 11:14:38 -0700163 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
164 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000165};
166
H. Peter Anvine2c80182005-01-15 22:15:51 +0000167struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000168 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000169 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700170 union {
171 SMacro *mac; /* associated macro for TOK_SMAC_END */
172 size_t len; /* scratch length field */
173 } a; /* Auxiliary data */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000174 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000175};
176
177/*
178 * Multi-line macro definitions are stored as a linked list of
179 * these, which is essentially a container to allow several linked
180 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700181 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000182 * Note that in this module, linked lists are treated as stacks
183 * wherever possible. For this reason, Lines are _pushed_ on to the
184 * `expansion' field in MMacro structures, so that the linked list,
185 * if walked, would give the macro lines in reverse order; this
186 * means that we can walk the list when expanding a macro, and thus
187 * push the lines on to the `expansion' field in _istk_ in reverse
188 * order (so that when popped back off they are in the right
189 * order). It may seem cockeyed, and it relies on my design having
190 * an even number of steps in, but it works...
191 *
192 * Some of these structures, rather than being actual lines, are
193 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000194 * This is for use in the cycle-tracking and %rep-handling code.
195 * Such structures have `finishes' non-NULL, and `first' NULL. All
196 * others have `finishes' NULL, but `first' may still be NULL if
197 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000198 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000199struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000200 Line *next;
201 MMacro *finishes;
202 Token *first;
203};
204
205/*
206 * To handle an arbitrary level of file inclusion, we maintain a
207 * stack (ie linked list) of these things.
208 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000209struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000210 Include *next;
211 FILE *fp;
212 Cond *conds;
213 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000214 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000215 int lineno, lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000216 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000217};
218
219/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000220 * Include search path. This is simply a list of strings which get
221 * prepended, in turn, to the name of an include file, in an
222 * attempt to find the file if it's not in the current directory.
223 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000224struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000225 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000226 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000227};
228
229/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000230 * Conditional assembly: we maintain a separate stack of these for
231 * each level of file inclusion. (The only reason we keep the
232 * stacks separate is to ensure that a stray `%endif' in a file
233 * included from within the true branch of a `%if' won't terminate
234 * it and cause confusion: instead, rightly, it'll cause an error.)
235 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000236struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000237 Cond *next;
238 int state;
239};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000240enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000241 /*
242 * These states are for use just after %if or %elif: IF_TRUE
243 * means the condition has evaluated to truth so we are
244 * currently emitting, whereas IF_FALSE means we are not
245 * currently emitting but will start doing so if a %else comes
246 * up. In these states, all directives are admissible: %elif,
247 * %else and %endif. (And of course %if.)
248 */
249 COND_IF_TRUE, COND_IF_FALSE,
250 /*
251 * These states come up after a %else: ELSE_TRUE means we're
252 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
253 * any %elif or %else will cause an error.
254 */
255 COND_ELSE_TRUE, COND_ELSE_FALSE,
256 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200257 * These states mean that we're not emitting now, and also that
258 * nothing until %endif will be emitted at all. COND_DONE is
259 * used when we've had our moment of emission
260 * and have now started seeing %elifs. COND_NEVER is used when
261 * the condition construct in question is contained within a
262 * non-emitting branch of a larger condition construct,
263 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000264 */
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200265 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000266};
267#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
268
H. Peter Anvin70653092007-10-19 14:42:29 -0700269/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000270 * These defines are used as the possible return values for do_directive
271 */
272#define NO_DIRECTIVE_FOUND 0
273#define DIRECTIVE_FOUND 1
274
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000275/*
276 * Condition codes. Note that we use c_ prefix not C_ because C_ is
277 * used in nasm.h for the "real" condition codes. At _this_ level,
278 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
279 * ones, so we need a different enum...
280 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700281static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000282 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
283 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000284 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000285};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700286enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000287 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
288 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 -0700289 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
290 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000291};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700292static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000293 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
294 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 +0000295 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000296};
297
H. Peter Anvin76690a12002-04-30 20:52:49 +0000298/*
299 * Directive names.
300 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000301/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000302static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000303{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000304 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000305}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000306
307/* For TASM compatibility we need to be able to recognise TASM compatible
308 * conditional compilation directives. Using the NASM pre-processor does
309 * not work, so we look for them specifically from the following list and
310 * then jam in the equivalent NASM directive into the input stream.
311 */
312
H. Peter Anvine2c80182005-01-15 22:15:51 +0000313enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000314 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
315 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
316};
317
H. Peter Anvin476d2862007-10-02 22:04:15 -0700318static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000319 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
320 "ifndef", "include", "local"
321};
322
323static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000324static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000325static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800326static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000327
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000328static Context *cstk;
329static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000330static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000331
H. Peter Anvine2c80182005-01-15 22:15:51 +0000332static efunc _error; /* Pointer to client-provided error reporting function */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000333static evalfunc evaluate;
334
H. Peter Anvine2c80182005-01-15 22:15:51 +0000335static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700336static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000337
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700338static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000339
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000340static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700341static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000342
343static ListGen *list;
344
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000345/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000346 * The current set of multi-line macros we have defined.
347 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700348static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000349
350/*
351 * The current set of single-line macros we have defined.
352 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700353static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000354
355/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000356 * The multi-line macro we are currently defining, or the %rep
357 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000358 */
359static MMacro *defining;
360
Charles Crayned4200be2008-07-12 16:42:33 -0700361static uint64_t nested_mac_count;
362static uint64_t nested_rep_count;
363
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000364/*
365 * The number of macro parameters to allocate space for at a time.
366 */
367#define PARAM_DELTA 16
368
369/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700370 * The standard macro set: defined in macros.c in the array nasm_stdmac.
371 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000372 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700373static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000374
375/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000376 * The extra standard macros that come from the object format, if
377 * any.
378 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700379static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700380static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000381
382/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000383 * Tokens are allocated in blocks to improve speed
384 */
385#define TOKEN_BLOCKSIZE 4096
386static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000387struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000388 Blocks *next;
389 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000390};
391
392static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000393
394/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000395 * Forward declarations.
396 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000397static Token *expand_mmac_params(Token * tline);
398static Token *expand_smacro(Token * tline);
399static Token *expand_id(Token * tline);
H. Peter Anvinf8ad5322009-02-21 17:55:08 -0800400static Context *get_ctx(const char *name, const char **namep,
401 bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700402static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000403static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200404static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000405static void *new_Block(size_t size);
406static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700407static Token *new_Token(Token * next, enum pp_token_type type,
408 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000409static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000410
411/*
412 * Macros for safe checking of token pointers, avoid *(NULL)
413 */
414#define tok_type_(x,t) ((x) && (x)->type == (t))
415#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
416#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
417#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000418
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000419/* Handle TASM specific directives, which do not contain a % in
420 * front of them. We do it here because I could not find any other
421 * place to do it for the moment, and it is a hack (ideally it would
422 * be nice to be able to use the NASM pre-processor to do it).
423 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000424static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000425{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000426 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000427 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000428
429 /* Skip whitespace */
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700430 while (nasm_isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000431 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000432
433 /* Binary search for the directive name */
434 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000435 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000436 len = 0;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700437 while (!nasm_isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000438 len++;
439 if (len) {
440 oldchar = p[len];
441 p[len] = 0;
442 while (j - i > 1) {
443 k = (j + i) / 2;
444 m = nasm_stricmp(p, tasm_directives[k]);
445 if (m == 0) {
446 /* We have found a directive, so jam a % in front of it
447 * so that NASM will then recognise it as one if it's own.
448 */
449 p[len] = oldchar;
450 len = strlen(p);
451 oldline = line;
452 line = nasm_malloc(len + 2);
453 line[0] = '%';
454 if (k == TM_IFDIFI) {
455 /* NASM does not recognise IFDIFI, so we convert it to
456 * %ifdef BOGUS. This is not used in NASM comaptible
457 * code, but does need to parse for the TASM macro
458 * package.
459 */
460 strcpy(line + 1, "ifdef BOGUS");
461 } else {
462 memcpy(line + 1, p, len + 1);
463 }
464 nasm_free(oldline);
465 return line;
466 } else if (m < 0) {
467 j = k;
468 } else
469 i = k;
470 }
471 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000472 }
473 return line;
474}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000475
H. Peter Anvin76690a12002-04-30 20:52:49 +0000476/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000477 * The pre-preprocessing stage... This function translates line
478 * number indications as they emerge from GNU cpp (`# lineno "file"
479 * flags') into NASM preprocessor line number indications (`%line
480 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000481 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000482static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000483{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000484 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000485 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000486
H. Peter Anvine2c80182005-01-15 22:15:51 +0000487 if (line[0] == '#' && line[1] == ' ') {
488 oldline = line;
489 fname = oldline + 2;
490 lineno = atoi(fname);
491 fname += strspn(fname, "0123456789 ");
492 if (*fname == '"')
493 fname++;
494 fnlen = strcspn(fname, "\"");
495 line = nasm_malloc(20 + fnlen);
496 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
497 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000498 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000499 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000501 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000502}
503
504/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000505 * Free a linked list of tokens.
506 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000507static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000508{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000509 while (list) {
510 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000511 }
512}
513
514/*
515 * Free a linked list of lines.
516 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000517static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000518{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000519 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000520 while (list) {
521 l = list;
522 list = list->next;
523 free_tlist(l->first);
524 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000525 }
526}
527
528/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000529 * Free an MMacro
530 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000531static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000532{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000533 nasm_free(m->name);
534 free_tlist(m->dlist);
535 nasm_free(m->defaults);
536 free_llist(m->expansion);
537 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000538}
539
540/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700541 * Free all currently defined macros, and free the hash tables
542 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700543static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700544{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700545 SMacro *s;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700546 const char *key;
547 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700548
H. Peter Anvin072771e2008-05-22 13:17:51 -0700549 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700550 nasm_free((void *)key);
551 while (s) {
552 SMacro *ns = s->next;
553 nasm_free(s->name);
554 free_tlist(s->expansion);
555 nasm_free(s);
556 s = ns;
557 }
558 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700559 hash_free(smt);
560}
561
562static void free_mmacro_table(struct hash_table *mmt)
563{
564 MMacro *m;
565 const char *key;
566 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700567
568 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700569 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700570 nasm_free((void *)key);
571 while (m) {
572 MMacro *nm = m->next;
573 free_mmacro(m);
574 m = nm;
575 }
576 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700577 hash_free(mmt);
578}
579
580static void free_macros(void)
581{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700582 free_smacro_table(&smacros);
583 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700584}
585
586/*
587 * Initialize the hash tables
588 */
589static void init_macros(void)
590{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700591 hash_init(&smacros, HASH_LARGE);
592 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700593}
594
595/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000596 * Pop the context stack.
597 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000598static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000599{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000600 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000601
602 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700603 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000604 nasm_free(c->name);
605 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000606}
607
H. Peter Anvin072771e2008-05-22 13:17:51 -0700608/*
609 * Search for a key in the hash index; adding it if necessary
610 * (in which case we initialize the data pointer to NULL.)
611 */
612static void **
613hash_findi_add(struct hash_table *hash, const char *str)
614{
615 struct hash_insert hi;
616 void **r;
617 char *strx;
618
619 r = hash_findi(hash, str, &hi);
620 if (r)
621 return r;
622
623 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
624 return hash_add(&hi, strx, NULL);
625}
626
627/*
628 * Like hash_findi, but returns the data element rather than a pointer
629 * to it. Used only when not adding a new element, hence no third
630 * argument.
631 */
632static void *
633hash_findix(struct hash_table *hash, const char *str)
634{
635 void **p;
636
637 p = hash_findi(hash, str, NULL);
638 return p ? *p : NULL;
639}
640
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000641#define BUF_DELTA 512
642/*
643 * Read a line from the top file in istk, handling multiple CR/LFs
644 * at the end of the line read, and handling spurious ^Zs. Will
645 * return lines from the standard macro set if this has not already
646 * been done.
647 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000648static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000649{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000650 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000651 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000652
H. Peter Anvine2c80182005-01-15 22:15:51 +0000653 if (stdmacpos) {
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700654 unsigned char c;
H. Peter Anvin7e50d232008-06-25 14:54:14 -0700655 const unsigned char *p = stdmacpos;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700656 char *ret, *q;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700657 size_t len = 0;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700658 while ((c = *p++)) {
659 if (c >= 0x80)
660 len += pp_directives_len[c-0x80]+1;
661 else
662 len++;
663 }
664 ret = nasm_malloc(len+1);
H. Peter Anvincda81632008-06-21 15:15:40 -0700665 q = ret;
666 while ((c = *stdmacpos++)) {
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700667 if (c >= 0x80) {
668 memcpy(q, pp_directives[c-0x80], pp_directives_len[c-0x80]);
669 q += pp_directives_len[c-0x80];
670 *q++ = ' ';
671 } else {
672 *q++ = c;
673 }
674 }
H. Peter Anvincda81632008-06-21 15:15:40 -0700675 stdmacpos = p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700676 *q = '\0';
677
H. Peter Anvind2456592008-06-19 15:04:18 -0700678 if (!*stdmacpos) {
679 /* This was the last of the standard macro chain... */
680 stdmacpos = NULL;
681 if (any_extrastdmac) {
682 stdmacpos = extrastdmac;
683 any_extrastdmac = false;
684 } else if (do_predef) {
685 Line *pd, *l;
686 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000687
H. Peter Anvind2456592008-06-19 15:04:18 -0700688 /*
689 * Nasty hack: here we push the contents of
690 * `predef' on to the top-level expansion stack,
691 * since this is the most convenient way to
692 * implement the pre-include and pre-define
693 * features.
694 */
695 for (pd = predef; pd; pd = pd->next) {
696 head = NULL;
697 tail = &head;
698 for (t = pd->first; t; t = t->next) {
699 *tail = new_Token(NULL, t->type, t->text, 0);
700 tail = &(*tail)->next;
701 }
702 l = nasm_malloc(sizeof(Line));
703 l->next = istk->expansion;
704 l->first = head;
H. Peter Anvin538002d2008-06-28 18:30:27 -0700705 l->finishes = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700706 istk->expansion = l;
707 }
708 do_predef = false;
709 }
710 }
711 return ret;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000712 }
713
714 bufsize = BUF_DELTA;
715 buffer = nasm_malloc(BUF_DELTA);
716 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000717 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000718 while (1) {
719 q = fgets(p, bufsize - (p - buffer), istk->fp);
720 if (!q)
721 break;
722 p += strlen(p);
723 if (p > buffer && p[-1] == '\n') {
724 /* Convert backslash-CRLF line continuation sequences into
725 nothing at all (for DOS and Windows) */
726 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
727 p -= 3;
728 *p = 0;
729 continued_count++;
730 }
731 /* Also convert backslash-LF line continuation sequences into
732 nothing at all (for Unix) */
733 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
734 p -= 2;
735 *p = 0;
736 continued_count++;
737 } else {
738 break;
739 }
740 }
741 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000742 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000743 bufsize += BUF_DELTA;
744 buffer = nasm_realloc(buffer, bufsize);
745 p = buffer + offset; /* prevent stale-pointer problems */
746 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000747 }
748
H. Peter Anvine2c80182005-01-15 22:15:51 +0000749 if (!q && p == buffer) {
750 nasm_free(buffer);
751 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000752 }
753
H. Peter Anvine2c80182005-01-15 22:15:51 +0000754 src_set_linnum(src_get_linnum() + istk->lineinc +
755 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000756
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000757 /*
758 * Play safe: remove CRs as well as LFs, if any of either are
759 * present at the end of the line.
760 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000761 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000762 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000763
764 /*
765 * Handle spurious ^Z, which may be inserted into source files
766 * by some file transfer utilities.
767 */
768 buffer[strcspn(buffer, "\032")] = '\0';
769
H. Peter Anvin734b1882002-04-30 21:01:08 +0000770 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000771
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000772 return buffer;
773}
774
775/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000776 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000777 * don't need to parse the value out of e.g. numeric tokens: we
778 * simply split one string into many.
779 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000780static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000781{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700782 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000783 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000784 Token *list = NULL;
785 Token *t, **tail = &list;
786
H. Peter Anvine2c80182005-01-15 22:15:51 +0000787 while (*line) {
788 p = line;
789 if (*p == '%') {
790 p++;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700791 if (nasm_isdigit(*p) ||
792 ((*p == '-' || *p == '+') && nasm_isdigit(p[1])) ||
793 ((*p == '+') && (nasm_isspace(p[1]) || !p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000794 do {
795 p++;
796 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700797 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000798 type = TOK_PREPROC_ID;
799 } else if (*p == '{') {
800 p++;
801 while (*p && *p != '}') {
802 p[-1] = *p;
803 p++;
804 }
805 p[-1] = '\0';
806 if (*p)
807 p++;
808 type = TOK_PREPROC_ID;
H. Peter Anvin992fe752008-10-19 15:45:05 -0700809 } else if (*p == '[') {
810 int lvl = 1;
811 line += 2; /* Skip the leading %[ */
812 p++;
H. Peter Anvinec033012008-10-19 22:12:06 -0700813 while (lvl && (c = *p++)) {
H. Peter Anvinca544db2008-10-19 19:30:11 -0700814 switch (c) {
815 case ']':
816 lvl--;
817 break;
818 case '%':
H. Peter Anvinca544db2008-10-19 19:30:11 -0700819 if (*p == '[')
820 lvl++;
821 break;
822 case '\'':
823 case '\"':
824 case '`':
H. Peter Anvinec033012008-10-19 22:12:06 -0700825 p = nasm_skip_string(p)+1;
H. Peter Anvinca544db2008-10-19 19:30:11 -0700826 break;
827 default:
828 break;
H. Peter Anvin992fe752008-10-19 15:45:05 -0700829 }
H. Peter Anvin992fe752008-10-19 15:45:05 -0700830 }
H. Peter Anvinec033012008-10-19 22:12:06 -0700831 p--;
H. Peter Anvin992fe752008-10-19 15:45:05 -0700832 if (*p)
833 *p++ = '\0';
H. Peter Anvine1265812008-10-19 22:14:30 -0700834 if (lvl)
835 error(ERR_NONFATAL, "unterminated %[ construct");
H. Peter Anvin992fe752008-10-19 15:45:05 -0700836 type = TOK_INDIRECT;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700837 } else if (*p == '?') {
838 type = TOK_PREPROC_Q; /* %? */
839 p++;
840 if (*p == '?') {
841 type = TOK_PREPROC_QQ; /* %?? */
842 p++;
843 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000844 } else if (isidchar(*p) ||
845 ((*p == '!' || *p == '%' || *p == '$') &&
846 isidchar(p[1]))) {
847 do {
848 p++;
849 }
850 while (isidchar(*p));
851 type = TOK_PREPROC_ID;
852 } else {
853 type = TOK_OTHER;
854 if (*p == '%')
855 p++;
856 }
857 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
858 type = TOK_ID;
859 p++;
860 while (*p && isidchar(*p))
861 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700862 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000863 /*
864 * A string token.
865 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000866 type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700867 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000868
H. Peter Anvine2c80182005-01-15 22:15:51 +0000869 if (*p) {
870 p++;
871 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -0700872 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000873 /* Handling unterminated strings by UNV */
874 /* type = -1; */
875 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200876 } else if (p[0] == '$' && p[1] == '$') {
H. Peter Anvin8e1f8112009-04-17 14:20:44 -0700877 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200878 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000879 } else if (isnumstart(*p)) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700880 bool is_hex = false;
881 bool is_float = false;
882 bool has_e = false;
883 char c, *r;
884
H. Peter Anvine2c80182005-01-15 22:15:51 +0000885 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700886 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000887 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700888
889 if (*p == '$') {
890 p++;
891 is_hex = true;
892 }
893
894 for (;;) {
895 c = *p++;
896
897 if (!is_hex && (c == 'e' || c == 'E')) {
898 has_e = true;
899 if (*p == '+' || *p == '-') {
900 /* e can only be followed by +/- if it is either a
901 prefixed hex number or a floating-point number */
902 p++;
903 is_float = true;
904 }
905 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
906 is_hex = true;
907 } else if (c == 'P' || c == 'p') {
908 is_float = true;
909 if (*p == '+' || *p == '-')
910 p++;
911 } else if (isnumchar(c) || c == '_')
912 ; /* just advance */
913 else if (c == '.') {
914 /* we need to deal with consequences of the legacy
915 parser, like "1.nolist" being two tokens
916 (TOK_NUMBER, TOK_ID) here; at least give it
917 a shot for now. In the future, we probably need
918 a flex-based scanner with proper pattern matching
919 to do it as well as it can be done. Nothing in
920 the world is going to help the person who wants
921 0x123.p16 interpreted as two tokens, though. */
922 r = p;
923 while (*r == '_')
924 r++;
925
H. Peter Anvinf221b9e2008-06-21 11:03:51 -0700926 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700927 (!is_hex && (*r == 'e' || *r == 'E')) ||
928 (*r == 'p' || *r == 'P')) {
929 p = r;
930 is_float = true;
931 } else
932 break; /* Terminate the token */
933 } else
934 break;
935 }
936 p--; /* Point to first character beyond number */
937
H. Peter Anvin8e1f8112009-04-17 14:20:44 -0700938 if (p == line+1 && *line == '$') {
939 type = TOK_OTHER; /* TOKEN_HERE */
940 } else {
941 if (has_e && !is_hex) {
942 /* 1e13 is floating-point, but 1e13h is not */
943 is_float = true;
944 }
945
946 type = is_float ? TOK_FLOAT : TOK_NUMBER;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700947 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700948 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000949 type = TOK_WHITESPACE;
950 p++;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700951 while (*p && nasm_isspace(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000952 p++;
953 /*
954 * Whitespace just before end-of-line is discarded by
955 * pretending it's a comment; whitespace just before a
956 * comment gets lumped into the comment.
957 */
958 if (!*p || *p == ';') {
959 type = TOK_COMMENT;
960 while (*p)
961 p++;
962 }
963 } else if (*p == ';') {
964 type = TOK_COMMENT;
965 while (*p)
966 p++;
967 } else {
968 /*
969 * Anything else is an operator of some kind. We check
970 * for all the double-character operators (>>, <<, //,
971 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000972 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973 */
974 type = TOK_OTHER;
975 if ((p[0] == '>' && p[1] == '>') ||
976 (p[0] == '<' && p[1] == '<') ||
977 (p[0] == '/' && p[1] == '/') ||
978 (p[0] == '<' && p[1] == '=') ||
979 (p[0] == '>' && p[1] == '=') ||
980 (p[0] == '=' && p[1] == '=') ||
981 (p[0] == '!' && p[1] == '=') ||
982 (p[0] == '<' && p[1] == '>') ||
983 (p[0] == '&' && p[1] == '&') ||
984 (p[0] == '|' && p[1] == '|') ||
985 (p[0] == '^' && p[1] == '^')) {
986 p++;
987 }
988 p++;
989 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000990
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 /* Handling unterminated string by UNV */
992 /*if (type == -1)
993 {
994 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
995 t->text[p-line] = *line;
996 tail = &t->next;
997 }
998 else */
999 if (type != TOK_COMMENT) {
1000 *tail = t = new_Token(NULL, type, line, p - line);
1001 tail = &t->next;
1002 }
1003 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001004 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001005 return list;
1006}
1007
H. Peter Anvince616072002-04-30 21:02:23 +00001008/*
1009 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001010 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001011 * deleted only all at once by the delete_Blocks function.
1012 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001014{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001015 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001017 /* first, get to the end of the linked list */
1018 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001019 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001020 /* now allocate the requested chunk */
1021 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001022
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001023 /* now allocate a new block for the next request */
1024 b->next = nasm_malloc(sizeof(Blocks));
1025 /* and initialize the contents of the new block */
1026 b->next->next = NULL;
1027 b->next->chunk = NULL;
1028 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001029}
1030
1031/*
1032 * this function deletes all managed blocks of memory
1033 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001034static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001035{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001036 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001037
H. Peter Anvin70653092007-10-19 14:42:29 -07001038 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001039 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001040 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001041 * free it.
1042 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001043 while (b) {
1044 if (b->chunk)
1045 nasm_free(b->chunk);
1046 a = b;
1047 b = b->next;
1048 if (a != &blocks)
1049 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001050 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001051}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001052
1053/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001054 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001055 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001056 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001057 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001058static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvinc751e862008-06-09 10:18:45 -07001059 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001060{
1061 Token *t;
1062 int i;
1063
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 if (freeTokens == NULL) {
1065 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1066 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1067 freeTokens[i].next = &freeTokens[i + 1];
1068 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001069 }
1070 t = freeTokens;
1071 freeTokens = t->next;
1072 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001073 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001074 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001075 if (type == TOK_WHITESPACE || text == NULL) {
1076 t->text = NULL;
1077 } else {
1078 if (txtlen == 0)
1079 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001080 t->text = nasm_malloc(txtlen+1);
1081 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001082 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001083 }
1084 return t;
1085}
1086
H. Peter Anvine2c80182005-01-15 22:15:51 +00001087static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001088{
1089 Token *next = t->next;
1090 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001091 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001092 freeTokens = t;
1093 return next;
1094}
1095
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001096/*
1097 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001098 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1099 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001100 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001101static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001102{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001103 Token *t;
1104 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001105 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001106 const char *q;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001107
1108 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001109 for (t = tlist; t; t = t->next) {
1110 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001111 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001112 nasm_free(t->text);
1113 if (p)
1114 t->text = nasm_strdup(p);
1115 else
1116 t->text = NULL;
1117 }
1118 /* Expand local macros here and not during preprocessing */
1119 if (expand_locals &&
1120 t->type == TOK_PREPROC_ID && t->text &&
1121 t->text[0] == '%' && t->text[1] == '$') {
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001122 const char *q;
1123 char *p;
1124 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001125 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001126 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001127 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128 p = nasm_strcat(buffer, q);
1129 nasm_free(t->text);
1130 t->text = p;
1131 }
1132 }
1133 if (t->type == TOK_WHITESPACE) {
1134 len++;
1135 } else if (t->text) {
1136 len += strlen(t->text);
1137 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001138 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001139 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001140 for (t = tlist; t; t = t->next) {
1141 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001142 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001143 } else if (t->text) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001144 q = t->text;
1145 while (*q)
1146 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001148 }
1149 *p = '\0';
1150 return line;
1151}
1152
1153/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001154 * A scanner, suitable for use by the expression evaluator, which
1155 * operates on a line of Tokens. Expects a pointer to a pointer to
1156 * the first token in the line to be passed in as its private_data
1157 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001158 *
1159 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001160 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001162{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001163 Token **tlineptr = private_data;
1164 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001165 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001166
H. Peter Anvine2c80182005-01-15 22:15:51 +00001167 do {
1168 tline = *tlineptr;
1169 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001170 }
1171 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001172 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001173
1174 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001175 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001176
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001177 tokval->t_charptr = tline->text;
1178
H. Peter Anvin76690a12002-04-30 20:52:49 +00001179 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001180 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001181 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001182 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001183
H. Peter Anvine2c80182005-01-15 22:15:51 +00001184 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001185 p = tokval->t_charptr = tline->text;
1186 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001187 tokval->t_charptr++;
1188 return tokval->t_type = TOKEN_ID;
1189 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001190
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001191 for (r = p, s = ourcopy; *r; r++) {
Philipp Thomas76ec8e72008-05-21 08:53:21 -07001192 if (r >= p+MAX_KEYWORD)
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001193 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001194 *s++ = nasm_tolower(*r);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001195 }
1196 *s = '\0';
1197 /* right, so we have an identifier sitting in temp storage. now,
1198 * is it actually a register or instruction name, or what? */
1199 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001200 }
1201
H. Peter Anvine2c80182005-01-15 22:15:51 +00001202 if (tline->type == TOK_NUMBER) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001203 bool rn_error;
1204 tokval->t_integer = readnum(tline->text, &rn_error);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001205 tokval->t_charptr = tline->text;
H. Peter Anvin11627042008-06-09 20:45:19 -07001206 if (rn_error)
1207 return tokval->t_type = TOKEN_ERRNUM;
1208 else
1209 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001210 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001211
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001212 if (tline->type == TOK_FLOAT) {
1213 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001214 }
1215
H. Peter Anvine2c80182005-01-15 22:15:51 +00001216 if (tline->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001217 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001218
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001219 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001220 tokval->t_charptr = tline->text;
1221 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001222
H. Peter Anvin11627042008-06-09 20:45:19 -07001223 if (ep[0] != bq || ep[1] != '\0')
1224 return tokval->t_type = TOKEN_ERRSTR;
1225 else
1226 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001227 }
1228
H. Peter Anvine2c80182005-01-15 22:15:51 +00001229 if (tline->type == TOK_OTHER) {
1230 if (!strcmp(tline->text, "<<"))
1231 return tokval->t_type = TOKEN_SHL;
1232 if (!strcmp(tline->text, ">>"))
1233 return tokval->t_type = TOKEN_SHR;
1234 if (!strcmp(tline->text, "//"))
1235 return tokval->t_type = TOKEN_SDIV;
1236 if (!strcmp(tline->text, "%%"))
1237 return tokval->t_type = TOKEN_SMOD;
1238 if (!strcmp(tline->text, "=="))
1239 return tokval->t_type = TOKEN_EQ;
1240 if (!strcmp(tline->text, "<>"))
1241 return tokval->t_type = TOKEN_NE;
1242 if (!strcmp(tline->text, "!="))
1243 return tokval->t_type = TOKEN_NE;
1244 if (!strcmp(tline->text, "<="))
1245 return tokval->t_type = TOKEN_LE;
1246 if (!strcmp(tline->text, ">="))
1247 return tokval->t_type = TOKEN_GE;
1248 if (!strcmp(tline->text, "&&"))
1249 return tokval->t_type = TOKEN_DBL_AND;
1250 if (!strcmp(tline->text, "^^"))
1251 return tokval->t_type = TOKEN_DBL_XOR;
1252 if (!strcmp(tline->text, "||"))
1253 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001254 }
1255
1256 /*
1257 * We have no other options: just return the first character of
1258 * the token text.
1259 */
1260 return tokval->t_type = tline->text[0];
1261}
1262
1263/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001264 * Compare a string to the name of an existing macro; this is a
1265 * simple wrapper which calls either strcmp or nasm_stricmp
1266 * depending on the value of the `casesense' parameter.
1267 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001268static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001269{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001270 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001271}
1272
1273/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001274 * Compare a string to the name of an existing macro; this is a
1275 * simple wrapper which calls either strcmp or nasm_stricmp
1276 * depending on the value of the `casesense' parameter.
1277 */
1278static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1279{
1280 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1281}
1282
1283/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001284 * Return the Context structure associated with a %$ token. Return
1285 * NULL, having _already_ reported an error condition, if the
1286 * context stack isn't deep enough for the supplied number of $
1287 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001288 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001289 * also scanned for such smacro, until it is found; if not -
1290 * only the context that directly results from the number of $'s
1291 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001292 *
1293 * If "namep" is non-NULL, set it to the pointer to the macro name
1294 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001295 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001296static Context *get_ctx(const char *name, const char **namep,
1297 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001298{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001299 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001300 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001301 int i;
1302
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001303 if (namep)
1304 *namep = name;
1305
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001306 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001307 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001308
H. Peter Anvine2c80182005-01-15 22:15:51 +00001309 if (!cstk) {
1310 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1311 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001312 }
1313
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001314 name += 2;
1315 ctx = cstk;
1316 i = 0;
1317 while (ctx && *name == '$') {
1318 name++;
1319 i++;
1320 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001321 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001322 if (!ctx) {
1323 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001324 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001326 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001327
1328 if (namep)
1329 *namep = name;
1330
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001331 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001332 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001333
H. Peter Anvine2c80182005-01-15 22:15:51 +00001334 do {
1335 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001336 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001337 while (m) {
1338 if (!mstrcmp(m->name, name, m->casesense))
1339 return ctx;
1340 m = m->next;
1341 }
1342 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001343 }
1344 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001345 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001346}
1347
1348/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001349 * Check to see if a file is already in a string list
1350 */
1351static bool in_list(const StrList *list, const char *str)
1352{
1353 while (list) {
1354 if (!strcmp(list->str, str))
1355 return true;
1356 list = list->next;
1357 }
1358 return false;
1359}
1360
1361/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001362 * Open an include file. This routine must always return a valid
1363 * file pointer if it returns - it's responsible for throwing an
1364 * ERR_FATAL and bombing out completely if not. It should also try
1365 * the include path one by one until it finds the file or reaches
1366 * the end of the path.
1367 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001368static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
H. Peter Anvin418ca702008-05-30 10:42:30 -07001369 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001370{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001371 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001372 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001373 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001374 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001375 size_t prefix_len = 0;
1376 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001377
H. Peter Anvine2c80182005-01-15 22:15:51 +00001378 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001379 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1380 memcpy(sl->str, prefix, prefix_len);
1381 memcpy(sl->str+prefix_len, file, len+1);
1382 fp = fopen(sl->str, "r");
H. Peter Anvin418ca702008-05-30 10:42:30 -07001383 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001384 sl->next = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001385 **dtail = sl;
1386 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001387 } else {
1388 nasm_free(sl);
1389 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001390 if (fp)
1391 return fp;
H. Peter Anvin418ca702008-05-30 10:42:30 -07001392 if (!ip) {
1393 if (!missing_ok)
1394 break;
1395 prefix = NULL;
1396 } else {
1397 prefix = ip->path;
1398 ip = ip->next;
1399 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001400 if (prefix) {
1401 prefix_len = strlen(prefix);
1402 } else {
1403 /* -MG given and file not found */
H. Peter Anvin418ca702008-05-30 10:42:30 -07001404 if (dhead && !in_list(*dhead, file)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001405 sl = nasm_malloc(len+1+sizeof sl->next);
1406 sl->next = NULL;
1407 strcpy(sl->str, file);
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001408 **dtail = sl;
1409 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001410 }
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001411 return NULL;
1412 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001413 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001414
H. Peter Anvin734b1882002-04-30 21:01:08 +00001415 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001416 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001417}
1418
1419/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001420 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001421 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001422 * return true if _any_ single-line macro of that name is defined.
1423 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001424 * `nparam' or no parameters is defined.
1425 *
1426 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001427 * defined, or nparam is -1, the address of the definition structure
1428 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001429 * is NULL, no action will be taken regarding its contents, and no
1430 * error will occur.
1431 *
1432 * Note that this is also called with nparam zero to resolve
1433 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001434 *
1435 * If you already know which context macro belongs to, you can pass
1436 * the context pointer as first parameter; if you won't but name begins
1437 * with %$ the context will be automatically computed. If all_contexts
1438 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001439 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001440static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001441smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001442 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001443{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001444 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001445 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001446
H. Peter Anvin97a23472007-09-16 17:57:25 -07001447 if (ctx) {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001448 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001449 } else if (name[0] == '%' && name[1] == '$') {
1450 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001451 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001452 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001453 return false; /* got to return _something_ */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001454 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001455 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001456 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001457 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001458 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001459
H. Peter Anvine2c80182005-01-15 22:15:51 +00001460 while (m) {
1461 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001462 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001463 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001464 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001465 *defn = m;
1466 else
1467 *defn = NULL;
1468 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001469 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001470 }
1471 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001472 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001473
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001474 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001475}
1476
1477/*
1478 * Count and mark off the parameters in a multi-line macro call.
1479 * This is called both from within the multi-line macro expansion
1480 * code, and also to mark off the default parameters when provided
1481 * in a %macro definition line.
1482 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001483static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001484{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001485 int paramsize, brace;
1486
1487 *nparam = paramsize = 0;
1488 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001489 while (t) {
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001490 /* +1: we need space for the final NULL */
1491 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001492 paramsize += PARAM_DELTA;
1493 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1494 }
1495 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001496 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001497 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001498 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001499 (*params)[(*nparam)++] = t;
1500 while (tok_isnt_(t, brace ? "}" : ","))
1501 t = t->next;
1502 if (t) { /* got a comma/brace */
1503 t = t->next;
1504 if (brace) {
1505 /*
1506 * Now we've found the closing brace, look further
1507 * for the comma.
1508 */
1509 skip_white_(t);
1510 if (tok_isnt_(t, ",")) {
1511 error(ERR_NONFATAL,
1512 "braces do not enclose all of macro parameter");
1513 while (tok_isnt_(t, ","))
1514 t = t->next;
1515 }
1516 if (t)
1517 t = t->next; /* eat the comma */
1518 }
1519 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001520 }
1521}
1522
1523/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001524 * Determine whether one of the various `if' conditions is true or
1525 * not.
1526 *
1527 * We must free the tline we get passed.
1528 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001529static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001530{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001531 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001532 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001533 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001534 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001535 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001536 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001537
1538 origline = tline;
1539
H. Peter Anvine2c80182005-01-15 22:15:51 +00001540 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001541 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001542 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001543 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001544 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001545 if (!tline)
1546 break;
1547 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001548 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001549 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001550 free_tlist(origline);
1551 return -1;
1552 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001553 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001554 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001555 tline = tline->next;
1556 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001557 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001558
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001559 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001560 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001561 while (tline) {
1562 skip_white_(tline);
1563 if (!tline || (tline->type != TOK_ID &&
1564 (tline->type != TOK_PREPROC_ID ||
1565 tline->text[1] != '$'))) {
1566 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001567 "`%s' expects macro identifiers", pp_directives[ct]);
1568 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001569 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001570 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001571 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001572 tline = tline->next;
1573 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001574 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001575
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001576 case PPC_IFIDN:
1577 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001578 tline = expand_smacro(tline);
1579 t = tt = tline;
1580 while (tok_isnt_(tt, ","))
1581 tt = tt->next;
1582 if (!tt) {
1583 error(ERR_NONFATAL,
1584 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001585 pp_directives[ct]);
1586 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 }
1588 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001589 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001590 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1591 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1592 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001593 pp_directives[ct]);
1594 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001595 }
1596 if (t->type == TOK_WHITESPACE) {
1597 t = t->next;
1598 continue;
1599 }
1600 if (tt->type == TOK_WHITESPACE) {
1601 tt = tt->next;
1602 continue;
1603 }
1604 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001605 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001606 break;
1607 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001608 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001609 if (t->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001610 size_t l1 = nasm_unquote(t->text, NULL);
1611 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001612
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001613 if (l1 != l2) {
1614 j = false;
1615 break;
1616 }
1617 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1618 j = false;
1619 break;
1620 }
1621 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001622 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001623 break;
1624 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001625
H. Peter Anvine2c80182005-01-15 22:15:51 +00001626 t = t->next;
1627 tt = tt->next;
1628 }
1629 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001630 j = false; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001631 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001632
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001633 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001634 {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001635 bool found = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001636 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001637
H. Peter Anvine2c80182005-01-15 22:15:51 +00001638 skip_white_(tline);
1639 tline = expand_id(tline);
1640 if (!tok_type_(tline, TOK_ID)) {
1641 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001642 "`%s' expects a macro name", pp_directives[ct]);
1643 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001644 }
1645 searching.name = nasm_strdup(tline->text);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001646 searching.casesense = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001647 searching.plus = false;
1648 searching.nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001649 searching.in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001650 searching.rep_nest = NULL;
1651 searching.nparam_min = 0;
1652 searching.nparam_max = INT_MAX;
1653 tline = expand_smacro(tline->next);
1654 skip_white_(tline);
1655 if (!tline) {
1656 } else if (!tok_type_(tline, TOK_NUMBER)) {
1657 error(ERR_NONFATAL,
1658 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001659 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001660 } else {
1661 searching.nparam_min = searching.nparam_max =
1662 readnum(tline->text, &j);
1663 if (j)
1664 error(ERR_NONFATAL,
1665 "unable to parse parameter count `%s'",
1666 tline->text);
1667 }
1668 if (tline && tok_is_(tline->next, "-")) {
1669 tline = tline->next->next;
1670 if (tok_is_(tline, "*"))
1671 searching.nparam_max = INT_MAX;
1672 else if (!tok_type_(tline, TOK_NUMBER))
1673 error(ERR_NONFATAL,
1674 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001675 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676 else {
1677 searching.nparam_max = readnum(tline->text, &j);
1678 if (j)
1679 error(ERR_NONFATAL,
1680 "unable to parse parameter count `%s'",
1681 tline->text);
1682 if (searching.nparam_min > searching.nparam_max)
1683 error(ERR_NONFATAL,
1684 "minimum parameter count exceeds maximum");
1685 }
1686 }
1687 if (tline && tok_is_(tline->next, "+")) {
1688 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001689 searching.plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001691 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001692 while (mmac) {
1693 if (!strcmp(mmac->name, searching.name) &&
1694 (mmac->nparam_min <= searching.nparam_max
1695 || searching.plus)
1696 && (searching.nparam_min <= mmac->nparam_max
1697 || mmac->plus)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001698 found = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001699 break;
1700 }
1701 mmac = mmac->next;
1702 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001703 if(tline && tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001704 error(ERR_WARNING|ERR_PASS1,
1705 "trailing garbage after %%ifmacro ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001706 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001707 j = found;
1708 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001709 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001710
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001711 case PPC_IFID:
1712 needtype = TOK_ID;
1713 goto iftype;
1714 case PPC_IFNUM:
1715 needtype = TOK_NUMBER;
1716 goto iftype;
1717 case PPC_IFSTR:
1718 needtype = TOK_STRING;
1719 goto iftype;
1720
1721 iftype:
H. Peter Anvin134b9462008-02-16 17:01:40 -08001722 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001723
H. Peter Anvin927c92b2008-02-16 13:44:52 -08001724 while (tok_type_(t, TOK_WHITESPACE) ||
1725 (needtype == TOK_NUMBER &&
1726 tok_type_(t, TOK_OTHER) &&
1727 (t->text[0] == '-' || t->text[0] == '+') &&
1728 !t->text[1]))
1729 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001730
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001731 j = tok_type_(t, needtype);
1732 break;
1733
1734 case PPC_IFTOKEN:
1735 t = tline = expand_smacro(tline);
1736 while (tok_type_(t, TOK_WHITESPACE))
1737 t = t->next;
1738
1739 j = false;
1740 if (t) {
1741 t = t->next; /* Skip the actual token */
1742 while (tok_type_(t, TOK_WHITESPACE))
1743 t = t->next;
1744 j = !t; /* Should be nothing left */
1745 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001746 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001747
H. Peter Anvin134b9462008-02-16 17:01:40 -08001748 case PPC_IFEMPTY:
1749 t = tline = expand_smacro(tline);
1750 while (tok_type_(t, TOK_WHITESPACE))
1751 t = t->next;
1752
1753 j = !t; /* Should be empty */
1754 break;
1755
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001756 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001757 t = tline = expand_smacro(tline);
1758 tptr = &t;
1759 tokval.t_type = TOKEN_INVALID;
1760 evalresult = evaluate(ppscan, tptr, &tokval,
1761 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001762 if (!evalresult)
1763 return -1;
1764 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001765 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001766 "trailing garbage after expression ignored");
1767 if (!is_simple(evalresult)) {
1768 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001769 "non-constant value given to `%s'", pp_directives[ct]);
1770 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001771 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001772 j = reloc_value(evalresult) != 0;
H. Peter Anvin90e6e812008-07-16 14:38:24 -07001773 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001774
H. Peter Anvine2c80182005-01-15 22:15:51 +00001775 default:
1776 error(ERR_FATAL,
1777 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001778 pp_directives[ct]);
1779 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001780 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001781
1782 free_tlist(origline);
1783 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001784
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001785fail:
1786 free_tlist(origline);
1787 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001788}
1789
1790/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001791 * Common code for defining an smacro
1792 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001793static bool define_smacro(Context *ctx, const char *mname, bool casesense,
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001794 int nparam, Token *expansion)
1795{
1796 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001797 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001798
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001799 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1800 if (!smac) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001801 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001802 "single-line macro `%s' defined both with and"
1803 " without parameters", mname);
1804
1805 /* Some instances of the old code considered this a failure,
1806 some others didn't. What is the right thing to do here? */
1807 free_tlist(expansion);
1808 return false; /* Failure */
1809 } else {
1810 /*
1811 * We're redefining, so we have to take over an
1812 * existing SMacro structure. This means freeing
1813 * what was already in it.
1814 */
1815 nasm_free(smac->name);
1816 free_tlist(smac->expansion);
1817 }
1818 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001819 smtbl = ctx ? &ctx->localmac : &smacros;
1820 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001821 smac = nasm_malloc(sizeof(SMacro));
1822 smac->next = *smhead;
1823 *smhead = smac;
1824 }
1825 smac->name = nasm_strdup(mname);
1826 smac->casesense = casesense;
1827 smac->nparam = nparam;
1828 smac->expansion = expansion;
1829 smac->in_progress = false;
1830 return true; /* Success */
1831}
1832
1833/*
1834 * Undefine an smacro
1835 */
1836static void undef_smacro(Context *ctx, const char *mname)
1837{
1838 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001839 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001840
H. Peter Anvin166c2472008-05-28 12:28:58 -07001841 smtbl = ctx ? &ctx->localmac : &smacros;
1842 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001843
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001844 if (smhead) {
1845 /*
1846 * We now have a macro name... go hunt for it.
1847 */
1848 sp = smhead;
1849 while ((s = *sp) != NULL) {
1850 if (!mstrcmp(s->name, mname, s->casesense)) {
1851 *sp = s->next;
1852 nasm_free(s->name);
1853 free_tlist(s->expansion);
1854 nasm_free(s);
1855 } else {
1856 sp = &s->next;
1857 }
1858 }
1859 }
1860}
1861
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001862/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07001863 * Parse a mmacro specification.
1864 */
1865static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1866{
1867 bool err;
1868
1869 tline = tline->next;
1870 skip_white_(tline);
1871 tline = expand_id(tline);
1872 if (!tok_type_(tline, TOK_ID)) {
1873 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1874 return false;
1875 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001876
H. Peter Anvina26433d2008-07-16 14:40:01 -07001877 def->name = nasm_strdup(tline->text);
1878 def->plus = false;
1879 def->nolist = false;
1880 def->in_progress = 0;
1881 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001882 def->nparam_min = 0;
1883 def->nparam_max = 0;
1884
H. Peter Anvina26433d2008-07-16 14:40:01 -07001885 tline = expand_smacro(tline->next);
1886 skip_white_(tline);
1887 if (!tok_type_(tline, TOK_NUMBER)) {
1888 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001889 } else {
1890 def->nparam_min = def->nparam_max =
1891 readnum(tline->text, &err);
1892 if (err)
1893 error(ERR_NONFATAL,
1894 "unable to parse parameter count `%s'", tline->text);
1895 }
1896 if (tline && tok_is_(tline->next, "-")) {
1897 tline = tline->next->next;
1898 if (tok_is_(tline, "*")) {
1899 def->nparam_max = INT_MAX;
1900 } else if (!tok_type_(tline, TOK_NUMBER)) {
1901 error(ERR_NONFATAL,
1902 "`%s' expects a parameter count after `-'", directive);
1903 } else {
1904 def->nparam_max = readnum(tline->text, &err);
1905 if (err) {
1906 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
1907 tline->text);
1908 }
1909 if (def->nparam_min > def->nparam_max) {
1910 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
1911 }
1912 }
1913 }
1914 if (tline && tok_is_(tline->next, "+")) {
1915 tline = tline->next;
1916 def->plus = true;
1917 }
1918 if (tline && tok_type_(tline->next, TOK_ID) &&
1919 !nasm_stricmp(tline->next->text, ".nolist")) {
1920 tline = tline->next;
1921 def->nolist = true;
1922 }
1923
1924 /*
1925 * Handle default parameters.
1926 */
1927 if (tline && tline->next) {
1928 def->dlist = tline->next;
1929 tline->next = NULL;
1930 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
1931 } else {
1932 def->dlist = NULL;
1933 def->defaults = NULL;
1934 }
1935 def->expansion = NULL;
1936
Victor van den Elzen22343c22008-08-06 14:47:54 +02001937 if(def->defaults &&
1938 def->ndefs > def->nparam_max - def->nparam_min &&
1939 !def->plus)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001940 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
1941 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001942
H. Peter Anvina26433d2008-07-16 14:40:01 -07001943 return true;
1944}
1945
1946
1947/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001948 * Decode a size directive
1949 */
1950static int parse_size(const char *str) {
1951 static const char *size_names[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001952 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001953 static const int sizes[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001954 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001955
1956 return sizes[bsii(str, size_names, elements(size_names))+1];
1957}
1958
Ed Beroset3ab3f412002-06-11 03:31:49 +00001959/**
1960 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001961 * Find out if a line contains a preprocessor directive, and deal
1962 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07001963 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001964 * If a directive _is_ found, it is the responsibility of this routine
1965 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001966 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001967 * @param tline a pointer to the current tokeninzed line linked list
1968 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07001969 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001970 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001971static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001972{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001973 enum preproc_token i;
1974 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07001975 bool err;
1976 int nparam;
1977 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001978 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001979 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001980 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001981 char *p, *pp;
1982 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001983 Include *inc;
1984 Context *ctx;
1985 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001986 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001987 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1988 Line *l;
1989 struct tokenval tokval;
1990 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001991 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001992 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001993 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07001994 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001995
1996 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001997
H. Peter Anvineba20a72002-04-30 20:53:55 +00001998 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07001999 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002000 (tline->text[1] == '%' || tline->text[1] == '$'
2001 || tline->text[1] == '!'))
2002 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002003
H. Peter Anvin4169a472007-09-12 01:29:43 +00002004 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002005
2006 /*
2007 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002008 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002009 * we should ignore all directives except for condition
2010 * directives.
2011 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002012 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002013 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2014 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002015 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002016
2017 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002018 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002019 * ignore all directives except for %macro/%imacro (which nest),
2020 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2021 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002022 */
2023 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00002024 i != PP_ENDMACRO && i != PP_ENDM &&
2025 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2026 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002027 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002028
Charles Crayned4200be2008-07-12 16:42:33 -07002029 if (defining) {
2030 if (i == PP_MACRO || i == PP_IMACRO) {
2031 nested_mac_count++;
2032 return NO_DIRECTIVE_FOUND;
2033 } else if (nested_mac_count > 0) {
2034 if (i == PP_ENDMACRO) {
2035 nested_mac_count--;
2036 return NO_DIRECTIVE_FOUND;
2037 }
2038 }
2039 if (!defining->name) {
2040 if (i == PP_REP) {
2041 nested_rep_count++;
2042 return NO_DIRECTIVE_FOUND;
2043 } else if (nested_rep_count > 0) {
2044 if (i == PP_ENDREP) {
2045 nested_rep_count--;
2046 return NO_DIRECTIVE_FOUND;
2047 }
2048 }
2049 }
2050 }
2051
H. Peter Anvin4169a472007-09-12 01:29:43 +00002052 switch (i) {
2053 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002054 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2055 tline->text);
2056 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002057
H. Peter Anvine2c80182005-01-15 22:15:51 +00002058 case PP_STACKSIZE:
2059 /* Directive to tell NASM what the default stack size is. The
2060 * default is for a 16-bit stack, and this can be overriden with
2061 * %stacksize large.
2062 * the following form:
2063 *
2064 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2065 */
2066 tline = tline->next;
2067 if (tline && tline->type == TOK_WHITESPACE)
2068 tline = tline->next;
2069 if (!tline || tline->type != TOK_ID) {
2070 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2071 free_tlist(origline);
2072 return DIRECTIVE_FOUND;
2073 }
2074 if (nasm_stricmp(tline->text, "flat") == 0) {
2075 /* All subsequent ARG directives are for a 32-bit stack */
2076 StackSize = 4;
2077 StackPointer = "ebp";
2078 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002079 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002080 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2081 /* All subsequent ARG directives are for a 64-bit stack */
2082 StackSize = 8;
2083 StackPointer = "rbp";
2084 ArgOffset = 8;
2085 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002086 } else if (nasm_stricmp(tline->text, "large") == 0) {
2087 /* All subsequent ARG directives are for a 16-bit stack,
2088 * far function call.
2089 */
2090 StackSize = 2;
2091 StackPointer = "bp";
2092 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002093 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002094 } else if (nasm_stricmp(tline->text, "small") == 0) {
2095 /* All subsequent ARG directives are for a 16-bit stack,
2096 * far function call. We don't support near functions.
2097 */
2098 StackSize = 2;
2099 StackPointer = "bp";
2100 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002101 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002102 } else {
2103 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2104 free_tlist(origline);
2105 return DIRECTIVE_FOUND;
2106 }
2107 free_tlist(origline);
2108 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002109
H. Peter Anvine2c80182005-01-15 22:15:51 +00002110 case PP_ARG:
2111 /* TASM like ARG directive to define arguments to functions, in
2112 * the following form:
2113 *
2114 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2115 */
2116 offset = ArgOffset;
2117 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002118 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002119 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002120
H. Peter Anvine2c80182005-01-15 22:15:51 +00002121 /* Find the argument name */
2122 tline = tline->next;
2123 if (tline && tline->type == TOK_WHITESPACE)
2124 tline = tline->next;
2125 if (!tline || tline->type != TOK_ID) {
2126 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2127 free_tlist(origline);
2128 return DIRECTIVE_FOUND;
2129 }
2130 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002131
H. Peter Anvine2c80182005-01-15 22:15:51 +00002132 /* Find the argument size type */
2133 tline = tline->next;
2134 if (!tline || tline->type != TOK_OTHER
2135 || tline->text[0] != ':') {
2136 error(ERR_NONFATAL,
2137 "Syntax error processing `%%arg' directive");
2138 free_tlist(origline);
2139 return DIRECTIVE_FOUND;
2140 }
2141 tline = tline->next;
2142 if (!tline || tline->type != TOK_ID) {
2143 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2144 free_tlist(origline);
2145 return DIRECTIVE_FOUND;
2146 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002147
H. Peter Anvine2c80182005-01-15 22:15:51 +00002148 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002149 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002150 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002151 size = parse_size(tt->text);
2152 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002153 error(ERR_NONFATAL,
2154 "Invalid size type for `%%arg' missing directive");
2155 free_tlist(tt);
2156 free_tlist(origline);
2157 return DIRECTIVE_FOUND;
2158 }
2159 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002160
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002161 /* Round up to even stack slots */
2162 size = (size+StackSize-1) & ~(StackSize-1);
2163
H. Peter Anvine2c80182005-01-15 22:15:51 +00002164 /* Now define the macro for the argument */
2165 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2166 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002167 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002168 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002169
H. Peter Anvine2c80182005-01-15 22:15:51 +00002170 /* Move to the next argument in the list */
2171 tline = tline->next;
2172 if (tline && tline->type == TOK_WHITESPACE)
2173 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002174 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2175 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002176 free_tlist(origline);
2177 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002178
H. Peter Anvine2c80182005-01-15 22:15:51 +00002179 case PP_LOCAL:
2180 /* TASM like LOCAL directive to define local variables for a
2181 * function, in the following form:
2182 *
2183 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2184 *
2185 * The '= LocalSize' at the end is ignored by NASM, but is
2186 * required by TASM to define the local parameter size (and used
2187 * by the TASM macro package).
2188 */
2189 offset = LocalOffset;
2190 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002191 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002192 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002193
H. Peter Anvine2c80182005-01-15 22:15:51 +00002194 /* Find the argument name */
2195 tline = tline->next;
2196 if (tline && tline->type == TOK_WHITESPACE)
2197 tline = tline->next;
2198 if (!tline || tline->type != TOK_ID) {
2199 error(ERR_NONFATAL,
2200 "`%%local' missing argument parameter");
2201 free_tlist(origline);
2202 return DIRECTIVE_FOUND;
2203 }
2204 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002205
H. Peter Anvine2c80182005-01-15 22:15:51 +00002206 /* Find the argument size type */
2207 tline = tline->next;
2208 if (!tline || tline->type != TOK_OTHER
2209 || tline->text[0] != ':') {
2210 error(ERR_NONFATAL,
2211 "Syntax error processing `%%local' directive");
2212 free_tlist(origline);
2213 return DIRECTIVE_FOUND;
2214 }
2215 tline = tline->next;
2216 if (!tline || tline->type != TOK_ID) {
2217 error(ERR_NONFATAL,
2218 "`%%local' missing size type parameter");
2219 free_tlist(origline);
2220 return DIRECTIVE_FOUND;
2221 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002222
H. Peter Anvine2c80182005-01-15 22:15:51 +00002223 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002224 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002225 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002226 size = parse_size(tt->text);
2227 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002228 error(ERR_NONFATAL,
2229 "Invalid size type for `%%local' missing directive");
2230 free_tlist(tt);
2231 free_tlist(origline);
2232 return DIRECTIVE_FOUND;
2233 }
2234 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002235
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002236 /* Round up to even stack slots */
2237 size = (size+StackSize-1) & ~(StackSize-1);
2238
2239 offset += size; /* Negative offset, increment before */
2240
2241 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002242 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2243 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002244 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002245
H. Peter Anvine2c80182005-01-15 22:15:51 +00002246 /* Now define the assign to setup the enter_c macro correctly */
2247 snprintf(directive, sizeof(directive),
2248 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002249 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002250
H. Peter Anvine2c80182005-01-15 22:15:51 +00002251 /* Move to the next argument in the list */
2252 tline = tline->next;
2253 if (tline && tline->type == TOK_WHITESPACE)
2254 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002255 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2256 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002257 free_tlist(origline);
2258 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002259
H. Peter Anvine2c80182005-01-15 22:15:51 +00002260 case PP_CLEAR:
2261 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002262 error(ERR_WARNING|ERR_PASS1,
2263 "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002264 free_macros();
2265 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002266 free_tlist(origline);
2267 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002268
H. Peter Anvin418ca702008-05-30 10:42:30 -07002269 case PP_DEPEND:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002270 t = tline->next = expand_smacro(tline->next);
2271 skip_white_(t);
2272 if (!t || (t->type != TOK_STRING &&
2273 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002274 error(ERR_NONFATAL, "`%%depend' expects a file name");
2275 free_tlist(origline);
2276 return DIRECTIVE_FOUND; /* but we did _something_ */
2277 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002278 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002279 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002280 "trailing garbage after `%%depend' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002281 p = t->text;
2282 if (t->type != TOK_INTERNAL_STRING)
2283 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002284 if (dephead && !in_list(*dephead, p)) {
2285 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2286 sl->next = NULL;
2287 strcpy(sl->str, p);
2288 *deptail = sl;
2289 deptail = &sl->next;
2290 }
2291 free_tlist(origline);
2292 return DIRECTIVE_FOUND;
2293
2294 case PP_INCLUDE:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002295 t = tline->next = expand_smacro(tline->next);
2296 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002297
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002298 if (!t || (t->type != TOK_STRING &&
2299 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002300 error(ERR_NONFATAL, "`%%include' expects a file name");
2301 free_tlist(origline);
2302 return DIRECTIVE_FOUND; /* but we did _something_ */
2303 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002304 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002305 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002306 "trailing garbage after `%%include' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002307 p = t->text;
2308 if (t->type != TOK_INTERNAL_STRING)
2309 nasm_unquote(p, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002310 inc = nasm_malloc(sizeof(Include));
2311 inc->next = istk;
2312 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002313 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002314 if (!inc->fp) {
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002315 /* -MG given but file not found */
2316 nasm_free(inc);
2317 } else {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002318 inc->fname = src_set_fname(nasm_strdup(p));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002319 inc->lineno = src_set_linnum(0);
2320 inc->lineinc = 1;
2321 inc->expansion = NULL;
2322 inc->mstk = NULL;
2323 istk = inc;
2324 list->uplevel(LIST_INCLUDE);
2325 }
2326 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002327 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002328
H. Peter Anvind2456592008-06-19 15:04:18 -07002329 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002330 {
H. Peter Anvina70547f2008-07-19 21:44:26 -07002331 static macros_t *use_pkg;
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002332 const char *pkg_macro;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002333
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002334 tline = tline->next;
2335 skip_white_(tline);
2336 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002337
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002338 if (!tline || (tline->type != TOK_STRING &&
2339 tline->type != TOK_INTERNAL_STRING &&
2340 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002341 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002342 free_tlist(origline);
2343 return DIRECTIVE_FOUND; /* but we did _something_ */
2344 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002345 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002346 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002347 "trailing garbage after `%%use' ignored");
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002348 if (tline->type == TOK_STRING)
2349 nasm_unquote(tline->text, NULL);
2350 use_pkg = nasm_stdmac_find_package(tline->text);
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002351 if (!use_pkg)
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002352 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002353 /* The first string will be <%define>__USE_*__ */
H. Peter Anvin7e50d232008-06-25 14:54:14 -07002354 pkg_macro = (char *)use_pkg + 1;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002355 if (!smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2356 /* Not already included, go ahead and include it */
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002357 stdmacpos = use_pkg;
2358 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002359 free_tlist(origline);
2360 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002361 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002362 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002363 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002364 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002365 tline = tline->next;
2366 skip_white_(tline);
2367 tline = expand_id(tline);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002368 if (tline) {
2369 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin42b56392008-10-24 16:24:21 -07002370 error(ERR_NONFATAL, "`%s' expects a context identifier",
2371 pp_directives[i]);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002372 free_tlist(origline);
2373 return DIRECTIVE_FOUND; /* but we did _something_ */
2374 }
2375 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002376 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin42b56392008-10-24 16:24:21 -07002377 "trailing garbage after `%s' ignored",
2378 pp_directives[i]);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002379 p = nasm_strdup(tline->text);
2380 } else {
H. Peter Anvin42b56392008-10-24 16:24:21 -07002381 p = NULL; /* Anonymous */
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002382 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002383
2384 if (i == PP_PUSH) {
2385 ctx = nasm_malloc(sizeof(Context));
2386 ctx->next = cstk;
2387 hash_init(&ctx->localmac, HASH_SMALL);
2388 ctx->name = p;
2389 ctx->number = unique++;
2390 cstk = ctx;
2391 } else {
2392 /* %pop or %repl */
2393 if (!cstk) {
2394 error(ERR_NONFATAL, "`%s': context stack is empty",
2395 pp_directives[i]);
2396 } else if (i == PP_POP) {
2397 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2398 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2399 "expected %s",
2400 cstk->name ? cstk->name : "anonymous", p);
2401 else
2402 ctx_pop();
2403 } else {
2404 /* i == PP_REPL */
2405 nasm_free(cstk->name);
2406 cstk->name = p;
2407 p = NULL;
2408 }
2409 nasm_free(p);
2410 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002411 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002412 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002413 case PP_FATAL:
2414 severity = ERR_FATAL|ERR_NO_SEVERITY;
2415 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002416 case PP_ERROR:
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002417 severity = ERR_NONFATAL|ERR_NO_SEVERITY;
2418 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002419 case PP_WARNING:
H. Peter Anvin2f160432008-09-30 16:39:17 -07002420 severity = ERR_WARNING|ERR_NO_SEVERITY|ERR_WARN_USER;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002421 goto issue_error;
2422
2423 issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002424 {
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002425 /* Only error out if this is the final pass */
2426 if (pass != 2 && i != PP_FATAL)
2427 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002428
H. Peter Anvine2c80182005-01-15 22:15:51 +00002429 tline->next = expand_smacro(tline->next);
2430 tline = tline->next;
2431 skip_white_(tline);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002432 t = tline ? tline->next : NULL;
2433 skip_white_(t);
2434 if (tok_type_(tline, TOK_STRING) && !t) {
2435 /* The line contains only a quoted string */
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002436 p = tline->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002437 nasm_unquote(p, NULL);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002438 error(severity, "%s: %s", pp_directives[i], p);
2439 } else {
2440 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002441 p = detoken(tline, false);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002442 error(severity, "%s: %s", pp_directives[i], p);
2443 nasm_free(p);
H. Peter Anvind2456592008-06-19 15:04:18 -07002444 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002445 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002446 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002447 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002448
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002449 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002450 if (istk->conds && !emitting(istk->conds->state))
2451 j = COND_NEVER;
2452 else {
2453 j = if_condition(tline->next, i);
2454 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002455 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2456 }
2457 cond = nasm_malloc(sizeof(Cond));
2458 cond->next = istk->conds;
2459 cond->state = j;
2460 istk->conds = cond;
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002461 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002462 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002463
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002464 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002465 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002466 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002467 switch(istk->conds->state) {
2468 case COND_IF_TRUE:
2469 istk->conds->state = COND_DONE;
2470 break;
2471
2472 case COND_DONE:
2473 case COND_NEVER:
2474 break;
2475
2476 case COND_ELSE_TRUE:
2477 case COND_ELSE_FALSE:
H. Peter Anvin917a3492008-09-24 09:14:49 -07002478 error_precond(ERR_WARNING|ERR_PASS1,
2479 "`%%elif' after `%%else' ignored");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002480 istk->conds->state = COND_NEVER;
2481 break;
2482
2483 case COND_IF_FALSE:
2484 /*
2485 * IMPORTANT: In the case of %if, we will already have
2486 * called expand_mmac_params(); however, if we're
2487 * processing an %elif we must have been in a
2488 * non-emitting mode, which would have inhibited
H. Peter Anvin67c63722008-10-26 23:49:00 -07002489 * the normal invocation of expand_mmac_params().
2490 * Therefore, we have to do it explicitly here.
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002491 */
H. Peter Anvin67c63722008-10-26 23:49:00 -07002492 j = if_condition(expand_mmac_params(tline->next), i);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002493 tline->next = NULL; /* it got freed */
2494 istk->conds->state =
2495 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2496 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002497 }
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002498 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002499 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002500
H. Peter Anvine2c80182005-01-15 22:15:51 +00002501 case PP_ELSE:
2502 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002503 error_precond(ERR_WARNING|ERR_PASS1,
2504 "trailing garbage after `%%else' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002505 if (!istk->conds)
2506 error(ERR_FATAL, "`%%else': no matching `%%if'");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002507 switch(istk->conds->state) {
2508 case COND_IF_TRUE:
2509 case COND_DONE:
2510 istk->conds->state = COND_ELSE_FALSE;
2511 break;
2512
2513 case COND_NEVER:
2514 break;
2515
2516 case COND_IF_FALSE:
2517 istk->conds->state = COND_ELSE_TRUE;
2518 break;
2519
2520 case COND_ELSE_TRUE:
2521 case COND_ELSE_FALSE:
H. Peter Anvin917a3492008-09-24 09:14:49 -07002522 error_precond(ERR_WARNING|ERR_PASS1,
2523 "`%%else' after `%%else' ignored.");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002524 istk->conds->state = COND_NEVER;
2525 break;
2526 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002527 free_tlist(origline);
2528 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002529
H. Peter Anvine2c80182005-01-15 22:15:51 +00002530 case PP_ENDIF:
2531 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002532 error_precond(ERR_WARNING|ERR_PASS1,
2533 "trailing garbage after `%%endif' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002534 if (!istk->conds)
2535 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2536 cond = istk->conds;
2537 istk->conds = cond->next;
2538 nasm_free(cond);
2539 free_tlist(origline);
2540 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002541
H. Peter Anvine2c80182005-01-15 22:15:51 +00002542 case PP_MACRO:
2543 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002544 if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002545 error(ERR_FATAL,
2546 "`%%%smacro': already defining a macro",
2547 (i == PP_IMACRO ? "i" : ""));
H. Peter Anvina26433d2008-07-16 14:40:01 -07002548 return DIRECTIVE_FOUND;
2549 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002550 defining = nasm_malloc(sizeof(MMacro));
H. Peter Anvina26433d2008-07-16 14:40:01 -07002551 defining->casesense = (i == PP_MACRO);
2552 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2553 nasm_free(defining);
2554 defining = NULL;
2555 return DIRECTIVE_FOUND;
2556 }
2557
H. Peter Anvin166c2472008-05-28 12:28:58 -07002558 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002559 while (mmac) {
2560 if (!strcmp(mmac->name, defining->name) &&
2561 (mmac->nparam_min <= defining->nparam_max
2562 || defining->plus)
2563 && (defining->nparam_min <= mmac->nparam_max
2564 || mmac->plus)) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002565 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002566 "redefining multi-line macro `%s'", defining->name);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002567 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002568 }
2569 mmac = mmac->next;
2570 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002571 free_tlist(origline);
2572 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002573
H. Peter Anvine2c80182005-01-15 22:15:51 +00002574 case PP_ENDM:
2575 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002576 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002577 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2578 return DIRECTIVE_FOUND;
2579 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002580 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002581 defining->next = *mmhead;
2582 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002583 defining = NULL;
2584 free_tlist(origline);
2585 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002586
H. Peter Anvina26433d2008-07-16 14:40:01 -07002587 case PP_UNMACRO:
2588 case PP_UNIMACRO:
2589 {
2590 MMacro **mmac_p;
2591 MMacro spec;
2592
2593 spec.casesense = (i == PP_UNMACRO);
2594 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2595 return DIRECTIVE_FOUND;
2596 }
2597 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2598 while (mmac_p && *mmac_p) {
2599 mmac = *mmac_p;
2600 if (mmac->casesense == spec.casesense &&
2601 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2602 mmac->nparam_min == spec.nparam_min &&
2603 mmac->nparam_max == spec.nparam_max &&
2604 mmac->plus == spec.plus) {
2605 *mmac_p = mmac->next;
2606 free_mmacro(mmac);
2607 } else {
2608 mmac_p = &mmac->next;
2609 }
2610 }
2611 free_tlist(origline);
2612 free_tlist(spec.dlist);
2613 return DIRECTIVE_FOUND;
2614 }
2615
H. Peter Anvine2c80182005-01-15 22:15:51 +00002616 case PP_ROTATE:
2617 if (tline->next && tline->next->type == TOK_WHITESPACE)
2618 tline = tline->next;
2619 if (tline->next == NULL) {
2620 free_tlist(origline);
2621 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2622 return DIRECTIVE_FOUND;
2623 }
2624 t = expand_smacro(tline->next);
2625 tline->next = NULL;
2626 free_tlist(origline);
2627 tline = t;
2628 tptr = &t;
2629 tokval.t_type = TOKEN_INVALID;
2630 evalresult =
2631 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2632 free_tlist(tline);
2633 if (!evalresult)
2634 return DIRECTIVE_FOUND;
2635 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002636 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002637 "trailing garbage after expression ignored");
2638 if (!is_simple(evalresult)) {
2639 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2640 return DIRECTIVE_FOUND;
2641 }
2642 mmac = istk->mstk;
2643 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2644 mmac = mmac->next_active;
2645 if (!mmac) {
2646 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2647 } else if (mmac->nparam == 0) {
2648 error(ERR_NONFATAL,
2649 "`%%rotate' invoked within macro without parameters");
2650 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002651 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002652
H. Peter Anvin25a99342007-09-22 17:45:45 -07002653 rotate %= (int)mmac->nparam;
2654 if (rotate < 0)
2655 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002656
H. Peter Anvin25a99342007-09-22 17:45:45 -07002657 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002658 }
2659 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002660
H. Peter Anvine2c80182005-01-15 22:15:51 +00002661 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002662 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002663 do {
2664 tline = tline->next;
2665 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002666
H. Peter Anvine2c80182005-01-15 22:15:51 +00002667 if (tok_type_(tline, TOK_ID) &&
2668 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002669 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002670 do {
2671 tline = tline->next;
2672 } while (tok_type_(tline, TOK_WHITESPACE));
2673 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002674
H. Peter Anvine2c80182005-01-15 22:15:51 +00002675 if (tline) {
2676 t = expand_smacro(tline);
2677 tptr = &t;
2678 tokval.t_type = TOKEN_INVALID;
2679 evalresult =
2680 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2681 if (!evalresult) {
2682 free_tlist(origline);
2683 return DIRECTIVE_FOUND;
2684 }
2685 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002686 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002687 "trailing garbage after expression ignored");
2688 if (!is_simple(evalresult)) {
2689 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2690 return DIRECTIVE_FOUND;
2691 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002692 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002693 } else {
2694 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002695 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002696 }
2697 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002698
H. Peter Anvine2c80182005-01-15 22:15:51 +00002699 tmp_defining = defining;
2700 defining = nasm_malloc(sizeof(MMacro));
2701 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002702 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002703 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002704 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002705 defining->in_progress = count;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002706 defining->nparam_min = defining->nparam_max = 0;
2707 defining->defaults = NULL;
2708 defining->dlist = NULL;
2709 defining->expansion = NULL;
2710 defining->next_active = istk->mstk;
2711 defining->rep_nest = tmp_defining;
2712 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002713
H. Peter Anvine2c80182005-01-15 22:15:51 +00002714 case PP_ENDREP:
2715 if (!defining || defining->name) {
2716 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2717 return DIRECTIVE_FOUND;
2718 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002719
H. Peter Anvine2c80182005-01-15 22:15:51 +00002720 /*
2721 * Now we have a "macro" defined - although it has no name
2722 * and we won't be entering it in the hash tables - we must
2723 * push a macro-end marker for it on to istk->expansion.
2724 * After that, it will take care of propagating itself (a
2725 * macro-end marker line for a macro which is really a %rep
2726 * block will cause the macro to be re-expanded, complete
2727 * with another macro-end marker to ensure the process
2728 * continues) until the whole expansion is forcibly removed
2729 * from istk->expansion by a %exitrep.
2730 */
2731 l = nasm_malloc(sizeof(Line));
2732 l->next = istk->expansion;
2733 l->finishes = defining;
2734 l->first = NULL;
2735 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002736
H. Peter Anvine2c80182005-01-15 22:15:51 +00002737 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002738
H. Peter Anvine2c80182005-01-15 22:15:51 +00002739 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2740 tmp_defining = defining;
2741 defining = defining->rep_nest;
2742 free_tlist(origline);
2743 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002744
H. Peter Anvine2c80182005-01-15 22:15:51 +00002745 case PP_EXITREP:
2746 /*
2747 * We must search along istk->expansion until we hit a
2748 * macro-end marker for a macro with no name. Then we set
2749 * its `in_progress' flag to 0.
2750 */
2751 for (l = istk->expansion; l; l = l->next)
2752 if (l->finishes && !l->finishes->name)
H. Peter Anvinca348b62008-07-23 10:49:26 -04002753 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002754
H. Peter Anvine2c80182005-01-15 22:15:51 +00002755 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07002756 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002757 else
2758 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2759 free_tlist(origline);
2760 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002761
H. Peter Anvine2c80182005-01-15 22:15:51 +00002762 case PP_XDEFINE:
2763 case PP_IXDEFINE:
2764 case PP_DEFINE:
2765 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002766 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002767
H. Peter Anvine2c80182005-01-15 22:15:51 +00002768 tline = tline->next;
2769 skip_white_(tline);
2770 tline = expand_id(tline);
2771 if (!tline || (tline->type != TOK_ID &&
2772 (tline->type != TOK_PREPROC_ID ||
2773 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002774 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2775 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002776 free_tlist(origline);
2777 return DIRECTIVE_FOUND;
2778 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002779
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002780 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002781 last = tline;
2782 param_start = tline = tline->next;
2783 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002784
H. Peter Anvine2c80182005-01-15 22:15:51 +00002785 /* Expand the macro definition now for %xdefine and %ixdefine */
2786 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2787 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002788
H. Peter Anvine2c80182005-01-15 22:15:51 +00002789 if (tok_is_(tline, "(")) {
2790 /*
2791 * This macro has parameters.
2792 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002793
H. Peter Anvine2c80182005-01-15 22:15:51 +00002794 tline = tline->next;
2795 while (1) {
2796 skip_white_(tline);
2797 if (!tline) {
2798 error(ERR_NONFATAL, "parameter identifier expected");
2799 free_tlist(origline);
2800 return DIRECTIVE_FOUND;
2801 }
2802 if (tline->type != TOK_ID) {
2803 error(ERR_NONFATAL,
2804 "`%s': parameter identifier expected",
2805 tline->text);
2806 free_tlist(origline);
2807 return DIRECTIVE_FOUND;
2808 }
2809 tline->type = TOK_SMAC_PARAM + nparam++;
2810 tline = tline->next;
2811 skip_white_(tline);
2812 if (tok_is_(tline, ",")) {
2813 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04002814 } else {
2815 if (!tok_is_(tline, ")")) {
2816 error(ERR_NONFATAL,
2817 "`)' expected to terminate macro template");
2818 free_tlist(origline);
2819 return DIRECTIVE_FOUND;
2820 }
2821 break;
2822 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002823 }
2824 last = tline;
2825 tline = tline->next;
2826 }
2827 if (tok_type_(tline, TOK_WHITESPACE))
2828 last = tline, tline = tline->next;
2829 macro_start = NULL;
2830 last->next = NULL;
2831 t = tline;
2832 while (t) {
2833 if (t->type == TOK_ID) {
2834 for (tt = param_start; tt; tt = tt->next)
2835 if (tt->type >= TOK_SMAC_PARAM &&
2836 !strcmp(tt->text, t->text))
2837 t->type = tt->type;
2838 }
2839 tt = t->next;
2840 t->next = macro_start;
2841 macro_start = t;
2842 t = tt;
2843 }
2844 /*
2845 * Good. We now have a macro name, a parameter count, and a
2846 * token list (in reverse order) for an expansion. We ought
2847 * to be OK just to create an SMacro, store it, and let
2848 * free_tlist have the rest of the line (which we have
2849 * carefully re-terminated after chopping off the expansion
2850 * from the end).
2851 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002852 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002853 free_tlist(origline);
2854 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002855
H. Peter Anvine2c80182005-01-15 22:15:51 +00002856 case PP_UNDEF:
2857 tline = tline->next;
2858 skip_white_(tline);
2859 tline = expand_id(tline);
2860 if (!tline || (tline->type != TOK_ID &&
2861 (tline->type != TOK_PREPROC_ID ||
2862 tline->text[1] != '$'))) {
2863 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2864 free_tlist(origline);
2865 return DIRECTIVE_FOUND;
2866 }
2867 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002868 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002869 "trailing garbage after macro name ignored");
2870 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002871
H. Peter Anvine2c80182005-01-15 22:15:51 +00002872 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002873 ctx = get_ctx(tline->text, &mname, false);
2874 undef_smacro(ctx, mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002875 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002876 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002877
H. Peter Anvin9e200162008-06-04 17:23:14 -07002878 case PP_DEFSTR:
2879 case PP_IDEFSTR:
2880 casesense = (i == PP_DEFSTR);
2881
2882 tline = tline->next;
2883 skip_white_(tline);
2884 tline = expand_id(tline);
2885 if (!tline || (tline->type != TOK_ID &&
2886 (tline->type != TOK_PREPROC_ID ||
2887 tline->text[1] != '$'))) {
2888 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2889 pp_directives[i]);
2890 free_tlist(origline);
2891 return DIRECTIVE_FOUND;
2892 }
2893
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002894 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07002895 last = tline;
2896 tline = expand_smacro(tline->next);
2897 last->next = NULL;
2898
2899 while (tok_type_(tline, TOK_WHITESPACE))
2900 tline = delete_Token(tline);
2901
2902 p = detoken(tline, false);
2903 macro_start = nasm_malloc(sizeof(*macro_start));
2904 macro_start->next = NULL;
2905 macro_start->text = nasm_quote(p, strlen(p));
2906 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002907 macro_start->a.mac = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07002908 nasm_free(p);
2909
2910 /*
2911 * We now have a macro name, an implicit parameter count of
2912 * zero, and a string token to use as an expansion. Create
2913 * and store an SMacro.
2914 */
2915 define_smacro(ctx, mname, casesense, 0, macro_start);
2916 free_tlist(origline);
2917 return DIRECTIVE_FOUND;
2918
H. Peter Anvin418ca702008-05-30 10:42:30 -07002919 case PP_PATHSEARCH:
2920 {
2921 FILE *fp;
2922 StrList *xsl = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002923 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002924
2925 casesense = true;
2926
2927 tline = tline->next;
2928 skip_white_(tline);
2929 tline = expand_id(tline);
2930 if (!tline || (tline->type != TOK_ID &&
2931 (tline->type != TOK_PREPROC_ID ||
2932 tline->text[1] != '$'))) {
2933 error(ERR_NONFATAL,
2934 "`%%pathsearch' expects a macro identifier as first parameter");
2935 free_tlist(origline);
2936 return DIRECTIVE_FOUND;
2937 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002938 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002939 last = tline;
2940 tline = expand_smacro(tline->next);
2941 last->next = NULL;
2942
2943 t = tline;
2944 while (tok_type_(t, TOK_WHITESPACE))
2945 t = t->next;
2946
2947 if (!t || (t->type != TOK_STRING &&
2948 t->type != TOK_INTERNAL_STRING)) {
2949 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
2950 free_tlist(tline);
2951 free_tlist(origline);
2952 return DIRECTIVE_FOUND; /* but we did _something_ */
2953 }
2954 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002955 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002956 "trailing garbage after `%%pathsearch' ignored");
H. Peter Anvin427cc912008-06-01 21:43:03 -07002957 p = t->text;
2958 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002959 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002960
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002961 fp = inc_fopen(p, &xsl, &xst, true);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002962 if (fp) {
2963 p = xsl->str;
2964 fclose(fp); /* Don't actually care about the file */
2965 }
2966 macro_start = nasm_malloc(sizeof(*macro_start));
2967 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002968 macro_start->text = nasm_quote(p, strlen(p));
H. Peter Anvin418ca702008-05-30 10:42:30 -07002969 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002970 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002971 if (xsl)
2972 nasm_free(xsl);
2973
2974 /*
2975 * We now have a macro name, an implicit parameter count of
2976 * zero, and a string token to use as an expansion. Create
2977 * and store an SMacro.
2978 */
2979 define_smacro(ctx, mname, casesense, 0, macro_start);
2980 free_tlist(tline);
2981 free_tlist(origline);
2982 return DIRECTIVE_FOUND;
2983 }
2984
H. Peter Anvine2c80182005-01-15 22:15:51 +00002985 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002986 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07002987
H. Peter Anvine2c80182005-01-15 22:15:51 +00002988 tline = tline->next;
2989 skip_white_(tline);
2990 tline = expand_id(tline);
2991 if (!tline || (tline->type != TOK_ID &&
2992 (tline->type != TOK_PREPROC_ID ||
2993 tline->text[1] != '$'))) {
2994 error(ERR_NONFATAL,
2995 "`%%strlen' expects a macro identifier as first parameter");
2996 free_tlist(origline);
2997 return DIRECTIVE_FOUND;
2998 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002999 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003000 last = tline;
3001 tline = expand_smacro(tline->next);
3002 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003003
H. Peter Anvine2c80182005-01-15 22:15:51 +00003004 t = tline;
3005 while (tok_type_(t, TOK_WHITESPACE))
3006 t = t->next;
3007 /* t should now point to the string */
3008 if (t->type != TOK_STRING) {
3009 error(ERR_NONFATAL,
3010 "`%%strlen` requires string as second parameter");
3011 free_tlist(tline);
3012 free_tlist(origline);
3013 return DIRECTIVE_FOUND;
3014 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003015
H. Peter Anvine2c80182005-01-15 22:15:51 +00003016 macro_start = nasm_malloc(sizeof(*macro_start));
3017 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003018 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003019 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003020
H. Peter Anvine2c80182005-01-15 22:15:51 +00003021 /*
3022 * We now have a macro name, an implicit parameter count of
3023 * zero, and a numeric token to use as an expansion. Create
3024 * and store an SMacro.
3025 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003026 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003027 free_tlist(tline);
3028 free_tlist(origline);
3029 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003030
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003031 case PP_STRCAT:
3032 casesense = true;
3033
3034 tline = tline->next;
3035 skip_white_(tline);
3036 tline = expand_id(tline);
3037 if (!tline || (tline->type != TOK_ID &&
3038 (tline->type != TOK_PREPROC_ID ||
3039 tline->text[1] != '$'))) {
3040 error(ERR_NONFATAL,
3041 "`%%strcat' expects a macro identifier as first parameter");
3042 free_tlist(origline);
3043 return DIRECTIVE_FOUND;
3044 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003045 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003046 last = tline;
3047 tline = expand_smacro(tline->next);
3048 last->next = NULL;
3049
3050 len = 0;
3051 for (t = tline; t; t = t->next) {
3052 switch (t->type) {
3053 case TOK_WHITESPACE:
3054 break;
3055 case TOK_STRING:
3056 len += t->a.len = nasm_unquote(t->text, NULL);
3057 break;
H. Peter Anvinaccf4332008-07-01 21:42:08 -07003058 case TOK_OTHER:
3059 if (!strcmp(t->text, ",")) /* permit comma separators */
3060 break;
3061 /* else fall through */
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003062 default:
3063 error(ERR_NONFATAL,
3064 "non-string passed to `%%strcat' (%d)", t->type);
3065 free_tlist(tline);
3066 free_tlist(origline);
3067 return DIRECTIVE_FOUND;
3068 }
3069 }
3070
3071 p = pp = nasm_malloc(len);
3072 t = tline;
3073 for (t = tline; t; t = t->next) {
3074 if (t->type == TOK_STRING) {
3075 memcpy(p, t->text, t->a.len);
3076 p += t->a.len;
3077 }
3078 }
3079
3080 /*
3081 * We now have a macro name, an implicit parameter count of
3082 * zero, and a numeric token to use as an expansion. Create
3083 * and store an SMacro.
3084 */
3085 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3086 macro_start->text = nasm_quote(pp, len);
3087 nasm_free(pp);
3088 define_smacro(ctx, mname, casesense, 0, macro_start);
3089 free_tlist(tline);
3090 free_tlist(origline);
3091 return DIRECTIVE_FOUND;
3092
H. Peter Anvine2c80182005-01-15 22:15:51 +00003093 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003094 {
3095 int64_t a1, a2;
3096 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003097
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003098 casesense = true;
3099
H. Peter Anvine2c80182005-01-15 22:15:51 +00003100 tline = tline->next;
3101 skip_white_(tline);
3102 tline = expand_id(tline);
3103 if (!tline || (tline->type != TOK_ID &&
3104 (tline->type != TOK_PREPROC_ID ||
3105 tline->text[1] != '$'))) {
3106 error(ERR_NONFATAL,
3107 "`%%substr' expects a macro identifier as first parameter");
3108 free_tlist(origline);
3109 return DIRECTIVE_FOUND;
3110 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003111 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003112 last = tline;
3113 tline = expand_smacro(tline->next);
3114 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003115
H. Peter Anvine2c80182005-01-15 22:15:51 +00003116 t = tline->next;
3117 while (tok_type_(t, TOK_WHITESPACE))
3118 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003119
H. Peter Anvine2c80182005-01-15 22:15:51 +00003120 /* t should now point to the string */
3121 if (t->type != TOK_STRING) {
3122 error(ERR_NONFATAL,
3123 "`%%substr` requires string as second parameter");
3124 free_tlist(tline);
3125 free_tlist(origline);
3126 return DIRECTIVE_FOUND;
3127 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003128
H. Peter Anvine2c80182005-01-15 22:15:51 +00003129 tt = t->next;
3130 tptr = &tt;
3131 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003132 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3133 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003134 if (!evalresult) {
3135 free_tlist(tline);
3136 free_tlist(origline);
3137 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003138 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003139 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3140 free_tlist(tline);
3141 free_tlist(origline);
3142 return DIRECTIVE_FOUND;
3143 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003144 a1 = evalresult->value-1;
3145
3146 while (tok_type_(tt, TOK_WHITESPACE))
3147 tt = tt->next;
3148 if (!tt) {
3149 a2 = 1; /* Backwards compatibility: one character */
3150 } else {
3151 tokval.t_type = TOKEN_INVALID;
3152 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3153 pass, error, NULL);
3154 if (!evalresult) {
3155 free_tlist(tline);
3156 free_tlist(origline);
3157 return DIRECTIVE_FOUND;
3158 } else if (!is_simple(evalresult)) {
3159 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3160 free_tlist(tline);
3161 free_tlist(origline);
3162 return DIRECTIVE_FOUND;
3163 }
3164 a2 = evalresult->value;
3165 }
3166
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003167 len = nasm_unquote(t->text, NULL);
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003168 if (a2 < 0)
3169 a2 = a2+1+len-a1;
3170 if (a1+a2 > (int64_t)len)
3171 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003172
H. Peter Anvine2c80182005-01-15 22:15:51 +00003173 macro_start = nasm_malloc(sizeof(*macro_start));
3174 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003175 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003176 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003177 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003178
H. Peter Anvine2c80182005-01-15 22:15:51 +00003179 /*
3180 * We now have a macro name, an implicit parameter count of
3181 * zero, and a numeric token to use as an expansion. Create
3182 * and store an SMacro.
3183 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003184 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003185 free_tlist(tline);
3186 free_tlist(origline);
3187 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003188 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003189
H. Peter Anvine2c80182005-01-15 22:15:51 +00003190 case PP_ASSIGN:
3191 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003192 casesense = (i == PP_ASSIGN);
3193
H. Peter Anvine2c80182005-01-15 22:15:51 +00003194 tline = tline->next;
3195 skip_white_(tline);
3196 tline = expand_id(tline);
3197 if (!tline || (tline->type != TOK_ID &&
3198 (tline->type != TOK_PREPROC_ID ||
3199 tline->text[1] != '$'))) {
3200 error(ERR_NONFATAL,
3201 "`%%%sassign' expects a macro identifier",
3202 (i == PP_IASSIGN ? "i" : ""));
3203 free_tlist(origline);
3204 return DIRECTIVE_FOUND;
3205 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003206 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003207 last = tline;
3208 tline = expand_smacro(tline->next);
3209 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003210
H. Peter Anvine2c80182005-01-15 22:15:51 +00003211 t = tline;
3212 tptr = &t;
3213 tokval.t_type = TOKEN_INVALID;
3214 evalresult =
3215 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3216 free_tlist(tline);
3217 if (!evalresult) {
3218 free_tlist(origline);
3219 return DIRECTIVE_FOUND;
3220 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003221
H. Peter Anvine2c80182005-01-15 22:15:51 +00003222 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003223 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003224 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003225
H. Peter Anvine2c80182005-01-15 22:15:51 +00003226 if (!is_simple(evalresult)) {
3227 error(ERR_NONFATAL,
3228 "non-constant value given to `%%%sassign'",
3229 (i == PP_IASSIGN ? "i" : ""));
3230 free_tlist(origline);
3231 return DIRECTIVE_FOUND;
3232 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003233
H. Peter Anvine2c80182005-01-15 22:15:51 +00003234 macro_start = nasm_malloc(sizeof(*macro_start));
3235 macro_start->next = NULL;
3236 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003237 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003238
H. Peter Anvine2c80182005-01-15 22:15:51 +00003239 /*
3240 * We now have a macro name, an implicit parameter count of
3241 * zero, and a numeric token to use as an expansion. Create
3242 * and store an SMacro.
3243 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003244 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003245 free_tlist(origline);
3246 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003247
H. Peter Anvine2c80182005-01-15 22:15:51 +00003248 case PP_LINE:
3249 /*
3250 * Syntax is `%line nnn[+mmm] [filename]'
3251 */
3252 tline = tline->next;
3253 skip_white_(tline);
3254 if (!tok_type_(tline, TOK_NUMBER)) {
3255 error(ERR_NONFATAL, "`%%line' expects line number");
3256 free_tlist(origline);
3257 return DIRECTIVE_FOUND;
3258 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003259 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003260 m = 1;
3261 tline = tline->next;
3262 if (tok_is_(tline, "+")) {
3263 tline = tline->next;
3264 if (!tok_type_(tline, TOK_NUMBER)) {
3265 error(ERR_NONFATAL, "`%%line' expects line increment");
3266 free_tlist(origline);
3267 return DIRECTIVE_FOUND;
3268 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003269 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003270 tline = tline->next;
3271 }
3272 skip_white_(tline);
3273 src_set_linnum(k);
3274 istk->lineinc = m;
3275 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003276 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003277 }
3278 free_tlist(origline);
3279 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003280
H. Peter Anvine2c80182005-01-15 22:15:51 +00003281 default:
3282 error(ERR_FATAL,
3283 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003284 pp_directives[i]);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003285 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003286 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003287}
3288
3289/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003290 * Ensure that a macro parameter contains a condition code and
3291 * nothing else. Return the condition code index if so, or -1
3292 * otherwise.
3293 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003294static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003295{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003296 Token *tt;
3297 int i, j, k, m;
3298
H. Peter Anvin25a99342007-09-22 17:45:45 -07003299 if (!t)
3300 return -1; /* Probably a %+ without a space */
3301
H. Peter Anvineba20a72002-04-30 20:53:55 +00003302 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003303 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003304 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003305 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003306 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003307 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003308 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003309
3310 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00003311 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003312 while (j - i > 1) {
3313 k = (j + i) / 2;
3314 m = nasm_stricmp(t->text, conditions[k]);
3315 if (m == 0) {
3316 i = k;
3317 j = -2;
3318 break;
3319 } else if (m < 0) {
3320 j = k;
3321 } else
3322 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003323 }
3324 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003325 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003326 return i;
3327}
3328
3329/*
3330 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003331 * %-n) and MMacro-local identifiers (%%foo) as well as
3332 * macro indirection (%[...]).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003333 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003334static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003335{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003336 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003337 bool changed = false;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003338
3339 tail = &thead;
3340 thead = NULL;
3341
H. Peter Anvine2c80182005-01-15 22:15:51 +00003342 while (tline) {
3343 if (tline->type == TOK_PREPROC_ID &&
3344 (((tline->text[1] == '+' || tline->text[1] == '-')
3345 && tline->text[2]) || tline->text[1] == '%'
3346 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003347 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003348 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003349 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003350 unsigned int n;
3351 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003352 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003353
H. Peter Anvine2c80182005-01-15 22:15:51 +00003354 t = tline;
3355 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003356
H. Peter Anvine2c80182005-01-15 22:15:51 +00003357 mac = istk->mstk;
3358 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3359 mac = mac->next_active;
3360 if (!mac)
3361 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3362 else
3363 switch (t->text[1]) {
3364 /*
3365 * We have to make a substitution of one of the
3366 * forms %1, %-1, %+1, %%foo, %0.
3367 */
3368 case '0':
3369 type = TOK_NUMBER;
3370 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3371 text = nasm_strdup(tmpbuf);
3372 break;
3373 case '%':
3374 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003375 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00003376 mac->unique);
3377 text = nasm_strcat(tmpbuf, t->text + 2);
3378 break;
3379 case '-':
3380 n = atoi(t->text + 2) - 1;
3381 if (n >= mac->nparam)
3382 tt = NULL;
3383 else {
3384 if (mac->nparam > 1)
3385 n = (n + mac->rotate) % mac->nparam;
3386 tt = mac->params[n];
3387 }
3388 cc = find_cc(tt);
3389 if (cc == -1) {
3390 error(ERR_NONFATAL,
3391 "macro parameter %d is not a condition code",
3392 n + 1);
3393 text = NULL;
3394 } else {
3395 type = TOK_ID;
3396 if (inverse_ccs[cc] == -1) {
3397 error(ERR_NONFATAL,
3398 "condition code `%s' is not invertible",
3399 conditions[cc]);
3400 text = NULL;
3401 } else
H. Peter Anvin67c63722008-10-26 23:49:00 -07003402 text = nasm_strdup(conditions[inverse_ccs[cc]]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003403 }
3404 break;
3405 case '+':
3406 n = atoi(t->text + 2) - 1;
3407 if (n >= mac->nparam)
3408 tt = NULL;
3409 else {
3410 if (mac->nparam > 1)
3411 n = (n + mac->rotate) % mac->nparam;
3412 tt = mac->params[n];
3413 }
3414 cc = find_cc(tt);
3415 if (cc == -1) {
3416 error(ERR_NONFATAL,
3417 "macro parameter %d is not a condition code",
3418 n + 1);
3419 text = NULL;
3420 } else {
3421 type = TOK_ID;
3422 text = nasm_strdup(conditions[cc]);
3423 }
3424 break;
3425 default:
3426 n = atoi(t->text + 1) - 1;
3427 if (n >= mac->nparam)
3428 tt = NULL;
3429 else {
3430 if (mac->nparam > 1)
3431 n = (n + mac->rotate) % mac->nparam;
3432 tt = mac->params[n];
3433 }
3434 if (tt) {
3435 for (i = 0; i < mac->paramlen[n]; i++) {
3436 *tail = new_Token(NULL, tt->type, tt->text, 0);
3437 tail = &(*tail)->next;
3438 tt = tt->next;
3439 }
3440 }
3441 text = NULL; /* we've done it here */
3442 break;
3443 }
3444 if (!text) {
3445 delete_Token(t);
3446 } else {
3447 *tail = t;
3448 tail = &t->next;
3449 t->type = type;
3450 nasm_free(t->text);
3451 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003452 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003453 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07003454 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003455 continue;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003456 } else if (tline->type == TOK_INDIRECT) {
3457 t = tline;
3458 tline = tline->next;
3459 tt = tokenize(t->text);
3460 tt = expand_mmac_params(tt);
3461 tt = expand_smacro(tt);
3462 *tail = tt;
3463 while (tt) {
3464 tt->a.mac = NULL; /* Necessary? */
3465 tail = &tt->next;
3466 tt = tt->next;
3467 }
3468 delete_Token(t);
H. Peter Anvin6125b622009-04-08 14:02:25 -07003469 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003470 } else {
3471 t = *tail = tline;
3472 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003473 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003474 tail = &t->next;
3475 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003476 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003477 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003478
H. Peter Anvin6125b622009-04-08 14:02:25 -07003479 if (!changed)
3480 return thead;
3481
H. Peter Anvin67c63722008-10-26 23:49:00 -07003482 /* Now handle token pasting... */
H. Peter Anvin9bb46df2009-04-07 21:59:24 -07003483 tail = &thead;
3484 while ((t = *tail) && (tt = t->next)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003485 switch (t->type) {
3486 case TOK_WHITESPACE:
3487 if (tt->type == TOK_WHITESPACE) {
3488 t->next = delete_Token(tt);
H. Peter Anvin67c63722008-10-26 23:49:00 -07003489 } else {
H. Peter Anvin9bb46df2009-04-07 21:59:24 -07003490 tail = &t->next;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003491 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003492 break;
3493 case TOK_ID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003494 case TOK_NUMBER:
H. Peter Anvin9bb46df2009-04-07 21:59:24 -07003495 case TOK_FLOAT:
3496 {
3497 size_t len = 0;
3498 char *tmp, *p;
3499
3500 while (tt &&
3501 (tt->type == TOK_ID || tt->type == TOK_NUMBER ||
H. Peter Anvin6125b622009-04-08 14:02:25 -07003502 tt->type == TOK_FLOAT || tt->type == TOK_OTHER)) {
H. Peter Anvin9bb46df2009-04-07 21:59:24 -07003503 len += strlen(tt->text);
3504 tt = tt->next;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003505 }
H. Peter Anvin9bb46df2009-04-07 21:59:24 -07003506
3507 /* Now tt points to the first token after the potential
3508 paste area... */
3509 if (tt != t->next) {
3510 /* We have at least two tokens... */
3511 len += strlen(t->text);
3512 p = tmp = nasm_malloc(len+1);
3513
3514 while (t != tt) {
3515 strcpy(p, t->text);
3516 p = strchr(p, '\0');
3517 t = delete_Token(t);
3518 }
3519
3520 t = *tail = tokenize(tmp);
3521 nasm_free(tmp);
3522
3523 while (t->next)
3524 t = t->next;
3525
3526 t->next = tt; /* Attach the remaining token chain */
3527 }
3528 tail = &t->next;
3529 break;
3530 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003531 default:
H. Peter Anvin9bb46df2009-04-07 21:59:24 -07003532 tail = &t->next;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003533 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003534 }
H. Peter Anvin67c63722008-10-26 23:49:00 -07003535 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003536 return thead;
3537}
3538
3539/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003540 * Expand all single-line macro calls made in the given line.
3541 * Return the expanded version of the line. The original is deemed
3542 * to be destroyed in the process. (In reality we'll just move
3543 * Tokens from input to output a lot of the time, rather than
3544 * actually bothering to destroy and replicate.)
3545 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003546#define DEADMAN_LIMIT (1 << 20)
3547
H. Peter Anvine2c80182005-01-15 22:15:51 +00003548static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003549{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003550 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003551 struct hash_table *smtbl;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003552 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003553 Token **params;
3554 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003555 unsigned int nparam, sparam;
3556 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003557 Token *org_tline = tline;
3558 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003559 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003560 int deadman = DEADMAN_LIMIT;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003561
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003562 /*
3563 * Trick: we should avoid changing the start token pointer since it can
3564 * be contained in "next" field of other token. Because of this
3565 * we allocate a copy of first token and work with it; at the end of
3566 * routine we copy it back
3567 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003568 if (org_tline) {
3569 tline =
3570 new_Token(org_tline->next, org_tline->type, org_tline->text,
3571 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003572 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003573 nasm_free(org_tline->text);
3574 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003575 }
3576
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003577again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003578 tail = &thead;
3579 thead = NULL;
3580
H. Peter Anvine2c80182005-01-15 22:15:51 +00003581 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003582 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003583 error(ERR_NONFATAL, "interminable macro recursion");
3584 break;
3585 }
3586
H. Peter Anvine2c80182005-01-15 22:15:51 +00003587 if ((mname = tline->text)) {
3588 /* if this token is a local macro, look in local context */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003589 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
3590 ctx = get_ctx(mname, &mname, true);
3591 else
3592 ctx = NULL;
3593 smtbl = ctx ? &ctx->localmac : &smacros;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003594 head = (SMacro *) hash_findix(smtbl, mname);
H. Peter Anvin072771e2008-05-22 13:17:51 -07003595
H. Peter Anvine2c80182005-01-15 22:15:51 +00003596 /*
3597 * We've hit an identifier. As in is_mmacro below, we first
3598 * check whether the identifier is a single-line macro at
3599 * all, then think about checking for parameters if
3600 * necessary.
3601 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003602 for (m = head; m; m = m->next)
3603 if (!mstrcmp(m->name, mname, m->casesense))
3604 break;
3605 if (m) {
3606 mstart = tline;
3607 params = NULL;
3608 paramsize = NULL;
3609 if (m->nparam == 0) {
3610 /*
3611 * Simple case: the macro is parameterless. Discard the
3612 * one token that the macro call took, and push the
3613 * expansion back on the to-do stack.
3614 */
3615 if (!m->expansion) {
3616 if (!strcmp("__FILE__", m->name)) {
3617 int32_t num = 0;
H. Peter Anvin932de6c2008-07-31 18:46:11 -07003618 char *file = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003619 src_get(&num, &file);
3620 tline->text = nasm_quote(file, strlen(file));
H. Peter Anvin97a23472007-09-16 17:57:25 -07003621 tline->type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003622 nasm_free(file);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003623 continue;
3624 }
3625 if (!strcmp("__LINE__", m->name)) {
3626 nasm_free(tline->text);
3627 make_tok_num(tline, src_get_linnum());
3628 continue;
3629 }
3630 if (!strcmp("__BITS__", m->name)) {
3631 nasm_free(tline->text);
3632 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003633 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003634 }
3635 tline = delete_Token(tline);
3636 continue;
3637 }
3638 } else {
3639 /*
3640 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003641 * exists and takes parameters. We must find the
3642 * parameters in the call, count them, find the SMacro
3643 * that corresponds to that form of the macro call, and
3644 * substitute for the parameters when we expand. What a
3645 * pain.
3646 */
3647 /*tline = tline->next;
3648 skip_white_(tline); */
3649 do {
3650 t = tline->next;
3651 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003652 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003653 t->text = NULL;
3654 t = tline->next = delete_Token(t);
3655 }
3656 tline = t;
3657 } while (tok_type_(tline, TOK_WHITESPACE));
3658 if (!tok_is_(tline, "(")) {
3659 /*
3660 * This macro wasn't called with parameters: ignore
3661 * the call. (Behaviour borrowed from gnu cpp.)
3662 */
3663 tline = mstart;
3664 m = NULL;
3665 } else {
3666 int paren = 0;
3667 int white = 0;
3668 brackets = 0;
3669 nparam = 0;
3670 sparam = PARAM_DELTA;
3671 params = nasm_malloc(sparam * sizeof(Token *));
3672 params[0] = tline->next;
3673 paramsize = nasm_malloc(sparam * sizeof(int));
3674 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003675 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003676 /*
3677 * For some unusual expansions
3678 * which concatenates function call
3679 */
3680 t = tline->next;
3681 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003682 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003683 t->text = NULL;
3684 t = tline->next = delete_Token(t);
3685 }
3686 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003687
H. Peter Anvine2c80182005-01-15 22:15:51 +00003688 if (!tline) {
3689 error(ERR_NONFATAL,
3690 "macro call expects terminating `)'");
3691 break;
3692 }
3693 if (tline->type == TOK_WHITESPACE
3694 && brackets <= 0) {
3695 if (paramsize[nparam])
3696 white++;
3697 else
3698 params[nparam] = tline->next;
3699 continue; /* parameter loop */
3700 }
3701 if (tline->type == TOK_OTHER
3702 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003703 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003704 if (ch == ',' && !paren && brackets <= 0) {
3705 if (++nparam >= sparam) {
3706 sparam += PARAM_DELTA;
3707 params = nasm_realloc(params,
3708 sparam *
3709 sizeof(Token
3710 *));
3711 paramsize =
3712 nasm_realloc(paramsize,
3713 sparam *
3714 sizeof(int));
3715 }
3716 params[nparam] = tline->next;
3717 paramsize[nparam] = 0;
3718 white = 0;
3719 continue; /* parameter loop */
3720 }
3721 if (ch == '{' &&
3722 (brackets > 0 || (brackets == 0 &&
3723 !paramsize[nparam])))
3724 {
3725 if (!(brackets++)) {
3726 params[nparam] = tline->next;
3727 continue; /* parameter loop */
3728 }
3729 }
3730 if (ch == '}' && brackets > 0)
3731 if (--brackets == 0) {
3732 brackets = -1;
3733 continue; /* parameter loop */
3734 }
3735 if (ch == '(' && !brackets)
3736 paren++;
3737 if (ch == ')' && brackets <= 0)
3738 if (--paren < 0)
3739 break;
3740 }
3741 if (brackets < 0) {
3742 brackets = 0;
3743 error(ERR_NONFATAL, "braces do not "
3744 "enclose all of macro parameter");
3745 }
3746 paramsize[nparam] += white + 1;
3747 white = 0;
3748 } /* parameter loop */
3749 nparam++;
3750 while (m && (m->nparam != nparam ||
3751 mstrcmp(m->name, mname,
3752 m->casesense)))
3753 m = m->next;
3754 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003755 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003756 "macro `%s' exists, "
3757 "but not taking %d parameters",
3758 mstart->text, nparam);
3759 }
3760 }
3761 if (m && m->in_progress)
3762 m = NULL;
3763 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003764 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003765 * Design question: should we handle !tline, which
3766 * indicates missing ')' here, or expand those
3767 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003768 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003769 */
3770 nasm_free(params);
3771 nasm_free(paramsize);
3772 tline = mstart;
3773 } else {
3774 /*
3775 * Expand the macro: we are placed on the last token of the
3776 * call, so that we can easily split the call from the
3777 * following tokens. We also start by pushing an SMAC_END
3778 * token for the cycle removal.
3779 */
3780 t = tline;
3781 if (t) {
3782 tline = t->next;
3783 t->next = NULL;
3784 }
3785 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003786 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003787 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003788 tline = tt;
3789 for (t = m->expansion; t; t = t->next) {
3790 if (t->type >= TOK_SMAC_PARAM) {
3791 Token *pcopy = tline, **ptail = &pcopy;
3792 Token *ttt, *pt;
3793 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003794
H. Peter Anvine2c80182005-01-15 22:15:51 +00003795 ttt = params[t->type - TOK_SMAC_PARAM];
3796 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3797 --i >= 0;) {
3798 pt = *ptail =
3799 new_Token(tline, ttt->type, ttt->text,
3800 0);
3801 ptail = &pt->next;
3802 ttt = ttt->next;
3803 }
3804 tline = pcopy;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -07003805 } else if (t->type == TOK_PREPROC_Q) {
3806 tt = new_Token(tline, TOK_ID, mname, 0);
3807 tline = tt;
3808 } else if (t->type == TOK_PREPROC_QQ) {
3809 tt = new_Token(tline, TOK_ID, m->name, 0);
3810 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003811 } else {
3812 tt = new_Token(tline, t->type, t->text, 0);
3813 tline = tt;
3814 }
3815 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003816
H. Peter Anvine2c80182005-01-15 22:15:51 +00003817 /*
3818 * Having done that, get rid of the macro call, and clean
3819 * up the parameters.
3820 */
3821 nasm_free(params);
3822 nasm_free(paramsize);
3823 free_tlist(mstart);
3824 continue; /* main token loop */
3825 }
3826 }
3827 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003828
H. Peter Anvine2c80182005-01-15 22:15:51 +00003829 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003830 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003831 tline = delete_Token(tline);
3832 } else {
3833 t = *tail = tline;
3834 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003835 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003836 t->next = NULL;
3837 tail = &t->next;
3838 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003839 }
3840
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003841 /*
3842 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003843 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003844 * TOK_IDs should be concatenated.
3845 * Also we look for %+ tokens and concatenate the tokens before and after
3846 * them (without white spaces in between).
3847 */
3848 t = thead;
3849 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003850 while (t) {
3851 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3852 t = t->next;
3853 if (!t || !t->next)
3854 break;
3855 if (t->next->type == TOK_ID ||
3856 t->next->type == TOK_PREPROC_ID ||
3857 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003858 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003859 nasm_free(t->text);
3860 t->next = delete_Token(t->next);
3861 t->text = p;
3862 rescan = 1;
3863 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3864 t->next->next->type == TOK_PREPROC_ID &&
3865 strcmp(t->next->next->text, "%+") == 0) {
3866 /* free the next whitespace, the %+ token and next whitespace */
3867 int i;
3868 for (i = 1; i <= 3; i++) {
3869 if (!t->next
3870 || (i != 2 && t->next->type != TOK_WHITESPACE))
3871 break;
3872 t->next = delete_Token(t->next);
3873 } /* endfor */
3874 } else
3875 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003876 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003877 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003878 if (rescan) {
3879 tline = thead;
3880 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003881 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003882
H. Peter Anvine2c80182005-01-15 22:15:51 +00003883 if (org_tline) {
3884 if (thead) {
3885 *org_tline = *thead;
3886 /* since we just gave text to org_line, don't free it */
3887 thead->text = NULL;
3888 delete_Token(thead);
3889 } else {
3890 /* the expression expanded to empty line;
3891 we can't return NULL for some reasons
3892 we just set the line to a single WHITESPACE token. */
3893 memset(org_tline, 0, sizeof(*org_tline));
3894 org_tline->text = NULL;
3895 org_tline->type = TOK_WHITESPACE;
3896 }
3897 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003898 }
3899
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003900 return thead;
3901}
3902
3903/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003904 * Similar to expand_smacro but used exclusively with macro identifiers
3905 * right before they are fetched in. The reason is that there can be
3906 * identifiers consisting of several subparts. We consider that if there
3907 * are more than one element forming the name, user wants a expansion,
3908 * otherwise it will be left as-is. Example:
3909 *
3910 * %define %$abc cde
3911 *
3912 * the identifier %$abc will be left as-is so that the handler for %define
3913 * will suck it and define the corresponding value. Other case:
3914 *
3915 * %define _%$abc cde
3916 *
3917 * In this case user wants name to be expanded *before* %define starts
3918 * working, so we'll expand %$abc into something (if it has a value;
3919 * otherwise it will be left as-is) then concatenate all successive
3920 * PP_IDs into one.
3921 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003922static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003923{
3924 Token *cur, *oldnext = NULL;
3925
H. Peter Anvin734b1882002-04-30 21:01:08 +00003926 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003927 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003928
3929 cur = tline;
3930 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003931 (cur->next->type == TOK_ID ||
3932 cur->next->type == TOK_PREPROC_ID
3933 || cur->next->type == TOK_NUMBER))
3934 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003935
3936 /* If identifier consists of just one token, don't expand */
3937 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003938 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003939
H. Peter Anvine2c80182005-01-15 22:15:51 +00003940 if (cur) {
3941 oldnext = cur->next; /* Detach the tail past identifier */
3942 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003943 }
3944
H. Peter Anvin734b1882002-04-30 21:01:08 +00003945 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003946
H. Peter Anvine2c80182005-01-15 22:15:51 +00003947 if (cur) {
3948 /* expand_smacro possibly changhed tline; re-scan for EOL */
3949 cur = tline;
3950 while (cur && cur->next)
3951 cur = cur->next;
3952 if (cur)
3953 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003954 }
3955
3956 return tline;
3957}
3958
3959/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003960 * Determine whether the given line constitutes a multi-line macro
3961 * call, and return the MMacro structure called if so. Doesn't have
3962 * to check for an initial label - that's taken care of in
3963 * expand_mmacro - but must check numbers of parameters. Guaranteed
3964 * to be called with tline->type == TOK_ID, so the putative macro
3965 * name is easy to find.
3966 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003967static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003968{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003969 MMacro *head, *m;
3970 Token **params;
3971 int nparam;
3972
H. Peter Anvin166c2472008-05-28 12:28:58 -07003973 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003974
3975 /*
3976 * Efficiency: first we see if any macro exists with the given
3977 * name. If not, we can return NULL immediately. _Then_ we
3978 * count the parameters, and then we look further along the
3979 * list if necessary to find the proper MMacro.
3980 */
3981 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003982 if (!mstrcmp(m->name, tline->text, m->casesense))
3983 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003984 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003985 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003986
3987 /*
3988 * OK, we have a potential macro. Count and demarcate the
3989 * parameters.
3990 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003991 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003992
3993 /*
3994 * So we know how many parameters we've got. Find the MMacro
3995 * structure that handles this number.
3996 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003997 while (m) {
3998 if (m->nparam_min <= nparam
3999 && (m->plus || nparam <= m->nparam_max)) {
4000 /*
4001 * This one is right. Just check if cycle removal
4002 * prohibits us using it before we actually celebrate...
4003 */
4004 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00004005#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00004006 error(ERR_NONFATAL,
4007 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004008#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00004009 nasm_free(params);
4010 return NULL;
4011 }
4012 /*
4013 * It's right, and we can use it. Add its default
4014 * parameters to the end of our list if necessary.
4015 */
4016 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4017 params =
4018 nasm_realloc(params,
4019 ((m->nparam_min + m->ndefs +
4020 1) * sizeof(*params)));
4021 while (nparam < m->nparam_min + m->ndefs) {
4022 params[nparam] = m->defaults[nparam - m->nparam_min];
4023 nparam++;
4024 }
4025 }
4026 /*
4027 * If we've gone over the maximum parameter count (and
4028 * we're in Plus mode), ignore parameters beyond
4029 * nparam_max.
4030 */
4031 if (m->plus && nparam > m->nparam_max)
4032 nparam = m->nparam_max;
4033 /*
4034 * Then terminate the parameter list, and leave.
4035 */
4036 if (!params) { /* need this special case */
4037 params = nasm_malloc(sizeof(*params));
4038 nparam = 0;
4039 }
4040 params[nparam] = NULL;
4041 *params_array = params;
4042 return m;
4043 }
4044 /*
4045 * This one wasn't right: look for the next one with the
4046 * same name.
4047 */
4048 for (m = m->next; m; m = m->next)
4049 if (!mstrcmp(m->name, tline->text, m->casesense))
4050 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004051 }
4052
4053 /*
4054 * After all that, we didn't find one with the right number of
4055 * parameters. Issue a warning, and fail to expand the macro.
4056 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004057 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004058 "macro `%s' exists, but not taking %d parameters",
4059 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004060 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004061 return NULL;
4062}
4063
4064/*
4065 * Expand the multi-line macro call made by the given line, if
4066 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00004067 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004068 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004069static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004070{
4071 Token *startline = tline;
4072 Token *label = NULL;
4073 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004074 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004075 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004076 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004077 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004078 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004079
4080 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004081 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004082 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004083 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004084 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004085 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004086 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004087 if (m) {
4088 mname = t->text;
4089 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004090 Token *last;
4091 /*
4092 * We have an id which isn't a macro call. We'll assume
4093 * it might be a label; we'll also check to see if a
4094 * colon follows it. Then, if there's another id after
4095 * that lot, we'll check it again for macro-hood.
4096 */
4097 label = last = t;
4098 t = t->next;
4099 if (tok_type_(t, TOK_WHITESPACE))
4100 last = t, t = t->next;
4101 if (tok_is_(t, ":")) {
4102 dont_prepend = 1;
4103 last = t, t = t->next;
4104 if (tok_type_(t, TOK_WHITESPACE))
4105 last = t, t = t->next;
4106 }
4107 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
4108 return 0;
4109 last->next = NULL;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004110 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004111 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004112 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004113
4114 /*
4115 * Fix up the parameters: this involves stripping leading and
4116 * trailing whitespace, then stripping braces if they are
4117 * present.
4118 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004119 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004120 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004121
H. Peter Anvine2c80182005-01-15 22:15:51 +00004122 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004123 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004124 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004125
H. Peter Anvine2c80182005-01-15 22:15:51 +00004126 t = params[i];
4127 skip_white_(t);
4128 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004129 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004130 params[i] = t;
4131 paramlen[i] = 0;
4132 while (t) {
4133 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4134 break; /* ... because we have hit a comma */
4135 if (comma && t->type == TOK_WHITESPACE
4136 && tok_is_(t->next, ","))
4137 break; /* ... or a space then a comma */
4138 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4139 break; /* ... or a brace */
4140 t = t->next;
4141 paramlen[i]++;
4142 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004143 }
4144
4145 /*
4146 * OK, we have a MMacro structure together with a set of
4147 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004148 * copies of each Line on to istk->expansion. Substitution of
4149 * parameter tokens and macro-local tokens doesn't get done
4150 * until the single-line macro substitution process; this is
4151 * because delaying them allows us to change the semantics
4152 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004153 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004154 * First, push an end marker on to istk->expansion, mark this
4155 * macro as in progress, and set up its invocation-specific
4156 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004157 */
4158 ll = nasm_malloc(sizeof(Line));
4159 ll->next = istk->expansion;
4160 ll->finishes = m;
4161 ll->first = NULL;
4162 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004163
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004164 m->in_progress = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004165 m->params = params;
4166 m->iline = tline;
4167 m->nparam = nparam;
4168 m->rotate = 0;
4169 m->paramlen = paramlen;
4170 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004171 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004172
4173 m->next_active = istk->mstk;
4174 istk->mstk = m;
4175
H. Peter Anvine2c80182005-01-15 22:15:51 +00004176 for (l = m->expansion; l; l = l->next) {
4177 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004178
H. Peter Anvine2c80182005-01-15 22:15:51 +00004179 ll = nasm_malloc(sizeof(Line));
4180 ll->finishes = NULL;
4181 ll->next = istk->expansion;
4182 istk->expansion = ll;
4183 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004184
H. Peter Anvine2c80182005-01-15 22:15:51 +00004185 for (t = l->first; t; t = t->next) {
4186 Token *x = t;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004187 switch (t->type) {
4188 case TOK_PREPROC_Q:
H. Peter Anvinc751e862008-06-09 10:18:45 -07004189 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004190 break;
4191 case TOK_PREPROC_QQ:
4192 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4193 break;
4194 case TOK_PREPROC_ID:
4195 if (t->text[1] == '0' && t->text[2] == '0') {
4196 dont_prepend = -1;
4197 x = label;
4198 if (!x)
4199 continue;
4200 }
4201 /* fall through */
4202 default:
4203 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4204 break;
H. Peter Anvin166c2472008-05-28 12:28:58 -07004205 }
H. Peter Anvince2233b2008-05-25 21:57:00 -07004206 tail = &tt->next;
4207 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004208 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004209 }
4210
4211 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004212 * If we had a label, push it on as the first line of
4213 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004214 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004215 if (label) {
4216 if (dont_prepend < 0)
4217 free_tlist(startline);
4218 else {
4219 ll = nasm_malloc(sizeof(Line));
4220 ll->finishes = NULL;
4221 ll->next = istk->expansion;
4222 istk->expansion = ll;
4223 ll->first = startline;
4224 if (!dont_prepend) {
4225 while (label->next)
4226 label = label->next;
4227 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4228 }
4229 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004230 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004231
H. Peter Anvin734b1882002-04-30 21:01:08 +00004232 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004233
H. Peter Anvineba20a72002-04-30 20:53:55 +00004234 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004235}
4236
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004237/* The function that actually does the error reporting */
4238static void verror(int severity, const char *fmt, va_list arg)
4239{
4240 char buff[1024];
4241
4242 vsnprintf(buff, sizeof(buff), fmt, arg);
4243
4244 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004245 _error(severity, "(%s:%d) %s", istk->mstk->name,
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004246 istk->mstk->lineno, buff);
4247 else
H. Peter Anvin917a3492008-09-24 09:14:49 -07004248 _error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004249}
4250
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004251/*
4252 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004253 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004254 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004255static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004256{
4257 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004258
4259 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004260 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004261 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004262
H. Peter Anvin734b1882002-04-30 21:01:08 +00004263 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004264 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004265 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004266}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004267
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004268/*
4269 * Because %else etc are evaluated in the state context
4270 * of the previous branch, errors might get lost with error():
4271 * %if 0 ... %else trailing garbage ... %endif
4272 * So %else etc should report errors with this function.
4273 */
4274static void error_precond(int severity, const char *fmt, ...)
4275{
4276 va_list arg;
4277
4278 /* Only ignore the error if it's really in a dead branch */
4279 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4280 return;
4281
4282 va_start(arg, fmt);
4283 verror(severity, fmt, arg);
4284 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004285}
4286
H. Peter Anvin734b1882002-04-30 21:01:08 +00004287static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004288pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004289 ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004290{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004291 Token *t;
4292
H. Peter Anvin99941bf2002-05-14 17:44:03 +00004293 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004294 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004295 istk = nasm_malloc(sizeof(Include));
4296 istk->next = NULL;
4297 istk->conds = NULL;
4298 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004299 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004300 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004301 istk->fname = NULL;
4302 src_set_fname(nasm_strdup(file));
4303 src_set_linnum(0);
4304 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004305 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004306 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004307 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004308 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004309 nested_mac_count = 0;
4310 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004311 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004312 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004313 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004314 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004315 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004316 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004317 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004318 any_extrastdmac = extrastdmac && *extrastdmac;
4319 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004320 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004321 evaluate = eval;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004322
4323 /*
4324 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4325 * The caller, however, will also pass in 3 for preprocess-only so
4326 * we can set __PASS__ accordingly.
4327 */
4328 pass = apass > 2 ? 2 : apass;
4329
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004330 dephead = deptail = deplist;
4331 if (deplist) {
4332 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4333 sl->next = NULL;
4334 strcpy(sl->str, file);
4335 *deptail = sl;
4336 deptail = &sl->next;
4337 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004338
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004339 /*
4340 * Define the __PASS__ macro. This is defined here unlike
4341 * all the other builtins, because it is special -- it varies between
4342 * passes.
4343 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004344 t = nasm_malloc(sizeof(*t));
4345 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004346 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004347 t->a.mac = NULL;
4348 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004349}
4350
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004351static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004352{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004353 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004354 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004355
H. Peter Anvine2c80182005-01-15 22:15:51 +00004356 while (1) {
4357 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004358 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004359 * buffer or from the input file.
4360 */
4361 tline = NULL;
4362 while (istk->expansion && istk->expansion->finishes) {
4363 Line *l = istk->expansion;
4364 if (!l->finishes->name && l->finishes->in_progress > 1) {
4365 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004366
H. Peter Anvine2c80182005-01-15 22:15:51 +00004367 /*
4368 * This is a macro-end marker for a macro with no
4369 * name, which means it's not really a macro at all
4370 * but a %rep block, and the `in_progress' field is
4371 * more than 1, meaning that we still need to
4372 * repeat. (1 means the natural last repetition; 0
4373 * means termination by %exitrep.) We have
4374 * therefore expanded up to the %endrep, and must
4375 * push the whole block on to the expansion buffer
4376 * again. We don't bother to remove the macro-end
4377 * marker: we'd only have to generate another one
4378 * if we did.
4379 */
4380 l->finishes->in_progress--;
4381 for (l = l->finishes->expansion; l; l = l->next) {
4382 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004383
H. Peter Anvine2c80182005-01-15 22:15:51 +00004384 ll = nasm_malloc(sizeof(Line));
4385 ll->next = istk->expansion;
4386 ll->finishes = NULL;
4387 ll->first = NULL;
4388 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004389
H. Peter Anvine2c80182005-01-15 22:15:51 +00004390 for (t = l->first; t; t = t->next) {
4391 if (t->text || t->type == TOK_WHITESPACE) {
4392 tt = *tail =
4393 new_Token(NULL, t->type, t->text, 0);
4394 tail = &tt->next;
4395 }
4396 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004397
H. Peter Anvine2c80182005-01-15 22:15:51 +00004398 istk->expansion = ll;
4399 }
4400 } else {
4401 /*
4402 * Check whether a `%rep' was started and not ended
4403 * within this macro expansion. This can happen and
4404 * should be detected. It's a fatal error because
4405 * I'm too confused to work out how to recover
4406 * sensibly from it.
4407 */
4408 if (defining) {
4409 if (defining->name)
4410 error(ERR_PANIC,
4411 "defining with name in expansion");
4412 else if (istk->mstk->name)
4413 error(ERR_FATAL,
4414 "`%%rep' without `%%endrep' within"
4415 " expansion of macro `%s'",
4416 istk->mstk->name);
4417 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004418
H. Peter Anvine2c80182005-01-15 22:15:51 +00004419 /*
4420 * FIXME: investigate the relationship at this point between
4421 * istk->mstk and l->finishes
4422 */
4423 {
4424 MMacro *m = istk->mstk;
4425 istk->mstk = m->next_active;
4426 if (m->name) {
4427 /*
4428 * This was a real macro call, not a %rep, and
4429 * therefore the parameter information needs to
4430 * be freed.
4431 */
4432 nasm_free(m->params);
4433 free_tlist(m->iline);
4434 nasm_free(m->paramlen);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004435 l->finishes->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004436 } else
4437 free_mmacro(m);
4438 }
4439 istk->expansion = l->next;
4440 nasm_free(l);
4441 list->downlevel(LIST_MACRO);
4442 }
4443 }
4444 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004445
H. Peter Anvine2c80182005-01-15 22:15:51 +00004446 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004447 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004448 Line *l = istk->expansion;
4449 if (istk->mstk)
4450 istk->mstk->lineno++;
4451 tline = l->first;
4452 istk->expansion = l->next;
4453 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004454 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004455 list->line(LIST_MACRO, p);
4456 nasm_free(p);
4457 break;
4458 }
4459 line = read_line();
4460 if (line) { /* from the current input file */
4461 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004462 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004463 nasm_free(line);
4464 break;
4465 }
4466 /*
4467 * The current file has ended; work down the istk
4468 */
4469 {
4470 Include *i = istk;
4471 fclose(i->fp);
4472 if (i->conds)
4473 error(ERR_FATAL,
4474 "expected `%%endif' before end of file");
4475 /* only set line and file name if there's a next node */
4476 if (i->next) {
4477 src_set_linnum(i->lineno);
4478 nasm_free(src_set_fname(i->fname));
H. Peter Anvin86877b22008-06-20 15:55:45 -07004479 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004480 istk = i->next;
4481 list->downlevel(LIST_INCLUDE);
4482 nasm_free(i);
4483 if (!istk)
4484 return NULL;
Victor van den Elzen4c9d6222008-10-01 13:08:50 +02004485 if (istk->expansion && istk->expansion->finishes)
4486 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004487 }
4488 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004489
H. Peter Anvine2c80182005-01-15 22:15:51 +00004490 /*
4491 * We must expand MMacro parameters and MMacro-local labels
4492 * _before_ we plunge into directive processing, to cope
4493 * with things like `%define something %1' such as STRUC
4494 * uses. Unless we're _defining_ a MMacro, in which case
4495 * those tokens should be left alone to go into the
4496 * definition; and unless we're in a non-emitting
4497 * condition, in which case we don't want to meddle with
4498 * anything.
4499 */
Charles Crayned4200be2008-07-12 16:42:33 -07004500 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin992fe752008-10-19 15:45:05 -07004501 && !(istk->mstk && !istk->mstk->in_progress)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004502 tline = expand_mmac_params(tline);
H. Peter Anvin992fe752008-10-19 15:45:05 -07004503 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004504
H. Peter Anvine2c80182005-01-15 22:15:51 +00004505 /*
4506 * Check the line to see if it's a preprocessor directive.
4507 */
4508 if (do_directive(tline) == DIRECTIVE_FOUND) {
4509 continue;
4510 } else if (defining) {
4511 /*
4512 * We're defining a multi-line macro. We emit nothing
4513 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004514 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004515 */
4516 Line *l = nasm_malloc(sizeof(Line));
4517 l->next = defining->expansion;
4518 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004519 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004520 defining->expansion = l;
4521 continue;
4522 } else if (istk->conds && !emitting(istk->conds->state)) {
4523 /*
4524 * We're in a non-emitting branch of a condition block.
4525 * Emit nothing at all, not even a blank line: when we
4526 * emerge from the condition we'll give a line-number
4527 * directive so we keep our place correctly.
4528 */
4529 free_tlist(tline);
4530 continue;
4531 } else if (istk->mstk && !istk->mstk->in_progress) {
4532 /*
4533 * We're in a %rep block which has been terminated, so
4534 * we're walking through to the %endrep without
4535 * emitting anything. Emit nothing at all, not even a
4536 * blank line: when we emerge from the %rep block we'll
4537 * give a line-number directive so we keep our place
4538 * correctly.
4539 */
4540 free_tlist(tline);
4541 continue;
4542 } else {
4543 tline = expand_smacro(tline);
4544 if (!expand_mmacro(tline)) {
4545 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004546 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004547 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004548 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004549 free_tlist(tline);
4550 break;
4551 } else {
4552 continue; /* expand_mmacro calls free_tlist */
4553 }
4554 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004555 }
4556
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004557 return line;
4558}
4559
H. Peter Anvine2c80182005-01-15 22:15:51 +00004560static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004561{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004562 if (defining) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02004563 if(defining->name) {
4564 error(ERR_NONFATAL,
4565 "end of file while still defining macro `%s'",
4566 defining->name);
4567 } else {
4568 error(ERR_NONFATAL, "end of file while still in %%rep");
4569 }
4570
H. Peter Anvine2c80182005-01-15 22:15:51 +00004571 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004572 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004573 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004574 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004575 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004576 while (istk) {
4577 Include *i = istk;
4578 istk = istk->next;
4579 fclose(i->fp);
4580 nasm_free(i->fname);
4581 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004582 }
4583 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004584 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004585 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004586 if (pass == 0) {
H. Peter Anvin86877b22008-06-20 15:55:45 -07004587 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004588 free_llist(predef);
4589 delete_Blocks();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004590 while ((i = ipath)) {
4591 ipath = i->next;
4592 if (i->path)
4593 nasm_free(i->path);
4594 nasm_free(i);
4595 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07004596 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004597}
4598
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004599void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004600{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004601 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004602
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004603 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004604 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004605 i->next = NULL;
4606
H. Peter Anvine2c80182005-01-15 22:15:51 +00004607 if (ipath != NULL) {
4608 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004609 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004610 j = j->next;
4611 j->next = i;
4612 } else {
4613 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004614 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004615}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004616
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004617void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004618{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004619 Token *inc, *space, *name;
4620 Line *l;
4621
H. Peter Anvin734b1882002-04-30 21:01:08 +00004622 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4623 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4624 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004625
4626 l = nasm_malloc(sizeof(Line));
4627 l->next = predef;
4628 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004629 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004630 predef = l;
4631}
4632
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004633void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004634{
4635 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004636 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004637 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004638
4639 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004640 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4641 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004642 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004643 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004644 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004645 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004646 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004647
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004648 l = nasm_malloc(sizeof(Line));
4649 l->next = predef;
4650 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004651 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004652 predef = l;
4653}
4654
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004655void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004656{
4657 Token *def, *space;
4658 Line *l;
4659
H. Peter Anvin734b1882002-04-30 21:01:08 +00004660 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4661 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004662 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004663
4664 l = nasm_malloc(sizeof(Line));
4665 l->next = predef;
4666 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004667 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004668 predef = l;
4669}
4670
Keith Kaniosb7a89542007-04-12 02:40:54 +00004671/*
4672 * Added by Keith Kanios:
4673 *
4674 * This function is used to assist with "runtime" preprocessor
4675 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4676 *
4677 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4678 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4679 */
4680
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004681void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004682{
4683 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004684
Keith Kaniosb7a89542007-04-12 02:40:54 +00004685 def = tokenize(definition);
4686 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4687 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004688
Keith Kaniosb7a89542007-04-12 02:40:54 +00004689}
4690
H. Peter Anvina70547f2008-07-19 21:44:26 -07004691void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004692{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004693 extrastdmac = macros;
4694}
4695
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004696static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004697{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004698 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004699 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004700 tok->text = nasm_strdup(numbuf);
4701 tok->type = TOK_NUMBER;
4702}
4703
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004704Preproc nasmpp = {
4705 pp_reset,
4706 pp_getline,
4707 pp_cleanup
4708};