blob: e83abbe9ccce834ab5e742b93ad2c144850006ca [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{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000782 char *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++;
813 while (*p) {
814 if (*p == ']') {
815 if (!--lvl)
816 break;
817 } else if (*p == '%' && p[1] == '[') {
818 lvl++;
819 }
820 p++;
821 }
822 if (*p)
823 *p++ = '\0';
824 type = TOK_INDIRECT;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700825 } else if (*p == '?') {
826 type = TOK_PREPROC_Q; /* %? */
827 p++;
828 if (*p == '?') {
829 type = TOK_PREPROC_QQ; /* %?? */
830 p++;
831 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000832 } else if (isidchar(*p) ||
833 ((*p == '!' || *p == '%' || *p == '$') &&
834 isidchar(p[1]))) {
835 do {
836 p++;
837 }
838 while (isidchar(*p));
839 type = TOK_PREPROC_ID;
840 } else {
841 type = TOK_OTHER;
842 if (*p == '%')
843 p++;
844 }
845 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
846 type = TOK_ID;
847 p++;
848 while (*p && isidchar(*p))
849 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700850 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000851 /*
852 * A string token.
853 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000854 type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700855 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000856
H. Peter Anvine2c80182005-01-15 22:15:51 +0000857 if (*p) {
858 p++;
859 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -0700860 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000861 /* Handling unterminated strings by UNV */
862 /* type = -1; */
863 }
864 } else if (isnumstart(*p)) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700865 bool is_hex = false;
866 bool is_float = false;
867 bool has_e = false;
868 char c, *r;
869
H. Peter Anvine2c80182005-01-15 22:15:51 +0000870 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700871 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000872 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700873
874 if (*p == '$') {
875 p++;
876 is_hex = true;
877 }
878
879 for (;;) {
880 c = *p++;
881
882 if (!is_hex && (c == 'e' || c == 'E')) {
883 has_e = true;
884 if (*p == '+' || *p == '-') {
885 /* e can only be followed by +/- if it is either a
886 prefixed hex number or a floating-point number */
887 p++;
888 is_float = true;
889 }
890 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
891 is_hex = true;
892 } else if (c == 'P' || c == 'p') {
893 is_float = true;
894 if (*p == '+' || *p == '-')
895 p++;
896 } else if (isnumchar(c) || c == '_')
897 ; /* just advance */
898 else if (c == '.') {
899 /* we need to deal with consequences of the legacy
900 parser, like "1.nolist" being two tokens
901 (TOK_NUMBER, TOK_ID) here; at least give it
902 a shot for now. In the future, we probably need
903 a flex-based scanner with proper pattern matching
904 to do it as well as it can be done. Nothing in
905 the world is going to help the person who wants
906 0x123.p16 interpreted as two tokens, though. */
907 r = p;
908 while (*r == '_')
909 r++;
910
H. Peter Anvinf221b9e2008-06-21 11:03:51 -0700911 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700912 (!is_hex && (*r == 'e' || *r == 'E')) ||
913 (*r == 'p' || *r == 'P')) {
914 p = r;
915 is_float = true;
916 } else
917 break; /* Terminate the token */
918 } else
919 break;
920 }
921 p--; /* Point to first character beyond number */
922
923 if (has_e && !is_hex) {
924 /* 1e13 is floating-point, but 1e13h is not */
925 is_float = true;
926 }
927
928 type = is_float ? TOK_FLOAT : TOK_NUMBER;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700929 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000930 type = TOK_WHITESPACE;
931 p++;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700932 while (*p && nasm_isspace(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000933 p++;
934 /*
935 * Whitespace just before end-of-line is discarded by
936 * pretending it's a comment; whitespace just before a
937 * comment gets lumped into the comment.
938 */
939 if (!*p || *p == ';') {
940 type = TOK_COMMENT;
941 while (*p)
942 p++;
943 }
944 } else if (*p == ';') {
945 type = TOK_COMMENT;
946 while (*p)
947 p++;
948 } else {
949 /*
950 * Anything else is an operator of some kind. We check
951 * for all the double-character operators (>>, <<, //,
952 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000953 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000954 */
955 type = TOK_OTHER;
956 if ((p[0] == '>' && p[1] == '>') ||
957 (p[0] == '<' && p[1] == '<') ||
958 (p[0] == '/' && p[1] == '/') ||
959 (p[0] == '<' && p[1] == '=') ||
960 (p[0] == '>' && p[1] == '=') ||
961 (p[0] == '=' && p[1] == '=') ||
962 (p[0] == '!' && p[1] == '=') ||
963 (p[0] == '<' && p[1] == '>') ||
964 (p[0] == '&' && p[1] == '&') ||
965 (p[0] == '|' && p[1] == '|') ||
966 (p[0] == '^' && p[1] == '^')) {
967 p++;
968 }
969 p++;
970 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000971
H. Peter Anvine2c80182005-01-15 22:15:51 +0000972 /* Handling unterminated string by UNV */
973 /*if (type == -1)
974 {
975 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
976 t->text[p-line] = *line;
977 tail = &t->next;
978 }
979 else */
980 if (type != TOK_COMMENT) {
981 *tail = t = new_Token(NULL, type, line, p - line);
982 tail = &t->next;
983 }
984 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000985 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000986 return list;
987}
988
H. Peter Anvince616072002-04-30 21:02:23 +0000989/*
990 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -0700991 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +0000992 * deleted only all at once by the delete_Blocks function.
993 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +0000995{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000996 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000998 /* first, get to the end of the linked list */
999 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001001 /* now allocate the requested chunk */
1002 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001004 /* now allocate a new block for the next request */
1005 b->next = nasm_malloc(sizeof(Blocks));
1006 /* and initialize the contents of the new block */
1007 b->next->next = NULL;
1008 b->next->chunk = NULL;
1009 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001010}
1011
1012/*
1013 * this function deletes all managed blocks of memory
1014 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001015static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001016{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001018
H. Peter Anvin70653092007-10-19 14:42:29 -07001019 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001020 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001021 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001022 * free it.
1023 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001024 while (b) {
1025 if (b->chunk)
1026 nasm_free(b->chunk);
1027 a = b;
1028 b = b->next;
1029 if (a != &blocks)
1030 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001031 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001032}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001033
1034/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001035 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001036 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001037 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001038 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001039static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvinc751e862008-06-09 10:18:45 -07001040 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001041{
1042 Token *t;
1043 int i;
1044
H. Peter Anvine2c80182005-01-15 22:15:51 +00001045 if (freeTokens == NULL) {
1046 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1047 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1048 freeTokens[i].next = &freeTokens[i + 1];
1049 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001050 }
1051 t = freeTokens;
1052 freeTokens = t->next;
1053 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001054 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001055 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001056 if (type == TOK_WHITESPACE || text == NULL) {
1057 t->text = NULL;
1058 } else {
1059 if (txtlen == 0)
1060 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001061 t->text = nasm_malloc(txtlen+1);
1062 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001063 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001064 }
1065 return t;
1066}
1067
H. Peter Anvine2c80182005-01-15 22:15:51 +00001068static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001069{
1070 Token *next = t->next;
1071 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001072 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001073 freeTokens = t;
1074 return next;
1075}
1076
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001077/*
1078 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001079 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1080 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001081 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001082static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001083{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001084 Token *t;
1085 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001086 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001087 const char *q;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001088
1089 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 for (t = tlist; t; t = t->next) {
1091 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001092 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093 nasm_free(t->text);
1094 if (p)
1095 t->text = nasm_strdup(p);
1096 else
1097 t->text = NULL;
1098 }
1099 /* Expand local macros here and not during preprocessing */
1100 if (expand_locals &&
1101 t->type == TOK_PREPROC_ID && t->text &&
1102 t->text[0] == '%' && t->text[1] == '$') {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001103 Context *ctx = get_ctx(t->text, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001104 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001105 char buffer[40];
1106 char *p, *q = t->text + 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001107
H. Peter Anvine2c80182005-01-15 22:15:51 +00001108 q += strspn(q, "$");
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001109 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001110 p = nasm_strcat(buffer, q);
1111 nasm_free(t->text);
1112 t->text = p;
1113 }
1114 }
1115 if (t->type == TOK_WHITESPACE) {
1116 len++;
1117 } else if (t->text) {
1118 len += strlen(t->text);
1119 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001120 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001121 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001122 for (t = tlist; t; t = t->next) {
1123 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001124 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001125 } else if (t->text) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001126 q = t->text;
1127 while (*q)
1128 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001130 }
1131 *p = '\0';
1132 return line;
1133}
1134
1135/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001136 * A scanner, suitable for use by the expression evaluator, which
1137 * operates on a line of Tokens. Expects a pointer to a pointer to
1138 * the first token in the line to be passed in as its private_data
1139 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001140 *
1141 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001142 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001143static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001144{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001145 Token **tlineptr = private_data;
1146 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001147 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001148
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 do {
1150 tline = *tlineptr;
1151 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001152 }
1153 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001154 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001155
1156 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001157 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001158
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001159 tokval->t_charptr = tline->text;
1160
H. Peter Anvin76690a12002-04-30 20:52:49 +00001161 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001163 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001164 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001165
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001167 p = tokval->t_charptr = tline->text;
1168 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001169 tokval->t_charptr++;
1170 return tokval->t_type = TOKEN_ID;
1171 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001172
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001173 for (r = p, s = ourcopy; *r; r++) {
Philipp Thomas76ec8e72008-05-21 08:53:21 -07001174 if (r >= p+MAX_KEYWORD)
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001175 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001176 *s++ = nasm_tolower(*r);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001177 }
1178 *s = '\0';
1179 /* right, so we have an identifier sitting in temp storage. now,
1180 * is it actually a register or instruction name, or what? */
1181 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001182 }
1183
H. Peter Anvine2c80182005-01-15 22:15:51 +00001184 if (tline->type == TOK_NUMBER) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001185 bool rn_error;
1186 tokval->t_integer = readnum(tline->text, &rn_error);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001187 tokval->t_charptr = tline->text;
H. Peter Anvin11627042008-06-09 20:45:19 -07001188 if (rn_error)
1189 return tokval->t_type = TOKEN_ERRNUM;
1190 else
1191 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001192 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001193
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001194 if (tline->type == TOK_FLOAT) {
1195 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001196 }
1197
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198 if (tline->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001199 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001200
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001201 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001202 tokval->t_charptr = tline->text;
1203 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001204
H. Peter Anvin11627042008-06-09 20:45:19 -07001205 if (ep[0] != bq || ep[1] != '\0')
1206 return tokval->t_type = TOKEN_ERRSTR;
1207 else
1208 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001209 }
1210
H. Peter Anvine2c80182005-01-15 22:15:51 +00001211 if (tline->type == TOK_OTHER) {
1212 if (!strcmp(tline->text, "<<"))
1213 return tokval->t_type = TOKEN_SHL;
1214 if (!strcmp(tline->text, ">>"))
1215 return tokval->t_type = TOKEN_SHR;
1216 if (!strcmp(tline->text, "//"))
1217 return tokval->t_type = TOKEN_SDIV;
1218 if (!strcmp(tline->text, "%%"))
1219 return tokval->t_type = TOKEN_SMOD;
1220 if (!strcmp(tline->text, "=="))
1221 return tokval->t_type = TOKEN_EQ;
1222 if (!strcmp(tline->text, "<>"))
1223 return tokval->t_type = TOKEN_NE;
1224 if (!strcmp(tline->text, "!="))
1225 return tokval->t_type = TOKEN_NE;
1226 if (!strcmp(tline->text, "<="))
1227 return tokval->t_type = TOKEN_LE;
1228 if (!strcmp(tline->text, ">="))
1229 return tokval->t_type = TOKEN_GE;
1230 if (!strcmp(tline->text, "&&"))
1231 return tokval->t_type = TOKEN_DBL_AND;
1232 if (!strcmp(tline->text, "^^"))
1233 return tokval->t_type = TOKEN_DBL_XOR;
1234 if (!strcmp(tline->text, "||"))
1235 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001236 }
1237
1238 /*
1239 * We have no other options: just return the first character of
1240 * the token text.
1241 */
1242 return tokval->t_type = tline->text[0];
1243}
1244
1245/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001246 * Compare a string to the name of an existing macro; this is a
1247 * simple wrapper which calls either strcmp or nasm_stricmp
1248 * depending on the value of the `casesense' parameter.
1249 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001250static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001251{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001252 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001253}
1254
1255/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001256 * Compare a string to the name of an existing macro; this is a
1257 * simple wrapper which calls either strcmp or nasm_stricmp
1258 * depending on the value of the `casesense' parameter.
1259 */
1260static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1261{
1262 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1263}
1264
1265/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001266 * Return the Context structure associated with a %$ token. Return
1267 * NULL, having _already_ reported an error condition, if the
1268 * context stack isn't deep enough for the supplied number of $
1269 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001270 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001271 * also scanned for such smacro, until it is found; if not -
1272 * only the context that directly results from the number of $'s
1273 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001274 */
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001275static Context *get_ctx(const char *name, bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001276{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001277 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001278 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001279 int i;
1280
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001281 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001282 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001283
H. Peter Anvine2c80182005-01-15 22:15:51 +00001284 if (!cstk) {
1285 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1286 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001287 }
1288
H. Peter Anvine2c80182005-01-15 22:15:51 +00001289 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1290 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001291/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001292 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001293 if (!ctx) {
1294 error(ERR_NONFATAL, "`%s': context stack is only"
1295 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1296 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001297 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001298 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001300
H. Peter Anvine2c80182005-01-15 22:15:51 +00001301 do {
1302 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001303 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001304 while (m) {
1305 if (!mstrcmp(m->name, name, m->casesense))
1306 return ctx;
1307 m = m->next;
1308 }
1309 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001310 }
1311 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001312 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001313}
1314
1315/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001316 * Check to see if a file is already in a string list
1317 */
1318static bool in_list(const StrList *list, const char *str)
1319{
1320 while (list) {
1321 if (!strcmp(list->str, str))
1322 return true;
1323 list = list->next;
1324 }
1325 return false;
1326}
1327
1328/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001329 * Open an include file. This routine must always return a valid
1330 * file pointer if it returns - it's responsible for throwing an
1331 * ERR_FATAL and bombing out completely if not. It should also try
1332 * the include path one by one until it finds the file or reaches
1333 * the end of the path.
1334 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001335static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
H. Peter Anvin418ca702008-05-30 10:42:30 -07001336 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001337{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001338 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001339 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001340 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001341 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001342 size_t prefix_len = 0;
1343 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001344
H. Peter Anvine2c80182005-01-15 22:15:51 +00001345 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001346 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1347 memcpy(sl->str, prefix, prefix_len);
1348 memcpy(sl->str+prefix_len, file, len+1);
1349 fp = fopen(sl->str, "r");
H. Peter Anvin418ca702008-05-30 10:42:30 -07001350 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001351 sl->next = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001352 **dtail = sl;
1353 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001354 } else {
1355 nasm_free(sl);
1356 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001357 if (fp)
1358 return fp;
H. Peter Anvin418ca702008-05-30 10:42:30 -07001359 if (!ip) {
1360 if (!missing_ok)
1361 break;
1362 prefix = NULL;
1363 } else {
1364 prefix = ip->path;
1365 ip = ip->next;
1366 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001367 if (prefix) {
1368 prefix_len = strlen(prefix);
1369 } else {
1370 /* -MG given and file not found */
H. Peter Anvin418ca702008-05-30 10:42:30 -07001371 if (dhead && !in_list(*dhead, file)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001372 sl = nasm_malloc(len+1+sizeof sl->next);
1373 sl->next = NULL;
1374 strcpy(sl->str, file);
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001375 **dtail = sl;
1376 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001377 }
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001378 return NULL;
1379 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001380 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001381
H. Peter Anvin734b1882002-04-30 21:01:08 +00001382 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001383 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001384}
1385
1386/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001387 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001388 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001389 * return true if _any_ single-line macro of that name is defined.
1390 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001391 * `nparam' or no parameters is defined.
1392 *
1393 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001394 * defined, or nparam is -1, the address of the definition structure
1395 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001396 * is NULL, no action will be taken regarding its contents, and no
1397 * error will occur.
1398 *
1399 * Note that this is also called with nparam zero to resolve
1400 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001401 *
1402 * If you already know which context macro belongs to, you can pass
1403 * the context pointer as first parameter; if you won't but name begins
1404 * with %$ the context will be automatically computed. If all_contexts
1405 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001406 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001407static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001408smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001409 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001410{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001411 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001412 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001413
H. Peter Anvin97a23472007-09-16 17:57:25 -07001414 if (ctx) {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001415 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001416 } else if (name[0] == '%' && name[1] == '$') {
1417 if (cstk)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001418 ctx = get_ctx(name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001419 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001420 return false; /* got to return _something_ */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001421 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001422 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001423 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001424 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001425 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001426
H. Peter Anvine2c80182005-01-15 22:15:51 +00001427 while (m) {
1428 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001429 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001431 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001432 *defn = m;
1433 else
1434 *defn = NULL;
1435 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001436 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 }
1438 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001439 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001440
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001441 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001442}
1443
1444/*
1445 * Count and mark off the parameters in a multi-line macro call.
1446 * This is called both from within the multi-line macro expansion
1447 * code, and also to mark off the default parameters when provided
1448 * in a %macro definition line.
1449 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001450static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001451{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001452 int paramsize, brace;
1453
1454 *nparam = paramsize = 0;
1455 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001456 while (t) {
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001457 /* +1: we need space for the final NULL */
1458 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001459 paramsize += PARAM_DELTA;
1460 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1461 }
1462 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001463 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001464 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001465 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001466 (*params)[(*nparam)++] = t;
1467 while (tok_isnt_(t, brace ? "}" : ","))
1468 t = t->next;
1469 if (t) { /* got a comma/brace */
1470 t = t->next;
1471 if (brace) {
1472 /*
1473 * Now we've found the closing brace, look further
1474 * for the comma.
1475 */
1476 skip_white_(t);
1477 if (tok_isnt_(t, ",")) {
1478 error(ERR_NONFATAL,
1479 "braces do not enclose all of macro parameter");
1480 while (tok_isnt_(t, ","))
1481 t = t->next;
1482 }
1483 if (t)
1484 t = t->next; /* eat the comma */
1485 }
1486 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001487 }
1488}
1489
1490/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001491 * Determine whether one of the various `if' conditions is true or
1492 * not.
1493 *
1494 * We must free the tline we get passed.
1495 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001496static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001497{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001498 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001499 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001500 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001501 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001502 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001503 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001504
1505 origline = tline;
1506
H. Peter Anvine2c80182005-01-15 22:15:51 +00001507 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001508 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001509 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001510 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001511 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001512 if (!tline)
1513 break;
1514 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001515 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001516 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001517 free_tlist(origline);
1518 return -1;
1519 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001520 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001521 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001522 tline = tline->next;
1523 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001524 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001525
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001526 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001527 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001528 while (tline) {
1529 skip_white_(tline);
1530 if (!tline || (tline->type != TOK_ID &&
1531 (tline->type != TOK_PREPROC_ID ||
1532 tline->text[1] != '$'))) {
1533 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001534 "`%s' expects macro identifiers", pp_directives[ct]);
1535 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001536 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001537 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001538 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001539 tline = tline->next;
1540 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001541 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001542
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001543 case PPC_IFIDN:
1544 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001545 tline = expand_smacro(tline);
1546 t = tt = tline;
1547 while (tok_isnt_(tt, ","))
1548 tt = tt->next;
1549 if (!tt) {
1550 error(ERR_NONFATAL,
1551 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001552 pp_directives[ct]);
1553 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001554 }
1555 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001556 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001557 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1558 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1559 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001560 pp_directives[ct]);
1561 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001562 }
1563 if (t->type == TOK_WHITESPACE) {
1564 t = t->next;
1565 continue;
1566 }
1567 if (tt->type == TOK_WHITESPACE) {
1568 tt = tt->next;
1569 continue;
1570 }
1571 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001572 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001573 break;
1574 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001575 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001576 if (t->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001577 size_t l1 = nasm_unquote(t->text, NULL);
1578 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001579
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001580 if (l1 != l2) {
1581 j = false;
1582 break;
1583 }
1584 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1585 j = false;
1586 break;
1587 }
1588 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001589 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001590 break;
1591 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001592
H. Peter Anvine2c80182005-01-15 22:15:51 +00001593 t = t->next;
1594 tt = tt->next;
1595 }
1596 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001597 j = false; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001598 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001599
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001600 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001601 {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001602 bool found = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001603 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001604
H. Peter Anvine2c80182005-01-15 22:15:51 +00001605 tline = tline->next;
1606 skip_white_(tline);
1607 tline = expand_id(tline);
1608 if (!tok_type_(tline, TOK_ID)) {
1609 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001610 "`%s' expects a macro name", pp_directives[ct]);
1611 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001612 }
1613 searching.name = nasm_strdup(tline->text);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001614 searching.casesense = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001615 searching.plus = false;
1616 searching.nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001617 searching.in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001618 searching.rep_nest = NULL;
1619 searching.nparam_min = 0;
1620 searching.nparam_max = INT_MAX;
1621 tline = expand_smacro(tline->next);
1622 skip_white_(tline);
1623 if (!tline) {
1624 } else if (!tok_type_(tline, TOK_NUMBER)) {
1625 error(ERR_NONFATAL,
1626 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001627 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 } else {
1629 searching.nparam_min = searching.nparam_max =
1630 readnum(tline->text, &j);
1631 if (j)
1632 error(ERR_NONFATAL,
1633 "unable to parse parameter count `%s'",
1634 tline->text);
1635 }
1636 if (tline && tok_is_(tline->next, "-")) {
1637 tline = tline->next->next;
1638 if (tok_is_(tline, "*"))
1639 searching.nparam_max = INT_MAX;
1640 else if (!tok_type_(tline, TOK_NUMBER))
1641 error(ERR_NONFATAL,
1642 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001643 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001644 else {
1645 searching.nparam_max = readnum(tline->text, &j);
1646 if (j)
1647 error(ERR_NONFATAL,
1648 "unable to parse parameter count `%s'",
1649 tline->text);
1650 if (searching.nparam_min > searching.nparam_max)
1651 error(ERR_NONFATAL,
1652 "minimum parameter count exceeds maximum");
1653 }
1654 }
1655 if (tline && tok_is_(tline->next, "+")) {
1656 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001657 searching.plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001658 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001659 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001660 while (mmac) {
1661 if (!strcmp(mmac->name, searching.name) &&
1662 (mmac->nparam_min <= searching.nparam_max
1663 || searching.plus)
1664 && (searching.nparam_min <= mmac->nparam_max
1665 || mmac->plus)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001666 found = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001667 break;
1668 }
1669 mmac = mmac->next;
1670 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001671 if(tline && tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001672 error(ERR_WARNING|ERR_PASS1,
1673 "trailing garbage after %%ifmacro ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001674 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001675 j = found;
1676 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001677 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001678
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001679 case PPC_IFID:
1680 needtype = TOK_ID;
1681 goto iftype;
1682 case PPC_IFNUM:
1683 needtype = TOK_NUMBER;
1684 goto iftype;
1685 case PPC_IFSTR:
1686 needtype = TOK_STRING;
1687 goto iftype;
1688
1689 iftype:
H. Peter Anvin134b9462008-02-16 17:01:40 -08001690 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001691
H. Peter Anvin927c92b2008-02-16 13:44:52 -08001692 while (tok_type_(t, TOK_WHITESPACE) ||
1693 (needtype == TOK_NUMBER &&
1694 tok_type_(t, TOK_OTHER) &&
1695 (t->text[0] == '-' || t->text[0] == '+') &&
1696 !t->text[1]))
1697 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001698
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001699 j = tok_type_(t, needtype);
1700 break;
1701
1702 case PPC_IFTOKEN:
1703 t = tline = expand_smacro(tline);
1704 while (tok_type_(t, TOK_WHITESPACE))
1705 t = t->next;
1706
1707 j = false;
1708 if (t) {
1709 t = t->next; /* Skip the actual token */
1710 while (tok_type_(t, TOK_WHITESPACE))
1711 t = t->next;
1712 j = !t; /* Should be nothing left */
1713 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001714 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001715
H. Peter Anvin134b9462008-02-16 17:01:40 -08001716 case PPC_IFEMPTY:
1717 t = tline = expand_smacro(tline);
1718 while (tok_type_(t, TOK_WHITESPACE))
1719 t = t->next;
1720
1721 j = !t; /* Should be empty */
1722 break;
1723
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001724 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001725 t = tline = expand_smacro(tline);
1726 tptr = &t;
1727 tokval.t_type = TOKEN_INVALID;
1728 evalresult = evaluate(ppscan, tptr, &tokval,
1729 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001730 if (!evalresult)
1731 return -1;
1732 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001733 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001734 "trailing garbage after expression ignored");
1735 if (!is_simple(evalresult)) {
1736 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001737 "non-constant value given to `%s'", pp_directives[ct]);
1738 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001739 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001740 j = reloc_value(evalresult) != 0;
H. Peter Anvin90e6e812008-07-16 14:38:24 -07001741 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001742
H. Peter Anvine2c80182005-01-15 22:15:51 +00001743 default:
1744 error(ERR_FATAL,
1745 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001746 pp_directives[ct]);
1747 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001748 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001749
1750 free_tlist(origline);
1751 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001752
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001753fail:
1754 free_tlist(origline);
1755 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001756}
1757
1758/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001759 * Common code for defining an smacro
1760 */
1761static bool define_smacro(Context *ctx, char *mname, bool casesense,
1762 int nparam, Token *expansion)
1763{
1764 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001765 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001766
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001767 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1768 if (!smac) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001769 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001770 "single-line macro `%s' defined both with and"
1771 " without parameters", mname);
1772
1773 /* Some instances of the old code considered this a failure,
1774 some others didn't. What is the right thing to do here? */
1775 free_tlist(expansion);
1776 return false; /* Failure */
1777 } else {
1778 /*
1779 * We're redefining, so we have to take over an
1780 * existing SMacro structure. This means freeing
1781 * what was already in it.
1782 */
1783 nasm_free(smac->name);
1784 free_tlist(smac->expansion);
1785 }
1786 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001787 smtbl = ctx ? &ctx->localmac : &smacros;
1788 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001789 smac = nasm_malloc(sizeof(SMacro));
1790 smac->next = *smhead;
1791 *smhead = smac;
1792 }
1793 smac->name = nasm_strdup(mname);
1794 smac->casesense = casesense;
1795 smac->nparam = nparam;
1796 smac->expansion = expansion;
1797 smac->in_progress = false;
1798 return true; /* Success */
1799}
1800
1801/*
1802 * Undefine an smacro
1803 */
1804static void undef_smacro(Context *ctx, const char *mname)
1805{
1806 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001807 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001808
H. Peter Anvin166c2472008-05-28 12:28:58 -07001809 smtbl = ctx ? &ctx->localmac : &smacros;
1810 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001811
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001812 if (smhead) {
1813 /*
1814 * We now have a macro name... go hunt for it.
1815 */
1816 sp = smhead;
1817 while ((s = *sp) != NULL) {
1818 if (!mstrcmp(s->name, mname, s->casesense)) {
1819 *sp = s->next;
1820 nasm_free(s->name);
1821 free_tlist(s->expansion);
1822 nasm_free(s);
1823 } else {
1824 sp = &s->next;
1825 }
1826 }
1827 }
1828}
1829
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001830/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07001831 * Parse a mmacro specification.
1832 */
1833static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1834{
1835 bool err;
1836
1837 tline = tline->next;
1838 skip_white_(tline);
1839 tline = expand_id(tline);
1840 if (!tok_type_(tline, TOK_ID)) {
1841 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1842 return false;
1843 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001844
H. Peter Anvina26433d2008-07-16 14:40:01 -07001845 def->name = nasm_strdup(tline->text);
1846 def->plus = false;
1847 def->nolist = false;
1848 def->in_progress = 0;
1849 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001850 def->nparam_min = 0;
1851 def->nparam_max = 0;
1852
H. Peter Anvina26433d2008-07-16 14:40:01 -07001853 tline = expand_smacro(tline->next);
1854 skip_white_(tline);
1855 if (!tok_type_(tline, TOK_NUMBER)) {
1856 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001857 } else {
1858 def->nparam_min = def->nparam_max =
1859 readnum(tline->text, &err);
1860 if (err)
1861 error(ERR_NONFATAL,
1862 "unable to parse parameter count `%s'", tline->text);
1863 }
1864 if (tline && tok_is_(tline->next, "-")) {
1865 tline = tline->next->next;
1866 if (tok_is_(tline, "*")) {
1867 def->nparam_max = INT_MAX;
1868 } else if (!tok_type_(tline, TOK_NUMBER)) {
1869 error(ERR_NONFATAL,
1870 "`%s' expects a parameter count after `-'", directive);
1871 } else {
1872 def->nparam_max = readnum(tline->text, &err);
1873 if (err) {
1874 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
1875 tline->text);
1876 }
1877 if (def->nparam_min > def->nparam_max) {
1878 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
1879 }
1880 }
1881 }
1882 if (tline && tok_is_(tline->next, "+")) {
1883 tline = tline->next;
1884 def->plus = true;
1885 }
1886 if (tline && tok_type_(tline->next, TOK_ID) &&
1887 !nasm_stricmp(tline->next->text, ".nolist")) {
1888 tline = tline->next;
1889 def->nolist = true;
1890 }
1891
1892 /*
1893 * Handle default parameters.
1894 */
1895 if (tline && tline->next) {
1896 def->dlist = tline->next;
1897 tline->next = NULL;
1898 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
1899 } else {
1900 def->dlist = NULL;
1901 def->defaults = NULL;
1902 }
1903 def->expansion = NULL;
1904
Victor van den Elzen22343c22008-08-06 14:47:54 +02001905 if(def->defaults &&
1906 def->ndefs > def->nparam_max - def->nparam_min &&
1907 !def->plus)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001908 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
1909 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001910
H. Peter Anvina26433d2008-07-16 14:40:01 -07001911 return true;
1912}
1913
1914
1915/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001916 * Decode a size directive
1917 */
1918static int parse_size(const char *str) {
1919 static const char *size_names[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001920 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001921 static const int sizes[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001922 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001923
1924 return sizes[bsii(str, size_names, elements(size_names))+1];
1925}
1926
Ed Beroset3ab3f412002-06-11 03:31:49 +00001927/**
1928 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001929 * Find out if a line contains a preprocessor directive, and deal
1930 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07001931 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001932 * If a directive _is_ found, it is the responsibility of this routine
1933 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001934 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001935 * @param tline a pointer to the current tokeninzed line linked list
1936 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07001937 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001938 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001939static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001940{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001941 enum preproc_token i;
1942 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07001943 bool err;
1944 int nparam;
1945 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001946 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001947 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001948 int offset;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001949 char *p, *pp, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001950 Include *inc;
1951 Context *ctx;
1952 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001953 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001954 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1955 Line *l;
1956 struct tokenval tokval;
1957 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001958 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001959 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001960 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07001961 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001962
1963 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001964
H. Peter Anvineba20a72002-04-30 20:53:55 +00001965 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07001966 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001967 (tline->text[1] == '%' || tline->text[1] == '$'
1968 || tline->text[1] == '!'))
1969 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001970
H. Peter Anvin4169a472007-09-12 01:29:43 +00001971 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001972
1973 /*
1974 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001975 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001976 * we should ignore all directives except for condition
1977 * directives.
1978 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001979 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001980 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1981 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001982 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001983
1984 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001985 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02001986 * ignore all directives except for %macro/%imacro (which nest),
1987 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
1988 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001989 */
1990 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001991 i != PP_ENDMACRO && i != PP_ENDM &&
1992 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1993 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001994 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001995
Charles Crayned4200be2008-07-12 16:42:33 -07001996 if (defining) {
1997 if (i == PP_MACRO || i == PP_IMACRO) {
1998 nested_mac_count++;
1999 return NO_DIRECTIVE_FOUND;
2000 } else if (nested_mac_count > 0) {
2001 if (i == PP_ENDMACRO) {
2002 nested_mac_count--;
2003 return NO_DIRECTIVE_FOUND;
2004 }
2005 }
2006 if (!defining->name) {
2007 if (i == PP_REP) {
2008 nested_rep_count++;
2009 return NO_DIRECTIVE_FOUND;
2010 } else if (nested_rep_count > 0) {
2011 if (i == PP_ENDREP) {
2012 nested_rep_count--;
2013 return NO_DIRECTIVE_FOUND;
2014 }
2015 }
2016 }
2017 }
2018
H. Peter Anvin4169a472007-09-12 01:29:43 +00002019 switch (i) {
2020 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002021 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2022 tline->text);
2023 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002024
H. Peter Anvine2c80182005-01-15 22:15:51 +00002025 case PP_STACKSIZE:
2026 /* Directive to tell NASM what the default stack size is. The
2027 * default is for a 16-bit stack, and this can be overriden with
2028 * %stacksize large.
2029 * the following form:
2030 *
2031 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2032 */
2033 tline = tline->next;
2034 if (tline && tline->type == TOK_WHITESPACE)
2035 tline = tline->next;
2036 if (!tline || tline->type != TOK_ID) {
2037 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2038 free_tlist(origline);
2039 return DIRECTIVE_FOUND;
2040 }
2041 if (nasm_stricmp(tline->text, "flat") == 0) {
2042 /* All subsequent ARG directives are for a 32-bit stack */
2043 StackSize = 4;
2044 StackPointer = "ebp";
2045 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002046 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002047 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2048 /* All subsequent ARG directives are for a 64-bit stack */
2049 StackSize = 8;
2050 StackPointer = "rbp";
2051 ArgOffset = 8;
2052 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002053 } else if (nasm_stricmp(tline->text, "large") == 0) {
2054 /* All subsequent ARG directives are for a 16-bit stack,
2055 * far function call.
2056 */
2057 StackSize = 2;
2058 StackPointer = "bp";
2059 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002060 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002061 } else if (nasm_stricmp(tline->text, "small") == 0) {
2062 /* All subsequent ARG directives are for a 16-bit stack,
2063 * far function call. We don't support near functions.
2064 */
2065 StackSize = 2;
2066 StackPointer = "bp";
2067 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002068 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002069 } else {
2070 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2071 free_tlist(origline);
2072 return DIRECTIVE_FOUND;
2073 }
2074 free_tlist(origline);
2075 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002076
H. Peter Anvine2c80182005-01-15 22:15:51 +00002077 case PP_ARG:
2078 /* TASM like ARG directive to define arguments to functions, in
2079 * the following form:
2080 *
2081 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2082 */
2083 offset = ArgOffset;
2084 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002085 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002086 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002087
H. Peter Anvine2c80182005-01-15 22:15:51 +00002088 /* Find the argument name */
2089 tline = tline->next;
2090 if (tline && tline->type == TOK_WHITESPACE)
2091 tline = tline->next;
2092 if (!tline || tline->type != TOK_ID) {
2093 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2094 free_tlist(origline);
2095 return DIRECTIVE_FOUND;
2096 }
2097 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002098
H. Peter Anvine2c80182005-01-15 22:15:51 +00002099 /* Find the argument size type */
2100 tline = tline->next;
2101 if (!tline || tline->type != TOK_OTHER
2102 || tline->text[0] != ':') {
2103 error(ERR_NONFATAL,
2104 "Syntax error processing `%%arg' directive");
2105 free_tlist(origline);
2106 return DIRECTIVE_FOUND;
2107 }
2108 tline = tline->next;
2109 if (!tline || tline->type != TOK_ID) {
2110 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2111 free_tlist(origline);
2112 return DIRECTIVE_FOUND;
2113 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002114
H. Peter Anvine2c80182005-01-15 22:15:51 +00002115 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002116 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002117 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002118 size = parse_size(tt->text);
2119 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002120 error(ERR_NONFATAL,
2121 "Invalid size type for `%%arg' missing directive");
2122 free_tlist(tt);
2123 free_tlist(origline);
2124 return DIRECTIVE_FOUND;
2125 }
2126 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002127
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002128 /* Round up to even stack slots */
2129 size = (size+StackSize-1) & ~(StackSize-1);
2130
H. Peter Anvine2c80182005-01-15 22:15:51 +00002131 /* Now define the macro for the argument */
2132 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2133 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002134 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002135 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002136
H. Peter Anvine2c80182005-01-15 22:15:51 +00002137 /* Move to the next argument in the list */
2138 tline = tline->next;
2139 if (tline && tline->type == TOK_WHITESPACE)
2140 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002141 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2142 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002143 free_tlist(origline);
2144 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002145
H. Peter Anvine2c80182005-01-15 22:15:51 +00002146 case PP_LOCAL:
2147 /* TASM like LOCAL directive to define local variables for a
2148 * function, in the following form:
2149 *
2150 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2151 *
2152 * The '= LocalSize' at the end is ignored by NASM, but is
2153 * required by TASM to define the local parameter size (and used
2154 * by the TASM macro package).
2155 */
2156 offset = LocalOffset;
2157 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002158 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002159 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002160
H. Peter Anvine2c80182005-01-15 22:15:51 +00002161 /* Find the argument name */
2162 tline = tline->next;
2163 if (tline && tline->type == TOK_WHITESPACE)
2164 tline = tline->next;
2165 if (!tline || tline->type != TOK_ID) {
2166 error(ERR_NONFATAL,
2167 "`%%local' missing argument parameter");
2168 free_tlist(origline);
2169 return DIRECTIVE_FOUND;
2170 }
2171 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002172
H. Peter Anvine2c80182005-01-15 22:15:51 +00002173 /* Find the argument size type */
2174 tline = tline->next;
2175 if (!tline || tline->type != TOK_OTHER
2176 || tline->text[0] != ':') {
2177 error(ERR_NONFATAL,
2178 "Syntax error processing `%%local' directive");
2179 free_tlist(origline);
2180 return DIRECTIVE_FOUND;
2181 }
2182 tline = tline->next;
2183 if (!tline || tline->type != TOK_ID) {
2184 error(ERR_NONFATAL,
2185 "`%%local' missing size type parameter");
2186 free_tlist(origline);
2187 return DIRECTIVE_FOUND;
2188 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002189
H. Peter Anvine2c80182005-01-15 22:15:51 +00002190 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002191 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002192 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002193 size = parse_size(tt->text);
2194 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002195 error(ERR_NONFATAL,
2196 "Invalid size type for `%%local' missing directive");
2197 free_tlist(tt);
2198 free_tlist(origline);
2199 return DIRECTIVE_FOUND;
2200 }
2201 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002202
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002203 /* Round up to even stack slots */
2204 size = (size+StackSize-1) & ~(StackSize-1);
2205
2206 offset += size; /* Negative offset, increment before */
2207
2208 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002209 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2210 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002211 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002212
H. Peter Anvine2c80182005-01-15 22:15:51 +00002213 /* Now define the assign to setup the enter_c macro correctly */
2214 snprintf(directive, sizeof(directive),
2215 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002216 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002217
H. Peter Anvine2c80182005-01-15 22:15:51 +00002218 /* Move to the next argument in the list */
2219 tline = tline->next;
2220 if (tline && tline->type == TOK_WHITESPACE)
2221 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002222 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2223 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002224 free_tlist(origline);
2225 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002226
H. Peter Anvine2c80182005-01-15 22:15:51 +00002227 case PP_CLEAR:
2228 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002229 error(ERR_WARNING|ERR_PASS1,
2230 "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002231 free_macros();
2232 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002233 free_tlist(origline);
2234 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002235
H. Peter Anvin418ca702008-05-30 10:42:30 -07002236 case PP_DEPEND:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002237 t = tline->next = expand_smacro(tline->next);
2238 skip_white_(t);
2239 if (!t || (t->type != TOK_STRING &&
2240 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002241 error(ERR_NONFATAL, "`%%depend' expects a file name");
2242 free_tlist(origline);
2243 return DIRECTIVE_FOUND; /* but we did _something_ */
2244 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002245 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002246 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002247 "trailing garbage after `%%depend' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002248 p = t->text;
2249 if (t->type != TOK_INTERNAL_STRING)
2250 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002251 if (dephead && !in_list(*dephead, p)) {
2252 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2253 sl->next = NULL;
2254 strcpy(sl->str, p);
2255 *deptail = sl;
2256 deptail = &sl->next;
2257 }
2258 free_tlist(origline);
2259 return DIRECTIVE_FOUND;
2260
2261 case PP_INCLUDE:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002262 t = tline->next = expand_smacro(tline->next);
2263 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002264
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002265 if (!t || (t->type != TOK_STRING &&
2266 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002267 error(ERR_NONFATAL, "`%%include' expects a file name");
2268 free_tlist(origline);
2269 return DIRECTIVE_FOUND; /* but we did _something_ */
2270 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002271 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002272 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002273 "trailing garbage after `%%include' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002274 p = t->text;
2275 if (t->type != TOK_INTERNAL_STRING)
2276 nasm_unquote(p, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002277 inc = nasm_malloc(sizeof(Include));
2278 inc->next = istk;
2279 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002280 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002281 if (!inc->fp) {
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002282 /* -MG given but file not found */
2283 nasm_free(inc);
2284 } else {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002285 inc->fname = src_set_fname(nasm_strdup(p));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002286 inc->lineno = src_set_linnum(0);
2287 inc->lineinc = 1;
2288 inc->expansion = NULL;
2289 inc->mstk = NULL;
2290 istk = inc;
2291 list->uplevel(LIST_INCLUDE);
2292 }
2293 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002294 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002295
H. Peter Anvind2456592008-06-19 15:04:18 -07002296 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002297 {
H. Peter Anvina70547f2008-07-19 21:44:26 -07002298 static macros_t *use_pkg;
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002299 const char *pkg_macro;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002300
H. Peter Anvind2456592008-06-19 15:04:18 -07002301 t = tline->next = expand_smacro(tline->next);
2302 skip_white_(t);
2303
2304 if (!t || (t->type != TOK_STRING &&
2305 t->type != TOK_INTERNAL_STRING &&
2306 t->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002307 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002308 free_tlist(origline);
2309 return DIRECTIVE_FOUND; /* but we did _something_ */
2310 }
2311 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002312 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002313 "trailing garbage after `%%use' ignored");
H. Peter Anvind2456592008-06-19 15:04:18 -07002314 if (t->type == TOK_STRING)
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002315 nasm_unquote(t->text, NULL);
2316 use_pkg = nasm_stdmac_find_package(t->text);
2317 if (!use_pkg)
2318 error(ERR_NONFATAL, "unknown `%%use' package: %s", t->text);
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002319 /* The first string will be <%define>__USE_*__ */
H. Peter Anvin7e50d232008-06-25 14:54:14 -07002320 pkg_macro = (char *)use_pkg + 1;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002321 if (!smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2322 /* Not already included, go ahead and include it */
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002323 stdmacpos = use_pkg;
2324 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002325 free_tlist(origline);
2326 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002327 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002328 case PP_PUSH:
2329 tline = tline->next;
2330 skip_white_(tline);
2331 tline = expand_id(tline);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002332 if (tline) {
2333 if (!tok_type_(tline, TOK_ID)) {
2334 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2335 free_tlist(origline);
2336 return DIRECTIVE_FOUND; /* but we did _something_ */
2337 }
2338 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002339 error(ERR_WARNING|ERR_PASS1,
2340 "trailing garbage after `%%push' ignored");
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002341 p = nasm_strdup(tline->text);
2342 } else {
2343 p = NULL; /* Anonymous context */
2344 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002345 ctx = nasm_malloc(sizeof(Context));
2346 ctx->next = cstk;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002347 hash_init(&ctx->localmac, HASH_SMALL);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002348 ctx->name = p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002349 ctx->number = unique++;
2350 cstk = ctx;
2351 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002352 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002353
H. Peter Anvine2c80182005-01-15 22:15:51 +00002354 case PP_REPL:
2355 tline = tline->next;
2356 skip_white_(tline);
2357 tline = expand_id(tline);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002358 if (tline) {
2359 if (!tok_type_(tline, TOK_ID)) {
2360 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2361 free_tlist(origline);
2362 return DIRECTIVE_FOUND; /* but we did _something_ */
2363 }
2364 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002365 error(ERR_WARNING|ERR_PASS1,
2366 "trailing garbage after `%%repl' ignored");
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002367 p = nasm_strdup(tline->text);
2368 } else {
2369 p = NULL;
2370 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002371 if (!cstk)
2372 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2373 else {
2374 nasm_free(cstk->name);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002375 cstk->name = p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002376 }
2377 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002378 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002379
H. Peter Anvine2c80182005-01-15 22:15:51 +00002380 case PP_POP:
2381 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002382 error(ERR_WARNING|ERR_PASS1,
2383 "trailing garbage after `%%pop' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 if (!cstk)
2385 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2386 else
2387 ctx_pop();
2388 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002389 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002390
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002391 case PP_FATAL:
2392 severity = ERR_FATAL|ERR_NO_SEVERITY;
2393 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002394 case PP_ERROR:
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002395 severity = ERR_NONFATAL|ERR_NO_SEVERITY;
2396 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002397 case PP_WARNING:
H. Peter Anvin2f160432008-09-30 16:39:17 -07002398 severity = ERR_WARNING|ERR_NO_SEVERITY|ERR_WARN_USER;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002399 goto issue_error;
2400
2401 issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002402 {
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002403 /* Only error out if this is the final pass */
2404 if (pass != 2 && i != PP_FATAL)
2405 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002406
H. Peter Anvine2c80182005-01-15 22:15:51 +00002407 tline->next = expand_smacro(tline->next);
2408 tline = tline->next;
2409 skip_white_(tline);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002410 t = tline ? tline->next : NULL;
2411 skip_white_(t);
2412 if (tok_type_(tline, TOK_STRING) && !t) {
2413 /* The line contains only a quoted string */
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002414 p = tline->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002415 nasm_unquote(p, NULL);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002416 error(severity, "%s: %s", pp_directives[i], p);
2417 } else {
2418 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002419 p = detoken(tline, false);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002420 error(severity, "%s: %s", pp_directives[i], p);
2421 nasm_free(p);
H. Peter Anvind2456592008-06-19 15:04:18 -07002422 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002423 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002424 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002425 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002426
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002427 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002428 if (istk->conds && !emitting(istk->conds->state))
2429 j = COND_NEVER;
2430 else {
2431 j = if_condition(tline->next, i);
2432 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002433 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2434 }
2435 cond = nasm_malloc(sizeof(Cond));
2436 cond->next = istk->conds;
2437 cond->state = j;
2438 istk->conds = cond;
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002439 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002440 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002441
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002442 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002443 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002444 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002445 switch(istk->conds->state) {
2446 case COND_IF_TRUE:
2447 istk->conds->state = COND_DONE;
2448 break;
2449
2450 case COND_DONE:
2451 case COND_NEVER:
2452 break;
2453
2454 case COND_ELSE_TRUE:
2455 case COND_ELSE_FALSE:
H. Peter Anvin917a3492008-09-24 09:14:49 -07002456 error_precond(ERR_WARNING|ERR_PASS1,
2457 "`%%elif' after `%%else' ignored");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002458 istk->conds->state = COND_NEVER;
2459 break;
2460
2461 case COND_IF_FALSE:
2462 /*
2463 * IMPORTANT: In the case of %if, we will already have
2464 * called expand_mmac_params(); however, if we're
2465 * processing an %elif we must have been in a
2466 * non-emitting mode, which would have inhibited
H. Peter Anvin992fe752008-10-19 15:45:05 -07002467 * the normal invocation of expand_indirect() and
2468 * expand_mmac_params(). Therefore, we have to do it
2469 * explicitly here.
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002470 */
H. Peter Anvin992fe752008-10-19 15:45:05 -07002471 t = expand_indirect(tline->next,0);
2472 t = expand_mmac_params(t);
2473 j = if_condition(expand_mmac_params(t), i);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002474 tline->next = NULL; /* it got freed */
2475 istk->conds->state =
2476 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2477 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002478 }
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002479 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002480 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002481
H. Peter Anvine2c80182005-01-15 22:15:51 +00002482 case PP_ELSE:
2483 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002484 error_precond(ERR_WARNING|ERR_PASS1,
2485 "trailing garbage after `%%else' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002486 if (!istk->conds)
2487 error(ERR_FATAL, "`%%else': no matching `%%if'");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002488 switch(istk->conds->state) {
2489 case COND_IF_TRUE:
2490 case COND_DONE:
2491 istk->conds->state = COND_ELSE_FALSE;
2492 break;
2493
2494 case COND_NEVER:
2495 break;
2496
2497 case COND_IF_FALSE:
2498 istk->conds->state = COND_ELSE_TRUE;
2499 break;
2500
2501 case COND_ELSE_TRUE:
2502 case COND_ELSE_FALSE:
H. Peter Anvin917a3492008-09-24 09:14:49 -07002503 error_precond(ERR_WARNING|ERR_PASS1,
2504 "`%%else' after `%%else' ignored.");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002505 istk->conds->state = COND_NEVER;
2506 break;
2507 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002508 free_tlist(origline);
2509 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002510
H. Peter Anvine2c80182005-01-15 22:15:51 +00002511 case PP_ENDIF:
2512 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002513 error_precond(ERR_WARNING|ERR_PASS1,
2514 "trailing garbage after `%%endif' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002515 if (!istk->conds)
2516 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2517 cond = istk->conds;
2518 istk->conds = cond->next;
2519 nasm_free(cond);
2520 free_tlist(origline);
2521 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002522
H. Peter Anvine2c80182005-01-15 22:15:51 +00002523 case PP_MACRO:
2524 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002525 if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002526 error(ERR_FATAL,
2527 "`%%%smacro': already defining a macro",
2528 (i == PP_IMACRO ? "i" : ""));
H. Peter Anvina26433d2008-07-16 14:40:01 -07002529 return DIRECTIVE_FOUND;
2530 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002531 defining = nasm_malloc(sizeof(MMacro));
H. Peter Anvina26433d2008-07-16 14:40:01 -07002532 defining->casesense = (i == PP_MACRO);
2533 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2534 nasm_free(defining);
2535 defining = NULL;
2536 return DIRECTIVE_FOUND;
2537 }
2538
H. Peter Anvin166c2472008-05-28 12:28:58 -07002539 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002540 while (mmac) {
2541 if (!strcmp(mmac->name, defining->name) &&
2542 (mmac->nparam_min <= defining->nparam_max
2543 || defining->plus)
2544 && (defining->nparam_min <= mmac->nparam_max
2545 || mmac->plus)) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002546 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002547 "redefining multi-line macro `%s'", defining->name);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002548 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002549 }
2550 mmac = mmac->next;
2551 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002552 free_tlist(origline);
2553 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002554
H. Peter Anvine2c80182005-01-15 22:15:51 +00002555 case PP_ENDM:
2556 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002557 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002558 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2559 return DIRECTIVE_FOUND;
2560 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002561 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002562 defining->next = *mmhead;
2563 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002564 defining = NULL;
2565 free_tlist(origline);
2566 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002567
H. Peter Anvina26433d2008-07-16 14:40:01 -07002568 case PP_UNMACRO:
2569 case PP_UNIMACRO:
2570 {
2571 MMacro **mmac_p;
2572 MMacro spec;
2573
2574 spec.casesense = (i == PP_UNMACRO);
2575 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2576 return DIRECTIVE_FOUND;
2577 }
2578 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2579 while (mmac_p && *mmac_p) {
2580 mmac = *mmac_p;
2581 if (mmac->casesense == spec.casesense &&
2582 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2583 mmac->nparam_min == spec.nparam_min &&
2584 mmac->nparam_max == spec.nparam_max &&
2585 mmac->plus == spec.plus) {
2586 *mmac_p = mmac->next;
2587 free_mmacro(mmac);
2588 } else {
2589 mmac_p = &mmac->next;
2590 }
2591 }
2592 free_tlist(origline);
2593 free_tlist(spec.dlist);
2594 return DIRECTIVE_FOUND;
2595 }
2596
H. Peter Anvine2c80182005-01-15 22:15:51 +00002597 case PP_ROTATE:
2598 if (tline->next && tline->next->type == TOK_WHITESPACE)
2599 tline = tline->next;
2600 if (tline->next == NULL) {
2601 free_tlist(origline);
2602 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2603 return DIRECTIVE_FOUND;
2604 }
2605 t = expand_smacro(tline->next);
2606 tline->next = NULL;
2607 free_tlist(origline);
2608 tline = t;
2609 tptr = &t;
2610 tokval.t_type = TOKEN_INVALID;
2611 evalresult =
2612 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2613 free_tlist(tline);
2614 if (!evalresult)
2615 return DIRECTIVE_FOUND;
2616 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002617 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002618 "trailing garbage after expression ignored");
2619 if (!is_simple(evalresult)) {
2620 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2621 return DIRECTIVE_FOUND;
2622 }
2623 mmac = istk->mstk;
2624 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2625 mmac = mmac->next_active;
2626 if (!mmac) {
2627 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2628 } else if (mmac->nparam == 0) {
2629 error(ERR_NONFATAL,
2630 "`%%rotate' invoked within macro without parameters");
2631 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002632 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002633
H. Peter Anvin25a99342007-09-22 17:45:45 -07002634 rotate %= (int)mmac->nparam;
2635 if (rotate < 0)
2636 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002637
H. Peter Anvin25a99342007-09-22 17:45:45 -07002638 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002639 }
2640 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002641
H. Peter Anvine2c80182005-01-15 22:15:51 +00002642 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002643 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002644 do {
2645 tline = tline->next;
2646 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002647
H. Peter Anvine2c80182005-01-15 22:15:51 +00002648 if (tok_type_(tline, TOK_ID) &&
2649 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002650 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002651 do {
2652 tline = tline->next;
2653 } while (tok_type_(tline, TOK_WHITESPACE));
2654 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002655
H. Peter Anvine2c80182005-01-15 22:15:51 +00002656 if (tline) {
2657 t = expand_smacro(tline);
2658 tptr = &t;
2659 tokval.t_type = TOKEN_INVALID;
2660 evalresult =
2661 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2662 if (!evalresult) {
2663 free_tlist(origline);
2664 return DIRECTIVE_FOUND;
2665 }
2666 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002667 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002668 "trailing garbage after expression ignored");
2669 if (!is_simple(evalresult)) {
2670 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2671 return DIRECTIVE_FOUND;
2672 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002673 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002674 } else {
2675 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002676 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002677 }
2678 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002679
H. Peter Anvine2c80182005-01-15 22:15:51 +00002680 tmp_defining = defining;
2681 defining = nasm_malloc(sizeof(MMacro));
2682 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002683 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002684 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002685 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002686 defining->in_progress = count;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002687 defining->nparam_min = defining->nparam_max = 0;
2688 defining->defaults = NULL;
2689 defining->dlist = NULL;
2690 defining->expansion = NULL;
2691 defining->next_active = istk->mstk;
2692 defining->rep_nest = tmp_defining;
2693 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002694
H. Peter Anvine2c80182005-01-15 22:15:51 +00002695 case PP_ENDREP:
2696 if (!defining || defining->name) {
2697 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2698 return DIRECTIVE_FOUND;
2699 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002700
H. Peter Anvine2c80182005-01-15 22:15:51 +00002701 /*
2702 * Now we have a "macro" defined - although it has no name
2703 * and we won't be entering it in the hash tables - we must
2704 * push a macro-end marker for it on to istk->expansion.
2705 * After that, it will take care of propagating itself (a
2706 * macro-end marker line for a macro which is really a %rep
2707 * block will cause the macro to be re-expanded, complete
2708 * with another macro-end marker to ensure the process
2709 * continues) until the whole expansion is forcibly removed
2710 * from istk->expansion by a %exitrep.
2711 */
2712 l = nasm_malloc(sizeof(Line));
2713 l->next = istk->expansion;
2714 l->finishes = defining;
2715 l->first = NULL;
2716 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002717
H. Peter Anvine2c80182005-01-15 22:15:51 +00002718 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002719
H. Peter Anvine2c80182005-01-15 22:15:51 +00002720 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2721 tmp_defining = defining;
2722 defining = defining->rep_nest;
2723 free_tlist(origline);
2724 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002725
H. Peter Anvine2c80182005-01-15 22:15:51 +00002726 case PP_EXITREP:
2727 /*
2728 * We must search along istk->expansion until we hit a
2729 * macro-end marker for a macro with no name. Then we set
2730 * its `in_progress' flag to 0.
2731 */
2732 for (l = istk->expansion; l; l = l->next)
2733 if (l->finishes && !l->finishes->name)
H. Peter Anvinca348b62008-07-23 10:49:26 -04002734 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002735
H. Peter Anvine2c80182005-01-15 22:15:51 +00002736 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07002737 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002738 else
2739 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2740 free_tlist(origline);
2741 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002742
H. Peter Anvine2c80182005-01-15 22:15:51 +00002743 case PP_XDEFINE:
2744 case PP_IXDEFINE:
2745 case PP_DEFINE:
2746 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002747 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002748
H. Peter Anvine2c80182005-01-15 22:15:51 +00002749 tline = tline->next;
2750 skip_white_(tline);
2751 tline = expand_id(tline);
2752 if (!tline || (tline->type != TOK_ID &&
2753 (tline->type != TOK_PREPROC_ID ||
2754 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002755 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2756 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002757 free_tlist(origline);
2758 return DIRECTIVE_FOUND;
2759 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002760
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002761 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002762
H. Peter Anvine2c80182005-01-15 22:15:51 +00002763 mname = tline->text;
2764 last = tline;
2765 param_start = tline = tline->next;
2766 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002767
H. Peter Anvine2c80182005-01-15 22:15:51 +00002768 /* Expand the macro definition now for %xdefine and %ixdefine */
2769 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2770 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002771
H. Peter Anvine2c80182005-01-15 22:15:51 +00002772 if (tok_is_(tline, "(")) {
2773 /*
2774 * This macro has parameters.
2775 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002776
H. Peter Anvine2c80182005-01-15 22:15:51 +00002777 tline = tline->next;
2778 while (1) {
2779 skip_white_(tline);
2780 if (!tline) {
2781 error(ERR_NONFATAL, "parameter identifier expected");
2782 free_tlist(origline);
2783 return DIRECTIVE_FOUND;
2784 }
2785 if (tline->type != TOK_ID) {
2786 error(ERR_NONFATAL,
2787 "`%s': parameter identifier expected",
2788 tline->text);
2789 free_tlist(origline);
2790 return DIRECTIVE_FOUND;
2791 }
2792 tline->type = TOK_SMAC_PARAM + nparam++;
2793 tline = tline->next;
2794 skip_white_(tline);
2795 if (tok_is_(tline, ",")) {
2796 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04002797 } else {
2798 if (!tok_is_(tline, ")")) {
2799 error(ERR_NONFATAL,
2800 "`)' expected to terminate macro template");
2801 free_tlist(origline);
2802 return DIRECTIVE_FOUND;
2803 }
2804 break;
2805 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002806 }
2807 last = tline;
2808 tline = tline->next;
2809 }
2810 if (tok_type_(tline, TOK_WHITESPACE))
2811 last = tline, tline = tline->next;
2812 macro_start = NULL;
2813 last->next = NULL;
2814 t = tline;
2815 while (t) {
2816 if (t->type == TOK_ID) {
2817 for (tt = param_start; tt; tt = tt->next)
2818 if (tt->type >= TOK_SMAC_PARAM &&
2819 !strcmp(tt->text, t->text))
2820 t->type = tt->type;
2821 }
2822 tt = t->next;
2823 t->next = macro_start;
2824 macro_start = t;
2825 t = tt;
2826 }
2827 /*
2828 * Good. We now have a macro name, a parameter count, and a
2829 * token list (in reverse order) for an expansion. We ought
2830 * to be OK just to create an SMacro, store it, and let
2831 * free_tlist have the rest of the line (which we have
2832 * carefully re-terminated after chopping off the expansion
2833 * from the end).
2834 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002835 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002836 free_tlist(origline);
2837 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002838
H. Peter Anvine2c80182005-01-15 22:15:51 +00002839 case PP_UNDEF:
2840 tline = tline->next;
2841 skip_white_(tline);
2842 tline = expand_id(tline);
2843 if (!tline || (tline->type != TOK_ID &&
2844 (tline->type != TOK_PREPROC_ID ||
2845 tline->text[1] != '$'))) {
2846 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2847 free_tlist(origline);
2848 return DIRECTIVE_FOUND;
2849 }
2850 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002851 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002852 "trailing garbage after macro name ignored");
2853 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002854
H. Peter Anvine2c80182005-01-15 22:15:51 +00002855 /* Find the context that symbol belongs to */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002856 ctx = get_ctx(tline->text, false);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002857 undef_smacro(ctx, tline->text);
2858 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002859 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002860
H. Peter Anvin9e200162008-06-04 17:23:14 -07002861 case PP_DEFSTR:
2862 case PP_IDEFSTR:
2863 casesense = (i == PP_DEFSTR);
2864
2865 tline = tline->next;
2866 skip_white_(tline);
2867 tline = expand_id(tline);
2868 if (!tline || (tline->type != TOK_ID &&
2869 (tline->type != TOK_PREPROC_ID ||
2870 tline->text[1] != '$'))) {
2871 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2872 pp_directives[i]);
2873 free_tlist(origline);
2874 return DIRECTIVE_FOUND;
2875 }
2876
2877 ctx = get_ctx(tline->text, false);
2878
2879 mname = tline->text;
2880 last = tline;
2881 tline = expand_smacro(tline->next);
2882 last->next = NULL;
2883
2884 while (tok_type_(tline, TOK_WHITESPACE))
2885 tline = delete_Token(tline);
2886
2887 p = detoken(tline, false);
2888 macro_start = nasm_malloc(sizeof(*macro_start));
2889 macro_start->next = NULL;
2890 macro_start->text = nasm_quote(p, strlen(p));
2891 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002892 macro_start->a.mac = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07002893 nasm_free(p);
2894
2895 /*
2896 * We now have a macro name, an implicit parameter count of
2897 * zero, and a string token to use as an expansion. Create
2898 * and store an SMacro.
2899 */
2900 define_smacro(ctx, mname, casesense, 0, macro_start);
2901 free_tlist(origline);
2902 return DIRECTIVE_FOUND;
2903
H. Peter Anvin418ca702008-05-30 10:42:30 -07002904 case PP_PATHSEARCH:
2905 {
2906 FILE *fp;
2907 StrList *xsl = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002908 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002909
2910 casesense = true;
2911
2912 tline = tline->next;
2913 skip_white_(tline);
2914 tline = expand_id(tline);
2915 if (!tline || (tline->type != TOK_ID &&
2916 (tline->type != TOK_PREPROC_ID ||
2917 tline->text[1] != '$'))) {
2918 error(ERR_NONFATAL,
2919 "`%%pathsearch' expects a macro identifier as first parameter");
2920 free_tlist(origline);
2921 return DIRECTIVE_FOUND;
2922 }
2923 ctx = get_ctx(tline->text, false);
2924
2925 mname = tline->text;
2926 last = tline;
2927 tline = expand_smacro(tline->next);
2928 last->next = NULL;
2929
2930 t = tline;
2931 while (tok_type_(t, TOK_WHITESPACE))
2932 t = t->next;
2933
2934 if (!t || (t->type != TOK_STRING &&
2935 t->type != TOK_INTERNAL_STRING)) {
2936 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
2937 free_tlist(tline);
2938 free_tlist(origline);
2939 return DIRECTIVE_FOUND; /* but we did _something_ */
2940 }
2941 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002942 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002943 "trailing garbage after `%%pathsearch' ignored");
H. Peter Anvin427cc912008-06-01 21:43:03 -07002944 p = t->text;
2945 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002946 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002947
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002948 fp = inc_fopen(p, &xsl, &xst, true);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002949 if (fp) {
2950 p = xsl->str;
2951 fclose(fp); /* Don't actually care about the file */
2952 }
2953 macro_start = nasm_malloc(sizeof(*macro_start));
2954 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002955 macro_start->text = nasm_quote(p, strlen(p));
H. Peter Anvin418ca702008-05-30 10:42:30 -07002956 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002957 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002958 if (xsl)
2959 nasm_free(xsl);
2960
2961 /*
2962 * We now have a macro name, an implicit parameter count of
2963 * zero, and a string token to use as an expansion. Create
2964 * and store an SMacro.
2965 */
2966 define_smacro(ctx, mname, casesense, 0, macro_start);
2967 free_tlist(tline);
2968 free_tlist(origline);
2969 return DIRECTIVE_FOUND;
2970 }
2971
H. Peter Anvine2c80182005-01-15 22:15:51 +00002972 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002973 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07002974
H. Peter Anvine2c80182005-01-15 22:15:51 +00002975 tline = tline->next;
2976 skip_white_(tline);
2977 tline = expand_id(tline);
2978 if (!tline || (tline->type != TOK_ID &&
2979 (tline->type != TOK_PREPROC_ID ||
2980 tline->text[1] != '$'))) {
2981 error(ERR_NONFATAL,
2982 "`%%strlen' expects a macro identifier as first parameter");
2983 free_tlist(origline);
2984 return DIRECTIVE_FOUND;
2985 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002986 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002987
H. Peter Anvine2c80182005-01-15 22:15:51 +00002988 mname = tline->text;
2989 last = tline;
2990 tline = expand_smacro(tline->next);
2991 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002992
H. Peter Anvine2c80182005-01-15 22:15:51 +00002993 t = tline;
2994 while (tok_type_(t, TOK_WHITESPACE))
2995 t = t->next;
2996 /* t should now point to the string */
2997 if (t->type != TOK_STRING) {
2998 error(ERR_NONFATAL,
2999 "`%%strlen` requires string as second parameter");
3000 free_tlist(tline);
3001 free_tlist(origline);
3002 return DIRECTIVE_FOUND;
3003 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003004
H. Peter Anvine2c80182005-01-15 22:15:51 +00003005 macro_start = nasm_malloc(sizeof(*macro_start));
3006 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003007 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003008 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003009
H. Peter Anvine2c80182005-01-15 22:15:51 +00003010 /*
3011 * We now have a macro name, an implicit parameter count of
3012 * zero, and a numeric token to use as an expansion. Create
3013 * and store an SMacro.
3014 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003015 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003016 free_tlist(tline);
3017 free_tlist(origline);
3018 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003019
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003020 case PP_STRCAT:
3021 casesense = true;
3022
3023 tline = tline->next;
3024 skip_white_(tline);
3025 tline = expand_id(tline);
3026 if (!tline || (tline->type != TOK_ID &&
3027 (tline->type != TOK_PREPROC_ID ||
3028 tline->text[1] != '$'))) {
3029 error(ERR_NONFATAL,
3030 "`%%strcat' expects a macro identifier as first parameter");
3031 free_tlist(origline);
3032 return DIRECTIVE_FOUND;
3033 }
3034 ctx = get_ctx(tline->text, false);
3035
3036 mname = tline->text;
3037 last = tline;
3038 tline = expand_smacro(tline->next);
3039 last->next = NULL;
3040
3041 len = 0;
3042 for (t = tline; t; t = t->next) {
3043 switch (t->type) {
3044 case TOK_WHITESPACE:
3045 break;
3046 case TOK_STRING:
3047 len += t->a.len = nasm_unquote(t->text, NULL);
3048 break;
H. Peter Anvinaccf4332008-07-01 21:42:08 -07003049 case TOK_OTHER:
3050 if (!strcmp(t->text, ",")) /* permit comma separators */
3051 break;
3052 /* else fall through */
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003053 default:
3054 error(ERR_NONFATAL,
3055 "non-string passed to `%%strcat' (%d)", t->type);
3056 free_tlist(tline);
3057 free_tlist(origline);
3058 return DIRECTIVE_FOUND;
3059 }
3060 }
3061
3062 p = pp = nasm_malloc(len);
3063 t = tline;
3064 for (t = tline; t; t = t->next) {
3065 if (t->type == TOK_STRING) {
3066 memcpy(p, t->text, t->a.len);
3067 p += t->a.len;
3068 }
3069 }
3070
3071 /*
3072 * We now have a macro name, an implicit parameter count of
3073 * zero, and a numeric token to use as an expansion. Create
3074 * and store an SMacro.
3075 */
3076 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3077 macro_start->text = nasm_quote(pp, len);
3078 nasm_free(pp);
3079 define_smacro(ctx, mname, casesense, 0, macro_start);
3080 free_tlist(tline);
3081 free_tlist(origline);
3082 return DIRECTIVE_FOUND;
3083
H. Peter Anvine2c80182005-01-15 22:15:51 +00003084 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003085 {
3086 int64_t a1, a2;
3087 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003088
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003089 casesense = true;
3090
H. Peter Anvine2c80182005-01-15 22:15:51 +00003091 tline = tline->next;
3092 skip_white_(tline);
3093 tline = expand_id(tline);
3094 if (!tline || (tline->type != TOK_ID &&
3095 (tline->type != TOK_PREPROC_ID ||
3096 tline->text[1] != '$'))) {
3097 error(ERR_NONFATAL,
3098 "`%%substr' expects a macro identifier as first parameter");
3099 free_tlist(origline);
3100 return DIRECTIVE_FOUND;
3101 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003102 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003103
H. Peter Anvine2c80182005-01-15 22:15:51 +00003104 mname = tline->text;
3105 last = tline;
3106 tline = expand_smacro(tline->next);
3107 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003108
H. Peter Anvine2c80182005-01-15 22:15:51 +00003109 t = tline->next;
3110 while (tok_type_(t, TOK_WHITESPACE))
3111 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003112
H. Peter Anvine2c80182005-01-15 22:15:51 +00003113 /* t should now point to the string */
3114 if (t->type != TOK_STRING) {
3115 error(ERR_NONFATAL,
3116 "`%%substr` requires string as second parameter");
3117 free_tlist(tline);
3118 free_tlist(origline);
3119 return DIRECTIVE_FOUND;
3120 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003121
H. Peter Anvine2c80182005-01-15 22:15:51 +00003122 tt = t->next;
3123 tptr = &tt;
3124 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003125 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3126 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003127 if (!evalresult) {
3128 free_tlist(tline);
3129 free_tlist(origline);
3130 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003131 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003132 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3133 free_tlist(tline);
3134 free_tlist(origline);
3135 return DIRECTIVE_FOUND;
3136 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003137 a1 = evalresult->value-1;
3138
3139 while (tok_type_(tt, TOK_WHITESPACE))
3140 tt = tt->next;
3141 if (!tt) {
3142 a2 = 1; /* Backwards compatibility: one character */
3143 } else {
3144 tokval.t_type = TOKEN_INVALID;
3145 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3146 pass, error, NULL);
3147 if (!evalresult) {
3148 free_tlist(tline);
3149 free_tlist(origline);
3150 return DIRECTIVE_FOUND;
3151 } else if (!is_simple(evalresult)) {
3152 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3153 free_tlist(tline);
3154 free_tlist(origline);
3155 return DIRECTIVE_FOUND;
3156 }
3157 a2 = evalresult->value;
3158 }
3159
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003160 len = nasm_unquote(t->text, NULL);
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003161 if (a2 < 0)
3162 a2 = a2+1+len-a1;
3163 if (a1+a2 > (int64_t)len)
3164 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003165
H. Peter Anvine2c80182005-01-15 22:15:51 +00003166 macro_start = nasm_malloc(sizeof(*macro_start));
3167 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003168 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003169 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003170 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003171
H. Peter Anvine2c80182005-01-15 22:15:51 +00003172 /*
3173 * We now have a macro name, an implicit parameter count of
3174 * zero, and a numeric token to use as an expansion. Create
3175 * and store an SMacro.
3176 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003177 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003178 free_tlist(tline);
3179 free_tlist(origline);
3180 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003181 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003182
H. Peter Anvine2c80182005-01-15 22:15:51 +00003183 case PP_ASSIGN:
3184 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003185 casesense = (i == PP_ASSIGN);
3186
H. Peter Anvine2c80182005-01-15 22:15:51 +00003187 tline = tline->next;
3188 skip_white_(tline);
3189 tline = expand_id(tline);
3190 if (!tline || (tline->type != TOK_ID &&
3191 (tline->type != TOK_PREPROC_ID ||
3192 tline->text[1] != '$'))) {
3193 error(ERR_NONFATAL,
3194 "`%%%sassign' expects a macro identifier",
3195 (i == PP_IASSIGN ? "i" : ""));
3196 free_tlist(origline);
3197 return DIRECTIVE_FOUND;
3198 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003199 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003200
H. Peter Anvine2c80182005-01-15 22:15:51 +00003201 mname = tline->text;
3202 last = tline;
3203 tline = expand_smacro(tline->next);
3204 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003205
H. Peter Anvine2c80182005-01-15 22:15:51 +00003206 t = tline;
3207 tptr = &t;
3208 tokval.t_type = TOKEN_INVALID;
3209 evalresult =
3210 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3211 free_tlist(tline);
3212 if (!evalresult) {
3213 free_tlist(origline);
3214 return DIRECTIVE_FOUND;
3215 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003216
H. Peter Anvine2c80182005-01-15 22:15:51 +00003217 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003218 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003219 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003220
H. Peter Anvine2c80182005-01-15 22:15:51 +00003221 if (!is_simple(evalresult)) {
3222 error(ERR_NONFATAL,
3223 "non-constant value given to `%%%sassign'",
3224 (i == PP_IASSIGN ? "i" : ""));
3225 free_tlist(origline);
3226 return DIRECTIVE_FOUND;
3227 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003228
H. Peter Anvine2c80182005-01-15 22:15:51 +00003229 macro_start = nasm_malloc(sizeof(*macro_start));
3230 macro_start->next = NULL;
3231 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003232 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003233
H. Peter Anvine2c80182005-01-15 22:15:51 +00003234 /*
3235 * We now have a macro name, an implicit parameter count of
3236 * zero, and a numeric token to use as an expansion. Create
3237 * and store an SMacro.
3238 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003239 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003240 free_tlist(origline);
3241 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003242
H. Peter Anvine2c80182005-01-15 22:15:51 +00003243 case PP_LINE:
3244 /*
3245 * Syntax is `%line nnn[+mmm] [filename]'
3246 */
3247 tline = tline->next;
3248 skip_white_(tline);
3249 if (!tok_type_(tline, TOK_NUMBER)) {
3250 error(ERR_NONFATAL, "`%%line' expects line number");
3251 free_tlist(origline);
3252 return DIRECTIVE_FOUND;
3253 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003254 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003255 m = 1;
3256 tline = tline->next;
3257 if (tok_is_(tline, "+")) {
3258 tline = tline->next;
3259 if (!tok_type_(tline, TOK_NUMBER)) {
3260 error(ERR_NONFATAL, "`%%line' expects line increment");
3261 free_tlist(origline);
3262 return DIRECTIVE_FOUND;
3263 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003264 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003265 tline = tline->next;
3266 }
3267 skip_white_(tline);
3268 src_set_linnum(k);
3269 istk->lineinc = m;
3270 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003271 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003272 }
3273 free_tlist(origline);
3274 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003275
H. Peter Anvine2c80182005-01-15 22:15:51 +00003276 default:
3277 error(ERR_FATAL,
3278 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003279 pp_directives[i]);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003280 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003281 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003282}
3283
3284/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003285 * Ensure that a macro parameter contains a condition code and
3286 * nothing else. Return the condition code index if so, or -1
3287 * otherwise.
3288 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003289static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003290{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003291 Token *tt;
3292 int i, j, k, m;
3293
H. Peter Anvin25a99342007-09-22 17:45:45 -07003294 if (!t)
3295 return -1; /* Probably a %+ without a space */
3296
H. Peter Anvineba20a72002-04-30 20:53:55 +00003297 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003298 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003299 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003300 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003301 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003302 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003303 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003304
3305 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00003306 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003307 while (j - i > 1) {
3308 k = (j + i) / 2;
3309 m = nasm_stricmp(t->text, conditions[k]);
3310 if (m == 0) {
3311 i = k;
3312 j = -2;
3313 break;
3314 } else if (m < 0) {
3315 j = k;
3316 } else
3317 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003318 }
3319 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003320 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003321 return i;
3322}
3323
3324/*
3325 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3326 * %-n) and MMacro-local identifiers (%%foo).
3327 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003328static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003329{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003330 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003331
3332 tail = &thead;
3333 thead = NULL;
3334
H. Peter Anvine2c80182005-01-15 22:15:51 +00003335 while (tline) {
3336 if (tline->type == TOK_PREPROC_ID &&
3337 (((tline->text[1] == '+' || tline->text[1] == '-')
3338 && tline->text[2]) || tline->text[1] == '%'
3339 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003340 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003341 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003342 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003343 unsigned int n;
3344 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003345 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003346
H. Peter Anvine2c80182005-01-15 22:15:51 +00003347 t = tline;
3348 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003349
H. Peter Anvine2c80182005-01-15 22:15:51 +00003350 mac = istk->mstk;
3351 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3352 mac = mac->next_active;
3353 if (!mac)
3354 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3355 else
3356 switch (t->text[1]) {
3357 /*
3358 * We have to make a substitution of one of the
3359 * forms %1, %-1, %+1, %%foo, %0.
3360 */
3361 case '0':
3362 type = TOK_NUMBER;
3363 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3364 text = nasm_strdup(tmpbuf);
3365 break;
3366 case '%':
3367 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003368 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00003369 mac->unique);
3370 text = nasm_strcat(tmpbuf, t->text + 2);
3371 break;
3372 case '-':
3373 n = atoi(t->text + 2) - 1;
3374 if (n >= mac->nparam)
3375 tt = NULL;
3376 else {
3377 if (mac->nparam > 1)
3378 n = (n + mac->rotate) % mac->nparam;
3379 tt = mac->params[n];
3380 }
3381 cc = find_cc(tt);
3382 if (cc == -1) {
3383 error(ERR_NONFATAL,
3384 "macro parameter %d is not a condition code",
3385 n + 1);
3386 text = NULL;
3387 } else {
3388 type = TOK_ID;
3389 if (inverse_ccs[cc] == -1) {
3390 error(ERR_NONFATAL,
3391 "condition code `%s' is not invertible",
3392 conditions[cc]);
3393 text = NULL;
3394 } else
3395 text =
3396 nasm_strdup(conditions[inverse_ccs[cc]]);
3397 }
3398 break;
3399 case '+':
3400 n = atoi(t->text + 2) - 1;
3401 if (n >= mac->nparam)
3402 tt = NULL;
3403 else {
3404 if (mac->nparam > 1)
3405 n = (n + mac->rotate) % mac->nparam;
3406 tt = mac->params[n];
3407 }
3408 cc = find_cc(tt);
3409 if (cc == -1) {
3410 error(ERR_NONFATAL,
3411 "macro parameter %d is not a condition code",
3412 n + 1);
3413 text = NULL;
3414 } else {
3415 type = TOK_ID;
3416 text = nasm_strdup(conditions[cc]);
3417 }
3418 break;
3419 default:
3420 n = atoi(t->text + 1) - 1;
3421 if (n >= mac->nparam)
3422 tt = NULL;
3423 else {
3424 if (mac->nparam > 1)
3425 n = (n + mac->rotate) % mac->nparam;
3426 tt = mac->params[n];
3427 }
3428 if (tt) {
3429 for (i = 0; i < mac->paramlen[n]; i++) {
3430 *tail = new_Token(NULL, tt->type, tt->text, 0);
3431 tail = &(*tail)->next;
3432 tt = tt->next;
3433 }
3434 }
3435 text = NULL; /* we've done it here */
3436 break;
3437 }
3438 if (!text) {
3439 delete_Token(t);
3440 } else {
3441 *tail = t;
3442 tail = &t->next;
3443 t->type = type;
3444 nasm_free(t->text);
3445 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003446 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003447 }
3448 continue;
3449 } else {
3450 t = *tail = tline;
3451 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003452 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003453 tail = &t->next;
3454 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003455 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003456 *tail = NULL;
3457 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003458 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003459 switch (t->type) {
3460 case TOK_WHITESPACE:
3461 if (tt->type == TOK_WHITESPACE) {
3462 t->next = delete_Token(tt);
3463 }
3464 break;
3465 case TOK_ID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003466 case TOK_NUMBER:
H. Peter Anvin992fe752008-10-19 15:45:05 -07003467 if (tt->type == t->type || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003468 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003469 nasm_free(t->text);
3470 t->text = tmp;
3471 t->next = delete_Token(tt);
3472 }
3473 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003474 default:
3475 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003476 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003477
H. Peter Anvin76690a12002-04-30 20:52:49 +00003478 return thead;
3479}
3480
3481/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003482 * Expand all single-line macro calls made in the given line.
3483 * Return the expanded version of the line. The original is deemed
3484 * to be destroyed in the process. (In reality we'll just move
3485 * Tokens from input to output a lot of the time, rather than
3486 * actually bothering to destroy and replicate.)
3487 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003488#define DEADMAN_LIMIT (1 << 20)
3489
H. Peter Anvine2c80182005-01-15 22:15:51 +00003490static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003491{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003492 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003493 struct hash_table *smtbl;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003494 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003495 Token **params;
3496 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003497 unsigned int nparam, sparam;
3498 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003499 Token *org_tline = tline;
3500 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003501 char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003502 int deadman = DEADMAN_LIMIT;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003503
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003504 /*
3505 * Trick: we should avoid changing the start token pointer since it can
3506 * be contained in "next" field of other token. Because of this
3507 * we allocate a copy of first token and work with it; at the end of
3508 * routine we copy it back
3509 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003510 if (org_tline) {
3511 tline =
3512 new_Token(org_tline->next, org_tline->type, org_tline->text,
3513 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003514 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003515 nasm_free(org_tline->text);
3516 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003517 }
3518
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003519again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003520 tail = &thead;
3521 thead = NULL;
3522
H. Peter Anvine2c80182005-01-15 22:15:51 +00003523 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003524 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003525 error(ERR_NONFATAL, "interminable macro recursion");
3526 break;
3527 }
3528
H. Peter Anvine2c80182005-01-15 22:15:51 +00003529 if ((mname = tline->text)) {
3530 /* if this token is a local macro, look in local context */
H. Peter Anvinb037a672008-05-29 19:08:08 -07003531 ctx = NULL;
3532 smtbl = &smacros;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003533 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003534 ctx = get_ctx(mname, true);
H. Peter Anvinb037a672008-05-29 19:08:08 -07003535 if (ctx)
3536 smtbl = &ctx->localmac;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003537 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07003538 head = (SMacro *) hash_findix(smtbl, mname);
H. Peter Anvin072771e2008-05-22 13:17:51 -07003539
H. Peter Anvine2c80182005-01-15 22:15:51 +00003540 /*
3541 * We've hit an identifier. As in is_mmacro below, we first
3542 * check whether the identifier is a single-line macro at
3543 * all, then think about checking for parameters if
3544 * necessary.
3545 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003546 for (m = head; m; m = m->next)
3547 if (!mstrcmp(m->name, mname, m->casesense))
3548 break;
3549 if (m) {
3550 mstart = tline;
3551 params = NULL;
3552 paramsize = NULL;
3553 if (m->nparam == 0) {
3554 /*
3555 * Simple case: the macro is parameterless. Discard the
3556 * one token that the macro call took, and push the
3557 * expansion back on the to-do stack.
3558 */
3559 if (!m->expansion) {
3560 if (!strcmp("__FILE__", m->name)) {
3561 int32_t num = 0;
H. Peter Anvin932de6c2008-07-31 18:46:11 -07003562 char *file = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003563 src_get(&num, &file);
3564 tline->text = nasm_quote(file, strlen(file));
H. Peter Anvin97a23472007-09-16 17:57:25 -07003565 tline->type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003566 nasm_free(file);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003567 continue;
3568 }
3569 if (!strcmp("__LINE__", m->name)) {
3570 nasm_free(tline->text);
3571 make_tok_num(tline, src_get_linnum());
3572 continue;
3573 }
3574 if (!strcmp("__BITS__", m->name)) {
3575 nasm_free(tline->text);
3576 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003577 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003578 }
3579 tline = delete_Token(tline);
3580 continue;
3581 }
3582 } else {
3583 /*
3584 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003585 * exists and takes parameters. We must find the
3586 * parameters in the call, count them, find the SMacro
3587 * that corresponds to that form of the macro call, and
3588 * substitute for the parameters when we expand. What a
3589 * pain.
3590 */
3591 /*tline = tline->next;
3592 skip_white_(tline); */
3593 do {
3594 t = tline->next;
3595 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003596 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003597 t->text = NULL;
3598 t = tline->next = delete_Token(t);
3599 }
3600 tline = t;
3601 } while (tok_type_(tline, TOK_WHITESPACE));
3602 if (!tok_is_(tline, "(")) {
3603 /*
3604 * This macro wasn't called with parameters: ignore
3605 * the call. (Behaviour borrowed from gnu cpp.)
3606 */
3607 tline = mstart;
3608 m = NULL;
3609 } else {
3610 int paren = 0;
3611 int white = 0;
3612 brackets = 0;
3613 nparam = 0;
3614 sparam = PARAM_DELTA;
3615 params = nasm_malloc(sparam * sizeof(Token *));
3616 params[0] = tline->next;
3617 paramsize = nasm_malloc(sparam * sizeof(int));
3618 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003619 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003620 /*
3621 * For some unusual expansions
3622 * which concatenates function call
3623 */
3624 t = tline->next;
3625 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003626 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003627 t->text = NULL;
3628 t = tline->next = delete_Token(t);
3629 }
3630 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003631
H. Peter Anvine2c80182005-01-15 22:15:51 +00003632 if (!tline) {
3633 error(ERR_NONFATAL,
3634 "macro call expects terminating `)'");
3635 break;
3636 }
3637 if (tline->type == TOK_WHITESPACE
3638 && brackets <= 0) {
3639 if (paramsize[nparam])
3640 white++;
3641 else
3642 params[nparam] = tline->next;
3643 continue; /* parameter loop */
3644 }
3645 if (tline->type == TOK_OTHER
3646 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003647 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003648 if (ch == ',' && !paren && brackets <= 0) {
3649 if (++nparam >= sparam) {
3650 sparam += PARAM_DELTA;
3651 params = nasm_realloc(params,
3652 sparam *
3653 sizeof(Token
3654 *));
3655 paramsize =
3656 nasm_realloc(paramsize,
3657 sparam *
3658 sizeof(int));
3659 }
3660 params[nparam] = tline->next;
3661 paramsize[nparam] = 0;
3662 white = 0;
3663 continue; /* parameter loop */
3664 }
3665 if (ch == '{' &&
3666 (brackets > 0 || (brackets == 0 &&
3667 !paramsize[nparam])))
3668 {
3669 if (!(brackets++)) {
3670 params[nparam] = tline->next;
3671 continue; /* parameter loop */
3672 }
3673 }
3674 if (ch == '}' && brackets > 0)
3675 if (--brackets == 0) {
3676 brackets = -1;
3677 continue; /* parameter loop */
3678 }
3679 if (ch == '(' && !brackets)
3680 paren++;
3681 if (ch == ')' && brackets <= 0)
3682 if (--paren < 0)
3683 break;
3684 }
3685 if (brackets < 0) {
3686 brackets = 0;
3687 error(ERR_NONFATAL, "braces do not "
3688 "enclose all of macro parameter");
3689 }
3690 paramsize[nparam] += white + 1;
3691 white = 0;
3692 } /* parameter loop */
3693 nparam++;
3694 while (m && (m->nparam != nparam ||
3695 mstrcmp(m->name, mname,
3696 m->casesense)))
3697 m = m->next;
3698 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003699 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003700 "macro `%s' exists, "
3701 "but not taking %d parameters",
3702 mstart->text, nparam);
3703 }
3704 }
3705 if (m && m->in_progress)
3706 m = NULL;
3707 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003708 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003709 * Design question: should we handle !tline, which
3710 * indicates missing ')' here, or expand those
3711 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003712 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003713 */
3714 nasm_free(params);
3715 nasm_free(paramsize);
3716 tline = mstart;
3717 } else {
3718 /*
3719 * Expand the macro: we are placed on the last token of the
3720 * call, so that we can easily split the call from the
3721 * following tokens. We also start by pushing an SMAC_END
3722 * token for the cycle removal.
3723 */
3724 t = tline;
3725 if (t) {
3726 tline = t->next;
3727 t->next = NULL;
3728 }
3729 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003730 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003731 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003732 tline = tt;
3733 for (t = m->expansion; t; t = t->next) {
3734 if (t->type >= TOK_SMAC_PARAM) {
3735 Token *pcopy = tline, **ptail = &pcopy;
3736 Token *ttt, *pt;
3737 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003738
H. Peter Anvine2c80182005-01-15 22:15:51 +00003739 ttt = params[t->type - TOK_SMAC_PARAM];
3740 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3741 --i >= 0;) {
3742 pt = *ptail =
3743 new_Token(tline, ttt->type, ttt->text,
3744 0);
3745 ptail = &pt->next;
3746 ttt = ttt->next;
3747 }
3748 tline = pcopy;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -07003749 } else if (t->type == TOK_PREPROC_Q) {
3750 tt = new_Token(tline, TOK_ID, mname, 0);
3751 tline = tt;
3752 } else if (t->type == TOK_PREPROC_QQ) {
3753 tt = new_Token(tline, TOK_ID, m->name, 0);
3754 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003755 } else {
3756 tt = new_Token(tline, t->type, t->text, 0);
3757 tline = tt;
3758 }
3759 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003760
H. Peter Anvine2c80182005-01-15 22:15:51 +00003761 /*
3762 * Having done that, get rid of the macro call, and clean
3763 * up the parameters.
3764 */
3765 nasm_free(params);
3766 nasm_free(paramsize);
3767 free_tlist(mstart);
3768 continue; /* main token loop */
3769 }
3770 }
3771 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003772
H. Peter Anvine2c80182005-01-15 22:15:51 +00003773 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003774 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003775 tline = delete_Token(tline);
3776 } else {
3777 t = *tail = tline;
3778 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003779 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003780 t->next = NULL;
3781 tail = &t->next;
3782 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003783 }
3784
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003785 /*
3786 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003787 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003788 * TOK_IDs should be concatenated.
3789 * Also we look for %+ tokens and concatenate the tokens before and after
3790 * them (without white spaces in between).
3791 */
3792 t = thead;
3793 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003794 while (t) {
3795 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3796 t = t->next;
3797 if (!t || !t->next)
3798 break;
3799 if (t->next->type == TOK_ID ||
3800 t->next->type == TOK_PREPROC_ID ||
3801 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003802 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003803 nasm_free(t->text);
3804 t->next = delete_Token(t->next);
3805 t->text = p;
3806 rescan = 1;
3807 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3808 t->next->next->type == TOK_PREPROC_ID &&
3809 strcmp(t->next->next->text, "%+") == 0) {
3810 /* free the next whitespace, the %+ token and next whitespace */
3811 int i;
3812 for (i = 1; i <= 3; i++) {
3813 if (!t->next
3814 || (i != 2 && t->next->type != TOK_WHITESPACE))
3815 break;
3816 t->next = delete_Token(t->next);
3817 } /* endfor */
3818 } else
3819 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003820 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003821 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003822 if (rescan) {
3823 tline = thead;
3824 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003825 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003826
H. Peter Anvine2c80182005-01-15 22:15:51 +00003827 if (org_tline) {
3828 if (thead) {
3829 *org_tline = *thead;
3830 /* since we just gave text to org_line, don't free it */
3831 thead->text = NULL;
3832 delete_Token(thead);
3833 } else {
3834 /* the expression expanded to empty line;
3835 we can't return NULL for some reasons
3836 we just set the line to a single WHITESPACE token. */
3837 memset(org_tline, 0, sizeof(*org_tline));
3838 org_tline->text = NULL;
3839 org_tline->type = TOK_WHITESPACE;
3840 }
3841 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003842 }
3843
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003844 return thead;
3845}
3846
3847/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003848 * Similar to expand_smacro but used exclusively with macro identifiers
3849 * right before they are fetched in. The reason is that there can be
3850 * identifiers consisting of several subparts. We consider that if there
3851 * are more than one element forming the name, user wants a expansion,
3852 * otherwise it will be left as-is. Example:
3853 *
3854 * %define %$abc cde
3855 *
3856 * the identifier %$abc will be left as-is so that the handler for %define
3857 * will suck it and define the corresponding value. Other case:
3858 *
3859 * %define _%$abc cde
3860 *
3861 * In this case user wants name to be expanded *before* %define starts
3862 * working, so we'll expand %$abc into something (if it has a value;
3863 * otherwise it will be left as-is) then concatenate all successive
3864 * PP_IDs into one.
3865 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003866static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003867{
3868 Token *cur, *oldnext = NULL;
3869
H. Peter Anvin734b1882002-04-30 21:01:08 +00003870 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003871 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003872
3873 cur = tline;
3874 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003875 (cur->next->type == TOK_ID ||
3876 cur->next->type == TOK_PREPROC_ID
3877 || cur->next->type == TOK_NUMBER))
3878 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003879
3880 /* If identifier consists of just one token, don't expand */
3881 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003882 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003883
H. Peter Anvine2c80182005-01-15 22:15:51 +00003884 if (cur) {
3885 oldnext = cur->next; /* Detach the tail past identifier */
3886 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003887 }
3888
H. Peter Anvin734b1882002-04-30 21:01:08 +00003889 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003890
H. Peter Anvine2c80182005-01-15 22:15:51 +00003891 if (cur) {
3892 /* expand_smacro possibly changhed tline; re-scan for EOL */
3893 cur = tline;
3894 while (cur && cur->next)
3895 cur = cur->next;
3896 if (cur)
3897 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003898 }
3899
3900 return tline;
3901}
3902
3903/*
H. Peter Anvinc0ab1cd2008-10-19 16:24:53 -07003904 * Expand indirect tokens, %[...]. Just like expand_smacro(),
3905 * the input is considered destroyed.
H. Peter Anvin992fe752008-10-19 15:45:05 -07003906 */
3907static Token *expand_indirect(Token * tline, int level)
3908{
3909 const int max_indirect_level = 1000;
3910 Token *t, *thead, **tp;
3911 Token *it;
3912 bool skip;
3913
3914 if (level >= max_indirect_level) {
3915 error(ERR_NONFATAL, "interminable indirect expansion");
3916 } else {
3917 thead = NULL;
H. Peter Anvinc0ab1cd2008-10-19 16:24:53 -07003918 tp = &tline;
3919 while ((t = *tp)) {
3920 if (t->type != TOK_INDIRECT) {
3921 thead = t;
3922 tp = &t->next;
3923 } else {
3924 it = tokenize(t->text);
3925 it = expand_indirect(it, level+1);
3926 it = expand_smacro(it);
3927 while (it) {
3928 skip = false;
3929 switch (thead ? thead->type : TOK_NONE) {
3930 case TOK_WHITESPACE:
3931 skip = (it->type == TOK_WHITESPACE);
3932 break;
3933 case TOK_ID:
3934 case TOK_NUMBER:
3935 if (it->type == thead->type || it->type == TOK_NUMBER) {
3936 char *tmp = nasm_strcat(thead->text, it->text);
3937 nasm_free(thead->text);
3938 thead->text = tmp;
3939 skip = true;
3940 }
3941 break;
3942 default:
3943 break;
H. Peter Anvin992fe752008-10-19 15:45:05 -07003944 }
H. Peter Anvinc0ab1cd2008-10-19 16:24:53 -07003945 if (skip) {
3946 it = delete_Token(it);
3947 } else {
3948 *tp = thead = it;
3949 tp = &it->next;
3950 it = it->next;
3951 }
H. Peter Anvin992fe752008-10-19 15:45:05 -07003952 }
H. Peter Anvinc0ab1cd2008-10-19 16:24:53 -07003953 *tp = thead = t->next;
3954 t = delete_Token(t);
H. Peter Anvin992fe752008-10-19 15:45:05 -07003955 }
H. Peter Anvin992fe752008-10-19 15:45:05 -07003956 }
3957 }
3958 return tline;
3959}
3960
3961/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003962 * Determine whether the given line constitutes a multi-line macro
3963 * call, and return the MMacro structure called if so. Doesn't have
3964 * to check for an initial label - that's taken care of in
3965 * expand_mmacro - but must check numbers of parameters. Guaranteed
3966 * to be called with tline->type == TOK_ID, so the putative macro
3967 * name is easy to find.
3968 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003969static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003970{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003971 MMacro *head, *m;
3972 Token **params;
3973 int nparam;
3974
H. Peter Anvin166c2472008-05-28 12:28:58 -07003975 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003976
3977 /*
3978 * Efficiency: first we see if any macro exists with the given
3979 * name. If not, we can return NULL immediately. _Then_ we
3980 * count the parameters, and then we look further along the
3981 * list if necessary to find the proper MMacro.
3982 */
3983 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003984 if (!mstrcmp(m->name, tline->text, m->casesense))
3985 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003986 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003987 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003988
3989 /*
3990 * OK, we have a potential macro. Count and demarcate the
3991 * parameters.
3992 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003993 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003994
3995 /*
3996 * So we know how many parameters we've got. Find the MMacro
3997 * structure that handles this number.
3998 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003999 while (m) {
4000 if (m->nparam_min <= nparam
4001 && (m->plus || nparam <= m->nparam_max)) {
4002 /*
4003 * This one is right. Just check if cycle removal
4004 * prohibits us using it before we actually celebrate...
4005 */
4006 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00004007#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00004008 error(ERR_NONFATAL,
4009 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004010#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00004011 nasm_free(params);
4012 return NULL;
4013 }
4014 /*
4015 * It's right, and we can use it. Add its default
4016 * parameters to the end of our list if necessary.
4017 */
4018 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4019 params =
4020 nasm_realloc(params,
4021 ((m->nparam_min + m->ndefs +
4022 1) * sizeof(*params)));
4023 while (nparam < m->nparam_min + m->ndefs) {
4024 params[nparam] = m->defaults[nparam - m->nparam_min];
4025 nparam++;
4026 }
4027 }
4028 /*
4029 * If we've gone over the maximum parameter count (and
4030 * we're in Plus mode), ignore parameters beyond
4031 * nparam_max.
4032 */
4033 if (m->plus && nparam > m->nparam_max)
4034 nparam = m->nparam_max;
4035 /*
4036 * Then terminate the parameter list, and leave.
4037 */
4038 if (!params) { /* need this special case */
4039 params = nasm_malloc(sizeof(*params));
4040 nparam = 0;
4041 }
4042 params[nparam] = NULL;
4043 *params_array = params;
4044 return m;
4045 }
4046 /*
4047 * This one wasn't right: look for the next one with the
4048 * same name.
4049 */
4050 for (m = m->next; m; m = m->next)
4051 if (!mstrcmp(m->name, tline->text, m->casesense))
4052 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004053 }
4054
4055 /*
4056 * After all that, we didn't find one with the right number of
4057 * parameters. Issue a warning, and fail to expand the macro.
4058 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004059 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004060 "macro `%s' exists, but not taking %d parameters",
4061 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004062 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004063 return NULL;
4064}
4065
4066/*
4067 * Expand the multi-line macro call made by the given line, if
4068 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00004069 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004070 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004071static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004072{
4073 Token *startline = tline;
4074 Token *label = NULL;
4075 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004076 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004077 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004078 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004079 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004080 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004081
4082 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004083 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004084 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004085 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004086 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004087 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004088 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004089 if (m) {
4090 mname = t->text;
4091 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004092 Token *last;
4093 /*
4094 * We have an id which isn't a macro call. We'll assume
4095 * it might be a label; we'll also check to see if a
4096 * colon follows it. Then, if there's another id after
4097 * that lot, we'll check it again for macro-hood.
4098 */
4099 label = last = t;
4100 t = t->next;
4101 if (tok_type_(t, TOK_WHITESPACE))
4102 last = t, t = t->next;
4103 if (tok_is_(t, ":")) {
4104 dont_prepend = 1;
4105 last = t, t = t->next;
4106 if (tok_type_(t, TOK_WHITESPACE))
4107 last = t, t = t->next;
4108 }
4109 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
4110 return 0;
4111 last->next = NULL;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004112 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004113 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004114 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004115
4116 /*
4117 * Fix up the parameters: this involves stripping leading and
4118 * trailing whitespace, then stripping braces if they are
4119 * present.
4120 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004121 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004122 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004123
H. Peter Anvine2c80182005-01-15 22:15:51 +00004124 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004125 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004126 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004127
H. Peter Anvine2c80182005-01-15 22:15:51 +00004128 t = params[i];
4129 skip_white_(t);
4130 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004131 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004132 params[i] = t;
4133 paramlen[i] = 0;
4134 while (t) {
4135 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4136 break; /* ... because we have hit a comma */
4137 if (comma && t->type == TOK_WHITESPACE
4138 && tok_is_(t->next, ","))
4139 break; /* ... or a space then a comma */
4140 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4141 break; /* ... or a brace */
4142 t = t->next;
4143 paramlen[i]++;
4144 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004145 }
4146
4147 /*
4148 * OK, we have a MMacro structure together with a set of
4149 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004150 * copies of each Line on to istk->expansion. Substitution of
4151 * parameter tokens and macro-local tokens doesn't get done
4152 * until the single-line macro substitution process; this is
4153 * because delaying them allows us to change the semantics
4154 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004155 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004156 * First, push an end marker on to istk->expansion, mark this
4157 * macro as in progress, and set up its invocation-specific
4158 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004159 */
4160 ll = nasm_malloc(sizeof(Line));
4161 ll->next = istk->expansion;
4162 ll->finishes = m;
4163 ll->first = NULL;
4164 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004165
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004166 m->in_progress = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004167 m->params = params;
4168 m->iline = tline;
4169 m->nparam = nparam;
4170 m->rotate = 0;
4171 m->paramlen = paramlen;
4172 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004173 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004174
4175 m->next_active = istk->mstk;
4176 istk->mstk = m;
4177
H. Peter Anvine2c80182005-01-15 22:15:51 +00004178 for (l = m->expansion; l; l = l->next) {
4179 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004180
H. Peter Anvine2c80182005-01-15 22:15:51 +00004181 ll = nasm_malloc(sizeof(Line));
4182 ll->finishes = NULL;
4183 ll->next = istk->expansion;
4184 istk->expansion = ll;
4185 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004186
H. Peter Anvine2c80182005-01-15 22:15:51 +00004187 for (t = l->first; t; t = t->next) {
4188 Token *x = t;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004189 switch (t->type) {
4190 case TOK_PREPROC_Q:
H. Peter Anvinc751e862008-06-09 10:18:45 -07004191 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004192 break;
4193 case TOK_PREPROC_QQ:
4194 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4195 break;
4196 case TOK_PREPROC_ID:
4197 if (t->text[1] == '0' && t->text[2] == '0') {
4198 dont_prepend = -1;
4199 x = label;
4200 if (!x)
4201 continue;
4202 }
4203 /* fall through */
4204 default:
4205 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4206 break;
H. Peter Anvin166c2472008-05-28 12:28:58 -07004207 }
H. Peter Anvince2233b2008-05-25 21:57:00 -07004208 tail = &tt->next;
4209 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004210 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004211 }
4212
4213 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004214 * If we had a label, push it on as the first line of
4215 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004216 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004217 if (label) {
4218 if (dont_prepend < 0)
4219 free_tlist(startline);
4220 else {
4221 ll = nasm_malloc(sizeof(Line));
4222 ll->finishes = NULL;
4223 ll->next = istk->expansion;
4224 istk->expansion = ll;
4225 ll->first = startline;
4226 if (!dont_prepend) {
4227 while (label->next)
4228 label = label->next;
4229 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4230 }
4231 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004232 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004233
H. Peter Anvin734b1882002-04-30 21:01:08 +00004234 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004235
H. Peter Anvineba20a72002-04-30 20:53:55 +00004236 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004237}
4238
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004239/* The function that actually does the error reporting */
4240static void verror(int severity, const char *fmt, va_list arg)
4241{
4242 char buff[1024];
4243
4244 vsnprintf(buff, sizeof(buff), fmt, arg);
4245
4246 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004247 _error(severity, "(%s:%d) %s", istk->mstk->name,
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004248 istk->mstk->lineno, buff);
4249 else
H. Peter Anvin917a3492008-09-24 09:14:49 -07004250 _error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004251}
4252
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004253/*
4254 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004255 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004256 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004257static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004258{
4259 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004260
4261 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004262 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004263 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004264
H. Peter Anvin734b1882002-04-30 21:01:08 +00004265 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004266 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004267 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004268}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004269
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004270/*
4271 * Because %else etc are evaluated in the state context
4272 * of the previous branch, errors might get lost with error():
4273 * %if 0 ... %else trailing garbage ... %endif
4274 * So %else etc should report errors with this function.
4275 */
4276static void error_precond(int severity, const char *fmt, ...)
4277{
4278 va_list arg;
4279
4280 /* Only ignore the error if it's really in a dead branch */
4281 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4282 return;
4283
4284 va_start(arg, fmt);
4285 verror(severity, fmt, arg);
4286 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004287}
4288
H. Peter Anvin734b1882002-04-30 21:01:08 +00004289static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004290pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004291 ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004292{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004293 Token *t;
4294
H. Peter Anvin99941bf2002-05-14 17:44:03 +00004295 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004296 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004297 istk = nasm_malloc(sizeof(Include));
4298 istk->next = NULL;
4299 istk->conds = NULL;
4300 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004301 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004302 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004303 istk->fname = NULL;
4304 src_set_fname(nasm_strdup(file));
4305 src_set_linnum(0);
4306 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004307 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004308 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004309 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004310 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004311 nested_mac_count = 0;
4312 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004313 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004314 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004315 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004316 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004317 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004318 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004319 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004320 any_extrastdmac = extrastdmac && *extrastdmac;
4321 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004322 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004323 evaluate = eval;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004324
4325 /*
4326 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4327 * The caller, however, will also pass in 3 for preprocess-only so
4328 * we can set __PASS__ accordingly.
4329 */
4330 pass = apass > 2 ? 2 : apass;
4331
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004332 dephead = deptail = deplist;
4333 if (deplist) {
4334 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4335 sl->next = NULL;
4336 strcpy(sl->str, file);
4337 *deptail = sl;
4338 deptail = &sl->next;
4339 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004340
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004341 /*
4342 * Define the __PASS__ macro. This is defined here unlike
4343 * all the other builtins, because it is special -- it varies between
4344 * passes.
4345 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004346 t = nasm_malloc(sizeof(*t));
4347 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004348 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004349 t->a.mac = NULL;
4350 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004351}
4352
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004353static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004354{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004355 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004356 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004357
H. Peter Anvine2c80182005-01-15 22:15:51 +00004358 while (1) {
4359 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004360 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004361 * buffer or from the input file.
4362 */
4363 tline = NULL;
4364 while (istk->expansion && istk->expansion->finishes) {
4365 Line *l = istk->expansion;
4366 if (!l->finishes->name && l->finishes->in_progress > 1) {
4367 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004368
H. Peter Anvine2c80182005-01-15 22:15:51 +00004369 /*
4370 * This is a macro-end marker for a macro with no
4371 * name, which means it's not really a macro at all
4372 * but a %rep block, and the `in_progress' field is
4373 * more than 1, meaning that we still need to
4374 * repeat. (1 means the natural last repetition; 0
4375 * means termination by %exitrep.) We have
4376 * therefore expanded up to the %endrep, and must
4377 * push the whole block on to the expansion buffer
4378 * again. We don't bother to remove the macro-end
4379 * marker: we'd only have to generate another one
4380 * if we did.
4381 */
4382 l->finishes->in_progress--;
4383 for (l = l->finishes->expansion; l; l = l->next) {
4384 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004385
H. Peter Anvine2c80182005-01-15 22:15:51 +00004386 ll = nasm_malloc(sizeof(Line));
4387 ll->next = istk->expansion;
4388 ll->finishes = NULL;
4389 ll->first = NULL;
4390 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004391
H. Peter Anvine2c80182005-01-15 22:15:51 +00004392 for (t = l->first; t; t = t->next) {
4393 if (t->text || t->type == TOK_WHITESPACE) {
4394 tt = *tail =
4395 new_Token(NULL, t->type, t->text, 0);
4396 tail = &tt->next;
4397 }
4398 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004399
H. Peter Anvine2c80182005-01-15 22:15:51 +00004400 istk->expansion = ll;
4401 }
4402 } else {
4403 /*
4404 * Check whether a `%rep' was started and not ended
4405 * within this macro expansion. This can happen and
4406 * should be detected. It's a fatal error because
4407 * I'm too confused to work out how to recover
4408 * sensibly from it.
4409 */
4410 if (defining) {
4411 if (defining->name)
4412 error(ERR_PANIC,
4413 "defining with name in expansion");
4414 else if (istk->mstk->name)
4415 error(ERR_FATAL,
4416 "`%%rep' without `%%endrep' within"
4417 " expansion of macro `%s'",
4418 istk->mstk->name);
4419 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004420
H. Peter Anvine2c80182005-01-15 22:15:51 +00004421 /*
4422 * FIXME: investigate the relationship at this point between
4423 * istk->mstk and l->finishes
4424 */
4425 {
4426 MMacro *m = istk->mstk;
4427 istk->mstk = m->next_active;
4428 if (m->name) {
4429 /*
4430 * This was a real macro call, not a %rep, and
4431 * therefore the parameter information needs to
4432 * be freed.
4433 */
4434 nasm_free(m->params);
4435 free_tlist(m->iline);
4436 nasm_free(m->paramlen);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004437 l->finishes->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004438 } else
4439 free_mmacro(m);
4440 }
4441 istk->expansion = l->next;
4442 nasm_free(l);
4443 list->downlevel(LIST_MACRO);
4444 }
4445 }
4446 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004447
H. Peter Anvine2c80182005-01-15 22:15:51 +00004448 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004449 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004450 Line *l = istk->expansion;
4451 if (istk->mstk)
4452 istk->mstk->lineno++;
4453 tline = l->first;
4454 istk->expansion = l->next;
4455 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004456 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004457 list->line(LIST_MACRO, p);
4458 nasm_free(p);
4459 break;
4460 }
4461 line = read_line();
4462 if (line) { /* from the current input file */
4463 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004464 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004465 nasm_free(line);
4466 break;
4467 }
4468 /*
4469 * The current file has ended; work down the istk
4470 */
4471 {
4472 Include *i = istk;
4473 fclose(i->fp);
4474 if (i->conds)
4475 error(ERR_FATAL,
4476 "expected `%%endif' before end of file");
4477 /* only set line and file name if there's a next node */
4478 if (i->next) {
4479 src_set_linnum(i->lineno);
4480 nasm_free(src_set_fname(i->fname));
H. Peter Anvin86877b22008-06-20 15:55:45 -07004481 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004482 istk = i->next;
4483 list->downlevel(LIST_INCLUDE);
4484 nasm_free(i);
4485 if (!istk)
4486 return NULL;
Victor van den Elzen4c9d6222008-10-01 13:08:50 +02004487 if (istk->expansion && istk->expansion->finishes)
4488 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004489 }
4490 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004491
H. Peter Anvine2c80182005-01-15 22:15:51 +00004492 /*
4493 * We must expand MMacro parameters and MMacro-local labels
4494 * _before_ we plunge into directive processing, to cope
4495 * with things like `%define something %1' such as STRUC
4496 * uses. Unless we're _defining_ a MMacro, in which case
4497 * those tokens should be left alone to go into the
4498 * definition; and unless we're in a non-emitting
4499 * condition, in which case we don't want to meddle with
4500 * anything.
4501 */
Charles Crayned4200be2008-07-12 16:42:33 -07004502 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin992fe752008-10-19 15:45:05 -07004503 && !(istk->mstk && !istk->mstk->in_progress)) {
4504 tline = expand_indirect(tline,0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004505 tline = expand_mmac_params(tline);
H. Peter Anvin992fe752008-10-19 15:45:05 -07004506 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004507
H. Peter Anvine2c80182005-01-15 22:15:51 +00004508 /*
4509 * Check the line to see if it's a preprocessor directive.
4510 */
4511 if (do_directive(tline) == DIRECTIVE_FOUND) {
4512 continue;
4513 } else if (defining) {
4514 /*
4515 * We're defining a multi-line macro. We emit nothing
4516 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004517 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004518 */
4519 Line *l = nasm_malloc(sizeof(Line));
4520 l->next = defining->expansion;
4521 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004522 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004523 defining->expansion = l;
4524 continue;
4525 } else if (istk->conds && !emitting(istk->conds->state)) {
4526 /*
4527 * We're in a non-emitting branch of a condition block.
4528 * Emit nothing at all, not even a blank line: when we
4529 * emerge from the condition we'll give a line-number
4530 * directive so we keep our place correctly.
4531 */
4532 free_tlist(tline);
4533 continue;
4534 } else if (istk->mstk && !istk->mstk->in_progress) {
4535 /*
4536 * We're in a %rep block which has been terminated, so
4537 * we're walking through to the %endrep without
4538 * emitting anything. Emit nothing at all, not even a
4539 * blank line: when we emerge from the %rep block we'll
4540 * give a line-number directive so we keep our place
4541 * correctly.
4542 */
4543 free_tlist(tline);
4544 continue;
4545 } else {
4546 tline = expand_smacro(tline);
4547 if (!expand_mmacro(tline)) {
4548 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004549 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004550 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004551 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004552 free_tlist(tline);
4553 break;
4554 } else {
4555 continue; /* expand_mmacro calls free_tlist */
4556 }
4557 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004558 }
4559
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004560 return line;
4561}
4562
H. Peter Anvine2c80182005-01-15 22:15:51 +00004563static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004564{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004565 if (defining) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02004566 if(defining->name) {
4567 error(ERR_NONFATAL,
4568 "end of file while still defining macro `%s'",
4569 defining->name);
4570 } else {
4571 error(ERR_NONFATAL, "end of file while still in %%rep");
4572 }
4573
H. Peter Anvine2c80182005-01-15 22:15:51 +00004574 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004575 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004576 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004577 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004578 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004579 while (istk) {
4580 Include *i = istk;
4581 istk = istk->next;
4582 fclose(i->fp);
4583 nasm_free(i->fname);
4584 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004585 }
4586 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004587 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004588 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004589 if (pass == 0) {
H. Peter Anvin86877b22008-06-20 15:55:45 -07004590 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004591 free_llist(predef);
4592 delete_Blocks();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004593 while ((i = ipath)) {
4594 ipath = i->next;
4595 if (i->path)
4596 nasm_free(i->path);
4597 nasm_free(i);
4598 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07004599 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004600}
4601
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004602void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004603{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004604 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004605
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004606 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004607 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004608 i->next = NULL;
4609
H. Peter Anvine2c80182005-01-15 22:15:51 +00004610 if (ipath != NULL) {
4611 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004612 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004613 j = j->next;
4614 j->next = i;
4615 } else {
4616 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004617 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004618}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004619
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004620void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004621{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004622 Token *inc, *space, *name;
4623 Line *l;
4624
H. Peter Anvin734b1882002-04-30 21:01:08 +00004625 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4626 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4627 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004628
4629 l = nasm_malloc(sizeof(Line));
4630 l->next = predef;
4631 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004632 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004633 predef = l;
4634}
4635
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004636void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004637{
4638 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004639 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004640 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004641
4642 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004643 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4644 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004645 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004646 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004647 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004648 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004649 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004650
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004651 l = nasm_malloc(sizeof(Line));
4652 l->next = predef;
4653 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004654 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004655 predef = l;
4656}
4657
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004658void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004659{
4660 Token *def, *space;
4661 Line *l;
4662
H. Peter Anvin734b1882002-04-30 21:01:08 +00004663 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4664 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004665 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004666
4667 l = nasm_malloc(sizeof(Line));
4668 l->next = predef;
4669 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004670 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004671 predef = l;
4672}
4673
Keith Kaniosb7a89542007-04-12 02:40:54 +00004674/*
4675 * Added by Keith Kanios:
4676 *
4677 * This function is used to assist with "runtime" preprocessor
4678 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4679 *
4680 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4681 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4682 */
4683
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004684void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004685{
4686 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004687
Keith Kaniosb7a89542007-04-12 02:40:54 +00004688 def = tokenize(definition);
4689 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4690 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004691
Keith Kaniosb7a89542007-04-12 02:40:54 +00004692}
4693
H. Peter Anvina70547f2008-07-19 21:44:26 -07004694void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004695{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004696 extrastdmac = macros;
4697}
4698
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004699static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004700{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004701 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004702 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004703 tok->text = nasm_strdup(numbuf);
4704 tok->type = TOK_NUMBER;
4705}
4706
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004707Preproc nasmpp = {
4708 pp_reset,
4709 pp_getline,
4710 pp_cleanup
4711};