blob: 66531364a5de731ed534b0fc6c67d9901a74b988 [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;
H. Peter Anvind2456592008-06-19 15:04:18 -0700336static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000337
338static ListGen *list;
339
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000340/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341 * The current set of multi-line macros we have defined.
342 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700343static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000344
345/*
346 * The current set of single-line macros we have defined.
347 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700348static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000349
350/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000351 * The multi-line macro we are currently defining, or the %rep
352 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000353 */
354static MMacro *defining;
355
356/*
357 * The number of macro parameters to allocate space for at a time.
358 */
359#define PARAM_DELTA 16
360
361/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700362 * The standard macro set: defined in macros.c in the array nasm_stdmac.
363 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000364 */
H. Peter Anvin188ce762008-02-16 13:58:45 -0800365static const char * const *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000366
367/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000368 * The extra standard macros that come from the object format, if
369 * any.
370 */
H. Peter Anvin188ce762008-02-16 13:58:45 -0800371static const char * const *extrastdmac = NULL;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700372bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000373
374/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000375 * Tokens are allocated in blocks to improve speed
376 */
377#define TOKEN_BLOCKSIZE 4096
378static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000379struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000380 Blocks *next;
381 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000382};
383
384static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000385
386/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000387 * Forward declarations.
388 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000389static Token *expand_mmac_params(Token * tline);
390static Token *expand_smacro(Token * tline);
391static Token *expand_id(Token * tline);
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700392static Context *get_ctx(char *name, bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700393static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000394static void error(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000395static void *new_Block(size_t size);
396static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700397static Token *new_Token(Token * next, enum pp_token_type type,
398 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000399static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000400
401/*
402 * Macros for safe checking of token pointers, avoid *(NULL)
403 */
404#define tok_type_(x,t) ((x) && (x)->type == (t))
405#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
406#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
407#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000408
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000409/* Handle TASM specific directives, which do not contain a % in
410 * front of them. We do it here because I could not find any other
411 * place to do it for the moment, and it is a hack (ideally it would
412 * be nice to be able to use the NASM pre-processor to do it).
413 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000414static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000415{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000416 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000417 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000418
419 /* Skip whitespace */
420 while (isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000421 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000422
423 /* Binary search for the directive name */
424 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000425 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000426 len = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000427 while (!isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000428 len++;
429 if (len) {
430 oldchar = p[len];
431 p[len] = 0;
432 while (j - i > 1) {
433 k = (j + i) / 2;
434 m = nasm_stricmp(p, tasm_directives[k]);
435 if (m == 0) {
436 /* We have found a directive, so jam a % in front of it
437 * so that NASM will then recognise it as one if it's own.
438 */
439 p[len] = oldchar;
440 len = strlen(p);
441 oldline = line;
442 line = nasm_malloc(len + 2);
443 line[0] = '%';
444 if (k == TM_IFDIFI) {
445 /* NASM does not recognise IFDIFI, so we convert it to
446 * %ifdef BOGUS. This is not used in NASM comaptible
447 * code, but does need to parse for the TASM macro
448 * package.
449 */
450 strcpy(line + 1, "ifdef BOGUS");
451 } else {
452 memcpy(line + 1, p, len + 1);
453 }
454 nasm_free(oldline);
455 return line;
456 } else if (m < 0) {
457 j = k;
458 } else
459 i = k;
460 }
461 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000462 }
463 return line;
464}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000465
H. Peter Anvin76690a12002-04-30 20:52:49 +0000466/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000467 * The pre-preprocessing stage... This function translates line
468 * number indications as they emerge from GNU cpp (`# lineno "file"
469 * flags') into NASM preprocessor line number indications (`%line
470 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000471 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000472static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000473{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000474 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000475 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000476
H. Peter Anvine2c80182005-01-15 22:15:51 +0000477 if (line[0] == '#' && line[1] == ' ') {
478 oldline = line;
479 fname = oldline + 2;
480 lineno = atoi(fname);
481 fname += strspn(fname, "0123456789 ");
482 if (*fname == '"')
483 fname++;
484 fnlen = strcspn(fname, "\"");
485 line = nasm_malloc(20 + fnlen);
486 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
487 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000488 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000489 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000490 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000491 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000492}
493
494/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000495 * Free a linked list of tokens.
496 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000497static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000498{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000499 while (list) {
500 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000501 }
502}
503
504/*
505 * Free a linked list of lines.
506 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000507static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000508{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000509 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000510 while (list) {
511 l = list;
512 list = list->next;
513 free_tlist(l->first);
514 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000515 }
516}
517
518/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000519 * Free an MMacro
520 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000521static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000522{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000523 nasm_free(m->name);
524 free_tlist(m->dlist);
525 nasm_free(m->defaults);
526 free_llist(m->expansion);
527 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000528}
529
530/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700531 * Free all currently defined macros, and free the hash tables
532 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700533static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700534{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700535 SMacro *s;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700536 const char *key;
537 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700538
H. Peter Anvin072771e2008-05-22 13:17:51 -0700539 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700540 nasm_free((void *)key);
541 while (s) {
542 SMacro *ns = s->next;
543 nasm_free(s->name);
544 free_tlist(s->expansion);
545 nasm_free(s);
546 s = ns;
547 }
548 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700549 hash_free(smt);
550}
551
552static void free_mmacro_table(struct hash_table *mmt)
553{
554 MMacro *m;
555 const char *key;
556 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700557
558 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700559 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700560 nasm_free((void *)key);
561 while (m) {
562 MMacro *nm = m->next;
563 free_mmacro(m);
564 m = nm;
565 }
566 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700567 hash_free(mmt);
568}
569
570static void free_macros(void)
571{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700572 free_smacro_table(&smacros);
573 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700574}
575
576/*
577 * Initialize the hash tables
578 */
579static void init_macros(void)
580{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700581 hash_init(&smacros, HASH_LARGE);
582 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700583}
584
585/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000586 * Pop the context stack.
587 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000588static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000589{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000590 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000591
592 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700593 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000594 nasm_free(c->name);
595 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000596}
597
H. Peter Anvin072771e2008-05-22 13:17:51 -0700598/*
599 * Search for a key in the hash index; adding it if necessary
600 * (in which case we initialize the data pointer to NULL.)
601 */
602static void **
603hash_findi_add(struct hash_table *hash, const char *str)
604{
605 struct hash_insert hi;
606 void **r;
607 char *strx;
608
609 r = hash_findi(hash, str, &hi);
610 if (r)
611 return r;
612
613 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
614 return hash_add(&hi, strx, NULL);
615}
616
617/*
618 * Like hash_findi, but returns the data element rather than a pointer
619 * to it. Used only when not adding a new element, hence no third
620 * argument.
621 */
622static void *
623hash_findix(struct hash_table *hash, const char *str)
624{
625 void **p;
626
627 p = hash_findi(hash, str, NULL);
628 return p ? *p : NULL;
629}
630
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000631#define BUF_DELTA 512
632/*
633 * Read a line from the top file in istk, handling multiple CR/LFs
634 * at the end of the line read, and handling spurious ^Zs. Will
635 * return lines from the standard macro set if this has not already
636 * been done.
637 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000638static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000639{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000640 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000641 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000642
H. Peter Anvine2c80182005-01-15 22:15:51 +0000643 if (stdmacpos) {
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700644 unsigned char c;
645 char *ret, *q;
646 const char *smac = *stdmacpos++, *p;
647 size_t len = 0;
648 p = smac;
649 while ((c = *p++)) {
650 if (c >= 0x80)
651 len += pp_directives_len[c-0x80]+1;
652 else
653 len++;
654 }
655 ret = nasm_malloc(len+1);
656 p = smac; q = ret;
657 while ((c = *p++)) {
658 if (c >= 0x80) {
659 memcpy(q, pp_directives[c-0x80], pp_directives_len[c-0x80]);
660 q += pp_directives_len[c-0x80];
661 *q++ = ' ';
662 } else {
663 *q++ = c;
664 }
665 }
666 *q = '\0';
667
H. Peter Anvind2456592008-06-19 15:04:18 -0700668 if (!*stdmacpos) {
669 /* This was the last of the standard macro chain... */
670 stdmacpos = NULL;
671 if (any_extrastdmac) {
672 stdmacpos = extrastdmac;
673 any_extrastdmac = false;
674 } else if (do_predef) {
675 Line *pd, *l;
676 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000677
H. Peter Anvind2456592008-06-19 15:04:18 -0700678 /*
679 * Nasty hack: here we push the contents of
680 * `predef' on to the top-level expansion stack,
681 * since this is the most convenient way to
682 * implement the pre-include and pre-define
683 * features.
684 */
685 for (pd = predef; pd; pd = pd->next) {
686 head = NULL;
687 tail = &head;
688 for (t = pd->first; t; t = t->next) {
689 *tail = new_Token(NULL, t->type, t->text, 0);
690 tail = &(*tail)->next;
691 }
692 l = nasm_malloc(sizeof(Line));
693 l->next = istk->expansion;
694 l->first = head;
695 l->finishes = false;
696 istk->expansion = l;
697 }
698 do_predef = false;
699 }
700 }
701 return ret;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000702 }
703
704 bufsize = BUF_DELTA;
705 buffer = nasm_malloc(BUF_DELTA);
706 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000707 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000708 while (1) {
709 q = fgets(p, bufsize - (p - buffer), istk->fp);
710 if (!q)
711 break;
712 p += strlen(p);
713 if (p > buffer && p[-1] == '\n') {
714 /* Convert backslash-CRLF line continuation sequences into
715 nothing at all (for DOS and Windows) */
716 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
717 p -= 3;
718 *p = 0;
719 continued_count++;
720 }
721 /* Also convert backslash-LF line continuation sequences into
722 nothing at all (for Unix) */
723 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
724 p -= 2;
725 *p = 0;
726 continued_count++;
727 } else {
728 break;
729 }
730 }
731 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000732 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000733 bufsize += BUF_DELTA;
734 buffer = nasm_realloc(buffer, bufsize);
735 p = buffer + offset; /* prevent stale-pointer problems */
736 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000737 }
738
H. Peter Anvine2c80182005-01-15 22:15:51 +0000739 if (!q && p == buffer) {
740 nasm_free(buffer);
741 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000742 }
743
H. Peter Anvine2c80182005-01-15 22:15:51 +0000744 src_set_linnum(src_get_linnum() + istk->lineinc +
745 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000746
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000747 /*
748 * Play safe: remove CRs as well as LFs, if any of either are
749 * present at the end of the line.
750 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000751 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000752 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000753
754 /*
755 * Handle spurious ^Z, which may be inserted into source files
756 * by some file transfer utilities.
757 */
758 buffer[strcspn(buffer, "\032")] = '\0';
759
H. Peter Anvin734b1882002-04-30 21:01:08 +0000760 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000761
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000762 return buffer;
763}
764
765/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000766 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000767 * don't need to parse the value out of e.g. numeric tokens: we
768 * simply split one string into many.
769 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000770static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000771{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000772 char *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000773 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000774 Token *list = NULL;
775 Token *t, **tail = &list;
776
H. Peter Anvine2c80182005-01-15 22:15:51 +0000777 while (*line) {
778 p = line;
779 if (*p == '%') {
780 p++;
781 if (isdigit(*p) ||
782 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
783 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
784 do {
785 p++;
786 }
787 while (isdigit(*p));
788 type = TOK_PREPROC_ID;
789 } else if (*p == '{') {
790 p++;
791 while (*p && *p != '}') {
792 p[-1] = *p;
793 p++;
794 }
795 p[-1] = '\0';
796 if (*p)
797 p++;
798 type = TOK_PREPROC_ID;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700799 } else if (*p == '?') {
800 type = TOK_PREPROC_Q; /* %? */
801 p++;
802 if (*p == '?') {
803 type = TOK_PREPROC_QQ; /* %?? */
804 p++;
805 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000806 } else if (isidchar(*p) ||
807 ((*p == '!' || *p == '%' || *p == '$') &&
808 isidchar(p[1]))) {
809 do {
810 p++;
811 }
812 while (isidchar(*p));
813 type = TOK_PREPROC_ID;
814 } else {
815 type = TOK_OTHER;
816 if (*p == '%')
817 p++;
818 }
819 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
820 type = TOK_ID;
821 p++;
822 while (*p && isidchar(*p))
823 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700824 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000825 /*
826 * A string token.
827 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000828 type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700829 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000830
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 if (*p) {
832 p++;
833 } else {
834 error(ERR_WARNING, "unterminated string");
835 /* Handling unterminated strings by UNV */
836 /* type = -1; */
837 }
838 } else if (isnumstart(*p)) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700839 bool is_hex = false;
840 bool is_float = false;
841 bool has_e = false;
842 char c, *r;
843
H. Peter Anvine2c80182005-01-15 22:15:51 +0000844 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700845 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000846 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700847
848 if (*p == '$') {
849 p++;
850 is_hex = true;
851 }
852
853 for (;;) {
854 c = *p++;
855
856 if (!is_hex && (c == 'e' || c == 'E')) {
857 has_e = true;
858 if (*p == '+' || *p == '-') {
859 /* e can only be followed by +/- if it is either a
860 prefixed hex number or a floating-point number */
861 p++;
862 is_float = true;
863 }
864 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
865 is_hex = true;
866 } else if (c == 'P' || c == 'p') {
867 is_float = true;
868 if (*p == '+' || *p == '-')
869 p++;
870 } else if (isnumchar(c) || c == '_')
871 ; /* just advance */
872 else if (c == '.') {
873 /* we need to deal with consequences of the legacy
874 parser, like "1.nolist" being two tokens
875 (TOK_NUMBER, TOK_ID) here; at least give it
876 a shot for now. In the future, we probably need
877 a flex-based scanner with proper pattern matching
878 to do it as well as it can be done. Nothing in
879 the world is going to help the person who wants
880 0x123.p16 interpreted as two tokens, though. */
881 r = p;
882 while (*r == '_')
883 r++;
884
885 if (isdigit(*r) || (is_hex && isxdigit(*r)) ||
886 (!is_hex && (*r == 'e' || *r == 'E')) ||
887 (*r == 'p' || *r == 'P')) {
888 p = r;
889 is_float = true;
890 } else
891 break; /* Terminate the token */
892 } else
893 break;
894 }
895 p--; /* Point to first character beyond number */
896
897 if (has_e && !is_hex) {
898 /* 1e13 is floating-point, but 1e13h is not */
899 is_float = true;
900 }
901
902 type = is_float ? TOK_FLOAT : TOK_NUMBER;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000903 } else if (isspace(*p)) {
904 type = TOK_WHITESPACE;
905 p++;
906 while (*p && isspace(*p))
907 p++;
908 /*
909 * Whitespace just before end-of-line is discarded by
910 * pretending it's a comment; whitespace just before a
911 * comment gets lumped into the comment.
912 */
913 if (!*p || *p == ';') {
914 type = TOK_COMMENT;
915 while (*p)
916 p++;
917 }
918 } else if (*p == ';') {
919 type = TOK_COMMENT;
920 while (*p)
921 p++;
922 } else {
923 /*
924 * Anything else is an operator of some kind. We check
925 * for all the double-character operators (>>, <<, //,
926 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000927 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000928 */
929 type = TOK_OTHER;
930 if ((p[0] == '>' && p[1] == '>') ||
931 (p[0] == '<' && p[1] == '<') ||
932 (p[0] == '/' && p[1] == '/') ||
933 (p[0] == '<' && p[1] == '=') ||
934 (p[0] == '>' && p[1] == '=') ||
935 (p[0] == '=' && p[1] == '=') ||
936 (p[0] == '!' && p[1] == '=') ||
937 (p[0] == '<' && p[1] == '>') ||
938 (p[0] == '&' && p[1] == '&') ||
939 (p[0] == '|' && p[1] == '|') ||
940 (p[0] == '^' && p[1] == '^')) {
941 p++;
942 }
943 p++;
944 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000945
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 /* Handling unterminated string by UNV */
947 /*if (type == -1)
948 {
949 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
950 t->text[p-line] = *line;
951 tail = &t->next;
952 }
953 else */
954 if (type != TOK_COMMENT) {
955 *tail = t = new_Token(NULL, type, line, p - line);
956 tail = &t->next;
957 }
958 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000959 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000960 return list;
961}
962
H. Peter Anvince616072002-04-30 21:02:23 +0000963/*
964 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -0700965 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +0000966 * deleted only all at once by the delete_Blocks function.
967 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000968static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +0000969{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000970 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000972 /* first, get to the end of the linked list */
973 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000975 /* now allocate the requested chunk */
976 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000977
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000978 /* now allocate a new block for the next request */
979 b->next = nasm_malloc(sizeof(Blocks));
980 /* and initialize the contents of the new block */
981 b->next->next = NULL;
982 b->next->chunk = NULL;
983 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000984}
985
986/*
987 * this function deletes all managed blocks of memory
988 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000989static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +0000990{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +0000992
H. Peter Anvin70653092007-10-19 14:42:29 -0700993 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000994 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -0700995 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000996 * free it.
997 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000998 while (b) {
999 if (b->chunk)
1000 nasm_free(b->chunk);
1001 a = b;
1002 b = b->next;
1003 if (a != &blocks)
1004 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001005 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001007
1008/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001009 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001010 * back to the caller. It sets the type and text elements, and
1011 * also the mac and next elements to NULL.
1012 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001013static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvinc751e862008-06-09 10:18:45 -07001014 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001015{
1016 Token *t;
1017 int i;
1018
H. Peter Anvine2c80182005-01-15 22:15:51 +00001019 if (freeTokens == NULL) {
1020 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1021 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1022 freeTokens[i].next = &freeTokens[i + 1];
1023 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001024 }
1025 t = freeTokens;
1026 freeTokens = t->next;
1027 t->next = next;
1028 t->mac = NULL;
1029 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001030 if (type == TOK_WHITESPACE || text == NULL) {
1031 t->text = NULL;
1032 } else {
1033 if (txtlen == 0)
1034 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001035 t->text = nasm_malloc(txtlen+1);
1036 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001037 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001038 }
1039 return t;
1040}
1041
H. Peter Anvine2c80182005-01-15 22:15:51 +00001042static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001043{
1044 Token *next = t->next;
1045 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001046 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001047 freeTokens = t;
1048 return next;
1049}
1050
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001051/*
1052 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001053 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1054 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001055 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001056static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001057{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001058 Token *t;
1059 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001060 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001061 const char *q;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001062
1063 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 for (t = tlist; t; t = t->next) {
1065 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001066 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001067 nasm_free(t->text);
1068 if (p)
1069 t->text = nasm_strdup(p);
1070 else
1071 t->text = NULL;
1072 }
1073 /* Expand local macros here and not during preprocessing */
1074 if (expand_locals &&
1075 t->type == TOK_PREPROC_ID && t->text &&
1076 t->text[0] == '%' && t->text[1] == '$') {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001077 Context *ctx = get_ctx(t->text, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001078 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001079 char buffer[40];
1080 char *p, *q = t->text + 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001081
H. Peter Anvine2c80182005-01-15 22:15:51 +00001082 q += strspn(q, "$");
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001083 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001084 p = nasm_strcat(buffer, q);
1085 nasm_free(t->text);
1086 t->text = p;
1087 }
1088 }
1089 if (t->type == TOK_WHITESPACE) {
1090 len++;
1091 } else if (t->text) {
1092 len += strlen(t->text);
1093 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001094 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001095 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001096 for (t = tlist; t; t = t->next) {
1097 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001098 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 } else if (t->text) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001100 q = t->text;
1101 while (*q)
1102 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001103 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001104 }
1105 *p = '\0';
1106 return line;
1107}
1108
1109/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001110 * A scanner, suitable for use by the expression evaluator, which
1111 * operates on a line of Tokens. Expects a pointer to a pointer to
1112 * the first token in the line to be passed in as its private_data
1113 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001114 *
1115 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001116 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001117static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001118{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001119 Token **tlineptr = private_data;
1120 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001121 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001122
H. Peter Anvine2c80182005-01-15 22:15:51 +00001123 do {
1124 tline = *tlineptr;
1125 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001126 }
1127 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001129
1130 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001131 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001132
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001133 tokval->t_charptr = tline->text;
1134
H. Peter Anvin76690a12002-04-30 20:52:49 +00001135 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001136 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001137 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001138 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001139
H. Peter Anvine2c80182005-01-15 22:15:51 +00001140 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001141 p = tokval->t_charptr = tline->text;
1142 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001143 tokval->t_charptr++;
1144 return tokval->t_type = TOKEN_ID;
1145 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001146
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001147 for (r = p, s = ourcopy; *r; r++) {
Philipp Thomas76ec8e72008-05-21 08:53:21 -07001148 if (r >= p+MAX_KEYWORD)
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001149 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001150 *s++ = nasm_tolower(*r);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001151 }
1152 *s = '\0';
1153 /* right, so we have an identifier sitting in temp storage. now,
1154 * is it actually a register or instruction name, or what? */
1155 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001156 }
1157
H. Peter Anvine2c80182005-01-15 22:15:51 +00001158 if (tline->type == TOK_NUMBER) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001159 bool rn_error;
1160 tokval->t_integer = readnum(tline->text, &rn_error);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001161 tokval->t_charptr = tline->text;
H. Peter Anvin11627042008-06-09 20:45:19 -07001162 if (rn_error)
1163 return tokval->t_type = TOKEN_ERRNUM;
1164 else
1165 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001166 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001167
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001168 if (tline->type == TOK_FLOAT) {
1169 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001170 }
1171
H. Peter Anvine2c80182005-01-15 22:15:51 +00001172 if (tline->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001173 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001174
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001175 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001176 tokval->t_charptr = tline->text;
1177 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001178
H. Peter Anvin11627042008-06-09 20:45:19 -07001179 if (ep[0] != bq || ep[1] != '\0')
1180 return tokval->t_type = TOKEN_ERRSTR;
1181 else
1182 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001183 }
1184
H. Peter Anvine2c80182005-01-15 22:15:51 +00001185 if (tline->type == TOK_OTHER) {
1186 if (!strcmp(tline->text, "<<"))
1187 return tokval->t_type = TOKEN_SHL;
1188 if (!strcmp(tline->text, ">>"))
1189 return tokval->t_type = TOKEN_SHR;
1190 if (!strcmp(tline->text, "//"))
1191 return tokval->t_type = TOKEN_SDIV;
1192 if (!strcmp(tline->text, "%%"))
1193 return tokval->t_type = TOKEN_SMOD;
1194 if (!strcmp(tline->text, "=="))
1195 return tokval->t_type = TOKEN_EQ;
1196 if (!strcmp(tline->text, "<>"))
1197 return tokval->t_type = TOKEN_NE;
1198 if (!strcmp(tline->text, "!="))
1199 return tokval->t_type = TOKEN_NE;
1200 if (!strcmp(tline->text, "<="))
1201 return tokval->t_type = TOKEN_LE;
1202 if (!strcmp(tline->text, ">="))
1203 return tokval->t_type = TOKEN_GE;
1204 if (!strcmp(tline->text, "&&"))
1205 return tokval->t_type = TOKEN_DBL_AND;
1206 if (!strcmp(tline->text, "^^"))
1207 return tokval->t_type = TOKEN_DBL_XOR;
1208 if (!strcmp(tline->text, "||"))
1209 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001210 }
1211
1212 /*
1213 * We have no other options: just return the first character of
1214 * the token text.
1215 */
1216 return tokval->t_type = tline->text[0];
1217}
1218
1219/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001220 * Compare a string to the name of an existing macro; this is a
1221 * simple wrapper which calls either strcmp or nasm_stricmp
1222 * depending on the value of the `casesense' parameter.
1223 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001224static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001225{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001226 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001227}
1228
1229/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001230 * Compare a string to the name of an existing macro; this is a
1231 * simple wrapper which calls either strcmp or nasm_stricmp
1232 * depending on the value of the `casesense' parameter.
1233 */
1234static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1235{
1236 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1237}
1238
1239/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001240 * Return the Context structure associated with a %$ token. Return
1241 * NULL, having _already_ reported an error condition, if the
1242 * context stack isn't deep enough for the supplied number of $
1243 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001244 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001245 * also scanned for such smacro, until it is found; if not -
1246 * only the context that directly results from the number of $'s
1247 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001248 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001249static Context *get_ctx(char *name, bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001250{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001251 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001252 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001253 int i;
1254
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001255 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001256 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001257
H. Peter Anvine2c80182005-01-15 22:15:51 +00001258 if (!cstk) {
1259 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1260 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001261 }
1262
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1264 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001265/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001266 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001267 if (!ctx) {
1268 error(ERR_NONFATAL, "`%s': context stack is only"
1269 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1270 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001271 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001272 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001273 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001274
H. Peter Anvine2c80182005-01-15 22:15:51 +00001275 do {
1276 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001277 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001278 while (m) {
1279 if (!mstrcmp(m->name, name, m->casesense))
1280 return ctx;
1281 m = m->next;
1282 }
1283 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001284 }
1285 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001286 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001287}
1288
1289/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001290 * Check to see if a file is already in a string list
1291 */
1292static bool in_list(const StrList *list, const char *str)
1293{
1294 while (list) {
1295 if (!strcmp(list->str, str))
1296 return true;
1297 list = list->next;
1298 }
1299 return false;
1300}
1301
1302/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001303 * Open an include file. This routine must always return a valid
1304 * file pointer if it returns - it's responsible for throwing an
1305 * ERR_FATAL and bombing out completely if not. It should also try
1306 * the include path one by one until it finds the file or reaches
1307 * the end of the path.
1308 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001309static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
H. Peter Anvin418ca702008-05-30 10:42:30 -07001310 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001311{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001312 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001313 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001314 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001315 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001316 size_t prefix_len = 0;
1317 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001318
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001320 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1321 memcpy(sl->str, prefix, prefix_len);
1322 memcpy(sl->str+prefix_len, file, len+1);
1323 fp = fopen(sl->str, "r");
H. Peter Anvin418ca702008-05-30 10:42:30 -07001324 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001325 sl->next = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001326 **dtail = sl;
1327 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001328 } else {
1329 nasm_free(sl);
1330 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001331 if (fp)
1332 return fp;
H. Peter Anvin418ca702008-05-30 10:42:30 -07001333 if (!ip) {
1334 if (!missing_ok)
1335 break;
1336 prefix = NULL;
1337 } else {
1338 prefix = ip->path;
1339 ip = ip->next;
1340 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001341 if (prefix) {
1342 prefix_len = strlen(prefix);
1343 } else {
1344 /* -MG given and file not found */
H. Peter Anvin418ca702008-05-30 10:42:30 -07001345 if (dhead && !in_list(*dhead, file)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001346 sl = nasm_malloc(len+1+sizeof sl->next);
1347 sl->next = NULL;
1348 strcpy(sl->str, file);
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001349 **dtail = sl;
1350 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001351 }
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001352 return NULL;
1353 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001354 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001355
H. Peter Anvin734b1882002-04-30 21:01:08 +00001356 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001357 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001358}
1359
1360/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001361 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001362 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001363 * return true if _any_ single-line macro of that name is defined.
1364 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001365 * `nparam' or no parameters is defined.
1366 *
1367 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001368 * defined, or nparam is -1, the address of the definition structure
1369 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001370 * is NULL, no action will be taken regarding its contents, and no
1371 * error will occur.
1372 *
1373 * Note that this is also called with nparam zero to resolve
1374 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001375 *
1376 * If you already know which context macro belongs to, you can pass
1377 * the context pointer as first parameter; if you won't but name begins
1378 * with %$ the context will be automatically computed. If all_contexts
1379 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001380 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001381static bool
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001382smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001383 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001384{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001385 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001386 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001387
H. Peter Anvin97a23472007-09-16 17:57:25 -07001388 if (ctx) {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001389 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001390 } else if (name[0] == '%' && name[1] == '$') {
1391 if (cstk)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001392 ctx = get_ctx(name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001393 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001394 return false; /* got to return _something_ */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001395 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001396 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001397 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001398 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001399 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001400
H. Peter Anvine2c80182005-01-15 22:15:51 +00001401 while (m) {
1402 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001403 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001404 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001405 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001406 *defn = m;
1407 else
1408 *defn = NULL;
1409 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001410 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001411 }
1412 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001413 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001414
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001415 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001416}
1417
1418/*
1419 * Count and mark off the parameters in a multi-line macro call.
1420 * This is called both from within the multi-line macro expansion
1421 * code, and also to mark off the default parameters when provided
1422 * in a %macro definition line.
1423 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001424static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001425{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001426 int paramsize, brace;
1427
1428 *nparam = paramsize = 0;
1429 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 while (t) {
1431 if (*nparam >= paramsize) {
1432 paramsize += PARAM_DELTA;
1433 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1434 }
1435 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001436 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001438 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001439 (*params)[(*nparam)++] = t;
1440 while (tok_isnt_(t, brace ? "}" : ","))
1441 t = t->next;
1442 if (t) { /* got a comma/brace */
1443 t = t->next;
1444 if (brace) {
1445 /*
1446 * Now we've found the closing brace, look further
1447 * for the comma.
1448 */
1449 skip_white_(t);
1450 if (tok_isnt_(t, ",")) {
1451 error(ERR_NONFATAL,
1452 "braces do not enclose all of macro parameter");
1453 while (tok_isnt_(t, ","))
1454 t = t->next;
1455 }
1456 if (t)
1457 t = t->next; /* eat the comma */
1458 }
1459 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001460 }
1461}
1462
1463/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001464 * Determine whether one of the various `if' conditions is true or
1465 * not.
1466 *
1467 * We must free the tline we get passed.
1468 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001469static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001470{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001471 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001472 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001473 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001474 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001475 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001476 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001477
1478 origline = tline;
1479
H. Peter Anvine2c80182005-01-15 22:15:51 +00001480 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001481 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001482 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001483 while (cstk && tline) {
1484 skip_white_(tline);
1485 if (!tline || tline->type != TOK_ID) {
1486 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001487 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001488 free_tlist(origline);
1489 return -1;
1490 }
1491 if (!nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001492 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001493 tline = tline->next;
1494 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001495 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001496
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001497 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001498 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001499 while (tline) {
1500 skip_white_(tline);
1501 if (!tline || (tline->type != TOK_ID &&
1502 (tline->type != TOK_PREPROC_ID ||
1503 tline->text[1] != '$'))) {
1504 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001505 "`%s' expects macro identifiers", pp_directives[ct]);
1506 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001507 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001508 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001509 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001510 tline = tline->next;
1511 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001512 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001513
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001514 case PPC_IFIDN:
1515 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001516 tline = expand_smacro(tline);
1517 t = tt = tline;
1518 while (tok_isnt_(tt, ","))
1519 tt = tt->next;
1520 if (!tt) {
1521 error(ERR_NONFATAL,
1522 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001523 pp_directives[ct]);
1524 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001525 }
1526 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001527 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001528 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1529 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1530 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001531 pp_directives[ct]);
1532 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001533 }
1534 if (t->type == TOK_WHITESPACE) {
1535 t = t->next;
1536 continue;
1537 }
1538 if (tt->type == TOK_WHITESPACE) {
1539 tt = tt->next;
1540 continue;
1541 }
1542 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001543 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001544 break;
1545 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001546 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001547 if (t->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001548 size_t l1 = nasm_unquote(t->text, NULL);
1549 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001550
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001551 if (l1 != l2) {
1552 j = false;
1553 break;
1554 }
1555 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1556 j = false;
1557 break;
1558 }
1559 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001560 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001561 break;
1562 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001563
H. Peter Anvine2c80182005-01-15 22:15:51 +00001564 t = t->next;
1565 tt = tt->next;
1566 }
1567 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001568 j = false; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001569 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001570
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001571 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001572 {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001573 bool found = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001574 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001575
H. Peter Anvine2c80182005-01-15 22:15:51 +00001576 tline = tline->next;
1577 skip_white_(tline);
1578 tline = expand_id(tline);
1579 if (!tok_type_(tline, TOK_ID)) {
1580 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001581 "`%s' expects a macro name", pp_directives[ct]);
1582 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001583 }
1584 searching.name = nasm_strdup(tline->text);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001585 searching.casesense = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001586 searching.plus = false;
1587 searching.nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001588 searching.in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001589 searching.rep_nest = NULL;
1590 searching.nparam_min = 0;
1591 searching.nparam_max = INT_MAX;
1592 tline = expand_smacro(tline->next);
1593 skip_white_(tline);
1594 if (!tline) {
1595 } else if (!tok_type_(tline, TOK_NUMBER)) {
1596 error(ERR_NONFATAL,
1597 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001598 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001599 } else {
1600 searching.nparam_min = searching.nparam_max =
1601 readnum(tline->text, &j);
1602 if (j)
1603 error(ERR_NONFATAL,
1604 "unable to parse parameter count `%s'",
1605 tline->text);
1606 }
1607 if (tline && tok_is_(tline->next, "-")) {
1608 tline = tline->next->next;
1609 if (tok_is_(tline, "*"))
1610 searching.nparam_max = INT_MAX;
1611 else if (!tok_type_(tline, TOK_NUMBER))
1612 error(ERR_NONFATAL,
1613 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001614 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 else {
1616 searching.nparam_max = readnum(tline->text, &j);
1617 if (j)
1618 error(ERR_NONFATAL,
1619 "unable to parse parameter count `%s'",
1620 tline->text);
1621 if (searching.nparam_min > searching.nparam_max)
1622 error(ERR_NONFATAL,
1623 "minimum parameter count exceeds maximum");
1624 }
1625 }
1626 if (tline && tok_is_(tline->next, "+")) {
1627 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001628 searching.plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001629 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001630 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001631 while (mmac) {
1632 if (!strcmp(mmac->name, searching.name) &&
1633 (mmac->nparam_min <= searching.nparam_max
1634 || searching.plus)
1635 && (searching.nparam_min <= mmac->nparam_max
1636 || mmac->plus)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001637 found = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001638 break;
1639 }
1640 mmac = mmac->next;
1641 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001642 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001643 j = found;
1644 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001646
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001647 case PPC_IFID:
1648 needtype = TOK_ID;
1649 goto iftype;
1650 case PPC_IFNUM:
1651 needtype = TOK_NUMBER;
1652 goto iftype;
1653 case PPC_IFSTR:
1654 needtype = TOK_STRING;
1655 goto iftype;
1656
1657 iftype:
H. Peter Anvin134b9462008-02-16 17:01:40 -08001658 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001659
H. Peter Anvin927c92b2008-02-16 13:44:52 -08001660 while (tok_type_(t, TOK_WHITESPACE) ||
1661 (needtype == TOK_NUMBER &&
1662 tok_type_(t, TOK_OTHER) &&
1663 (t->text[0] == '-' || t->text[0] == '+') &&
1664 !t->text[1]))
1665 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001666
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001667 j = tok_type_(t, needtype);
1668 break;
1669
1670 case PPC_IFTOKEN:
1671 t = tline = expand_smacro(tline);
1672 while (tok_type_(t, TOK_WHITESPACE))
1673 t = t->next;
1674
1675 j = false;
1676 if (t) {
1677 t = t->next; /* Skip the actual token */
1678 while (tok_type_(t, TOK_WHITESPACE))
1679 t = t->next;
1680 j = !t; /* Should be nothing left */
1681 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001682 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001683
H. Peter Anvin134b9462008-02-16 17:01:40 -08001684 case PPC_IFEMPTY:
1685 t = tline = expand_smacro(tline);
1686 while (tok_type_(t, TOK_WHITESPACE))
1687 t = t->next;
1688
1689 j = !t; /* Should be empty */
1690 break;
1691
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001692 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001693 t = tline = expand_smacro(tline);
1694 tptr = &t;
1695 tokval.t_type = TOKEN_INVALID;
1696 evalresult = evaluate(ppscan, tptr, &tokval,
1697 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001698 if (!evalresult)
1699 return -1;
1700 if (tokval.t_type)
1701 error(ERR_WARNING,
1702 "trailing garbage after expression ignored");
1703 if (!is_simple(evalresult)) {
1704 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001705 "non-constant value given to `%s'", pp_directives[ct]);
1706 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001707 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001708 j = reloc_value(evalresult) != 0;
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001709 return j;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001710
H. Peter Anvine2c80182005-01-15 22:15:51 +00001711 default:
1712 error(ERR_FATAL,
1713 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001714 pp_directives[ct]);
1715 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001716 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001717
1718 free_tlist(origline);
1719 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001720
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001721fail:
1722 free_tlist(origline);
1723 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001724}
1725
1726/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001727 * Common code for defining an smacro
1728 */
1729static bool define_smacro(Context *ctx, char *mname, bool casesense,
1730 int nparam, Token *expansion)
1731{
1732 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001733 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001734
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001735 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1736 if (!smac) {
1737 error(ERR_WARNING,
1738 "single-line macro `%s' defined both with and"
1739 " without parameters", mname);
1740
1741 /* Some instances of the old code considered this a failure,
1742 some others didn't. What is the right thing to do here? */
1743 free_tlist(expansion);
1744 return false; /* Failure */
1745 } else {
1746 /*
1747 * We're redefining, so we have to take over an
1748 * existing SMacro structure. This means freeing
1749 * what was already in it.
1750 */
1751 nasm_free(smac->name);
1752 free_tlist(smac->expansion);
1753 }
1754 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001755 smtbl = ctx ? &ctx->localmac : &smacros;
1756 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001757 smac = nasm_malloc(sizeof(SMacro));
1758 smac->next = *smhead;
1759 *smhead = smac;
1760 }
1761 smac->name = nasm_strdup(mname);
1762 smac->casesense = casesense;
1763 smac->nparam = nparam;
1764 smac->expansion = expansion;
1765 smac->in_progress = false;
1766 return true; /* Success */
1767}
1768
1769/*
1770 * Undefine an smacro
1771 */
1772static void undef_smacro(Context *ctx, const char *mname)
1773{
1774 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001775 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001776
H. Peter Anvin166c2472008-05-28 12:28:58 -07001777 smtbl = ctx ? &ctx->localmac : &smacros;
1778 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001779
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001780 if (smhead) {
1781 /*
1782 * We now have a macro name... go hunt for it.
1783 */
1784 sp = smhead;
1785 while ((s = *sp) != NULL) {
1786 if (!mstrcmp(s->name, mname, s->casesense)) {
1787 *sp = s->next;
1788 nasm_free(s->name);
1789 free_tlist(s->expansion);
1790 nasm_free(s);
1791 } else {
1792 sp = &s->next;
1793 }
1794 }
1795 }
1796}
1797
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001798/*
1799 * Decode a size directive
1800 */
1801static int parse_size(const char *str) {
1802 static const char *size_names[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001803 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001804 static const int sizes[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001805 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001806
1807 return sizes[bsii(str, size_names, elements(size_names))+1];
1808}
1809
Ed Beroset3ab3f412002-06-11 03:31:49 +00001810/**
1811 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001812 * Find out if a line contains a preprocessor directive, and deal
1813 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07001814 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001815 * If a directive _is_ found, it is the responsibility of this routine
1816 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001817 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001818 * @param tline a pointer to the current tokeninzed line linked list
1819 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07001820 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001821 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001822static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001823{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001824 enum preproc_token i;
1825 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07001826 bool err;
1827 int nparam;
1828 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001829 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001830 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001831 int offset;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001832 char *p, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001833 Include *inc;
1834 Context *ctx;
1835 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001836 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001837 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1838 Line *l;
1839 struct tokenval tokval;
1840 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001841 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001842 int64_t count;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001843
1844 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001845
H. Peter Anvineba20a72002-04-30 20:53:55 +00001846 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07001847 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001848 (tline->text[1] == '%' || tline->text[1] == '$'
1849 || tline->text[1] == '!'))
1850 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001851
H. Peter Anvin4169a472007-09-12 01:29:43 +00001852 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001853
1854 /*
1855 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001856 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001857 * we should ignore all directives except for condition
1858 * directives.
1859 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001860 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001861 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1862 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001863 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001864
1865 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001866 * If we're defining a macro or reading a %rep block, we should
1867 * ignore all directives except for %macro/%imacro (which
1868 * generate an error), %endm/%endmacro, and (only if we're in a
H. Peter Anvineba20a72002-04-30 20:53:55 +00001869 * %rep block) %endrep. If we're in a %rep block, another %rep
1870 * causes an error, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001871 */
1872 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001873 i != PP_ENDMACRO && i != PP_ENDM &&
1874 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1875 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001876 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001877
H. Peter Anvin4169a472007-09-12 01:29:43 +00001878 switch (i) {
1879 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001880 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1881 tline->text);
1882 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001883
H. Peter Anvine2c80182005-01-15 22:15:51 +00001884 case PP_STACKSIZE:
1885 /* Directive to tell NASM what the default stack size is. The
1886 * default is for a 16-bit stack, and this can be overriden with
1887 * %stacksize large.
1888 * the following form:
1889 *
1890 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1891 */
1892 tline = tline->next;
1893 if (tline && tline->type == TOK_WHITESPACE)
1894 tline = tline->next;
1895 if (!tline || tline->type != TOK_ID) {
1896 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1897 free_tlist(origline);
1898 return DIRECTIVE_FOUND;
1899 }
1900 if (nasm_stricmp(tline->text, "flat") == 0) {
1901 /* All subsequent ARG directives are for a 32-bit stack */
1902 StackSize = 4;
1903 StackPointer = "ebp";
1904 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001905 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08001906 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
1907 /* All subsequent ARG directives are for a 64-bit stack */
1908 StackSize = 8;
1909 StackPointer = "rbp";
1910 ArgOffset = 8;
1911 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001912 } else if (nasm_stricmp(tline->text, "large") == 0) {
1913 /* All subsequent ARG directives are for a 16-bit stack,
1914 * far function call.
1915 */
1916 StackSize = 2;
1917 StackPointer = "bp";
1918 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001919 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001920 } else if (nasm_stricmp(tline->text, "small") == 0) {
1921 /* All subsequent ARG directives are for a 16-bit stack,
1922 * far function call. We don't support near functions.
1923 */
1924 StackSize = 2;
1925 StackPointer = "bp";
1926 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001927 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001928 } else {
1929 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1930 free_tlist(origline);
1931 return DIRECTIVE_FOUND;
1932 }
1933 free_tlist(origline);
1934 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001935
H. Peter Anvine2c80182005-01-15 22:15:51 +00001936 case PP_ARG:
1937 /* TASM like ARG directive to define arguments to functions, in
1938 * the following form:
1939 *
1940 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1941 */
1942 offset = ArgOffset;
1943 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001944 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001945 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001946
H. Peter Anvine2c80182005-01-15 22:15:51 +00001947 /* Find the argument name */
1948 tline = tline->next;
1949 if (tline && tline->type == TOK_WHITESPACE)
1950 tline = tline->next;
1951 if (!tline || tline->type != TOK_ID) {
1952 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1953 free_tlist(origline);
1954 return DIRECTIVE_FOUND;
1955 }
1956 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001957
H. Peter Anvine2c80182005-01-15 22:15:51 +00001958 /* Find the argument size type */
1959 tline = tline->next;
1960 if (!tline || tline->type != TOK_OTHER
1961 || tline->text[0] != ':') {
1962 error(ERR_NONFATAL,
1963 "Syntax error processing `%%arg' directive");
1964 free_tlist(origline);
1965 return DIRECTIVE_FOUND;
1966 }
1967 tline = tline->next;
1968 if (!tline || tline->type != TOK_ID) {
1969 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1970 free_tlist(origline);
1971 return DIRECTIVE_FOUND;
1972 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001973
H. Peter Anvine2c80182005-01-15 22:15:51 +00001974 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001975 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001976 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001977 size = parse_size(tt->text);
1978 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001979 error(ERR_NONFATAL,
1980 "Invalid size type for `%%arg' missing directive");
1981 free_tlist(tt);
1982 free_tlist(origline);
1983 return DIRECTIVE_FOUND;
1984 }
1985 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001986
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001987 /* Round up to even stack slots */
1988 size = (size+StackSize-1) & ~(StackSize-1);
1989
H. Peter Anvine2c80182005-01-15 22:15:51 +00001990 /* Now define the macro for the argument */
1991 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1992 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001993 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001994 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001995
H. Peter Anvine2c80182005-01-15 22:15:51 +00001996 /* Move to the next argument in the list */
1997 tline = tline->next;
1998 if (tline && tline->type == TOK_WHITESPACE)
1999 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002000 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2001 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002002 free_tlist(origline);
2003 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002004
H. Peter Anvine2c80182005-01-15 22:15:51 +00002005 case PP_LOCAL:
2006 /* TASM like LOCAL directive to define local variables for a
2007 * function, in the following form:
2008 *
2009 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2010 *
2011 * The '= LocalSize' at the end is ignored by NASM, but is
2012 * required by TASM to define the local parameter size (and used
2013 * by the TASM macro package).
2014 */
2015 offset = LocalOffset;
2016 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002017 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002018 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002019
H. Peter Anvine2c80182005-01-15 22:15:51 +00002020 /* Find the argument name */
2021 tline = tline->next;
2022 if (tline && tline->type == TOK_WHITESPACE)
2023 tline = tline->next;
2024 if (!tline || tline->type != TOK_ID) {
2025 error(ERR_NONFATAL,
2026 "`%%local' missing argument parameter");
2027 free_tlist(origline);
2028 return DIRECTIVE_FOUND;
2029 }
2030 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002031
H. Peter Anvine2c80182005-01-15 22:15:51 +00002032 /* Find the argument size type */
2033 tline = tline->next;
2034 if (!tline || tline->type != TOK_OTHER
2035 || tline->text[0] != ':') {
2036 error(ERR_NONFATAL,
2037 "Syntax error processing `%%local' directive");
2038 free_tlist(origline);
2039 return DIRECTIVE_FOUND;
2040 }
2041 tline = tline->next;
2042 if (!tline || tline->type != TOK_ID) {
2043 error(ERR_NONFATAL,
2044 "`%%local' missing size type parameter");
2045 free_tlist(origline);
2046 return DIRECTIVE_FOUND;
2047 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002048
H. Peter Anvine2c80182005-01-15 22:15:51 +00002049 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002050 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002051 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002052 size = parse_size(tt->text);
2053 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002054 error(ERR_NONFATAL,
2055 "Invalid size type for `%%local' missing directive");
2056 free_tlist(tt);
2057 free_tlist(origline);
2058 return DIRECTIVE_FOUND;
2059 }
2060 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002061
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002062 /* Round up to even stack slots */
2063 size = (size+StackSize-1) & ~(StackSize-1);
2064
2065 offset += size; /* Negative offset, increment before */
2066
2067 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002068 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2069 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002070 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002071
H. Peter Anvine2c80182005-01-15 22:15:51 +00002072 /* Now define the assign to setup the enter_c macro correctly */
2073 snprintf(directive, sizeof(directive),
2074 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002075 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002076
H. Peter Anvine2c80182005-01-15 22:15:51 +00002077 /* Move to the next argument in the list */
2078 tline = tline->next;
2079 if (tline && tline->type == TOK_WHITESPACE)
2080 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002081 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2082 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002083 free_tlist(origline);
2084 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002085
H. Peter Anvine2c80182005-01-15 22:15:51 +00002086 case PP_CLEAR:
2087 if (tline->next)
2088 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002089 free_macros();
2090 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002091 free_tlist(origline);
2092 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002093
H. Peter Anvin418ca702008-05-30 10:42:30 -07002094 case PP_DEPEND:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002095 t = tline->next = expand_smacro(tline->next);
2096 skip_white_(t);
2097 if (!t || (t->type != TOK_STRING &&
2098 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002099 error(ERR_NONFATAL, "`%%depend' expects a file name");
2100 free_tlist(origline);
2101 return DIRECTIVE_FOUND; /* but we did _something_ */
2102 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002103 if (t->next)
H. Peter Anvin418ca702008-05-30 10:42:30 -07002104 error(ERR_WARNING,
2105 "trailing garbage after `%%depend' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002106 p = t->text;
2107 if (t->type != TOK_INTERNAL_STRING)
2108 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002109 if (dephead && !in_list(*dephead, p)) {
2110 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2111 sl->next = NULL;
2112 strcpy(sl->str, p);
2113 *deptail = sl;
2114 deptail = &sl->next;
2115 }
2116 free_tlist(origline);
2117 return DIRECTIVE_FOUND;
2118
2119 case PP_INCLUDE:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002120 t = tline->next = expand_smacro(tline->next);
2121 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002122
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002123 if (!t || (t->type != TOK_STRING &&
2124 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002125 error(ERR_NONFATAL, "`%%include' expects a file name");
2126 free_tlist(origline);
2127 return DIRECTIVE_FOUND; /* but we did _something_ */
2128 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002129 if (t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002130 error(ERR_WARNING,
2131 "trailing garbage after `%%include' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002132 p = t->text;
2133 if (t->type != TOK_INTERNAL_STRING)
2134 nasm_unquote(p, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002135 inc = nasm_malloc(sizeof(Include));
2136 inc->next = istk;
2137 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002138 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002139 if (!inc->fp) {
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002140 /* -MG given but file not found */
2141 nasm_free(inc);
2142 } else {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002143 inc->fname = src_set_fname(nasm_strdup(p));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002144 inc->lineno = src_set_linnum(0);
2145 inc->lineinc = 1;
2146 inc->expansion = NULL;
2147 inc->mstk = NULL;
2148 istk = inc;
2149 list->uplevel(LIST_INCLUDE);
2150 }
2151 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002152 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002153
H. Peter Anvind2456592008-06-19 15:04:18 -07002154 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002155 {
2156 static const char * const *use_pkg;
2157 const char *s;
2158 char *pkg_macro;
2159
H. Peter Anvind2456592008-06-19 15:04:18 -07002160 t = tline->next = expand_smacro(tline->next);
2161 skip_white_(t);
2162
2163 if (!t || (t->type != TOK_STRING &&
2164 t->type != TOK_INTERNAL_STRING &&
2165 t->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002166 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002167 free_tlist(origline);
2168 return DIRECTIVE_FOUND; /* but we did _something_ */
2169 }
2170 if (t->next)
2171 error(ERR_WARNING,
2172 "trailing garbage after `%%use' ignored");
H. Peter Anvind2456592008-06-19 15:04:18 -07002173 if (t->type == TOK_STRING)
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002174 nasm_unquote(t->text, NULL);
2175 use_pkg = nasm_stdmac_find_package(t->text);
2176 if (!use_pkg)
2177 error(ERR_NONFATAL, "unknown `%%use' package: %s", t->text);
2178 p = pkg_macro = nasm_malloc(strlen(t->text) + 9);
2179 strcpy(p, "__USE_"); p += 6;
2180 for (s = t->text; *s; s++)
2181 *p++ = toupper(*s);
2182 strcpy(p, "__");
2183 if (!smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2184 /* Not already included, go ahead and include it */
2185 define_smacro(NULL, pkg_macro, true, 0, NULL);
2186 stdmacpos = use_pkg;
2187 }
2188 nasm_free(pkg_macro);
H. Peter Anvind2456592008-06-19 15:04:18 -07002189 free_tlist(origline);
2190 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002191 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002192 case PP_PUSH:
2193 tline = tline->next;
2194 skip_white_(tline);
2195 tline = expand_id(tline);
2196 if (!tok_type_(tline, TOK_ID)) {
2197 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2198 free_tlist(origline);
2199 return DIRECTIVE_FOUND; /* but we did _something_ */
2200 }
2201 if (tline->next)
2202 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2203 ctx = nasm_malloc(sizeof(Context));
2204 ctx->next = cstk;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002205 hash_init(&ctx->localmac, HASH_SMALL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002206 ctx->name = nasm_strdup(tline->text);
2207 ctx->number = unique++;
2208 cstk = ctx;
2209 free_tlist(origline);
2210 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002211
H. Peter Anvine2c80182005-01-15 22:15:51 +00002212 case PP_REPL:
2213 tline = tline->next;
2214 skip_white_(tline);
2215 tline = expand_id(tline);
2216 if (!tok_type_(tline, TOK_ID)) {
2217 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2218 free_tlist(origline);
2219 return DIRECTIVE_FOUND; /* but we did _something_ */
2220 }
2221 if (tline->next)
2222 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2223 if (!cstk)
2224 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2225 else {
2226 nasm_free(cstk->name);
2227 cstk->name = nasm_strdup(tline->text);
2228 }
2229 free_tlist(origline);
2230 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002231
H. Peter Anvine2c80182005-01-15 22:15:51 +00002232 case PP_POP:
2233 if (tline->next)
2234 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2235 if (!cstk)
2236 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2237 else
2238 ctx_pop();
2239 free_tlist(origline);
2240 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002241
H. Peter Anvine2c80182005-01-15 22:15:51 +00002242 case PP_ERROR:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002243 case PP_WARNING:
2244 {
2245 int severity = PP_ERROR ? ERR_NONFATAL|ERR_NO_SEVERITY :
2246 ERR_WARNING|ERR_NO_SEVERITY;
2247
H. Peter Anvine2c80182005-01-15 22:15:51 +00002248 tline->next = expand_smacro(tline->next);
2249 tline = tline->next;
2250 skip_white_(tline);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002251 t = tline ? tline->next : NULL;
2252 skip_white_(t);
2253 if (tok_type_(tline, TOK_STRING) && !t) {
2254 /* The line contains only a quoted string */
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002255 p = tline->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002256 nasm_unquote(p, NULL);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002257 error(severity, "%s: %s", pp_directives[i], p);
2258 } else {
2259 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002260 p = detoken(tline, false);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002261 error(severity, "%s: %s", pp_directives[i], p);
2262 nasm_free(p);
H. Peter Anvind2456592008-06-19 15:04:18 -07002263 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002264 free_tlist(origline);
2265 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002266 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002267
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002268 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002269 if (istk->conds && !emitting(istk->conds->state))
2270 j = COND_NEVER;
2271 else {
2272 j = if_condition(tline->next, i);
2273 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002274 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2275 }
2276 cond = nasm_malloc(sizeof(Cond));
2277 cond->next = istk->conds;
2278 cond->state = j;
2279 istk->conds = cond;
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002280 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002281 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002282
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002283 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002284 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002285 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002286 if (emitting(istk->conds->state)
2287 || istk->conds->state == COND_NEVER)
2288 istk->conds->state = COND_NEVER;
2289 else {
2290 /*
2291 * IMPORTANT: In the case of %if, we will already have
2292 * called expand_mmac_params(); however, if we're
2293 * processing an %elif we must have been in a
2294 * non-emitting mode, which would have inhibited
2295 * the normal invocation of expand_mmac_params(). Therefore,
2296 * we have to do it explicitly here.
2297 */
2298 j = if_condition(expand_mmac_params(tline->next), i);
2299 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002300 istk->conds->state =
2301 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2302 }
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002303 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002304 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002305
H. Peter Anvine2c80182005-01-15 22:15:51 +00002306 case PP_ELSE:
2307 if (tline->next)
2308 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2309 if (!istk->conds)
2310 error(ERR_FATAL, "`%%else': no matching `%%if'");
2311 if (emitting(istk->conds->state)
2312 || istk->conds->state == COND_NEVER)
2313 istk->conds->state = COND_ELSE_FALSE;
2314 else
2315 istk->conds->state = COND_ELSE_TRUE;
2316 free_tlist(origline);
2317 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002318
H. Peter Anvine2c80182005-01-15 22:15:51 +00002319 case PP_ENDIF:
2320 if (tline->next)
2321 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2322 if (!istk->conds)
2323 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2324 cond = istk->conds;
2325 istk->conds = cond->next;
2326 nasm_free(cond);
2327 free_tlist(origline);
2328 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002329
H. Peter Anvine2c80182005-01-15 22:15:51 +00002330 case PP_MACRO:
2331 case PP_IMACRO:
2332 if (defining)
2333 error(ERR_FATAL,
2334 "`%%%smacro': already defining a macro",
2335 (i == PP_IMACRO ? "i" : ""));
2336 tline = tline->next;
2337 skip_white_(tline);
2338 tline = expand_id(tline);
2339 if (!tok_type_(tline, TOK_ID)) {
2340 error(ERR_NONFATAL,
2341 "`%%%smacro' expects a macro name",
2342 (i == PP_IMACRO ? "i" : ""));
2343 return DIRECTIVE_FOUND;
2344 }
2345 defining = nasm_malloc(sizeof(MMacro));
2346 defining->name = nasm_strdup(tline->text);
2347 defining->casesense = (i == PP_MACRO);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002348 defining->plus = false;
2349 defining->nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002350 defining->in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002351 defining->rep_nest = NULL;
2352 tline = expand_smacro(tline->next);
2353 skip_white_(tline);
2354 if (!tok_type_(tline, TOK_NUMBER)) {
2355 error(ERR_NONFATAL,
2356 "`%%%smacro' expects a parameter count",
2357 (i == PP_IMACRO ? "i" : ""));
2358 defining->nparam_min = defining->nparam_max = 0;
2359 } else {
2360 defining->nparam_min = defining->nparam_max =
H. Peter Anvin70055962007-10-11 00:05:31 -07002361 readnum(tline->text, &err);
2362 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002363 error(ERR_NONFATAL,
2364 "unable to parse parameter count `%s'", tline->text);
2365 }
2366 if (tline && tok_is_(tline->next, "-")) {
2367 tline = tline->next->next;
2368 if (tok_is_(tline, "*"))
2369 defining->nparam_max = INT_MAX;
2370 else if (!tok_type_(tline, TOK_NUMBER))
2371 error(ERR_NONFATAL,
2372 "`%%%smacro' expects a parameter count after `-'",
2373 (i == PP_IMACRO ? "i" : ""));
2374 else {
H. Peter Anvin70055962007-10-11 00:05:31 -07002375 defining->nparam_max = readnum(tline->text, &err);
2376 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002377 error(ERR_NONFATAL,
2378 "unable to parse parameter count `%s'",
2379 tline->text);
2380 if (defining->nparam_min > defining->nparam_max)
2381 error(ERR_NONFATAL,
2382 "minimum parameter count exceeds maximum");
2383 }
2384 }
2385 if (tline && tok_is_(tline->next, "+")) {
2386 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002387 defining->plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002388 }
2389 if (tline && tok_type_(tline->next, TOK_ID) &&
2390 !nasm_stricmp(tline->next->text, ".nolist")) {
2391 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002392 defining->nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002393 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002394 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002395 while (mmac) {
2396 if (!strcmp(mmac->name, defining->name) &&
2397 (mmac->nparam_min <= defining->nparam_max
2398 || defining->plus)
2399 && (defining->nparam_min <= mmac->nparam_max
2400 || mmac->plus)) {
2401 error(ERR_WARNING,
2402 "redefining multi-line macro `%s'", defining->name);
2403 break;
2404 }
2405 mmac = mmac->next;
2406 }
2407 /*
2408 * Handle default parameters.
2409 */
2410 if (tline && tline->next) {
2411 defining->dlist = tline->next;
2412 tline->next = NULL;
2413 count_mmac_params(defining->dlist, &defining->ndefs,
2414 &defining->defaults);
2415 } else {
2416 defining->dlist = NULL;
2417 defining->defaults = NULL;
2418 }
2419 defining->expansion = NULL;
2420 free_tlist(origline);
2421 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002422
H. Peter Anvine2c80182005-01-15 22:15:51 +00002423 case PP_ENDM:
2424 case PP_ENDMACRO:
2425 if (!defining) {
2426 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2427 return DIRECTIVE_FOUND;
2428 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002429 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002430 defining->next = *mmhead;
2431 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002432 defining = NULL;
2433 free_tlist(origline);
2434 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002435
H. Peter Anvine2c80182005-01-15 22:15:51 +00002436 case PP_ROTATE:
2437 if (tline->next && tline->next->type == TOK_WHITESPACE)
2438 tline = tline->next;
2439 if (tline->next == NULL) {
2440 free_tlist(origline);
2441 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2442 return DIRECTIVE_FOUND;
2443 }
2444 t = expand_smacro(tline->next);
2445 tline->next = NULL;
2446 free_tlist(origline);
2447 tline = t;
2448 tptr = &t;
2449 tokval.t_type = TOKEN_INVALID;
2450 evalresult =
2451 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2452 free_tlist(tline);
2453 if (!evalresult)
2454 return DIRECTIVE_FOUND;
2455 if (tokval.t_type)
2456 error(ERR_WARNING,
2457 "trailing garbage after expression ignored");
2458 if (!is_simple(evalresult)) {
2459 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2460 return DIRECTIVE_FOUND;
2461 }
2462 mmac = istk->mstk;
2463 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2464 mmac = mmac->next_active;
2465 if (!mmac) {
2466 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2467 } else if (mmac->nparam == 0) {
2468 error(ERR_NONFATAL,
2469 "`%%rotate' invoked within macro without parameters");
2470 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002471 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002472
H. Peter Anvin25a99342007-09-22 17:45:45 -07002473 rotate %= (int)mmac->nparam;
2474 if (rotate < 0)
2475 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002476
H. Peter Anvin25a99342007-09-22 17:45:45 -07002477 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002478 }
2479 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002480
H. Peter Anvine2c80182005-01-15 22:15:51 +00002481 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002482 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002483 do {
2484 tline = tline->next;
2485 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002486
H. Peter Anvine2c80182005-01-15 22:15:51 +00002487 if (tok_type_(tline, TOK_ID) &&
2488 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002489 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002490 do {
2491 tline = tline->next;
2492 } while (tok_type_(tline, TOK_WHITESPACE));
2493 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002494
H. Peter Anvine2c80182005-01-15 22:15:51 +00002495 if (tline) {
2496 t = expand_smacro(tline);
2497 tptr = &t;
2498 tokval.t_type = TOKEN_INVALID;
2499 evalresult =
2500 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2501 if (!evalresult) {
2502 free_tlist(origline);
2503 return DIRECTIVE_FOUND;
2504 }
2505 if (tokval.t_type)
2506 error(ERR_WARNING,
2507 "trailing garbage after expression ignored");
2508 if (!is_simple(evalresult)) {
2509 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2510 return DIRECTIVE_FOUND;
2511 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002512 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002513 } else {
2514 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002515 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002516 }
2517 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002518
H. Peter Anvine2c80182005-01-15 22:15:51 +00002519 tmp_defining = defining;
2520 defining = nasm_malloc(sizeof(MMacro));
2521 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002522 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002523 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002524 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002525 defining->in_progress = count;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002526 defining->nparam_min = defining->nparam_max = 0;
2527 defining->defaults = NULL;
2528 defining->dlist = NULL;
2529 defining->expansion = NULL;
2530 defining->next_active = istk->mstk;
2531 defining->rep_nest = tmp_defining;
2532 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002533
H. Peter Anvine2c80182005-01-15 22:15:51 +00002534 case PP_ENDREP:
2535 if (!defining || defining->name) {
2536 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2537 return DIRECTIVE_FOUND;
2538 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002539
H. Peter Anvine2c80182005-01-15 22:15:51 +00002540 /*
2541 * Now we have a "macro" defined - although it has no name
2542 * and we won't be entering it in the hash tables - we must
2543 * push a macro-end marker for it on to istk->expansion.
2544 * After that, it will take care of propagating itself (a
2545 * macro-end marker line for a macro which is really a %rep
2546 * block will cause the macro to be re-expanded, complete
2547 * with another macro-end marker to ensure the process
2548 * continues) until the whole expansion is forcibly removed
2549 * from istk->expansion by a %exitrep.
2550 */
2551 l = nasm_malloc(sizeof(Line));
2552 l->next = istk->expansion;
2553 l->finishes = defining;
2554 l->first = NULL;
2555 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002556
H. Peter Anvine2c80182005-01-15 22:15:51 +00002557 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002558
H. Peter Anvine2c80182005-01-15 22:15:51 +00002559 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2560 tmp_defining = defining;
2561 defining = defining->rep_nest;
2562 free_tlist(origline);
2563 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002564
H. Peter Anvine2c80182005-01-15 22:15:51 +00002565 case PP_EXITREP:
2566 /*
2567 * We must search along istk->expansion until we hit a
2568 * macro-end marker for a macro with no name. Then we set
2569 * its `in_progress' flag to 0.
2570 */
2571 for (l = istk->expansion; l; l = l->next)
2572 if (l->finishes && !l->finishes->name)
2573 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002574
H. Peter Anvine2c80182005-01-15 22:15:51 +00002575 if (l)
2576 l->finishes->in_progress = 0;
2577 else
2578 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2579 free_tlist(origline);
2580 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002581
H. Peter Anvine2c80182005-01-15 22:15:51 +00002582 case PP_XDEFINE:
2583 case PP_IXDEFINE:
2584 case PP_DEFINE:
2585 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002586 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002587
H. Peter Anvine2c80182005-01-15 22:15:51 +00002588 tline = tline->next;
2589 skip_white_(tline);
2590 tline = expand_id(tline);
2591 if (!tline || (tline->type != TOK_ID &&
2592 (tline->type != TOK_PREPROC_ID ||
2593 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002594 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2595 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002596 free_tlist(origline);
2597 return DIRECTIVE_FOUND;
2598 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002599
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002600 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002601
H. Peter Anvine2c80182005-01-15 22:15:51 +00002602 mname = tline->text;
2603 last = tline;
2604 param_start = tline = tline->next;
2605 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002606
H. Peter Anvine2c80182005-01-15 22:15:51 +00002607 /* Expand the macro definition now for %xdefine and %ixdefine */
2608 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2609 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002610
H. Peter Anvine2c80182005-01-15 22:15:51 +00002611 if (tok_is_(tline, "(")) {
2612 /*
2613 * This macro has parameters.
2614 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002615
H. Peter Anvine2c80182005-01-15 22:15:51 +00002616 tline = tline->next;
2617 while (1) {
2618 skip_white_(tline);
2619 if (!tline) {
2620 error(ERR_NONFATAL, "parameter identifier expected");
2621 free_tlist(origline);
2622 return DIRECTIVE_FOUND;
2623 }
2624 if (tline->type != TOK_ID) {
2625 error(ERR_NONFATAL,
2626 "`%s': parameter identifier expected",
2627 tline->text);
2628 free_tlist(origline);
2629 return DIRECTIVE_FOUND;
2630 }
2631 tline->type = TOK_SMAC_PARAM + nparam++;
2632 tline = tline->next;
2633 skip_white_(tline);
2634 if (tok_is_(tline, ",")) {
2635 tline = tline->next;
2636 continue;
2637 }
2638 if (!tok_is_(tline, ")")) {
2639 error(ERR_NONFATAL,
2640 "`)' expected to terminate macro template");
2641 free_tlist(origline);
2642 return DIRECTIVE_FOUND;
2643 }
2644 break;
2645 }
2646 last = tline;
2647 tline = tline->next;
2648 }
2649 if (tok_type_(tline, TOK_WHITESPACE))
2650 last = tline, tline = tline->next;
2651 macro_start = NULL;
2652 last->next = NULL;
2653 t = tline;
2654 while (t) {
2655 if (t->type == TOK_ID) {
2656 for (tt = param_start; tt; tt = tt->next)
2657 if (tt->type >= TOK_SMAC_PARAM &&
2658 !strcmp(tt->text, t->text))
2659 t->type = tt->type;
2660 }
2661 tt = t->next;
2662 t->next = macro_start;
2663 macro_start = t;
2664 t = tt;
2665 }
2666 /*
2667 * Good. We now have a macro name, a parameter count, and a
2668 * token list (in reverse order) for an expansion. We ought
2669 * to be OK just to create an SMacro, store it, and let
2670 * free_tlist have the rest of the line (which we have
2671 * carefully re-terminated after chopping off the expansion
2672 * from the end).
2673 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002674 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002675 free_tlist(origline);
2676 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002677
H. Peter Anvine2c80182005-01-15 22:15:51 +00002678 case PP_UNDEF:
2679 tline = tline->next;
2680 skip_white_(tline);
2681 tline = expand_id(tline);
2682 if (!tline || (tline->type != TOK_ID &&
2683 (tline->type != TOK_PREPROC_ID ||
2684 tline->text[1] != '$'))) {
2685 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2686 free_tlist(origline);
2687 return DIRECTIVE_FOUND;
2688 }
2689 if (tline->next) {
2690 error(ERR_WARNING,
2691 "trailing garbage after macro name ignored");
2692 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002693
H. Peter Anvine2c80182005-01-15 22:15:51 +00002694 /* Find the context that symbol belongs to */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002695 ctx = get_ctx(tline->text, false);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002696 undef_smacro(ctx, tline->text);
2697 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002698 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002699
H. Peter Anvin9e200162008-06-04 17:23:14 -07002700 case PP_DEFSTR:
2701 case PP_IDEFSTR:
2702 casesense = (i == PP_DEFSTR);
2703
2704 tline = tline->next;
2705 skip_white_(tline);
2706 tline = expand_id(tline);
2707 if (!tline || (tline->type != TOK_ID &&
2708 (tline->type != TOK_PREPROC_ID ||
2709 tline->text[1] != '$'))) {
2710 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2711 pp_directives[i]);
2712 free_tlist(origline);
2713 return DIRECTIVE_FOUND;
2714 }
2715
2716 ctx = get_ctx(tline->text, false);
2717
2718 mname = tline->text;
2719 last = tline;
2720 tline = expand_smacro(tline->next);
2721 last->next = NULL;
2722
2723 while (tok_type_(tline, TOK_WHITESPACE))
2724 tline = delete_Token(tline);
2725
2726 p = detoken(tline, false);
2727 macro_start = nasm_malloc(sizeof(*macro_start));
2728 macro_start->next = NULL;
2729 macro_start->text = nasm_quote(p, strlen(p));
2730 macro_start->type = TOK_STRING;
2731 macro_start->mac = NULL;
2732 nasm_free(p);
2733
2734 /*
2735 * We now have a macro name, an implicit parameter count of
2736 * zero, and a string token to use as an expansion. Create
2737 * and store an SMacro.
2738 */
2739 define_smacro(ctx, mname, casesense, 0, macro_start);
2740 free_tlist(origline);
2741 return DIRECTIVE_FOUND;
2742
H. Peter Anvin418ca702008-05-30 10:42:30 -07002743 case PP_PATHSEARCH:
2744 {
2745 FILE *fp;
2746 StrList *xsl = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002747 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002748
2749 casesense = true;
2750
2751 tline = tline->next;
2752 skip_white_(tline);
2753 tline = expand_id(tline);
2754 if (!tline || (tline->type != TOK_ID &&
2755 (tline->type != TOK_PREPROC_ID ||
2756 tline->text[1] != '$'))) {
2757 error(ERR_NONFATAL,
2758 "`%%pathsearch' expects a macro identifier as first parameter");
2759 free_tlist(origline);
2760 return DIRECTIVE_FOUND;
2761 }
2762 ctx = get_ctx(tline->text, false);
2763
2764 mname = tline->text;
2765 last = tline;
2766 tline = expand_smacro(tline->next);
2767 last->next = NULL;
2768
2769 t = tline;
2770 while (tok_type_(t, TOK_WHITESPACE))
2771 t = t->next;
2772
2773 if (!t || (t->type != TOK_STRING &&
2774 t->type != TOK_INTERNAL_STRING)) {
2775 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
2776 free_tlist(tline);
2777 free_tlist(origline);
2778 return DIRECTIVE_FOUND; /* but we did _something_ */
2779 }
2780 if (t->next)
2781 error(ERR_WARNING,
2782 "trailing garbage after `%%pathsearch' ignored");
H. Peter Anvin427cc912008-06-01 21:43:03 -07002783 p = t->text;
2784 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002785 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002786
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002787 fp = inc_fopen(p, &xsl, &xst, true);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002788 if (fp) {
2789 p = xsl->str;
2790 fclose(fp); /* Don't actually care about the file */
2791 }
2792 macro_start = nasm_malloc(sizeof(*macro_start));
2793 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002794 macro_start->text = nasm_quote(p, strlen(p));
H. Peter Anvin418ca702008-05-30 10:42:30 -07002795 macro_start->type = TOK_STRING;
2796 macro_start->mac = NULL;
2797 if (xsl)
2798 nasm_free(xsl);
2799
2800 /*
2801 * We now have a macro name, an implicit parameter count of
2802 * zero, and a string token to use as an expansion. Create
2803 * and store an SMacro.
2804 */
2805 define_smacro(ctx, mname, casesense, 0, macro_start);
2806 free_tlist(tline);
2807 free_tlist(origline);
2808 return DIRECTIVE_FOUND;
2809 }
2810
H. Peter Anvine2c80182005-01-15 22:15:51 +00002811 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002812 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07002813
H. Peter Anvine2c80182005-01-15 22:15:51 +00002814 tline = tline->next;
2815 skip_white_(tline);
2816 tline = expand_id(tline);
2817 if (!tline || (tline->type != TOK_ID &&
2818 (tline->type != TOK_PREPROC_ID ||
2819 tline->text[1] != '$'))) {
2820 error(ERR_NONFATAL,
2821 "`%%strlen' expects a macro identifier as first parameter");
2822 free_tlist(origline);
2823 return DIRECTIVE_FOUND;
2824 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002825 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002826
H. Peter Anvine2c80182005-01-15 22:15:51 +00002827 mname = tline->text;
2828 last = tline;
2829 tline = expand_smacro(tline->next);
2830 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002831
H. Peter Anvine2c80182005-01-15 22:15:51 +00002832 t = tline;
2833 while (tok_type_(t, TOK_WHITESPACE))
2834 t = t->next;
2835 /* t should now point to the string */
2836 if (t->type != TOK_STRING) {
2837 error(ERR_NONFATAL,
2838 "`%%strlen` requires string as second parameter");
2839 free_tlist(tline);
2840 free_tlist(origline);
2841 return DIRECTIVE_FOUND;
2842 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002843
H. Peter Anvine2c80182005-01-15 22:15:51 +00002844 macro_start = nasm_malloc(sizeof(*macro_start));
2845 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002846 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002847 macro_start->mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002848
H. Peter Anvine2c80182005-01-15 22:15:51 +00002849 /*
2850 * We now have a macro name, an implicit parameter count of
2851 * zero, and a numeric token to use as an expansion. Create
2852 * and store an SMacro.
2853 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002854 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002855 free_tlist(tline);
2856 free_tlist(origline);
2857 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002858
H. Peter Anvine2c80182005-01-15 22:15:51 +00002859 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002860 {
2861 int64_t a1, a2;
2862 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07002863
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002864 casesense = true;
2865
H. Peter Anvine2c80182005-01-15 22:15:51 +00002866 tline = tline->next;
2867 skip_white_(tline);
2868 tline = expand_id(tline);
2869 if (!tline || (tline->type != TOK_ID &&
2870 (tline->type != TOK_PREPROC_ID ||
2871 tline->text[1] != '$'))) {
2872 error(ERR_NONFATAL,
2873 "`%%substr' expects a macro identifier as first parameter");
2874 free_tlist(origline);
2875 return DIRECTIVE_FOUND;
2876 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002877 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002878
H. Peter Anvine2c80182005-01-15 22:15:51 +00002879 mname = tline->text;
2880 last = tline;
2881 tline = expand_smacro(tline->next);
2882 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002883
H. Peter Anvine2c80182005-01-15 22:15:51 +00002884 t = tline->next;
2885 while (tok_type_(t, TOK_WHITESPACE))
2886 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002887
H. Peter Anvine2c80182005-01-15 22:15:51 +00002888 /* t should now point to the string */
2889 if (t->type != TOK_STRING) {
2890 error(ERR_NONFATAL,
2891 "`%%substr` requires string as second parameter");
2892 free_tlist(tline);
2893 free_tlist(origline);
2894 return DIRECTIVE_FOUND;
2895 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002896
H. Peter Anvine2c80182005-01-15 22:15:51 +00002897 tt = t->next;
2898 tptr = &tt;
2899 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002900 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
2901 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002902 if (!evalresult) {
2903 free_tlist(tline);
2904 free_tlist(origline);
2905 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002906 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002907 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2908 free_tlist(tline);
2909 free_tlist(origline);
2910 return DIRECTIVE_FOUND;
2911 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002912 a1 = evalresult->value-1;
2913
2914 while (tok_type_(tt, TOK_WHITESPACE))
2915 tt = tt->next;
2916 if (!tt) {
2917 a2 = 1; /* Backwards compatibility: one character */
2918 } else {
2919 tokval.t_type = TOKEN_INVALID;
2920 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
2921 pass, error, NULL);
2922 if (!evalresult) {
2923 free_tlist(tline);
2924 free_tlist(origline);
2925 return DIRECTIVE_FOUND;
2926 } else if (!is_simple(evalresult)) {
2927 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2928 free_tlist(tline);
2929 free_tlist(origline);
2930 return DIRECTIVE_FOUND;
2931 }
2932 a2 = evalresult->value;
2933 }
2934
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002935 len = nasm_unquote(t->text, NULL);
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002936 if (a2 < 0)
2937 a2 = a2+1+len-a1;
2938 if (a1+a2 > (int64_t)len)
2939 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002940
H. Peter Anvine2c80182005-01-15 22:15:51 +00002941 macro_start = nasm_malloc(sizeof(*macro_start));
2942 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002943 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002944 macro_start->type = TOK_STRING;
2945 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002946
H. Peter Anvine2c80182005-01-15 22:15:51 +00002947 /*
2948 * We now have a macro name, an implicit parameter count of
2949 * zero, and a numeric token to use as an expansion. Create
2950 * and store an SMacro.
2951 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002952 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002953 free_tlist(tline);
2954 free_tlist(origline);
2955 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002956 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002957
H. Peter Anvine2c80182005-01-15 22:15:51 +00002958 case PP_ASSIGN:
2959 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002960 casesense = (i == PP_ASSIGN);
2961
H. Peter Anvine2c80182005-01-15 22:15:51 +00002962 tline = tline->next;
2963 skip_white_(tline);
2964 tline = expand_id(tline);
2965 if (!tline || (tline->type != TOK_ID &&
2966 (tline->type != TOK_PREPROC_ID ||
2967 tline->text[1] != '$'))) {
2968 error(ERR_NONFATAL,
2969 "`%%%sassign' expects a macro identifier",
2970 (i == PP_IASSIGN ? "i" : ""));
2971 free_tlist(origline);
2972 return DIRECTIVE_FOUND;
2973 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002974 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002975
H. Peter Anvine2c80182005-01-15 22:15:51 +00002976 mname = tline->text;
2977 last = tline;
2978 tline = expand_smacro(tline->next);
2979 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002980
H. Peter Anvine2c80182005-01-15 22:15:51 +00002981 t = tline;
2982 tptr = &t;
2983 tokval.t_type = TOKEN_INVALID;
2984 evalresult =
2985 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2986 free_tlist(tline);
2987 if (!evalresult) {
2988 free_tlist(origline);
2989 return DIRECTIVE_FOUND;
2990 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002991
H. Peter Anvine2c80182005-01-15 22:15:51 +00002992 if (tokval.t_type)
2993 error(ERR_WARNING,
2994 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00002995
H. Peter Anvine2c80182005-01-15 22:15:51 +00002996 if (!is_simple(evalresult)) {
2997 error(ERR_NONFATAL,
2998 "non-constant value given to `%%%sassign'",
2999 (i == PP_IASSIGN ? "i" : ""));
3000 free_tlist(origline);
3001 return DIRECTIVE_FOUND;
3002 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003003
H. Peter Anvine2c80182005-01-15 22:15:51 +00003004 macro_start = nasm_malloc(sizeof(*macro_start));
3005 macro_start->next = NULL;
3006 make_tok_num(macro_start, reloc_value(evalresult));
3007 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003008
H. Peter Anvine2c80182005-01-15 22:15:51 +00003009 /*
3010 * We now have a macro name, an implicit parameter count of
3011 * zero, and a numeric token to use as an expansion. Create
3012 * and store an SMacro.
3013 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003014 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003015 free_tlist(origline);
3016 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003017
H. Peter Anvine2c80182005-01-15 22:15:51 +00003018 case PP_LINE:
3019 /*
3020 * Syntax is `%line nnn[+mmm] [filename]'
3021 */
3022 tline = tline->next;
3023 skip_white_(tline);
3024 if (!tok_type_(tline, TOK_NUMBER)) {
3025 error(ERR_NONFATAL, "`%%line' expects line number");
3026 free_tlist(origline);
3027 return DIRECTIVE_FOUND;
3028 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003029 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003030 m = 1;
3031 tline = tline->next;
3032 if (tok_is_(tline, "+")) {
3033 tline = tline->next;
3034 if (!tok_type_(tline, TOK_NUMBER)) {
3035 error(ERR_NONFATAL, "`%%line' expects line increment");
3036 free_tlist(origline);
3037 return DIRECTIVE_FOUND;
3038 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003039 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003040 tline = tline->next;
3041 }
3042 skip_white_(tline);
3043 src_set_linnum(k);
3044 istk->lineinc = m;
3045 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003046 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003047 }
3048 free_tlist(origline);
3049 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003050
H. Peter Anvine2c80182005-01-15 22:15:51 +00003051 default:
3052 error(ERR_FATAL,
3053 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003054 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003055 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003056 }
Ed Beroset3ab3f412002-06-11 03:31:49 +00003057 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003058}
3059
3060/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003061 * Ensure that a macro parameter contains a condition code and
3062 * nothing else. Return the condition code index if so, or -1
3063 * otherwise.
3064 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003065static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003066{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003067 Token *tt;
3068 int i, j, k, m;
3069
H. Peter Anvin25a99342007-09-22 17:45:45 -07003070 if (!t)
3071 return -1; /* Probably a %+ without a space */
3072
H. Peter Anvineba20a72002-04-30 20:53:55 +00003073 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003074 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003075 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003076 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003077 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003078 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003079 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003080
3081 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00003082 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003083 while (j - i > 1) {
3084 k = (j + i) / 2;
3085 m = nasm_stricmp(t->text, conditions[k]);
3086 if (m == 0) {
3087 i = k;
3088 j = -2;
3089 break;
3090 } else if (m < 0) {
3091 j = k;
3092 } else
3093 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003094 }
3095 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003096 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003097 return i;
3098}
3099
3100/*
3101 * Expand MMacro-local things: parameter references (%0, %n, %+n,
3102 * %-n) and MMacro-local identifiers (%%foo).
3103 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003104static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003105{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003106 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003107
3108 tail = &thead;
3109 thead = NULL;
3110
H. Peter Anvine2c80182005-01-15 22:15:51 +00003111 while (tline) {
3112 if (tline->type == TOK_PREPROC_ID &&
3113 (((tline->text[1] == '+' || tline->text[1] == '-')
3114 && tline->text[2]) || tline->text[1] == '%'
3115 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003116 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003117 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003118 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003119 unsigned int n;
3120 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003121 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003122
H. Peter Anvine2c80182005-01-15 22:15:51 +00003123 t = tline;
3124 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003125
H. Peter Anvine2c80182005-01-15 22:15:51 +00003126 mac = istk->mstk;
3127 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3128 mac = mac->next_active;
3129 if (!mac)
3130 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3131 else
3132 switch (t->text[1]) {
3133 /*
3134 * We have to make a substitution of one of the
3135 * forms %1, %-1, %+1, %%foo, %0.
3136 */
3137 case '0':
3138 type = TOK_NUMBER;
3139 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3140 text = nasm_strdup(tmpbuf);
3141 break;
3142 case '%':
3143 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003144 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00003145 mac->unique);
3146 text = nasm_strcat(tmpbuf, t->text + 2);
3147 break;
3148 case '-':
3149 n = atoi(t->text + 2) - 1;
3150 if (n >= mac->nparam)
3151 tt = NULL;
3152 else {
3153 if (mac->nparam > 1)
3154 n = (n + mac->rotate) % mac->nparam;
3155 tt = mac->params[n];
3156 }
3157 cc = find_cc(tt);
3158 if (cc == -1) {
3159 error(ERR_NONFATAL,
3160 "macro parameter %d is not a condition code",
3161 n + 1);
3162 text = NULL;
3163 } else {
3164 type = TOK_ID;
3165 if (inverse_ccs[cc] == -1) {
3166 error(ERR_NONFATAL,
3167 "condition code `%s' is not invertible",
3168 conditions[cc]);
3169 text = NULL;
3170 } else
3171 text =
3172 nasm_strdup(conditions[inverse_ccs[cc]]);
3173 }
3174 break;
3175 case '+':
3176 n = atoi(t->text + 2) - 1;
3177 if (n >= mac->nparam)
3178 tt = NULL;
3179 else {
3180 if (mac->nparam > 1)
3181 n = (n + mac->rotate) % mac->nparam;
3182 tt = mac->params[n];
3183 }
3184 cc = find_cc(tt);
3185 if (cc == -1) {
3186 error(ERR_NONFATAL,
3187 "macro parameter %d is not a condition code",
3188 n + 1);
3189 text = NULL;
3190 } else {
3191 type = TOK_ID;
3192 text = nasm_strdup(conditions[cc]);
3193 }
3194 break;
3195 default:
3196 n = atoi(t->text + 1) - 1;
3197 if (n >= mac->nparam)
3198 tt = NULL;
3199 else {
3200 if (mac->nparam > 1)
3201 n = (n + mac->rotate) % mac->nparam;
3202 tt = mac->params[n];
3203 }
3204 if (tt) {
3205 for (i = 0; i < mac->paramlen[n]; i++) {
3206 *tail = new_Token(NULL, tt->type, tt->text, 0);
3207 tail = &(*tail)->next;
3208 tt = tt->next;
3209 }
3210 }
3211 text = NULL; /* we've done it here */
3212 break;
3213 }
3214 if (!text) {
3215 delete_Token(t);
3216 } else {
3217 *tail = t;
3218 tail = &t->next;
3219 t->type = type;
3220 nasm_free(t->text);
3221 t->text = text;
3222 t->mac = NULL;
3223 }
3224 continue;
3225 } else {
3226 t = *tail = tline;
3227 tline = tline->next;
3228 t->mac = NULL;
3229 tail = &t->next;
3230 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003231 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003232 *tail = NULL;
3233 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003234 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003235 switch (t->type) {
3236 case TOK_WHITESPACE:
3237 if (tt->type == TOK_WHITESPACE) {
3238 t->next = delete_Token(tt);
3239 }
3240 break;
3241 case TOK_ID:
3242 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003243 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003244 nasm_free(t->text);
3245 t->text = tmp;
3246 t->next = delete_Token(tt);
3247 }
3248 break;
3249 case TOK_NUMBER:
3250 if (tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003251 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003252 nasm_free(t->text);
3253 t->text = tmp;
3254 t->next = delete_Token(tt);
3255 }
3256 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003257 default:
3258 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003259 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003260
H. Peter Anvin76690a12002-04-30 20:52:49 +00003261 return thead;
3262}
3263
3264/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003265 * Expand all single-line macro calls made in the given line.
3266 * Return the expanded version of the line. The original is deemed
3267 * to be destroyed in the process. (In reality we'll just move
3268 * Tokens from input to output a lot of the time, rather than
3269 * actually bothering to destroy and replicate.)
3270 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003271#define DEADMAN_LIMIT (1 << 20)
3272
H. Peter Anvine2c80182005-01-15 22:15:51 +00003273static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003274{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003275 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003276 struct hash_table *smtbl;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003277 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003278 Token **params;
3279 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003280 unsigned int nparam, sparam;
3281 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003282 Token *org_tline = tline;
3283 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003284 char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003285 int deadman = DEADMAN_LIMIT;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003286
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003287 /*
3288 * Trick: we should avoid changing the start token pointer since it can
3289 * be contained in "next" field of other token. Because of this
3290 * we allocate a copy of first token and work with it; at the end of
3291 * routine we copy it back
3292 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003293 if (org_tline) {
3294 tline =
3295 new_Token(org_tline->next, org_tline->type, org_tline->text,
3296 0);
3297 tline->mac = org_tline->mac;
3298 nasm_free(org_tline->text);
3299 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003300 }
3301
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003302again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003303 tail = &thead;
3304 thead = NULL;
3305
H. Peter Anvine2c80182005-01-15 22:15:51 +00003306 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003307 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003308 error(ERR_NONFATAL, "interminable macro recursion");
3309 break;
3310 }
3311
H. Peter Anvine2c80182005-01-15 22:15:51 +00003312 if ((mname = tline->text)) {
3313 /* if this token is a local macro, look in local context */
H. Peter Anvinb037a672008-05-29 19:08:08 -07003314 ctx = NULL;
3315 smtbl = &smacros;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003316 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003317 ctx = get_ctx(mname, true);
H. Peter Anvinb037a672008-05-29 19:08:08 -07003318 if (ctx)
3319 smtbl = &ctx->localmac;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003320 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07003321 head = (SMacro *) hash_findix(smtbl, mname);
H. Peter Anvin072771e2008-05-22 13:17:51 -07003322
H. Peter Anvine2c80182005-01-15 22:15:51 +00003323 /*
3324 * We've hit an identifier. As in is_mmacro below, we first
3325 * check whether the identifier is a single-line macro at
3326 * all, then think about checking for parameters if
3327 * necessary.
3328 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003329 for (m = head; m; m = m->next)
3330 if (!mstrcmp(m->name, mname, m->casesense))
3331 break;
3332 if (m) {
3333 mstart = tline;
3334 params = NULL;
3335 paramsize = NULL;
3336 if (m->nparam == 0) {
3337 /*
3338 * Simple case: the macro is parameterless. Discard the
3339 * one token that the macro call took, and push the
3340 * expansion back on the to-do stack.
3341 */
3342 if (!m->expansion) {
3343 if (!strcmp("__FILE__", m->name)) {
3344 int32_t num = 0;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003345 char *file;
3346 src_get(&num, &file);
3347 tline->text = nasm_quote(file, strlen(file));
H. Peter Anvin97a23472007-09-16 17:57:25 -07003348 tline->type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003349 nasm_free(file);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003350 continue;
3351 }
3352 if (!strcmp("__LINE__", m->name)) {
3353 nasm_free(tline->text);
3354 make_tok_num(tline, src_get_linnum());
3355 continue;
3356 }
3357 if (!strcmp("__BITS__", m->name)) {
3358 nasm_free(tline->text);
3359 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003360 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003361 }
3362 tline = delete_Token(tline);
3363 continue;
3364 }
3365 } else {
3366 /*
3367 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003368 * exists and takes parameters. We must find the
3369 * parameters in the call, count them, find the SMacro
3370 * that corresponds to that form of the macro call, and
3371 * substitute for the parameters when we expand. What a
3372 * pain.
3373 */
3374 /*tline = tline->next;
3375 skip_white_(tline); */
3376 do {
3377 t = tline->next;
3378 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003379 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003380 t->text = NULL;
3381 t = tline->next = delete_Token(t);
3382 }
3383 tline = t;
3384 } while (tok_type_(tline, TOK_WHITESPACE));
3385 if (!tok_is_(tline, "(")) {
3386 /*
3387 * This macro wasn't called with parameters: ignore
3388 * the call. (Behaviour borrowed from gnu cpp.)
3389 */
3390 tline = mstart;
3391 m = NULL;
3392 } else {
3393 int paren = 0;
3394 int white = 0;
3395 brackets = 0;
3396 nparam = 0;
3397 sparam = PARAM_DELTA;
3398 params = nasm_malloc(sparam * sizeof(Token *));
3399 params[0] = tline->next;
3400 paramsize = nasm_malloc(sparam * sizeof(int));
3401 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003402 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003403 /*
3404 * For some unusual expansions
3405 * which concatenates function call
3406 */
3407 t = tline->next;
3408 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003409 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003410 t->text = NULL;
3411 t = tline->next = delete_Token(t);
3412 }
3413 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003414
H. Peter Anvine2c80182005-01-15 22:15:51 +00003415 if (!tline) {
3416 error(ERR_NONFATAL,
3417 "macro call expects terminating `)'");
3418 break;
3419 }
3420 if (tline->type == TOK_WHITESPACE
3421 && brackets <= 0) {
3422 if (paramsize[nparam])
3423 white++;
3424 else
3425 params[nparam] = tline->next;
3426 continue; /* parameter loop */
3427 }
3428 if (tline->type == TOK_OTHER
3429 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003430 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003431 if (ch == ',' && !paren && brackets <= 0) {
3432 if (++nparam >= sparam) {
3433 sparam += PARAM_DELTA;
3434 params = nasm_realloc(params,
3435 sparam *
3436 sizeof(Token
3437 *));
3438 paramsize =
3439 nasm_realloc(paramsize,
3440 sparam *
3441 sizeof(int));
3442 }
3443 params[nparam] = tline->next;
3444 paramsize[nparam] = 0;
3445 white = 0;
3446 continue; /* parameter loop */
3447 }
3448 if (ch == '{' &&
3449 (brackets > 0 || (brackets == 0 &&
3450 !paramsize[nparam])))
3451 {
3452 if (!(brackets++)) {
3453 params[nparam] = tline->next;
3454 continue; /* parameter loop */
3455 }
3456 }
3457 if (ch == '}' && brackets > 0)
3458 if (--brackets == 0) {
3459 brackets = -1;
3460 continue; /* parameter loop */
3461 }
3462 if (ch == '(' && !brackets)
3463 paren++;
3464 if (ch == ')' && brackets <= 0)
3465 if (--paren < 0)
3466 break;
3467 }
3468 if (brackets < 0) {
3469 brackets = 0;
3470 error(ERR_NONFATAL, "braces do not "
3471 "enclose all of macro parameter");
3472 }
3473 paramsize[nparam] += white + 1;
3474 white = 0;
3475 } /* parameter loop */
3476 nparam++;
3477 while (m && (m->nparam != nparam ||
3478 mstrcmp(m->name, mname,
3479 m->casesense)))
3480 m = m->next;
3481 if (!m)
3482 error(ERR_WARNING | ERR_WARN_MNP,
3483 "macro `%s' exists, "
3484 "but not taking %d parameters",
3485 mstart->text, nparam);
3486 }
3487 }
3488 if (m && m->in_progress)
3489 m = NULL;
3490 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003491 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003492 * Design question: should we handle !tline, which
3493 * indicates missing ')' here, or expand those
3494 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003495 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003496 */
3497 nasm_free(params);
3498 nasm_free(paramsize);
3499 tline = mstart;
3500 } else {
3501 /*
3502 * Expand the macro: we are placed on the last token of the
3503 * call, so that we can easily split the call from the
3504 * following tokens. We also start by pushing an SMAC_END
3505 * token for the cycle removal.
3506 */
3507 t = tline;
3508 if (t) {
3509 tline = t->next;
3510 t->next = NULL;
3511 }
3512 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3513 tt->mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003514 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003515 tline = tt;
3516 for (t = m->expansion; t; t = t->next) {
3517 if (t->type >= TOK_SMAC_PARAM) {
3518 Token *pcopy = tline, **ptail = &pcopy;
3519 Token *ttt, *pt;
3520 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003521
H. Peter Anvine2c80182005-01-15 22:15:51 +00003522 ttt = params[t->type - TOK_SMAC_PARAM];
3523 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3524 --i >= 0;) {
3525 pt = *ptail =
3526 new_Token(tline, ttt->type, ttt->text,
3527 0);
3528 ptail = &pt->next;
3529 ttt = ttt->next;
3530 }
3531 tline = pcopy;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -07003532 } else if (t->type == TOK_PREPROC_Q) {
3533 tt = new_Token(tline, TOK_ID, mname, 0);
3534 tline = tt;
3535 } else if (t->type == TOK_PREPROC_QQ) {
3536 tt = new_Token(tline, TOK_ID, m->name, 0);
3537 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003538 } else {
3539 tt = new_Token(tline, t->type, t->text, 0);
3540 tline = tt;
3541 }
3542 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003543
H. Peter Anvine2c80182005-01-15 22:15:51 +00003544 /*
3545 * Having done that, get rid of the macro call, and clean
3546 * up the parameters.
3547 */
3548 nasm_free(params);
3549 nasm_free(paramsize);
3550 free_tlist(mstart);
3551 continue; /* main token loop */
3552 }
3553 }
3554 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003555
H. Peter Anvine2c80182005-01-15 22:15:51 +00003556 if (tline->type == TOK_SMAC_END) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003557 tline->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003558 tline = delete_Token(tline);
3559 } else {
3560 t = *tail = tline;
3561 tline = tline->next;
3562 t->mac = NULL;
3563 t->next = NULL;
3564 tail = &t->next;
3565 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003566 }
3567
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003568 /*
3569 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003570 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003571 * TOK_IDs should be concatenated.
3572 * Also we look for %+ tokens and concatenate the tokens before and after
3573 * them (without white spaces in between).
3574 */
3575 t = thead;
3576 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003577 while (t) {
3578 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3579 t = t->next;
3580 if (!t || !t->next)
3581 break;
3582 if (t->next->type == TOK_ID ||
3583 t->next->type == TOK_PREPROC_ID ||
3584 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003585 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003586 nasm_free(t->text);
3587 t->next = delete_Token(t->next);
3588 t->text = p;
3589 rescan = 1;
3590 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3591 t->next->next->type == TOK_PREPROC_ID &&
3592 strcmp(t->next->next->text, "%+") == 0) {
3593 /* free the next whitespace, the %+ token and next whitespace */
3594 int i;
3595 for (i = 1; i <= 3; i++) {
3596 if (!t->next
3597 || (i != 2 && t->next->type != TOK_WHITESPACE))
3598 break;
3599 t->next = delete_Token(t->next);
3600 } /* endfor */
3601 } else
3602 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003603 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003604 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003605 if (rescan) {
3606 tline = thead;
3607 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003608 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003609
H. Peter Anvine2c80182005-01-15 22:15:51 +00003610 if (org_tline) {
3611 if (thead) {
3612 *org_tline = *thead;
3613 /* since we just gave text to org_line, don't free it */
3614 thead->text = NULL;
3615 delete_Token(thead);
3616 } else {
3617 /* the expression expanded to empty line;
3618 we can't return NULL for some reasons
3619 we just set the line to a single WHITESPACE token. */
3620 memset(org_tline, 0, sizeof(*org_tline));
3621 org_tline->text = NULL;
3622 org_tline->type = TOK_WHITESPACE;
3623 }
3624 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003625 }
3626
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003627 return thead;
3628}
3629
3630/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003631 * Similar to expand_smacro but used exclusively with macro identifiers
3632 * right before they are fetched in. The reason is that there can be
3633 * identifiers consisting of several subparts. We consider that if there
3634 * are more than one element forming the name, user wants a expansion,
3635 * otherwise it will be left as-is. Example:
3636 *
3637 * %define %$abc cde
3638 *
3639 * the identifier %$abc will be left as-is so that the handler for %define
3640 * will suck it and define the corresponding value. Other case:
3641 *
3642 * %define _%$abc cde
3643 *
3644 * In this case user wants name to be expanded *before* %define starts
3645 * working, so we'll expand %$abc into something (if it has a value;
3646 * otherwise it will be left as-is) then concatenate all successive
3647 * PP_IDs into one.
3648 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003649static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003650{
3651 Token *cur, *oldnext = NULL;
3652
H. Peter Anvin734b1882002-04-30 21:01:08 +00003653 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003654 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003655
3656 cur = tline;
3657 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003658 (cur->next->type == TOK_ID ||
3659 cur->next->type == TOK_PREPROC_ID
3660 || cur->next->type == TOK_NUMBER))
3661 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003662
3663 /* If identifier consists of just one token, don't expand */
3664 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003665 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003666
H. Peter Anvine2c80182005-01-15 22:15:51 +00003667 if (cur) {
3668 oldnext = cur->next; /* Detach the tail past identifier */
3669 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003670 }
3671
H. Peter Anvin734b1882002-04-30 21:01:08 +00003672 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003673
H. Peter Anvine2c80182005-01-15 22:15:51 +00003674 if (cur) {
3675 /* expand_smacro possibly changhed tline; re-scan for EOL */
3676 cur = tline;
3677 while (cur && cur->next)
3678 cur = cur->next;
3679 if (cur)
3680 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003681 }
3682
3683 return tline;
3684}
3685
3686/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003687 * Determine whether the given line constitutes a multi-line macro
3688 * call, and return the MMacro structure called if so. Doesn't have
3689 * to check for an initial label - that's taken care of in
3690 * expand_mmacro - but must check numbers of parameters. Guaranteed
3691 * to be called with tline->type == TOK_ID, so the putative macro
3692 * name is easy to find.
3693 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003694static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003695{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003696 MMacro *head, *m;
3697 Token **params;
3698 int nparam;
3699
H. Peter Anvin166c2472008-05-28 12:28:58 -07003700 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003701
3702 /*
3703 * Efficiency: first we see if any macro exists with the given
3704 * name. If not, we can return NULL immediately. _Then_ we
3705 * count the parameters, and then we look further along the
3706 * list if necessary to find the proper MMacro.
3707 */
3708 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003709 if (!mstrcmp(m->name, tline->text, m->casesense))
3710 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003711 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003712 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003713
3714 /*
3715 * OK, we have a potential macro. Count and demarcate the
3716 * parameters.
3717 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003718 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003719
3720 /*
3721 * So we know how many parameters we've got. Find the MMacro
3722 * structure that handles this number.
3723 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003724 while (m) {
3725 if (m->nparam_min <= nparam
3726 && (m->plus || nparam <= m->nparam_max)) {
3727 /*
3728 * This one is right. Just check if cycle removal
3729 * prohibits us using it before we actually celebrate...
3730 */
3731 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003732#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003733 error(ERR_NONFATAL,
3734 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003735#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003736 nasm_free(params);
3737 return NULL;
3738 }
3739 /*
3740 * It's right, and we can use it. Add its default
3741 * parameters to the end of our list if necessary.
3742 */
3743 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3744 params =
3745 nasm_realloc(params,
3746 ((m->nparam_min + m->ndefs +
3747 1) * sizeof(*params)));
3748 while (nparam < m->nparam_min + m->ndefs) {
3749 params[nparam] = m->defaults[nparam - m->nparam_min];
3750 nparam++;
3751 }
3752 }
3753 /*
3754 * If we've gone over the maximum parameter count (and
3755 * we're in Plus mode), ignore parameters beyond
3756 * nparam_max.
3757 */
3758 if (m->plus && nparam > m->nparam_max)
3759 nparam = m->nparam_max;
3760 /*
3761 * Then terminate the parameter list, and leave.
3762 */
3763 if (!params) { /* need this special case */
3764 params = nasm_malloc(sizeof(*params));
3765 nparam = 0;
3766 }
3767 params[nparam] = NULL;
3768 *params_array = params;
3769 return m;
3770 }
3771 /*
3772 * This one wasn't right: look for the next one with the
3773 * same name.
3774 */
3775 for (m = m->next; m; m = m->next)
3776 if (!mstrcmp(m->name, tline->text, m->casesense))
3777 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003778 }
3779
3780 /*
3781 * After all that, we didn't find one with the right number of
3782 * parameters. Issue a warning, and fail to expand the macro.
3783 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003784 error(ERR_WARNING | ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003785 "macro `%s' exists, but not taking %d parameters",
3786 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003787 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003788 return NULL;
3789}
3790
3791/*
3792 * Expand the multi-line macro call made by the given line, if
3793 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003794 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003795 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003796static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003797{
3798 Token *startline = tline;
3799 Token *label = NULL;
3800 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003801 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003802 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003803 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003804 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07003805 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003806
3807 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003808 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07003809 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003810 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003811 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003812 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003813 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07003814 if (m) {
3815 mname = t->text;
3816 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003817 Token *last;
3818 /*
3819 * We have an id which isn't a macro call. We'll assume
3820 * it might be a label; we'll also check to see if a
3821 * colon follows it. Then, if there's another id after
3822 * that lot, we'll check it again for macro-hood.
3823 */
3824 label = last = t;
3825 t = t->next;
3826 if (tok_type_(t, TOK_WHITESPACE))
3827 last = t, t = t->next;
3828 if (tok_is_(t, ":")) {
3829 dont_prepend = 1;
3830 last = t, t = t->next;
3831 if (tok_type_(t, TOK_WHITESPACE))
3832 last = t, t = t->next;
3833 }
3834 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3835 return 0;
3836 last->next = NULL;
H. Peter Anvinc751e862008-06-09 10:18:45 -07003837 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003838 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003839 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003840
3841 /*
3842 * Fix up the parameters: this involves stripping leading and
3843 * trailing whitespace, then stripping braces if they are
3844 * present.
3845 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003846 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003847 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003848
H. Peter Anvine2c80182005-01-15 22:15:51 +00003849 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003850 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003851 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003852
H. Peter Anvine2c80182005-01-15 22:15:51 +00003853 t = params[i];
3854 skip_white_(t);
3855 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003856 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003857 params[i] = t;
3858 paramlen[i] = 0;
3859 while (t) {
3860 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3861 break; /* ... because we have hit a comma */
3862 if (comma && t->type == TOK_WHITESPACE
3863 && tok_is_(t->next, ","))
3864 break; /* ... or a space then a comma */
3865 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3866 break; /* ... or a brace */
3867 t = t->next;
3868 paramlen[i]++;
3869 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003870 }
3871
3872 /*
3873 * OK, we have a MMacro structure together with a set of
3874 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003875 * copies of each Line on to istk->expansion. Substitution of
3876 * parameter tokens and macro-local tokens doesn't get done
3877 * until the single-line macro substitution process; this is
3878 * because delaying them allows us to change the semantics
3879 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003880 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003881 * First, push an end marker on to istk->expansion, mark this
3882 * macro as in progress, and set up its invocation-specific
3883 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003884 */
3885 ll = nasm_malloc(sizeof(Line));
3886 ll->next = istk->expansion;
3887 ll->finishes = m;
3888 ll->first = NULL;
3889 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003890
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003891 m->in_progress = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003892 m->params = params;
3893 m->iline = tline;
3894 m->nparam = nparam;
3895 m->rotate = 0;
3896 m->paramlen = paramlen;
3897 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003898 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003899
3900 m->next_active = istk->mstk;
3901 istk->mstk = m;
3902
H. Peter Anvine2c80182005-01-15 22:15:51 +00003903 for (l = m->expansion; l; l = l->next) {
3904 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003905
H. Peter Anvine2c80182005-01-15 22:15:51 +00003906 ll = nasm_malloc(sizeof(Line));
3907 ll->finishes = NULL;
3908 ll->next = istk->expansion;
3909 istk->expansion = ll;
3910 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003911
H. Peter Anvine2c80182005-01-15 22:15:51 +00003912 for (t = l->first; t; t = t->next) {
3913 Token *x = t;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003914 switch (t->type) {
3915 case TOK_PREPROC_Q:
H. Peter Anvinc751e862008-06-09 10:18:45 -07003916 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
H. Peter Anvince2233b2008-05-25 21:57:00 -07003917 break;
3918 case TOK_PREPROC_QQ:
3919 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
3920 break;
3921 case TOK_PREPROC_ID:
3922 if (t->text[1] == '0' && t->text[2] == '0') {
3923 dont_prepend = -1;
3924 x = label;
3925 if (!x)
3926 continue;
3927 }
3928 /* fall through */
3929 default:
3930 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3931 break;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003932 }
H. Peter Anvince2233b2008-05-25 21:57:00 -07003933 tail = &tt->next;
3934 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003935 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003936 }
3937
3938 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00003939 * If we had a label, push it on as the first line of
3940 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003941 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003942 if (label) {
3943 if (dont_prepend < 0)
3944 free_tlist(startline);
3945 else {
3946 ll = nasm_malloc(sizeof(Line));
3947 ll->finishes = NULL;
3948 ll->next = istk->expansion;
3949 istk->expansion = ll;
3950 ll->first = startline;
3951 if (!dont_prepend) {
3952 while (label->next)
3953 label = label->next;
3954 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3955 }
3956 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003957 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003958
H. Peter Anvin734b1882002-04-30 21:01:08 +00003959 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003960
H. Peter Anvineba20a72002-04-30 20:53:55 +00003961 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003962}
3963
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003964/*
3965 * Since preprocessor always operate only on the line that didn't
3966 * arrived yet, we should always use ERR_OFFBY1. Also since user
3967 * won't want to see same error twice (preprocessing is done once
3968 * per pass) we will want to show errors only during pass one.
3969 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003970static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003971{
3972 va_list arg;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003973 char buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003974
3975 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003976 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003977 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003978
H. Peter Anvin734b1882002-04-30 21:01:08 +00003979 va_start(arg, fmt);
Ed Beroset19f927a2004-12-15 17:07:03 +00003980 vsnprintf(buff, sizeof(buff), fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003981 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003982
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003983 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003984 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3985 istk->mstk->lineno, buff);
3986 else
3987 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003988}
3989
H. Peter Anvin734b1882002-04-30 21:01:08 +00003990static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003991pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07003992 ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003993{
H. Peter Anvin99941bf2002-05-14 17:44:03 +00003994 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003995 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003996 istk = nasm_malloc(sizeof(Include));
3997 istk->next = NULL;
3998 istk->conds = NULL;
3999 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004000 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004001 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004002 istk->fname = NULL;
4003 src_set_fname(nasm_strdup(file));
4004 src_set_linnum(0);
4005 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004006 if (!istk->fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004007 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
4008 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004009 defining = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004010 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004011 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004012 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004013 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004014 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004015 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004016 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004017 any_extrastdmac = extrastdmac && *extrastdmac;
4018 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004019 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004020 evaluate = eval;
4021 pass = apass;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004022 dephead = deptail = deplist;
4023 if (deplist) {
4024 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4025 sl->next = NULL;
4026 strcpy(sl->str, file);
4027 *deptail = sl;
4028 deptail = &sl->next;
4029 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004030}
4031
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004032static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004033{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004034 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004035 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004036
H. Peter Anvine2c80182005-01-15 22:15:51 +00004037 while (1) {
4038 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004039 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004040 * buffer or from the input file.
4041 */
4042 tline = NULL;
4043 while (istk->expansion && istk->expansion->finishes) {
4044 Line *l = istk->expansion;
4045 if (!l->finishes->name && l->finishes->in_progress > 1) {
4046 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004047
H. Peter Anvine2c80182005-01-15 22:15:51 +00004048 /*
4049 * This is a macro-end marker for a macro with no
4050 * name, which means it's not really a macro at all
4051 * but a %rep block, and the `in_progress' field is
4052 * more than 1, meaning that we still need to
4053 * repeat. (1 means the natural last repetition; 0
4054 * means termination by %exitrep.) We have
4055 * therefore expanded up to the %endrep, and must
4056 * push the whole block on to the expansion buffer
4057 * again. We don't bother to remove the macro-end
4058 * marker: we'd only have to generate another one
4059 * if we did.
4060 */
4061 l->finishes->in_progress--;
4062 for (l = l->finishes->expansion; l; l = l->next) {
4063 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004064
H. Peter Anvine2c80182005-01-15 22:15:51 +00004065 ll = nasm_malloc(sizeof(Line));
4066 ll->next = istk->expansion;
4067 ll->finishes = NULL;
4068 ll->first = NULL;
4069 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004070
H. Peter Anvine2c80182005-01-15 22:15:51 +00004071 for (t = l->first; t; t = t->next) {
4072 if (t->text || t->type == TOK_WHITESPACE) {
4073 tt = *tail =
4074 new_Token(NULL, t->type, t->text, 0);
4075 tail = &tt->next;
4076 }
4077 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004078
H. Peter Anvine2c80182005-01-15 22:15:51 +00004079 istk->expansion = ll;
4080 }
4081 } else {
4082 /*
4083 * Check whether a `%rep' was started and not ended
4084 * within this macro expansion. This can happen and
4085 * should be detected. It's a fatal error because
4086 * I'm too confused to work out how to recover
4087 * sensibly from it.
4088 */
4089 if (defining) {
4090 if (defining->name)
4091 error(ERR_PANIC,
4092 "defining with name in expansion");
4093 else if (istk->mstk->name)
4094 error(ERR_FATAL,
4095 "`%%rep' without `%%endrep' within"
4096 " expansion of macro `%s'",
4097 istk->mstk->name);
4098 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004099
H. Peter Anvine2c80182005-01-15 22:15:51 +00004100 /*
4101 * FIXME: investigate the relationship at this point between
4102 * istk->mstk and l->finishes
4103 */
4104 {
4105 MMacro *m = istk->mstk;
4106 istk->mstk = m->next_active;
4107 if (m->name) {
4108 /*
4109 * This was a real macro call, not a %rep, and
4110 * therefore the parameter information needs to
4111 * be freed.
4112 */
4113 nasm_free(m->params);
4114 free_tlist(m->iline);
4115 nasm_free(m->paramlen);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004116 l->finishes->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004117 } else
4118 free_mmacro(m);
4119 }
4120 istk->expansion = l->next;
4121 nasm_free(l);
4122 list->downlevel(LIST_MACRO);
4123 }
4124 }
4125 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004126
H. Peter Anvine2c80182005-01-15 22:15:51 +00004127 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004128 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004129 Line *l = istk->expansion;
4130 if (istk->mstk)
4131 istk->mstk->lineno++;
4132 tline = l->first;
4133 istk->expansion = l->next;
4134 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004135 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004136 list->line(LIST_MACRO, p);
4137 nasm_free(p);
4138 break;
4139 }
4140 line = read_line();
4141 if (line) { /* from the current input file */
4142 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004143 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004144 nasm_free(line);
4145 break;
4146 }
4147 /*
4148 * The current file has ended; work down the istk
4149 */
4150 {
4151 Include *i = istk;
4152 fclose(i->fp);
4153 if (i->conds)
4154 error(ERR_FATAL,
4155 "expected `%%endif' before end of file");
4156 /* only set line and file name if there's a next node */
4157 if (i->next) {
4158 src_set_linnum(i->lineno);
4159 nasm_free(src_set_fname(i->fname));
4160 }
4161 istk = i->next;
4162 list->downlevel(LIST_INCLUDE);
4163 nasm_free(i);
4164 if (!istk)
4165 return NULL;
4166 }
4167 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004168
H. Peter Anvine2c80182005-01-15 22:15:51 +00004169 /*
4170 * We must expand MMacro parameters and MMacro-local labels
4171 * _before_ we plunge into directive processing, to cope
4172 * with things like `%define something %1' such as STRUC
4173 * uses. Unless we're _defining_ a MMacro, in which case
4174 * those tokens should be left alone to go into the
4175 * definition; and unless we're in a non-emitting
4176 * condition, in which case we don't want to meddle with
4177 * anything.
4178 */
4179 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
4180 tline = expand_mmac_params(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004181
H. Peter Anvine2c80182005-01-15 22:15:51 +00004182 /*
4183 * Check the line to see if it's a preprocessor directive.
4184 */
4185 if (do_directive(tline) == DIRECTIVE_FOUND) {
4186 continue;
4187 } else if (defining) {
4188 /*
4189 * We're defining a multi-line macro. We emit nothing
4190 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004191 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004192 */
4193 Line *l = nasm_malloc(sizeof(Line));
4194 l->next = defining->expansion;
4195 l->first = tline;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004196 l->finishes = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004197 defining->expansion = l;
4198 continue;
4199 } else if (istk->conds && !emitting(istk->conds->state)) {
4200 /*
4201 * We're in a non-emitting branch of a condition block.
4202 * Emit nothing at all, not even a blank line: when we
4203 * emerge from the condition we'll give a line-number
4204 * directive so we keep our place correctly.
4205 */
4206 free_tlist(tline);
4207 continue;
4208 } else if (istk->mstk && !istk->mstk->in_progress) {
4209 /*
4210 * We're in a %rep block which has been terminated, so
4211 * we're walking through to the %endrep without
4212 * emitting anything. Emit nothing at all, not even a
4213 * blank line: when we emerge from the %rep block we'll
4214 * give a line-number directive so we keep our place
4215 * correctly.
4216 */
4217 free_tlist(tline);
4218 continue;
4219 } else {
4220 tline = expand_smacro(tline);
4221 if (!expand_mmacro(tline)) {
4222 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004223 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004224 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004225 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004226 free_tlist(tline);
4227 break;
4228 } else {
4229 continue; /* expand_mmacro calls free_tlist */
4230 }
4231 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004232 }
4233
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004234 return line;
4235}
4236
H. Peter Anvine2c80182005-01-15 22:15:51 +00004237static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004238{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004239 if (defining) {
4240 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
4241 defining->name);
4242 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004243 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004244 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004245 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004246 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004247 while (istk) {
4248 Include *i = istk;
4249 istk = istk->next;
4250 fclose(i->fp);
4251 nasm_free(i->fname);
4252 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004253 }
4254 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004255 ctx_pop();
4256 if (pass == 0) {
4257 free_llist(predef);
4258 delete_Blocks();
4259 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004260}
4261
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004262void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004263{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004264 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004265
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004266 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004267 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004268 i->next = NULL;
4269
H. Peter Anvine2c80182005-01-15 22:15:51 +00004270 if (ipath != NULL) {
4271 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004272 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004273 j = j->next;
4274 j->next = i;
4275 } else {
4276 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004277 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004278}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004279
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004280void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004281{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004282 Token *inc, *space, *name;
4283 Line *l;
4284
H. Peter Anvin734b1882002-04-30 21:01:08 +00004285 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4286 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4287 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004288
4289 l = nasm_malloc(sizeof(Line));
4290 l->next = predef;
4291 l->first = inc;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004292 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004293 predef = l;
4294}
4295
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004296void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004297{
4298 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004299 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004300 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004301
4302 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004303 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4304 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004305 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004306 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004307 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004308 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004309 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004310
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004311 l = nasm_malloc(sizeof(Line));
4312 l->next = predef;
4313 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004314 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004315 predef = l;
4316}
4317
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004318void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004319{
4320 Token *def, *space;
4321 Line *l;
4322
H. Peter Anvin734b1882002-04-30 21:01:08 +00004323 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4324 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004325 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004326
4327 l = nasm_malloc(sizeof(Line));
4328 l->next = predef;
4329 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004330 l->finishes = false;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004331 predef = l;
4332}
4333
Keith Kaniosb7a89542007-04-12 02:40:54 +00004334/*
4335 * Added by Keith Kanios:
4336 *
4337 * This function is used to assist with "runtime" preprocessor
4338 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4339 *
4340 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4341 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4342 */
4343
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004344void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004345{
4346 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004347
Keith Kaniosb7a89542007-04-12 02:40:54 +00004348 def = tokenize(definition);
4349 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4350 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004351
Keith Kaniosb7a89542007-04-12 02:40:54 +00004352}
4353
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004354void pp_extra_stdmac(const char **macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004355{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004356 extrastdmac = macros;
4357}
4358
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004359static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004360{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004361 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004362 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004363 tok->text = nasm_strdup(numbuf);
4364 tok->type = TOK_NUMBER;
4365}
4366
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004367Preproc nasmpp = {
4368 pp_reset,
4369 pp_getline,
4370 pp_cleanup
4371};