blob: 40526729318b00c3985aefc9b3ca3e36fb467a25 [file] [log] [blame]
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001/* preproc.c macro preprocessor for the Netwide Assembler
2 *
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
Beroset095e6a22007-12-29 09:44:23 -05005 * redistributable under the license given in the file "LICENSE"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 * distributed in the NASM archive.
7 *
8 * initial version 18/iii/97 by Simon Tatham
9 */
10
H. Peter Anvin4836e332002-04-30 20:56:43 +000011/* Typical flow of text through preproc
12 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000013 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000014 *
15 * from a macro expansion
16 *
17 * or
18 * {
19 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000020 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000021 * }
22 *
23 * expand_mmac_params is used to expand %1 etc., unless a macro is being
24 * defined or a false conditional is being processed
25 * (%0, %1, %+1, %-1, %%foo
26 *
27 * do_directive checks for directives
28 *
29 * expand_smacro is used to expand single line macros
30 *
31 * expand_mmacro is used to expand multi-line macros
32 *
33 * detoken is used to convert the line back to text
34 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000035
H. Peter Anvinfe501952007-10-02 21:53:51 -070036#include "compiler.h"
37
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000038#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000039#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000040#include <stdlib.h>
41#include <stddef.h>
42#include <string.h>
43#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000044#include <limits.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000045#include <inttypes.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000046
47#include "nasm.h"
48#include "nasmlib.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000049#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070050#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070051#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070052#include "stdscan.h"
53#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070054#include "tables.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000055
56typedef struct SMacro SMacro;
57typedef struct MMacro MMacro;
58typedef struct Context Context;
59typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000060typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000061typedef struct Line Line;
62typedef struct Include Include;
63typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000064typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000065
66/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070067 * Note on the storage of both SMacro and MMacros: the hash table
68 * indexes them case-insensitively, and we then have to go through a
69 * linked list of potential case aliases (and, for MMacros, parameter
70 * ranges); this is to preserve the matching semantics of the earlier
71 * code. If the number of case aliases for a specific macro is a
72 * performance issue, you may want to reconsider your coding style.
73 */
74
75/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000076 * Store the definition of a single-line macro.
77 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000078struct SMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000079 SMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +000080 char *name;
H. Peter Anvin70055962007-10-11 00:05:31 -070081 bool casesense;
H. Peter Anvin16ed4382007-10-11 10:06:19 -070082 bool in_progress;
H. Peter Anvin70055962007-10-11 00:05:31 -070083 unsigned int nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000084 Token *expansion;
85};
86
87/*
H. Peter Anvin76690a12002-04-30 20:52:49 +000088 * Store the definition of a multi-line macro. This is also used to
89 * store the interiors of `%rep...%endrep' blocks, which are
90 * effectively self-re-invoking multi-line macros which simply
91 * don't have a name or bother to appear in the hash tables. %rep
92 * blocks are signified by having a NULL `name' field.
93 *
94 * In a MMacro describing a `%rep' block, the `in_progress' field
95 * isn't merely boolean, but gives the number of repeats left to
96 * run.
97 *
98 * The `next' field is used for storing MMacros in hash tables; the
99 * `next_active' field is for stacking them on istk entries.
100 *
101 * When a MMacro is being expanded, `params', `iline', `nparam',
102 * `paramlen', `rotate' and `unique' are local to the invocation.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000103 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000104struct MMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 MMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000106 char *name;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -0700107 int nparam_min, nparam_max;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700108 bool casesense;
109 bool plus; /* is the last parameter greedy? */
110 bool nolist; /* is this macro listing-inhibited? */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700111 int64_t in_progress;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000112 Token *dlist; /* All defaults as one list */
113 Token **defaults; /* Parameter default pointers */
114 int ndefs; /* number of default parameters */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000115 Line *expansion;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000116
117 MMacro *next_active;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000118 MMacro *rep_nest; /* used for nesting %rep */
119 Token **params; /* actual parameters */
120 Token *iline; /* invocation line */
H. Peter Anvin25a99342007-09-22 17:45:45 -0700121 unsigned int nparam, rotate;
122 int *paramlen;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700123 uint64_t unique;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000124 int lineno; /* Current line number on expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000125};
126
127/*
128 * The context stack is composed of a linked list of these.
129 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000130struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000131 Context *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000132 char *name;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700133 struct hash_table localmac;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000134 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000135};
136
137/*
138 * This is the internal form which we break input lines up into.
139 * Typically stored in linked lists.
140 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000141 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
142 * necessarily used as-is, but is intended to denote the number of
143 * the substituted parameter. So in the definition
144 *
145 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700146 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000147 * the token representing `x' will have its type changed to
148 * TOK_SMAC_PARAM, but the one representing `y' will be
149 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000150 *
151 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
152 * which doesn't need quotes around it. Used in the pre-include
153 * mechanism as an alternative to trying to find a sensible type of
154 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000155 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000156enum pp_token_type {
157 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
158 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700159 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
160 TOK_INTERNAL_STRING,
161 TOK_PREPROC_Q, TOK_PREPROC_QQ,
H. Peter Anvin5b76fa22008-05-26 11:14:38 -0700162 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
163 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000164};
165
H. Peter Anvine2c80182005-01-15 22:15:51 +0000166struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000167 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000168 char *text;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000169 SMacro *mac; /* associated macro for TOK_SMAC_END */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000170 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000171};
172
173/*
174 * Multi-line macro definitions are stored as a linked list of
175 * these, which is essentially a container to allow several linked
176 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700177 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000178 * Note that in this module, linked lists are treated as stacks
179 * wherever possible. For this reason, Lines are _pushed_ on to the
180 * `expansion' field in MMacro structures, so that the linked list,
181 * if walked, would give the macro lines in reverse order; this
182 * means that we can walk the list when expanding a macro, and thus
183 * push the lines on to the `expansion' field in _istk_ in reverse
184 * order (so that when popped back off they are in the right
185 * order). It may seem cockeyed, and it relies on my design having
186 * an even number of steps in, but it works...
187 *
188 * Some of these structures, rather than being actual lines, are
189 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000190 * This is for use in the cycle-tracking and %rep-handling code.
191 * Such structures have `finishes' non-NULL, and `first' NULL. All
192 * others have `finishes' NULL, but `first' may still be NULL if
193 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000194 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000195struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000196 Line *next;
197 MMacro *finishes;
198 Token *first;
199};
200
201/*
202 * To handle an arbitrary level of file inclusion, we maintain a
203 * stack (ie linked list) of these things.
204 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000205struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000206 Include *next;
207 FILE *fp;
208 Cond *conds;
209 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000210 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000211 int lineno, lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000212 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000213};
214
215/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000216 * Include search path. This is simply a list of strings which get
217 * prepended, in turn, to the name of an include file, in an
218 * attempt to find the file if it's not in the current directory.
219 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000220struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000221 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000222 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000223};
224
225/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000226 * Conditional assembly: we maintain a separate stack of these for
227 * each level of file inclusion. (The only reason we keep the
228 * stacks separate is to ensure that a stray `%endif' in a file
229 * included from within the true branch of a `%if' won't terminate
230 * it and cause confusion: instead, rightly, it'll cause an error.)
231 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000232struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000233 Cond *next;
234 int state;
235};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000236enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000237 /*
238 * These states are for use just after %if or %elif: IF_TRUE
239 * means the condition has evaluated to truth so we are
240 * currently emitting, whereas IF_FALSE means we are not
241 * currently emitting but will start doing so if a %else comes
242 * up. In these states, all directives are admissible: %elif,
243 * %else and %endif. (And of course %if.)
244 */
245 COND_IF_TRUE, COND_IF_FALSE,
246 /*
247 * These states come up after a %else: ELSE_TRUE means we're
248 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
249 * any %elif or %else will cause an error.
250 */
251 COND_ELSE_TRUE, COND_ELSE_FALSE,
252 /*
253 * This state means that we're not emitting now, and also that
254 * nothing until %endif will be emitted at all. It's for use in
255 * two circumstances: (i) when we've had our moment of emission
256 * and have now started seeing %elifs, and (ii) when the
257 * condition construct in question is contained within a
258 * non-emitting branch of a larger condition construct.
259 */
260 COND_NEVER
261};
262#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
263
H. Peter Anvin70653092007-10-19 14:42:29 -0700264/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000265 * These defines are used as the possible return values for do_directive
266 */
267#define NO_DIRECTIVE_FOUND 0
268#define DIRECTIVE_FOUND 1
269
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000270/*
271 * Condition codes. Note that we use c_ prefix not C_ because C_ is
272 * used in nasm.h for the "real" condition codes. At _this_ level,
273 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
274 * ones, so we need a different enum...
275 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700276static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000277 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
278 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000279 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000280};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700281enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000282 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
283 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
H. Peter Anvin476d2862007-10-02 22:04:15 -0700284 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
285 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000286};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700287static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000288 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
289 c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
H. Peter Anvince9be342007-09-12 00:22:29 +0000290 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000291};
292
H. Peter Anvin76690a12002-04-30 20:52:49 +0000293/*
294 * Directive names.
295 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000296/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000297static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000298{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000299 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000300}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000301
302/* For TASM compatibility we need to be able to recognise TASM compatible
303 * conditional compilation directives. Using the NASM pre-processor does
304 * not work, so we look for them specifically from the following list and
305 * then jam in the equivalent NASM directive into the input stream.
306 */
307
H. Peter Anvine2c80182005-01-15 22:15:51 +0000308enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000309 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
310 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
311};
312
H. Peter Anvin476d2862007-10-02 22:04:15 -0700313static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000314 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
315 "ifndef", "include", "local"
316};
317
318static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000319static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000320static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800321static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000322
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000323static Context *cstk;
324static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000325static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000326
H. Peter Anvine2c80182005-01-15 22:15:51 +0000327static efunc _error; /* Pointer to client-provided error reporting function */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000328static evalfunc evaluate;
329
H. Peter Anvine2c80182005-01-15 22:15:51 +0000330static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700331static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000332
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700333static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000334
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000335static Line *predef = NULL;
336
337static ListGen *list;
338
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000339/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000340 * The current set of multi-line macros we have defined.
341 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700342static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000343
344/*
345 * The current set of single-line macros we have defined.
346 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700347static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000348
349/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000350 * The multi-line macro we are currently defining, or the %rep
351 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000352 */
353static MMacro *defining;
354
355/*
356 * The number of macro parameters to allocate space for at a time.
357 */
358#define PARAM_DELTA 16
359
360/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700361 * The standard macro set: defined in macros.c in the array nasm_stdmac.
362 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000363 */
H. Peter Anvin188ce762008-02-16 13:58:45 -0800364static const char * const *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000365
366/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000367 * The extra standard macros that come from the object format, if
368 * any.
369 */
H. Peter Anvin188ce762008-02-16 13:58:45 -0800370static const char * const *extrastdmac = NULL;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700371bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000372
373/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000374 * Tokens are allocated in blocks to improve speed
375 */
376#define TOKEN_BLOCKSIZE 4096
377static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000378struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000379 Blocks *next;
380 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000381};
382
383static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000384
385/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000386 * Forward declarations.
387 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000388static Token *expand_mmac_params(Token * tline);
389static Token *expand_smacro(Token * tline);
390static Token *expand_id(Token * tline);
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700391static Context *get_ctx(char *name, bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700392static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000393static void error(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000394static void *new_Block(size_t size);
395static void delete_Blocks(void);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000396static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000397static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000398
399/*
400 * Macros for safe checking of token pointers, avoid *(NULL)
401 */
402#define tok_type_(x,t) ((x) && (x)->type == (t))
403#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
404#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
405#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000406
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000407/* Handle TASM specific directives, which do not contain a % in
408 * front of them. We do it here because I could not find any other
409 * place to do it for the moment, and it is a hack (ideally it would
410 * be nice to be able to use the NASM pre-processor to do it).
411 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000412static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000413{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000414 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000415 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000416
417 /* Skip whitespace */
418 while (isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000419 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000420
421 /* Binary search for the directive name */
422 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000423 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000424 len = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000425 while (!isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000426 len++;
427 if (len) {
428 oldchar = p[len];
429 p[len] = 0;
430 while (j - i > 1) {
431 k = (j + i) / 2;
432 m = nasm_stricmp(p, tasm_directives[k]);
433 if (m == 0) {
434 /* We have found a directive, so jam a % in front of it
435 * so that NASM will then recognise it as one if it's own.
436 */
437 p[len] = oldchar;
438 len = strlen(p);
439 oldline = line;
440 line = nasm_malloc(len + 2);
441 line[0] = '%';
442 if (k == TM_IFDIFI) {
443 /* NASM does not recognise IFDIFI, so we convert it to
444 * %ifdef BOGUS. This is not used in NASM comaptible
445 * code, but does need to parse for the TASM macro
446 * package.
447 */
448 strcpy(line + 1, "ifdef BOGUS");
449 } else {
450 memcpy(line + 1, p, len + 1);
451 }
452 nasm_free(oldline);
453 return line;
454 } else if (m < 0) {
455 j = k;
456 } else
457 i = k;
458 }
459 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000460 }
461 return line;
462}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000463
H. Peter Anvin76690a12002-04-30 20:52:49 +0000464/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000465 * The pre-preprocessing stage... This function translates line
466 * number indications as they emerge from GNU cpp (`# lineno "file"
467 * flags') into NASM preprocessor line number indications (`%line
468 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000469 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000470static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000471{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000472 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000473 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000474
H. Peter Anvine2c80182005-01-15 22:15:51 +0000475 if (line[0] == '#' && line[1] == ' ') {
476 oldline = line;
477 fname = oldline + 2;
478 lineno = atoi(fname);
479 fname += strspn(fname, "0123456789 ");
480 if (*fname == '"')
481 fname++;
482 fnlen = strcspn(fname, "\"");
483 line = nasm_malloc(20 + fnlen);
484 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
485 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000486 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000487 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000488 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000489 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000490}
491
492/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000493 * Free a linked list of tokens.
494 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000495static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000496{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000497 while (list) {
498 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000499 }
500}
501
502/*
503 * Free a linked list of lines.
504 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000505static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000506{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000507 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508 while (list) {
509 l = list;
510 list = list->next;
511 free_tlist(l->first);
512 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000513 }
514}
515
516/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000517 * Free an MMacro
518 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000519static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000520{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000521 nasm_free(m->name);
522 free_tlist(m->dlist);
523 nasm_free(m->defaults);
524 free_llist(m->expansion);
525 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000526}
527
528/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700529 * Free all currently defined macros, and free the hash tables
530 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700531static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700532{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700533 SMacro *s;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700534 const char *key;
535 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700536
H. Peter Anvin072771e2008-05-22 13:17:51 -0700537 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700538 nasm_free((void *)key);
539 while (s) {
540 SMacro *ns = s->next;
541 nasm_free(s->name);
542 free_tlist(s->expansion);
543 nasm_free(s);
544 s = ns;
545 }
546 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700547 hash_free(smt);
548}
549
550static void free_mmacro_table(struct hash_table *mmt)
551{
552 MMacro *m;
553 const char *key;
554 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700555
556 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700557 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700558 nasm_free((void *)key);
559 while (m) {
560 MMacro *nm = m->next;
561 free_mmacro(m);
562 m = nm;
563 }
564 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700565 hash_free(mmt);
566}
567
568static void free_macros(void)
569{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700570 free_smacro_table(&smacros);
571 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700572}
573
574/*
575 * Initialize the hash tables
576 */
577static void init_macros(void)
578{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700579 hash_init(&smacros, HASH_LARGE);
580 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700581}
582
583/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000584 * Pop the context stack.
585 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000586static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000587{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000588 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000589
590 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700591 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000592 nasm_free(c->name);
593 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000594}
595
H. Peter Anvin072771e2008-05-22 13:17:51 -0700596/*
597 * Search for a key in the hash index; adding it if necessary
598 * (in which case we initialize the data pointer to NULL.)
599 */
600static void **
601hash_findi_add(struct hash_table *hash, const char *str)
602{
603 struct hash_insert hi;
604 void **r;
605 char *strx;
606
607 r = hash_findi(hash, str, &hi);
608 if (r)
609 return r;
610
611 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
612 return hash_add(&hi, strx, NULL);
613}
614
615/*
616 * Like hash_findi, but returns the data element rather than a pointer
617 * to it. Used only when not adding a new element, hence no third
618 * argument.
619 */
620static void *
621hash_findix(struct hash_table *hash, const char *str)
622{
623 void **p;
624
625 p = hash_findi(hash, str, NULL);
626 return p ? *p : NULL;
627}
628
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000629#define BUF_DELTA 512
630/*
631 * Read a line from the top file in istk, handling multiple CR/LFs
632 * at the end of the line read, and handling spurious ^Zs. Will
633 * return lines from the standard macro set if this has not already
634 * been done.
635 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000636static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000637{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000638 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000639 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000640
H. Peter Anvine2c80182005-01-15 22:15:51 +0000641 if (stdmacpos) {
642 if (*stdmacpos) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000643 char *ret = nasm_strdup(*stdmacpos++);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000644 if (!*stdmacpos && any_extrastdmac) {
645 stdmacpos = extrastdmac;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700646 any_extrastdmac = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000647 return ret;
648 }
649 /*
650 * Nasty hack: here we push the contents of `predef' on
651 * to the top-level expansion stack, since this is the
652 * most convenient way to implement the pre-include and
653 * pre-define features.
654 */
655 if (!*stdmacpos) {
656 Line *pd, *l;
657 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000658
H. Peter Anvine2c80182005-01-15 22:15:51 +0000659 for (pd = predef; pd; pd = pd->next) {
660 head = NULL;
661 tail = &head;
662 for (t = pd->first; t; t = t->next) {
663 *tail = new_Token(NULL, t->type, t->text, 0);
664 tail = &(*tail)->next;
665 }
666 l = nasm_malloc(sizeof(Line));
667 l->next = istk->expansion;
668 l->first = head;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700669 l->finishes = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000670 istk->expansion = l;
671 }
672 }
673 return ret;
674 } else {
675 stdmacpos = NULL;
676 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000677 }
678
679 bufsize = BUF_DELTA;
680 buffer = nasm_malloc(BUF_DELTA);
681 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000682 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000683 while (1) {
684 q = fgets(p, bufsize - (p - buffer), istk->fp);
685 if (!q)
686 break;
687 p += strlen(p);
688 if (p > buffer && p[-1] == '\n') {
689 /* Convert backslash-CRLF line continuation sequences into
690 nothing at all (for DOS and Windows) */
691 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
692 p -= 3;
693 *p = 0;
694 continued_count++;
695 }
696 /* Also convert backslash-LF line continuation sequences into
697 nothing at all (for Unix) */
698 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
699 p -= 2;
700 *p = 0;
701 continued_count++;
702 } else {
703 break;
704 }
705 }
706 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000707 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000708 bufsize += BUF_DELTA;
709 buffer = nasm_realloc(buffer, bufsize);
710 p = buffer + offset; /* prevent stale-pointer problems */
711 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000712 }
713
H. Peter Anvine2c80182005-01-15 22:15:51 +0000714 if (!q && p == buffer) {
715 nasm_free(buffer);
716 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000717 }
718
H. Peter Anvine2c80182005-01-15 22:15:51 +0000719 src_set_linnum(src_get_linnum() + istk->lineinc +
720 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000721
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000722 /*
723 * Play safe: remove CRs as well as LFs, if any of either are
724 * present at the end of the line.
725 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000726 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000727 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000728
729 /*
730 * Handle spurious ^Z, which may be inserted into source files
731 * by some file transfer utilities.
732 */
733 buffer[strcspn(buffer, "\032")] = '\0';
734
H. Peter Anvin734b1882002-04-30 21:01:08 +0000735 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000736
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000737 return buffer;
738}
739
740/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000741 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000742 * don't need to parse the value out of e.g. numeric tokens: we
743 * simply split one string into many.
744 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000745static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000746{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000747 char *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000748 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000749 Token *list = NULL;
750 Token *t, **tail = &list;
751
H. Peter Anvine2c80182005-01-15 22:15:51 +0000752 while (*line) {
753 p = line;
754 if (*p == '%') {
755 p++;
756 if (isdigit(*p) ||
757 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
758 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
759 do {
760 p++;
761 }
762 while (isdigit(*p));
763 type = TOK_PREPROC_ID;
764 } else if (*p == '{') {
765 p++;
766 while (*p && *p != '}') {
767 p[-1] = *p;
768 p++;
769 }
770 p[-1] = '\0';
771 if (*p)
772 p++;
773 type = TOK_PREPROC_ID;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700774 } else if (*p == '?') {
775 type = TOK_PREPROC_Q; /* %? */
776 p++;
777 if (*p == '?') {
778 type = TOK_PREPROC_QQ; /* %?? */
779 p++;
780 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000781 } else if (isidchar(*p) ||
782 ((*p == '!' || *p == '%' || *p == '$') &&
783 isidchar(p[1]))) {
784 do {
785 p++;
786 }
787 while (isidchar(*p));
788 type = TOK_PREPROC_ID;
789 } else {
790 type = TOK_OTHER;
791 if (*p == '%')
792 p++;
793 }
794 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
795 type = TOK_ID;
796 p++;
797 while (*p && isidchar(*p))
798 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700799 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000800 /*
801 * A string token.
802 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700804 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000805
H. Peter Anvine2c80182005-01-15 22:15:51 +0000806 if (*p) {
807 p++;
808 } else {
809 error(ERR_WARNING, "unterminated string");
810 /* Handling unterminated strings by UNV */
811 /* type = -1; */
812 }
813 } else if (isnumstart(*p)) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700814 bool is_hex = false;
815 bool is_float = false;
816 bool has_e = false;
817 char c, *r;
818
H. Peter Anvine2c80182005-01-15 22:15:51 +0000819 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700820 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000821 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700822
823 if (*p == '$') {
824 p++;
825 is_hex = true;
826 }
827
828 for (;;) {
829 c = *p++;
830
831 if (!is_hex && (c == 'e' || c == 'E')) {
832 has_e = true;
833 if (*p == '+' || *p == '-') {
834 /* e can only be followed by +/- if it is either a
835 prefixed hex number or a floating-point number */
836 p++;
837 is_float = true;
838 }
839 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
840 is_hex = true;
841 } else if (c == 'P' || c == 'p') {
842 is_float = true;
843 if (*p == '+' || *p == '-')
844 p++;
845 } else if (isnumchar(c) || c == '_')
846 ; /* just advance */
847 else if (c == '.') {
848 /* we need to deal with consequences of the legacy
849 parser, like "1.nolist" being two tokens
850 (TOK_NUMBER, TOK_ID) here; at least give it
851 a shot for now. In the future, we probably need
852 a flex-based scanner with proper pattern matching
853 to do it as well as it can be done. Nothing in
854 the world is going to help the person who wants
855 0x123.p16 interpreted as two tokens, though. */
856 r = p;
857 while (*r == '_')
858 r++;
859
860 if (isdigit(*r) || (is_hex && isxdigit(*r)) ||
861 (!is_hex && (*r == 'e' || *r == 'E')) ||
862 (*r == 'p' || *r == 'P')) {
863 p = r;
864 is_float = true;
865 } else
866 break; /* Terminate the token */
867 } else
868 break;
869 }
870 p--; /* Point to first character beyond number */
871
872 if (has_e && !is_hex) {
873 /* 1e13 is floating-point, but 1e13h is not */
874 is_float = true;
875 }
876
877 type = is_float ? TOK_FLOAT : TOK_NUMBER;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000878 } else if (isspace(*p)) {
879 type = TOK_WHITESPACE;
880 p++;
881 while (*p && isspace(*p))
882 p++;
883 /*
884 * Whitespace just before end-of-line is discarded by
885 * pretending it's a comment; whitespace just before a
886 * comment gets lumped into the comment.
887 */
888 if (!*p || *p == ';') {
889 type = TOK_COMMENT;
890 while (*p)
891 p++;
892 }
893 } else if (*p == ';') {
894 type = TOK_COMMENT;
895 while (*p)
896 p++;
897 } else {
898 /*
899 * Anything else is an operator of some kind. We check
900 * for all the double-character operators (>>, <<, //,
901 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000902 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000903 */
904 type = TOK_OTHER;
905 if ((p[0] == '>' && p[1] == '>') ||
906 (p[0] == '<' && p[1] == '<') ||
907 (p[0] == '/' && p[1] == '/') ||
908 (p[0] == '<' && p[1] == '=') ||
909 (p[0] == '>' && p[1] == '=') ||
910 (p[0] == '=' && p[1] == '=') ||
911 (p[0] == '!' && p[1] == '=') ||
912 (p[0] == '<' && p[1] == '>') ||
913 (p[0] == '&' && p[1] == '&') ||
914 (p[0] == '|' && p[1] == '|') ||
915 (p[0] == '^' && p[1] == '^')) {
916 p++;
917 }
918 p++;
919 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000920
H. Peter Anvine2c80182005-01-15 22:15:51 +0000921 /* Handling unterminated string by UNV */
922 /*if (type == -1)
923 {
924 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
925 t->text[p-line] = *line;
926 tail = &t->next;
927 }
928 else */
929 if (type != TOK_COMMENT) {
930 *tail = t = new_Token(NULL, type, line, p - line);
931 tail = &t->next;
932 }
933 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000934 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000935 return list;
936}
937
H. Peter Anvince616072002-04-30 21:02:23 +0000938/*
939 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -0700940 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +0000941 * deleted only all at once by the delete_Blocks function.
942 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000943static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +0000944{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000945 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000947 /* first, get to the end of the linked list */
948 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000949 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000950 /* now allocate the requested chunk */
951 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000952
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000953 /* now allocate a new block for the next request */
954 b->next = nasm_malloc(sizeof(Blocks));
955 /* and initialize the contents of the new block */
956 b->next->next = NULL;
957 b->next->chunk = NULL;
958 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000959}
960
961/*
962 * this function deletes all managed blocks of memory
963 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000964static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +0000965{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +0000967
H. Peter Anvin70653092007-10-19 14:42:29 -0700968 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000969 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -0700970 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000971 * free it.
972 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973 while (b) {
974 if (b->chunk)
975 nasm_free(b->chunk);
976 a = b;
977 b = b->next;
978 if (a != &blocks)
979 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000980 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000981}
H. Peter Anvin734b1882002-04-30 21:01:08 +0000982
983/*
H. Peter Anvin70653092007-10-19 14:42:29 -0700984 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +0000985 * back to the caller. It sets the type and text elements, and
986 * also the mac and next elements to NULL.
987 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -0700988static Token *new_Token(Token * next, enum pp_token_type type,
989 char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +0000990{
991 Token *t;
992 int i;
993
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 if (freeTokens == NULL) {
995 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
996 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
997 freeTokens[i].next = &freeTokens[i + 1];
998 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000999 }
1000 t = freeTokens;
1001 freeTokens = t->next;
1002 t->next = next;
1003 t->mac = NULL;
1004 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 if (type == TOK_WHITESPACE || text == NULL) {
1006 t->text = NULL;
1007 } else {
1008 if (txtlen == 0)
1009 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001010 t->text = nasm_malloc(txtlen+1);
1011 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001013 }
1014 return t;
1015}
1016
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001018{
1019 Token *next = t->next;
1020 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001021 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001022 freeTokens = t;
1023 return next;
1024}
1025
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001026/*
1027 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001028 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1029 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001030 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001031static char *detoken(Token * tlist, int expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001032{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001033 Token *t;
1034 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001035 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001036 const char *q;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001037
1038 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001039 for (t = tlist; t; t = t->next) {
1040 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001041 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001042 nasm_free(t->text);
1043 if (p)
1044 t->text = nasm_strdup(p);
1045 else
1046 t->text = NULL;
1047 }
1048 /* Expand local macros here and not during preprocessing */
1049 if (expand_locals &&
1050 t->type == TOK_PREPROC_ID && t->text &&
1051 t->text[0] == '%' && t->text[1] == '$') {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001052 Context *ctx = get_ctx(t->text, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001053 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001054 char buffer[40];
1055 char *p, *q = t->text + 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001056
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 q += strspn(q, "$");
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001058 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 p = nasm_strcat(buffer, q);
1060 nasm_free(t->text);
1061 t->text = p;
1062 }
1063 }
1064 if (t->type == TOK_WHITESPACE) {
1065 len++;
1066 } else if (t->text) {
1067 len += strlen(t->text);
1068 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001069 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001070 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071 for (t = tlist; t; t = t->next) {
1072 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001073 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 } else if (t->text) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001075 q = t->text;
1076 while (*q)
1077 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001078 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001079 }
1080 *p = '\0';
1081 return line;
1082}
1083
1084/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001085 * A scanner, suitable for use by the expression evaluator, which
1086 * operates on a line of Tokens. Expects a pointer to a pointer to
1087 * the first token in the line to be passed in as its private_data
1088 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001089 *
1090 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001091 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001093{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001094 Token **tlineptr = private_data;
1095 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001096 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001097
H. Peter Anvine2c80182005-01-15 22:15:51 +00001098 do {
1099 tline = *tlineptr;
1100 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001101 }
1102 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001103 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001104
1105 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001106 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001107
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001108 tokval->t_charptr = tline->text;
1109
H. Peter Anvin76690a12002-04-30 20:52:49 +00001110 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001112 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001113 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001114
H. Peter Anvine2c80182005-01-15 22:15:51 +00001115 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001116 p = tokval->t_charptr = tline->text;
1117 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001118 tokval->t_charptr++;
1119 return tokval->t_type = TOKEN_ID;
1120 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001121
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001122 for (r = p, s = ourcopy; *r; r++) {
Philipp Thomas76ec8e72008-05-21 08:53:21 -07001123 if (r >= p+MAX_KEYWORD)
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001124 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1125 *s++ = tolower(*r);
1126 }
1127 *s = '\0';
1128 /* right, so we have an identifier sitting in temp storage. now,
1129 * is it actually a register or instruction name, or what? */
1130 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001131 }
1132
H. Peter Anvine2c80182005-01-15 22:15:51 +00001133 if (tline->type == TOK_NUMBER) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001134 bool rn_error;
1135 tokval->t_integer = readnum(tline->text, &rn_error);
1136 if (rn_error)
1137 return tokval->t_type = TOKEN_ERRNUM; /* some malformation occurred */
1138 tokval->t_charptr = tline->text;
1139 return tokval->t_type = TOKEN_NUM;
1140 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001141
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001142 if (tline->type == TOK_FLOAT) {
1143 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001144 }
1145
H. Peter Anvine2c80182005-01-15 22:15:51 +00001146 if (tline->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001147 char bq, *ep;
1148 bool errquote;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001149 bool rn_warn;
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001150 size_t l;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001151
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001152 bq = tline->text[0];
1153 l = nasm_unquote(tline->text, &ep);
1154 if (ep[0] != bq || ep[1] != '\0')
1155 errquote = true;
1156
1157 if (errquote)
1158 return tokval->t_type = TOKEN_ERRNUM;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001159
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001160 tokval->t_integer = readstrnum(tline->text, l, &rn_warn);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161 if (rn_warn)
1162 error(ERR_WARNING | ERR_PASS1, "character constant too long");
1163 tokval->t_charptr = NULL;
1164 return tokval->t_type = TOKEN_NUM;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001165 }
1166
H. Peter Anvine2c80182005-01-15 22:15:51 +00001167 if (tline->type == TOK_OTHER) {
1168 if (!strcmp(tline->text, "<<"))
1169 return tokval->t_type = TOKEN_SHL;
1170 if (!strcmp(tline->text, ">>"))
1171 return tokval->t_type = TOKEN_SHR;
1172 if (!strcmp(tline->text, "//"))
1173 return tokval->t_type = TOKEN_SDIV;
1174 if (!strcmp(tline->text, "%%"))
1175 return tokval->t_type = TOKEN_SMOD;
1176 if (!strcmp(tline->text, "=="))
1177 return tokval->t_type = TOKEN_EQ;
1178 if (!strcmp(tline->text, "<>"))
1179 return tokval->t_type = TOKEN_NE;
1180 if (!strcmp(tline->text, "!="))
1181 return tokval->t_type = TOKEN_NE;
1182 if (!strcmp(tline->text, "<="))
1183 return tokval->t_type = TOKEN_LE;
1184 if (!strcmp(tline->text, ">="))
1185 return tokval->t_type = TOKEN_GE;
1186 if (!strcmp(tline->text, "&&"))
1187 return tokval->t_type = TOKEN_DBL_AND;
1188 if (!strcmp(tline->text, "^^"))
1189 return tokval->t_type = TOKEN_DBL_XOR;
1190 if (!strcmp(tline->text, "||"))
1191 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001192 }
1193
1194 /*
1195 * We have no other options: just return the first character of
1196 * the token text.
1197 */
1198 return tokval->t_type = tline->text[0];
1199}
1200
1201/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001202 * Compare a string to the name of an existing macro; this is a
1203 * simple wrapper which calls either strcmp or nasm_stricmp
1204 * depending on the value of the `casesense' parameter.
1205 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001206static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001207{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001208 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001209}
1210
1211/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001212 * Compare a string to the name of an existing macro; this is a
1213 * simple wrapper which calls either strcmp or nasm_stricmp
1214 * depending on the value of the `casesense' parameter.
1215 */
1216static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1217{
1218 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1219}
1220
1221/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001222 * Return the Context structure associated with a %$ token. Return
1223 * NULL, having _already_ reported an error condition, if the
1224 * context stack isn't deep enough for the supplied number of $
1225 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001226 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001227 * also scanned for such smacro, until it is found; if not -
1228 * only the context that directly results from the number of $'s
1229 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001230 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001231static Context *get_ctx(char *name, bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001232{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001233 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001234 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001235 int i;
1236
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001237 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001238 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001239
H. Peter Anvine2c80182005-01-15 22:15:51 +00001240 if (!cstk) {
1241 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1242 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001243 }
1244
H. Peter Anvine2c80182005-01-15 22:15:51 +00001245 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1246 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001247/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001248 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001249 if (!ctx) {
1250 error(ERR_NONFATAL, "`%s': context stack is only"
1251 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1252 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001253 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001254 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001255 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001256
H. Peter Anvine2c80182005-01-15 22:15:51 +00001257 do {
1258 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001259 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001260 while (m) {
1261 if (!mstrcmp(m->name, name, m->casesense))
1262 return ctx;
1263 m = m->next;
1264 }
1265 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001266 }
1267 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001268 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001269}
1270
1271/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001272 * Check to see if a file is already in a string list
1273 */
1274static bool in_list(const StrList *list, const char *str)
1275{
1276 while (list) {
1277 if (!strcmp(list->str, str))
1278 return true;
1279 list = list->next;
1280 }
1281 return false;
1282}
1283
1284/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001285 * Open an include file. This routine must always return a valid
1286 * file pointer if it returns - it's responsible for throwing an
1287 * ERR_FATAL and bombing out completely if not. It should also try
1288 * the include path one by one until it finds the file or reaches
1289 * the end of the path.
1290 */
H. Peter Anvin418ca702008-05-30 10:42:30 -07001291static FILE *inc_fopen(const char *file, StrList **dhead, StrList **dtail,
1292 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001293{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001294 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001295 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001296 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001297 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001298 size_t prefix_len = 0;
1299 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001300
H. Peter Anvine2c80182005-01-15 22:15:51 +00001301 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001302 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1303 memcpy(sl->str, prefix, prefix_len);
1304 memcpy(sl->str+prefix_len, file, len+1);
1305 fp = fopen(sl->str, "r");
H. Peter Anvin418ca702008-05-30 10:42:30 -07001306 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001307 sl->next = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07001308 *dtail = sl;
1309 dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001310 } else {
1311 nasm_free(sl);
1312 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 if (fp)
1314 return fp;
H. Peter Anvin418ca702008-05-30 10:42:30 -07001315 if (!ip) {
1316 if (!missing_ok)
1317 break;
1318 prefix = NULL;
1319 } else {
1320 prefix = ip->path;
1321 ip = ip->next;
1322 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001323 if (prefix) {
1324 prefix_len = strlen(prefix);
1325 } else {
1326 /* -MG given and file not found */
H. Peter Anvin418ca702008-05-30 10:42:30 -07001327 if (dhead && !in_list(*dhead, file)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001328 sl = nasm_malloc(len+1+sizeof sl->next);
1329 sl->next = NULL;
1330 strcpy(sl->str, file);
H. Peter Anvin418ca702008-05-30 10:42:30 -07001331 *dtail = sl;
1332 dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001333 }
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001334 return NULL;
1335 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001336 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001337
H. Peter Anvin734b1882002-04-30 21:01:08 +00001338 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001339 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001340}
1341
1342/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001343 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001344 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001345 * return true if _any_ single-line macro of that name is defined.
1346 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001347 * `nparam' or no parameters is defined.
1348 *
1349 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001350 * defined, or nparam is -1, the address of the definition structure
1351 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001352 * is NULL, no action will be taken regarding its contents, and no
1353 * error will occur.
1354 *
1355 * Note that this is also called with nparam zero to resolve
1356 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001357 *
1358 * If you already know which context macro belongs to, you can pass
1359 * the context pointer as first parameter; if you won't but name begins
1360 * with %$ the context will be automatically computed. If all_contexts
1361 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001362 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001363static bool
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001364smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001365 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001366{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001367 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001368 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001369
H. Peter Anvin97a23472007-09-16 17:57:25 -07001370 if (ctx) {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001371 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001372 } else if (name[0] == '%' && name[1] == '$') {
1373 if (cstk)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001374 ctx = get_ctx(name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001375 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001376 return false; /* got to return _something_ */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001377 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001378 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001379 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001380 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001381 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001382
H. Peter Anvine2c80182005-01-15 22:15:51 +00001383 while (m) {
1384 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001385 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001386 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001387 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001388 *defn = m;
1389 else
1390 *defn = NULL;
1391 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001392 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001393 }
1394 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001395 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001396
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001397 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001398}
1399
1400/*
1401 * Count and mark off the parameters in a multi-line macro call.
1402 * This is called both from within the multi-line macro expansion
1403 * code, and also to mark off the default parameters when provided
1404 * in a %macro definition line.
1405 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001406static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001407{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001408 int paramsize, brace;
1409
1410 *nparam = paramsize = 0;
1411 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001412 while (t) {
1413 if (*nparam >= paramsize) {
1414 paramsize += PARAM_DELTA;
1415 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1416 }
1417 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001418 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001419 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001420 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001421 (*params)[(*nparam)++] = t;
1422 while (tok_isnt_(t, brace ? "}" : ","))
1423 t = t->next;
1424 if (t) { /* got a comma/brace */
1425 t = t->next;
1426 if (brace) {
1427 /*
1428 * Now we've found the closing brace, look further
1429 * for the comma.
1430 */
1431 skip_white_(t);
1432 if (tok_isnt_(t, ",")) {
1433 error(ERR_NONFATAL,
1434 "braces do not enclose all of macro parameter");
1435 while (tok_isnt_(t, ","))
1436 t = t->next;
1437 }
1438 if (t)
1439 t = t->next; /* eat the comma */
1440 }
1441 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001442 }
1443}
1444
1445/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001446 * Determine whether one of the various `if' conditions is true or
1447 * not.
1448 *
1449 * We must free the tline we get passed.
1450 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001451static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001452{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001453 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001454 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001455 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001456 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001457 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001458 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001459
1460 origline = tline;
1461
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001463 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001464 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001465 while (cstk && tline) {
1466 skip_white_(tline);
1467 if (!tline || tline->type != TOK_ID) {
1468 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001469 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001470 free_tlist(origline);
1471 return -1;
1472 }
1473 if (!nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001474 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001475 tline = tline->next;
1476 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001477 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001478
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001479 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001480 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001481 while (tline) {
1482 skip_white_(tline);
1483 if (!tline || (tline->type != TOK_ID &&
1484 (tline->type != TOK_PREPROC_ID ||
1485 tline->text[1] != '$'))) {
1486 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001487 "`%s' expects macro identifiers", pp_directives[ct]);
1488 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001489 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001490 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001491 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001492 tline = tline->next;
1493 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001494 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001495
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001496 case PPC_IFIDN:
1497 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001498 tline = expand_smacro(tline);
1499 t = tt = tline;
1500 while (tok_isnt_(tt, ","))
1501 tt = tt->next;
1502 if (!tt) {
1503 error(ERR_NONFATAL,
1504 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001505 pp_directives[ct]);
1506 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001507 }
1508 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001509 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001510 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1511 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1512 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001513 pp_directives[ct]);
1514 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001515 }
1516 if (t->type == TOK_WHITESPACE) {
1517 t = t->next;
1518 continue;
1519 }
1520 if (tt->type == TOK_WHITESPACE) {
1521 tt = tt->next;
1522 continue;
1523 }
1524 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001525 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001526 break;
1527 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001528 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001529 if (t->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001530 size_t l1 = nasm_unquote(t->text, NULL);
1531 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001532
1533 if (l1 != l2) {
1534 j = false;
1535 break;
1536 }
1537 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1538 j = false;
1539 break;
1540 }
1541 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001542 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001543 break;
1544 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001545
H. Peter Anvine2c80182005-01-15 22:15:51 +00001546 t = t->next;
1547 tt = tt->next;
1548 }
1549 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001550 j = false; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001551 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001552
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001553 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001554 {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001555 bool found = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001556 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001557
H. Peter Anvine2c80182005-01-15 22:15:51 +00001558 tline = tline->next;
1559 skip_white_(tline);
1560 tline = expand_id(tline);
1561 if (!tok_type_(tline, TOK_ID)) {
1562 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001563 "`%s' expects a macro name", pp_directives[ct]);
1564 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001565 }
1566 searching.name = nasm_strdup(tline->text);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001567 searching.casesense = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001568 searching.plus = false;
1569 searching.nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001570 searching.in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001571 searching.rep_nest = NULL;
1572 searching.nparam_min = 0;
1573 searching.nparam_max = INT_MAX;
1574 tline = expand_smacro(tline->next);
1575 skip_white_(tline);
1576 if (!tline) {
1577 } else if (!tok_type_(tline, TOK_NUMBER)) {
1578 error(ERR_NONFATAL,
1579 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001580 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001581 } else {
1582 searching.nparam_min = searching.nparam_max =
1583 readnum(tline->text, &j);
1584 if (j)
1585 error(ERR_NONFATAL,
1586 "unable to parse parameter count `%s'",
1587 tline->text);
1588 }
1589 if (tline && tok_is_(tline->next, "-")) {
1590 tline = tline->next->next;
1591 if (tok_is_(tline, "*"))
1592 searching.nparam_max = INT_MAX;
1593 else if (!tok_type_(tline, TOK_NUMBER))
1594 error(ERR_NONFATAL,
1595 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001596 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001597 else {
1598 searching.nparam_max = readnum(tline->text, &j);
1599 if (j)
1600 error(ERR_NONFATAL,
1601 "unable to parse parameter count `%s'",
1602 tline->text);
1603 if (searching.nparam_min > searching.nparam_max)
1604 error(ERR_NONFATAL,
1605 "minimum parameter count exceeds maximum");
1606 }
1607 }
1608 if (tline && tok_is_(tline->next, "+")) {
1609 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001610 searching.plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001611 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001612 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001613 while (mmac) {
1614 if (!strcmp(mmac->name, searching.name) &&
1615 (mmac->nparam_min <= searching.nparam_max
1616 || searching.plus)
1617 && (searching.nparam_min <= mmac->nparam_max
1618 || mmac->plus)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001619 found = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001620 break;
1621 }
1622 mmac = mmac->next;
1623 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001624 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001625 j = found;
1626 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001627 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001628
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001629 case PPC_IFID:
1630 needtype = TOK_ID;
1631 goto iftype;
1632 case PPC_IFNUM:
1633 needtype = TOK_NUMBER;
1634 goto iftype;
1635 case PPC_IFSTR:
1636 needtype = TOK_STRING;
1637 goto iftype;
1638
1639 iftype:
H. Peter Anvin134b9462008-02-16 17:01:40 -08001640 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001641
H. Peter Anvin927c92b2008-02-16 13:44:52 -08001642 while (tok_type_(t, TOK_WHITESPACE) ||
1643 (needtype == TOK_NUMBER &&
1644 tok_type_(t, TOK_OTHER) &&
1645 (t->text[0] == '-' || t->text[0] == '+') &&
1646 !t->text[1]))
1647 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001648
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001649 j = tok_type_(t, needtype);
1650 break;
1651
1652 case PPC_IFTOKEN:
1653 t = tline = expand_smacro(tline);
1654 while (tok_type_(t, TOK_WHITESPACE))
1655 t = t->next;
1656
1657 j = false;
1658 if (t) {
1659 t = t->next; /* Skip the actual token */
1660 while (tok_type_(t, TOK_WHITESPACE))
1661 t = t->next;
1662 j = !t; /* Should be nothing left */
1663 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001664 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001665
H. Peter Anvin134b9462008-02-16 17:01:40 -08001666 case PPC_IFEMPTY:
1667 t = tline = expand_smacro(tline);
1668 while (tok_type_(t, TOK_WHITESPACE))
1669 t = t->next;
1670
1671 j = !t; /* Should be empty */
1672 break;
1673
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001674 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001675 t = tline = expand_smacro(tline);
1676 tptr = &t;
1677 tokval.t_type = TOKEN_INVALID;
1678 evalresult = evaluate(ppscan, tptr, &tokval,
1679 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001680 if (!evalresult)
1681 return -1;
1682 if (tokval.t_type)
1683 error(ERR_WARNING,
1684 "trailing garbage after expression ignored");
1685 if (!is_simple(evalresult)) {
1686 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001687 "non-constant value given to `%s'", pp_directives[ct]);
1688 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001689 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001690 j = reloc_value(evalresult) != 0;
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001691 return j;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001692
H. Peter Anvine2c80182005-01-15 22:15:51 +00001693 default:
1694 error(ERR_FATAL,
1695 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001696 pp_directives[ct]);
1697 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001698 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001699
1700 free_tlist(origline);
1701 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001702
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001703fail:
1704 free_tlist(origline);
1705 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001706}
1707
1708/*
H. Peter Anvin418ca702008-05-30 10:42:30 -07001709 * Expand macros in a string. Used in %error directives (and it should
1710 * almost certainly be removed from there, too.)
1711 *
Keith Kaniosb7a89542007-04-12 02:40:54 +00001712 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001713 * The returned variable should ALWAYS be freed after usage.
1714 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001715void expand_macros_in_string(char **p)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001716{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001717 Token *line = tokenize(*p);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001718 line = expand_smacro(line);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001719 *p = detoken(line, false);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001720}
1721
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001722/*
1723 * Common code for defining an smacro
1724 */
1725static bool define_smacro(Context *ctx, char *mname, bool casesense,
1726 int nparam, Token *expansion)
1727{
1728 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001729 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001730
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001731 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1732 if (!smac) {
1733 error(ERR_WARNING,
1734 "single-line macro `%s' defined both with and"
1735 " without parameters", mname);
1736
1737 /* Some instances of the old code considered this a failure,
1738 some others didn't. What is the right thing to do here? */
1739 free_tlist(expansion);
1740 return false; /* Failure */
1741 } else {
1742 /*
1743 * We're redefining, so we have to take over an
1744 * existing SMacro structure. This means freeing
1745 * what was already in it.
1746 */
1747 nasm_free(smac->name);
1748 free_tlist(smac->expansion);
1749 }
1750 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001751 smtbl = ctx ? &ctx->localmac : &smacros;
1752 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001753 smac = nasm_malloc(sizeof(SMacro));
1754 smac->next = *smhead;
1755 *smhead = smac;
1756 }
1757 smac->name = nasm_strdup(mname);
1758 smac->casesense = casesense;
1759 smac->nparam = nparam;
1760 smac->expansion = expansion;
1761 smac->in_progress = false;
1762 return true; /* Success */
1763}
1764
1765/*
1766 * Undefine an smacro
1767 */
1768static void undef_smacro(Context *ctx, const char *mname)
1769{
1770 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001771 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001772
H. Peter Anvin166c2472008-05-28 12:28:58 -07001773 smtbl = ctx ? &ctx->localmac : &smacros;
1774 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001775
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001776 if (smhead) {
1777 /*
1778 * We now have a macro name... go hunt for it.
1779 */
1780 sp = smhead;
1781 while ((s = *sp) != NULL) {
1782 if (!mstrcmp(s->name, mname, s->casesense)) {
1783 *sp = s->next;
1784 nasm_free(s->name);
1785 free_tlist(s->expansion);
1786 nasm_free(s);
1787 } else {
1788 sp = &s->next;
1789 }
1790 }
1791 }
1792}
1793
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001794/*
1795 * Decode a size directive
1796 */
1797static int parse_size(const char *str) {
1798 static const char *size_names[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001799 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001800 static const int sizes[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001801 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001802
1803 return sizes[bsii(str, size_names, elements(size_names))+1];
1804}
1805
Ed Beroset3ab3f412002-06-11 03:31:49 +00001806/**
1807 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001808 * Find out if a line contains a preprocessor directive, and deal
1809 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07001810 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001811 * If a directive _is_ found, it is the responsibility of this routine
1812 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001813 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001814 * @param tline a pointer to the current tokeninzed line linked list
1815 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07001816 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001817 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001818static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001819{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001820 enum preproc_token i;
1821 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07001822 bool err;
1823 int nparam;
1824 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001825 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001826 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001827 int offset;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001828 char *p, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001829 Include *inc;
1830 Context *ctx;
1831 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001832 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001833 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1834 Line *l;
1835 struct tokenval tokval;
1836 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001837 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001838 int64_t count;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001839
1840 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001841
H. Peter Anvineba20a72002-04-30 20:53:55 +00001842 skip_white_(tline);
1843 if (!tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001844 (tline->text[1] == '%' || tline->text[1] == '$'
1845 || tline->text[1] == '!'))
1846 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001847
H. Peter Anvin4169a472007-09-12 01:29:43 +00001848 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001849
1850 /*
1851 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001852 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001853 * we should ignore all directives except for condition
1854 * directives.
1855 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001856 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001857 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1858 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001859 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001860
1861 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001862 * If we're defining a macro or reading a %rep block, we should
1863 * ignore all directives except for %macro/%imacro (which
1864 * generate an error), %endm/%endmacro, and (only if we're in a
H. Peter Anvineba20a72002-04-30 20:53:55 +00001865 * %rep block) %endrep. If we're in a %rep block, another %rep
1866 * causes an error, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001867 */
1868 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001869 i != PP_ENDMACRO && i != PP_ENDM &&
1870 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1871 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001872 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001873
H. Peter Anvin4169a472007-09-12 01:29:43 +00001874 switch (i) {
1875 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001876 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1877 tline->text);
1878 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001879
H. Peter Anvine2c80182005-01-15 22:15:51 +00001880 case PP_STACKSIZE:
1881 /* Directive to tell NASM what the default stack size is. The
1882 * default is for a 16-bit stack, and this can be overriden with
1883 * %stacksize large.
1884 * the following form:
1885 *
1886 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1887 */
1888 tline = tline->next;
1889 if (tline && tline->type == TOK_WHITESPACE)
1890 tline = tline->next;
1891 if (!tline || tline->type != TOK_ID) {
1892 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1893 free_tlist(origline);
1894 return DIRECTIVE_FOUND;
1895 }
1896 if (nasm_stricmp(tline->text, "flat") == 0) {
1897 /* All subsequent ARG directives are for a 32-bit stack */
1898 StackSize = 4;
1899 StackPointer = "ebp";
1900 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001901 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08001902 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
1903 /* All subsequent ARG directives are for a 64-bit stack */
1904 StackSize = 8;
1905 StackPointer = "rbp";
1906 ArgOffset = 8;
1907 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001908 } else if (nasm_stricmp(tline->text, "large") == 0) {
1909 /* All subsequent ARG directives are for a 16-bit stack,
1910 * far function call.
1911 */
1912 StackSize = 2;
1913 StackPointer = "bp";
1914 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001915 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001916 } else if (nasm_stricmp(tline->text, "small") == 0) {
1917 /* All subsequent ARG directives are for a 16-bit stack,
1918 * far function call. We don't support near functions.
1919 */
1920 StackSize = 2;
1921 StackPointer = "bp";
1922 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001923 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001924 } else {
1925 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1926 free_tlist(origline);
1927 return DIRECTIVE_FOUND;
1928 }
1929 free_tlist(origline);
1930 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001931
H. Peter Anvine2c80182005-01-15 22:15:51 +00001932 case PP_ARG:
1933 /* TASM like ARG directive to define arguments to functions, in
1934 * the following form:
1935 *
1936 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1937 */
1938 offset = ArgOffset;
1939 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001940 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001941 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001942
H. Peter Anvine2c80182005-01-15 22:15:51 +00001943 /* Find the argument name */
1944 tline = tline->next;
1945 if (tline && tline->type == TOK_WHITESPACE)
1946 tline = tline->next;
1947 if (!tline || tline->type != TOK_ID) {
1948 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1949 free_tlist(origline);
1950 return DIRECTIVE_FOUND;
1951 }
1952 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001953
H. Peter Anvine2c80182005-01-15 22:15:51 +00001954 /* Find the argument size type */
1955 tline = tline->next;
1956 if (!tline || tline->type != TOK_OTHER
1957 || tline->text[0] != ':') {
1958 error(ERR_NONFATAL,
1959 "Syntax error processing `%%arg' directive");
1960 free_tlist(origline);
1961 return DIRECTIVE_FOUND;
1962 }
1963 tline = tline->next;
1964 if (!tline || tline->type != TOK_ID) {
1965 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1966 free_tlist(origline);
1967 return DIRECTIVE_FOUND;
1968 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001969
H. Peter Anvine2c80182005-01-15 22:15:51 +00001970 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001971 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001972 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001973 size = parse_size(tt->text);
1974 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001975 error(ERR_NONFATAL,
1976 "Invalid size type for `%%arg' missing directive");
1977 free_tlist(tt);
1978 free_tlist(origline);
1979 return DIRECTIVE_FOUND;
1980 }
1981 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001982
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001983 /* Round up to even stack slots */
1984 size = (size+StackSize-1) & ~(StackSize-1);
1985
H. Peter Anvine2c80182005-01-15 22:15:51 +00001986 /* Now define the macro for the argument */
1987 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1988 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001989 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001990 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001991
H. Peter Anvine2c80182005-01-15 22:15:51 +00001992 /* Move to the next argument in the list */
1993 tline = tline->next;
1994 if (tline && tline->type == TOK_WHITESPACE)
1995 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001996 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1997 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001998 free_tlist(origline);
1999 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002000
H. Peter Anvine2c80182005-01-15 22:15:51 +00002001 case PP_LOCAL:
2002 /* TASM like LOCAL directive to define local variables for a
2003 * function, in the following form:
2004 *
2005 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2006 *
2007 * The '= LocalSize' at the end is ignored by NASM, but is
2008 * required by TASM to define the local parameter size (and used
2009 * by the TASM macro package).
2010 */
2011 offset = LocalOffset;
2012 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002013 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002014 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002015
H. Peter Anvine2c80182005-01-15 22:15:51 +00002016 /* Find the argument name */
2017 tline = tline->next;
2018 if (tline && tline->type == TOK_WHITESPACE)
2019 tline = tline->next;
2020 if (!tline || tline->type != TOK_ID) {
2021 error(ERR_NONFATAL,
2022 "`%%local' missing argument parameter");
2023 free_tlist(origline);
2024 return DIRECTIVE_FOUND;
2025 }
2026 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002027
H. Peter Anvine2c80182005-01-15 22:15:51 +00002028 /* Find the argument size type */
2029 tline = tline->next;
2030 if (!tline || tline->type != TOK_OTHER
2031 || tline->text[0] != ':') {
2032 error(ERR_NONFATAL,
2033 "Syntax error processing `%%local' directive");
2034 free_tlist(origline);
2035 return DIRECTIVE_FOUND;
2036 }
2037 tline = tline->next;
2038 if (!tline || tline->type != TOK_ID) {
2039 error(ERR_NONFATAL,
2040 "`%%local' missing size type parameter");
2041 free_tlist(origline);
2042 return DIRECTIVE_FOUND;
2043 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002044
H. Peter Anvine2c80182005-01-15 22:15:51 +00002045 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002046 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002047 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002048 size = parse_size(tt->text);
2049 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002050 error(ERR_NONFATAL,
2051 "Invalid size type for `%%local' missing directive");
2052 free_tlist(tt);
2053 free_tlist(origline);
2054 return DIRECTIVE_FOUND;
2055 }
2056 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002057
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002058 /* Round up to even stack slots */
2059 size = (size+StackSize-1) & ~(StackSize-1);
2060
2061 offset += size; /* Negative offset, increment before */
2062
2063 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002064 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2065 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002066 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002067
H. Peter Anvine2c80182005-01-15 22:15:51 +00002068 /* Now define the assign to setup the enter_c macro correctly */
2069 snprintf(directive, sizeof(directive),
2070 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002071 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002072
H. Peter Anvine2c80182005-01-15 22:15:51 +00002073 /* Move to the next argument in the list */
2074 tline = tline->next;
2075 if (tline && tline->type == TOK_WHITESPACE)
2076 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002077 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2078 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002079 free_tlist(origline);
2080 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002081
H. Peter Anvine2c80182005-01-15 22:15:51 +00002082 case PP_CLEAR:
2083 if (tline->next)
2084 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002085 free_macros();
2086 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002087 free_tlist(origline);
2088 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002089
H. Peter Anvin418ca702008-05-30 10:42:30 -07002090 case PP_DEPEND:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002091 t = tline->next = expand_smacro(tline->next);
2092 skip_white_(t);
2093 if (!t || (t->type != TOK_STRING &&
2094 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002095 error(ERR_NONFATAL, "`%%depend' expects a file name");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002096 free_tlist(t);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002097 free_tlist(origline);
2098 return DIRECTIVE_FOUND; /* but we did _something_ */
2099 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002100 if (t->next)
H. Peter Anvin418ca702008-05-30 10:42:30 -07002101 error(ERR_WARNING,
2102 "trailing garbage after `%%depend' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002103 p = t->text;
2104 if (t->type != TOK_INTERNAL_STRING)
2105 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002106 if (dephead && !in_list(*dephead, p)) {
2107 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2108 sl->next = NULL;
2109 strcpy(sl->str, p);
2110 *deptail = sl;
2111 deptail = &sl->next;
2112 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002113 free_tlist(t);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002114 free_tlist(origline);
2115 return DIRECTIVE_FOUND;
2116
2117 case PP_INCLUDE:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002118 t = tline->next = expand_smacro(tline->next);
2119 skip_white_(t);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002120
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002121 if (!t || (t->type != TOK_STRING &&
2122 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002123 error(ERR_NONFATAL, "`%%include' expects a file name");
2124 free_tlist(origline);
2125 return DIRECTIVE_FOUND; /* but we did _something_ */
2126 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002127 if (t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002128 error(ERR_WARNING,
2129 "trailing garbage after `%%include' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002130 p = t->text;
2131 if (t->type != TOK_INTERNAL_STRING)
2132 nasm_unquote(p, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002133 inc = nasm_malloc(sizeof(Include));
2134 inc->next = istk;
2135 inc->conds = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002136 inc->fp = inc_fopen(p, dephead, deptail, pass == 0);
2137 if (!inc->fp) {
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002138 /* -MG given but file not found */
2139 nasm_free(inc);
2140 } else {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002141 inc->fname = src_set_fname(nasm_strdup(p));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002142 inc->lineno = src_set_linnum(0);
2143 inc->lineinc = 1;
2144 inc->expansion = NULL;
2145 inc->mstk = NULL;
2146 istk = inc;
2147 list->uplevel(LIST_INCLUDE);
2148 }
2149 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002150 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002151
H. Peter Anvine2c80182005-01-15 22:15:51 +00002152 case PP_PUSH:
2153 tline = tline->next;
2154 skip_white_(tline);
2155 tline = expand_id(tline);
2156 if (!tok_type_(tline, TOK_ID)) {
2157 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2158 free_tlist(origline);
2159 return DIRECTIVE_FOUND; /* but we did _something_ */
2160 }
2161 if (tline->next)
2162 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2163 ctx = nasm_malloc(sizeof(Context));
2164 ctx->next = cstk;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002165 hash_init(&ctx->localmac, HASH_SMALL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002166 ctx->name = nasm_strdup(tline->text);
2167 ctx->number = unique++;
2168 cstk = ctx;
2169 free_tlist(origline);
2170 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002171
H. Peter Anvine2c80182005-01-15 22:15:51 +00002172 case PP_REPL:
2173 tline = tline->next;
2174 skip_white_(tline);
2175 tline = expand_id(tline);
2176 if (!tok_type_(tline, TOK_ID)) {
2177 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2178 free_tlist(origline);
2179 return DIRECTIVE_FOUND; /* but we did _something_ */
2180 }
2181 if (tline->next)
2182 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2183 if (!cstk)
2184 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2185 else {
2186 nasm_free(cstk->name);
2187 cstk->name = nasm_strdup(tline->text);
2188 }
2189 free_tlist(origline);
2190 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002191
H. Peter Anvine2c80182005-01-15 22:15:51 +00002192 case PP_POP:
2193 if (tline->next)
2194 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2195 if (!cstk)
2196 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2197 else
2198 ctx_pop();
2199 free_tlist(origline);
2200 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002201
H. Peter Anvine2c80182005-01-15 22:15:51 +00002202 case PP_ERROR:
2203 tline->next = expand_smacro(tline->next);
2204 tline = tline->next;
2205 skip_white_(tline);
2206 if (tok_type_(tline, TOK_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 Anvin8cad14b2008-06-01 17:23:51 -07002209 expand_macros_in_string(&p); /* WHY? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002210 error(ERR_NONFATAL, "%s", p);
2211 nasm_free(p);
2212 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002213 p = detoken(tline, false);
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002214 error(ERR_WARNING, "%s", p); /* WARNING!??!! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002215 nasm_free(p);
2216 }
2217 free_tlist(origline);
2218 break;
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 Anvin418ca702008-05-30 10:42:30 -07002652 case PP_PATHSEARCH:
2653 {
2654 FILE *fp;
2655 StrList *xsl = NULL;
2656
2657 casesense = true;
2658
2659 tline = tline->next;
2660 skip_white_(tline);
2661 tline = expand_id(tline);
2662 if (!tline || (tline->type != TOK_ID &&
2663 (tline->type != TOK_PREPROC_ID ||
2664 tline->text[1] != '$'))) {
2665 error(ERR_NONFATAL,
2666 "`%%pathsearch' expects a macro identifier as first parameter");
2667 free_tlist(origline);
2668 return DIRECTIVE_FOUND;
2669 }
2670 ctx = get_ctx(tline->text, false);
2671
2672 mname = tline->text;
2673 last = tline;
2674 tline = expand_smacro(tline->next);
2675 last->next = NULL;
2676
2677 t = tline;
2678 while (tok_type_(t, TOK_WHITESPACE))
2679 t = t->next;
2680
2681 if (!t || (t->type != TOK_STRING &&
2682 t->type != TOK_INTERNAL_STRING)) {
2683 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
2684 free_tlist(tline);
2685 free_tlist(origline);
2686 return DIRECTIVE_FOUND; /* but we did _something_ */
2687 }
2688 if (t->next)
2689 error(ERR_WARNING,
2690 "trailing garbage after `%%pathsearch' ignored");
H. Peter Anvin427cc912008-06-01 21:43:03 -07002691 p = t->text;
2692 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002693 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002694
2695 fp = inc_fopen(p, &xsl, &xsl, true);
2696 if (fp) {
2697 p = xsl->str;
2698 fclose(fp); /* Don't actually care about the file */
2699 }
2700 macro_start = nasm_malloc(sizeof(*macro_start));
2701 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002702 macro_start->text = nasm_quote(p, strlen(p));
H. Peter Anvin418ca702008-05-30 10:42:30 -07002703 macro_start->type = TOK_STRING;
2704 macro_start->mac = NULL;
2705 if (xsl)
2706 nasm_free(xsl);
2707
2708 /*
2709 * We now have a macro name, an implicit parameter count of
2710 * zero, and a string token to use as an expansion. Create
2711 * and store an SMacro.
2712 */
2713 define_smacro(ctx, mname, casesense, 0, macro_start);
2714 free_tlist(tline);
2715 free_tlist(origline);
2716 return DIRECTIVE_FOUND;
2717 }
2718
H. Peter Anvine2c80182005-01-15 22:15:51 +00002719 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002720 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07002721
H. Peter Anvine2c80182005-01-15 22:15:51 +00002722 tline = tline->next;
2723 skip_white_(tline);
2724 tline = expand_id(tline);
2725 if (!tline || (tline->type != TOK_ID &&
2726 (tline->type != TOK_PREPROC_ID ||
2727 tline->text[1] != '$'))) {
2728 error(ERR_NONFATAL,
2729 "`%%strlen' expects a macro identifier as first parameter");
2730 free_tlist(origline);
2731 return DIRECTIVE_FOUND;
2732 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002733 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002734
H. Peter Anvine2c80182005-01-15 22:15:51 +00002735 mname = tline->text;
2736 last = tline;
2737 tline = expand_smacro(tline->next);
2738 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002739
H. Peter Anvine2c80182005-01-15 22:15:51 +00002740 t = tline;
2741 while (tok_type_(t, TOK_WHITESPACE))
2742 t = t->next;
2743 /* t should now point to the string */
2744 if (t->type != TOK_STRING) {
2745 error(ERR_NONFATAL,
2746 "`%%strlen` requires string as second parameter");
2747 free_tlist(tline);
2748 free_tlist(origline);
2749 return DIRECTIVE_FOUND;
2750 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002751
H. Peter Anvine2c80182005-01-15 22:15:51 +00002752 macro_start = nasm_malloc(sizeof(*macro_start));
2753 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002754 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002755 macro_start->mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002756
H. Peter Anvine2c80182005-01-15 22:15:51 +00002757 /*
2758 * We now have a macro name, an implicit parameter count of
2759 * zero, and a numeric token to use as an expansion. Create
2760 * and store an SMacro.
2761 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002762 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002763 free_tlist(tline);
2764 free_tlist(origline);
2765 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002766
H. Peter Anvine2c80182005-01-15 22:15:51 +00002767 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002768 {
2769 int64_t a1, a2;
2770 size_t len;
2771
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002772 casesense = true;
2773
H. Peter Anvine2c80182005-01-15 22:15:51 +00002774 tline = tline->next;
2775 skip_white_(tline);
2776 tline = expand_id(tline);
2777 if (!tline || (tline->type != TOK_ID &&
2778 (tline->type != TOK_PREPROC_ID ||
2779 tline->text[1] != '$'))) {
2780 error(ERR_NONFATAL,
2781 "`%%substr' expects a macro identifier as first parameter");
2782 free_tlist(origline);
2783 return DIRECTIVE_FOUND;
2784 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002785 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002786
H. Peter Anvine2c80182005-01-15 22:15:51 +00002787 mname = tline->text;
2788 last = tline;
2789 tline = expand_smacro(tline->next);
2790 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002791
H. Peter Anvine2c80182005-01-15 22:15:51 +00002792 t = tline->next;
2793 while (tok_type_(t, TOK_WHITESPACE))
2794 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002795
H. Peter Anvine2c80182005-01-15 22:15:51 +00002796 /* t should now point to the string */
2797 if (t->type != TOK_STRING) {
2798 error(ERR_NONFATAL,
2799 "`%%substr` requires string as second parameter");
2800 free_tlist(tline);
2801 free_tlist(origline);
2802 return DIRECTIVE_FOUND;
2803 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002804
H. Peter Anvine2c80182005-01-15 22:15:51 +00002805 tt = t->next;
2806 tptr = &tt;
2807 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002808 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
2809 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002810 if (!evalresult) {
2811 free_tlist(tline);
2812 free_tlist(origline);
2813 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002814 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002815 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2816 free_tlist(tline);
2817 free_tlist(origline);
2818 return DIRECTIVE_FOUND;
2819 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002820 a1 = evalresult->value-1;
2821
2822 while (tok_type_(tt, TOK_WHITESPACE))
2823 tt = tt->next;
2824 if (!tt) {
2825 a2 = 1; /* Backwards compatibility: one character */
2826 } else {
2827 tokval.t_type = TOKEN_INVALID;
2828 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
2829 pass, error, NULL);
2830 if (!evalresult) {
2831 free_tlist(tline);
2832 free_tlist(origline);
2833 return DIRECTIVE_FOUND;
2834 } else if (!is_simple(evalresult)) {
2835 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2836 free_tlist(tline);
2837 free_tlist(origline);
2838 return DIRECTIVE_FOUND;
2839 }
2840 a2 = evalresult->value;
2841 }
2842
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002843 len = nasm_unquote(t->text, NULL);
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002844 if (a2 < 0)
2845 a2 = a2+1+len-a1;
2846 if (a1+a2 > (int64_t)len)
2847 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002848
H. Peter Anvine2c80182005-01-15 22:15:51 +00002849 macro_start = nasm_malloc(sizeof(*macro_start));
2850 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002851 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002852 macro_start->type = TOK_STRING;
2853 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002854
H. Peter Anvine2c80182005-01-15 22:15:51 +00002855 /*
2856 * We now have a macro name, an implicit parameter count of
2857 * zero, and a numeric token to use as an expansion. Create
2858 * and store an SMacro.
2859 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002860 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002861 free_tlist(tline);
2862 free_tlist(origline);
2863 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002864 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002865
H. Peter Anvine2c80182005-01-15 22:15:51 +00002866 case PP_ASSIGN:
2867 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002868 casesense = (i == PP_ASSIGN);
2869
H. Peter Anvine2c80182005-01-15 22:15:51 +00002870 tline = tline->next;
2871 skip_white_(tline);
2872 tline = expand_id(tline);
2873 if (!tline || (tline->type != TOK_ID &&
2874 (tline->type != TOK_PREPROC_ID ||
2875 tline->text[1] != '$'))) {
2876 error(ERR_NONFATAL,
2877 "`%%%sassign' expects a macro identifier",
2878 (i == PP_IASSIGN ? "i" : ""));
2879 free_tlist(origline);
2880 return DIRECTIVE_FOUND;
2881 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002882 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002883
H. Peter Anvine2c80182005-01-15 22:15:51 +00002884 mname = tline->text;
2885 last = tline;
2886 tline = expand_smacro(tline->next);
2887 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002888
H. Peter Anvine2c80182005-01-15 22:15:51 +00002889 t = tline;
2890 tptr = &t;
2891 tokval.t_type = TOKEN_INVALID;
2892 evalresult =
2893 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2894 free_tlist(tline);
2895 if (!evalresult) {
2896 free_tlist(origline);
2897 return DIRECTIVE_FOUND;
2898 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002899
H. Peter Anvine2c80182005-01-15 22:15:51 +00002900 if (tokval.t_type)
2901 error(ERR_WARNING,
2902 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00002903
H. Peter Anvine2c80182005-01-15 22:15:51 +00002904 if (!is_simple(evalresult)) {
2905 error(ERR_NONFATAL,
2906 "non-constant value given to `%%%sassign'",
2907 (i == PP_IASSIGN ? "i" : ""));
2908 free_tlist(origline);
2909 return DIRECTIVE_FOUND;
2910 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002911
H. Peter Anvine2c80182005-01-15 22:15:51 +00002912 macro_start = nasm_malloc(sizeof(*macro_start));
2913 macro_start->next = NULL;
2914 make_tok_num(macro_start, reloc_value(evalresult));
2915 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002916
H. Peter Anvine2c80182005-01-15 22:15:51 +00002917 /*
2918 * We now have a macro name, an implicit parameter count of
2919 * zero, and a numeric token to use as an expansion. Create
2920 * and store an SMacro.
2921 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002922 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002923 free_tlist(origline);
2924 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002925
H. Peter Anvine2c80182005-01-15 22:15:51 +00002926 case PP_LINE:
2927 /*
2928 * Syntax is `%line nnn[+mmm] [filename]'
2929 */
2930 tline = tline->next;
2931 skip_white_(tline);
2932 if (!tok_type_(tline, TOK_NUMBER)) {
2933 error(ERR_NONFATAL, "`%%line' expects line number");
2934 free_tlist(origline);
2935 return DIRECTIVE_FOUND;
2936 }
H. Peter Anvin70055962007-10-11 00:05:31 -07002937 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002938 m = 1;
2939 tline = tline->next;
2940 if (tok_is_(tline, "+")) {
2941 tline = tline->next;
2942 if (!tok_type_(tline, TOK_NUMBER)) {
2943 error(ERR_NONFATAL, "`%%line' expects line increment");
2944 free_tlist(origline);
2945 return DIRECTIVE_FOUND;
2946 }
H. Peter Anvin70055962007-10-11 00:05:31 -07002947 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002948 tline = tline->next;
2949 }
2950 skip_white_(tline);
2951 src_set_linnum(k);
2952 istk->lineinc = m;
2953 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002954 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002955 }
2956 free_tlist(origline);
2957 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002958
H. Peter Anvine2c80182005-01-15 22:15:51 +00002959 default:
2960 error(ERR_FATAL,
2961 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00002962 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002963 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002964 }
Ed Beroset3ab3f412002-06-11 03:31:49 +00002965 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002966}
2967
2968/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002969 * Ensure that a macro parameter contains a condition code and
2970 * nothing else. Return the condition code index if so, or -1
2971 * otherwise.
2972 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002973static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002974{
H. Peter Anvin76690a12002-04-30 20:52:49 +00002975 Token *tt;
2976 int i, j, k, m;
2977
H. Peter Anvin25a99342007-09-22 17:45:45 -07002978 if (!t)
2979 return -1; /* Probably a %+ without a space */
2980
H. Peter Anvineba20a72002-04-30 20:53:55 +00002981 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002982 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002983 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002984 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002985 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002986 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002987 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002988
2989 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00002990 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002991 while (j - i > 1) {
2992 k = (j + i) / 2;
2993 m = nasm_stricmp(t->text, conditions[k]);
2994 if (m == 0) {
2995 i = k;
2996 j = -2;
2997 break;
2998 } else if (m < 0) {
2999 j = k;
3000 } else
3001 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003002 }
3003 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003004 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003005 return i;
3006}
3007
3008/*
3009 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3010 * %-n) and MMacro-local identifiers (%%foo).
3011 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003012static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003013{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003014 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003015
3016 tail = &thead;
3017 thead = NULL;
3018
H. Peter Anvine2c80182005-01-15 22:15:51 +00003019 while (tline) {
3020 if (tline->type == TOK_PREPROC_ID &&
3021 (((tline->text[1] == '+' || tline->text[1] == '-')
3022 && tline->text[2]) || tline->text[1] == '%'
3023 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003024 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003025 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003026 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003027 unsigned int n;
3028 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003029 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003030
H. Peter Anvine2c80182005-01-15 22:15:51 +00003031 t = tline;
3032 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003033
H. Peter Anvine2c80182005-01-15 22:15:51 +00003034 mac = istk->mstk;
3035 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3036 mac = mac->next_active;
3037 if (!mac)
3038 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3039 else
3040 switch (t->text[1]) {
3041 /*
3042 * We have to make a substitution of one of the
3043 * forms %1, %-1, %+1, %%foo, %0.
3044 */
3045 case '0':
3046 type = TOK_NUMBER;
3047 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3048 text = nasm_strdup(tmpbuf);
3049 break;
3050 case '%':
3051 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003052 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00003053 mac->unique);
3054 text = nasm_strcat(tmpbuf, t->text + 2);
3055 break;
3056 case '-':
3057 n = atoi(t->text + 2) - 1;
3058 if (n >= mac->nparam)
3059 tt = NULL;
3060 else {
3061 if (mac->nparam > 1)
3062 n = (n + mac->rotate) % mac->nparam;
3063 tt = mac->params[n];
3064 }
3065 cc = find_cc(tt);
3066 if (cc == -1) {
3067 error(ERR_NONFATAL,
3068 "macro parameter %d is not a condition code",
3069 n + 1);
3070 text = NULL;
3071 } else {
3072 type = TOK_ID;
3073 if (inverse_ccs[cc] == -1) {
3074 error(ERR_NONFATAL,
3075 "condition code `%s' is not invertible",
3076 conditions[cc]);
3077 text = NULL;
3078 } else
3079 text =
3080 nasm_strdup(conditions[inverse_ccs[cc]]);
3081 }
3082 break;
3083 case '+':
3084 n = atoi(t->text + 2) - 1;
3085 if (n >= mac->nparam)
3086 tt = NULL;
3087 else {
3088 if (mac->nparam > 1)
3089 n = (n + mac->rotate) % mac->nparam;
3090 tt = mac->params[n];
3091 }
3092 cc = find_cc(tt);
3093 if (cc == -1) {
3094 error(ERR_NONFATAL,
3095 "macro parameter %d is not a condition code",
3096 n + 1);
3097 text = NULL;
3098 } else {
3099 type = TOK_ID;
3100 text = nasm_strdup(conditions[cc]);
3101 }
3102 break;
3103 default:
3104 n = atoi(t->text + 1) - 1;
3105 if (n >= mac->nparam)
3106 tt = NULL;
3107 else {
3108 if (mac->nparam > 1)
3109 n = (n + mac->rotate) % mac->nparam;
3110 tt = mac->params[n];
3111 }
3112 if (tt) {
3113 for (i = 0; i < mac->paramlen[n]; i++) {
3114 *tail = new_Token(NULL, tt->type, tt->text, 0);
3115 tail = &(*tail)->next;
3116 tt = tt->next;
3117 }
3118 }
3119 text = NULL; /* we've done it here */
3120 break;
3121 }
3122 if (!text) {
3123 delete_Token(t);
3124 } else {
3125 *tail = t;
3126 tail = &t->next;
3127 t->type = type;
3128 nasm_free(t->text);
3129 t->text = text;
3130 t->mac = NULL;
3131 }
3132 continue;
3133 } else {
3134 t = *tail = tline;
3135 tline = tline->next;
3136 t->mac = NULL;
3137 tail = &t->next;
3138 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003139 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003140 *tail = NULL;
3141 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003142 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003143 switch (t->type) {
3144 case TOK_WHITESPACE:
3145 if (tt->type == TOK_WHITESPACE) {
3146 t->next = delete_Token(tt);
3147 }
3148 break;
3149 case TOK_ID:
3150 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003151 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003152 nasm_free(t->text);
3153 t->text = tmp;
3154 t->next = delete_Token(tt);
3155 }
3156 break;
3157 case TOK_NUMBER:
3158 if (tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003159 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003160 nasm_free(t->text);
3161 t->text = tmp;
3162 t->next = delete_Token(tt);
3163 }
3164 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003165 default:
3166 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003167 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003168
H. Peter Anvin76690a12002-04-30 20:52:49 +00003169 return thead;
3170}
3171
3172/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003173 * Expand all single-line macro calls made in the given line.
3174 * Return the expanded version of the line. The original is deemed
3175 * to be destroyed in the process. (In reality we'll just move
3176 * Tokens from input to output a lot of the time, rather than
3177 * actually bothering to destroy and replicate.)
3178 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003179#define DEADMAN_LIMIT (1 << 20)
3180
H. Peter Anvine2c80182005-01-15 22:15:51 +00003181static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003182{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003183 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003184 struct hash_table *smtbl;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003185 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003186 Token **params;
3187 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003188 unsigned int nparam, sparam;
3189 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003190 Token *org_tline = tline;
3191 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003192 char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003193 int deadman = DEADMAN_LIMIT;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003194
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003195 /*
3196 * Trick: we should avoid changing the start token pointer since it can
3197 * be contained in "next" field of other token. Because of this
3198 * we allocate a copy of first token and work with it; at the end of
3199 * routine we copy it back
3200 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003201 if (org_tline) {
3202 tline =
3203 new_Token(org_tline->next, org_tline->type, org_tline->text,
3204 0);
3205 tline->mac = org_tline->mac;
3206 nasm_free(org_tline->text);
3207 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003208 }
3209
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003210again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003211 tail = &thead;
3212 thead = NULL;
3213
H. Peter Anvine2c80182005-01-15 22:15:51 +00003214 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003215 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003216 error(ERR_NONFATAL, "interminable macro recursion");
3217 break;
3218 }
3219
H. Peter Anvine2c80182005-01-15 22:15:51 +00003220 if ((mname = tline->text)) {
3221 /* if this token is a local macro, look in local context */
H. Peter Anvinb037a672008-05-29 19:08:08 -07003222 ctx = NULL;
3223 smtbl = &smacros;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003224 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003225 ctx = get_ctx(mname, true);
H. Peter Anvinb037a672008-05-29 19:08:08 -07003226 if (ctx)
3227 smtbl = &ctx->localmac;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003228 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07003229 head = (SMacro *) hash_findix(smtbl, mname);
H. Peter Anvin072771e2008-05-22 13:17:51 -07003230
H. Peter Anvine2c80182005-01-15 22:15:51 +00003231 /*
3232 * We've hit an identifier. As in is_mmacro below, we first
3233 * check whether the identifier is a single-line macro at
3234 * all, then think about checking for parameters if
3235 * necessary.
3236 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003237 for (m = head; m; m = m->next)
3238 if (!mstrcmp(m->name, mname, m->casesense))
3239 break;
3240 if (m) {
3241 mstart = tline;
3242 params = NULL;
3243 paramsize = NULL;
3244 if (m->nparam == 0) {
3245 /*
3246 * Simple case: the macro is parameterless. Discard the
3247 * one token that the macro call took, and push the
3248 * expansion back on the to-do stack.
3249 */
3250 if (!m->expansion) {
3251 if (!strcmp("__FILE__", m->name)) {
3252 int32_t num = 0;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003253 char *file;
3254 src_get(&num, &file);
3255 tline->text = nasm_quote(file, strlen(file));
H. Peter Anvin97a23472007-09-16 17:57:25 -07003256 tline->type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003257 nasm_free(file);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003258 continue;
3259 }
3260 if (!strcmp("__LINE__", m->name)) {
3261 nasm_free(tline->text);
3262 make_tok_num(tline, src_get_linnum());
3263 continue;
3264 }
3265 if (!strcmp("__BITS__", m->name)) {
3266 nasm_free(tline->text);
3267 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003268 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003269 }
3270 tline = delete_Token(tline);
3271 continue;
3272 }
3273 } else {
3274 /*
3275 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003276 * exists and takes parameters. We must find the
3277 * parameters in the call, count them, find the SMacro
3278 * that corresponds to that form of the macro call, and
3279 * substitute for the parameters when we expand. What a
3280 * pain.
3281 */
3282 /*tline = tline->next;
3283 skip_white_(tline); */
3284 do {
3285 t = tline->next;
3286 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003287 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003288 t->text = NULL;
3289 t = tline->next = delete_Token(t);
3290 }
3291 tline = t;
3292 } while (tok_type_(tline, TOK_WHITESPACE));
3293 if (!tok_is_(tline, "(")) {
3294 /*
3295 * This macro wasn't called with parameters: ignore
3296 * the call. (Behaviour borrowed from gnu cpp.)
3297 */
3298 tline = mstart;
3299 m = NULL;
3300 } else {
3301 int paren = 0;
3302 int white = 0;
3303 brackets = 0;
3304 nparam = 0;
3305 sparam = PARAM_DELTA;
3306 params = nasm_malloc(sparam * sizeof(Token *));
3307 params[0] = tline->next;
3308 paramsize = nasm_malloc(sparam * sizeof(int));
3309 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003310 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003311 /*
3312 * For some unusual expansions
3313 * which concatenates function call
3314 */
3315 t = tline->next;
3316 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003317 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003318 t->text = NULL;
3319 t = tline->next = delete_Token(t);
3320 }
3321 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003322
H. Peter Anvine2c80182005-01-15 22:15:51 +00003323 if (!tline) {
3324 error(ERR_NONFATAL,
3325 "macro call expects terminating `)'");
3326 break;
3327 }
3328 if (tline->type == TOK_WHITESPACE
3329 && brackets <= 0) {
3330 if (paramsize[nparam])
3331 white++;
3332 else
3333 params[nparam] = tline->next;
3334 continue; /* parameter loop */
3335 }
3336 if (tline->type == TOK_OTHER
3337 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003338 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003339 if (ch == ',' && !paren && brackets <= 0) {
3340 if (++nparam >= sparam) {
3341 sparam += PARAM_DELTA;
3342 params = nasm_realloc(params,
3343 sparam *
3344 sizeof(Token
3345 *));
3346 paramsize =
3347 nasm_realloc(paramsize,
3348 sparam *
3349 sizeof(int));
3350 }
3351 params[nparam] = tline->next;
3352 paramsize[nparam] = 0;
3353 white = 0;
3354 continue; /* parameter loop */
3355 }
3356 if (ch == '{' &&
3357 (brackets > 0 || (brackets == 0 &&
3358 !paramsize[nparam])))
3359 {
3360 if (!(brackets++)) {
3361 params[nparam] = tline->next;
3362 continue; /* parameter loop */
3363 }
3364 }
3365 if (ch == '}' && brackets > 0)
3366 if (--brackets == 0) {
3367 brackets = -1;
3368 continue; /* parameter loop */
3369 }
3370 if (ch == '(' && !brackets)
3371 paren++;
3372 if (ch == ')' && brackets <= 0)
3373 if (--paren < 0)
3374 break;
3375 }
3376 if (brackets < 0) {
3377 brackets = 0;
3378 error(ERR_NONFATAL, "braces do not "
3379 "enclose all of macro parameter");
3380 }
3381 paramsize[nparam] += white + 1;
3382 white = 0;
3383 } /* parameter loop */
3384 nparam++;
3385 while (m && (m->nparam != nparam ||
3386 mstrcmp(m->name, mname,
3387 m->casesense)))
3388 m = m->next;
3389 if (!m)
3390 error(ERR_WARNING | ERR_WARN_MNP,
3391 "macro `%s' exists, "
3392 "but not taking %d parameters",
3393 mstart->text, nparam);
3394 }
3395 }
3396 if (m && m->in_progress)
3397 m = NULL;
3398 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003399 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003400 * Design question: should we handle !tline, which
3401 * indicates missing ')' here, or expand those
3402 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003403 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003404 */
3405 nasm_free(params);
3406 nasm_free(paramsize);
3407 tline = mstart;
3408 } else {
3409 /*
3410 * Expand the macro: we are placed on the last token of the
3411 * call, so that we can easily split the call from the
3412 * following tokens. We also start by pushing an SMAC_END
3413 * token for the cycle removal.
3414 */
3415 t = tline;
3416 if (t) {
3417 tline = t->next;
3418 t->next = NULL;
3419 }
3420 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3421 tt->mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003422 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003423 tline = tt;
3424 for (t = m->expansion; t; t = t->next) {
3425 if (t->type >= TOK_SMAC_PARAM) {
3426 Token *pcopy = tline, **ptail = &pcopy;
3427 Token *ttt, *pt;
3428 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003429
H. Peter Anvine2c80182005-01-15 22:15:51 +00003430 ttt = params[t->type - TOK_SMAC_PARAM];
3431 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3432 --i >= 0;) {
3433 pt = *ptail =
3434 new_Token(tline, ttt->type, ttt->text,
3435 0);
3436 ptail = &pt->next;
3437 ttt = ttt->next;
3438 }
3439 tline = pcopy;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -07003440 } else if (t->type == TOK_PREPROC_Q) {
3441 tt = new_Token(tline, TOK_ID, mname, 0);
3442 tline = tt;
3443 } else if (t->type == TOK_PREPROC_QQ) {
3444 tt = new_Token(tline, TOK_ID, m->name, 0);
3445 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003446 } else {
3447 tt = new_Token(tline, t->type, t->text, 0);
3448 tline = tt;
3449 }
3450 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003451
H. Peter Anvine2c80182005-01-15 22:15:51 +00003452 /*
3453 * Having done that, get rid of the macro call, and clean
3454 * up the parameters.
3455 */
3456 nasm_free(params);
3457 nasm_free(paramsize);
3458 free_tlist(mstart);
3459 continue; /* main token loop */
3460 }
3461 }
3462 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003463
H. Peter Anvine2c80182005-01-15 22:15:51 +00003464 if (tline->type == TOK_SMAC_END) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003465 tline->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003466 tline = delete_Token(tline);
3467 } else {
3468 t = *tail = tline;
3469 tline = tline->next;
3470 t->mac = NULL;
3471 t->next = NULL;
3472 tail = &t->next;
3473 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003474 }
3475
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003476 /*
3477 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003478 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003479 * TOK_IDs should be concatenated.
3480 * Also we look for %+ tokens and concatenate the tokens before and after
3481 * them (without white spaces in between).
3482 */
3483 t = thead;
3484 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003485 while (t) {
3486 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3487 t = t->next;
3488 if (!t || !t->next)
3489 break;
3490 if (t->next->type == TOK_ID ||
3491 t->next->type == TOK_PREPROC_ID ||
3492 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003493 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003494 nasm_free(t->text);
3495 t->next = delete_Token(t->next);
3496 t->text = p;
3497 rescan = 1;
3498 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3499 t->next->next->type == TOK_PREPROC_ID &&
3500 strcmp(t->next->next->text, "%+") == 0) {
3501 /* free the next whitespace, the %+ token and next whitespace */
3502 int i;
3503 for (i = 1; i <= 3; i++) {
3504 if (!t->next
3505 || (i != 2 && t->next->type != TOK_WHITESPACE))
3506 break;
3507 t->next = delete_Token(t->next);
3508 } /* endfor */
3509 } else
3510 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003511 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003512 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003513 if (rescan) {
3514 tline = thead;
3515 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003516 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003517
H. Peter Anvine2c80182005-01-15 22:15:51 +00003518 if (org_tline) {
3519 if (thead) {
3520 *org_tline = *thead;
3521 /* since we just gave text to org_line, don't free it */
3522 thead->text = NULL;
3523 delete_Token(thead);
3524 } else {
3525 /* the expression expanded to empty line;
3526 we can't return NULL for some reasons
3527 we just set the line to a single WHITESPACE token. */
3528 memset(org_tline, 0, sizeof(*org_tline));
3529 org_tline->text = NULL;
3530 org_tline->type = TOK_WHITESPACE;
3531 }
3532 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003533 }
3534
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003535 return thead;
3536}
3537
3538/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003539 * Similar to expand_smacro but used exclusively with macro identifiers
3540 * right before they are fetched in. The reason is that there can be
3541 * identifiers consisting of several subparts. We consider that if there
3542 * are more than one element forming the name, user wants a expansion,
3543 * otherwise it will be left as-is. Example:
3544 *
3545 * %define %$abc cde
3546 *
3547 * the identifier %$abc will be left as-is so that the handler for %define
3548 * will suck it and define the corresponding value. Other case:
3549 *
3550 * %define _%$abc cde
3551 *
3552 * In this case user wants name to be expanded *before* %define starts
3553 * working, so we'll expand %$abc into something (if it has a value;
3554 * otherwise it will be left as-is) then concatenate all successive
3555 * PP_IDs into one.
3556 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003557static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003558{
3559 Token *cur, *oldnext = NULL;
3560
H. Peter Anvin734b1882002-04-30 21:01:08 +00003561 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003562 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003563
3564 cur = tline;
3565 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003566 (cur->next->type == TOK_ID ||
3567 cur->next->type == TOK_PREPROC_ID
3568 || cur->next->type == TOK_NUMBER))
3569 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003570
3571 /* If identifier consists of just one token, don't expand */
3572 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003573 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003574
H. Peter Anvine2c80182005-01-15 22:15:51 +00003575 if (cur) {
3576 oldnext = cur->next; /* Detach the tail past identifier */
3577 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003578 }
3579
H. Peter Anvin734b1882002-04-30 21:01:08 +00003580 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003581
H. Peter Anvine2c80182005-01-15 22:15:51 +00003582 if (cur) {
3583 /* expand_smacro possibly changhed tline; re-scan for EOL */
3584 cur = tline;
3585 while (cur && cur->next)
3586 cur = cur->next;
3587 if (cur)
3588 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003589 }
3590
3591 return tline;
3592}
3593
3594/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003595 * Determine whether the given line constitutes a multi-line macro
3596 * call, and return the MMacro structure called if so. Doesn't have
3597 * to check for an initial label - that's taken care of in
3598 * expand_mmacro - but must check numbers of parameters. Guaranteed
3599 * to be called with tline->type == TOK_ID, so the putative macro
3600 * name is easy to find.
3601 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003602static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003603{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003604 MMacro *head, *m;
3605 Token **params;
3606 int nparam;
3607
H. Peter Anvin166c2472008-05-28 12:28:58 -07003608 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003609
3610 /*
3611 * Efficiency: first we see if any macro exists with the given
3612 * name. If not, we can return NULL immediately. _Then_ we
3613 * count the parameters, and then we look further along the
3614 * list if necessary to find the proper MMacro.
3615 */
3616 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003617 if (!mstrcmp(m->name, tline->text, m->casesense))
3618 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003619 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003620 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003621
3622 /*
3623 * OK, we have a potential macro. Count and demarcate the
3624 * parameters.
3625 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003626 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003627
3628 /*
3629 * So we know how many parameters we've got. Find the MMacro
3630 * structure that handles this number.
3631 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003632 while (m) {
3633 if (m->nparam_min <= nparam
3634 && (m->plus || nparam <= m->nparam_max)) {
3635 /*
3636 * This one is right. Just check if cycle removal
3637 * prohibits us using it before we actually celebrate...
3638 */
3639 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003640#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003641 error(ERR_NONFATAL,
3642 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003643#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003644 nasm_free(params);
3645 return NULL;
3646 }
3647 /*
3648 * It's right, and we can use it. Add its default
3649 * parameters to the end of our list if necessary.
3650 */
3651 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3652 params =
3653 nasm_realloc(params,
3654 ((m->nparam_min + m->ndefs +
3655 1) * sizeof(*params)));
3656 while (nparam < m->nparam_min + m->ndefs) {
3657 params[nparam] = m->defaults[nparam - m->nparam_min];
3658 nparam++;
3659 }
3660 }
3661 /*
3662 * If we've gone over the maximum parameter count (and
3663 * we're in Plus mode), ignore parameters beyond
3664 * nparam_max.
3665 */
3666 if (m->plus && nparam > m->nparam_max)
3667 nparam = m->nparam_max;
3668 /*
3669 * Then terminate the parameter list, and leave.
3670 */
3671 if (!params) { /* need this special case */
3672 params = nasm_malloc(sizeof(*params));
3673 nparam = 0;
3674 }
3675 params[nparam] = NULL;
3676 *params_array = params;
3677 return m;
3678 }
3679 /*
3680 * This one wasn't right: look for the next one with the
3681 * same name.
3682 */
3683 for (m = m->next; m; m = m->next)
3684 if (!mstrcmp(m->name, tline->text, m->casesense))
3685 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003686 }
3687
3688 /*
3689 * After all that, we didn't find one with the right number of
3690 * parameters. Issue a warning, and fail to expand the macro.
3691 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003692 error(ERR_WARNING | ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003693 "macro `%s' exists, but not taking %d parameters",
3694 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003695 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003696 return NULL;
3697}
3698
3699/*
3700 * Expand the multi-line macro call made by the given line, if
3701 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003702 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003703 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003704static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003705{
3706 Token *startline = tline;
3707 Token *label = NULL;
3708 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003709 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003710 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003711 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003712 int i, nparam, *paramlen;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003713
3714 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003715 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07003716 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003717 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003718 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003719 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003720 m = is_mmacro(t, &params);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003721 if (!m) {
3722 Token *last;
3723 /*
3724 * We have an id which isn't a macro call. We'll assume
3725 * it might be a label; we'll also check to see if a
3726 * colon follows it. Then, if there's another id after
3727 * that lot, we'll check it again for macro-hood.
3728 */
3729 label = last = t;
3730 t = t->next;
3731 if (tok_type_(t, TOK_WHITESPACE))
3732 last = t, t = t->next;
3733 if (tok_is_(t, ":")) {
3734 dont_prepend = 1;
3735 last = t, t = t->next;
3736 if (tok_type_(t, TOK_WHITESPACE))
3737 last = t, t = t->next;
3738 }
3739 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3740 return 0;
3741 last->next = NULL;
3742 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003743 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003744
3745 /*
3746 * Fix up the parameters: this involves stripping leading and
3747 * trailing whitespace, then stripping braces if they are
3748 * present.
3749 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003750 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003751 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003752
H. Peter Anvine2c80182005-01-15 22:15:51 +00003753 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003754 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003755 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003756
H. Peter Anvine2c80182005-01-15 22:15:51 +00003757 t = params[i];
3758 skip_white_(t);
3759 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003760 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003761 params[i] = t;
3762 paramlen[i] = 0;
3763 while (t) {
3764 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3765 break; /* ... because we have hit a comma */
3766 if (comma && t->type == TOK_WHITESPACE
3767 && tok_is_(t->next, ","))
3768 break; /* ... or a space then a comma */
3769 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3770 break; /* ... or a brace */
3771 t = t->next;
3772 paramlen[i]++;
3773 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003774 }
3775
3776 /*
3777 * OK, we have a MMacro structure together with a set of
3778 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003779 * copies of each Line on to istk->expansion. Substitution of
3780 * parameter tokens and macro-local tokens doesn't get done
3781 * until the single-line macro substitution process; this is
3782 * because delaying them allows us to change the semantics
3783 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003784 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003785 * First, push an end marker on to istk->expansion, mark this
3786 * macro as in progress, and set up its invocation-specific
3787 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003788 */
3789 ll = nasm_malloc(sizeof(Line));
3790 ll->next = istk->expansion;
3791 ll->finishes = m;
3792 ll->first = NULL;
3793 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003794
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003795 m->in_progress = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003796 m->params = params;
3797 m->iline = tline;
3798 m->nparam = nparam;
3799 m->rotate = 0;
3800 m->paramlen = paramlen;
3801 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003802 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003803
3804 m->next_active = istk->mstk;
3805 istk->mstk = m;
3806
H. Peter Anvine2c80182005-01-15 22:15:51 +00003807 for (l = m->expansion; l; l = l->next) {
3808 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003809
H. Peter Anvine2c80182005-01-15 22:15:51 +00003810 ll = nasm_malloc(sizeof(Line));
3811 ll->finishes = NULL;
3812 ll->next = istk->expansion;
3813 istk->expansion = ll;
3814 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003815
H. Peter Anvine2c80182005-01-15 22:15:51 +00003816 for (t = l->first; t; t = t->next) {
3817 Token *x = t;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003818 switch (t->type) {
3819 case TOK_PREPROC_Q:
3820 tt = *tail = new_Token(NULL, TOK_ID, mtok->text, 0);
3821 break;
3822 case TOK_PREPROC_QQ:
3823 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
3824 break;
3825 case TOK_PREPROC_ID:
3826 if (t->text[1] == '0' && t->text[2] == '0') {
3827 dont_prepend = -1;
3828 x = label;
3829 if (!x)
3830 continue;
3831 }
3832 /* fall through */
3833 default:
3834 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3835 break;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003836 }
H. Peter Anvince2233b2008-05-25 21:57:00 -07003837 tail = &tt->next;
3838 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003839 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003840 }
3841
3842 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00003843 * If we had a label, push it on as the first line of
3844 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003845 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003846 if (label) {
3847 if (dont_prepend < 0)
3848 free_tlist(startline);
3849 else {
3850 ll = nasm_malloc(sizeof(Line));
3851 ll->finishes = NULL;
3852 ll->next = istk->expansion;
3853 istk->expansion = ll;
3854 ll->first = startline;
3855 if (!dont_prepend) {
3856 while (label->next)
3857 label = label->next;
3858 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3859 }
3860 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003861 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003862
H. Peter Anvin734b1882002-04-30 21:01:08 +00003863 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003864
H. Peter Anvineba20a72002-04-30 20:53:55 +00003865 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003866}
3867
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003868/*
3869 * Since preprocessor always operate only on the line that didn't
3870 * arrived yet, we should always use ERR_OFFBY1. Also since user
3871 * won't want to see same error twice (preprocessing is done once
3872 * per pass) we will want to show errors only during pass one.
3873 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003874static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003875{
3876 va_list arg;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003877 char buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003878
3879 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003880 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003881 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003882
H. Peter Anvin734b1882002-04-30 21:01:08 +00003883 va_start(arg, fmt);
Ed Beroset19f927a2004-12-15 17:07:03 +00003884 vsnprintf(buff, sizeof(buff), fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003885 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003886
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003887 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003888 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3889 istk->mstk->lineno, buff);
3890 else
3891 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003892}
3893
H. Peter Anvin734b1882002-04-30 21:01:08 +00003894static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003895pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07003896 ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003897{
H. Peter Anvin99941bf2002-05-14 17:44:03 +00003898 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003899 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003900 istk = nasm_malloc(sizeof(Include));
3901 istk->next = NULL;
3902 istk->conds = NULL;
3903 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003904 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003905 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00003906 istk->fname = NULL;
3907 src_set_fname(nasm_strdup(file));
3908 src_set_linnum(0);
3909 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003910 if (!istk->fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003911 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3912 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003913 defining = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003914 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003915 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003916 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07003917 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003918 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07003919 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003920 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003921 any_extrastdmac = (extrastdmac != NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003922 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003923 evaluate = eval;
3924 pass = apass;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07003925 dephead = deptail = deplist;
3926 if (deplist) {
3927 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
3928 sl->next = NULL;
3929 strcpy(sl->str, file);
3930 *deptail = sl;
3931 deptail = &sl->next;
3932 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003933}
3934
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003935static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003936{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003937 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003938 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003939
H. Peter Anvine2c80182005-01-15 22:15:51 +00003940 while (1) {
3941 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003942 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00003943 * buffer or from the input file.
3944 */
3945 tline = NULL;
3946 while (istk->expansion && istk->expansion->finishes) {
3947 Line *l = istk->expansion;
3948 if (!l->finishes->name && l->finishes->in_progress > 1) {
3949 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003950
H. Peter Anvine2c80182005-01-15 22:15:51 +00003951 /*
3952 * This is a macro-end marker for a macro with no
3953 * name, which means it's not really a macro at all
3954 * but a %rep block, and the `in_progress' field is
3955 * more than 1, meaning that we still need to
3956 * repeat. (1 means the natural last repetition; 0
3957 * means termination by %exitrep.) We have
3958 * therefore expanded up to the %endrep, and must
3959 * push the whole block on to the expansion buffer
3960 * again. We don't bother to remove the macro-end
3961 * marker: we'd only have to generate another one
3962 * if we did.
3963 */
3964 l->finishes->in_progress--;
3965 for (l = l->finishes->expansion; l; l = l->next) {
3966 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003967
H. Peter Anvine2c80182005-01-15 22:15:51 +00003968 ll = nasm_malloc(sizeof(Line));
3969 ll->next = istk->expansion;
3970 ll->finishes = NULL;
3971 ll->first = NULL;
3972 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003973
H. Peter Anvine2c80182005-01-15 22:15:51 +00003974 for (t = l->first; t; t = t->next) {
3975 if (t->text || t->type == TOK_WHITESPACE) {
3976 tt = *tail =
3977 new_Token(NULL, t->type, t->text, 0);
3978 tail = &tt->next;
3979 }
3980 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003981
H. Peter Anvine2c80182005-01-15 22:15:51 +00003982 istk->expansion = ll;
3983 }
3984 } else {
3985 /*
3986 * Check whether a `%rep' was started and not ended
3987 * within this macro expansion. This can happen and
3988 * should be detected. It's a fatal error because
3989 * I'm too confused to work out how to recover
3990 * sensibly from it.
3991 */
3992 if (defining) {
3993 if (defining->name)
3994 error(ERR_PANIC,
3995 "defining with name in expansion");
3996 else if (istk->mstk->name)
3997 error(ERR_FATAL,
3998 "`%%rep' without `%%endrep' within"
3999 " expansion of macro `%s'",
4000 istk->mstk->name);
4001 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004002
H. Peter Anvine2c80182005-01-15 22:15:51 +00004003 /*
4004 * FIXME: investigate the relationship at this point between
4005 * istk->mstk and l->finishes
4006 */
4007 {
4008 MMacro *m = istk->mstk;
4009 istk->mstk = m->next_active;
4010 if (m->name) {
4011 /*
4012 * This was a real macro call, not a %rep, and
4013 * therefore the parameter information needs to
4014 * be freed.
4015 */
4016 nasm_free(m->params);
4017 free_tlist(m->iline);
4018 nasm_free(m->paramlen);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004019 l->finishes->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004020 } else
4021 free_mmacro(m);
4022 }
4023 istk->expansion = l->next;
4024 nasm_free(l);
4025 list->downlevel(LIST_MACRO);
4026 }
4027 }
4028 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004029
H. Peter Anvine2c80182005-01-15 22:15:51 +00004030 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004031 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004032 Line *l = istk->expansion;
4033 if (istk->mstk)
4034 istk->mstk->lineno++;
4035 tline = l->first;
4036 istk->expansion = l->next;
4037 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004038 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004039 list->line(LIST_MACRO, p);
4040 nasm_free(p);
4041 break;
4042 }
4043 line = read_line();
4044 if (line) { /* from the current input file */
4045 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004046 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004047 nasm_free(line);
4048 break;
4049 }
4050 /*
4051 * The current file has ended; work down the istk
4052 */
4053 {
4054 Include *i = istk;
4055 fclose(i->fp);
4056 if (i->conds)
4057 error(ERR_FATAL,
4058 "expected `%%endif' before end of file");
4059 /* only set line and file name if there's a next node */
4060 if (i->next) {
4061 src_set_linnum(i->lineno);
4062 nasm_free(src_set_fname(i->fname));
4063 }
4064 istk = i->next;
4065 list->downlevel(LIST_INCLUDE);
4066 nasm_free(i);
4067 if (!istk)
4068 return NULL;
4069 }
4070 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004071
H. Peter Anvine2c80182005-01-15 22:15:51 +00004072 /*
4073 * We must expand MMacro parameters and MMacro-local labels
4074 * _before_ we plunge into directive processing, to cope
4075 * with things like `%define something %1' such as STRUC
4076 * uses. Unless we're _defining_ a MMacro, in which case
4077 * those tokens should be left alone to go into the
4078 * definition; and unless we're in a non-emitting
4079 * condition, in which case we don't want to meddle with
4080 * anything.
4081 */
4082 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
4083 tline = expand_mmac_params(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004084
H. Peter Anvine2c80182005-01-15 22:15:51 +00004085 /*
4086 * Check the line to see if it's a preprocessor directive.
4087 */
4088 if (do_directive(tline) == DIRECTIVE_FOUND) {
4089 continue;
4090 } else if (defining) {
4091 /*
4092 * We're defining a multi-line macro. We emit nothing
4093 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004094 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004095 */
4096 Line *l = nasm_malloc(sizeof(Line));
4097 l->next = defining->expansion;
4098 l->first = tline;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004099 l->finishes = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004100 defining->expansion = l;
4101 continue;
4102 } else if (istk->conds && !emitting(istk->conds->state)) {
4103 /*
4104 * We're in a non-emitting branch of a condition block.
4105 * Emit nothing at all, not even a blank line: when we
4106 * emerge from the condition we'll give a line-number
4107 * directive so we keep our place correctly.
4108 */
4109 free_tlist(tline);
4110 continue;
4111 } else if (istk->mstk && !istk->mstk->in_progress) {
4112 /*
4113 * We're in a %rep block which has been terminated, so
4114 * we're walking through to the %endrep without
4115 * emitting anything. Emit nothing at all, not even a
4116 * blank line: when we emerge from the %rep block we'll
4117 * give a line-number directive so we keep our place
4118 * correctly.
4119 */
4120 free_tlist(tline);
4121 continue;
4122 } else {
4123 tline = expand_smacro(tline);
4124 if (!expand_mmacro(tline)) {
4125 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004126 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004127 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004128 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004129 free_tlist(tline);
4130 break;
4131 } else {
4132 continue; /* expand_mmacro calls free_tlist */
4133 }
4134 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004135 }
4136
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004137 return line;
4138}
4139
H. Peter Anvine2c80182005-01-15 22:15:51 +00004140static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004141{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004142 if (defining) {
4143 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
4144 defining->name);
4145 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004146 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004147 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004148 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004149 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004150 while (istk) {
4151 Include *i = istk;
4152 istk = istk->next;
4153 fclose(i->fp);
4154 nasm_free(i->fname);
4155 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004156 }
4157 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004158 ctx_pop();
4159 if (pass == 0) {
4160 free_llist(predef);
4161 delete_Blocks();
4162 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004163}
4164
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004165void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004166{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004167 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004168
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004169 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004170 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004171 i->next = NULL;
4172
H. Peter Anvine2c80182005-01-15 22:15:51 +00004173 if (ipath != NULL) {
4174 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004175 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004176 j = j->next;
4177 j->next = i;
4178 } else {
4179 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004180 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004181}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004182
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004183void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004184{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004185 Token *inc, *space, *name;
4186 Line *l;
4187
H. Peter Anvin734b1882002-04-30 21:01:08 +00004188 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4189 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4190 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004191
4192 l = nasm_malloc(sizeof(Line));
4193 l->next = predef;
4194 l->first = inc;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004195 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004196 predef = l;
4197}
4198
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004199void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004200{
4201 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004202 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004203 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004204
4205 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004206 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4207 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004208 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004209 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004210 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004211 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004212 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004213
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004214 l = nasm_malloc(sizeof(Line));
4215 l->next = predef;
4216 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004217 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004218 predef = l;
4219}
4220
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004221void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004222{
4223 Token *def, *space;
4224 Line *l;
4225
H. Peter Anvin734b1882002-04-30 21:01:08 +00004226 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4227 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004228 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004229
4230 l = nasm_malloc(sizeof(Line));
4231 l->next = predef;
4232 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004233 l->finishes = false;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004234 predef = l;
4235}
4236
Keith Kaniosb7a89542007-04-12 02:40:54 +00004237/*
4238 * Added by Keith Kanios:
4239 *
4240 * This function is used to assist with "runtime" preprocessor
4241 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4242 *
4243 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4244 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4245 */
4246
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004247void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004248{
4249 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004250
Keith Kaniosb7a89542007-04-12 02:40:54 +00004251 def = tokenize(definition);
4252 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4253 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004254
Keith Kaniosb7a89542007-04-12 02:40:54 +00004255}
4256
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004257void pp_extra_stdmac(const char **macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004258{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004259 extrastdmac = macros;
4260}
4261
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004262static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004263{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004264 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004265 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004266 tok->text = nasm_strdup(numbuf);
4267 tok->type = TOK_NUMBER;
4268}
4269
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004270Preproc nasmpp = {
4271 pp_reset,
4272 pp_getline,
4273 pp_cleanup
4274};