blob: ea86dcb5f0167a374565447d2000f4957e4fca71 [file] [log] [blame]
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001/* preproc.c macro preprocessor for the Netwide Assembler
2 *
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
Beroset095e6a22007-12-29 09:44:23 -05005 * redistributable under the license given in the file "LICENSE"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 * distributed in the NASM archive.
7 *
8 * initial version 18/iii/97 by Simon Tatham
9 */
10
H. Peter Anvin4836e332002-04-30 20:56:43 +000011/* Typical flow of text through preproc
12 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000013 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000014 *
15 * from a macro expansion
16 *
17 * or
18 * {
19 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000020 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000021 * }
22 *
23 * expand_mmac_params is used to expand %1 etc., unless a macro is being
24 * defined or a false conditional is being processed
25 * (%0, %1, %+1, %-1, %%foo
26 *
27 * do_directive checks for directives
28 *
29 * expand_smacro is used to expand single line macros
30 *
31 * expand_mmacro is used to expand multi-line macros
32 *
33 * detoken is used to convert the line back to text
34 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000035
H. Peter Anvinfe501952007-10-02 21:53:51 -070036#include "compiler.h"
37
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000038#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000039#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000040#include <stdlib.h>
41#include <stddef.h>
42#include <string.h>
43#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000044#include <limits.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000045#include <inttypes.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000046
47#include "nasm.h"
48#include "nasmlib.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000049#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070050#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070051#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070052#include "stdscan.h"
53#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070054#include "tables.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000055
56typedef struct SMacro SMacro;
57typedef struct MMacro MMacro;
58typedef struct Context Context;
59typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000060typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000061typedef struct Line Line;
62typedef struct Include Include;
63typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000064typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000065
66/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070067 * Note on the storage of both SMacro and MMacros: the hash table
68 * indexes them case-insensitively, and we then have to go through a
69 * linked list of potential case aliases (and, for MMacros, parameter
70 * ranges); this is to preserve the matching semantics of the earlier
71 * code. If the number of case aliases for a specific macro is a
72 * performance issue, you may want to reconsider your coding style.
73 */
74
75/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000076 * Store the definition of a single-line macro.
77 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000078struct SMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000079 SMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +000080 char *name;
H. Peter Anvin70055962007-10-11 00:05:31 -070081 bool casesense;
H. Peter Anvin16ed4382007-10-11 10:06:19 -070082 bool in_progress;
H. Peter Anvin70055962007-10-11 00:05:31 -070083 unsigned int nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000084 Token *expansion;
85};
86
87/*
H. Peter Anvin76690a12002-04-30 20:52:49 +000088 * Store the definition of a multi-line macro. This is also used to
89 * store the interiors of `%rep...%endrep' blocks, which are
90 * effectively self-re-invoking multi-line macros which simply
91 * don't have a name or bother to appear in the hash tables. %rep
92 * blocks are signified by having a NULL `name' field.
93 *
94 * In a MMacro describing a `%rep' block, the `in_progress' field
95 * isn't merely boolean, but gives the number of repeats left to
96 * run.
97 *
98 * The `next' field is used for storing MMacros in hash tables; the
99 * `next_active' field is for stacking them on istk entries.
100 *
101 * When a MMacro is being expanded, `params', `iline', `nparam',
102 * `paramlen', `rotate' and `unique' are local to the invocation.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000103 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000104struct MMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 MMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000106 char *name;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -0700107 int nparam_min, nparam_max;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700108 bool casesense;
109 bool plus; /* is the last parameter greedy? */
110 bool nolist; /* is this macro listing-inhibited? */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700111 int64_t in_progress;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000112 Token *dlist; /* All defaults as one list */
113 Token **defaults; /* Parameter default pointers */
114 int ndefs; /* number of default parameters */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000115 Line *expansion;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000116
117 MMacro *next_active;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000118 MMacro *rep_nest; /* used for nesting %rep */
119 Token **params; /* actual parameters */
120 Token *iline; /* invocation line */
H. Peter Anvin25a99342007-09-22 17:45:45 -0700121 unsigned int nparam, rotate;
122 int *paramlen;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700123 uint64_t unique;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000124 int lineno; /* Current line number on expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000125};
126
127/*
128 * The context stack is composed of a linked list of these.
129 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000130struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000131 Context *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000132 char *name;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700133 struct hash_table localmac;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000134 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000135};
136
137/*
138 * This is the internal form which we break input lines up into.
139 * Typically stored in linked lists.
140 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000141 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
142 * necessarily used as-is, but is intended to denote the number of
143 * the substituted parameter. So in the definition
144 *
145 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700146 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000147 * the token representing `x' will have its type changed to
148 * TOK_SMAC_PARAM, but the one representing `y' will be
149 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000150 *
151 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
152 * which doesn't need quotes around it. Used in the pre-include
153 * mechanism as an alternative to trying to find a sensible type of
154 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000155 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000156enum pp_token_type {
157 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
158 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700159 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
160 TOK_INTERNAL_STRING,
161 TOK_PREPROC_Q, TOK_PREPROC_QQ,
H. Peter Anvin992fe752008-10-19 15:45:05 -0700162 TOK_INDIRECT, /* %[...] */
H. Peter Anvin5b76fa22008-05-26 11:14:38 -0700163 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
164 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000165};
166
H. Peter Anvine2c80182005-01-15 22:15:51 +0000167struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000168 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000169 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700170 union {
171 SMacro *mac; /* associated macro for TOK_SMAC_END */
172 size_t len; /* scratch length field */
173 } a; /* Auxiliary data */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000174 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000175};
176
177/*
178 * Multi-line macro definitions are stored as a linked list of
179 * these, which is essentially a container to allow several linked
180 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700181 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000182 * Note that in this module, linked lists are treated as stacks
183 * wherever possible. For this reason, Lines are _pushed_ on to the
184 * `expansion' field in MMacro structures, so that the linked list,
185 * if walked, would give the macro lines in reverse order; this
186 * means that we can walk the list when expanding a macro, and thus
187 * push the lines on to the `expansion' field in _istk_ in reverse
188 * order (so that when popped back off they are in the right
189 * order). It may seem cockeyed, and it relies on my design having
190 * an even number of steps in, but it works...
191 *
192 * Some of these structures, rather than being actual lines, are
193 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000194 * This is for use in the cycle-tracking and %rep-handling code.
195 * Such structures have `finishes' non-NULL, and `first' NULL. All
196 * others have `finishes' NULL, but `first' may still be NULL if
197 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000198 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000199struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000200 Line *next;
201 MMacro *finishes;
202 Token *first;
203};
204
205/*
206 * To handle an arbitrary level of file inclusion, we maintain a
207 * stack (ie linked list) of these things.
208 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000209struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000210 Include *next;
211 FILE *fp;
212 Cond *conds;
213 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000214 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000215 int lineno, lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000216 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000217};
218
219/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000220 * Include search path. This is simply a list of strings which get
221 * prepended, in turn, to the name of an include file, in an
222 * attempt to find the file if it's not in the current directory.
223 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000224struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000225 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000226 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000227};
228
229/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000230 * Conditional assembly: we maintain a separate stack of these for
231 * each level of file inclusion. (The only reason we keep the
232 * stacks separate is to ensure that a stray `%endif' in a file
233 * included from within the true branch of a `%if' won't terminate
234 * it and cause confusion: instead, rightly, it'll cause an error.)
235 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000236struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000237 Cond *next;
238 int state;
239};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000240enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000241 /*
242 * These states are for use just after %if or %elif: IF_TRUE
243 * means the condition has evaluated to truth so we are
244 * currently emitting, whereas IF_FALSE means we are not
245 * currently emitting but will start doing so if a %else comes
246 * up. In these states, all directives are admissible: %elif,
247 * %else and %endif. (And of course %if.)
248 */
249 COND_IF_TRUE, COND_IF_FALSE,
250 /*
251 * These states come up after a %else: ELSE_TRUE means we're
252 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
253 * any %elif or %else will cause an error.
254 */
255 COND_ELSE_TRUE, COND_ELSE_FALSE,
256 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200257 * These states mean that we're not emitting now, and also that
258 * nothing until %endif will be emitted at all. COND_DONE is
259 * used when we've had our moment of emission
260 * and have now started seeing %elifs. COND_NEVER is used when
261 * the condition construct in question is contained within a
262 * non-emitting branch of a larger condition construct,
263 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000264 */
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200265 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000266};
267#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
268
H. Peter Anvin70653092007-10-19 14:42:29 -0700269/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000270 * These defines are used as the possible return values for do_directive
271 */
272#define NO_DIRECTIVE_FOUND 0
273#define DIRECTIVE_FOUND 1
274
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000275/*
276 * Condition codes. Note that we use c_ prefix not C_ because C_ is
277 * used in nasm.h for the "real" condition codes. At _this_ level,
278 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
279 * ones, so we need a different enum...
280 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700281static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000282 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
283 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000284 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000285};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700286enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000287 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
288 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
H. Peter Anvin476d2862007-10-02 22:04:15 -0700289 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
290 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000291};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700292static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000293 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
294 c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
H. Peter Anvince9be342007-09-12 00:22:29 +0000295 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000296};
297
H. Peter Anvin76690a12002-04-30 20:52:49 +0000298/*
299 * Directive names.
300 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000301/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000302static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000303{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000304 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000305}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000306
307/* For TASM compatibility we need to be able to recognise TASM compatible
308 * conditional compilation directives. Using the NASM pre-processor does
309 * not work, so we look for them specifically from the following list and
310 * then jam in the equivalent NASM directive into the input stream.
311 */
312
H. Peter Anvine2c80182005-01-15 22:15:51 +0000313enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000314 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
315 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
316};
317
H. Peter Anvin476d2862007-10-02 22:04:15 -0700318static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000319 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
320 "ifndef", "include", "local"
321};
322
323static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000324static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000325static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800326static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000327
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000328static Context *cstk;
329static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000330static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000331
H. Peter Anvine2c80182005-01-15 22:15:51 +0000332static efunc _error; /* Pointer to client-provided error reporting function */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000333static evalfunc evaluate;
334
H. Peter Anvine2c80182005-01-15 22:15:51 +0000335static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700336static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000337
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700338static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000339
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000340static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700341static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000342
343static ListGen *list;
344
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000345/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000346 * The current set of multi-line macros we have defined.
347 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700348static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000349
350/*
351 * The current set of single-line macros we have defined.
352 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700353static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000354
355/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000356 * The multi-line macro we are currently defining, or the %rep
357 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000358 */
359static MMacro *defining;
360
Charles Crayned4200be2008-07-12 16:42:33 -0700361static uint64_t nested_mac_count;
362static uint64_t nested_rep_count;
363
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000364/*
365 * The number of macro parameters to allocate space for at a time.
366 */
367#define PARAM_DELTA 16
368
369/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700370 * The standard macro set: defined in macros.c in the array nasm_stdmac.
371 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000372 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700373static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000374
375/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000376 * The extra standard macros that come from the object format, if
377 * any.
378 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700379static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700380static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000381
382/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000383 * Tokens are allocated in blocks to improve speed
384 */
385#define TOKEN_BLOCKSIZE 4096
386static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000387struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000388 Blocks *next;
389 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000390};
391
392static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000393
394/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000395 * Forward declarations.
396 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000397static Token *expand_mmac_params(Token * tline);
398static Token *expand_smacro(Token * tline);
399static Token *expand_id(Token * tline);
H. Peter Anvin992fe752008-10-19 15:45:05 -0700400static Token *expand_indirect(Token * tline, int level);
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -0700401static Context *get_ctx(const char *name, bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700402static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000403static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200404static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000405static void *new_Block(size_t size);
406static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700407static Token *new_Token(Token * next, enum pp_token_type type,
408 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000409static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000410
411/*
412 * Macros for safe checking of token pointers, avoid *(NULL)
413 */
414#define tok_type_(x,t) ((x) && (x)->type == (t))
415#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
416#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
417#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000418
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000419/* Handle TASM specific directives, which do not contain a % in
420 * front of them. We do it here because I could not find any other
421 * place to do it for the moment, and it is a hack (ideally it would
422 * be nice to be able to use the NASM pre-processor to do it).
423 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000424static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000425{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000426 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000427 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000428
429 /* Skip whitespace */
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700430 while (nasm_isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000431 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000432
433 /* Binary search for the directive name */
434 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000435 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000436 len = 0;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700437 while (!nasm_isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000438 len++;
439 if (len) {
440 oldchar = p[len];
441 p[len] = 0;
442 while (j - i > 1) {
443 k = (j + i) / 2;
444 m = nasm_stricmp(p, tasm_directives[k]);
445 if (m == 0) {
446 /* We have found a directive, so jam a % in front of it
447 * so that NASM will then recognise it as one if it's own.
448 */
449 p[len] = oldchar;
450 len = strlen(p);
451 oldline = line;
452 line = nasm_malloc(len + 2);
453 line[0] = '%';
454 if (k == TM_IFDIFI) {
455 /* NASM does not recognise IFDIFI, so we convert it to
456 * %ifdef BOGUS. This is not used in NASM comaptible
457 * code, but does need to parse for the TASM macro
458 * package.
459 */
460 strcpy(line + 1, "ifdef BOGUS");
461 } else {
462 memcpy(line + 1, p, len + 1);
463 }
464 nasm_free(oldline);
465 return line;
466 } else if (m < 0) {
467 j = k;
468 } else
469 i = k;
470 }
471 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000472 }
473 return line;
474}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000475
H. Peter Anvin76690a12002-04-30 20:52:49 +0000476/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000477 * The pre-preprocessing stage... This function translates line
478 * number indications as they emerge from GNU cpp (`# lineno "file"
479 * flags') into NASM preprocessor line number indications (`%line
480 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000481 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000482static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000483{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000484 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000485 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000486
H. Peter Anvine2c80182005-01-15 22:15:51 +0000487 if (line[0] == '#' && line[1] == ' ') {
488 oldline = line;
489 fname = oldline + 2;
490 lineno = atoi(fname);
491 fname += strspn(fname, "0123456789 ");
492 if (*fname == '"')
493 fname++;
494 fnlen = strcspn(fname, "\"");
495 line = nasm_malloc(20 + fnlen);
496 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
497 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000498 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000499 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000501 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000502}
503
504/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000505 * Free a linked list of tokens.
506 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000507static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000508{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000509 while (list) {
510 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000511 }
512}
513
514/*
515 * Free a linked list of lines.
516 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000517static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000518{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000519 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000520 while (list) {
521 l = list;
522 list = list->next;
523 free_tlist(l->first);
524 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000525 }
526}
527
528/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000529 * Free an MMacro
530 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000531static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000532{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000533 nasm_free(m->name);
534 free_tlist(m->dlist);
535 nasm_free(m->defaults);
536 free_llist(m->expansion);
537 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000538}
539
540/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700541 * Free all currently defined macros, and free the hash tables
542 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700543static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700544{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700545 SMacro *s;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700546 const char *key;
547 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700548
H. Peter Anvin072771e2008-05-22 13:17:51 -0700549 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700550 nasm_free((void *)key);
551 while (s) {
552 SMacro *ns = s->next;
553 nasm_free(s->name);
554 free_tlist(s->expansion);
555 nasm_free(s);
556 s = ns;
557 }
558 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700559 hash_free(smt);
560}
561
562static void free_mmacro_table(struct hash_table *mmt)
563{
564 MMacro *m;
565 const char *key;
566 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700567
568 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700569 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700570 nasm_free((void *)key);
571 while (m) {
572 MMacro *nm = m->next;
573 free_mmacro(m);
574 m = nm;
575 }
576 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700577 hash_free(mmt);
578}
579
580static void free_macros(void)
581{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700582 free_smacro_table(&smacros);
583 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700584}
585
586/*
587 * Initialize the hash tables
588 */
589static void init_macros(void)
590{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700591 hash_init(&smacros, HASH_LARGE);
592 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700593}
594
595/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000596 * Pop the context stack.
597 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000598static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000599{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000600 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000601
602 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700603 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000604 nasm_free(c->name);
605 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000606}
607
H. Peter Anvin072771e2008-05-22 13:17:51 -0700608/*
609 * Search for a key in the hash index; adding it if necessary
610 * (in which case we initialize the data pointer to NULL.)
611 */
612static void **
613hash_findi_add(struct hash_table *hash, const char *str)
614{
615 struct hash_insert hi;
616 void **r;
617 char *strx;
618
619 r = hash_findi(hash, str, &hi);
620 if (r)
621 return r;
622
623 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
624 return hash_add(&hi, strx, NULL);
625}
626
627/*
628 * Like hash_findi, but returns the data element rather than a pointer
629 * to it. Used only when not adding a new element, hence no third
630 * argument.
631 */
632static void *
633hash_findix(struct hash_table *hash, const char *str)
634{
635 void **p;
636
637 p = hash_findi(hash, str, NULL);
638 return p ? *p : NULL;
639}
640
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000641#define BUF_DELTA 512
642/*
643 * Read a line from the top file in istk, handling multiple CR/LFs
644 * at the end of the line read, and handling spurious ^Zs. Will
645 * return lines from the standard macro set if this has not already
646 * been done.
647 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000648static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000649{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000650 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000651 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000652
H. Peter Anvine2c80182005-01-15 22:15:51 +0000653 if (stdmacpos) {
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700654 unsigned char c;
H. Peter Anvin7e50d232008-06-25 14:54:14 -0700655 const unsigned char *p = stdmacpos;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700656 char *ret, *q;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700657 size_t len = 0;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700658 while ((c = *p++)) {
659 if (c >= 0x80)
660 len += pp_directives_len[c-0x80]+1;
661 else
662 len++;
663 }
664 ret = nasm_malloc(len+1);
H. Peter Anvincda81632008-06-21 15:15:40 -0700665 q = ret;
666 while ((c = *stdmacpos++)) {
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700667 if (c >= 0x80) {
668 memcpy(q, pp_directives[c-0x80], pp_directives_len[c-0x80]);
669 q += pp_directives_len[c-0x80];
670 *q++ = ' ';
671 } else {
672 *q++ = c;
673 }
674 }
H. Peter Anvincda81632008-06-21 15:15:40 -0700675 stdmacpos = p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700676 *q = '\0';
677
H. Peter Anvind2456592008-06-19 15:04:18 -0700678 if (!*stdmacpos) {
679 /* This was the last of the standard macro chain... */
680 stdmacpos = NULL;
681 if (any_extrastdmac) {
682 stdmacpos = extrastdmac;
683 any_extrastdmac = false;
684 } else if (do_predef) {
685 Line *pd, *l;
686 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000687
H. Peter Anvind2456592008-06-19 15:04:18 -0700688 /*
689 * Nasty hack: here we push the contents of
690 * `predef' on to the top-level expansion stack,
691 * since this is the most convenient way to
692 * implement the pre-include and pre-define
693 * features.
694 */
695 for (pd = predef; pd; pd = pd->next) {
696 head = NULL;
697 tail = &head;
698 for (t = pd->first; t; t = t->next) {
699 *tail = new_Token(NULL, t->type, t->text, 0);
700 tail = &(*tail)->next;
701 }
702 l = nasm_malloc(sizeof(Line));
703 l->next = istk->expansion;
704 l->first = head;
H. Peter Anvin538002d2008-06-28 18:30:27 -0700705 l->finishes = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700706 istk->expansion = l;
707 }
708 do_predef = false;
709 }
710 }
711 return ret;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000712 }
713
714 bufsize = BUF_DELTA;
715 buffer = nasm_malloc(BUF_DELTA);
716 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000717 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000718 while (1) {
719 q = fgets(p, bufsize - (p - buffer), istk->fp);
720 if (!q)
721 break;
722 p += strlen(p);
723 if (p > buffer && p[-1] == '\n') {
724 /* Convert backslash-CRLF line continuation sequences into
725 nothing at all (for DOS and Windows) */
726 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
727 p -= 3;
728 *p = 0;
729 continued_count++;
730 }
731 /* Also convert backslash-LF line continuation sequences into
732 nothing at all (for Unix) */
733 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
734 p -= 2;
735 *p = 0;
736 continued_count++;
737 } else {
738 break;
739 }
740 }
741 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000742 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000743 bufsize += BUF_DELTA;
744 buffer = nasm_realloc(buffer, bufsize);
745 p = buffer + offset; /* prevent stale-pointer problems */
746 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000747 }
748
H. Peter Anvine2c80182005-01-15 22:15:51 +0000749 if (!q && p == buffer) {
750 nasm_free(buffer);
751 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000752 }
753
H. Peter Anvine2c80182005-01-15 22:15:51 +0000754 src_set_linnum(src_get_linnum() + istk->lineinc +
755 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000756
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000757 /*
758 * Play safe: remove CRs as well as LFs, if any of either are
759 * present at the end of the line.
760 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000761 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000762 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000763
764 /*
765 * Handle spurious ^Z, which may be inserted into source files
766 * by some file transfer utilities.
767 */
768 buffer[strcspn(buffer, "\032")] = '\0';
769
H. Peter Anvin734b1882002-04-30 21:01:08 +0000770 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000771
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000772 return buffer;
773}
774
775/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000776 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000777 * don't need to parse the value out of e.g. numeric tokens: we
778 * simply split one string into many.
779 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000780static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000781{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700782 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000783 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000784 Token *list = NULL;
785 Token *t, **tail = &list;
786
H. Peter Anvine2c80182005-01-15 22:15:51 +0000787 while (*line) {
788 p = line;
789 if (*p == '%') {
790 p++;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700791 if (nasm_isdigit(*p) ||
792 ((*p == '-' || *p == '+') && nasm_isdigit(p[1])) ||
793 ((*p == '+') && (nasm_isspace(p[1]) || !p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000794 do {
795 p++;
796 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700797 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000798 type = TOK_PREPROC_ID;
799 } else if (*p == '{') {
800 p++;
801 while (*p && *p != '}') {
802 p[-1] = *p;
803 p++;
804 }
805 p[-1] = '\0';
806 if (*p)
807 p++;
808 type = TOK_PREPROC_ID;
H. Peter Anvin992fe752008-10-19 15:45:05 -0700809 } else if (*p == '[') {
810 int lvl = 1;
811 line += 2; /* Skip the leading %[ */
812 p++;
H. Peter Anvinec033012008-10-19 22:12:06 -0700813 while (lvl && (c = *p++)) {
H. Peter Anvinca544db2008-10-19 19:30:11 -0700814 switch (c) {
815 case ']':
816 lvl--;
817 break;
818 case '%':
H. Peter Anvinca544db2008-10-19 19:30:11 -0700819 if (*p == '[')
820 lvl++;
821 break;
822 case '\'':
823 case '\"':
824 case '`':
H. Peter Anvinec033012008-10-19 22:12:06 -0700825 p = nasm_skip_string(p)+1;
H. Peter Anvinca544db2008-10-19 19:30:11 -0700826 break;
827 default:
828 break;
H. Peter Anvin992fe752008-10-19 15:45:05 -0700829 }
H. Peter Anvin992fe752008-10-19 15:45:05 -0700830 }
H. Peter Anvinec033012008-10-19 22:12:06 -0700831 p--;
H. Peter Anvin992fe752008-10-19 15:45:05 -0700832 if (*p)
833 *p++ = '\0';
H. Peter Anvine1265812008-10-19 22:14:30 -0700834 if (lvl)
835 error(ERR_NONFATAL, "unterminated %[ construct");
H. Peter Anvin992fe752008-10-19 15:45:05 -0700836 type = TOK_INDIRECT;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700837 } else if (*p == '?') {
838 type = TOK_PREPROC_Q; /* %? */
839 p++;
840 if (*p == '?') {
841 type = TOK_PREPROC_QQ; /* %?? */
842 p++;
843 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000844 } else if (isidchar(*p) ||
845 ((*p == '!' || *p == '%' || *p == '$') &&
846 isidchar(p[1]))) {
847 do {
848 p++;
849 }
850 while (isidchar(*p));
851 type = TOK_PREPROC_ID;
852 } else {
853 type = TOK_OTHER;
854 if (*p == '%')
855 p++;
856 }
857 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
858 type = TOK_ID;
859 p++;
860 while (*p && isidchar(*p))
861 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700862 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000863 /*
864 * A string token.
865 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000866 type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700867 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000868
H. Peter Anvine2c80182005-01-15 22:15:51 +0000869 if (*p) {
870 p++;
871 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -0700872 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000873 /* Handling unterminated strings by UNV */
874 /* type = -1; */
875 }
876 } else if (isnumstart(*p)) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700877 bool is_hex = false;
878 bool is_float = false;
879 bool has_e = false;
880 char c, *r;
881
H. Peter Anvine2c80182005-01-15 22:15:51 +0000882 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700883 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000884 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700885
886 if (*p == '$') {
887 p++;
888 is_hex = true;
889 }
890
891 for (;;) {
892 c = *p++;
893
894 if (!is_hex && (c == 'e' || c == 'E')) {
895 has_e = true;
896 if (*p == '+' || *p == '-') {
897 /* e can only be followed by +/- if it is either a
898 prefixed hex number or a floating-point number */
899 p++;
900 is_float = true;
901 }
902 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
903 is_hex = true;
904 } else if (c == 'P' || c == 'p') {
905 is_float = true;
906 if (*p == '+' || *p == '-')
907 p++;
908 } else if (isnumchar(c) || c == '_')
909 ; /* just advance */
910 else if (c == '.') {
911 /* we need to deal with consequences of the legacy
912 parser, like "1.nolist" being two tokens
913 (TOK_NUMBER, TOK_ID) here; at least give it
914 a shot for now. In the future, we probably need
915 a flex-based scanner with proper pattern matching
916 to do it as well as it can be done. Nothing in
917 the world is going to help the person who wants
918 0x123.p16 interpreted as two tokens, though. */
919 r = p;
920 while (*r == '_')
921 r++;
922
H. Peter Anvinf221b9e2008-06-21 11:03:51 -0700923 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700924 (!is_hex && (*r == 'e' || *r == 'E')) ||
925 (*r == 'p' || *r == 'P')) {
926 p = r;
927 is_float = true;
928 } else
929 break; /* Terminate the token */
930 } else
931 break;
932 }
933 p--; /* Point to first character beyond number */
934
935 if (has_e && !is_hex) {
936 /* 1e13 is floating-point, but 1e13h is not */
937 is_float = true;
938 }
939
940 type = is_float ? TOK_FLOAT : TOK_NUMBER;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700941 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000942 type = TOK_WHITESPACE;
943 p++;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700944 while (*p && nasm_isspace(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000945 p++;
946 /*
947 * Whitespace just before end-of-line is discarded by
948 * pretending it's a comment; whitespace just before a
949 * comment gets lumped into the comment.
950 */
951 if (!*p || *p == ';') {
952 type = TOK_COMMENT;
953 while (*p)
954 p++;
955 }
956 } else if (*p == ';') {
957 type = TOK_COMMENT;
958 while (*p)
959 p++;
960 } else {
961 /*
962 * Anything else is an operator of some kind. We check
963 * for all the double-character operators (>>, <<, //,
964 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000965 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966 */
967 type = TOK_OTHER;
968 if ((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[0] == '^' && p[1] == '^')) {
979 p++;
980 }
981 p++;
982 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000983
H. Peter Anvine2c80182005-01-15 22:15:51 +0000984 /* Handling unterminated string by UNV */
985 /*if (type == -1)
986 {
987 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
988 t->text[p-line] = *line;
989 tail = &t->next;
990 }
991 else */
992 if (type != TOK_COMMENT) {
993 *tail = t = new_Token(NULL, type, line, p - line);
994 tail = &t->next;
995 }
996 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000997 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000998 return list;
999}
1000
H. Peter Anvince616072002-04-30 21:02:23 +00001001/*
1002 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001003 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001004 * deleted only all at once by the delete_Blocks function.
1005 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001007{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001008 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001010 /* first, get to the end of the linked list */
1011 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001013 /* now allocate the requested chunk */
1014 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001015
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001016 /* now allocate a new block for the next request */
1017 b->next = nasm_malloc(sizeof(Blocks));
1018 /* and initialize the contents of the new block */
1019 b->next->next = NULL;
1020 b->next->chunk = NULL;
1021 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001022}
1023
1024/*
1025 * this function deletes all managed blocks of memory
1026 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001027static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001028{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001030
H. Peter Anvin70653092007-10-19 14:42:29 -07001031 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001032 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001033 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001034 * free it.
1035 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001036 while (b) {
1037 if (b->chunk)
1038 nasm_free(b->chunk);
1039 a = b;
1040 b = b->next;
1041 if (a != &blocks)
1042 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001043 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001044}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001045
1046/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001047 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001048 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001049 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001050 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001051static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvinc751e862008-06-09 10:18:45 -07001052 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001053{
1054 Token *t;
1055 int i;
1056
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 if (freeTokens == NULL) {
1058 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1059 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1060 freeTokens[i].next = &freeTokens[i + 1];
1061 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001062 }
1063 t = freeTokens;
1064 freeTokens = t->next;
1065 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001066 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001067 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001068 if (type == TOK_WHITESPACE || text == NULL) {
1069 t->text = NULL;
1070 } else {
1071 if (txtlen == 0)
1072 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001073 t->text = nasm_malloc(txtlen+1);
1074 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001075 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001076 }
1077 return t;
1078}
1079
H. Peter Anvine2c80182005-01-15 22:15:51 +00001080static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001081{
1082 Token *next = t->next;
1083 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001084 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001085 freeTokens = t;
1086 return next;
1087}
1088
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001089/*
1090 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001091 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1092 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001093 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001094static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001095{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001096 Token *t;
1097 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001098 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001099 const char *q;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001100
1101 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001102 for (t = tlist; t; t = t->next) {
1103 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001104 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001105 nasm_free(t->text);
1106 if (p)
1107 t->text = nasm_strdup(p);
1108 else
1109 t->text = NULL;
1110 }
1111 /* Expand local macros here and not during preprocessing */
1112 if (expand_locals &&
1113 t->type == TOK_PREPROC_ID && t->text &&
1114 t->text[0] == '%' && t->text[1] == '$') {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001115 Context *ctx = get_ctx(t->text, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001116 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001117 char buffer[40];
1118 char *p, *q = t->text + 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001119
H. Peter Anvine2c80182005-01-15 22:15:51 +00001120 q += strspn(q, "$");
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001121 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001122 p = nasm_strcat(buffer, q);
1123 nasm_free(t->text);
1124 t->text = p;
1125 }
1126 }
1127 if (t->type == TOK_WHITESPACE) {
1128 len++;
1129 } else if (t->text) {
1130 len += strlen(t->text);
1131 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001132 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001133 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 for (t = tlist; t; t = t->next) {
1135 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001136 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 } else if (t->text) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001138 q = t->text;
1139 while (*q)
1140 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001141 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001142 }
1143 *p = '\0';
1144 return line;
1145}
1146
1147/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001148 * A scanner, suitable for use by the expression evaluator, which
1149 * operates on a line of Tokens. Expects a pointer to a pointer to
1150 * the first token in the line to be passed in as its private_data
1151 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001152 *
1153 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001154 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001156{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001157 Token **tlineptr = private_data;
1158 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001159 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001160
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161 do {
1162 tline = *tlineptr;
1163 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001164 }
1165 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001167
1168 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001169 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001170
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001171 tokval->t_charptr = tline->text;
1172
H. Peter Anvin76690a12002-04-30 20:52:49 +00001173 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001174 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001175 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001176 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001177
H. Peter Anvine2c80182005-01-15 22:15:51 +00001178 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001179 p = tokval->t_charptr = tline->text;
1180 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001181 tokval->t_charptr++;
1182 return tokval->t_type = TOKEN_ID;
1183 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001184
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001185 for (r = p, s = ourcopy; *r; r++) {
Philipp Thomas76ec8e72008-05-21 08:53:21 -07001186 if (r >= p+MAX_KEYWORD)
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001187 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001188 *s++ = nasm_tolower(*r);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001189 }
1190 *s = '\0';
1191 /* right, so we have an identifier sitting in temp storage. now,
1192 * is it actually a register or instruction name, or what? */
1193 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001194 }
1195
H. Peter Anvine2c80182005-01-15 22:15:51 +00001196 if (tline->type == TOK_NUMBER) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001197 bool rn_error;
1198 tokval->t_integer = readnum(tline->text, &rn_error);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001199 tokval->t_charptr = tline->text;
H. Peter Anvin11627042008-06-09 20:45:19 -07001200 if (rn_error)
1201 return tokval->t_type = TOKEN_ERRNUM;
1202 else
1203 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001204 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001205
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001206 if (tline->type == TOK_FLOAT) {
1207 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001208 }
1209
H. Peter Anvine2c80182005-01-15 22:15:51 +00001210 if (tline->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001211 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001212
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001213 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001214 tokval->t_charptr = tline->text;
1215 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001216
H. Peter Anvin11627042008-06-09 20:45:19 -07001217 if (ep[0] != bq || ep[1] != '\0')
1218 return tokval->t_type = TOKEN_ERRSTR;
1219 else
1220 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001221 }
1222
H. Peter Anvine2c80182005-01-15 22:15:51 +00001223 if (tline->type == TOK_OTHER) {
1224 if (!strcmp(tline->text, "<<"))
1225 return tokval->t_type = TOKEN_SHL;
1226 if (!strcmp(tline->text, ">>"))
1227 return tokval->t_type = TOKEN_SHR;
1228 if (!strcmp(tline->text, "//"))
1229 return tokval->t_type = TOKEN_SDIV;
1230 if (!strcmp(tline->text, "%%"))
1231 return tokval->t_type = TOKEN_SMOD;
1232 if (!strcmp(tline->text, "=="))
1233 return tokval->t_type = TOKEN_EQ;
1234 if (!strcmp(tline->text, "<>"))
1235 return tokval->t_type = TOKEN_NE;
1236 if (!strcmp(tline->text, "!="))
1237 return tokval->t_type = TOKEN_NE;
1238 if (!strcmp(tline->text, "<="))
1239 return tokval->t_type = TOKEN_LE;
1240 if (!strcmp(tline->text, ">="))
1241 return tokval->t_type = TOKEN_GE;
1242 if (!strcmp(tline->text, "&&"))
1243 return tokval->t_type = TOKEN_DBL_AND;
1244 if (!strcmp(tline->text, "^^"))
1245 return tokval->t_type = TOKEN_DBL_XOR;
1246 if (!strcmp(tline->text, "||"))
1247 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001248 }
1249
1250 /*
1251 * We have no other options: just return the first character of
1252 * the token text.
1253 */
1254 return tokval->t_type = tline->text[0];
1255}
1256
1257/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001258 * Compare a string to the name of an existing macro; this is a
1259 * simple wrapper which calls either strcmp or nasm_stricmp
1260 * depending on the value of the `casesense' parameter.
1261 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001262static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001263{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001264 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001265}
1266
1267/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001268 * Compare a string to the name of an existing macro; this is a
1269 * simple wrapper which calls either strcmp or nasm_stricmp
1270 * depending on the value of the `casesense' parameter.
1271 */
1272static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1273{
1274 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1275}
1276
1277/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001278 * Return the Context structure associated with a %$ token. Return
1279 * NULL, having _already_ reported an error condition, if the
1280 * context stack isn't deep enough for the supplied number of $
1281 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001282 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001283 * also scanned for such smacro, until it is found; if not -
1284 * only the context that directly results from the number of $'s
1285 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001286 */
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001287static Context *get_ctx(const char *name, bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001288{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001289 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001290 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001291 int i;
1292
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001293 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001294 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001295
H. Peter Anvine2c80182005-01-15 22:15:51 +00001296 if (!cstk) {
1297 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1298 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001299 }
1300
H. Peter Anvine2c80182005-01-15 22:15:51 +00001301 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1302 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001303/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001304 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001305 if (!ctx) {
1306 error(ERR_NONFATAL, "`%s': context stack is only"
1307 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1308 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001309 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001310 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001312
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 do {
1314 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001315 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001316 while (m) {
1317 if (!mstrcmp(m->name, name, m->casesense))
1318 return ctx;
1319 m = m->next;
1320 }
1321 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001322 }
1323 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001324 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001325}
1326
1327/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001328 * Check to see if a file is already in a string list
1329 */
1330static bool in_list(const StrList *list, const char *str)
1331{
1332 while (list) {
1333 if (!strcmp(list->str, str))
1334 return true;
1335 list = list->next;
1336 }
1337 return false;
1338}
1339
1340/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001341 * Open an include file. This routine must always return a valid
1342 * file pointer if it returns - it's responsible for throwing an
1343 * ERR_FATAL and bombing out completely if not. It should also try
1344 * the include path one by one until it finds the file or reaches
1345 * the end of the path.
1346 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001347static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
H. Peter Anvin418ca702008-05-30 10:42:30 -07001348 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001349{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001350 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001351 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001352 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001353 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001354 size_t prefix_len = 0;
1355 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001356
H. Peter Anvine2c80182005-01-15 22:15:51 +00001357 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001358 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1359 memcpy(sl->str, prefix, prefix_len);
1360 memcpy(sl->str+prefix_len, file, len+1);
1361 fp = fopen(sl->str, "r");
H. Peter Anvin418ca702008-05-30 10:42:30 -07001362 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001363 sl->next = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001364 **dtail = sl;
1365 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001366 } else {
1367 nasm_free(sl);
1368 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001369 if (fp)
1370 return fp;
H. Peter Anvin418ca702008-05-30 10:42:30 -07001371 if (!ip) {
1372 if (!missing_ok)
1373 break;
1374 prefix = NULL;
1375 } else {
1376 prefix = ip->path;
1377 ip = ip->next;
1378 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001379 if (prefix) {
1380 prefix_len = strlen(prefix);
1381 } else {
1382 /* -MG given and file not found */
H. Peter Anvin418ca702008-05-30 10:42:30 -07001383 if (dhead && !in_list(*dhead, file)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001384 sl = nasm_malloc(len+1+sizeof sl->next);
1385 sl->next = NULL;
1386 strcpy(sl->str, file);
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001387 **dtail = sl;
1388 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001389 }
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001390 return NULL;
1391 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001392 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001393
H. Peter Anvin734b1882002-04-30 21:01:08 +00001394 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001395 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001396}
1397
1398/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001399 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001400 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001401 * return true if _any_ single-line macro of that name is defined.
1402 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001403 * `nparam' or no parameters is defined.
1404 *
1405 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001406 * defined, or nparam is -1, the address of the definition structure
1407 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001408 * is NULL, no action will be taken regarding its contents, and no
1409 * error will occur.
1410 *
1411 * Note that this is also called with nparam zero to resolve
1412 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001413 *
1414 * If you already know which context macro belongs to, you can pass
1415 * the context pointer as first parameter; if you won't but name begins
1416 * with %$ the context will be automatically computed. If all_contexts
1417 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001418 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001419static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001420smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001421 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001422{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001423 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001424 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001425
H. Peter Anvin97a23472007-09-16 17:57:25 -07001426 if (ctx) {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001427 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001428 } else if (name[0] == '%' && name[1] == '$') {
1429 if (cstk)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001430 ctx = get_ctx(name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001431 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001432 return false; /* got to return _something_ */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001433 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001434 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001435 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001436 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001437 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001438
H. Peter Anvine2c80182005-01-15 22:15:51 +00001439 while (m) {
1440 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001441 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001442 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001443 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001444 *defn = m;
1445 else
1446 *defn = NULL;
1447 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001448 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001449 }
1450 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001451 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001452
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001453 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001454}
1455
1456/*
1457 * Count and mark off the parameters in a multi-line macro call.
1458 * This is called both from within the multi-line macro expansion
1459 * code, and also to mark off the default parameters when provided
1460 * in a %macro definition line.
1461 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001463{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001464 int paramsize, brace;
1465
1466 *nparam = paramsize = 0;
1467 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001468 while (t) {
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001469 /* +1: we need space for the final NULL */
1470 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001471 paramsize += PARAM_DELTA;
1472 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1473 }
1474 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001475 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001476 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001477 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001478 (*params)[(*nparam)++] = t;
1479 while (tok_isnt_(t, brace ? "}" : ","))
1480 t = t->next;
1481 if (t) { /* got a comma/brace */
1482 t = t->next;
1483 if (brace) {
1484 /*
1485 * Now we've found the closing brace, look further
1486 * for the comma.
1487 */
1488 skip_white_(t);
1489 if (tok_isnt_(t, ",")) {
1490 error(ERR_NONFATAL,
1491 "braces do not enclose all of macro parameter");
1492 while (tok_isnt_(t, ","))
1493 t = t->next;
1494 }
1495 if (t)
1496 t = t->next; /* eat the comma */
1497 }
1498 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001499 }
1500}
1501
1502/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001503 * Determine whether one of the various `if' conditions is true or
1504 * not.
1505 *
1506 * We must free the tline we get passed.
1507 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001508static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001509{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001510 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001511 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001512 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001513 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001514 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001515 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001516
1517 origline = tline;
1518
H. Peter Anvine2c80182005-01-15 22:15:51 +00001519 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001520 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001521 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001522 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001523 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001524 if (!tline)
1525 break;
1526 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001527 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001528 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001529 free_tlist(origline);
1530 return -1;
1531 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001532 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001533 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001534 tline = tline->next;
1535 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001536 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001537
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001538 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001539 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001540 while (tline) {
1541 skip_white_(tline);
1542 if (!tline || (tline->type != TOK_ID &&
1543 (tline->type != TOK_PREPROC_ID ||
1544 tline->text[1] != '$'))) {
1545 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001546 "`%s' expects macro identifiers", pp_directives[ct]);
1547 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001548 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001549 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001550 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001551 tline = tline->next;
1552 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001553 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001554
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001555 case PPC_IFIDN:
1556 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001557 tline = expand_smacro(tline);
1558 t = tt = tline;
1559 while (tok_isnt_(tt, ","))
1560 tt = tt->next;
1561 if (!tt) {
1562 error(ERR_NONFATAL,
1563 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001564 pp_directives[ct]);
1565 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001566 }
1567 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001568 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001569 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1570 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1571 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001572 pp_directives[ct]);
1573 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001574 }
1575 if (t->type == TOK_WHITESPACE) {
1576 t = t->next;
1577 continue;
1578 }
1579 if (tt->type == TOK_WHITESPACE) {
1580 tt = tt->next;
1581 continue;
1582 }
1583 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001584 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001585 break;
1586 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001587 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001588 if (t->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001589 size_t l1 = nasm_unquote(t->text, NULL);
1590 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001591
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001592 if (l1 != l2) {
1593 j = false;
1594 break;
1595 }
1596 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1597 j = false;
1598 break;
1599 }
1600 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001601 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001602 break;
1603 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001604
H. Peter Anvine2c80182005-01-15 22:15:51 +00001605 t = t->next;
1606 tt = tt->next;
1607 }
1608 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001609 j = false; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001610 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001611
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001612 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001613 {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001614 bool found = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001616
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 tline = tline->next;
1618 skip_white_(tline);
1619 tline = expand_id(tline);
1620 if (!tok_type_(tline, TOK_ID)) {
1621 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001622 "`%s' expects a macro name", pp_directives[ct]);
1623 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001624 }
1625 searching.name = nasm_strdup(tline->text);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001626 searching.casesense = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001627 searching.plus = false;
1628 searching.nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001629 searching.in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001630 searching.rep_nest = NULL;
1631 searching.nparam_min = 0;
1632 searching.nparam_max = INT_MAX;
1633 tline = expand_smacro(tline->next);
1634 skip_white_(tline);
1635 if (!tline) {
1636 } else if (!tok_type_(tline, TOK_NUMBER)) {
1637 error(ERR_NONFATAL,
1638 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001639 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001640 } else {
1641 searching.nparam_min = searching.nparam_max =
1642 readnum(tline->text, &j);
1643 if (j)
1644 error(ERR_NONFATAL,
1645 "unable to parse parameter count `%s'",
1646 tline->text);
1647 }
1648 if (tline && tok_is_(tline->next, "-")) {
1649 tline = tline->next->next;
1650 if (tok_is_(tline, "*"))
1651 searching.nparam_max = INT_MAX;
1652 else if (!tok_type_(tline, TOK_NUMBER))
1653 error(ERR_NONFATAL,
1654 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001655 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001656 else {
1657 searching.nparam_max = readnum(tline->text, &j);
1658 if (j)
1659 error(ERR_NONFATAL,
1660 "unable to parse parameter count `%s'",
1661 tline->text);
1662 if (searching.nparam_min > searching.nparam_max)
1663 error(ERR_NONFATAL,
1664 "minimum parameter count exceeds maximum");
1665 }
1666 }
1667 if (tline && tok_is_(tline->next, "+")) {
1668 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001669 searching.plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001670 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001671 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001672 while (mmac) {
1673 if (!strcmp(mmac->name, searching.name) &&
1674 (mmac->nparam_min <= searching.nparam_max
1675 || searching.plus)
1676 && (searching.nparam_min <= mmac->nparam_max
1677 || mmac->plus)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001678 found = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001679 break;
1680 }
1681 mmac = mmac->next;
1682 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001683 if(tline && tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001684 error(ERR_WARNING|ERR_PASS1,
1685 "trailing garbage after %%ifmacro ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001687 j = found;
1688 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001689 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001690
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001691 case PPC_IFID:
1692 needtype = TOK_ID;
1693 goto iftype;
1694 case PPC_IFNUM:
1695 needtype = TOK_NUMBER;
1696 goto iftype;
1697 case PPC_IFSTR:
1698 needtype = TOK_STRING;
1699 goto iftype;
1700
1701 iftype:
H. Peter Anvin134b9462008-02-16 17:01:40 -08001702 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001703
H. Peter Anvin927c92b2008-02-16 13:44:52 -08001704 while (tok_type_(t, TOK_WHITESPACE) ||
1705 (needtype == TOK_NUMBER &&
1706 tok_type_(t, TOK_OTHER) &&
1707 (t->text[0] == '-' || t->text[0] == '+') &&
1708 !t->text[1]))
1709 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001710
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001711 j = tok_type_(t, needtype);
1712 break;
1713
1714 case PPC_IFTOKEN:
1715 t = tline = expand_smacro(tline);
1716 while (tok_type_(t, TOK_WHITESPACE))
1717 t = t->next;
1718
1719 j = false;
1720 if (t) {
1721 t = t->next; /* Skip the actual token */
1722 while (tok_type_(t, TOK_WHITESPACE))
1723 t = t->next;
1724 j = !t; /* Should be nothing left */
1725 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001726 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001727
H. Peter Anvin134b9462008-02-16 17:01:40 -08001728 case PPC_IFEMPTY:
1729 t = tline = expand_smacro(tline);
1730 while (tok_type_(t, TOK_WHITESPACE))
1731 t = t->next;
1732
1733 j = !t; /* Should be empty */
1734 break;
1735
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001736 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001737 t = tline = expand_smacro(tline);
1738 tptr = &t;
1739 tokval.t_type = TOKEN_INVALID;
1740 evalresult = evaluate(ppscan, tptr, &tokval,
1741 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 if (!evalresult)
1743 return -1;
1744 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001745 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001746 "trailing garbage after expression ignored");
1747 if (!is_simple(evalresult)) {
1748 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001749 "non-constant value given to `%s'", pp_directives[ct]);
1750 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001752 j = reloc_value(evalresult) != 0;
H. Peter Anvin90e6e812008-07-16 14:38:24 -07001753 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001754
H. Peter Anvine2c80182005-01-15 22:15:51 +00001755 default:
1756 error(ERR_FATAL,
1757 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001758 pp_directives[ct]);
1759 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001760 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001761
1762 free_tlist(origline);
1763 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001764
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001765fail:
1766 free_tlist(origline);
1767 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001768}
1769
1770/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001771 * Common code for defining an smacro
1772 */
1773static bool define_smacro(Context *ctx, char *mname, bool casesense,
1774 int nparam, Token *expansion)
1775{
1776 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001777 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001778
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001779 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1780 if (!smac) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001781 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001782 "single-line macro `%s' defined both with and"
1783 " without parameters", mname);
1784
1785 /* Some instances of the old code considered this a failure,
1786 some others didn't. What is the right thing to do here? */
1787 free_tlist(expansion);
1788 return false; /* Failure */
1789 } else {
1790 /*
1791 * We're redefining, so we have to take over an
1792 * existing SMacro structure. This means freeing
1793 * what was already in it.
1794 */
1795 nasm_free(smac->name);
1796 free_tlist(smac->expansion);
1797 }
1798 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001799 smtbl = ctx ? &ctx->localmac : &smacros;
1800 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001801 smac = nasm_malloc(sizeof(SMacro));
1802 smac->next = *smhead;
1803 *smhead = smac;
1804 }
1805 smac->name = nasm_strdup(mname);
1806 smac->casesense = casesense;
1807 smac->nparam = nparam;
1808 smac->expansion = expansion;
1809 smac->in_progress = false;
1810 return true; /* Success */
1811}
1812
1813/*
1814 * Undefine an smacro
1815 */
1816static void undef_smacro(Context *ctx, const char *mname)
1817{
1818 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001819 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001820
H. Peter Anvin166c2472008-05-28 12:28:58 -07001821 smtbl = ctx ? &ctx->localmac : &smacros;
1822 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001823
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001824 if (smhead) {
1825 /*
1826 * We now have a macro name... go hunt for it.
1827 */
1828 sp = smhead;
1829 while ((s = *sp) != NULL) {
1830 if (!mstrcmp(s->name, mname, s->casesense)) {
1831 *sp = s->next;
1832 nasm_free(s->name);
1833 free_tlist(s->expansion);
1834 nasm_free(s);
1835 } else {
1836 sp = &s->next;
1837 }
1838 }
1839 }
1840}
1841
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001842/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07001843 * Parse a mmacro specification.
1844 */
1845static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1846{
1847 bool err;
1848
1849 tline = tline->next;
1850 skip_white_(tline);
1851 tline = expand_id(tline);
1852 if (!tok_type_(tline, TOK_ID)) {
1853 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1854 return false;
1855 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001856
H. Peter Anvina26433d2008-07-16 14:40:01 -07001857 def->name = nasm_strdup(tline->text);
1858 def->plus = false;
1859 def->nolist = false;
1860 def->in_progress = 0;
1861 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001862 def->nparam_min = 0;
1863 def->nparam_max = 0;
1864
H. Peter Anvina26433d2008-07-16 14:40:01 -07001865 tline = expand_smacro(tline->next);
1866 skip_white_(tline);
1867 if (!tok_type_(tline, TOK_NUMBER)) {
1868 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001869 } else {
1870 def->nparam_min = def->nparam_max =
1871 readnum(tline->text, &err);
1872 if (err)
1873 error(ERR_NONFATAL,
1874 "unable to parse parameter count `%s'", tline->text);
1875 }
1876 if (tline && tok_is_(tline->next, "-")) {
1877 tline = tline->next->next;
1878 if (tok_is_(tline, "*")) {
1879 def->nparam_max = INT_MAX;
1880 } else if (!tok_type_(tline, TOK_NUMBER)) {
1881 error(ERR_NONFATAL,
1882 "`%s' expects a parameter count after `-'", directive);
1883 } else {
1884 def->nparam_max = readnum(tline->text, &err);
1885 if (err) {
1886 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
1887 tline->text);
1888 }
1889 if (def->nparam_min > def->nparam_max) {
1890 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
1891 }
1892 }
1893 }
1894 if (tline && tok_is_(tline->next, "+")) {
1895 tline = tline->next;
1896 def->plus = true;
1897 }
1898 if (tline && tok_type_(tline->next, TOK_ID) &&
1899 !nasm_stricmp(tline->next->text, ".nolist")) {
1900 tline = tline->next;
1901 def->nolist = true;
1902 }
1903
1904 /*
1905 * Handle default parameters.
1906 */
1907 if (tline && tline->next) {
1908 def->dlist = tline->next;
1909 tline->next = NULL;
1910 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
1911 } else {
1912 def->dlist = NULL;
1913 def->defaults = NULL;
1914 }
1915 def->expansion = NULL;
1916
Victor van den Elzen22343c22008-08-06 14:47:54 +02001917 if(def->defaults &&
1918 def->ndefs > def->nparam_max - def->nparam_min &&
1919 !def->plus)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001920 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
1921 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001922
H. Peter Anvina26433d2008-07-16 14:40:01 -07001923 return true;
1924}
1925
1926
1927/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001928 * Decode a size directive
1929 */
1930static int parse_size(const char *str) {
1931 static const char *size_names[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001932 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001933 static const int sizes[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001934 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001935
1936 return sizes[bsii(str, size_names, elements(size_names))+1];
1937}
1938
Ed Beroset3ab3f412002-06-11 03:31:49 +00001939/**
1940 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001941 * Find out if a line contains a preprocessor directive, and deal
1942 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07001943 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001944 * If a directive _is_ found, it is the responsibility of this routine
1945 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001946 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001947 * @param tline a pointer to the current tokeninzed line linked list
1948 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07001949 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001950 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001951static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001952{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001953 enum preproc_token i;
1954 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07001955 bool err;
1956 int nparam;
1957 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001958 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001959 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001960 int offset;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001961 char *p, *pp, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001962 Include *inc;
1963 Context *ctx;
1964 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001965 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001966 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1967 Line *l;
1968 struct tokenval tokval;
1969 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001970 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001971 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001972 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07001973 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001974
1975 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001976
H. Peter Anvineba20a72002-04-30 20:53:55 +00001977 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07001978 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001979 (tline->text[1] == '%' || tline->text[1] == '$'
1980 || tline->text[1] == '!'))
1981 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001982
H. Peter Anvin4169a472007-09-12 01:29:43 +00001983 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001984
1985 /*
1986 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001987 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001988 * we should ignore all directives except for condition
1989 * directives.
1990 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001991 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001992 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1993 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001994 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001995
1996 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001997 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02001998 * ignore all directives except for %macro/%imacro (which nest),
1999 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2000 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002001 */
2002 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00002003 i != PP_ENDMACRO && i != PP_ENDM &&
2004 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2005 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002006 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002007
Charles Crayned4200be2008-07-12 16:42:33 -07002008 if (defining) {
2009 if (i == PP_MACRO || i == PP_IMACRO) {
2010 nested_mac_count++;
2011 return NO_DIRECTIVE_FOUND;
2012 } else if (nested_mac_count > 0) {
2013 if (i == PP_ENDMACRO) {
2014 nested_mac_count--;
2015 return NO_DIRECTIVE_FOUND;
2016 }
2017 }
2018 if (!defining->name) {
2019 if (i == PP_REP) {
2020 nested_rep_count++;
2021 return NO_DIRECTIVE_FOUND;
2022 } else if (nested_rep_count > 0) {
2023 if (i == PP_ENDREP) {
2024 nested_rep_count--;
2025 return NO_DIRECTIVE_FOUND;
2026 }
2027 }
2028 }
2029 }
2030
H. Peter Anvin4169a472007-09-12 01:29:43 +00002031 switch (i) {
2032 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002033 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2034 tline->text);
2035 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002036
H. Peter Anvine2c80182005-01-15 22:15:51 +00002037 case PP_STACKSIZE:
2038 /* Directive to tell NASM what the default stack size is. The
2039 * default is for a 16-bit stack, and this can be overriden with
2040 * %stacksize large.
2041 * the following form:
2042 *
2043 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2044 */
2045 tline = tline->next;
2046 if (tline && tline->type == TOK_WHITESPACE)
2047 tline = tline->next;
2048 if (!tline || tline->type != TOK_ID) {
2049 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2050 free_tlist(origline);
2051 return DIRECTIVE_FOUND;
2052 }
2053 if (nasm_stricmp(tline->text, "flat") == 0) {
2054 /* All subsequent ARG directives are for a 32-bit stack */
2055 StackSize = 4;
2056 StackPointer = "ebp";
2057 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002058 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002059 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2060 /* All subsequent ARG directives are for a 64-bit stack */
2061 StackSize = 8;
2062 StackPointer = "rbp";
2063 ArgOffset = 8;
2064 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002065 } else if (nasm_stricmp(tline->text, "large") == 0) {
2066 /* All subsequent ARG directives are for a 16-bit stack,
2067 * far function call.
2068 */
2069 StackSize = 2;
2070 StackPointer = "bp";
2071 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002072 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002073 } else if (nasm_stricmp(tline->text, "small") == 0) {
2074 /* All subsequent ARG directives are for a 16-bit stack,
2075 * far function call. We don't support near functions.
2076 */
2077 StackSize = 2;
2078 StackPointer = "bp";
2079 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002080 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002081 } else {
2082 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2083 free_tlist(origline);
2084 return DIRECTIVE_FOUND;
2085 }
2086 free_tlist(origline);
2087 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002088
H. Peter Anvine2c80182005-01-15 22:15:51 +00002089 case PP_ARG:
2090 /* TASM like ARG directive to define arguments to functions, in
2091 * the following form:
2092 *
2093 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2094 */
2095 offset = ArgOffset;
2096 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002097 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002098 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002099
H. Peter Anvine2c80182005-01-15 22:15:51 +00002100 /* Find the argument name */
2101 tline = tline->next;
2102 if (tline && tline->type == TOK_WHITESPACE)
2103 tline = tline->next;
2104 if (!tline || tline->type != TOK_ID) {
2105 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2106 free_tlist(origline);
2107 return DIRECTIVE_FOUND;
2108 }
2109 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002110
H. Peter Anvine2c80182005-01-15 22:15:51 +00002111 /* Find the argument size type */
2112 tline = tline->next;
2113 if (!tline || tline->type != TOK_OTHER
2114 || tline->text[0] != ':') {
2115 error(ERR_NONFATAL,
2116 "Syntax error processing `%%arg' directive");
2117 free_tlist(origline);
2118 return DIRECTIVE_FOUND;
2119 }
2120 tline = tline->next;
2121 if (!tline || tline->type != TOK_ID) {
2122 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2123 free_tlist(origline);
2124 return DIRECTIVE_FOUND;
2125 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002126
H. Peter Anvine2c80182005-01-15 22:15:51 +00002127 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002128 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002129 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002130 size = parse_size(tt->text);
2131 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002132 error(ERR_NONFATAL,
2133 "Invalid size type for `%%arg' missing directive");
2134 free_tlist(tt);
2135 free_tlist(origline);
2136 return DIRECTIVE_FOUND;
2137 }
2138 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002139
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002140 /* Round up to even stack slots */
2141 size = (size+StackSize-1) & ~(StackSize-1);
2142
H. Peter Anvine2c80182005-01-15 22:15:51 +00002143 /* Now define the macro for the argument */
2144 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2145 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002146 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002147 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002148
H. Peter Anvine2c80182005-01-15 22:15:51 +00002149 /* Move to the next argument in the list */
2150 tline = tline->next;
2151 if (tline && tline->type == TOK_WHITESPACE)
2152 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002153 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2154 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002155 free_tlist(origline);
2156 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002157
H. Peter Anvine2c80182005-01-15 22:15:51 +00002158 case PP_LOCAL:
2159 /* TASM like LOCAL directive to define local variables for a
2160 * function, in the following form:
2161 *
2162 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2163 *
2164 * The '= LocalSize' at the end is ignored by NASM, but is
2165 * required by TASM to define the local parameter size (and used
2166 * by the TASM macro package).
2167 */
2168 offset = LocalOffset;
2169 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002170 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002171 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002172
H. Peter Anvine2c80182005-01-15 22:15:51 +00002173 /* Find the argument name */
2174 tline = tline->next;
2175 if (tline && tline->type == TOK_WHITESPACE)
2176 tline = tline->next;
2177 if (!tline || tline->type != TOK_ID) {
2178 error(ERR_NONFATAL,
2179 "`%%local' missing argument parameter");
2180 free_tlist(origline);
2181 return DIRECTIVE_FOUND;
2182 }
2183 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002184
H. Peter Anvine2c80182005-01-15 22:15:51 +00002185 /* Find the argument size type */
2186 tline = tline->next;
2187 if (!tline || tline->type != TOK_OTHER
2188 || tline->text[0] != ':') {
2189 error(ERR_NONFATAL,
2190 "Syntax error processing `%%local' directive");
2191 free_tlist(origline);
2192 return DIRECTIVE_FOUND;
2193 }
2194 tline = tline->next;
2195 if (!tline || tline->type != TOK_ID) {
2196 error(ERR_NONFATAL,
2197 "`%%local' missing size type parameter");
2198 free_tlist(origline);
2199 return DIRECTIVE_FOUND;
2200 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002201
H. Peter Anvine2c80182005-01-15 22:15:51 +00002202 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002203 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002204 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002205 size = parse_size(tt->text);
2206 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002207 error(ERR_NONFATAL,
2208 "Invalid size type for `%%local' missing directive");
2209 free_tlist(tt);
2210 free_tlist(origline);
2211 return DIRECTIVE_FOUND;
2212 }
2213 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002214
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002215 /* Round up to even stack slots */
2216 size = (size+StackSize-1) & ~(StackSize-1);
2217
2218 offset += size; /* Negative offset, increment before */
2219
2220 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002221 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2222 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002223 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002224
H. Peter Anvine2c80182005-01-15 22:15:51 +00002225 /* Now define the assign to setup the enter_c macro correctly */
2226 snprintf(directive, sizeof(directive),
2227 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002228 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002229
H. Peter Anvine2c80182005-01-15 22:15:51 +00002230 /* Move to the next argument in the list */
2231 tline = tline->next;
2232 if (tline && tline->type == TOK_WHITESPACE)
2233 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002234 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2235 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002236 free_tlist(origline);
2237 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002238
H. Peter Anvine2c80182005-01-15 22:15:51 +00002239 case PP_CLEAR:
2240 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002241 error(ERR_WARNING|ERR_PASS1,
2242 "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002243 free_macros();
2244 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002245 free_tlist(origline);
2246 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002247
H. Peter Anvin418ca702008-05-30 10:42:30 -07002248 case PP_DEPEND:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002249 t = tline->next = expand_smacro(tline->next);
2250 skip_white_(t);
2251 if (!t || (t->type != TOK_STRING &&
2252 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002253 error(ERR_NONFATAL, "`%%depend' expects a file name");
2254 free_tlist(origline);
2255 return DIRECTIVE_FOUND; /* but we did _something_ */
2256 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002257 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002258 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002259 "trailing garbage after `%%depend' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002260 p = t->text;
2261 if (t->type != TOK_INTERNAL_STRING)
2262 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002263 if (dephead && !in_list(*dephead, p)) {
2264 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2265 sl->next = NULL;
2266 strcpy(sl->str, p);
2267 *deptail = sl;
2268 deptail = &sl->next;
2269 }
2270 free_tlist(origline);
2271 return DIRECTIVE_FOUND;
2272
2273 case PP_INCLUDE:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002274 t = tline->next = expand_smacro(tline->next);
2275 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002276
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002277 if (!t || (t->type != TOK_STRING &&
2278 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002279 error(ERR_NONFATAL, "`%%include' expects a file name");
2280 free_tlist(origline);
2281 return DIRECTIVE_FOUND; /* but we did _something_ */
2282 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002283 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002284 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002285 "trailing garbage after `%%include' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002286 p = t->text;
2287 if (t->type != TOK_INTERNAL_STRING)
2288 nasm_unquote(p, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002289 inc = nasm_malloc(sizeof(Include));
2290 inc->next = istk;
2291 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002292 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002293 if (!inc->fp) {
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002294 /* -MG given but file not found */
2295 nasm_free(inc);
2296 } else {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002297 inc->fname = src_set_fname(nasm_strdup(p));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002298 inc->lineno = src_set_linnum(0);
2299 inc->lineinc = 1;
2300 inc->expansion = NULL;
2301 inc->mstk = NULL;
2302 istk = inc;
2303 list->uplevel(LIST_INCLUDE);
2304 }
2305 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002306 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002307
H. Peter Anvind2456592008-06-19 15:04:18 -07002308 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002309 {
H. Peter Anvina70547f2008-07-19 21:44:26 -07002310 static macros_t *use_pkg;
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002311 const char *pkg_macro;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002312
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002313 tline = tline->next;
2314 skip_white_(tline);
2315 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002316
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002317 if (!tline || (tline->type != TOK_STRING &&
2318 tline->type != TOK_INTERNAL_STRING &&
2319 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002320 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002321 free_tlist(origline);
2322 return DIRECTIVE_FOUND; /* but we did _something_ */
2323 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002324 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002325 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002326 "trailing garbage after `%%use' ignored");
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002327 if (tline->type == TOK_STRING)
2328 nasm_unquote(tline->text, NULL);
2329 use_pkg = nasm_stdmac_find_package(tline->text);
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002330 if (!use_pkg)
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002331 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002332 /* The first string will be <%define>__USE_*__ */
H. Peter Anvin7e50d232008-06-25 14:54:14 -07002333 pkg_macro = (char *)use_pkg + 1;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002334 if (!smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2335 /* Not already included, go ahead and include it */
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002336 stdmacpos = use_pkg;
2337 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002338 free_tlist(origline);
2339 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002340 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002341 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002342 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002343 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002344 tline = tline->next;
2345 skip_white_(tline);
2346 tline = expand_id(tline);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002347 if (tline) {
2348 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin42b56392008-10-24 16:24:21 -07002349 error(ERR_NONFATAL, "`%s' expects a context identifier",
2350 pp_directives[i]);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002351 free_tlist(origline);
2352 return DIRECTIVE_FOUND; /* but we did _something_ */
2353 }
2354 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002355 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin42b56392008-10-24 16:24:21 -07002356 "trailing garbage after `%s' ignored",
2357 pp_directives[i]);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002358 p = nasm_strdup(tline->text);
2359 } else {
H. Peter Anvin42b56392008-10-24 16:24:21 -07002360 p = NULL; /* Anonymous */
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002361 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002362
2363 if (i == PP_PUSH) {
2364 ctx = nasm_malloc(sizeof(Context));
2365 ctx->next = cstk;
2366 hash_init(&ctx->localmac, HASH_SMALL);
2367 ctx->name = p;
2368 ctx->number = unique++;
2369 cstk = ctx;
2370 } else {
2371 /* %pop or %repl */
2372 if (!cstk) {
2373 error(ERR_NONFATAL, "`%s': context stack is empty",
2374 pp_directives[i]);
2375 } else if (i == PP_POP) {
2376 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2377 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2378 "expected %s",
2379 cstk->name ? cstk->name : "anonymous", p);
2380 else
2381 ctx_pop();
2382 } else {
2383 /* i == PP_REPL */
2384 nasm_free(cstk->name);
2385 cstk->name = p;
2386 p = NULL;
2387 }
2388 nasm_free(p);
2389 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002390 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002391 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002392 case PP_FATAL:
2393 severity = ERR_FATAL|ERR_NO_SEVERITY;
2394 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002395 case PP_ERROR:
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002396 severity = ERR_NONFATAL|ERR_NO_SEVERITY;
2397 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002398 case PP_WARNING:
H. Peter Anvin2f160432008-09-30 16:39:17 -07002399 severity = ERR_WARNING|ERR_NO_SEVERITY|ERR_WARN_USER;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002400 goto issue_error;
2401
2402 issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002403 {
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002404 /* Only error out if this is the final pass */
2405 if (pass != 2 && i != PP_FATAL)
2406 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002407
H. Peter Anvine2c80182005-01-15 22:15:51 +00002408 tline->next = expand_smacro(tline->next);
2409 tline = tline->next;
2410 skip_white_(tline);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002411 t = tline ? tline->next : NULL;
2412 skip_white_(t);
2413 if (tok_type_(tline, TOK_STRING) && !t) {
2414 /* The line contains only a quoted string */
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002415 p = tline->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002416 nasm_unquote(p, NULL);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002417 error(severity, "%s: %s", pp_directives[i], p);
2418 } else {
2419 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002420 p = detoken(tline, false);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002421 error(severity, "%s: %s", pp_directives[i], p);
2422 nasm_free(p);
H. Peter Anvind2456592008-06-19 15:04:18 -07002423 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002424 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002425 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002426 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002427
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002428 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002429 if (istk->conds && !emitting(istk->conds->state))
2430 j = COND_NEVER;
2431 else {
2432 j = if_condition(tline->next, i);
2433 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002434 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2435 }
2436 cond = nasm_malloc(sizeof(Cond));
2437 cond->next = istk->conds;
2438 cond->state = j;
2439 istk->conds = cond;
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002440 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002441 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002442
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002443 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002444 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002445 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002446 switch(istk->conds->state) {
2447 case COND_IF_TRUE:
2448 istk->conds->state = COND_DONE;
2449 break;
2450
2451 case COND_DONE:
2452 case COND_NEVER:
2453 break;
2454
2455 case COND_ELSE_TRUE:
2456 case COND_ELSE_FALSE:
H. Peter Anvin917a3492008-09-24 09:14:49 -07002457 error_precond(ERR_WARNING|ERR_PASS1,
2458 "`%%elif' after `%%else' ignored");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002459 istk->conds->state = COND_NEVER;
2460 break;
2461
2462 case COND_IF_FALSE:
2463 /*
2464 * IMPORTANT: In the case of %if, we will already have
2465 * called expand_mmac_params(); however, if we're
2466 * processing an %elif we must have been in a
2467 * non-emitting mode, which would have inhibited
H. Peter Anvin992fe752008-10-19 15:45:05 -07002468 * the normal invocation of expand_indirect() and
2469 * expand_mmac_params(). Therefore, we have to do it
2470 * explicitly here.
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002471 */
H. Peter Anvin992fe752008-10-19 15:45:05 -07002472 t = expand_indirect(tline->next,0);
2473 t = expand_mmac_params(t);
2474 j = if_condition(expand_mmac_params(t), i);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002475 tline->next = NULL; /* it got freed */
2476 istk->conds->state =
2477 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2478 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002479 }
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002480 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002481 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002482
H. Peter Anvine2c80182005-01-15 22:15:51 +00002483 case PP_ELSE:
2484 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002485 error_precond(ERR_WARNING|ERR_PASS1,
2486 "trailing garbage after `%%else' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002487 if (!istk->conds)
2488 error(ERR_FATAL, "`%%else': no matching `%%if'");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002489 switch(istk->conds->state) {
2490 case COND_IF_TRUE:
2491 case COND_DONE:
2492 istk->conds->state = COND_ELSE_FALSE;
2493 break;
2494
2495 case COND_NEVER:
2496 break;
2497
2498 case COND_IF_FALSE:
2499 istk->conds->state = COND_ELSE_TRUE;
2500 break;
2501
2502 case COND_ELSE_TRUE:
2503 case COND_ELSE_FALSE:
H. Peter Anvin917a3492008-09-24 09:14:49 -07002504 error_precond(ERR_WARNING|ERR_PASS1,
2505 "`%%else' after `%%else' ignored.");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002506 istk->conds->state = COND_NEVER;
2507 break;
2508 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002509 free_tlist(origline);
2510 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002511
H. Peter Anvine2c80182005-01-15 22:15:51 +00002512 case PP_ENDIF:
2513 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002514 error_precond(ERR_WARNING|ERR_PASS1,
2515 "trailing garbage after `%%endif' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002516 if (!istk->conds)
2517 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2518 cond = istk->conds;
2519 istk->conds = cond->next;
2520 nasm_free(cond);
2521 free_tlist(origline);
2522 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002523
H. Peter Anvine2c80182005-01-15 22:15:51 +00002524 case PP_MACRO:
2525 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002526 if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002527 error(ERR_FATAL,
2528 "`%%%smacro': already defining a macro",
2529 (i == PP_IMACRO ? "i" : ""));
H. Peter Anvina26433d2008-07-16 14:40:01 -07002530 return DIRECTIVE_FOUND;
2531 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002532 defining = nasm_malloc(sizeof(MMacro));
H. Peter Anvina26433d2008-07-16 14:40:01 -07002533 defining->casesense = (i == PP_MACRO);
2534 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2535 nasm_free(defining);
2536 defining = NULL;
2537 return DIRECTIVE_FOUND;
2538 }
2539
H. Peter Anvin166c2472008-05-28 12:28:58 -07002540 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002541 while (mmac) {
2542 if (!strcmp(mmac->name, defining->name) &&
2543 (mmac->nparam_min <= defining->nparam_max
2544 || defining->plus)
2545 && (defining->nparam_min <= mmac->nparam_max
2546 || mmac->plus)) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002547 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002548 "redefining multi-line macro `%s'", defining->name);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002549 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002550 }
2551 mmac = mmac->next;
2552 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002553 free_tlist(origline);
2554 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002555
H. Peter Anvine2c80182005-01-15 22:15:51 +00002556 case PP_ENDM:
2557 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002558 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002559 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2560 return DIRECTIVE_FOUND;
2561 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002562 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002563 defining->next = *mmhead;
2564 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002565 defining = NULL;
2566 free_tlist(origline);
2567 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002568
H. Peter Anvina26433d2008-07-16 14:40:01 -07002569 case PP_UNMACRO:
2570 case PP_UNIMACRO:
2571 {
2572 MMacro **mmac_p;
2573 MMacro spec;
2574
2575 spec.casesense = (i == PP_UNMACRO);
2576 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2577 return DIRECTIVE_FOUND;
2578 }
2579 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2580 while (mmac_p && *mmac_p) {
2581 mmac = *mmac_p;
2582 if (mmac->casesense == spec.casesense &&
2583 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2584 mmac->nparam_min == spec.nparam_min &&
2585 mmac->nparam_max == spec.nparam_max &&
2586 mmac->plus == spec.plus) {
2587 *mmac_p = mmac->next;
2588 free_mmacro(mmac);
2589 } else {
2590 mmac_p = &mmac->next;
2591 }
2592 }
2593 free_tlist(origline);
2594 free_tlist(spec.dlist);
2595 return DIRECTIVE_FOUND;
2596 }
2597
H. Peter Anvine2c80182005-01-15 22:15:51 +00002598 case PP_ROTATE:
2599 if (tline->next && tline->next->type == TOK_WHITESPACE)
2600 tline = tline->next;
2601 if (tline->next == NULL) {
2602 free_tlist(origline);
2603 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2604 return DIRECTIVE_FOUND;
2605 }
2606 t = expand_smacro(tline->next);
2607 tline->next = NULL;
2608 free_tlist(origline);
2609 tline = t;
2610 tptr = &t;
2611 tokval.t_type = TOKEN_INVALID;
2612 evalresult =
2613 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2614 free_tlist(tline);
2615 if (!evalresult)
2616 return DIRECTIVE_FOUND;
2617 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002618 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002619 "trailing garbage after expression ignored");
2620 if (!is_simple(evalresult)) {
2621 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2622 return DIRECTIVE_FOUND;
2623 }
2624 mmac = istk->mstk;
2625 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2626 mmac = mmac->next_active;
2627 if (!mmac) {
2628 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2629 } else if (mmac->nparam == 0) {
2630 error(ERR_NONFATAL,
2631 "`%%rotate' invoked within macro without parameters");
2632 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002633 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002634
H. Peter Anvin25a99342007-09-22 17:45:45 -07002635 rotate %= (int)mmac->nparam;
2636 if (rotate < 0)
2637 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002638
H. Peter Anvin25a99342007-09-22 17:45:45 -07002639 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002640 }
2641 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002642
H. Peter Anvine2c80182005-01-15 22:15:51 +00002643 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002644 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002645 do {
2646 tline = tline->next;
2647 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002648
H. Peter Anvine2c80182005-01-15 22:15:51 +00002649 if (tok_type_(tline, TOK_ID) &&
2650 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002651 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002652 do {
2653 tline = tline->next;
2654 } while (tok_type_(tline, TOK_WHITESPACE));
2655 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002656
H. Peter Anvine2c80182005-01-15 22:15:51 +00002657 if (tline) {
2658 t = expand_smacro(tline);
2659 tptr = &t;
2660 tokval.t_type = TOKEN_INVALID;
2661 evalresult =
2662 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2663 if (!evalresult) {
2664 free_tlist(origline);
2665 return DIRECTIVE_FOUND;
2666 }
2667 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002668 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002669 "trailing garbage after expression ignored");
2670 if (!is_simple(evalresult)) {
2671 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2672 return DIRECTIVE_FOUND;
2673 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002674 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002675 } else {
2676 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002677 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002678 }
2679 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002680
H. Peter Anvine2c80182005-01-15 22:15:51 +00002681 tmp_defining = defining;
2682 defining = nasm_malloc(sizeof(MMacro));
2683 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002684 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002685 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002686 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002687 defining->in_progress = count;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002688 defining->nparam_min = defining->nparam_max = 0;
2689 defining->defaults = NULL;
2690 defining->dlist = NULL;
2691 defining->expansion = NULL;
2692 defining->next_active = istk->mstk;
2693 defining->rep_nest = tmp_defining;
2694 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002695
H. Peter Anvine2c80182005-01-15 22:15:51 +00002696 case PP_ENDREP:
2697 if (!defining || defining->name) {
2698 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2699 return DIRECTIVE_FOUND;
2700 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002701
H. Peter Anvine2c80182005-01-15 22:15:51 +00002702 /*
2703 * Now we have a "macro" defined - although it has no name
2704 * and we won't be entering it in the hash tables - we must
2705 * push a macro-end marker for it on to istk->expansion.
2706 * After that, it will take care of propagating itself (a
2707 * macro-end marker line for a macro which is really a %rep
2708 * block will cause the macro to be re-expanded, complete
2709 * with another macro-end marker to ensure the process
2710 * continues) until the whole expansion is forcibly removed
2711 * from istk->expansion by a %exitrep.
2712 */
2713 l = nasm_malloc(sizeof(Line));
2714 l->next = istk->expansion;
2715 l->finishes = defining;
2716 l->first = NULL;
2717 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002718
H. Peter Anvine2c80182005-01-15 22:15:51 +00002719 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002720
H. Peter Anvine2c80182005-01-15 22:15:51 +00002721 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2722 tmp_defining = defining;
2723 defining = defining->rep_nest;
2724 free_tlist(origline);
2725 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002726
H. Peter Anvine2c80182005-01-15 22:15:51 +00002727 case PP_EXITREP:
2728 /*
2729 * We must search along istk->expansion until we hit a
2730 * macro-end marker for a macro with no name. Then we set
2731 * its `in_progress' flag to 0.
2732 */
2733 for (l = istk->expansion; l; l = l->next)
2734 if (l->finishes && !l->finishes->name)
H. Peter Anvinca348b62008-07-23 10:49:26 -04002735 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002736
H. Peter Anvine2c80182005-01-15 22:15:51 +00002737 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07002738 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002739 else
2740 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2741 free_tlist(origline);
2742 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002743
H. Peter Anvine2c80182005-01-15 22:15:51 +00002744 case PP_XDEFINE:
2745 case PP_IXDEFINE:
2746 case PP_DEFINE:
2747 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002748 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002749
H. Peter Anvine2c80182005-01-15 22:15:51 +00002750 tline = tline->next;
2751 skip_white_(tline);
2752 tline = expand_id(tline);
2753 if (!tline || (tline->type != TOK_ID &&
2754 (tline->type != TOK_PREPROC_ID ||
2755 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002756 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2757 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002758 free_tlist(origline);
2759 return DIRECTIVE_FOUND;
2760 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002761
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002762 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002763
H. Peter Anvine2c80182005-01-15 22:15:51 +00002764 mname = tline->text;
2765 last = tline;
2766 param_start = tline = tline->next;
2767 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002768
H. Peter Anvine2c80182005-01-15 22:15:51 +00002769 /* Expand the macro definition now for %xdefine and %ixdefine */
2770 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2771 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002772
H. Peter Anvine2c80182005-01-15 22:15:51 +00002773 if (tok_is_(tline, "(")) {
2774 /*
2775 * This macro has parameters.
2776 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002777
H. Peter Anvine2c80182005-01-15 22:15:51 +00002778 tline = tline->next;
2779 while (1) {
2780 skip_white_(tline);
2781 if (!tline) {
2782 error(ERR_NONFATAL, "parameter identifier expected");
2783 free_tlist(origline);
2784 return DIRECTIVE_FOUND;
2785 }
2786 if (tline->type != TOK_ID) {
2787 error(ERR_NONFATAL,
2788 "`%s': parameter identifier expected",
2789 tline->text);
2790 free_tlist(origline);
2791 return DIRECTIVE_FOUND;
2792 }
2793 tline->type = TOK_SMAC_PARAM + nparam++;
2794 tline = tline->next;
2795 skip_white_(tline);
2796 if (tok_is_(tline, ",")) {
2797 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04002798 } else {
2799 if (!tok_is_(tline, ")")) {
2800 error(ERR_NONFATAL,
2801 "`)' expected to terminate macro template");
2802 free_tlist(origline);
2803 return DIRECTIVE_FOUND;
2804 }
2805 break;
2806 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002807 }
2808 last = tline;
2809 tline = tline->next;
2810 }
2811 if (tok_type_(tline, TOK_WHITESPACE))
2812 last = tline, tline = tline->next;
2813 macro_start = NULL;
2814 last->next = NULL;
2815 t = tline;
2816 while (t) {
2817 if (t->type == TOK_ID) {
2818 for (tt = param_start; tt; tt = tt->next)
2819 if (tt->type >= TOK_SMAC_PARAM &&
2820 !strcmp(tt->text, t->text))
2821 t->type = tt->type;
2822 }
2823 tt = t->next;
2824 t->next = macro_start;
2825 macro_start = t;
2826 t = tt;
2827 }
2828 /*
2829 * Good. We now have a macro name, a parameter count, and a
2830 * token list (in reverse order) for an expansion. We ought
2831 * to be OK just to create an SMacro, store it, and let
2832 * free_tlist have the rest of the line (which we have
2833 * carefully re-terminated after chopping off the expansion
2834 * from the end).
2835 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002836 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002837 free_tlist(origline);
2838 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002839
H. Peter Anvine2c80182005-01-15 22:15:51 +00002840 case PP_UNDEF:
2841 tline = tline->next;
2842 skip_white_(tline);
2843 tline = expand_id(tline);
2844 if (!tline || (tline->type != TOK_ID &&
2845 (tline->type != TOK_PREPROC_ID ||
2846 tline->text[1] != '$'))) {
2847 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2848 free_tlist(origline);
2849 return DIRECTIVE_FOUND;
2850 }
2851 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002852 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002853 "trailing garbage after macro name ignored");
2854 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002855
H. Peter Anvine2c80182005-01-15 22:15:51 +00002856 /* Find the context that symbol belongs to */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002857 ctx = get_ctx(tline->text, false);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002858 undef_smacro(ctx, tline->text);
2859 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002860 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002861
H. Peter Anvin9e200162008-06-04 17:23:14 -07002862 case PP_DEFSTR:
2863 case PP_IDEFSTR:
2864 casesense = (i == PP_DEFSTR);
2865
2866 tline = tline->next;
2867 skip_white_(tline);
2868 tline = expand_id(tline);
2869 if (!tline || (tline->type != TOK_ID &&
2870 (tline->type != TOK_PREPROC_ID ||
2871 tline->text[1] != '$'))) {
2872 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2873 pp_directives[i]);
2874 free_tlist(origline);
2875 return DIRECTIVE_FOUND;
2876 }
2877
2878 ctx = get_ctx(tline->text, false);
2879
2880 mname = tline->text;
2881 last = tline;
2882 tline = expand_smacro(tline->next);
2883 last->next = NULL;
2884
2885 while (tok_type_(tline, TOK_WHITESPACE))
2886 tline = delete_Token(tline);
2887
2888 p = detoken(tline, false);
2889 macro_start = nasm_malloc(sizeof(*macro_start));
2890 macro_start->next = NULL;
2891 macro_start->text = nasm_quote(p, strlen(p));
2892 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002893 macro_start->a.mac = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07002894 nasm_free(p);
2895
2896 /*
2897 * We now have a macro name, an implicit parameter count of
2898 * zero, and a string token to use as an expansion. Create
2899 * and store an SMacro.
2900 */
2901 define_smacro(ctx, mname, casesense, 0, macro_start);
2902 free_tlist(origline);
2903 return DIRECTIVE_FOUND;
2904
H. Peter Anvin418ca702008-05-30 10:42:30 -07002905 case PP_PATHSEARCH:
2906 {
2907 FILE *fp;
2908 StrList *xsl = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002909 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002910
2911 casesense = true;
2912
2913 tline = tline->next;
2914 skip_white_(tline);
2915 tline = expand_id(tline);
2916 if (!tline || (tline->type != TOK_ID &&
2917 (tline->type != TOK_PREPROC_ID ||
2918 tline->text[1] != '$'))) {
2919 error(ERR_NONFATAL,
2920 "`%%pathsearch' expects a macro identifier as first parameter");
2921 free_tlist(origline);
2922 return DIRECTIVE_FOUND;
2923 }
2924 ctx = get_ctx(tline->text, false);
2925
2926 mname = tline->text;
2927 last = tline;
2928 tline = expand_smacro(tline->next);
2929 last->next = NULL;
2930
2931 t = tline;
2932 while (tok_type_(t, TOK_WHITESPACE))
2933 t = t->next;
2934
2935 if (!t || (t->type != TOK_STRING &&
2936 t->type != TOK_INTERNAL_STRING)) {
2937 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
2938 free_tlist(tline);
2939 free_tlist(origline);
2940 return DIRECTIVE_FOUND; /* but we did _something_ */
2941 }
2942 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002943 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002944 "trailing garbage after `%%pathsearch' ignored");
H. Peter Anvin427cc912008-06-01 21:43:03 -07002945 p = t->text;
2946 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002947 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002948
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002949 fp = inc_fopen(p, &xsl, &xst, true);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002950 if (fp) {
2951 p = xsl->str;
2952 fclose(fp); /* Don't actually care about the file */
2953 }
2954 macro_start = nasm_malloc(sizeof(*macro_start));
2955 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002956 macro_start->text = nasm_quote(p, strlen(p));
H. Peter Anvin418ca702008-05-30 10:42:30 -07002957 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002958 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002959 if (xsl)
2960 nasm_free(xsl);
2961
2962 /*
2963 * We now have a macro name, an implicit parameter count of
2964 * zero, and a string token to use as an expansion. Create
2965 * and store an SMacro.
2966 */
2967 define_smacro(ctx, mname, casesense, 0, macro_start);
2968 free_tlist(tline);
2969 free_tlist(origline);
2970 return DIRECTIVE_FOUND;
2971 }
2972
H. Peter Anvine2c80182005-01-15 22:15:51 +00002973 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002974 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07002975
H. Peter Anvine2c80182005-01-15 22:15:51 +00002976 tline = tline->next;
2977 skip_white_(tline);
2978 tline = expand_id(tline);
2979 if (!tline || (tline->type != TOK_ID &&
2980 (tline->type != TOK_PREPROC_ID ||
2981 tline->text[1] != '$'))) {
2982 error(ERR_NONFATAL,
2983 "`%%strlen' expects a macro identifier as first parameter");
2984 free_tlist(origline);
2985 return DIRECTIVE_FOUND;
2986 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002987 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002988
H. Peter Anvine2c80182005-01-15 22:15:51 +00002989 mname = tline->text;
2990 last = tline;
2991 tline = expand_smacro(tline->next);
2992 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002993
H. Peter Anvine2c80182005-01-15 22:15:51 +00002994 t = tline;
2995 while (tok_type_(t, TOK_WHITESPACE))
2996 t = t->next;
2997 /* t should now point to the string */
2998 if (t->type != TOK_STRING) {
2999 error(ERR_NONFATAL,
3000 "`%%strlen` requires string as second parameter");
3001 free_tlist(tline);
3002 free_tlist(origline);
3003 return DIRECTIVE_FOUND;
3004 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003005
H. Peter Anvine2c80182005-01-15 22:15:51 +00003006 macro_start = nasm_malloc(sizeof(*macro_start));
3007 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003008 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003009 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003010
H. Peter Anvine2c80182005-01-15 22:15:51 +00003011 /*
3012 * We now have a macro name, an implicit parameter count of
3013 * zero, and a numeric token to use as an expansion. Create
3014 * and store an SMacro.
3015 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003016 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003017 free_tlist(tline);
3018 free_tlist(origline);
3019 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003020
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003021 case PP_STRCAT:
3022 casesense = true;
3023
3024 tline = tline->next;
3025 skip_white_(tline);
3026 tline = expand_id(tline);
3027 if (!tline || (tline->type != TOK_ID &&
3028 (tline->type != TOK_PREPROC_ID ||
3029 tline->text[1] != '$'))) {
3030 error(ERR_NONFATAL,
3031 "`%%strcat' expects a macro identifier as first parameter");
3032 free_tlist(origline);
3033 return DIRECTIVE_FOUND;
3034 }
3035 ctx = get_ctx(tline->text, false);
3036
3037 mname = tline->text;
3038 last = tline;
3039 tline = expand_smacro(tline->next);
3040 last->next = NULL;
3041
3042 len = 0;
3043 for (t = tline; t; t = t->next) {
3044 switch (t->type) {
3045 case TOK_WHITESPACE:
3046 break;
3047 case TOK_STRING:
3048 len += t->a.len = nasm_unquote(t->text, NULL);
3049 break;
H. Peter Anvinaccf4332008-07-01 21:42:08 -07003050 case TOK_OTHER:
3051 if (!strcmp(t->text, ",")) /* permit comma separators */
3052 break;
3053 /* else fall through */
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003054 default:
3055 error(ERR_NONFATAL,
3056 "non-string passed to `%%strcat' (%d)", t->type);
3057 free_tlist(tline);
3058 free_tlist(origline);
3059 return DIRECTIVE_FOUND;
3060 }
3061 }
3062
3063 p = pp = nasm_malloc(len);
3064 t = tline;
3065 for (t = tline; t; t = t->next) {
3066 if (t->type == TOK_STRING) {
3067 memcpy(p, t->text, t->a.len);
3068 p += t->a.len;
3069 }
3070 }
3071
3072 /*
3073 * We now have a macro name, an implicit parameter count of
3074 * zero, and a numeric token to use as an expansion. Create
3075 * and store an SMacro.
3076 */
3077 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3078 macro_start->text = nasm_quote(pp, len);
3079 nasm_free(pp);
3080 define_smacro(ctx, mname, casesense, 0, macro_start);
3081 free_tlist(tline);
3082 free_tlist(origline);
3083 return DIRECTIVE_FOUND;
3084
H. Peter Anvine2c80182005-01-15 22:15:51 +00003085 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003086 {
3087 int64_t a1, a2;
3088 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003089
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003090 casesense = true;
3091
H. Peter Anvine2c80182005-01-15 22:15:51 +00003092 tline = tline->next;
3093 skip_white_(tline);
3094 tline = expand_id(tline);
3095 if (!tline || (tline->type != TOK_ID &&
3096 (tline->type != TOK_PREPROC_ID ||
3097 tline->text[1] != '$'))) {
3098 error(ERR_NONFATAL,
3099 "`%%substr' expects a macro identifier as first parameter");
3100 free_tlist(origline);
3101 return DIRECTIVE_FOUND;
3102 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003103 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003104
H. Peter Anvine2c80182005-01-15 22:15:51 +00003105 mname = tline->text;
3106 last = tline;
3107 tline = expand_smacro(tline->next);
3108 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003109
H. Peter Anvine2c80182005-01-15 22:15:51 +00003110 t = tline->next;
3111 while (tok_type_(t, TOK_WHITESPACE))
3112 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003113
H. Peter Anvine2c80182005-01-15 22:15:51 +00003114 /* t should now point to the string */
3115 if (t->type != TOK_STRING) {
3116 error(ERR_NONFATAL,
3117 "`%%substr` requires string as second parameter");
3118 free_tlist(tline);
3119 free_tlist(origline);
3120 return DIRECTIVE_FOUND;
3121 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003122
H. Peter Anvine2c80182005-01-15 22:15:51 +00003123 tt = t->next;
3124 tptr = &tt;
3125 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003126 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3127 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003128 if (!evalresult) {
3129 free_tlist(tline);
3130 free_tlist(origline);
3131 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003132 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003133 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3134 free_tlist(tline);
3135 free_tlist(origline);
3136 return DIRECTIVE_FOUND;
3137 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003138 a1 = evalresult->value-1;
3139
3140 while (tok_type_(tt, TOK_WHITESPACE))
3141 tt = tt->next;
3142 if (!tt) {
3143 a2 = 1; /* Backwards compatibility: one character */
3144 } else {
3145 tokval.t_type = TOKEN_INVALID;
3146 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3147 pass, error, NULL);
3148 if (!evalresult) {
3149 free_tlist(tline);
3150 free_tlist(origline);
3151 return DIRECTIVE_FOUND;
3152 } else if (!is_simple(evalresult)) {
3153 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3154 free_tlist(tline);
3155 free_tlist(origline);
3156 return DIRECTIVE_FOUND;
3157 }
3158 a2 = evalresult->value;
3159 }
3160
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003161 len = nasm_unquote(t->text, NULL);
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003162 if (a2 < 0)
3163 a2 = a2+1+len-a1;
3164 if (a1+a2 > (int64_t)len)
3165 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003166
H. Peter Anvine2c80182005-01-15 22:15:51 +00003167 macro_start = nasm_malloc(sizeof(*macro_start));
3168 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003169 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003170 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003171 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003172
H. Peter Anvine2c80182005-01-15 22:15:51 +00003173 /*
3174 * We now have a macro name, an implicit parameter count of
3175 * zero, and a numeric token to use as an expansion. Create
3176 * and store an SMacro.
3177 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003178 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003179 free_tlist(tline);
3180 free_tlist(origline);
3181 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003182 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003183
H. Peter Anvine2c80182005-01-15 22:15:51 +00003184 case PP_ASSIGN:
3185 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003186 casesense = (i == PP_ASSIGN);
3187
H. Peter Anvine2c80182005-01-15 22:15:51 +00003188 tline = tline->next;
3189 skip_white_(tline);
3190 tline = expand_id(tline);
3191 if (!tline || (tline->type != TOK_ID &&
3192 (tline->type != TOK_PREPROC_ID ||
3193 tline->text[1] != '$'))) {
3194 error(ERR_NONFATAL,
3195 "`%%%sassign' expects a macro identifier",
3196 (i == PP_IASSIGN ? "i" : ""));
3197 free_tlist(origline);
3198 return DIRECTIVE_FOUND;
3199 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003200 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003201
H. Peter Anvine2c80182005-01-15 22:15:51 +00003202 mname = tline->text;
3203 last = tline;
3204 tline = expand_smacro(tline->next);
3205 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003206
H. Peter Anvine2c80182005-01-15 22:15:51 +00003207 t = tline;
3208 tptr = &t;
3209 tokval.t_type = TOKEN_INVALID;
3210 evalresult =
3211 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3212 free_tlist(tline);
3213 if (!evalresult) {
3214 free_tlist(origline);
3215 return DIRECTIVE_FOUND;
3216 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003217
H. Peter Anvine2c80182005-01-15 22:15:51 +00003218 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003219 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003220 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003221
H. Peter Anvine2c80182005-01-15 22:15:51 +00003222 if (!is_simple(evalresult)) {
3223 error(ERR_NONFATAL,
3224 "non-constant value given to `%%%sassign'",
3225 (i == PP_IASSIGN ? "i" : ""));
3226 free_tlist(origline);
3227 return DIRECTIVE_FOUND;
3228 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003229
H. Peter Anvine2c80182005-01-15 22:15:51 +00003230 macro_start = nasm_malloc(sizeof(*macro_start));
3231 macro_start->next = NULL;
3232 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003233 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003234
H. Peter Anvine2c80182005-01-15 22:15:51 +00003235 /*
3236 * We now have a macro name, an implicit parameter count of
3237 * zero, and a numeric token to use as an expansion. Create
3238 * and store an SMacro.
3239 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003240 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003241 free_tlist(origline);
3242 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003243
H. Peter Anvine2c80182005-01-15 22:15:51 +00003244 case PP_LINE:
3245 /*
3246 * Syntax is `%line nnn[+mmm] [filename]'
3247 */
3248 tline = tline->next;
3249 skip_white_(tline);
3250 if (!tok_type_(tline, TOK_NUMBER)) {
3251 error(ERR_NONFATAL, "`%%line' expects line number");
3252 free_tlist(origline);
3253 return DIRECTIVE_FOUND;
3254 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003255 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003256 m = 1;
3257 tline = tline->next;
3258 if (tok_is_(tline, "+")) {
3259 tline = tline->next;
3260 if (!tok_type_(tline, TOK_NUMBER)) {
3261 error(ERR_NONFATAL, "`%%line' expects line increment");
3262 free_tlist(origline);
3263 return DIRECTIVE_FOUND;
3264 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003265 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003266 tline = tline->next;
3267 }
3268 skip_white_(tline);
3269 src_set_linnum(k);
3270 istk->lineinc = m;
3271 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003272 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003273 }
3274 free_tlist(origline);
3275 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003276
H. Peter Anvine2c80182005-01-15 22:15:51 +00003277 default:
3278 error(ERR_FATAL,
3279 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003280 pp_directives[i]);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003281 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003282 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003283}
3284
3285/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003286 * Ensure that a macro parameter contains a condition code and
3287 * nothing else. Return the condition code index if so, or -1
3288 * otherwise.
3289 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003290static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003291{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003292 Token *tt;
3293 int i, j, k, m;
3294
H. Peter Anvin25a99342007-09-22 17:45:45 -07003295 if (!t)
3296 return -1; /* Probably a %+ without a space */
3297
H. Peter Anvineba20a72002-04-30 20:53:55 +00003298 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003299 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003300 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003301 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003302 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003303 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003304 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003305
3306 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00003307 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003308 while (j - i > 1) {
3309 k = (j + i) / 2;
3310 m = nasm_stricmp(t->text, conditions[k]);
3311 if (m == 0) {
3312 i = k;
3313 j = -2;
3314 break;
3315 } else if (m < 0) {
3316 j = k;
3317 } else
3318 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003319 }
3320 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003321 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003322 return i;
3323}
3324
3325/*
3326 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3327 * %-n) and MMacro-local identifiers (%%foo).
3328 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003329static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003330{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003331 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003332
3333 tail = &thead;
3334 thead = NULL;
3335
H. Peter Anvine2c80182005-01-15 22:15:51 +00003336 while (tline) {
3337 if (tline->type == TOK_PREPROC_ID &&
3338 (((tline->text[1] == '+' || tline->text[1] == '-')
3339 && tline->text[2]) || tline->text[1] == '%'
3340 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003341 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003342 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003343 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003344 unsigned int n;
3345 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003346 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003347
H. Peter Anvine2c80182005-01-15 22:15:51 +00003348 t = tline;
3349 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003350
H. Peter Anvine2c80182005-01-15 22:15:51 +00003351 mac = istk->mstk;
3352 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3353 mac = mac->next_active;
3354 if (!mac)
3355 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3356 else
3357 switch (t->text[1]) {
3358 /*
3359 * We have to make a substitution of one of the
3360 * forms %1, %-1, %+1, %%foo, %0.
3361 */
3362 case '0':
3363 type = TOK_NUMBER;
3364 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3365 text = nasm_strdup(tmpbuf);
3366 break;
3367 case '%':
3368 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003369 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00003370 mac->unique);
3371 text = nasm_strcat(tmpbuf, t->text + 2);
3372 break;
3373 case '-':
3374 n = atoi(t->text + 2) - 1;
3375 if (n >= mac->nparam)
3376 tt = NULL;
3377 else {
3378 if (mac->nparam > 1)
3379 n = (n + mac->rotate) % mac->nparam;
3380 tt = mac->params[n];
3381 }
3382 cc = find_cc(tt);
3383 if (cc == -1) {
3384 error(ERR_NONFATAL,
3385 "macro parameter %d is not a condition code",
3386 n + 1);
3387 text = NULL;
3388 } else {
3389 type = TOK_ID;
3390 if (inverse_ccs[cc] == -1) {
3391 error(ERR_NONFATAL,
3392 "condition code `%s' is not invertible",
3393 conditions[cc]);
3394 text = NULL;
3395 } else
3396 text =
3397 nasm_strdup(conditions[inverse_ccs[cc]]);
3398 }
3399 break;
3400 case '+':
3401 n = atoi(t->text + 2) - 1;
3402 if (n >= mac->nparam)
3403 tt = NULL;
3404 else {
3405 if (mac->nparam > 1)
3406 n = (n + mac->rotate) % mac->nparam;
3407 tt = mac->params[n];
3408 }
3409 cc = find_cc(tt);
3410 if (cc == -1) {
3411 error(ERR_NONFATAL,
3412 "macro parameter %d is not a condition code",
3413 n + 1);
3414 text = NULL;
3415 } else {
3416 type = TOK_ID;
3417 text = nasm_strdup(conditions[cc]);
3418 }
3419 break;
3420 default:
3421 n = atoi(t->text + 1) - 1;
3422 if (n >= mac->nparam)
3423 tt = NULL;
3424 else {
3425 if (mac->nparam > 1)
3426 n = (n + mac->rotate) % mac->nparam;
3427 tt = mac->params[n];
3428 }
3429 if (tt) {
3430 for (i = 0; i < mac->paramlen[n]; i++) {
3431 *tail = new_Token(NULL, tt->type, tt->text, 0);
3432 tail = &(*tail)->next;
3433 tt = tt->next;
3434 }
3435 }
3436 text = NULL; /* we've done it here */
3437 break;
3438 }
3439 if (!text) {
3440 delete_Token(t);
3441 } else {
3442 *tail = t;
3443 tail = &t->next;
3444 t->type = type;
3445 nasm_free(t->text);
3446 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003447 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003448 }
3449 continue;
3450 } else {
3451 t = *tail = tline;
3452 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003453 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003454 tail = &t->next;
3455 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003456 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003457 *tail = NULL;
3458 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003459 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003460 switch (t->type) {
3461 case TOK_WHITESPACE:
3462 if (tt->type == TOK_WHITESPACE) {
3463 t->next = delete_Token(tt);
3464 }
3465 break;
3466 case TOK_ID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003467 case TOK_NUMBER:
H. Peter Anvin992fe752008-10-19 15:45:05 -07003468 if (tt->type == t->type || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003469 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003470 nasm_free(t->text);
3471 t->text = tmp;
3472 t->next = delete_Token(tt);
3473 }
3474 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003475 default:
3476 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003477 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003478
H. Peter Anvin76690a12002-04-30 20:52:49 +00003479 return thead;
3480}
3481
3482/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003483 * Expand all single-line macro calls made in the given line.
3484 * Return the expanded version of the line. The original is deemed
3485 * to be destroyed in the process. (In reality we'll just move
3486 * Tokens from input to output a lot of the time, rather than
3487 * actually bothering to destroy and replicate.)
3488 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003489#define DEADMAN_LIMIT (1 << 20)
3490
H. Peter Anvine2c80182005-01-15 22:15:51 +00003491static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003492{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003493 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003494 struct hash_table *smtbl;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003495 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003496 Token **params;
3497 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003498 unsigned int nparam, sparam;
3499 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003500 Token *org_tline = tline;
3501 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003502 char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003503 int deadman = DEADMAN_LIMIT;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003504
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003505 /*
3506 * Trick: we should avoid changing the start token pointer since it can
3507 * be contained in "next" field of other token. Because of this
3508 * we allocate a copy of first token and work with it; at the end of
3509 * routine we copy it back
3510 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003511 if (org_tline) {
3512 tline =
3513 new_Token(org_tline->next, org_tline->type, org_tline->text,
3514 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003515 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003516 nasm_free(org_tline->text);
3517 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003518 }
3519
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003520again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003521 tail = &thead;
3522 thead = NULL;
3523
H. Peter Anvine2c80182005-01-15 22:15:51 +00003524 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003525 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003526 error(ERR_NONFATAL, "interminable macro recursion");
3527 break;
3528 }
3529
H. Peter Anvine2c80182005-01-15 22:15:51 +00003530 if ((mname = tline->text)) {
3531 /* if this token is a local macro, look in local context */
H. Peter Anvinb037a672008-05-29 19:08:08 -07003532 ctx = NULL;
3533 smtbl = &smacros;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003534 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003535 ctx = get_ctx(mname, true);
H. Peter Anvinb037a672008-05-29 19:08:08 -07003536 if (ctx)
3537 smtbl = &ctx->localmac;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003538 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07003539 head = (SMacro *) hash_findix(smtbl, mname);
H. Peter Anvin072771e2008-05-22 13:17:51 -07003540
H. Peter Anvine2c80182005-01-15 22:15:51 +00003541 /*
3542 * We've hit an identifier. As in is_mmacro below, we first
3543 * check whether the identifier is a single-line macro at
3544 * all, then think about checking for parameters if
3545 * necessary.
3546 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003547 for (m = head; m; m = m->next)
3548 if (!mstrcmp(m->name, mname, m->casesense))
3549 break;
3550 if (m) {
3551 mstart = tline;
3552 params = NULL;
3553 paramsize = NULL;
3554 if (m->nparam == 0) {
3555 /*
3556 * Simple case: the macro is parameterless. Discard the
3557 * one token that the macro call took, and push the
3558 * expansion back on the to-do stack.
3559 */
3560 if (!m->expansion) {
3561 if (!strcmp("__FILE__", m->name)) {
3562 int32_t num = 0;
H. Peter Anvin932de6c2008-07-31 18:46:11 -07003563 char *file = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003564 src_get(&num, &file);
3565 tline->text = nasm_quote(file, strlen(file));
H. Peter Anvin97a23472007-09-16 17:57:25 -07003566 tline->type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003567 nasm_free(file);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003568 continue;
3569 }
3570 if (!strcmp("__LINE__", m->name)) {
3571 nasm_free(tline->text);
3572 make_tok_num(tline, src_get_linnum());
3573 continue;
3574 }
3575 if (!strcmp("__BITS__", m->name)) {
3576 nasm_free(tline->text);
3577 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003578 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003579 }
3580 tline = delete_Token(tline);
3581 continue;
3582 }
3583 } else {
3584 /*
3585 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003586 * exists and takes parameters. We must find the
3587 * parameters in the call, count them, find the SMacro
3588 * that corresponds to that form of the macro call, and
3589 * substitute for the parameters when we expand. What a
3590 * pain.
3591 */
3592 /*tline = tline->next;
3593 skip_white_(tline); */
3594 do {
3595 t = tline->next;
3596 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003597 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003598 t->text = NULL;
3599 t = tline->next = delete_Token(t);
3600 }
3601 tline = t;
3602 } while (tok_type_(tline, TOK_WHITESPACE));
3603 if (!tok_is_(tline, "(")) {
3604 /*
3605 * This macro wasn't called with parameters: ignore
3606 * the call. (Behaviour borrowed from gnu cpp.)
3607 */
3608 tline = mstart;
3609 m = NULL;
3610 } else {
3611 int paren = 0;
3612 int white = 0;
3613 brackets = 0;
3614 nparam = 0;
3615 sparam = PARAM_DELTA;
3616 params = nasm_malloc(sparam * sizeof(Token *));
3617 params[0] = tline->next;
3618 paramsize = nasm_malloc(sparam * sizeof(int));
3619 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003620 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003621 /*
3622 * For some unusual expansions
3623 * which concatenates function call
3624 */
3625 t = tline->next;
3626 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003627 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003628 t->text = NULL;
3629 t = tline->next = delete_Token(t);
3630 }
3631 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003632
H. Peter Anvine2c80182005-01-15 22:15:51 +00003633 if (!tline) {
3634 error(ERR_NONFATAL,
3635 "macro call expects terminating `)'");
3636 break;
3637 }
3638 if (tline->type == TOK_WHITESPACE
3639 && brackets <= 0) {
3640 if (paramsize[nparam])
3641 white++;
3642 else
3643 params[nparam] = tline->next;
3644 continue; /* parameter loop */
3645 }
3646 if (tline->type == TOK_OTHER
3647 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003648 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003649 if (ch == ',' && !paren && brackets <= 0) {
3650 if (++nparam >= sparam) {
3651 sparam += PARAM_DELTA;
3652 params = nasm_realloc(params,
3653 sparam *
3654 sizeof(Token
3655 *));
3656 paramsize =
3657 nasm_realloc(paramsize,
3658 sparam *
3659 sizeof(int));
3660 }
3661 params[nparam] = tline->next;
3662 paramsize[nparam] = 0;
3663 white = 0;
3664 continue; /* parameter loop */
3665 }
3666 if (ch == '{' &&
3667 (brackets > 0 || (brackets == 0 &&
3668 !paramsize[nparam])))
3669 {
3670 if (!(brackets++)) {
3671 params[nparam] = tline->next;
3672 continue; /* parameter loop */
3673 }
3674 }
3675 if (ch == '}' && brackets > 0)
3676 if (--brackets == 0) {
3677 brackets = -1;
3678 continue; /* parameter loop */
3679 }
3680 if (ch == '(' && !brackets)
3681 paren++;
3682 if (ch == ')' && brackets <= 0)
3683 if (--paren < 0)
3684 break;
3685 }
3686 if (brackets < 0) {
3687 brackets = 0;
3688 error(ERR_NONFATAL, "braces do not "
3689 "enclose all of macro parameter");
3690 }
3691 paramsize[nparam] += white + 1;
3692 white = 0;
3693 } /* parameter loop */
3694 nparam++;
3695 while (m && (m->nparam != nparam ||
3696 mstrcmp(m->name, mname,
3697 m->casesense)))
3698 m = m->next;
3699 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003700 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003701 "macro `%s' exists, "
3702 "but not taking %d parameters",
3703 mstart->text, nparam);
3704 }
3705 }
3706 if (m && m->in_progress)
3707 m = NULL;
3708 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003709 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003710 * Design question: should we handle !tline, which
3711 * indicates missing ')' here, or expand those
3712 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003713 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003714 */
3715 nasm_free(params);
3716 nasm_free(paramsize);
3717 tline = mstart;
3718 } else {
3719 /*
3720 * Expand the macro: we are placed on the last token of the
3721 * call, so that we can easily split the call from the
3722 * following tokens. We also start by pushing an SMAC_END
3723 * token for the cycle removal.
3724 */
3725 t = tline;
3726 if (t) {
3727 tline = t->next;
3728 t->next = NULL;
3729 }
3730 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003731 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003732 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003733 tline = tt;
3734 for (t = m->expansion; t; t = t->next) {
3735 if (t->type >= TOK_SMAC_PARAM) {
3736 Token *pcopy = tline, **ptail = &pcopy;
3737 Token *ttt, *pt;
3738 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003739
H. Peter Anvine2c80182005-01-15 22:15:51 +00003740 ttt = params[t->type - TOK_SMAC_PARAM];
3741 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3742 --i >= 0;) {
3743 pt = *ptail =
3744 new_Token(tline, ttt->type, ttt->text,
3745 0);
3746 ptail = &pt->next;
3747 ttt = ttt->next;
3748 }
3749 tline = pcopy;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -07003750 } else if (t->type == TOK_PREPROC_Q) {
3751 tt = new_Token(tline, TOK_ID, mname, 0);
3752 tline = tt;
3753 } else if (t->type == TOK_PREPROC_QQ) {
3754 tt = new_Token(tline, TOK_ID, m->name, 0);
3755 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003756 } else {
3757 tt = new_Token(tline, t->type, t->text, 0);
3758 tline = tt;
3759 }
3760 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003761
H. Peter Anvine2c80182005-01-15 22:15:51 +00003762 /*
3763 * Having done that, get rid of the macro call, and clean
3764 * up the parameters.
3765 */
3766 nasm_free(params);
3767 nasm_free(paramsize);
3768 free_tlist(mstart);
3769 continue; /* main token loop */
3770 }
3771 }
3772 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003773
H. Peter Anvine2c80182005-01-15 22:15:51 +00003774 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003775 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003776 tline = delete_Token(tline);
3777 } else {
3778 t = *tail = tline;
3779 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003780 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003781 t->next = NULL;
3782 tail = &t->next;
3783 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003784 }
3785
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003786 /*
3787 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003788 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003789 * TOK_IDs should be concatenated.
3790 * Also we look for %+ tokens and concatenate the tokens before and after
3791 * them (without white spaces in between).
3792 */
3793 t = thead;
3794 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003795 while (t) {
3796 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3797 t = t->next;
3798 if (!t || !t->next)
3799 break;
3800 if (t->next->type == TOK_ID ||
3801 t->next->type == TOK_PREPROC_ID ||
3802 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003803 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003804 nasm_free(t->text);
3805 t->next = delete_Token(t->next);
3806 t->text = p;
3807 rescan = 1;
3808 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3809 t->next->next->type == TOK_PREPROC_ID &&
3810 strcmp(t->next->next->text, "%+") == 0) {
3811 /* free the next whitespace, the %+ token and next whitespace */
3812 int i;
3813 for (i = 1; i <= 3; i++) {
3814 if (!t->next
3815 || (i != 2 && t->next->type != TOK_WHITESPACE))
3816 break;
3817 t->next = delete_Token(t->next);
3818 } /* endfor */
3819 } else
3820 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003821 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003822 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003823 if (rescan) {
3824 tline = thead;
3825 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003826 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003827
H. Peter Anvine2c80182005-01-15 22:15:51 +00003828 if (org_tline) {
3829 if (thead) {
3830 *org_tline = *thead;
3831 /* since we just gave text to org_line, don't free it */
3832 thead->text = NULL;
3833 delete_Token(thead);
3834 } else {
3835 /* the expression expanded to empty line;
3836 we can't return NULL for some reasons
3837 we just set the line to a single WHITESPACE token. */
3838 memset(org_tline, 0, sizeof(*org_tline));
3839 org_tline->text = NULL;
3840 org_tline->type = TOK_WHITESPACE;
3841 }
3842 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003843 }
3844
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003845 return thead;
3846}
3847
3848/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003849 * Similar to expand_smacro but used exclusively with macro identifiers
3850 * right before they are fetched in. The reason is that there can be
3851 * identifiers consisting of several subparts. We consider that if there
3852 * are more than one element forming the name, user wants a expansion,
3853 * otherwise it will be left as-is. Example:
3854 *
3855 * %define %$abc cde
3856 *
3857 * the identifier %$abc will be left as-is so that the handler for %define
3858 * will suck it and define the corresponding value. Other case:
3859 *
3860 * %define _%$abc cde
3861 *
3862 * In this case user wants name to be expanded *before* %define starts
3863 * working, so we'll expand %$abc into something (if it has a value;
3864 * otherwise it will be left as-is) then concatenate all successive
3865 * PP_IDs into one.
3866 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003867static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003868{
3869 Token *cur, *oldnext = NULL;
3870
H. Peter Anvin734b1882002-04-30 21:01:08 +00003871 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003872 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003873
3874 cur = tline;
3875 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003876 (cur->next->type == TOK_ID ||
3877 cur->next->type == TOK_PREPROC_ID
3878 || cur->next->type == TOK_NUMBER))
3879 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003880
3881 /* If identifier consists of just one token, don't expand */
3882 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003883 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003884
H. Peter Anvine2c80182005-01-15 22:15:51 +00003885 if (cur) {
3886 oldnext = cur->next; /* Detach the tail past identifier */
3887 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003888 }
3889
H. Peter Anvin734b1882002-04-30 21:01:08 +00003890 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003891
H. Peter Anvine2c80182005-01-15 22:15:51 +00003892 if (cur) {
3893 /* expand_smacro possibly changhed tline; re-scan for EOL */
3894 cur = tline;
3895 while (cur && cur->next)
3896 cur = cur->next;
3897 if (cur)
3898 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003899 }
3900
3901 return tline;
3902}
3903
3904/*
H. Peter Anvinc0ab1cd2008-10-19 16:24:53 -07003905 * Expand indirect tokens, %[...]. Just like expand_smacro(),
3906 * the input is considered destroyed.
H. Peter Anvin49b33f32008-10-19 22:22:05 -07003907 *
3908 * XXX: fix duplicated code in this function and in expand_mmac_params()
H. Peter Anvin992fe752008-10-19 15:45:05 -07003909 */
3910static Token *expand_indirect(Token * tline, int level)
3911{
3912 const int max_indirect_level = 1000;
3913 Token *t, *thead, **tp;
3914 Token *it;
3915 bool skip;
3916
3917 if (level >= max_indirect_level) {
3918 error(ERR_NONFATAL, "interminable indirect expansion");
3919 } else {
3920 thead = NULL;
H. Peter Anvinc0ab1cd2008-10-19 16:24:53 -07003921 tp = &tline;
3922 while ((t = *tp)) {
3923 if (t->type != TOK_INDIRECT) {
3924 thead = t;
3925 tp = &t->next;
3926 } else {
3927 it = tokenize(t->text);
3928 it = expand_indirect(it, level+1);
3929 it = expand_smacro(it);
3930 while (it) {
3931 skip = false;
3932 switch (thead ? thead->type : TOK_NONE) {
3933 case TOK_WHITESPACE:
3934 skip = (it->type == TOK_WHITESPACE);
3935 break;
3936 case TOK_ID:
3937 case TOK_NUMBER:
3938 if (it->type == thead->type || it->type == TOK_NUMBER) {
3939 char *tmp = nasm_strcat(thead->text, it->text);
3940 nasm_free(thead->text);
3941 thead->text = tmp;
3942 skip = true;
3943 }
3944 break;
3945 default:
3946 break;
H. Peter Anvin992fe752008-10-19 15:45:05 -07003947 }
H. Peter Anvinc0ab1cd2008-10-19 16:24:53 -07003948 if (skip) {
3949 it = delete_Token(it);
3950 } else {
3951 *tp = thead = it;
3952 tp = &it->next;
3953 it = it->next;
3954 }
H. Peter Anvin992fe752008-10-19 15:45:05 -07003955 }
H. Peter Anvin49b33f32008-10-19 22:22:05 -07003956
3957 skip = false;
3958 it = t->next;
3959 if (it) {
3960 switch (thead ? thead->type : TOK_NONE) {
3961 case TOK_WHITESPACE:
3962 skip = (it->type == TOK_WHITESPACE);
3963 break;
3964 case TOK_ID:
3965 case TOK_NUMBER:
3966 if (it->type == thead->type || it->type == TOK_NUMBER) {
3967 char *tmp = nasm_strcat(thead->text, it->text);
3968 nasm_free(thead->text);
3969 thead->text = tmp;
3970 skip = true;
3971 }
3972 break;
3973 default:
3974 break;
3975 }
3976 }
3977 if (skip) {
3978 *tp = thead = it->next;
3979 t = delete_Token(t);
3980 } else {
3981 *tp = thead = it;
3982 }
H. Peter Anvinc0ab1cd2008-10-19 16:24:53 -07003983 t = delete_Token(t);
H. Peter Anvin992fe752008-10-19 15:45:05 -07003984 }
H. Peter Anvin992fe752008-10-19 15:45:05 -07003985 }
3986 }
3987 return tline;
3988}
3989
3990/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003991 * Determine whether the given line constitutes a multi-line macro
3992 * call, and return the MMacro structure called if so. Doesn't have
3993 * to check for an initial label - that's taken care of in
3994 * expand_mmacro - but must check numbers of parameters. Guaranteed
3995 * to be called with tline->type == TOK_ID, so the putative macro
3996 * name is easy to find.
3997 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003998static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003999{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004000 MMacro *head, *m;
4001 Token **params;
4002 int nparam;
4003
H. Peter Anvin166c2472008-05-28 12:28:58 -07004004 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004005
4006 /*
4007 * Efficiency: first we see if any macro exists with the given
4008 * name. If not, we can return NULL immediately. _Then_ we
4009 * count the parameters, and then we look further along the
4010 * list if necessary to find the proper MMacro.
4011 */
4012 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004013 if (!mstrcmp(m->name, tline->text, m->casesense))
4014 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004015 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004016 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004017
4018 /*
4019 * OK, we have a potential macro. Count and demarcate the
4020 * parameters.
4021 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004022 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004023
4024 /*
4025 * So we know how many parameters we've got. Find the MMacro
4026 * structure that handles this number.
4027 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004028 while (m) {
4029 if (m->nparam_min <= nparam
4030 && (m->plus || nparam <= m->nparam_max)) {
4031 /*
4032 * This one is right. Just check if cycle removal
4033 * prohibits us using it before we actually celebrate...
4034 */
4035 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00004036#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00004037 error(ERR_NONFATAL,
4038 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004039#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00004040 nasm_free(params);
4041 return NULL;
4042 }
4043 /*
4044 * It's right, and we can use it. Add its default
4045 * parameters to the end of our list if necessary.
4046 */
4047 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4048 params =
4049 nasm_realloc(params,
4050 ((m->nparam_min + m->ndefs +
4051 1) * sizeof(*params)));
4052 while (nparam < m->nparam_min + m->ndefs) {
4053 params[nparam] = m->defaults[nparam - m->nparam_min];
4054 nparam++;
4055 }
4056 }
4057 /*
4058 * If we've gone over the maximum parameter count (and
4059 * we're in Plus mode), ignore parameters beyond
4060 * nparam_max.
4061 */
4062 if (m->plus && nparam > m->nparam_max)
4063 nparam = m->nparam_max;
4064 /*
4065 * Then terminate the parameter list, and leave.
4066 */
4067 if (!params) { /* need this special case */
4068 params = nasm_malloc(sizeof(*params));
4069 nparam = 0;
4070 }
4071 params[nparam] = NULL;
4072 *params_array = params;
4073 return m;
4074 }
4075 /*
4076 * This one wasn't right: look for the next one with the
4077 * same name.
4078 */
4079 for (m = m->next; m; m = m->next)
4080 if (!mstrcmp(m->name, tline->text, m->casesense))
4081 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004082 }
4083
4084 /*
4085 * After all that, we didn't find one with the right number of
4086 * parameters. Issue a warning, and fail to expand the macro.
4087 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004088 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004089 "macro `%s' exists, but not taking %d parameters",
4090 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004091 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004092 return NULL;
4093}
4094
4095/*
4096 * Expand the multi-line macro call made by the given line, if
4097 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00004098 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004099 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004100static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004101{
4102 Token *startline = tline;
4103 Token *label = NULL;
4104 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004105 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004106 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004107 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004108 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004109 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004110
4111 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004112 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004113 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004114 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004115 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004116 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004117 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004118 if (m) {
4119 mname = t->text;
4120 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004121 Token *last;
4122 /*
4123 * We have an id which isn't a macro call. We'll assume
4124 * it might be a label; we'll also check to see if a
4125 * colon follows it. Then, if there's another id after
4126 * that lot, we'll check it again for macro-hood.
4127 */
4128 label = last = t;
4129 t = t->next;
4130 if (tok_type_(t, TOK_WHITESPACE))
4131 last = t, t = t->next;
4132 if (tok_is_(t, ":")) {
4133 dont_prepend = 1;
4134 last = t, t = t->next;
4135 if (tok_type_(t, TOK_WHITESPACE))
4136 last = t, t = t->next;
4137 }
4138 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
4139 return 0;
4140 last->next = NULL;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004141 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004142 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004143 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004144
4145 /*
4146 * Fix up the parameters: this involves stripping leading and
4147 * trailing whitespace, then stripping braces if they are
4148 * present.
4149 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004150 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004151 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004152
H. Peter Anvine2c80182005-01-15 22:15:51 +00004153 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004154 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004155 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004156
H. Peter Anvine2c80182005-01-15 22:15:51 +00004157 t = params[i];
4158 skip_white_(t);
4159 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004160 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004161 params[i] = t;
4162 paramlen[i] = 0;
4163 while (t) {
4164 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4165 break; /* ... because we have hit a comma */
4166 if (comma && t->type == TOK_WHITESPACE
4167 && tok_is_(t->next, ","))
4168 break; /* ... or a space then a comma */
4169 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4170 break; /* ... or a brace */
4171 t = t->next;
4172 paramlen[i]++;
4173 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004174 }
4175
4176 /*
4177 * OK, we have a MMacro structure together with a set of
4178 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004179 * copies of each Line on to istk->expansion. Substitution of
4180 * parameter tokens and macro-local tokens doesn't get done
4181 * until the single-line macro substitution process; this is
4182 * because delaying them allows us to change the semantics
4183 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004184 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004185 * First, push an end marker on to istk->expansion, mark this
4186 * macro as in progress, and set up its invocation-specific
4187 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004188 */
4189 ll = nasm_malloc(sizeof(Line));
4190 ll->next = istk->expansion;
4191 ll->finishes = m;
4192 ll->first = NULL;
4193 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004194
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004195 m->in_progress = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004196 m->params = params;
4197 m->iline = tline;
4198 m->nparam = nparam;
4199 m->rotate = 0;
4200 m->paramlen = paramlen;
4201 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004202 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004203
4204 m->next_active = istk->mstk;
4205 istk->mstk = m;
4206
H. Peter Anvine2c80182005-01-15 22:15:51 +00004207 for (l = m->expansion; l; l = l->next) {
4208 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004209
H. Peter Anvine2c80182005-01-15 22:15:51 +00004210 ll = nasm_malloc(sizeof(Line));
4211 ll->finishes = NULL;
4212 ll->next = istk->expansion;
4213 istk->expansion = ll;
4214 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004215
H. Peter Anvine2c80182005-01-15 22:15:51 +00004216 for (t = l->first; t; t = t->next) {
4217 Token *x = t;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004218 switch (t->type) {
4219 case TOK_PREPROC_Q:
H. Peter Anvinc751e862008-06-09 10:18:45 -07004220 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004221 break;
4222 case TOK_PREPROC_QQ:
4223 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4224 break;
4225 case TOK_PREPROC_ID:
4226 if (t->text[1] == '0' && t->text[2] == '0') {
4227 dont_prepend = -1;
4228 x = label;
4229 if (!x)
4230 continue;
4231 }
4232 /* fall through */
4233 default:
4234 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4235 break;
H. Peter Anvin166c2472008-05-28 12:28:58 -07004236 }
H. Peter Anvince2233b2008-05-25 21:57:00 -07004237 tail = &tt->next;
4238 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004239 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004240 }
4241
4242 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004243 * If we had a label, push it on as the first line of
4244 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004245 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004246 if (label) {
4247 if (dont_prepend < 0)
4248 free_tlist(startline);
4249 else {
4250 ll = nasm_malloc(sizeof(Line));
4251 ll->finishes = NULL;
4252 ll->next = istk->expansion;
4253 istk->expansion = ll;
4254 ll->first = startline;
4255 if (!dont_prepend) {
4256 while (label->next)
4257 label = label->next;
4258 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4259 }
4260 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004261 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004262
H. Peter Anvin734b1882002-04-30 21:01:08 +00004263 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004264
H. Peter Anvineba20a72002-04-30 20:53:55 +00004265 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004266}
4267
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004268/* The function that actually does the error reporting */
4269static void verror(int severity, const char *fmt, va_list arg)
4270{
4271 char buff[1024];
4272
4273 vsnprintf(buff, sizeof(buff), fmt, arg);
4274
4275 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004276 _error(severity, "(%s:%d) %s", istk->mstk->name,
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004277 istk->mstk->lineno, buff);
4278 else
H. Peter Anvin917a3492008-09-24 09:14:49 -07004279 _error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004280}
4281
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004282/*
4283 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004284 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004285 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004286static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004287{
4288 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004289
4290 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004291 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004292 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004293
H. Peter Anvin734b1882002-04-30 21:01:08 +00004294 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004295 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004296 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004297}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004298
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004299/*
4300 * Because %else etc are evaluated in the state context
4301 * of the previous branch, errors might get lost with error():
4302 * %if 0 ... %else trailing garbage ... %endif
4303 * So %else etc should report errors with this function.
4304 */
4305static void error_precond(int severity, const char *fmt, ...)
4306{
4307 va_list arg;
4308
4309 /* Only ignore the error if it's really in a dead branch */
4310 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4311 return;
4312
4313 va_start(arg, fmt);
4314 verror(severity, fmt, arg);
4315 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004316}
4317
H. Peter Anvin734b1882002-04-30 21:01:08 +00004318static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004319pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004320 ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004321{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004322 Token *t;
4323
H. Peter Anvin99941bf2002-05-14 17:44:03 +00004324 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004325 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004326 istk = nasm_malloc(sizeof(Include));
4327 istk->next = NULL;
4328 istk->conds = NULL;
4329 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004330 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004331 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004332 istk->fname = NULL;
4333 src_set_fname(nasm_strdup(file));
4334 src_set_linnum(0);
4335 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004336 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004337 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004338 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004339 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004340 nested_mac_count = 0;
4341 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004342 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004343 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004344 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004345 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004346 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004347 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004348 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004349 any_extrastdmac = extrastdmac && *extrastdmac;
4350 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004351 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004352 evaluate = eval;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004353
4354 /*
4355 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4356 * The caller, however, will also pass in 3 for preprocess-only so
4357 * we can set __PASS__ accordingly.
4358 */
4359 pass = apass > 2 ? 2 : apass;
4360
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004361 dephead = deptail = deplist;
4362 if (deplist) {
4363 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4364 sl->next = NULL;
4365 strcpy(sl->str, file);
4366 *deptail = sl;
4367 deptail = &sl->next;
4368 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004369
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004370 /*
4371 * Define the __PASS__ macro. This is defined here unlike
4372 * all the other builtins, because it is special -- it varies between
4373 * passes.
4374 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004375 t = nasm_malloc(sizeof(*t));
4376 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004377 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004378 t->a.mac = NULL;
4379 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004380}
4381
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004382static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004383{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004384 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004385 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004386
H. Peter Anvine2c80182005-01-15 22:15:51 +00004387 while (1) {
4388 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004389 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004390 * buffer or from the input file.
4391 */
4392 tline = NULL;
4393 while (istk->expansion && istk->expansion->finishes) {
4394 Line *l = istk->expansion;
4395 if (!l->finishes->name && l->finishes->in_progress > 1) {
4396 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004397
H. Peter Anvine2c80182005-01-15 22:15:51 +00004398 /*
4399 * This is a macro-end marker for a macro with no
4400 * name, which means it's not really a macro at all
4401 * but a %rep block, and the `in_progress' field is
4402 * more than 1, meaning that we still need to
4403 * repeat. (1 means the natural last repetition; 0
4404 * means termination by %exitrep.) We have
4405 * therefore expanded up to the %endrep, and must
4406 * push the whole block on to the expansion buffer
4407 * again. We don't bother to remove the macro-end
4408 * marker: we'd only have to generate another one
4409 * if we did.
4410 */
4411 l->finishes->in_progress--;
4412 for (l = l->finishes->expansion; l; l = l->next) {
4413 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004414
H. Peter Anvine2c80182005-01-15 22:15:51 +00004415 ll = nasm_malloc(sizeof(Line));
4416 ll->next = istk->expansion;
4417 ll->finishes = NULL;
4418 ll->first = NULL;
4419 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004420
H. Peter Anvine2c80182005-01-15 22:15:51 +00004421 for (t = l->first; t; t = t->next) {
4422 if (t->text || t->type == TOK_WHITESPACE) {
4423 tt = *tail =
4424 new_Token(NULL, t->type, t->text, 0);
4425 tail = &tt->next;
4426 }
4427 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004428
H. Peter Anvine2c80182005-01-15 22:15:51 +00004429 istk->expansion = ll;
4430 }
4431 } else {
4432 /*
4433 * Check whether a `%rep' was started and not ended
4434 * within this macro expansion. This can happen and
4435 * should be detected. It's a fatal error because
4436 * I'm too confused to work out how to recover
4437 * sensibly from it.
4438 */
4439 if (defining) {
4440 if (defining->name)
4441 error(ERR_PANIC,
4442 "defining with name in expansion");
4443 else if (istk->mstk->name)
4444 error(ERR_FATAL,
4445 "`%%rep' without `%%endrep' within"
4446 " expansion of macro `%s'",
4447 istk->mstk->name);
4448 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004449
H. Peter Anvine2c80182005-01-15 22:15:51 +00004450 /*
4451 * FIXME: investigate the relationship at this point between
4452 * istk->mstk and l->finishes
4453 */
4454 {
4455 MMacro *m = istk->mstk;
4456 istk->mstk = m->next_active;
4457 if (m->name) {
4458 /*
4459 * This was a real macro call, not a %rep, and
4460 * therefore the parameter information needs to
4461 * be freed.
4462 */
4463 nasm_free(m->params);
4464 free_tlist(m->iline);
4465 nasm_free(m->paramlen);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004466 l->finishes->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004467 } else
4468 free_mmacro(m);
4469 }
4470 istk->expansion = l->next;
4471 nasm_free(l);
4472 list->downlevel(LIST_MACRO);
4473 }
4474 }
4475 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004476
H. Peter Anvine2c80182005-01-15 22:15:51 +00004477 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004478 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004479 Line *l = istk->expansion;
4480 if (istk->mstk)
4481 istk->mstk->lineno++;
4482 tline = l->first;
4483 istk->expansion = l->next;
4484 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004485 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004486 list->line(LIST_MACRO, p);
4487 nasm_free(p);
4488 break;
4489 }
4490 line = read_line();
4491 if (line) { /* from the current input file */
4492 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004493 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004494 nasm_free(line);
4495 break;
4496 }
4497 /*
4498 * The current file has ended; work down the istk
4499 */
4500 {
4501 Include *i = istk;
4502 fclose(i->fp);
4503 if (i->conds)
4504 error(ERR_FATAL,
4505 "expected `%%endif' before end of file");
4506 /* only set line and file name if there's a next node */
4507 if (i->next) {
4508 src_set_linnum(i->lineno);
4509 nasm_free(src_set_fname(i->fname));
H. Peter Anvin86877b22008-06-20 15:55:45 -07004510 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004511 istk = i->next;
4512 list->downlevel(LIST_INCLUDE);
4513 nasm_free(i);
4514 if (!istk)
4515 return NULL;
Victor van den Elzen4c9d6222008-10-01 13:08:50 +02004516 if (istk->expansion && istk->expansion->finishes)
4517 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004518 }
4519 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004520
H. Peter Anvine2c80182005-01-15 22:15:51 +00004521 /*
4522 * We must expand MMacro parameters and MMacro-local labels
4523 * _before_ we plunge into directive processing, to cope
4524 * with things like `%define something %1' such as STRUC
4525 * uses. Unless we're _defining_ a MMacro, in which case
4526 * those tokens should be left alone to go into the
4527 * definition; and unless we're in a non-emitting
4528 * condition, in which case we don't want to meddle with
4529 * anything.
4530 */
Charles Crayned4200be2008-07-12 16:42:33 -07004531 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin992fe752008-10-19 15:45:05 -07004532 && !(istk->mstk && !istk->mstk->in_progress)) {
4533 tline = expand_indirect(tline,0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004534 tline = expand_mmac_params(tline);
H. Peter Anvin992fe752008-10-19 15:45:05 -07004535 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004536
H. Peter Anvine2c80182005-01-15 22:15:51 +00004537 /*
4538 * Check the line to see if it's a preprocessor directive.
4539 */
4540 if (do_directive(tline) == DIRECTIVE_FOUND) {
4541 continue;
4542 } else if (defining) {
4543 /*
4544 * We're defining a multi-line macro. We emit nothing
4545 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004546 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004547 */
4548 Line *l = nasm_malloc(sizeof(Line));
4549 l->next = defining->expansion;
4550 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004551 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004552 defining->expansion = l;
4553 continue;
4554 } else if (istk->conds && !emitting(istk->conds->state)) {
4555 /*
4556 * We're in a non-emitting branch of a condition block.
4557 * Emit nothing at all, not even a blank line: when we
4558 * emerge from the condition we'll give a line-number
4559 * directive so we keep our place correctly.
4560 */
4561 free_tlist(tline);
4562 continue;
4563 } else if (istk->mstk && !istk->mstk->in_progress) {
4564 /*
4565 * We're in a %rep block which has been terminated, so
4566 * we're walking through to the %endrep without
4567 * emitting anything. Emit nothing at all, not even a
4568 * blank line: when we emerge from the %rep block we'll
4569 * give a line-number directive so we keep our place
4570 * correctly.
4571 */
4572 free_tlist(tline);
4573 continue;
4574 } else {
4575 tline = expand_smacro(tline);
4576 if (!expand_mmacro(tline)) {
4577 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004578 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004579 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004580 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004581 free_tlist(tline);
4582 break;
4583 } else {
4584 continue; /* expand_mmacro calls free_tlist */
4585 }
4586 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004587 }
4588
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004589 return line;
4590}
4591
H. Peter Anvine2c80182005-01-15 22:15:51 +00004592static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004593{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004594 if (defining) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02004595 if(defining->name) {
4596 error(ERR_NONFATAL,
4597 "end of file while still defining macro `%s'",
4598 defining->name);
4599 } else {
4600 error(ERR_NONFATAL, "end of file while still in %%rep");
4601 }
4602
H. Peter Anvine2c80182005-01-15 22:15:51 +00004603 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004604 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004605 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004606 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004607 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004608 while (istk) {
4609 Include *i = istk;
4610 istk = istk->next;
4611 fclose(i->fp);
4612 nasm_free(i->fname);
4613 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004614 }
4615 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004616 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004617 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004618 if (pass == 0) {
H. Peter Anvin86877b22008-06-20 15:55:45 -07004619 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004620 free_llist(predef);
4621 delete_Blocks();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004622 while ((i = ipath)) {
4623 ipath = i->next;
4624 if (i->path)
4625 nasm_free(i->path);
4626 nasm_free(i);
4627 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07004628 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004629}
4630
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004631void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004632{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004633 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004634
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004635 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004636 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004637 i->next = NULL;
4638
H. Peter Anvine2c80182005-01-15 22:15:51 +00004639 if (ipath != NULL) {
4640 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004641 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004642 j = j->next;
4643 j->next = i;
4644 } else {
4645 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004646 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004647}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004648
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004649void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004650{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004651 Token *inc, *space, *name;
4652 Line *l;
4653
H. Peter Anvin734b1882002-04-30 21:01:08 +00004654 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4655 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4656 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004657
4658 l = nasm_malloc(sizeof(Line));
4659 l->next = predef;
4660 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004661 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004662 predef = l;
4663}
4664
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004665void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004666{
4667 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004668 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004669 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004670
4671 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004672 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4673 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004674 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004675 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004676 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004677 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004678 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004679
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004680 l = nasm_malloc(sizeof(Line));
4681 l->next = predef;
4682 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004683 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004684 predef = l;
4685}
4686
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004687void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004688{
4689 Token *def, *space;
4690 Line *l;
4691
H. Peter Anvin734b1882002-04-30 21:01:08 +00004692 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4693 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004694 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004695
4696 l = nasm_malloc(sizeof(Line));
4697 l->next = predef;
4698 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004699 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004700 predef = l;
4701}
4702
Keith Kaniosb7a89542007-04-12 02:40:54 +00004703/*
4704 * Added by Keith Kanios:
4705 *
4706 * This function is used to assist with "runtime" preprocessor
4707 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4708 *
4709 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4710 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4711 */
4712
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004713void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004714{
4715 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004716
Keith Kaniosb7a89542007-04-12 02:40:54 +00004717 def = tokenize(definition);
4718 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4719 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004720
Keith Kaniosb7a89542007-04-12 02:40:54 +00004721}
4722
H. Peter Anvina70547f2008-07-19 21:44:26 -07004723void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004724{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004725 extrastdmac = macros;
4726}
4727
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004728static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004729{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004730 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004731 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004732 tok->text = nasm_strdup(numbuf);
4733 tok->type = TOK_NUMBER;
4734}
4735
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004736Preproc nasmpp = {
4737 pp_reset,
4738 pp_getline,
4739 pp_cleanup
4740};