blob: b34710d16f1c7cf2f12056aac08745662d625093 [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 Anvinc751e862008-06-09 10:18:45 -0700396static Token *new_Token(Token * next, enum pp_token_type type,
397 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000398static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000399
400/*
401 * Macros for safe checking of token pointers, avoid *(NULL)
402 */
403#define tok_type_(x,t) ((x) && (x)->type == (t))
404#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
405#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
406#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000407
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000408/* Handle TASM specific directives, which do not contain a % in
409 * front of them. We do it here because I could not find any other
410 * place to do it for the moment, and it is a hack (ideally it would
411 * be nice to be able to use the NASM pre-processor to do it).
412 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000413static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000414{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000415 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000416 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000417
418 /* Skip whitespace */
419 while (isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000420 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000421
422 /* Binary search for the directive name */
423 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000424 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000425 len = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000426 while (!isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000427 len++;
428 if (len) {
429 oldchar = p[len];
430 p[len] = 0;
431 while (j - i > 1) {
432 k = (j + i) / 2;
433 m = nasm_stricmp(p, tasm_directives[k]);
434 if (m == 0) {
435 /* We have found a directive, so jam a % in front of it
436 * so that NASM will then recognise it as one if it's own.
437 */
438 p[len] = oldchar;
439 len = strlen(p);
440 oldline = line;
441 line = nasm_malloc(len + 2);
442 line[0] = '%';
443 if (k == TM_IFDIFI) {
444 /* NASM does not recognise IFDIFI, so we convert it to
445 * %ifdef BOGUS. This is not used in NASM comaptible
446 * code, but does need to parse for the TASM macro
447 * package.
448 */
449 strcpy(line + 1, "ifdef BOGUS");
450 } else {
451 memcpy(line + 1, p, len + 1);
452 }
453 nasm_free(oldline);
454 return line;
455 } else if (m < 0) {
456 j = k;
457 } else
458 i = k;
459 }
460 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000461 }
462 return line;
463}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000464
H. Peter Anvin76690a12002-04-30 20:52:49 +0000465/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000466 * The pre-preprocessing stage... This function translates line
467 * number indications as they emerge from GNU cpp (`# lineno "file"
468 * flags') into NASM preprocessor line number indications (`%line
469 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000470 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000471static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000472{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000473 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000474 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000475
H. Peter Anvine2c80182005-01-15 22:15:51 +0000476 if (line[0] == '#' && line[1] == ' ') {
477 oldline = line;
478 fname = oldline + 2;
479 lineno = atoi(fname);
480 fname += strspn(fname, "0123456789 ");
481 if (*fname == '"')
482 fname++;
483 fnlen = strcspn(fname, "\"");
484 line = nasm_malloc(20 + fnlen);
485 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
486 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000487 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000488 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000489 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000490 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000491}
492
493/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000494 * Free a linked list of tokens.
495 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000496static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000497{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000498 while (list) {
499 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000500 }
501}
502
503/*
504 * Free a linked list of lines.
505 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000506static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000507{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000508 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000509 while (list) {
510 l = list;
511 list = list->next;
512 free_tlist(l->first);
513 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000514 }
515}
516
517/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000518 * Free an MMacro
519 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000520static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000521{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000522 nasm_free(m->name);
523 free_tlist(m->dlist);
524 nasm_free(m->defaults);
525 free_llist(m->expansion);
526 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000527}
528
529/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700530 * Free all currently defined macros, and free the hash tables
531 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700532static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700533{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700534 SMacro *s;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700535 const char *key;
536 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700537
H. Peter Anvin072771e2008-05-22 13:17:51 -0700538 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700539 nasm_free((void *)key);
540 while (s) {
541 SMacro *ns = s->next;
542 nasm_free(s->name);
543 free_tlist(s->expansion);
544 nasm_free(s);
545 s = ns;
546 }
547 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700548 hash_free(smt);
549}
550
551static void free_mmacro_table(struct hash_table *mmt)
552{
553 MMacro *m;
554 const char *key;
555 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700556
557 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700558 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700559 nasm_free((void *)key);
560 while (m) {
561 MMacro *nm = m->next;
562 free_mmacro(m);
563 m = nm;
564 }
565 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700566 hash_free(mmt);
567}
568
569static void free_macros(void)
570{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700571 free_smacro_table(&smacros);
572 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700573}
574
575/*
576 * Initialize the hash tables
577 */
578static void init_macros(void)
579{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700580 hash_init(&smacros, HASH_LARGE);
581 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700582}
583
584/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000585 * Pop the context stack.
586 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000587static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000588{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000589 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000590
591 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700592 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000593 nasm_free(c->name);
594 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000595}
596
H. Peter Anvin072771e2008-05-22 13:17:51 -0700597/*
598 * Search for a key in the hash index; adding it if necessary
599 * (in which case we initialize the data pointer to NULL.)
600 */
601static void **
602hash_findi_add(struct hash_table *hash, const char *str)
603{
604 struct hash_insert hi;
605 void **r;
606 char *strx;
607
608 r = hash_findi(hash, str, &hi);
609 if (r)
610 return r;
611
612 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
613 return hash_add(&hi, strx, NULL);
614}
615
616/*
617 * Like hash_findi, but returns the data element rather than a pointer
618 * to it. Used only when not adding a new element, hence no third
619 * argument.
620 */
621static void *
622hash_findix(struct hash_table *hash, const char *str)
623{
624 void **p;
625
626 p = hash_findi(hash, str, NULL);
627 return p ? *p : NULL;
628}
629
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000630#define BUF_DELTA 512
631/*
632 * Read a line from the top file in istk, handling multiple CR/LFs
633 * at the end of the line read, and handling spurious ^Zs. Will
634 * return lines from the standard macro set if this has not already
635 * been done.
636 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000637static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000638{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000639 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000640 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000641
H. Peter Anvine2c80182005-01-15 22:15:51 +0000642 if (stdmacpos) {
643 if (*stdmacpos) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000644 char *ret = nasm_strdup(*stdmacpos++);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000645 if (!*stdmacpos && any_extrastdmac) {
646 stdmacpos = extrastdmac;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700647 any_extrastdmac = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000648 return ret;
649 }
650 /*
651 * Nasty hack: here we push the contents of `predef' on
652 * to the top-level expansion stack, since this is the
653 * most convenient way to implement the pre-include and
654 * pre-define features.
655 */
656 if (!*stdmacpos) {
657 Line *pd, *l;
658 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000659
H. Peter Anvine2c80182005-01-15 22:15:51 +0000660 for (pd = predef; pd; pd = pd->next) {
661 head = NULL;
662 tail = &head;
663 for (t = pd->first; t; t = t->next) {
664 *tail = new_Token(NULL, t->type, t->text, 0);
665 tail = &(*tail)->next;
666 }
667 l = nasm_malloc(sizeof(Line));
668 l->next = istk->expansion;
669 l->first = head;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700670 l->finishes = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000671 istk->expansion = l;
672 }
673 }
674 return ret;
675 } else {
676 stdmacpos = NULL;
677 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000678 }
679
680 bufsize = BUF_DELTA;
681 buffer = nasm_malloc(BUF_DELTA);
682 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000683 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000684 while (1) {
685 q = fgets(p, bufsize - (p - buffer), istk->fp);
686 if (!q)
687 break;
688 p += strlen(p);
689 if (p > buffer && p[-1] == '\n') {
690 /* Convert backslash-CRLF line continuation sequences into
691 nothing at all (for DOS and Windows) */
692 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
693 p -= 3;
694 *p = 0;
695 continued_count++;
696 }
697 /* Also convert backslash-LF line continuation sequences into
698 nothing at all (for Unix) */
699 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
700 p -= 2;
701 *p = 0;
702 continued_count++;
703 } else {
704 break;
705 }
706 }
707 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000708 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000709 bufsize += BUF_DELTA;
710 buffer = nasm_realloc(buffer, bufsize);
711 p = buffer + offset; /* prevent stale-pointer problems */
712 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000713 }
714
H. Peter Anvine2c80182005-01-15 22:15:51 +0000715 if (!q && p == buffer) {
716 nasm_free(buffer);
717 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000718 }
719
H. Peter Anvine2c80182005-01-15 22:15:51 +0000720 src_set_linnum(src_get_linnum() + istk->lineinc +
721 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000722
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000723 /*
724 * Play safe: remove CRs as well as LFs, if any of either are
725 * present at the end of the line.
726 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000727 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000728 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000729
730 /*
731 * Handle spurious ^Z, which may be inserted into source files
732 * by some file transfer utilities.
733 */
734 buffer[strcspn(buffer, "\032")] = '\0';
735
H. Peter Anvin734b1882002-04-30 21:01:08 +0000736 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000737
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000738 return buffer;
739}
740
741/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000742 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000743 * don't need to parse the value out of e.g. numeric tokens: we
744 * simply split one string into many.
745 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000746static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000747{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000748 char *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000749 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000750 Token *list = NULL;
751 Token *t, **tail = &list;
752
H. Peter Anvine2c80182005-01-15 22:15:51 +0000753 while (*line) {
754 p = line;
755 if (*p == '%') {
756 p++;
757 if (isdigit(*p) ||
758 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
759 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
760 do {
761 p++;
762 }
763 while (isdigit(*p));
764 type = TOK_PREPROC_ID;
765 } else if (*p == '{') {
766 p++;
767 while (*p && *p != '}') {
768 p[-1] = *p;
769 p++;
770 }
771 p[-1] = '\0';
772 if (*p)
773 p++;
774 type = TOK_PREPROC_ID;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700775 } else if (*p == '?') {
776 type = TOK_PREPROC_Q; /* %? */
777 p++;
778 if (*p == '?') {
779 type = TOK_PREPROC_QQ; /* %?? */
780 p++;
781 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000782 } else if (isidchar(*p) ||
783 ((*p == '!' || *p == '%' || *p == '$') &&
784 isidchar(p[1]))) {
785 do {
786 p++;
787 }
788 while (isidchar(*p));
789 type = TOK_PREPROC_ID;
790 } else {
791 type = TOK_OTHER;
792 if (*p == '%')
793 p++;
794 }
795 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
796 type = TOK_ID;
797 p++;
798 while (*p && isidchar(*p))
799 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700800 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000801 /*
802 * A string token.
803 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000804 type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700805 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000806
H. Peter Anvine2c80182005-01-15 22:15:51 +0000807 if (*p) {
808 p++;
809 } else {
810 error(ERR_WARNING, "unterminated string");
811 /* Handling unterminated strings by UNV */
812 /* type = -1; */
813 }
814 } else if (isnumstart(*p)) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700815 bool is_hex = false;
816 bool is_float = false;
817 bool has_e = false;
818 char c, *r;
819
H. Peter Anvine2c80182005-01-15 22:15:51 +0000820 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700821 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000822 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700823
824 if (*p == '$') {
825 p++;
826 is_hex = true;
827 }
828
829 for (;;) {
830 c = *p++;
831
832 if (!is_hex && (c == 'e' || c == 'E')) {
833 has_e = true;
834 if (*p == '+' || *p == '-') {
835 /* e can only be followed by +/- if it is either a
836 prefixed hex number or a floating-point number */
837 p++;
838 is_float = true;
839 }
840 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
841 is_hex = true;
842 } else if (c == 'P' || c == 'p') {
843 is_float = true;
844 if (*p == '+' || *p == '-')
845 p++;
846 } else if (isnumchar(c) || c == '_')
847 ; /* just advance */
848 else if (c == '.') {
849 /* we need to deal with consequences of the legacy
850 parser, like "1.nolist" being two tokens
851 (TOK_NUMBER, TOK_ID) here; at least give it
852 a shot for now. In the future, we probably need
853 a flex-based scanner with proper pattern matching
854 to do it as well as it can be done. Nothing in
855 the world is going to help the person who wants
856 0x123.p16 interpreted as two tokens, though. */
857 r = p;
858 while (*r == '_')
859 r++;
860
861 if (isdigit(*r) || (is_hex && isxdigit(*r)) ||
862 (!is_hex && (*r == 'e' || *r == 'E')) ||
863 (*r == 'p' || *r == 'P')) {
864 p = r;
865 is_float = true;
866 } else
867 break; /* Terminate the token */
868 } else
869 break;
870 }
871 p--; /* Point to first character beyond number */
872
873 if (has_e && !is_hex) {
874 /* 1e13 is floating-point, but 1e13h is not */
875 is_float = true;
876 }
877
878 type = is_float ? TOK_FLOAT : TOK_NUMBER;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000879 } else if (isspace(*p)) {
880 type = TOK_WHITESPACE;
881 p++;
882 while (*p && isspace(*p))
883 p++;
884 /*
885 * Whitespace just before end-of-line is discarded by
886 * pretending it's a comment; whitespace just before a
887 * comment gets lumped into the comment.
888 */
889 if (!*p || *p == ';') {
890 type = TOK_COMMENT;
891 while (*p)
892 p++;
893 }
894 } else if (*p == ';') {
895 type = TOK_COMMENT;
896 while (*p)
897 p++;
898 } else {
899 /*
900 * Anything else is an operator of some kind. We check
901 * for all the double-character operators (>>, <<, //,
902 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000903 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000904 */
905 type = TOK_OTHER;
906 if ((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[0] == '^' && p[1] == '^')) {
917 p++;
918 }
919 p++;
920 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000921
H. Peter Anvine2c80182005-01-15 22:15:51 +0000922 /* Handling unterminated string by UNV */
923 /*if (type == -1)
924 {
925 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
926 t->text[p-line] = *line;
927 tail = &t->next;
928 }
929 else */
930 if (type != TOK_COMMENT) {
931 *tail = t = new_Token(NULL, type, line, p - line);
932 tail = &t->next;
933 }
934 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000935 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000936 return list;
937}
938
H. Peter Anvince616072002-04-30 21:02:23 +0000939/*
940 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -0700941 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +0000942 * deleted only all at once by the delete_Blocks function.
943 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000944static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +0000945{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000946 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000947
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000948 /* first, get to the end of the linked list */
949 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000950 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000951 /* now allocate the requested chunk */
952 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000954 /* now allocate a new block for the next request */
955 b->next = nasm_malloc(sizeof(Blocks));
956 /* and initialize the contents of the new block */
957 b->next->next = NULL;
958 b->next->chunk = NULL;
959 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000960}
961
962/*
963 * this function deletes all managed blocks of memory
964 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000965static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +0000966{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +0000968
H. Peter Anvin70653092007-10-19 14:42:29 -0700969 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000970 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -0700971 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000972 * free it.
973 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 while (b) {
975 if (b->chunk)
976 nasm_free(b->chunk);
977 a = b;
978 b = b->next;
979 if (a != &blocks)
980 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000981 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000982}
H. Peter Anvin734b1882002-04-30 21:01:08 +0000983
984/*
H. Peter Anvin70653092007-10-19 14:42:29 -0700985 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +0000986 * back to the caller. It sets the type and text elements, and
987 * also the mac and next elements to NULL.
988 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -0700989static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvinc751e862008-06-09 10:18:45 -0700990 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +0000991{
992 Token *t;
993 int i;
994
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995 if (freeTokens == NULL) {
996 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
997 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
998 freeTokens[i].next = &freeTokens[i + 1];
999 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001000 }
1001 t = freeTokens;
1002 freeTokens = t->next;
1003 t->next = next;
1004 t->mac = NULL;
1005 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 if (type == TOK_WHITESPACE || text == NULL) {
1007 t->text = NULL;
1008 } else {
1009 if (txtlen == 0)
1010 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001011 t->text = nasm_malloc(txtlen+1);
1012 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001014 }
1015 return t;
1016}
1017
H. Peter Anvine2c80182005-01-15 22:15:51 +00001018static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001019{
1020 Token *next = t->next;
1021 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001022 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001023 freeTokens = t;
1024 return next;
1025}
1026
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001027/*
1028 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001029 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1030 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001031 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001032static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001033{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001034 Token *t;
1035 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001036 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001037 const char *q;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001038
1039 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001040 for (t = tlist; t; t = t->next) {
1041 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001042 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001043 nasm_free(t->text);
1044 if (p)
1045 t->text = nasm_strdup(p);
1046 else
1047 t->text = NULL;
1048 }
1049 /* Expand local macros here and not during preprocessing */
1050 if (expand_locals &&
1051 t->type == TOK_PREPROC_ID && t->text &&
1052 t->text[0] == '%' && t->text[1] == '$') {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001053 Context *ctx = get_ctx(t->text, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001054 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001055 char buffer[40];
1056 char *p, *q = t->text + 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001057
H. Peter Anvine2c80182005-01-15 22:15:51 +00001058 q += strspn(q, "$");
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001059 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001060 p = nasm_strcat(buffer, q);
1061 nasm_free(t->text);
1062 t->text = p;
1063 }
1064 }
1065 if (t->type == TOK_WHITESPACE) {
1066 len++;
1067 } else if (t->text) {
1068 len += strlen(t->text);
1069 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001070 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001071 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 for (t = tlist; t; t = t->next) {
1073 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001074 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001075 } else if (t->text) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001076 q = t->text;
1077 while (*q)
1078 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001079 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001080 }
1081 *p = '\0';
1082 return line;
1083}
1084
1085/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001086 * A scanner, suitable for use by the expression evaluator, which
1087 * operates on a line of Tokens. Expects a pointer to a pointer to
1088 * the first token in the line to be passed in as its private_data
1089 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001090 *
1091 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001092 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001094{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001095 Token **tlineptr = private_data;
1096 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001097 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001098
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 do {
1100 tline = *tlineptr;
1101 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001102 }
1103 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001104 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001105
1106 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001107 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001108
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001109 tokval->t_charptr = tline->text;
1110
H. Peter Anvin76690a12002-04-30 20:52:49 +00001111 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001112 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001113 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001114 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001115
H. Peter Anvine2c80182005-01-15 22:15:51 +00001116 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001117 p = tokval->t_charptr = tline->text;
1118 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001119 tokval->t_charptr++;
1120 return tokval->t_type = TOKEN_ID;
1121 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001122
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001123 for (r = p, s = ourcopy; *r; r++) {
Philipp Thomas76ec8e72008-05-21 08:53:21 -07001124 if (r >= p+MAX_KEYWORD)
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001125 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1126 *s++ = tolower(*r);
1127 }
1128 *s = '\0';
1129 /* right, so we have an identifier sitting in temp storage. now,
1130 * is it actually a register or instruction name, or what? */
1131 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001132 }
1133
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 if (tline->type == TOK_NUMBER) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001135 bool rn_error;
1136 tokval->t_integer = readnum(tline->text, &rn_error);
1137 if (rn_error)
1138 return tokval->t_type = TOKEN_ERRNUM; /* some malformation occurred */
1139 tokval->t_charptr = tline->text;
1140 return tokval->t_type = TOKEN_NUM;
1141 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001142
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001143 if (tline->type == TOK_FLOAT) {
1144 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001145 }
1146
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 if (tline->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001148 char bq, *ep;
1149 bool errquote;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001150 bool rn_warn;
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001151 size_t l;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001152
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001153 bq = tline->text[0];
1154 l = nasm_unquote(tline->text, &ep);
1155 if (ep[0] != bq || ep[1] != '\0')
1156 errquote = true;
1157
1158 if (errquote)
1159 return tokval->t_type = TOKEN_ERRNUM;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001160
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001161 tokval->t_integer = readstrnum(tline->text, l, &rn_warn);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162 if (rn_warn)
1163 error(ERR_WARNING | ERR_PASS1, "character constant too long");
1164 tokval->t_charptr = NULL;
1165 return tokval->t_type = TOKEN_NUM;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001166 }
1167
H. Peter Anvine2c80182005-01-15 22:15:51 +00001168 if (tline->type == TOK_OTHER) {
1169 if (!strcmp(tline->text, "<<"))
1170 return tokval->t_type = TOKEN_SHL;
1171 if (!strcmp(tline->text, ">>"))
1172 return tokval->t_type = TOKEN_SHR;
1173 if (!strcmp(tline->text, "//"))
1174 return tokval->t_type = TOKEN_SDIV;
1175 if (!strcmp(tline->text, "%%"))
1176 return tokval->t_type = TOKEN_SMOD;
1177 if (!strcmp(tline->text, "=="))
1178 return tokval->t_type = TOKEN_EQ;
1179 if (!strcmp(tline->text, "<>"))
1180 return tokval->t_type = TOKEN_NE;
1181 if (!strcmp(tline->text, "!="))
1182 return tokval->t_type = TOKEN_NE;
1183 if (!strcmp(tline->text, "<="))
1184 return tokval->t_type = TOKEN_LE;
1185 if (!strcmp(tline->text, ">="))
1186 return tokval->t_type = TOKEN_GE;
1187 if (!strcmp(tline->text, "&&"))
1188 return tokval->t_type = TOKEN_DBL_AND;
1189 if (!strcmp(tline->text, "^^"))
1190 return tokval->t_type = TOKEN_DBL_XOR;
1191 if (!strcmp(tline->text, "||"))
1192 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001193 }
1194
1195 /*
1196 * We have no other options: just return the first character of
1197 * the token text.
1198 */
1199 return tokval->t_type = tline->text[0];
1200}
1201
1202/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001203 * Compare a string to the name of an existing macro; this is a
1204 * simple wrapper which calls either strcmp or nasm_stricmp
1205 * depending on the value of the `casesense' parameter.
1206 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001207static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001208{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001209 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001210}
1211
1212/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001213 * Compare a string to the name of an existing macro; this is a
1214 * simple wrapper which calls either strcmp or nasm_stricmp
1215 * depending on the value of the `casesense' parameter.
1216 */
1217static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1218{
1219 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1220}
1221
1222/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001223 * Return the Context structure associated with a %$ token. Return
1224 * NULL, having _already_ reported an error condition, if the
1225 * context stack isn't deep enough for the supplied number of $
1226 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001227 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001228 * also scanned for such smacro, until it is found; if not -
1229 * only the context that directly results from the number of $'s
1230 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001231 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001232static Context *get_ctx(char *name, bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001233{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001234 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001235 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001236 int i;
1237
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001238 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001239 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001240
H. Peter Anvine2c80182005-01-15 22:15:51 +00001241 if (!cstk) {
1242 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1243 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001244 }
1245
H. Peter Anvine2c80182005-01-15 22:15:51 +00001246 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1247 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001248/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001249 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001250 if (!ctx) {
1251 error(ERR_NONFATAL, "`%s': context stack is only"
1252 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1253 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001254 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001255 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001256 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001257
H. Peter Anvine2c80182005-01-15 22:15:51 +00001258 do {
1259 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001260 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001261 while (m) {
1262 if (!mstrcmp(m->name, name, m->casesense))
1263 return ctx;
1264 m = m->next;
1265 }
1266 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001267 }
1268 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001269 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001270}
1271
1272/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001273 * Check to see if a file is already in a string list
1274 */
1275static bool in_list(const StrList *list, const char *str)
1276{
1277 while (list) {
1278 if (!strcmp(list->str, str))
1279 return true;
1280 list = list->next;
1281 }
1282 return false;
1283}
1284
1285/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001286 * Open an include file. This routine must always return a valid
1287 * file pointer if it returns - it's responsible for throwing an
1288 * ERR_FATAL and bombing out completely if not. It should also try
1289 * the include path one by one until it finds the file or reaches
1290 * the end of the path.
1291 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001292static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
H. Peter Anvin418ca702008-05-30 10:42:30 -07001293 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001294{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001295 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001296 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001297 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001298 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001299 size_t prefix_len = 0;
1300 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001301
H. Peter Anvine2c80182005-01-15 22:15:51 +00001302 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001303 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1304 memcpy(sl->str, prefix, prefix_len);
1305 memcpy(sl->str+prefix_len, file, len+1);
1306 fp = fopen(sl->str, "r");
H. Peter Anvin418ca702008-05-30 10:42:30 -07001307 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001308 sl->next = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001309 **dtail = sl;
1310 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001311 } else {
1312 nasm_free(sl);
1313 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001314 if (fp)
1315 return fp;
H. Peter Anvin418ca702008-05-30 10:42:30 -07001316 if (!ip) {
1317 if (!missing_ok)
1318 break;
1319 prefix = NULL;
1320 } else {
1321 prefix = ip->path;
1322 ip = ip->next;
1323 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001324 if (prefix) {
1325 prefix_len = strlen(prefix);
1326 } else {
1327 /* -MG given and file not found */
H. Peter Anvin418ca702008-05-30 10:42:30 -07001328 if (dhead && !in_list(*dhead, file)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001329 sl = nasm_malloc(len+1+sizeof sl->next);
1330 sl->next = NULL;
1331 strcpy(sl->str, file);
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001332 **dtail = sl;
1333 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001334 }
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001335 return NULL;
1336 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001337 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001338
H. Peter Anvin734b1882002-04-30 21:01:08 +00001339 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001341}
1342
1343/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001344 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001345 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001346 * return true if _any_ single-line macro of that name is defined.
1347 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001348 * `nparam' or no parameters is defined.
1349 *
1350 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001351 * defined, or nparam is -1, the address of the definition structure
1352 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001353 * is NULL, no action will be taken regarding its contents, and no
1354 * error will occur.
1355 *
1356 * Note that this is also called with nparam zero to resolve
1357 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001358 *
1359 * If you already know which context macro belongs to, you can pass
1360 * the context pointer as first parameter; if you won't but name begins
1361 * with %$ the context will be automatically computed. If all_contexts
1362 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001363 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001364static bool
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001365smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001366 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001367{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001368 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001369 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001370
H. Peter Anvin97a23472007-09-16 17:57:25 -07001371 if (ctx) {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001372 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001373 } else if (name[0] == '%' && name[1] == '$') {
1374 if (cstk)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001375 ctx = get_ctx(name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001376 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001377 return false; /* got to return _something_ */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001378 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001379 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001380 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001381 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001382 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001383
H. Peter Anvine2c80182005-01-15 22:15:51 +00001384 while (m) {
1385 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001386 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001387 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001388 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001389 *defn = m;
1390 else
1391 *defn = NULL;
1392 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001393 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001394 }
1395 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001396 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001397
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001398 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001399}
1400
1401/*
1402 * Count and mark off the parameters in a multi-line macro call.
1403 * This is called both from within the multi-line macro expansion
1404 * code, and also to mark off the default parameters when provided
1405 * in a %macro definition line.
1406 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001407static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001408{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001409 int paramsize, brace;
1410
1411 *nparam = paramsize = 0;
1412 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001413 while (t) {
1414 if (*nparam >= paramsize) {
1415 paramsize += PARAM_DELTA;
1416 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1417 }
1418 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001419 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001420 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001421 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001422 (*params)[(*nparam)++] = t;
1423 while (tok_isnt_(t, brace ? "}" : ","))
1424 t = t->next;
1425 if (t) { /* got a comma/brace */
1426 t = t->next;
1427 if (brace) {
1428 /*
1429 * Now we've found the closing brace, look further
1430 * for the comma.
1431 */
1432 skip_white_(t);
1433 if (tok_isnt_(t, ",")) {
1434 error(ERR_NONFATAL,
1435 "braces do not enclose all of macro parameter");
1436 while (tok_isnt_(t, ","))
1437 t = t->next;
1438 }
1439 if (t)
1440 t = t->next; /* eat the comma */
1441 }
1442 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001443 }
1444}
1445
1446/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001447 * Determine whether one of the various `if' conditions is true or
1448 * not.
1449 *
1450 * We must free the tline we get passed.
1451 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001452static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001453{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001454 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001455 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001456 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001457 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001458 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001459 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001460
1461 origline = tline;
1462
H. Peter Anvine2c80182005-01-15 22:15:51 +00001463 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001464 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001465 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001466 while (cstk && tline) {
1467 skip_white_(tline);
1468 if (!tline || tline->type != TOK_ID) {
1469 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001470 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001471 free_tlist(origline);
1472 return -1;
1473 }
1474 if (!nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001475 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001476 tline = tline->next;
1477 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001478 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001479
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001480 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001481 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001482 while (tline) {
1483 skip_white_(tline);
1484 if (!tline || (tline->type != TOK_ID &&
1485 (tline->type != TOK_PREPROC_ID ||
1486 tline->text[1] != '$'))) {
1487 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001488 "`%s' expects macro identifiers", pp_directives[ct]);
1489 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001490 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001491 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001492 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001493 tline = tline->next;
1494 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001495 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001496
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001497 case PPC_IFIDN:
1498 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001499 tline = expand_smacro(tline);
1500 t = tt = tline;
1501 while (tok_isnt_(tt, ","))
1502 tt = tt->next;
1503 if (!tt) {
1504 error(ERR_NONFATAL,
1505 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001506 pp_directives[ct]);
1507 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001508 }
1509 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001510 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001511 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1512 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1513 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001514 pp_directives[ct]);
1515 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001516 }
1517 if (t->type == TOK_WHITESPACE) {
1518 t = t->next;
1519 continue;
1520 }
1521 if (tt->type == TOK_WHITESPACE) {
1522 tt = tt->next;
1523 continue;
1524 }
1525 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001526 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001527 break;
1528 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001529 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001530 if (t->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001531 size_t l1 = nasm_unquote(t->text, NULL);
1532 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001533
1534 if (l1 != l2) {
1535 j = false;
1536 break;
1537 }
1538 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1539 j = false;
1540 break;
1541 }
1542 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001543 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001544 break;
1545 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001546
H. Peter Anvine2c80182005-01-15 22:15:51 +00001547 t = t->next;
1548 tt = tt->next;
1549 }
1550 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001551 j = false; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001552 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001553
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001554 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001555 {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001556 bool found = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001557 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001558
H. Peter Anvine2c80182005-01-15 22:15:51 +00001559 tline = tline->next;
1560 skip_white_(tline);
1561 tline = expand_id(tline);
1562 if (!tok_type_(tline, TOK_ID)) {
1563 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001564 "`%s' expects a macro name", pp_directives[ct]);
1565 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001566 }
1567 searching.name = nasm_strdup(tline->text);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001568 searching.casesense = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001569 searching.plus = false;
1570 searching.nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001571 searching.in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001572 searching.rep_nest = NULL;
1573 searching.nparam_min = 0;
1574 searching.nparam_max = INT_MAX;
1575 tline = expand_smacro(tline->next);
1576 skip_white_(tline);
1577 if (!tline) {
1578 } else if (!tok_type_(tline, TOK_NUMBER)) {
1579 error(ERR_NONFATAL,
1580 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001581 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001582 } else {
1583 searching.nparam_min = searching.nparam_max =
1584 readnum(tline->text, &j);
1585 if (j)
1586 error(ERR_NONFATAL,
1587 "unable to parse parameter count `%s'",
1588 tline->text);
1589 }
1590 if (tline && tok_is_(tline->next, "-")) {
1591 tline = tline->next->next;
1592 if (tok_is_(tline, "*"))
1593 searching.nparam_max = INT_MAX;
1594 else if (!tok_type_(tline, TOK_NUMBER))
1595 error(ERR_NONFATAL,
1596 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001597 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001598 else {
1599 searching.nparam_max = readnum(tline->text, &j);
1600 if (j)
1601 error(ERR_NONFATAL,
1602 "unable to parse parameter count `%s'",
1603 tline->text);
1604 if (searching.nparam_min > searching.nparam_max)
1605 error(ERR_NONFATAL,
1606 "minimum parameter count exceeds maximum");
1607 }
1608 }
1609 if (tline && tok_is_(tline->next, "+")) {
1610 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001611 searching.plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001612 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001613 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001614 while (mmac) {
1615 if (!strcmp(mmac->name, searching.name) &&
1616 (mmac->nparam_min <= searching.nparam_max
1617 || searching.plus)
1618 && (searching.nparam_min <= mmac->nparam_max
1619 || mmac->plus)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001620 found = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001621 break;
1622 }
1623 mmac = mmac->next;
1624 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001625 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001626 j = found;
1627 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001629
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001630 case PPC_IFID:
1631 needtype = TOK_ID;
1632 goto iftype;
1633 case PPC_IFNUM:
1634 needtype = TOK_NUMBER;
1635 goto iftype;
1636 case PPC_IFSTR:
1637 needtype = TOK_STRING;
1638 goto iftype;
1639
1640 iftype:
H. Peter Anvin134b9462008-02-16 17:01:40 -08001641 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001642
H. Peter Anvin927c92b2008-02-16 13:44:52 -08001643 while (tok_type_(t, TOK_WHITESPACE) ||
1644 (needtype == TOK_NUMBER &&
1645 tok_type_(t, TOK_OTHER) &&
1646 (t->text[0] == '-' || t->text[0] == '+') &&
1647 !t->text[1]))
1648 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001649
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001650 j = tok_type_(t, needtype);
1651 break;
1652
1653 case PPC_IFTOKEN:
1654 t = tline = expand_smacro(tline);
1655 while (tok_type_(t, TOK_WHITESPACE))
1656 t = t->next;
1657
1658 j = false;
1659 if (t) {
1660 t = t->next; /* Skip the actual token */
1661 while (tok_type_(t, TOK_WHITESPACE))
1662 t = t->next;
1663 j = !t; /* Should be nothing left */
1664 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001665 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001666
H. Peter Anvin134b9462008-02-16 17:01:40 -08001667 case PPC_IFEMPTY:
1668 t = tline = expand_smacro(tline);
1669 while (tok_type_(t, TOK_WHITESPACE))
1670 t = t->next;
1671
1672 j = !t; /* Should be empty */
1673 break;
1674
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001675 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676 t = tline = expand_smacro(tline);
1677 tptr = &t;
1678 tokval.t_type = TOKEN_INVALID;
1679 evalresult = evaluate(ppscan, tptr, &tokval,
1680 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001681 if (!evalresult)
1682 return -1;
1683 if (tokval.t_type)
1684 error(ERR_WARNING,
1685 "trailing garbage after expression ignored");
1686 if (!is_simple(evalresult)) {
1687 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001688 "non-constant value given to `%s'", pp_directives[ct]);
1689 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001691 j = reloc_value(evalresult) != 0;
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001692 return j;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001693
H. Peter Anvine2c80182005-01-15 22:15:51 +00001694 default:
1695 error(ERR_FATAL,
1696 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001697 pp_directives[ct]);
1698 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001699 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001700
1701 free_tlist(origline);
1702 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001703
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001704fail:
1705 free_tlist(origline);
1706 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001707}
1708
1709/*
H. Peter Anvin418ca702008-05-30 10:42:30 -07001710 * Expand macros in a string. Used in %error directives (and it should
1711 * almost certainly be removed from there, too.)
1712 *
Keith Kaniosb7a89542007-04-12 02:40:54 +00001713 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001714 * The returned variable should ALWAYS be freed after usage.
1715 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001716void expand_macros_in_string(char **p)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001717{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001718 Token *line = tokenize(*p);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001719 line = expand_smacro(line);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001720 *p = detoken(line, false);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001721}
1722
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001723/*
1724 * Common code for defining an smacro
1725 */
1726static bool define_smacro(Context *ctx, char *mname, bool casesense,
1727 int nparam, Token *expansion)
1728{
1729 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001730 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001731
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001732 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1733 if (!smac) {
1734 error(ERR_WARNING,
1735 "single-line macro `%s' defined both with and"
1736 " without parameters", mname);
1737
1738 /* Some instances of the old code considered this a failure,
1739 some others didn't. What is the right thing to do here? */
1740 free_tlist(expansion);
1741 return false; /* Failure */
1742 } else {
1743 /*
1744 * We're redefining, so we have to take over an
1745 * existing SMacro structure. This means freeing
1746 * what was already in it.
1747 */
1748 nasm_free(smac->name);
1749 free_tlist(smac->expansion);
1750 }
1751 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001752 smtbl = ctx ? &ctx->localmac : &smacros;
1753 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001754 smac = nasm_malloc(sizeof(SMacro));
1755 smac->next = *smhead;
1756 *smhead = smac;
1757 }
1758 smac->name = nasm_strdup(mname);
1759 smac->casesense = casesense;
1760 smac->nparam = nparam;
1761 smac->expansion = expansion;
1762 smac->in_progress = false;
1763 return true; /* Success */
1764}
1765
1766/*
1767 * Undefine an smacro
1768 */
1769static void undef_smacro(Context *ctx, const char *mname)
1770{
1771 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001772 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001773
H. Peter Anvin166c2472008-05-28 12:28:58 -07001774 smtbl = ctx ? &ctx->localmac : &smacros;
1775 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001776
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001777 if (smhead) {
1778 /*
1779 * We now have a macro name... go hunt for it.
1780 */
1781 sp = smhead;
1782 while ((s = *sp) != NULL) {
1783 if (!mstrcmp(s->name, mname, s->casesense)) {
1784 *sp = s->next;
1785 nasm_free(s->name);
1786 free_tlist(s->expansion);
1787 nasm_free(s);
1788 } else {
1789 sp = &s->next;
1790 }
1791 }
1792 }
1793}
1794
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001795/*
1796 * Decode a size directive
1797 */
1798static int parse_size(const char *str) {
1799 static const char *size_names[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001800 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001801 static const int sizes[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001802 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001803
1804 return sizes[bsii(str, size_names, elements(size_names))+1];
1805}
1806
Ed Beroset3ab3f412002-06-11 03:31:49 +00001807/**
1808 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001809 * Find out if a line contains a preprocessor directive, and deal
1810 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07001811 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001812 * If a directive _is_ found, it is the responsibility of this routine
1813 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001814 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001815 * @param tline a pointer to the current tokeninzed line linked list
1816 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07001817 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001818 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001819static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001820{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001821 enum preproc_token i;
1822 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07001823 bool err;
1824 int nparam;
1825 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001826 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001827 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001828 int offset;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001829 char *p, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001830 Include *inc;
1831 Context *ctx;
1832 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001833 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001834 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1835 Line *l;
1836 struct tokenval tokval;
1837 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001838 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001839 int64_t count;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001840
1841 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001842
H. Peter Anvineba20a72002-04-30 20:53:55 +00001843 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07001844 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001845 (tline->text[1] == '%' || tline->text[1] == '$'
1846 || tline->text[1] == '!'))
1847 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001848
H. Peter Anvin4169a472007-09-12 01:29:43 +00001849 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001850
1851 /*
1852 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001853 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001854 * we should ignore all directives except for condition
1855 * directives.
1856 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001857 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001858 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1859 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001860 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001861
1862 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001863 * If we're defining a macro or reading a %rep block, we should
1864 * ignore all directives except for %macro/%imacro (which
1865 * generate an error), %endm/%endmacro, and (only if we're in a
H. Peter Anvineba20a72002-04-30 20:53:55 +00001866 * %rep block) %endrep. If we're in a %rep block, another %rep
1867 * causes an error, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001868 */
1869 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001870 i != PP_ENDMACRO && i != PP_ENDM &&
1871 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1872 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001873 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001874
H. Peter Anvin4169a472007-09-12 01:29:43 +00001875 switch (i) {
1876 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001877 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1878 tline->text);
1879 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001880
H. Peter Anvine2c80182005-01-15 22:15:51 +00001881 case PP_STACKSIZE:
1882 /* Directive to tell NASM what the default stack size is. The
1883 * default is for a 16-bit stack, and this can be overriden with
1884 * %stacksize large.
1885 * the following form:
1886 *
1887 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1888 */
1889 tline = tline->next;
1890 if (tline && tline->type == TOK_WHITESPACE)
1891 tline = tline->next;
1892 if (!tline || tline->type != TOK_ID) {
1893 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1894 free_tlist(origline);
1895 return DIRECTIVE_FOUND;
1896 }
1897 if (nasm_stricmp(tline->text, "flat") == 0) {
1898 /* All subsequent ARG directives are for a 32-bit stack */
1899 StackSize = 4;
1900 StackPointer = "ebp";
1901 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001902 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08001903 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
1904 /* All subsequent ARG directives are for a 64-bit stack */
1905 StackSize = 8;
1906 StackPointer = "rbp";
1907 ArgOffset = 8;
1908 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001909 } else if (nasm_stricmp(tline->text, "large") == 0) {
1910 /* All subsequent ARG directives are for a 16-bit stack,
1911 * far function call.
1912 */
1913 StackSize = 2;
1914 StackPointer = "bp";
1915 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001916 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001917 } else if (nasm_stricmp(tline->text, "small") == 0) {
1918 /* All subsequent ARG directives are for a 16-bit stack,
1919 * far function call. We don't support near functions.
1920 */
1921 StackSize = 2;
1922 StackPointer = "bp";
1923 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001924 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001925 } else {
1926 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1927 free_tlist(origline);
1928 return DIRECTIVE_FOUND;
1929 }
1930 free_tlist(origline);
1931 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001932
H. Peter Anvine2c80182005-01-15 22:15:51 +00001933 case PP_ARG:
1934 /* TASM like ARG directive to define arguments to functions, in
1935 * the following form:
1936 *
1937 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1938 */
1939 offset = ArgOffset;
1940 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001941 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001942 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001943
H. Peter Anvine2c80182005-01-15 22:15:51 +00001944 /* Find the argument name */
1945 tline = tline->next;
1946 if (tline && tline->type == TOK_WHITESPACE)
1947 tline = tline->next;
1948 if (!tline || tline->type != TOK_ID) {
1949 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1950 free_tlist(origline);
1951 return DIRECTIVE_FOUND;
1952 }
1953 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001954
H. Peter Anvine2c80182005-01-15 22:15:51 +00001955 /* Find the argument size type */
1956 tline = tline->next;
1957 if (!tline || tline->type != TOK_OTHER
1958 || tline->text[0] != ':') {
1959 error(ERR_NONFATAL,
1960 "Syntax error processing `%%arg' directive");
1961 free_tlist(origline);
1962 return DIRECTIVE_FOUND;
1963 }
1964 tline = tline->next;
1965 if (!tline || tline->type != TOK_ID) {
1966 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1967 free_tlist(origline);
1968 return DIRECTIVE_FOUND;
1969 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001970
H. Peter Anvine2c80182005-01-15 22:15:51 +00001971 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001972 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001973 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001974 size = parse_size(tt->text);
1975 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001976 error(ERR_NONFATAL,
1977 "Invalid size type for `%%arg' missing directive");
1978 free_tlist(tt);
1979 free_tlist(origline);
1980 return DIRECTIVE_FOUND;
1981 }
1982 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001983
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001984 /* Round up to even stack slots */
1985 size = (size+StackSize-1) & ~(StackSize-1);
1986
H. Peter Anvine2c80182005-01-15 22:15:51 +00001987 /* Now define the macro for the argument */
1988 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1989 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001990 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001991 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001992
H. Peter Anvine2c80182005-01-15 22:15:51 +00001993 /* Move to the next argument in the list */
1994 tline = tline->next;
1995 if (tline && tline->type == TOK_WHITESPACE)
1996 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001997 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1998 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001999 free_tlist(origline);
2000 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002001
H. Peter Anvine2c80182005-01-15 22:15:51 +00002002 case PP_LOCAL:
2003 /* TASM like LOCAL directive to define local variables for a
2004 * function, in the following form:
2005 *
2006 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2007 *
2008 * The '= LocalSize' at the end is ignored by NASM, but is
2009 * required by TASM to define the local parameter size (and used
2010 * by the TASM macro package).
2011 */
2012 offset = LocalOffset;
2013 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002014 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002015 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002016
H. Peter Anvine2c80182005-01-15 22:15:51 +00002017 /* Find the argument name */
2018 tline = tline->next;
2019 if (tline && tline->type == TOK_WHITESPACE)
2020 tline = tline->next;
2021 if (!tline || tline->type != TOK_ID) {
2022 error(ERR_NONFATAL,
2023 "`%%local' missing argument parameter");
2024 free_tlist(origline);
2025 return DIRECTIVE_FOUND;
2026 }
2027 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002028
H. Peter Anvine2c80182005-01-15 22:15:51 +00002029 /* Find the argument size type */
2030 tline = tline->next;
2031 if (!tline || tline->type != TOK_OTHER
2032 || tline->text[0] != ':') {
2033 error(ERR_NONFATAL,
2034 "Syntax error processing `%%local' directive");
2035 free_tlist(origline);
2036 return DIRECTIVE_FOUND;
2037 }
2038 tline = tline->next;
2039 if (!tline || tline->type != TOK_ID) {
2040 error(ERR_NONFATAL,
2041 "`%%local' missing size type parameter");
2042 free_tlist(origline);
2043 return DIRECTIVE_FOUND;
2044 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002045
H. Peter Anvine2c80182005-01-15 22:15:51 +00002046 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002047 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002048 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002049 size = parse_size(tt->text);
2050 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002051 error(ERR_NONFATAL,
2052 "Invalid size type for `%%local' missing directive");
2053 free_tlist(tt);
2054 free_tlist(origline);
2055 return DIRECTIVE_FOUND;
2056 }
2057 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002058
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002059 /* Round up to even stack slots */
2060 size = (size+StackSize-1) & ~(StackSize-1);
2061
2062 offset += size; /* Negative offset, increment before */
2063
2064 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002065 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2066 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002067 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002068
H. Peter Anvine2c80182005-01-15 22:15:51 +00002069 /* Now define the assign to setup the enter_c macro correctly */
2070 snprintf(directive, sizeof(directive),
2071 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002072 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002073
H. Peter Anvine2c80182005-01-15 22:15:51 +00002074 /* Move to the next argument in the list */
2075 tline = tline->next;
2076 if (tline && tline->type == TOK_WHITESPACE)
2077 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002078 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2079 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002080 free_tlist(origline);
2081 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002082
H. Peter Anvine2c80182005-01-15 22:15:51 +00002083 case PP_CLEAR:
2084 if (tline->next)
2085 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002086 free_macros();
2087 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002088 free_tlist(origline);
2089 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002090
H. Peter Anvin418ca702008-05-30 10:42:30 -07002091 case PP_DEPEND:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002092 t = tline->next = expand_smacro(tline->next);
2093 skip_white_(t);
2094 if (!t || (t->type != TOK_STRING &&
2095 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002096 error(ERR_NONFATAL, "`%%depend' expects a file name");
2097 free_tlist(origline);
2098 return DIRECTIVE_FOUND; /* but we did _something_ */
2099 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002100 if (t->next)
H. Peter Anvin418ca702008-05-30 10:42:30 -07002101 error(ERR_WARNING,
2102 "trailing garbage after `%%depend' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002103 p = t->text;
2104 if (t->type != TOK_INTERNAL_STRING)
2105 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002106 if (dephead && !in_list(*dephead, p)) {
2107 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2108 sl->next = NULL;
2109 strcpy(sl->str, p);
2110 *deptail = sl;
2111 deptail = &sl->next;
2112 }
2113 free_tlist(origline);
2114 return DIRECTIVE_FOUND;
2115
2116 case PP_INCLUDE:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002117 t = tline->next = expand_smacro(tline->next);
2118 skip_white_(t);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002119
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002120 if (!t || (t->type != TOK_STRING &&
2121 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002122 error(ERR_NONFATAL, "`%%include' expects a file name");
2123 free_tlist(origline);
2124 return DIRECTIVE_FOUND; /* but we did _something_ */
2125 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002126 if (t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002127 error(ERR_WARNING,
2128 "trailing garbage after `%%include' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002129 p = t->text;
2130 if (t->type != TOK_INTERNAL_STRING)
2131 nasm_unquote(p, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002132 inc = nasm_malloc(sizeof(Include));
2133 inc->next = istk;
2134 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002135 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002136 if (!inc->fp) {
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002137 /* -MG given but file not found */
2138 nasm_free(inc);
2139 } else {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002140 inc->fname = src_set_fname(nasm_strdup(p));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002141 inc->lineno = src_set_linnum(0);
2142 inc->lineinc = 1;
2143 inc->expansion = NULL;
2144 inc->mstk = NULL;
2145 istk = inc;
2146 list->uplevel(LIST_INCLUDE);
2147 }
2148 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002149 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002150
H. Peter Anvine2c80182005-01-15 22:15:51 +00002151 case PP_PUSH:
2152 tline = tline->next;
2153 skip_white_(tline);
2154 tline = expand_id(tline);
2155 if (!tok_type_(tline, TOK_ID)) {
2156 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2157 free_tlist(origline);
2158 return DIRECTIVE_FOUND; /* but we did _something_ */
2159 }
2160 if (tline->next)
2161 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2162 ctx = nasm_malloc(sizeof(Context));
2163 ctx->next = cstk;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002164 hash_init(&ctx->localmac, HASH_SMALL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002165 ctx->name = nasm_strdup(tline->text);
2166 ctx->number = unique++;
2167 cstk = ctx;
2168 free_tlist(origline);
2169 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002170
H. Peter Anvine2c80182005-01-15 22:15:51 +00002171 case PP_REPL:
2172 tline = tline->next;
2173 skip_white_(tline);
2174 tline = expand_id(tline);
2175 if (!tok_type_(tline, TOK_ID)) {
2176 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2177 free_tlist(origline);
2178 return DIRECTIVE_FOUND; /* but we did _something_ */
2179 }
2180 if (tline->next)
2181 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2182 if (!cstk)
2183 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2184 else {
2185 nasm_free(cstk->name);
2186 cstk->name = nasm_strdup(tline->text);
2187 }
2188 free_tlist(origline);
2189 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002190
H. Peter Anvine2c80182005-01-15 22:15:51 +00002191 case PP_POP:
2192 if (tline->next)
2193 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2194 if (!cstk)
2195 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2196 else
2197 ctx_pop();
2198 free_tlist(origline);
2199 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002200
H. Peter Anvine2c80182005-01-15 22:15:51 +00002201 case PP_ERROR:
2202 tline->next = expand_smacro(tline->next);
2203 tline = tline->next;
2204 skip_white_(tline);
2205 if (tok_type_(tline, TOK_STRING)) {
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002206 p = tline->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002207 nasm_unquote(p, NULL);
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002208 expand_macros_in_string(&p); /* WHY? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002209 error(ERR_NONFATAL, "%s", p);
2210 nasm_free(p);
2211 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002212 p = detoken(tline, false);
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002213 error(ERR_WARNING, "%s", p); /* WARNING!??!! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002214 nasm_free(p);
2215 }
2216 free_tlist(origline);
2217 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002218
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002219 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002220 if (istk->conds && !emitting(istk->conds->state))
2221 j = COND_NEVER;
2222 else {
2223 j = if_condition(tline->next, i);
2224 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002225 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2226 }
2227 cond = nasm_malloc(sizeof(Cond));
2228 cond->next = istk->conds;
2229 cond->state = j;
2230 istk->conds = cond;
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002231 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002232 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002233
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002234 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002235 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002236 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002237 if (emitting(istk->conds->state)
2238 || istk->conds->state == COND_NEVER)
2239 istk->conds->state = COND_NEVER;
2240 else {
2241 /*
2242 * IMPORTANT: In the case of %if, we will already have
2243 * called expand_mmac_params(); however, if we're
2244 * processing an %elif we must have been in a
2245 * non-emitting mode, which would have inhibited
2246 * the normal invocation of expand_mmac_params(). Therefore,
2247 * we have to do it explicitly here.
2248 */
2249 j = if_condition(expand_mmac_params(tline->next), i);
2250 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002251 istk->conds->state =
2252 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2253 }
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002254 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002255 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002256
H. Peter Anvine2c80182005-01-15 22:15:51 +00002257 case PP_ELSE:
2258 if (tline->next)
2259 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2260 if (!istk->conds)
2261 error(ERR_FATAL, "`%%else': no matching `%%if'");
2262 if (emitting(istk->conds->state)
2263 || istk->conds->state == COND_NEVER)
2264 istk->conds->state = COND_ELSE_FALSE;
2265 else
2266 istk->conds->state = COND_ELSE_TRUE;
2267 free_tlist(origline);
2268 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002269
H. Peter Anvine2c80182005-01-15 22:15:51 +00002270 case PP_ENDIF:
2271 if (tline->next)
2272 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2273 if (!istk->conds)
2274 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2275 cond = istk->conds;
2276 istk->conds = cond->next;
2277 nasm_free(cond);
2278 free_tlist(origline);
2279 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002280
H. Peter Anvine2c80182005-01-15 22:15:51 +00002281 case PP_MACRO:
2282 case PP_IMACRO:
2283 if (defining)
2284 error(ERR_FATAL,
2285 "`%%%smacro': already defining a macro",
2286 (i == PP_IMACRO ? "i" : ""));
2287 tline = tline->next;
2288 skip_white_(tline);
2289 tline = expand_id(tline);
2290 if (!tok_type_(tline, TOK_ID)) {
2291 error(ERR_NONFATAL,
2292 "`%%%smacro' expects a macro name",
2293 (i == PP_IMACRO ? "i" : ""));
2294 return DIRECTIVE_FOUND;
2295 }
2296 defining = nasm_malloc(sizeof(MMacro));
2297 defining->name = nasm_strdup(tline->text);
2298 defining->casesense = (i == PP_MACRO);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002299 defining->plus = false;
2300 defining->nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002301 defining->in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002302 defining->rep_nest = NULL;
2303 tline = expand_smacro(tline->next);
2304 skip_white_(tline);
2305 if (!tok_type_(tline, TOK_NUMBER)) {
2306 error(ERR_NONFATAL,
2307 "`%%%smacro' expects a parameter count",
2308 (i == PP_IMACRO ? "i" : ""));
2309 defining->nparam_min = defining->nparam_max = 0;
2310 } else {
2311 defining->nparam_min = defining->nparam_max =
H. Peter Anvin70055962007-10-11 00:05:31 -07002312 readnum(tline->text, &err);
2313 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002314 error(ERR_NONFATAL,
2315 "unable to parse parameter count `%s'", tline->text);
2316 }
2317 if (tline && tok_is_(tline->next, "-")) {
2318 tline = tline->next->next;
2319 if (tok_is_(tline, "*"))
2320 defining->nparam_max = INT_MAX;
2321 else if (!tok_type_(tline, TOK_NUMBER))
2322 error(ERR_NONFATAL,
2323 "`%%%smacro' expects a parameter count after `-'",
2324 (i == PP_IMACRO ? "i" : ""));
2325 else {
H. Peter Anvin70055962007-10-11 00:05:31 -07002326 defining->nparam_max = readnum(tline->text, &err);
2327 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002328 error(ERR_NONFATAL,
2329 "unable to parse parameter count `%s'",
2330 tline->text);
2331 if (defining->nparam_min > defining->nparam_max)
2332 error(ERR_NONFATAL,
2333 "minimum parameter count exceeds maximum");
2334 }
2335 }
2336 if (tline && tok_is_(tline->next, "+")) {
2337 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002338 defining->plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002339 }
2340 if (tline && tok_type_(tline->next, TOK_ID) &&
2341 !nasm_stricmp(tline->next->text, ".nolist")) {
2342 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002343 defining->nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002344 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002345 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002346 while (mmac) {
2347 if (!strcmp(mmac->name, defining->name) &&
2348 (mmac->nparam_min <= defining->nparam_max
2349 || defining->plus)
2350 && (defining->nparam_min <= mmac->nparam_max
2351 || mmac->plus)) {
2352 error(ERR_WARNING,
2353 "redefining multi-line macro `%s'", defining->name);
2354 break;
2355 }
2356 mmac = mmac->next;
2357 }
2358 /*
2359 * Handle default parameters.
2360 */
2361 if (tline && tline->next) {
2362 defining->dlist = tline->next;
2363 tline->next = NULL;
2364 count_mmac_params(defining->dlist, &defining->ndefs,
2365 &defining->defaults);
2366 } else {
2367 defining->dlist = NULL;
2368 defining->defaults = NULL;
2369 }
2370 defining->expansion = NULL;
2371 free_tlist(origline);
2372 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002373
H. Peter Anvine2c80182005-01-15 22:15:51 +00002374 case PP_ENDM:
2375 case PP_ENDMACRO:
2376 if (!defining) {
2377 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2378 return DIRECTIVE_FOUND;
2379 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002380 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002381 defining->next = *mmhead;
2382 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002383 defining = NULL;
2384 free_tlist(origline);
2385 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002386
H. Peter Anvine2c80182005-01-15 22:15:51 +00002387 case PP_ROTATE:
2388 if (tline->next && tline->next->type == TOK_WHITESPACE)
2389 tline = tline->next;
2390 if (tline->next == NULL) {
2391 free_tlist(origline);
2392 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2393 return DIRECTIVE_FOUND;
2394 }
2395 t = expand_smacro(tline->next);
2396 tline->next = NULL;
2397 free_tlist(origline);
2398 tline = t;
2399 tptr = &t;
2400 tokval.t_type = TOKEN_INVALID;
2401 evalresult =
2402 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2403 free_tlist(tline);
2404 if (!evalresult)
2405 return DIRECTIVE_FOUND;
2406 if (tokval.t_type)
2407 error(ERR_WARNING,
2408 "trailing garbage after expression ignored");
2409 if (!is_simple(evalresult)) {
2410 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2411 return DIRECTIVE_FOUND;
2412 }
2413 mmac = istk->mstk;
2414 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2415 mmac = mmac->next_active;
2416 if (!mmac) {
2417 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2418 } else if (mmac->nparam == 0) {
2419 error(ERR_NONFATAL,
2420 "`%%rotate' invoked within macro without parameters");
2421 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002422 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002423
H. Peter Anvin25a99342007-09-22 17:45:45 -07002424 rotate %= (int)mmac->nparam;
2425 if (rotate < 0)
2426 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002427
H. Peter Anvin25a99342007-09-22 17:45:45 -07002428 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002429 }
2430 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002431
H. Peter Anvine2c80182005-01-15 22:15:51 +00002432 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002433 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002434 do {
2435 tline = tline->next;
2436 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002437
H. Peter Anvine2c80182005-01-15 22:15:51 +00002438 if (tok_type_(tline, TOK_ID) &&
2439 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002440 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002441 do {
2442 tline = tline->next;
2443 } while (tok_type_(tline, TOK_WHITESPACE));
2444 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002445
H. Peter Anvine2c80182005-01-15 22:15:51 +00002446 if (tline) {
2447 t = expand_smacro(tline);
2448 tptr = &t;
2449 tokval.t_type = TOKEN_INVALID;
2450 evalresult =
2451 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2452 if (!evalresult) {
2453 free_tlist(origline);
2454 return DIRECTIVE_FOUND;
2455 }
2456 if (tokval.t_type)
2457 error(ERR_WARNING,
2458 "trailing garbage after expression ignored");
2459 if (!is_simple(evalresult)) {
2460 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2461 return DIRECTIVE_FOUND;
2462 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002463 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002464 } else {
2465 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002466 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002467 }
2468 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002469
H. Peter Anvine2c80182005-01-15 22:15:51 +00002470 tmp_defining = defining;
2471 defining = nasm_malloc(sizeof(MMacro));
2472 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002473 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002474 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002475 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002476 defining->in_progress = count;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002477 defining->nparam_min = defining->nparam_max = 0;
2478 defining->defaults = NULL;
2479 defining->dlist = NULL;
2480 defining->expansion = NULL;
2481 defining->next_active = istk->mstk;
2482 defining->rep_nest = tmp_defining;
2483 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002484
H. Peter Anvine2c80182005-01-15 22:15:51 +00002485 case PP_ENDREP:
2486 if (!defining || defining->name) {
2487 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2488 return DIRECTIVE_FOUND;
2489 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002490
H. Peter Anvine2c80182005-01-15 22:15:51 +00002491 /*
2492 * Now we have a "macro" defined - although it has no name
2493 * and we won't be entering it in the hash tables - we must
2494 * push a macro-end marker for it on to istk->expansion.
2495 * After that, it will take care of propagating itself (a
2496 * macro-end marker line for a macro which is really a %rep
2497 * block will cause the macro to be re-expanded, complete
2498 * with another macro-end marker to ensure the process
2499 * continues) until the whole expansion is forcibly removed
2500 * from istk->expansion by a %exitrep.
2501 */
2502 l = nasm_malloc(sizeof(Line));
2503 l->next = istk->expansion;
2504 l->finishes = defining;
2505 l->first = NULL;
2506 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002507
H. Peter Anvine2c80182005-01-15 22:15:51 +00002508 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002509
H. Peter Anvine2c80182005-01-15 22:15:51 +00002510 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2511 tmp_defining = defining;
2512 defining = defining->rep_nest;
2513 free_tlist(origline);
2514 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002515
H. Peter Anvine2c80182005-01-15 22:15:51 +00002516 case PP_EXITREP:
2517 /*
2518 * We must search along istk->expansion until we hit a
2519 * macro-end marker for a macro with no name. Then we set
2520 * its `in_progress' flag to 0.
2521 */
2522 for (l = istk->expansion; l; l = l->next)
2523 if (l->finishes && !l->finishes->name)
2524 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002525
H. Peter Anvine2c80182005-01-15 22:15:51 +00002526 if (l)
2527 l->finishes->in_progress = 0;
2528 else
2529 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2530 free_tlist(origline);
2531 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002532
H. Peter Anvine2c80182005-01-15 22:15:51 +00002533 case PP_XDEFINE:
2534 case PP_IXDEFINE:
2535 case PP_DEFINE:
2536 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002537 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002538
H. Peter Anvine2c80182005-01-15 22:15:51 +00002539 tline = tline->next;
2540 skip_white_(tline);
2541 tline = expand_id(tline);
2542 if (!tline || (tline->type != TOK_ID &&
2543 (tline->type != TOK_PREPROC_ID ||
2544 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002545 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2546 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002547 free_tlist(origline);
2548 return DIRECTIVE_FOUND;
2549 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002550
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002551 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002552
H. Peter Anvine2c80182005-01-15 22:15:51 +00002553 mname = tline->text;
2554 last = tline;
2555 param_start = tline = tline->next;
2556 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002557
H. Peter Anvine2c80182005-01-15 22:15:51 +00002558 /* Expand the macro definition now for %xdefine and %ixdefine */
2559 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2560 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002561
H. Peter Anvine2c80182005-01-15 22:15:51 +00002562 if (tok_is_(tline, "(")) {
2563 /*
2564 * This macro has parameters.
2565 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002566
H. Peter Anvine2c80182005-01-15 22:15:51 +00002567 tline = tline->next;
2568 while (1) {
2569 skip_white_(tline);
2570 if (!tline) {
2571 error(ERR_NONFATAL, "parameter identifier expected");
2572 free_tlist(origline);
2573 return DIRECTIVE_FOUND;
2574 }
2575 if (tline->type != TOK_ID) {
2576 error(ERR_NONFATAL,
2577 "`%s': parameter identifier expected",
2578 tline->text);
2579 free_tlist(origline);
2580 return DIRECTIVE_FOUND;
2581 }
2582 tline->type = TOK_SMAC_PARAM + nparam++;
2583 tline = tline->next;
2584 skip_white_(tline);
2585 if (tok_is_(tline, ",")) {
2586 tline = tline->next;
2587 continue;
2588 }
2589 if (!tok_is_(tline, ")")) {
2590 error(ERR_NONFATAL,
2591 "`)' expected to terminate macro template");
2592 free_tlist(origline);
2593 return DIRECTIVE_FOUND;
2594 }
2595 break;
2596 }
2597 last = tline;
2598 tline = tline->next;
2599 }
2600 if (tok_type_(tline, TOK_WHITESPACE))
2601 last = tline, tline = tline->next;
2602 macro_start = NULL;
2603 last->next = NULL;
2604 t = tline;
2605 while (t) {
2606 if (t->type == TOK_ID) {
2607 for (tt = param_start; tt; tt = tt->next)
2608 if (tt->type >= TOK_SMAC_PARAM &&
2609 !strcmp(tt->text, t->text))
2610 t->type = tt->type;
2611 }
2612 tt = t->next;
2613 t->next = macro_start;
2614 macro_start = t;
2615 t = tt;
2616 }
2617 /*
2618 * Good. We now have a macro name, a parameter count, and a
2619 * token list (in reverse order) for an expansion. We ought
2620 * to be OK just to create an SMacro, store it, and let
2621 * free_tlist have the rest of the line (which we have
2622 * carefully re-terminated after chopping off the expansion
2623 * from the end).
2624 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002625 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002626 free_tlist(origline);
2627 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002628
H. Peter Anvine2c80182005-01-15 22:15:51 +00002629 case PP_UNDEF:
2630 tline = tline->next;
2631 skip_white_(tline);
2632 tline = expand_id(tline);
2633 if (!tline || (tline->type != TOK_ID &&
2634 (tline->type != TOK_PREPROC_ID ||
2635 tline->text[1] != '$'))) {
2636 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2637 free_tlist(origline);
2638 return DIRECTIVE_FOUND;
2639 }
2640 if (tline->next) {
2641 error(ERR_WARNING,
2642 "trailing garbage after macro name ignored");
2643 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002644
H. Peter Anvine2c80182005-01-15 22:15:51 +00002645 /* Find the context that symbol belongs to */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002646 ctx = get_ctx(tline->text, false);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002647 undef_smacro(ctx, tline->text);
2648 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002649 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002650
H. Peter Anvin9e200162008-06-04 17:23:14 -07002651 case PP_DEFSTR:
2652 case PP_IDEFSTR:
2653 casesense = (i == PP_DEFSTR);
2654
2655 tline = tline->next;
2656 skip_white_(tline);
2657 tline = expand_id(tline);
2658 if (!tline || (tline->type != TOK_ID &&
2659 (tline->type != TOK_PREPROC_ID ||
2660 tline->text[1] != '$'))) {
2661 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2662 pp_directives[i]);
2663 free_tlist(origline);
2664 return DIRECTIVE_FOUND;
2665 }
2666
2667 ctx = get_ctx(tline->text, false);
2668
2669 mname = tline->text;
2670 last = tline;
2671 tline = expand_smacro(tline->next);
2672 last->next = NULL;
2673
2674 while (tok_type_(tline, TOK_WHITESPACE))
2675 tline = delete_Token(tline);
2676
2677 p = detoken(tline, false);
2678 macro_start = nasm_malloc(sizeof(*macro_start));
2679 macro_start->next = NULL;
2680 macro_start->text = nasm_quote(p, strlen(p));
2681 macro_start->type = TOK_STRING;
2682 macro_start->mac = NULL;
2683 nasm_free(p);
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(origline);
2692 return DIRECTIVE_FOUND;
2693
H. Peter Anvin418ca702008-05-30 10:42:30 -07002694 case PP_PATHSEARCH:
2695 {
2696 FILE *fp;
2697 StrList *xsl = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002698 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002699
2700 casesense = true;
2701
2702 tline = tline->next;
2703 skip_white_(tline);
2704 tline = expand_id(tline);
2705 if (!tline || (tline->type != TOK_ID &&
2706 (tline->type != TOK_PREPROC_ID ||
2707 tline->text[1] != '$'))) {
2708 error(ERR_NONFATAL,
2709 "`%%pathsearch' expects a macro identifier as first parameter");
2710 free_tlist(origline);
2711 return DIRECTIVE_FOUND;
2712 }
2713 ctx = get_ctx(tline->text, false);
2714
2715 mname = tline->text;
2716 last = tline;
2717 tline = expand_smacro(tline->next);
2718 last->next = NULL;
2719
2720 t = tline;
2721 while (tok_type_(t, TOK_WHITESPACE))
2722 t = t->next;
2723
2724 if (!t || (t->type != TOK_STRING &&
2725 t->type != TOK_INTERNAL_STRING)) {
2726 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
2727 free_tlist(tline);
2728 free_tlist(origline);
2729 return DIRECTIVE_FOUND; /* but we did _something_ */
2730 }
2731 if (t->next)
2732 error(ERR_WARNING,
2733 "trailing garbage after `%%pathsearch' ignored");
H. Peter Anvin427cc912008-06-01 21:43:03 -07002734 p = t->text;
2735 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002736 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002737
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002738 fp = inc_fopen(p, &xsl, &xst, true);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002739 if (fp) {
2740 p = xsl->str;
2741 fclose(fp); /* Don't actually care about the file */
2742 }
2743 macro_start = nasm_malloc(sizeof(*macro_start));
2744 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002745 macro_start->text = nasm_quote(p, strlen(p));
H. Peter Anvin418ca702008-05-30 10:42:30 -07002746 macro_start->type = TOK_STRING;
2747 macro_start->mac = NULL;
2748 if (xsl)
2749 nasm_free(xsl);
2750
2751 /*
2752 * We now have a macro name, an implicit parameter count of
2753 * zero, and a string token to use as an expansion. Create
2754 * and store an SMacro.
2755 */
2756 define_smacro(ctx, mname, casesense, 0, macro_start);
2757 free_tlist(tline);
2758 free_tlist(origline);
2759 return DIRECTIVE_FOUND;
2760 }
2761
H. Peter Anvine2c80182005-01-15 22:15:51 +00002762 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002763 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07002764
H. Peter Anvine2c80182005-01-15 22:15:51 +00002765 tline = tline->next;
2766 skip_white_(tline);
2767 tline = expand_id(tline);
2768 if (!tline || (tline->type != TOK_ID &&
2769 (tline->type != TOK_PREPROC_ID ||
2770 tline->text[1] != '$'))) {
2771 error(ERR_NONFATAL,
2772 "`%%strlen' expects a macro identifier as first parameter");
2773 free_tlist(origline);
2774 return DIRECTIVE_FOUND;
2775 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002776 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002777
H. Peter Anvine2c80182005-01-15 22:15:51 +00002778 mname = tline->text;
2779 last = tline;
2780 tline = expand_smacro(tline->next);
2781 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002782
H. Peter Anvine2c80182005-01-15 22:15:51 +00002783 t = tline;
2784 while (tok_type_(t, TOK_WHITESPACE))
2785 t = t->next;
2786 /* t should now point to the string */
2787 if (t->type != TOK_STRING) {
2788 error(ERR_NONFATAL,
2789 "`%%strlen` requires string as second parameter");
2790 free_tlist(tline);
2791 free_tlist(origline);
2792 return DIRECTIVE_FOUND;
2793 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002794
H. Peter Anvine2c80182005-01-15 22:15:51 +00002795 macro_start = nasm_malloc(sizeof(*macro_start));
2796 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002797 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002798 macro_start->mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002799
H. Peter Anvine2c80182005-01-15 22:15:51 +00002800 /*
2801 * We now have a macro name, an implicit parameter count of
2802 * zero, and a numeric token to use as an expansion. Create
2803 * and store an SMacro.
2804 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002805 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002806 free_tlist(tline);
2807 free_tlist(origline);
2808 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002809
H. Peter Anvine2c80182005-01-15 22:15:51 +00002810 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002811 {
2812 int64_t a1, a2;
2813 size_t len;
2814
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002815 casesense = true;
2816
H. Peter Anvine2c80182005-01-15 22:15:51 +00002817 tline = tline->next;
2818 skip_white_(tline);
2819 tline = expand_id(tline);
2820 if (!tline || (tline->type != TOK_ID &&
2821 (tline->type != TOK_PREPROC_ID ||
2822 tline->text[1] != '$'))) {
2823 error(ERR_NONFATAL,
2824 "`%%substr' expects a macro identifier as first parameter");
2825 free_tlist(origline);
2826 return DIRECTIVE_FOUND;
2827 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002828 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002829
H. Peter Anvine2c80182005-01-15 22:15:51 +00002830 mname = tline->text;
2831 last = tline;
2832 tline = expand_smacro(tline->next);
2833 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002834
H. Peter Anvine2c80182005-01-15 22:15:51 +00002835 t = tline->next;
2836 while (tok_type_(t, TOK_WHITESPACE))
2837 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002838
H. Peter Anvine2c80182005-01-15 22:15:51 +00002839 /* t should now point to the string */
2840 if (t->type != TOK_STRING) {
2841 error(ERR_NONFATAL,
2842 "`%%substr` requires string as second parameter");
2843 free_tlist(tline);
2844 free_tlist(origline);
2845 return DIRECTIVE_FOUND;
2846 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002847
H. Peter Anvine2c80182005-01-15 22:15:51 +00002848 tt = t->next;
2849 tptr = &tt;
2850 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002851 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
2852 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002853 if (!evalresult) {
2854 free_tlist(tline);
2855 free_tlist(origline);
2856 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002857 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002858 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2859 free_tlist(tline);
2860 free_tlist(origline);
2861 return DIRECTIVE_FOUND;
2862 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002863 a1 = evalresult->value-1;
2864
2865 while (tok_type_(tt, TOK_WHITESPACE))
2866 tt = tt->next;
2867 if (!tt) {
2868 a2 = 1; /* Backwards compatibility: one character */
2869 } else {
2870 tokval.t_type = TOKEN_INVALID;
2871 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
2872 pass, error, NULL);
2873 if (!evalresult) {
2874 free_tlist(tline);
2875 free_tlist(origline);
2876 return DIRECTIVE_FOUND;
2877 } else if (!is_simple(evalresult)) {
2878 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2879 free_tlist(tline);
2880 free_tlist(origline);
2881 return DIRECTIVE_FOUND;
2882 }
2883 a2 = evalresult->value;
2884 }
2885
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002886 len = nasm_unquote(t->text, NULL);
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002887 if (a2 < 0)
2888 a2 = a2+1+len-a1;
2889 if (a1+a2 > (int64_t)len)
2890 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002891
H. Peter Anvine2c80182005-01-15 22:15:51 +00002892 macro_start = nasm_malloc(sizeof(*macro_start));
2893 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002894 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002895 macro_start->type = TOK_STRING;
2896 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002897
H. Peter Anvine2c80182005-01-15 22:15:51 +00002898 /*
2899 * We now have a macro name, an implicit parameter count of
2900 * zero, and a numeric token to use as an expansion. Create
2901 * and store an SMacro.
2902 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002903 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002904 free_tlist(tline);
2905 free_tlist(origline);
2906 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002907 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002908
H. Peter Anvine2c80182005-01-15 22:15:51 +00002909 case PP_ASSIGN:
2910 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002911 casesense = (i == PP_ASSIGN);
2912
H. Peter Anvine2c80182005-01-15 22:15:51 +00002913 tline = tline->next;
2914 skip_white_(tline);
2915 tline = expand_id(tline);
2916 if (!tline || (tline->type != TOK_ID &&
2917 (tline->type != TOK_PREPROC_ID ||
2918 tline->text[1] != '$'))) {
2919 error(ERR_NONFATAL,
2920 "`%%%sassign' expects a macro identifier",
2921 (i == PP_IASSIGN ? "i" : ""));
2922 free_tlist(origline);
2923 return DIRECTIVE_FOUND;
2924 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002925 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002926
H. Peter Anvine2c80182005-01-15 22:15:51 +00002927 mname = tline->text;
2928 last = tline;
2929 tline = expand_smacro(tline->next);
2930 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002931
H. Peter Anvine2c80182005-01-15 22:15:51 +00002932 t = tline;
2933 tptr = &t;
2934 tokval.t_type = TOKEN_INVALID;
2935 evalresult =
2936 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2937 free_tlist(tline);
2938 if (!evalresult) {
2939 free_tlist(origline);
2940 return DIRECTIVE_FOUND;
2941 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002942
H. Peter Anvine2c80182005-01-15 22:15:51 +00002943 if (tokval.t_type)
2944 error(ERR_WARNING,
2945 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00002946
H. Peter Anvine2c80182005-01-15 22:15:51 +00002947 if (!is_simple(evalresult)) {
2948 error(ERR_NONFATAL,
2949 "non-constant value given to `%%%sassign'",
2950 (i == PP_IASSIGN ? "i" : ""));
2951 free_tlist(origline);
2952 return DIRECTIVE_FOUND;
2953 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002954
H. Peter Anvine2c80182005-01-15 22:15:51 +00002955 macro_start = nasm_malloc(sizeof(*macro_start));
2956 macro_start->next = NULL;
2957 make_tok_num(macro_start, reloc_value(evalresult));
2958 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002959
H. Peter Anvine2c80182005-01-15 22:15:51 +00002960 /*
2961 * We now have a macro name, an implicit parameter count of
2962 * zero, and a numeric token to use as an expansion. Create
2963 * and store an SMacro.
2964 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002965 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002966 free_tlist(origline);
2967 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002968
H. Peter Anvine2c80182005-01-15 22:15:51 +00002969 case PP_LINE:
2970 /*
2971 * Syntax is `%line nnn[+mmm] [filename]'
2972 */
2973 tline = tline->next;
2974 skip_white_(tline);
2975 if (!tok_type_(tline, TOK_NUMBER)) {
2976 error(ERR_NONFATAL, "`%%line' expects line number");
2977 free_tlist(origline);
2978 return DIRECTIVE_FOUND;
2979 }
H. Peter Anvin70055962007-10-11 00:05:31 -07002980 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002981 m = 1;
2982 tline = tline->next;
2983 if (tok_is_(tline, "+")) {
2984 tline = tline->next;
2985 if (!tok_type_(tline, TOK_NUMBER)) {
2986 error(ERR_NONFATAL, "`%%line' expects line increment");
2987 free_tlist(origline);
2988 return DIRECTIVE_FOUND;
2989 }
H. Peter Anvin70055962007-10-11 00:05:31 -07002990 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002991 tline = tline->next;
2992 }
2993 skip_white_(tline);
2994 src_set_linnum(k);
2995 istk->lineinc = m;
2996 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002997 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002998 }
2999 free_tlist(origline);
3000 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003001
H. Peter Anvine2c80182005-01-15 22:15:51 +00003002 default:
3003 error(ERR_FATAL,
3004 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003005 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003006 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003007 }
Ed Beroset3ab3f412002-06-11 03:31:49 +00003008 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003009}
3010
3011/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003012 * Ensure that a macro parameter contains a condition code and
3013 * nothing else. Return the condition code index if so, or -1
3014 * otherwise.
3015 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003016static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003017{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003018 Token *tt;
3019 int i, j, k, m;
3020
H. Peter Anvin25a99342007-09-22 17:45:45 -07003021 if (!t)
3022 return -1; /* Probably a %+ without a space */
3023
H. Peter Anvineba20a72002-04-30 20:53:55 +00003024 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003025 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003026 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003027 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003028 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003029 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003030 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003031
3032 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00003033 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003034 while (j - i > 1) {
3035 k = (j + i) / 2;
3036 m = nasm_stricmp(t->text, conditions[k]);
3037 if (m == 0) {
3038 i = k;
3039 j = -2;
3040 break;
3041 } else if (m < 0) {
3042 j = k;
3043 } else
3044 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003045 }
3046 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003047 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003048 return i;
3049}
3050
3051/*
3052 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3053 * %-n) and MMacro-local identifiers (%%foo).
3054 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003055static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003056{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003057 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003058
3059 tail = &thead;
3060 thead = NULL;
3061
H. Peter Anvine2c80182005-01-15 22:15:51 +00003062 while (tline) {
3063 if (tline->type == TOK_PREPROC_ID &&
3064 (((tline->text[1] == '+' || tline->text[1] == '-')
3065 && tline->text[2]) || tline->text[1] == '%'
3066 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003067 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003068 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003069 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003070 unsigned int n;
3071 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003072 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003073
H. Peter Anvine2c80182005-01-15 22:15:51 +00003074 t = tline;
3075 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003076
H. Peter Anvine2c80182005-01-15 22:15:51 +00003077 mac = istk->mstk;
3078 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3079 mac = mac->next_active;
3080 if (!mac)
3081 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3082 else
3083 switch (t->text[1]) {
3084 /*
3085 * We have to make a substitution of one of the
3086 * forms %1, %-1, %+1, %%foo, %0.
3087 */
3088 case '0':
3089 type = TOK_NUMBER;
3090 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3091 text = nasm_strdup(tmpbuf);
3092 break;
3093 case '%':
3094 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003095 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00003096 mac->unique);
3097 text = nasm_strcat(tmpbuf, t->text + 2);
3098 break;
3099 case '-':
3100 n = atoi(t->text + 2) - 1;
3101 if (n >= mac->nparam)
3102 tt = NULL;
3103 else {
3104 if (mac->nparam > 1)
3105 n = (n + mac->rotate) % mac->nparam;
3106 tt = mac->params[n];
3107 }
3108 cc = find_cc(tt);
3109 if (cc == -1) {
3110 error(ERR_NONFATAL,
3111 "macro parameter %d is not a condition code",
3112 n + 1);
3113 text = NULL;
3114 } else {
3115 type = TOK_ID;
3116 if (inverse_ccs[cc] == -1) {
3117 error(ERR_NONFATAL,
3118 "condition code `%s' is not invertible",
3119 conditions[cc]);
3120 text = NULL;
3121 } else
3122 text =
3123 nasm_strdup(conditions[inverse_ccs[cc]]);
3124 }
3125 break;
3126 case '+':
3127 n = atoi(t->text + 2) - 1;
3128 if (n >= mac->nparam)
3129 tt = NULL;
3130 else {
3131 if (mac->nparam > 1)
3132 n = (n + mac->rotate) % mac->nparam;
3133 tt = mac->params[n];
3134 }
3135 cc = find_cc(tt);
3136 if (cc == -1) {
3137 error(ERR_NONFATAL,
3138 "macro parameter %d is not a condition code",
3139 n + 1);
3140 text = NULL;
3141 } else {
3142 type = TOK_ID;
3143 text = nasm_strdup(conditions[cc]);
3144 }
3145 break;
3146 default:
3147 n = atoi(t->text + 1) - 1;
3148 if (n >= mac->nparam)
3149 tt = NULL;
3150 else {
3151 if (mac->nparam > 1)
3152 n = (n + mac->rotate) % mac->nparam;
3153 tt = mac->params[n];
3154 }
3155 if (tt) {
3156 for (i = 0; i < mac->paramlen[n]; i++) {
3157 *tail = new_Token(NULL, tt->type, tt->text, 0);
3158 tail = &(*tail)->next;
3159 tt = tt->next;
3160 }
3161 }
3162 text = NULL; /* we've done it here */
3163 break;
3164 }
3165 if (!text) {
3166 delete_Token(t);
3167 } else {
3168 *tail = t;
3169 tail = &t->next;
3170 t->type = type;
3171 nasm_free(t->text);
3172 t->text = text;
3173 t->mac = NULL;
3174 }
3175 continue;
3176 } else {
3177 t = *tail = tline;
3178 tline = tline->next;
3179 t->mac = NULL;
3180 tail = &t->next;
3181 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003182 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003183 *tail = NULL;
3184 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003185 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003186 switch (t->type) {
3187 case TOK_WHITESPACE:
3188 if (tt->type == TOK_WHITESPACE) {
3189 t->next = delete_Token(tt);
3190 }
3191 break;
3192 case TOK_ID:
3193 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003194 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003195 nasm_free(t->text);
3196 t->text = tmp;
3197 t->next = delete_Token(tt);
3198 }
3199 break;
3200 case TOK_NUMBER:
3201 if (tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003202 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003203 nasm_free(t->text);
3204 t->text = tmp;
3205 t->next = delete_Token(tt);
3206 }
3207 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003208 default:
3209 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003210 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003211
H. Peter Anvin76690a12002-04-30 20:52:49 +00003212 return thead;
3213}
3214
3215/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003216 * Expand all single-line macro calls made in the given line.
3217 * Return the expanded version of the line. The original is deemed
3218 * to be destroyed in the process. (In reality we'll just move
3219 * Tokens from input to output a lot of the time, rather than
3220 * actually bothering to destroy and replicate.)
3221 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003222#define DEADMAN_LIMIT (1 << 20)
3223
H. Peter Anvine2c80182005-01-15 22:15:51 +00003224static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003225{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003226 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003227 struct hash_table *smtbl;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003228 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003229 Token **params;
3230 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003231 unsigned int nparam, sparam;
3232 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003233 Token *org_tline = tline;
3234 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003235 char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003236 int deadman = DEADMAN_LIMIT;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003237
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003238 /*
3239 * Trick: we should avoid changing the start token pointer since it can
3240 * be contained in "next" field of other token. Because of this
3241 * we allocate a copy of first token and work with it; at the end of
3242 * routine we copy it back
3243 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003244 if (org_tline) {
3245 tline =
3246 new_Token(org_tline->next, org_tline->type, org_tline->text,
3247 0);
3248 tline->mac = org_tline->mac;
3249 nasm_free(org_tline->text);
3250 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003251 }
3252
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003253again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003254 tail = &thead;
3255 thead = NULL;
3256
H. Peter Anvine2c80182005-01-15 22:15:51 +00003257 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003258 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003259 error(ERR_NONFATAL, "interminable macro recursion");
3260 break;
3261 }
3262
H. Peter Anvine2c80182005-01-15 22:15:51 +00003263 if ((mname = tline->text)) {
3264 /* if this token is a local macro, look in local context */
H. Peter Anvinb037a672008-05-29 19:08:08 -07003265 ctx = NULL;
3266 smtbl = &smacros;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003267 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003268 ctx = get_ctx(mname, true);
H. Peter Anvinb037a672008-05-29 19:08:08 -07003269 if (ctx)
3270 smtbl = &ctx->localmac;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003271 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07003272 head = (SMacro *) hash_findix(smtbl, mname);
H. Peter Anvin072771e2008-05-22 13:17:51 -07003273
H. Peter Anvine2c80182005-01-15 22:15:51 +00003274 /*
3275 * We've hit an identifier. As in is_mmacro below, we first
3276 * check whether the identifier is a single-line macro at
3277 * all, then think about checking for parameters if
3278 * necessary.
3279 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003280 for (m = head; m; m = m->next)
3281 if (!mstrcmp(m->name, mname, m->casesense))
3282 break;
3283 if (m) {
3284 mstart = tline;
3285 params = NULL;
3286 paramsize = NULL;
3287 if (m->nparam == 0) {
3288 /*
3289 * Simple case: the macro is parameterless. Discard the
3290 * one token that the macro call took, and push the
3291 * expansion back on the to-do stack.
3292 */
3293 if (!m->expansion) {
3294 if (!strcmp("__FILE__", m->name)) {
3295 int32_t num = 0;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003296 char *file;
3297 src_get(&num, &file);
3298 tline->text = nasm_quote(file, strlen(file));
H. Peter Anvin97a23472007-09-16 17:57:25 -07003299 tline->type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003300 nasm_free(file);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003301 continue;
3302 }
3303 if (!strcmp("__LINE__", m->name)) {
3304 nasm_free(tline->text);
3305 make_tok_num(tline, src_get_linnum());
3306 continue;
3307 }
3308 if (!strcmp("__BITS__", m->name)) {
3309 nasm_free(tline->text);
3310 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003311 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003312 }
3313 tline = delete_Token(tline);
3314 continue;
3315 }
3316 } else {
3317 /*
3318 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003319 * exists and takes parameters. We must find the
3320 * parameters in the call, count them, find the SMacro
3321 * that corresponds to that form of the macro call, and
3322 * substitute for the parameters when we expand. What a
3323 * pain.
3324 */
3325 /*tline = tline->next;
3326 skip_white_(tline); */
3327 do {
3328 t = tline->next;
3329 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003330 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003331 t->text = NULL;
3332 t = tline->next = delete_Token(t);
3333 }
3334 tline = t;
3335 } while (tok_type_(tline, TOK_WHITESPACE));
3336 if (!tok_is_(tline, "(")) {
3337 /*
3338 * This macro wasn't called with parameters: ignore
3339 * the call. (Behaviour borrowed from gnu cpp.)
3340 */
3341 tline = mstart;
3342 m = NULL;
3343 } else {
3344 int paren = 0;
3345 int white = 0;
3346 brackets = 0;
3347 nparam = 0;
3348 sparam = PARAM_DELTA;
3349 params = nasm_malloc(sparam * sizeof(Token *));
3350 params[0] = tline->next;
3351 paramsize = nasm_malloc(sparam * sizeof(int));
3352 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003353 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003354 /*
3355 * For some unusual expansions
3356 * which concatenates function call
3357 */
3358 t = tline->next;
3359 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003360 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003361 t->text = NULL;
3362 t = tline->next = delete_Token(t);
3363 }
3364 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003365
H. Peter Anvine2c80182005-01-15 22:15:51 +00003366 if (!tline) {
3367 error(ERR_NONFATAL,
3368 "macro call expects terminating `)'");
3369 break;
3370 }
3371 if (tline->type == TOK_WHITESPACE
3372 && brackets <= 0) {
3373 if (paramsize[nparam])
3374 white++;
3375 else
3376 params[nparam] = tline->next;
3377 continue; /* parameter loop */
3378 }
3379 if (tline->type == TOK_OTHER
3380 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003381 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003382 if (ch == ',' && !paren && brackets <= 0) {
3383 if (++nparam >= sparam) {
3384 sparam += PARAM_DELTA;
3385 params = nasm_realloc(params,
3386 sparam *
3387 sizeof(Token
3388 *));
3389 paramsize =
3390 nasm_realloc(paramsize,
3391 sparam *
3392 sizeof(int));
3393 }
3394 params[nparam] = tline->next;
3395 paramsize[nparam] = 0;
3396 white = 0;
3397 continue; /* parameter loop */
3398 }
3399 if (ch == '{' &&
3400 (brackets > 0 || (brackets == 0 &&
3401 !paramsize[nparam])))
3402 {
3403 if (!(brackets++)) {
3404 params[nparam] = tline->next;
3405 continue; /* parameter loop */
3406 }
3407 }
3408 if (ch == '}' && brackets > 0)
3409 if (--brackets == 0) {
3410 brackets = -1;
3411 continue; /* parameter loop */
3412 }
3413 if (ch == '(' && !brackets)
3414 paren++;
3415 if (ch == ')' && brackets <= 0)
3416 if (--paren < 0)
3417 break;
3418 }
3419 if (brackets < 0) {
3420 brackets = 0;
3421 error(ERR_NONFATAL, "braces do not "
3422 "enclose all of macro parameter");
3423 }
3424 paramsize[nparam] += white + 1;
3425 white = 0;
3426 } /* parameter loop */
3427 nparam++;
3428 while (m && (m->nparam != nparam ||
3429 mstrcmp(m->name, mname,
3430 m->casesense)))
3431 m = m->next;
3432 if (!m)
3433 error(ERR_WARNING | ERR_WARN_MNP,
3434 "macro `%s' exists, "
3435 "but not taking %d parameters",
3436 mstart->text, nparam);
3437 }
3438 }
3439 if (m && m->in_progress)
3440 m = NULL;
3441 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003442 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003443 * Design question: should we handle !tline, which
3444 * indicates missing ')' here, or expand those
3445 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003446 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003447 */
3448 nasm_free(params);
3449 nasm_free(paramsize);
3450 tline = mstart;
3451 } else {
3452 /*
3453 * Expand the macro: we are placed on the last token of the
3454 * call, so that we can easily split the call from the
3455 * following tokens. We also start by pushing an SMAC_END
3456 * token for the cycle removal.
3457 */
3458 t = tline;
3459 if (t) {
3460 tline = t->next;
3461 t->next = NULL;
3462 }
3463 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3464 tt->mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003465 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003466 tline = tt;
3467 for (t = m->expansion; t; t = t->next) {
3468 if (t->type >= TOK_SMAC_PARAM) {
3469 Token *pcopy = tline, **ptail = &pcopy;
3470 Token *ttt, *pt;
3471 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003472
H. Peter Anvine2c80182005-01-15 22:15:51 +00003473 ttt = params[t->type - TOK_SMAC_PARAM];
3474 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3475 --i >= 0;) {
3476 pt = *ptail =
3477 new_Token(tline, ttt->type, ttt->text,
3478 0);
3479 ptail = &pt->next;
3480 ttt = ttt->next;
3481 }
3482 tline = pcopy;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -07003483 } else if (t->type == TOK_PREPROC_Q) {
3484 tt = new_Token(tline, TOK_ID, mname, 0);
3485 tline = tt;
3486 } else if (t->type == TOK_PREPROC_QQ) {
3487 tt = new_Token(tline, TOK_ID, m->name, 0);
3488 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003489 } else {
3490 tt = new_Token(tline, t->type, t->text, 0);
3491 tline = tt;
3492 }
3493 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003494
H. Peter Anvine2c80182005-01-15 22:15:51 +00003495 /*
3496 * Having done that, get rid of the macro call, and clean
3497 * up the parameters.
3498 */
3499 nasm_free(params);
3500 nasm_free(paramsize);
3501 free_tlist(mstart);
3502 continue; /* main token loop */
3503 }
3504 }
3505 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003506
H. Peter Anvine2c80182005-01-15 22:15:51 +00003507 if (tline->type == TOK_SMAC_END) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003508 tline->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003509 tline = delete_Token(tline);
3510 } else {
3511 t = *tail = tline;
3512 tline = tline->next;
3513 t->mac = NULL;
3514 t->next = NULL;
3515 tail = &t->next;
3516 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003517 }
3518
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003519 /*
3520 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003521 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003522 * TOK_IDs should be concatenated.
3523 * Also we look for %+ tokens and concatenate the tokens before and after
3524 * them (without white spaces in between).
3525 */
3526 t = thead;
3527 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003528 while (t) {
3529 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3530 t = t->next;
3531 if (!t || !t->next)
3532 break;
3533 if (t->next->type == TOK_ID ||
3534 t->next->type == TOK_PREPROC_ID ||
3535 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003536 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003537 nasm_free(t->text);
3538 t->next = delete_Token(t->next);
3539 t->text = p;
3540 rescan = 1;
3541 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3542 t->next->next->type == TOK_PREPROC_ID &&
3543 strcmp(t->next->next->text, "%+") == 0) {
3544 /* free the next whitespace, the %+ token and next whitespace */
3545 int i;
3546 for (i = 1; i <= 3; i++) {
3547 if (!t->next
3548 || (i != 2 && t->next->type != TOK_WHITESPACE))
3549 break;
3550 t->next = delete_Token(t->next);
3551 } /* endfor */
3552 } else
3553 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003554 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003555 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003556 if (rescan) {
3557 tline = thead;
3558 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003559 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003560
H. Peter Anvine2c80182005-01-15 22:15:51 +00003561 if (org_tline) {
3562 if (thead) {
3563 *org_tline = *thead;
3564 /* since we just gave text to org_line, don't free it */
3565 thead->text = NULL;
3566 delete_Token(thead);
3567 } else {
3568 /* the expression expanded to empty line;
3569 we can't return NULL for some reasons
3570 we just set the line to a single WHITESPACE token. */
3571 memset(org_tline, 0, sizeof(*org_tline));
3572 org_tline->text = NULL;
3573 org_tline->type = TOK_WHITESPACE;
3574 }
3575 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003576 }
3577
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003578 return thead;
3579}
3580
3581/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003582 * Similar to expand_smacro but used exclusively with macro identifiers
3583 * right before they are fetched in. The reason is that there can be
3584 * identifiers consisting of several subparts. We consider that if there
3585 * are more than one element forming the name, user wants a expansion,
3586 * otherwise it will be left as-is. Example:
3587 *
3588 * %define %$abc cde
3589 *
3590 * the identifier %$abc will be left as-is so that the handler for %define
3591 * will suck it and define the corresponding value. Other case:
3592 *
3593 * %define _%$abc cde
3594 *
3595 * In this case user wants name to be expanded *before* %define starts
3596 * working, so we'll expand %$abc into something (if it has a value;
3597 * otherwise it will be left as-is) then concatenate all successive
3598 * PP_IDs into one.
3599 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003600static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003601{
3602 Token *cur, *oldnext = NULL;
3603
H. Peter Anvin734b1882002-04-30 21:01:08 +00003604 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003605 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003606
3607 cur = tline;
3608 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003609 (cur->next->type == TOK_ID ||
3610 cur->next->type == TOK_PREPROC_ID
3611 || cur->next->type == TOK_NUMBER))
3612 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003613
3614 /* If identifier consists of just one token, don't expand */
3615 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003616 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003617
H. Peter Anvine2c80182005-01-15 22:15:51 +00003618 if (cur) {
3619 oldnext = cur->next; /* Detach the tail past identifier */
3620 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003621 }
3622
H. Peter Anvin734b1882002-04-30 21:01:08 +00003623 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003624
H. Peter Anvine2c80182005-01-15 22:15:51 +00003625 if (cur) {
3626 /* expand_smacro possibly changhed tline; re-scan for EOL */
3627 cur = tline;
3628 while (cur && cur->next)
3629 cur = cur->next;
3630 if (cur)
3631 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003632 }
3633
3634 return tline;
3635}
3636
3637/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003638 * Determine whether the given line constitutes a multi-line macro
3639 * call, and return the MMacro structure called if so. Doesn't have
3640 * to check for an initial label - that's taken care of in
3641 * expand_mmacro - but must check numbers of parameters. Guaranteed
3642 * to be called with tline->type == TOK_ID, so the putative macro
3643 * name is easy to find.
3644 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003645static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003646{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003647 MMacro *head, *m;
3648 Token **params;
3649 int nparam;
3650
H. Peter Anvin166c2472008-05-28 12:28:58 -07003651 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003652
3653 /*
3654 * Efficiency: first we see if any macro exists with the given
3655 * name. If not, we can return NULL immediately. _Then_ we
3656 * count the parameters, and then we look further along the
3657 * list if necessary to find the proper MMacro.
3658 */
3659 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003660 if (!mstrcmp(m->name, tline->text, m->casesense))
3661 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003662 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003663 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003664
3665 /*
3666 * OK, we have a potential macro. Count and demarcate the
3667 * parameters.
3668 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003669 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003670
3671 /*
3672 * So we know how many parameters we've got. Find the MMacro
3673 * structure that handles this number.
3674 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003675 while (m) {
3676 if (m->nparam_min <= nparam
3677 && (m->plus || nparam <= m->nparam_max)) {
3678 /*
3679 * This one is right. Just check if cycle removal
3680 * prohibits us using it before we actually celebrate...
3681 */
3682 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003683#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003684 error(ERR_NONFATAL,
3685 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003686#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003687 nasm_free(params);
3688 return NULL;
3689 }
3690 /*
3691 * It's right, and we can use it. Add its default
3692 * parameters to the end of our list if necessary.
3693 */
3694 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3695 params =
3696 nasm_realloc(params,
3697 ((m->nparam_min + m->ndefs +
3698 1) * sizeof(*params)));
3699 while (nparam < m->nparam_min + m->ndefs) {
3700 params[nparam] = m->defaults[nparam - m->nparam_min];
3701 nparam++;
3702 }
3703 }
3704 /*
3705 * If we've gone over the maximum parameter count (and
3706 * we're in Plus mode), ignore parameters beyond
3707 * nparam_max.
3708 */
3709 if (m->plus && nparam > m->nparam_max)
3710 nparam = m->nparam_max;
3711 /*
3712 * Then terminate the parameter list, and leave.
3713 */
3714 if (!params) { /* need this special case */
3715 params = nasm_malloc(sizeof(*params));
3716 nparam = 0;
3717 }
3718 params[nparam] = NULL;
3719 *params_array = params;
3720 return m;
3721 }
3722 /*
3723 * This one wasn't right: look for the next one with the
3724 * same name.
3725 */
3726 for (m = m->next; m; m = m->next)
3727 if (!mstrcmp(m->name, tline->text, m->casesense))
3728 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003729 }
3730
3731 /*
3732 * After all that, we didn't find one with the right number of
3733 * parameters. Issue a warning, and fail to expand the macro.
3734 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003735 error(ERR_WARNING | ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003736 "macro `%s' exists, but not taking %d parameters",
3737 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003738 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003739 return NULL;
3740}
3741
3742/*
3743 * Expand the multi-line macro call made by the given line, if
3744 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003745 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003746 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003747static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003748{
3749 Token *startline = tline;
3750 Token *label = NULL;
3751 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003752 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003753 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003754 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003755 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07003756 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003757
3758 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003759 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07003760 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003761 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003762 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003763 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003764 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07003765 if (m) {
3766 mname = t->text;
3767 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003768 Token *last;
3769 /*
3770 * We have an id which isn't a macro call. We'll assume
3771 * it might be a label; we'll also check to see if a
3772 * colon follows it. Then, if there's another id after
3773 * that lot, we'll check it again for macro-hood.
3774 */
3775 label = last = t;
3776 t = t->next;
3777 if (tok_type_(t, TOK_WHITESPACE))
3778 last = t, t = t->next;
3779 if (tok_is_(t, ":")) {
3780 dont_prepend = 1;
3781 last = t, t = t->next;
3782 if (tok_type_(t, TOK_WHITESPACE))
3783 last = t, t = t->next;
3784 }
3785 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3786 return 0;
3787 last->next = NULL;
H. Peter Anvinc751e862008-06-09 10:18:45 -07003788 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003789 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003790 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003791
3792 /*
3793 * Fix up the parameters: this involves stripping leading and
3794 * trailing whitespace, then stripping braces if they are
3795 * present.
3796 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003797 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003798 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003799
H. Peter Anvine2c80182005-01-15 22:15:51 +00003800 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003801 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003802 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003803
H. Peter Anvine2c80182005-01-15 22:15:51 +00003804 t = params[i];
3805 skip_white_(t);
3806 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003807 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003808 params[i] = t;
3809 paramlen[i] = 0;
3810 while (t) {
3811 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3812 break; /* ... because we have hit a comma */
3813 if (comma && t->type == TOK_WHITESPACE
3814 && tok_is_(t->next, ","))
3815 break; /* ... or a space then a comma */
3816 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3817 break; /* ... or a brace */
3818 t = t->next;
3819 paramlen[i]++;
3820 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003821 }
3822
3823 /*
3824 * OK, we have a MMacro structure together with a set of
3825 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003826 * copies of each Line on to istk->expansion. Substitution of
3827 * parameter tokens and macro-local tokens doesn't get done
3828 * until the single-line macro substitution process; this is
3829 * because delaying them allows us to change the semantics
3830 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003831 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003832 * First, push an end marker on to istk->expansion, mark this
3833 * macro as in progress, and set up its invocation-specific
3834 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003835 */
3836 ll = nasm_malloc(sizeof(Line));
3837 ll->next = istk->expansion;
3838 ll->finishes = m;
3839 ll->first = NULL;
3840 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003841
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003842 m->in_progress = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003843 m->params = params;
3844 m->iline = tline;
3845 m->nparam = nparam;
3846 m->rotate = 0;
3847 m->paramlen = paramlen;
3848 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003849 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003850
3851 m->next_active = istk->mstk;
3852 istk->mstk = m;
3853
H. Peter Anvine2c80182005-01-15 22:15:51 +00003854 for (l = m->expansion; l; l = l->next) {
3855 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003856
H. Peter Anvine2c80182005-01-15 22:15:51 +00003857 ll = nasm_malloc(sizeof(Line));
3858 ll->finishes = NULL;
3859 ll->next = istk->expansion;
3860 istk->expansion = ll;
3861 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003862
H. Peter Anvine2c80182005-01-15 22:15:51 +00003863 for (t = l->first; t; t = t->next) {
3864 Token *x = t;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003865 switch (t->type) {
3866 case TOK_PREPROC_Q:
H. Peter Anvinc751e862008-06-09 10:18:45 -07003867 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
H. Peter Anvince2233b2008-05-25 21:57:00 -07003868 break;
3869 case TOK_PREPROC_QQ:
3870 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
3871 break;
3872 case TOK_PREPROC_ID:
3873 if (t->text[1] == '0' && t->text[2] == '0') {
3874 dont_prepend = -1;
3875 x = label;
3876 if (!x)
3877 continue;
3878 }
3879 /* fall through */
3880 default:
3881 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3882 break;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003883 }
H. Peter Anvince2233b2008-05-25 21:57:00 -07003884 tail = &tt->next;
3885 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003886 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003887 }
3888
3889 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00003890 * If we had a label, push it on as the first line of
3891 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003892 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003893 if (label) {
3894 if (dont_prepend < 0)
3895 free_tlist(startline);
3896 else {
3897 ll = nasm_malloc(sizeof(Line));
3898 ll->finishes = NULL;
3899 ll->next = istk->expansion;
3900 istk->expansion = ll;
3901 ll->first = startline;
3902 if (!dont_prepend) {
3903 while (label->next)
3904 label = label->next;
3905 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3906 }
3907 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003908 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003909
H. Peter Anvin734b1882002-04-30 21:01:08 +00003910 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003911
H. Peter Anvineba20a72002-04-30 20:53:55 +00003912 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003913}
3914
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003915/*
3916 * Since preprocessor always operate only on the line that didn't
3917 * arrived yet, we should always use ERR_OFFBY1. Also since user
3918 * won't want to see same error twice (preprocessing is done once
3919 * per pass) we will want to show errors only during pass one.
3920 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003921static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003922{
3923 va_list arg;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003924 char buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003925
3926 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003927 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003928 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003929
H. Peter Anvin734b1882002-04-30 21:01:08 +00003930 va_start(arg, fmt);
Ed Beroset19f927a2004-12-15 17:07:03 +00003931 vsnprintf(buff, sizeof(buff), fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003932 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003933
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003934 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003935 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3936 istk->mstk->lineno, buff);
3937 else
3938 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003939}
3940
H. Peter Anvin734b1882002-04-30 21:01:08 +00003941static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003942pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07003943 ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003944{
H. Peter Anvin99941bf2002-05-14 17:44:03 +00003945 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003946 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003947 istk = nasm_malloc(sizeof(Include));
3948 istk->next = NULL;
3949 istk->conds = NULL;
3950 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003951 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003952 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00003953 istk->fname = NULL;
3954 src_set_fname(nasm_strdup(file));
3955 src_set_linnum(0);
3956 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003957 if (!istk->fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003958 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3959 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003960 defining = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003961 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003962 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003963 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07003964 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003965 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07003966 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003967 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003968 any_extrastdmac = (extrastdmac != NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003969 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003970 evaluate = eval;
3971 pass = apass;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07003972 dephead = deptail = deplist;
3973 if (deplist) {
3974 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
3975 sl->next = NULL;
3976 strcpy(sl->str, file);
3977 *deptail = sl;
3978 deptail = &sl->next;
3979 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003980}
3981
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003982static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003983{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003984 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003985 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003986
H. Peter Anvine2c80182005-01-15 22:15:51 +00003987 while (1) {
3988 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003989 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00003990 * buffer or from the input file.
3991 */
3992 tline = NULL;
3993 while (istk->expansion && istk->expansion->finishes) {
3994 Line *l = istk->expansion;
3995 if (!l->finishes->name && l->finishes->in_progress > 1) {
3996 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003997
H. Peter Anvine2c80182005-01-15 22:15:51 +00003998 /*
3999 * This is a macro-end marker for a macro with no
4000 * name, which means it's not really a macro at all
4001 * but a %rep block, and the `in_progress' field is
4002 * more than 1, meaning that we still need to
4003 * repeat. (1 means the natural last repetition; 0
4004 * means termination by %exitrep.) We have
4005 * therefore expanded up to the %endrep, and must
4006 * push the whole block on to the expansion buffer
4007 * again. We don't bother to remove the macro-end
4008 * marker: we'd only have to generate another one
4009 * if we did.
4010 */
4011 l->finishes->in_progress--;
4012 for (l = l->finishes->expansion; l; l = l->next) {
4013 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004014
H. Peter Anvine2c80182005-01-15 22:15:51 +00004015 ll = nasm_malloc(sizeof(Line));
4016 ll->next = istk->expansion;
4017 ll->finishes = NULL;
4018 ll->first = NULL;
4019 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004020
H. Peter Anvine2c80182005-01-15 22:15:51 +00004021 for (t = l->first; t; t = t->next) {
4022 if (t->text || t->type == TOK_WHITESPACE) {
4023 tt = *tail =
4024 new_Token(NULL, t->type, t->text, 0);
4025 tail = &tt->next;
4026 }
4027 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004028
H. Peter Anvine2c80182005-01-15 22:15:51 +00004029 istk->expansion = ll;
4030 }
4031 } else {
4032 /*
4033 * Check whether a `%rep' was started and not ended
4034 * within this macro expansion. This can happen and
4035 * should be detected. It's a fatal error because
4036 * I'm too confused to work out how to recover
4037 * sensibly from it.
4038 */
4039 if (defining) {
4040 if (defining->name)
4041 error(ERR_PANIC,
4042 "defining with name in expansion");
4043 else if (istk->mstk->name)
4044 error(ERR_FATAL,
4045 "`%%rep' without `%%endrep' within"
4046 " expansion of macro `%s'",
4047 istk->mstk->name);
4048 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004049
H. Peter Anvine2c80182005-01-15 22:15:51 +00004050 /*
4051 * FIXME: investigate the relationship at this point between
4052 * istk->mstk and l->finishes
4053 */
4054 {
4055 MMacro *m = istk->mstk;
4056 istk->mstk = m->next_active;
4057 if (m->name) {
4058 /*
4059 * This was a real macro call, not a %rep, and
4060 * therefore the parameter information needs to
4061 * be freed.
4062 */
4063 nasm_free(m->params);
4064 free_tlist(m->iline);
4065 nasm_free(m->paramlen);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004066 l->finishes->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004067 } else
4068 free_mmacro(m);
4069 }
4070 istk->expansion = l->next;
4071 nasm_free(l);
4072 list->downlevel(LIST_MACRO);
4073 }
4074 }
4075 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004076
H. Peter Anvine2c80182005-01-15 22:15:51 +00004077 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004078 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004079 Line *l = istk->expansion;
4080 if (istk->mstk)
4081 istk->mstk->lineno++;
4082 tline = l->first;
4083 istk->expansion = l->next;
4084 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004085 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004086 list->line(LIST_MACRO, p);
4087 nasm_free(p);
4088 break;
4089 }
4090 line = read_line();
4091 if (line) { /* from the current input file */
4092 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004093 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004094 nasm_free(line);
4095 break;
4096 }
4097 /*
4098 * The current file has ended; work down the istk
4099 */
4100 {
4101 Include *i = istk;
4102 fclose(i->fp);
4103 if (i->conds)
4104 error(ERR_FATAL,
4105 "expected `%%endif' before end of file");
4106 /* only set line and file name if there's a next node */
4107 if (i->next) {
4108 src_set_linnum(i->lineno);
4109 nasm_free(src_set_fname(i->fname));
4110 }
4111 istk = i->next;
4112 list->downlevel(LIST_INCLUDE);
4113 nasm_free(i);
4114 if (!istk)
4115 return NULL;
4116 }
4117 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004118
H. Peter Anvine2c80182005-01-15 22:15:51 +00004119 /*
4120 * We must expand MMacro parameters and MMacro-local labels
4121 * _before_ we plunge into directive processing, to cope
4122 * with things like `%define something %1' such as STRUC
4123 * uses. Unless we're _defining_ a MMacro, in which case
4124 * those tokens should be left alone to go into the
4125 * definition; and unless we're in a non-emitting
4126 * condition, in which case we don't want to meddle with
4127 * anything.
4128 */
4129 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
4130 tline = expand_mmac_params(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004131
H. Peter Anvine2c80182005-01-15 22:15:51 +00004132 /*
4133 * Check the line to see if it's a preprocessor directive.
4134 */
4135 if (do_directive(tline) == DIRECTIVE_FOUND) {
4136 continue;
4137 } else if (defining) {
4138 /*
4139 * We're defining a multi-line macro. We emit nothing
4140 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004141 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004142 */
4143 Line *l = nasm_malloc(sizeof(Line));
4144 l->next = defining->expansion;
4145 l->first = tline;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004146 l->finishes = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004147 defining->expansion = l;
4148 continue;
4149 } else if (istk->conds && !emitting(istk->conds->state)) {
4150 /*
4151 * We're in a non-emitting branch of a condition block.
4152 * Emit nothing at all, not even a blank line: when we
4153 * emerge from the condition we'll give a line-number
4154 * directive so we keep our place correctly.
4155 */
4156 free_tlist(tline);
4157 continue;
4158 } else if (istk->mstk && !istk->mstk->in_progress) {
4159 /*
4160 * We're in a %rep block which has been terminated, so
4161 * we're walking through to the %endrep without
4162 * emitting anything. Emit nothing at all, not even a
4163 * blank line: when we emerge from the %rep block we'll
4164 * give a line-number directive so we keep our place
4165 * correctly.
4166 */
4167 free_tlist(tline);
4168 continue;
4169 } else {
4170 tline = expand_smacro(tline);
4171 if (!expand_mmacro(tline)) {
4172 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004173 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004174 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004175 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004176 free_tlist(tline);
4177 break;
4178 } else {
4179 continue; /* expand_mmacro calls free_tlist */
4180 }
4181 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004182 }
4183
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004184 return line;
4185}
4186
H. Peter Anvine2c80182005-01-15 22:15:51 +00004187static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004188{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004189 if (defining) {
4190 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
4191 defining->name);
4192 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004193 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004194 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004195 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004196 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004197 while (istk) {
4198 Include *i = istk;
4199 istk = istk->next;
4200 fclose(i->fp);
4201 nasm_free(i->fname);
4202 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004203 }
4204 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004205 ctx_pop();
4206 if (pass == 0) {
4207 free_llist(predef);
4208 delete_Blocks();
4209 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004210}
4211
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004212void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004213{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004214 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004215
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004216 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004217 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004218 i->next = NULL;
4219
H. Peter Anvine2c80182005-01-15 22:15:51 +00004220 if (ipath != NULL) {
4221 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004222 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004223 j = j->next;
4224 j->next = i;
4225 } else {
4226 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004227 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004228}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004229
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004230void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004231{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004232 Token *inc, *space, *name;
4233 Line *l;
4234
H. Peter Anvin734b1882002-04-30 21:01:08 +00004235 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4236 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4237 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004238
4239 l = nasm_malloc(sizeof(Line));
4240 l->next = predef;
4241 l->first = inc;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004242 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004243 predef = l;
4244}
4245
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004246void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004247{
4248 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004249 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004250 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004251
4252 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004253 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4254 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004255 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004256 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004257 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004258 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004259 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004260
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004261 l = nasm_malloc(sizeof(Line));
4262 l->next = predef;
4263 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004264 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004265 predef = l;
4266}
4267
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004268void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004269{
4270 Token *def, *space;
4271 Line *l;
4272
H. Peter Anvin734b1882002-04-30 21:01:08 +00004273 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4274 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004275 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004276
4277 l = nasm_malloc(sizeof(Line));
4278 l->next = predef;
4279 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004280 l->finishes = false;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004281 predef = l;
4282}
4283
Keith Kaniosb7a89542007-04-12 02:40:54 +00004284/*
4285 * Added by Keith Kanios:
4286 *
4287 * This function is used to assist with "runtime" preprocessor
4288 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4289 *
4290 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4291 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4292 */
4293
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004294void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004295{
4296 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004297
Keith Kaniosb7a89542007-04-12 02:40:54 +00004298 def = tokenize(definition);
4299 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4300 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004301
Keith Kaniosb7a89542007-04-12 02:40:54 +00004302}
4303
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004304void pp_extra_stdmac(const char **macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004305{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004306 extrastdmac = macros;
4307}
4308
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004309static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004310{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004311 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004312 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004313 tok->text = nasm_strdup(numbuf);
4314 tok->type = TOK_NUMBER;
4315}
4316
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004317Preproc nasmpp = {
4318 pp_reset,
4319 pp_getline,
4320 pp_cleanup
4321};