blob: 434383b555f0b3ad6b60961988e6b5127b7b84b4 [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 Anvin992fe752008-10-19 15:45:05 -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 Anvin992fe752008-10-19 15:45:05 -0700400static Token *expand_indirect(Token * tline, int level);
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -0700401static Context *get_ctx(const char *name, 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 Anvinca544db2008-10-19 19:30:11 -0700813 while (lvl && (c = *p)) {
814 switch (c) {
815 case ']':
816 lvl--;
817 break;
818 case '%':
819 p++;
820 if (*p == '[')
821 lvl++;
822 break;
823 case '\'':
824 case '\"':
825 case '`':
826 p = nasm_skip_string(p);
827 break;
828 default:
829 break;
H. Peter Anvin992fe752008-10-19 15:45:05 -0700830 }
831 p++;
832 }
833 if (*p)
834 *p++ = '\0';
835 type = TOK_INDIRECT;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700836 } else if (*p == '?') {
837 type = TOK_PREPROC_Q; /* %? */
838 p++;
839 if (*p == '?') {
840 type = TOK_PREPROC_QQ; /* %?? */
841 p++;
842 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000843 } else if (isidchar(*p) ||
844 ((*p == '!' || *p == '%' || *p == '$') &&
845 isidchar(p[1]))) {
846 do {
847 p++;
848 }
849 while (isidchar(*p));
850 type = TOK_PREPROC_ID;
851 } else {
852 type = TOK_OTHER;
853 if (*p == '%')
854 p++;
855 }
856 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
857 type = TOK_ID;
858 p++;
859 while (*p && isidchar(*p))
860 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700861 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000862 /*
863 * A string token.
864 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000865 type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700866 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000867
H. Peter Anvine2c80182005-01-15 22:15:51 +0000868 if (*p) {
869 p++;
870 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -0700871 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000872 /* Handling unterminated strings by UNV */
873 /* type = -1; */
874 }
875 } else if (isnumstart(*p)) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700876 bool is_hex = false;
877 bool is_float = false;
878 bool has_e = false;
879 char c, *r;
880
H. Peter Anvine2c80182005-01-15 22:15:51 +0000881 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700882 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000883 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700884
885 if (*p == '$') {
886 p++;
887 is_hex = true;
888 }
889
890 for (;;) {
891 c = *p++;
892
893 if (!is_hex && (c == 'e' || c == 'E')) {
894 has_e = true;
895 if (*p == '+' || *p == '-') {
896 /* e can only be followed by +/- if it is either a
897 prefixed hex number or a floating-point number */
898 p++;
899 is_float = true;
900 }
901 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
902 is_hex = true;
903 } else if (c == 'P' || c == 'p') {
904 is_float = true;
905 if (*p == '+' || *p == '-')
906 p++;
907 } else if (isnumchar(c) || c == '_')
908 ; /* just advance */
909 else if (c == '.') {
910 /* we need to deal with consequences of the legacy
911 parser, like "1.nolist" being two tokens
912 (TOK_NUMBER, TOK_ID) here; at least give it
913 a shot for now. In the future, we probably need
914 a flex-based scanner with proper pattern matching
915 to do it as well as it can be done. Nothing in
916 the world is going to help the person who wants
917 0x123.p16 interpreted as two tokens, though. */
918 r = p;
919 while (*r == '_')
920 r++;
921
H. Peter Anvinf221b9e2008-06-21 11:03:51 -0700922 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700923 (!is_hex && (*r == 'e' || *r == 'E')) ||
924 (*r == 'p' || *r == 'P')) {
925 p = r;
926 is_float = true;
927 } else
928 break; /* Terminate the token */
929 } else
930 break;
931 }
932 p--; /* Point to first character beyond number */
933
934 if (has_e && !is_hex) {
935 /* 1e13 is floating-point, but 1e13h is not */
936 is_float = true;
937 }
938
939 type = is_float ? TOK_FLOAT : TOK_NUMBER;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700940 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000941 type = TOK_WHITESPACE;
942 p++;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700943 while (*p && nasm_isspace(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000944 p++;
945 /*
946 * Whitespace just before end-of-line is discarded by
947 * pretending it's a comment; whitespace just before a
948 * comment gets lumped into the comment.
949 */
950 if (!*p || *p == ';') {
951 type = TOK_COMMENT;
952 while (*p)
953 p++;
954 }
955 } else if (*p == ';') {
956 type = TOK_COMMENT;
957 while (*p)
958 p++;
959 } else {
960 /*
961 * Anything else is an operator of some kind. We check
962 * for all the double-character operators (>>, <<, //,
963 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000964 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000965 */
966 type = TOK_OTHER;
967 if ((p[0] == '>' && p[1] == '>') ||
968 (p[0] == '<' && p[1] == '<') ||
969 (p[0] == '/' && p[1] == '/') ||
970 (p[0] == '<' && p[1] == '=') ||
971 (p[0] == '>' && p[1] == '=') ||
972 (p[0] == '=' && p[1] == '=') ||
973 (p[0] == '!' && p[1] == '=') ||
974 (p[0] == '<' && p[1] == '>') ||
975 (p[0] == '&' && p[1] == '&') ||
976 (p[0] == '|' && p[1] == '|') ||
977 (p[0] == '^' && p[1] == '^')) {
978 p++;
979 }
980 p++;
981 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000982
H. Peter Anvine2c80182005-01-15 22:15:51 +0000983 /* Handling unterminated string by UNV */
984 /*if (type == -1)
985 {
986 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
987 t->text[p-line] = *line;
988 tail = &t->next;
989 }
990 else */
991 if (type != TOK_COMMENT) {
992 *tail = t = new_Token(NULL, type, line, p - line);
993 tail = &t->next;
994 }
995 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000996 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000997 return list;
998}
999
H. Peter Anvince616072002-04-30 21:02:23 +00001000/*
1001 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001002 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001003 * deleted only all at once by the delete_Blocks function.
1004 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001006{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001007 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001008
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001009 /* first, get to the end of the linked list */
1010 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001012 /* now allocate the requested chunk */
1013 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001015 /* now allocate a new block for the next request */
1016 b->next = nasm_malloc(sizeof(Blocks));
1017 /* and initialize the contents of the new block */
1018 b->next->next = NULL;
1019 b->next->chunk = NULL;
1020 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001021}
1022
1023/*
1024 * this function deletes all managed blocks of memory
1025 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001026static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001027{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001028 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001029
H. Peter Anvin70653092007-10-19 14:42:29 -07001030 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001031 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001032 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001033 * free it.
1034 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001035 while (b) {
1036 if (b->chunk)
1037 nasm_free(b->chunk);
1038 a = b;
1039 b = b->next;
1040 if (a != &blocks)
1041 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001042 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001043}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001044
1045/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001046 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001047 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001048 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001049 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001050static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvinc751e862008-06-09 10:18:45 -07001051 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001052{
1053 Token *t;
1054 int i;
1055
H. Peter Anvine2c80182005-01-15 22:15:51 +00001056 if (freeTokens == NULL) {
1057 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1058 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1059 freeTokens[i].next = &freeTokens[i + 1];
1060 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001061 }
1062 t = freeTokens;
1063 freeTokens = t->next;
1064 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001065 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001066 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001067 if (type == TOK_WHITESPACE || text == NULL) {
1068 t->text = NULL;
1069 } else {
1070 if (txtlen == 0)
1071 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001072 t->text = nasm_malloc(txtlen+1);
1073 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001075 }
1076 return t;
1077}
1078
H. Peter Anvine2c80182005-01-15 22:15:51 +00001079static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001080{
1081 Token *next = t->next;
1082 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001083 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001084 freeTokens = t;
1085 return next;
1086}
1087
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001088/*
1089 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001090 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1091 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001092 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001093static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001094{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001095 Token *t;
1096 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001097 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001098 const char *q;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001099
1100 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101 for (t = tlist; t; t = t->next) {
1102 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001103 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001104 nasm_free(t->text);
1105 if (p)
1106 t->text = nasm_strdup(p);
1107 else
1108 t->text = NULL;
1109 }
1110 /* Expand local macros here and not during preprocessing */
1111 if (expand_locals &&
1112 t->type == TOK_PREPROC_ID && t->text &&
1113 t->text[0] == '%' && t->text[1] == '$') {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001114 Context *ctx = get_ctx(t->text, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001115 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001116 char buffer[40];
1117 char *p, *q = t->text + 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001118
H. Peter Anvine2c80182005-01-15 22:15:51 +00001119 q += strspn(q, "$");
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001120 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001121 p = nasm_strcat(buffer, q);
1122 nasm_free(t->text);
1123 t->text = p;
1124 }
1125 }
1126 if (t->type == TOK_WHITESPACE) {
1127 len++;
1128 } else if (t->text) {
1129 len += strlen(t->text);
1130 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001131 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001132 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001133 for (t = tlist; t; t = t->next) {
1134 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001135 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001136 } else if (t->text) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001137 q = t->text;
1138 while (*q)
1139 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001140 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001141 }
1142 *p = '\0';
1143 return line;
1144}
1145
1146/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001147 * A scanner, suitable for use by the expression evaluator, which
1148 * operates on a line of Tokens. Expects a pointer to a pointer to
1149 * the first token in the line to be passed in as its private_data
1150 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001151 *
1152 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001153 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001154static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001155{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001156 Token **tlineptr = private_data;
1157 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001158 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001159
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160 do {
1161 tline = *tlineptr;
1162 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001163 }
1164 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001165 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001166
1167 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001168 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001169
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001170 tokval->t_charptr = tline->text;
1171
H. Peter Anvin76690a12002-04-30 20:52:49 +00001172 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001173 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001174 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001175 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001176
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001178 p = tokval->t_charptr = tline->text;
1179 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001180 tokval->t_charptr++;
1181 return tokval->t_type = TOKEN_ID;
1182 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001183
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001184 for (r = p, s = ourcopy; *r; r++) {
Philipp Thomas76ec8e72008-05-21 08:53:21 -07001185 if (r >= p+MAX_KEYWORD)
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001186 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001187 *s++ = nasm_tolower(*r);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001188 }
1189 *s = '\0';
1190 /* right, so we have an identifier sitting in temp storage. now,
1191 * is it actually a register or instruction name, or what? */
1192 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001193 }
1194
H. Peter Anvine2c80182005-01-15 22:15:51 +00001195 if (tline->type == TOK_NUMBER) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001196 bool rn_error;
1197 tokval->t_integer = readnum(tline->text, &rn_error);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001198 tokval->t_charptr = tline->text;
H. Peter Anvin11627042008-06-09 20:45:19 -07001199 if (rn_error)
1200 return tokval->t_type = TOKEN_ERRNUM;
1201 else
1202 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001203 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001204
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001205 if (tline->type == TOK_FLOAT) {
1206 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001207 }
1208
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209 if (tline->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001210 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001211
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001212 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001213 tokval->t_charptr = tline->text;
1214 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001215
H. Peter Anvin11627042008-06-09 20:45:19 -07001216 if (ep[0] != bq || ep[1] != '\0')
1217 return tokval->t_type = TOKEN_ERRSTR;
1218 else
1219 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001220 }
1221
H. Peter Anvine2c80182005-01-15 22:15:51 +00001222 if (tline->type == TOK_OTHER) {
1223 if (!strcmp(tline->text, "<<"))
1224 return tokval->t_type = TOKEN_SHL;
1225 if (!strcmp(tline->text, ">>"))
1226 return tokval->t_type = TOKEN_SHR;
1227 if (!strcmp(tline->text, "//"))
1228 return tokval->t_type = TOKEN_SDIV;
1229 if (!strcmp(tline->text, "%%"))
1230 return tokval->t_type = TOKEN_SMOD;
1231 if (!strcmp(tline->text, "=="))
1232 return tokval->t_type = TOKEN_EQ;
1233 if (!strcmp(tline->text, "<>"))
1234 return tokval->t_type = TOKEN_NE;
1235 if (!strcmp(tline->text, "!="))
1236 return tokval->t_type = TOKEN_NE;
1237 if (!strcmp(tline->text, "<="))
1238 return tokval->t_type = TOKEN_LE;
1239 if (!strcmp(tline->text, ">="))
1240 return tokval->t_type = TOKEN_GE;
1241 if (!strcmp(tline->text, "&&"))
1242 return tokval->t_type = TOKEN_DBL_AND;
1243 if (!strcmp(tline->text, "^^"))
1244 return tokval->t_type = TOKEN_DBL_XOR;
1245 if (!strcmp(tline->text, "||"))
1246 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001247 }
1248
1249 /*
1250 * We have no other options: just return the first character of
1251 * the token text.
1252 */
1253 return tokval->t_type = tline->text[0];
1254}
1255
1256/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001257 * Compare a string to the name of an existing macro; this is a
1258 * simple wrapper which calls either strcmp or nasm_stricmp
1259 * depending on the value of the `casesense' parameter.
1260 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001261static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001262{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001263 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001264}
1265
1266/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001267 * Compare a string to the name of an existing macro; this is a
1268 * simple wrapper which calls either strcmp or nasm_stricmp
1269 * depending on the value of the `casesense' parameter.
1270 */
1271static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1272{
1273 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1274}
1275
1276/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001277 * Return the Context structure associated with a %$ token. Return
1278 * NULL, having _already_ reported an error condition, if the
1279 * context stack isn't deep enough for the supplied number of $
1280 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001281 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001282 * also scanned for such smacro, until it is found; if not -
1283 * only the context that directly results from the number of $'s
1284 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001285 */
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001286static Context *get_ctx(const char *name, bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001287{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001288 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001289 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001290 int i;
1291
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001292 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001293 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001294
H. Peter Anvine2c80182005-01-15 22:15:51 +00001295 if (!cstk) {
1296 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1297 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001298 }
1299
H. Peter Anvine2c80182005-01-15 22:15:51 +00001300 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1301 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001302/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001303 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001304 if (!ctx) {
1305 error(ERR_NONFATAL, "`%s': context stack is only"
1306 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1307 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001308 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001309 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001311
H. Peter Anvine2c80182005-01-15 22:15:51 +00001312 do {
1313 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001314 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001315 while (m) {
1316 if (!mstrcmp(m->name, name, m->casesense))
1317 return ctx;
1318 m = m->next;
1319 }
1320 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001321 }
1322 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001323 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001324}
1325
1326/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001327 * Check to see if a file is already in a string list
1328 */
1329static bool in_list(const StrList *list, const char *str)
1330{
1331 while (list) {
1332 if (!strcmp(list->str, str))
1333 return true;
1334 list = list->next;
1335 }
1336 return false;
1337}
1338
1339/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001340 * Open an include file. This routine must always return a valid
1341 * file pointer if it returns - it's responsible for throwing an
1342 * ERR_FATAL and bombing out completely if not. It should also try
1343 * the include path one by one until it finds the file or reaches
1344 * the end of the path.
1345 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001346static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
H. Peter Anvin418ca702008-05-30 10:42:30 -07001347 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001348{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001349 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001350 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001351 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001352 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001353 size_t prefix_len = 0;
1354 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001355
H. Peter Anvine2c80182005-01-15 22:15:51 +00001356 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001357 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1358 memcpy(sl->str, prefix, prefix_len);
1359 memcpy(sl->str+prefix_len, file, len+1);
1360 fp = fopen(sl->str, "r");
H. Peter Anvin418ca702008-05-30 10:42:30 -07001361 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001362 sl->next = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001363 **dtail = sl;
1364 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001365 } else {
1366 nasm_free(sl);
1367 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001368 if (fp)
1369 return fp;
H. Peter Anvin418ca702008-05-30 10:42:30 -07001370 if (!ip) {
1371 if (!missing_ok)
1372 break;
1373 prefix = NULL;
1374 } else {
1375 prefix = ip->path;
1376 ip = ip->next;
1377 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001378 if (prefix) {
1379 prefix_len = strlen(prefix);
1380 } else {
1381 /* -MG given and file not found */
H. Peter Anvin418ca702008-05-30 10:42:30 -07001382 if (dhead && !in_list(*dhead, file)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001383 sl = nasm_malloc(len+1+sizeof sl->next);
1384 sl->next = NULL;
1385 strcpy(sl->str, file);
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001386 **dtail = sl;
1387 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001388 }
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001389 return NULL;
1390 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001391 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001392
H. Peter Anvin734b1882002-04-30 21:01:08 +00001393 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001394 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001395}
1396
1397/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001398 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001399 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001400 * return true if _any_ single-line macro of that name is defined.
1401 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001402 * `nparam' or no parameters is defined.
1403 *
1404 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001405 * defined, or nparam is -1, the address of the definition structure
1406 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001407 * is NULL, no action will be taken regarding its contents, and no
1408 * error will occur.
1409 *
1410 * Note that this is also called with nparam zero to resolve
1411 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001412 *
1413 * If you already know which context macro belongs to, you can pass
1414 * the context pointer as first parameter; if you won't but name begins
1415 * with %$ the context will be automatically computed. If all_contexts
1416 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001417 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001418static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001419smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001420 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001421{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001422 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001423 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001424
H. Peter Anvin97a23472007-09-16 17:57:25 -07001425 if (ctx) {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001426 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001427 } else if (name[0] == '%' && name[1] == '$') {
1428 if (cstk)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001429 ctx = get_ctx(name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001431 return false; /* got to return _something_ */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001432 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001433 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001434 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001435 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001436 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001437
H. Peter Anvine2c80182005-01-15 22:15:51 +00001438 while (m) {
1439 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001440 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001441 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001442 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001443 *defn = m;
1444 else
1445 *defn = NULL;
1446 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001447 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001448 }
1449 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001450 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001451
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001452 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001453}
1454
1455/*
1456 * Count and mark off the parameters in a multi-line macro call.
1457 * This is called both from within the multi-line macro expansion
1458 * code, and also to mark off the default parameters when provided
1459 * in a %macro definition line.
1460 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001461static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001462{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001463 int paramsize, brace;
1464
1465 *nparam = paramsize = 0;
1466 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001467 while (t) {
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001468 /* +1: we need space for the final NULL */
1469 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001470 paramsize += PARAM_DELTA;
1471 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1472 }
1473 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001474 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001475 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001476 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001477 (*params)[(*nparam)++] = t;
1478 while (tok_isnt_(t, brace ? "}" : ","))
1479 t = t->next;
1480 if (t) { /* got a comma/brace */
1481 t = t->next;
1482 if (brace) {
1483 /*
1484 * Now we've found the closing brace, look further
1485 * for the comma.
1486 */
1487 skip_white_(t);
1488 if (tok_isnt_(t, ",")) {
1489 error(ERR_NONFATAL,
1490 "braces do not enclose all of macro parameter");
1491 while (tok_isnt_(t, ","))
1492 t = t->next;
1493 }
1494 if (t)
1495 t = t->next; /* eat the comma */
1496 }
1497 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001498 }
1499}
1500
1501/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001502 * Determine whether one of the various `if' conditions is true or
1503 * not.
1504 *
1505 * We must free the tline we get passed.
1506 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001507static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001508{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001509 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001510 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001511 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001512 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001513 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001514 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001515
1516 origline = tline;
1517
H. Peter Anvine2c80182005-01-15 22:15:51 +00001518 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001519 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001520 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001521 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001522 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001523 if (!tline)
1524 break;
1525 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001526 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001527 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001528 free_tlist(origline);
1529 return -1;
1530 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001531 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001532 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001533 tline = tline->next;
1534 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001535 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001536
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001537 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001538 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001539 while (tline) {
1540 skip_white_(tline);
1541 if (!tline || (tline->type != TOK_ID &&
1542 (tline->type != TOK_PREPROC_ID ||
1543 tline->text[1] != '$'))) {
1544 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001545 "`%s' expects macro identifiers", pp_directives[ct]);
1546 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001547 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001548 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001549 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001550 tline = tline->next;
1551 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001552 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001553
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001554 case PPC_IFIDN:
1555 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001556 tline = expand_smacro(tline);
1557 t = tt = tline;
1558 while (tok_isnt_(tt, ","))
1559 tt = tt->next;
1560 if (!tt) {
1561 error(ERR_NONFATAL,
1562 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001563 pp_directives[ct]);
1564 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001565 }
1566 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001567 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001568 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1569 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1570 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001571 pp_directives[ct]);
1572 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001573 }
1574 if (t->type == TOK_WHITESPACE) {
1575 t = t->next;
1576 continue;
1577 }
1578 if (tt->type == TOK_WHITESPACE) {
1579 tt = tt->next;
1580 continue;
1581 }
1582 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001583 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001584 break;
1585 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001586 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 if (t->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001588 size_t l1 = nasm_unquote(t->text, NULL);
1589 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001590
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001591 if (l1 != l2) {
1592 j = false;
1593 break;
1594 }
1595 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1596 j = false;
1597 break;
1598 }
1599 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001600 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001601 break;
1602 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001603
H. Peter Anvine2c80182005-01-15 22:15:51 +00001604 t = t->next;
1605 tt = tt->next;
1606 }
1607 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001608 j = false; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001609 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001610
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001611 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001612 {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001613 bool found = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001614 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001615
H. Peter Anvine2c80182005-01-15 22:15:51 +00001616 tline = tline->next;
1617 skip_white_(tline);
1618 tline = expand_id(tline);
1619 if (!tok_type_(tline, TOK_ID)) {
1620 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001621 "`%s' expects a macro name", pp_directives[ct]);
1622 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001623 }
1624 searching.name = nasm_strdup(tline->text);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001625 searching.casesense = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001626 searching.plus = false;
1627 searching.nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001628 searching.in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001629 searching.rep_nest = NULL;
1630 searching.nparam_min = 0;
1631 searching.nparam_max = INT_MAX;
1632 tline = expand_smacro(tline->next);
1633 skip_white_(tline);
1634 if (!tline) {
1635 } else if (!tok_type_(tline, TOK_NUMBER)) {
1636 error(ERR_NONFATAL,
1637 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001638 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 } else {
1640 searching.nparam_min = searching.nparam_max =
1641 readnum(tline->text, &j);
1642 if (j)
1643 error(ERR_NONFATAL,
1644 "unable to parse parameter count `%s'",
1645 tline->text);
1646 }
1647 if (tline && tok_is_(tline->next, "-")) {
1648 tline = tline->next->next;
1649 if (tok_is_(tline, "*"))
1650 searching.nparam_max = INT_MAX;
1651 else if (!tok_type_(tline, TOK_NUMBER))
1652 error(ERR_NONFATAL,
1653 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001654 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001655 else {
1656 searching.nparam_max = readnum(tline->text, &j);
1657 if (j)
1658 error(ERR_NONFATAL,
1659 "unable to parse parameter count `%s'",
1660 tline->text);
1661 if (searching.nparam_min > searching.nparam_max)
1662 error(ERR_NONFATAL,
1663 "minimum parameter count exceeds maximum");
1664 }
1665 }
1666 if (tline && tok_is_(tline->next, "+")) {
1667 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001668 searching.plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001669 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001670 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001671 while (mmac) {
1672 if (!strcmp(mmac->name, searching.name) &&
1673 (mmac->nparam_min <= searching.nparam_max
1674 || searching.plus)
1675 && (searching.nparam_min <= mmac->nparam_max
1676 || mmac->plus)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001677 found = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001678 break;
1679 }
1680 mmac = mmac->next;
1681 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001682 if(tline && tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001683 error(ERR_WARNING|ERR_PASS1,
1684 "trailing garbage after %%ifmacro ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001686 j = found;
1687 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001688 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001689
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001690 case PPC_IFID:
1691 needtype = TOK_ID;
1692 goto iftype;
1693 case PPC_IFNUM:
1694 needtype = TOK_NUMBER;
1695 goto iftype;
1696 case PPC_IFSTR:
1697 needtype = TOK_STRING;
1698 goto iftype;
1699
1700 iftype:
H. Peter Anvin134b9462008-02-16 17:01:40 -08001701 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001702
H. Peter Anvin927c92b2008-02-16 13:44:52 -08001703 while (tok_type_(t, TOK_WHITESPACE) ||
1704 (needtype == TOK_NUMBER &&
1705 tok_type_(t, TOK_OTHER) &&
1706 (t->text[0] == '-' || t->text[0] == '+') &&
1707 !t->text[1]))
1708 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001709
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001710 j = tok_type_(t, needtype);
1711 break;
1712
1713 case PPC_IFTOKEN:
1714 t = tline = expand_smacro(tline);
1715 while (tok_type_(t, TOK_WHITESPACE))
1716 t = t->next;
1717
1718 j = false;
1719 if (t) {
1720 t = t->next; /* Skip the actual token */
1721 while (tok_type_(t, TOK_WHITESPACE))
1722 t = t->next;
1723 j = !t; /* Should be nothing left */
1724 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001725 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001726
H. Peter Anvin134b9462008-02-16 17:01:40 -08001727 case PPC_IFEMPTY:
1728 t = tline = expand_smacro(tline);
1729 while (tok_type_(t, TOK_WHITESPACE))
1730 t = t->next;
1731
1732 j = !t; /* Should be empty */
1733 break;
1734
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001735 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001736 t = tline = expand_smacro(tline);
1737 tptr = &t;
1738 tokval.t_type = TOKEN_INVALID;
1739 evalresult = evaluate(ppscan, tptr, &tokval,
1740 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001741 if (!evalresult)
1742 return -1;
1743 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001744 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 "trailing garbage after expression ignored");
1746 if (!is_simple(evalresult)) {
1747 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001748 "non-constant value given to `%s'", pp_directives[ct]);
1749 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001750 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001751 j = reloc_value(evalresult) != 0;
H. Peter Anvin90e6e812008-07-16 14:38:24 -07001752 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001753
H. Peter Anvine2c80182005-01-15 22:15:51 +00001754 default:
1755 error(ERR_FATAL,
1756 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001757 pp_directives[ct]);
1758 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001759 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001760
1761 free_tlist(origline);
1762 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001763
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001764fail:
1765 free_tlist(origline);
1766 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001767}
1768
1769/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001770 * Common code for defining an smacro
1771 */
1772static bool define_smacro(Context *ctx, char *mname, bool casesense,
1773 int nparam, Token *expansion)
1774{
1775 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001776 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001777
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001778 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1779 if (!smac) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001780 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001781 "single-line macro `%s' defined both with and"
1782 " without parameters", mname);
1783
1784 /* Some instances of the old code considered this a failure,
1785 some others didn't. What is the right thing to do here? */
1786 free_tlist(expansion);
1787 return false; /* Failure */
1788 } else {
1789 /*
1790 * We're redefining, so we have to take over an
1791 * existing SMacro structure. This means freeing
1792 * what was already in it.
1793 */
1794 nasm_free(smac->name);
1795 free_tlist(smac->expansion);
1796 }
1797 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001798 smtbl = ctx ? &ctx->localmac : &smacros;
1799 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001800 smac = nasm_malloc(sizeof(SMacro));
1801 smac->next = *smhead;
1802 *smhead = smac;
1803 }
1804 smac->name = nasm_strdup(mname);
1805 smac->casesense = casesense;
1806 smac->nparam = nparam;
1807 smac->expansion = expansion;
1808 smac->in_progress = false;
1809 return true; /* Success */
1810}
1811
1812/*
1813 * Undefine an smacro
1814 */
1815static void undef_smacro(Context *ctx, const char *mname)
1816{
1817 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001818 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001819
H. Peter Anvin166c2472008-05-28 12:28:58 -07001820 smtbl = ctx ? &ctx->localmac : &smacros;
1821 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001822
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001823 if (smhead) {
1824 /*
1825 * We now have a macro name... go hunt for it.
1826 */
1827 sp = smhead;
1828 while ((s = *sp) != NULL) {
1829 if (!mstrcmp(s->name, mname, s->casesense)) {
1830 *sp = s->next;
1831 nasm_free(s->name);
1832 free_tlist(s->expansion);
1833 nasm_free(s);
1834 } else {
1835 sp = &s->next;
1836 }
1837 }
1838 }
1839}
1840
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001841/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07001842 * Parse a mmacro specification.
1843 */
1844static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1845{
1846 bool err;
1847
1848 tline = tline->next;
1849 skip_white_(tline);
1850 tline = expand_id(tline);
1851 if (!tok_type_(tline, TOK_ID)) {
1852 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1853 return false;
1854 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001855
H. Peter Anvina26433d2008-07-16 14:40:01 -07001856 def->name = nasm_strdup(tline->text);
1857 def->plus = false;
1858 def->nolist = false;
1859 def->in_progress = 0;
1860 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001861 def->nparam_min = 0;
1862 def->nparam_max = 0;
1863
H. Peter Anvina26433d2008-07-16 14:40:01 -07001864 tline = expand_smacro(tline->next);
1865 skip_white_(tline);
1866 if (!tok_type_(tline, TOK_NUMBER)) {
1867 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001868 } else {
1869 def->nparam_min = def->nparam_max =
1870 readnum(tline->text, &err);
1871 if (err)
1872 error(ERR_NONFATAL,
1873 "unable to parse parameter count `%s'", tline->text);
1874 }
1875 if (tline && tok_is_(tline->next, "-")) {
1876 tline = tline->next->next;
1877 if (tok_is_(tline, "*")) {
1878 def->nparam_max = INT_MAX;
1879 } else if (!tok_type_(tline, TOK_NUMBER)) {
1880 error(ERR_NONFATAL,
1881 "`%s' expects a parameter count after `-'", directive);
1882 } else {
1883 def->nparam_max = readnum(tline->text, &err);
1884 if (err) {
1885 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
1886 tline->text);
1887 }
1888 if (def->nparam_min > def->nparam_max) {
1889 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
1890 }
1891 }
1892 }
1893 if (tline && tok_is_(tline->next, "+")) {
1894 tline = tline->next;
1895 def->plus = true;
1896 }
1897 if (tline && tok_type_(tline->next, TOK_ID) &&
1898 !nasm_stricmp(tline->next->text, ".nolist")) {
1899 tline = tline->next;
1900 def->nolist = true;
1901 }
1902
1903 /*
1904 * Handle default parameters.
1905 */
1906 if (tline && tline->next) {
1907 def->dlist = tline->next;
1908 tline->next = NULL;
1909 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
1910 } else {
1911 def->dlist = NULL;
1912 def->defaults = NULL;
1913 }
1914 def->expansion = NULL;
1915
Victor van den Elzen22343c22008-08-06 14:47:54 +02001916 if(def->defaults &&
1917 def->ndefs > def->nparam_max - def->nparam_min &&
1918 !def->plus)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001919 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
1920 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001921
H. Peter Anvina26433d2008-07-16 14:40:01 -07001922 return true;
1923}
1924
1925
1926/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001927 * Decode a size directive
1928 */
1929static int parse_size(const char *str) {
1930 static const char *size_names[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001931 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001932 static const int sizes[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001933 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001934
1935 return sizes[bsii(str, size_names, elements(size_names))+1];
1936}
1937
Ed Beroset3ab3f412002-06-11 03:31:49 +00001938/**
1939 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001940 * Find out if a line contains a preprocessor directive, and deal
1941 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07001942 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001943 * If a directive _is_ found, it is the responsibility of this routine
1944 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001945 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001946 * @param tline a pointer to the current tokeninzed line linked list
1947 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07001948 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001949 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001950static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001951{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001952 enum preproc_token i;
1953 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07001954 bool err;
1955 int nparam;
1956 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001957 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001958 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001959 int offset;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001960 char *p, *pp, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001961 Include *inc;
1962 Context *ctx;
1963 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001964 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001965 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1966 Line *l;
1967 struct tokenval tokval;
1968 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001969 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001970 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001971 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07001972 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001973
1974 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001975
H. Peter Anvineba20a72002-04-30 20:53:55 +00001976 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07001977 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001978 (tline->text[1] == '%' || tline->text[1] == '$'
1979 || tline->text[1] == '!'))
1980 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001981
H. Peter Anvin4169a472007-09-12 01:29:43 +00001982 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001983
1984 /*
1985 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001986 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001987 * we should ignore all directives except for condition
1988 * directives.
1989 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001990 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001991 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1992 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001993 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001994
1995 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001996 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02001997 * ignore all directives except for %macro/%imacro (which nest),
1998 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
1999 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002000 */
2001 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00002002 i != PP_ENDMACRO && i != PP_ENDM &&
2003 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2004 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002005 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002006
Charles Crayned4200be2008-07-12 16:42:33 -07002007 if (defining) {
2008 if (i == PP_MACRO || i == PP_IMACRO) {
2009 nested_mac_count++;
2010 return NO_DIRECTIVE_FOUND;
2011 } else if (nested_mac_count > 0) {
2012 if (i == PP_ENDMACRO) {
2013 nested_mac_count--;
2014 return NO_DIRECTIVE_FOUND;
2015 }
2016 }
2017 if (!defining->name) {
2018 if (i == PP_REP) {
2019 nested_rep_count++;
2020 return NO_DIRECTIVE_FOUND;
2021 } else if (nested_rep_count > 0) {
2022 if (i == PP_ENDREP) {
2023 nested_rep_count--;
2024 return NO_DIRECTIVE_FOUND;
2025 }
2026 }
2027 }
2028 }
2029
H. Peter Anvin4169a472007-09-12 01:29:43 +00002030 switch (i) {
2031 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002032 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2033 tline->text);
2034 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002035
H. Peter Anvine2c80182005-01-15 22:15:51 +00002036 case PP_STACKSIZE:
2037 /* Directive to tell NASM what the default stack size is. The
2038 * default is for a 16-bit stack, and this can be overriden with
2039 * %stacksize large.
2040 * the following form:
2041 *
2042 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2043 */
2044 tline = tline->next;
2045 if (tline && tline->type == TOK_WHITESPACE)
2046 tline = tline->next;
2047 if (!tline || tline->type != TOK_ID) {
2048 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2049 free_tlist(origline);
2050 return DIRECTIVE_FOUND;
2051 }
2052 if (nasm_stricmp(tline->text, "flat") == 0) {
2053 /* All subsequent ARG directives are for a 32-bit stack */
2054 StackSize = 4;
2055 StackPointer = "ebp";
2056 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002057 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002058 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2059 /* All subsequent ARG directives are for a 64-bit stack */
2060 StackSize = 8;
2061 StackPointer = "rbp";
2062 ArgOffset = 8;
2063 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002064 } else if (nasm_stricmp(tline->text, "large") == 0) {
2065 /* All subsequent ARG directives are for a 16-bit stack,
2066 * far function call.
2067 */
2068 StackSize = 2;
2069 StackPointer = "bp";
2070 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002071 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002072 } else if (nasm_stricmp(tline->text, "small") == 0) {
2073 /* All subsequent ARG directives are for a 16-bit stack,
2074 * far function call. We don't support near functions.
2075 */
2076 StackSize = 2;
2077 StackPointer = "bp";
2078 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002079 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002080 } else {
2081 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2082 free_tlist(origline);
2083 return DIRECTIVE_FOUND;
2084 }
2085 free_tlist(origline);
2086 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002087
H. Peter Anvine2c80182005-01-15 22:15:51 +00002088 case PP_ARG:
2089 /* TASM like ARG directive to define arguments to functions, in
2090 * the following form:
2091 *
2092 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2093 */
2094 offset = ArgOffset;
2095 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002096 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002097 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002098
H. Peter Anvine2c80182005-01-15 22:15:51 +00002099 /* Find the argument name */
2100 tline = tline->next;
2101 if (tline && tline->type == TOK_WHITESPACE)
2102 tline = tline->next;
2103 if (!tline || tline->type != TOK_ID) {
2104 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2105 free_tlist(origline);
2106 return DIRECTIVE_FOUND;
2107 }
2108 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002109
H. Peter Anvine2c80182005-01-15 22:15:51 +00002110 /* Find the argument size type */
2111 tline = tline->next;
2112 if (!tline || tline->type != TOK_OTHER
2113 || tline->text[0] != ':') {
2114 error(ERR_NONFATAL,
2115 "Syntax error processing `%%arg' directive");
2116 free_tlist(origline);
2117 return DIRECTIVE_FOUND;
2118 }
2119 tline = tline->next;
2120 if (!tline || tline->type != TOK_ID) {
2121 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2122 free_tlist(origline);
2123 return DIRECTIVE_FOUND;
2124 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002125
H. Peter Anvine2c80182005-01-15 22:15:51 +00002126 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002127 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002128 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002129 size = parse_size(tt->text);
2130 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002131 error(ERR_NONFATAL,
2132 "Invalid size type for `%%arg' missing directive");
2133 free_tlist(tt);
2134 free_tlist(origline);
2135 return DIRECTIVE_FOUND;
2136 }
2137 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002138
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002139 /* Round up to even stack slots */
2140 size = (size+StackSize-1) & ~(StackSize-1);
2141
H. Peter Anvine2c80182005-01-15 22:15:51 +00002142 /* Now define the macro for the argument */
2143 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2144 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002145 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002146 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002147
H. Peter Anvine2c80182005-01-15 22:15:51 +00002148 /* Move to the next argument in the list */
2149 tline = tline->next;
2150 if (tline && tline->type == TOK_WHITESPACE)
2151 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002152 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2153 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002154 free_tlist(origline);
2155 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002156
H. Peter Anvine2c80182005-01-15 22:15:51 +00002157 case PP_LOCAL:
2158 /* TASM like LOCAL directive to define local variables for a
2159 * function, in the following form:
2160 *
2161 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2162 *
2163 * The '= LocalSize' at the end is ignored by NASM, but is
2164 * required by TASM to define the local parameter size (and used
2165 * by the TASM macro package).
2166 */
2167 offset = LocalOffset;
2168 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002169 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002170 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002171
H. Peter Anvine2c80182005-01-15 22:15:51 +00002172 /* Find the argument name */
2173 tline = tline->next;
2174 if (tline && tline->type == TOK_WHITESPACE)
2175 tline = tline->next;
2176 if (!tline || tline->type != TOK_ID) {
2177 error(ERR_NONFATAL,
2178 "`%%local' missing argument parameter");
2179 free_tlist(origline);
2180 return DIRECTIVE_FOUND;
2181 }
2182 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002183
H. Peter Anvine2c80182005-01-15 22:15:51 +00002184 /* Find the argument size type */
2185 tline = tline->next;
2186 if (!tline || tline->type != TOK_OTHER
2187 || tline->text[0] != ':') {
2188 error(ERR_NONFATAL,
2189 "Syntax error processing `%%local' directive");
2190 free_tlist(origline);
2191 return DIRECTIVE_FOUND;
2192 }
2193 tline = tline->next;
2194 if (!tline || tline->type != TOK_ID) {
2195 error(ERR_NONFATAL,
2196 "`%%local' missing size type parameter");
2197 free_tlist(origline);
2198 return DIRECTIVE_FOUND;
2199 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002200
H. Peter Anvine2c80182005-01-15 22:15:51 +00002201 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002202 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002203 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002204 size = parse_size(tt->text);
2205 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002206 error(ERR_NONFATAL,
2207 "Invalid size type for `%%local' missing directive");
2208 free_tlist(tt);
2209 free_tlist(origline);
2210 return DIRECTIVE_FOUND;
2211 }
2212 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002213
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002214 /* Round up to even stack slots */
2215 size = (size+StackSize-1) & ~(StackSize-1);
2216
2217 offset += size; /* Negative offset, increment before */
2218
2219 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002220 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2221 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002222 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002223
H. Peter Anvine2c80182005-01-15 22:15:51 +00002224 /* Now define the assign to setup the enter_c macro correctly */
2225 snprintf(directive, sizeof(directive),
2226 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002227 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002228
H. Peter Anvine2c80182005-01-15 22:15:51 +00002229 /* Move to the next argument in the list */
2230 tline = tline->next;
2231 if (tline && tline->type == TOK_WHITESPACE)
2232 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002233 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2234 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002235 free_tlist(origline);
2236 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002237
H. Peter Anvine2c80182005-01-15 22:15:51 +00002238 case PP_CLEAR:
2239 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002240 error(ERR_WARNING|ERR_PASS1,
2241 "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002242 free_macros();
2243 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002244 free_tlist(origline);
2245 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002246
H. Peter Anvin418ca702008-05-30 10:42:30 -07002247 case PP_DEPEND:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002248 t = tline->next = expand_smacro(tline->next);
2249 skip_white_(t);
2250 if (!t || (t->type != TOK_STRING &&
2251 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002252 error(ERR_NONFATAL, "`%%depend' expects a file name");
2253 free_tlist(origline);
2254 return DIRECTIVE_FOUND; /* but we did _something_ */
2255 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002256 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002257 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002258 "trailing garbage after `%%depend' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002259 p = t->text;
2260 if (t->type != TOK_INTERNAL_STRING)
2261 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002262 if (dephead && !in_list(*dephead, p)) {
2263 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2264 sl->next = NULL;
2265 strcpy(sl->str, p);
2266 *deptail = sl;
2267 deptail = &sl->next;
2268 }
2269 free_tlist(origline);
2270 return DIRECTIVE_FOUND;
2271
2272 case PP_INCLUDE:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002273 t = tline->next = expand_smacro(tline->next);
2274 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002275
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002276 if (!t || (t->type != TOK_STRING &&
2277 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002278 error(ERR_NONFATAL, "`%%include' expects a file name");
2279 free_tlist(origline);
2280 return DIRECTIVE_FOUND; /* but we did _something_ */
2281 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002282 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002283 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002284 "trailing garbage after `%%include' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002285 p = t->text;
2286 if (t->type != TOK_INTERNAL_STRING)
2287 nasm_unquote(p, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002288 inc = nasm_malloc(sizeof(Include));
2289 inc->next = istk;
2290 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002291 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002292 if (!inc->fp) {
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002293 /* -MG given but file not found */
2294 nasm_free(inc);
2295 } else {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002296 inc->fname = src_set_fname(nasm_strdup(p));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002297 inc->lineno = src_set_linnum(0);
2298 inc->lineinc = 1;
2299 inc->expansion = NULL;
2300 inc->mstk = NULL;
2301 istk = inc;
2302 list->uplevel(LIST_INCLUDE);
2303 }
2304 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002305 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002306
H. Peter Anvind2456592008-06-19 15:04:18 -07002307 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002308 {
H. Peter Anvina70547f2008-07-19 21:44:26 -07002309 static macros_t *use_pkg;
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002310 const char *pkg_macro;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002311
H. Peter Anvind2456592008-06-19 15:04:18 -07002312 t = tline->next = expand_smacro(tline->next);
2313 skip_white_(t);
2314
2315 if (!t || (t->type != TOK_STRING &&
2316 t->type != TOK_INTERNAL_STRING &&
2317 t->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002318 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002319 free_tlist(origline);
2320 return DIRECTIVE_FOUND; /* but we did _something_ */
2321 }
2322 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002323 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002324 "trailing garbage after `%%use' ignored");
H. Peter Anvind2456592008-06-19 15:04:18 -07002325 if (t->type == TOK_STRING)
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002326 nasm_unquote(t->text, NULL);
2327 use_pkg = nasm_stdmac_find_package(t->text);
2328 if (!use_pkg)
2329 error(ERR_NONFATAL, "unknown `%%use' package: %s", t->text);
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002330 /* The first string will be <%define>__USE_*__ */
H. Peter Anvin7e50d232008-06-25 14:54:14 -07002331 pkg_macro = (char *)use_pkg + 1;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002332 if (!smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2333 /* Not already included, go ahead and include it */
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002334 stdmacpos = use_pkg;
2335 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002336 free_tlist(origline);
2337 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002338 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002339 case PP_PUSH:
2340 tline = tline->next;
2341 skip_white_(tline);
2342 tline = expand_id(tline);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002343 if (tline) {
2344 if (!tok_type_(tline, TOK_ID)) {
2345 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2346 free_tlist(origline);
2347 return DIRECTIVE_FOUND; /* but we did _something_ */
2348 }
2349 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002350 error(ERR_WARNING|ERR_PASS1,
2351 "trailing garbage after `%%push' ignored");
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002352 p = nasm_strdup(tline->text);
2353 } else {
2354 p = NULL; /* Anonymous context */
2355 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002356 ctx = nasm_malloc(sizeof(Context));
2357 ctx->next = cstk;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002358 hash_init(&ctx->localmac, HASH_SMALL);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002359 ctx->name = p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002360 ctx->number = unique++;
2361 cstk = ctx;
2362 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002363 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002364
H. Peter Anvine2c80182005-01-15 22:15:51 +00002365 case PP_REPL:
2366 tline = tline->next;
2367 skip_white_(tline);
2368 tline = expand_id(tline);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002369 if (tline) {
2370 if (!tok_type_(tline, TOK_ID)) {
2371 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2372 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,
2377 "trailing garbage after `%%repl' ignored");
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002378 p = nasm_strdup(tline->text);
2379 } else {
2380 p = NULL;
2381 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 if (!cstk)
2383 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2384 else {
2385 nasm_free(cstk->name);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002386 cstk->name = p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002387 }
2388 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002389 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002390
H. Peter Anvine2c80182005-01-15 22:15:51 +00002391 case PP_POP:
2392 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002393 error(ERR_WARNING|ERR_PASS1,
2394 "trailing garbage after `%%pop' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002395 if (!cstk)
2396 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2397 else
2398 ctx_pop();
2399 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002400 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002401
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002402 case PP_FATAL:
2403 severity = ERR_FATAL|ERR_NO_SEVERITY;
2404 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002405 case PP_ERROR:
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002406 severity = ERR_NONFATAL|ERR_NO_SEVERITY;
2407 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002408 case PP_WARNING:
H. Peter Anvin2f160432008-09-30 16:39:17 -07002409 severity = ERR_WARNING|ERR_NO_SEVERITY|ERR_WARN_USER;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002410 goto issue_error;
2411
2412 issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002413 {
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002414 /* Only error out if this is the final pass */
2415 if (pass != 2 && i != PP_FATAL)
2416 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002417
H. Peter Anvine2c80182005-01-15 22:15:51 +00002418 tline->next = expand_smacro(tline->next);
2419 tline = tline->next;
2420 skip_white_(tline);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002421 t = tline ? tline->next : NULL;
2422 skip_white_(t);
2423 if (tok_type_(tline, TOK_STRING) && !t) {
2424 /* The line contains only a quoted string */
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002425 p = tline->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002426 nasm_unquote(p, NULL);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002427 error(severity, "%s: %s", pp_directives[i], p);
2428 } else {
2429 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002430 p = detoken(tline, false);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002431 error(severity, "%s: %s", pp_directives[i], p);
2432 nasm_free(p);
H. Peter Anvind2456592008-06-19 15:04:18 -07002433 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002434 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002435 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002436 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002437
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002438 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002439 if (istk->conds && !emitting(istk->conds->state))
2440 j = COND_NEVER;
2441 else {
2442 j = if_condition(tline->next, i);
2443 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002444 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2445 }
2446 cond = nasm_malloc(sizeof(Cond));
2447 cond->next = istk->conds;
2448 cond->state = j;
2449 istk->conds = cond;
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002450 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002451 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002452
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002453 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002454 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002455 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002456 switch(istk->conds->state) {
2457 case COND_IF_TRUE:
2458 istk->conds->state = COND_DONE;
2459 break;
2460
2461 case COND_DONE:
2462 case COND_NEVER:
2463 break;
2464
2465 case COND_ELSE_TRUE:
2466 case COND_ELSE_FALSE:
H. Peter Anvin917a3492008-09-24 09:14:49 -07002467 error_precond(ERR_WARNING|ERR_PASS1,
2468 "`%%elif' after `%%else' ignored");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002469 istk->conds->state = COND_NEVER;
2470 break;
2471
2472 case COND_IF_FALSE:
2473 /*
2474 * IMPORTANT: In the case of %if, we will already have
2475 * called expand_mmac_params(); however, if we're
2476 * processing an %elif we must have been in a
2477 * non-emitting mode, which would have inhibited
H. Peter Anvin992fe752008-10-19 15:45:05 -07002478 * the normal invocation of expand_indirect() and
2479 * expand_mmac_params(). Therefore, we have to do it
2480 * explicitly here.
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002481 */
H. Peter Anvin992fe752008-10-19 15:45:05 -07002482 t = expand_indirect(tline->next,0);
2483 t = expand_mmac_params(t);
2484 j = if_condition(expand_mmac_params(t), i);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002485 tline->next = NULL; /* it got freed */
2486 istk->conds->state =
2487 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2488 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002489 }
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002490 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002491 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002492
H. Peter Anvine2c80182005-01-15 22:15:51 +00002493 case PP_ELSE:
2494 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002495 error_precond(ERR_WARNING|ERR_PASS1,
2496 "trailing garbage after `%%else' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002497 if (!istk->conds)
2498 error(ERR_FATAL, "`%%else': no matching `%%if'");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002499 switch(istk->conds->state) {
2500 case COND_IF_TRUE:
2501 case COND_DONE:
2502 istk->conds->state = COND_ELSE_FALSE;
2503 break;
2504
2505 case COND_NEVER:
2506 break;
2507
2508 case COND_IF_FALSE:
2509 istk->conds->state = COND_ELSE_TRUE;
2510 break;
2511
2512 case COND_ELSE_TRUE:
2513 case COND_ELSE_FALSE:
H. Peter Anvin917a3492008-09-24 09:14:49 -07002514 error_precond(ERR_WARNING|ERR_PASS1,
2515 "`%%else' after `%%else' ignored.");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002516 istk->conds->state = COND_NEVER;
2517 break;
2518 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002519 free_tlist(origline);
2520 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002521
H. Peter Anvine2c80182005-01-15 22:15:51 +00002522 case PP_ENDIF:
2523 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002524 error_precond(ERR_WARNING|ERR_PASS1,
2525 "trailing garbage after `%%endif' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002526 if (!istk->conds)
2527 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2528 cond = istk->conds;
2529 istk->conds = cond->next;
2530 nasm_free(cond);
2531 free_tlist(origline);
2532 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002533
H. Peter Anvine2c80182005-01-15 22:15:51 +00002534 case PP_MACRO:
2535 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002536 if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002537 error(ERR_FATAL,
2538 "`%%%smacro': already defining a macro",
2539 (i == PP_IMACRO ? "i" : ""));
H. Peter Anvina26433d2008-07-16 14:40:01 -07002540 return DIRECTIVE_FOUND;
2541 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002542 defining = nasm_malloc(sizeof(MMacro));
H. Peter Anvina26433d2008-07-16 14:40:01 -07002543 defining->casesense = (i == PP_MACRO);
2544 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2545 nasm_free(defining);
2546 defining = NULL;
2547 return DIRECTIVE_FOUND;
2548 }
2549
H. Peter Anvin166c2472008-05-28 12:28:58 -07002550 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002551 while (mmac) {
2552 if (!strcmp(mmac->name, defining->name) &&
2553 (mmac->nparam_min <= defining->nparam_max
2554 || defining->plus)
2555 && (defining->nparam_min <= mmac->nparam_max
2556 || mmac->plus)) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002557 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002558 "redefining multi-line macro `%s'", defining->name);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002559 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002560 }
2561 mmac = mmac->next;
2562 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002563 free_tlist(origline);
2564 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002565
H. Peter Anvine2c80182005-01-15 22:15:51 +00002566 case PP_ENDM:
2567 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002568 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002569 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2570 return DIRECTIVE_FOUND;
2571 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002572 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002573 defining->next = *mmhead;
2574 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002575 defining = NULL;
2576 free_tlist(origline);
2577 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002578
H. Peter Anvina26433d2008-07-16 14:40:01 -07002579 case PP_UNMACRO:
2580 case PP_UNIMACRO:
2581 {
2582 MMacro **mmac_p;
2583 MMacro spec;
2584
2585 spec.casesense = (i == PP_UNMACRO);
2586 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2587 return DIRECTIVE_FOUND;
2588 }
2589 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2590 while (mmac_p && *mmac_p) {
2591 mmac = *mmac_p;
2592 if (mmac->casesense == spec.casesense &&
2593 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2594 mmac->nparam_min == spec.nparam_min &&
2595 mmac->nparam_max == spec.nparam_max &&
2596 mmac->plus == spec.plus) {
2597 *mmac_p = mmac->next;
2598 free_mmacro(mmac);
2599 } else {
2600 mmac_p = &mmac->next;
2601 }
2602 }
2603 free_tlist(origline);
2604 free_tlist(spec.dlist);
2605 return DIRECTIVE_FOUND;
2606 }
2607
H. Peter Anvine2c80182005-01-15 22:15:51 +00002608 case PP_ROTATE:
2609 if (tline->next && tline->next->type == TOK_WHITESPACE)
2610 tline = tline->next;
2611 if (tline->next == NULL) {
2612 free_tlist(origline);
2613 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2614 return DIRECTIVE_FOUND;
2615 }
2616 t = expand_smacro(tline->next);
2617 tline->next = NULL;
2618 free_tlist(origline);
2619 tline = t;
2620 tptr = &t;
2621 tokval.t_type = TOKEN_INVALID;
2622 evalresult =
2623 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2624 free_tlist(tline);
2625 if (!evalresult)
2626 return DIRECTIVE_FOUND;
2627 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002628 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002629 "trailing garbage after expression ignored");
2630 if (!is_simple(evalresult)) {
2631 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2632 return DIRECTIVE_FOUND;
2633 }
2634 mmac = istk->mstk;
2635 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2636 mmac = mmac->next_active;
2637 if (!mmac) {
2638 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2639 } else if (mmac->nparam == 0) {
2640 error(ERR_NONFATAL,
2641 "`%%rotate' invoked within macro without parameters");
2642 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002643 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002644
H. Peter Anvin25a99342007-09-22 17:45:45 -07002645 rotate %= (int)mmac->nparam;
2646 if (rotate < 0)
2647 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002648
H. Peter Anvin25a99342007-09-22 17:45:45 -07002649 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002650 }
2651 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002652
H. Peter Anvine2c80182005-01-15 22:15:51 +00002653 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002654 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002655 do {
2656 tline = tline->next;
2657 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002658
H. Peter Anvine2c80182005-01-15 22:15:51 +00002659 if (tok_type_(tline, TOK_ID) &&
2660 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002661 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002662 do {
2663 tline = tline->next;
2664 } while (tok_type_(tline, TOK_WHITESPACE));
2665 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002666
H. Peter Anvine2c80182005-01-15 22:15:51 +00002667 if (tline) {
2668 t = expand_smacro(tline);
2669 tptr = &t;
2670 tokval.t_type = TOKEN_INVALID;
2671 evalresult =
2672 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2673 if (!evalresult) {
2674 free_tlist(origline);
2675 return DIRECTIVE_FOUND;
2676 }
2677 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002678 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002679 "trailing garbage after expression ignored");
2680 if (!is_simple(evalresult)) {
2681 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2682 return DIRECTIVE_FOUND;
2683 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002684 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002685 } else {
2686 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002687 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002688 }
2689 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002690
H. Peter Anvine2c80182005-01-15 22:15:51 +00002691 tmp_defining = defining;
2692 defining = nasm_malloc(sizeof(MMacro));
2693 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002694 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002695 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002696 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002697 defining->in_progress = count;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002698 defining->nparam_min = defining->nparam_max = 0;
2699 defining->defaults = NULL;
2700 defining->dlist = NULL;
2701 defining->expansion = NULL;
2702 defining->next_active = istk->mstk;
2703 defining->rep_nest = tmp_defining;
2704 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002705
H. Peter Anvine2c80182005-01-15 22:15:51 +00002706 case PP_ENDREP:
2707 if (!defining || defining->name) {
2708 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2709 return DIRECTIVE_FOUND;
2710 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002711
H. Peter Anvine2c80182005-01-15 22:15:51 +00002712 /*
2713 * Now we have a "macro" defined - although it has no name
2714 * and we won't be entering it in the hash tables - we must
2715 * push a macro-end marker for it on to istk->expansion.
2716 * After that, it will take care of propagating itself (a
2717 * macro-end marker line for a macro which is really a %rep
2718 * block will cause the macro to be re-expanded, complete
2719 * with another macro-end marker to ensure the process
2720 * continues) until the whole expansion is forcibly removed
2721 * from istk->expansion by a %exitrep.
2722 */
2723 l = nasm_malloc(sizeof(Line));
2724 l->next = istk->expansion;
2725 l->finishes = defining;
2726 l->first = NULL;
2727 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002728
H. Peter Anvine2c80182005-01-15 22:15:51 +00002729 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002730
H. Peter Anvine2c80182005-01-15 22:15:51 +00002731 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2732 tmp_defining = defining;
2733 defining = defining->rep_nest;
2734 free_tlist(origline);
2735 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002736
H. Peter Anvine2c80182005-01-15 22:15:51 +00002737 case PP_EXITREP:
2738 /*
2739 * We must search along istk->expansion until we hit a
2740 * macro-end marker for a macro with no name. Then we set
2741 * its `in_progress' flag to 0.
2742 */
2743 for (l = istk->expansion; l; l = l->next)
2744 if (l->finishes && !l->finishes->name)
H. Peter Anvinca348b62008-07-23 10:49:26 -04002745 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002746
H. Peter Anvine2c80182005-01-15 22:15:51 +00002747 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07002748 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002749 else
2750 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2751 free_tlist(origline);
2752 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002753
H. Peter Anvine2c80182005-01-15 22:15:51 +00002754 case PP_XDEFINE:
2755 case PP_IXDEFINE:
2756 case PP_DEFINE:
2757 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002758 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002759
H. Peter Anvine2c80182005-01-15 22:15:51 +00002760 tline = tline->next;
2761 skip_white_(tline);
2762 tline = expand_id(tline);
2763 if (!tline || (tline->type != TOK_ID &&
2764 (tline->type != TOK_PREPROC_ID ||
2765 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002766 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2767 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002768 free_tlist(origline);
2769 return DIRECTIVE_FOUND;
2770 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002771
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002772 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002773
H. Peter Anvine2c80182005-01-15 22:15:51 +00002774 mname = tline->text;
2775 last = tline;
2776 param_start = tline = tline->next;
2777 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002778
H. Peter Anvine2c80182005-01-15 22:15:51 +00002779 /* Expand the macro definition now for %xdefine and %ixdefine */
2780 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2781 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002782
H. Peter Anvine2c80182005-01-15 22:15:51 +00002783 if (tok_is_(tline, "(")) {
2784 /*
2785 * This macro has parameters.
2786 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002787
H. Peter Anvine2c80182005-01-15 22:15:51 +00002788 tline = tline->next;
2789 while (1) {
2790 skip_white_(tline);
2791 if (!tline) {
2792 error(ERR_NONFATAL, "parameter identifier expected");
2793 free_tlist(origline);
2794 return DIRECTIVE_FOUND;
2795 }
2796 if (tline->type != TOK_ID) {
2797 error(ERR_NONFATAL,
2798 "`%s': parameter identifier expected",
2799 tline->text);
2800 free_tlist(origline);
2801 return DIRECTIVE_FOUND;
2802 }
2803 tline->type = TOK_SMAC_PARAM + nparam++;
2804 tline = tline->next;
2805 skip_white_(tline);
2806 if (tok_is_(tline, ",")) {
2807 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04002808 } else {
2809 if (!tok_is_(tline, ")")) {
2810 error(ERR_NONFATAL,
2811 "`)' expected to terminate macro template");
2812 free_tlist(origline);
2813 return DIRECTIVE_FOUND;
2814 }
2815 break;
2816 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002817 }
2818 last = tline;
2819 tline = tline->next;
2820 }
2821 if (tok_type_(tline, TOK_WHITESPACE))
2822 last = tline, tline = tline->next;
2823 macro_start = NULL;
2824 last->next = NULL;
2825 t = tline;
2826 while (t) {
2827 if (t->type == TOK_ID) {
2828 for (tt = param_start; tt; tt = tt->next)
2829 if (tt->type >= TOK_SMAC_PARAM &&
2830 !strcmp(tt->text, t->text))
2831 t->type = tt->type;
2832 }
2833 tt = t->next;
2834 t->next = macro_start;
2835 macro_start = t;
2836 t = tt;
2837 }
2838 /*
2839 * Good. We now have a macro name, a parameter count, and a
2840 * token list (in reverse order) for an expansion. We ought
2841 * to be OK just to create an SMacro, store it, and let
2842 * free_tlist have the rest of the line (which we have
2843 * carefully re-terminated after chopping off the expansion
2844 * from the end).
2845 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002846 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002847 free_tlist(origline);
2848 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002849
H. Peter Anvine2c80182005-01-15 22:15:51 +00002850 case PP_UNDEF:
2851 tline = tline->next;
2852 skip_white_(tline);
2853 tline = expand_id(tline);
2854 if (!tline || (tline->type != TOK_ID &&
2855 (tline->type != TOK_PREPROC_ID ||
2856 tline->text[1] != '$'))) {
2857 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2858 free_tlist(origline);
2859 return DIRECTIVE_FOUND;
2860 }
2861 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002862 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002863 "trailing garbage after macro name ignored");
2864 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002865
H. Peter Anvine2c80182005-01-15 22:15:51 +00002866 /* Find the context that symbol belongs to */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002867 ctx = get_ctx(tline->text, false);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002868 undef_smacro(ctx, tline->text);
2869 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002870 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002871
H. Peter Anvin9e200162008-06-04 17:23:14 -07002872 case PP_DEFSTR:
2873 case PP_IDEFSTR:
2874 casesense = (i == PP_DEFSTR);
2875
2876 tline = tline->next;
2877 skip_white_(tline);
2878 tline = expand_id(tline);
2879 if (!tline || (tline->type != TOK_ID &&
2880 (tline->type != TOK_PREPROC_ID ||
2881 tline->text[1] != '$'))) {
2882 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2883 pp_directives[i]);
2884 free_tlist(origline);
2885 return DIRECTIVE_FOUND;
2886 }
2887
2888 ctx = get_ctx(tline->text, false);
2889
2890 mname = tline->text;
2891 last = tline;
2892 tline = expand_smacro(tline->next);
2893 last->next = NULL;
2894
2895 while (tok_type_(tline, TOK_WHITESPACE))
2896 tline = delete_Token(tline);
2897
2898 p = detoken(tline, false);
2899 macro_start = nasm_malloc(sizeof(*macro_start));
2900 macro_start->next = NULL;
2901 macro_start->text = nasm_quote(p, strlen(p));
2902 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002903 macro_start->a.mac = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07002904 nasm_free(p);
2905
2906 /*
2907 * We now have a macro name, an implicit parameter count of
2908 * zero, and a string token to use as an expansion. Create
2909 * and store an SMacro.
2910 */
2911 define_smacro(ctx, mname, casesense, 0, macro_start);
2912 free_tlist(origline);
2913 return DIRECTIVE_FOUND;
2914
H. Peter Anvin418ca702008-05-30 10:42:30 -07002915 case PP_PATHSEARCH:
2916 {
2917 FILE *fp;
2918 StrList *xsl = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002919 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002920
2921 casesense = true;
2922
2923 tline = tline->next;
2924 skip_white_(tline);
2925 tline = expand_id(tline);
2926 if (!tline || (tline->type != TOK_ID &&
2927 (tline->type != TOK_PREPROC_ID ||
2928 tline->text[1] != '$'))) {
2929 error(ERR_NONFATAL,
2930 "`%%pathsearch' expects a macro identifier as first parameter");
2931 free_tlist(origline);
2932 return DIRECTIVE_FOUND;
2933 }
2934 ctx = get_ctx(tline->text, false);
2935
2936 mname = tline->text;
2937 last = tline;
2938 tline = expand_smacro(tline->next);
2939 last->next = NULL;
2940
2941 t = tline;
2942 while (tok_type_(t, TOK_WHITESPACE))
2943 t = t->next;
2944
2945 if (!t || (t->type != TOK_STRING &&
2946 t->type != TOK_INTERNAL_STRING)) {
2947 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
2948 free_tlist(tline);
2949 free_tlist(origline);
2950 return DIRECTIVE_FOUND; /* but we did _something_ */
2951 }
2952 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002953 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002954 "trailing garbage after `%%pathsearch' ignored");
H. Peter Anvin427cc912008-06-01 21:43:03 -07002955 p = t->text;
2956 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002957 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002958
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002959 fp = inc_fopen(p, &xsl, &xst, true);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002960 if (fp) {
2961 p = xsl->str;
2962 fclose(fp); /* Don't actually care about the file */
2963 }
2964 macro_start = nasm_malloc(sizeof(*macro_start));
2965 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002966 macro_start->text = nasm_quote(p, strlen(p));
H. Peter Anvin418ca702008-05-30 10:42:30 -07002967 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002968 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002969 if (xsl)
2970 nasm_free(xsl);
2971
2972 /*
2973 * We now have a macro name, an implicit parameter count of
2974 * zero, and a string token to use as an expansion. Create
2975 * and store an SMacro.
2976 */
2977 define_smacro(ctx, mname, casesense, 0, macro_start);
2978 free_tlist(tline);
2979 free_tlist(origline);
2980 return DIRECTIVE_FOUND;
2981 }
2982
H. Peter Anvine2c80182005-01-15 22:15:51 +00002983 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002984 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07002985
H. Peter Anvine2c80182005-01-15 22:15:51 +00002986 tline = tline->next;
2987 skip_white_(tline);
2988 tline = expand_id(tline);
2989 if (!tline || (tline->type != TOK_ID &&
2990 (tline->type != TOK_PREPROC_ID ||
2991 tline->text[1] != '$'))) {
2992 error(ERR_NONFATAL,
2993 "`%%strlen' expects a macro identifier as first parameter");
2994 free_tlist(origline);
2995 return DIRECTIVE_FOUND;
2996 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002997 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002998
H. Peter Anvine2c80182005-01-15 22:15:51 +00002999 mname = tline->text;
3000 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 }
3045 ctx = get_ctx(tline->text, false);
3046
3047 mname = tline->text;
3048 last = tline;
3049 tline = expand_smacro(tline->next);
3050 last->next = NULL;
3051
3052 len = 0;
3053 for (t = tline; t; t = t->next) {
3054 switch (t->type) {
3055 case TOK_WHITESPACE:
3056 break;
3057 case TOK_STRING:
3058 len += t->a.len = nasm_unquote(t->text, NULL);
3059 break;
H. Peter Anvinaccf4332008-07-01 21:42:08 -07003060 case TOK_OTHER:
3061 if (!strcmp(t->text, ",")) /* permit comma separators */
3062 break;
3063 /* else fall through */
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003064 default:
3065 error(ERR_NONFATAL,
3066 "non-string passed to `%%strcat' (%d)", t->type);
3067 free_tlist(tline);
3068 free_tlist(origline);
3069 return DIRECTIVE_FOUND;
3070 }
3071 }
3072
3073 p = pp = nasm_malloc(len);
3074 t = tline;
3075 for (t = tline; t; t = t->next) {
3076 if (t->type == TOK_STRING) {
3077 memcpy(p, t->text, t->a.len);
3078 p += t->a.len;
3079 }
3080 }
3081
3082 /*
3083 * We now have a macro name, an implicit parameter count of
3084 * zero, and a numeric token to use as an expansion. Create
3085 * and store an SMacro.
3086 */
3087 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3088 macro_start->text = nasm_quote(pp, len);
3089 nasm_free(pp);
3090 define_smacro(ctx, mname, casesense, 0, macro_start);
3091 free_tlist(tline);
3092 free_tlist(origline);
3093 return DIRECTIVE_FOUND;
3094
H. Peter Anvine2c80182005-01-15 22:15:51 +00003095 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003096 {
3097 int64_t a1, a2;
3098 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003099
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003100 casesense = true;
3101
H. Peter Anvine2c80182005-01-15 22:15:51 +00003102 tline = tline->next;
3103 skip_white_(tline);
3104 tline = expand_id(tline);
3105 if (!tline || (tline->type != TOK_ID &&
3106 (tline->type != TOK_PREPROC_ID ||
3107 tline->text[1] != '$'))) {
3108 error(ERR_NONFATAL,
3109 "`%%substr' expects a macro identifier as first parameter");
3110 free_tlist(origline);
3111 return DIRECTIVE_FOUND;
3112 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003113 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003114
H. Peter Anvine2c80182005-01-15 22:15:51 +00003115 mname = tline->text;
3116 last = tline;
3117 tline = expand_smacro(tline->next);
3118 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003119
H. Peter Anvine2c80182005-01-15 22:15:51 +00003120 t = tline->next;
3121 while (tok_type_(t, TOK_WHITESPACE))
3122 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003123
H. Peter Anvine2c80182005-01-15 22:15:51 +00003124 /* t should now point to the string */
3125 if (t->type != TOK_STRING) {
3126 error(ERR_NONFATAL,
3127 "`%%substr` requires string as second parameter");
3128 free_tlist(tline);
3129 free_tlist(origline);
3130 return DIRECTIVE_FOUND;
3131 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003132
H. Peter Anvine2c80182005-01-15 22:15:51 +00003133 tt = t->next;
3134 tptr = &tt;
3135 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003136 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3137 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003138 if (!evalresult) {
3139 free_tlist(tline);
3140 free_tlist(origline);
3141 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003142 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003143 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3144 free_tlist(tline);
3145 free_tlist(origline);
3146 return DIRECTIVE_FOUND;
3147 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003148 a1 = evalresult->value-1;
3149
3150 while (tok_type_(tt, TOK_WHITESPACE))
3151 tt = tt->next;
3152 if (!tt) {
3153 a2 = 1; /* Backwards compatibility: one character */
3154 } else {
3155 tokval.t_type = TOKEN_INVALID;
3156 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3157 pass, error, NULL);
3158 if (!evalresult) {
3159 free_tlist(tline);
3160 free_tlist(origline);
3161 return DIRECTIVE_FOUND;
3162 } else if (!is_simple(evalresult)) {
3163 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3164 free_tlist(tline);
3165 free_tlist(origline);
3166 return DIRECTIVE_FOUND;
3167 }
3168 a2 = evalresult->value;
3169 }
3170
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003171 len = nasm_unquote(t->text, NULL);
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003172 if (a2 < 0)
3173 a2 = a2+1+len-a1;
3174 if (a1+a2 > (int64_t)len)
3175 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003176
H. Peter Anvine2c80182005-01-15 22:15:51 +00003177 macro_start = nasm_malloc(sizeof(*macro_start));
3178 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003179 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003180 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003181 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003182
H. Peter Anvine2c80182005-01-15 22:15:51 +00003183 /*
3184 * We now have a macro name, an implicit parameter count of
3185 * zero, and a numeric token to use as an expansion. Create
3186 * and store an SMacro.
3187 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003188 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003189 free_tlist(tline);
3190 free_tlist(origline);
3191 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003192 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003193
H. Peter Anvine2c80182005-01-15 22:15:51 +00003194 case PP_ASSIGN:
3195 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003196 casesense = (i == PP_ASSIGN);
3197
H. Peter Anvine2c80182005-01-15 22:15:51 +00003198 tline = tline->next;
3199 skip_white_(tline);
3200 tline = expand_id(tline);
3201 if (!tline || (tline->type != TOK_ID &&
3202 (tline->type != TOK_PREPROC_ID ||
3203 tline->text[1] != '$'))) {
3204 error(ERR_NONFATAL,
3205 "`%%%sassign' expects a macro identifier",
3206 (i == PP_IASSIGN ? "i" : ""));
3207 free_tlist(origline);
3208 return DIRECTIVE_FOUND;
3209 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003210 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003211
H. Peter Anvine2c80182005-01-15 22:15:51 +00003212 mname = tline->text;
3213 last = tline;
3214 tline = expand_smacro(tline->next);
3215 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003216
H. Peter Anvine2c80182005-01-15 22:15:51 +00003217 t = tline;
3218 tptr = &t;
3219 tokval.t_type = TOKEN_INVALID;
3220 evalresult =
3221 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3222 free_tlist(tline);
3223 if (!evalresult) {
3224 free_tlist(origline);
3225 return DIRECTIVE_FOUND;
3226 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003227
H. Peter Anvine2c80182005-01-15 22:15:51 +00003228 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003229 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003230 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003231
H. Peter Anvine2c80182005-01-15 22:15:51 +00003232 if (!is_simple(evalresult)) {
3233 error(ERR_NONFATAL,
3234 "non-constant value given to `%%%sassign'",
3235 (i == PP_IASSIGN ? "i" : ""));
3236 free_tlist(origline);
3237 return DIRECTIVE_FOUND;
3238 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003239
H. Peter Anvine2c80182005-01-15 22:15:51 +00003240 macro_start = nasm_malloc(sizeof(*macro_start));
3241 macro_start->next = NULL;
3242 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003243 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003244
H. Peter Anvine2c80182005-01-15 22:15:51 +00003245 /*
3246 * We now have a macro name, an implicit parameter count of
3247 * zero, and a numeric token to use as an expansion. Create
3248 * and store an SMacro.
3249 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003250 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003251 free_tlist(origline);
3252 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003253
H. Peter Anvine2c80182005-01-15 22:15:51 +00003254 case PP_LINE:
3255 /*
3256 * Syntax is `%line nnn[+mmm] [filename]'
3257 */
3258 tline = tline->next;
3259 skip_white_(tline);
3260 if (!tok_type_(tline, TOK_NUMBER)) {
3261 error(ERR_NONFATAL, "`%%line' expects line number");
3262 free_tlist(origline);
3263 return DIRECTIVE_FOUND;
3264 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003265 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003266 m = 1;
3267 tline = tline->next;
3268 if (tok_is_(tline, "+")) {
3269 tline = tline->next;
3270 if (!tok_type_(tline, TOK_NUMBER)) {
3271 error(ERR_NONFATAL, "`%%line' expects line increment");
3272 free_tlist(origline);
3273 return DIRECTIVE_FOUND;
3274 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003275 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003276 tline = tline->next;
3277 }
3278 skip_white_(tline);
3279 src_set_linnum(k);
3280 istk->lineinc = m;
3281 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003282 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003283 }
3284 free_tlist(origline);
3285 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003286
H. Peter Anvine2c80182005-01-15 22:15:51 +00003287 default:
3288 error(ERR_FATAL,
3289 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003290 pp_directives[i]);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003291 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003292 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003293}
3294
3295/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003296 * Ensure that a macro parameter contains a condition code and
3297 * nothing else. Return the condition code index if so, or -1
3298 * otherwise.
3299 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003300static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003301{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003302 Token *tt;
3303 int i, j, k, m;
3304
H. Peter Anvin25a99342007-09-22 17:45:45 -07003305 if (!t)
3306 return -1; /* Probably a %+ without a space */
3307
H. Peter Anvineba20a72002-04-30 20:53:55 +00003308 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003309 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003310 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003311 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003312 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003313 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003314 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003315
3316 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00003317 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003318 while (j - i > 1) {
3319 k = (j + i) / 2;
3320 m = nasm_stricmp(t->text, conditions[k]);
3321 if (m == 0) {
3322 i = k;
3323 j = -2;
3324 break;
3325 } else if (m < 0) {
3326 j = k;
3327 } else
3328 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003329 }
3330 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003331 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003332 return i;
3333}
3334
3335/*
3336 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3337 * %-n) and MMacro-local identifiers (%%foo).
3338 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003339static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003340{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003341 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003342
3343 tail = &thead;
3344 thead = NULL;
3345
H. Peter Anvine2c80182005-01-15 22:15:51 +00003346 while (tline) {
3347 if (tline->type == TOK_PREPROC_ID &&
3348 (((tline->text[1] == '+' || tline->text[1] == '-')
3349 && tline->text[2]) || tline->text[1] == '%'
3350 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003351 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003352 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003353 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003354 unsigned int n;
3355 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003356 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003357
H. Peter Anvine2c80182005-01-15 22:15:51 +00003358 t = tline;
3359 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003360
H. Peter Anvine2c80182005-01-15 22:15:51 +00003361 mac = istk->mstk;
3362 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3363 mac = mac->next_active;
3364 if (!mac)
3365 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3366 else
3367 switch (t->text[1]) {
3368 /*
3369 * We have to make a substitution of one of the
3370 * forms %1, %-1, %+1, %%foo, %0.
3371 */
3372 case '0':
3373 type = TOK_NUMBER;
3374 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3375 text = nasm_strdup(tmpbuf);
3376 break;
3377 case '%':
3378 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003379 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00003380 mac->unique);
3381 text = nasm_strcat(tmpbuf, t->text + 2);
3382 break;
3383 case '-':
3384 n = atoi(t->text + 2) - 1;
3385 if (n >= mac->nparam)
3386 tt = NULL;
3387 else {
3388 if (mac->nparam > 1)
3389 n = (n + mac->rotate) % mac->nparam;
3390 tt = mac->params[n];
3391 }
3392 cc = find_cc(tt);
3393 if (cc == -1) {
3394 error(ERR_NONFATAL,
3395 "macro parameter %d is not a condition code",
3396 n + 1);
3397 text = NULL;
3398 } else {
3399 type = TOK_ID;
3400 if (inverse_ccs[cc] == -1) {
3401 error(ERR_NONFATAL,
3402 "condition code `%s' is not invertible",
3403 conditions[cc]);
3404 text = NULL;
3405 } else
3406 text =
3407 nasm_strdup(conditions[inverse_ccs[cc]]);
3408 }
3409 break;
3410 case '+':
3411 n = atoi(t->text + 2) - 1;
3412 if (n >= mac->nparam)
3413 tt = NULL;
3414 else {
3415 if (mac->nparam > 1)
3416 n = (n + mac->rotate) % mac->nparam;
3417 tt = mac->params[n];
3418 }
3419 cc = find_cc(tt);
3420 if (cc == -1) {
3421 error(ERR_NONFATAL,
3422 "macro parameter %d is not a condition code",
3423 n + 1);
3424 text = NULL;
3425 } else {
3426 type = TOK_ID;
3427 text = nasm_strdup(conditions[cc]);
3428 }
3429 break;
3430 default:
3431 n = atoi(t->text + 1) - 1;
3432 if (n >= mac->nparam)
3433 tt = NULL;
3434 else {
3435 if (mac->nparam > 1)
3436 n = (n + mac->rotate) % mac->nparam;
3437 tt = mac->params[n];
3438 }
3439 if (tt) {
3440 for (i = 0; i < mac->paramlen[n]; i++) {
3441 *tail = new_Token(NULL, tt->type, tt->text, 0);
3442 tail = &(*tail)->next;
3443 tt = tt->next;
3444 }
3445 }
3446 text = NULL; /* we've done it here */
3447 break;
3448 }
3449 if (!text) {
3450 delete_Token(t);
3451 } else {
3452 *tail = t;
3453 tail = &t->next;
3454 t->type = type;
3455 nasm_free(t->text);
3456 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003457 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003458 }
3459 continue;
3460 } else {
3461 t = *tail = tline;
3462 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003463 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003464 tail = &t->next;
3465 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003466 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003467 *tail = NULL;
3468 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003469 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003470 switch (t->type) {
3471 case TOK_WHITESPACE:
3472 if (tt->type == TOK_WHITESPACE) {
3473 t->next = delete_Token(tt);
3474 }
3475 break;
3476 case TOK_ID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003477 case TOK_NUMBER:
H. Peter Anvin992fe752008-10-19 15:45:05 -07003478 if (tt->type == t->type || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003479 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003480 nasm_free(t->text);
3481 t->text = tmp;
3482 t->next = delete_Token(tt);
3483 }
3484 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003485 default:
3486 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003487 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003488
H. Peter Anvin76690a12002-04-30 20:52:49 +00003489 return thead;
3490}
3491
3492/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003493 * Expand all single-line macro calls made in the given line.
3494 * Return the expanded version of the line. The original is deemed
3495 * to be destroyed in the process. (In reality we'll just move
3496 * Tokens from input to output a lot of the time, rather than
3497 * actually bothering to destroy and replicate.)
3498 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003499#define DEADMAN_LIMIT (1 << 20)
3500
H. Peter Anvine2c80182005-01-15 22:15:51 +00003501static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003502{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003503 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003504 struct hash_table *smtbl;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003505 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003506 Token **params;
3507 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003508 unsigned int nparam, sparam;
3509 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003510 Token *org_tline = tline;
3511 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003512 char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003513 int deadman = DEADMAN_LIMIT;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003514
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003515 /*
3516 * Trick: we should avoid changing the start token pointer since it can
3517 * be contained in "next" field of other token. Because of this
3518 * we allocate a copy of first token and work with it; at the end of
3519 * routine we copy it back
3520 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003521 if (org_tline) {
3522 tline =
3523 new_Token(org_tline->next, org_tline->type, org_tline->text,
3524 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003525 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003526 nasm_free(org_tline->text);
3527 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003528 }
3529
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003530again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003531 tail = &thead;
3532 thead = NULL;
3533
H. Peter Anvine2c80182005-01-15 22:15:51 +00003534 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003535 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003536 error(ERR_NONFATAL, "interminable macro recursion");
3537 break;
3538 }
3539
H. Peter Anvine2c80182005-01-15 22:15:51 +00003540 if ((mname = tline->text)) {
3541 /* if this token is a local macro, look in local context */
H. Peter Anvinb037a672008-05-29 19:08:08 -07003542 ctx = NULL;
3543 smtbl = &smacros;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003544 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003545 ctx = get_ctx(mname, true);
H. Peter Anvinb037a672008-05-29 19:08:08 -07003546 if (ctx)
3547 smtbl = &ctx->localmac;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003548 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07003549 head = (SMacro *) hash_findix(smtbl, mname);
H. Peter Anvin072771e2008-05-22 13:17:51 -07003550
H. Peter Anvine2c80182005-01-15 22:15:51 +00003551 /*
3552 * We've hit an identifier. As in is_mmacro below, we first
3553 * check whether the identifier is a single-line macro at
3554 * all, then think about checking for parameters if
3555 * necessary.
3556 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003557 for (m = head; m; m = m->next)
3558 if (!mstrcmp(m->name, mname, m->casesense))
3559 break;
3560 if (m) {
3561 mstart = tline;
3562 params = NULL;
3563 paramsize = NULL;
3564 if (m->nparam == 0) {
3565 /*
3566 * Simple case: the macro is parameterless. Discard the
3567 * one token that the macro call took, and push the
3568 * expansion back on the to-do stack.
3569 */
3570 if (!m->expansion) {
3571 if (!strcmp("__FILE__", m->name)) {
3572 int32_t num = 0;
H. Peter Anvin932de6c2008-07-31 18:46:11 -07003573 char *file = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003574 src_get(&num, &file);
3575 tline->text = nasm_quote(file, strlen(file));
H. Peter Anvin97a23472007-09-16 17:57:25 -07003576 tline->type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003577 nasm_free(file);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003578 continue;
3579 }
3580 if (!strcmp("__LINE__", m->name)) {
3581 nasm_free(tline->text);
3582 make_tok_num(tline, src_get_linnum());
3583 continue;
3584 }
3585 if (!strcmp("__BITS__", m->name)) {
3586 nasm_free(tline->text);
3587 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003588 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003589 }
3590 tline = delete_Token(tline);
3591 continue;
3592 }
3593 } else {
3594 /*
3595 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003596 * exists and takes parameters. We must find the
3597 * parameters in the call, count them, find the SMacro
3598 * that corresponds to that form of the macro call, and
3599 * substitute for the parameters when we expand. What a
3600 * pain.
3601 */
3602 /*tline = tline->next;
3603 skip_white_(tline); */
3604 do {
3605 t = tline->next;
3606 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003607 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003608 t->text = NULL;
3609 t = tline->next = delete_Token(t);
3610 }
3611 tline = t;
3612 } while (tok_type_(tline, TOK_WHITESPACE));
3613 if (!tok_is_(tline, "(")) {
3614 /*
3615 * This macro wasn't called with parameters: ignore
3616 * the call. (Behaviour borrowed from gnu cpp.)
3617 */
3618 tline = mstart;
3619 m = NULL;
3620 } else {
3621 int paren = 0;
3622 int white = 0;
3623 brackets = 0;
3624 nparam = 0;
3625 sparam = PARAM_DELTA;
3626 params = nasm_malloc(sparam * sizeof(Token *));
3627 params[0] = tline->next;
3628 paramsize = nasm_malloc(sparam * sizeof(int));
3629 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003630 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003631 /*
3632 * For some unusual expansions
3633 * which concatenates function call
3634 */
3635 t = tline->next;
3636 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003637 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003638 t->text = NULL;
3639 t = tline->next = delete_Token(t);
3640 }
3641 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003642
H. Peter Anvine2c80182005-01-15 22:15:51 +00003643 if (!tline) {
3644 error(ERR_NONFATAL,
3645 "macro call expects terminating `)'");
3646 break;
3647 }
3648 if (tline->type == TOK_WHITESPACE
3649 && brackets <= 0) {
3650 if (paramsize[nparam])
3651 white++;
3652 else
3653 params[nparam] = tline->next;
3654 continue; /* parameter loop */
3655 }
3656 if (tline->type == TOK_OTHER
3657 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003658 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003659 if (ch == ',' && !paren && brackets <= 0) {
3660 if (++nparam >= sparam) {
3661 sparam += PARAM_DELTA;
3662 params = nasm_realloc(params,
3663 sparam *
3664 sizeof(Token
3665 *));
3666 paramsize =
3667 nasm_realloc(paramsize,
3668 sparam *
3669 sizeof(int));
3670 }
3671 params[nparam] = tline->next;
3672 paramsize[nparam] = 0;
3673 white = 0;
3674 continue; /* parameter loop */
3675 }
3676 if (ch == '{' &&
3677 (brackets > 0 || (brackets == 0 &&
3678 !paramsize[nparam])))
3679 {
3680 if (!(brackets++)) {
3681 params[nparam] = tline->next;
3682 continue; /* parameter loop */
3683 }
3684 }
3685 if (ch == '}' && brackets > 0)
3686 if (--brackets == 0) {
3687 brackets = -1;
3688 continue; /* parameter loop */
3689 }
3690 if (ch == '(' && !brackets)
3691 paren++;
3692 if (ch == ')' && brackets <= 0)
3693 if (--paren < 0)
3694 break;
3695 }
3696 if (brackets < 0) {
3697 brackets = 0;
3698 error(ERR_NONFATAL, "braces do not "
3699 "enclose all of macro parameter");
3700 }
3701 paramsize[nparam] += white + 1;
3702 white = 0;
3703 } /* parameter loop */
3704 nparam++;
3705 while (m && (m->nparam != nparam ||
3706 mstrcmp(m->name, mname,
3707 m->casesense)))
3708 m = m->next;
3709 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003710 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003711 "macro `%s' exists, "
3712 "but not taking %d parameters",
3713 mstart->text, nparam);
3714 }
3715 }
3716 if (m && m->in_progress)
3717 m = NULL;
3718 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003719 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003720 * Design question: should we handle !tline, which
3721 * indicates missing ')' here, or expand those
3722 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003723 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003724 */
3725 nasm_free(params);
3726 nasm_free(paramsize);
3727 tline = mstart;
3728 } else {
3729 /*
3730 * Expand the macro: we are placed on the last token of the
3731 * call, so that we can easily split the call from the
3732 * following tokens. We also start by pushing an SMAC_END
3733 * token for the cycle removal.
3734 */
3735 t = tline;
3736 if (t) {
3737 tline = t->next;
3738 t->next = NULL;
3739 }
3740 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003741 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003742 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003743 tline = tt;
3744 for (t = m->expansion; t; t = t->next) {
3745 if (t->type >= TOK_SMAC_PARAM) {
3746 Token *pcopy = tline, **ptail = &pcopy;
3747 Token *ttt, *pt;
3748 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003749
H. Peter Anvine2c80182005-01-15 22:15:51 +00003750 ttt = params[t->type - TOK_SMAC_PARAM];
3751 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3752 --i >= 0;) {
3753 pt = *ptail =
3754 new_Token(tline, ttt->type, ttt->text,
3755 0);
3756 ptail = &pt->next;
3757 ttt = ttt->next;
3758 }
3759 tline = pcopy;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -07003760 } else if (t->type == TOK_PREPROC_Q) {
3761 tt = new_Token(tline, TOK_ID, mname, 0);
3762 tline = tt;
3763 } else if (t->type == TOK_PREPROC_QQ) {
3764 tt = new_Token(tline, TOK_ID, m->name, 0);
3765 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003766 } else {
3767 tt = new_Token(tline, t->type, t->text, 0);
3768 tline = tt;
3769 }
3770 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003771
H. Peter Anvine2c80182005-01-15 22:15:51 +00003772 /*
3773 * Having done that, get rid of the macro call, and clean
3774 * up the parameters.
3775 */
3776 nasm_free(params);
3777 nasm_free(paramsize);
3778 free_tlist(mstart);
3779 continue; /* main token loop */
3780 }
3781 }
3782 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003783
H. Peter Anvine2c80182005-01-15 22:15:51 +00003784 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003785 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003786 tline = delete_Token(tline);
3787 } else {
3788 t = *tail = tline;
3789 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003790 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003791 t->next = NULL;
3792 tail = &t->next;
3793 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003794 }
3795
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003796 /*
3797 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003798 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003799 * TOK_IDs should be concatenated.
3800 * Also we look for %+ tokens and concatenate the tokens before and after
3801 * them (without white spaces in between).
3802 */
3803 t = thead;
3804 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003805 while (t) {
3806 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3807 t = t->next;
3808 if (!t || !t->next)
3809 break;
3810 if (t->next->type == TOK_ID ||
3811 t->next->type == TOK_PREPROC_ID ||
3812 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003813 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003814 nasm_free(t->text);
3815 t->next = delete_Token(t->next);
3816 t->text = p;
3817 rescan = 1;
3818 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3819 t->next->next->type == TOK_PREPROC_ID &&
3820 strcmp(t->next->next->text, "%+") == 0) {
3821 /* free the next whitespace, the %+ token and next whitespace */
3822 int i;
3823 for (i = 1; i <= 3; i++) {
3824 if (!t->next
3825 || (i != 2 && t->next->type != TOK_WHITESPACE))
3826 break;
3827 t->next = delete_Token(t->next);
3828 } /* endfor */
3829 } else
3830 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003831 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003832 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003833 if (rescan) {
3834 tline = thead;
3835 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003836 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003837
H. Peter Anvine2c80182005-01-15 22:15:51 +00003838 if (org_tline) {
3839 if (thead) {
3840 *org_tline = *thead;
3841 /* since we just gave text to org_line, don't free it */
3842 thead->text = NULL;
3843 delete_Token(thead);
3844 } else {
3845 /* the expression expanded to empty line;
3846 we can't return NULL for some reasons
3847 we just set the line to a single WHITESPACE token. */
3848 memset(org_tline, 0, sizeof(*org_tline));
3849 org_tline->text = NULL;
3850 org_tline->type = TOK_WHITESPACE;
3851 }
3852 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003853 }
3854
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003855 return thead;
3856}
3857
3858/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003859 * Similar to expand_smacro but used exclusively with macro identifiers
3860 * right before they are fetched in. The reason is that there can be
3861 * identifiers consisting of several subparts. We consider that if there
3862 * are more than one element forming the name, user wants a expansion,
3863 * otherwise it will be left as-is. Example:
3864 *
3865 * %define %$abc cde
3866 *
3867 * the identifier %$abc will be left as-is so that the handler for %define
3868 * will suck it and define the corresponding value. Other case:
3869 *
3870 * %define _%$abc cde
3871 *
3872 * In this case user wants name to be expanded *before* %define starts
3873 * working, so we'll expand %$abc into something (if it has a value;
3874 * otherwise it will be left as-is) then concatenate all successive
3875 * PP_IDs into one.
3876 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003877static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003878{
3879 Token *cur, *oldnext = NULL;
3880
H. Peter Anvin734b1882002-04-30 21:01:08 +00003881 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003882 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003883
3884 cur = tline;
3885 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003886 (cur->next->type == TOK_ID ||
3887 cur->next->type == TOK_PREPROC_ID
3888 || cur->next->type == TOK_NUMBER))
3889 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003890
3891 /* If identifier consists of just one token, don't expand */
3892 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003893 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003894
H. Peter Anvine2c80182005-01-15 22:15:51 +00003895 if (cur) {
3896 oldnext = cur->next; /* Detach the tail past identifier */
3897 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003898 }
3899
H. Peter Anvin734b1882002-04-30 21:01:08 +00003900 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003901
H. Peter Anvine2c80182005-01-15 22:15:51 +00003902 if (cur) {
3903 /* expand_smacro possibly changhed tline; re-scan for EOL */
3904 cur = tline;
3905 while (cur && cur->next)
3906 cur = cur->next;
3907 if (cur)
3908 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003909 }
3910
3911 return tline;
3912}
3913
3914/*
H. Peter Anvinc0ab1cd2008-10-19 16:24:53 -07003915 * Expand indirect tokens, %[...]. Just like expand_smacro(),
3916 * the input is considered destroyed.
H. Peter Anvin992fe752008-10-19 15:45:05 -07003917 */
3918static Token *expand_indirect(Token * tline, int level)
3919{
3920 const int max_indirect_level = 1000;
3921 Token *t, *thead, **tp;
3922 Token *it;
3923 bool skip;
3924
3925 if (level >= max_indirect_level) {
3926 error(ERR_NONFATAL, "interminable indirect expansion");
3927 } else {
3928 thead = NULL;
H. Peter Anvinc0ab1cd2008-10-19 16:24:53 -07003929 tp = &tline;
3930 while ((t = *tp)) {
3931 if (t->type != TOK_INDIRECT) {
3932 thead = t;
3933 tp = &t->next;
3934 } else {
3935 it = tokenize(t->text);
3936 it = expand_indirect(it, level+1);
3937 it = expand_smacro(it);
3938 while (it) {
3939 skip = false;
3940 switch (thead ? thead->type : TOK_NONE) {
3941 case TOK_WHITESPACE:
3942 skip = (it->type == TOK_WHITESPACE);
3943 break;
3944 case TOK_ID:
3945 case TOK_NUMBER:
3946 if (it->type == thead->type || it->type == TOK_NUMBER) {
3947 char *tmp = nasm_strcat(thead->text, it->text);
3948 nasm_free(thead->text);
3949 thead->text = tmp;
3950 skip = true;
3951 }
3952 break;
3953 default:
3954 break;
H. Peter Anvin992fe752008-10-19 15:45:05 -07003955 }
H. Peter Anvinc0ab1cd2008-10-19 16:24:53 -07003956 if (skip) {
3957 it = delete_Token(it);
3958 } else {
3959 *tp = thead = it;
3960 tp = &it->next;
3961 it = it->next;
3962 }
H. Peter Anvin992fe752008-10-19 15:45:05 -07003963 }
H. Peter Anvinc0ab1cd2008-10-19 16:24:53 -07003964 *tp = thead = t->next;
3965 t = delete_Token(t);
H. Peter Anvin992fe752008-10-19 15:45:05 -07003966 }
H. Peter Anvin992fe752008-10-19 15:45:05 -07003967 }
3968 }
3969 return tline;
3970}
3971
3972/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003973 * Determine whether the given line constitutes a multi-line macro
3974 * call, and return the MMacro structure called if so. Doesn't have
3975 * to check for an initial label - that's taken care of in
3976 * expand_mmacro - but must check numbers of parameters. Guaranteed
3977 * to be called with tline->type == TOK_ID, so the putative macro
3978 * name is easy to find.
3979 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003980static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003981{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003982 MMacro *head, *m;
3983 Token **params;
3984 int nparam;
3985
H. Peter Anvin166c2472008-05-28 12:28:58 -07003986 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003987
3988 /*
3989 * Efficiency: first we see if any macro exists with the given
3990 * name. If not, we can return NULL immediately. _Then_ we
3991 * count the parameters, and then we look further along the
3992 * list if necessary to find the proper MMacro.
3993 */
3994 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003995 if (!mstrcmp(m->name, tline->text, m->casesense))
3996 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003997 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003998 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003999
4000 /*
4001 * OK, we have a potential macro. Count and demarcate the
4002 * parameters.
4003 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004004 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004005
4006 /*
4007 * So we know how many parameters we've got. Find the MMacro
4008 * structure that handles this number.
4009 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004010 while (m) {
4011 if (m->nparam_min <= nparam
4012 && (m->plus || nparam <= m->nparam_max)) {
4013 /*
4014 * This one is right. Just check if cycle removal
4015 * prohibits us using it before we actually celebrate...
4016 */
4017 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00004018#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00004019 error(ERR_NONFATAL,
4020 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004021#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00004022 nasm_free(params);
4023 return NULL;
4024 }
4025 /*
4026 * It's right, and we can use it. Add its default
4027 * parameters to the end of our list if necessary.
4028 */
4029 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4030 params =
4031 nasm_realloc(params,
4032 ((m->nparam_min + m->ndefs +
4033 1) * sizeof(*params)));
4034 while (nparam < m->nparam_min + m->ndefs) {
4035 params[nparam] = m->defaults[nparam - m->nparam_min];
4036 nparam++;
4037 }
4038 }
4039 /*
4040 * If we've gone over the maximum parameter count (and
4041 * we're in Plus mode), ignore parameters beyond
4042 * nparam_max.
4043 */
4044 if (m->plus && nparam > m->nparam_max)
4045 nparam = m->nparam_max;
4046 /*
4047 * Then terminate the parameter list, and leave.
4048 */
4049 if (!params) { /* need this special case */
4050 params = nasm_malloc(sizeof(*params));
4051 nparam = 0;
4052 }
4053 params[nparam] = NULL;
4054 *params_array = params;
4055 return m;
4056 }
4057 /*
4058 * This one wasn't right: look for the next one with the
4059 * same name.
4060 */
4061 for (m = m->next; m; m = m->next)
4062 if (!mstrcmp(m->name, tline->text, m->casesense))
4063 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004064 }
4065
4066 /*
4067 * After all that, we didn't find one with the right number of
4068 * parameters. Issue a warning, and fail to expand the macro.
4069 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004070 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004071 "macro `%s' exists, but not taking %d parameters",
4072 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004073 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004074 return NULL;
4075}
4076
4077/*
4078 * Expand the multi-line macro call made by the given line, if
4079 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00004080 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004081 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004082static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004083{
4084 Token *startline = tline;
4085 Token *label = NULL;
4086 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004087 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004088 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004089 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004090 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004091 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004092
4093 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004094 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004095 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004096 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004097 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004098 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004099 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004100 if (m) {
4101 mname = t->text;
4102 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004103 Token *last;
4104 /*
4105 * We have an id which isn't a macro call. We'll assume
4106 * it might be a label; we'll also check to see if a
4107 * colon follows it. Then, if there's another id after
4108 * that lot, we'll check it again for macro-hood.
4109 */
4110 label = last = t;
4111 t = t->next;
4112 if (tok_type_(t, TOK_WHITESPACE))
4113 last = t, t = t->next;
4114 if (tok_is_(t, ":")) {
4115 dont_prepend = 1;
4116 last = t, t = t->next;
4117 if (tok_type_(t, TOK_WHITESPACE))
4118 last = t, t = t->next;
4119 }
4120 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
4121 return 0;
4122 last->next = NULL;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004123 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004124 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004125 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004126
4127 /*
4128 * Fix up the parameters: this involves stripping leading and
4129 * trailing whitespace, then stripping braces if they are
4130 * present.
4131 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004132 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004133 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004134
H. Peter Anvine2c80182005-01-15 22:15:51 +00004135 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004136 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004137 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004138
H. Peter Anvine2c80182005-01-15 22:15:51 +00004139 t = params[i];
4140 skip_white_(t);
4141 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004142 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004143 params[i] = t;
4144 paramlen[i] = 0;
4145 while (t) {
4146 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4147 break; /* ... because we have hit a comma */
4148 if (comma && t->type == TOK_WHITESPACE
4149 && tok_is_(t->next, ","))
4150 break; /* ... or a space then a comma */
4151 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4152 break; /* ... or a brace */
4153 t = t->next;
4154 paramlen[i]++;
4155 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004156 }
4157
4158 /*
4159 * OK, we have a MMacro structure together with a set of
4160 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004161 * copies of each Line on to istk->expansion. Substitution of
4162 * parameter tokens and macro-local tokens doesn't get done
4163 * until the single-line macro substitution process; this is
4164 * because delaying them allows us to change the semantics
4165 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004166 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004167 * First, push an end marker on to istk->expansion, mark this
4168 * macro as in progress, and set up its invocation-specific
4169 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004170 */
4171 ll = nasm_malloc(sizeof(Line));
4172 ll->next = istk->expansion;
4173 ll->finishes = m;
4174 ll->first = NULL;
4175 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004176
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004177 m->in_progress = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004178 m->params = params;
4179 m->iline = tline;
4180 m->nparam = nparam;
4181 m->rotate = 0;
4182 m->paramlen = paramlen;
4183 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004184 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004185
4186 m->next_active = istk->mstk;
4187 istk->mstk = m;
4188
H. Peter Anvine2c80182005-01-15 22:15:51 +00004189 for (l = m->expansion; l; l = l->next) {
4190 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004191
H. Peter Anvine2c80182005-01-15 22:15:51 +00004192 ll = nasm_malloc(sizeof(Line));
4193 ll->finishes = NULL;
4194 ll->next = istk->expansion;
4195 istk->expansion = ll;
4196 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004197
H. Peter Anvine2c80182005-01-15 22:15:51 +00004198 for (t = l->first; t; t = t->next) {
4199 Token *x = t;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004200 switch (t->type) {
4201 case TOK_PREPROC_Q:
H. Peter Anvinc751e862008-06-09 10:18:45 -07004202 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004203 break;
4204 case TOK_PREPROC_QQ:
4205 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4206 break;
4207 case TOK_PREPROC_ID:
4208 if (t->text[1] == '0' && t->text[2] == '0') {
4209 dont_prepend = -1;
4210 x = label;
4211 if (!x)
4212 continue;
4213 }
4214 /* fall through */
4215 default:
4216 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4217 break;
H. Peter Anvin166c2472008-05-28 12:28:58 -07004218 }
H. Peter Anvince2233b2008-05-25 21:57:00 -07004219 tail = &tt->next;
4220 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004221 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004222 }
4223
4224 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004225 * If we had a label, push it on as the first line of
4226 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004227 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004228 if (label) {
4229 if (dont_prepend < 0)
4230 free_tlist(startline);
4231 else {
4232 ll = nasm_malloc(sizeof(Line));
4233 ll->finishes = NULL;
4234 ll->next = istk->expansion;
4235 istk->expansion = ll;
4236 ll->first = startline;
4237 if (!dont_prepend) {
4238 while (label->next)
4239 label = label->next;
4240 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4241 }
4242 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004243 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004244
H. Peter Anvin734b1882002-04-30 21:01:08 +00004245 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004246
H. Peter Anvineba20a72002-04-30 20:53:55 +00004247 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004248}
4249
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004250/* The function that actually does the error reporting */
4251static void verror(int severity, const char *fmt, va_list arg)
4252{
4253 char buff[1024];
4254
4255 vsnprintf(buff, sizeof(buff), fmt, arg);
4256
4257 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004258 _error(severity, "(%s:%d) %s", istk->mstk->name,
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004259 istk->mstk->lineno, buff);
4260 else
H. Peter Anvin917a3492008-09-24 09:14:49 -07004261 _error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004262}
4263
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004264/*
4265 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004266 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004267 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004268static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004269{
4270 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004271
4272 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004273 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004274 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004275
H. Peter Anvin734b1882002-04-30 21:01:08 +00004276 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004277 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004278 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004279}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004280
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004281/*
4282 * Because %else etc are evaluated in the state context
4283 * of the previous branch, errors might get lost with error():
4284 * %if 0 ... %else trailing garbage ... %endif
4285 * So %else etc should report errors with this function.
4286 */
4287static void error_precond(int severity, const char *fmt, ...)
4288{
4289 va_list arg;
4290
4291 /* Only ignore the error if it's really in a dead branch */
4292 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4293 return;
4294
4295 va_start(arg, fmt);
4296 verror(severity, fmt, arg);
4297 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004298}
4299
H. Peter Anvin734b1882002-04-30 21:01:08 +00004300static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004301pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004302 ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004303{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004304 Token *t;
4305
H. Peter Anvin99941bf2002-05-14 17:44:03 +00004306 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004307 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004308 istk = nasm_malloc(sizeof(Include));
4309 istk->next = NULL;
4310 istk->conds = NULL;
4311 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004312 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004313 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004314 istk->fname = NULL;
4315 src_set_fname(nasm_strdup(file));
4316 src_set_linnum(0);
4317 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004318 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004319 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004320 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004321 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004322 nested_mac_count = 0;
4323 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004324 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004325 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004326 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004327 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004328 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004329 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004330 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004331 any_extrastdmac = extrastdmac && *extrastdmac;
4332 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004333 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004334 evaluate = eval;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004335
4336 /*
4337 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4338 * The caller, however, will also pass in 3 for preprocess-only so
4339 * we can set __PASS__ accordingly.
4340 */
4341 pass = apass > 2 ? 2 : apass;
4342
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004343 dephead = deptail = deplist;
4344 if (deplist) {
4345 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4346 sl->next = NULL;
4347 strcpy(sl->str, file);
4348 *deptail = sl;
4349 deptail = &sl->next;
4350 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004351
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004352 /*
4353 * Define the __PASS__ macro. This is defined here unlike
4354 * all the other builtins, because it is special -- it varies between
4355 * passes.
4356 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004357 t = nasm_malloc(sizeof(*t));
4358 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004359 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004360 t->a.mac = NULL;
4361 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004362}
4363
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004364static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004365{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004366 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004367 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004368
H. Peter Anvine2c80182005-01-15 22:15:51 +00004369 while (1) {
4370 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004371 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004372 * buffer or from the input file.
4373 */
4374 tline = NULL;
4375 while (istk->expansion && istk->expansion->finishes) {
4376 Line *l = istk->expansion;
4377 if (!l->finishes->name && l->finishes->in_progress > 1) {
4378 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004379
H. Peter Anvine2c80182005-01-15 22:15:51 +00004380 /*
4381 * This is a macro-end marker for a macro with no
4382 * name, which means it's not really a macro at all
4383 * but a %rep block, and the `in_progress' field is
4384 * more than 1, meaning that we still need to
4385 * repeat. (1 means the natural last repetition; 0
4386 * means termination by %exitrep.) We have
4387 * therefore expanded up to the %endrep, and must
4388 * push the whole block on to the expansion buffer
4389 * again. We don't bother to remove the macro-end
4390 * marker: we'd only have to generate another one
4391 * if we did.
4392 */
4393 l->finishes->in_progress--;
4394 for (l = l->finishes->expansion; l; l = l->next) {
4395 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004396
H. Peter Anvine2c80182005-01-15 22:15:51 +00004397 ll = nasm_malloc(sizeof(Line));
4398 ll->next = istk->expansion;
4399 ll->finishes = NULL;
4400 ll->first = NULL;
4401 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004402
H. Peter Anvine2c80182005-01-15 22:15:51 +00004403 for (t = l->first; t; t = t->next) {
4404 if (t->text || t->type == TOK_WHITESPACE) {
4405 tt = *tail =
4406 new_Token(NULL, t->type, t->text, 0);
4407 tail = &tt->next;
4408 }
4409 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004410
H. Peter Anvine2c80182005-01-15 22:15:51 +00004411 istk->expansion = ll;
4412 }
4413 } else {
4414 /*
4415 * Check whether a `%rep' was started and not ended
4416 * within this macro expansion. This can happen and
4417 * should be detected. It's a fatal error because
4418 * I'm too confused to work out how to recover
4419 * sensibly from it.
4420 */
4421 if (defining) {
4422 if (defining->name)
4423 error(ERR_PANIC,
4424 "defining with name in expansion");
4425 else if (istk->mstk->name)
4426 error(ERR_FATAL,
4427 "`%%rep' without `%%endrep' within"
4428 " expansion of macro `%s'",
4429 istk->mstk->name);
4430 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004431
H. Peter Anvine2c80182005-01-15 22:15:51 +00004432 /*
4433 * FIXME: investigate the relationship at this point between
4434 * istk->mstk and l->finishes
4435 */
4436 {
4437 MMacro *m = istk->mstk;
4438 istk->mstk = m->next_active;
4439 if (m->name) {
4440 /*
4441 * This was a real macro call, not a %rep, and
4442 * therefore the parameter information needs to
4443 * be freed.
4444 */
4445 nasm_free(m->params);
4446 free_tlist(m->iline);
4447 nasm_free(m->paramlen);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004448 l->finishes->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004449 } else
4450 free_mmacro(m);
4451 }
4452 istk->expansion = l->next;
4453 nasm_free(l);
4454 list->downlevel(LIST_MACRO);
4455 }
4456 }
4457 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004458
H. Peter Anvine2c80182005-01-15 22:15:51 +00004459 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004460 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004461 Line *l = istk->expansion;
4462 if (istk->mstk)
4463 istk->mstk->lineno++;
4464 tline = l->first;
4465 istk->expansion = l->next;
4466 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004467 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004468 list->line(LIST_MACRO, p);
4469 nasm_free(p);
4470 break;
4471 }
4472 line = read_line();
4473 if (line) { /* from the current input file */
4474 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004475 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004476 nasm_free(line);
4477 break;
4478 }
4479 /*
4480 * The current file has ended; work down the istk
4481 */
4482 {
4483 Include *i = istk;
4484 fclose(i->fp);
4485 if (i->conds)
4486 error(ERR_FATAL,
4487 "expected `%%endif' before end of file");
4488 /* only set line and file name if there's a next node */
4489 if (i->next) {
4490 src_set_linnum(i->lineno);
4491 nasm_free(src_set_fname(i->fname));
H. Peter Anvin86877b22008-06-20 15:55:45 -07004492 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004493 istk = i->next;
4494 list->downlevel(LIST_INCLUDE);
4495 nasm_free(i);
4496 if (!istk)
4497 return NULL;
Victor van den Elzen4c9d6222008-10-01 13:08:50 +02004498 if (istk->expansion && istk->expansion->finishes)
4499 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004500 }
4501 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004502
H. Peter Anvine2c80182005-01-15 22:15:51 +00004503 /*
4504 * We must expand MMacro parameters and MMacro-local labels
4505 * _before_ we plunge into directive processing, to cope
4506 * with things like `%define something %1' such as STRUC
4507 * uses. Unless we're _defining_ a MMacro, in which case
4508 * those tokens should be left alone to go into the
4509 * definition; and unless we're in a non-emitting
4510 * condition, in which case we don't want to meddle with
4511 * anything.
4512 */
Charles Crayned4200be2008-07-12 16:42:33 -07004513 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin992fe752008-10-19 15:45:05 -07004514 && !(istk->mstk && !istk->mstk->in_progress)) {
4515 tline = expand_indirect(tline,0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004516 tline = expand_mmac_params(tline);
H. Peter Anvin992fe752008-10-19 15:45:05 -07004517 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004518
H. Peter Anvine2c80182005-01-15 22:15:51 +00004519 /*
4520 * Check the line to see if it's a preprocessor directive.
4521 */
4522 if (do_directive(tline) == DIRECTIVE_FOUND) {
4523 continue;
4524 } else if (defining) {
4525 /*
4526 * We're defining a multi-line macro. We emit nothing
4527 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004528 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004529 */
4530 Line *l = nasm_malloc(sizeof(Line));
4531 l->next = defining->expansion;
4532 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004533 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004534 defining->expansion = l;
4535 continue;
4536 } else if (istk->conds && !emitting(istk->conds->state)) {
4537 /*
4538 * We're in a non-emitting branch of a condition block.
4539 * Emit nothing at all, not even a blank line: when we
4540 * emerge from the condition we'll give a line-number
4541 * directive so we keep our place correctly.
4542 */
4543 free_tlist(tline);
4544 continue;
4545 } else if (istk->mstk && !istk->mstk->in_progress) {
4546 /*
4547 * We're in a %rep block which has been terminated, so
4548 * we're walking through to the %endrep without
4549 * emitting anything. Emit nothing at all, not even a
4550 * blank line: when we emerge from the %rep block we'll
4551 * give a line-number directive so we keep our place
4552 * correctly.
4553 */
4554 free_tlist(tline);
4555 continue;
4556 } else {
4557 tline = expand_smacro(tline);
4558 if (!expand_mmacro(tline)) {
4559 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004560 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004561 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004562 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004563 free_tlist(tline);
4564 break;
4565 } else {
4566 continue; /* expand_mmacro calls free_tlist */
4567 }
4568 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004569 }
4570
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004571 return line;
4572}
4573
H. Peter Anvine2c80182005-01-15 22:15:51 +00004574static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004575{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004576 if (defining) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02004577 if(defining->name) {
4578 error(ERR_NONFATAL,
4579 "end of file while still defining macro `%s'",
4580 defining->name);
4581 } else {
4582 error(ERR_NONFATAL, "end of file while still in %%rep");
4583 }
4584
H. Peter Anvine2c80182005-01-15 22:15:51 +00004585 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004586 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004587 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004588 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004589 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004590 while (istk) {
4591 Include *i = istk;
4592 istk = istk->next;
4593 fclose(i->fp);
4594 nasm_free(i->fname);
4595 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004596 }
4597 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004598 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004599 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004600 if (pass == 0) {
H. Peter Anvin86877b22008-06-20 15:55:45 -07004601 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004602 free_llist(predef);
4603 delete_Blocks();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004604 while ((i = ipath)) {
4605 ipath = i->next;
4606 if (i->path)
4607 nasm_free(i->path);
4608 nasm_free(i);
4609 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07004610 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004611}
4612
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004613void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004614{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004615 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004616
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004617 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004618 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004619 i->next = NULL;
4620
H. Peter Anvine2c80182005-01-15 22:15:51 +00004621 if (ipath != NULL) {
4622 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004623 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004624 j = j->next;
4625 j->next = i;
4626 } else {
4627 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004628 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004629}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004630
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004631void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004632{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004633 Token *inc, *space, *name;
4634 Line *l;
4635
H. Peter Anvin734b1882002-04-30 21:01:08 +00004636 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4637 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4638 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004639
4640 l = nasm_malloc(sizeof(Line));
4641 l->next = predef;
4642 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004643 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004644 predef = l;
4645}
4646
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004647void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004648{
4649 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004650 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004651 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004652
4653 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004654 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4655 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004656 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004657 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004658 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004659 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004660 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004661
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004662 l = nasm_malloc(sizeof(Line));
4663 l->next = predef;
4664 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004665 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004666 predef = l;
4667}
4668
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004669void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004670{
4671 Token *def, *space;
4672 Line *l;
4673
H. Peter Anvin734b1882002-04-30 21:01:08 +00004674 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4675 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004676 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004677
4678 l = nasm_malloc(sizeof(Line));
4679 l->next = predef;
4680 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004681 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004682 predef = l;
4683}
4684
Keith Kaniosb7a89542007-04-12 02:40:54 +00004685/*
4686 * Added by Keith Kanios:
4687 *
4688 * This function is used to assist with "runtime" preprocessor
4689 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4690 *
4691 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4692 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4693 */
4694
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004695void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004696{
4697 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004698
Keith Kaniosb7a89542007-04-12 02:40:54 +00004699 def = tokenize(definition);
4700 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4701 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004702
Keith Kaniosb7a89542007-04-12 02:40:54 +00004703}
4704
H. Peter Anvina70547f2008-07-19 21:44:26 -07004705void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004706{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004707 extrastdmac = macros;
4708}
4709
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004710static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004711{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004712 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004713 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004714 tok->text = nasm_strdup(numbuf);
4715 tok->type = TOK_NUMBER;
4716}
4717
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004718Preproc nasmpp = {
4719 pp_reset,
4720 pp_getline,
4721 pp_cleanup
4722};