blob: 46f8c6b6fc847d92c36bf2b378338b65bfc780e6 [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 Anvin5b76fa22008-05-26 11:14:38 -0700162 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
163 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000164};
165
H. Peter Anvine2c80182005-01-15 22:15:51 +0000166struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000167 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000168 char *text;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000169 SMacro *mac; /* associated macro for TOK_SMAC_END */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000170 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000171};
172
173/*
174 * Multi-line macro definitions are stored as a linked list of
175 * these, which is essentially a container to allow several linked
176 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700177 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000178 * Note that in this module, linked lists are treated as stacks
179 * wherever possible. For this reason, Lines are _pushed_ on to the
180 * `expansion' field in MMacro structures, so that the linked list,
181 * if walked, would give the macro lines in reverse order; this
182 * means that we can walk the list when expanding a macro, and thus
183 * push the lines on to the `expansion' field in _istk_ in reverse
184 * order (so that when popped back off they are in the right
185 * order). It may seem cockeyed, and it relies on my design having
186 * an even number of steps in, but it works...
187 *
188 * Some of these structures, rather than being actual lines, are
189 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000190 * This is for use in the cycle-tracking and %rep-handling code.
191 * Such structures have `finishes' non-NULL, and `first' NULL. All
192 * others have `finishes' NULL, but `first' may still be NULL if
193 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000194 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000195struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000196 Line *next;
197 MMacro *finishes;
198 Token *first;
199};
200
201/*
202 * To handle an arbitrary level of file inclusion, we maintain a
203 * stack (ie linked list) of these things.
204 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000205struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000206 Include *next;
207 FILE *fp;
208 Cond *conds;
209 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000210 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000211 int lineno, lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000212 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000213};
214
215/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000216 * Include search path. This is simply a list of strings which get
217 * prepended, in turn, to the name of an include file, in an
218 * attempt to find the file if it's not in the current directory.
219 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000220struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000221 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000222 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000223};
224
225/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000226 * Conditional assembly: we maintain a separate stack of these for
227 * each level of file inclusion. (The only reason we keep the
228 * stacks separate is to ensure that a stray `%endif' in a file
229 * included from within the true branch of a `%if' won't terminate
230 * it and cause confusion: instead, rightly, it'll cause an error.)
231 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000232struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000233 Cond *next;
234 int state;
235};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000236enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000237 /*
238 * These states are for use just after %if or %elif: IF_TRUE
239 * means the condition has evaluated to truth so we are
240 * currently emitting, whereas IF_FALSE means we are not
241 * currently emitting but will start doing so if a %else comes
242 * up. In these states, all directives are admissible: %elif,
243 * %else and %endif. (And of course %if.)
244 */
245 COND_IF_TRUE, COND_IF_FALSE,
246 /*
247 * These states come up after a %else: ELSE_TRUE means we're
248 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
249 * any %elif or %else will cause an error.
250 */
251 COND_ELSE_TRUE, COND_ELSE_FALSE,
252 /*
253 * This state means that we're not emitting now, and also that
254 * nothing until %endif will be emitted at all. It's for use in
255 * two circumstances: (i) when we've had our moment of emission
256 * and have now started seeing %elifs, and (ii) when the
257 * condition construct in question is contained within a
258 * non-emitting branch of a larger condition construct.
259 */
260 COND_NEVER
261};
262#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
263
H. Peter Anvin70653092007-10-19 14:42:29 -0700264/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000265 * These defines are used as the possible return values for do_directive
266 */
267#define NO_DIRECTIVE_FOUND 0
268#define DIRECTIVE_FOUND 1
269
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000270/*
271 * Condition codes. Note that we use c_ prefix not C_ because C_ is
272 * used in nasm.h for the "real" condition codes. At _this_ level,
273 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
274 * ones, so we need a different enum...
275 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700276static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000277 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
278 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000279 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000280};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700281enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000282 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
283 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 -0700284 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
285 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000286};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700287static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000288 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
289 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 +0000290 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000291};
292
H. Peter Anvin76690a12002-04-30 20:52:49 +0000293/*
294 * Directive names.
295 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000296/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000297static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000298{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000299 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000300}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000301
302/* For TASM compatibility we need to be able to recognise TASM compatible
303 * conditional compilation directives. Using the NASM pre-processor does
304 * not work, so we look for them specifically from the following list and
305 * then jam in the equivalent NASM directive into the input stream.
306 */
307
H. Peter Anvine2c80182005-01-15 22:15:51 +0000308enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000309 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
310 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
311};
312
H. Peter Anvin476d2862007-10-02 22:04:15 -0700313static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000314 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
315 "ifndef", "include", "local"
316};
317
318static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000319static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000320static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800321static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000322
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000323static Context *cstk;
324static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000325static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000326
H. Peter Anvine2c80182005-01-15 22:15:51 +0000327static efunc _error; /* Pointer to client-provided error reporting function */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000328static evalfunc evaluate;
329
H. Peter Anvine2c80182005-01-15 22:15:51 +0000330static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700331static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000332
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700333static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000334
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000335static Line *predef = NULL;
336
337static ListGen *list;
338
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000339/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000340 * The current set of multi-line macros we have defined.
341 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700342static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000343
344/*
345 * The current set of single-line macros we have defined.
346 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700347static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000348
349/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000350 * The multi-line macro we are currently defining, or the %rep
351 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000352 */
353static MMacro *defining;
354
355/*
356 * The number of macro parameters to allocate space for at a time.
357 */
358#define PARAM_DELTA 16
359
360/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700361 * The standard macro set: defined in macros.c in the array nasm_stdmac.
362 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000363 */
H. Peter Anvin188ce762008-02-16 13:58:45 -0800364static const char * const *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000365
366/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000367 * The extra standard macros that come from the object format, if
368 * any.
369 */
H. Peter Anvin188ce762008-02-16 13:58:45 -0800370static const char * const *extrastdmac = NULL;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700371bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000372
373/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000374 * Tokens are allocated in blocks to improve speed
375 */
376#define TOKEN_BLOCKSIZE 4096
377static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000378struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000379 Blocks *next;
380 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000381};
382
383static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000384
385/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000386 * Forward declarations.
387 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000388static Token *expand_mmac_params(Token * tline);
389static Token *expand_smacro(Token * tline);
390static Token *expand_id(Token * tline);
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700391static Context *get_ctx(char *name, bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700392static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000393static void error(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000394static void *new_Block(size_t size);
395static void delete_Blocks(void);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000396static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000397static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000398
399/*
400 * Macros for safe checking of token pointers, avoid *(NULL)
401 */
402#define tok_type_(x,t) ((x) && (x)->type == (t))
403#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
404#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
405#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000406
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000407/* Handle TASM specific directives, which do not contain a % in
408 * front of them. We do it here because I could not find any other
409 * place to do it for the moment, and it is a hack (ideally it would
410 * be nice to be able to use the NASM pre-processor to do it).
411 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000412static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000413{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000414 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000415 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000416
417 /* Skip whitespace */
418 while (isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000419 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000420
421 /* Binary search for the directive name */
422 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000423 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000424 len = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000425 while (!isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000426 len++;
427 if (len) {
428 oldchar = p[len];
429 p[len] = 0;
430 while (j - i > 1) {
431 k = (j + i) / 2;
432 m = nasm_stricmp(p, tasm_directives[k]);
433 if (m == 0) {
434 /* We have found a directive, so jam a % in front of it
435 * so that NASM will then recognise it as one if it's own.
436 */
437 p[len] = oldchar;
438 len = strlen(p);
439 oldline = line;
440 line = nasm_malloc(len + 2);
441 line[0] = '%';
442 if (k == TM_IFDIFI) {
443 /* NASM does not recognise IFDIFI, so we convert it to
444 * %ifdef BOGUS. This is not used in NASM comaptible
445 * code, but does need to parse for the TASM macro
446 * package.
447 */
448 strcpy(line + 1, "ifdef BOGUS");
449 } else {
450 memcpy(line + 1, p, len + 1);
451 }
452 nasm_free(oldline);
453 return line;
454 } else if (m < 0) {
455 j = k;
456 } else
457 i = k;
458 }
459 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000460 }
461 return line;
462}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000463
H. Peter Anvin76690a12002-04-30 20:52:49 +0000464/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000465 * The pre-preprocessing stage... This function translates line
466 * number indications as they emerge from GNU cpp (`# lineno "file"
467 * flags') into NASM preprocessor line number indications (`%line
468 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000469 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000470static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000471{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000472 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000473 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000474
H. Peter Anvine2c80182005-01-15 22:15:51 +0000475 if (line[0] == '#' && line[1] == ' ') {
476 oldline = line;
477 fname = oldline + 2;
478 lineno = atoi(fname);
479 fname += strspn(fname, "0123456789 ");
480 if (*fname == '"')
481 fname++;
482 fnlen = strcspn(fname, "\"");
483 line = nasm_malloc(20 + fnlen);
484 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
485 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000486 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000487 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000488 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000489 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000490}
491
492/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000493 * Free a linked list of tokens.
494 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000495static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000496{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000497 while (list) {
498 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000499 }
500}
501
502/*
503 * Free a linked list of lines.
504 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000505static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000506{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000507 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508 while (list) {
509 l = list;
510 list = list->next;
511 free_tlist(l->first);
512 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000513 }
514}
515
516/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000517 * Free an MMacro
518 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000519static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000520{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000521 nasm_free(m->name);
522 free_tlist(m->dlist);
523 nasm_free(m->defaults);
524 free_llist(m->expansion);
525 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000526}
527
528/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700529 * Free all currently defined macros, and free the hash tables
530 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700531static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700532{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700533 SMacro *s;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700534 const char *key;
535 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700536
H. Peter Anvin072771e2008-05-22 13:17:51 -0700537 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700538 nasm_free((void *)key);
539 while (s) {
540 SMacro *ns = s->next;
541 nasm_free(s->name);
542 free_tlist(s->expansion);
543 nasm_free(s);
544 s = ns;
545 }
546 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700547 hash_free(smt);
548}
549
550static void free_mmacro_table(struct hash_table *mmt)
551{
552 MMacro *m;
553 const char *key;
554 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700555
556 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700557 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700558 nasm_free((void *)key);
559 while (m) {
560 MMacro *nm = m->next;
561 free_mmacro(m);
562 m = nm;
563 }
564 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700565 hash_free(mmt);
566}
567
568static void free_macros(void)
569{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700570 free_smacro_table(&smacros);
571 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700572}
573
574/*
575 * Initialize the hash tables
576 */
577static void init_macros(void)
578{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700579 hash_init(&smacros, HASH_LARGE);
580 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700581}
582
583/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000584 * Pop the context stack.
585 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000586static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000587{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000588 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000589
590 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700591 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000592 nasm_free(c->name);
593 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000594}
595
H. Peter Anvin072771e2008-05-22 13:17:51 -0700596/*
597 * Search for a key in the hash index; adding it if necessary
598 * (in which case we initialize the data pointer to NULL.)
599 */
600static void **
601hash_findi_add(struct hash_table *hash, const char *str)
602{
603 struct hash_insert hi;
604 void **r;
605 char *strx;
606
607 r = hash_findi(hash, str, &hi);
608 if (r)
609 return r;
610
611 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
612 return hash_add(&hi, strx, NULL);
613}
614
615/*
616 * Like hash_findi, but returns the data element rather than a pointer
617 * to it. Used only when not adding a new element, hence no third
618 * argument.
619 */
620static void *
621hash_findix(struct hash_table *hash, const char *str)
622{
623 void **p;
624
625 p = hash_findi(hash, str, NULL);
626 return p ? *p : NULL;
627}
628
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000629#define BUF_DELTA 512
630/*
631 * Read a line from the top file in istk, handling multiple CR/LFs
632 * at the end of the line read, and handling spurious ^Zs. Will
633 * return lines from the standard macro set if this has not already
634 * been done.
635 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000636static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000637{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000638 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000639 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000640
H. Peter Anvine2c80182005-01-15 22:15:51 +0000641 if (stdmacpos) {
642 if (*stdmacpos) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000643 char *ret = nasm_strdup(*stdmacpos++);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000644 if (!*stdmacpos && any_extrastdmac) {
645 stdmacpos = extrastdmac;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700646 any_extrastdmac = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000647 return ret;
648 }
649 /*
650 * Nasty hack: here we push the contents of `predef' on
651 * to the top-level expansion stack, since this is the
652 * most convenient way to implement the pre-include and
653 * pre-define features.
654 */
655 if (!*stdmacpos) {
656 Line *pd, *l;
657 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000658
H. Peter Anvine2c80182005-01-15 22:15:51 +0000659 for (pd = predef; pd; pd = pd->next) {
660 head = NULL;
661 tail = &head;
662 for (t = pd->first; t; t = t->next) {
663 *tail = new_Token(NULL, t->type, t->text, 0);
664 tail = &(*tail)->next;
665 }
666 l = nasm_malloc(sizeof(Line));
667 l->next = istk->expansion;
668 l->first = head;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700669 l->finishes = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000670 istk->expansion = l;
671 }
672 }
673 return ret;
674 } else {
675 stdmacpos = NULL;
676 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000677 }
678
679 bufsize = BUF_DELTA;
680 buffer = nasm_malloc(BUF_DELTA);
681 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000682 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000683 while (1) {
684 q = fgets(p, bufsize - (p - buffer), istk->fp);
685 if (!q)
686 break;
687 p += strlen(p);
688 if (p > buffer && p[-1] == '\n') {
689 /* Convert backslash-CRLF line continuation sequences into
690 nothing at all (for DOS and Windows) */
691 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
692 p -= 3;
693 *p = 0;
694 continued_count++;
695 }
696 /* Also convert backslash-LF line continuation sequences into
697 nothing at all (for Unix) */
698 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
699 p -= 2;
700 *p = 0;
701 continued_count++;
702 } else {
703 break;
704 }
705 }
706 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000707 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000708 bufsize += BUF_DELTA;
709 buffer = nasm_realloc(buffer, bufsize);
710 p = buffer + offset; /* prevent stale-pointer problems */
711 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000712 }
713
H. Peter Anvine2c80182005-01-15 22:15:51 +0000714 if (!q && p == buffer) {
715 nasm_free(buffer);
716 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000717 }
718
H. Peter Anvine2c80182005-01-15 22:15:51 +0000719 src_set_linnum(src_get_linnum() + istk->lineinc +
720 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000721
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000722 /*
723 * Play safe: remove CRs as well as LFs, if any of either are
724 * present at the end of the line.
725 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000726 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000727 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000728
729 /*
730 * Handle spurious ^Z, which may be inserted into source files
731 * by some file transfer utilities.
732 */
733 buffer[strcspn(buffer, "\032")] = '\0';
734
H. Peter Anvin734b1882002-04-30 21:01:08 +0000735 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000736
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000737 return buffer;
738}
739
740/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000741 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000742 * don't need to parse the value out of e.g. numeric tokens: we
743 * simply split one string into many.
744 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000745static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000746{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000747 char *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000748 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000749 Token *list = NULL;
750 Token *t, **tail = &list;
751
H. Peter Anvine2c80182005-01-15 22:15:51 +0000752 while (*line) {
753 p = line;
754 if (*p == '%') {
755 p++;
756 if (isdigit(*p) ||
757 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
758 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
759 do {
760 p++;
761 }
762 while (isdigit(*p));
763 type = TOK_PREPROC_ID;
764 } else if (*p == '{') {
765 p++;
766 while (*p && *p != '}') {
767 p[-1] = *p;
768 p++;
769 }
770 p[-1] = '\0';
771 if (*p)
772 p++;
773 type = TOK_PREPROC_ID;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700774 } else if (*p == '?') {
775 type = TOK_PREPROC_Q; /* %? */
776 p++;
777 if (*p == '?') {
778 type = TOK_PREPROC_QQ; /* %?? */
779 p++;
780 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000781 } else if (isidchar(*p) ||
782 ((*p == '!' || *p == '%' || *p == '$') &&
783 isidchar(p[1]))) {
784 do {
785 p++;
786 }
787 while (isidchar(*p));
788 type = TOK_PREPROC_ID;
789 } else {
790 type = TOK_OTHER;
791 if (*p == '%')
792 p++;
793 }
794 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
795 type = TOK_ID;
796 p++;
797 while (*p && isidchar(*p))
798 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700799 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000800 /*
801 * A string token.
802 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700804 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000805
H. Peter Anvine2c80182005-01-15 22:15:51 +0000806 if (*p) {
807 p++;
808 } else {
809 error(ERR_WARNING, "unterminated string");
810 /* Handling unterminated strings by UNV */
811 /* type = -1; */
812 }
813 } else if (isnumstart(*p)) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700814 bool is_hex = false;
815 bool is_float = false;
816 bool has_e = false;
817 char c, *r;
818
H. Peter Anvine2c80182005-01-15 22:15:51 +0000819 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700820 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000821 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700822
823 if (*p == '$') {
824 p++;
825 is_hex = true;
826 }
827
828 for (;;) {
829 c = *p++;
830
831 if (!is_hex && (c == 'e' || c == 'E')) {
832 has_e = true;
833 if (*p == '+' || *p == '-') {
834 /* e can only be followed by +/- if it is either a
835 prefixed hex number or a floating-point number */
836 p++;
837 is_float = true;
838 }
839 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
840 is_hex = true;
841 } else if (c == 'P' || c == 'p') {
842 is_float = true;
843 if (*p == '+' || *p == '-')
844 p++;
845 } else if (isnumchar(c) || c == '_')
846 ; /* just advance */
847 else if (c == '.') {
848 /* we need to deal with consequences of the legacy
849 parser, like "1.nolist" being two tokens
850 (TOK_NUMBER, TOK_ID) here; at least give it
851 a shot for now. In the future, we probably need
852 a flex-based scanner with proper pattern matching
853 to do it as well as it can be done. Nothing in
854 the world is going to help the person who wants
855 0x123.p16 interpreted as two tokens, though. */
856 r = p;
857 while (*r == '_')
858 r++;
859
860 if (isdigit(*r) || (is_hex && isxdigit(*r)) ||
861 (!is_hex && (*r == 'e' || *r == 'E')) ||
862 (*r == 'p' || *r == 'P')) {
863 p = r;
864 is_float = true;
865 } else
866 break; /* Terminate the token */
867 } else
868 break;
869 }
870 p--; /* Point to first character beyond number */
871
872 if (has_e && !is_hex) {
873 /* 1e13 is floating-point, but 1e13h is not */
874 is_float = true;
875 }
876
877 type = is_float ? TOK_FLOAT : TOK_NUMBER;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000878 } else if (isspace(*p)) {
879 type = TOK_WHITESPACE;
880 p++;
881 while (*p && isspace(*p))
882 p++;
883 /*
884 * Whitespace just before end-of-line is discarded by
885 * pretending it's a comment; whitespace just before a
886 * comment gets lumped into the comment.
887 */
888 if (!*p || *p == ';') {
889 type = TOK_COMMENT;
890 while (*p)
891 p++;
892 }
893 } else if (*p == ';') {
894 type = TOK_COMMENT;
895 while (*p)
896 p++;
897 } else {
898 /*
899 * Anything else is an operator of some kind. We check
900 * for all the double-character operators (>>, <<, //,
901 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000902 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000903 */
904 type = TOK_OTHER;
905 if ((p[0] == '>' && p[1] == '>') ||
906 (p[0] == '<' && p[1] == '<') ||
907 (p[0] == '/' && p[1] == '/') ||
908 (p[0] == '<' && p[1] == '=') ||
909 (p[0] == '>' && p[1] == '=') ||
910 (p[0] == '=' && p[1] == '=') ||
911 (p[0] == '!' && p[1] == '=') ||
912 (p[0] == '<' && p[1] == '>') ||
913 (p[0] == '&' && p[1] == '&') ||
914 (p[0] == '|' && p[1] == '|') ||
915 (p[0] == '^' && p[1] == '^')) {
916 p++;
917 }
918 p++;
919 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000920
H. Peter Anvine2c80182005-01-15 22:15:51 +0000921 /* Handling unterminated string by UNV */
922 /*if (type == -1)
923 {
924 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
925 t->text[p-line] = *line;
926 tail = &t->next;
927 }
928 else */
929 if (type != TOK_COMMENT) {
930 *tail = t = new_Token(NULL, type, line, p - line);
931 tail = &t->next;
932 }
933 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000934 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000935 return list;
936}
937
H. Peter Anvince616072002-04-30 21:02:23 +0000938/*
939 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -0700940 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +0000941 * deleted only all at once by the delete_Blocks function.
942 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000943static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +0000944{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000945 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000947 /* first, get to the end of the linked list */
948 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000949 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000950 /* now allocate the requested chunk */
951 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000952
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000953 /* now allocate a new block for the next request */
954 b->next = nasm_malloc(sizeof(Blocks));
955 /* and initialize the contents of the new block */
956 b->next->next = NULL;
957 b->next->chunk = NULL;
958 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000959}
960
961/*
962 * this function deletes all managed blocks of memory
963 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000964static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +0000965{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +0000967
H. Peter Anvin70653092007-10-19 14:42:29 -0700968 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000969 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -0700970 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000971 * free it.
972 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973 while (b) {
974 if (b->chunk)
975 nasm_free(b->chunk);
976 a = b;
977 b = b->next;
978 if (a != &blocks)
979 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000980 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000981}
H. Peter Anvin734b1882002-04-30 21:01:08 +0000982
983/*
H. Peter Anvin70653092007-10-19 14:42:29 -0700984 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +0000985 * back to the caller. It sets the type and text elements, and
986 * also the mac and next elements to NULL.
987 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000988static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +0000989{
990 Token *t;
991 int i;
992
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 if (freeTokens == NULL) {
994 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
995 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
996 freeTokens[i].next = &freeTokens[i + 1];
997 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000998 }
999 t = freeTokens;
1000 freeTokens = t->next;
1001 t->next = next;
1002 t->mac = NULL;
1003 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 if (type == TOK_WHITESPACE || text == NULL) {
1005 t->text = NULL;
1006 } else {
1007 if (txtlen == 0)
1008 txtlen = strlen(text);
1009 t->text = nasm_malloc(1 + txtlen);
1010 strncpy(t->text, text, txtlen);
1011 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001012 }
1013 return t;
1014}
1015
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001017{
1018 Token *next = t->next;
1019 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001020 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001021 freeTokens = t;
1022 return next;
1023}
1024
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001025/*
1026 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001027 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1028 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001029 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001030static char *detoken(Token * tlist, int expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001031{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001032 Token *t;
1033 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001034 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001035 const char *q;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001036
1037 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001038 for (t = tlist; t; t = t->next) {
1039 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001040 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001041 nasm_free(t->text);
1042 if (p)
1043 t->text = nasm_strdup(p);
1044 else
1045 t->text = NULL;
1046 }
1047 /* Expand local macros here and not during preprocessing */
1048 if (expand_locals &&
1049 t->type == TOK_PREPROC_ID && t->text &&
1050 t->text[0] == '%' && t->text[1] == '$') {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001051 Context *ctx = get_ctx(t->text, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001052 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001053 char buffer[40];
1054 char *p, *q = t->text + 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001055
H. Peter Anvine2c80182005-01-15 22:15:51 +00001056 q += strspn(q, "$");
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001057 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001058 p = nasm_strcat(buffer, q);
1059 nasm_free(t->text);
1060 t->text = p;
1061 }
1062 }
1063 if (t->type == TOK_WHITESPACE) {
1064 len++;
1065 } else if (t->text) {
1066 len += strlen(t->text);
1067 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001068 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001069 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001070 for (t = tlist; t; t = t->next) {
1071 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001072 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001073 } else if (t->text) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001074 q = t->text;
1075 while (*q)
1076 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001077 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001078 }
1079 *p = '\0';
1080 return line;
1081}
1082
1083/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001084 * A scanner, suitable for use by the expression evaluator, which
1085 * operates on a line of Tokens. Expects a pointer to a pointer to
1086 * the first token in the line to be passed in as its private_data
1087 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001088 *
1089 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001090 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001091static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001092{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001093 Token **tlineptr = private_data;
1094 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001095 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001096
H. Peter Anvine2c80182005-01-15 22:15:51 +00001097 do {
1098 tline = *tlineptr;
1099 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001100 }
1101 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001102 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001103
1104 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001105 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001106
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001107 tokval->t_charptr = tline->text;
1108
H. Peter Anvin76690a12002-04-30 20:52:49 +00001109 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001110 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001111 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001112 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001113
H. Peter Anvine2c80182005-01-15 22:15:51 +00001114 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001115 p = tokval->t_charptr = tline->text;
1116 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001117 tokval->t_charptr++;
1118 return tokval->t_type = TOKEN_ID;
1119 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001120
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001121 for (r = p, s = ourcopy; *r; r++) {
Philipp Thomas76ec8e72008-05-21 08:53:21 -07001122 if (r >= p+MAX_KEYWORD)
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001123 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1124 *s++ = tolower(*r);
1125 }
1126 *s = '\0';
1127 /* right, so we have an identifier sitting in temp storage. now,
1128 * is it actually a register or instruction name, or what? */
1129 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001130 }
1131
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 if (tline->type == TOK_NUMBER) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001133 bool rn_error;
1134 tokval->t_integer = readnum(tline->text, &rn_error);
1135 if (rn_error)
1136 return tokval->t_type = TOKEN_ERRNUM; /* some malformation occurred */
1137 tokval->t_charptr = tline->text;
1138 return tokval->t_type = TOKEN_NUM;
1139 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001140
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001141 if (tline->type == TOK_FLOAT) {
1142 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001143 }
1144
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145 if (tline->type == TOK_STRING) {
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001146 bool rn_warn;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001147 char q, *r;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001148 int l;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001149
H. Peter Anvine2c80182005-01-15 22:15:51 +00001150 r = tline->text;
1151 q = *r++;
1152 l = strlen(r);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001153
H. Peter Anvine2c80182005-01-15 22:15:51 +00001154 if (l == 0 || r[l - 1] != q)
1155 return tokval->t_type = TOKEN_ERRNUM;
1156 tokval->t_integer = readstrnum(r, l - 1, &rn_warn);
1157 if (rn_warn)
1158 error(ERR_WARNING | ERR_PASS1, "character constant too long");
1159 tokval->t_charptr = NULL;
1160 return tokval->t_type = TOKEN_NUM;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001161 }
1162
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163 if (tline->type == TOK_OTHER) {
1164 if (!strcmp(tline->text, "<<"))
1165 return tokval->t_type = TOKEN_SHL;
1166 if (!strcmp(tline->text, ">>"))
1167 return tokval->t_type = TOKEN_SHR;
1168 if (!strcmp(tline->text, "//"))
1169 return tokval->t_type = TOKEN_SDIV;
1170 if (!strcmp(tline->text, "%%"))
1171 return tokval->t_type = TOKEN_SMOD;
1172 if (!strcmp(tline->text, "=="))
1173 return tokval->t_type = TOKEN_EQ;
1174 if (!strcmp(tline->text, "<>"))
1175 return tokval->t_type = TOKEN_NE;
1176 if (!strcmp(tline->text, "!="))
1177 return tokval->t_type = TOKEN_NE;
1178 if (!strcmp(tline->text, "<="))
1179 return tokval->t_type = TOKEN_LE;
1180 if (!strcmp(tline->text, ">="))
1181 return tokval->t_type = TOKEN_GE;
1182 if (!strcmp(tline->text, "&&"))
1183 return tokval->t_type = TOKEN_DBL_AND;
1184 if (!strcmp(tline->text, "^^"))
1185 return tokval->t_type = TOKEN_DBL_XOR;
1186 if (!strcmp(tline->text, "||"))
1187 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001188 }
1189
1190 /*
1191 * We have no other options: just return the first character of
1192 * the token text.
1193 */
1194 return tokval->t_type = tline->text[0];
1195}
1196
1197/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001198 * Compare a string to the name of an existing macro; this is a
1199 * simple wrapper which calls either strcmp or nasm_stricmp
1200 * depending on the value of the `casesense' parameter.
1201 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001202static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001203{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001204 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001205}
1206
1207/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001208 * Return the Context structure associated with a %$ token. Return
1209 * NULL, having _already_ reported an error condition, if the
1210 * context stack isn't deep enough for the supplied number of $
1211 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001212 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001213 * also scanned for such smacro, until it is found; if not -
1214 * only the context that directly results from the number of $'s
1215 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001216 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001217static Context *get_ctx(char *name, bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001218{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001219 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001220 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001221 int i;
1222
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001223 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001224 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001225
H. Peter Anvine2c80182005-01-15 22:15:51 +00001226 if (!cstk) {
1227 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1228 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001229 }
1230
H. Peter Anvine2c80182005-01-15 22:15:51 +00001231 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1232 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001233/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001234 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001235 if (!ctx) {
1236 error(ERR_NONFATAL, "`%s': context stack is only"
1237 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1238 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001239 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001240 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001241 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001242
H. Peter Anvine2c80182005-01-15 22:15:51 +00001243 do {
1244 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001245 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001246 while (m) {
1247 if (!mstrcmp(m->name, name, m->casesense))
1248 return ctx;
1249 m = m->next;
1250 }
1251 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001252 }
1253 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001254 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001255}
1256
1257/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001258 * Check to see if a file is already in a string list
1259 */
1260static bool in_list(const StrList *list, const char *str)
1261{
1262 while (list) {
1263 if (!strcmp(list->str, str))
1264 return true;
1265 list = list->next;
1266 }
1267 return false;
1268}
1269
1270/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001271 * Open an include file. This routine must always return a valid
1272 * file pointer if it returns - it's responsible for throwing an
1273 * ERR_FATAL and bombing out completely if not. It should also try
1274 * the include path one by one until it finds the file or reaches
1275 * the end of the path.
1276 */
H. Peter Anvin418ca702008-05-30 10:42:30 -07001277static FILE *inc_fopen(const char *file, StrList **dhead, StrList **dtail,
1278 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001279{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001280 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001281 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001282 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001283 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001284 size_t prefix_len = 0;
1285 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001286
H. Peter Anvine2c80182005-01-15 22:15:51 +00001287 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001288 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1289 memcpy(sl->str, prefix, prefix_len);
1290 memcpy(sl->str+prefix_len, file, len+1);
1291 fp = fopen(sl->str, "r");
H. Peter Anvin418ca702008-05-30 10:42:30 -07001292 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001293 sl->next = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07001294 *dtail = sl;
1295 dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001296 } else {
1297 nasm_free(sl);
1298 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299 if (fp)
1300 return fp;
H. Peter Anvin418ca702008-05-30 10:42:30 -07001301 if (!ip) {
1302 if (!missing_ok)
1303 break;
1304 prefix = NULL;
1305 } else {
1306 prefix = ip->path;
1307 ip = ip->next;
1308 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001309 if (prefix) {
1310 prefix_len = strlen(prefix);
1311 } else {
1312 /* -MG given and file not found */
H. Peter Anvin418ca702008-05-30 10:42:30 -07001313 if (dhead && !in_list(*dhead, file)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001314 sl = nasm_malloc(len+1+sizeof sl->next);
1315 sl->next = NULL;
1316 strcpy(sl->str, file);
H. Peter Anvin418ca702008-05-30 10:42:30 -07001317 *dtail = sl;
1318 dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001319 }
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001320 return NULL;
1321 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001322 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001323
H. Peter Anvin734b1882002-04-30 21:01:08 +00001324 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001326}
1327
1328/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001329 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001330 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001331 * return true if _any_ single-line macro of that name is defined.
1332 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001333 * `nparam' or no parameters is defined.
1334 *
1335 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001336 * defined, or nparam is -1, the address of the definition structure
1337 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001338 * is NULL, no action will be taken regarding its contents, and no
1339 * error will occur.
1340 *
1341 * Note that this is also called with nparam zero to resolve
1342 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001343 *
1344 * If you already know which context macro belongs to, you can pass
1345 * the context pointer as first parameter; if you won't but name begins
1346 * with %$ the context will be automatically computed. If all_contexts
1347 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001348 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001349static bool
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001350smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001351 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001352{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001353 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001354 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001355
H. Peter Anvin97a23472007-09-16 17:57:25 -07001356 if (ctx) {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001357 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001358 } else if (name[0] == '%' && name[1] == '$') {
1359 if (cstk)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001360 ctx = get_ctx(name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001362 return false; /* got to return _something_ */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001363 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001364 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001365 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001366 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001367 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001368
H. Peter Anvine2c80182005-01-15 22:15:51 +00001369 while (m) {
1370 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001371 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001372 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001373 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001374 *defn = m;
1375 else
1376 *defn = NULL;
1377 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001378 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001379 }
1380 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001381 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001382
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001383 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001384}
1385
1386/*
1387 * Count and mark off the parameters in a multi-line macro call.
1388 * This is called both from within the multi-line macro expansion
1389 * code, and also to mark off the default parameters when provided
1390 * in a %macro definition line.
1391 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001392static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001393{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001394 int paramsize, brace;
1395
1396 *nparam = paramsize = 0;
1397 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001398 while (t) {
1399 if (*nparam >= paramsize) {
1400 paramsize += PARAM_DELTA;
1401 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1402 }
1403 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001404 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001405 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001406 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001407 (*params)[(*nparam)++] = t;
1408 while (tok_isnt_(t, brace ? "}" : ","))
1409 t = t->next;
1410 if (t) { /* got a comma/brace */
1411 t = t->next;
1412 if (brace) {
1413 /*
1414 * Now we've found the closing brace, look further
1415 * for the comma.
1416 */
1417 skip_white_(t);
1418 if (tok_isnt_(t, ",")) {
1419 error(ERR_NONFATAL,
1420 "braces do not enclose all of macro parameter");
1421 while (tok_isnt_(t, ","))
1422 t = t->next;
1423 }
1424 if (t)
1425 t = t->next; /* eat the comma */
1426 }
1427 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001428 }
1429}
1430
1431/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001432 * Determine whether one of the various `if' conditions is true or
1433 * not.
1434 *
1435 * We must free the tline we get passed.
1436 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001437static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001438{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001439 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001440 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001441 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001442 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001443 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001444 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001445
1446 origline = tline;
1447
H. Peter Anvine2c80182005-01-15 22:15:51 +00001448 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001449 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001450 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001451 while (cstk && tline) {
1452 skip_white_(tline);
1453 if (!tline || tline->type != TOK_ID) {
1454 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001455 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001456 free_tlist(origline);
1457 return -1;
1458 }
1459 if (!nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001460 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001461 tline = tline->next;
1462 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001463 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001464
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001465 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001466 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001467 while (tline) {
1468 skip_white_(tline);
1469 if (!tline || (tline->type != TOK_ID &&
1470 (tline->type != TOK_PREPROC_ID ||
1471 tline->text[1] != '$'))) {
1472 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001473 "`%s' expects macro identifiers", pp_directives[ct]);
1474 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001475 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001476 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001477 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001478 tline = tline->next;
1479 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001480 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001481
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001482 case PPC_IFIDN:
1483 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001484 tline = expand_smacro(tline);
1485 t = tt = tline;
1486 while (tok_isnt_(tt, ","))
1487 tt = tt->next;
1488 if (!tt) {
1489 error(ERR_NONFATAL,
1490 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001491 pp_directives[ct]);
1492 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001493 }
1494 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001495 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001496 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1497 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1498 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001499 pp_directives[ct]);
1500 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001501 }
1502 if (t->type == TOK_WHITESPACE) {
1503 t = t->next;
1504 continue;
1505 }
1506 if (tt->type == TOK_WHITESPACE) {
1507 tt = tt->next;
1508 continue;
1509 }
1510 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001511 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001512 break;
1513 }
1514 /* Unify surrounding quotes for strings */
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07001515 /* XXX: this doesn't work anymore */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001516 if (t->type == TOK_STRING) {
1517 tt->text[0] = t->text[0];
1518 tt->text[strlen(tt->text) - 1] = t->text[0];
1519 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001520 if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001521 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001522 break;
1523 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001524
H. Peter Anvine2c80182005-01-15 22:15:51 +00001525 t = t->next;
1526 tt = tt->next;
1527 }
1528 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001529 j = false; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001530 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001531
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001532 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001533 {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001534 bool found = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001535 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001536
H. Peter Anvine2c80182005-01-15 22:15:51 +00001537 tline = tline->next;
1538 skip_white_(tline);
1539 tline = expand_id(tline);
1540 if (!tok_type_(tline, TOK_ID)) {
1541 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001542 "`%s' expects a macro name", pp_directives[ct]);
1543 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001544 }
1545 searching.name = nasm_strdup(tline->text);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001546 searching.casesense = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001547 searching.plus = false;
1548 searching.nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001549 searching.in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001550 searching.rep_nest = NULL;
1551 searching.nparam_min = 0;
1552 searching.nparam_max = INT_MAX;
1553 tline = expand_smacro(tline->next);
1554 skip_white_(tline);
1555 if (!tline) {
1556 } else if (!tok_type_(tline, TOK_NUMBER)) {
1557 error(ERR_NONFATAL,
1558 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001559 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001560 } else {
1561 searching.nparam_min = searching.nparam_max =
1562 readnum(tline->text, &j);
1563 if (j)
1564 error(ERR_NONFATAL,
1565 "unable to parse parameter count `%s'",
1566 tline->text);
1567 }
1568 if (tline && tok_is_(tline->next, "-")) {
1569 tline = tline->next->next;
1570 if (tok_is_(tline, "*"))
1571 searching.nparam_max = INT_MAX;
1572 else if (!tok_type_(tline, TOK_NUMBER))
1573 error(ERR_NONFATAL,
1574 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001575 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001576 else {
1577 searching.nparam_max = readnum(tline->text, &j);
1578 if (j)
1579 error(ERR_NONFATAL,
1580 "unable to parse parameter count `%s'",
1581 tline->text);
1582 if (searching.nparam_min > searching.nparam_max)
1583 error(ERR_NONFATAL,
1584 "minimum parameter count exceeds maximum");
1585 }
1586 }
1587 if (tline && tok_is_(tline->next, "+")) {
1588 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001589 searching.plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001590 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001591 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001592 while (mmac) {
1593 if (!strcmp(mmac->name, searching.name) &&
1594 (mmac->nparam_min <= searching.nparam_max
1595 || searching.plus)
1596 && (searching.nparam_min <= mmac->nparam_max
1597 || mmac->plus)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001598 found = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001599 break;
1600 }
1601 mmac = mmac->next;
1602 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001603 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001604 j = found;
1605 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001606 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001607
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001608 case PPC_IFID:
1609 needtype = TOK_ID;
1610 goto iftype;
1611 case PPC_IFNUM:
1612 needtype = TOK_NUMBER;
1613 goto iftype;
1614 case PPC_IFSTR:
1615 needtype = TOK_STRING;
1616 goto iftype;
1617
1618 iftype:
H. Peter Anvin134b9462008-02-16 17:01:40 -08001619 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001620
H. Peter Anvin927c92b2008-02-16 13:44:52 -08001621 while (tok_type_(t, TOK_WHITESPACE) ||
1622 (needtype == TOK_NUMBER &&
1623 tok_type_(t, TOK_OTHER) &&
1624 (t->text[0] == '-' || t->text[0] == '+') &&
1625 !t->text[1]))
1626 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001627
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001628 j = tok_type_(t, needtype);
1629 break;
1630
1631 case PPC_IFTOKEN:
1632 t = tline = expand_smacro(tline);
1633 while (tok_type_(t, TOK_WHITESPACE))
1634 t = t->next;
1635
1636 j = false;
1637 if (t) {
1638 t = t->next; /* Skip the actual token */
1639 while (tok_type_(t, TOK_WHITESPACE))
1640 t = t->next;
1641 j = !t; /* Should be nothing left */
1642 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001643 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001644
H. Peter Anvin134b9462008-02-16 17:01:40 -08001645 case PPC_IFEMPTY:
1646 t = tline = expand_smacro(tline);
1647 while (tok_type_(t, TOK_WHITESPACE))
1648 t = t->next;
1649
1650 j = !t; /* Should be empty */
1651 break;
1652
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001653 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001654 t = tline = expand_smacro(tline);
1655 tptr = &t;
1656 tokval.t_type = TOKEN_INVALID;
1657 evalresult = evaluate(ppscan, tptr, &tokval,
1658 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001659 if (!evalresult)
1660 return -1;
1661 if (tokval.t_type)
1662 error(ERR_WARNING,
1663 "trailing garbage after expression ignored");
1664 if (!is_simple(evalresult)) {
1665 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001666 "non-constant value given to `%s'", pp_directives[ct]);
1667 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001668 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001669 j = reloc_value(evalresult) != 0;
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001670 return j;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001671
H. Peter Anvine2c80182005-01-15 22:15:51 +00001672 default:
1673 error(ERR_FATAL,
1674 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001675 pp_directives[ct]);
1676 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001677 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001678
1679 free_tlist(origline);
1680 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001681
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001682fail:
1683 free_tlist(origline);
1684 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001685}
1686
1687/*
H. Peter Anvin418ca702008-05-30 10:42:30 -07001688 * Expand macros in a string. Used in %error directives (and it should
1689 * almost certainly be removed from there, too.)
1690 *
Keith Kaniosb7a89542007-04-12 02:40:54 +00001691 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001692 * The returned variable should ALWAYS be freed after usage.
1693 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001694void expand_macros_in_string(char **p)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001695{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001696 Token *line = tokenize(*p);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001697 line = expand_smacro(line);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001698 *p = detoken(line, false);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001699}
1700
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001701/*
1702 * Common code for defining an smacro
1703 */
1704static bool define_smacro(Context *ctx, char *mname, bool casesense,
1705 int nparam, Token *expansion)
1706{
1707 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001708 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001709
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001710 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1711 if (!smac) {
1712 error(ERR_WARNING,
1713 "single-line macro `%s' defined both with and"
1714 " without parameters", mname);
1715
1716 /* Some instances of the old code considered this a failure,
1717 some others didn't. What is the right thing to do here? */
1718 free_tlist(expansion);
1719 return false; /* Failure */
1720 } else {
1721 /*
1722 * We're redefining, so we have to take over an
1723 * existing SMacro structure. This means freeing
1724 * what was already in it.
1725 */
1726 nasm_free(smac->name);
1727 free_tlist(smac->expansion);
1728 }
1729 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001730 smtbl = ctx ? &ctx->localmac : &smacros;
1731 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001732 smac = nasm_malloc(sizeof(SMacro));
1733 smac->next = *smhead;
1734 *smhead = smac;
1735 }
1736 smac->name = nasm_strdup(mname);
1737 smac->casesense = casesense;
1738 smac->nparam = nparam;
1739 smac->expansion = expansion;
1740 smac->in_progress = false;
1741 return true; /* Success */
1742}
1743
1744/*
1745 * Undefine an smacro
1746 */
1747static void undef_smacro(Context *ctx, const char *mname)
1748{
1749 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001750 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001751
H. Peter Anvin166c2472008-05-28 12:28:58 -07001752 smtbl = ctx ? &ctx->localmac : &smacros;
1753 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001754
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001755 if (smhead) {
1756 /*
1757 * We now have a macro name... go hunt for it.
1758 */
1759 sp = smhead;
1760 while ((s = *sp) != NULL) {
1761 if (!mstrcmp(s->name, mname, s->casesense)) {
1762 *sp = s->next;
1763 nasm_free(s->name);
1764 free_tlist(s->expansion);
1765 nasm_free(s);
1766 } else {
1767 sp = &s->next;
1768 }
1769 }
1770 }
1771}
1772
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001773/*
1774 * Decode a size directive
1775 */
1776static int parse_size(const char *str) {
1777 static const char *size_names[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001778 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001779 static const int sizes[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001780 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001781
1782 return sizes[bsii(str, size_names, elements(size_names))+1];
1783}
1784
Ed Beroset3ab3f412002-06-11 03:31:49 +00001785/**
1786 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001787 * Find out if a line contains a preprocessor directive, and deal
1788 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07001789 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001790 * If a directive _is_ found, it is the responsibility of this routine
1791 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001792 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001793 * @param tline a pointer to the current tokeninzed line linked list
1794 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07001795 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001796 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001797static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001798{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001799 enum preproc_token i;
1800 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07001801 bool err;
1802 int nparam;
1803 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001804 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001805 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001806 int offset;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001807 char *p, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001808 Include *inc;
1809 Context *ctx;
1810 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001811 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001812 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1813 Line *l;
1814 struct tokenval tokval;
1815 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001816 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001817 int64_t count;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001818
1819 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001820
H. Peter Anvineba20a72002-04-30 20:53:55 +00001821 skip_white_(tline);
1822 if (!tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823 (tline->text[1] == '%' || tline->text[1] == '$'
1824 || tline->text[1] == '!'))
1825 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001826
H. Peter Anvin4169a472007-09-12 01:29:43 +00001827 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001828
1829 /*
1830 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001831 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001832 * we should ignore all directives except for condition
1833 * directives.
1834 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001835 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001836 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1837 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001838 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001839
1840 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001841 * If we're defining a macro or reading a %rep block, we should
1842 * ignore all directives except for %macro/%imacro (which
1843 * generate an error), %endm/%endmacro, and (only if we're in a
H. Peter Anvineba20a72002-04-30 20:53:55 +00001844 * %rep block) %endrep. If we're in a %rep block, another %rep
1845 * causes an error, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001846 */
1847 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001848 i != PP_ENDMACRO && i != PP_ENDM &&
1849 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1850 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001851 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001852
H. Peter Anvin4169a472007-09-12 01:29:43 +00001853 switch (i) {
1854 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001855 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1856 tline->text);
1857 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001858
H. Peter Anvine2c80182005-01-15 22:15:51 +00001859 case PP_STACKSIZE:
1860 /* Directive to tell NASM what the default stack size is. The
1861 * default is for a 16-bit stack, and this can be overriden with
1862 * %stacksize large.
1863 * the following form:
1864 *
1865 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1866 */
1867 tline = tline->next;
1868 if (tline && tline->type == TOK_WHITESPACE)
1869 tline = tline->next;
1870 if (!tline || tline->type != TOK_ID) {
1871 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1872 free_tlist(origline);
1873 return DIRECTIVE_FOUND;
1874 }
1875 if (nasm_stricmp(tline->text, "flat") == 0) {
1876 /* All subsequent ARG directives are for a 32-bit stack */
1877 StackSize = 4;
1878 StackPointer = "ebp";
1879 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001880 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08001881 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
1882 /* All subsequent ARG directives are for a 64-bit stack */
1883 StackSize = 8;
1884 StackPointer = "rbp";
1885 ArgOffset = 8;
1886 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001887 } else if (nasm_stricmp(tline->text, "large") == 0) {
1888 /* All subsequent ARG directives are for a 16-bit stack,
1889 * far function call.
1890 */
1891 StackSize = 2;
1892 StackPointer = "bp";
1893 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001894 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001895 } else if (nasm_stricmp(tline->text, "small") == 0) {
1896 /* All subsequent ARG directives are for a 16-bit stack,
1897 * far function call. We don't support near functions.
1898 */
1899 StackSize = 2;
1900 StackPointer = "bp";
1901 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001902 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001903 } else {
1904 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1905 free_tlist(origline);
1906 return DIRECTIVE_FOUND;
1907 }
1908 free_tlist(origline);
1909 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001910
H. Peter Anvine2c80182005-01-15 22:15:51 +00001911 case PP_ARG:
1912 /* TASM like ARG directive to define arguments to functions, in
1913 * the following form:
1914 *
1915 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1916 */
1917 offset = ArgOffset;
1918 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001919 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001920 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001921
H. Peter Anvine2c80182005-01-15 22:15:51 +00001922 /* Find the argument name */
1923 tline = tline->next;
1924 if (tline && tline->type == TOK_WHITESPACE)
1925 tline = tline->next;
1926 if (!tline || tline->type != TOK_ID) {
1927 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1928 free_tlist(origline);
1929 return DIRECTIVE_FOUND;
1930 }
1931 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001932
H. Peter Anvine2c80182005-01-15 22:15:51 +00001933 /* Find the argument size type */
1934 tline = tline->next;
1935 if (!tline || tline->type != TOK_OTHER
1936 || tline->text[0] != ':') {
1937 error(ERR_NONFATAL,
1938 "Syntax error processing `%%arg' directive");
1939 free_tlist(origline);
1940 return DIRECTIVE_FOUND;
1941 }
1942 tline = tline->next;
1943 if (!tline || tline->type != TOK_ID) {
1944 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1945 free_tlist(origline);
1946 return DIRECTIVE_FOUND;
1947 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001948
H. Peter Anvine2c80182005-01-15 22:15:51 +00001949 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001950 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001951 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001952 size = parse_size(tt->text);
1953 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001954 error(ERR_NONFATAL,
1955 "Invalid size type for `%%arg' missing directive");
1956 free_tlist(tt);
1957 free_tlist(origline);
1958 return DIRECTIVE_FOUND;
1959 }
1960 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001961
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001962 /* Round up to even stack slots */
1963 size = (size+StackSize-1) & ~(StackSize-1);
1964
H. Peter Anvine2c80182005-01-15 22:15:51 +00001965 /* Now define the macro for the argument */
1966 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1967 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001968 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001969 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001970
H. Peter Anvine2c80182005-01-15 22:15:51 +00001971 /* Move to the next argument in the list */
1972 tline = tline->next;
1973 if (tline && tline->type == TOK_WHITESPACE)
1974 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001975 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1976 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001977 free_tlist(origline);
1978 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001979
H. Peter Anvine2c80182005-01-15 22:15:51 +00001980 case PP_LOCAL:
1981 /* TASM like LOCAL directive to define local variables for a
1982 * function, in the following form:
1983 *
1984 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1985 *
1986 * The '= LocalSize' at the end is ignored by NASM, but is
1987 * required by TASM to define the local parameter size (and used
1988 * by the TASM macro package).
1989 */
1990 offset = LocalOffset;
1991 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001992 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001993 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001994
H. Peter Anvine2c80182005-01-15 22:15:51 +00001995 /* Find the argument name */
1996 tline = tline->next;
1997 if (tline && tline->type == TOK_WHITESPACE)
1998 tline = tline->next;
1999 if (!tline || tline->type != TOK_ID) {
2000 error(ERR_NONFATAL,
2001 "`%%local' missing argument parameter");
2002 free_tlist(origline);
2003 return DIRECTIVE_FOUND;
2004 }
2005 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002006
H. Peter Anvine2c80182005-01-15 22:15:51 +00002007 /* Find the argument size type */
2008 tline = tline->next;
2009 if (!tline || tline->type != TOK_OTHER
2010 || tline->text[0] != ':') {
2011 error(ERR_NONFATAL,
2012 "Syntax error processing `%%local' directive");
2013 free_tlist(origline);
2014 return DIRECTIVE_FOUND;
2015 }
2016 tline = tline->next;
2017 if (!tline || tline->type != TOK_ID) {
2018 error(ERR_NONFATAL,
2019 "`%%local' missing size type parameter");
2020 free_tlist(origline);
2021 return DIRECTIVE_FOUND;
2022 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002023
H. Peter Anvine2c80182005-01-15 22:15:51 +00002024 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002025 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002026 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002027 size = parse_size(tt->text);
2028 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002029 error(ERR_NONFATAL,
2030 "Invalid size type for `%%local' missing directive");
2031 free_tlist(tt);
2032 free_tlist(origline);
2033 return DIRECTIVE_FOUND;
2034 }
2035 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002036
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002037 /* Round up to even stack slots */
2038 size = (size+StackSize-1) & ~(StackSize-1);
2039
2040 offset += size; /* Negative offset, increment before */
2041
2042 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002043 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2044 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002045 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002046
H. Peter Anvine2c80182005-01-15 22:15:51 +00002047 /* Now define the assign to setup the enter_c macro correctly */
2048 snprintf(directive, sizeof(directive),
2049 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002050 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002051
H. Peter Anvine2c80182005-01-15 22:15:51 +00002052 /* Move to the next argument in the list */
2053 tline = tline->next;
2054 if (tline && tline->type == TOK_WHITESPACE)
2055 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002056 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2057 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002058 free_tlist(origline);
2059 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002060
H. Peter Anvine2c80182005-01-15 22:15:51 +00002061 case PP_CLEAR:
2062 if (tline->next)
2063 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002064 free_macros();
2065 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002066 free_tlist(origline);
2067 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002068
H. Peter Anvin418ca702008-05-30 10:42:30 -07002069 case PP_DEPEND:
2070 tline = expand_smacro(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002071 skip_white_(tline);
2072 if (!tline || (tline->type != TOK_STRING &&
2073 tline->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002074 error(ERR_NONFATAL, "`%%depend' expects a file name");
2075 free_tlist(origline);
2076 return DIRECTIVE_FOUND; /* but we did _something_ */
2077 }
2078 if (tline->next)
2079 error(ERR_WARNING,
2080 "trailing garbage after `%%depend' ignored");
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002081 p = tline->text;
2082 if (tline->type != TOK_INTERNAL_STRING)
2083 nasm_unquote(p);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002084 if (dephead && !in_list(*dephead, p)) {
2085 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2086 sl->next = NULL;
2087 strcpy(sl->str, p);
2088 *deptail = sl;
2089 deptail = &sl->next;
2090 }
2091 free_tlist(origline);
2092 return DIRECTIVE_FOUND;
2093
2094 case PP_INCLUDE:
2095 tline = expand_smacro(tline->next);
2096 skip_white_(tline);
2097
2098 if (!tline || (tline->type != TOK_STRING &&
2099 tline->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002100 error(ERR_NONFATAL, "`%%include' expects a file name");
2101 free_tlist(origline);
2102 return DIRECTIVE_FOUND; /* but we did _something_ */
2103 }
2104 if (tline->next)
2105 error(ERR_WARNING,
2106 "trailing garbage after `%%include' ignored");
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002107 p = tline->text;
2108 if (tline->type != TOK_INTERNAL_STRING)
2109 nasm_unquote(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002110 inc = nasm_malloc(sizeof(Include));
2111 inc->next = istk;
2112 inc->conds = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002113 inc->fp = inc_fopen(p, dephead, deptail, pass == 0);
2114 if (!inc->fp) {
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002115 /* -MG given but file not found */
2116 nasm_free(inc);
2117 } else {
2118 inc->fname = src_set_fname(p);
2119 inc->lineno = src_set_linnum(0);
2120 inc->lineinc = 1;
2121 inc->expansion = NULL;
2122 inc->mstk = NULL;
2123 istk = inc;
2124 list->uplevel(LIST_INCLUDE);
2125 }
2126 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002127 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002128
H. Peter Anvine2c80182005-01-15 22:15:51 +00002129 case PP_PUSH:
2130 tline = tline->next;
2131 skip_white_(tline);
2132 tline = expand_id(tline);
2133 if (!tok_type_(tline, TOK_ID)) {
2134 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2135 free_tlist(origline);
2136 return DIRECTIVE_FOUND; /* but we did _something_ */
2137 }
2138 if (tline->next)
2139 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2140 ctx = nasm_malloc(sizeof(Context));
2141 ctx->next = cstk;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002142 hash_init(&ctx->localmac, HASH_SMALL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002143 ctx->name = nasm_strdup(tline->text);
2144 ctx->number = unique++;
2145 cstk = ctx;
2146 free_tlist(origline);
2147 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002148
H. Peter Anvine2c80182005-01-15 22:15:51 +00002149 case PP_REPL:
2150 tline = tline->next;
2151 skip_white_(tline);
2152 tline = expand_id(tline);
2153 if (!tok_type_(tline, TOK_ID)) {
2154 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2155 free_tlist(origline);
2156 return DIRECTIVE_FOUND; /* but we did _something_ */
2157 }
2158 if (tline->next)
2159 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2160 if (!cstk)
2161 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2162 else {
2163 nasm_free(cstk->name);
2164 cstk->name = nasm_strdup(tline->text);
2165 }
2166 free_tlist(origline);
2167 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002168
H. Peter Anvine2c80182005-01-15 22:15:51 +00002169 case PP_POP:
2170 if (tline->next)
2171 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2172 if (!cstk)
2173 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2174 else
2175 ctx_pop();
2176 free_tlist(origline);
2177 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002178
H. Peter Anvine2c80182005-01-15 22:15:51 +00002179 case PP_ERROR:
2180 tline->next = expand_smacro(tline->next);
2181 tline = tline->next;
2182 skip_white_(tline);
2183 if (tok_type_(tline, TOK_STRING)) {
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002184 p = tline->text;
2185 nasm_unquote(p);
2186 expand_macros_in_string(&p); /* WHY? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002187 error(ERR_NONFATAL, "%s", p);
2188 nasm_free(p);
2189 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002190 p = detoken(tline, false);
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002191 error(ERR_WARNING, "%s", p); /* WARNING!??!! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002192 nasm_free(p);
2193 }
2194 free_tlist(origline);
2195 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002196
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002197 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002198 if (istk->conds && !emitting(istk->conds->state))
2199 j = COND_NEVER;
2200 else {
2201 j = if_condition(tline->next, i);
2202 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002203 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2204 }
2205 cond = nasm_malloc(sizeof(Cond));
2206 cond->next = istk->conds;
2207 cond->state = j;
2208 istk->conds = cond;
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002209 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002210 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002211
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002212 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002213 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002214 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002215 if (emitting(istk->conds->state)
2216 || istk->conds->state == COND_NEVER)
2217 istk->conds->state = COND_NEVER;
2218 else {
2219 /*
2220 * IMPORTANT: In the case of %if, we will already have
2221 * called expand_mmac_params(); however, if we're
2222 * processing an %elif we must have been in a
2223 * non-emitting mode, which would have inhibited
2224 * the normal invocation of expand_mmac_params(). Therefore,
2225 * we have to do it explicitly here.
2226 */
2227 j = if_condition(expand_mmac_params(tline->next), i);
2228 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002229 istk->conds->state =
2230 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2231 }
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002232 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002233 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002234
H. Peter Anvine2c80182005-01-15 22:15:51 +00002235 case PP_ELSE:
2236 if (tline->next)
2237 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2238 if (!istk->conds)
2239 error(ERR_FATAL, "`%%else': no matching `%%if'");
2240 if (emitting(istk->conds->state)
2241 || istk->conds->state == COND_NEVER)
2242 istk->conds->state = COND_ELSE_FALSE;
2243 else
2244 istk->conds->state = COND_ELSE_TRUE;
2245 free_tlist(origline);
2246 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002247
H. Peter Anvine2c80182005-01-15 22:15:51 +00002248 case PP_ENDIF:
2249 if (tline->next)
2250 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2251 if (!istk->conds)
2252 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2253 cond = istk->conds;
2254 istk->conds = cond->next;
2255 nasm_free(cond);
2256 free_tlist(origline);
2257 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002258
H. Peter Anvine2c80182005-01-15 22:15:51 +00002259 case PP_MACRO:
2260 case PP_IMACRO:
2261 if (defining)
2262 error(ERR_FATAL,
2263 "`%%%smacro': already defining a macro",
2264 (i == PP_IMACRO ? "i" : ""));
2265 tline = tline->next;
2266 skip_white_(tline);
2267 tline = expand_id(tline);
2268 if (!tok_type_(tline, TOK_ID)) {
2269 error(ERR_NONFATAL,
2270 "`%%%smacro' expects a macro name",
2271 (i == PP_IMACRO ? "i" : ""));
2272 return DIRECTIVE_FOUND;
2273 }
2274 defining = nasm_malloc(sizeof(MMacro));
2275 defining->name = nasm_strdup(tline->text);
2276 defining->casesense = (i == PP_MACRO);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002277 defining->plus = false;
2278 defining->nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002279 defining->in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002280 defining->rep_nest = NULL;
2281 tline = expand_smacro(tline->next);
2282 skip_white_(tline);
2283 if (!tok_type_(tline, TOK_NUMBER)) {
2284 error(ERR_NONFATAL,
2285 "`%%%smacro' expects a parameter count",
2286 (i == PP_IMACRO ? "i" : ""));
2287 defining->nparam_min = defining->nparam_max = 0;
2288 } else {
2289 defining->nparam_min = defining->nparam_max =
H. Peter Anvin70055962007-10-11 00:05:31 -07002290 readnum(tline->text, &err);
2291 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002292 error(ERR_NONFATAL,
2293 "unable to parse parameter count `%s'", tline->text);
2294 }
2295 if (tline && tok_is_(tline->next, "-")) {
2296 tline = tline->next->next;
2297 if (tok_is_(tline, "*"))
2298 defining->nparam_max = INT_MAX;
2299 else if (!tok_type_(tline, TOK_NUMBER))
2300 error(ERR_NONFATAL,
2301 "`%%%smacro' expects a parameter count after `-'",
2302 (i == PP_IMACRO ? "i" : ""));
2303 else {
H. Peter Anvin70055962007-10-11 00:05:31 -07002304 defining->nparam_max = readnum(tline->text, &err);
2305 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002306 error(ERR_NONFATAL,
2307 "unable to parse parameter count `%s'",
2308 tline->text);
2309 if (defining->nparam_min > defining->nparam_max)
2310 error(ERR_NONFATAL,
2311 "minimum parameter count exceeds maximum");
2312 }
2313 }
2314 if (tline && tok_is_(tline->next, "+")) {
2315 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002316 defining->plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002317 }
2318 if (tline && tok_type_(tline->next, TOK_ID) &&
2319 !nasm_stricmp(tline->next->text, ".nolist")) {
2320 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002321 defining->nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002322 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002323 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002324 while (mmac) {
2325 if (!strcmp(mmac->name, defining->name) &&
2326 (mmac->nparam_min <= defining->nparam_max
2327 || defining->plus)
2328 && (defining->nparam_min <= mmac->nparam_max
2329 || mmac->plus)) {
2330 error(ERR_WARNING,
2331 "redefining multi-line macro `%s'", defining->name);
2332 break;
2333 }
2334 mmac = mmac->next;
2335 }
2336 /*
2337 * Handle default parameters.
2338 */
2339 if (tline && tline->next) {
2340 defining->dlist = tline->next;
2341 tline->next = NULL;
2342 count_mmac_params(defining->dlist, &defining->ndefs,
2343 &defining->defaults);
2344 } else {
2345 defining->dlist = NULL;
2346 defining->defaults = NULL;
2347 }
2348 defining->expansion = NULL;
2349 free_tlist(origline);
2350 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002351
H. Peter Anvine2c80182005-01-15 22:15:51 +00002352 case PP_ENDM:
2353 case PP_ENDMACRO:
2354 if (!defining) {
2355 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2356 return DIRECTIVE_FOUND;
2357 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002358 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002359 defining->next = *mmhead;
2360 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002361 defining = NULL;
2362 free_tlist(origline);
2363 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002364
H. Peter Anvine2c80182005-01-15 22:15:51 +00002365 case PP_ROTATE:
2366 if (tline->next && tline->next->type == TOK_WHITESPACE)
2367 tline = tline->next;
2368 if (tline->next == NULL) {
2369 free_tlist(origline);
2370 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2371 return DIRECTIVE_FOUND;
2372 }
2373 t = expand_smacro(tline->next);
2374 tline->next = NULL;
2375 free_tlist(origline);
2376 tline = t;
2377 tptr = &t;
2378 tokval.t_type = TOKEN_INVALID;
2379 evalresult =
2380 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2381 free_tlist(tline);
2382 if (!evalresult)
2383 return DIRECTIVE_FOUND;
2384 if (tokval.t_type)
2385 error(ERR_WARNING,
2386 "trailing garbage after expression ignored");
2387 if (!is_simple(evalresult)) {
2388 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2389 return DIRECTIVE_FOUND;
2390 }
2391 mmac = istk->mstk;
2392 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2393 mmac = mmac->next_active;
2394 if (!mmac) {
2395 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2396 } else if (mmac->nparam == 0) {
2397 error(ERR_NONFATAL,
2398 "`%%rotate' invoked within macro without parameters");
2399 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002400 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002401
H. Peter Anvin25a99342007-09-22 17:45:45 -07002402 rotate %= (int)mmac->nparam;
2403 if (rotate < 0)
2404 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002405
H. Peter Anvin25a99342007-09-22 17:45:45 -07002406 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002407 }
2408 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002409
H. Peter Anvine2c80182005-01-15 22:15:51 +00002410 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002411 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002412 do {
2413 tline = tline->next;
2414 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002415
H. Peter Anvine2c80182005-01-15 22:15:51 +00002416 if (tok_type_(tline, TOK_ID) &&
2417 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002418 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002419 do {
2420 tline = tline->next;
2421 } while (tok_type_(tline, TOK_WHITESPACE));
2422 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002423
H. Peter Anvine2c80182005-01-15 22:15:51 +00002424 if (tline) {
2425 t = expand_smacro(tline);
2426 tptr = &t;
2427 tokval.t_type = TOKEN_INVALID;
2428 evalresult =
2429 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2430 if (!evalresult) {
2431 free_tlist(origline);
2432 return DIRECTIVE_FOUND;
2433 }
2434 if (tokval.t_type)
2435 error(ERR_WARNING,
2436 "trailing garbage after expression ignored");
2437 if (!is_simple(evalresult)) {
2438 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2439 return DIRECTIVE_FOUND;
2440 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002441 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002442 } else {
2443 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002444 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002445 }
2446 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002447
H. Peter Anvine2c80182005-01-15 22:15:51 +00002448 tmp_defining = defining;
2449 defining = nasm_malloc(sizeof(MMacro));
2450 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002451 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002452 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002453 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002454 defining->in_progress = count;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002455 defining->nparam_min = defining->nparam_max = 0;
2456 defining->defaults = NULL;
2457 defining->dlist = NULL;
2458 defining->expansion = NULL;
2459 defining->next_active = istk->mstk;
2460 defining->rep_nest = tmp_defining;
2461 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002462
H. Peter Anvine2c80182005-01-15 22:15:51 +00002463 case PP_ENDREP:
2464 if (!defining || defining->name) {
2465 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2466 return DIRECTIVE_FOUND;
2467 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002468
H. Peter Anvine2c80182005-01-15 22:15:51 +00002469 /*
2470 * Now we have a "macro" defined - although it has no name
2471 * and we won't be entering it in the hash tables - we must
2472 * push a macro-end marker for it on to istk->expansion.
2473 * After that, it will take care of propagating itself (a
2474 * macro-end marker line for a macro which is really a %rep
2475 * block will cause the macro to be re-expanded, complete
2476 * with another macro-end marker to ensure the process
2477 * continues) until the whole expansion is forcibly removed
2478 * from istk->expansion by a %exitrep.
2479 */
2480 l = nasm_malloc(sizeof(Line));
2481 l->next = istk->expansion;
2482 l->finishes = defining;
2483 l->first = NULL;
2484 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002485
H. Peter Anvine2c80182005-01-15 22:15:51 +00002486 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002487
H. Peter Anvine2c80182005-01-15 22:15:51 +00002488 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2489 tmp_defining = defining;
2490 defining = defining->rep_nest;
2491 free_tlist(origline);
2492 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002493
H. Peter Anvine2c80182005-01-15 22:15:51 +00002494 case PP_EXITREP:
2495 /*
2496 * We must search along istk->expansion until we hit a
2497 * macro-end marker for a macro with no name. Then we set
2498 * its `in_progress' flag to 0.
2499 */
2500 for (l = istk->expansion; l; l = l->next)
2501 if (l->finishes && !l->finishes->name)
2502 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002503
H. Peter Anvine2c80182005-01-15 22:15:51 +00002504 if (l)
2505 l->finishes->in_progress = 0;
2506 else
2507 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2508 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_XDEFINE:
2512 case PP_IXDEFINE:
2513 case PP_DEFINE:
2514 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002515 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002516
H. Peter Anvine2c80182005-01-15 22:15:51 +00002517 tline = tline->next;
2518 skip_white_(tline);
2519 tline = expand_id(tline);
2520 if (!tline || (tline->type != TOK_ID &&
2521 (tline->type != TOK_PREPROC_ID ||
2522 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002523 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2524 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002525 free_tlist(origline);
2526 return DIRECTIVE_FOUND;
2527 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002528
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002529 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002530
H. Peter Anvine2c80182005-01-15 22:15:51 +00002531 mname = tline->text;
2532 last = tline;
2533 param_start = tline = tline->next;
2534 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002535
H. Peter Anvine2c80182005-01-15 22:15:51 +00002536 /* Expand the macro definition now for %xdefine and %ixdefine */
2537 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2538 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002539
H. Peter Anvine2c80182005-01-15 22:15:51 +00002540 if (tok_is_(tline, "(")) {
2541 /*
2542 * This macro has parameters.
2543 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002544
H. Peter Anvine2c80182005-01-15 22:15:51 +00002545 tline = tline->next;
2546 while (1) {
2547 skip_white_(tline);
2548 if (!tline) {
2549 error(ERR_NONFATAL, "parameter identifier expected");
2550 free_tlist(origline);
2551 return DIRECTIVE_FOUND;
2552 }
2553 if (tline->type != TOK_ID) {
2554 error(ERR_NONFATAL,
2555 "`%s': parameter identifier expected",
2556 tline->text);
2557 free_tlist(origline);
2558 return DIRECTIVE_FOUND;
2559 }
2560 tline->type = TOK_SMAC_PARAM + nparam++;
2561 tline = tline->next;
2562 skip_white_(tline);
2563 if (tok_is_(tline, ",")) {
2564 tline = tline->next;
2565 continue;
2566 }
2567 if (!tok_is_(tline, ")")) {
2568 error(ERR_NONFATAL,
2569 "`)' expected to terminate macro template");
2570 free_tlist(origline);
2571 return DIRECTIVE_FOUND;
2572 }
2573 break;
2574 }
2575 last = tline;
2576 tline = tline->next;
2577 }
2578 if (tok_type_(tline, TOK_WHITESPACE))
2579 last = tline, tline = tline->next;
2580 macro_start = NULL;
2581 last->next = NULL;
2582 t = tline;
2583 while (t) {
2584 if (t->type == TOK_ID) {
2585 for (tt = param_start; tt; tt = tt->next)
2586 if (tt->type >= TOK_SMAC_PARAM &&
2587 !strcmp(tt->text, t->text))
2588 t->type = tt->type;
2589 }
2590 tt = t->next;
2591 t->next = macro_start;
2592 macro_start = t;
2593 t = tt;
2594 }
2595 /*
2596 * Good. We now have a macro name, a parameter count, and a
2597 * token list (in reverse order) for an expansion. We ought
2598 * to be OK just to create an SMacro, store it, and let
2599 * free_tlist have the rest of the line (which we have
2600 * carefully re-terminated after chopping off the expansion
2601 * from the end).
2602 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002603 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002604 free_tlist(origline);
2605 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002606
H. Peter Anvine2c80182005-01-15 22:15:51 +00002607 case PP_UNDEF:
2608 tline = tline->next;
2609 skip_white_(tline);
2610 tline = expand_id(tline);
2611 if (!tline || (tline->type != TOK_ID &&
2612 (tline->type != TOK_PREPROC_ID ||
2613 tline->text[1] != '$'))) {
2614 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2615 free_tlist(origline);
2616 return DIRECTIVE_FOUND;
2617 }
2618 if (tline->next) {
2619 error(ERR_WARNING,
2620 "trailing garbage after macro name ignored");
2621 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002622
H. Peter Anvine2c80182005-01-15 22:15:51 +00002623 /* Find the context that symbol belongs to */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002624 ctx = get_ctx(tline->text, false);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002625 undef_smacro(ctx, tline->text);
2626 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002627 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002628
H. Peter Anvin418ca702008-05-30 10:42:30 -07002629 case PP_PATHSEARCH:
2630 {
2631 FILE *fp;
2632 StrList *xsl = NULL;
2633
2634 casesense = true;
2635
2636 tline = tline->next;
2637 skip_white_(tline);
2638 tline = expand_id(tline);
2639 if (!tline || (tline->type != TOK_ID &&
2640 (tline->type != TOK_PREPROC_ID ||
2641 tline->text[1] != '$'))) {
2642 error(ERR_NONFATAL,
2643 "`%%pathsearch' expects a macro identifier as first parameter");
2644 free_tlist(origline);
2645 return DIRECTIVE_FOUND;
2646 }
2647 ctx = get_ctx(tline->text, false);
2648
2649 mname = tline->text;
2650 last = tline;
2651 tline = expand_smacro(tline->next);
2652 last->next = NULL;
2653
2654 t = tline;
2655 while (tok_type_(t, TOK_WHITESPACE))
2656 t = t->next;
2657
2658 if (!t || (t->type != TOK_STRING &&
2659 t->type != TOK_INTERNAL_STRING)) {
2660 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
2661 free_tlist(tline);
2662 free_tlist(origline);
2663 return DIRECTIVE_FOUND; /* but we did _something_ */
2664 }
2665 if (t->next)
2666 error(ERR_WARNING,
2667 "trailing garbage after `%%pathsearch' ignored");
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002668 p = tline->text;
2669 if (tline->type != TOK_INTERNAL_STRING)
2670 nasm_unquote(p);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002671
2672 fp = inc_fopen(p, &xsl, &xsl, true);
2673 if (fp) {
2674 p = xsl->str;
2675 fclose(fp); /* Don't actually care about the file */
2676 }
2677 macro_start = nasm_malloc(sizeof(*macro_start));
2678 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002679 macro_start->text = nasm_quote(p, strlen(p));
H. Peter Anvin418ca702008-05-30 10:42:30 -07002680 macro_start->type = TOK_STRING;
2681 macro_start->mac = NULL;
2682 if (xsl)
2683 nasm_free(xsl);
2684
2685 /*
2686 * We now have a macro name, an implicit parameter count of
2687 * zero, and a string token to use as an expansion. Create
2688 * and store an SMacro.
2689 */
2690 define_smacro(ctx, mname, casesense, 0, macro_start);
2691 free_tlist(tline);
2692 free_tlist(origline);
2693 return DIRECTIVE_FOUND;
2694 }
2695
H. Peter Anvine2c80182005-01-15 22:15:51 +00002696 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002697 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07002698
H. Peter Anvine2c80182005-01-15 22:15:51 +00002699 tline = tline->next;
2700 skip_white_(tline);
2701 tline = expand_id(tline);
2702 if (!tline || (tline->type != TOK_ID &&
2703 (tline->type != TOK_PREPROC_ID ||
2704 tline->text[1] != '$'))) {
2705 error(ERR_NONFATAL,
2706 "`%%strlen' expects a macro identifier as first parameter");
2707 free_tlist(origline);
2708 return DIRECTIVE_FOUND;
2709 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002710 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002711
H. Peter Anvine2c80182005-01-15 22:15:51 +00002712 mname = tline->text;
2713 last = tline;
2714 tline = expand_smacro(tline->next);
2715 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002716
H. Peter Anvine2c80182005-01-15 22:15:51 +00002717 t = tline;
2718 while (tok_type_(t, TOK_WHITESPACE))
2719 t = t->next;
2720 /* t should now point to the string */
2721 if (t->type != TOK_STRING) {
2722 error(ERR_NONFATAL,
2723 "`%%strlen` requires string as second parameter");
2724 free_tlist(tline);
2725 free_tlist(origline);
2726 return DIRECTIVE_FOUND;
2727 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002728
H. Peter Anvine2c80182005-01-15 22:15:51 +00002729 macro_start = nasm_malloc(sizeof(*macro_start));
2730 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002731 make_tok_num(macro_start, nasm_unquote(t->text));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002732 macro_start->mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002733
H. Peter Anvine2c80182005-01-15 22:15:51 +00002734 /*
2735 * We now have a macro name, an implicit parameter count of
2736 * zero, and a numeric token to use as an expansion. Create
2737 * and store an SMacro.
2738 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002739 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002740 free_tlist(tline);
2741 free_tlist(origline);
2742 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002743
H. Peter Anvine2c80182005-01-15 22:15:51 +00002744 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002745 {
2746 int64_t a1, a2;
2747 size_t len;
2748
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002749 casesense = true;
2750
H. Peter Anvine2c80182005-01-15 22:15:51 +00002751 tline = tline->next;
2752 skip_white_(tline);
2753 tline = expand_id(tline);
2754 if (!tline || (tline->type != TOK_ID &&
2755 (tline->type != TOK_PREPROC_ID ||
2756 tline->text[1] != '$'))) {
2757 error(ERR_NONFATAL,
2758 "`%%substr' expects a macro identifier as first parameter");
2759 free_tlist(origline);
2760 return DIRECTIVE_FOUND;
2761 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002762 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002763
H. Peter Anvine2c80182005-01-15 22:15:51 +00002764 mname = tline->text;
2765 last = tline;
2766 tline = expand_smacro(tline->next);
2767 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002768
H. Peter Anvine2c80182005-01-15 22:15:51 +00002769 t = tline->next;
2770 while (tok_type_(t, TOK_WHITESPACE))
2771 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002772
H. Peter Anvine2c80182005-01-15 22:15:51 +00002773 /* t should now point to the string */
2774 if (t->type != TOK_STRING) {
2775 error(ERR_NONFATAL,
2776 "`%%substr` requires string as second parameter");
2777 free_tlist(tline);
2778 free_tlist(origline);
2779 return DIRECTIVE_FOUND;
2780 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002781
H. Peter Anvine2c80182005-01-15 22:15:51 +00002782 tt = t->next;
2783 tptr = &tt;
2784 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002785 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
2786 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002787 if (!evalresult) {
2788 free_tlist(tline);
2789 free_tlist(origline);
2790 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002791 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002792 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2793 free_tlist(tline);
2794 free_tlist(origline);
2795 return DIRECTIVE_FOUND;
2796 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002797 a1 = evalresult->value-1;
2798
2799 while (tok_type_(tt, TOK_WHITESPACE))
2800 tt = tt->next;
2801 if (!tt) {
2802 a2 = 1; /* Backwards compatibility: one character */
2803 } else {
2804 tokval.t_type = TOKEN_INVALID;
2805 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
2806 pass, error, NULL);
2807 if (!evalresult) {
2808 free_tlist(tline);
2809 free_tlist(origline);
2810 return DIRECTIVE_FOUND;
2811 } else if (!is_simple(evalresult)) {
2812 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2813 free_tlist(tline);
2814 free_tlist(origline);
2815 return DIRECTIVE_FOUND;
2816 }
2817 a2 = evalresult->value;
2818 }
2819
2820 len = nasm_unquote(t->text);
2821 if (a2 < 0)
2822 a2 = a2+1+len-a1;
2823 if (a1+a2 > (int64_t)len)
2824 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002825
H. Peter Anvine2c80182005-01-15 22:15:51 +00002826 macro_start = nasm_malloc(sizeof(*macro_start));
2827 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002828 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002829 macro_start->type = TOK_STRING;
2830 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002831
H. Peter Anvine2c80182005-01-15 22:15:51 +00002832 /*
2833 * We now have a macro name, an implicit parameter count of
2834 * zero, and a numeric token to use as an expansion. Create
2835 * and store an SMacro.
2836 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002837 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002838 free_tlist(tline);
2839 free_tlist(origline);
2840 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002841 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002842
H. Peter Anvine2c80182005-01-15 22:15:51 +00002843 case PP_ASSIGN:
2844 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002845 casesense = (i == PP_ASSIGN);
2846
H. Peter Anvine2c80182005-01-15 22:15:51 +00002847 tline = tline->next;
2848 skip_white_(tline);
2849 tline = expand_id(tline);
2850 if (!tline || (tline->type != TOK_ID &&
2851 (tline->type != TOK_PREPROC_ID ||
2852 tline->text[1] != '$'))) {
2853 error(ERR_NONFATAL,
2854 "`%%%sassign' expects a macro identifier",
2855 (i == PP_IASSIGN ? "i" : ""));
2856 free_tlist(origline);
2857 return DIRECTIVE_FOUND;
2858 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002859 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002860
H. Peter Anvine2c80182005-01-15 22:15:51 +00002861 mname = tline->text;
2862 last = tline;
2863 tline = expand_smacro(tline->next);
2864 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002865
H. Peter Anvine2c80182005-01-15 22:15:51 +00002866 t = tline;
2867 tptr = &t;
2868 tokval.t_type = TOKEN_INVALID;
2869 evalresult =
2870 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2871 free_tlist(tline);
2872 if (!evalresult) {
2873 free_tlist(origline);
2874 return DIRECTIVE_FOUND;
2875 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002876
H. Peter Anvine2c80182005-01-15 22:15:51 +00002877 if (tokval.t_type)
2878 error(ERR_WARNING,
2879 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00002880
H. Peter Anvine2c80182005-01-15 22:15:51 +00002881 if (!is_simple(evalresult)) {
2882 error(ERR_NONFATAL,
2883 "non-constant value given to `%%%sassign'",
2884 (i == PP_IASSIGN ? "i" : ""));
2885 free_tlist(origline);
2886 return DIRECTIVE_FOUND;
2887 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002888
H. Peter Anvine2c80182005-01-15 22:15:51 +00002889 macro_start = nasm_malloc(sizeof(*macro_start));
2890 macro_start->next = NULL;
2891 make_tok_num(macro_start, reloc_value(evalresult));
2892 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002893
H. Peter Anvine2c80182005-01-15 22:15:51 +00002894 /*
2895 * We now have a macro name, an implicit parameter count of
2896 * zero, and a numeric token to use as an expansion. Create
2897 * and store an SMacro.
2898 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002899 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002900 free_tlist(origline);
2901 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002902
H. Peter Anvine2c80182005-01-15 22:15:51 +00002903 case PP_LINE:
2904 /*
2905 * Syntax is `%line nnn[+mmm] [filename]'
2906 */
2907 tline = tline->next;
2908 skip_white_(tline);
2909 if (!tok_type_(tline, TOK_NUMBER)) {
2910 error(ERR_NONFATAL, "`%%line' expects line number");
2911 free_tlist(origline);
2912 return DIRECTIVE_FOUND;
2913 }
H. Peter Anvin70055962007-10-11 00:05:31 -07002914 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002915 m = 1;
2916 tline = tline->next;
2917 if (tok_is_(tline, "+")) {
2918 tline = tline->next;
2919 if (!tok_type_(tline, TOK_NUMBER)) {
2920 error(ERR_NONFATAL, "`%%line' expects line increment");
2921 free_tlist(origline);
2922 return DIRECTIVE_FOUND;
2923 }
H. Peter Anvin70055962007-10-11 00:05:31 -07002924 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002925 tline = tline->next;
2926 }
2927 skip_white_(tline);
2928 src_set_linnum(k);
2929 istk->lineinc = m;
2930 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002931 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002932 }
2933 free_tlist(origline);
2934 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002935
H. Peter Anvine2c80182005-01-15 22:15:51 +00002936 default:
2937 error(ERR_FATAL,
2938 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00002939 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002940 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002941 }
Ed Beroset3ab3f412002-06-11 03:31:49 +00002942 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002943}
2944
2945/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002946 * Ensure that a macro parameter contains a condition code and
2947 * nothing else. Return the condition code index if so, or -1
2948 * otherwise.
2949 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002950static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002951{
H. Peter Anvin76690a12002-04-30 20:52:49 +00002952 Token *tt;
2953 int i, j, k, m;
2954
H. Peter Anvin25a99342007-09-22 17:45:45 -07002955 if (!t)
2956 return -1; /* Probably a %+ without a space */
2957
H. Peter Anvineba20a72002-04-30 20:53:55 +00002958 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002959 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002960 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002961 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002962 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002963 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002964 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002965
2966 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00002967 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002968 while (j - i > 1) {
2969 k = (j + i) / 2;
2970 m = nasm_stricmp(t->text, conditions[k]);
2971 if (m == 0) {
2972 i = k;
2973 j = -2;
2974 break;
2975 } else if (m < 0) {
2976 j = k;
2977 } else
2978 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002979 }
2980 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002981 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002982 return i;
2983}
2984
2985/*
2986 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2987 * %-n) and MMacro-local identifiers (%%foo).
2988 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002989static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002990{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002991 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002992
2993 tail = &thead;
2994 thead = NULL;
2995
H. Peter Anvine2c80182005-01-15 22:15:51 +00002996 while (tline) {
2997 if (tline->type == TOK_PREPROC_ID &&
2998 (((tline->text[1] == '+' || tline->text[1] == '-')
2999 && tline->text[2]) || tline->text[1] == '%'
3000 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003001 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003002 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003003 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003004 unsigned int n;
3005 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003006 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003007
H. Peter Anvine2c80182005-01-15 22:15:51 +00003008 t = tline;
3009 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003010
H. Peter Anvine2c80182005-01-15 22:15:51 +00003011 mac = istk->mstk;
3012 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3013 mac = mac->next_active;
3014 if (!mac)
3015 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3016 else
3017 switch (t->text[1]) {
3018 /*
3019 * We have to make a substitution of one of the
3020 * forms %1, %-1, %+1, %%foo, %0.
3021 */
3022 case '0':
3023 type = TOK_NUMBER;
3024 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3025 text = nasm_strdup(tmpbuf);
3026 break;
3027 case '%':
3028 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003029 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00003030 mac->unique);
3031 text = nasm_strcat(tmpbuf, t->text + 2);
3032 break;
3033 case '-':
3034 n = atoi(t->text + 2) - 1;
3035 if (n >= mac->nparam)
3036 tt = NULL;
3037 else {
3038 if (mac->nparam > 1)
3039 n = (n + mac->rotate) % mac->nparam;
3040 tt = mac->params[n];
3041 }
3042 cc = find_cc(tt);
3043 if (cc == -1) {
3044 error(ERR_NONFATAL,
3045 "macro parameter %d is not a condition code",
3046 n + 1);
3047 text = NULL;
3048 } else {
3049 type = TOK_ID;
3050 if (inverse_ccs[cc] == -1) {
3051 error(ERR_NONFATAL,
3052 "condition code `%s' is not invertible",
3053 conditions[cc]);
3054 text = NULL;
3055 } else
3056 text =
3057 nasm_strdup(conditions[inverse_ccs[cc]]);
3058 }
3059 break;
3060 case '+':
3061 n = atoi(t->text + 2) - 1;
3062 if (n >= mac->nparam)
3063 tt = NULL;
3064 else {
3065 if (mac->nparam > 1)
3066 n = (n + mac->rotate) % mac->nparam;
3067 tt = mac->params[n];
3068 }
3069 cc = find_cc(tt);
3070 if (cc == -1) {
3071 error(ERR_NONFATAL,
3072 "macro parameter %d is not a condition code",
3073 n + 1);
3074 text = NULL;
3075 } else {
3076 type = TOK_ID;
3077 text = nasm_strdup(conditions[cc]);
3078 }
3079 break;
3080 default:
3081 n = atoi(t->text + 1) - 1;
3082 if (n >= mac->nparam)
3083 tt = NULL;
3084 else {
3085 if (mac->nparam > 1)
3086 n = (n + mac->rotate) % mac->nparam;
3087 tt = mac->params[n];
3088 }
3089 if (tt) {
3090 for (i = 0; i < mac->paramlen[n]; i++) {
3091 *tail = new_Token(NULL, tt->type, tt->text, 0);
3092 tail = &(*tail)->next;
3093 tt = tt->next;
3094 }
3095 }
3096 text = NULL; /* we've done it here */
3097 break;
3098 }
3099 if (!text) {
3100 delete_Token(t);
3101 } else {
3102 *tail = t;
3103 tail = &t->next;
3104 t->type = type;
3105 nasm_free(t->text);
3106 t->text = text;
3107 t->mac = NULL;
3108 }
3109 continue;
3110 } else {
3111 t = *tail = tline;
3112 tline = tline->next;
3113 t->mac = NULL;
3114 tail = &t->next;
3115 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003116 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003117 *tail = NULL;
3118 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003119 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003120 switch (t->type) {
3121 case TOK_WHITESPACE:
3122 if (tt->type == TOK_WHITESPACE) {
3123 t->next = delete_Token(tt);
3124 }
3125 break;
3126 case TOK_ID:
3127 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003128 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003129 nasm_free(t->text);
3130 t->text = tmp;
3131 t->next = delete_Token(tt);
3132 }
3133 break;
3134 case TOK_NUMBER:
3135 if (tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003136 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003137 nasm_free(t->text);
3138 t->text = tmp;
3139 t->next = delete_Token(tt);
3140 }
3141 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003142 default:
3143 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003144 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003145
H. Peter Anvin76690a12002-04-30 20:52:49 +00003146 return thead;
3147}
3148
3149/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003150 * Expand all single-line macro calls made in the given line.
3151 * Return the expanded version of the line. The original is deemed
3152 * to be destroyed in the process. (In reality we'll just move
3153 * Tokens from input to output a lot of the time, rather than
3154 * actually bothering to destroy and replicate.)
3155 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003156#define DEADMAN_LIMIT (1 << 20)
3157
H. Peter Anvine2c80182005-01-15 22:15:51 +00003158static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003159{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003160 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003161 struct hash_table *smtbl;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003162 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003163 Token **params;
3164 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003165 unsigned int nparam, sparam;
3166 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003167 Token *org_tline = tline;
3168 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003169 char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003170 int deadman = DEADMAN_LIMIT;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003171
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003172 /*
3173 * Trick: we should avoid changing the start token pointer since it can
3174 * be contained in "next" field of other token. Because of this
3175 * we allocate a copy of first token and work with it; at the end of
3176 * routine we copy it back
3177 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003178 if (org_tline) {
3179 tline =
3180 new_Token(org_tline->next, org_tline->type, org_tline->text,
3181 0);
3182 tline->mac = org_tline->mac;
3183 nasm_free(org_tline->text);
3184 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003185 }
3186
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003187again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003188 tail = &thead;
3189 thead = NULL;
3190
H. Peter Anvine2c80182005-01-15 22:15:51 +00003191 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003192 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003193 error(ERR_NONFATAL, "interminable macro recursion");
3194 break;
3195 }
3196
H. Peter Anvine2c80182005-01-15 22:15:51 +00003197 if ((mname = tline->text)) {
3198 /* if this token is a local macro, look in local context */
H. Peter Anvinb037a672008-05-29 19:08:08 -07003199 ctx = NULL;
3200 smtbl = &smacros;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003201 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003202 ctx = get_ctx(mname, true);
H. Peter Anvinb037a672008-05-29 19:08:08 -07003203 if (ctx)
3204 smtbl = &ctx->localmac;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003205 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07003206 head = (SMacro *) hash_findix(smtbl, mname);
H. Peter Anvin072771e2008-05-22 13:17:51 -07003207
H. Peter Anvine2c80182005-01-15 22:15:51 +00003208 /*
3209 * We've hit an identifier. As in is_mmacro below, we first
3210 * check whether the identifier is a single-line macro at
3211 * all, then think about checking for parameters if
3212 * necessary.
3213 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003214 for (m = head; m; m = m->next)
3215 if (!mstrcmp(m->name, mname, m->casesense))
3216 break;
3217 if (m) {
3218 mstart = tline;
3219 params = NULL;
3220 paramsize = NULL;
3221 if (m->nparam == 0) {
3222 /*
3223 * Simple case: the macro is parameterless. Discard the
3224 * one token that the macro call took, and push the
3225 * expansion back on the to-do stack.
3226 */
3227 if (!m->expansion) {
3228 if (!strcmp("__FILE__", m->name)) {
3229 int32_t num = 0;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003230 char *file;
3231 src_get(&num, &file);
3232 tline->text = nasm_quote(file, strlen(file));
H. Peter Anvin97a23472007-09-16 17:57:25 -07003233 tline->type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003234 nasm_free(file);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003235 continue;
3236 }
3237 if (!strcmp("__LINE__", m->name)) {
3238 nasm_free(tline->text);
3239 make_tok_num(tline, src_get_linnum());
3240 continue;
3241 }
3242 if (!strcmp("__BITS__", m->name)) {
3243 nasm_free(tline->text);
3244 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003245 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003246 }
3247 tline = delete_Token(tline);
3248 continue;
3249 }
3250 } else {
3251 /*
3252 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003253 * exists and takes parameters. We must find the
3254 * parameters in the call, count them, find the SMacro
3255 * that corresponds to that form of the macro call, and
3256 * substitute for the parameters when we expand. What a
3257 * pain.
3258 */
3259 /*tline = tline->next;
3260 skip_white_(tline); */
3261 do {
3262 t = tline->next;
3263 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003264 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003265 t->text = NULL;
3266 t = tline->next = delete_Token(t);
3267 }
3268 tline = t;
3269 } while (tok_type_(tline, TOK_WHITESPACE));
3270 if (!tok_is_(tline, "(")) {
3271 /*
3272 * This macro wasn't called with parameters: ignore
3273 * the call. (Behaviour borrowed from gnu cpp.)
3274 */
3275 tline = mstart;
3276 m = NULL;
3277 } else {
3278 int paren = 0;
3279 int white = 0;
3280 brackets = 0;
3281 nparam = 0;
3282 sparam = PARAM_DELTA;
3283 params = nasm_malloc(sparam * sizeof(Token *));
3284 params[0] = tline->next;
3285 paramsize = nasm_malloc(sparam * sizeof(int));
3286 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003287 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003288 /*
3289 * For some unusual expansions
3290 * which concatenates function call
3291 */
3292 t = tline->next;
3293 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003294 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003295 t->text = NULL;
3296 t = tline->next = delete_Token(t);
3297 }
3298 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003299
H. Peter Anvine2c80182005-01-15 22:15:51 +00003300 if (!tline) {
3301 error(ERR_NONFATAL,
3302 "macro call expects terminating `)'");
3303 break;
3304 }
3305 if (tline->type == TOK_WHITESPACE
3306 && brackets <= 0) {
3307 if (paramsize[nparam])
3308 white++;
3309 else
3310 params[nparam] = tline->next;
3311 continue; /* parameter loop */
3312 }
3313 if (tline->type == TOK_OTHER
3314 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003315 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003316 if (ch == ',' && !paren && brackets <= 0) {
3317 if (++nparam >= sparam) {
3318 sparam += PARAM_DELTA;
3319 params = nasm_realloc(params,
3320 sparam *
3321 sizeof(Token
3322 *));
3323 paramsize =
3324 nasm_realloc(paramsize,
3325 sparam *
3326 sizeof(int));
3327 }
3328 params[nparam] = tline->next;
3329 paramsize[nparam] = 0;
3330 white = 0;
3331 continue; /* parameter loop */
3332 }
3333 if (ch == '{' &&
3334 (brackets > 0 || (brackets == 0 &&
3335 !paramsize[nparam])))
3336 {
3337 if (!(brackets++)) {
3338 params[nparam] = tline->next;
3339 continue; /* parameter loop */
3340 }
3341 }
3342 if (ch == '}' && brackets > 0)
3343 if (--brackets == 0) {
3344 brackets = -1;
3345 continue; /* parameter loop */
3346 }
3347 if (ch == '(' && !brackets)
3348 paren++;
3349 if (ch == ')' && brackets <= 0)
3350 if (--paren < 0)
3351 break;
3352 }
3353 if (brackets < 0) {
3354 brackets = 0;
3355 error(ERR_NONFATAL, "braces do not "
3356 "enclose all of macro parameter");
3357 }
3358 paramsize[nparam] += white + 1;
3359 white = 0;
3360 } /* parameter loop */
3361 nparam++;
3362 while (m && (m->nparam != nparam ||
3363 mstrcmp(m->name, mname,
3364 m->casesense)))
3365 m = m->next;
3366 if (!m)
3367 error(ERR_WARNING | ERR_WARN_MNP,
3368 "macro `%s' exists, "
3369 "but not taking %d parameters",
3370 mstart->text, nparam);
3371 }
3372 }
3373 if (m && m->in_progress)
3374 m = NULL;
3375 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003376 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003377 * Design question: should we handle !tline, which
3378 * indicates missing ')' here, or expand those
3379 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003380 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003381 */
3382 nasm_free(params);
3383 nasm_free(paramsize);
3384 tline = mstart;
3385 } else {
3386 /*
3387 * Expand the macro: we are placed on the last token of the
3388 * call, so that we can easily split the call from the
3389 * following tokens. We also start by pushing an SMAC_END
3390 * token for the cycle removal.
3391 */
3392 t = tline;
3393 if (t) {
3394 tline = t->next;
3395 t->next = NULL;
3396 }
3397 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3398 tt->mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003399 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003400 tline = tt;
3401 for (t = m->expansion; t; t = t->next) {
3402 if (t->type >= TOK_SMAC_PARAM) {
3403 Token *pcopy = tline, **ptail = &pcopy;
3404 Token *ttt, *pt;
3405 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003406
H. Peter Anvine2c80182005-01-15 22:15:51 +00003407 ttt = params[t->type - TOK_SMAC_PARAM];
3408 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3409 --i >= 0;) {
3410 pt = *ptail =
3411 new_Token(tline, ttt->type, ttt->text,
3412 0);
3413 ptail = &pt->next;
3414 ttt = ttt->next;
3415 }
3416 tline = pcopy;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -07003417 } else if (t->type == TOK_PREPROC_Q) {
3418 tt = new_Token(tline, TOK_ID, mname, 0);
3419 tline = tt;
3420 } else if (t->type == TOK_PREPROC_QQ) {
3421 tt = new_Token(tline, TOK_ID, m->name, 0);
3422 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003423 } else {
3424 tt = new_Token(tline, t->type, t->text, 0);
3425 tline = tt;
3426 }
3427 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003428
H. Peter Anvine2c80182005-01-15 22:15:51 +00003429 /*
3430 * Having done that, get rid of the macro call, and clean
3431 * up the parameters.
3432 */
3433 nasm_free(params);
3434 nasm_free(paramsize);
3435 free_tlist(mstart);
3436 continue; /* main token loop */
3437 }
3438 }
3439 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003440
H. Peter Anvine2c80182005-01-15 22:15:51 +00003441 if (tline->type == TOK_SMAC_END) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003442 tline->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003443 tline = delete_Token(tline);
3444 } else {
3445 t = *tail = tline;
3446 tline = tline->next;
3447 t->mac = NULL;
3448 t->next = NULL;
3449 tail = &t->next;
3450 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003451 }
3452
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003453 /*
3454 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003455 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003456 * TOK_IDs should be concatenated.
3457 * Also we look for %+ tokens and concatenate the tokens before and after
3458 * them (without white spaces in between).
3459 */
3460 t = thead;
3461 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003462 while (t) {
3463 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3464 t = t->next;
3465 if (!t || !t->next)
3466 break;
3467 if (t->next->type == TOK_ID ||
3468 t->next->type == TOK_PREPROC_ID ||
3469 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003470 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003471 nasm_free(t->text);
3472 t->next = delete_Token(t->next);
3473 t->text = p;
3474 rescan = 1;
3475 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3476 t->next->next->type == TOK_PREPROC_ID &&
3477 strcmp(t->next->next->text, "%+") == 0) {
3478 /* free the next whitespace, the %+ token and next whitespace */
3479 int i;
3480 for (i = 1; i <= 3; i++) {
3481 if (!t->next
3482 || (i != 2 && t->next->type != TOK_WHITESPACE))
3483 break;
3484 t->next = delete_Token(t->next);
3485 } /* endfor */
3486 } else
3487 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003488 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003489 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003490 if (rescan) {
3491 tline = thead;
3492 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003493 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003494
H. Peter Anvine2c80182005-01-15 22:15:51 +00003495 if (org_tline) {
3496 if (thead) {
3497 *org_tline = *thead;
3498 /* since we just gave text to org_line, don't free it */
3499 thead->text = NULL;
3500 delete_Token(thead);
3501 } else {
3502 /* the expression expanded to empty line;
3503 we can't return NULL for some reasons
3504 we just set the line to a single WHITESPACE token. */
3505 memset(org_tline, 0, sizeof(*org_tline));
3506 org_tline->text = NULL;
3507 org_tline->type = TOK_WHITESPACE;
3508 }
3509 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003510 }
3511
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003512 return thead;
3513}
3514
3515/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003516 * Similar to expand_smacro but used exclusively with macro identifiers
3517 * right before they are fetched in. The reason is that there can be
3518 * identifiers consisting of several subparts. We consider that if there
3519 * are more than one element forming the name, user wants a expansion,
3520 * otherwise it will be left as-is. Example:
3521 *
3522 * %define %$abc cde
3523 *
3524 * the identifier %$abc will be left as-is so that the handler for %define
3525 * will suck it and define the corresponding value. Other case:
3526 *
3527 * %define _%$abc cde
3528 *
3529 * In this case user wants name to be expanded *before* %define starts
3530 * working, so we'll expand %$abc into something (if it has a value;
3531 * otherwise it will be left as-is) then concatenate all successive
3532 * PP_IDs into one.
3533 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003534static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003535{
3536 Token *cur, *oldnext = NULL;
3537
H. Peter Anvin734b1882002-04-30 21:01:08 +00003538 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003539 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003540
3541 cur = tline;
3542 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003543 (cur->next->type == TOK_ID ||
3544 cur->next->type == TOK_PREPROC_ID
3545 || cur->next->type == TOK_NUMBER))
3546 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003547
3548 /* If identifier consists of just one token, don't expand */
3549 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003550 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003551
H. Peter Anvine2c80182005-01-15 22:15:51 +00003552 if (cur) {
3553 oldnext = cur->next; /* Detach the tail past identifier */
3554 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003555 }
3556
H. Peter Anvin734b1882002-04-30 21:01:08 +00003557 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003558
H. Peter Anvine2c80182005-01-15 22:15:51 +00003559 if (cur) {
3560 /* expand_smacro possibly changhed tline; re-scan for EOL */
3561 cur = tline;
3562 while (cur && cur->next)
3563 cur = cur->next;
3564 if (cur)
3565 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003566 }
3567
3568 return tline;
3569}
3570
3571/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003572 * Determine whether the given line constitutes a multi-line macro
3573 * call, and return the MMacro structure called if so. Doesn't have
3574 * to check for an initial label - that's taken care of in
3575 * expand_mmacro - but must check numbers of parameters. Guaranteed
3576 * to be called with tline->type == TOK_ID, so the putative macro
3577 * name is easy to find.
3578 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003579static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003580{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003581 MMacro *head, *m;
3582 Token **params;
3583 int nparam;
3584
H. Peter Anvin166c2472008-05-28 12:28:58 -07003585 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003586
3587 /*
3588 * Efficiency: first we see if any macro exists with the given
3589 * name. If not, we can return NULL immediately. _Then_ we
3590 * count the parameters, and then we look further along the
3591 * list if necessary to find the proper MMacro.
3592 */
3593 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003594 if (!mstrcmp(m->name, tline->text, m->casesense))
3595 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003596 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003597 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003598
3599 /*
3600 * OK, we have a potential macro. Count and demarcate the
3601 * parameters.
3602 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003603 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003604
3605 /*
3606 * So we know how many parameters we've got. Find the MMacro
3607 * structure that handles this number.
3608 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003609 while (m) {
3610 if (m->nparam_min <= nparam
3611 && (m->plus || nparam <= m->nparam_max)) {
3612 /*
3613 * This one is right. Just check if cycle removal
3614 * prohibits us using it before we actually celebrate...
3615 */
3616 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003617#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003618 error(ERR_NONFATAL,
3619 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003620#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003621 nasm_free(params);
3622 return NULL;
3623 }
3624 /*
3625 * It's right, and we can use it. Add its default
3626 * parameters to the end of our list if necessary.
3627 */
3628 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3629 params =
3630 nasm_realloc(params,
3631 ((m->nparam_min + m->ndefs +
3632 1) * sizeof(*params)));
3633 while (nparam < m->nparam_min + m->ndefs) {
3634 params[nparam] = m->defaults[nparam - m->nparam_min];
3635 nparam++;
3636 }
3637 }
3638 /*
3639 * If we've gone over the maximum parameter count (and
3640 * we're in Plus mode), ignore parameters beyond
3641 * nparam_max.
3642 */
3643 if (m->plus && nparam > m->nparam_max)
3644 nparam = m->nparam_max;
3645 /*
3646 * Then terminate the parameter list, and leave.
3647 */
3648 if (!params) { /* need this special case */
3649 params = nasm_malloc(sizeof(*params));
3650 nparam = 0;
3651 }
3652 params[nparam] = NULL;
3653 *params_array = params;
3654 return m;
3655 }
3656 /*
3657 * This one wasn't right: look for the next one with the
3658 * same name.
3659 */
3660 for (m = m->next; m; m = m->next)
3661 if (!mstrcmp(m->name, tline->text, m->casesense))
3662 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003663 }
3664
3665 /*
3666 * After all that, we didn't find one with the right number of
3667 * parameters. Issue a warning, and fail to expand the macro.
3668 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003669 error(ERR_WARNING | ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003670 "macro `%s' exists, but not taking %d parameters",
3671 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003672 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003673 return NULL;
3674}
3675
3676/*
3677 * Expand the multi-line macro call made by the given line, if
3678 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003679 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003680 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003681static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003682{
3683 Token *startline = tline;
3684 Token *label = NULL;
3685 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003686 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003687 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003688 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003689 int i, nparam, *paramlen;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003690
3691 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003692 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07003693 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003694 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003695 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003696 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003697 m = is_mmacro(t, &params);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003698 if (!m) {
3699 Token *last;
3700 /*
3701 * We have an id which isn't a macro call. We'll assume
3702 * it might be a label; we'll also check to see if a
3703 * colon follows it. Then, if there's another id after
3704 * that lot, we'll check it again for macro-hood.
3705 */
3706 label = last = t;
3707 t = t->next;
3708 if (tok_type_(t, TOK_WHITESPACE))
3709 last = t, t = t->next;
3710 if (tok_is_(t, ":")) {
3711 dont_prepend = 1;
3712 last = t, t = t->next;
3713 if (tok_type_(t, TOK_WHITESPACE))
3714 last = t, t = t->next;
3715 }
3716 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3717 return 0;
3718 last->next = NULL;
3719 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003720 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003721
3722 /*
3723 * Fix up the parameters: this involves stripping leading and
3724 * trailing whitespace, then stripping braces if they are
3725 * present.
3726 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003727 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003728 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003729
H. Peter Anvine2c80182005-01-15 22:15:51 +00003730 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003731 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003732 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003733
H. Peter Anvine2c80182005-01-15 22:15:51 +00003734 t = params[i];
3735 skip_white_(t);
3736 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003737 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003738 params[i] = t;
3739 paramlen[i] = 0;
3740 while (t) {
3741 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3742 break; /* ... because we have hit a comma */
3743 if (comma && t->type == TOK_WHITESPACE
3744 && tok_is_(t->next, ","))
3745 break; /* ... or a space then a comma */
3746 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3747 break; /* ... or a brace */
3748 t = t->next;
3749 paramlen[i]++;
3750 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003751 }
3752
3753 /*
3754 * OK, we have a MMacro structure together with a set of
3755 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003756 * copies of each Line on to istk->expansion. Substitution of
3757 * parameter tokens and macro-local tokens doesn't get done
3758 * until the single-line macro substitution process; this is
3759 * because delaying them allows us to change the semantics
3760 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003761 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003762 * First, push an end marker on to istk->expansion, mark this
3763 * macro as in progress, and set up its invocation-specific
3764 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003765 */
3766 ll = nasm_malloc(sizeof(Line));
3767 ll->next = istk->expansion;
3768 ll->finishes = m;
3769 ll->first = NULL;
3770 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003771
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003772 m->in_progress = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003773 m->params = params;
3774 m->iline = tline;
3775 m->nparam = nparam;
3776 m->rotate = 0;
3777 m->paramlen = paramlen;
3778 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003779 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003780
3781 m->next_active = istk->mstk;
3782 istk->mstk = m;
3783
H. Peter Anvine2c80182005-01-15 22:15:51 +00003784 for (l = m->expansion; l; l = l->next) {
3785 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003786
H. Peter Anvine2c80182005-01-15 22:15:51 +00003787 ll = nasm_malloc(sizeof(Line));
3788 ll->finishes = NULL;
3789 ll->next = istk->expansion;
3790 istk->expansion = ll;
3791 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003792
H. Peter Anvine2c80182005-01-15 22:15:51 +00003793 for (t = l->first; t; t = t->next) {
3794 Token *x = t;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003795 switch (t->type) {
3796 case TOK_PREPROC_Q:
3797 tt = *tail = new_Token(NULL, TOK_ID, mtok->text, 0);
3798 break;
3799 case TOK_PREPROC_QQ:
3800 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
3801 break;
3802 case TOK_PREPROC_ID:
3803 if (t->text[1] == '0' && t->text[2] == '0') {
3804 dont_prepend = -1;
3805 x = label;
3806 if (!x)
3807 continue;
3808 }
3809 /* fall through */
3810 default:
3811 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3812 break;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003813 }
H. Peter Anvince2233b2008-05-25 21:57:00 -07003814 tail = &tt->next;
3815 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003816 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003817 }
3818
3819 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00003820 * If we had a label, push it on as the first line of
3821 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003822 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003823 if (label) {
3824 if (dont_prepend < 0)
3825 free_tlist(startline);
3826 else {
3827 ll = nasm_malloc(sizeof(Line));
3828 ll->finishes = NULL;
3829 ll->next = istk->expansion;
3830 istk->expansion = ll;
3831 ll->first = startline;
3832 if (!dont_prepend) {
3833 while (label->next)
3834 label = label->next;
3835 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3836 }
3837 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003838 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003839
H. Peter Anvin734b1882002-04-30 21:01:08 +00003840 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003841
H. Peter Anvineba20a72002-04-30 20:53:55 +00003842 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003843}
3844
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003845/*
3846 * Since preprocessor always operate only on the line that didn't
3847 * arrived yet, we should always use ERR_OFFBY1. Also since user
3848 * won't want to see same error twice (preprocessing is done once
3849 * per pass) we will want to show errors only during pass one.
3850 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003851static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003852{
3853 va_list arg;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003854 char buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003855
3856 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003857 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003858 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003859
H. Peter Anvin734b1882002-04-30 21:01:08 +00003860 va_start(arg, fmt);
Ed Beroset19f927a2004-12-15 17:07:03 +00003861 vsnprintf(buff, sizeof(buff), fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003862 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003863
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003864 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003865 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3866 istk->mstk->lineno, buff);
3867 else
3868 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003869}
3870
H. Peter Anvin734b1882002-04-30 21:01:08 +00003871static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003872pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07003873 ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003874{
H. Peter Anvin99941bf2002-05-14 17:44:03 +00003875 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003876 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003877 istk = nasm_malloc(sizeof(Include));
3878 istk->next = NULL;
3879 istk->conds = NULL;
3880 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003881 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003882 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00003883 istk->fname = NULL;
3884 src_set_fname(nasm_strdup(file));
3885 src_set_linnum(0);
3886 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003887 if (!istk->fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003888 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3889 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003890 defining = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003891 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003892 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003893 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07003894 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003895 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07003896 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003897 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003898 any_extrastdmac = (extrastdmac != NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003899 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003900 evaluate = eval;
3901 pass = apass;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07003902 dephead = deptail = deplist;
3903 if (deplist) {
3904 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
3905 sl->next = NULL;
3906 strcpy(sl->str, file);
3907 *deptail = sl;
3908 deptail = &sl->next;
3909 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003910}
3911
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003912static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003913{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003914 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003915 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003916
H. Peter Anvine2c80182005-01-15 22:15:51 +00003917 while (1) {
3918 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003919 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00003920 * buffer or from the input file.
3921 */
3922 tline = NULL;
3923 while (istk->expansion && istk->expansion->finishes) {
3924 Line *l = istk->expansion;
3925 if (!l->finishes->name && l->finishes->in_progress > 1) {
3926 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003927
H. Peter Anvine2c80182005-01-15 22:15:51 +00003928 /*
3929 * This is a macro-end marker for a macro with no
3930 * name, which means it's not really a macro at all
3931 * but a %rep block, and the `in_progress' field is
3932 * more than 1, meaning that we still need to
3933 * repeat. (1 means the natural last repetition; 0
3934 * means termination by %exitrep.) We have
3935 * therefore expanded up to the %endrep, and must
3936 * push the whole block on to the expansion buffer
3937 * again. We don't bother to remove the macro-end
3938 * marker: we'd only have to generate another one
3939 * if we did.
3940 */
3941 l->finishes->in_progress--;
3942 for (l = l->finishes->expansion; l; l = l->next) {
3943 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003944
H. Peter Anvine2c80182005-01-15 22:15:51 +00003945 ll = nasm_malloc(sizeof(Line));
3946 ll->next = istk->expansion;
3947 ll->finishes = NULL;
3948 ll->first = NULL;
3949 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003950
H. Peter Anvine2c80182005-01-15 22:15:51 +00003951 for (t = l->first; t; t = t->next) {
3952 if (t->text || t->type == TOK_WHITESPACE) {
3953 tt = *tail =
3954 new_Token(NULL, t->type, t->text, 0);
3955 tail = &tt->next;
3956 }
3957 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003958
H. Peter Anvine2c80182005-01-15 22:15:51 +00003959 istk->expansion = ll;
3960 }
3961 } else {
3962 /*
3963 * Check whether a `%rep' was started and not ended
3964 * within this macro expansion. This can happen and
3965 * should be detected. It's a fatal error because
3966 * I'm too confused to work out how to recover
3967 * sensibly from it.
3968 */
3969 if (defining) {
3970 if (defining->name)
3971 error(ERR_PANIC,
3972 "defining with name in expansion");
3973 else if (istk->mstk->name)
3974 error(ERR_FATAL,
3975 "`%%rep' without `%%endrep' within"
3976 " expansion of macro `%s'",
3977 istk->mstk->name);
3978 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00003979
H. Peter Anvine2c80182005-01-15 22:15:51 +00003980 /*
3981 * FIXME: investigate the relationship at this point between
3982 * istk->mstk and l->finishes
3983 */
3984 {
3985 MMacro *m = istk->mstk;
3986 istk->mstk = m->next_active;
3987 if (m->name) {
3988 /*
3989 * This was a real macro call, not a %rep, and
3990 * therefore the parameter information needs to
3991 * be freed.
3992 */
3993 nasm_free(m->params);
3994 free_tlist(m->iline);
3995 nasm_free(m->paramlen);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003996 l->finishes->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003997 } else
3998 free_mmacro(m);
3999 }
4000 istk->expansion = l->next;
4001 nasm_free(l);
4002 list->downlevel(LIST_MACRO);
4003 }
4004 }
4005 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004006
H. Peter Anvine2c80182005-01-15 22:15:51 +00004007 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004008 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004009 Line *l = istk->expansion;
4010 if (istk->mstk)
4011 istk->mstk->lineno++;
4012 tline = l->first;
4013 istk->expansion = l->next;
4014 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004015 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004016 list->line(LIST_MACRO, p);
4017 nasm_free(p);
4018 break;
4019 }
4020 line = read_line();
4021 if (line) { /* from the current input file */
4022 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004023 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004024 nasm_free(line);
4025 break;
4026 }
4027 /*
4028 * The current file has ended; work down the istk
4029 */
4030 {
4031 Include *i = istk;
4032 fclose(i->fp);
4033 if (i->conds)
4034 error(ERR_FATAL,
4035 "expected `%%endif' before end of file");
4036 /* only set line and file name if there's a next node */
4037 if (i->next) {
4038 src_set_linnum(i->lineno);
4039 nasm_free(src_set_fname(i->fname));
4040 }
4041 istk = i->next;
4042 list->downlevel(LIST_INCLUDE);
4043 nasm_free(i);
4044 if (!istk)
4045 return NULL;
4046 }
4047 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004048
H. Peter Anvine2c80182005-01-15 22:15:51 +00004049 /*
4050 * We must expand MMacro parameters and MMacro-local labels
4051 * _before_ we plunge into directive processing, to cope
4052 * with things like `%define something %1' such as STRUC
4053 * uses. Unless we're _defining_ a MMacro, in which case
4054 * those tokens should be left alone to go into the
4055 * definition; and unless we're in a non-emitting
4056 * condition, in which case we don't want to meddle with
4057 * anything.
4058 */
4059 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
4060 tline = expand_mmac_params(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004061
H. Peter Anvine2c80182005-01-15 22:15:51 +00004062 /*
4063 * Check the line to see if it's a preprocessor directive.
4064 */
4065 if (do_directive(tline) == DIRECTIVE_FOUND) {
4066 continue;
4067 } else if (defining) {
4068 /*
4069 * We're defining a multi-line macro. We emit nothing
4070 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004071 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004072 */
4073 Line *l = nasm_malloc(sizeof(Line));
4074 l->next = defining->expansion;
4075 l->first = tline;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004076 l->finishes = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004077 defining->expansion = l;
4078 continue;
4079 } else if (istk->conds && !emitting(istk->conds->state)) {
4080 /*
4081 * We're in a non-emitting branch of a condition block.
4082 * Emit nothing at all, not even a blank line: when we
4083 * emerge from the condition we'll give a line-number
4084 * directive so we keep our place correctly.
4085 */
4086 free_tlist(tline);
4087 continue;
4088 } else if (istk->mstk && !istk->mstk->in_progress) {
4089 /*
4090 * We're in a %rep block which has been terminated, so
4091 * we're walking through to the %endrep without
4092 * emitting anything. Emit nothing at all, not even a
4093 * blank line: when we emerge from the %rep block we'll
4094 * give a line-number directive so we keep our place
4095 * correctly.
4096 */
4097 free_tlist(tline);
4098 continue;
4099 } else {
4100 tline = expand_smacro(tline);
4101 if (!expand_mmacro(tline)) {
4102 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004103 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004104 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004105 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004106 free_tlist(tline);
4107 break;
4108 } else {
4109 continue; /* expand_mmacro calls free_tlist */
4110 }
4111 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004112 }
4113
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004114 return line;
4115}
4116
H. Peter Anvine2c80182005-01-15 22:15:51 +00004117static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004118{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004119 if (defining) {
4120 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
4121 defining->name);
4122 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004123 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004124 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004125 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004126 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004127 while (istk) {
4128 Include *i = istk;
4129 istk = istk->next;
4130 fclose(i->fp);
4131 nasm_free(i->fname);
4132 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004133 }
4134 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004135 ctx_pop();
4136 if (pass == 0) {
4137 free_llist(predef);
4138 delete_Blocks();
4139 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004140}
4141
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004142void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004143{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004144 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004145
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004146 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004147 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004148 i->next = NULL;
4149
H. Peter Anvine2c80182005-01-15 22:15:51 +00004150 if (ipath != NULL) {
4151 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004152 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004153 j = j->next;
4154 j->next = i;
4155 } else {
4156 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004157 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004158}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004159
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004160void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004161{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004162 Token *inc, *space, *name;
4163 Line *l;
4164
H. Peter Anvin734b1882002-04-30 21:01:08 +00004165 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4166 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4167 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004168
4169 l = nasm_malloc(sizeof(Line));
4170 l->next = predef;
4171 l->first = inc;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004172 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004173 predef = l;
4174}
4175
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004176void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004177{
4178 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004179 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004180 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004181
4182 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004183 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4184 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004185 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004186 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004187 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004188 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004189 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004190
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004191 l = nasm_malloc(sizeof(Line));
4192 l->next = predef;
4193 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004194 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004195 predef = l;
4196}
4197
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004198void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004199{
4200 Token *def, *space;
4201 Line *l;
4202
H. Peter Anvin734b1882002-04-30 21:01:08 +00004203 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4204 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004205 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004206
4207 l = nasm_malloc(sizeof(Line));
4208 l->next = predef;
4209 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004210 l->finishes = false;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004211 predef = l;
4212}
4213
Keith Kaniosb7a89542007-04-12 02:40:54 +00004214/*
4215 * Added by Keith Kanios:
4216 *
4217 * This function is used to assist with "runtime" preprocessor
4218 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4219 *
4220 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4221 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4222 */
4223
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004224void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004225{
4226 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004227
Keith Kaniosb7a89542007-04-12 02:40:54 +00004228 def = tokenize(definition);
4229 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4230 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004231
Keith Kaniosb7a89542007-04-12 02:40:54 +00004232}
4233
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004234void pp_extra_stdmac(const char **macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004235{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004236 extrastdmac = macros;
4237}
4238
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004239static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004240{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004241 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004242 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004243 tok->text = nasm_strdup(numbuf);
4244 tok->type = TOK_NUMBER;
4245}
4246
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004247Preproc nasmpp = {
4248 pp_reset,
4249 pp_getline,
4250 pp_cleanup
4251};