blob: c6dd1b729b30847ecd1587130e02c98b9e062ab5 [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 Anvinb2a5fda2008-06-19 21:42:42 -0700400static Context *get_ctx(const char *name, bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700401static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000402static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200403static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000404static void *new_Block(size_t size);
405static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700406static Token *new_Token(Token * next, enum pp_token_type type,
407 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000408static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000409
410/*
411 * Macros for safe checking of token pointers, avoid *(NULL)
412 */
413#define tok_type_(x,t) ((x) && (x)->type == (t))
414#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
415#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
416#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000417
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000418/* Handle TASM specific directives, which do not contain a % in
419 * front of them. We do it here because I could not find any other
420 * place to do it for the moment, and it is a hack (ideally it would
421 * be nice to be able to use the NASM pre-processor to do it).
422 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000423static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000424{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000425 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000426 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000427
428 /* Skip whitespace */
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700429 while (nasm_isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000430 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000431
432 /* Binary search for the directive name */
433 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000434 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000435 len = 0;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700436 while (!nasm_isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000437 len++;
438 if (len) {
439 oldchar = p[len];
440 p[len] = 0;
441 while (j - i > 1) {
442 k = (j + i) / 2;
443 m = nasm_stricmp(p, tasm_directives[k]);
444 if (m == 0) {
445 /* We have found a directive, so jam a % in front of it
446 * so that NASM will then recognise it as one if it's own.
447 */
448 p[len] = oldchar;
449 len = strlen(p);
450 oldline = line;
451 line = nasm_malloc(len + 2);
452 line[0] = '%';
453 if (k == TM_IFDIFI) {
454 /* NASM does not recognise IFDIFI, so we convert it to
455 * %ifdef BOGUS. This is not used in NASM comaptible
456 * code, but does need to parse for the TASM macro
457 * package.
458 */
459 strcpy(line + 1, "ifdef BOGUS");
460 } else {
461 memcpy(line + 1, p, len + 1);
462 }
463 nasm_free(oldline);
464 return line;
465 } else if (m < 0) {
466 j = k;
467 } else
468 i = k;
469 }
470 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000471 }
472 return line;
473}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000474
H. Peter Anvin76690a12002-04-30 20:52:49 +0000475/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000476 * The pre-preprocessing stage... This function translates line
477 * number indications as they emerge from GNU cpp (`# lineno "file"
478 * flags') into NASM preprocessor line number indications (`%line
479 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000480 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000481static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000482{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000483 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000484 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000485
H. Peter Anvine2c80182005-01-15 22:15:51 +0000486 if (line[0] == '#' && line[1] == ' ') {
487 oldline = line;
488 fname = oldline + 2;
489 lineno = atoi(fname);
490 fname += strspn(fname, "0123456789 ");
491 if (*fname == '"')
492 fname++;
493 fnlen = strcspn(fname, "\"");
494 line = nasm_malloc(20 + fnlen);
495 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
496 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000497 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000498 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000499 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000500 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000501}
502
503/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000504 * Free a linked list of tokens.
505 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000506static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000507{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508 while (list) {
509 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000510 }
511}
512
513/*
514 * Free a linked list of lines.
515 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000516static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000517{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000518 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000519 while (list) {
520 l = list;
521 list = list->next;
522 free_tlist(l->first);
523 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000524 }
525}
526
527/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000528 * Free an MMacro
529 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000530static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000531{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000532 nasm_free(m->name);
533 free_tlist(m->dlist);
534 nasm_free(m->defaults);
535 free_llist(m->expansion);
536 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000537}
538
539/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700540 * Free all currently defined macros, and free the hash tables
541 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700542static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700543{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700544 SMacro *s;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700545 const char *key;
546 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700547
H. Peter Anvin072771e2008-05-22 13:17:51 -0700548 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700549 nasm_free((void *)key);
550 while (s) {
551 SMacro *ns = s->next;
552 nasm_free(s->name);
553 free_tlist(s->expansion);
554 nasm_free(s);
555 s = ns;
556 }
557 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700558 hash_free(smt);
559}
560
561static void free_mmacro_table(struct hash_table *mmt)
562{
563 MMacro *m;
564 const char *key;
565 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700566
567 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700568 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700569 nasm_free((void *)key);
570 while (m) {
571 MMacro *nm = m->next;
572 free_mmacro(m);
573 m = nm;
574 }
575 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700576 hash_free(mmt);
577}
578
579static void free_macros(void)
580{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700581 free_smacro_table(&smacros);
582 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700583}
584
585/*
586 * Initialize the hash tables
587 */
588static void init_macros(void)
589{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700590 hash_init(&smacros, HASH_LARGE);
591 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700592}
593
594/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000595 * Pop the context stack.
596 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000597static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000598{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000599 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000600
601 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700602 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000603 nasm_free(c->name);
604 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000605}
606
H. Peter Anvin072771e2008-05-22 13:17:51 -0700607/*
608 * Search for a key in the hash index; adding it if necessary
609 * (in which case we initialize the data pointer to NULL.)
610 */
611static void **
612hash_findi_add(struct hash_table *hash, const char *str)
613{
614 struct hash_insert hi;
615 void **r;
616 char *strx;
617
618 r = hash_findi(hash, str, &hi);
619 if (r)
620 return r;
621
622 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
623 return hash_add(&hi, strx, NULL);
624}
625
626/*
627 * Like hash_findi, but returns the data element rather than a pointer
628 * to it. Used only when not adding a new element, hence no third
629 * argument.
630 */
631static void *
632hash_findix(struct hash_table *hash, const char *str)
633{
634 void **p;
635
636 p = hash_findi(hash, str, NULL);
637 return p ? *p : NULL;
638}
639
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000640#define BUF_DELTA 512
641/*
642 * Read a line from the top file in istk, handling multiple CR/LFs
643 * at the end of the line read, and handling spurious ^Zs. Will
644 * return lines from the standard macro set if this has not already
645 * been done.
646 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000647static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000648{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000649 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000650 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000651
H. Peter Anvine2c80182005-01-15 22:15:51 +0000652 if (stdmacpos) {
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700653 unsigned char c;
H. Peter Anvin7e50d232008-06-25 14:54:14 -0700654 const unsigned char *p = stdmacpos;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700655 char *ret, *q;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700656 size_t len = 0;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700657 while ((c = *p++)) {
658 if (c >= 0x80)
659 len += pp_directives_len[c-0x80]+1;
660 else
661 len++;
662 }
663 ret = nasm_malloc(len+1);
H. Peter Anvincda81632008-06-21 15:15:40 -0700664 q = ret;
665 while ((c = *stdmacpos++)) {
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700666 if (c >= 0x80) {
667 memcpy(q, pp_directives[c-0x80], pp_directives_len[c-0x80]);
668 q += pp_directives_len[c-0x80];
669 *q++ = ' ';
670 } else {
671 *q++ = c;
672 }
673 }
H. Peter Anvincda81632008-06-21 15:15:40 -0700674 stdmacpos = p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700675 *q = '\0';
676
H. Peter Anvind2456592008-06-19 15:04:18 -0700677 if (!*stdmacpos) {
678 /* This was the last of the standard macro chain... */
679 stdmacpos = NULL;
680 if (any_extrastdmac) {
681 stdmacpos = extrastdmac;
682 any_extrastdmac = false;
683 } else if (do_predef) {
684 Line *pd, *l;
685 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000686
H. Peter Anvind2456592008-06-19 15:04:18 -0700687 /*
688 * Nasty hack: here we push the contents of
689 * `predef' on to the top-level expansion stack,
690 * since this is the most convenient way to
691 * implement the pre-include and pre-define
692 * features.
693 */
694 for (pd = predef; pd; pd = pd->next) {
695 head = NULL;
696 tail = &head;
697 for (t = pd->first; t; t = t->next) {
698 *tail = new_Token(NULL, t->type, t->text, 0);
699 tail = &(*tail)->next;
700 }
701 l = nasm_malloc(sizeof(Line));
702 l->next = istk->expansion;
703 l->first = head;
H. Peter Anvin538002d2008-06-28 18:30:27 -0700704 l->finishes = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700705 istk->expansion = l;
706 }
707 do_predef = false;
708 }
709 }
710 return ret;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000711 }
712
713 bufsize = BUF_DELTA;
714 buffer = nasm_malloc(BUF_DELTA);
715 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000716 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000717 while (1) {
718 q = fgets(p, bufsize - (p - buffer), istk->fp);
719 if (!q)
720 break;
721 p += strlen(p);
722 if (p > buffer && p[-1] == '\n') {
723 /* Convert backslash-CRLF line continuation sequences into
724 nothing at all (for DOS and Windows) */
725 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
726 p -= 3;
727 *p = 0;
728 continued_count++;
729 }
730 /* Also convert backslash-LF line continuation sequences into
731 nothing at all (for Unix) */
732 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
733 p -= 2;
734 *p = 0;
735 continued_count++;
736 } else {
737 break;
738 }
739 }
740 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000741 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000742 bufsize += BUF_DELTA;
743 buffer = nasm_realloc(buffer, bufsize);
744 p = buffer + offset; /* prevent stale-pointer problems */
745 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000746 }
747
H. Peter Anvine2c80182005-01-15 22:15:51 +0000748 if (!q && p == buffer) {
749 nasm_free(buffer);
750 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000751 }
752
H. Peter Anvine2c80182005-01-15 22:15:51 +0000753 src_set_linnum(src_get_linnum() + istk->lineinc +
754 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000755
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000756 /*
757 * Play safe: remove CRs as well as LFs, if any of either are
758 * present at the end of the line.
759 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000760 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000761 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000762
763 /*
764 * Handle spurious ^Z, which may be inserted into source files
765 * by some file transfer utilities.
766 */
767 buffer[strcspn(buffer, "\032")] = '\0';
768
H. Peter Anvin734b1882002-04-30 21:01:08 +0000769 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000770
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000771 return buffer;
772}
773
774/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000775 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000776 * don't need to parse the value out of e.g. numeric tokens: we
777 * simply split one string into many.
778 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000779static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000780{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700781 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000782 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000783 Token *list = NULL;
784 Token *t, **tail = &list;
785
H. Peter Anvine2c80182005-01-15 22:15:51 +0000786 while (*line) {
787 p = line;
788 if (*p == '%') {
789 p++;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700790 if (nasm_isdigit(*p) ||
791 ((*p == '-' || *p == '+') && nasm_isdigit(p[1])) ||
792 ((*p == '+') && (nasm_isspace(p[1]) || !p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000793 do {
794 p++;
795 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700796 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000797 type = TOK_PREPROC_ID;
798 } else if (*p == '{') {
799 p++;
800 while (*p && *p != '}') {
801 p[-1] = *p;
802 p++;
803 }
804 p[-1] = '\0';
805 if (*p)
806 p++;
807 type = TOK_PREPROC_ID;
H. Peter Anvin992fe752008-10-19 15:45:05 -0700808 } else if (*p == '[') {
809 int lvl = 1;
810 line += 2; /* Skip the leading %[ */
811 p++;
H. Peter Anvinec033012008-10-19 22:12:06 -0700812 while (lvl && (c = *p++)) {
H. Peter Anvinca544db2008-10-19 19:30:11 -0700813 switch (c) {
814 case ']':
815 lvl--;
816 break;
817 case '%':
H. Peter Anvinca544db2008-10-19 19:30:11 -0700818 if (*p == '[')
819 lvl++;
820 break;
821 case '\'':
822 case '\"':
823 case '`':
H. Peter Anvinec033012008-10-19 22:12:06 -0700824 p = nasm_skip_string(p)+1;
H. Peter Anvinca544db2008-10-19 19:30:11 -0700825 break;
826 default:
827 break;
H. Peter Anvin992fe752008-10-19 15:45:05 -0700828 }
H. Peter Anvin992fe752008-10-19 15:45:05 -0700829 }
H. Peter Anvinec033012008-10-19 22:12:06 -0700830 p--;
H. Peter Anvin992fe752008-10-19 15:45:05 -0700831 if (*p)
832 *p++ = '\0';
H. Peter Anvine1265812008-10-19 22:14:30 -0700833 if (lvl)
834 error(ERR_NONFATAL, "unterminated %[ construct");
H. Peter Anvin992fe752008-10-19 15:45:05 -0700835 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 skip_white_(tline);
1617 tline = expand_id(tline);
1618 if (!tok_type_(tline, TOK_ID)) {
1619 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001620 "`%s' expects a macro name", pp_directives[ct]);
1621 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001622 }
1623 searching.name = nasm_strdup(tline->text);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001624 searching.casesense = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001625 searching.plus = false;
1626 searching.nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001627 searching.in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 searching.rep_nest = NULL;
1629 searching.nparam_min = 0;
1630 searching.nparam_max = INT_MAX;
1631 tline = expand_smacro(tline->next);
1632 skip_white_(tline);
1633 if (!tline) {
1634 } else if (!tok_type_(tline, TOK_NUMBER)) {
1635 error(ERR_NONFATAL,
1636 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001637 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001638 } else {
1639 searching.nparam_min = searching.nparam_max =
1640 readnum(tline->text, &j);
1641 if (j)
1642 error(ERR_NONFATAL,
1643 "unable to parse parameter count `%s'",
1644 tline->text);
1645 }
1646 if (tline && tok_is_(tline->next, "-")) {
1647 tline = tline->next->next;
1648 if (tok_is_(tline, "*"))
1649 searching.nparam_max = INT_MAX;
1650 else if (!tok_type_(tline, TOK_NUMBER))
1651 error(ERR_NONFATAL,
1652 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001653 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001654 else {
1655 searching.nparam_max = readnum(tline->text, &j);
1656 if (j)
1657 error(ERR_NONFATAL,
1658 "unable to parse parameter count `%s'",
1659 tline->text);
1660 if (searching.nparam_min > searching.nparam_max)
1661 error(ERR_NONFATAL,
1662 "minimum parameter count exceeds maximum");
1663 }
1664 }
1665 if (tline && tok_is_(tline->next, "+")) {
1666 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001667 searching.plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001668 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001669 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001670 while (mmac) {
1671 if (!strcmp(mmac->name, searching.name) &&
1672 (mmac->nparam_min <= searching.nparam_max
1673 || searching.plus)
1674 && (searching.nparam_min <= mmac->nparam_max
1675 || mmac->plus)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001676 found = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001677 break;
1678 }
1679 mmac = mmac->next;
1680 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001681 if(tline && tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001682 error(ERR_WARNING|ERR_PASS1,
1683 "trailing garbage after %%ifmacro ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001685 j = found;
1686 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001687 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001688
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001689 case PPC_IFID:
1690 needtype = TOK_ID;
1691 goto iftype;
1692 case PPC_IFNUM:
1693 needtype = TOK_NUMBER;
1694 goto iftype;
1695 case PPC_IFSTR:
1696 needtype = TOK_STRING;
1697 goto iftype;
1698
1699 iftype:
H. Peter Anvin134b9462008-02-16 17:01:40 -08001700 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001701
H. Peter Anvin927c92b2008-02-16 13:44:52 -08001702 while (tok_type_(t, TOK_WHITESPACE) ||
1703 (needtype == TOK_NUMBER &&
1704 tok_type_(t, TOK_OTHER) &&
1705 (t->text[0] == '-' || t->text[0] == '+') &&
1706 !t->text[1]))
1707 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001708
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001709 j = tok_type_(t, needtype);
1710 break;
1711
1712 case PPC_IFTOKEN:
1713 t = tline = expand_smacro(tline);
1714 while (tok_type_(t, TOK_WHITESPACE))
1715 t = t->next;
1716
1717 j = false;
1718 if (t) {
1719 t = t->next; /* Skip the actual token */
1720 while (tok_type_(t, TOK_WHITESPACE))
1721 t = t->next;
1722 j = !t; /* Should be nothing left */
1723 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001724 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001725
H. Peter Anvin134b9462008-02-16 17:01:40 -08001726 case PPC_IFEMPTY:
1727 t = tline = expand_smacro(tline);
1728 while (tok_type_(t, TOK_WHITESPACE))
1729 t = t->next;
1730
1731 j = !t; /* Should be empty */
1732 break;
1733
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001734 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001735 t = tline = expand_smacro(tline);
1736 tptr = &t;
1737 tokval.t_type = TOKEN_INVALID;
1738 evalresult = evaluate(ppscan, tptr, &tokval,
1739 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001740 if (!evalresult)
1741 return -1;
1742 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001743 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001744 "trailing garbage after expression ignored");
1745 if (!is_simple(evalresult)) {
1746 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001747 "non-constant value given to `%s'", pp_directives[ct]);
1748 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001749 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001750 j = reloc_value(evalresult) != 0;
H. Peter Anvin90e6e812008-07-16 14:38:24 -07001751 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001752
H. Peter Anvine2c80182005-01-15 22:15:51 +00001753 default:
1754 error(ERR_FATAL,
1755 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001756 pp_directives[ct]);
1757 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001758 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001759
1760 free_tlist(origline);
1761 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001762
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001763fail:
1764 free_tlist(origline);
1765 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001766}
1767
1768/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001769 * Common code for defining an smacro
1770 */
1771static bool define_smacro(Context *ctx, char *mname, bool casesense,
1772 int nparam, Token *expansion)
1773{
1774 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001775 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001776
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001777 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1778 if (!smac) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001779 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001780 "single-line macro `%s' defined both with and"
1781 " without parameters", mname);
1782
1783 /* Some instances of the old code considered this a failure,
1784 some others didn't. What is the right thing to do here? */
1785 free_tlist(expansion);
1786 return false; /* Failure */
1787 } else {
1788 /*
1789 * We're redefining, so we have to take over an
1790 * existing SMacro structure. This means freeing
1791 * what was already in it.
1792 */
1793 nasm_free(smac->name);
1794 free_tlist(smac->expansion);
1795 }
1796 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001797 smtbl = ctx ? &ctx->localmac : &smacros;
1798 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001799 smac = nasm_malloc(sizeof(SMacro));
1800 smac->next = *smhead;
1801 *smhead = smac;
1802 }
1803 smac->name = nasm_strdup(mname);
1804 smac->casesense = casesense;
1805 smac->nparam = nparam;
1806 smac->expansion = expansion;
1807 smac->in_progress = false;
1808 return true; /* Success */
1809}
1810
1811/*
1812 * Undefine an smacro
1813 */
1814static void undef_smacro(Context *ctx, const char *mname)
1815{
1816 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001817 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001818
H. Peter Anvin166c2472008-05-28 12:28:58 -07001819 smtbl = ctx ? &ctx->localmac : &smacros;
1820 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001821
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001822 if (smhead) {
1823 /*
1824 * We now have a macro name... go hunt for it.
1825 */
1826 sp = smhead;
1827 while ((s = *sp) != NULL) {
1828 if (!mstrcmp(s->name, mname, s->casesense)) {
1829 *sp = s->next;
1830 nasm_free(s->name);
1831 free_tlist(s->expansion);
1832 nasm_free(s);
1833 } else {
1834 sp = &s->next;
1835 }
1836 }
1837 }
1838}
1839
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001840/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07001841 * Parse a mmacro specification.
1842 */
1843static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1844{
1845 bool err;
1846
1847 tline = tline->next;
1848 skip_white_(tline);
1849 tline = expand_id(tline);
1850 if (!tok_type_(tline, TOK_ID)) {
1851 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1852 return false;
1853 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001854
H. Peter Anvina26433d2008-07-16 14:40:01 -07001855 def->name = nasm_strdup(tline->text);
1856 def->plus = false;
1857 def->nolist = false;
1858 def->in_progress = 0;
1859 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001860 def->nparam_min = 0;
1861 def->nparam_max = 0;
1862
H. Peter Anvina26433d2008-07-16 14:40:01 -07001863 tline = expand_smacro(tline->next);
1864 skip_white_(tline);
1865 if (!tok_type_(tline, TOK_NUMBER)) {
1866 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001867 } else {
1868 def->nparam_min = def->nparam_max =
1869 readnum(tline->text, &err);
1870 if (err)
1871 error(ERR_NONFATAL,
1872 "unable to parse parameter count `%s'", tline->text);
1873 }
1874 if (tline && tok_is_(tline->next, "-")) {
1875 tline = tline->next->next;
1876 if (tok_is_(tline, "*")) {
1877 def->nparam_max = INT_MAX;
1878 } else if (!tok_type_(tline, TOK_NUMBER)) {
1879 error(ERR_NONFATAL,
1880 "`%s' expects a parameter count after `-'", directive);
1881 } else {
1882 def->nparam_max = readnum(tline->text, &err);
1883 if (err) {
1884 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
1885 tline->text);
1886 }
1887 if (def->nparam_min > def->nparam_max) {
1888 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
1889 }
1890 }
1891 }
1892 if (tline && tok_is_(tline->next, "+")) {
1893 tline = tline->next;
1894 def->plus = true;
1895 }
1896 if (tline && tok_type_(tline->next, TOK_ID) &&
1897 !nasm_stricmp(tline->next->text, ".nolist")) {
1898 tline = tline->next;
1899 def->nolist = true;
1900 }
1901
1902 /*
1903 * Handle default parameters.
1904 */
1905 if (tline && tline->next) {
1906 def->dlist = tline->next;
1907 tline->next = NULL;
1908 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
1909 } else {
1910 def->dlist = NULL;
1911 def->defaults = NULL;
1912 }
1913 def->expansion = NULL;
1914
Victor van den Elzen22343c22008-08-06 14:47:54 +02001915 if(def->defaults &&
1916 def->ndefs > def->nparam_max - def->nparam_min &&
1917 !def->plus)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001918 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
1919 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001920
H. Peter Anvina26433d2008-07-16 14:40:01 -07001921 return true;
1922}
1923
1924
1925/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001926 * Decode a size directive
1927 */
1928static int parse_size(const char *str) {
1929 static const char *size_names[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001930 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001931 static const int sizes[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001932 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001933
1934 return sizes[bsii(str, size_names, elements(size_names))+1];
1935}
1936
Ed Beroset3ab3f412002-06-11 03:31:49 +00001937/**
1938 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001939 * Find out if a line contains a preprocessor directive, and deal
1940 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07001941 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001942 * If a directive _is_ found, it is the responsibility of this routine
1943 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001944 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001945 * @param tline a pointer to the current tokeninzed line linked list
1946 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07001947 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001948 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001949static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001950{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001951 enum preproc_token i;
1952 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07001953 bool err;
1954 int nparam;
1955 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001956 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001957 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001958 int offset;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001959 char *p, *pp, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001960 Include *inc;
1961 Context *ctx;
1962 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001963 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001964 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1965 Line *l;
1966 struct tokenval tokval;
1967 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001968 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001969 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001970 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07001971 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001972
1973 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001974
H. Peter Anvineba20a72002-04-30 20:53:55 +00001975 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07001976 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001977 (tline->text[1] == '%' || tline->text[1] == '$'
1978 || tline->text[1] == '!'))
1979 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001980
H. Peter Anvin4169a472007-09-12 01:29:43 +00001981 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001982
1983 /*
1984 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001985 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001986 * we should ignore all directives except for condition
1987 * directives.
1988 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001989 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001990 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1991 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001992 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001993
1994 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001995 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02001996 * ignore all directives except for %macro/%imacro (which nest),
1997 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
1998 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001999 */
2000 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00002001 i != PP_ENDMACRO && i != PP_ENDM &&
2002 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2003 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002004 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002005
Charles Crayned4200be2008-07-12 16:42:33 -07002006 if (defining) {
2007 if (i == PP_MACRO || i == PP_IMACRO) {
2008 nested_mac_count++;
2009 return NO_DIRECTIVE_FOUND;
2010 } else if (nested_mac_count > 0) {
2011 if (i == PP_ENDMACRO) {
2012 nested_mac_count--;
2013 return NO_DIRECTIVE_FOUND;
2014 }
2015 }
2016 if (!defining->name) {
2017 if (i == PP_REP) {
2018 nested_rep_count++;
2019 return NO_DIRECTIVE_FOUND;
2020 } else if (nested_rep_count > 0) {
2021 if (i == PP_ENDREP) {
2022 nested_rep_count--;
2023 return NO_DIRECTIVE_FOUND;
2024 }
2025 }
2026 }
2027 }
2028
H. Peter Anvin4169a472007-09-12 01:29:43 +00002029 switch (i) {
2030 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002031 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2032 tline->text);
2033 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002034
H. Peter Anvine2c80182005-01-15 22:15:51 +00002035 case PP_STACKSIZE:
2036 /* Directive to tell NASM what the default stack size is. The
2037 * default is for a 16-bit stack, and this can be overriden with
2038 * %stacksize large.
2039 * the following form:
2040 *
2041 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2042 */
2043 tline = tline->next;
2044 if (tline && tline->type == TOK_WHITESPACE)
2045 tline = tline->next;
2046 if (!tline || tline->type != TOK_ID) {
2047 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2048 free_tlist(origline);
2049 return DIRECTIVE_FOUND;
2050 }
2051 if (nasm_stricmp(tline->text, "flat") == 0) {
2052 /* All subsequent ARG directives are for a 32-bit stack */
2053 StackSize = 4;
2054 StackPointer = "ebp";
2055 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002056 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002057 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2058 /* All subsequent ARG directives are for a 64-bit stack */
2059 StackSize = 8;
2060 StackPointer = "rbp";
2061 ArgOffset = 8;
2062 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002063 } else if (nasm_stricmp(tline->text, "large") == 0) {
2064 /* All subsequent ARG directives are for a 16-bit stack,
2065 * far function call.
2066 */
2067 StackSize = 2;
2068 StackPointer = "bp";
2069 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002070 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002071 } else if (nasm_stricmp(tline->text, "small") == 0) {
2072 /* All subsequent ARG directives are for a 16-bit stack,
2073 * far function call. We don't support near functions.
2074 */
2075 StackSize = 2;
2076 StackPointer = "bp";
2077 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002078 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002079 } else {
2080 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2081 free_tlist(origline);
2082 return DIRECTIVE_FOUND;
2083 }
2084 free_tlist(origline);
2085 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002086
H. Peter Anvine2c80182005-01-15 22:15:51 +00002087 case PP_ARG:
2088 /* TASM like ARG directive to define arguments to functions, in
2089 * the following form:
2090 *
2091 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2092 */
2093 offset = ArgOffset;
2094 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002095 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002096 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002097
H. Peter Anvine2c80182005-01-15 22:15:51 +00002098 /* Find the argument name */
2099 tline = tline->next;
2100 if (tline && tline->type == TOK_WHITESPACE)
2101 tline = tline->next;
2102 if (!tline || tline->type != TOK_ID) {
2103 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2104 free_tlist(origline);
2105 return DIRECTIVE_FOUND;
2106 }
2107 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002108
H. Peter Anvine2c80182005-01-15 22:15:51 +00002109 /* Find the argument size type */
2110 tline = tline->next;
2111 if (!tline || tline->type != TOK_OTHER
2112 || tline->text[0] != ':') {
2113 error(ERR_NONFATAL,
2114 "Syntax error processing `%%arg' directive");
2115 free_tlist(origline);
2116 return DIRECTIVE_FOUND;
2117 }
2118 tline = tline->next;
2119 if (!tline || tline->type != TOK_ID) {
2120 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2121 free_tlist(origline);
2122 return DIRECTIVE_FOUND;
2123 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002124
H. Peter Anvine2c80182005-01-15 22:15:51 +00002125 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002126 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002127 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002128 size = parse_size(tt->text);
2129 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002130 error(ERR_NONFATAL,
2131 "Invalid size type for `%%arg' missing directive");
2132 free_tlist(tt);
2133 free_tlist(origline);
2134 return DIRECTIVE_FOUND;
2135 }
2136 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002137
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002138 /* Round up to even stack slots */
2139 size = (size+StackSize-1) & ~(StackSize-1);
2140
H. Peter Anvine2c80182005-01-15 22:15:51 +00002141 /* Now define the macro for the argument */
2142 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2143 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002144 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002145 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002146
H. Peter Anvine2c80182005-01-15 22:15:51 +00002147 /* Move to the next argument in the list */
2148 tline = tline->next;
2149 if (tline && tline->type == TOK_WHITESPACE)
2150 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002151 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2152 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002153 free_tlist(origline);
2154 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002155
H. Peter Anvine2c80182005-01-15 22:15:51 +00002156 case PP_LOCAL:
2157 /* TASM like LOCAL directive to define local variables for a
2158 * function, in the following form:
2159 *
2160 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2161 *
2162 * The '= LocalSize' at the end is ignored by NASM, but is
2163 * required by TASM to define the local parameter size (and used
2164 * by the TASM macro package).
2165 */
2166 offset = LocalOffset;
2167 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002168 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002169 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002170
H. Peter Anvine2c80182005-01-15 22:15:51 +00002171 /* Find the argument name */
2172 tline = tline->next;
2173 if (tline && tline->type == TOK_WHITESPACE)
2174 tline = tline->next;
2175 if (!tline || tline->type != TOK_ID) {
2176 error(ERR_NONFATAL,
2177 "`%%local' missing argument parameter");
2178 free_tlist(origline);
2179 return DIRECTIVE_FOUND;
2180 }
2181 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002182
H. Peter Anvine2c80182005-01-15 22:15:51 +00002183 /* Find the argument size type */
2184 tline = tline->next;
2185 if (!tline || tline->type != TOK_OTHER
2186 || tline->text[0] != ':') {
2187 error(ERR_NONFATAL,
2188 "Syntax error processing `%%local' directive");
2189 free_tlist(origline);
2190 return DIRECTIVE_FOUND;
2191 }
2192 tline = tline->next;
2193 if (!tline || tline->type != TOK_ID) {
2194 error(ERR_NONFATAL,
2195 "`%%local' missing size type parameter");
2196 free_tlist(origline);
2197 return DIRECTIVE_FOUND;
2198 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002199
H. Peter Anvine2c80182005-01-15 22:15:51 +00002200 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002201 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002202 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002203 size = parse_size(tt->text);
2204 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002205 error(ERR_NONFATAL,
2206 "Invalid size type for `%%local' missing directive");
2207 free_tlist(tt);
2208 free_tlist(origline);
2209 return DIRECTIVE_FOUND;
2210 }
2211 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002212
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002213 /* Round up to even stack slots */
2214 size = (size+StackSize-1) & ~(StackSize-1);
2215
2216 offset += size; /* Negative offset, increment before */
2217
2218 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002219 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2220 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002221 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002222
H. Peter Anvine2c80182005-01-15 22:15:51 +00002223 /* Now define the assign to setup the enter_c macro correctly */
2224 snprintf(directive, sizeof(directive),
2225 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002226 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002227
H. Peter Anvine2c80182005-01-15 22:15:51 +00002228 /* Move to the next argument in the list */
2229 tline = tline->next;
2230 if (tline && tline->type == TOK_WHITESPACE)
2231 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002232 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2233 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002234 free_tlist(origline);
2235 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002236
H. Peter Anvine2c80182005-01-15 22:15:51 +00002237 case PP_CLEAR:
2238 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002239 error(ERR_WARNING|ERR_PASS1,
2240 "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002241 free_macros();
2242 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002243 free_tlist(origline);
2244 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002245
H. Peter Anvin418ca702008-05-30 10:42:30 -07002246 case PP_DEPEND:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002247 t = tline->next = expand_smacro(tline->next);
2248 skip_white_(t);
2249 if (!t || (t->type != TOK_STRING &&
2250 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002251 error(ERR_NONFATAL, "`%%depend' expects a file name");
2252 free_tlist(origline);
2253 return DIRECTIVE_FOUND; /* but we did _something_ */
2254 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002255 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002256 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002257 "trailing garbage after `%%depend' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002258 p = t->text;
2259 if (t->type != TOK_INTERNAL_STRING)
2260 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002261 if (dephead && !in_list(*dephead, p)) {
2262 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2263 sl->next = NULL;
2264 strcpy(sl->str, p);
2265 *deptail = sl;
2266 deptail = &sl->next;
2267 }
2268 free_tlist(origline);
2269 return DIRECTIVE_FOUND;
2270
2271 case PP_INCLUDE:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002272 t = tline->next = expand_smacro(tline->next);
2273 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002274
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002275 if (!t || (t->type != TOK_STRING &&
2276 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002277 error(ERR_NONFATAL, "`%%include' expects a file name");
2278 free_tlist(origline);
2279 return DIRECTIVE_FOUND; /* but we did _something_ */
2280 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002281 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002282 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002283 "trailing garbage after `%%include' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002284 p = t->text;
2285 if (t->type != TOK_INTERNAL_STRING)
2286 nasm_unquote(p, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002287 inc = nasm_malloc(sizeof(Include));
2288 inc->next = istk;
2289 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002290 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002291 if (!inc->fp) {
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002292 /* -MG given but file not found */
2293 nasm_free(inc);
2294 } else {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002295 inc->fname = src_set_fname(nasm_strdup(p));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002296 inc->lineno = src_set_linnum(0);
2297 inc->lineinc = 1;
2298 inc->expansion = NULL;
2299 inc->mstk = NULL;
2300 istk = inc;
2301 list->uplevel(LIST_INCLUDE);
2302 }
2303 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002304 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002305
H. Peter Anvind2456592008-06-19 15:04:18 -07002306 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002307 {
H. Peter Anvina70547f2008-07-19 21:44:26 -07002308 static macros_t *use_pkg;
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002309 const char *pkg_macro;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002310
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002311 tline = tline->next;
2312 skip_white_(tline);
2313 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002314
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002315 if (!tline || (tline->type != TOK_STRING &&
2316 tline->type != TOK_INTERNAL_STRING &&
2317 tline->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 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002322 if (tline->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 Anvin264b7b92008-10-24 16:38:17 -07002325 if (tline->type == TOK_STRING)
2326 nasm_unquote(tline->text, NULL);
2327 use_pkg = nasm_stdmac_find_package(tline->text);
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002328 if (!use_pkg)
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002329 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->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:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002340 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002341 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002342 tline = tline->next;
2343 skip_white_(tline);
2344 tline = expand_id(tline);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002345 if (tline) {
2346 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin42b56392008-10-24 16:24:21 -07002347 error(ERR_NONFATAL, "`%s' expects a context identifier",
2348 pp_directives[i]);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002349 free_tlist(origline);
2350 return DIRECTIVE_FOUND; /* but we did _something_ */
2351 }
2352 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002353 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin42b56392008-10-24 16:24:21 -07002354 "trailing garbage after `%s' ignored",
2355 pp_directives[i]);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002356 p = nasm_strdup(tline->text);
2357 } else {
H. Peter Anvin42b56392008-10-24 16:24:21 -07002358 p = NULL; /* Anonymous */
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002359 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002360
2361 if (i == PP_PUSH) {
2362 ctx = nasm_malloc(sizeof(Context));
2363 ctx->next = cstk;
2364 hash_init(&ctx->localmac, HASH_SMALL);
2365 ctx->name = p;
2366 ctx->number = unique++;
2367 cstk = ctx;
2368 } else {
2369 /* %pop or %repl */
2370 if (!cstk) {
2371 error(ERR_NONFATAL, "`%s': context stack is empty",
2372 pp_directives[i]);
2373 } else if (i == PP_POP) {
2374 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2375 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2376 "expected %s",
2377 cstk->name ? cstk->name : "anonymous", p);
2378 else
2379 ctx_pop();
2380 } else {
2381 /* i == PP_REPL */
2382 nasm_free(cstk->name);
2383 cstk->name = p;
2384 p = NULL;
2385 }
2386 nasm_free(p);
2387 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002388 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002389 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002390 case PP_FATAL:
2391 severity = ERR_FATAL|ERR_NO_SEVERITY;
2392 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002393 case PP_ERROR:
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002394 severity = ERR_NONFATAL|ERR_NO_SEVERITY;
2395 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002396 case PP_WARNING:
H. Peter Anvin2f160432008-09-30 16:39:17 -07002397 severity = ERR_WARNING|ERR_NO_SEVERITY|ERR_WARN_USER;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002398 goto issue_error;
2399
2400 issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002401 {
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002402 /* Only error out if this is the final pass */
2403 if (pass != 2 && i != PP_FATAL)
2404 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002405
H. Peter Anvine2c80182005-01-15 22:15:51 +00002406 tline->next = expand_smacro(tline->next);
2407 tline = tline->next;
2408 skip_white_(tline);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002409 t = tline ? tline->next : NULL;
2410 skip_white_(t);
2411 if (tok_type_(tline, TOK_STRING) && !t) {
2412 /* The line contains only a quoted string */
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002413 p = tline->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002414 nasm_unquote(p, NULL);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002415 error(severity, "%s: %s", pp_directives[i], p);
2416 } else {
2417 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002418 p = detoken(tline, false);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002419 error(severity, "%s: %s", pp_directives[i], p);
2420 nasm_free(p);
H. Peter Anvind2456592008-06-19 15:04:18 -07002421 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002422 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002423 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002424 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002425
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002426 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002427 if (istk->conds && !emitting(istk->conds->state))
2428 j = COND_NEVER;
2429 else {
2430 j = if_condition(tline->next, i);
2431 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002432 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2433 }
2434 cond = nasm_malloc(sizeof(Cond));
2435 cond->next = istk->conds;
2436 cond->state = j;
2437 istk->conds = cond;
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002438 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002439 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002440
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002441 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002442 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002443 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002444 switch(istk->conds->state) {
2445 case COND_IF_TRUE:
2446 istk->conds->state = COND_DONE;
2447 break;
2448
2449 case COND_DONE:
2450 case COND_NEVER:
2451 break;
2452
2453 case COND_ELSE_TRUE:
2454 case COND_ELSE_FALSE:
H. Peter Anvin917a3492008-09-24 09:14:49 -07002455 error_precond(ERR_WARNING|ERR_PASS1,
2456 "`%%elif' after `%%else' ignored");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002457 istk->conds->state = COND_NEVER;
2458 break;
2459
2460 case COND_IF_FALSE:
2461 /*
2462 * IMPORTANT: In the case of %if, we will already have
2463 * called expand_mmac_params(); however, if we're
2464 * processing an %elif we must have been in a
2465 * non-emitting mode, which would have inhibited
H. Peter Anvin67c63722008-10-26 23:49:00 -07002466 * the normal invocation of expand_mmac_params().
2467 * Therefore, we have to do it explicitly here.
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002468 */
H. Peter Anvin67c63722008-10-26 23:49:00 -07002469 j = if_condition(expand_mmac_params(tline->next), i);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002470 tline->next = NULL; /* it got freed */
2471 istk->conds->state =
2472 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2473 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002474 }
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002475 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002476 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002477
H. Peter Anvine2c80182005-01-15 22:15:51 +00002478 case PP_ELSE:
2479 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002480 error_precond(ERR_WARNING|ERR_PASS1,
2481 "trailing garbage after `%%else' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002482 if (!istk->conds)
2483 error(ERR_FATAL, "`%%else': no matching `%%if'");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002484 switch(istk->conds->state) {
2485 case COND_IF_TRUE:
2486 case COND_DONE:
2487 istk->conds->state = COND_ELSE_FALSE;
2488 break;
2489
2490 case COND_NEVER:
2491 break;
2492
2493 case COND_IF_FALSE:
2494 istk->conds->state = COND_ELSE_TRUE;
2495 break;
2496
2497 case COND_ELSE_TRUE:
2498 case COND_ELSE_FALSE:
H. Peter Anvin917a3492008-09-24 09:14:49 -07002499 error_precond(ERR_WARNING|ERR_PASS1,
2500 "`%%else' after `%%else' ignored.");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002501 istk->conds->state = COND_NEVER;
2502 break;
2503 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002504 free_tlist(origline);
2505 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002506
H. Peter Anvine2c80182005-01-15 22:15:51 +00002507 case PP_ENDIF:
2508 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002509 error_precond(ERR_WARNING|ERR_PASS1,
2510 "trailing garbage after `%%endif' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002511 if (!istk->conds)
2512 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2513 cond = istk->conds;
2514 istk->conds = cond->next;
2515 nasm_free(cond);
2516 free_tlist(origline);
2517 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002518
H. Peter Anvine2c80182005-01-15 22:15:51 +00002519 case PP_MACRO:
2520 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002521 if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002522 error(ERR_FATAL,
2523 "`%%%smacro': already defining a macro",
2524 (i == PP_IMACRO ? "i" : ""));
H. Peter Anvina26433d2008-07-16 14:40:01 -07002525 return DIRECTIVE_FOUND;
2526 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002527 defining = nasm_malloc(sizeof(MMacro));
H. Peter Anvina26433d2008-07-16 14:40:01 -07002528 defining->casesense = (i == PP_MACRO);
2529 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2530 nasm_free(defining);
2531 defining = NULL;
2532 return DIRECTIVE_FOUND;
2533 }
2534
H. Peter Anvin166c2472008-05-28 12:28:58 -07002535 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002536 while (mmac) {
2537 if (!strcmp(mmac->name, defining->name) &&
2538 (mmac->nparam_min <= defining->nparam_max
2539 || defining->plus)
2540 && (defining->nparam_min <= mmac->nparam_max
2541 || mmac->plus)) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002542 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002543 "redefining multi-line macro `%s'", defining->name);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002544 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002545 }
2546 mmac = mmac->next;
2547 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002548 free_tlist(origline);
2549 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002550
H. Peter Anvine2c80182005-01-15 22:15:51 +00002551 case PP_ENDM:
2552 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002553 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002554 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2555 return DIRECTIVE_FOUND;
2556 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002557 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002558 defining->next = *mmhead;
2559 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002560 defining = NULL;
2561 free_tlist(origline);
2562 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002563
H. Peter Anvina26433d2008-07-16 14:40:01 -07002564 case PP_UNMACRO:
2565 case PP_UNIMACRO:
2566 {
2567 MMacro **mmac_p;
2568 MMacro spec;
2569
2570 spec.casesense = (i == PP_UNMACRO);
2571 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2572 return DIRECTIVE_FOUND;
2573 }
2574 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2575 while (mmac_p && *mmac_p) {
2576 mmac = *mmac_p;
2577 if (mmac->casesense == spec.casesense &&
2578 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2579 mmac->nparam_min == spec.nparam_min &&
2580 mmac->nparam_max == spec.nparam_max &&
2581 mmac->plus == spec.plus) {
2582 *mmac_p = mmac->next;
2583 free_mmacro(mmac);
2584 } else {
2585 mmac_p = &mmac->next;
2586 }
2587 }
2588 free_tlist(origline);
2589 free_tlist(spec.dlist);
2590 return DIRECTIVE_FOUND;
2591 }
2592
H. Peter Anvine2c80182005-01-15 22:15:51 +00002593 case PP_ROTATE:
2594 if (tline->next && tline->next->type == TOK_WHITESPACE)
2595 tline = tline->next;
2596 if (tline->next == NULL) {
2597 free_tlist(origline);
2598 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2599 return DIRECTIVE_FOUND;
2600 }
2601 t = expand_smacro(tline->next);
2602 tline->next = NULL;
2603 free_tlist(origline);
2604 tline = t;
2605 tptr = &t;
2606 tokval.t_type = TOKEN_INVALID;
2607 evalresult =
2608 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2609 free_tlist(tline);
2610 if (!evalresult)
2611 return DIRECTIVE_FOUND;
2612 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002613 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002614 "trailing garbage after expression ignored");
2615 if (!is_simple(evalresult)) {
2616 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2617 return DIRECTIVE_FOUND;
2618 }
2619 mmac = istk->mstk;
2620 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2621 mmac = mmac->next_active;
2622 if (!mmac) {
2623 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2624 } else if (mmac->nparam == 0) {
2625 error(ERR_NONFATAL,
2626 "`%%rotate' invoked within macro without parameters");
2627 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002628 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002629
H. Peter Anvin25a99342007-09-22 17:45:45 -07002630 rotate %= (int)mmac->nparam;
2631 if (rotate < 0)
2632 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002633
H. Peter Anvin25a99342007-09-22 17:45:45 -07002634 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002635 }
2636 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002637
H. Peter Anvine2c80182005-01-15 22:15:51 +00002638 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002639 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002640 do {
2641 tline = tline->next;
2642 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002643
H. Peter Anvine2c80182005-01-15 22:15:51 +00002644 if (tok_type_(tline, TOK_ID) &&
2645 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002646 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002647 do {
2648 tline = tline->next;
2649 } while (tok_type_(tline, TOK_WHITESPACE));
2650 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002651
H. Peter Anvine2c80182005-01-15 22:15:51 +00002652 if (tline) {
2653 t = expand_smacro(tline);
2654 tptr = &t;
2655 tokval.t_type = TOKEN_INVALID;
2656 evalresult =
2657 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2658 if (!evalresult) {
2659 free_tlist(origline);
2660 return DIRECTIVE_FOUND;
2661 }
2662 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002663 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002664 "trailing garbage after expression ignored");
2665 if (!is_simple(evalresult)) {
2666 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2667 return DIRECTIVE_FOUND;
2668 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002669 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002670 } else {
2671 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002672 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002673 }
2674 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002675
H. Peter Anvine2c80182005-01-15 22:15:51 +00002676 tmp_defining = defining;
2677 defining = nasm_malloc(sizeof(MMacro));
2678 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002679 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002680 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002681 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002682 defining->in_progress = count;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002683 defining->nparam_min = defining->nparam_max = 0;
2684 defining->defaults = NULL;
2685 defining->dlist = NULL;
2686 defining->expansion = NULL;
2687 defining->next_active = istk->mstk;
2688 defining->rep_nest = tmp_defining;
2689 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002690
H. Peter Anvine2c80182005-01-15 22:15:51 +00002691 case PP_ENDREP:
2692 if (!defining || defining->name) {
2693 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2694 return DIRECTIVE_FOUND;
2695 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002696
H. Peter Anvine2c80182005-01-15 22:15:51 +00002697 /*
2698 * Now we have a "macro" defined - although it has no name
2699 * and we won't be entering it in the hash tables - we must
2700 * push a macro-end marker for it on to istk->expansion.
2701 * After that, it will take care of propagating itself (a
2702 * macro-end marker line for a macro which is really a %rep
2703 * block will cause the macro to be re-expanded, complete
2704 * with another macro-end marker to ensure the process
2705 * continues) until the whole expansion is forcibly removed
2706 * from istk->expansion by a %exitrep.
2707 */
2708 l = nasm_malloc(sizeof(Line));
2709 l->next = istk->expansion;
2710 l->finishes = defining;
2711 l->first = NULL;
2712 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002713
H. Peter Anvine2c80182005-01-15 22:15:51 +00002714 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002715
H. Peter Anvine2c80182005-01-15 22:15:51 +00002716 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2717 tmp_defining = defining;
2718 defining = defining->rep_nest;
2719 free_tlist(origline);
2720 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002721
H. Peter Anvine2c80182005-01-15 22:15:51 +00002722 case PP_EXITREP:
2723 /*
2724 * We must search along istk->expansion until we hit a
2725 * macro-end marker for a macro with no name. Then we set
2726 * its `in_progress' flag to 0.
2727 */
2728 for (l = istk->expansion; l; l = l->next)
2729 if (l->finishes && !l->finishes->name)
H. Peter Anvinca348b62008-07-23 10:49:26 -04002730 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002731
H. Peter Anvine2c80182005-01-15 22:15:51 +00002732 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07002733 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002734 else
2735 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2736 free_tlist(origline);
2737 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002738
H. Peter Anvine2c80182005-01-15 22:15:51 +00002739 case PP_XDEFINE:
2740 case PP_IXDEFINE:
2741 case PP_DEFINE:
2742 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002743 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002744
H. Peter Anvine2c80182005-01-15 22:15:51 +00002745 tline = tline->next;
2746 skip_white_(tline);
2747 tline = expand_id(tline);
2748 if (!tline || (tline->type != TOK_ID &&
2749 (tline->type != TOK_PREPROC_ID ||
2750 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002751 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2752 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002753 free_tlist(origline);
2754 return DIRECTIVE_FOUND;
2755 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002756
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002757 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002758
H. Peter Anvine2c80182005-01-15 22:15:51 +00002759 mname = tline->text;
2760 last = tline;
2761 param_start = tline = tline->next;
2762 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002763
H. Peter Anvine2c80182005-01-15 22:15:51 +00002764 /* Expand the macro definition now for %xdefine and %ixdefine */
2765 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2766 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002767
H. Peter Anvine2c80182005-01-15 22:15:51 +00002768 if (tok_is_(tline, "(")) {
2769 /*
2770 * This macro has parameters.
2771 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002772
H. Peter Anvine2c80182005-01-15 22:15:51 +00002773 tline = tline->next;
2774 while (1) {
2775 skip_white_(tline);
2776 if (!tline) {
2777 error(ERR_NONFATAL, "parameter identifier expected");
2778 free_tlist(origline);
2779 return DIRECTIVE_FOUND;
2780 }
2781 if (tline->type != TOK_ID) {
2782 error(ERR_NONFATAL,
2783 "`%s': parameter identifier expected",
2784 tline->text);
2785 free_tlist(origline);
2786 return DIRECTIVE_FOUND;
2787 }
2788 tline->type = TOK_SMAC_PARAM + nparam++;
2789 tline = tline->next;
2790 skip_white_(tline);
2791 if (tok_is_(tline, ",")) {
2792 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04002793 } else {
2794 if (!tok_is_(tline, ")")) {
2795 error(ERR_NONFATAL,
2796 "`)' expected to terminate macro template");
2797 free_tlist(origline);
2798 return DIRECTIVE_FOUND;
2799 }
2800 break;
2801 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002802 }
2803 last = tline;
2804 tline = tline->next;
2805 }
2806 if (tok_type_(tline, TOK_WHITESPACE))
2807 last = tline, tline = tline->next;
2808 macro_start = NULL;
2809 last->next = NULL;
2810 t = tline;
2811 while (t) {
2812 if (t->type == TOK_ID) {
2813 for (tt = param_start; tt; tt = tt->next)
2814 if (tt->type >= TOK_SMAC_PARAM &&
2815 !strcmp(tt->text, t->text))
2816 t->type = tt->type;
2817 }
2818 tt = t->next;
2819 t->next = macro_start;
2820 macro_start = t;
2821 t = tt;
2822 }
2823 /*
2824 * Good. We now have a macro name, a parameter count, and a
2825 * token list (in reverse order) for an expansion. We ought
2826 * to be OK just to create an SMacro, store it, and let
2827 * free_tlist have the rest of the line (which we have
2828 * carefully re-terminated after chopping off the expansion
2829 * from the end).
2830 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002831 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002832 free_tlist(origline);
2833 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002834
H. Peter Anvine2c80182005-01-15 22:15:51 +00002835 case PP_UNDEF:
2836 tline = tline->next;
2837 skip_white_(tline);
2838 tline = expand_id(tline);
2839 if (!tline || (tline->type != TOK_ID &&
2840 (tline->type != TOK_PREPROC_ID ||
2841 tline->text[1] != '$'))) {
2842 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2843 free_tlist(origline);
2844 return DIRECTIVE_FOUND;
2845 }
2846 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002847 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002848 "trailing garbage after macro name ignored");
2849 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002850
H. Peter Anvine2c80182005-01-15 22:15:51 +00002851 /* Find the context that symbol belongs to */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002852 ctx = get_ctx(tline->text, false);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002853 undef_smacro(ctx, tline->text);
2854 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002855 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002856
H. Peter Anvin9e200162008-06-04 17:23:14 -07002857 case PP_DEFSTR:
2858 case PP_IDEFSTR:
2859 casesense = (i == PP_DEFSTR);
2860
2861 tline = tline->next;
2862 skip_white_(tline);
2863 tline = expand_id(tline);
2864 if (!tline || (tline->type != TOK_ID &&
2865 (tline->type != TOK_PREPROC_ID ||
2866 tline->text[1] != '$'))) {
2867 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2868 pp_directives[i]);
2869 free_tlist(origline);
2870 return DIRECTIVE_FOUND;
2871 }
2872
2873 ctx = get_ctx(tline->text, false);
2874
2875 mname = tline->text;
2876 last = tline;
2877 tline = expand_smacro(tline->next);
2878 last->next = NULL;
2879
2880 while (tok_type_(tline, TOK_WHITESPACE))
2881 tline = delete_Token(tline);
2882
2883 p = detoken(tline, false);
2884 macro_start = nasm_malloc(sizeof(*macro_start));
2885 macro_start->next = NULL;
2886 macro_start->text = nasm_quote(p, strlen(p));
2887 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002888 macro_start->a.mac = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07002889 nasm_free(p);
2890
2891 /*
2892 * We now have a macro name, an implicit parameter count of
2893 * zero, and a string token to use as an expansion. Create
2894 * and store an SMacro.
2895 */
2896 define_smacro(ctx, mname, casesense, 0, macro_start);
2897 free_tlist(origline);
2898 return DIRECTIVE_FOUND;
2899
H. Peter Anvin418ca702008-05-30 10:42:30 -07002900 case PP_PATHSEARCH:
2901 {
2902 FILE *fp;
2903 StrList *xsl = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002904 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002905
2906 casesense = true;
2907
2908 tline = tline->next;
2909 skip_white_(tline);
2910 tline = expand_id(tline);
2911 if (!tline || (tline->type != TOK_ID &&
2912 (tline->type != TOK_PREPROC_ID ||
2913 tline->text[1] != '$'))) {
2914 error(ERR_NONFATAL,
2915 "`%%pathsearch' expects a macro identifier as first parameter");
2916 free_tlist(origline);
2917 return DIRECTIVE_FOUND;
2918 }
2919 ctx = get_ctx(tline->text, false);
2920
2921 mname = tline->text;
2922 last = tline;
2923 tline = expand_smacro(tline->next);
2924 last->next = NULL;
2925
2926 t = tline;
2927 while (tok_type_(t, TOK_WHITESPACE))
2928 t = t->next;
2929
2930 if (!t || (t->type != TOK_STRING &&
2931 t->type != TOK_INTERNAL_STRING)) {
2932 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
2933 free_tlist(tline);
2934 free_tlist(origline);
2935 return DIRECTIVE_FOUND; /* but we did _something_ */
2936 }
2937 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002938 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002939 "trailing garbage after `%%pathsearch' ignored");
H. Peter Anvin427cc912008-06-01 21:43:03 -07002940 p = t->text;
2941 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002942 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002943
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002944 fp = inc_fopen(p, &xsl, &xst, true);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002945 if (fp) {
2946 p = xsl->str;
2947 fclose(fp); /* Don't actually care about the file */
2948 }
2949 macro_start = nasm_malloc(sizeof(*macro_start));
2950 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002951 macro_start->text = nasm_quote(p, strlen(p));
H. Peter Anvin418ca702008-05-30 10:42:30 -07002952 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002953 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002954 if (xsl)
2955 nasm_free(xsl);
2956
2957 /*
2958 * We now have a macro name, an implicit parameter count of
2959 * zero, and a string token to use as an expansion. Create
2960 * and store an SMacro.
2961 */
2962 define_smacro(ctx, mname, casesense, 0, macro_start);
2963 free_tlist(tline);
2964 free_tlist(origline);
2965 return DIRECTIVE_FOUND;
2966 }
2967
H. Peter Anvine2c80182005-01-15 22:15:51 +00002968 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002969 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07002970
H. Peter Anvine2c80182005-01-15 22:15:51 +00002971 tline = tline->next;
2972 skip_white_(tline);
2973 tline = expand_id(tline);
2974 if (!tline || (tline->type != TOK_ID &&
2975 (tline->type != TOK_PREPROC_ID ||
2976 tline->text[1] != '$'))) {
2977 error(ERR_NONFATAL,
2978 "`%%strlen' expects a macro identifier as first parameter");
2979 free_tlist(origline);
2980 return DIRECTIVE_FOUND;
2981 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002982 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002983
H. Peter Anvine2c80182005-01-15 22:15:51 +00002984 mname = tline->text;
2985 last = tline;
2986 tline = expand_smacro(tline->next);
2987 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002988
H. Peter Anvine2c80182005-01-15 22:15:51 +00002989 t = tline;
2990 while (tok_type_(t, TOK_WHITESPACE))
2991 t = t->next;
2992 /* t should now point to the string */
2993 if (t->type != TOK_STRING) {
2994 error(ERR_NONFATAL,
2995 "`%%strlen` requires string as second parameter");
2996 free_tlist(tline);
2997 free_tlist(origline);
2998 return DIRECTIVE_FOUND;
2999 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003000
H. Peter Anvine2c80182005-01-15 22:15:51 +00003001 macro_start = nasm_malloc(sizeof(*macro_start));
3002 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003003 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003004 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003005
H. Peter Anvine2c80182005-01-15 22:15:51 +00003006 /*
3007 * We now have a macro name, an implicit parameter count of
3008 * zero, and a numeric token to use as an expansion. Create
3009 * and store an SMacro.
3010 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003011 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003012 free_tlist(tline);
3013 free_tlist(origline);
3014 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003015
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003016 case PP_STRCAT:
3017 casesense = true;
3018
3019 tline = tline->next;
3020 skip_white_(tline);
3021 tline = expand_id(tline);
3022 if (!tline || (tline->type != TOK_ID &&
3023 (tline->type != TOK_PREPROC_ID ||
3024 tline->text[1] != '$'))) {
3025 error(ERR_NONFATAL,
3026 "`%%strcat' expects a macro identifier as first parameter");
3027 free_tlist(origline);
3028 return DIRECTIVE_FOUND;
3029 }
3030 ctx = get_ctx(tline->text, false);
3031
3032 mname = tline->text;
3033 last = tline;
3034 tline = expand_smacro(tline->next);
3035 last->next = NULL;
3036
3037 len = 0;
3038 for (t = tline; t; t = t->next) {
3039 switch (t->type) {
3040 case TOK_WHITESPACE:
3041 break;
3042 case TOK_STRING:
3043 len += t->a.len = nasm_unquote(t->text, NULL);
3044 break;
H. Peter Anvinaccf4332008-07-01 21:42:08 -07003045 case TOK_OTHER:
3046 if (!strcmp(t->text, ",")) /* permit comma separators */
3047 break;
3048 /* else fall through */
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003049 default:
3050 error(ERR_NONFATAL,
3051 "non-string passed to `%%strcat' (%d)", t->type);
3052 free_tlist(tline);
3053 free_tlist(origline);
3054 return DIRECTIVE_FOUND;
3055 }
3056 }
3057
3058 p = pp = nasm_malloc(len);
3059 t = tline;
3060 for (t = tline; t; t = t->next) {
3061 if (t->type == TOK_STRING) {
3062 memcpy(p, t->text, t->a.len);
3063 p += t->a.len;
3064 }
3065 }
3066
3067 /*
3068 * We now have a macro name, an implicit parameter count of
3069 * zero, and a numeric token to use as an expansion. Create
3070 * and store an SMacro.
3071 */
3072 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3073 macro_start->text = nasm_quote(pp, len);
3074 nasm_free(pp);
3075 define_smacro(ctx, mname, casesense, 0, macro_start);
3076 free_tlist(tline);
3077 free_tlist(origline);
3078 return DIRECTIVE_FOUND;
3079
H. Peter Anvine2c80182005-01-15 22:15:51 +00003080 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003081 {
3082 int64_t a1, a2;
3083 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003084
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003085 casesense = true;
3086
H. Peter Anvine2c80182005-01-15 22:15:51 +00003087 tline = tline->next;
3088 skip_white_(tline);
3089 tline = expand_id(tline);
3090 if (!tline || (tline->type != TOK_ID &&
3091 (tline->type != TOK_PREPROC_ID ||
3092 tline->text[1] != '$'))) {
3093 error(ERR_NONFATAL,
3094 "`%%substr' expects a macro identifier as first parameter");
3095 free_tlist(origline);
3096 return DIRECTIVE_FOUND;
3097 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003098 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003099
H. Peter Anvine2c80182005-01-15 22:15:51 +00003100 mname = tline->text;
3101 last = tline;
3102 tline = expand_smacro(tline->next);
3103 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003104
H. Peter Anvine2c80182005-01-15 22:15:51 +00003105 t = tline->next;
3106 while (tok_type_(t, TOK_WHITESPACE))
3107 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003108
H. Peter Anvine2c80182005-01-15 22:15:51 +00003109 /* t should now point to the string */
3110 if (t->type != TOK_STRING) {
3111 error(ERR_NONFATAL,
3112 "`%%substr` requires string as second parameter");
3113 free_tlist(tline);
3114 free_tlist(origline);
3115 return DIRECTIVE_FOUND;
3116 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003117
H. Peter Anvine2c80182005-01-15 22:15:51 +00003118 tt = t->next;
3119 tptr = &tt;
3120 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003121 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3122 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003123 if (!evalresult) {
3124 free_tlist(tline);
3125 free_tlist(origline);
3126 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003127 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003128 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3129 free_tlist(tline);
3130 free_tlist(origline);
3131 return DIRECTIVE_FOUND;
3132 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003133 a1 = evalresult->value-1;
3134
3135 while (tok_type_(tt, TOK_WHITESPACE))
3136 tt = tt->next;
3137 if (!tt) {
3138 a2 = 1; /* Backwards compatibility: one character */
3139 } else {
3140 tokval.t_type = TOKEN_INVALID;
3141 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3142 pass, error, NULL);
3143 if (!evalresult) {
3144 free_tlist(tline);
3145 free_tlist(origline);
3146 return DIRECTIVE_FOUND;
3147 } else if (!is_simple(evalresult)) {
3148 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3149 free_tlist(tline);
3150 free_tlist(origline);
3151 return DIRECTIVE_FOUND;
3152 }
3153 a2 = evalresult->value;
3154 }
3155
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003156 len = nasm_unquote(t->text, NULL);
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003157 if (a2 < 0)
3158 a2 = a2+1+len-a1;
3159 if (a1+a2 > (int64_t)len)
3160 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003161
H. Peter Anvine2c80182005-01-15 22:15:51 +00003162 macro_start = nasm_malloc(sizeof(*macro_start));
3163 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003164 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003165 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003166 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003167
H. Peter Anvine2c80182005-01-15 22:15:51 +00003168 /*
3169 * We now have a macro name, an implicit parameter count of
3170 * zero, and a numeric token to use as an expansion. Create
3171 * and store an SMacro.
3172 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003173 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003174 free_tlist(tline);
3175 free_tlist(origline);
3176 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003177 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003178
H. Peter Anvine2c80182005-01-15 22:15:51 +00003179 case PP_ASSIGN:
3180 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003181 casesense = (i == PP_ASSIGN);
3182
H. Peter Anvine2c80182005-01-15 22:15:51 +00003183 tline = tline->next;
3184 skip_white_(tline);
3185 tline = expand_id(tline);
3186 if (!tline || (tline->type != TOK_ID &&
3187 (tline->type != TOK_PREPROC_ID ||
3188 tline->text[1] != '$'))) {
3189 error(ERR_NONFATAL,
3190 "`%%%sassign' expects a macro identifier",
3191 (i == PP_IASSIGN ? "i" : ""));
3192 free_tlist(origline);
3193 return DIRECTIVE_FOUND;
3194 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003195 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003196
H. Peter Anvine2c80182005-01-15 22:15:51 +00003197 mname = tline->text;
3198 last = tline;
3199 tline = expand_smacro(tline->next);
3200 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003201
H. Peter Anvine2c80182005-01-15 22:15:51 +00003202 t = tline;
3203 tptr = &t;
3204 tokval.t_type = TOKEN_INVALID;
3205 evalresult =
3206 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3207 free_tlist(tline);
3208 if (!evalresult) {
3209 free_tlist(origline);
3210 return DIRECTIVE_FOUND;
3211 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003212
H. Peter Anvine2c80182005-01-15 22:15:51 +00003213 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003214 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003215 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003216
H. Peter Anvine2c80182005-01-15 22:15:51 +00003217 if (!is_simple(evalresult)) {
3218 error(ERR_NONFATAL,
3219 "non-constant value given to `%%%sassign'",
3220 (i == PP_IASSIGN ? "i" : ""));
3221 free_tlist(origline);
3222 return DIRECTIVE_FOUND;
3223 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003224
H. Peter Anvine2c80182005-01-15 22:15:51 +00003225 macro_start = nasm_malloc(sizeof(*macro_start));
3226 macro_start->next = NULL;
3227 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003228 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003229
H. Peter Anvine2c80182005-01-15 22:15:51 +00003230 /*
3231 * We now have a macro name, an implicit parameter count of
3232 * zero, and a numeric token to use as an expansion. Create
3233 * and store an SMacro.
3234 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003235 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003236 free_tlist(origline);
3237 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003238
H. Peter Anvine2c80182005-01-15 22:15:51 +00003239 case PP_LINE:
3240 /*
3241 * Syntax is `%line nnn[+mmm] [filename]'
3242 */
3243 tline = tline->next;
3244 skip_white_(tline);
3245 if (!tok_type_(tline, TOK_NUMBER)) {
3246 error(ERR_NONFATAL, "`%%line' expects line number");
3247 free_tlist(origline);
3248 return DIRECTIVE_FOUND;
3249 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003250 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003251 m = 1;
3252 tline = tline->next;
3253 if (tok_is_(tline, "+")) {
3254 tline = tline->next;
3255 if (!tok_type_(tline, TOK_NUMBER)) {
3256 error(ERR_NONFATAL, "`%%line' expects line increment");
3257 free_tlist(origline);
3258 return DIRECTIVE_FOUND;
3259 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003260 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003261 tline = tline->next;
3262 }
3263 skip_white_(tline);
3264 src_set_linnum(k);
3265 istk->lineinc = m;
3266 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003267 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003268 }
3269 free_tlist(origline);
3270 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003271
H. Peter Anvine2c80182005-01-15 22:15:51 +00003272 default:
3273 error(ERR_FATAL,
3274 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003275 pp_directives[i]);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003276 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003277 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003278}
3279
3280/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003281 * Ensure that a macro parameter contains a condition code and
3282 * nothing else. Return the condition code index if so, or -1
3283 * otherwise.
3284 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003285static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003286{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003287 Token *tt;
3288 int i, j, k, m;
3289
H. Peter Anvin25a99342007-09-22 17:45:45 -07003290 if (!t)
3291 return -1; /* Probably a %+ without a space */
3292
H. Peter Anvineba20a72002-04-30 20:53:55 +00003293 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003294 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003295 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003296 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003297 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003298 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003299 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003300
3301 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00003302 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003303 while (j - i > 1) {
3304 k = (j + i) / 2;
3305 m = nasm_stricmp(t->text, conditions[k]);
3306 if (m == 0) {
3307 i = k;
3308 j = -2;
3309 break;
3310 } else if (m < 0) {
3311 j = k;
3312 } else
3313 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003314 }
3315 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003316 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003317 return i;
3318}
3319
3320/*
3321 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003322 * %-n) and MMacro-local identifiers (%%foo) as well as
3323 * macro indirection (%[...]).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003324 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003325static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003326{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003327 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003328
3329 tail = &thead;
3330 thead = NULL;
3331
H. Peter Anvine2c80182005-01-15 22:15:51 +00003332 while (tline) {
3333 if (tline->type == TOK_PREPROC_ID &&
3334 (((tline->text[1] == '+' || tline->text[1] == '-')
3335 && tline->text[2]) || tline->text[1] == '%'
3336 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003337 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003338 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003339 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003340 unsigned int n;
3341 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003342 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003343
H. Peter Anvine2c80182005-01-15 22:15:51 +00003344 t = tline;
3345 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003346
H. Peter Anvine2c80182005-01-15 22:15:51 +00003347 mac = istk->mstk;
3348 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3349 mac = mac->next_active;
3350 if (!mac)
3351 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3352 else
3353 switch (t->text[1]) {
3354 /*
3355 * We have to make a substitution of one of the
3356 * forms %1, %-1, %+1, %%foo, %0.
3357 */
3358 case '0':
3359 type = TOK_NUMBER;
3360 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3361 text = nasm_strdup(tmpbuf);
3362 break;
3363 case '%':
3364 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003365 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00003366 mac->unique);
3367 text = nasm_strcat(tmpbuf, t->text + 2);
3368 break;
3369 case '-':
3370 n = atoi(t->text + 2) - 1;
3371 if (n >= mac->nparam)
3372 tt = NULL;
3373 else {
3374 if (mac->nparam > 1)
3375 n = (n + mac->rotate) % mac->nparam;
3376 tt = mac->params[n];
3377 }
3378 cc = find_cc(tt);
3379 if (cc == -1) {
3380 error(ERR_NONFATAL,
3381 "macro parameter %d is not a condition code",
3382 n + 1);
3383 text = NULL;
3384 } else {
3385 type = TOK_ID;
3386 if (inverse_ccs[cc] == -1) {
3387 error(ERR_NONFATAL,
3388 "condition code `%s' is not invertible",
3389 conditions[cc]);
3390 text = NULL;
3391 } else
H. Peter Anvin67c63722008-10-26 23:49:00 -07003392 text = nasm_strdup(conditions[inverse_ccs[cc]]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003393 }
3394 break;
3395 case '+':
3396 n = atoi(t->text + 2) - 1;
3397 if (n >= mac->nparam)
3398 tt = NULL;
3399 else {
3400 if (mac->nparam > 1)
3401 n = (n + mac->rotate) % mac->nparam;
3402 tt = mac->params[n];
3403 }
3404 cc = find_cc(tt);
3405 if (cc == -1) {
3406 error(ERR_NONFATAL,
3407 "macro parameter %d is not a condition code",
3408 n + 1);
3409 text = NULL;
3410 } else {
3411 type = TOK_ID;
3412 text = nasm_strdup(conditions[cc]);
3413 }
3414 break;
3415 default:
3416 n = atoi(t->text + 1) - 1;
3417 if (n >= mac->nparam)
3418 tt = NULL;
3419 else {
3420 if (mac->nparam > 1)
3421 n = (n + mac->rotate) % mac->nparam;
3422 tt = mac->params[n];
3423 }
3424 if (tt) {
3425 for (i = 0; i < mac->paramlen[n]; i++) {
3426 *tail = new_Token(NULL, tt->type, tt->text, 0);
3427 tail = &(*tail)->next;
3428 tt = tt->next;
3429 }
3430 }
3431 text = NULL; /* we've done it here */
3432 break;
3433 }
3434 if (!text) {
3435 delete_Token(t);
3436 } else {
3437 *tail = t;
3438 tail = &t->next;
3439 t->type = type;
3440 nasm_free(t->text);
3441 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003442 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003443 }
3444 continue;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003445 } else if (tline->type == TOK_INDIRECT) {
3446 t = tline;
3447 tline = tline->next;
3448 tt = tokenize(t->text);
3449 tt = expand_mmac_params(tt);
3450 tt = expand_smacro(tt);
3451 *tail = tt;
3452 while (tt) {
3453 tt->a.mac = NULL; /* Necessary? */
3454 tail = &tt->next;
3455 tt = tt->next;
3456 }
3457 delete_Token(t);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003458 } else {
3459 t = *tail = tline;
3460 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003461 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003462 tail = &t->next;
3463 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003464 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003465 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003466
3467 /* Now handle token pasting... */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003468 t = thead;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003469 while (t && (tt = 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);
H. Peter Anvin67c63722008-10-26 23:49:00 -07003474 } else {
3475 t = tt;
3476 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003477 break;
3478 case TOK_ID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003479 case TOK_NUMBER:
H. Peter Anvin992fe752008-10-19 15:45:05 -07003480 if (tt->type == t->type || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003481 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003482 nasm_free(t->text);
3483 t->text = tmp;
3484 t->next = delete_Token(tt);
H. Peter Anvin67c63722008-10-26 23:49:00 -07003485 } else {
3486 t = tt;
3487 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003488 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003489 default:
H. Peter Anvin67c63722008-10-26 23:49:00 -07003490 t = tt;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003491 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003492 }
H. Peter Anvin67c63722008-10-26 23:49:00 -07003493 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003494 return thead;
3495}
3496
3497/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003498 * Expand all single-line macro calls made in the given line.
3499 * Return the expanded version of the line. The original is deemed
3500 * to be destroyed in the process. (In reality we'll just move
3501 * Tokens from input to output a lot of the time, rather than
3502 * actually bothering to destroy and replicate.)
3503 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003504#define DEADMAN_LIMIT (1 << 20)
3505
H. Peter Anvine2c80182005-01-15 22:15:51 +00003506static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003507{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003508 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003509 struct hash_table *smtbl;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003510 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003511 Token **params;
3512 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003513 unsigned int nparam, sparam;
3514 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003515 Token *org_tline = tline;
3516 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003517 char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003518 int deadman = DEADMAN_LIMIT;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003519
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003520 /*
3521 * Trick: we should avoid changing the start token pointer since it can
3522 * be contained in "next" field of other token. Because of this
3523 * we allocate a copy of first token and work with it; at the end of
3524 * routine we copy it back
3525 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003526 if (org_tline) {
3527 tline =
3528 new_Token(org_tline->next, org_tline->type, org_tline->text,
3529 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003530 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003531 nasm_free(org_tline->text);
3532 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003533 }
3534
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003535again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003536 tail = &thead;
3537 thead = NULL;
3538
H. Peter Anvine2c80182005-01-15 22:15:51 +00003539 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003540 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003541 error(ERR_NONFATAL, "interminable macro recursion");
3542 break;
3543 }
3544
H. Peter Anvine2c80182005-01-15 22:15:51 +00003545 if ((mname = tline->text)) {
3546 /* if this token is a local macro, look in local context */
H. Peter Anvinb037a672008-05-29 19:08:08 -07003547 ctx = NULL;
3548 smtbl = &smacros;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003549 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003550 ctx = get_ctx(mname, true);
H. Peter Anvinb037a672008-05-29 19:08:08 -07003551 if (ctx)
3552 smtbl = &ctx->localmac;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003553 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07003554 head = (SMacro *) hash_findix(smtbl, mname);
H. Peter Anvin072771e2008-05-22 13:17:51 -07003555
H. Peter Anvine2c80182005-01-15 22:15:51 +00003556 /*
3557 * We've hit an identifier. As in is_mmacro below, we first
3558 * check whether the identifier is a single-line macro at
3559 * all, then think about checking for parameters if
3560 * necessary.
3561 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003562 for (m = head; m; m = m->next)
3563 if (!mstrcmp(m->name, mname, m->casesense))
3564 break;
3565 if (m) {
3566 mstart = tline;
3567 params = NULL;
3568 paramsize = NULL;
3569 if (m->nparam == 0) {
3570 /*
3571 * Simple case: the macro is parameterless. Discard the
3572 * one token that the macro call took, and push the
3573 * expansion back on the to-do stack.
3574 */
3575 if (!m->expansion) {
3576 if (!strcmp("__FILE__", m->name)) {
3577 int32_t num = 0;
H. Peter Anvin932de6c2008-07-31 18:46:11 -07003578 char *file = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003579 src_get(&num, &file);
3580 tline->text = nasm_quote(file, strlen(file));
H. Peter Anvin97a23472007-09-16 17:57:25 -07003581 tline->type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003582 nasm_free(file);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003583 continue;
3584 }
3585 if (!strcmp("__LINE__", m->name)) {
3586 nasm_free(tline->text);
3587 make_tok_num(tline, src_get_linnum());
3588 continue;
3589 }
3590 if (!strcmp("__BITS__", m->name)) {
3591 nasm_free(tline->text);
3592 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003593 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003594 }
3595 tline = delete_Token(tline);
3596 continue;
3597 }
3598 } else {
3599 /*
3600 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003601 * exists and takes parameters. We must find the
3602 * parameters in the call, count them, find the SMacro
3603 * that corresponds to that form of the macro call, and
3604 * substitute for the parameters when we expand. What a
3605 * pain.
3606 */
3607 /*tline = tline->next;
3608 skip_white_(tline); */
3609 do {
3610 t = tline->next;
3611 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003612 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003613 t->text = NULL;
3614 t = tline->next = delete_Token(t);
3615 }
3616 tline = t;
3617 } while (tok_type_(tline, TOK_WHITESPACE));
3618 if (!tok_is_(tline, "(")) {
3619 /*
3620 * This macro wasn't called with parameters: ignore
3621 * the call. (Behaviour borrowed from gnu cpp.)
3622 */
3623 tline = mstart;
3624 m = NULL;
3625 } else {
3626 int paren = 0;
3627 int white = 0;
3628 brackets = 0;
3629 nparam = 0;
3630 sparam = PARAM_DELTA;
3631 params = nasm_malloc(sparam * sizeof(Token *));
3632 params[0] = tline->next;
3633 paramsize = nasm_malloc(sparam * sizeof(int));
3634 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003635 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003636 /*
3637 * For some unusual expansions
3638 * which concatenates function call
3639 */
3640 t = tline->next;
3641 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003642 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003643 t->text = NULL;
3644 t = tline->next = delete_Token(t);
3645 }
3646 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003647
H. Peter Anvine2c80182005-01-15 22:15:51 +00003648 if (!tline) {
3649 error(ERR_NONFATAL,
3650 "macro call expects terminating `)'");
3651 break;
3652 }
3653 if (tline->type == TOK_WHITESPACE
3654 && brackets <= 0) {
3655 if (paramsize[nparam])
3656 white++;
3657 else
3658 params[nparam] = tline->next;
3659 continue; /* parameter loop */
3660 }
3661 if (tline->type == TOK_OTHER
3662 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003663 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003664 if (ch == ',' && !paren && brackets <= 0) {
3665 if (++nparam >= sparam) {
3666 sparam += PARAM_DELTA;
3667 params = nasm_realloc(params,
3668 sparam *
3669 sizeof(Token
3670 *));
3671 paramsize =
3672 nasm_realloc(paramsize,
3673 sparam *
3674 sizeof(int));
3675 }
3676 params[nparam] = tline->next;
3677 paramsize[nparam] = 0;
3678 white = 0;
3679 continue; /* parameter loop */
3680 }
3681 if (ch == '{' &&
3682 (brackets > 0 || (brackets == 0 &&
3683 !paramsize[nparam])))
3684 {
3685 if (!(brackets++)) {
3686 params[nparam] = tline->next;
3687 continue; /* parameter loop */
3688 }
3689 }
3690 if (ch == '}' && brackets > 0)
3691 if (--brackets == 0) {
3692 brackets = -1;
3693 continue; /* parameter loop */
3694 }
3695 if (ch == '(' && !brackets)
3696 paren++;
3697 if (ch == ')' && brackets <= 0)
3698 if (--paren < 0)
3699 break;
3700 }
3701 if (brackets < 0) {
3702 brackets = 0;
3703 error(ERR_NONFATAL, "braces do not "
3704 "enclose all of macro parameter");
3705 }
3706 paramsize[nparam] += white + 1;
3707 white = 0;
3708 } /* parameter loop */
3709 nparam++;
3710 while (m && (m->nparam != nparam ||
3711 mstrcmp(m->name, mname,
3712 m->casesense)))
3713 m = m->next;
3714 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003715 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003716 "macro `%s' exists, "
3717 "but not taking %d parameters",
3718 mstart->text, nparam);
3719 }
3720 }
3721 if (m && m->in_progress)
3722 m = NULL;
3723 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003724 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003725 * Design question: should we handle !tline, which
3726 * indicates missing ')' here, or expand those
3727 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003728 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003729 */
3730 nasm_free(params);
3731 nasm_free(paramsize);
3732 tline = mstart;
3733 } else {
3734 /*
3735 * Expand the macro: we are placed on the last token of the
3736 * call, so that we can easily split the call from the
3737 * following tokens. We also start by pushing an SMAC_END
3738 * token for the cycle removal.
3739 */
3740 t = tline;
3741 if (t) {
3742 tline = t->next;
3743 t->next = NULL;
3744 }
3745 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003746 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003747 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003748 tline = tt;
3749 for (t = m->expansion; t; t = t->next) {
3750 if (t->type >= TOK_SMAC_PARAM) {
3751 Token *pcopy = tline, **ptail = &pcopy;
3752 Token *ttt, *pt;
3753 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003754
H. Peter Anvine2c80182005-01-15 22:15:51 +00003755 ttt = params[t->type - TOK_SMAC_PARAM];
3756 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3757 --i >= 0;) {
3758 pt = *ptail =
3759 new_Token(tline, ttt->type, ttt->text,
3760 0);
3761 ptail = &pt->next;
3762 ttt = ttt->next;
3763 }
3764 tline = pcopy;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -07003765 } else if (t->type == TOK_PREPROC_Q) {
3766 tt = new_Token(tline, TOK_ID, mname, 0);
3767 tline = tt;
3768 } else if (t->type == TOK_PREPROC_QQ) {
3769 tt = new_Token(tline, TOK_ID, m->name, 0);
3770 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003771 } else {
3772 tt = new_Token(tline, t->type, t->text, 0);
3773 tline = tt;
3774 }
3775 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003776
H. Peter Anvine2c80182005-01-15 22:15:51 +00003777 /*
3778 * Having done that, get rid of the macro call, and clean
3779 * up the parameters.
3780 */
3781 nasm_free(params);
3782 nasm_free(paramsize);
3783 free_tlist(mstart);
3784 continue; /* main token loop */
3785 }
3786 }
3787 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003788
H. Peter Anvine2c80182005-01-15 22:15:51 +00003789 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003790 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003791 tline = delete_Token(tline);
3792 } else {
3793 t = *tail = tline;
3794 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003795 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003796 t->next = NULL;
3797 tail = &t->next;
3798 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003799 }
3800
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003801 /*
3802 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003803 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003804 * TOK_IDs should be concatenated.
3805 * Also we look for %+ tokens and concatenate the tokens before and after
3806 * them (without white spaces in between).
3807 */
3808 t = thead;
3809 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003810 while (t) {
3811 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3812 t = t->next;
3813 if (!t || !t->next)
3814 break;
3815 if (t->next->type == TOK_ID ||
3816 t->next->type == TOK_PREPROC_ID ||
3817 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003818 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003819 nasm_free(t->text);
3820 t->next = delete_Token(t->next);
3821 t->text = p;
3822 rescan = 1;
3823 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3824 t->next->next->type == TOK_PREPROC_ID &&
3825 strcmp(t->next->next->text, "%+") == 0) {
3826 /* free the next whitespace, the %+ token and next whitespace */
3827 int i;
3828 for (i = 1; i <= 3; i++) {
3829 if (!t->next
3830 || (i != 2 && t->next->type != TOK_WHITESPACE))
3831 break;
3832 t->next = delete_Token(t->next);
3833 } /* endfor */
3834 } else
3835 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003836 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003837 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003838 if (rescan) {
3839 tline = thead;
3840 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003841 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003842
H. Peter Anvine2c80182005-01-15 22:15:51 +00003843 if (org_tline) {
3844 if (thead) {
3845 *org_tline = *thead;
3846 /* since we just gave text to org_line, don't free it */
3847 thead->text = NULL;
3848 delete_Token(thead);
3849 } else {
3850 /* the expression expanded to empty line;
3851 we can't return NULL for some reasons
3852 we just set the line to a single WHITESPACE token. */
3853 memset(org_tline, 0, sizeof(*org_tline));
3854 org_tline->text = NULL;
3855 org_tline->type = TOK_WHITESPACE;
3856 }
3857 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003858 }
3859
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003860 return thead;
3861}
3862
3863/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003864 * Similar to expand_smacro but used exclusively with macro identifiers
3865 * right before they are fetched in. The reason is that there can be
3866 * identifiers consisting of several subparts. We consider that if there
3867 * are more than one element forming the name, user wants a expansion,
3868 * otherwise it will be left as-is. Example:
3869 *
3870 * %define %$abc cde
3871 *
3872 * the identifier %$abc will be left as-is so that the handler for %define
3873 * will suck it and define the corresponding value. Other case:
3874 *
3875 * %define _%$abc cde
3876 *
3877 * In this case user wants name to be expanded *before* %define starts
3878 * working, so we'll expand %$abc into something (if it has a value;
3879 * otherwise it will be left as-is) then concatenate all successive
3880 * PP_IDs into one.
3881 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003882static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003883{
3884 Token *cur, *oldnext = NULL;
3885
H. Peter Anvin734b1882002-04-30 21:01:08 +00003886 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003887 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003888
3889 cur = tline;
3890 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003891 (cur->next->type == TOK_ID ||
3892 cur->next->type == TOK_PREPROC_ID
3893 || cur->next->type == TOK_NUMBER))
3894 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003895
3896 /* If identifier consists of just one token, don't expand */
3897 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003898 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003899
H. Peter Anvine2c80182005-01-15 22:15:51 +00003900 if (cur) {
3901 oldnext = cur->next; /* Detach the tail past identifier */
3902 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003903 }
3904
H. Peter Anvin734b1882002-04-30 21:01:08 +00003905 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003906
H. Peter Anvine2c80182005-01-15 22:15:51 +00003907 if (cur) {
3908 /* expand_smacro possibly changhed tline; re-scan for EOL */
3909 cur = tline;
3910 while (cur && cur->next)
3911 cur = cur->next;
3912 if (cur)
3913 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003914 }
3915
3916 return tline;
3917}
3918
3919/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003920 * Determine whether the given line constitutes a multi-line macro
3921 * call, and return the MMacro structure called if so. Doesn't have
3922 * to check for an initial label - that's taken care of in
3923 * expand_mmacro - but must check numbers of parameters. Guaranteed
3924 * to be called with tline->type == TOK_ID, so the putative macro
3925 * name is easy to find.
3926 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003927static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003928{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003929 MMacro *head, *m;
3930 Token **params;
3931 int nparam;
3932
H. Peter Anvin166c2472008-05-28 12:28:58 -07003933 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003934
3935 /*
3936 * Efficiency: first we see if any macro exists with the given
3937 * name. If not, we can return NULL immediately. _Then_ we
3938 * count the parameters, and then we look further along the
3939 * list if necessary to find the proper MMacro.
3940 */
3941 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003942 if (!mstrcmp(m->name, tline->text, m->casesense))
3943 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003944 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003945 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003946
3947 /*
3948 * OK, we have a potential macro. Count and demarcate the
3949 * parameters.
3950 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003951 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003952
3953 /*
3954 * So we know how many parameters we've got. Find the MMacro
3955 * structure that handles this number.
3956 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003957 while (m) {
3958 if (m->nparam_min <= nparam
3959 && (m->plus || nparam <= m->nparam_max)) {
3960 /*
3961 * This one is right. Just check if cycle removal
3962 * prohibits us using it before we actually celebrate...
3963 */
3964 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003965#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003966 error(ERR_NONFATAL,
3967 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003968#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003969 nasm_free(params);
3970 return NULL;
3971 }
3972 /*
3973 * It's right, and we can use it. Add its default
3974 * parameters to the end of our list if necessary.
3975 */
3976 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3977 params =
3978 nasm_realloc(params,
3979 ((m->nparam_min + m->ndefs +
3980 1) * sizeof(*params)));
3981 while (nparam < m->nparam_min + m->ndefs) {
3982 params[nparam] = m->defaults[nparam - m->nparam_min];
3983 nparam++;
3984 }
3985 }
3986 /*
3987 * If we've gone over the maximum parameter count (and
3988 * we're in Plus mode), ignore parameters beyond
3989 * nparam_max.
3990 */
3991 if (m->plus && nparam > m->nparam_max)
3992 nparam = m->nparam_max;
3993 /*
3994 * Then terminate the parameter list, and leave.
3995 */
3996 if (!params) { /* need this special case */
3997 params = nasm_malloc(sizeof(*params));
3998 nparam = 0;
3999 }
4000 params[nparam] = NULL;
4001 *params_array = params;
4002 return m;
4003 }
4004 /*
4005 * This one wasn't right: look for the next one with the
4006 * same name.
4007 */
4008 for (m = m->next; m; m = m->next)
4009 if (!mstrcmp(m->name, tline->text, m->casesense))
4010 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004011 }
4012
4013 /*
4014 * After all that, we didn't find one with the right number of
4015 * parameters. Issue a warning, and fail to expand the macro.
4016 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004017 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004018 "macro `%s' exists, but not taking %d parameters",
4019 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004020 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004021 return NULL;
4022}
4023
4024/*
4025 * Expand the multi-line macro call made by the given line, if
4026 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00004027 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004028 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004029static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004030{
4031 Token *startline = tline;
4032 Token *label = NULL;
4033 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004034 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004035 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004036 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004037 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004038 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004039
4040 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004041 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004042 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004043 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004044 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004045 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004046 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004047 if (m) {
4048 mname = t->text;
4049 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004050 Token *last;
4051 /*
4052 * We have an id which isn't a macro call. We'll assume
4053 * it might be a label; we'll also check to see if a
4054 * colon follows it. Then, if there's another id after
4055 * that lot, we'll check it again for macro-hood.
4056 */
4057 label = last = t;
4058 t = t->next;
4059 if (tok_type_(t, TOK_WHITESPACE))
4060 last = t, t = t->next;
4061 if (tok_is_(t, ":")) {
4062 dont_prepend = 1;
4063 last = t, t = t->next;
4064 if (tok_type_(t, TOK_WHITESPACE))
4065 last = t, t = t->next;
4066 }
4067 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
4068 return 0;
4069 last->next = NULL;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004070 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004071 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004072 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004073
4074 /*
4075 * Fix up the parameters: this involves stripping leading and
4076 * trailing whitespace, then stripping braces if they are
4077 * present.
4078 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004079 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004080 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004081
H. Peter Anvine2c80182005-01-15 22:15:51 +00004082 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004083 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004084 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004085
H. Peter Anvine2c80182005-01-15 22:15:51 +00004086 t = params[i];
4087 skip_white_(t);
4088 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004089 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004090 params[i] = t;
4091 paramlen[i] = 0;
4092 while (t) {
4093 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4094 break; /* ... because we have hit a comma */
4095 if (comma && t->type == TOK_WHITESPACE
4096 && tok_is_(t->next, ","))
4097 break; /* ... or a space then a comma */
4098 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4099 break; /* ... or a brace */
4100 t = t->next;
4101 paramlen[i]++;
4102 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004103 }
4104
4105 /*
4106 * OK, we have a MMacro structure together with a set of
4107 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004108 * copies of each Line on to istk->expansion. Substitution of
4109 * parameter tokens and macro-local tokens doesn't get done
4110 * until the single-line macro substitution process; this is
4111 * because delaying them allows us to change the semantics
4112 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004113 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004114 * First, push an end marker on to istk->expansion, mark this
4115 * macro as in progress, and set up its invocation-specific
4116 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004117 */
4118 ll = nasm_malloc(sizeof(Line));
4119 ll->next = istk->expansion;
4120 ll->finishes = m;
4121 ll->first = NULL;
4122 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004123
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004124 m->in_progress = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004125 m->params = params;
4126 m->iline = tline;
4127 m->nparam = nparam;
4128 m->rotate = 0;
4129 m->paramlen = paramlen;
4130 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004131 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004132
4133 m->next_active = istk->mstk;
4134 istk->mstk = m;
4135
H. Peter Anvine2c80182005-01-15 22:15:51 +00004136 for (l = m->expansion; l; l = l->next) {
4137 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004138
H. Peter Anvine2c80182005-01-15 22:15:51 +00004139 ll = nasm_malloc(sizeof(Line));
4140 ll->finishes = NULL;
4141 ll->next = istk->expansion;
4142 istk->expansion = ll;
4143 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004144
H. Peter Anvine2c80182005-01-15 22:15:51 +00004145 for (t = l->first; t; t = t->next) {
4146 Token *x = t;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004147 switch (t->type) {
4148 case TOK_PREPROC_Q:
H. Peter Anvinc751e862008-06-09 10:18:45 -07004149 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004150 break;
4151 case TOK_PREPROC_QQ:
4152 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4153 break;
4154 case TOK_PREPROC_ID:
4155 if (t->text[1] == '0' && t->text[2] == '0') {
4156 dont_prepend = -1;
4157 x = label;
4158 if (!x)
4159 continue;
4160 }
4161 /* fall through */
4162 default:
4163 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4164 break;
H. Peter Anvin166c2472008-05-28 12:28:58 -07004165 }
H. Peter Anvince2233b2008-05-25 21:57:00 -07004166 tail = &tt->next;
4167 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004168 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004169 }
4170
4171 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004172 * If we had a label, push it on as the first line of
4173 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004174 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004175 if (label) {
4176 if (dont_prepend < 0)
4177 free_tlist(startline);
4178 else {
4179 ll = nasm_malloc(sizeof(Line));
4180 ll->finishes = NULL;
4181 ll->next = istk->expansion;
4182 istk->expansion = ll;
4183 ll->first = startline;
4184 if (!dont_prepend) {
4185 while (label->next)
4186 label = label->next;
4187 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4188 }
4189 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004190 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004191
H. Peter Anvin734b1882002-04-30 21:01:08 +00004192 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004193
H. Peter Anvineba20a72002-04-30 20:53:55 +00004194 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004195}
4196
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004197/* The function that actually does the error reporting */
4198static void verror(int severity, const char *fmt, va_list arg)
4199{
4200 char buff[1024];
4201
4202 vsnprintf(buff, sizeof(buff), fmt, arg);
4203
4204 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004205 _error(severity, "(%s:%d) %s", istk->mstk->name,
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004206 istk->mstk->lineno, buff);
4207 else
H. Peter Anvin917a3492008-09-24 09:14:49 -07004208 _error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004209}
4210
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004211/*
4212 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004213 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004214 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004215static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004216{
4217 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004218
4219 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004220 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004221 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004222
H. Peter Anvin734b1882002-04-30 21:01:08 +00004223 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004224 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004225 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004226}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004227
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004228/*
4229 * Because %else etc are evaluated in the state context
4230 * of the previous branch, errors might get lost with error():
4231 * %if 0 ... %else trailing garbage ... %endif
4232 * So %else etc should report errors with this function.
4233 */
4234static void error_precond(int severity, const char *fmt, ...)
4235{
4236 va_list arg;
4237
4238 /* Only ignore the error if it's really in a dead branch */
4239 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4240 return;
4241
4242 va_start(arg, fmt);
4243 verror(severity, fmt, arg);
4244 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004245}
4246
H. Peter Anvin734b1882002-04-30 21:01:08 +00004247static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004248pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004249 ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004250{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004251 Token *t;
4252
H. Peter Anvin99941bf2002-05-14 17:44:03 +00004253 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004254 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004255 istk = nasm_malloc(sizeof(Include));
4256 istk->next = NULL;
4257 istk->conds = NULL;
4258 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004259 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004260 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004261 istk->fname = NULL;
4262 src_set_fname(nasm_strdup(file));
4263 src_set_linnum(0);
4264 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004265 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004266 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004267 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004268 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004269 nested_mac_count = 0;
4270 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004271 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004272 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004273 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004274 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004275 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004276 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004277 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004278 any_extrastdmac = extrastdmac && *extrastdmac;
4279 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004280 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004281 evaluate = eval;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004282
4283 /*
4284 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4285 * The caller, however, will also pass in 3 for preprocess-only so
4286 * we can set __PASS__ accordingly.
4287 */
4288 pass = apass > 2 ? 2 : apass;
4289
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004290 dephead = deptail = deplist;
4291 if (deplist) {
4292 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4293 sl->next = NULL;
4294 strcpy(sl->str, file);
4295 *deptail = sl;
4296 deptail = &sl->next;
4297 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004298
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004299 /*
4300 * Define the __PASS__ macro. This is defined here unlike
4301 * all the other builtins, because it is special -- it varies between
4302 * passes.
4303 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004304 t = nasm_malloc(sizeof(*t));
4305 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004306 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004307 t->a.mac = NULL;
4308 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004309}
4310
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004311static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004312{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004313 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004314 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004315
H. Peter Anvine2c80182005-01-15 22:15:51 +00004316 while (1) {
4317 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004318 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004319 * buffer or from the input file.
4320 */
4321 tline = NULL;
4322 while (istk->expansion && istk->expansion->finishes) {
4323 Line *l = istk->expansion;
4324 if (!l->finishes->name && l->finishes->in_progress > 1) {
4325 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004326
H. Peter Anvine2c80182005-01-15 22:15:51 +00004327 /*
4328 * This is a macro-end marker for a macro with no
4329 * name, which means it's not really a macro at all
4330 * but a %rep block, and the `in_progress' field is
4331 * more than 1, meaning that we still need to
4332 * repeat. (1 means the natural last repetition; 0
4333 * means termination by %exitrep.) We have
4334 * therefore expanded up to the %endrep, and must
4335 * push the whole block on to the expansion buffer
4336 * again. We don't bother to remove the macro-end
4337 * marker: we'd only have to generate another one
4338 * if we did.
4339 */
4340 l->finishes->in_progress--;
4341 for (l = l->finishes->expansion; l; l = l->next) {
4342 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004343
H. Peter Anvine2c80182005-01-15 22:15:51 +00004344 ll = nasm_malloc(sizeof(Line));
4345 ll->next = istk->expansion;
4346 ll->finishes = NULL;
4347 ll->first = NULL;
4348 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004349
H. Peter Anvine2c80182005-01-15 22:15:51 +00004350 for (t = l->first; t; t = t->next) {
4351 if (t->text || t->type == TOK_WHITESPACE) {
4352 tt = *tail =
4353 new_Token(NULL, t->type, t->text, 0);
4354 tail = &tt->next;
4355 }
4356 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004357
H. Peter Anvine2c80182005-01-15 22:15:51 +00004358 istk->expansion = ll;
4359 }
4360 } else {
4361 /*
4362 * Check whether a `%rep' was started and not ended
4363 * within this macro expansion. This can happen and
4364 * should be detected. It's a fatal error because
4365 * I'm too confused to work out how to recover
4366 * sensibly from it.
4367 */
4368 if (defining) {
4369 if (defining->name)
4370 error(ERR_PANIC,
4371 "defining with name in expansion");
4372 else if (istk->mstk->name)
4373 error(ERR_FATAL,
4374 "`%%rep' without `%%endrep' within"
4375 " expansion of macro `%s'",
4376 istk->mstk->name);
4377 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004378
H. Peter Anvine2c80182005-01-15 22:15:51 +00004379 /*
4380 * FIXME: investigate the relationship at this point between
4381 * istk->mstk and l->finishes
4382 */
4383 {
4384 MMacro *m = istk->mstk;
4385 istk->mstk = m->next_active;
4386 if (m->name) {
4387 /*
4388 * This was a real macro call, not a %rep, and
4389 * therefore the parameter information needs to
4390 * be freed.
4391 */
4392 nasm_free(m->params);
4393 free_tlist(m->iline);
4394 nasm_free(m->paramlen);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004395 l->finishes->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004396 } else
4397 free_mmacro(m);
4398 }
4399 istk->expansion = l->next;
4400 nasm_free(l);
4401 list->downlevel(LIST_MACRO);
4402 }
4403 }
4404 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004405
H. Peter Anvine2c80182005-01-15 22:15:51 +00004406 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004407 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004408 Line *l = istk->expansion;
4409 if (istk->mstk)
4410 istk->mstk->lineno++;
4411 tline = l->first;
4412 istk->expansion = l->next;
4413 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004414 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004415 list->line(LIST_MACRO, p);
4416 nasm_free(p);
4417 break;
4418 }
4419 line = read_line();
4420 if (line) { /* from the current input file */
4421 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004422 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004423 nasm_free(line);
4424 break;
4425 }
4426 /*
4427 * The current file has ended; work down the istk
4428 */
4429 {
4430 Include *i = istk;
4431 fclose(i->fp);
4432 if (i->conds)
4433 error(ERR_FATAL,
4434 "expected `%%endif' before end of file");
4435 /* only set line and file name if there's a next node */
4436 if (i->next) {
4437 src_set_linnum(i->lineno);
4438 nasm_free(src_set_fname(i->fname));
H. Peter Anvin86877b22008-06-20 15:55:45 -07004439 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004440 istk = i->next;
4441 list->downlevel(LIST_INCLUDE);
4442 nasm_free(i);
4443 if (!istk)
4444 return NULL;
Victor van den Elzen4c9d6222008-10-01 13:08:50 +02004445 if (istk->expansion && istk->expansion->finishes)
4446 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004447 }
4448 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004449
H. Peter Anvine2c80182005-01-15 22:15:51 +00004450 /*
4451 * We must expand MMacro parameters and MMacro-local labels
4452 * _before_ we plunge into directive processing, to cope
4453 * with things like `%define something %1' such as STRUC
4454 * uses. Unless we're _defining_ a MMacro, in which case
4455 * those tokens should be left alone to go into the
4456 * definition; and unless we're in a non-emitting
4457 * condition, in which case we don't want to meddle with
4458 * anything.
4459 */
Charles Crayned4200be2008-07-12 16:42:33 -07004460 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin992fe752008-10-19 15:45:05 -07004461 && !(istk->mstk && !istk->mstk->in_progress)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004462 tline = expand_mmac_params(tline);
H. Peter Anvin992fe752008-10-19 15:45:05 -07004463 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004464
H. Peter Anvine2c80182005-01-15 22:15:51 +00004465 /*
4466 * Check the line to see if it's a preprocessor directive.
4467 */
4468 if (do_directive(tline) == DIRECTIVE_FOUND) {
4469 continue;
4470 } else if (defining) {
4471 /*
4472 * We're defining a multi-line macro. We emit nothing
4473 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004474 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004475 */
4476 Line *l = nasm_malloc(sizeof(Line));
4477 l->next = defining->expansion;
4478 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004479 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004480 defining->expansion = l;
4481 continue;
4482 } else if (istk->conds && !emitting(istk->conds->state)) {
4483 /*
4484 * We're in a non-emitting branch of a condition block.
4485 * Emit nothing at all, not even a blank line: when we
4486 * emerge from the condition we'll give a line-number
4487 * directive so we keep our place correctly.
4488 */
4489 free_tlist(tline);
4490 continue;
4491 } else if (istk->mstk && !istk->mstk->in_progress) {
4492 /*
4493 * We're in a %rep block which has been terminated, so
4494 * we're walking through to the %endrep without
4495 * emitting anything. Emit nothing at all, not even a
4496 * blank line: when we emerge from the %rep block we'll
4497 * give a line-number directive so we keep our place
4498 * correctly.
4499 */
4500 free_tlist(tline);
4501 continue;
4502 } else {
4503 tline = expand_smacro(tline);
4504 if (!expand_mmacro(tline)) {
4505 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004506 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004507 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004508 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004509 free_tlist(tline);
4510 break;
4511 } else {
4512 continue; /* expand_mmacro calls free_tlist */
4513 }
4514 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004515 }
4516
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004517 return line;
4518}
4519
H. Peter Anvine2c80182005-01-15 22:15:51 +00004520static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004521{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004522 if (defining) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02004523 if(defining->name) {
4524 error(ERR_NONFATAL,
4525 "end of file while still defining macro `%s'",
4526 defining->name);
4527 } else {
4528 error(ERR_NONFATAL, "end of file while still in %%rep");
4529 }
4530
H. Peter Anvine2c80182005-01-15 22:15:51 +00004531 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004532 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004533 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004534 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004535 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004536 while (istk) {
4537 Include *i = istk;
4538 istk = istk->next;
4539 fclose(i->fp);
4540 nasm_free(i->fname);
4541 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004542 }
4543 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004544 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004545 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004546 if (pass == 0) {
H. Peter Anvin86877b22008-06-20 15:55:45 -07004547 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004548 free_llist(predef);
4549 delete_Blocks();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004550 while ((i = ipath)) {
4551 ipath = i->next;
4552 if (i->path)
4553 nasm_free(i->path);
4554 nasm_free(i);
4555 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07004556 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004557}
4558
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004559void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004560{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004561 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004562
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004563 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004564 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004565 i->next = NULL;
4566
H. Peter Anvine2c80182005-01-15 22:15:51 +00004567 if (ipath != NULL) {
4568 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004569 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004570 j = j->next;
4571 j->next = i;
4572 } else {
4573 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004574 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004575}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004576
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004577void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004578{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004579 Token *inc, *space, *name;
4580 Line *l;
4581
H. Peter Anvin734b1882002-04-30 21:01:08 +00004582 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4583 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4584 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004585
4586 l = nasm_malloc(sizeof(Line));
4587 l->next = predef;
4588 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004589 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004590 predef = l;
4591}
4592
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004593void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004594{
4595 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004596 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004597 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004598
4599 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004600 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4601 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004602 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004603 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004604 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004605 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004606 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004607
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004608 l = nasm_malloc(sizeof(Line));
4609 l->next = predef;
4610 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004611 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004612 predef = l;
4613}
4614
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004615void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004616{
4617 Token *def, *space;
4618 Line *l;
4619
H. Peter Anvin734b1882002-04-30 21:01:08 +00004620 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4621 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004622 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004623
4624 l = nasm_malloc(sizeof(Line));
4625 l->next = predef;
4626 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004627 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004628 predef = l;
4629}
4630
Keith Kaniosb7a89542007-04-12 02:40:54 +00004631/*
4632 * Added by Keith Kanios:
4633 *
4634 * This function is used to assist with "runtime" preprocessor
4635 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4636 *
4637 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4638 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4639 */
4640
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004641void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004642{
4643 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004644
Keith Kaniosb7a89542007-04-12 02:40:54 +00004645 def = tokenize(definition);
4646 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4647 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004648
Keith Kaniosb7a89542007-04-12 02:40:54 +00004649}
4650
H. Peter Anvina70547f2008-07-19 21:44:26 -07004651void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004652{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004653 extrastdmac = macros;
4654}
4655
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004656static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004657{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004658 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004659 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004660 tok->text = nasm_strdup(numbuf);
4661 tok->type = TOK_NUMBER;
4662}
4663
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004664Preproc nasmpp = {
4665 pp_reset,
4666 pp_getline,
4667 pp_cleanup
4668};