blob: 04e50b1588f45cfa3ab9f9e2bee1ea6bebea2936 [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);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001137 tokval->t_charptr = tline->text;
H. Peter Anvin11627042008-06-09 20:45:19 -07001138 if (rn_error)
1139 return tokval->t_type = TOKEN_ERRNUM;
1140 else
1141 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001142 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001143
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001144 if (tline->type == TOK_FLOAT) {
1145 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001146 }
1147
H. Peter Anvine2c80182005-01-15 22:15:51 +00001148 if (tline->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001149 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001150
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001151 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001152 tokval->t_charptr = tline->text;
1153 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001154
H. Peter Anvin11627042008-06-09 20:45:19 -07001155 if (ep[0] != bq || ep[1] != '\0')
1156 return tokval->t_type = TOKEN_ERRSTR;
1157 else
1158 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001159 }
1160
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161 if (tline->type == TOK_OTHER) {
1162 if (!strcmp(tline->text, "<<"))
1163 return tokval->t_type = TOKEN_SHL;
1164 if (!strcmp(tline->text, ">>"))
1165 return tokval->t_type = TOKEN_SHR;
1166 if (!strcmp(tline->text, "//"))
1167 return tokval->t_type = TOKEN_SDIV;
1168 if (!strcmp(tline->text, "%%"))
1169 return tokval->t_type = TOKEN_SMOD;
1170 if (!strcmp(tline->text, "=="))
1171 return tokval->t_type = TOKEN_EQ;
1172 if (!strcmp(tline->text, "<>"))
1173 return tokval->t_type = TOKEN_NE;
1174 if (!strcmp(tline->text, "!="))
1175 return tokval->t_type = TOKEN_NE;
1176 if (!strcmp(tline->text, "<="))
1177 return tokval->t_type = TOKEN_LE;
1178 if (!strcmp(tline->text, ">="))
1179 return tokval->t_type = TOKEN_GE;
1180 if (!strcmp(tline->text, "&&"))
1181 return tokval->t_type = TOKEN_DBL_AND;
1182 if (!strcmp(tline->text, "^^"))
1183 return tokval->t_type = TOKEN_DBL_XOR;
1184 if (!strcmp(tline->text, "||"))
1185 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001186 }
1187
1188 /*
1189 * We have no other options: just return the first character of
1190 * the token text.
1191 */
1192 return tokval->t_type = tline->text[0];
1193}
1194
1195/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001196 * Compare a string to the name of an existing macro; this is a
1197 * simple wrapper which calls either strcmp or nasm_stricmp
1198 * depending on the value of the `casesense' parameter.
1199 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001200static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001201{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001202 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001203}
1204
1205/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001206 * Compare a string to the name of an existing macro; this is a
1207 * simple wrapper which calls either strcmp or nasm_stricmp
1208 * depending on the value of the `casesense' parameter.
1209 */
1210static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1211{
1212 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1213}
1214
1215/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001216 * Return the Context structure associated with a %$ token. Return
1217 * NULL, having _already_ reported an error condition, if the
1218 * context stack isn't deep enough for the supplied number of $
1219 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001220 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001221 * also scanned for such smacro, until it is found; if not -
1222 * only the context that directly results from the number of $'s
1223 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001224 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001225static Context *get_ctx(char *name, bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001226{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001227 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001228 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001229 int i;
1230
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001231 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001232 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001233
H. Peter Anvine2c80182005-01-15 22:15:51 +00001234 if (!cstk) {
1235 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1236 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001237 }
1238
H. Peter Anvine2c80182005-01-15 22:15:51 +00001239 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1240 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001241/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001242 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001243 if (!ctx) {
1244 error(ERR_NONFATAL, "`%s': context stack is only"
1245 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1246 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001247 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001248 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001249 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001250
H. Peter Anvine2c80182005-01-15 22:15:51 +00001251 do {
1252 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001253 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001254 while (m) {
1255 if (!mstrcmp(m->name, name, m->casesense))
1256 return ctx;
1257 m = m->next;
1258 }
1259 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001260 }
1261 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001262 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001263}
1264
1265/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001266 * Check to see if a file is already in a string list
1267 */
1268static bool in_list(const StrList *list, const char *str)
1269{
1270 while (list) {
1271 if (!strcmp(list->str, str))
1272 return true;
1273 list = list->next;
1274 }
1275 return false;
1276}
1277
1278/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001279 * Open an include file. This routine must always return a valid
1280 * file pointer if it returns - it's responsible for throwing an
1281 * ERR_FATAL and bombing out completely if not. It should also try
1282 * the include path one by one until it finds the file or reaches
1283 * the end of the path.
1284 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001285static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
H. Peter Anvin418ca702008-05-30 10:42:30 -07001286 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001287{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001288 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001289 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001290 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001291 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001292 size_t prefix_len = 0;
1293 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001294
H. Peter Anvine2c80182005-01-15 22:15:51 +00001295 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001296 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1297 memcpy(sl->str, prefix, prefix_len);
1298 memcpy(sl->str+prefix_len, file, len+1);
1299 fp = fopen(sl->str, "r");
H. Peter Anvin418ca702008-05-30 10:42:30 -07001300 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001301 sl->next = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001302 **dtail = sl;
1303 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001304 } else {
1305 nasm_free(sl);
1306 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001307 if (fp)
1308 return fp;
H. Peter Anvin418ca702008-05-30 10:42:30 -07001309 if (!ip) {
1310 if (!missing_ok)
1311 break;
1312 prefix = NULL;
1313 } else {
1314 prefix = ip->path;
1315 ip = ip->next;
1316 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001317 if (prefix) {
1318 prefix_len = strlen(prefix);
1319 } else {
1320 /* -MG given and file not found */
H. Peter Anvin418ca702008-05-30 10:42:30 -07001321 if (dhead && !in_list(*dhead, file)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001322 sl = nasm_malloc(len+1+sizeof sl->next);
1323 sl->next = NULL;
1324 strcpy(sl->str, file);
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001325 **dtail = sl;
1326 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001327 }
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001328 return NULL;
1329 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001330 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001331
H. Peter Anvin734b1882002-04-30 21:01:08 +00001332 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001334}
1335
1336/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001337 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001338 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001339 * return true if _any_ single-line macro of that name is defined.
1340 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001341 * `nparam' or no parameters is defined.
1342 *
1343 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001344 * defined, or nparam is -1, the address of the definition structure
1345 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001346 * is NULL, no action will be taken regarding its contents, and no
1347 * error will occur.
1348 *
1349 * Note that this is also called with nparam zero to resolve
1350 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001351 *
1352 * If you already know which context macro belongs to, you can pass
1353 * the context pointer as first parameter; if you won't but name begins
1354 * with %$ the context will be automatically computed. If all_contexts
1355 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001356 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001357static bool
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001358smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001359 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001360{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001361 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001362 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001363
H. Peter Anvin97a23472007-09-16 17:57:25 -07001364 if (ctx) {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001365 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001366 } else if (name[0] == '%' && name[1] == '$') {
1367 if (cstk)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001368 ctx = get_ctx(name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001369 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001370 return false; /* got to return _something_ */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001371 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001372 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001373 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001374 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001375 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001376
H. Peter Anvine2c80182005-01-15 22:15:51 +00001377 while (m) {
1378 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001379 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001381 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001382 *defn = m;
1383 else
1384 *defn = NULL;
1385 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001386 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001387 }
1388 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001389 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001390
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001391 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001392}
1393
1394/*
1395 * Count and mark off the parameters in a multi-line macro call.
1396 * This is called both from within the multi-line macro expansion
1397 * code, and also to mark off the default parameters when provided
1398 * in a %macro definition line.
1399 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001400static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001401{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001402 int paramsize, brace;
1403
1404 *nparam = paramsize = 0;
1405 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001406 while (t) {
1407 if (*nparam >= paramsize) {
1408 paramsize += PARAM_DELTA;
1409 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1410 }
1411 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001412 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001413 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001414 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001415 (*params)[(*nparam)++] = t;
1416 while (tok_isnt_(t, brace ? "}" : ","))
1417 t = t->next;
1418 if (t) { /* got a comma/brace */
1419 t = t->next;
1420 if (brace) {
1421 /*
1422 * Now we've found the closing brace, look further
1423 * for the comma.
1424 */
1425 skip_white_(t);
1426 if (tok_isnt_(t, ",")) {
1427 error(ERR_NONFATAL,
1428 "braces do not enclose all of macro parameter");
1429 while (tok_isnt_(t, ","))
1430 t = t->next;
1431 }
1432 if (t)
1433 t = t->next; /* eat the comma */
1434 }
1435 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001436 }
1437}
1438
1439/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001440 * Determine whether one of the various `if' conditions is true or
1441 * not.
1442 *
1443 * We must free the tline we get passed.
1444 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001445static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001446{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001447 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001448 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001449 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001450 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001451 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001452 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001453
1454 origline = tline;
1455
H. Peter Anvine2c80182005-01-15 22:15:51 +00001456 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001457 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001458 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001459 while (cstk && tline) {
1460 skip_white_(tline);
1461 if (!tline || tline->type != TOK_ID) {
1462 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001463 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001464 free_tlist(origline);
1465 return -1;
1466 }
1467 if (!nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001468 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001469 tline = tline->next;
1470 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001471 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001472
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001473 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001474 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001475 while (tline) {
1476 skip_white_(tline);
1477 if (!tline || (tline->type != TOK_ID &&
1478 (tline->type != TOK_PREPROC_ID ||
1479 tline->text[1] != '$'))) {
1480 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001481 "`%s' expects macro identifiers", pp_directives[ct]);
1482 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001483 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001484 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001485 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001486 tline = tline->next;
1487 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001488 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001489
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001490 case PPC_IFIDN:
1491 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001492 tline = expand_smacro(tline);
1493 t = tt = tline;
1494 while (tok_isnt_(tt, ","))
1495 tt = tt->next;
1496 if (!tt) {
1497 error(ERR_NONFATAL,
1498 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001499 pp_directives[ct]);
1500 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001501 }
1502 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001503 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001504 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1505 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1506 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001507 pp_directives[ct]);
1508 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001509 }
1510 if (t->type == TOK_WHITESPACE) {
1511 t = t->next;
1512 continue;
1513 }
1514 if (tt->type == TOK_WHITESPACE) {
1515 tt = tt->next;
1516 continue;
1517 }
1518 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001519 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001520 break;
1521 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001522 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001523 if (t->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001524 size_t l1 = nasm_unquote(t->text, NULL);
1525 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001526
1527 if (l1 != l2) {
1528 j = false;
1529 break;
1530 }
1531 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1532 j = false;
1533 break;
1534 }
1535 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001536 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001537 break;
1538 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001539
H. Peter Anvine2c80182005-01-15 22:15:51 +00001540 t = t->next;
1541 tt = tt->next;
1542 }
1543 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001544 j = false; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001545 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001546
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001547 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001548 {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001549 bool found = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001550 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001551
H. Peter Anvine2c80182005-01-15 22:15:51 +00001552 tline = tline->next;
1553 skip_white_(tline);
1554 tline = expand_id(tline);
1555 if (!tok_type_(tline, TOK_ID)) {
1556 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001557 "`%s' expects a macro name", pp_directives[ct]);
1558 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001559 }
1560 searching.name = nasm_strdup(tline->text);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001561 searching.casesense = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001562 searching.plus = false;
1563 searching.nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001564 searching.in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001565 searching.rep_nest = NULL;
1566 searching.nparam_min = 0;
1567 searching.nparam_max = INT_MAX;
1568 tline = expand_smacro(tline->next);
1569 skip_white_(tline);
1570 if (!tline) {
1571 } else if (!tok_type_(tline, TOK_NUMBER)) {
1572 error(ERR_NONFATAL,
1573 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001574 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001575 } else {
1576 searching.nparam_min = searching.nparam_max =
1577 readnum(tline->text, &j);
1578 if (j)
1579 error(ERR_NONFATAL,
1580 "unable to parse parameter count `%s'",
1581 tline->text);
1582 }
1583 if (tline && tok_is_(tline->next, "-")) {
1584 tline = tline->next->next;
1585 if (tok_is_(tline, "*"))
1586 searching.nparam_max = INT_MAX;
1587 else if (!tok_type_(tline, TOK_NUMBER))
1588 error(ERR_NONFATAL,
1589 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001590 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001591 else {
1592 searching.nparam_max = readnum(tline->text, &j);
1593 if (j)
1594 error(ERR_NONFATAL,
1595 "unable to parse parameter count `%s'",
1596 tline->text);
1597 if (searching.nparam_min > searching.nparam_max)
1598 error(ERR_NONFATAL,
1599 "minimum parameter count exceeds maximum");
1600 }
1601 }
1602 if (tline && tok_is_(tline->next, "+")) {
1603 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001604 searching.plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001605 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001606 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001607 while (mmac) {
1608 if (!strcmp(mmac->name, searching.name) &&
1609 (mmac->nparam_min <= searching.nparam_max
1610 || searching.plus)
1611 && (searching.nparam_min <= mmac->nparam_max
1612 || mmac->plus)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001613 found = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001614 break;
1615 }
1616 mmac = mmac->next;
1617 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001618 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001619 j = found;
1620 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001621 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001622
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001623 case PPC_IFID:
1624 needtype = TOK_ID;
1625 goto iftype;
1626 case PPC_IFNUM:
1627 needtype = TOK_NUMBER;
1628 goto iftype;
1629 case PPC_IFSTR:
1630 needtype = TOK_STRING;
1631 goto iftype;
1632
1633 iftype:
H. Peter Anvin134b9462008-02-16 17:01:40 -08001634 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001635
H. Peter Anvin927c92b2008-02-16 13:44:52 -08001636 while (tok_type_(t, TOK_WHITESPACE) ||
1637 (needtype == TOK_NUMBER &&
1638 tok_type_(t, TOK_OTHER) &&
1639 (t->text[0] == '-' || t->text[0] == '+') &&
1640 !t->text[1]))
1641 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001642
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001643 j = tok_type_(t, needtype);
1644 break;
1645
1646 case PPC_IFTOKEN:
1647 t = tline = expand_smacro(tline);
1648 while (tok_type_(t, TOK_WHITESPACE))
1649 t = t->next;
1650
1651 j = false;
1652 if (t) {
1653 t = t->next; /* Skip the actual token */
1654 while (tok_type_(t, TOK_WHITESPACE))
1655 t = t->next;
1656 j = !t; /* Should be nothing left */
1657 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001658 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001659
H. Peter Anvin134b9462008-02-16 17:01:40 -08001660 case PPC_IFEMPTY:
1661 t = tline = expand_smacro(tline);
1662 while (tok_type_(t, TOK_WHITESPACE))
1663 t = t->next;
1664
1665 j = !t; /* Should be empty */
1666 break;
1667
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001668 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001669 t = tline = expand_smacro(tline);
1670 tptr = &t;
1671 tokval.t_type = TOKEN_INVALID;
1672 evalresult = evaluate(ppscan, tptr, &tokval,
1673 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001674 if (!evalresult)
1675 return -1;
1676 if (tokval.t_type)
1677 error(ERR_WARNING,
1678 "trailing garbage after expression ignored");
1679 if (!is_simple(evalresult)) {
1680 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001681 "non-constant value given to `%s'", pp_directives[ct]);
1682 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001683 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001684 j = reloc_value(evalresult) != 0;
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001685 return j;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001686
H. Peter Anvine2c80182005-01-15 22:15:51 +00001687 default:
1688 error(ERR_FATAL,
1689 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001690 pp_directives[ct]);
1691 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001692 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001693
1694 free_tlist(origline);
1695 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001696
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001697fail:
1698 free_tlist(origline);
1699 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001700}
1701
1702/*
H. Peter Anvin418ca702008-05-30 10:42:30 -07001703 * Expand macros in a string. Used in %error directives (and it should
1704 * almost certainly be removed from there, too.)
1705 *
Keith Kaniosb7a89542007-04-12 02:40:54 +00001706 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001707 * The returned variable should ALWAYS be freed after usage.
1708 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001709void expand_macros_in_string(char **p)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001710{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001711 Token *line = tokenize(*p);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001712 line = expand_smacro(line);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001713 *p = detoken(line, false);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001714}
1715
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001716/*
1717 * Common code for defining an smacro
1718 */
1719static bool define_smacro(Context *ctx, char *mname, bool casesense,
1720 int nparam, Token *expansion)
1721{
1722 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001723 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001724
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001725 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1726 if (!smac) {
1727 error(ERR_WARNING,
1728 "single-line macro `%s' defined both with and"
1729 " without parameters", mname);
1730
1731 /* Some instances of the old code considered this a failure,
1732 some others didn't. What is the right thing to do here? */
1733 free_tlist(expansion);
1734 return false; /* Failure */
1735 } else {
1736 /*
1737 * We're redefining, so we have to take over an
1738 * existing SMacro structure. This means freeing
1739 * what was already in it.
1740 */
1741 nasm_free(smac->name);
1742 free_tlist(smac->expansion);
1743 }
1744 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001745 smtbl = ctx ? &ctx->localmac : &smacros;
1746 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001747 smac = nasm_malloc(sizeof(SMacro));
1748 smac->next = *smhead;
1749 *smhead = smac;
1750 }
1751 smac->name = nasm_strdup(mname);
1752 smac->casesense = casesense;
1753 smac->nparam = nparam;
1754 smac->expansion = expansion;
1755 smac->in_progress = false;
1756 return true; /* Success */
1757}
1758
1759/*
1760 * Undefine an smacro
1761 */
1762static void undef_smacro(Context *ctx, const char *mname)
1763{
1764 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001765 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001766
H. Peter Anvin166c2472008-05-28 12:28:58 -07001767 smtbl = ctx ? &ctx->localmac : &smacros;
1768 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001769
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001770 if (smhead) {
1771 /*
1772 * We now have a macro name... go hunt for it.
1773 */
1774 sp = smhead;
1775 while ((s = *sp) != NULL) {
1776 if (!mstrcmp(s->name, mname, s->casesense)) {
1777 *sp = s->next;
1778 nasm_free(s->name);
1779 free_tlist(s->expansion);
1780 nasm_free(s);
1781 } else {
1782 sp = &s->next;
1783 }
1784 }
1785 }
1786}
1787
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001788/*
1789 * Decode a size directive
1790 */
1791static int parse_size(const char *str) {
1792 static const char *size_names[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001793 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001794 static const int sizes[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001795 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001796
1797 return sizes[bsii(str, size_names, elements(size_names))+1];
1798}
1799
Ed Beroset3ab3f412002-06-11 03:31:49 +00001800/**
1801 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001802 * Find out if a line contains a preprocessor directive, and deal
1803 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07001804 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001805 * If a directive _is_ found, it is the responsibility of this routine
1806 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001807 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001808 * @param tline a pointer to the current tokeninzed line linked list
1809 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07001810 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001811 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001813{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001814 enum preproc_token i;
1815 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07001816 bool err;
1817 int nparam;
1818 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001819 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001820 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001821 int offset;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001822 char *p, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001823 Include *inc;
1824 Context *ctx;
1825 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001826 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001827 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1828 Line *l;
1829 struct tokenval tokval;
1830 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001832 int64_t count;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001833
1834 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001835
H. Peter Anvineba20a72002-04-30 20:53:55 +00001836 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07001837 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001838 (tline->text[1] == '%' || tline->text[1] == '$'
1839 || tline->text[1] == '!'))
1840 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001841
H. Peter Anvin4169a472007-09-12 01:29:43 +00001842 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001843
1844 /*
1845 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001846 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001847 * we should ignore all directives except for condition
1848 * directives.
1849 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001850 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001851 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1852 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001853 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001854
1855 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001856 * If we're defining a macro or reading a %rep block, we should
1857 * ignore all directives except for %macro/%imacro (which
1858 * generate an error), %endm/%endmacro, and (only if we're in a
H. Peter Anvineba20a72002-04-30 20:53:55 +00001859 * %rep block) %endrep. If we're in a %rep block, another %rep
1860 * causes an error, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001861 */
1862 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001863 i != PP_ENDMACRO && i != PP_ENDM &&
1864 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1865 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001866 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001867
H. Peter Anvin4169a472007-09-12 01:29:43 +00001868 switch (i) {
1869 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001870 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1871 tline->text);
1872 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001873
H. Peter Anvine2c80182005-01-15 22:15:51 +00001874 case PP_STACKSIZE:
1875 /* Directive to tell NASM what the default stack size is. The
1876 * default is for a 16-bit stack, and this can be overriden with
1877 * %stacksize large.
1878 * the following form:
1879 *
1880 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1881 */
1882 tline = tline->next;
1883 if (tline && tline->type == TOK_WHITESPACE)
1884 tline = tline->next;
1885 if (!tline || tline->type != TOK_ID) {
1886 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1887 free_tlist(origline);
1888 return DIRECTIVE_FOUND;
1889 }
1890 if (nasm_stricmp(tline->text, "flat") == 0) {
1891 /* All subsequent ARG directives are for a 32-bit stack */
1892 StackSize = 4;
1893 StackPointer = "ebp";
1894 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001895 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08001896 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
1897 /* All subsequent ARG directives are for a 64-bit stack */
1898 StackSize = 8;
1899 StackPointer = "rbp";
1900 ArgOffset = 8;
1901 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001902 } else if (nasm_stricmp(tline->text, "large") == 0) {
1903 /* All subsequent ARG directives are for a 16-bit stack,
1904 * far function call.
1905 */
1906 StackSize = 2;
1907 StackPointer = "bp";
1908 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001909 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001910 } else if (nasm_stricmp(tline->text, "small") == 0) {
1911 /* All subsequent ARG directives are for a 16-bit stack,
1912 * far function call. We don't support near functions.
1913 */
1914 StackSize = 2;
1915 StackPointer = "bp";
1916 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001917 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001918 } else {
1919 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1920 free_tlist(origline);
1921 return DIRECTIVE_FOUND;
1922 }
1923 free_tlist(origline);
1924 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001925
H. Peter Anvine2c80182005-01-15 22:15:51 +00001926 case PP_ARG:
1927 /* TASM like ARG directive to define arguments to functions, in
1928 * the following form:
1929 *
1930 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1931 */
1932 offset = ArgOffset;
1933 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001934 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001935 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001936
H. Peter Anvine2c80182005-01-15 22:15:51 +00001937 /* Find the argument name */
1938 tline = tline->next;
1939 if (tline && tline->type == TOK_WHITESPACE)
1940 tline = tline->next;
1941 if (!tline || tline->type != TOK_ID) {
1942 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1943 free_tlist(origline);
1944 return DIRECTIVE_FOUND;
1945 }
1946 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001947
H. Peter Anvine2c80182005-01-15 22:15:51 +00001948 /* Find the argument size type */
1949 tline = tline->next;
1950 if (!tline || tline->type != TOK_OTHER
1951 || tline->text[0] != ':') {
1952 error(ERR_NONFATAL,
1953 "Syntax error processing `%%arg' directive");
1954 free_tlist(origline);
1955 return DIRECTIVE_FOUND;
1956 }
1957 tline = tline->next;
1958 if (!tline || tline->type != TOK_ID) {
1959 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1960 free_tlist(origline);
1961 return DIRECTIVE_FOUND;
1962 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001963
H. Peter Anvine2c80182005-01-15 22:15:51 +00001964 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001965 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001966 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001967 size = parse_size(tt->text);
1968 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001969 error(ERR_NONFATAL,
1970 "Invalid size type for `%%arg' missing directive");
1971 free_tlist(tt);
1972 free_tlist(origline);
1973 return DIRECTIVE_FOUND;
1974 }
1975 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001976
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001977 /* Round up to even stack slots */
1978 size = (size+StackSize-1) & ~(StackSize-1);
1979
H. Peter Anvine2c80182005-01-15 22:15:51 +00001980 /* Now define the macro for the argument */
1981 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1982 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001983 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001984 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001985
H. Peter Anvine2c80182005-01-15 22:15:51 +00001986 /* Move to the next argument in the list */
1987 tline = tline->next;
1988 if (tline && tline->type == TOK_WHITESPACE)
1989 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001990 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1991 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001992 free_tlist(origline);
1993 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001994
H. Peter Anvine2c80182005-01-15 22:15:51 +00001995 case PP_LOCAL:
1996 /* TASM like LOCAL directive to define local variables for a
1997 * function, in the following form:
1998 *
1999 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2000 *
2001 * The '= LocalSize' at the end is ignored by NASM, but is
2002 * required by TASM to define the local parameter size (and used
2003 * by the TASM macro package).
2004 */
2005 offset = LocalOffset;
2006 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002007 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002008 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002009
H. Peter Anvine2c80182005-01-15 22:15:51 +00002010 /* Find the argument name */
2011 tline = tline->next;
2012 if (tline && tline->type == TOK_WHITESPACE)
2013 tline = tline->next;
2014 if (!tline || tline->type != TOK_ID) {
2015 error(ERR_NONFATAL,
2016 "`%%local' missing argument parameter");
2017 free_tlist(origline);
2018 return DIRECTIVE_FOUND;
2019 }
2020 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002021
H. Peter Anvine2c80182005-01-15 22:15:51 +00002022 /* Find the argument size type */
2023 tline = tline->next;
2024 if (!tline || tline->type != TOK_OTHER
2025 || tline->text[0] != ':') {
2026 error(ERR_NONFATAL,
2027 "Syntax error processing `%%local' directive");
2028 free_tlist(origline);
2029 return DIRECTIVE_FOUND;
2030 }
2031 tline = tline->next;
2032 if (!tline || tline->type != TOK_ID) {
2033 error(ERR_NONFATAL,
2034 "`%%local' missing size type parameter");
2035 free_tlist(origline);
2036 return DIRECTIVE_FOUND;
2037 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002038
H. Peter Anvine2c80182005-01-15 22:15:51 +00002039 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002040 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002041 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002042 size = parse_size(tt->text);
2043 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002044 error(ERR_NONFATAL,
2045 "Invalid size type for `%%local' missing directive");
2046 free_tlist(tt);
2047 free_tlist(origline);
2048 return DIRECTIVE_FOUND;
2049 }
2050 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002051
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002052 /* Round up to even stack slots */
2053 size = (size+StackSize-1) & ~(StackSize-1);
2054
2055 offset += size; /* Negative offset, increment before */
2056
2057 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002058 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2059 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002060 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002061
H. Peter Anvine2c80182005-01-15 22:15:51 +00002062 /* Now define the assign to setup the enter_c macro correctly */
2063 snprintf(directive, sizeof(directive),
2064 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002065 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002066
H. Peter Anvine2c80182005-01-15 22:15:51 +00002067 /* Move to the next argument in the list */
2068 tline = tline->next;
2069 if (tline && tline->type == TOK_WHITESPACE)
2070 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002071 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2072 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002073 free_tlist(origline);
2074 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002075
H. Peter Anvine2c80182005-01-15 22:15:51 +00002076 case PP_CLEAR:
2077 if (tline->next)
2078 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002079 free_macros();
2080 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002081 free_tlist(origline);
2082 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002083
H. Peter Anvin418ca702008-05-30 10:42:30 -07002084 case PP_DEPEND:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002085 t = tline->next = expand_smacro(tline->next);
2086 skip_white_(t);
2087 if (!t || (t->type != TOK_STRING &&
2088 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002089 error(ERR_NONFATAL, "`%%depend' expects a file name");
2090 free_tlist(origline);
2091 return DIRECTIVE_FOUND; /* but we did _something_ */
2092 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002093 if (t->next)
H. Peter Anvin418ca702008-05-30 10:42:30 -07002094 error(ERR_WARNING,
2095 "trailing garbage after `%%depend' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002096 p = t->text;
2097 if (t->type != TOK_INTERNAL_STRING)
2098 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002099 if (dephead && !in_list(*dephead, p)) {
2100 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2101 sl->next = NULL;
2102 strcpy(sl->str, p);
2103 *deptail = sl;
2104 deptail = &sl->next;
2105 }
2106 free_tlist(origline);
2107 return DIRECTIVE_FOUND;
2108
2109 case PP_INCLUDE:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002110 t = tline->next = expand_smacro(tline->next);
2111 skip_white_(t);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002112
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002113 if (!t || (t->type != TOK_STRING &&
2114 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002115 error(ERR_NONFATAL, "`%%include' expects a file name");
2116 free_tlist(origline);
2117 return DIRECTIVE_FOUND; /* but we did _something_ */
2118 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002119 if (t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002120 error(ERR_WARNING,
2121 "trailing garbage after `%%include' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002122 p = t->text;
2123 if (t->type != TOK_INTERNAL_STRING)
2124 nasm_unquote(p, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002125 inc = nasm_malloc(sizeof(Include));
2126 inc->next = istk;
2127 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002128 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002129 if (!inc->fp) {
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002130 /* -MG given but file not found */
2131 nasm_free(inc);
2132 } else {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002133 inc->fname = src_set_fname(nasm_strdup(p));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002134 inc->lineno = src_set_linnum(0);
2135 inc->lineinc = 1;
2136 inc->expansion = NULL;
2137 inc->mstk = NULL;
2138 istk = inc;
2139 list->uplevel(LIST_INCLUDE);
2140 }
2141 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002142 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002143
H. Peter Anvine2c80182005-01-15 22:15:51 +00002144 case PP_PUSH:
2145 tline = tline->next;
2146 skip_white_(tline);
2147 tline = expand_id(tline);
2148 if (!tok_type_(tline, TOK_ID)) {
2149 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2150 free_tlist(origline);
2151 return DIRECTIVE_FOUND; /* but we did _something_ */
2152 }
2153 if (tline->next)
2154 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2155 ctx = nasm_malloc(sizeof(Context));
2156 ctx->next = cstk;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002157 hash_init(&ctx->localmac, HASH_SMALL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002158 ctx->name = nasm_strdup(tline->text);
2159 ctx->number = unique++;
2160 cstk = ctx;
2161 free_tlist(origline);
2162 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002163
H. Peter Anvine2c80182005-01-15 22:15:51 +00002164 case PP_REPL:
2165 tline = tline->next;
2166 skip_white_(tline);
2167 tline = expand_id(tline);
2168 if (!tok_type_(tline, TOK_ID)) {
2169 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2170 free_tlist(origline);
2171 return DIRECTIVE_FOUND; /* but we did _something_ */
2172 }
2173 if (tline->next)
2174 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2175 if (!cstk)
2176 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2177 else {
2178 nasm_free(cstk->name);
2179 cstk->name = nasm_strdup(tline->text);
2180 }
2181 free_tlist(origline);
2182 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002183
H. Peter Anvine2c80182005-01-15 22:15:51 +00002184 case PP_POP:
2185 if (tline->next)
2186 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2187 if (!cstk)
2188 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2189 else
2190 ctx_pop();
2191 free_tlist(origline);
2192 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002193
H. Peter Anvine2c80182005-01-15 22:15:51 +00002194 case PP_ERROR:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002195 case PP_WARNING:
2196 {
2197 int severity = PP_ERROR ? ERR_NONFATAL|ERR_NO_SEVERITY :
2198 ERR_WARNING|ERR_NO_SEVERITY;
2199
H. Peter Anvine2c80182005-01-15 22:15:51 +00002200 tline->next = expand_smacro(tline->next);
2201 tline = tline->next;
2202 skip_white_(tline);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002203 t = tline ? tline->next : NULL;
2204 skip_white_(t);
2205 if (tok_type_(tline, TOK_STRING) && !t) {
2206 /* The line contains only a quoted string */
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002207 p = tline->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002208 nasm_unquote(p, NULL);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002209 error(severity, "%s: %s", pp_directives[i], p);
2210 } else {
2211 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002212 p = detoken(tline, false);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002213 error(severity, "%s: %s", pp_directives[i], p);
2214 nasm_free(p);
2215 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002216 free_tlist(origline);
2217 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002218 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002219
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002220 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002221 if (istk->conds && !emitting(istk->conds->state))
2222 j = COND_NEVER;
2223 else {
2224 j = if_condition(tline->next, i);
2225 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002226 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2227 }
2228 cond = nasm_malloc(sizeof(Cond));
2229 cond->next = istk->conds;
2230 cond->state = j;
2231 istk->conds = cond;
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002232 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002233 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002234
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002235 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002236 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002237 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002238 if (emitting(istk->conds->state)
2239 || istk->conds->state == COND_NEVER)
2240 istk->conds->state = COND_NEVER;
2241 else {
2242 /*
2243 * IMPORTANT: In the case of %if, we will already have
2244 * called expand_mmac_params(); however, if we're
2245 * processing an %elif we must have been in a
2246 * non-emitting mode, which would have inhibited
2247 * the normal invocation of expand_mmac_params(). Therefore,
2248 * we have to do it explicitly here.
2249 */
2250 j = if_condition(expand_mmac_params(tline->next), i);
2251 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002252 istk->conds->state =
2253 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2254 }
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002255 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002256 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002257
H. Peter Anvine2c80182005-01-15 22:15:51 +00002258 case PP_ELSE:
2259 if (tline->next)
2260 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2261 if (!istk->conds)
2262 error(ERR_FATAL, "`%%else': no matching `%%if'");
2263 if (emitting(istk->conds->state)
2264 || istk->conds->state == COND_NEVER)
2265 istk->conds->state = COND_ELSE_FALSE;
2266 else
2267 istk->conds->state = COND_ELSE_TRUE;
2268 free_tlist(origline);
2269 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002270
H. Peter Anvine2c80182005-01-15 22:15:51 +00002271 case PP_ENDIF:
2272 if (tline->next)
2273 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2274 if (!istk->conds)
2275 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2276 cond = istk->conds;
2277 istk->conds = cond->next;
2278 nasm_free(cond);
2279 free_tlist(origline);
2280 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002281
H. Peter Anvine2c80182005-01-15 22:15:51 +00002282 case PP_MACRO:
2283 case PP_IMACRO:
2284 if (defining)
2285 error(ERR_FATAL,
2286 "`%%%smacro': already defining a macro",
2287 (i == PP_IMACRO ? "i" : ""));
2288 tline = tline->next;
2289 skip_white_(tline);
2290 tline = expand_id(tline);
2291 if (!tok_type_(tline, TOK_ID)) {
2292 error(ERR_NONFATAL,
2293 "`%%%smacro' expects a macro name",
2294 (i == PP_IMACRO ? "i" : ""));
2295 return DIRECTIVE_FOUND;
2296 }
2297 defining = nasm_malloc(sizeof(MMacro));
2298 defining->name = nasm_strdup(tline->text);
2299 defining->casesense = (i == PP_MACRO);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002300 defining->plus = false;
2301 defining->nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002302 defining->in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002303 defining->rep_nest = NULL;
2304 tline = expand_smacro(tline->next);
2305 skip_white_(tline);
2306 if (!tok_type_(tline, TOK_NUMBER)) {
2307 error(ERR_NONFATAL,
2308 "`%%%smacro' expects a parameter count",
2309 (i == PP_IMACRO ? "i" : ""));
2310 defining->nparam_min = defining->nparam_max = 0;
2311 } else {
2312 defining->nparam_min = defining->nparam_max =
H. Peter Anvin70055962007-10-11 00:05:31 -07002313 readnum(tline->text, &err);
2314 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002315 error(ERR_NONFATAL,
2316 "unable to parse parameter count `%s'", tline->text);
2317 }
2318 if (tline && tok_is_(tline->next, "-")) {
2319 tline = tline->next->next;
2320 if (tok_is_(tline, "*"))
2321 defining->nparam_max = INT_MAX;
2322 else if (!tok_type_(tline, TOK_NUMBER))
2323 error(ERR_NONFATAL,
2324 "`%%%smacro' expects a parameter count after `-'",
2325 (i == PP_IMACRO ? "i" : ""));
2326 else {
H. Peter Anvin70055962007-10-11 00:05:31 -07002327 defining->nparam_max = readnum(tline->text, &err);
2328 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002329 error(ERR_NONFATAL,
2330 "unable to parse parameter count `%s'",
2331 tline->text);
2332 if (defining->nparam_min > defining->nparam_max)
2333 error(ERR_NONFATAL,
2334 "minimum parameter count exceeds maximum");
2335 }
2336 }
2337 if (tline && tok_is_(tline->next, "+")) {
2338 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002339 defining->plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002340 }
2341 if (tline && tok_type_(tline->next, TOK_ID) &&
2342 !nasm_stricmp(tline->next->text, ".nolist")) {
2343 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002344 defining->nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002345 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002346 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002347 while (mmac) {
2348 if (!strcmp(mmac->name, defining->name) &&
2349 (mmac->nparam_min <= defining->nparam_max
2350 || defining->plus)
2351 && (defining->nparam_min <= mmac->nparam_max
2352 || mmac->plus)) {
2353 error(ERR_WARNING,
2354 "redefining multi-line macro `%s'", defining->name);
2355 break;
2356 }
2357 mmac = mmac->next;
2358 }
2359 /*
2360 * Handle default parameters.
2361 */
2362 if (tline && tline->next) {
2363 defining->dlist = tline->next;
2364 tline->next = NULL;
2365 count_mmac_params(defining->dlist, &defining->ndefs,
2366 &defining->defaults);
2367 } else {
2368 defining->dlist = NULL;
2369 defining->defaults = NULL;
2370 }
2371 defining->expansion = NULL;
2372 free_tlist(origline);
2373 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002374
H. Peter Anvine2c80182005-01-15 22:15:51 +00002375 case PP_ENDM:
2376 case PP_ENDMACRO:
2377 if (!defining) {
2378 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2379 return DIRECTIVE_FOUND;
2380 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002381 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002382 defining->next = *mmhead;
2383 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 defining = NULL;
2385 free_tlist(origline);
2386 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002387
H. Peter Anvine2c80182005-01-15 22:15:51 +00002388 case PP_ROTATE:
2389 if (tline->next && tline->next->type == TOK_WHITESPACE)
2390 tline = tline->next;
2391 if (tline->next == NULL) {
2392 free_tlist(origline);
2393 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2394 return DIRECTIVE_FOUND;
2395 }
2396 t = expand_smacro(tline->next);
2397 tline->next = NULL;
2398 free_tlist(origline);
2399 tline = t;
2400 tptr = &t;
2401 tokval.t_type = TOKEN_INVALID;
2402 evalresult =
2403 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2404 free_tlist(tline);
2405 if (!evalresult)
2406 return DIRECTIVE_FOUND;
2407 if (tokval.t_type)
2408 error(ERR_WARNING,
2409 "trailing garbage after expression ignored");
2410 if (!is_simple(evalresult)) {
2411 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2412 return DIRECTIVE_FOUND;
2413 }
2414 mmac = istk->mstk;
2415 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2416 mmac = mmac->next_active;
2417 if (!mmac) {
2418 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2419 } else if (mmac->nparam == 0) {
2420 error(ERR_NONFATAL,
2421 "`%%rotate' invoked within macro without parameters");
2422 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002423 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002424
H. Peter Anvin25a99342007-09-22 17:45:45 -07002425 rotate %= (int)mmac->nparam;
2426 if (rotate < 0)
2427 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002428
H. Peter Anvin25a99342007-09-22 17:45:45 -07002429 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002430 }
2431 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002432
H. Peter Anvine2c80182005-01-15 22:15:51 +00002433 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002434 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002435 do {
2436 tline = tline->next;
2437 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002438
H. Peter Anvine2c80182005-01-15 22:15:51 +00002439 if (tok_type_(tline, TOK_ID) &&
2440 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002441 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002442 do {
2443 tline = tline->next;
2444 } while (tok_type_(tline, TOK_WHITESPACE));
2445 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002446
H. Peter Anvine2c80182005-01-15 22:15:51 +00002447 if (tline) {
2448 t = expand_smacro(tline);
2449 tptr = &t;
2450 tokval.t_type = TOKEN_INVALID;
2451 evalresult =
2452 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2453 if (!evalresult) {
2454 free_tlist(origline);
2455 return DIRECTIVE_FOUND;
2456 }
2457 if (tokval.t_type)
2458 error(ERR_WARNING,
2459 "trailing garbage after expression ignored");
2460 if (!is_simple(evalresult)) {
2461 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2462 return DIRECTIVE_FOUND;
2463 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002464 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002465 } else {
2466 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002467 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002468 }
2469 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002470
H. Peter Anvine2c80182005-01-15 22:15:51 +00002471 tmp_defining = defining;
2472 defining = nasm_malloc(sizeof(MMacro));
2473 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002474 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002475 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002476 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002477 defining->in_progress = count;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002478 defining->nparam_min = defining->nparam_max = 0;
2479 defining->defaults = NULL;
2480 defining->dlist = NULL;
2481 defining->expansion = NULL;
2482 defining->next_active = istk->mstk;
2483 defining->rep_nest = tmp_defining;
2484 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002485
H. Peter Anvine2c80182005-01-15 22:15:51 +00002486 case PP_ENDREP:
2487 if (!defining || defining->name) {
2488 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2489 return DIRECTIVE_FOUND;
2490 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002491
H. Peter Anvine2c80182005-01-15 22:15:51 +00002492 /*
2493 * Now we have a "macro" defined - although it has no name
2494 * and we won't be entering it in the hash tables - we must
2495 * push a macro-end marker for it on to istk->expansion.
2496 * After that, it will take care of propagating itself (a
2497 * macro-end marker line for a macro which is really a %rep
2498 * block will cause the macro to be re-expanded, complete
2499 * with another macro-end marker to ensure the process
2500 * continues) until the whole expansion is forcibly removed
2501 * from istk->expansion by a %exitrep.
2502 */
2503 l = nasm_malloc(sizeof(Line));
2504 l->next = istk->expansion;
2505 l->finishes = defining;
2506 l->first = NULL;
2507 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002508
H. Peter Anvine2c80182005-01-15 22:15:51 +00002509 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002510
H. Peter Anvine2c80182005-01-15 22:15:51 +00002511 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2512 tmp_defining = defining;
2513 defining = defining->rep_nest;
2514 free_tlist(origline);
2515 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002516
H. Peter Anvine2c80182005-01-15 22:15:51 +00002517 case PP_EXITREP:
2518 /*
2519 * We must search along istk->expansion until we hit a
2520 * macro-end marker for a macro with no name. Then we set
2521 * its `in_progress' flag to 0.
2522 */
2523 for (l = istk->expansion; l; l = l->next)
2524 if (l->finishes && !l->finishes->name)
2525 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002526
H. Peter Anvine2c80182005-01-15 22:15:51 +00002527 if (l)
2528 l->finishes->in_progress = 0;
2529 else
2530 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2531 free_tlist(origline);
2532 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002533
H. Peter Anvine2c80182005-01-15 22:15:51 +00002534 case PP_XDEFINE:
2535 case PP_IXDEFINE:
2536 case PP_DEFINE:
2537 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002538 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002539
H. Peter Anvine2c80182005-01-15 22:15:51 +00002540 tline = tline->next;
2541 skip_white_(tline);
2542 tline = expand_id(tline);
2543 if (!tline || (tline->type != TOK_ID &&
2544 (tline->type != TOK_PREPROC_ID ||
2545 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002546 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2547 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002548 free_tlist(origline);
2549 return DIRECTIVE_FOUND;
2550 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002551
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002552 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002553
H. Peter Anvine2c80182005-01-15 22:15:51 +00002554 mname = tline->text;
2555 last = tline;
2556 param_start = tline = tline->next;
2557 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002558
H. Peter Anvine2c80182005-01-15 22:15:51 +00002559 /* Expand the macro definition now for %xdefine and %ixdefine */
2560 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2561 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002562
H. Peter Anvine2c80182005-01-15 22:15:51 +00002563 if (tok_is_(tline, "(")) {
2564 /*
2565 * This macro has parameters.
2566 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002567
H. Peter Anvine2c80182005-01-15 22:15:51 +00002568 tline = tline->next;
2569 while (1) {
2570 skip_white_(tline);
2571 if (!tline) {
2572 error(ERR_NONFATAL, "parameter identifier expected");
2573 free_tlist(origline);
2574 return DIRECTIVE_FOUND;
2575 }
2576 if (tline->type != TOK_ID) {
2577 error(ERR_NONFATAL,
2578 "`%s': parameter identifier expected",
2579 tline->text);
2580 free_tlist(origline);
2581 return DIRECTIVE_FOUND;
2582 }
2583 tline->type = TOK_SMAC_PARAM + nparam++;
2584 tline = tline->next;
2585 skip_white_(tline);
2586 if (tok_is_(tline, ",")) {
2587 tline = tline->next;
2588 continue;
2589 }
2590 if (!tok_is_(tline, ")")) {
2591 error(ERR_NONFATAL,
2592 "`)' expected to terminate macro template");
2593 free_tlist(origline);
2594 return DIRECTIVE_FOUND;
2595 }
2596 break;
2597 }
2598 last = tline;
2599 tline = tline->next;
2600 }
2601 if (tok_type_(tline, TOK_WHITESPACE))
2602 last = tline, tline = tline->next;
2603 macro_start = NULL;
2604 last->next = NULL;
2605 t = tline;
2606 while (t) {
2607 if (t->type == TOK_ID) {
2608 for (tt = param_start; tt; tt = tt->next)
2609 if (tt->type >= TOK_SMAC_PARAM &&
2610 !strcmp(tt->text, t->text))
2611 t->type = tt->type;
2612 }
2613 tt = t->next;
2614 t->next = macro_start;
2615 macro_start = t;
2616 t = tt;
2617 }
2618 /*
2619 * Good. We now have a macro name, a parameter count, and a
2620 * token list (in reverse order) for an expansion. We ought
2621 * to be OK just to create an SMacro, store it, and let
2622 * free_tlist have the rest of the line (which we have
2623 * carefully re-terminated after chopping off the expansion
2624 * from the end).
2625 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002626 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002627 free_tlist(origline);
2628 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002629
H. Peter Anvine2c80182005-01-15 22:15:51 +00002630 case PP_UNDEF:
2631 tline = tline->next;
2632 skip_white_(tline);
2633 tline = expand_id(tline);
2634 if (!tline || (tline->type != TOK_ID &&
2635 (tline->type != TOK_PREPROC_ID ||
2636 tline->text[1] != '$'))) {
2637 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2638 free_tlist(origline);
2639 return DIRECTIVE_FOUND;
2640 }
2641 if (tline->next) {
2642 error(ERR_WARNING,
2643 "trailing garbage after macro name ignored");
2644 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002645
H. Peter Anvine2c80182005-01-15 22:15:51 +00002646 /* Find the context that symbol belongs to */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002647 ctx = get_ctx(tline->text, false);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002648 undef_smacro(ctx, tline->text);
2649 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002650 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002651
H. Peter Anvin9e200162008-06-04 17:23:14 -07002652 case PP_DEFSTR:
2653 case PP_IDEFSTR:
2654 casesense = (i == PP_DEFSTR);
2655
2656 tline = tline->next;
2657 skip_white_(tline);
2658 tline = expand_id(tline);
2659 if (!tline || (tline->type != TOK_ID &&
2660 (tline->type != TOK_PREPROC_ID ||
2661 tline->text[1] != '$'))) {
2662 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2663 pp_directives[i]);
2664 free_tlist(origline);
2665 return DIRECTIVE_FOUND;
2666 }
2667
2668 ctx = get_ctx(tline->text, false);
2669
2670 mname = tline->text;
2671 last = tline;
2672 tline = expand_smacro(tline->next);
2673 last->next = NULL;
2674
2675 while (tok_type_(tline, TOK_WHITESPACE))
2676 tline = delete_Token(tline);
2677
2678 p = detoken(tline, false);
2679 macro_start = nasm_malloc(sizeof(*macro_start));
2680 macro_start->next = NULL;
2681 macro_start->text = nasm_quote(p, strlen(p));
2682 macro_start->type = TOK_STRING;
2683 macro_start->mac = NULL;
2684 nasm_free(p);
2685
2686 /*
2687 * We now have a macro name, an implicit parameter count of
2688 * zero, and a string token to use as an expansion. Create
2689 * and store an SMacro.
2690 */
2691 define_smacro(ctx, mname, casesense, 0, macro_start);
2692 free_tlist(origline);
2693 return DIRECTIVE_FOUND;
2694
H. Peter Anvin418ca702008-05-30 10:42:30 -07002695 case PP_PATHSEARCH:
2696 {
2697 FILE *fp;
2698 StrList *xsl = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002699 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002700
2701 casesense = true;
2702
2703 tline = tline->next;
2704 skip_white_(tline);
2705 tline = expand_id(tline);
2706 if (!tline || (tline->type != TOK_ID &&
2707 (tline->type != TOK_PREPROC_ID ||
2708 tline->text[1] != '$'))) {
2709 error(ERR_NONFATAL,
2710 "`%%pathsearch' expects a macro identifier as first parameter");
2711 free_tlist(origline);
2712 return DIRECTIVE_FOUND;
2713 }
2714 ctx = get_ctx(tline->text, false);
2715
2716 mname = tline->text;
2717 last = tline;
2718 tline = expand_smacro(tline->next);
2719 last->next = NULL;
2720
2721 t = tline;
2722 while (tok_type_(t, TOK_WHITESPACE))
2723 t = t->next;
2724
2725 if (!t || (t->type != TOK_STRING &&
2726 t->type != TOK_INTERNAL_STRING)) {
2727 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
2728 free_tlist(tline);
2729 free_tlist(origline);
2730 return DIRECTIVE_FOUND; /* but we did _something_ */
2731 }
2732 if (t->next)
2733 error(ERR_WARNING,
2734 "trailing garbage after `%%pathsearch' ignored");
H. Peter Anvin427cc912008-06-01 21:43:03 -07002735 p = t->text;
2736 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002737 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002738
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002739 fp = inc_fopen(p, &xsl, &xst, true);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002740 if (fp) {
2741 p = xsl->str;
2742 fclose(fp); /* Don't actually care about the file */
2743 }
2744 macro_start = nasm_malloc(sizeof(*macro_start));
2745 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002746 macro_start->text = nasm_quote(p, strlen(p));
H. Peter Anvin418ca702008-05-30 10:42:30 -07002747 macro_start->type = TOK_STRING;
2748 macro_start->mac = NULL;
2749 if (xsl)
2750 nasm_free(xsl);
2751
2752 /*
2753 * We now have a macro name, an implicit parameter count of
2754 * zero, and a string token to use as an expansion. Create
2755 * and store an SMacro.
2756 */
2757 define_smacro(ctx, mname, casesense, 0, macro_start);
2758 free_tlist(tline);
2759 free_tlist(origline);
2760 return DIRECTIVE_FOUND;
2761 }
2762
H. Peter Anvine2c80182005-01-15 22:15:51 +00002763 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002764 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07002765
H. Peter Anvine2c80182005-01-15 22:15:51 +00002766 tline = tline->next;
2767 skip_white_(tline);
2768 tline = expand_id(tline);
2769 if (!tline || (tline->type != TOK_ID &&
2770 (tline->type != TOK_PREPROC_ID ||
2771 tline->text[1] != '$'))) {
2772 error(ERR_NONFATAL,
2773 "`%%strlen' expects a macro identifier as first parameter");
2774 free_tlist(origline);
2775 return DIRECTIVE_FOUND;
2776 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002777 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002778
H. Peter Anvine2c80182005-01-15 22:15:51 +00002779 mname = tline->text;
2780 last = tline;
2781 tline = expand_smacro(tline->next);
2782 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002783
H. Peter Anvine2c80182005-01-15 22:15:51 +00002784 t = tline;
2785 while (tok_type_(t, TOK_WHITESPACE))
2786 t = t->next;
2787 /* t should now point to the string */
2788 if (t->type != TOK_STRING) {
2789 error(ERR_NONFATAL,
2790 "`%%strlen` requires string as second parameter");
2791 free_tlist(tline);
2792 free_tlist(origline);
2793 return DIRECTIVE_FOUND;
2794 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002795
H. Peter Anvine2c80182005-01-15 22:15:51 +00002796 macro_start = nasm_malloc(sizeof(*macro_start));
2797 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002798 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002799 macro_start->mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002800
H. Peter Anvine2c80182005-01-15 22:15:51 +00002801 /*
2802 * We now have a macro name, an implicit parameter count of
2803 * zero, and a numeric token to use as an expansion. Create
2804 * and store an SMacro.
2805 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002806 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002807 free_tlist(tline);
2808 free_tlist(origline);
2809 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002810
H. Peter Anvine2c80182005-01-15 22:15:51 +00002811 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002812 {
2813 int64_t a1, a2;
2814 size_t len;
2815
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002816 casesense = true;
2817
H. Peter Anvine2c80182005-01-15 22:15:51 +00002818 tline = tline->next;
2819 skip_white_(tline);
2820 tline = expand_id(tline);
2821 if (!tline || (tline->type != TOK_ID &&
2822 (tline->type != TOK_PREPROC_ID ||
2823 tline->text[1] != '$'))) {
2824 error(ERR_NONFATAL,
2825 "`%%substr' expects a macro identifier as first parameter");
2826 free_tlist(origline);
2827 return DIRECTIVE_FOUND;
2828 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002829 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002830
H. Peter Anvine2c80182005-01-15 22:15:51 +00002831 mname = tline->text;
2832 last = tline;
2833 tline = expand_smacro(tline->next);
2834 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002835
H. Peter Anvine2c80182005-01-15 22:15:51 +00002836 t = tline->next;
2837 while (tok_type_(t, TOK_WHITESPACE))
2838 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002839
H. Peter Anvine2c80182005-01-15 22:15:51 +00002840 /* t should now point to the string */
2841 if (t->type != TOK_STRING) {
2842 error(ERR_NONFATAL,
2843 "`%%substr` requires string as second parameter");
2844 free_tlist(tline);
2845 free_tlist(origline);
2846 return DIRECTIVE_FOUND;
2847 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002848
H. Peter Anvine2c80182005-01-15 22:15:51 +00002849 tt = t->next;
2850 tptr = &tt;
2851 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002852 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
2853 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002854 if (!evalresult) {
2855 free_tlist(tline);
2856 free_tlist(origline);
2857 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002858 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002859 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2860 free_tlist(tline);
2861 free_tlist(origline);
2862 return DIRECTIVE_FOUND;
2863 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002864 a1 = evalresult->value-1;
2865
2866 while (tok_type_(tt, TOK_WHITESPACE))
2867 tt = tt->next;
2868 if (!tt) {
2869 a2 = 1; /* Backwards compatibility: one character */
2870 } else {
2871 tokval.t_type = TOKEN_INVALID;
2872 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
2873 pass, error, NULL);
2874 if (!evalresult) {
2875 free_tlist(tline);
2876 free_tlist(origline);
2877 return DIRECTIVE_FOUND;
2878 } else if (!is_simple(evalresult)) {
2879 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2880 free_tlist(tline);
2881 free_tlist(origline);
2882 return DIRECTIVE_FOUND;
2883 }
2884 a2 = evalresult->value;
2885 }
2886
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002887 len = nasm_unquote(t->text, NULL);
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002888 if (a2 < 0)
2889 a2 = a2+1+len-a1;
2890 if (a1+a2 > (int64_t)len)
2891 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002892
H. Peter Anvine2c80182005-01-15 22:15:51 +00002893 macro_start = nasm_malloc(sizeof(*macro_start));
2894 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002895 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002896 macro_start->type = TOK_STRING;
2897 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002898
H. Peter Anvine2c80182005-01-15 22:15:51 +00002899 /*
2900 * We now have a macro name, an implicit parameter count of
2901 * zero, and a numeric token to use as an expansion. Create
2902 * and store an SMacro.
2903 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002904 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002905 free_tlist(tline);
2906 free_tlist(origline);
2907 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002908 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002909
H. Peter Anvine2c80182005-01-15 22:15:51 +00002910 case PP_ASSIGN:
2911 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002912 casesense = (i == PP_ASSIGN);
2913
H. Peter Anvine2c80182005-01-15 22:15:51 +00002914 tline = tline->next;
2915 skip_white_(tline);
2916 tline = expand_id(tline);
2917 if (!tline || (tline->type != TOK_ID &&
2918 (tline->type != TOK_PREPROC_ID ||
2919 tline->text[1] != '$'))) {
2920 error(ERR_NONFATAL,
2921 "`%%%sassign' expects a macro identifier",
2922 (i == PP_IASSIGN ? "i" : ""));
2923 free_tlist(origline);
2924 return DIRECTIVE_FOUND;
2925 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002926 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002927
H. Peter Anvine2c80182005-01-15 22:15:51 +00002928 mname = tline->text;
2929 last = tline;
2930 tline = expand_smacro(tline->next);
2931 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002932
H. Peter Anvine2c80182005-01-15 22:15:51 +00002933 t = tline;
2934 tptr = &t;
2935 tokval.t_type = TOKEN_INVALID;
2936 evalresult =
2937 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2938 free_tlist(tline);
2939 if (!evalresult) {
2940 free_tlist(origline);
2941 return DIRECTIVE_FOUND;
2942 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002943
H. Peter Anvine2c80182005-01-15 22:15:51 +00002944 if (tokval.t_type)
2945 error(ERR_WARNING,
2946 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00002947
H. Peter Anvine2c80182005-01-15 22:15:51 +00002948 if (!is_simple(evalresult)) {
2949 error(ERR_NONFATAL,
2950 "non-constant value given to `%%%sassign'",
2951 (i == PP_IASSIGN ? "i" : ""));
2952 free_tlist(origline);
2953 return DIRECTIVE_FOUND;
2954 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002955
H. Peter Anvine2c80182005-01-15 22:15:51 +00002956 macro_start = nasm_malloc(sizeof(*macro_start));
2957 macro_start->next = NULL;
2958 make_tok_num(macro_start, reloc_value(evalresult));
2959 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002960
H. Peter Anvine2c80182005-01-15 22:15:51 +00002961 /*
2962 * We now have a macro name, an implicit parameter count of
2963 * zero, and a numeric token to use as an expansion. Create
2964 * and store an SMacro.
2965 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002966 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002967 free_tlist(origline);
2968 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002969
H. Peter Anvine2c80182005-01-15 22:15:51 +00002970 case PP_LINE:
2971 /*
2972 * Syntax is `%line nnn[+mmm] [filename]'
2973 */
2974 tline = tline->next;
2975 skip_white_(tline);
2976 if (!tok_type_(tline, TOK_NUMBER)) {
2977 error(ERR_NONFATAL, "`%%line' expects line number");
2978 free_tlist(origline);
2979 return DIRECTIVE_FOUND;
2980 }
H. Peter Anvin70055962007-10-11 00:05:31 -07002981 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002982 m = 1;
2983 tline = tline->next;
2984 if (tok_is_(tline, "+")) {
2985 tline = tline->next;
2986 if (!tok_type_(tline, TOK_NUMBER)) {
2987 error(ERR_NONFATAL, "`%%line' expects line increment");
2988 free_tlist(origline);
2989 return DIRECTIVE_FOUND;
2990 }
H. Peter Anvin70055962007-10-11 00:05:31 -07002991 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002992 tline = tline->next;
2993 }
2994 skip_white_(tline);
2995 src_set_linnum(k);
2996 istk->lineinc = m;
2997 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002998 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002999 }
3000 free_tlist(origline);
3001 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003002
H. Peter Anvine2c80182005-01-15 22:15:51 +00003003 default:
3004 error(ERR_FATAL,
3005 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003006 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003007 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003008 }
Ed Beroset3ab3f412002-06-11 03:31:49 +00003009 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003010}
3011
3012/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003013 * Ensure that a macro parameter contains a condition code and
3014 * nothing else. Return the condition code index if so, or -1
3015 * otherwise.
3016 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003017static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003018{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003019 Token *tt;
3020 int i, j, k, m;
3021
H. Peter Anvin25a99342007-09-22 17:45:45 -07003022 if (!t)
3023 return -1; /* Probably a %+ without a space */
3024
H. Peter Anvineba20a72002-04-30 20:53:55 +00003025 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003026 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003027 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003028 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003029 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003030 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003031 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003032
3033 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00003034 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003035 while (j - i > 1) {
3036 k = (j + i) / 2;
3037 m = nasm_stricmp(t->text, conditions[k]);
3038 if (m == 0) {
3039 i = k;
3040 j = -2;
3041 break;
3042 } else if (m < 0) {
3043 j = k;
3044 } else
3045 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003046 }
3047 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003048 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003049 return i;
3050}
3051
3052/*
3053 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3054 * %-n) and MMacro-local identifiers (%%foo).
3055 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003056static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003057{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003058 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003059
3060 tail = &thead;
3061 thead = NULL;
3062
H. Peter Anvine2c80182005-01-15 22:15:51 +00003063 while (tline) {
3064 if (tline->type == TOK_PREPROC_ID &&
3065 (((tline->text[1] == '+' || tline->text[1] == '-')
3066 && tline->text[2]) || tline->text[1] == '%'
3067 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003068 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003069 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003070 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003071 unsigned int n;
3072 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003073 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003074
H. Peter Anvine2c80182005-01-15 22:15:51 +00003075 t = tline;
3076 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003077
H. Peter Anvine2c80182005-01-15 22:15:51 +00003078 mac = istk->mstk;
3079 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3080 mac = mac->next_active;
3081 if (!mac)
3082 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3083 else
3084 switch (t->text[1]) {
3085 /*
3086 * We have to make a substitution of one of the
3087 * forms %1, %-1, %+1, %%foo, %0.
3088 */
3089 case '0':
3090 type = TOK_NUMBER;
3091 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3092 text = nasm_strdup(tmpbuf);
3093 break;
3094 case '%':
3095 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003096 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00003097 mac->unique);
3098 text = nasm_strcat(tmpbuf, t->text + 2);
3099 break;
3100 case '-':
3101 n = atoi(t->text + 2) - 1;
3102 if (n >= mac->nparam)
3103 tt = NULL;
3104 else {
3105 if (mac->nparam > 1)
3106 n = (n + mac->rotate) % mac->nparam;
3107 tt = mac->params[n];
3108 }
3109 cc = find_cc(tt);
3110 if (cc == -1) {
3111 error(ERR_NONFATAL,
3112 "macro parameter %d is not a condition code",
3113 n + 1);
3114 text = NULL;
3115 } else {
3116 type = TOK_ID;
3117 if (inverse_ccs[cc] == -1) {
3118 error(ERR_NONFATAL,
3119 "condition code `%s' is not invertible",
3120 conditions[cc]);
3121 text = NULL;
3122 } else
3123 text =
3124 nasm_strdup(conditions[inverse_ccs[cc]]);
3125 }
3126 break;
3127 case '+':
3128 n = atoi(t->text + 2) - 1;
3129 if (n >= mac->nparam)
3130 tt = NULL;
3131 else {
3132 if (mac->nparam > 1)
3133 n = (n + mac->rotate) % mac->nparam;
3134 tt = mac->params[n];
3135 }
3136 cc = find_cc(tt);
3137 if (cc == -1) {
3138 error(ERR_NONFATAL,
3139 "macro parameter %d is not a condition code",
3140 n + 1);
3141 text = NULL;
3142 } else {
3143 type = TOK_ID;
3144 text = nasm_strdup(conditions[cc]);
3145 }
3146 break;
3147 default:
3148 n = atoi(t->text + 1) - 1;
3149 if (n >= mac->nparam)
3150 tt = NULL;
3151 else {
3152 if (mac->nparam > 1)
3153 n = (n + mac->rotate) % mac->nparam;
3154 tt = mac->params[n];
3155 }
3156 if (tt) {
3157 for (i = 0; i < mac->paramlen[n]; i++) {
3158 *tail = new_Token(NULL, tt->type, tt->text, 0);
3159 tail = &(*tail)->next;
3160 tt = tt->next;
3161 }
3162 }
3163 text = NULL; /* we've done it here */
3164 break;
3165 }
3166 if (!text) {
3167 delete_Token(t);
3168 } else {
3169 *tail = t;
3170 tail = &t->next;
3171 t->type = type;
3172 nasm_free(t->text);
3173 t->text = text;
3174 t->mac = NULL;
3175 }
3176 continue;
3177 } else {
3178 t = *tail = tline;
3179 tline = tline->next;
3180 t->mac = NULL;
3181 tail = &t->next;
3182 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003183 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003184 *tail = NULL;
3185 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003186 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003187 switch (t->type) {
3188 case TOK_WHITESPACE:
3189 if (tt->type == TOK_WHITESPACE) {
3190 t->next = delete_Token(tt);
3191 }
3192 break;
3193 case TOK_ID:
3194 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003195 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003196 nasm_free(t->text);
3197 t->text = tmp;
3198 t->next = delete_Token(tt);
3199 }
3200 break;
3201 case TOK_NUMBER:
3202 if (tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003203 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003204 nasm_free(t->text);
3205 t->text = tmp;
3206 t->next = delete_Token(tt);
3207 }
3208 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003209 default:
3210 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003211 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003212
H. Peter Anvin76690a12002-04-30 20:52:49 +00003213 return thead;
3214}
3215
3216/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003217 * Expand all single-line macro calls made in the given line.
3218 * Return the expanded version of the line. The original is deemed
3219 * to be destroyed in the process. (In reality we'll just move
3220 * Tokens from input to output a lot of the time, rather than
3221 * actually bothering to destroy and replicate.)
3222 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003223#define DEADMAN_LIMIT (1 << 20)
3224
H. Peter Anvine2c80182005-01-15 22:15:51 +00003225static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003226{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003227 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003228 struct hash_table *smtbl;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003229 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003230 Token **params;
3231 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003232 unsigned int nparam, sparam;
3233 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003234 Token *org_tline = tline;
3235 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003236 char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003237 int deadman = DEADMAN_LIMIT;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003238
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003239 /*
3240 * Trick: we should avoid changing the start token pointer since it can
3241 * be contained in "next" field of other token. Because of this
3242 * we allocate a copy of first token and work with it; at the end of
3243 * routine we copy it back
3244 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003245 if (org_tline) {
3246 tline =
3247 new_Token(org_tline->next, org_tline->type, org_tline->text,
3248 0);
3249 tline->mac = org_tline->mac;
3250 nasm_free(org_tline->text);
3251 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003252 }
3253
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003254again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003255 tail = &thead;
3256 thead = NULL;
3257
H. Peter Anvine2c80182005-01-15 22:15:51 +00003258 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003259 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003260 error(ERR_NONFATAL, "interminable macro recursion");
3261 break;
3262 }
3263
H. Peter Anvine2c80182005-01-15 22:15:51 +00003264 if ((mname = tline->text)) {
3265 /* if this token is a local macro, look in local context */
H. Peter Anvinb037a672008-05-29 19:08:08 -07003266 ctx = NULL;
3267 smtbl = &smacros;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003268 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003269 ctx = get_ctx(mname, true);
H. Peter Anvinb037a672008-05-29 19:08:08 -07003270 if (ctx)
3271 smtbl = &ctx->localmac;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003272 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07003273 head = (SMacro *) hash_findix(smtbl, mname);
H. Peter Anvin072771e2008-05-22 13:17:51 -07003274
H. Peter Anvine2c80182005-01-15 22:15:51 +00003275 /*
3276 * We've hit an identifier. As in is_mmacro below, we first
3277 * check whether the identifier is a single-line macro at
3278 * all, then think about checking for parameters if
3279 * necessary.
3280 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003281 for (m = head; m; m = m->next)
3282 if (!mstrcmp(m->name, mname, m->casesense))
3283 break;
3284 if (m) {
3285 mstart = tline;
3286 params = NULL;
3287 paramsize = NULL;
3288 if (m->nparam == 0) {
3289 /*
3290 * Simple case: the macro is parameterless. Discard the
3291 * one token that the macro call took, and push the
3292 * expansion back on the to-do stack.
3293 */
3294 if (!m->expansion) {
3295 if (!strcmp("__FILE__", m->name)) {
3296 int32_t num = 0;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003297 char *file;
3298 src_get(&num, &file);
3299 tline->text = nasm_quote(file, strlen(file));
H. Peter Anvin97a23472007-09-16 17:57:25 -07003300 tline->type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003301 nasm_free(file);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003302 continue;
3303 }
3304 if (!strcmp("__LINE__", m->name)) {
3305 nasm_free(tline->text);
3306 make_tok_num(tline, src_get_linnum());
3307 continue;
3308 }
3309 if (!strcmp("__BITS__", m->name)) {
3310 nasm_free(tline->text);
3311 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003312 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003313 }
3314 tline = delete_Token(tline);
3315 continue;
3316 }
3317 } else {
3318 /*
3319 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003320 * exists and takes parameters. We must find the
3321 * parameters in the call, count them, find the SMacro
3322 * that corresponds to that form of the macro call, and
3323 * substitute for the parameters when we expand. What a
3324 * pain.
3325 */
3326 /*tline = tline->next;
3327 skip_white_(tline); */
3328 do {
3329 t = tline->next;
3330 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003331 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003332 t->text = NULL;
3333 t = tline->next = delete_Token(t);
3334 }
3335 tline = t;
3336 } while (tok_type_(tline, TOK_WHITESPACE));
3337 if (!tok_is_(tline, "(")) {
3338 /*
3339 * This macro wasn't called with parameters: ignore
3340 * the call. (Behaviour borrowed from gnu cpp.)
3341 */
3342 tline = mstart;
3343 m = NULL;
3344 } else {
3345 int paren = 0;
3346 int white = 0;
3347 brackets = 0;
3348 nparam = 0;
3349 sparam = PARAM_DELTA;
3350 params = nasm_malloc(sparam * sizeof(Token *));
3351 params[0] = tline->next;
3352 paramsize = nasm_malloc(sparam * sizeof(int));
3353 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003354 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003355 /*
3356 * For some unusual expansions
3357 * which concatenates function call
3358 */
3359 t = tline->next;
3360 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003361 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003362 t->text = NULL;
3363 t = tline->next = delete_Token(t);
3364 }
3365 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003366
H. Peter Anvine2c80182005-01-15 22:15:51 +00003367 if (!tline) {
3368 error(ERR_NONFATAL,
3369 "macro call expects terminating `)'");
3370 break;
3371 }
3372 if (tline->type == TOK_WHITESPACE
3373 && brackets <= 0) {
3374 if (paramsize[nparam])
3375 white++;
3376 else
3377 params[nparam] = tline->next;
3378 continue; /* parameter loop */
3379 }
3380 if (tline->type == TOK_OTHER
3381 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003382 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003383 if (ch == ',' && !paren && brackets <= 0) {
3384 if (++nparam >= sparam) {
3385 sparam += PARAM_DELTA;
3386 params = nasm_realloc(params,
3387 sparam *
3388 sizeof(Token
3389 *));
3390 paramsize =
3391 nasm_realloc(paramsize,
3392 sparam *
3393 sizeof(int));
3394 }
3395 params[nparam] = tline->next;
3396 paramsize[nparam] = 0;
3397 white = 0;
3398 continue; /* parameter loop */
3399 }
3400 if (ch == '{' &&
3401 (brackets > 0 || (brackets == 0 &&
3402 !paramsize[nparam])))
3403 {
3404 if (!(brackets++)) {
3405 params[nparam] = tline->next;
3406 continue; /* parameter loop */
3407 }
3408 }
3409 if (ch == '}' && brackets > 0)
3410 if (--brackets == 0) {
3411 brackets = -1;
3412 continue; /* parameter loop */
3413 }
3414 if (ch == '(' && !brackets)
3415 paren++;
3416 if (ch == ')' && brackets <= 0)
3417 if (--paren < 0)
3418 break;
3419 }
3420 if (brackets < 0) {
3421 brackets = 0;
3422 error(ERR_NONFATAL, "braces do not "
3423 "enclose all of macro parameter");
3424 }
3425 paramsize[nparam] += white + 1;
3426 white = 0;
3427 } /* parameter loop */
3428 nparam++;
3429 while (m && (m->nparam != nparam ||
3430 mstrcmp(m->name, mname,
3431 m->casesense)))
3432 m = m->next;
3433 if (!m)
3434 error(ERR_WARNING | ERR_WARN_MNP,
3435 "macro `%s' exists, "
3436 "but not taking %d parameters",
3437 mstart->text, nparam);
3438 }
3439 }
3440 if (m && m->in_progress)
3441 m = NULL;
3442 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003443 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003444 * Design question: should we handle !tline, which
3445 * indicates missing ')' here, or expand those
3446 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003447 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003448 */
3449 nasm_free(params);
3450 nasm_free(paramsize);
3451 tline = mstart;
3452 } else {
3453 /*
3454 * Expand the macro: we are placed on the last token of the
3455 * call, so that we can easily split the call from the
3456 * following tokens. We also start by pushing an SMAC_END
3457 * token for the cycle removal.
3458 */
3459 t = tline;
3460 if (t) {
3461 tline = t->next;
3462 t->next = NULL;
3463 }
3464 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3465 tt->mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003466 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003467 tline = tt;
3468 for (t = m->expansion; t; t = t->next) {
3469 if (t->type >= TOK_SMAC_PARAM) {
3470 Token *pcopy = tline, **ptail = &pcopy;
3471 Token *ttt, *pt;
3472 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003473
H. Peter Anvine2c80182005-01-15 22:15:51 +00003474 ttt = params[t->type - TOK_SMAC_PARAM];
3475 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3476 --i >= 0;) {
3477 pt = *ptail =
3478 new_Token(tline, ttt->type, ttt->text,
3479 0);
3480 ptail = &pt->next;
3481 ttt = ttt->next;
3482 }
3483 tline = pcopy;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -07003484 } else if (t->type == TOK_PREPROC_Q) {
3485 tt = new_Token(tline, TOK_ID, mname, 0);
3486 tline = tt;
3487 } else if (t->type == TOK_PREPROC_QQ) {
3488 tt = new_Token(tline, TOK_ID, m->name, 0);
3489 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003490 } else {
3491 tt = new_Token(tline, t->type, t->text, 0);
3492 tline = tt;
3493 }
3494 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003495
H. Peter Anvine2c80182005-01-15 22:15:51 +00003496 /*
3497 * Having done that, get rid of the macro call, and clean
3498 * up the parameters.
3499 */
3500 nasm_free(params);
3501 nasm_free(paramsize);
3502 free_tlist(mstart);
3503 continue; /* main token loop */
3504 }
3505 }
3506 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003507
H. Peter Anvine2c80182005-01-15 22:15:51 +00003508 if (tline->type == TOK_SMAC_END) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003509 tline->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003510 tline = delete_Token(tline);
3511 } else {
3512 t = *tail = tline;
3513 tline = tline->next;
3514 t->mac = NULL;
3515 t->next = NULL;
3516 tail = &t->next;
3517 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003518 }
3519
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003520 /*
3521 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003522 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003523 * TOK_IDs should be concatenated.
3524 * Also we look for %+ tokens and concatenate the tokens before and after
3525 * them (without white spaces in between).
3526 */
3527 t = thead;
3528 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003529 while (t) {
3530 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3531 t = t->next;
3532 if (!t || !t->next)
3533 break;
3534 if (t->next->type == TOK_ID ||
3535 t->next->type == TOK_PREPROC_ID ||
3536 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003537 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003538 nasm_free(t->text);
3539 t->next = delete_Token(t->next);
3540 t->text = p;
3541 rescan = 1;
3542 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3543 t->next->next->type == TOK_PREPROC_ID &&
3544 strcmp(t->next->next->text, "%+") == 0) {
3545 /* free the next whitespace, the %+ token and next whitespace */
3546 int i;
3547 for (i = 1; i <= 3; i++) {
3548 if (!t->next
3549 || (i != 2 && t->next->type != TOK_WHITESPACE))
3550 break;
3551 t->next = delete_Token(t->next);
3552 } /* endfor */
3553 } else
3554 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003555 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003556 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003557 if (rescan) {
3558 tline = thead;
3559 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003560 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003561
H. Peter Anvine2c80182005-01-15 22:15:51 +00003562 if (org_tline) {
3563 if (thead) {
3564 *org_tline = *thead;
3565 /* since we just gave text to org_line, don't free it */
3566 thead->text = NULL;
3567 delete_Token(thead);
3568 } else {
3569 /* the expression expanded to empty line;
3570 we can't return NULL for some reasons
3571 we just set the line to a single WHITESPACE token. */
3572 memset(org_tline, 0, sizeof(*org_tline));
3573 org_tline->text = NULL;
3574 org_tline->type = TOK_WHITESPACE;
3575 }
3576 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003577 }
3578
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003579 return thead;
3580}
3581
3582/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003583 * Similar to expand_smacro but used exclusively with macro identifiers
3584 * right before they are fetched in. The reason is that there can be
3585 * identifiers consisting of several subparts. We consider that if there
3586 * are more than one element forming the name, user wants a expansion,
3587 * otherwise it will be left as-is. Example:
3588 *
3589 * %define %$abc cde
3590 *
3591 * the identifier %$abc will be left as-is so that the handler for %define
3592 * will suck it and define the corresponding value. Other case:
3593 *
3594 * %define _%$abc cde
3595 *
3596 * In this case user wants name to be expanded *before* %define starts
3597 * working, so we'll expand %$abc into something (if it has a value;
3598 * otherwise it will be left as-is) then concatenate all successive
3599 * PP_IDs into one.
3600 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003601static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003602{
3603 Token *cur, *oldnext = NULL;
3604
H. Peter Anvin734b1882002-04-30 21:01:08 +00003605 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003606 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003607
3608 cur = tline;
3609 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003610 (cur->next->type == TOK_ID ||
3611 cur->next->type == TOK_PREPROC_ID
3612 || cur->next->type == TOK_NUMBER))
3613 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003614
3615 /* If identifier consists of just one token, don't expand */
3616 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003617 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003618
H. Peter Anvine2c80182005-01-15 22:15:51 +00003619 if (cur) {
3620 oldnext = cur->next; /* Detach the tail past identifier */
3621 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003622 }
3623
H. Peter Anvin734b1882002-04-30 21:01:08 +00003624 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003625
H. Peter Anvine2c80182005-01-15 22:15:51 +00003626 if (cur) {
3627 /* expand_smacro possibly changhed tline; re-scan for EOL */
3628 cur = tline;
3629 while (cur && cur->next)
3630 cur = cur->next;
3631 if (cur)
3632 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003633 }
3634
3635 return tline;
3636}
3637
3638/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003639 * Determine whether the given line constitutes a multi-line macro
3640 * call, and return the MMacro structure called if so. Doesn't have
3641 * to check for an initial label - that's taken care of in
3642 * expand_mmacro - but must check numbers of parameters. Guaranteed
3643 * to be called with tline->type == TOK_ID, so the putative macro
3644 * name is easy to find.
3645 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003646static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003647{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003648 MMacro *head, *m;
3649 Token **params;
3650 int nparam;
3651
H. Peter Anvin166c2472008-05-28 12:28:58 -07003652 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003653
3654 /*
3655 * Efficiency: first we see if any macro exists with the given
3656 * name. If not, we can return NULL immediately. _Then_ we
3657 * count the parameters, and then we look further along the
3658 * list if necessary to find the proper MMacro.
3659 */
3660 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003661 if (!mstrcmp(m->name, tline->text, m->casesense))
3662 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003663 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003664 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003665
3666 /*
3667 * OK, we have a potential macro. Count and demarcate the
3668 * parameters.
3669 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003670 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003671
3672 /*
3673 * So we know how many parameters we've got. Find the MMacro
3674 * structure that handles this number.
3675 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003676 while (m) {
3677 if (m->nparam_min <= nparam
3678 && (m->plus || nparam <= m->nparam_max)) {
3679 /*
3680 * This one is right. Just check if cycle removal
3681 * prohibits us using it before we actually celebrate...
3682 */
3683 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003684#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003685 error(ERR_NONFATAL,
3686 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003687#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003688 nasm_free(params);
3689 return NULL;
3690 }
3691 /*
3692 * It's right, and we can use it. Add its default
3693 * parameters to the end of our list if necessary.
3694 */
3695 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3696 params =
3697 nasm_realloc(params,
3698 ((m->nparam_min + m->ndefs +
3699 1) * sizeof(*params)));
3700 while (nparam < m->nparam_min + m->ndefs) {
3701 params[nparam] = m->defaults[nparam - m->nparam_min];
3702 nparam++;
3703 }
3704 }
3705 /*
3706 * If we've gone over the maximum parameter count (and
3707 * we're in Plus mode), ignore parameters beyond
3708 * nparam_max.
3709 */
3710 if (m->plus && nparam > m->nparam_max)
3711 nparam = m->nparam_max;
3712 /*
3713 * Then terminate the parameter list, and leave.
3714 */
3715 if (!params) { /* need this special case */
3716 params = nasm_malloc(sizeof(*params));
3717 nparam = 0;
3718 }
3719 params[nparam] = NULL;
3720 *params_array = params;
3721 return m;
3722 }
3723 /*
3724 * This one wasn't right: look for the next one with the
3725 * same name.
3726 */
3727 for (m = m->next; m; m = m->next)
3728 if (!mstrcmp(m->name, tline->text, m->casesense))
3729 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003730 }
3731
3732 /*
3733 * After all that, we didn't find one with the right number of
3734 * parameters. Issue a warning, and fail to expand the macro.
3735 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003736 error(ERR_WARNING | ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003737 "macro `%s' exists, but not taking %d parameters",
3738 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003739 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003740 return NULL;
3741}
3742
3743/*
3744 * Expand the multi-line macro call made by the given line, if
3745 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003746 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003747 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003748static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003749{
3750 Token *startline = tline;
3751 Token *label = NULL;
3752 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003753 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003754 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003755 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003756 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07003757 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003758
3759 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003760 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07003761 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003762 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003763 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003764 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003765 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07003766 if (m) {
3767 mname = t->text;
3768 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003769 Token *last;
3770 /*
3771 * We have an id which isn't a macro call. We'll assume
3772 * it might be a label; we'll also check to see if a
3773 * colon follows it. Then, if there's another id after
3774 * that lot, we'll check it again for macro-hood.
3775 */
3776 label = last = t;
3777 t = t->next;
3778 if (tok_type_(t, TOK_WHITESPACE))
3779 last = t, t = t->next;
3780 if (tok_is_(t, ":")) {
3781 dont_prepend = 1;
3782 last = t, t = t->next;
3783 if (tok_type_(t, TOK_WHITESPACE))
3784 last = t, t = t->next;
3785 }
3786 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3787 return 0;
3788 last->next = NULL;
H. Peter Anvinc751e862008-06-09 10:18:45 -07003789 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003790 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003791 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003792
3793 /*
3794 * Fix up the parameters: this involves stripping leading and
3795 * trailing whitespace, then stripping braces if they are
3796 * present.
3797 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003798 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003799 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003800
H. Peter Anvine2c80182005-01-15 22:15:51 +00003801 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003802 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003803 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003804
H. Peter Anvine2c80182005-01-15 22:15:51 +00003805 t = params[i];
3806 skip_white_(t);
3807 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003808 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003809 params[i] = t;
3810 paramlen[i] = 0;
3811 while (t) {
3812 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3813 break; /* ... because we have hit a comma */
3814 if (comma && t->type == TOK_WHITESPACE
3815 && tok_is_(t->next, ","))
3816 break; /* ... or a space then a comma */
3817 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3818 break; /* ... or a brace */
3819 t = t->next;
3820 paramlen[i]++;
3821 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003822 }
3823
3824 /*
3825 * OK, we have a MMacro structure together with a set of
3826 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003827 * copies of each Line on to istk->expansion. Substitution of
3828 * parameter tokens and macro-local tokens doesn't get done
3829 * until the single-line macro substitution process; this is
3830 * because delaying them allows us to change the semantics
3831 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003832 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003833 * First, push an end marker on to istk->expansion, mark this
3834 * macro as in progress, and set up its invocation-specific
3835 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003836 */
3837 ll = nasm_malloc(sizeof(Line));
3838 ll->next = istk->expansion;
3839 ll->finishes = m;
3840 ll->first = NULL;
3841 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003842
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003843 m->in_progress = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003844 m->params = params;
3845 m->iline = tline;
3846 m->nparam = nparam;
3847 m->rotate = 0;
3848 m->paramlen = paramlen;
3849 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003850 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003851
3852 m->next_active = istk->mstk;
3853 istk->mstk = m;
3854
H. Peter Anvine2c80182005-01-15 22:15:51 +00003855 for (l = m->expansion; l; l = l->next) {
3856 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003857
H. Peter Anvine2c80182005-01-15 22:15:51 +00003858 ll = nasm_malloc(sizeof(Line));
3859 ll->finishes = NULL;
3860 ll->next = istk->expansion;
3861 istk->expansion = ll;
3862 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003863
H. Peter Anvine2c80182005-01-15 22:15:51 +00003864 for (t = l->first; t; t = t->next) {
3865 Token *x = t;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003866 switch (t->type) {
3867 case TOK_PREPROC_Q:
H. Peter Anvinc751e862008-06-09 10:18:45 -07003868 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
H. Peter Anvince2233b2008-05-25 21:57:00 -07003869 break;
3870 case TOK_PREPROC_QQ:
3871 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
3872 break;
3873 case TOK_PREPROC_ID:
3874 if (t->text[1] == '0' && t->text[2] == '0') {
3875 dont_prepend = -1;
3876 x = label;
3877 if (!x)
3878 continue;
3879 }
3880 /* fall through */
3881 default:
3882 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3883 break;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003884 }
H. Peter Anvince2233b2008-05-25 21:57:00 -07003885 tail = &tt->next;
3886 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003887 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003888 }
3889
3890 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00003891 * If we had a label, push it on as the first line of
3892 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003893 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003894 if (label) {
3895 if (dont_prepend < 0)
3896 free_tlist(startline);
3897 else {
3898 ll = nasm_malloc(sizeof(Line));
3899 ll->finishes = NULL;
3900 ll->next = istk->expansion;
3901 istk->expansion = ll;
3902 ll->first = startline;
3903 if (!dont_prepend) {
3904 while (label->next)
3905 label = label->next;
3906 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3907 }
3908 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003909 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003910
H. Peter Anvin734b1882002-04-30 21:01:08 +00003911 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003912
H. Peter Anvineba20a72002-04-30 20:53:55 +00003913 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003914}
3915
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003916/*
3917 * Since preprocessor always operate only on the line that didn't
3918 * arrived yet, we should always use ERR_OFFBY1. Also since user
3919 * won't want to see same error twice (preprocessing is done once
3920 * per pass) we will want to show errors only during pass one.
3921 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003922static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003923{
3924 va_list arg;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003925 char buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003926
3927 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003928 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003929 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003930
H. Peter Anvin734b1882002-04-30 21:01:08 +00003931 va_start(arg, fmt);
Ed Beroset19f927a2004-12-15 17:07:03 +00003932 vsnprintf(buff, sizeof(buff), fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003933 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003934
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003935 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003936 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3937 istk->mstk->lineno, buff);
3938 else
3939 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003940}
3941
H. Peter Anvin734b1882002-04-30 21:01:08 +00003942static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003943pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07003944 ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003945{
H. Peter Anvin99941bf2002-05-14 17:44:03 +00003946 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003947 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003948 istk = nasm_malloc(sizeof(Include));
3949 istk->next = NULL;
3950 istk->conds = NULL;
3951 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003952 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003953 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00003954 istk->fname = NULL;
3955 src_set_fname(nasm_strdup(file));
3956 src_set_linnum(0);
3957 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003958 if (!istk->fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003959 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3960 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003961 defining = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003962 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003963 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003964 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07003965 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003966 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07003967 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003968 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003969 any_extrastdmac = (extrastdmac != NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003970 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003971 evaluate = eval;
3972 pass = apass;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07003973 dephead = deptail = deplist;
3974 if (deplist) {
3975 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
3976 sl->next = NULL;
3977 strcpy(sl->str, file);
3978 *deptail = sl;
3979 deptail = &sl->next;
3980 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003981}
3982
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003983static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003984{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003985 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003986 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003987
H. Peter Anvine2c80182005-01-15 22:15:51 +00003988 while (1) {
3989 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003990 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00003991 * buffer or from the input file.
3992 */
3993 tline = NULL;
3994 while (istk->expansion && istk->expansion->finishes) {
3995 Line *l = istk->expansion;
3996 if (!l->finishes->name && l->finishes->in_progress > 1) {
3997 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003998
H. Peter Anvine2c80182005-01-15 22:15:51 +00003999 /*
4000 * This is a macro-end marker for a macro with no
4001 * name, which means it's not really a macro at all
4002 * but a %rep block, and the `in_progress' field is
4003 * more than 1, meaning that we still need to
4004 * repeat. (1 means the natural last repetition; 0
4005 * means termination by %exitrep.) We have
4006 * therefore expanded up to the %endrep, and must
4007 * push the whole block on to the expansion buffer
4008 * again. We don't bother to remove the macro-end
4009 * marker: we'd only have to generate another one
4010 * if we did.
4011 */
4012 l->finishes->in_progress--;
4013 for (l = l->finishes->expansion; l; l = l->next) {
4014 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004015
H. Peter Anvine2c80182005-01-15 22:15:51 +00004016 ll = nasm_malloc(sizeof(Line));
4017 ll->next = istk->expansion;
4018 ll->finishes = NULL;
4019 ll->first = NULL;
4020 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004021
H. Peter Anvine2c80182005-01-15 22:15:51 +00004022 for (t = l->first; t; t = t->next) {
4023 if (t->text || t->type == TOK_WHITESPACE) {
4024 tt = *tail =
4025 new_Token(NULL, t->type, t->text, 0);
4026 tail = &tt->next;
4027 }
4028 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004029
H. Peter Anvine2c80182005-01-15 22:15:51 +00004030 istk->expansion = ll;
4031 }
4032 } else {
4033 /*
4034 * Check whether a `%rep' was started and not ended
4035 * within this macro expansion. This can happen and
4036 * should be detected. It's a fatal error because
4037 * I'm too confused to work out how to recover
4038 * sensibly from it.
4039 */
4040 if (defining) {
4041 if (defining->name)
4042 error(ERR_PANIC,
4043 "defining with name in expansion");
4044 else if (istk->mstk->name)
4045 error(ERR_FATAL,
4046 "`%%rep' without `%%endrep' within"
4047 " expansion of macro `%s'",
4048 istk->mstk->name);
4049 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004050
H. Peter Anvine2c80182005-01-15 22:15:51 +00004051 /*
4052 * FIXME: investigate the relationship at this point between
4053 * istk->mstk and l->finishes
4054 */
4055 {
4056 MMacro *m = istk->mstk;
4057 istk->mstk = m->next_active;
4058 if (m->name) {
4059 /*
4060 * This was a real macro call, not a %rep, and
4061 * therefore the parameter information needs to
4062 * be freed.
4063 */
4064 nasm_free(m->params);
4065 free_tlist(m->iline);
4066 nasm_free(m->paramlen);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004067 l->finishes->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004068 } else
4069 free_mmacro(m);
4070 }
4071 istk->expansion = l->next;
4072 nasm_free(l);
4073 list->downlevel(LIST_MACRO);
4074 }
4075 }
4076 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004077
H. Peter Anvine2c80182005-01-15 22:15:51 +00004078 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004079 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004080 Line *l = istk->expansion;
4081 if (istk->mstk)
4082 istk->mstk->lineno++;
4083 tline = l->first;
4084 istk->expansion = l->next;
4085 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004086 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004087 list->line(LIST_MACRO, p);
4088 nasm_free(p);
4089 break;
4090 }
4091 line = read_line();
4092 if (line) { /* from the current input file */
4093 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004094 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004095 nasm_free(line);
4096 break;
4097 }
4098 /*
4099 * The current file has ended; work down the istk
4100 */
4101 {
4102 Include *i = istk;
4103 fclose(i->fp);
4104 if (i->conds)
4105 error(ERR_FATAL,
4106 "expected `%%endif' before end of file");
4107 /* only set line and file name if there's a next node */
4108 if (i->next) {
4109 src_set_linnum(i->lineno);
4110 nasm_free(src_set_fname(i->fname));
4111 }
4112 istk = i->next;
4113 list->downlevel(LIST_INCLUDE);
4114 nasm_free(i);
4115 if (!istk)
4116 return NULL;
4117 }
4118 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004119
H. Peter Anvine2c80182005-01-15 22:15:51 +00004120 /*
4121 * We must expand MMacro parameters and MMacro-local labels
4122 * _before_ we plunge into directive processing, to cope
4123 * with things like `%define something %1' such as STRUC
4124 * uses. Unless we're _defining_ a MMacro, in which case
4125 * those tokens should be left alone to go into the
4126 * definition; and unless we're in a non-emitting
4127 * condition, in which case we don't want to meddle with
4128 * anything.
4129 */
4130 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
4131 tline = expand_mmac_params(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004132
H. Peter Anvine2c80182005-01-15 22:15:51 +00004133 /*
4134 * Check the line to see if it's a preprocessor directive.
4135 */
4136 if (do_directive(tline) == DIRECTIVE_FOUND) {
4137 continue;
4138 } else if (defining) {
4139 /*
4140 * We're defining a multi-line macro. We emit nothing
4141 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004142 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004143 */
4144 Line *l = nasm_malloc(sizeof(Line));
4145 l->next = defining->expansion;
4146 l->first = tline;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004147 l->finishes = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004148 defining->expansion = l;
4149 continue;
4150 } else if (istk->conds && !emitting(istk->conds->state)) {
4151 /*
4152 * We're in a non-emitting branch of a condition block.
4153 * Emit nothing at all, not even a blank line: when we
4154 * emerge from the condition we'll give a line-number
4155 * directive so we keep our place correctly.
4156 */
4157 free_tlist(tline);
4158 continue;
4159 } else if (istk->mstk && !istk->mstk->in_progress) {
4160 /*
4161 * We're in a %rep block which has been terminated, so
4162 * we're walking through to the %endrep without
4163 * emitting anything. Emit nothing at all, not even a
4164 * blank line: when we emerge from the %rep block we'll
4165 * give a line-number directive so we keep our place
4166 * correctly.
4167 */
4168 free_tlist(tline);
4169 continue;
4170 } else {
4171 tline = expand_smacro(tline);
4172 if (!expand_mmacro(tline)) {
4173 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004174 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004175 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004176 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004177 free_tlist(tline);
4178 break;
4179 } else {
4180 continue; /* expand_mmacro calls free_tlist */
4181 }
4182 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004183 }
4184
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004185 return line;
4186}
4187
H. Peter Anvine2c80182005-01-15 22:15:51 +00004188static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004189{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004190 if (defining) {
4191 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
4192 defining->name);
4193 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004194 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004195 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004196 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004197 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004198 while (istk) {
4199 Include *i = istk;
4200 istk = istk->next;
4201 fclose(i->fp);
4202 nasm_free(i->fname);
4203 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004204 }
4205 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004206 ctx_pop();
4207 if (pass == 0) {
4208 free_llist(predef);
4209 delete_Blocks();
4210 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004211}
4212
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004213void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004214{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004215 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004216
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004217 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004218 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004219 i->next = NULL;
4220
H. Peter Anvine2c80182005-01-15 22:15:51 +00004221 if (ipath != NULL) {
4222 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004223 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004224 j = j->next;
4225 j->next = i;
4226 } else {
4227 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004228 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004229}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004230
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004231void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004232{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004233 Token *inc, *space, *name;
4234 Line *l;
4235
H. Peter Anvin734b1882002-04-30 21:01:08 +00004236 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4237 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4238 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004239
4240 l = nasm_malloc(sizeof(Line));
4241 l->next = predef;
4242 l->first = inc;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004243 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004244 predef = l;
4245}
4246
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004247void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004248{
4249 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004250 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004251 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004252
4253 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004254 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4255 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004256 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004257 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004258 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004259 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004260 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004261
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004262 l = nasm_malloc(sizeof(Line));
4263 l->next = predef;
4264 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004265 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004266 predef = l;
4267}
4268
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004269void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004270{
4271 Token *def, *space;
4272 Line *l;
4273
H. Peter Anvin734b1882002-04-30 21:01:08 +00004274 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4275 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004276 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004277
4278 l = nasm_malloc(sizeof(Line));
4279 l->next = predef;
4280 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004281 l->finishes = false;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004282 predef = l;
4283}
4284
Keith Kaniosb7a89542007-04-12 02:40:54 +00004285/*
4286 * Added by Keith Kanios:
4287 *
4288 * This function is used to assist with "runtime" preprocessor
4289 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4290 *
4291 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4292 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4293 */
4294
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004295void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004296{
4297 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004298
Keith Kaniosb7a89542007-04-12 02:40:54 +00004299 def = tokenize(definition);
4300 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4301 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004302
Keith Kaniosb7a89542007-04-12 02:40:54 +00004303}
4304
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004305void pp_extra_stdmac(const char **macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004306{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004307 extrastdmac = macros;
4308}
4309
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004310static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004311{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004312 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004313 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004314 tok->text = nasm_strdup(numbuf);
4315 tok->type = TOK_NUMBER;
4316}
4317
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004318Preproc nasmpp = {
4319 pp_reset,
4320 pp_getline,
4321 pp_cleanup
4322};