blob: 6f8eb0b5ead6b5473272add216a51d18450a820e [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 Anvinc2df2822007-10-24 15:29:28 -070051#include "stdscan.h"
52#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070053#include "tables.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000054
55typedef struct SMacro SMacro;
56typedef struct MMacro MMacro;
57typedef struct Context Context;
58typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000059typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000060typedef struct Line Line;
61typedef struct Include Include;
62typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000063typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000064
65/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070066 * Note on the storage of both SMacro and MMacros: the hash table
67 * indexes them case-insensitively, and we then have to go through a
68 * linked list of potential case aliases (and, for MMacros, parameter
69 * ranges); this is to preserve the matching semantics of the earlier
70 * code. If the number of case aliases for a specific macro is a
71 * performance issue, you may want to reconsider your coding style.
72 */
73
74/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000075 * Store the definition of a single-line macro.
76 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000077struct SMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000078 SMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +000079 char *name;
H. Peter Anvin70055962007-10-11 00:05:31 -070080 bool casesense;
H. Peter Anvin16ed4382007-10-11 10:06:19 -070081 bool in_progress;
H. Peter Anvin70055962007-10-11 00:05:31 -070082 unsigned int nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000083 Token *expansion;
84};
85
86/*
H. Peter Anvin76690a12002-04-30 20:52:49 +000087 * Store the definition of a multi-line macro. This is also used to
88 * store the interiors of `%rep...%endrep' blocks, which are
89 * effectively self-re-invoking multi-line macros which simply
90 * don't have a name or bother to appear in the hash tables. %rep
91 * blocks are signified by having a NULL `name' field.
92 *
93 * In a MMacro describing a `%rep' block, the `in_progress' field
94 * isn't merely boolean, but gives the number of repeats left to
95 * run.
96 *
97 * The `next' field is used for storing MMacros in hash tables; the
98 * `next_active' field is for stacking them on istk entries.
99 *
100 * When a MMacro is being expanded, `params', `iline', `nparam',
101 * `paramlen', `rotate' and `unique' are local to the invocation.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000102 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000103struct MMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000104 MMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000105 char *name;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -0700106 int nparam_min, nparam_max;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700107 bool casesense;
108 bool plus; /* is the last parameter greedy? */
109 bool nolist; /* is this macro listing-inhibited? */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700110 int64_t in_progress;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000111 Token *dlist; /* All defaults as one list */
112 Token **defaults; /* Parameter default pointers */
113 int ndefs; /* number of default parameters */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000114 Line *expansion;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000115
116 MMacro *next_active;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000117 MMacro *rep_nest; /* used for nesting %rep */
118 Token **params; /* actual parameters */
119 Token *iline; /* invocation line */
H. Peter Anvin25a99342007-09-22 17:45:45 -0700120 unsigned int nparam, rotate;
121 int *paramlen;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700122 uint64_t unique;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000123 int lineno; /* Current line number on expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000124};
125
126/*
127 * The context stack is composed of a linked list of these.
128 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000129struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000130 Context *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000131 char *name;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700132 struct hash_table localmac;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000133 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000134};
135
136/*
137 * This is the internal form which we break input lines up into.
138 * Typically stored in linked lists.
139 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000140 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
141 * necessarily used as-is, but is intended to denote the number of
142 * the substituted parameter. So in the definition
143 *
144 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700145 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000146 * the token representing `x' will have its type changed to
147 * TOK_SMAC_PARAM, but the one representing `y' will be
148 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000149 *
150 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
151 * which doesn't need quotes around it. Used in the pre-include
152 * mechanism as an alternative to trying to find a sensible type of
153 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000154 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000155enum pp_token_type {
156 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
157 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700158 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
159 TOK_INTERNAL_STRING,
160 TOK_PREPROC_Q, TOK_PREPROC_QQ,
H. Peter Anvin5b76fa22008-05-26 11:14:38 -0700161 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
162 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000163};
164
H. Peter Anvine2c80182005-01-15 22:15:51 +0000165struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000166 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000167 char *text;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000168 SMacro *mac; /* associated macro for TOK_SMAC_END */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000169 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000170};
171
172/*
173 * Multi-line macro definitions are stored as a linked list of
174 * these, which is essentially a container to allow several linked
175 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700176 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000177 * Note that in this module, linked lists are treated as stacks
178 * wherever possible. For this reason, Lines are _pushed_ on to the
179 * `expansion' field in MMacro structures, so that the linked list,
180 * if walked, would give the macro lines in reverse order; this
181 * means that we can walk the list when expanding a macro, and thus
182 * push the lines on to the `expansion' field in _istk_ in reverse
183 * order (so that when popped back off they are in the right
184 * order). It may seem cockeyed, and it relies on my design having
185 * an even number of steps in, but it works...
186 *
187 * Some of these structures, rather than being actual lines, are
188 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000189 * This is for use in the cycle-tracking and %rep-handling code.
190 * Such structures have `finishes' non-NULL, and `first' NULL. All
191 * others have `finishes' NULL, but `first' may still be NULL if
192 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000193 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000194struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000195 Line *next;
196 MMacro *finishes;
197 Token *first;
198};
199
200/*
201 * To handle an arbitrary level of file inclusion, we maintain a
202 * stack (ie linked list) of these things.
203 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000204struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000205 Include *next;
206 FILE *fp;
207 Cond *conds;
208 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000209 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000210 int lineno, lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000211 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000212};
213
214/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000215 * Include search path. This is simply a list of strings which get
216 * prepended, in turn, to the name of an include file, in an
217 * attempt to find the file if it's not in the current directory.
218 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000219struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000220 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000221 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000222};
223
224/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000225 * Conditional assembly: we maintain a separate stack of these for
226 * each level of file inclusion. (The only reason we keep the
227 * stacks separate is to ensure that a stray `%endif' in a file
228 * included from within the true branch of a `%if' won't terminate
229 * it and cause confusion: instead, rightly, it'll cause an error.)
230 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000231struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000232 Cond *next;
233 int state;
234};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000235enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000236 /*
237 * These states are for use just after %if or %elif: IF_TRUE
238 * means the condition has evaluated to truth so we are
239 * currently emitting, whereas IF_FALSE means we are not
240 * currently emitting but will start doing so if a %else comes
241 * up. In these states, all directives are admissible: %elif,
242 * %else and %endif. (And of course %if.)
243 */
244 COND_IF_TRUE, COND_IF_FALSE,
245 /*
246 * These states come up after a %else: ELSE_TRUE means we're
247 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
248 * any %elif or %else will cause an error.
249 */
250 COND_ELSE_TRUE, COND_ELSE_FALSE,
251 /*
252 * This state means that we're not emitting now, and also that
253 * nothing until %endif will be emitted at all. It's for use in
254 * two circumstances: (i) when we've had our moment of emission
255 * and have now started seeing %elifs, and (ii) when the
256 * condition construct in question is contained within a
257 * non-emitting branch of a larger condition construct.
258 */
259 COND_NEVER
260};
261#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
262
H. Peter Anvin70653092007-10-19 14:42:29 -0700263/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000264 * These defines are used as the possible return values for do_directive
265 */
266#define NO_DIRECTIVE_FOUND 0
267#define DIRECTIVE_FOUND 1
268
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000269/*
270 * Condition codes. Note that we use c_ prefix not C_ because C_ is
271 * used in nasm.h for the "real" condition codes. At _this_ level,
272 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
273 * ones, so we need a different enum...
274 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700275static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000276 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
277 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000278 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000279};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700280enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000281 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
282 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 -0700283 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
284 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000285};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700286static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000287 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
288 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 +0000289 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000290};
291
H. Peter Anvin76690a12002-04-30 20:52:49 +0000292/*
293 * Directive names.
294 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000295/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000296static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000297{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000298 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000299}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000300
301/* For TASM compatibility we need to be able to recognise TASM compatible
302 * conditional compilation directives. Using the NASM pre-processor does
303 * not work, so we look for them specifically from the following list and
304 * then jam in the equivalent NASM directive into the input stream.
305 */
306
H. Peter Anvine2c80182005-01-15 22:15:51 +0000307enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000308 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
309 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
310};
311
H. Peter Anvin476d2862007-10-02 22:04:15 -0700312static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000313 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
314 "ifndef", "include", "local"
315};
316
317static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000318static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000319static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800320static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000321
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000322static Context *cstk;
323static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000324static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000325
H. Peter Anvine2c80182005-01-15 22:15:51 +0000326static efunc _error; /* Pointer to client-provided error reporting function */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000327static evalfunc evaluate;
328
H. Peter Anvine2c80182005-01-15 22:15:51 +0000329static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700330static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000331
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700332static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000333
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000334static Line *predef = NULL;
335
336static ListGen *list;
337
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000338/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000339 * The current set of multi-line macros we have defined.
340 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700341static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000342
343/*
344 * The current set of single-line macros we have defined.
345 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700346static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000347
348/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000349 * The multi-line macro we are currently defining, or the %rep
350 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000351 */
352static MMacro *defining;
353
354/*
355 * The number of macro parameters to allocate space for at a time.
356 */
357#define PARAM_DELTA 16
358
359/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700360 * The standard macro set: defined in macros.c in the array nasm_stdmac.
361 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000362 */
H. Peter Anvin188ce762008-02-16 13:58:45 -0800363static const char * const *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000364
365/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000366 * The extra standard macros that come from the object format, if
367 * any.
368 */
H. Peter Anvin188ce762008-02-16 13:58:45 -0800369static const char * const *extrastdmac = NULL;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700370bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000371
372/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000373 * Tokens are allocated in blocks to improve speed
374 */
375#define TOKEN_BLOCKSIZE 4096
376static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000377struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000378 Blocks *next;
379 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000380};
381
382static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000383
384/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000385 * Forward declarations.
386 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000387static Token *expand_mmac_params(Token * tline);
388static Token *expand_smacro(Token * tline);
389static Token *expand_id(Token * tline);
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700390static Context *get_ctx(char *name, bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700391static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000392static void error(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000393static void *new_Block(size_t size);
394static void delete_Blocks(void);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000395static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000396static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000397
398/*
399 * Macros for safe checking of token pointers, avoid *(NULL)
400 */
401#define tok_type_(x,t) ((x) && (x)->type == (t))
402#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
403#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
404#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000405
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000406/* Handle TASM specific directives, which do not contain a % in
407 * front of them. We do it here because I could not find any other
408 * place to do it for the moment, and it is a hack (ideally it would
409 * be nice to be able to use the NASM pre-processor to do it).
410 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000411static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000412{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000413 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000414 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000415
416 /* Skip whitespace */
417 while (isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000418 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000419
420 /* Binary search for the directive name */
421 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000422 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000423 len = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000424 while (!isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000425 len++;
426 if (len) {
427 oldchar = p[len];
428 p[len] = 0;
429 while (j - i > 1) {
430 k = (j + i) / 2;
431 m = nasm_stricmp(p, tasm_directives[k]);
432 if (m == 0) {
433 /* We have found a directive, so jam a % in front of it
434 * so that NASM will then recognise it as one if it's own.
435 */
436 p[len] = oldchar;
437 len = strlen(p);
438 oldline = line;
439 line = nasm_malloc(len + 2);
440 line[0] = '%';
441 if (k == TM_IFDIFI) {
442 /* NASM does not recognise IFDIFI, so we convert it to
443 * %ifdef BOGUS. This is not used in NASM comaptible
444 * code, but does need to parse for the TASM macro
445 * package.
446 */
447 strcpy(line + 1, "ifdef BOGUS");
448 } else {
449 memcpy(line + 1, p, len + 1);
450 }
451 nasm_free(oldline);
452 return line;
453 } else if (m < 0) {
454 j = k;
455 } else
456 i = k;
457 }
458 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000459 }
460 return line;
461}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000462
H. Peter Anvin76690a12002-04-30 20:52:49 +0000463/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000464 * The pre-preprocessing stage... This function translates line
465 * number indications as they emerge from GNU cpp (`# lineno "file"
466 * flags') into NASM preprocessor line number indications (`%line
467 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000468 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000469static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000470{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000471 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000472 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000473
H. Peter Anvine2c80182005-01-15 22:15:51 +0000474 if (line[0] == '#' && line[1] == ' ') {
475 oldline = line;
476 fname = oldline + 2;
477 lineno = atoi(fname);
478 fname += strspn(fname, "0123456789 ");
479 if (*fname == '"')
480 fname++;
481 fnlen = strcspn(fname, "\"");
482 line = nasm_malloc(20 + fnlen);
483 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
484 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000485 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000486 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000487 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000488 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000489}
490
491/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000492 * Free a linked list of tokens.
493 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000494static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000495{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000496 while (list) {
497 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000498 }
499}
500
501/*
502 * Free a linked list of lines.
503 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000504static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000505{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000506 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000507 while (list) {
508 l = list;
509 list = list->next;
510 free_tlist(l->first);
511 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000512 }
513}
514
515/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000516 * Free an MMacro
517 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000518static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000519{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000520 nasm_free(m->name);
521 free_tlist(m->dlist);
522 nasm_free(m->defaults);
523 free_llist(m->expansion);
524 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000525}
526
527/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700528 * Free all currently defined macros, and free the hash tables
529 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700530static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700531{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700532 SMacro *s;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700533 const char *key;
534 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700535
H. Peter Anvin072771e2008-05-22 13:17:51 -0700536 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700537 nasm_free((void *)key);
538 while (s) {
539 SMacro *ns = s->next;
540 nasm_free(s->name);
541 free_tlist(s->expansion);
542 nasm_free(s);
543 s = ns;
544 }
545 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700546 hash_free(smt);
547}
548
549static void free_mmacro_table(struct hash_table *mmt)
550{
551 MMacro *m;
552 const char *key;
553 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700554
555 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700556 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700557 nasm_free((void *)key);
558 while (m) {
559 MMacro *nm = m->next;
560 free_mmacro(m);
561 m = nm;
562 }
563 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700564 hash_free(mmt);
565}
566
567static void free_macros(void)
568{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700569 free_smacro_table(&smacros);
570 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700571}
572
573/*
574 * Initialize the hash tables
575 */
576static void init_macros(void)
577{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700578 hash_init(&smacros, HASH_LARGE);
579 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700580}
581
582/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000583 * Pop the context stack.
584 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000585static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000586{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000587 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000588
589 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700590 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000591 nasm_free(c->name);
592 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000593}
594
H. Peter Anvin072771e2008-05-22 13:17:51 -0700595/*
596 * Search for a key in the hash index; adding it if necessary
597 * (in which case we initialize the data pointer to NULL.)
598 */
599static void **
600hash_findi_add(struct hash_table *hash, const char *str)
601{
602 struct hash_insert hi;
603 void **r;
604 char *strx;
605
606 r = hash_findi(hash, str, &hi);
607 if (r)
608 return r;
609
610 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
611 return hash_add(&hi, strx, NULL);
612}
613
614/*
615 * Like hash_findi, but returns the data element rather than a pointer
616 * to it. Used only when not adding a new element, hence no third
617 * argument.
618 */
619static void *
620hash_findix(struct hash_table *hash, const char *str)
621{
622 void **p;
623
624 p = hash_findi(hash, str, NULL);
625 return p ? *p : NULL;
626}
627
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000628#define BUF_DELTA 512
629/*
630 * Read a line from the top file in istk, handling multiple CR/LFs
631 * at the end of the line read, and handling spurious ^Zs. Will
632 * return lines from the standard macro set if this has not already
633 * been done.
634 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000635static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000636{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000637 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000638 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000639
H. Peter Anvine2c80182005-01-15 22:15:51 +0000640 if (stdmacpos) {
641 if (*stdmacpos) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000642 char *ret = nasm_strdup(*stdmacpos++);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000643 if (!*stdmacpos && any_extrastdmac) {
644 stdmacpos = extrastdmac;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700645 any_extrastdmac = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000646 return ret;
647 }
648 /*
649 * Nasty hack: here we push the contents of `predef' on
650 * to the top-level expansion stack, since this is the
651 * most convenient way to implement the pre-include and
652 * pre-define features.
653 */
654 if (!*stdmacpos) {
655 Line *pd, *l;
656 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000657
H. Peter Anvine2c80182005-01-15 22:15:51 +0000658 for (pd = predef; pd; pd = pd->next) {
659 head = NULL;
660 tail = &head;
661 for (t = pd->first; t; t = t->next) {
662 *tail = new_Token(NULL, t->type, t->text, 0);
663 tail = &(*tail)->next;
664 }
665 l = nasm_malloc(sizeof(Line));
666 l->next = istk->expansion;
667 l->first = head;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700668 l->finishes = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000669 istk->expansion = l;
670 }
671 }
672 return ret;
673 } else {
674 stdmacpos = NULL;
675 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000676 }
677
678 bufsize = BUF_DELTA;
679 buffer = nasm_malloc(BUF_DELTA);
680 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000681 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000682 while (1) {
683 q = fgets(p, bufsize - (p - buffer), istk->fp);
684 if (!q)
685 break;
686 p += strlen(p);
687 if (p > buffer && p[-1] == '\n') {
688 /* Convert backslash-CRLF line continuation sequences into
689 nothing at all (for DOS and Windows) */
690 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
691 p -= 3;
692 *p = 0;
693 continued_count++;
694 }
695 /* Also convert backslash-LF line continuation sequences into
696 nothing at all (for Unix) */
697 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
698 p -= 2;
699 *p = 0;
700 continued_count++;
701 } else {
702 break;
703 }
704 }
705 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000706 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000707 bufsize += BUF_DELTA;
708 buffer = nasm_realloc(buffer, bufsize);
709 p = buffer + offset; /* prevent stale-pointer problems */
710 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000711 }
712
H. Peter Anvine2c80182005-01-15 22:15:51 +0000713 if (!q && p == buffer) {
714 nasm_free(buffer);
715 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000716 }
717
H. Peter Anvine2c80182005-01-15 22:15:51 +0000718 src_set_linnum(src_get_linnum() + istk->lineinc +
719 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000720
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000721 /*
722 * Play safe: remove CRs as well as LFs, if any of either are
723 * present at the end of the line.
724 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000725 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000726 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000727
728 /*
729 * Handle spurious ^Z, which may be inserted into source files
730 * by some file transfer utilities.
731 */
732 buffer[strcspn(buffer, "\032")] = '\0';
733
H. Peter Anvin734b1882002-04-30 21:01:08 +0000734 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000735
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000736 return buffer;
737}
738
739/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000740 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000741 * don't need to parse the value out of e.g. numeric tokens: we
742 * simply split one string into many.
743 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000744static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000745{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000746 char *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000747 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000748 Token *list = NULL;
749 Token *t, **tail = &list;
750
H. Peter Anvine2c80182005-01-15 22:15:51 +0000751 while (*line) {
752 p = line;
753 if (*p == '%') {
754 p++;
755 if (isdigit(*p) ||
756 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
757 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
758 do {
759 p++;
760 }
761 while (isdigit(*p));
762 type = TOK_PREPROC_ID;
763 } else if (*p == '{') {
764 p++;
765 while (*p && *p != '}') {
766 p[-1] = *p;
767 p++;
768 }
769 p[-1] = '\0';
770 if (*p)
771 p++;
772 type = TOK_PREPROC_ID;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700773 } else if (*p == '?') {
774 type = TOK_PREPROC_Q; /* %? */
775 p++;
776 if (*p == '?') {
777 type = TOK_PREPROC_QQ; /* %?? */
778 p++;
779 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000780 } else if (isidchar(*p) ||
781 ((*p == '!' || *p == '%' || *p == '$') &&
782 isidchar(p[1]))) {
783 do {
784 p++;
785 }
786 while (isidchar(*p));
787 type = TOK_PREPROC_ID;
788 } else {
789 type = TOK_OTHER;
790 if (*p == '%')
791 p++;
792 }
793 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
794 type = TOK_ID;
795 p++;
796 while (*p && isidchar(*p))
797 p++;
798 } else if (*p == '\'' || *p == '"') {
799 /*
800 * A string token.
801 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000802 char c = *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 p++;
804 type = TOK_STRING;
805 while (*p && *p != c)
806 p++;
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000807
H. Peter Anvine2c80182005-01-15 22:15:51 +0000808 if (*p) {
809 p++;
810 } else {
811 error(ERR_WARNING, "unterminated string");
812 /* Handling unterminated strings by UNV */
813 /* type = -1; */
814 }
815 } else if (isnumstart(*p)) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700816 bool is_hex = false;
817 bool is_float = false;
818 bool has_e = false;
819 char c, *r;
820
H. Peter Anvine2c80182005-01-15 22:15:51 +0000821 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700822 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000823 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700824
825 if (*p == '$') {
826 p++;
827 is_hex = true;
828 }
829
830 for (;;) {
831 c = *p++;
832
833 if (!is_hex && (c == 'e' || c == 'E')) {
834 has_e = true;
835 if (*p == '+' || *p == '-') {
836 /* e can only be followed by +/- if it is either a
837 prefixed hex number or a floating-point number */
838 p++;
839 is_float = true;
840 }
841 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
842 is_hex = true;
843 } else if (c == 'P' || c == 'p') {
844 is_float = true;
845 if (*p == '+' || *p == '-')
846 p++;
847 } else if (isnumchar(c) || c == '_')
848 ; /* just advance */
849 else if (c == '.') {
850 /* we need to deal with consequences of the legacy
851 parser, like "1.nolist" being two tokens
852 (TOK_NUMBER, TOK_ID) here; at least give it
853 a shot for now. In the future, we probably need
854 a flex-based scanner with proper pattern matching
855 to do it as well as it can be done. Nothing in
856 the world is going to help the person who wants
857 0x123.p16 interpreted as two tokens, though. */
858 r = p;
859 while (*r == '_')
860 r++;
861
862 if (isdigit(*r) || (is_hex && isxdigit(*r)) ||
863 (!is_hex && (*r == 'e' || *r == 'E')) ||
864 (*r == 'p' || *r == 'P')) {
865 p = r;
866 is_float = true;
867 } else
868 break; /* Terminate the token */
869 } else
870 break;
871 }
872 p--; /* Point to first character beyond number */
873
874 if (has_e && !is_hex) {
875 /* 1e13 is floating-point, but 1e13h is not */
876 is_float = true;
877 }
878
879 type = is_float ? TOK_FLOAT : TOK_NUMBER;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000880 } else if (isspace(*p)) {
881 type = TOK_WHITESPACE;
882 p++;
883 while (*p && isspace(*p))
884 p++;
885 /*
886 * Whitespace just before end-of-line is discarded by
887 * pretending it's a comment; whitespace just before a
888 * comment gets lumped into the comment.
889 */
890 if (!*p || *p == ';') {
891 type = TOK_COMMENT;
892 while (*p)
893 p++;
894 }
895 } else if (*p == ';') {
896 type = TOK_COMMENT;
897 while (*p)
898 p++;
899 } else {
900 /*
901 * Anything else is an operator of some kind. We check
902 * for all the double-character operators (>>, <<, //,
903 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000904 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000905 */
906 type = TOK_OTHER;
907 if ((p[0] == '>' && p[1] == '>') ||
908 (p[0] == '<' && p[1] == '<') ||
909 (p[0] == '/' && p[1] == '/') ||
910 (p[0] == '<' && p[1] == '=') ||
911 (p[0] == '>' && p[1] == '=') ||
912 (p[0] == '=' && p[1] == '=') ||
913 (p[0] == '!' && p[1] == '=') ||
914 (p[0] == '<' && p[1] == '>') ||
915 (p[0] == '&' && p[1] == '&') ||
916 (p[0] == '|' && p[1] == '|') ||
917 (p[0] == '^' && p[1] == '^')) {
918 p++;
919 }
920 p++;
921 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000922
H. Peter Anvine2c80182005-01-15 22:15:51 +0000923 /* Handling unterminated string by UNV */
924 /*if (type == -1)
925 {
926 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
927 t->text[p-line] = *line;
928 tail = &t->next;
929 }
930 else */
931 if (type != TOK_COMMENT) {
932 *tail = t = new_Token(NULL, type, line, p - line);
933 tail = &t->next;
934 }
935 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000936 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000937 return list;
938}
939
H. Peter Anvince616072002-04-30 21:02:23 +0000940/*
941 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -0700942 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +0000943 * deleted only all at once by the delete_Blocks function.
944 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000945static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +0000946{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000947 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000948
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000949 /* first, get to the end of the linked list */
950 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000951 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000952 /* now allocate the requested chunk */
953 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000954
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000955 /* now allocate a new block for the next request */
956 b->next = nasm_malloc(sizeof(Blocks));
957 /* and initialize the contents of the new block */
958 b->next->next = NULL;
959 b->next->chunk = NULL;
960 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000961}
962
963/*
964 * this function deletes all managed blocks of memory
965 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +0000967{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000968 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +0000969
H. Peter Anvin70653092007-10-19 14:42:29 -0700970 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000971 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -0700972 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000973 * free it.
974 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 while (b) {
976 if (b->chunk)
977 nasm_free(b->chunk);
978 a = b;
979 b = b->next;
980 if (a != &blocks)
981 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000982 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000983}
H. Peter Anvin734b1882002-04-30 21:01:08 +0000984
985/*
H. Peter Anvin70653092007-10-19 14:42:29 -0700986 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +0000987 * back to the caller. It sets the type and text elements, and
988 * also the mac and next elements to NULL.
989 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000990static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +0000991{
992 Token *t;
993 int i;
994
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995 if (freeTokens == NULL) {
996 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
997 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
998 freeTokens[i].next = &freeTokens[i + 1];
999 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001000 }
1001 t = freeTokens;
1002 freeTokens = t->next;
1003 t->next = next;
1004 t->mac = NULL;
1005 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 if (type == TOK_WHITESPACE || text == NULL) {
1007 t->text = NULL;
1008 } else {
1009 if (txtlen == 0)
1010 txtlen = strlen(text);
1011 t->text = nasm_malloc(1 + txtlen);
1012 strncpy(t->text, text, txtlen);
1013 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001014 }
1015 return t;
1016}
1017
H. Peter Anvine2c80182005-01-15 22:15:51 +00001018static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001019{
1020 Token *next = t->next;
1021 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001022 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001023 freeTokens = t;
1024 return next;
1025}
1026
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001027/*
1028 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001029 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1030 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001031 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001032static char *detoken(Token * tlist, int expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001033{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001034 Token *t;
1035 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001036 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001037 const char *q;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001038
1039 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001040 for (t = tlist; t; t = t->next) {
1041 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001042 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001043 nasm_free(t->text);
1044 if (p)
1045 t->text = nasm_strdup(p);
1046 else
1047 t->text = NULL;
1048 }
1049 /* Expand local macros here and not during preprocessing */
1050 if (expand_locals &&
1051 t->type == TOK_PREPROC_ID && t->text &&
1052 t->text[0] == '%' && t->text[1] == '$') {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001053 Context *ctx = get_ctx(t->text, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001054 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001055 char buffer[40];
1056 char *p, *q = t->text + 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001057
H. Peter Anvine2c80182005-01-15 22:15:51 +00001058 q += strspn(q, "$");
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001059 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001060 p = nasm_strcat(buffer, q);
1061 nasm_free(t->text);
1062 t->text = p;
1063 }
1064 }
1065 if (t->type == TOK_WHITESPACE) {
1066 len++;
1067 } else if (t->text) {
1068 len += strlen(t->text);
1069 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001070 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001071 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 for (t = tlist; t; t = t->next) {
1073 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001074 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001075 } else if (t->text) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001076 q = t->text;
1077 while (*q)
1078 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001079 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001080 }
1081 *p = '\0';
1082 return line;
1083}
1084
1085/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001086 * A scanner, suitable for use by the expression evaluator, which
1087 * operates on a line of Tokens. Expects a pointer to a pointer to
1088 * the first token in the line to be passed in as its private_data
1089 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001090 *
1091 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001092 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001094{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001095 Token **tlineptr = private_data;
1096 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001097 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001098
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 do {
1100 tline = *tlineptr;
1101 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001102 }
1103 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001104 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001105
1106 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001107 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001108
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001109 tokval->t_charptr = tline->text;
1110
H. Peter Anvin76690a12002-04-30 20:52:49 +00001111 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001112 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001113 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001114 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001115
H. Peter Anvine2c80182005-01-15 22:15:51 +00001116 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001117 p = tokval->t_charptr = tline->text;
1118 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001119 tokval->t_charptr++;
1120 return tokval->t_type = TOKEN_ID;
1121 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001122
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001123 for (r = p, s = ourcopy; *r; r++) {
Philipp Thomas76ec8e72008-05-21 08:53:21 -07001124 if (r >= p+MAX_KEYWORD)
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001125 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1126 *s++ = tolower(*r);
1127 }
1128 *s = '\0';
1129 /* right, so we have an identifier sitting in temp storage. now,
1130 * is it actually a register or instruction name, or what? */
1131 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001132 }
1133
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 if (tline->type == TOK_NUMBER) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001135 bool rn_error;
1136 tokval->t_integer = readnum(tline->text, &rn_error);
1137 if (rn_error)
1138 return tokval->t_type = TOKEN_ERRNUM; /* some malformation occurred */
1139 tokval->t_charptr = tline->text;
1140 return tokval->t_type = TOKEN_NUM;
1141 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001142
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001143 if (tline->type == TOK_FLOAT) {
1144 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001145 }
1146
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 if (tline->type == TOK_STRING) {
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001148 bool rn_warn;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001149 char q, *r;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001150 int l;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001151
H. Peter Anvine2c80182005-01-15 22:15:51 +00001152 r = tline->text;
1153 q = *r++;
1154 l = strlen(r);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001155
H. Peter Anvine2c80182005-01-15 22:15:51 +00001156 if (l == 0 || r[l - 1] != q)
1157 return tokval->t_type = TOKEN_ERRNUM;
1158 tokval->t_integer = readstrnum(r, l - 1, &rn_warn);
1159 if (rn_warn)
1160 error(ERR_WARNING | ERR_PASS1, "character constant too long");
1161 tokval->t_charptr = NULL;
1162 return tokval->t_type = TOKEN_NUM;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001163 }
1164
H. Peter Anvine2c80182005-01-15 22:15:51 +00001165 if (tline->type == TOK_OTHER) {
1166 if (!strcmp(tline->text, "<<"))
1167 return tokval->t_type = TOKEN_SHL;
1168 if (!strcmp(tline->text, ">>"))
1169 return tokval->t_type = TOKEN_SHR;
1170 if (!strcmp(tline->text, "//"))
1171 return tokval->t_type = TOKEN_SDIV;
1172 if (!strcmp(tline->text, "%%"))
1173 return tokval->t_type = TOKEN_SMOD;
1174 if (!strcmp(tline->text, "=="))
1175 return tokval->t_type = TOKEN_EQ;
1176 if (!strcmp(tline->text, "<>"))
1177 return tokval->t_type = TOKEN_NE;
1178 if (!strcmp(tline->text, "!="))
1179 return tokval->t_type = TOKEN_NE;
1180 if (!strcmp(tline->text, "<="))
1181 return tokval->t_type = TOKEN_LE;
1182 if (!strcmp(tline->text, ">="))
1183 return tokval->t_type = TOKEN_GE;
1184 if (!strcmp(tline->text, "&&"))
1185 return tokval->t_type = TOKEN_DBL_AND;
1186 if (!strcmp(tline->text, "^^"))
1187 return tokval->t_type = TOKEN_DBL_XOR;
1188 if (!strcmp(tline->text, "||"))
1189 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001190 }
1191
1192 /*
1193 * We have no other options: just return the first character of
1194 * the token text.
1195 */
1196 return tokval->t_type = tline->text[0];
1197}
1198
1199/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001200 * Compare a string to the name of an existing macro; this is a
1201 * simple wrapper which calls either strcmp or nasm_stricmp
1202 * depending on the value of the `casesense' parameter.
1203 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001204static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001205{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001206 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001207}
1208
1209/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001210 * Return the Context structure associated with a %$ token. Return
1211 * NULL, having _already_ reported an error condition, if the
1212 * context stack isn't deep enough for the supplied number of $
1213 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001214 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001215 * also scanned for such smacro, until it is found; if not -
1216 * only the context that directly results from the number of $'s
1217 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001218 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001219static Context *get_ctx(char *name, bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001220{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001221 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001222 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001223 int i;
1224
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001225 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001226 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001227
H. Peter Anvine2c80182005-01-15 22:15:51 +00001228 if (!cstk) {
1229 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1230 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001231 }
1232
H. Peter Anvine2c80182005-01-15 22:15:51 +00001233 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1234 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001235/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001236 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001237 if (!ctx) {
1238 error(ERR_NONFATAL, "`%s': context stack is only"
1239 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1240 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001241 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001242 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001243 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001244
H. Peter Anvine2c80182005-01-15 22:15:51 +00001245 do {
1246 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001247 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001248 while (m) {
1249 if (!mstrcmp(m->name, name, m->casesense))
1250 return ctx;
1251 m = m->next;
1252 }
1253 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001254 }
1255 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001256 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001257}
1258
1259/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001260 * Check to see if a file is already in a string list
1261 */
1262static bool in_list(const StrList *list, const char *str)
1263{
1264 while (list) {
1265 if (!strcmp(list->str, str))
1266 return true;
1267 list = list->next;
1268 }
1269 return false;
1270}
1271
1272/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001273 * Open an include file. This routine must always return a valid
1274 * file pointer if it returns - it's responsible for throwing an
1275 * ERR_FATAL and bombing out completely if not. It should also try
1276 * the include path one by one until it finds the file or reaches
1277 * the end of the path.
1278 */
H. Peter Anvin418ca702008-05-30 10:42:30 -07001279static FILE *inc_fopen(const char *file, StrList **dhead, StrList **dtail,
1280 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001281{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001282 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001283 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001284 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001285 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001286 size_t prefix_len = 0;
1287 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001288
H. Peter Anvine2c80182005-01-15 22:15:51 +00001289 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001290 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1291 memcpy(sl->str, prefix, prefix_len);
1292 memcpy(sl->str+prefix_len, file, len+1);
1293 fp = fopen(sl->str, "r");
H. Peter Anvin418ca702008-05-30 10:42:30 -07001294 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001295 sl->next = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07001296 *dtail = sl;
1297 dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001298 } else {
1299 nasm_free(sl);
1300 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001301 if (fp)
1302 return fp;
H. Peter Anvin418ca702008-05-30 10:42:30 -07001303 if (!ip) {
1304 if (!missing_ok)
1305 break;
1306 prefix = NULL;
1307 } else {
1308 prefix = ip->path;
1309 ip = ip->next;
1310 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001311 if (prefix) {
1312 prefix_len = strlen(prefix);
1313 } else {
1314 /* -MG given and file not found */
H. Peter Anvin418ca702008-05-30 10:42:30 -07001315 if (dhead && !in_list(*dhead, file)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001316 sl = nasm_malloc(len+1+sizeof sl->next);
1317 sl->next = NULL;
1318 strcpy(sl->str, file);
H. Peter Anvin418ca702008-05-30 10:42:30 -07001319 *dtail = sl;
1320 dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001321 }
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001322 return NULL;
1323 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001324 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001325
H. Peter Anvin734b1882002-04-30 21:01:08 +00001326 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001328}
1329
1330/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001331 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001332 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001333 * return true if _any_ single-line macro of that name is defined.
1334 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001335 * `nparam' or no parameters is defined.
1336 *
1337 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001338 * defined, or nparam is -1, the address of the definition structure
1339 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001340 * is NULL, no action will be taken regarding its contents, and no
1341 * error will occur.
1342 *
1343 * Note that this is also called with nparam zero to resolve
1344 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001345 *
1346 * If you already know which context macro belongs to, you can pass
1347 * the context pointer as first parameter; if you won't but name begins
1348 * with %$ the context will be automatically computed. If all_contexts
1349 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001350 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001351static bool
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001352smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001353 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001354{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001355 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001356 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001357
H. Peter Anvin97a23472007-09-16 17:57:25 -07001358 if (ctx) {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001359 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001360 } else if (name[0] == '%' && name[1] == '$') {
1361 if (cstk)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001362 ctx = get_ctx(name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001363 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001364 return false; /* got to return _something_ */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001365 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001366 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001367 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001368 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001369 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001370
H. Peter Anvine2c80182005-01-15 22:15:51 +00001371 while (m) {
1372 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001373 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001374 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001375 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001376 *defn = m;
1377 else
1378 *defn = NULL;
1379 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001380 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001381 }
1382 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001383 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001384
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001385 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001386}
1387
1388/*
1389 * Count and mark off the parameters in a multi-line macro call.
1390 * This is called both from within the multi-line macro expansion
1391 * code, and also to mark off the default parameters when provided
1392 * in a %macro definition line.
1393 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001394static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001395{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001396 int paramsize, brace;
1397
1398 *nparam = paramsize = 0;
1399 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001400 while (t) {
1401 if (*nparam >= paramsize) {
1402 paramsize += PARAM_DELTA;
1403 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1404 }
1405 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001406 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001407 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001408 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001409 (*params)[(*nparam)++] = t;
1410 while (tok_isnt_(t, brace ? "}" : ","))
1411 t = t->next;
1412 if (t) { /* got a comma/brace */
1413 t = t->next;
1414 if (brace) {
1415 /*
1416 * Now we've found the closing brace, look further
1417 * for the comma.
1418 */
1419 skip_white_(t);
1420 if (tok_isnt_(t, ",")) {
1421 error(ERR_NONFATAL,
1422 "braces do not enclose all of macro parameter");
1423 while (tok_isnt_(t, ","))
1424 t = t->next;
1425 }
1426 if (t)
1427 t = t->next; /* eat the comma */
1428 }
1429 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001430 }
1431}
1432
1433/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001434 * Determine whether one of the various `if' conditions is true or
1435 * not.
1436 *
1437 * We must free the tline we get passed.
1438 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001439static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001440{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001441 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001442 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001443 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001444 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001445 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001446 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001447
1448 origline = tline;
1449
H. Peter Anvine2c80182005-01-15 22:15:51 +00001450 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001451 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001452 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001453 while (cstk && tline) {
1454 skip_white_(tline);
1455 if (!tline || tline->type != TOK_ID) {
1456 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001457 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001458 free_tlist(origline);
1459 return -1;
1460 }
1461 if (!nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001462 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001463 tline = tline->next;
1464 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001465 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001466
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001467 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001468 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001469 while (tline) {
1470 skip_white_(tline);
1471 if (!tline || (tline->type != TOK_ID &&
1472 (tline->type != TOK_PREPROC_ID ||
1473 tline->text[1] != '$'))) {
1474 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001475 "`%s' expects macro identifiers", pp_directives[ct]);
1476 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001477 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001478 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001479 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001480 tline = tline->next;
1481 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001482 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001483
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001484 case PPC_IFIDN:
1485 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001486 tline = expand_smacro(tline);
1487 t = tt = tline;
1488 while (tok_isnt_(tt, ","))
1489 tt = tt->next;
1490 if (!tt) {
1491 error(ERR_NONFATAL,
1492 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001493 pp_directives[ct]);
1494 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001495 }
1496 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001497 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001498 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1499 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1500 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001501 pp_directives[ct]);
1502 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001503 }
1504 if (t->type == TOK_WHITESPACE) {
1505 t = t->next;
1506 continue;
1507 }
1508 if (tt->type == TOK_WHITESPACE) {
1509 tt = tt->next;
1510 continue;
1511 }
1512 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001513 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001514 break;
1515 }
1516 /* Unify surrounding quotes for strings */
1517 if (t->type == TOK_STRING) {
1518 tt->text[0] = t->text[0];
1519 tt->text[strlen(tt->text) - 1] = t->text[0];
1520 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001521 if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001522 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001523 break;
1524 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001525
H. Peter Anvine2c80182005-01-15 22:15:51 +00001526 t = t->next;
1527 tt = tt->next;
1528 }
1529 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001530 j = false; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001531 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001532
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001533 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001534 {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001535 bool found = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001536 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001537
H. Peter Anvine2c80182005-01-15 22:15:51 +00001538 tline = tline->next;
1539 skip_white_(tline);
1540 tline = expand_id(tline);
1541 if (!tok_type_(tline, TOK_ID)) {
1542 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001543 "`%s' expects a macro name", pp_directives[ct]);
1544 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001545 }
1546 searching.name = nasm_strdup(tline->text);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001547 searching.casesense = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001548 searching.plus = false;
1549 searching.nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001550 searching.in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001551 searching.rep_nest = NULL;
1552 searching.nparam_min = 0;
1553 searching.nparam_max = INT_MAX;
1554 tline = expand_smacro(tline->next);
1555 skip_white_(tline);
1556 if (!tline) {
1557 } else if (!tok_type_(tline, TOK_NUMBER)) {
1558 error(ERR_NONFATAL,
1559 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001560 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001561 } else {
1562 searching.nparam_min = searching.nparam_max =
1563 readnum(tline->text, &j);
1564 if (j)
1565 error(ERR_NONFATAL,
1566 "unable to parse parameter count `%s'",
1567 tline->text);
1568 }
1569 if (tline && tok_is_(tline->next, "-")) {
1570 tline = tline->next->next;
1571 if (tok_is_(tline, "*"))
1572 searching.nparam_max = INT_MAX;
1573 else if (!tok_type_(tline, TOK_NUMBER))
1574 error(ERR_NONFATAL,
1575 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001576 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001577 else {
1578 searching.nparam_max = readnum(tline->text, &j);
1579 if (j)
1580 error(ERR_NONFATAL,
1581 "unable to parse parameter count `%s'",
1582 tline->text);
1583 if (searching.nparam_min > searching.nparam_max)
1584 error(ERR_NONFATAL,
1585 "minimum parameter count exceeds maximum");
1586 }
1587 }
1588 if (tline && tok_is_(tline->next, "+")) {
1589 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001590 searching.plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001591 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001592 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001593 while (mmac) {
1594 if (!strcmp(mmac->name, searching.name) &&
1595 (mmac->nparam_min <= searching.nparam_max
1596 || searching.plus)
1597 && (searching.nparam_min <= mmac->nparam_max
1598 || mmac->plus)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001599 found = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001600 break;
1601 }
1602 mmac = mmac->next;
1603 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001604 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001605 j = found;
1606 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001607 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001608
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001609 case PPC_IFID:
1610 needtype = TOK_ID;
1611 goto iftype;
1612 case PPC_IFNUM:
1613 needtype = TOK_NUMBER;
1614 goto iftype;
1615 case PPC_IFSTR:
1616 needtype = TOK_STRING;
1617 goto iftype;
1618
1619 iftype:
H. Peter Anvin134b9462008-02-16 17:01:40 -08001620 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001621
H. Peter Anvin927c92b2008-02-16 13:44:52 -08001622 while (tok_type_(t, TOK_WHITESPACE) ||
1623 (needtype == TOK_NUMBER &&
1624 tok_type_(t, TOK_OTHER) &&
1625 (t->text[0] == '-' || t->text[0] == '+') &&
1626 !t->text[1]))
1627 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001628
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001629 j = tok_type_(t, needtype);
1630 break;
1631
1632 case PPC_IFTOKEN:
1633 t = tline = expand_smacro(tline);
1634 while (tok_type_(t, TOK_WHITESPACE))
1635 t = t->next;
1636
1637 j = false;
1638 if (t) {
1639 t = t->next; /* Skip the actual token */
1640 while (tok_type_(t, TOK_WHITESPACE))
1641 t = t->next;
1642 j = !t; /* Should be nothing left */
1643 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001644 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001645
H. Peter Anvin134b9462008-02-16 17:01:40 -08001646 case PPC_IFEMPTY:
1647 t = tline = expand_smacro(tline);
1648 while (tok_type_(t, TOK_WHITESPACE))
1649 t = t->next;
1650
1651 j = !t; /* Should be empty */
1652 break;
1653
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001654 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001655 t = tline = expand_smacro(tline);
1656 tptr = &t;
1657 tokval.t_type = TOKEN_INVALID;
1658 evalresult = evaluate(ppscan, tptr, &tokval,
1659 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001660 if (!evalresult)
1661 return -1;
1662 if (tokval.t_type)
1663 error(ERR_WARNING,
1664 "trailing garbage after expression ignored");
1665 if (!is_simple(evalresult)) {
1666 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001667 "non-constant value given to `%s'", pp_directives[ct]);
1668 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001669 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001670 j = reloc_value(evalresult) != 0;
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001671 return j;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001672
H. Peter Anvine2c80182005-01-15 22:15:51 +00001673 default:
1674 error(ERR_FATAL,
1675 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001676 pp_directives[ct]);
1677 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001678 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001679
1680 free_tlist(origline);
1681 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001682
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001683fail:
1684 free_tlist(origline);
1685 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001686}
1687
1688/*
H. Peter Anvin418ca702008-05-30 10:42:30 -07001689 * Expand macros in a string. Used in %error directives (and it should
1690 * almost certainly be removed from there, too.)
1691 *
Keith Kaniosb7a89542007-04-12 02:40:54 +00001692 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001693 * The returned variable should ALWAYS be freed after usage.
1694 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001695void expand_macros_in_string(char **p)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001696{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001697 Token *line = tokenize(*p);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001698 line = expand_smacro(line);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001699 *p = detoken(line, false);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001700}
1701
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001702/*
1703 * Common code for defining an smacro
1704 */
1705static bool define_smacro(Context *ctx, char *mname, bool casesense,
1706 int nparam, Token *expansion)
1707{
1708 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001709 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001710
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001711 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1712 if (!smac) {
1713 error(ERR_WARNING,
1714 "single-line macro `%s' defined both with and"
1715 " without parameters", mname);
1716
1717 /* Some instances of the old code considered this a failure,
1718 some others didn't. What is the right thing to do here? */
1719 free_tlist(expansion);
1720 return false; /* Failure */
1721 } else {
1722 /*
1723 * We're redefining, so we have to take over an
1724 * existing SMacro structure. This means freeing
1725 * what was already in it.
1726 */
1727 nasm_free(smac->name);
1728 free_tlist(smac->expansion);
1729 }
1730 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001731 smtbl = ctx ? &ctx->localmac : &smacros;
1732 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001733 smac = nasm_malloc(sizeof(SMacro));
1734 smac->next = *smhead;
1735 *smhead = smac;
1736 }
1737 smac->name = nasm_strdup(mname);
1738 smac->casesense = casesense;
1739 smac->nparam = nparam;
1740 smac->expansion = expansion;
1741 smac->in_progress = false;
1742 return true; /* Success */
1743}
1744
1745/*
1746 * Undefine an smacro
1747 */
1748static void undef_smacro(Context *ctx, const char *mname)
1749{
1750 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001751 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001752
H. Peter Anvin166c2472008-05-28 12:28:58 -07001753 smtbl = ctx ? &ctx->localmac : &smacros;
1754 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001755
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001756 if (smhead) {
1757 /*
1758 * We now have a macro name... go hunt for it.
1759 */
1760 sp = smhead;
1761 while ((s = *sp) != NULL) {
1762 if (!mstrcmp(s->name, mname, s->casesense)) {
1763 *sp = s->next;
1764 nasm_free(s->name);
1765 free_tlist(s->expansion);
1766 nasm_free(s);
1767 } else {
1768 sp = &s->next;
1769 }
1770 }
1771 }
1772}
1773
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001774/*
1775 * Decode a size directive
1776 */
1777static int parse_size(const char *str) {
1778 static const char *size_names[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001779 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001780 static const int sizes[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001781 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001782
1783 return sizes[bsii(str, size_names, elements(size_names))+1];
1784}
1785
Ed Beroset3ab3f412002-06-11 03:31:49 +00001786/**
1787 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001788 * Find out if a line contains a preprocessor directive, and deal
1789 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07001790 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001791 * If a directive _is_ found, it is the responsibility of this routine
1792 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001793 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001794 * @param tline a pointer to the current tokeninzed line linked list
1795 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07001796 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001797 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001798static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001799{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001800 enum preproc_token i;
1801 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07001802 bool err;
1803 int nparam;
1804 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001805 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001806 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001807 int offset;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001808 char *p, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001809 Include *inc;
1810 Context *ctx;
1811 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001812 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001813 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1814 Line *l;
1815 struct tokenval tokval;
1816 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001818 int64_t count;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001819
1820 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001821
H. Peter Anvineba20a72002-04-30 20:53:55 +00001822 skip_white_(tline);
1823 if (!tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001824 (tline->text[1] == '%' || tline->text[1] == '$'
1825 || tline->text[1] == '!'))
1826 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001827
H. Peter Anvin4169a472007-09-12 01:29:43 +00001828 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001829
1830 /*
1831 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001832 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001833 * we should ignore all directives except for condition
1834 * directives.
1835 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001836 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001837 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1838 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001839 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001840
1841 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001842 * If we're defining a macro or reading a %rep block, we should
1843 * ignore all directives except for %macro/%imacro (which
1844 * generate an error), %endm/%endmacro, and (only if we're in a
H. Peter Anvineba20a72002-04-30 20:53:55 +00001845 * %rep block) %endrep. If we're in a %rep block, another %rep
1846 * causes an error, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001847 */
1848 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001849 i != PP_ENDMACRO && i != PP_ENDM &&
1850 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1851 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001852 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001853
H. Peter Anvin4169a472007-09-12 01:29:43 +00001854 switch (i) {
1855 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001856 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1857 tline->text);
1858 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001859
H. Peter Anvine2c80182005-01-15 22:15:51 +00001860 case PP_STACKSIZE:
1861 /* Directive to tell NASM what the default stack size is. The
1862 * default is for a 16-bit stack, and this can be overriden with
1863 * %stacksize large.
1864 * the following form:
1865 *
1866 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1867 */
1868 tline = tline->next;
1869 if (tline && tline->type == TOK_WHITESPACE)
1870 tline = tline->next;
1871 if (!tline || tline->type != TOK_ID) {
1872 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1873 free_tlist(origline);
1874 return DIRECTIVE_FOUND;
1875 }
1876 if (nasm_stricmp(tline->text, "flat") == 0) {
1877 /* All subsequent ARG directives are for a 32-bit stack */
1878 StackSize = 4;
1879 StackPointer = "ebp";
1880 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001881 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08001882 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
1883 /* All subsequent ARG directives are for a 64-bit stack */
1884 StackSize = 8;
1885 StackPointer = "rbp";
1886 ArgOffset = 8;
1887 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001888 } else if (nasm_stricmp(tline->text, "large") == 0) {
1889 /* All subsequent ARG directives are for a 16-bit stack,
1890 * far function call.
1891 */
1892 StackSize = 2;
1893 StackPointer = "bp";
1894 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001895 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001896 } else if (nasm_stricmp(tline->text, "small") == 0) {
1897 /* All subsequent ARG directives are for a 16-bit stack,
1898 * far function call. We don't support near functions.
1899 */
1900 StackSize = 2;
1901 StackPointer = "bp";
1902 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001903 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001904 } else {
1905 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1906 free_tlist(origline);
1907 return DIRECTIVE_FOUND;
1908 }
1909 free_tlist(origline);
1910 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001911
H. Peter Anvine2c80182005-01-15 22:15:51 +00001912 case PP_ARG:
1913 /* TASM like ARG directive to define arguments to functions, in
1914 * the following form:
1915 *
1916 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1917 */
1918 offset = ArgOffset;
1919 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001920 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001921 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001922
H. Peter Anvine2c80182005-01-15 22:15:51 +00001923 /* Find the argument name */
1924 tline = tline->next;
1925 if (tline && tline->type == TOK_WHITESPACE)
1926 tline = tline->next;
1927 if (!tline || tline->type != TOK_ID) {
1928 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1929 free_tlist(origline);
1930 return DIRECTIVE_FOUND;
1931 }
1932 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001933
H. Peter Anvine2c80182005-01-15 22:15:51 +00001934 /* Find the argument size type */
1935 tline = tline->next;
1936 if (!tline || tline->type != TOK_OTHER
1937 || tline->text[0] != ':') {
1938 error(ERR_NONFATAL,
1939 "Syntax error processing `%%arg' directive");
1940 free_tlist(origline);
1941 return DIRECTIVE_FOUND;
1942 }
1943 tline = tline->next;
1944 if (!tline || tline->type != TOK_ID) {
1945 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1946 free_tlist(origline);
1947 return DIRECTIVE_FOUND;
1948 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001949
H. Peter Anvine2c80182005-01-15 22:15:51 +00001950 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001951 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001952 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001953 size = parse_size(tt->text);
1954 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001955 error(ERR_NONFATAL,
1956 "Invalid size type for `%%arg' missing directive");
1957 free_tlist(tt);
1958 free_tlist(origline);
1959 return DIRECTIVE_FOUND;
1960 }
1961 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001962
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001963 /* Round up to even stack slots */
1964 size = (size+StackSize-1) & ~(StackSize-1);
1965
H. Peter Anvine2c80182005-01-15 22:15:51 +00001966 /* Now define the macro for the argument */
1967 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1968 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001969 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001970 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001971
H. Peter Anvine2c80182005-01-15 22:15:51 +00001972 /* Move to the next argument in the list */
1973 tline = tline->next;
1974 if (tline && tline->type == TOK_WHITESPACE)
1975 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001976 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1977 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001978 free_tlist(origline);
1979 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001980
H. Peter Anvine2c80182005-01-15 22:15:51 +00001981 case PP_LOCAL:
1982 /* TASM like LOCAL directive to define local variables for a
1983 * function, in the following form:
1984 *
1985 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1986 *
1987 * The '= LocalSize' at the end is ignored by NASM, but is
1988 * required by TASM to define the local parameter size (and used
1989 * by the TASM macro package).
1990 */
1991 offset = LocalOffset;
1992 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001993 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001994 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001995
H. Peter Anvine2c80182005-01-15 22:15:51 +00001996 /* Find the argument name */
1997 tline = tline->next;
1998 if (tline && tline->type == TOK_WHITESPACE)
1999 tline = tline->next;
2000 if (!tline || tline->type != TOK_ID) {
2001 error(ERR_NONFATAL,
2002 "`%%local' missing argument parameter");
2003 free_tlist(origline);
2004 return DIRECTIVE_FOUND;
2005 }
2006 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002007
H. Peter Anvine2c80182005-01-15 22:15:51 +00002008 /* Find the argument size type */
2009 tline = tline->next;
2010 if (!tline || tline->type != TOK_OTHER
2011 || tline->text[0] != ':') {
2012 error(ERR_NONFATAL,
2013 "Syntax error processing `%%local' directive");
2014 free_tlist(origline);
2015 return DIRECTIVE_FOUND;
2016 }
2017 tline = tline->next;
2018 if (!tline || tline->type != TOK_ID) {
2019 error(ERR_NONFATAL,
2020 "`%%local' missing size type parameter");
2021 free_tlist(origline);
2022 return DIRECTIVE_FOUND;
2023 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002024
H. Peter Anvine2c80182005-01-15 22:15:51 +00002025 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002026 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002027 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002028 size = parse_size(tt->text);
2029 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002030 error(ERR_NONFATAL,
2031 "Invalid size type for `%%local' missing directive");
2032 free_tlist(tt);
2033 free_tlist(origline);
2034 return DIRECTIVE_FOUND;
2035 }
2036 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002037
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002038 /* Round up to even stack slots */
2039 size = (size+StackSize-1) & ~(StackSize-1);
2040
2041 offset += size; /* Negative offset, increment before */
2042
2043 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002044 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2045 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002046 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002047
H. Peter Anvine2c80182005-01-15 22:15:51 +00002048 /* Now define the assign to setup the enter_c macro correctly */
2049 snprintf(directive, sizeof(directive),
2050 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002051 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002052
H. Peter Anvine2c80182005-01-15 22:15:51 +00002053 /* Move to the next argument in the list */
2054 tline = tline->next;
2055 if (tline && tline->type == TOK_WHITESPACE)
2056 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002057 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2058 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002059 free_tlist(origline);
2060 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002061
H. Peter Anvine2c80182005-01-15 22:15:51 +00002062 case PP_CLEAR:
2063 if (tline->next)
2064 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002065 free_macros();
2066 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002067 free_tlist(origline);
2068 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002069
H. Peter Anvin418ca702008-05-30 10:42:30 -07002070 case PP_DEPEND:
2071 tline = expand_smacro(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002072 skip_white_(tline);
2073 if (!tline || (tline->type != TOK_STRING &&
2074 tline->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002075 error(ERR_NONFATAL, "`%%depend' expects a file name");
2076 free_tlist(origline);
2077 return DIRECTIVE_FOUND; /* but we did _something_ */
2078 }
2079 if (tline->next)
2080 error(ERR_WARNING,
2081 "trailing garbage after `%%depend' ignored");
2082 if (tline->type != TOK_INTERNAL_STRING) {
2083 p = tline->text + 1; /* point past the quote to the name */
2084 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2085 } else
2086 p = tline->text; /* internal_string is easier */
2087 if (dephead && !in_list(*dephead, p)) {
2088 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2089 sl->next = NULL;
2090 strcpy(sl->str, p);
2091 *deptail = sl;
2092 deptail = &sl->next;
2093 }
2094 free_tlist(origline);
2095 return DIRECTIVE_FOUND;
2096
2097 case PP_INCLUDE:
2098 tline = expand_smacro(tline->next);
2099 skip_white_(tline);
2100
2101 if (!tline || (tline->type != TOK_STRING &&
2102 tline->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002103 error(ERR_NONFATAL, "`%%include' expects a file name");
2104 free_tlist(origline);
2105 return DIRECTIVE_FOUND; /* but we did _something_ */
2106 }
2107 if (tline->next)
2108 error(ERR_WARNING,
2109 "trailing garbage after `%%include' ignored");
2110 if (tline->type != TOK_INTERNAL_STRING) {
2111 p = tline->text + 1; /* point past the quote to the name */
2112 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2113 } else
2114 p = tline->text; /* internal_string is easier */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002115 inc = nasm_malloc(sizeof(Include));
2116 inc->next = istk;
2117 inc->conds = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002118 inc->fp = inc_fopen(p, dephead, deptail, pass == 0);
2119 if (!inc->fp) {
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002120 /* -MG given but file not found */
2121 nasm_free(inc);
2122 } else {
2123 inc->fname = src_set_fname(p);
2124 inc->lineno = src_set_linnum(0);
2125 inc->lineinc = 1;
2126 inc->expansion = NULL;
2127 inc->mstk = NULL;
2128 istk = inc;
2129 list->uplevel(LIST_INCLUDE);
2130 }
2131 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002132 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002133
H. Peter Anvine2c80182005-01-15 22:15:51 +00002134 case PP_PUSH:
2135 tline = tline->next;
2136 skip_white_(tline);
2137 tline = expand_id(tline);
2138 if (!tok_type_(tline, TOK_ID)) {
2139 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2140 free_tlist(origline);
2141 return DIRECTIVE_FOUND; /* but we did _something_ */
2142 }
2143 if (tline->next)
2144 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2145 ctx = nasm_malloc(sizeof(Context));
2146 ctx->next = cstk;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002147 hash_init(&ctx->localmac, HASH_SMALL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002148 ctx->name = nasm_strdup(tline->text);
2149 ctx->number = unique++;
2150 cstk = ctx;
2151 free_tlist(origline);
2152 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002153
H. Peter Anvine2c80182005-01-15 22:15:51 +00002154 case PP_REPL:
2155 tline = tline->next;
2156 skip_white_(tline);
2157 tline = expand_id(tline);
2158 if (!tok_type_(tline, TOK_ID)) {
2159 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2160 free_tlist(origline);
2161 return DIRECTIVE_FOUND; /* but we did _something_ */
2162 }
2163 if (tline->next)
2164 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2165 if (!cstk)
2166 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2167 else {
2168 nasm_free(cstk->name);
2169 cstk->name = nasm_strdup(tline->text);
2170 }
2171 free_tlist(origline);
2172 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002173
H. Peter Anvine2c80182005-01-15 22:15:51 +00002174 case PP_POP:
2175 if (tline->next)
2176 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2177 if (!cstk)
2178 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2179 else
2180 ctx_pop();
2181 free_tlist(origline);
2182 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002183
H. Peter Anvine2c80182005-01-15 22:15:51 +00002184 case PP_ERROR:
2185 tline->next = expand_smacro(tline->next);
2186 tline = tline->next;
2187 skip_white_(tline);
2188 if (tok_type_(tline, TOK_STRING)) {
2189 p = tline->text + 1; /* point past the quote to the name */
2190 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
H. Peter Anvin418ca702008-05-30 10:42:30 -07002191 expand_macros_in_string(&p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002192 error(ERR_NONFATAL, "%s", p);
2193 nasm_free(p);
2194 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002195 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002196 error(ERR_WARNING, "%s", p);
2197 nasm_free(p);
2198 }
2199 free_tlist(origline);
2200 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002201
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002202 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002203 if (istk->conds && !emitting(istk->conds->state))
2204 j = COND_NEVER;
2205 else {
2206 j = if_condition(tline->next, i);
2207 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002208 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2209 }
2210 cond = nasm_malloc(sizeof(Cond));
2211 cond->next = istk->conds;
2212 cond->state = j;
2213 istk->conds = cond;
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002214 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002215 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002216
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002217 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002218 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002219 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002220 if (emitting(istk->conds->state)
2221 || istk->conds->state == COND_NEVER)
2222 istk->conds->state = COND_NEVER;
2223 else {
2224 /*
2225 * IMPORTANT: In the case of %if, we will already have
2226 * called expand_mmac_params(); however, if we're
2227 * processing an %elif we must have been in a
2228 * non-emitting mode, which would have inhibited
2229 * the normal invocation of expand_mmac_params(). Therefore,
2230 * we have to do it explicitly here.
2231 */
2232 j = if_condition(expand_mmac_params(tline->next), i);
2233 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002234 istk->conds->state =
2235 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2236 }
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002237 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002238 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002239
H. Peter Anvine2c80182005-01-15 22:15:51 +00002240 case PP_ELSE:
2241 if (tline->next)
2242 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2243 if (!istk->conds)
2244 error(ERR_FATAL, "`%%else': no matching `%%if'");
2245 if (emitting(istk->conds->state)
2246 || istk->conds->state == COND_NEVER)
2247 istk->conds->state = COND_ELSE_FALSE;
2248 else
2249 istk->conds->state = COND_ELSE_TRUE;
2250 free_tlist(origline);
2251 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002252
H. Peter Anvine2c80182005-01-15 22:15:51 +00002253 case PP_ENDIF:
2254 if (tline->next)
2255 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2256 if (!istk->conds)
2257 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2258 cond = istk->conds;
2259 istk->conds = cond->next;
2260 nasm_free(cond);
2261 free_tlist(origline);
2262 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002263
H. Peter Anvine2c80182005-01-15 22:15:51 +00002264 case PP_MACRO:
2265 case PP_IMACRO:
2266 if (defining)
2267 error(ERR_FATAL,
2268 "`%%%smacro': already defining a macro",
2269 (i == PP_IMACRO ? "i" : ""));
2270 tline = tline->next;
2271 skip_white_(tline);
2272 tline = expand_id(tline);
2273 if (!tok_type_(tline, TOK_ID)) {
2274 error(ERR_NONFATAL,
2275 "`%%%smacro' expects a macro name",
2276 (i == PP_IMACRO ? "i" : ""));
2277 return DIRECTIVE_FOUND;
2278 }
2279 defining = nasm_malloc(sizeof(MMacro));
2280 defining->name = nasm_strdup(tline->text);
2281 defining->casesense = (i == PP_MACRO);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002282 defining->plus = false;
2283 defining->nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002284 defining->in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002285 defining->rep_nest = NULL;
2286 tline = expand_smacro(tline->next);
2287 skip_white_(tline);
2288 if (!tok_type_(tline, TOK_NUMBER)) {
2289 error(ERR_NONFATAL,
2290 "`%%%smacro' expects a parameter count",
2291 (i == PP_IMACRO ? "i" : ""));
2292 defining->nparam_min = defining->nparam_max = 0;
2293 } else {
2294 defining->nparam_min = defining->nparam_max =
H. Peter Anvin70055962007-10-11 00:05:31 -07002295 readnum(tline->text, &err);
2296 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002297 error(ERR_NONFATAL,
2298 "unable to parse parameter count `%s'", tline->text);
2299 }
2300 if (tline && tok_is_(tline->next, "-")) {
2301 tline = tline->next->next;
2302 if (tok_is_(tline, "*"))
2303 defining->nparam_max = INT_MAX;
2304 else if (!tok_type_(tline, TOK_NUMBER))
2305 error(ERR_NONFATAL,
2306 "`%%%smacro' expects a parameter count after `-'",
2307 (i == PP_IMACRO ? "i" : ""));
2308 else {
H. Peter Anvin70055962007-10-11 00:05:31 -07002309 defining->nparam_max = readnum(tline->text, &err);
2310 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002311 error(ERR_NONFATAL,
2312 "unable to parse parameter count `%s'",
2313 tline->text);
2314 if (defining->nparam_min > defining->nparam_max)
2315 error(ERR_NONFATAL,
2316 "minimum parameter count exceeds maximum");
2317 }
2318 }
2319 if (tline && tok_is_(tline->next, "+")) {
2320 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002321 defining->plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002322 }
2323 if (tline && tok_type_(tline->next, TOK_ID) &&
2324 !nasm_stricmp(tline->next->text, ".nolist")) {
2325 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002326 defining->nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002327 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002328 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002329 while (mmac) {
2330 if (!strcmp(mmac->name, defining->name) &&
2331 (mmac->nparam_min <= defining->nparam_max
2332 || defining->plus)
2333 && (defining->nparam_min <= mmac->nparam_max
2334 || mmac->plus)) {
2335 error(ERR_WARNING,
2336 "redefining multi-line macro `%s'", defining->name);
2337 break;
2338 }
2339 mmac = mmac->next;
2340 }
2341 /*
2342 * Handle default parameters.
2343 */
2344 if (tline && tline->next) {
2345 defining->dlist = tline->next;
2346 tline->next = NULL;
2347 count_mmac_params(defining->dlist, &defining->ndefs,
2348 &defining->defaults);
2349 } else {
2350 defining->dlist = NULL;
2351 defining->defaults = NULL;
2352 }
2353 defining->expansion = NULL;
2354 free_tlist(origline);
2355 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002356
H. Peter Anvine2c80182005-01-15 22:15:51 +00002357 case PP_ENDM:
2358 case PP_ENDMACRO:
2359 if (!defining) {
2360 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2361 return DIRECTIVE_FOUND;
2362 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002363 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002364 defining->next = *mmhead;
2365 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002366 defining = NULL;
2367 free_tlist(origline);
2368 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002369
H. Peter Anvine2c80182005-01-15 22:15:51 +00002370 case PP_ROTATE:
2371 if (tline->next && tline->next->type == TOK_WHITESPACE)
2372 tline = tline->next;
2373 if (tline->next == NULL) {
2374 free_tlist(origline);
2375 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2376 return DIRECTIVE_FOUND;
2377 }
2378 t = expand_smacro(tline->next);
2379 tline->next = NULL;
2380 free_tlist(origline);
2381 tline = t;
2382 tptr = &t;
2383 tokval.t_type = TOKEN_INVALID;
2384 evalresult =
2385 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2386 free_tlist(tline);
2387 if (!evalresult)
2388 return DIRECTIVE_FOUND;
2389 if (tokval.t_type)
2390 error(ERR_WARNING,
2391 "trailing garbage after expression ignored");
2392 if (!is_simple(evalresult)) {
2393 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2394 return DIRECTIVE_FOUND;
2395 }
2396 mmac = istk->mstk;
2397 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2398 mmac = mmac->next_active;
2399 if (!mmac) {
2400 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2401 } else if (mmac->nparam == 0) {
2402 error(ERR_NONFATAL,
2403 "`%%rotate' invoked within macro without parameters");
2404 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002405 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002406
H. Peter Anvin25a99342007-09-22 17:45:45 -07002407 rotate %= (int)mmac->nparam;
2408 if (rotate < 0)
2409 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002410
H. Peter Anvin25a99342007-09-22 17:45:45 -07002411 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002412 }
2413 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002414
H. Peter Anvine2c80182005-01-15 22:15:51 +00002415 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002416 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002417 do {
2418 tline = tline->next;
2419 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002420
H. Peter Anvine2c80182005-01-15 22:15:51 +00002421 if (tok_type_(tline, TOK_ID) &&
2422 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002423 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002424 do {
2425 tline = tline->next;
2426 } while (tok_type_(tline, TOK_WHITESPACE));
2427 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002428
H. Peter Anvine2c80182005-01-15 22:15:51 +00002429 if (tline) {
2430 t = expand_smacro(tline);
2431 tptr = &t;
2432 tokval.t_type = TOKEN_INVALID;
2433 evalresult =
2434 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2435 if (!evalresult) {
2436 free_tlist(origline);
2437 return DIRECTIVE_FOUND;
2438 }
2439 if (tokval.t_type)
2440 error(ERR_WARNING,
2441 "trailing garbage after expression ignored");
2442 if (!is_simple(evalresult)) {
2443 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2444 return DIRECTIVE_FOUND;
2445 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002446 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002447 } else {
2448 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002449 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002450 }
2451 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002452
H. Peter Anvine2c80182005-01-15 22:15:51 +00002453 tmp_defining = defining;
2454 defining = nasm_malloc(sizeof(MMacro));
2455 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002456 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002457 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002458 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002459 defining->in_progress = count;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002460 defining->nparam_min = defining->nparam_max = 0;
2461 defining->defaults = NULL;
2462 defining->dlist = NULL;
2463 defining->expansion = NULL;
2464 defining->next_active = istk->mstk;
2465 defining->rep_nest = tmp_defining;
2466 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002467
H. Peter Anvine2c80182005-01-15 22:15:51 +00002468 case PP_ENDREP:
2469 if (!defining || defining->name) {
2470 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2471 return DIRECTIVE_FOUND;
2472 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002473
H. Peter Anvine2c80182005-01-15 22:15:51 +00002474 /*
2475 * Now we have a "macro" defined - although it has no name
2476 * and we won't be entering it in the hash tables - we must
2477 * push a macro-end marker for it on to istk->expansion.
2478 * After that, it will take care of propagating itself (a
2479 * macro-end marker line for a macro which is really a %rep
2480 * block will cause the macro to be re-expanded, complete
2481 * with another macro-end marker to ensure the process
2482 * continues) until the whole expansion is forcibly removed
2483 * from istk->expansion by a %exitrep.
2484 */
2485 l = nasm_malloc(sizeof(Line));
2486 l->next = istk->expansion;
2487 l->finishes = defining;
2488 l->first = NULL;
2489 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002490
H. Peter Anvine2c80182005-01-15 22:15:51 +00002491 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002492
H. Peter Anvine2c80182005-01-15 22:15:51 +00002493 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2494 tmp_defining = defining;
2495 defining = defining->rep_nest;
2496 free_tlist(origline);
2497 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002498
H. Peter Anvine2c80182005-01-15 22:15:51 +00002499 case PP_EXITREP:
2500 /*
2501 * We must search along istk->expansion until we hit a
2502 * macro-end marker for a macro with no name. Then we set
2503 * its `in_progress' flag to 0.
2504 */
2505 for (l = istk->expansion; l; l = l->next)
2506 if (l->finishes && !l->finishes->name)
2507 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002508
H. Peter Anvine2c80182005-01-15 22:15:51 +00002509 if (l)
2510 l->finishes->in_progress = 0;
2511 else
2512 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2513 free_tlist(origline);
2514 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002515
H. Peter Anvine2c80182005-01-15 22:15:51 +00002516 case PP_XDEFINE:
2517 case PP_IXDEFINE:
2518 case PP_DEFINE:
2519 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002520 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002521
H. Peter Anvine2c80182005-01-15 22:15:51 +00002522 tline = tline->next;
2523 skip_white_(tline);
2524 tline = expand_id(tline);
2525 if (!tline || (tline->type != TOK_ID &&
2526 (tline->type != TOK_PREPROC_ID ||
2527 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002528 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2529 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002530 free_tlist(origline);
2531 return DIRECTIVE_FOUND;
2532 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002533
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002534 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002535
H. Peter Anvine2c80182005-01-15 22:15:51 +00002536 mname = tline->text;
2537 last = tline;
2538 param_start = tline = tline->next;
2539 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002540
H. Peter Anvine2c80182005-01-15 22:15:51 +00002541 /* Expand the macro definition now for %xdefine and %ixdefine */
2542 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2543 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002544
H. Peter Anvine2c80182005-01-15 22:15:51 +00002545 if (tok_is_(tline, "(")) {
2546 /*
2547 * This macro has parameters.
2548 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002549
H. Peter Anvine2c80182005-01-15 22:15:51 +00002550 tline = tline->next;
2551 while (1) {
2552 skip_white_(tline);
2553 if (!tline) {
2554 error(ERR_NONFATAL, "parameter identifier expected");
2555 free_tlist(origline);
2556 return DIRECTIVE_FOUND;
2557 }
2558 if (tline->type != TOK_ID) {
2559 error(ERR_NONFATAL,
2560 "`%s': parameter identifier expected",
2561 tline->text);
2562 free_tlist(origline);
2563 return DIRECTIVE_FOUND;
2564 }
2565 tline->type = TOK_SMAC_PARAM + nparam++;
2566 tline = tline->next;
2567 skip_white_(tline);
2568 if (tok_is_(tline, ",")) {
2569 tline = tline->next;
2570 continue;
2571 }
2572 if (!tok_is_(tline, ")")) {
2573 error(ERR_NONFATAL,
2574 "`)' expected to terminate macro template");
2575 free_tlist(origline);
2576 return DIRECTIVE_FOUND;
2577 }
2578 break;
2579 }
2580 last = tline;
2581 tline = tline->next;
2582 }
2583 if (tok_type_(tline, TOK_WHITESPACE))
2584 last = tline, tline = tline->next;
2585 macro_start = NULL;
2586 last->next = NULL;
2587 t = tline;
2588 while (t) {
2589 if (t->type == TOK_ID) {
2590 for (tt = param_start; tt; tt = tt->next)
2591 if (tt->type >= TOK_SMAC_PARAM &&
2592 !strcmp(tt->text, t->text))
2593 t->type = tt->type;
2594 }
2595 tt = t->next;
2596 t->next = macro_start;
2597 macro_start = t;
2598 t = tt;
2599 }
2600 /*
2601 * Good. We now have a macro name, a parameter count, and a
2602 * token list (in reverse order) for an expansion. We ought
2603 * to be OK just to create an SMacro, store it, and let
2604 * free_tlist have the rest of the line (which we have
2605 * carefully re-terminated after chopping off the expansion
2606 * from the end).
2607 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002608 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002609 free_tlist(origline);
2610 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002611
H. Peter Anvine2c80182005-01-15 22:15:51 +00002612 case PP_UNDEF:
2613 tline = tline->next;
2614 skip_white_(tline);
2615 tline = expand_id(tline);
2616 if (!tline || (tline->type != TOK_ID &&
2617 (tline->type != TOK_PREPROC_ID ||
2618 tline->text[1] != '$'))) {
2619 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2620 free_tlist(origline);
2621 return DIRECTIVE_FOUND;
2622 }
2623 if (tline->next) {
2624 error(ERR_WARNING,
2625 "trailing garbage after macro name ignored");
2626 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002627
H. Peter Anvine2c80182005-01-15 22:15:51 +00002628 /* Find the context that symbol belongs to */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002629 ctx = get_ctx(tline->text, false);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002630 undef_smacro(ctx, tline->text);
2631 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002632 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002633
H. Peter Anvin418ca702008-05-30 10:42:30 -07002634 case PP_PATHSEARCH:
2635 {
2636 FILE *fp;
2637 StrList *xsl = NULL;
2638
2639 casesense = true;
2640
2641 tline = tline->next;
2642 skip_white_(tline);
2643 tline = expand_id(tline);
2644 if (!tline || (tline->type != TOK_ID &&
2645 (tline->type != TOK_PREPROC_ID ||
2646 tline->text[1] != '$'))) {
2647 error(ERR_NONFATAL,
2648 "`%%pathsearch' expects a macro identifier as first parameter");
2649 free_tlist(origline);
2650 return DIRECTIVE_FOUND;
2651 }
2652 ctx = get_ctx(tline->text, false);
2653
2654 mname = tline->text;
2655 last = tline;
2656 tline = expand_smacro(tline->next);
2657 last->next = NULL;
2658
2659 t = tline;
2660 while (tok_type_(t, TOK_WHITESPACE))
2661 t = t->next;
2662
2663 if (!t || (t->type != TOK_STRING &&
2664 t->type != TOK_INTERNAL_STRING)) {
2665 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
2666 free_tlist(tline);
2667 free_tlist(origline);
2668 return DIRECTIVE_FOUND; /* but we did _something_ */
2669 }
2670 if (t->next)
2671 error(ERR_WARNING,
2672 "trailing garbage after `%%pathsearch' ignored");
2673 if (t->type != TOK_INTERNAL_STRING) {
2674 p = t->text + 1; /* point past the quote to the name */
2675 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2676 } else
2677 p = t->text; /* internal_string is easier */
2678
2679 fp = inc_fopen(p, &xsl, &xsl, true);
2680 if (fp) {
2681 p = xsl->str;
2682 fclose(fp); /* Don't actually care about the file */
2683 }
2684 macro_start = nasm_malloc(sizeof(*macro_start));
2685 macro_start->next = NULL;
2686 macro_start->text = nasm_strdup(p);
2687 nasm_quote(&macro_start->text);
2688 macro_start->type = TOK_STRING;
2689 macro_start->mac = NULL;
2690 if (xsl)
2691 nasm_free(xsl);
2692
2693 /*
2694 * We now have a macro name, an implicit parameter count of
2695 * zero, and a string token to use as an expansion. Create
2696 * and store an SMacro.
2697 */
2698 define_smacro(ctx, mname, casesense, 0, macro_start);
2699 free_tlist(tline);
2700 free_tlist(origline);
2701 return DIRECTIVE_FOUND;
2702 }
2703
H. Peter Anvine2c80182005-01-15 22:15:51 +00002704 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002705 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07002706
H. Peter Anvine2c80182005-01-15 22:15:51 +00002707 tline = tline->next;
2708 skip_white_(tline);
2709 tline = expand_id(tline);
2710 if (!tline || (tline->type != TOK_ID &&
2711 (tline->type != TOK_PREPROC_ID ||
2712 tline->text[1] != '$'))) {
2713 error(ERR_NONFATAL,
2714 "`%%strlen' expects a macro identifier as first parameter");
2715 free_tlist(origline);
2716 return DIRECTIVE_FOUND;
2717 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002718 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002719
H. Peter Anvine2c80182005-01-15 22:15:51 +00002720 mname = tline->text;
2721 last = tline;
2722 tline = expand_smacro(tline->next);
2723 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002724
H. Peter Anvine2c80182005-01-15 22:15:51 +00002725 t = tline;
2726 while (tok_type_(t, TOK_WHITESPACE))
2727 t = t->next;
2728 /* t should now point to the string */
2729 if (t->type != TOK_STRING) {
2730 error(ERR_NONFATAL,
2731 "`%%strlen` requires string as second parameter");
2732 free_tlist(tline);
2733 free_tlist(origline);
2734 return DIRECTIVE_FOUND;
2735 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002736
H. Peter Anvine2c80182005-01-15 22:15:51 +00002737 macro_start = nasm_malloc(sizeof(*macro_start));
2738 macro_start->next = NULL;
2739 make_tok_num(macro_start, strlen(t->text) - 2);
2740 macro_start->mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002741
H. Peter Anvine2c80182005-01-15 22:15:51 +00002742 /*
2743 * We now have a macro name, an implicit parameter count of
2744 * zero, and a numeric token to use as an expansion. Create
2745 * and store an SMacro.
2746 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002747 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002748 free_tlist(tline);
2749 free_tlist(origline);
2750 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002751
H. Peter Anvine2c80182005-01-15 22:15:51 +00002752 case PP_SUBSTR:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002753 casesense = true;
2754
H. Peter Anvine2c80182005-01-15 22:15:51 +00002755 tline = tline->next;
2756 skip_white_(tline);
2757 tline = expand_id(tline);
2758 if (!tline || (tline->type != TOK_ID &&
2759 (tline->type != TOK_PREPROC_ID ||
2760 tline->text[1] != '$'))) {
2761 error(ERR_NONFATAL,
2762 "`%%substr' expects a macro identifier as first parameter");
2763 free_tlist(origline);
2764 return DIRECTIVE_FOUND;
2765 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002766 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002767
H. Peter Anvine2c80182005-01-15 22:15:51 +00002768 mname = tline->text;
2769 last = tline;
2770 tline = expand_smacro(tline->next);
2771 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002772
H. Peter Anvine2c80182005-01-15 22:15:51 +00002773 t = tline->next;
2774 while (tok_type_(t, TOK_WHITESPACE))
2775 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002776
H. Peter Anvine2c80182005-01-15 22:15:51 +00002777 /* t should now point to the string */
2778 if (t->type != TOK_STRING) {
2779 error(ERR_NONFATAL,
2780 "`%%substr` requires string as second parameter");
2781 free_tlist(tline);
2782 free_tlist(origline);
2783 return DIRECTIVE_FOUND;
2784 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002785
H. Peter Anvine2c80182005-01-15 22:15:51 +00002786 tt = t->next;
2787 tptr = &tt;
2788 tokval.t_type = TOKEN_INVALID;
2789 evalresult =
2790 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2791 if (!evalresult) {
2792 free_tlist(tline);
2793 free_tlist(origline);
2794 return DIRECTIVE_FOUND;
2795 }
2796 if (!is_simple(evalresult)) {
2797 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2798 free_tlist(tline);
2799 free_tlist(origline);
2800 return DIRECTIVE_FOUND;
2801 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002802
H. Peter Anvine2c80182005-01-15 22:15:51 +00002803 macro_start = nasm_malloc(sizeof(*macro_start));
2804 macro_start->next = NULL;
2805 macro_start->text = nasm_strdup("'''");
2806 if (evalresult->value > 0
Charles Crayne192d5b52007-10-18 19:02:42 -07002807 && evalresult->value < (int) strlen(t->text) - 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002808 macro_start->text[1] = t->text[evalresult->value];
2809 } else {
2810 macro_start->text[2] = '\0';
2811 }
2812 macro_start->type = TOK_STRING;
2813 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002814
H. Peter Anvine2c80182005-01-15 22:15:51 +00002815 /*
2816 * We now have a macro name, an implicit parameter count of
2817 * zero, and a numeric token to use as an expansion. Create
2818 * and store an SMacro.
2819 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002820 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002821 free_tlist(tline);
2822 free_tlist(origline);
2823 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002824
H. Peter Anvine2c80182005-01-15 22:15:51 +00002825 case PP_ASSIGN:
2826 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002827 casesense = (i == PP_ASSIGN);
2828
H. Peter Anvine2c80182005-01-15 22:15:51 +00002829 tline = tline->next;
2830 skip_white_(tline);
2831 tline = expand_id(tline);
2832 if (!tline || (tline->type != TOK_ID &&
2833 (tline->type != TOK_PREPROC_ID ||
2834 tline->text[1] != '$'))) {
2835 error(ERR_NONFATAL,
2836 "`%%%sassign' expects a macro identifier",
2837 (i == PP_IASSIGN ? "i" : ""));
2838 free_tlist(origline);
2839 return DIRECTIVE_FOUND;
2840 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002841 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002842
H. Peter Anvine2c80182005-01-15 22:15:51 +00002843 mname = tline->text;
2844 last = tline;
2845 tline = expand_smacro(tline->next);
2846 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002847
H. Peter Anvine2c80182005-01-15 22:15:51 +00002848 t = tline;
2849 tptr = &t;
2850 tokval.t_type = TOKEN_INVALID;
2851 evalresult =
2852 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2853 free_tlist(tline);
2854 if (!evalresult) {
2855 free_tlist(origline);
2856 return DIRECTIVE_FOUND;
2857 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002858
H. Peter Anvine2c80182005-01-15 22:15:51 +00002859 if (tokval.t_type)
2860 error(ERR_WARNING,
2861 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00002862
H. Peter Anvine2c80182005-01-15 22:15:51 +00002863 if (!is_simple(evalresult)) {
2864 error(ERR_NONFATAL,
2865 "non-constant value given to `%%%sassign'",
2866 (i == PP_IASSIGN ? "i" : ""));
2867 free_tlist(origline);
2868 return DIRECTIVE_FOUND;
2869 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002870
H. Peter Anvine2c80182005-01-15 22:15:51 +00002871 macro_start = nasm_malloc(sizeof(*macro_start));
2872 macro_start->next = NULL;
2873 make_tok_num(macro_start, reloc_value(evalresult));
2874 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002875
H. Peter Anvine2c80182005-01-15 22:15:51 +00002876 /*
2877 * We now have a macro name, an implicit parameter count of
2878 * zero, and a numeric token to use as an expansion. Create
2879 * and store an SMacro.
2880 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002881 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002882 free_tlist(origline);
2883 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002884
H. Peter Anvine2c80182005-01-15 22:15:51 +00002885 case PP_LINE:
2886 /*
2887 * Syntax is `%line nnn[+mmm] [filename]'
2888 */
2889 tline = tline->next;
2890 skip_white_(tline);
2891 if (!tok_type_(tline, TOK_NUMBER)) {
2892 error(ERR_NONFATAL, "`%%line' expects line number");
2893 free_tlist(origline);
2894 return DIRECTIVE_FOUND;
2895 }
H. Peter Anvin70055962007-10-11 00:05:31 -07002896 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002897 m = 1;
2898 tline = tline->next;
2899 if (tok_is_(tline, "+")) {
2900 tline = tline->next;
2901 if (!tok_type_(tline, TOK_NUMBER)) {
2902 error(ERR_NONFATAL, "`%%line' expects line increment");
2903 free_tlist(origline);
2904 return DIRECTIVE_FOUND;
2905 }
H. Peter Anvin70055962007-10-11 00:05:31 -07002906 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002907 tline = tline->next;
2908 }
2909 skip_white_(tline);
2910 src_set_linnum(k);
2911 istk->lineinc = m;
2912 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002913 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002914 }
2915 free_tlist(origline);
2916 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002917
H. Peter Anvine2c80182005-01-15 22:15:51 +00002918 default:
2919 error(ERR_FATAL,
2920 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00002921 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002922 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002923 }
Ed Beroset3ab3f412002-06-11 03:31:49 +00002924 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002925}
2926
2927/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002928 * Ensure that a macro parameter contains a condition code and
2929 * nothing else. Return the condition code index if so, or -1
2930 * otherwise.
2931 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002932static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002933{
H. Peter Anvin76690a12002-04-30 20:52:49 +00002934 Token *tt;
2935 int i, j, k, m;
2936
H. Peter Anvin25a99342007-09-22 17:45:45 -07002937 if (!t)
2938 return -1; /* Probably a %+ without a space */
2939
H. Peter Anvineba20a72002-04-30 20:53:55 +00002940 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002941 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002942 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002943 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002944 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002945 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002946 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002947
2948 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00002949 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002950 while (j - i > 1) {
2951 k = (j + i) / 2;
2952 m = nasm_stricmp(t->text, conditions[k]);
2953 if (m == 0) {
2954 i = k;
2955 j = -2;
2956 break;
2957 } else if (m < 0) {
2958 j = k;
2959 } else
2960 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002961 }
2962 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002963 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002964 return i;
2965}
2966
2967/*
2968 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2969 * %-n) and MMacro-local identifiers (%%foo).
2970 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002971static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002972{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002973 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002974
2975 tail = &thead;
2976 thead = NULL;
2977
H. Peter Anvine2c80182005-01-15 22:15:51 +00002978 while (tline) {
2979 if (tline->type == TOK_PREPROC_ID &&
2980 (((tline->text[1] == '+' || tline->text[1] == '-')
2981 && tline->text[2]) || tline->text[1] == '%'
2982 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002983 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002984 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002985 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07002986 unsigned int n;
2987 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002988 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002989
H. Peter Anvine2c80182005-01-15 22:15:51 +00002990 t = tline;
2991 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002992
H. Peter Anvine2c80182005-01-15 22:15:51 +00002993 mac = istk->mstk;
2994 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2995 mac = mac->next_active;
2996 if (!mac)
2997 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2998 else
2999 switch (t->text[1]) {
3000 /*
3001 * We have to make a substitution of one of the
3002 * forms %1, %-1, %+1, %%foo, %0.
3003 */
3004 case '0':
3005 type = TOK_NUMBER;
3006 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3007 text = nasm_strdup(tmpbuf);
3008 break;
3009 case '%':
3010 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003011 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00003012 mac->unique);
3013 text = nasm_strcat(tmpbuf, t->text + 2);
3014 break;
3015 case '-':
3016 n = atoi(t->text + 2) - 1;
3017 if (n >= mac->nparam)
3018 tt = NULL;
3019 else {
3020 if (mac->nparam > 1)
3021 n = (n + mac->rotate) % mac->nparam;
3022 tt = mac->params[n];
3023 }
3024 cc = find_cc(tt);
3025 if (cc == -1) {
3026 error(ERR_NONFATAL,
3027 "macro parameter %d is not a condition code",
3028 n + 1);
3029 text = NULL;
3030 } else {
3031 type = TOK_ID;
3032 if (inverse_ccs[cc] == -1) {
3033 error(ERR_NONFATAL,
3034 "condition code `%s' is not invertible",
3035 conditions[cc]);
3036 text = NULL;
3037 } else
3038 text =
3039 nasm_strdup(conditions[inverse_ccs[cc]]);
3040 }
3041 break;
3042 case '+':
3043 n = atoi(t->text + 2) - 1;
3044 if (n >= mac->nparam)
3045 tt = NULL;
3046 else {
3047 if (mac->nparam > 1)
3048 n = (n + mac->rotate) % mac->nparam;
3049 tt = mac->params[n];
3050 }
3051 cc = find_cc(tt);
3052 if (cc == -1) {
3053 error(ERR_NONFATAL,
3054 "macro parameter %d is not a condition code",
3055 n + 1);
3056 text = NULL;
3057 } else {
3058 type = TOK_ID;
3059 text = nasm_strdup(conditions[cc]);
3060 }
3061 break;
3062 default:
3063 n = atoi(t->text + 1) - 1;
3064 if (n >= mac->nparam)
3065 tt = NULL;
3066 else {
3067 if (mac->nparam > 1)
3068 n = (n + mac->rotate) % mac->nparam;
3069 tt = mac->params[n];
3070 }
3071 if (tt) {
3072 for (i = 0; i < mac->paramlen[n]; i++) {
3073 *tail = new_Token(NULL, tt->type, tt->text, 0);
3074 tail = &(*tail)->next;
3075 tt = tt->next;
3076 }
3077 }
3078 text = NULL; /* we've done it here */
3079 break;
3080 }
3081 if (!text) {
3082 delete_Token(t);
3083 } else {
3084 *tail = t;
3085 tail = &t->next;
3086 t->type = type;
3087 nasm_free(t->text);
3088 t->text = text;
3089 t->mac = NULL;
3090 }
3091 continue;
3092 } else {
3093 t = *tail = tline;
3094 tline = tline->next;
3095 t->mac = NULL;
3096 tail = &t->next;
3097 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003098 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003099 *tail = NULL;
3100 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003101 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003102 switch (t->type) {
3103 case TOK_WHITESPACE:
3104 if (tt->type == TOK_WHITESPACE) {
3105 t->next = delete_Token(tt);
3106 }
3107 break;
3108 case TOK_ID:
3109 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003110 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003111 nasm_free(t->text);
3112 t->text = tmp;
3113 t->next = delete_Token(tt);
3114 }
3115 break;
3116 case TOK_NUMBER:
3117 if (tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003118 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003119 nasm_free(t->text);
3120 t->text = tmp;
3121 t->next = delete_Token(tt);
3122 }
3123 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003124 default:
3125 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003126 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003127
H. Peter Anvin76690a12002-04-30 20:52:49 +00003128 return thead;
3129}
3130
3131/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003132 * Expand all single-line macro calls made in the given line.
3133 * Return the expanded version of the line. The original is deemed
3134 * to be destroyed in the process. (In reality we'll just move
3135 * Tokens from input to output a lot of the time, rather than
3136 * actually bothering to destroy and replicate.)
3137 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003138#define DEADMAN_LIMIT (1 << 20)
3139
H. Peter Anvine2c80182005-01-15 22:15:51 +00003140static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003141{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003142 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003143 struct hash_table *smtbl;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003144 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003145 Token **params;
3146 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003147 unsigned int nparam, sparam;
3148 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003149 Token *org_tline = tline;
3150 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003151 char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003152 int deadman = DEADMAN_LIMIT;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003153
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003154 /*
3155 * Trick: we should avoid changing the start token pointer since it can
3156 * be contained in "next" field of other token. Because of this
3157 * we allocate a copy of first token and work with it; at the end of
3158 * routine we copy it back
3159 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003160 if (org_tline) {
3161 tline =
3162 new_Token(org_tline->next, org_tline->type, org_tline->text,
3163 0);
3164 tline->mac = org_tline->mac;
3165 nasm_free(org_tline->text);
3166 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003167 }
3168
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003169again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003170 tail = &thead;
3171 thead = NULL;
3172
H. Peter Anvine2c80182005-01-15 22:15:51 +00003173 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003174 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003175 error(ERR_NONFATAL, "interminable macro recursion");
3176 break;
3177 }
3178
H. Peter Anvine2c80182005-01-15 22:15:51 +00003179 if ((mname = tline->text)) {
3180 /* if this token is a local macro, look in local context */
H. Peter Anvinb037a672008-05-29 19:08:08 -07003181 ctx = NULL;
3182 smtbl = &smacros;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003183 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003184 ctx = get_ctx(mname, true);
H. Peter Anvinb037a672008-05-29 19:08:08 -07003185 if (ctx)
3186 smtbl = &ctx->localmac;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003187 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07003188 head = (SMacro *) hash_findix(smtbl, mname);
H. Peter Anvin072771e2008-05-22 13:17:51 -07003189
H. Peter Anvine2c80182005-01-15 22:15:51 +00003190 /*
3191 * We've hit an identifier. As in is_mmacro below, we first
3192 * check whether the identifier is a single-line macro at
3193 * all, then think about checking for parameters if
3194 * necessary.
3195 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003196 for (m = head; m; m = m->next)
3197 if (!mstrcmp(m->name, mname, m->casesense))
3198 break;
3199 if (m) {
3200 mstart = tline;
3201 params = NULL;
3202 paramsize = NULL;
3203 if (m->nparam == 0) {
3204 /*
3205 * Simple case: the macro is parameterless. Discard the
3206 * one token that the macro call took, and push the
3207 * expansion back on the to-do stack.
3208 */
3209 if (!m->expansion) {
3210 if (!strcmp("__FILE__", m->name)) {
3211 int32_t num = 0;
3212 src_get(&num, &(tline->text));
3213 nasm_quote(&(tline->text));
3214 tline->type = TOK_STRING;
3215 continue;
3216 }
3217 if (!strcmp("__LINE__", m->name)) {
3218 nasm_free(tline->text);
3219 make_tok_num(tline, src_get_linnum());
3220 continue;
3221 }
3222 if (!strcmp("__BITS__", m->name)) {
3223 nasm_free(tline->text);
3224 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003225 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003226 }
3227 tline = delete_Token(tline);
3228 continue;
3229 }
3230 } else {
3231 /*
3232 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003233 * exists and takes parameters. We must find the
3234 * parameters in the call, count them, find the SMacro
3235 * that corresponds to that form of the macro call, and
3236 * substitute for the parameters when we expand. What a
3237 * pain.
3238 */
3239 /*tline = tline->next;
3240 skip_white_(tline); */
3241 do {
3242 t = tline->next;
3243 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003244 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003245 t->text = NULL;
3246 t = tline->next = delete_Token(t);
3247 }
3248 tline = t;
3249 } while (tok_type_(tline, TOK_WHITESPACE));
3250 if (!tok_is_(tline, "(")) {
3251 /*
3252 * This macro wasn't called with parameters: ignore
3253 * the call. (Behaviour borrowed from gnu cpp.)
3254 */
3255 tline = mstart;
3256 m = NULL;
3257 } else {
3258 int paren = 0;
3259 int white = 0;
3260 brackets = 0;
3261 nparam = 0;
3262 sparam = PARAM_DELTA;
3263 params = nasm_malloc(sparam * sizeof(Token *));
3264 params[0] = tline->next;
3265 paramsize = nasm_malloc(sparam * sizeof(int));
3266 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003267 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003268 /*
3269 * For some unusual expansions
3270 * which concatenates function call
3271 */
3272 t = tline->next;
3273 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003274 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003275 t->text = NULL;
3276 t = tline->next = delete_Token(t);
3277 }
3278 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003279
H. Peter Anvine2c80182005-01-15 22:15:51 +00003280 if (!tline) {
3281 error(ERR_NONFATAL,
3282 "macro call expects terminating `)'");
3283 break;
3284 }
3285 if (tline->type == TOK_WHITESPACE
3286 && brackets <= 0) {
3287 if (paramsize[nparam])
3288 white++;
3289 else
3290 params[nparam] = tline->next;
3291 continue; /* parameter loop */
3292 }
3293 if (tline->type == TOK_OTHER
3294 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003295 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003296 if (ch == ',' && !paren && brackets <= 0) {
3297 if (++nparam >= sparam) {
3298 sparam += PARAM_DELTA;
3299 params = nasm_realloc(params,
3300 sparam *
3301 sizeof(Token
3302 *));
3303 paramsize =
3304 nasm_realloc(paramsize,
3305 sparam *
3306 sizeof(int));
3307 }
3308 params[nparam] = tline->next;
3309 paramsize[nparam] = 0;
3310 white = 0;
3311 continue; /* parameter loop */
3312 }
3313 if (ch == '{' &&
3314 (brackets > 0 || (brackets == 0 &&
3315 !paramsize[nparam])))
3316 {
3317 if (!(brackets++)) {
3318 params[nparam] = tline->next;
3319 continue; /* parameter loop */
3320 }
3321 }
3322 if (ch == '}' && brackets > 0)
3323 if (--brackets == 0) {
3324 brackets = -1;
3325 continue; /* parameter loop */
3326 }
3327 if (ch == '(' && !brackets)
3328 paren++;
3329 if (ch == ')' && brackets <= 0)
3330 if (--paren < 0)
3331 break;
3332 }
3333 if (brackets < 0) {
3334 brackets = 0;
3335 error(ERR_NONFATAL, "braces do not "
3336 "enclose all of macro parameter");
3337 }
3338 paramsize[nparam] += white + 1;
3339 white = 0;
3340 } /* parameter loop */
3341 nparam++;
3342 while (m && (m->nparam != nparam ||
3343 mstrcmp(m->name, mname,
3344 m->casesense)))
3345 m = m->next;
3346 if (!m)
3347 error(ERR_WARNING | ERR_WARN_MNP,
3348 "macro `%s' exists, "
3349 "but not taking %d parameters",
3350 mstart->text, nparam);
3351 }
3352 }
3353 if (m && m->in_progress)
3354 m = NULL;
3355 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003356 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003357 * Design question: should we handle !tline, which
3358 * indicates missing ')' here, or expand those
3359 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003360 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003361 */
3362 nasm_free(params);
3363 nasm_free(paramsize);
3364 tline = mstart;
3365 } else {
3366 /*
3367 * Expand the macro: we are placed on the last token of the
3368 * call, so that we can easily split the call from the
3369 * following tokens. We also start by pushing an SMAC_END
3370 * token for the cycle removal.
3371 */
3372 t = tline;
3373 if (t) {
3374 tline = t->next;
3375 t->next = NULL;
3376 }
3377 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3378 tt->mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003379 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003380 tline = tt;
3381 for (t = m->expansion; t; t = t->next) {
3382 if (t->type >= TOK_SMAC_PARAM) {
3383 Token *pcopy = tline, **ptail = &pcopy;
3384 Token *ttt, *pt;
3385 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003386
H. Peter Anvine2c80182005-01-15 22:15:51 +00003387 ttt = params[t->type - TOK_SMAC_PARAM];
3388 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3389 --i >= 0;) {
3390 pt = *ptail =
3391 new_Token(tline, ttt->type, ttt->text,
3392 0);
3393 ptail = &pt->next;
3394 ttt = ttt->next;
3395 }
3396 tline = pcopy;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -07003397 } else if (t->type == TOK_PREPROC_Q) {
3398 tt = new_Token(tline, TOK_ID, mname, 0);
3399 tline = tt;
3400 } else if (t->type == TOK_PREPROC_QQ) {
3401 tt = new_Token(tline, TOK_ID, m->name, 0);
3402 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003403 } else {
3404 tt = new_Token(tline, t->type, t->text, 0);
3405 tline = tt;
3406 }
3407 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003408
H. Peter Anvine2c80182005-01-15 22:15:51 +00003409 /*
3410 * Having done that, get rid of the macro call, and clean
3411 * up the parameters.
3412 */
3413 nasm_free(params);
3414 nasm_free(paramsize);
3415 free_tlist(mstart);
3416 continue; /* main token loop */
3417 }
3418 }
3419 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003420
H. Peter Anvine2c80182005-01-15 22:15:51 +00003421 if (tline->type == TOK_SMAC_END) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003422 tline->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003423 tline = delete_Token(tline);
3424 } else {
3425 t = *tail = tline;
3426 tline = tline->next;
3427 t->mac = NULL;
3428 t->next = NULL;
3429 tail = &t->next;
3430 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003431 }
3432
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003433 /*
3434 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003435 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003436 * TOK_IDs should be concatenated.
3437 * Also we look for %+ tokens and concatenate the tokens before and after
3438 * them (without white spaces in between).
3439 */
3440 t = thead;
3441 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003442 while (t) {
3443 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3444 t = t->next;
3445 if (!t || !t->next)
3446 break;
3447 if (t->next->type == TOK_ID ||
3448 t->next->type == TOK_PREPROC_ID ||
3449 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003450 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003451 nasm_free(t->text);
3452 t->next = delete_Token(t->next);
3453 t->text = p;
3454 rescan = 1;
3455 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3456 t->next->next->type == TOK_PREPROC_ID &&
3457 strcmp(t->next->next->text, "%+") == 0) {
3458 /* free the next whitespace, the %+ token and next whitespace */
3459 int i;
3460 for (i = 1; i <= 3; i++) {
3461 if (!t->next
3462 || (i != 2 && t->next->type != TOK_WHITESPACE))
3463 break;
3464 t->next = delete_Token(t->next);
3465 } /* endfor */
3466 } else
3467 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003468 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003469 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003470 if (rescan) {
3471 tline = thead;
3472 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003473 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003474
H. Peter Anvine2c80182005-01-15 22:15:51 +00003475 if (org_tline) {
3476 if (thead) {
3477 *org_tline = *thead;
3478 /* since we just gave text to org_line, don't free it */
3479 thead->text = NULL;
3480 delete_Token(thead);
3481 } else {
3482 /* the expression expanded to empty line;
3483 we can't return NULL for some reasons
3484 we just set the line to a single WHITESPACE token. */
3485 memset(org_tline, 0, sizeof(*org_tline));
3486 org_tline->text = NULL;
3487 org_tline->type = TOK_WHITESPACE;
3488 }
3489 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003490 }
3491
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003492 return thead;
3493}
3494
3495/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003496 * Similar to expand_smacro but used exclusively with macro identifiers
3497 * right before they are fetched in. The reason is that there can be
3498 * identifiers consisting of several subparts. We consider that if there
3499 * are more than one element forming the name, user wants a expansion,
3500 * otherwise it will be left as-is. Example:
3501 *
3502 * %define %$abc cde
3503 *
3504 * the identifier %$abc will be left as-is so that the handler for %define
3505 * will suck it and define the corresponding value. Other case:
3506 *
3507 * %define _%$abc cde
3508 *
3509 * In this case user wants name to be expanded *before* %define starts
3510 * working, so we'll expand %$abc into something (if it has a value;
3511 * otherwise it will be left as-is) then concatenate all successive
3512 * PP_IDs into one.
3513 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003514static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003515{
3516 Token *cur, *oldnext = NULL;
3517
H. Peter Anvin734b1882002-04-30 21:01:08 +00003518 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003519 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003520
3521 cur = tline;
3522 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003523 (cur->next->type == TOK_ID ||
3524 cur->next->type == TOK_PREPROC_ID
3525 || cur->next->type == TOK_NUMBER))
3526 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003527
3528 /* If identifier consists of just one token, don't expand */
3529 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003530 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003531
H. Peter Anvine2c80182005-01-15 22:15:51 +00003532 if (cur) {
3533 oldnext = cur->next; /* Detach the tail past identifier */
3534 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003535 }
3536
H. Peter Anvin734b1882002-04-30 21:01:08 +00003537 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003538
H. Peter Anvine2c80182005-01-15 22:15:51 +00003539 if (cur) {
3540 /* expand_smacro possibly changhed tline; re-scan for EOL */
3541 cur = tline;
3542 while (cur && cur->next)
3543 cur = cur->next;
3544 if (cur)
3545 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003546 }
3547
3548 return tline;
3549}
3550
3551/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003552 * Determine whether the given line constitutes a multi-line macro
3553 * call, and return the MMacro structure called if so. Doesn't have
3554 * to check for an initial label - that's taken care of in
3555 * expand_mmacro - but must check numbers of parameters. Guaranteed
3556 * to be called with tline->type == TOK_ID, so the putative macro
3557 * name is easy to find.
3558 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003559static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003560{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003561 MMacro *head, *m;
3562 Token **params;
3563 int nparam;
3564
H. Peter Anvin166c2472008-05-28 12:28:58 -07003565 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003566
3567 /*
3568 * Efficiency: first we see if any macro exists with the given
3569 * name. If not, we can return NULL immediately. _Then_ we
3570 * count the parameters, and then we look further along the
3571 * list if necessary to find the proper MMacro.
3572 */
3573 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003574 if (!mstrcmp(m->name, tline->text, m->casesense))
3575 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003576 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003577 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003578
3579 /*
3580 * OK, we have a potential macro. Count and demarcate the
3581 * parameters.
3582 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003583 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003584
3585 /*
3586 * So we know how many parameters we've got. Find the MMacro
3587 * structure that handles this number.
3588 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003589 while (m) {
3590 if (m->nparam_min <= nparam
3591 && (m->plus || nparam <= m->nparam_max)) {
3592 /*
3593 * This one is right. Just check if cycle removal
3594 * prohibits us using it before we actually celebrate...
3595 */
3596 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003597#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003598 error(ERR_NONFATAL,
3599 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003600#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003601 nasm_free(params);
3602 return NULL;
3603 }
3604 /*
3605 * It's right, and we can use it. Add its default
3606 * parameters to the end of our list if necessary.
3607 */
3608 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3609 params =
3610 nasm_realloc(params,
3611 ((m->nparam_min + m->ndefs +
3612 1) * sizeof(*params)));
3613 while (nparam < m->nparam_min + m->ndefs) {
3614 params[nparam] = m->defaults[nparam - m->nparam_min];
3615 nparam++;
3616 }
3617 }
3618 /*
3619 * If we've gone over the maximum parameter count (and
3620 * we're in Plus mode), ignore parameters beyond
3621 * nparam_max.
3622 */
3623 if (m->plus && nparam > m->nparam_max)
3624 nparam = m->nparam_max;
3625 /*
3626 * Then terminate the parameter list, and leave.
3627 */
3628 if (!params) { /* need this special case */
3629 params = nasm_malloc(sizeof(*params));
3630 nparam = 0;
3631 }
3632 params[nparam] = NULL;
3633 *params_array = params;
3634 return m;
3635 }
3636 /*
3637 * This one wasn't right: look for the next one with the
3638 * same name.
3639 */
3640 for (m = m->next; m; m = m->next)
3641 if (!mstrcmp(m->name, tline->text, m->casesense))
3642 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003643 }
3644
3645 /*
3646 * After all that, we didn't find one with the right number of
3647 * parameters. Issue a warning, and fail to expand the macro.
3648 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003649 error(ERR_WARNING | ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003650 "macro `%s' exists, but not taking %d parameters",
3651 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003652 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003653 return NULL;
3654}
3655
3656/*
3657 * Expand the multi-line macro call made by the given line, if
3658 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003659 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003660 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003661static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003662{
3663 Token *startline = tline;
3664 Token *label = NULL;
3665 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003666 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003667 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003668 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003669 int i, nparam, *paramlen;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003670
3671 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003672 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07003673 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003674 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003675 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003676 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003677 m = is_mmacro(t, &params);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003678 if (!m) {
3679 Token *last;
3680 /*
3681 * We have an id which isn't a macro call. We'll assume
3682 * it might be a label; we'll also check to see if a
3683 * colon follows it. Then, if there's another id after
3684 * that lot, we'll check it again for macro-hood.
3685 */
3686 label = last = t;
3687 t = t->next;
3688 if (tok_type_(t, TOK_WHITESPACE))
3689 last = t, t = t->next;
3690 if (tok_is_(t, ":")) {
3691 dont_prepend = 1;
3692 last = t, t = t->next;
3693 if (tok_type_(t, TOK_WHITESPACE))
3694 last = t, t = t->next;
3695 }
3696 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3697 return 0;
3698 last->next = NULL;
3699 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003700 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003701
3702 /*
3703 * Fix up the parameters: this involves stripping leading and
3704 * trailing whitespace, then stripping braces if they are
3705 * present.
3706 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003707 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003708 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003709
H. Peter Anvine2c80182005-01-15 22:15:51 +00003710 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003711 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003712 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003713
H. Peter Anvine2c80182005-01-15 22:15:51 +00003714 t = params[i];
3715 skip_white_(t);
3716 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003717 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003718 params[i] = t;
3719 paramlen[i] = 0;
3720 while (t) {
3721 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3722 break; /* ... because we have hit a comma */
3723 if (comma && t->type == TOK_WHITESPACE
3724 && tok_is_(t->next, ","))
3725 break; /* ... or a space then a comma */
3726 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3727 break; /* ... or a brace */
3728 t = t->next;
3729 paramlen[i]++;
3730 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003731 }
3732
3733 /*
3734 * OK, we have a MMacro structure together with a set of
3735 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003736 * copies of each Line on to istk->expansion. Substitution of
3737 * parameter tokens and macro-local tokens doesn't get done
3738 * until the single-line macro substitution process; this is
3739 * because delaying them allows us to change the semantics
3740 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003741 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003742 * First, push an end marker on to istk->expansion, mark this
3743 * macro as in progress, and set up its invocation-specific
3744 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003745 */
3746 ll = nasm_malloc(sizeof(Line));
3747 ll->next = istk->expansion;
3748 ll->finishes = m;
3749 ll->first = NULL;
3750 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003751
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003752 m->in_progress = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003753 m->params = params;
3754 m->iline = tline;
3755 m->nparam = nparam;
3756 m->rotate = 0;
3757 m->paramlen = paramlen;
3758 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003759 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003760
3761 m->next_active = istk->mstk;
3762 istk->mstk = m;
3763
H. Peter Anvine2c80182005-01-15 22:15:51 +00003764 for (l = m->expansion; l; l = l->next) {
3765 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003766
H. Peter Anvine2c80182005-01-15 22:15:51 +00003767 ll = nasm_malloc(sizeof(Line));
3768 ll->finishes = NULL;
3769 ll->next = istk->expansion;
3770 istk->expansion = ll;
3771 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003772
H. Peter Anvine2c80182005-01-15 22:15:51 +00003773 for (t = l->first; t; t = t->next) {
3774 Token *x = t;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003775 switch (t->type) {
3776 case TOK_PREPROC_Q:
3777 tt = *tail = new_Token(NULL, TOK_ID, mtok->text, 0);
3778 break;
3779 case TOK_PREPROC_QQ:
3780 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
3781 break;
3782 case TOK_PREPROC_ID:
3783 if (t->text[1] == '0' && t->text[2] == '0') {
3784 dont_prepend = -1;
3785 x = label;
3786 if (!x)
3787 continue;
3788 }
3789 /* fall through */
3790 default:
3791 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3792 break;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003793 }
H. Peter Anvince2233b2008-05-25 21:57:00 -07003794 tail = &tt->next;
3795 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003796 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003797 }
3798
3799 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00003800 * If we had a label, push it on as the first line of
3801 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003802 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003803 if (label) {
3804 if (dont_prepend < 0)
3805 free_tlist(startline);
3806 else {
3807 ll = nasm_malloc(sizeof(Line));
3808 ll->finishes = NULL;
3809 ll->next = istk->expansion;
3810 istk->expansion = ll;
3811 ll->first = startline;
3812 if (!dont_prepend) {
3813 while (label->next)
3814 label = label->next;
3815 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3816 }
3817 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003818 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003819
H. Peter Anvin734b1882002-04-30 21:01:08 +00003820 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003821
H. Peter Anvineba20a72002-04-30 20:53:55 +00003822 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003823}
3824
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003825/*
3826 * Since preprocessor always operate only on the line that didn't
3827 * arrived yet, we should always use ERR_OFFBY1. Also since user
3828 * won't want to see same error twice (preprocessing is done once
3829 * per pass) we will want to show errors only during pass one.
3830 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003831static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003832{
3833 va_list arg;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003834 char buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003835
3836 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003837 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003838 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003839
H. Peter Anvin734b1882002-04-30 21:01:08 +00003840 va_start(arg, fmt);
Ed Beroset19f927a2004-12-15 17:07:03 +00003841 vsnprintf(buff, sizeof(buff), fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003842 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003843
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003844 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003845 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3846 istk->mstk->lineno, buff);
3847 else
3848 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003849}
3850
H. Peter Anvin734b1882002-04-30 21:01:08 +00003851static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003852pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07003853 ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003854{
H. Peter Anvin99941bf2002-05-14 17:44:03 +00003855 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003856 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003857 istk = nasm_malloc(sizeof(Include));
3858 istk->next = NULL;
3859 istk->conds = NULL;
3860 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003861 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003862 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00003863 istk->fname = NULL;
3864 src_set_fname(nasm_strdup(file));
3865 src_set_linnum(0);
3866 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003867 if (!istk->fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003868 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3869 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003870 defining = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003871 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003872 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003873 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07003874 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003875 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07003876 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003877 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003878 any_extrastdmac = (extrastdmac != NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003879 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003880 evaluate = eval;
3881 pass = apass;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07003882 dephead = deptail = deplist;
3883 if (deplist) {
3884 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
3885 sl->next = NULL;
3886 strcpy(sl->str, file);
3887 *deptail = sl;
3888 deptail = &sl->next;
3889 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003890}
3891
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003892static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003893{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003894 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003895 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003896
H. Peter Anvine2c80182005-01-15 22:15:51 +00003897 while (1) {
3898 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003899 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00003900 * buffer or from the input file.
3901 */
3902 tline = NULL;
3903 while (istk->expansion && istk->expansion->finishes) {
3904 Line *l = istk->expansion;
3905 if (!l->finishes->name && l->finishes->in_progress > 1) {
3906 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003907
H. Peter Anvine2c80182005-01-15 22:15:51 +00003908 /*
3909 * This is a macro-end marker for a macro with no
3910 * name, which means it's not really a macro at all
3911 * but a %rep block, and the `in_progress' field is
3912 * more than 1, meaning that we still need to
3913 * repeat. (1 means the natural last repetition; 0
3914 * means termination by %exitrep.) We have
3915 * therefore expanded up to the %endrep, and must
3916 * push the whole block on to the expansion buffer
3917 * again. We don't bother to remove the macro-end
3918 * marker: we'd only have to generate another one
3919 * if we did.
3920 */
3921 l->finishes->in_progress--;
3922 for (l = l->finishes->expansion; l; l = l->next) {
3923 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003924
H. Peter Anvine2c80182005-01-15 22:15:51 +00003925 ll = nasm_malloc(sizeof(Line));
3926 ll->next = istk->expansion;
3927 ll->finishes = NULL;
3928 ll->first = NULL;
3929 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003930
H. Peter Anvine2c80182005-01-15 22:15:51 +00003931 for (t = l->first; t; t = t->next) {
3932 if (t->text || t->type == TOK_WHITESPACE) {
3933 tt = *tail =
3934 new_Token(NULL, t->type, t->text, 0);
3935 tail = &tt->next;
3936 }
3937 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003938
H. Peter Anvine2c80182005-01-15 22:15:51 +00003939 istk->expansion = ll;
3940 }
3941 } else {
3942 /*
3943 * Check whether a `%rep' was started and not ended
3944 * within this macro expansion. This can happen and
3945 * should be detected. It's a fatal error because
3946 * I'm too confused to work out how to recover
3947 * sensibly from it.
3948 */
3949 if (defining) {
3950 if (defining->name)
3951 error(ERR_PANIC,
3952 "defining with name in expansion");
3953 else if (istk->mstk->name)
3954 error(ERR_FATAL,
3955 "`%%rep' without `%%endrep' within"
3956 " expansion of macro `%s'",
3957 istk->mstk->name);
3958 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00003959
H. Peter Anvine2c80182005-01-15 22:15:51 +00003960 /*
3961 * FIXME: investigate the relationship at this point between
3962 * istk->mstk and l->finishes
3963 */
3964 {
3965 MMacro *m = istk->mstk;
3966 istk->mstk = m->next_active;
3967 if (m->name) {
3968 /*
3969 * This was a real macro call, not a %rep, and
3970 * therefore the parameter information needs to
3971 * be freed.
3972 */
3973 nasm_free(m->params);
3974 free_tlist(m->iline);
3975 nasm_free(m->paramlen);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003976 l->finishes->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003977 } else
3978 free_mmacro(m);
3979 }
3980 istk->expansion = l->next;
3981 nasm_free(l);
3982 list->downlevel(LIST_MACRO);
3983 }
3984 }
3985 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003986
H. Peter Anvine2c80182005-01-15 22:15:51 +00003987 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003988 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003989 Line *l = istk->expansion;
3990 if (istk->mstk)
3991 istk->mstk->lineno++;
3992 tline = l->first;
3993 istk->expansion = l->next;
3994 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003995 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003996 list->line(LIST_MACRO, p);
3997 nasm_free(p);
3998 break;
3999 }
4000 line = read_line();
4001 if (line) { /* from the current input file */
4002 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004003 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004004 nasm_free(line);
4005 break;
4006 }
4007 /*
4008 * The current file has ended; work down the istk
4009 */
4010 {
4011 Include *i = istk;
4012 fclose(i->fp);
4013 if (i->conds)
4014 error(ERR_FATAL,
4015 "expected `%%endif' before end of file");
4016 /* only set line and file name if there's a next node */
4017 if (i->next) {
4018 src_set_linnum(i->lineno);
4019 nasm_free(src_set_fname(i->fname));
4020 }
4021 istk = i->next;
4022 list->downlevel(LIST_INCLUDE);
4023 nasm_free(i);
4024 if (!istk)
4025 return NULL;
4026 }
4027 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004028
H. Peter Anvine2c80182005-01-15 22:15:51 +00004029 /*
4030 * We must expand MMacro parameters and MMacro-local labels
4031 * _before_ we plunge into directive processing, to cope
4032 * with things like `%define something %1' such as STRUC
4033 * uses. Unless we're _defining_ a MMacro, in which case
4034 * those tokens should be left alone to go into the
4035 * definition; and unless we're in a non-emitting
4036 * condition, in which case we don't want to meddle with
4037 * anything.
4038 */
4039 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
4040 tline = expand_mmac_params(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004041
H. Peter Anvine2c80182005-01-15 22:15:51 +00004042 /*
4043 * Check the line to see if it's a preprocessor directive.
4044 */
4045 if (do_directive(tline) == DIRECTIVE_FOUND) {
4046 continue;
4047 } else if (defining) {
4048 /*
4049 * We're defining a multi-line macro. We emit nothing
4050 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004051 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004052 */
4053 Line *l = nasm_malloc(sizeof(Line));
4054 l->next = defining->expansion;
4055 l->first = tline;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004056 l->finishes = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004057 defining->expansion = l;
4058 continue;
4059 } else if (istk->conds && !emitting(istk->conds->state)) {
4060 /*
4061 * We're in a non-emitting branch of a condition block.
4062 * Emit nothing at all, not even a blank line: when we
4063 * emerge from the condition we'll give a line-number
4064 * directive so we keep our place correctly.
4065 */
4066 free_tlist(tline);
4067 continue;
4068 } else if (istk->mstk && !istk->mstk->in_progress) {
4069 /*
4070 * We're in a %rep block which has been terminated, so
4071 * we're walking through to the %endrep without
4072 * emitting anything. Emit nothing at all, not even a
4073 * blank line: when we emerge from the %rep block we'll
4074 * give a line-number directive so we keep our place
4075 * correctly.
4076 */
4077 free_tlist(tline);
4078 continue;
4079 } else {
4080 tline = expand_smacro(tline);
4081 if (!expand_mmacro(tline)) {
4082 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004083 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004084 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004085 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004086 free_tlist(tline);
4087 break;
4088 } else {
4089 continue; /* expand_mmacro calls free_tlist */
4090 }
4091 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004092 }
4093
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004094 return line;
4095}
4096
H. Peter Anvine2c80182005-01-15 22:15:51 +00004097static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004098{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004099 if (defining) {
4100 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
4101 defining->name);
4102 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004103 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004104 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004105 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004106 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004107 while (istk) {
4108 Include *i = istk;
4109 istk = istk->next;
4110 fclose(i->fp);
4111 nasm_free(i->fname);
4112 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004113 }
4114 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004115 ctx_pop();
4116 if (pass == 0) {
4117 free_llist(predef);
4118 delete_Blocks();
4119 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004120}
4121
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004122void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004123{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004124 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004125
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004126 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004127 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004128 i->next = NULL;
4129
H. Peter Anvine2c80182005-01-15 22:15:51 +00004130 if (ipath != NULL) {
4131 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004132 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004133 j = j->next;
4134 j->next = i;
4135 } else {
4136 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004137 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004138}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004139
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004140void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004141{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004142 Token *inc, *space, *name;
4143 Line *l;
4144
H. Peter Anvin734b1882002-04-30 21:01:08 +00004145 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4146 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4147 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004148
4149 l = nasm_malloc(sizeof(Line));
4150 l->next = predef;
4151 l->first = inc;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004152 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004153 predef = l;
4154}
4155
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004156void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004157{
4158 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004159 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004160 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004161
4162 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004163 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4164 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004165 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004166 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004167 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004168 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004169 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004170
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004171 l = nasm_malloc(sizeof(Line));
4172 l->next = predef;
4173 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004174 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004175 predef = l;
4176}
4177
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004178void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004179{
4180 Token *def, *space;
4181 Line *l;
4182
H. Peter Anvin734b1882002-04-30 21:01:08 +00004183 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4184 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004185 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004186
4187 l = nasm_malloc(sizeof(Line));
4188 l->next = predef;
4189 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004190 l->finishes = false;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004191 predef = l;
4192}
4193
Keith Kaniosb7a89542007-04-12 02:40:54 +00004194/*
4195 * Added by Keith Kanios:
4196 *
4197 * This function is used to assist with "runtime" preprocessor
4198 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4199 *
4200 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4201 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4202 */
4203
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004204void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004205{
4206 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004207
Keith Kaniosb7a89542007-04-12 02:40:54 +00004208 def = tokenize(definition);
4209 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4210 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004211
Keith Kaniosb7a89542007-04-12 02:40:54 +00004212}
4213
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004214void pp_extra_stdmac(const char **macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004215{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004216 extrastdmac = macros;
4217}
4218
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004219static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004220{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004221 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004222 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004223 tok->text = nasm_strdup(numbuf);
4224 tok->type = TOK_NUMBER;
4225}
4226
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004227Preproc nasmpp = {
4228 pp_reset,
4229 pp_getline,
4230 pp_cleanup
4231};