blob: c244e8c55834d6a38332285bb080b4c39b954d35 [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;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700131 struct hash_table *localmac;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000132 char *name;
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 Anvinc2df2822007-10-24 15:29:28 -0700158 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER, TOK_SMAC_PARAM,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000159 TOK_INTERNAL_STRING
160};
161
H. Peter Anvine2c80182005-01-15 22:15:51 +0000162struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000163 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000164 char *text;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000165 SMacro *mac; /* associated macro for TOK_SMAC_END */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000166 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000167};
168
169/*
170 * Multi-line macro definitions are stored as a linked list of
171 * these, which is essentially a container to allow several linked
172 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700173 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000174 * Note that in this module, linked lists are treated as stacks
175 * wherever possible. For this reason, Lines are _pushed_ on to the
176 * `expansion' field in MMacro structures, so that the linked list,
177 * if walked, would give the macro lines in reverse order; this
178 * means that we can walk the list when expanding a macro, and thus
179 * push the lines on to the `expansion' field in _istk_ in reverse
180 * order (so that when popped back off they are in the right
181 * order). It may seem cockeyed, and it relies on my design having
182 * an even number of steps in, but it works...
183 *
184 * Some of these structures, rather than being actual lines, are
185 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000186 * This is for use in the cycle-tracking and %rep-handling code.
187 * Such structures have `finishes' non-NULL, and `first' NULL. All
188 * others have `finishes' NULL, but `first' may still be NULL if
189 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000190 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000191struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000192 Line *next;
193 MMacro *finishes;
194 Token *first;
195};
196
197/*
198 * To handle an arbitrary level of file inclusion, we maintain a
199 * stack (ie linked list) of these things.
200 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000201struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000202 Include *next;
203 FILE *fp;
204 Cond *conds;
205 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000206 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000207 int lineno, lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000208 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000209};
210
211/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000212 * Include search path. This is simply a list of strings which get
213 * prepended, in turn, to the name of an include file, in an
214 * attempt to find the file if it's not in the current directory.
215 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000216struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000217 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000218 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000219};
220
221/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000222 * Conditional assembly: we maintain a separate stack of these for
223 * each level of file inclusion. (The only reason we keep the
224 * stacks separate is to ensure that a stray `%endif' in a file
225 * included from within the true branch of a `%if' won't terminate
226 * it and cause confusion: instead, rightly, it'll cause an error.)
227 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000228struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000229 Cond *next;
230 int state;
231};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000232enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000233 /*
234 * These states are for use just after %if or %elif: IF_TRUE
235 * means the condition has evaluated to truth so we are
236 * currently emitting, whereas IF_FALSE means we are not
237 * currently emitting but will start doing so if a %else comes
238 * up. In these states, all directives are admissible: %elif,
239 * %else and %endif. (And of course %if.)
240 */
241 COND_IF_TRUE, COND_IF_FALSE,
242 /*
243 * These states come up after a %else: ELSE_TRUE means we're
244 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
245 * any %elif or %else will cause an error.
246 */
247 COND_ELSE_TRUE, COND_ELSE_FALSE,
248 /*
249 * This state means that we're not emitting now, and also that
250 * nothing until %endif will be emitted at all. It's for use in
251 * two circumstances: (i) when we've had our moment of emission
252 * and have now started seeing %elifs, and (ii) when the
253 * condition construct in question is contained within a
254 * non-emitting branch of a larger condition construct.
255 */
256 COND_NEVER
257};
258#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
259
H. Peter Anvin70653092007-10-19 14:42:29 -0700260/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000261 * These defines are used as the possible return values for do_directive
262 */
263#define NO_DIRECTIVE_FOUND 0
264#define DIRECTIVE_FOUND 1
265
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000266/*
267 * Condition codes. Note that we use c_ prefix not C_ because C_ is
268 * used in nasm.h for the "real" condition codes. At _this_ level,
269 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
270 * ones, so we need a different enum...
271 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700272static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000273 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
274 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000275 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000276};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700277enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000278 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
279 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 -0700280 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
281 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000282};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700283static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000284 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
285 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 +0000286 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000287};
288
H. Peter Anvin76690a12002-04-30 20:52:49 +0000289/*
290 * Directive names.
291 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000292/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000293static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000294{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000295 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000296}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000297
298/* For TASM compatibility we need to be able to recognise TASM compatible
299 * conditional compilation directives. Using the NASM pre-processor does
300 * not work, so we look for them specifically from the following list and
301 * then jam in the equivalent NASM directive into the input stream.
302 */
303
H. Peter Anvine2c80182005-01-15 22:15:51 +0000304enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000305 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
306 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
307};
308
H. Peter Anvin476d2862007-10-02 22:04:15 -0700309static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000310 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
311 "ifndef", "include", "local"
312};
313
314static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000315static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000316static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800317static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000318
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000319static Context *cstk;
320static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000321static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000322
H. Peter Anvine2c80182005-01-15 22:15:51 +0000323static efunc _error; /* Pointer to client-provided error reporting function */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000324static evalfunc evaluate;
325
H. Peter Anvine2c80182005-01-15 22:15:51 +0000326static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000327
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700328static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000329
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000330static Line *predef = NULL;
331
332static ListGen *list;
333
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000334/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000335 * The current set of multi-line macros we have defined.
336 */
H. Peter Anvin97a23472007-09-16 17:57:25 -0700337static struct hash_table *mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000338
339/*
340 * The current set of single-line macros we have defined.
341 */
H. Peter Anvin97a23472007-09-16 17:57:25 -0700342static struct hash_table *smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000343
344/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000345 * The multi-line macro we are currently defining, or the %rep
346 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000347 */
348static MMacro *defining;
349
350/*
351 * The number of macro parameters to allocate space for at a time.
352 */
353#define PARAM_DELTA 16
354
355/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700356 * The standard macro set: defined in macros.c in the array nasm_stdmac.
357 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000358 */
H. Peter Anvin188ce762008-02-16 13:58:45 -0800359static const char * const *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000360
361/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000362 * The extra standard macros that come from the object format, if
363 * any.
364 */
H. Peter Anvin188ce762008-02-16 13:58:45 -0800365static const char * const *extrastdmac = NULL;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700366bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000367
368/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000369 * Tokens are allocated in blocks to improve speed
370 */
371#define TOKEN_BLOCKSIZE 4096
372static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000373struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000374 Blocks *next;
375 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000376};
377
378static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000379
380/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000381 * Forward declarations.
382 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000383static Token *expand_mmac_params(Token * tline);
384static Token *expand_smacro(Token * tline);
385static Token *expand_id(Token * tline);
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700386static Context *get_ctx(char *name, bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700387static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000388static void error(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000389static void *new_Block(size_t size);
390static void delete_Blocks(void);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000391static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000392static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000393
394/*
395 * Macros for safe checking of token pointers, avoid *(NULL)
396 */
397#define tok_type_(x,t) ((x) && (x)->type == (t))
398#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
399#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
400#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000401
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000402/* Handle TASM specific directives, which do not contain a % in
403 * front of them. We do it here because I could not find any other
404 * place to do it for the moment, and it is a hack (ideally it would
405 * be nice to be able to use the NASM pre-processor to do it).
406 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000407static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000408{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000409 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000410 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000411
412 /* Skip whitespace */
413 while (isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000414 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000415
416 /* Binary search for the directive name */
417 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000418 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000419 len = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000420 while (!isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000421 len++;
422 if (len) {
423 oldchar = p[len];
424 p[len] = 0;
425 while (j - i > 1) {
426 k = (j + i) / 2;
427 m = nasm_stricmp(p, tasm_directives[k]);
428 if (m == 0) {
429 /* We have found a directive, so jam a % in front of it
430 * so that NASM will then recognise it as one if it's own.
431 */
432 p[len] = oldchar;
433 len = strlen(p);
434 oldline = line;
435 line = nasm_malloc(len + 2);
436 line[0] = '%';
437 if (k == TM_IFDIFI) {
438 /* NASM does not recognise IFDIFI, so we convert it to
439 * %ifdef BOGUS. This is not used in NASM comaptible
440 * code, but does need to parse for the TASM macro
441 * package.
442 */
443 strcpy(line + 1, "ifdef BOGUS");
444 } else {
445 memcpy(line + 1, p, len + 1);
446 }
447 nasm_free(oldline);
448 return line;
449 } else if (m < 0) {
450 j = k;
451 } else
452 i = k;
453 }
454 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000455 }
456 return line;
457}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000458
H. Peter Anvin76690a12002-04-30 20:52:49 +0000459/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000460 * The pre-preprocessing stage... This function translates line
461 * number indications as they emerge from GNU cpp (`# lineno "file"
462 * flags') into NASM preprocessor line number indications (`%line
463 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000464 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000465static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000466{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000467 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000468 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000469
H. Peter Anvine2c80182005-01-15 22:15:51 +0000470 if (line[0] == '#' && line[1] == ' ') {
471 oldline = line;
472 fname = oldline + 2;
473 lineno = atoi(fname);
474 fname += strspn(fname, "0123456789 ");
475 if (*fname == '"')
476 fname++;
477 fnlen = strcspn(fname, "\"");
478 line = nasm_malloc(20 + fnlen);
479 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
480 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000481 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000482 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000483 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000484 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000485}
486
487/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000488 * Free a linked list of tokens.
489 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000490static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000491{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000492 while (list) {
493 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000494 }
495}
496
497/*
498 * Free a linked list of lines.
499 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000501{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000502 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503 while (list) {
504 l = list;
505 list = list->next;
506 free_tlist(l->first);
507 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000508 }
509}
510
511/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000512 * Free an MMacro
513 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000514static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000515{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000516 nasm_free(m->name);
517 free_tlist(m->dlist);
518 nasm_free(m->defaults);
519 free_llist(m->expansion);
520 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000521}
522
523/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700524 * Free all currently defined macros, and free the hash tables
525 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700526static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700527{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700528 SMacro *s;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700529 const char *key;
530 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700531
H. Peter Anvin072771e2008-05-22 13:17:51 -0700532 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700533 nasm_free((void *)key);
534 while (s) {
535 SMacro *ns = s->next;
536 nasm_free(s->name);
537 free_tlist(s->expansion);
538 nasm_free(s);
539 s = ns;
540 }
541 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700542 hash_free(smt);
543}
544
545static void free_mmacro_table(struct hash_table *mmt)
546{
547 MMacro *m;
548 const char *key;
549 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700550
551 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700552 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700553 nasm_free((void *)key);
554 while (m) {
555 MMacro *nm = m->next;
556 free_mmacro(m);
557 m = nm;
558 }
559 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700560 hash_free(mmt);
561}
562
563static void free_macros(void)
564{
565 free_smacro_table(smacros);
566 free_mmacro_table(mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700567}
568
569/*
570 * Initialize the hash tables
571 */
572static void init_macros(void)
573{
H. Peter Anvin072771e2008-05-22 13:17:51 -0700574 smacros = hash_init(HASH_LARGE);
575 mmacros = hash_init(HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700576}
577
578/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000579 * Pop the context stack.
580 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000581static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000582{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000583 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000584
585 cstk = cstk->next;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700586 free_smacro_table(c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000587 nasm_free(c->name);
588 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000589}
590
H. Peter Anvin072771e2008-05-22 13:17:51 -0700591/*
592 * Search for a key in the hash index; adding it if necessary
593 * (in which case we initialize the data pointer to NULL.)
594 */
595static void **
596hash_findi_add(struct hash_table *hash, const char *str)
597{
598 struct hash_insert hi;
599 void **r;
600 char *strx;
601
602 r = hash_findi(hash, str, &hi);
603 if (r)
604 return r;
605
606 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
607 return hash_add(&hi, strx, NULL);
608}
609
610/*
611 * Like hash_findi, but returns the data element rather than a pointer
612 * to it. Used only when not adding a new element, hence no third
613 * argument.
614 */
615static void *
616hash_findix(struct hash_table *hash, const char *str)
617{
618 void **p;
619
620 p = hash_findi(hash, str, NULL);
621 return p ? *p : NULL;
622}
623
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000624#define BUF_DELTA 512
625/*
626 * Read a line from the top file in istk, handling multiple CR/LFs
627 * at the end of the line read, and handling spurious ^Zs. Will
628 * return lines from the standard macro set if this has not already
629 * been done.
630 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000631static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000632{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000633 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000634 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000635
H. Peter Anvine2c80182005-01-15 22:15:51 +0000636 if (stdmacpos) {
637 if (*stdmacpos) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000638 char *ret = nasm_strdup(*stdmacpos++);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000639 if (!*stdmacpos && any_extrastdmac) {
640 stdmacpos = extrastdmac;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700641 any_extrastdmac = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000642 return ret;
643 }
644 /*
645 * Nasty hack: here we push the contents of `predef' on
646 * to the top-level expansion stack, since this is the
647 * most convenient way to implement the pre-include and
648 * pre-define features.
649 */
650 if (!*stdmacpos) {
651 Line *pd, *l;
652 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000653
H. Peter Anvine2c80182005-01-15 22:15:51 +0000654 for (pd = predef; pd; pd = pd->next) {
655 head = NULL;
656 tail = &head;
657 for (t = pd->first; t; t = t->next) {
658 *tail = new_Token(NULL, t->type, t->text, 0);
659 tail = &(*tail)->next;
660 }
661 l = nasm_malloc(sizeof(Line));
662 l->next = istk->expansion;
663 l->first = head;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700664 l->finishes = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000665 istk->expansion = l;
666 }
667 }
668 return ret;
669 } else {
670 stdmacpos = NULL;
671 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000672 }
673
674 bufsize = BUF_DELTA;
675 buffer = nasm_malloc(BUF_DELTA);
676 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000677 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000678 while (1) {
679 q = fgets(p, bufsize - (p - buffer), istk->fp);
680 if (!q)
681 break;
682 p += strlen(p);
683 if (p > buffer && p[-1] == '\n') {
684 /* Convert backslash-CRLF line continuation sequences into
685 nothing at all (for DOS and Windows) */
686 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
687 p -= 3;
688 *p = 0;
689 continued_count++;
690 }
691 /* Also convert backslash-LF line continuation sequences into
692 nothing at all (for Unix) */
693 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
694 p -= 2;
695 *p = 0;
696 continued_count++;
697 } else {
698 break;
699 }
700 }
701 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000702 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000703 bufsize += BUF_DELTA;
704 buffer = nasm_realloc(buffer, bufsize);
705 p = buffer + offset; /* prevent stale-pointer problems */
706 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000707 }
708
H. Peter Anvine2c80182005-01-15 22:15:51 +0000709 if (!q && p == buffer) {
710 nasm_free(buffer);
711 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000712 }
713
H. Peter Anvine2c80182005-01-15 22:15:51 +0000714 src_set_linnum(src_get_linnum() + istk->lineinc +
715 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000716
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000717 /*
718 * Play safe: remove CRs as well as LFs, if any of either are
719 * present at the end of the line.
720 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000721 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000722 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000723
724 /*
725 * Handle spurious ^Z, which may be inserted into source files
726 * by some file transfer utilities.
727 */
728 buffer[strcspn(buffer, "\032")] = '\0';
729
H. Peter Anvin734b1882002-04-30 21:01:08 +0000730 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000731
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000732 return buffer;
733}
734
735/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000736 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000737 * don't need to parse the value out of e.g. numeric tokens: we
738 * simply split one string into many.
739 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000740static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000741{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000742 char *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000743 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000744 Token *list = NULL;
745 Token *t, **tail = &list;
746
H. Peter Anvine2c80182005-01-15 22:15:51 +0000747 while (*line) {
748 p = line;
749 if (*p == '%') {
750 p++;
751 if (isdigit(*p) ||
752 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
753 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
754 do {
755 p++;
756 }
757 while (isdigit(*p));
758 type = TOK_PREPROC_ID;
759 } else if (*p == '{') {
760 p++;
761 while (*p && *p != '}') {
762 p[-1] = *p;
763 p++;
764 }
765 p[-1] = '\0';
766 if (*p)
767 p++;
768 type = TOK_PREPROC_ID;
769 } else if (isidchar(*p) ||
770 ((*p == '!' || *p == '%' || *p == '$') &&
771 isidchar(p[1]))) {
772 do {
773 p++;
774 }
775 while (isidchar(*p));
776 type = TOK_PREPROC_ID;
777 } else {
778 type = TOK_OTHER;
779 if (*p == '%')
780 p++;
781 }
782 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
783 type = TOK_ID;
784 p++;
785 while (*p && isidchar(*p))
786 p++;
787 } else if (*p == '\'' || *p == '"') {
788 /*
789 * A string token.
790 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000791 char c = *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000792 p++;
793 type = TOK_STRING;
794 while (*p && *p != c)
795 p++;
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000796
H. Peter Anvine2c80182005-01-15 22:15:51 +0000797 if (*p) {
798 p++;
799 } else {
800 error(ERR_WARNING, "unterminated string");
801 /* Handling unterminated strings by UNV */
802 /* type = -1; */
803 }
804 } else if (isnumstart(*p)) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700805 bool is_hex = false;
806 bool is_float = false;
807 bool has_e = false;
808 char c, *r;
809
H. Peter Anvine2c80182005-01-15 22:15:51 +0000810 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700811 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000812 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700813
814 if (*p == '$') {
815 p++;
816 is_hex = true;
817 }
818
819 for (;;) {
820 c = *p++;
821
822 if (!is_hex && (c == 'e' || c == 'E')) {
823 has_e = true;
824 if (*p == '+' || *p == '-') {
825 /* e can only be followed by +/- if it is either a
826 prefixed hex number or a floating-point number */
827 p++;
828 is_float = true;
829 }
830 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
831 is_hex = true;
832 } else if (c == 'P' || c == 'p') {
833 is_float = true;
834 if (*p == '+' || *p == '-')
835 p++;
836 } else if (isnumchar(c) || c == '_')
837 ; /* just advance */
838 else if (c == '.') {
839 /* we need to deal with consequences of the legacy
840 parser, like "1.nolist" being two tokens
841 (TOK_NUMBER, TOK_ID) here; at least give it
842 a shot for now. In the future, we probably need
843 a flex-based scanner with proper pattern matching
844 to do it as well as it can be done. Nothing in
845 the world is going to help the person who wants
846 0x123.p16 interpreted as two tokens, though. */
847 r = p;
848 while (*r == '_')
849 r++;
850
851 if (isdigit(*r) || (is_hex && isxdigit(*r)) ||
852 (!is_hex && (*r == 'e' || *r == 'E')) ||
853 (*r == 'p' || *r == 'P')) {
854 p = r;
855 is_float = true;
856 } else
857 break; /* Terminate the token */
858 } else
859 break;
860 }
861 p--; /* Point to first character beyond number */
862
863 if (has_e && !is_hex) {
864 /* 1e13 is floating-point, but 1e13h is not */
865 is_float = true;
866 }
867
868 type = is_float ? TOK_FLOAT : TOK_NUMBER;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000869 } else if (isspace(*p)) {
870 type = TOK_WHITESPACE;
871 p++;
872 while (*p && isspace(*p))
873 p++;
874 /*
875 * Whitespace just before end-of-line is discarded by
876 * pretending it's a comment; whitespace just before a
877 * comment gets lumped into the comment.
878 */
879 if (!*p || *p == ';') {
880 type = TOK_COMMENT;
881 while (*p)
882 p++;
883 }
884 } else if (*p == ';') {
885 type = TOK_COMMENT;
886 while (*p)
887 p++;
888 } else {
889 /*
890 * Anything else is an operator of some kind. We check
891 * for all the double-character operators (>>, <<, //,
892 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000893 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000894 */
895 type = TOK_OTHER;
896 if ((p[0] == '>' && p[1] == '>') ||
897 (p[0] == '<' && p[1] == '<') ||
898 (p[0] == '/' && p[1] == '/') ||
899 (p[0] == '<' && p[1] == '=') ||
900 (p[0] == '>' && p[1] == '=') ||
901 (p[0] == '=' && p[1] == '=') ||
902 (p[0] == '!' && p[1] == '=') ||
903 (p[0] == '<' && p[1] == '>') ||
904 (p[0] == '&' && p[1] == '&') ||
905 (p[0] == '|' && p[1] == '|') ||
906 (p[0] == '^' && p[1] == '^')) {
907 p++;
908 }
909 p++;
910 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000911
H. Peter Anvine2c80182005-01-15 22:15:51 +0000912 /* Handling unterminated string by UNV */
913 /*if (type == -1)
914 {
915 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
916 t->text[p-line] = *line;
917 tail = &t->next;
918 }
919 else */
920 if (type != TOK_COMMENT) {
921 *tail = t = new_Token(NULL, type, line, p - line);
922 tail = &t->next;
923 }
924 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000925 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000926 return list;
927}
928
H. Peter Anvince616072002-04-30 21:02:23 +0000929/*
930 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -0700931 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +0000932 * deleted only all at once by the delete_Blocks function.
933 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +0000935{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000936 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000937
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000938 /* first, get to the end of the linked list */
939 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000941 /* now allocate the requested chunk */
942 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000943
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000944 /* now allocate a new block for the next request */
945 b->next = nasm_malloc(sizeof(Blocks));
946 /* and initialize the contents of the new block */
947 b->next->next = NULL;
948 b->next->chunk = NULL;
949 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000950}
951
952/*
953 * this function deletes all managed blocks of memory
954 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000955static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +0000956{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000957 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +0000958
H. Peter Anvin70653092007-10-19 14:42:29 -0700959 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000960 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -0700961 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000962 * free it.
963 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000964 while (b) {
965 if (b->chunk)
966 nasm_free(b->chunk);
967 a = b;
968 b = b->next;
969 if (a != &blocks)
970 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000971 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000972}
H. Peter Anvin734b1882002-04-30 21:01:08 +0000973
974/*
H. Peter Anvin70653092007-10-19 14:42:29 -0700975 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +0000976 * back to the caller. It sets the type and text elements, and
977 * also the mac and next elements to NULL.
978 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000979static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +0000980{
981 Token *t;
982 int i;
983
H. Peter Anvine2c80182005-01-15 22:15:51 +0000984 if (freeTokens == NULL) {
985 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
986 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
987 freeTokens[i].next = &freeTokens[i + 1];
988 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000989 }
990 t = freeTokens;
991 freeTokens = t->next;
992 t->next = next;
993 t->mac = NULL;
994 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995 if (type == TOK_WHITESPACE || text == NULL) {
996 t->text = NULL;
997 } else {
998 if (txtlen == 0)
999 txtlen = strlen(text);
1000 t->text = nasm_malloc(1 + txtlen);
1001 strncpy(t->text, text, txtlen);
1002 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001003 }
1004 return t;
1005}
1006
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001008{
1009 Token *next = t->next;
1010 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001011 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001012 freeTokens = t;
1013 return next;
1014}
1015
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001016/*
1017 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001018 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1019 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001020 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001021static char *detoken(Token * tlist, int expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001022{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001023 Token *t;
1024 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001025 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001026 const char *q;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001027
1028 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 for (t = tlist; t; t = t->next) {
1030 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001031 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001032 nasm_free(t->text);
1033 if (p)
1034 t->text = nasm_strdup(p);
1035 else
1036 t->text = NULL;
1037 }
1038 /* Expand local macros here and not during preprocessing */
1039 if (expand_locals &&
1040 t->type == TOK_PREPROC_ID && t->text &&
1041 t->text[0] == '%' && t->text[1] == '$') {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001042 Context *ctx = get_ctx(t->text, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001043 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001044 char buffer[40];
1045 char *p, *q = t->text + 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001046
H. Peter Anvine2c80182005-01-15 22:15:51 +00001047 q += strspn(q, "$");
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001048 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001049 p = nasm_strcat(buffer, q);
1050 nasm_free(t->text);
1051 t->text = p;
1052 }
1053 }
1054 if (t->type == TOK_WHITESPACE) {
1055 len++;
1056 } else if (t->text) {
1057 len += strlen(t->text);
1058 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001059 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001060 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001061 for (t = tlist; t; t = t->next) {
1062 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001063 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 } else if (t->text) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001065 q = t->text;
1066 while (*q)
1067 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001068 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001069 }
1070 *p = '\0';
1071 return line;
1072}
1073
1074/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001075 * A scanner, suitable for use by the expression evaluator, which
1076 * operates on a line of Tokens. Expects a pointer to a pointer to
1077 * the first token in the line to be passed in as its private_data
1078 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001079 *
1080 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001081 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001082static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001083{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001084 Token **tlineptr = private_data;
1085 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001086 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001087
H. Peter Anvine2c80182005-01-15 22:15:51 +00001088 do {
1089 tline = *tlineptr;
1090 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001091 }
1092 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001094
1095 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001096 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001097
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001098 tokval->t_charptr = tline->text;
1099
H. Peter Anvin76690a12002-04-30 20:52:49 +00001100 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001102 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001103 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001104
H. Peter Anvine2c80182005-01-15 22:15:51 +00001105 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001106 p = tokval->t_charptr = tline->text;
1107 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001108 tokval->t_charptr++;
1109 return tokval->t_type = TOKEN_ID;
1110 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001111
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001112 for (r = p, s = ourcopy; *r; r++) {
Philipp Thomas76ec8e72008-05-21 08:53:21 -07001113 if (r >= p+MAX_KEYWORD)
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001114 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1115 *s++ = tolower(*r);
1116 }
1117 *s = '\0';
1118 /* right, so we have an identifier sitting in temp storage. now,
1119 * is it actually a register or instruction name, or what? */
1120 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001121 }
1122
H. Peter Anvine2c80182005-01-15 22:15:51 +00001123 if (tline->type == TOK_NUMBER) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001124 bool rn_error;
1125 tokval->t_integer = readnum(tline->text, &rn_error);
1126 if (rn_error)
1127 return tokval->t_type = TOKEN_ERRNUM; /* some malformation occurred */
1128 tokval->t_charptr = tline->text;
1129 return tokval->t_type = TOKEN_NUM;
1130 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001131
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001132 if (tline->type == TOK_FLOAT) {
1133 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001134 }
1135
H. Peter Anvine2c80182005-01-15 22:15:51 +00001136 if (tline->type == TOK_STRING) {
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001137 bool rn_warn;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001138 char q, *r;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 int l;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001140
H. Peter Anvine2c80182005-01-15 22:15:51 +00001141 r = tline->text;
1142 q = *r++;
1143 l = strlen(r);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001144
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145 if (l == 0 || r[l - 1] != q)
1146 return tokval->t_type = TOKEN_ERRNUM;
1147 tokval->t_integer = readstrnum(r, l - 1, &rn_warn);
1148 if (rn_warn)
1149 error(ERR_WARNING | ERR_PASS1, "character constant too long");
1150 tokval->t_charptr = NULL;
1151 return tokval->t_type = TOKEN_NUM;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001152 }
1153
H. Peter Anvine2c80182005-01-15 22:15:51 +00001154 if (tline->type == TOK_OTHER) {
1155 if (!strcmp(tline->text, "<<"))
1156 return tokval->t_type = TOKEN_SHL;
1157 if (!strcmp(tline->text, ">>"))
1158 return tokval->t_type = TOKEN_SHR;
1159 if (!strcmp(tline->text, "//"))
1160 return tokval->t_type = TOKEN_SDIV;
1161 if (!strcmp(tline->text, "%%"))
1162 return tokval->t_type = TOKEN_SMOD;
1163 if (!strcmp(tline->text, "=="))
1164 return tokval->t_type = TOKEN_EQ;
1165 if (!strcmp(tline->text, "<>"))
1166 return tokval->t_type = TOKEN_NE;
1167 if (!strcmp(tline->text, "!="))
1168 return tokval->t_type = TOKEN_NE;
1169 if (!strcmp(tline->text, "<="))
1170 return tokval->t_type = TOKEN_LE;
1171 if (!strcmp(tline->text, ">="))
1172 return tokval->t_type = TOKEN_GE;
1173 if (!strcmp(tline->text, "&&"))
1174 return tokval->t_type = TOKEN_DBL_AND;
1175 if (!strcmp(tline->text, "^^"))
1176 return tokval->t_type = TOKEN_DBL_XOR;
1177 if (!strcmp(tline->text, "||"))
1178 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001179 }
1180
1181 /*
1182 * We have no other options: just return the first character of
1183 * the token text.
1184 */
1185 return tokval->t_type = tline->text[0];
1186}
1187
1188/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001189 * Compare a string to the name of an existing macro; this is a
1190 * simple wrapper which calls either strcmp or nasm_stricmp
1191 * depending on the value of the `casesense' parameter.
1192 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001193static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001194{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001195 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001196}
1197
1198/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001199 * Return the Context structure associated with a %$ token. Return
1200 * NULL, having _already_ reported an error condition, if the
1201 * context stack isn't deep enough for the supplied number of $
1202 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001203 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001204 * also scanned for such smacro, until it is found; if not -
1205 * only the context that directly results from the number of $'s
1206 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001207 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001208static Context *get_ctx(char *name, bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001209{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001210 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001211 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001212 int i;
1213
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001214 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001215 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001216
H. Peter Anvine2c80182005-01-15 22:15:51 +00001217 if (!cstk) {
1218 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1219 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001220 }
1221
H. Peter Anvine2c80182005-01-15 22:15:51 +00001222 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1223 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001224/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001225 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001226 if (!ctx) {
1227 error(ERR_NONFATAL, "`%s': context stack is only"
1228 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1229 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001230 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001231 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001232 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001233
H. Peter Anvine2c80182005-01-15 22:15:51 +00001234 do {
1235 /* Search for this smacro in found context */
H. Peter Anvin072771e2008-05-22 13:17:51 -07001236 m = hash_findix(ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001237 while (m) {
1238 if (!mstrcmp(m->name, name, m->casesense))
1239 return ctx;
1240 m = m->next;
1241 }
1242 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001243 }
1244 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001245 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001246}
1247
1248/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001249 * Open an include file. This routine must always return a valid
1250 * file pointer if it returns - it's responsible for throwing an
1251 * ERR_FATAL and bombing out completely if not. It should also try
1252 * the include path one by one until it finds the file or reaches
1253 * the end of the path.
1254 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001255static FILE *inc_fopen(char *file)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001256{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001257 FILE *fp;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001258 char *prefix = "", *combine;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001259 IncPath *ip = ipath;
H. Peter Anvin620515a2002-04-30 20:57:38 +00001260 static int namelen = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001261 int len = strlen(file);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001262
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 while (1) {
1264 combine = nasm_malloc(strlen(prefix) + len + 1);
1265 strcpy(combine, prefix);
1266 strcat(combine, file);
1267 fp = fopen(combine, "r");
1268 if (pass == 0 && fp) {
1269 namelen += strlen(combine) + 1;
1270 if (namelen > 62) {
1271 printf(" \\\n ");
1272 namelen = 2;
1273 }
1274 printf(" %s", combine);
1275 }
1276 nasm_free(combine);
1277 if (fp)
1278 return fp;
1279 if (!ip)
1280 break;
1281 prefix = ip->path;
1282 ip = ip->next;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001283
1284 if (!prefix) {
1285 /* -MG given and file not found */
1286 if (pass == 0) {
1287 namelen += strlen(file) + 1;
1288 if (namelen > 62) {
1289 printf(" \\\n ");
1290 namelen = 2;
1291 }
1292 printf(" %s", file);
1293 }
1294 return NULL;
1295 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001296 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001297
H. Peter Anvin734b1882002-04-30 21:01:08 +00001298 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001300}
1301
1302/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001303 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001304 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001305 * return true if _any_ single-line macro of that name is defined.
1306 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001307 * `nparam' or no parameters is defined.
1308 *
1309 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001310 * defined, or nparam is -1, the address of the definition structure
1311 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001312 * is NULL, no action will be taken regarding its contents, and no
1313 * error will occur.
1314 *
1315 * Note that this is also called with nparam zero to resolve
1316 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001317 *
1318 * If you already know which context macro belongs to, you can pass
1319 * the context pointer as first parameter; if you won't but name begins
1320 * with %$ the context will be automatically computed. If all_contexts
1321 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001322 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001323static bool
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001324smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001325 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001326{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001327 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001328
H. Peter Anvin97a23472007-09-16 17:57:25 -07001329 if (ctx) {
H. Peter Anvin072771e2008-05-22 13:17:51 -07001330 m = (SMacro *) hash_findix(ctx->localmac, name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001331 } else if (name[0] == '%' && name[1] == '$') {
1332 if (cstk)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001333 ctx = get_ctx(name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001334 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001335 return false; /* got to return _something_ */
H. Peter Anvin072771e2008-05-22 13:17:51 -07001336 m = (SMacro *) hash_findix(ctx->localmac, name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001337 } else {
1338 m = (SMacro *) hash_findix(smacros, name);
1339 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001340
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 while (m) {
1342 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001343 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001344 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001345 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001346 *defn = m;
1347 else
1348 *defn = NULL;
1349 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001350 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001351 }
1352 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001353 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001354
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001355 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001356}
1357
1358/*
1359 * Count and mark off the parameters in a multi-line macro call.
1360 * This is called both from within the multi-line macro expansion
1361 * code, and also to mark off the default parameters when provided
1362 * in a %macro definition line.
1363 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001364static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001365{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001366 int paramsize, brace;
1367
1368 *nparam = paramsize = 0;
1369 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001370 while (t) {
1371 if (*nparam >= paramsize) {
1372 paramsize += PARAM_DELTA;
1373 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1374 }
1375 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001376 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001377 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001378 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001379 (*params)[(*nparam)++] = t;
1380 while (tok_isnt_(t, brace ? "}" : ","))
1381 t = t->next;
1382 if (t) { /* got a comma/brace */
1383 t = t->next;
1384 if (brace) {
1385 /*
1386 * Now we've found the closing brace, look further
1387 * for the comma.
1388 */
1389 skip_white_(t);
1390 if (tok_isnt_(t, ",")) {
1391 error(ERR_NONFATAL,
1392 "braces do not enclose all of macro parameter");
1393 while (tok_isnt_(t, ","))
1394 t = t->next;
1395 }
1396 if (t)
1397 t = t->next; /* eat the comma */
1398 }
1399 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001400 }
1401}
1402
1403/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001404 * Determine whether one of the various `if' conditions is true or
1405 * not.
1406 *
1407 * We must free the tline we get passed.
1408 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001409static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001410{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001411 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001412 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001413 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001414 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001415 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001416 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001417
1418 origline = tline;
1419
H. Peter Anvine2c80182005-01-15 22:15:51 +00001420 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001421 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001422 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001423 while (cstk && tline) {
1424 skip_white_(tline);
1425 if (!tline || tline->type != TOK_ID) {
1426 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001427 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001428 free_tlist(origline);
1429 return -1;
1430 }
1431 if (!nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001432 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433 tline = tline->next;
1434 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001435 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001436
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001437 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001438 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001439 while (tline) {
1440 skip_white_(tline);
1441 if (!tline || (tline->type != TOK_ID &&
1442 (tline->type != TOK_PREPROC_ID ||
1443 tline->text[1] != '$'))) {
1444 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001445 "`%s' expects macro identifiers", pp_directives[ct]);
1446 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001447 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001448 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001449 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001450 tline = tline->next;
1451 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001452 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001453
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001454 case PPC_IFIDN:
1455 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001456 tline = expand_smacro(tline);
1457 t = tt = tline;
1458 while (tok_isnt_(tt, ","))
1459 tt = tt->next;
1460 if (!tt) {
1461 error(ERR_NONFATAL,
1462 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001463 pp_directives[ct]);
1464 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001465 }
1466 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001467 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001468 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1469 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1470 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001471 pp_directives[ct]);
1472 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001473 }
1474 if (t->type == TOK_WHITESPACE) {
1475 t = t->next;
1476 continue;
1477 }
1478 if (tt->type == TOK_WHITESPACE) {
1479 tt = tt->next;
1480 continue;
1481 }
1482 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001483 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001484 break;
1485 }
1486 /* Unify surrounding quotes for strings */
1487 if (t->type == TOK_STRING) {
1488 tt->text[0] = t->text[0];
1489 tt->text[strlen(tt->text) - 1] = t->text[0];
1490 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001491 if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001492 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001493 break;
1494 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001495
H. Peter Anvine2c80182005-01-15 22:15:51 +00001496 t = t->next;
1497 tt = tt->next;
1498 }
1499 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001500 j = false; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001501 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001502
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001503 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001504 {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001505 bool found = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001506 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001507
H. Peter Anvine2c80182005-01-15 22:15:51 +00001508 tline = tline->next;
1509 skip_white_(tline);
1510 tline = expand_id(tline);
1511 if (!tok_type_(tline, TOK_ID)) {
1512 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001513 "`%s' expects a macro name", pp_directives[ct]);
1514 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001515 }
1516 searching.name = nasm_strdup(tline->text);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001517 searching.casesense = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001518 searching.plus = false;
1519 searching.nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001520 searching.in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 searching.rep_nest = NULL;
1522 searching.nparam_min = 0;
1523 searching.nparam_max = INT_MAX;
1524 tline = expand_smacro(tline->next);
1525 skip_white_(tline);
1526 if (!tline) {
1527 } else if (!tok_type_(tline, TOK_NUMBER)) {
1528 error(ERR_NONFATAL,
1529 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001530 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001531 } else {
1532 searching.nparam_min = searching.nparam_max =
1533 readnum(tline->text, &j);
1534 if (j)
1535 error(ERR_NONFATAL,
1536 "unable to parse parameter count `%s'",
1537 tline->text);
1538 }
1539 if (tline && tok_is_(tline->next, "-")) {
1540 tline = tline->next->next;
1541 if (tok_is_(tline, "*"))
1542 searching.nparam_max = INT_MAX;
1543 else if (!tok_type_(tline, TOK_NUMBER))
1544 error(ERR_NONFATAL,
1545 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001546 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001547 else {
1548 searching.nparam_max = readnum(tline->text, &j);
1549 if (j)
1550 error(ERR_NONFATAL,
1551 "unable to parse parameter count `%s'",
1552 tline->text);
1553 if (searching.nparam_min > searching.nparam_max)
1554 error(ERR_NONFATAL,
1555 "minimum parameter count exceeds maximum");
1556 }
1557 }
1558 if (tline && tok_is_(tline->next, "+")) {
1559 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001560 searching.plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001561 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07001562 mmac = (MMacro *) hash_findix(mmacros, searching.name);
1563 while (mmac) {
1564 if (!strcmp(mmac->name, searching.name) &&
1565 (mmac->nparam_min <= searching.nparam_max
1566 || searching.plus)
1567 && (searching.nparam_min <= mmac->nparam_max
1568 || mmac->plus)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001569 found = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001570 break;
1571 }
1572 mmac = mmac->next;
1573 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001574 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001575 j = found;
1576 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001577 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001578
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001579 case PPC_IFID:
1580 needtype = TOK_ID;
1581 goto iftype;
1582 case PPC_IFNUM:
1583 needtype = TOK_NUMBER;
1584 goto iftype;
1585 case PPC_IFSTR:
1586 needtype = TOK_STRING;
1587 goto iftype;
1588
1589 iftype:
H. Peter Anvin134b9462008-02-16 17:01:40 -08001590 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001591
H. Peter Anvin927c92b2008-02-16 13:44:52 -08001592 while (tok_type_(t, TOK_WHITESPACE) ||
1593 (needtype == TOK_NUMBER &&
1594 tok_type_(t, TOK_OTHER) &&
1595 (t->text[0] == '-' || t->text[0] == '+') &&
1596 !t->text[1]))
1597 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001598
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001599 j = tok_type_(t, needtype);
1600 break;
1601
1602 case PPC_IFTOKEN:
1603 t = tline = expand_smacro(tline);
1604 while (tok_type_(t, TOK_WHITESPACE))
1605 t = t->next;
1606
1607 j = false;
1608 if (t) {
1609 t = t->next; /* Skip the actual token */
1610 while (tok_type_(t, TOK_WHITESPACE))
1611 t = t->next;
1612 j = !t; /* Should be nothing left */
1613 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001614 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001615
H. Peter Anvin134b9462008-02-16 17:01:40 -08001616 case PPC_IFEMPTY:
1617 t = tline = expand_smacro(tline);
1618 while (tok_type_(t, TOK_WHITESPACE))
1619 t = t->next;
1620
1621 j = !t; /* Should be empty */
1622 break;
1623
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001624 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001625 t = tline = expand_smacro(tline);
1626 tptr = &t;
1627 tokval.t_type = TOKEN_INVALID;
1628 evalresult = evaluate(ppscan, tptr, &tokval,
1629 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001630 if (!evalresult)
1631 return -1;
1632 if (tokval.t_type)
1633 error(ERR_WARNING,
1634 "trailing garbage after expression ignored");
1635 if (!is_simple(evalresult)) {
1636 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001637 "non-constant value given to `%s'", pp_directives[ct]);
1638 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001640 j = reloc_value(evalresult) != 0;
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001641 return j;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001642
H. Peter Anvine2c80182005-01-15 22:15:51 +00001643 default:
1644 error(ERR_FATAL,
1645 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001646 pp_directives[ct]);
1647 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001648 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001649
1650 free_tlist(origline);
1651 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001652
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001653fail:
1654 free_tlist(origline);
1655 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001656}
1657
1658/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001659 * Expand macros in a string. Used in %error and %include directives.
Keith Kaniosb7a89542007-04-12 02:40:54 +00001660 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001661 * The returned variable should ALWAYS be freed after usage.
1662 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001663void expand_macros_in_string(char **p)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001664{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001665 Token *line = tokenize(*p);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001666 line = expand_smacro(line);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001667 *p = detoken(line, false);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001668}
1669
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001670/*
1671 * Common code for defining an smacro
1672 */
1673static bool define_smacro(Context *ctx, char *mname, bool casesense,
1674 int nparam, Token *expansion)
1675{
1676 SMacro *smac, **smhead;
H. Peter Anvin70653092007-10-19 14:42:29 -07001677
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001678 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1679 if (!smac) {
1680 error(ERR_WARNING,
1681 "single-line macro `%s' defined both with and"
1682 " without parameters", mname);
1683
1684 /* Some instances of the old code considered this a failure,
1685 some others didn't. What is the right thing to do here? */
1686 free_tlist(expansion);
1687 return false; /* Failure */
1688 } else {
1689 /*
1690 * We're redefining, so we have to take over an
1691 * existing SMacro structure. This means freeing
1692 * what was already in it.
1693 */
1694 nasm_free(smac->name);
1695 free_tlist(smac->expansion);
1696 }
1697 } else {
H. Peter Anvin072771e2008-05-22 13:17:51 -07001698 smhead = (SMacro **) hash_findi_add(ctx ? ctx->localmac : smacros,
1699 mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001700 smac = nasm_malloc(sizeof(SMacro));
1701 smac->next = *smhead;
1702 *smhead = smac;
1703 }
1704 smac->name = nasm_strdup(mname);
1705 smac->casesense = casesense;
1706 smac->nparam = nparam;
1707 smac->expansion = expansion;
1708 smac->in_progress = false;
1709 return true; /* Success */
1710}
1711
1712/*
1713 * Undefine an smacro
1714 */
1715static void undef_smacro(Context *ctx, const char *mname)
1716{
1717 SMacro **smhead, *s, **sp;
1718
H. Peter Anvin072771e2008-05-22 13:17:51 -07001719 smhead = (SMacro **)hash_findi(ctx ? ctx->localmac : smacros, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001720
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001721 if (smhead) {
1722 /*
1723 * We now have a macro name... go hunt for it.
1724 */
1725 sp = smhead;
1726 while ((s = *sp) != NULL) {
1727 if (!mstrcmp(s->name, mname, s->casesense)) {
1728 *sp = s->next;
1729 nasm_free(s->name);
1730 free_tlist(s->expansion);
1731 nasm_free(s);
1732 } else {
1733 sp = &s->next;
1734 }
1735 }
1736 }
1737}
1738
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001739/*
1740 * Decode a size directive
1741 */
1742static int parse_size(const char *str) {
1743 static const char *size_names[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001744 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001745 static const int sizes[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001746 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001747
1748 return sizes[bsii(str, size_names, elements(size_names))+1];
1749}
1750
Ed Beroset3ab3f412002-06-11 03:31:49 +00001751/**
1752 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001753 * Find out if a line contains a preprocessor directive, and deal
1754 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07001755 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001756 * If a directive _is_ found, it is the responsibility of this routine
1757 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001758 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001759 * @param tline a pointer to the current tokeninzed line linked list
1760 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07001761 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001762 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001763static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001764{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001765 enum preproc_token i;
1766 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07001767 bool err;
1768 int nparam;
1769 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001770 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001771 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001772 int offset;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001773 char *p, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001774 Include *inc;
1775 Context *ctx;
1776 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001777 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001778 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1779 Line *l;
1780 struct tokenval tokval;
1781 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001783 int64_t count;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001784
1785 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001786
H. Peter Anvineba20a72002-04-30 20:53:55 +00001787 skip_white_(tline);
1788 if (!tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001789 (tline->text[1] == '%' || tline->text[1] == '$'
1790 || tline->text[1] == '!'))
1791 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001792
H. Peter Anvin4169a472007-09-12 01:29:43 +00001793 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001794
1795 /*
1796 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001797 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001798 * we should ignore all directives except for condition
1799 * directives.
1800 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001801 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1803 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001804 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001805
1806 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001807 * If we're defining a macro or reading a %rep block, we should
1808 * ignore all directives except for %macro/%imacro (which
1809 * generate an error), %endm/%endmacro, and (only if we're in a
H. Peter Anvineba20a72002-04-30 20:53:55 +00001810 * %rep block) %endrep. If we're in a %rep block, another %rep
1811 * causes an error, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001812 */
1813 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001814 i != PP_ENDMACRO && i != PP_ENDM &&
1815 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1816 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001817 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001818
H. Peter Anvin4169a472007-09-12 01:29:43 +00001819 switch (i) {
1820 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001821 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1822 tline->text);
1823 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001824
H. Peter Anvine2c80182005-01-15 22:15:51 +00001825 case PP_STACKSIZE:
1826 /* Directive to tell NASM what the default stack size is. The
1827 * default is for a 16-bit stack, and this can be overriden with
1828 * %stacksize large.
1829 * the following form:
1830 *
1831 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1832 */
1833 tline = tline->next;
1834 if (tline && tline->type == TOK_WHITESPACE)
1835 tline = tline->next;
1836 if (!tline || tline->type != TOK_ID) {
1837 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1838 free_tlist(origline);
1839 return DIRECTIVE_FOUND;
1840 }
1841 if (nasm_stricmp(tline->text, "flat") == 0) {
1842 /* All subsequent ARG directives are for a 32-bit stack */
1843 StackSize = 4;
1844 StackPointer = "ebp";
1845 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001846 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08001847 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
1848 /* All subsequent ARG directives are for a 64-bit stack */
1849 StackSize = 8;
1850 StackPointer = "rbp";
1851 ArgOffset = 8;
1852 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001853 } else if (nasm_stricmp(tline->text, "large") == 0) {
1854 /* All subsequent ARG directives are for a 16-bit stack,
1855 * far function call.
1856 */
1857 StackSize = 2;
1858 StackPointer = "bp";
1859 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001860 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001861 } else if (nasm_stricmp(tline->text, "small") == 0) {
1862 /* All subsequent ARG directives are for a 16-bit stack,
1863 * far function call. We don't support near functions.
1864 */
1865 StackSize = 2;
1866 StackPointer = "bp";
1867 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001868 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001869 } else {
1870 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1871 free_tlist(origline);
1872 return DIRECTIVE_FOUND;
1873 }
1874 free_tlist(origline);
1875 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001876
H. Peter Anvine2c80182005-01-15 22:15:51 +00001877 case PP_ARG:
1878 /* TASM like ARG directive to define arguments to functions, in
1879 * the following form:
1880 *
1881 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1882 */
1883 offset = ArgOffset;
1884 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001885 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001886 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001887
H. Peter Anvine2c80182005-01-15 22:15:51 +00001888 /* Find the argument name */
1889 tline = tline->next;
1890 if (tline && tline->type == TOK_WHITESPACE)
1891 tline = tline->next;
1892 if (!tline || tline->type != TOK_ID) {
1893 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1894 free_tlist(origline);
1895 return DIRECTIVE_FOUND;
1896 }
1897 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001898
H. Peter Anvine2c80182005-01-15 22:15:51 +00001899 /* Find the argument size type */
1900 tline = tline->next;
1901 if (!tline || tline->type != TOK_OTHER
1902 || tline->text[0] != ':') {
1903 error(ERR_NONFATAL,
1904 "Syntax error processing `%%arg' directive");
1905 free_tlist(origline);
1906 return DIRECTIVE_FOUND;
1907 }
1908 tline = tline->next;
1909 if (!tline || tline->type != TOK_ID) {
1910 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1911 free_tlist(origline);
1912 return DIRECTIVE_FOUND;
1913 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001914
H. Peter Anvine2c80182005-01-15 22:15:51 +00001915 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001916 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001917 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001918 size = parse_size(tt->text);
1919 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001920 error(ERR_NONFATAL,
1921 "Invalid size type for `%%arg' missing directive");
1922 free_tlist(tt);
1923 free_tlist(origline);
1924 return DIRECTIVE_FOUND;
1925 }
1926 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001927
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001928 /* Round up to even stack slots */
1929 size = (size+StackSize-1) & ~(StackSize-1);
1930
H. Peter Anvine2c80182005-01-15 22:15:51 +00001931 /* Now define the macro for the argument */
1932 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1933 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001934 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001935 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001936
H. Peter Anvine2c80182005-01-15 22:15:51 +00001937 /* Move to the next argument in the list */
1938 tline = tline->next;
1939 if (tline && tline->type == TOK_WHITESPACE)
1940 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001941 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1942 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001943 free_tlist(origline);
1944 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001945
H. Peter Anvine2c80182005-01-15 22:15:51 +00001946 case PP_LOCAL:
1947 /* TASM like LOCAL directive to define local variables for a
1948 * function, in the following form:
1949 *
1950 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1951 *
1952 * The '= LocalSize' at the end is ignored by NASM, but is
1953 * required by TASM to define the local parameter size (and used
1954 * by the TASM macro package).
1955 */
1956 offset = LocalOffset;
1957 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001958 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001959 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001960
H. Peter Anvine2c80182005-01-15 22:15:51 +00001961 /* Find the argument name */
1962 tline = tline->next;
1963 if (tline && tline->type == TOK_WHITESPACE)
1964 tline = tline->next;
1965 if (!tline || tline->type != TOK_ID) {
1966 error(ERR_NONFATAL,
1967 "`%%local' missing argument parameter");
1968 free_tlist(origline);
1969 return DIRECTIVE_FOUND;
1970 }
1971 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001972
H. Peter Anvine2c80182005-01-15 22:15:51 +00001973 /* Find the argument size type */
1974 tline = tline->next;
1975 if (!tline || tline->type != TOK_OTHER
1976 || tline->text[0] != ':') {
1977 error(ERR_NONFATAL,
1978 "Syntax error processing `%%local' directive");
1979 free_tlist(origline);
1980 return DIRECTIVE_FOUND;
1981 }
1982 tline = tline->next;
1983 if (!tline || tline->type != TOK_ID) {
1984 error(ERR_NONFATAL,
1985 "`%%local' missing size type parameter");
1986 free_tlist(origline);
1987 return DIRECTIVE_FOUND;
1988 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001989
H. Peter Anvine2c80182005-01-15 22:15:51 +00001990 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001991 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001992 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001993 size = parse_size(tt->text);
1994 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001995 error(ERR_NONFATAL,
1996 "Invalid size type for `%%local' missing directive");
1997 free_tlist(tt);
1998 free_tlist(origline);
1999 return DIRECTIVE_FOUND;
2000 }
2001 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002002
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002003 /* Round up to even stack slots */
2004 size = (size+StackSize-1) & ~(StackSize-1);
2005
2006 offset += size; /* Negative offset, increment before */
2007
2008 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002009 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2010 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002011 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002012
H. Peter Anvine2c80182005-01-15 22:15:51 +00002013 /* Now define the assign to setup the enter_c macro correctly */
2014 snprintf(directive, sizeof(directive),
2015 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002016 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002017
H. Peter Anvine2c80182005-01-15 22:15:51 +00002018 /* Move to the next argument in the list */
2019 tline = tline->next;
2020 if (tline && tline->type == TOK_WHITESPACE)
2021 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002022 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2023 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002024 free_tlist(origline);
2025 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002026
H. Peter Anvine2c80182005-01-15 22:15:51 +00002027 case PP_CLEAR:
2028 if (tline->next)
2029 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002030 free_macros();
2031 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002032 free_tlist(origline);
2033 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002034
H. Peter Anvine2c80182005-01-15 22:15:51 +00002035 case PP_INCLUDE:
2036 tline = tline->next;
2037 skip_white_(tline);
2038 if (!tline || (tline->type != TOK_STRING &&
2039 tline->type != TOK_INTERNAL_STRING)) {
2040 error(ERR_NONFATAL, "`%%include' expects a file name");
2041 free_tlist(origline);
2042 return DIRECTIVE_FOUND; /* but we did _something_ */
2043 }
2044 if (tline->next)
2045 error(ERR_WARNING,
2046 "trailing garbage after `%%include' ignored");
2047 if (tline->type != TOK_INTERNAL_STRING) {
2048 p = tline->text + 1; /* point past the quote to the name */
2049 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2050 } else
2051 p = tline->text; /* internal_string is easier */
2052 expand_macros_in_string(&p);
2053 inc = nasm_malloc(sizeof(Include));
2054 inc->next = istk;
2055 inc->conds = NULL;
2056 inc->fp = inc_fopen(p);
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002057 if (!inc->fp && pass == 0) {
2058 /* -MG given but file not found */
2059 nasm_free(inc);
2060 } else {
2061 inc->fname = src_set_fname(p);
2062 inc->lineno = src_set_linnum(0);
2063 inc->lineinc = 1;
2064 inc->expansion = NULL;
2065 inc->mstk = NULL;
2066 istk = inc;
2067 list->uplevel(LIST_INCLUDE);
2068 }
2069 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002070 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002071
H. Peter Anvine2c80182005-01-15 22:15:51 +00002072 case PP_PUSH:
2073 tline = tline->next;
2074 skip_white_(tline);
2075 tline = expand_id(tline);
2076 if (!tok_type_(tline, TOK_ID)) {
2077 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2078 free_tlist(origline);
2079 return DIRECTIVE_FOUND; /* but we did _something_ */
2080 }
2081 if (tline->next)
2082 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2083 ctx = nasm_malloc(sizeof(Context));
2084 ctx->next = cstk;
H. Peter Anvin072771e2008-05-22 13:17:51 -07002085 ctx->localmac = hash_init(HASH_SMALL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002086 ctx->name = nasm_strdup(tline->text);
2087 ctx->number = unique++;
2088 cstk = ctx;
2089 free_tlist(origline);
2090 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002091
H. Peter Anvine2c80182005-01-15 22:15:51 +00002092 case PP_REPL:
2093 tline = tline->next;
2094 skip_white_(tline);
2095 tline = expand_id(tline);
2096 if (!tok_type_(tline, TOK_ID)) {
2097 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2098 free_tlist(origline);
2099 return DIRECTIVE_FOUND; /* but we did _something_ */
2100 }
2101 if (tline->next)
2102 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2103 if (!cstk)
2104 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2105 else {
2106 nasm_free(cstk->name);
2107 cstk->name = nasm_strdup(tline->text);
2108 }
2109 free_tlist(origline);
2110 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002111
H. Peter Anvine2c80182005-01-15 22:15:51 +00002112 case PP_POP:
2113 if (tline->next)
2114 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2115 if (!cstk)
2116 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2117 else
2118 ctx_pop();
2119 free_tlist(origline);
2120 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002121
H. Peter Anvine2c80182005-01-15 22:15:51 +00002122 case PP_ERROR:
2123 tline->next = expand_smacro(tline->next);
2124 tline = tline->next;
2125 skip_white_(tline);
2126 if (tok_type_(tline, TOK_STRING)) {
2127 p = tline->text + 1; /* point past the quote to the name */
2128 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2129 expand_macros_in_string(&p);
2130 error(ERR_NONFATAL, "%s", p);
2131 nasm_free(p);
2132 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002133 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002134 error(ERR_WARNING, "%s", p);
2135 nasm_free(p);
2136 }
2137 free_tlist(origline);
2138 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002139
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002140 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002141 if (istk->conds && !emitting(istk->conds->state))
2142 j = COND_NEVER;
2143 else {
2144 j = if_condition(tline->next, i);
2145 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002146 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2147 }
2148 cond = nasm_malloc(sizeof(Cond));
2149 cond->next = istk->conds;
2150 cond->state = j;
2151 istk->conds = cond;
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002152 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002153 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002154
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002155 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002156 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002157 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002158 if (emitting(istk->conds->state)
2159 || istk->conds->state == COND_NEVER)
2160 istk->conds->state = COND_NEVER;
2161 else {
2162 /*
2163 * IMPORTANT: In the case of %if, we will already have
2164 * called expand_mmac_params(); however, if we're
2165 * processing an %elif we must have been in a
2166 * non-emitting mode, which would have inhibited
2167 * the normal invocation of expand_mmac_params(). Therefore,
2168 * we have to do it explicitly here.
2169 */
2170 j = if_condition(expand_mmac_params(tline->next), i);
2171 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002172 istk->conds->state =
2173 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2174 }
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002175 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002176 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002177
H. Peter Anvine2c80182005-01-15 22:15:51 +00002178 case PP_ELSE:
2179 if (tline->next)
2180 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2181 if (!istk->conds)
2182 error(ERR_FATAL, "`%%else': no matching `%%if'");
2183 if (emitting(istk->conds->state)
2184 || istk->conds->state == COND_NEVER)
2185 istk->conds->state = COND_ELSE_FALSE;
2186 else
2187 istk->conds->state = COND_ELSE_TRUE;
2188 free_tlist(origline);
2189 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002190
H. Peter Anvine2c80182005-01-15 22:15:51 +00002191 case PP_ENDIF:
2192 if (tline->next)
2193 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2194 if (!istk->conds)
2195 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2196 cond = istk->conds;
2197 istk->conds = cond->next;
2198 nasm_free(cond);
2199 free_tlist(origline);
2200 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002201
H. Peter Anvine2c80182005-01-15 22:15:51 +00002202 case PP_MACRO:
2203 case PP_IMACRO:
2204 if (defining)
2205 error(ERR_FATAL,
2206 "`%%%smacro': already defining a macro",
2207 (i == PP_IMACRO ? "i" : ""));
2208 tline = tline->next;
2209 skip_white_(tline);
2210 tline = expand_id(tline);
2211 if (!tok_type_(tline, TOK_ID)) {
2212 error(ERR_NONFATAL,
2213 "`%%%smacro' expects a macro name",
2214 (i == PP_IMACRO ? "i" : ""));
2215 return DIRECTIVE_FOUND;
2216 }
2217 defining = nasm_malloc(sizeof(MMacro));
2218 defining->name = nasm_strdup(tline->text);
2219 defining->casesense = (i == PP_MACRO);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002220 defining->plus = false;
2221 defining->nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002222 defining->in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002223 defining->rep_nest = NULL;
2224 tline = expand_smacro(tline->next);
2225 skip_white_(tline);
2226 if (!tok_type_(tline, TOK_NUMBER)) {
2227 error(ERR_NONFATAL,
2228 "`%%%smacro' expects a parameter count",
2229 (i == PP_IMACRO ? "i" : ""));
2230 defining->nparam_min = defining->nparam_max = 0;
2231 } else {
2232 defining->nparam_min = defining->nparam_max =
H. Peter Anvin70055962007-10-11 00:05:31 -07002233 readnum(tline->text, &err);
2234 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002235 error(ERR_NONFATAL,
2236 "unable to parse parameter count `%s'", tline->text);
2237 }
2238 if (tline && tok_is_(tline->next, "-")) {
2239 tline = tline->next->next;
2240 if (tok_is_(tline, "*"))
2241 defining->nparam_max = INT_MAX;
2242 else if (!tok_type_(tline, TOK_NUMBER))
2243 error(ERR_NONFATAL,
2244 "`%%%smacro' expects a parameter count after `-'",
2245 (i == PP_IMACRO ? "i" : ""));
2246 else {
H. Peter Anvin70055962007-10-11 00:05:31 -07002247 defining->nparam_max = readnum(tline->text, &err);
2248 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002249 error(ERR_NONFATAL,
2250 "unable to parse parameter count `%s'",
2251 tline->text);
2252 if (defining->nparam_min > defining->nparam_max)
2253 error(ERR_NONFATAL,
2254 "minimum parameter count exceeds maximum");
2255 }
2256 }
2257 if (tline && tok_is_(tline->next, "+")) {
2258 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002259 defining->plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002260 }
2261 if (tline && tok_type_(tline->next, TOK_ID) &&
2262 !nasm_stricmp(tline->next->text, ".nolist")) {
2263 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002264 defining->nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002265 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07002266 mmac = (MMacro *) hash_findix(mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002267 while (mmac) {
2268 if (!strcmp(mmac->name, defining->name) &&
2269 (mmac->nparam_min <= defining->nparam_max
2270 || defining->plus)
2271 && (defining->nparam_min <= mmac->nparam_max
2272 || mmac->plus)) {
2273 error(ERR_WARNING,
2274 "redefining multi-line macro `%s'", defining->name);
2275 break;
2276 }
2277 mmac = mmac->next;
2278 }
2279 /*
2280 * Handle default parameters.
2281 */
2282 if (tline && tline->next) {
2283 defining->dlist = tline->next;
2284 tline->next = NULL;
2285 count_mmac_params(defining->dlist, &defining->ndefs,
2286 &defining->defaults);
2287 } else {
2288 defining->dlist = NULL;
2289 defining->defaults = NULL;
2290 }
2291 defining->expansion = NULL;
2292 free_tlist(origline);
2293 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002294
H. Peter Anvine2c80182005-01-15 22:15:51 +00002295 case PP_ENDM:
2296 case PP_ENDMACRO:
2297 if (!defining) {
2298 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2299 return DIRECTIVE_FOUND;
2300 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07002301 mmhead = (MMacro **) hash_findi_add(mmacros, defining->name);
2302 defining->next = *mmhead;
2303 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002304 defining = NULL;
2305 free_tlist(origline);
2306 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002307
H. Peter Anvine2c80182005-01-15 22:15:51 +00002308 case PP_ROTATE:
2309 if (tline->next && tline->next->type == TOK_WHITESPACE)
2310 tline = tline->next;
2311 if (tline->next == NULL) {
2312 free_tlist(origline);
2313 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2314 return DIRECTIVE_FOUND;
2315 }
2316 t = expand_smacro(tline->next);
2317 tline->next = NULL;
2318 free_tlist(origline);
2319 tline = t;
2320 tptr = &t;
2321 tokval.t_type = TOKEN_INVALID;
2322 evalresult =
2323 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2324 free_tlist(tline);
2325 if (!evalresult)
2326 return DIRECTIVE_FOUND;
2327 if (tokval.t_type)
2328 error(ERR_WARNING,
2329 "trailing garbage after expression ignored");
2330 if (!is_simple(evalresult)) {
2331 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2332 return DIRECTIVE_FOUND;
2333 }
2334 mmac = istk->mstk;
2335 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2336 mmac = mmac->next_active;
2337 if (!mmac) {
2338 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2339 } else if (mmac->nparam == 0) {
2340 error(ERR_NONFATAL,
2341 "`%%rotate' invoked within macro without parameters");
2342 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002343 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002344
H. Peter Anvin25a99342007-09-22 17:45:45 -07002345 rotate %= (int)mmac->nparam;
2346 if (rotate < 0)
2347 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002348
H. Peter Anvin25a99342007-09-22 17:45:45 -07002349 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002350 }
2351 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002352
H. Peter Anvine2c80182005-01-15 22:15:51 +00002353 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002354 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002355 do {
2356 tline = tline->next;
2357 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002358
H. Peter Anvine2c80182005-01-15 22:15:51 +00002359 if (tok_type_(tline, TOK_ID) &&
2360 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002361 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002362 do {
2363 tline = tline->next;
2364 } while (tok_type_(tline, TOK_WHITESPACE));
2365 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002366
H. Peter Anvine2c80182005-01-15 22:15:51 +00002367 if (tline) {
2368 t = expand_smacro(tline);
2369 tptr = &t;
2370 tokval.t_type = TOKEN_INVALID;
2371 evalresult =
2372 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2373 if (!evalresult) {
2374 free_tlist(origline);
2375 return DIRECTIVE_FOUND;
2376 }
2377 if (tokval.t_type)
2378 error(ERR_WARNING,
2379 "trailing garbage after expression ignored");
2380 if (!is_simple(evalresult)) {
2381 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2382 return DIRECTIVE_FOUND;
2383 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002384 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002385 } else {
2386 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002387 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002388 }
2389 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002390
H. Peter Anvine2c80182005-01-15 22:15:51 +00002391 tmp_defining = defining;
2392 defining = nasm_malloc(sizeof(MMacro));
2393 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002394 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002395 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002396 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002397 defining->in_progress = count;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002398 defining->nparam_min = defining->nparam_max = 0;
2399 defining->defaults = NULL;
2400 defining->dlist = NULL;
2401 defining->expansion = NULL;
2402 defining->next_active = istk->mstk;
2403 defining->rep_nest = tmp_defining;
2404 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002405
H. Peter Anvine2c80182005-01-15 22:15:51 +00002406 case PP_ENDREP:
2407 if (!defining || defining->name) {
2408 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2409 return DIRECTIVE_FOUND;
2410 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002411
H. Peter Anvine2c80182005-01-15 22:15:51 +00002412 /*
2413 * Now we have a "macro" defined - although it has no name
2414 * and we won't be entering it in the hash tables - we must
2415 * push a macro-end marker for it on to istk->expansion.
2416 * After that, it will take care of propagating itself (a
2417 * macro-end marker line for a macro which is really a %rep
2418 * block will cause the macro to be re-expanded, complete
2419 * with another macro-end marker to ensure the process
2420 * continues) until the whole expansion is forcibly removed
2421 * from istk->expansion by a %exitrep.
2422 */
2423 l = nasm_malloc(sizeof(Line));
2424 l->next = istk->expansion;
2425 l->finishes = defining;
2426 l->first = NULL;
2427 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002428
H. Peter Anvine2c80182005-01-15 22:15:51 +00002429 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002430
H. Peter Anvine2c80182005-01-15 22:15:51 +00002431 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2432 tmp_defining = defining;
2433 defining = defining->rep_nest;
2434 free_tlist(origline);
2435 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002436
H. Peter Anvine2c80182005-01-15 22:15:51 +00002437 case PP_EXITREP:
2438 /*
2439 * We must search along istk->expansion until we hit a
2440 * macro-end marker for a macro with no name. Then we set
2441 * its `in_progress' flag to 0.
2442 */
2443 for (l = istk->expansion; l; l = l->next)
2444 if (l->finishes && !l->finishes->name)
2445 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002446
H. Peter Anvine2c80182005-01-15 22:15:51 +00002447 if (l)
2448 l->finishes->in_progress = 0;
2449 else
2450 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2451 free_tlist(origline);
2452 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002453
H. Peter Anvine2c80182005-01-15 22:15:51 +00002454 case PP_XDEFINE:
2455 case PP_IXDEFINE:
2456 case PP_DEFINE:
2457 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002458 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002459
H. Peter Anvine2c80182005-01-15 22:15:51 +00002460 tline = tline->next;
2461 skip_white_(tline);
2462 tline = expand_id(tline);
2463 if (!tline || (tline->type != TOK_ID &&
2464 (tline->type != TOK_PREPROC_ID ||
2465 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002466 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2467 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002468 free_tlist(origline);
2469 return DIRECTIVE_FOUND;
2470 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002471
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002472 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002473
H. Peter Anvine2c80182005-01-15 22:15:51 +00002474 mname = tline->text;
2475 last = tline;
2476 param_start = tline = tline->next;
2477 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002478
H. Peter Anvine2c80182005-01-15 22:15:51 +00002479 /* Expand the macro definition now for %xdefine and %ixdefine */
2480 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2481 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002482
H. Peter Anvine2c80182005-01-15 22:15:51 +00002483 if (tok_is_(tline, "(")) {
2484 /*
2485 * This macro has parameters.
2486 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002487
H. Peter Anvine2c80182005-01-15 22:15:51 +00002488 tline = tline->next;
2489 while (1) {
2490 skip_white_(tline);
2491 if (!tline) {
2492 error(ERR_NONFATAL, "parameter identifier expected");
2493 free_tlist(origline);
2494 return DIRECTIVE_FOUND;
2495 }
2496 if (tline->type != TOK_ID) {
2497 error(ERR_NONFATAL,
2498 "`%s': parameter identifier expected",
2499 tline->text);
2500 free_tlist(origline);
2501 return DIRECTIVE_FOUND;
2502 }
2503 tline->type = TOK_SMAC_PARAM + nparam++;
2504 tline = tline->next;
2505 skip_white_(tline);
2506 if (tok_is_(tline, ",")) {
2507 tline = tline->next;
2508 continue;
2509 }
2510 if (!tok_is_(tline, ")")) {
2511 error(ERR_NONFATAL,
2512 "`)' expected to terminate macro template");
2513 free_tlist(origline);
2514 return DIRECTIVE_FOUND;
2515 }
2516 break;
2517 }
2518 last = tline;
2519 tline = tline->next;
2520 }
2521 if (tok_type_(tline, TOK_WHITESPACE))
2522 last = tline, tline = tline->next;
2523 macro_start = NULL;
2524 last->next = NULL;
2525 t = tline;
2526 while (t) {
2527 if (t->type == TOK_ID) {
2528 for (tt = param_start; tt; tt = tt->next)
2529 if (tt->type >= TOK_SMAC_PARAM &&
2530 !strcmp(tt->text, t->text))
2531 t->type = tt->type;
2532 }
2533 tt = t->next;
2534 t->next = macro_start;
2535 macro_start = t;
2536 t = tt;
2537 }
2538 /*
2539 * Good. We now have a macro name, a parameter count, and a
2540 * token list (in reverse order) for an expansion. We ought
2541 * to be OK just to create an SMacro, store it, and let
2542 * free_tlist have the rest of the line (which we have
2543 * carefully re-terminated after chopping off the expansion
2544 * from the end).
2545 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002546 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002547 free_tlist(origline);
2548 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002549
H. Peter Anvine2c80182005-01-15 22:15:51 +00002550 case PP_UNDEF:
2551 tline = tline->next;
2552 skip_white_(tline);
2553 tline = expand_id(tline);
2554 if (!tline || (tline->type != TOK_ID &&
2555 (tline->type != TOK_PREPROC_ID ||
2556 tline->text[1] != '$'))) {
2557 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2558 free_tlist(origline);
2559 return DIRECTIVE_FOUND;
2560 }
2561 if (tline->next) {
2562 error(ERR_WARNING,
2563 "trailing garbage after macro name ignored");
2564 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002565
H. Peter Anvine2c80182005-01-15 22:15:51 +00002566 /* Find the context that symbol belongs to */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002567 ctx = get_ctx(tline->text, false);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002568 undef_smacro(ctx, tline->text);
2569 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002570 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002571
H. Peter Anvine2c80182005-01-15 22:15:51 +00002572 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002573 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07002574
H. Peter Anvine2c80182005-01-15 22:15:51 +00002575 tline = tline->next;
2576 skip_white_(tline);
2577 tline = expand_id(tline);
2578 if (!tline || (tline->type != TOK_ID &&
2579 (tline->type != TOK_PREPROC_ID ||
2580 tline->text[1] != '$'))) {
2581 error(ERR_NONFATAL,
2582 "`%%strlen' expects a macro identifier as first parameter");
2583 free_tlist(origline);
2584 return DIRECTIVE_FOUND;
2585 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002586 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002587
H. Peter Anvine2c80182005-01-15 22:15:51 +00002588 mname = tline->text;
2589 last = tline;
2590 tline = expand_smacro(tline->next);
2591 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002592
H. Peter Anvine2c80182005-01-15 22:15:51 +00002593 t = tline;
2594 while (tok_type_(t, TOK_WHITESPACE))
2595 t = t->next;
2596 /* t should now point to the string */
2597 if (t->type != TOK_STRING) {
2598 error(ERR_NONFATAL,
2599 "`%%strlen` requires string as second parameter");
2600 free_tlist(tline);
2601 free_tlist(origline);
2602 return DIRECTIVE_FOUND;
2603 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002604
H. Peter Anvine2c80182005-01-15 22:15:51 +00002605 macro_start = nasm_malloc(sizeof(*macro_start));
2606 macro_start->next = NULL;
2607 make_tok_num(macro_start, strlen(t->text) - 2);
2608 macro_start->mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002609
H. Peter Anvine2c80182005-01-15 22:15:51 +00002610 /*
2611 * We now have a macro name, an implicit parameter count of
2612 * zero, and a numeric token to use as an expansion. Create
2613 * and store an SMacro.
2614 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002615 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002616 free_tlist(tline);
2617 free_tlist(origline);
2618 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002619
H. Peter Anvine2c80182005-01-15 22:15:51 +00002620 case PP_SUBSTR:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002621 casesense = true;
2622
H. Peter Anvine2c80182005-01-15 22:15:51 +00002623 tline = tline->next;
2624 skip_white_(tline);
2625 tline = expand_id(tline);
2626 if (!tline || (tline->type != TOK_ID &&
2627 (tline->type != TOK_PREPROC_ID ||
2628 tline->text[1] != '$'))) {
2629 error(ERR_NONFATAL,
2630 "`%%substr' expects a macro identifier as first parameter");
2631 free_tlist(origline);
2632 return DIRECTIVE_FOUND;
2633 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002634 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002635
H. Peter Anvine2c80182005-01-15 22:15:51 +00002636 mname = tline->text;
2637 last = tline;
2638 tline = expand_smacro(tline->next);
2639 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002640
H. Peter Anvine2c80182005-01-15 22:15:51 +00002641 t = tline->next;
2642 while (tok_type_(t, TOK_WHITESPACE))
2643 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002644
H. Peter Anvine2c80182005-01-15 22:15:51 +00002645 /* t should now point to the string */
2646 if (t->type != TOK_STRING) {
2647 error(ERR_NONFATAL,
2648 "`%%substr` requires string as second parameter");
2649 free_tlist(tline);
2650 free_tlist(origline);
2651 return DIRECTIVE_FOUND;
2652 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002653
H. Peter Anvine2c80182005-01-15 22:15:51 +00002654 tt = t->next;
2655 tptr = &tt;
2656 tokval.t_type = TOKEN_INVALID;
2657 evalresult =
2658 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2659 if (!evalresult) {
2660 free_tlist(tline);
2661 free_tlist(origline);
2662 return DIRECTIVE_FOUND;
2663 }
2664 if (!is_simple(evalresult)) {
2665 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2666 free_tlist(tline);
2667 free_tlist(origline);
2668 return DIRECTIVE_FOUND;
2669 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002670
H. Peter Anvine2c80182005-01-15 22:15:51 +00002671 macro_start = nasm_malloc(sizeof(*macro_start));
2672 macro_start->next = NULL;
2673 macro_start->text = nasm_strdup("'''");
2674 if (evalresult->value > 0
Charles Crayne192d5b52007-10-18 19:02:42 -07002675 && evalresult->value < (int) strlen(t->text) - 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002676 macro_start->text[1] = t->text[evalresult->value];
2677 } else {
2678 macro_start->text[2] = '\0';
2679 }
2680 macro_start->type = TOK_STRING;
2681 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002682
H. Peter Anvine2c80182005-01-15 22:15:51 +00002683 /*
2684 * We now have a macro name, an implicit parameter count of
2685 * zero, and a numeric token to use as an expansion. Create
2686 * and store an SMacro.
2687 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002688 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002689 free_tlist(tline);
2690 free_tlist(origline);
2691 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002692
H. Peter Anvine2c80182005-01-15 22:15:51 +00002693 case PP_ASSIGN:
2694 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002695 casesense = (i == PP_ASSIGN);
2696
H. Peter Anvine2c80182005-01-15 22:15:51 +00002697 tline = tline->next;
2698 skip_white_(tline);
2699 tline = expand_id(tline);
2700 if (!tline || (tline->type != TOK_ID &&
2701 (tline->type != TOK_PREPROC_ID ||
2702 tline->text[1] != '$'))) {
2703 error(ERR_NONFATAL,
2704 "`%%%sassign' expects a macro identifier",
2705 (i == PP_IASSIGN ? "i" : ""));
2706 free_tlist(origline);
2707 return DIRECTIVE_FOUND;
2708 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002709 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002710
H. Peter Anvine2c80182005-01-15 22:15:51 +00002711 mname = tline->text;
2712 last = tline;
2713 tline = expand_smacro(tline->next);
2714 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002715
H. Peter Anvine2c80182005-01-15 22:15:51 +00002716 t = tline;
2717 tptr = &t;
2718 tokval.t_type = TOKEN_INVALID;
2719 evalresult =
2720 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2721 free_tlist(tline);
2722 if (!evalresult) {
2723 free_tlist(origline);
2724 return DIRECTIVE_FOUND;
2725 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002726
H. Peter Anvine2c80182005-01-15 22:15:51 +00002727 if (tokval.t_type)
2728 error(ERR_WARNING,
2729 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00002730
H. Peter Anvine2c80182005-01-15 22:15:51 +00002731 if (!is_simple(evalresult)) {
2732 error(ERR_NONFATAL,
2733 "non-constant value given to `%%%sassign'",
2734 (i == PP_IASSIGN ? "i" : ""));
2735 free_tlist(origline);
2736 return DIRECTIVE_FOUND;
2737 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002738
H. Peter Anvine2c80182005-01-15 22:15:51 +00002739 macro_start = nasm_malloc(sizeof(*macro_start));
2740 macro_start->next = NULL;
2741 make_tok_num(macro_start, reloc_value(evalresult));
2742 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002743
H. Peter Anvine2c80182005-01-15 22:15:51 +00002744 /*
2745 * We now have a macro name, an implicit parameter count of
2746 * zero, and a numeric token to use as an expansion. Create
2747 * and store an SMacro.
2748 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002749 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002750 free_tlist(origline);
2751 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002752
H. Peter Anvine2c80182005-01-15 22:15:51 +00002753 case PP_LINE:
2754 /*
2755 * Syntax is `%line nnn[+mmm] [filename]'
2756 */
2757 tline = tline->next;
2758 skip_white_(tline);
2759 if (!tok_type_(tline, TOK_NUMBER)) {
2760 error(ERR_NONFATAL, "`%%line' expects line number");
2761 free_tlist(origline);
2762 return DIRECTIVE_FOUND;
2763 }
H. Peter Anvin70055962007-10-11 00:05:31 -07002764 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002765 m = 1;
2766 tline = tline->next;
2767 if (tok_is_(tline, "+")) {
2768 tline = tline->next;
2769 if (!tok_type_(tline, TOK_NUMBER)) {
2770 error(ERR_NONFATAL, "`%%line' expects line increment");
2771 free_tlist(origline);
2772 return DIRECTIVE_FOUND;
2773 }
H. Peter Anvin70055962007-10-11 00:05:31 -07002774 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002775 tline = tline->next;
2776 }
2777 skip_white_(tline);
2778 src_set_linnum(k);
2779 istk->lineinc = m;
2780 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002781 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002782 }
2783 free_tlist(origline);
2784 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002785
H. Peter Anvine2c80182005-01-15 22:15:51 +00002786 default:
2787 error(ERR_FATAL,
2788 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00002789 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002790 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002791 }
Ed Beroset3ab3f412002-06-11 03:31:49 +00002792 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002793}
2794
2795/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002796 * Ensure that a macro parameter contains a condition code and
2797 * nothing else. Return the condition code index if so, or -1
2798 * otherwise.
2799 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002800static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002801{
H. Peter Anvin76690a12002-04-30 20:52:49 +00002802 Token *tt;
2803 int i, j, k, m;
2804
H. Peter Anvin25a99342007-09-22 17:45:45 -07002805 if (!t)
2806 return -1; /* Probably a %+ without a space */
2807
H. Peter Anvineba20a72002-04-30 20:53:55 +00002808 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002809 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002810 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002811 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002812 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002813 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002814 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002815
2816 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00002817 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002818 while (j - i > 1) {
2819 k = (j + i) / 2;
2820 m = nasm_stricmp(t->text, conditions[k]);
2821 if (m == 0) {
2822 i = k;
2823 j = -2;
2824 break;
2825 } else if (m < 0) {
2826 j = k;
2827 } else
2828 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002829 }
2830 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002831 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002832 return i;
2833}
2834
2835/*
2836 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2837 * %-n) and MMacro-local identifiers (%%foo).
2838 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002839static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002840{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002841 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002842
2843 tail = &thead;
2844 thead = NULL;
2845
H. Peter Anvine2c80182005-01-15 22:15:51 +00002846 while (tline) {
2847 if (tline->type == TOK_PREPROC_ID &&
2848 (((tline->text[1] == '+' || tline->text[1] == '-')
2849 && tline->text[2]) || tline->text[1] == '%'
2850 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002851 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002852 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002853 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07002854 unsigned int n;
2855 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002856 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002857
H. Peter Anvine2c80182005-01-15 22:15:51 +00002858 t = tline;
2859 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002860
H. Peter Anvine2c80182005-01-15 22:15:51 +00002861 mac = istk->mstk;
2862 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2863 mac = mac->next_active;
2864 if (!mac)
2865 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2866 else
2867 switch (t->text[1]) {
2868 /*
2869 * We have to make a substitution of one of the
2870 * forms %1, %-1, %+1, %%foo, %0.
2871 */
2872 case '0':
2873 type = TOK_NUMBER;
2874 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
2875 text = nasm_strdup(tmpbuf);
2876 break;
2877 case '%':
2878 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002879 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002880 mac->unique);
2881 text = nasm_strcat(tmpbuf, t->text + 2);
2882 break;
2883 case '-':
2884 n = atoi(t->text + 2) - 1;
2885 if (n >= mac->nparam)
2886 tt = NULL;
2887 else {
2888 if (mac->nparam > 1)
2889 n = (n + mac->rotate) % mac->nparam;
2890 tt = mac->params[n];
2891 }
2892 cc = find_cc(tt);
2893 if (cc == -1) {
2894 error(ERR_NONFATAL,
2895 "macro parameter %d is not a condition code",
2896 n + 1);
2897 text = NULL;
2898 } else {
2899 type = TOK_ID;
2900 if (inverse_ccs[cc] == -1) {
2901 error(ERR_NONFATAL,
2902 "condition code `%s' is not invertible",
2903 conditions[cc]);
2904 text = NULL;
2905 } else
2906 text =
2907 nasm_strdup(conditions[inverse_ccs[cc]]);
2908 }
2909 break;
2910 case '+':
2911 n = atoi(t->text + 2) - 1;
2912 if (n >= mac->nparam)
2913 tt = NULL;
2914 else {
2915 if (mac->nparam > 1)
2916 n = (n + mac->rotate) % mac->nparam;
2917 tt = mac->params[n];
2918 }
2919 cc = find_cc(tt);
2920 if (cc == -1) {
2921 error(ERR_NONFATAL,
2922 "macro parameter %d is not a condition code",
2923 n + 1);
2924 text = NULL;
2925 } else {
2926 type = TOK_ID;
2927 text = nasm_strdup(conditions[cc]);
2928 }
2929 break;
2930 default:
2931 n = atoi(t->text + 1) - 1;
2932 if (n >= mac->nparam)
2933 tt = NULL;
2934 else {
2935 if (mac->nparam > 1)
2936 n = (n + mac->rotate) % mac->nparam;
2937 tt = mac->params[n];
2938 }
2939 if (tt) {
2940 for (i = 0; i < mac->paramlen[n]; i++) {
2941 *tail = new_Token(NULL, tt->type, tt->text, 0);
2942 tail = &(*tail)->next;
2943 tt = tt->next;
2944 }
2945 }
2946 text = NULL; /* we've done it here */
2947 break;
2948 }
2949 if (!text) {
2950 delete_Token(t);
2951 } else {
2952 *tail = t;
2953 tail = &t->next;
2954 t->type = type;
2955 nasm_free(t->text);
2956 t->text = text;
2957 t->mac = NULL;
2958 }
2959 continue;
2960 } else {
2961 t = *tail = tline;
2962 tline = tline->next;
2963 t->mac = NULL;
2964 tail = &t->next;
2965 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002966 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00002967 *tail = NULL;
2968 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002969 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002970 switch (t->type) {
2971 case TOK_WHITESPACE:
2972 if (tt->type == TOK_WHITESPACE) {
2973 t->next = delete_Token(tt);
2974 }
2975 break;
2976 case TOK_ID:
2977 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002978 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002979 nasm_free(t->text);
2980 t->text = tmp;
2981 t->next = delete_Token(tt);
2982 }
2983 break;
2984 case TOK_NUMBER:
2985 if (tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002986 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002987 nasm_free(t->text);
2988 t->text = tmp;
2989 t->next = delete_Token(tt);
2990 }
2991 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002992 default:
2993 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002994 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002995
H. Peter Anvin76690a12002-04-30 20:52:49 +00002996 return thead;
2997}
2998
2999/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003000 * Expand all single-line macro calls made in the given line.
3001 * Return the expanded version of the line. The original is deemed
3002 * to be destroyed in the process. (In reality we'll just move
3003 * Tokens from input to output a lot of the time, rather than
3004 * actually bothering to destroy and replicate.)
3005 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003006#define DEADMAN_LIMIT (1 << 20)
3007
H. Peter Anvine2c80182005-01-15 22:15:51 +00003008static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003009{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003010 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003011 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003012 Token **params;
3013 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003014 unsigned int nparam, sparam;
3015 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003016 Token *org_tline = tline;
3017 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003018 char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003019 int deadman = DEADMAN_LIMIT;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003020
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003021 /*
3022 * Trick: we should avoid changing the start token pointer since it can
3023 * be contained in "next" field of other token. Because of this
3024 * we allocate a copy of first token and work with it; at the end of
3025 * routine we copy it back
3026 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003027 if (org_tline) {
3028 tline =
3029 new_Token(org_tline->next, org_tline->type, org_tline->text,
3030 0);
3031 tline->mac = org_tline->mac;
3032 nasm_free(org_tline->text);
3033 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003034 }
3035
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003036again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003037 tail = &thead;
3038 thead = NULL;
3039
H. Peter Anvine2c80182005-01-15 22:15:51 +00003040 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003041 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003042 error(ERR_NONFATAL, "interminable macro recursion");
3043 break;
3044 }
3045
H. Peter Anvine2c80182005-01-15 22:15:51 +00003046 if ((mname = tline->text)) {
3047 /* if this token is a local macro, look in local context */
3048 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003049 ctx = get_ctx(mname, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003050 else
3051 ctx = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07003052
3053 head = (SMacro *) hash_findix(ctx ? ctx->localmac : smacros,
3054 mname);
3055
H. Peter Anvine2c80182005-01-15 22:15:51 +00003056 /*
3057 * We've hit an identifier. As in is_mmacro below, we first
3058 * check whether the identifier is a single-line macro at
3059 * all, then think about checking for parameters if
3060 * necessary.
3061 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003062 for (m = head; m; m = m->next)
3063 if (!mstrcmp(m->name, mname, m->casesense))
3064 break;
3065 if (m) {
3066 mstart = tline;
3067 params = NULL;
3068 paramsize = NULL;
3069 if (m->nparam == 0) {
3070 /*
3071 * Simple case: the macro is parameterless. Discard the
3072 * one token that the macro call took, and push the
3073 * expansion back on the to-do stack.
3074 */
3075 if (!m->expansion) {
3076 if (!strcmp("__FILE__", m->name)) {
3077 int32_t num = 0;
3078 src_get(&num, &(tline->text));
3079 nasm_quote(&(tline->text));
3080 tline->type = TOK_STRING;
3081 continue;
3082 }
3083 if (!strcmp("__LINE__", m->name)) {
3084 nasm_free(tline->text);
3085 make_tok_num(tline, src_get_linnum());
3086 continue;
3087 }
3088 if (!strcmp("__BITS__", m->name)) {
3089 nasm_free(tline->text);
3090 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003091 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003092 }
3093 tline = delete_Token(tline);
3094 continue;
3095 }
3096 } else {
3097 /*
3098 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003099 * exists and takes parameters. We must find the
3100 * parameters in the call, count them, find the SMacro
3101 * that corresponds to that form of the macro call, and
3102 * substitute for the parameters when we expand. What a
3103 * pain.
3104 */
3105 /*tline = tline->next;
3106 skip_white_(tline); */
3107 do {
3108 t = tline->next;
3109 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003110 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003111 t->text = NULL;
3112 t = tline->next = delete_Token(t);
3113 }
3114 tline = t;
3115 } while (tok_type_(tline, TOK_WHITESPACE));
3116 if (!tok_is_(tline, "(")) {
3117 /*
3118 * This macro wasn't called with parameters: ignore
3119 * the call. (Behaviour borrowed from gnu cpp.)
3120 */
3121 tline = mstart;
3122 m = NULL;
3123 } else {
3124 int paren = 0;
3125 int white = 0;
3126 brackets = 0;
3127 nparam = 0;
3128 sparam = PARAM_DELTA;
3129 params = nasm_malloc(sparam * sizeof(Token *));
3130 params[0] = tline->next;
3131 paramsize = nasm_malloc(sparam * sizeof(int));
3132 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003133 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003134 /*
3135 * For some unusual expansions
3136 * which concatenates function call
3137 */
3138 t = tline->next;
3139 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003140 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003141 t->text = NULL;
3142 t = tline->next = delete_Token(t);
3143 }
3144 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003145
H. Peter Anvine2c80182005-01-15 22:15:51 +00003146 if (!tline) {
3147 error(ERR_NONFATAL,
3148 "macro call expects terminating `)'");
3149 break;
3150 }
3151 if (tline->type == TOK_WHITESPACE
3152 && brackets <= 0) {
3153 if (paramsize[nparam])
3154 white++;
3155 else
3156 params[nparam] = tline->next;
3157 continue; /* parameter loop */
3158 }
3159 if (tline->type == TOK_OTHER
3160 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003161 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003162 if (ch == ',' && !paren && brackets <= 0) {
3163 if (++nparam >= sparam) {
3164 sparam += PARAM_DELTA;
3165 params = nasm_realloc(params,
3166 sparam *
3167 sizeof(Token
3168 *));
3169 paramsize =
3170 nasm_realloc(paramsize,
3171 sparam *
3172 sizeof(int));
3173 }
3174 params[nparam] = tline->next;
3175 paramsize[nparam] = 0;
3176 white = 0;
3177 continue; /* parameter loop */
3178 }
3179 if (ch == '{' &&
3180 (brackets > 0 || (brackets == 0 &&
3181 !paramsize[nparam])))
3182 {
3183 if (!(brackets++)) {
3184 params[nparam] = tline->next;
3185 continue; /* parameter loop */
3186 }
3187 }
3188 if (ch == '}' && brackets > 0)
3189 if (--brackets == 0) {
3190 brackets = -1;
3191 continue; /* parameter loop */
3192 }
3193 if (ch == '(' && !brackets)
3194 paren++;
3195 if (ch == ')' && brackets <= 0)
3196 if (--paren < 0)
3197 break;
3198 }
3199 if (brackets < 0) {
3200 brackets = 0;
3201 error(ERR_NONFATAL, "braces do not "
3202 "enclose all of macro parameter");
3203 }
3204 paramsize[nparam] += white + 1;
3205 white = 0;
3206 } /* parameter loop */
3207 nparam++;
3208 while (m && (m->nparam != nparam ||
3209 mstrcmp(m->name, mname,
3210 m->casesense)))
3211 m = m->next;
3212 if (!m)
3213 error(ERR_WARNING | ERR_WARN_MNP,
3214 "macro `%s' exists, "
3215 "but not taking %d parameters",
3216 mstart->text, nparam);
3217 }
3218 }
3219 if (m && m->in_progress)
3220 m = NULL;
3221 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003222 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003223 * Design question: should we handle !tline, which
3224 * indicates missing ')' here, or expand those
3225 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003226 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003227 */
3228 nasm_free(params);
3229 nasm_free(paramsize);
3230 tline = mstart;
3231 } else {
3232 /*
3233 * Expand the macro: we are placed on the last token of the
3234 * call, so that we can easily split the call from the
3235 * following tokens. We also start by pushing an SMAC_END
3236 * token for the cycle removal.
3237 */
3238 t = tline;
3239 if (t) {
3240 tline = t->next;
3241 t->next = NULL;
3242 }
3243 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3244 tt->mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003245 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003246 tline = tt;
3247 for (t = m->expansion; t; t = t->next) {
3248 if (t->type >= TOK_SMAC_PARAM) {
3249 Token *pcopy = tline, **ptail = &pcopy;
3250 Token *ttt, *pt;
3251 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003252
H. Peter Anvine2c80182005-01-15 22:15:51 +00003253 ttt = params[t->type - TOK_SMAC_PARAM];
3254 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3255 --i >= 0;) {
3256 pt = *ptail =
3257 new_Token(tline, ttt->type, ttt->text,
3258 0);
3259 ptail = &pt->next;
3260 ttt = ttt->next;
3261 }
3262 tline = pcopy;
3263 } else {
3264 tt = new_Token(tline, t->type, t->text, 0);
3265 tline = tt;
3266 }
3267 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003268
H. Peter Anvine2c80182005-01-15 22:15:51 +00003269 /*
3270 * Having done that, get rid of the macro call, and clean
3271 * up the parameters.
3272 */
3273 nasm_free(params);
3274 nasm_free(paramsize);
3275 free_tlist(mstart);
3276 continue; /* main token loop */
3277 }
3278 }
3279 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003280
H. Peter Anvine2c80182005-01-15 22:15:51 +00003281 if (tline->type == TOK_SMAC_END) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003282 tline->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003283 tline = delete_Token(tline);
3284 } else {
3285 t = *tail = tline;
3286 tline = tline->next;
3287 t->mac = NULL;
3288 t->next = NULL;
3289 tail = &t->next;
3290 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003291 }
3292
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003293 /*
3294 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003295 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003296 * TOK_IDs should be concatenated.
3297 * Also we look for %+ tokens and concatenate the tokens before and after
3298 * them (without white spaces in between).
3299 */
3300 t = thead;
3301 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003302 while (t) {
3303 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3304 t = t->next;
3305 if (!t || !t->next)
3306 break;
3307 if (t->next->type == TOK_ID ||
3308 t->next->type == TOK_PREPROC_ID ||
3309 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003310 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003311 nasm_free(t->text);
3312 t->next = delete_Token(t->next);
3313 t->text = p;
3314 rescan = 1;
3315 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3316 t->next->next->type == TOK_PREPROC_ID &&
3317 strcmp(t->next->next->text, "%+") == 0) {
3318 /* free the next whitespace, the %+ token and next whitespace */
3319 int i;
3320 for (i = 1; i <= 3; i++) {
3321 if (!t->next
3322 || (i != 2 && t->next->type != TOK_WHITESPACE))
3323 break;
3324 t->next = delete_Token(t->next);
3325 } /* endfor */
3326 } else
3327 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003328 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003329 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003330 if (rescan) {
3331 tline = thead;
3332 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003333 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003334
H. Peter Anvine2c80182005-01-15 22:15:51 +00003335 if (org_tline) {
3336 if (thead) {
3337 *org_tline = *thead;
3338 /* since we just gave text to org_line, don't free it */
3339 thead->text = NULL;
3340 delete_Token(thead);
3341 } else {
3342 /* the expression expanded to empty line;
3343 we can't return NULL for some reasons
3344 we just set the line to a single WHITESPACE token. */
3345 memset(org_tline, 0, sizeof(*org_tline));
3346 org_tline->text = NULL;
3347 org_tline->type = TOK_WHITESPACE;
3348 }
3349 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003350 }
3351
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003352 return thead;
3353}
3354
3355/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003356 * Similar to expand_smacro but used exclusively with macro identifiers
3357 * right before they are fetched in. The reason is that there can be
3358 * identifiers consisting of several subparts. We consider that if there
3359 * are more than one element forming the name, user wants a expansion,
3360 * otherwise it will be left as-is. Example:
3361 *
3362 * %define %$abc cde
3363 *
3364 * the identifier %$abc will be left as-is so that the handler for %define
3365 * will suck it and define the corresponding value. Other case:
3366 *
3367 * %define _%$abc cde
3368 *
3369 * In this case user wants name to be expanded *before* %define starts
3370 * working, so we'll expand %$abc into something (if it has a value;
3371 * otherwise it will be left as-is) then concatenate all successive
3372 * PP_IDs into one.
3373 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003374static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003375{
3376 Token *cur, *oldnext = NULL;
3377
H. Peter Anvin734b1882002-04-30 21:01:08 +00003378 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003379 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003380
3381 cur = tline;
3382 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003383 (cur->next->type == TOK_ID ||
3384 cur->next->type == TOK_PREPROC_ID
3385 || cur->next->type == TOK_NUMBER))
3386 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003387
3388 /* If identifier consists of just one token, don't expand */
3389 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003390 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003391
H. Peter Anvine2c80182005-01-15 22:15:51 +00003392 if (cur) {
3393 oldnext = cur->next; /* Detach the tail past identifier */
3394 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003395 }
3396
H. Peter Anvin734b1882002-04-30 21:01:08 +00003397 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003398
H. Peter Anvine2c80182005-01-15 22:15:51 +00003399 if (cur) {
3400 /* expand_smacro possibly changhed tline; re-scan for EOL */
3401 cur = tline;
3402 while (cur && cur->next)
3403 cur = cur->next;
3404 if (cur)
3405 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003406 }
3407
3408 return tline;
3409}
3410
3411/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003412 * Determine whether the given line constitutes a multi-line macro
3413 * call, and return the MMacro structure called if so. Doesn't have
3414 * to check for an initial label - that's taken care of in
3415 * expand_mmacro - but must check numbers of parameters. Guaranteed
3416 * to be called with tline->type == TOK_ID, so the putative macro
3417 * name is easy to find.
3418 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003419static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003420{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003421 MMacro *head, *m;
3422 Token **params;
3423 int nparam;
3424
H. Peter Anvin97a23472007-09-16 17:57:25 -07003425 head = (MMacro *) hash_findix(mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003426
3427 /*
3428 * Efficiency: first we see if any macro exists with the given
3429 * name. If not, we can return NULL immediately. _Then_ we
3430 * count the parameters, and then we look further along the
3431 * list if necessary to find the proper MMacro.
3432 */
3433 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003434 if (!mstrcmp(m->name, tline->text, m->casesense))
3435 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003436 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003437 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003438
3439 /*
3440 * OK, we have a potential macro. Count and demarcate the
3441 * parameters.
3442 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003443 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003444
3445 /*
3446 * So we know how many parameters we've got. Find the MMacro
3447 * structure that handles this number.
3448 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003449 while (m) {
3450 if (m->nparam_min <= nparam
3451 && (m->plus || nparam <= m->nparam_max)) {
3452 /*
3453 * This one is right. Just check if cycle removal
3454 * prohibits us using it before we actually celebrate...
3455 */
3456 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003457#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003458 error(ERR_NONFATAL,
3459 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003460#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003461 nasm_free(params);
3462 return NULL;
3463 }
3464 /*
3465 * It's right, and we can use it. Add its default
3466 * parameters to the end of our list if necessary.
3467 */
3468 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3469 params =
3470 nasm_realloc(params,
3471 ((m->nparam_min + m->ndefs +
3472 1) * sizeof(*params)));
3473 while (nparam < m->nparam_min + m->ndefs) {
3474 params[nparam] = m->defaults[nparam - m->nparam_min];
3475 nparam++;
3476 }
3477 }
3478 /*
3479 * If we've gone over the maximum parameter count (and
3480 * we're in Plus mode), ignore parameters beyond
3481 * nparam_max.
3482 */
3483 if (m->plus && nparam > m->nparam_max)
3484 nparam = m->nparam_max;
3485 /*
3486 * Then terminate the parameter list, and leave.
3487 */
3488 if (!params) { /* need this special case */
3489 params = nasm_malloc(sizeof(*params));
3490 nparam = 0;
3491 }
3492 params[nparam] = NULL;
3493 *params_array = params;
3494 return m;
3495 }
3496 /*
3497 * This one wasn't right: look for the next one with the
3498 * same name.
3499 */
3500 for (m = m->next; m; m = m->next)
3501 if (!mstrcmp(m->name, tline->text, m->casesense))
3502 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003503 }
3504
3505 /*
3506 * After all that, we didn't find one with the right number of
3507 * parameters. Issue a warning, and fail to expand the macro.
3508 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003509 error(ERR_WARNING | ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003510 "macro `%s' exists, but not taking %d parameters",
3511 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003512 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003513 return NULL;
3514}
3515
3516/*
3517 * Expand the multi-line macro call made by the given line, if
3518 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003519 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003520 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003521static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003522{
3523 Token *startline = tline;
3524 Token *label = NULL;
3525 int dont_prepend = 0;
3526 Token **params, *t, *tt;
3527 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003528 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003529 int i, nparam, *paramlen;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003530
3531 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003532 skip_white_(t);
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003533/* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3534 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003535 return 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003536 m = is_mmacro(t, &params);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003537 if (!m) {
3538 Token *last;
3539 /*
3540 * We have an id which isn't a macro call. We'll assume
3541 * it might be a label; we'll also check to see if a
3542 * colon follows it. Then, if there's another id after
3543 * that lot, we'll check it again for macro-hood.
3544 */
3545 label = last = t;
3546 t = t->next;
3547 if (tok_type_(t, TOK_WHITESPACE))
3548 last = t, t = t->next;
3549 if (tok_is_(t, ":")) {
3550 dont_prepend = 1;
3551 last = t, t = t->next;
3552 if (tok_type_(t, TOK_WHITESPACE))
3553 last = t, t = t->next;
3554 }
3555 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3556 return 0;
3557 last->next = NULL;
3558 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003559 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003560
3561 /*
3562 * Fix up the parameters: this involves stripping leading and
3563 * trailing whitespace, then stripping braces if they are
3564 * present.
3565 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003566 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003567 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003568
H. Peter Anvine2c80182005-01-15 22:15:51 +00003569 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003570 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003571 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003572
H. Peter Anvine2c80182005-01-15 22:15:51 +00003573 t = params[i];
3574 skip_white_(t);
3575 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003576 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003577 params[i] = t;
3578 paramlen[i] = 0;
3579 while (t) {
3580 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3581 break; /* ... because we have hit a comma */
3582 if (comma && t->type == TOK_WHITESPACE
3583 && tok_is_(t->next, ","))
3584 break; /* ... or a space then a comma */
3585 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3586 break; /* ... or a brace */
3587 t = t->next;
3588 paramlen[i]++;
3589 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003590 }
3591
3592 /*
3593 * OK, we have a MMacro structure together with a set of
3594 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003595 * copies of each Line on to istk->expansion. Substitution of
3596 * parameter tokens and macro-local tokens doesn't get done
3597 * until the single-line macro substitution process; this is
3598 * because delaying them allows us to change the semantics
3599 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003600 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003601 * First, push an end marker on to istk->expansion, mark this
3602 * macro as in progress, and set up its invocation-specific
3603 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003604 */
3605 ll = nasm_malloc(sizeof(Line));
3606 ll->next = istk->expansion;
3607 ll->finishes = m;
3608 ll->first = NULL;
3609 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003610
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003611 m->in_progress = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003612 m->params = params;
3613 m->iline = tline;
3614 m->nparam = nparam;
3615 m->rotate = 0;
3616 m->paramlen = paramlen;
3617 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003618 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003619
3620 m->next_active = istk->mstk;
3621 istk->mstk = m;
3622
H. Peter Anvine2c80182005-01-15 22:15:51 +00003623 for (l = m->expansion; l; l = l->next) {
3624 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003625
H. Peter Anvine2c80182005-01-15 22:15:51 +00003626 ll = nasm_malloc(sizeof(Line));
3627 ll->finishes = NULL;
3628 ll->next = istk->expansion;
3629 istk->expansion = ll;
3630 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003631
H. Peter Anvine2c80182005-01-15 22:15:51 +00003632 for (t = l->first; t; t = t->next) {
3633 Token *x = t;
3634 if (t->type == TOK_PREPROC_ID &&
3635 t->text[1] == '0' && t->text[2] == '0') {
3636 dont_prepend = -1;
3637 x = label;
3638 if (!x)
3639 continue;
3640 }
3641 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3642 tail = &tt->next;
3643 }
3644 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003645 }
3646
3647 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00003648 * If we had a label, push it on as the first line of
3649 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003650 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003651 if (label) {
3652 if (dont_prepend < 0)
3653 free_tlist(startline);
3654 else {
3655 ll = nasm_malloc(sizeof(Line));
3656 ll->finishes = NULL;
3657 ll->next = istk->expansion;
3658 istk->expansion = ll;
3659 ll->first = startline;
3660 if (!dont_prepend) {
3661 while (label->next)
3662 label = label->next;
3663 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3664 }
3665 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003666 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003667
H. Peter Anvin734b1882002-04-30 21:01:08 +00003668 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003669
H. Peter Anvineba20a72002-04-30 20:53:55 +00003670 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003671}
3672
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003673/*
3674 * Since preprocessor always operate only on the line that didn't
3675 * arrived yet, we should always use ERR_OFFBY1. Also since user
3676 * won't want to see same error twice (preprocessing is done once
3677 * per pass) we will want to show errors only during pass one.
3678 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003679static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003680{
3681 va_list arg;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003682 char buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003683
3684 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003685 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003686 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003687
H. Peter Anvin734b1882002-04-30 21:01:08 +00003688 va_start(arg, fmt);
Ed Beroset19f927a2004-12-15 17:07:03 +00003689 vsnprintf(buff, sizeof(buff), fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003690 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003691
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003692 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003693 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3694 istk->mstk->lineno, buff);
3695 else
3696 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003697}
3698
H. Peter Anvin734b1882002-04-30 21:01:08 +00003699static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003700pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003701 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003702{
H. Peter Anvin99941bf2002-05-14 17:44:03 +00003703 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003704 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003705 istk = nasm_malloc(sizeof(Include));
3706 istk->next = NULL;
3707 istk->conds = NULL;
3708 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003709 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003710 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00003711 istk->fname = NULL;
3712 src_set_fname(nasm_strdup(file));
3713 src_set_linnum(0);
3714 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003715 if (!istk->fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003716 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3717 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003718 defining = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003719 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003720 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003721 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07003722 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003723 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07003724 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003725 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003726 any_extrastdmac = (extrastdmac != NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003727 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003728 evaluate = eval;
3729 pass = apass;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003730}
3731
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003732static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003733{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003734 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003735 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003736
H. Peter Anvine2c80182005-01-15 22:15:51 +00003737 while (1) {
3738 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003739 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00003740 * buffer or from the input file.
3741 */
3742 tline = NULL;
3743 while (istk->expansion && istk->expansion->finishes) {
3744 Line *l = istk->expansion;
3745 if (!l->finishes->name && l->finishes->in_progress > 1) {
3746 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003747
H. Peter Anvine2c80182005-01-15 22:15:51 +00003748 /*
3749 * This is a macro-end marker for a macro with no
3750 * name, which means it's not really a macro at all
3751 * but a %rep block, and the `in_progress' field is
3752 * more than 1, meaning that we still need to
3753 * repeat. (1 means the natural last repetition; 0
3754 * means termination by %exitrep.) We have
3755 * therefore expanded up to the %endrep, and must
3756 * push the whole block on to the expansion buffer
3757 * again. We don't bother to remove the macro-end
3758 * marker: we'd only have to generate another one
3759 * if we did.
3760 */
3761 l->finishes->in_progress--;
3762 for (l = l->finishes->expansion; l; l = l->next) {
3763 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003764
H. Peter Anvine2c80182005-01-15 22:15:51 +00003765 ll = nasm_malloc(sizeof(Line));
3766 ll->next = istk->expansion;
3767 ll->finishes = NULL;
3768 ll->first = NULL;
3769 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003770
H. Peter Anvine2c80182005-01-15 22:15:51 +00003771 for (t = l->first; t; t = t->next) {
3772 if (t->text || t->type == TOK_WHITESPACE) {
3773 tt = *tail =
3774 new_Token(NULL, t->type, t->text, 0);
3775 tail = &tt->next;
3776 }
3777 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003778
H. Peter Anvine2c80182005-01-15 22:15:51 +00003779 istk->expansion = ll;
3780 }
3781 } else {
3782 /*
3783 * Check whether a `%rep' was started and not ended
3784 * within this macro expansion. This can happen and
3785 * should be detected. It's a fatal error because
3786 * I'm too confused to work out how to recover
3787 * sensibly from it.
3788 */
3789 if (defining) {
3790 if (defining->name)
3791 error(ERR_PANIC,
3792 "defining with name in expansion");
3793 else if (istk->mstk->name)
3794 error(ERR_FATAL,
3795 "`%%rep' without `%%endrep' within"
3796 " expansion of macro `%s'",
3797 istk->mstk->name);
3798 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00003799
H. Peter Anvine2c80182005-01-15 22:15:51 +00003800 /*
3801 * FIXME: investigate the relationship at this point between
3802 * istk->mstk and l->finishes
3803 */
3804 {
3805 MMacro *m = istk->mstk;
3806 istk->mstk = m->next_active;
3807 if (m->name) {
3808 /*
3809 * This was a real macro call, not a %rep, and
3810 * therefore the parameter information needs to
3811 * be freed.
3812 */
3813 nasm_free(m->params);
3814 free_tlist(m->iline);
3815 nasm_free(m->paramlen);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003816 l->finishes->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003817 } else
3818 free_mmacro(m);
3819 }
3820 istk->expansion = l->next;
3821 nasm_free(l);
3822 list->downlevel(LIST_MACRO);
3823 }
3824 }
3825 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003826
H. Peter Anvine2c80182005-01-15 22:15:51 +00003827 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003828 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003829 Line *l = istk->expansion;
3830 if (istk->mstk)
3831 istk->mstk->lineno++;
3832 tline = l->first;
3833 istk->expansion = l->next;
3834 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003835 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003836 list->line(LIST_MACRO, p);
3837 nasm_free(p);
3838 break;
3839 }
3840 line = read_line();
3841 if (line) { /* from the current input file */
3842 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00003843 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003844 nasm_free(line);
3845 break;
3846 }
3847 /*
3848 * The current file has ended; work down the istk
3849 */
3850 {
3851 Include *i = istk;
3852 fclose(i->fp);
3853 if (i->conds)
3854 error(ERR_FATAL,
3855 "expected `%%endif' before end of file");
3856 /* only set line and file name if there's a next node */
3857 if (i->next) {
3858 src_set_linnum(i->lineno);
3859 nasm_free(src_set_fname(i->fname));
3860 }
3861 istk = i->next;
3862 list->downlevel(LIST_INCLUDE);
3863 nasm_free(i);
3864 if (!istk)
3865 return NULL;
3866 }
3867 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003868
H. Peter Anvine2c80182005-01-15 22:15:51 +00003869 /*
3870 * We must expand MMacro parameters and MMacro-local labels
3871 * _before_ we plunge into directive processing, to cope
3872 * with things like `%define something %1' such as STRUC
3873 * uses. Unless we're _defining_ a MMacro, in which case
3874 * those tokens should be left alone to go into the
3875 * definition; and unless we're in a non-emitting
3876 * condition, in which case we don't want to meddle with
3877 * anything.
3878 */
3879 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
3880 tline = expand_mmac_params(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003881
H. Peter Anvine2c80182005-01-15 22:15:51 +00003882 /*
3883 * Check the line to see if it's a preprocessor directive.
3884 */
3885 if (do_directive(tline) == DIRECTIVE_FOUND) {
3886 continue;
3887 } else if (defining) {
3888 /*
3889 * We're defining a multi-line macro. We emit nothing
3890 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00003891 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003892 */
3893 Line *l = nasm_malloc(sizeof(Line));
3894 l->next = defining->expansion;
3895 l->first = tline;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003896 l->finishes = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003897 defining->expansion = l;
3898 continue;
3899 } else if (istk->conds && !emitting(istk->conds->state)) {
3900 /*
3901 * We're in a non-emitting branch of a condition block.
3902 * Emit nothing at all, not even a blank line: when we
3903 * emerge from the condition we'll give a line-number
3904 * directive so we keep our place correctly.
3905 */
3906 free_tlist(tline);
3907 continue;
3908 } else if (istk->mstk && !istk->mstk->in_progress) {
3909 /*
3910 * We're in a %rep block which has been terminated, so
3911 * we're walking through to the %endrep without
3912 * emitting anything. Emit nothing at all, not even a
3913 * blank line: when we emerge from the %rep block we'll
3914 * give a line-number directive so we keep our place
3915 * correctly.
3916 */
3917 free_tlist(tline);
3918 continue;
3919 } else {
3920 tline = expand_smacro(tline);
3921 if (!expand_mmacro(tline)) {
3922 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003923 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003924 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003925 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003926 free_tlist(tline);
3927 break;
3928 } else {
3929 continue; /* expand_mmacro calls free_tlist */
3930 }
3931 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003932 }
3933
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003934 return line;
3935}
3936
H. Peter Anvine2c80182005-01-15 22:15:51 +00003937static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003938{
H. Peter Anvine2c80182005-01-15 22:15:51 +00003939 if (defining) {
3940 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
3941 defining->name);
3942 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003943 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003944 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003945 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07003946 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00003947 while (istk) {
3948 Include *i = istk;
3949 istk = istk->next;
3950 fclose(i->fp);
3951 nasm_free(i->fname);
3952 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003953 }
3954 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003955 ctx_pop();
3956 if (pass == 0) {
3957 free_llist(predef);
3958 delete_Blocks();
3959 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003960}
3961
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003962void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003963{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003964 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07003965
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003966 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07003967 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003968 i->next = NULL;
3969
H. Peter Anvine2c80182005-01-15 22:15:51 +00003970 if (ipath != NULL) {
3971 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003972 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003973 j = j->next;
3974 j->next = i;
3975 } else {
3976 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003977 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003978}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003979
3980/*
3981 * added by alexfru:
3982 *
3983 * This function is used to "export" the include paths, e.g.
3984 * the paths specified in the '-I' command switch.
3985 * The need for such exporting is due to the 'incbin' directive,
3986 * which includes raw binary files (unlike '%include', which
3987 * includes text source files). It would be real nice to be
3988 * able to specify paths to search for incbin'ned files also.
3989 * So, this is a simple workaround.
3990 *
3991 * The function use is simple:
3992 *
3993 * The 1st call (with NULL argument) returns a pointer to the 1st path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003994 * (char** type) or NULL if none include paths available.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003995 *
3996 * All subsequent calls take as argument the value returned by this
3997 * function last. The return value is either the next path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003998 * (char** type) or NULL if the end of the paths list is reached.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003999 *
4000 * It is maybe not the best way to do things, but I didn't want
4001 * to export too much, just one or two functions and no types or
4002 * variables exported.
4003 *
4004 * Can't say I like the current situation with e.g. this path list either,
4005 * it seems to be never deallocated after creation...
4006 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004007char **pp_get_include_path_ptr(char **pPrevPath)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004008{
4009/* This macro returns offset of a member of a structure */
4010#define GetMemberOffset(StructType,MemberName)\
4011 ((size_t)&((StructType*)0)->MemberName)
4012 IncPath *i;
4013
H. Peter Anvine2c80182005-01-15 22:15:51 +00004014 if (pPrevPath == NULL) {
4015 if (ipath != NULL)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004016 return &ipath->path;
4017 else
4018 return NULL;
4019 }
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004020 i = (IncPath *) ((char *)pPrevPath - GetMemberOffset(IncPath, path));
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004021 i = i->next;
4022 if (i != NULL)
4023 return &i->path;
4024 else
4025 return NULL;
4026#undef GetMemberOffset
4027}
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004028
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004029void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004030{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004031 Token *inc, *space, *name;
4032 Line *l;
4033
H. Peter Anvin734b1882002-04-30 21:01:08 +00004034 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4035 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4036 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004037
4038 l = nasm_malloc(sizeof(Line));
4039 l->next = predef;
4040 l->first = inc;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004041 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004042 predef = l;
4043}
4044
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004045void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004046{
4047 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004048 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004049 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004050
4051 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004052 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4053 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004054 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004055 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004056 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004057 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004058 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004059
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004060 l = nasm_malloc(sizeof(Line));
4061 l->next = predef;
4062 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004063 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004064 predef = l;
4065}
4066
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004067void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004068{
4069 Token *def, *space;
4070 Line *l;
4071
H. Peter Anvin734b1882002-04-30 21:01:08 +00004072 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4073 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004074 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004075
4076 l = nasm_malloc(sizeof(Line));
4077 l->next = predef;
4078 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004079 l->finishes = false;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004080 predef = l;
4081}
4082
Keith Kaniosb7a89542007-04-12 02:40:54 +00004083/*
4084 * Added by Keith Kanios:
4085 *
4086 * This function is used to assist with "runtime" preprocessor
4087 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4088 *
4089 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4090 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4091 */
4092
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004093void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004094{
4095 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004096
Keith Kaniosb7a89542007-04-12 02:40:54 +00004097 def = tokenize(definition);
4098 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4099 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004100
Keith Kaniosb7a89542007-04-12 02:40:54 +00004101}
4102
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004103void pp_extra_stdmac(const char **macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004104{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004105 extrastdmac = macros;
4106}
4107
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004108static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004109{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004110 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004111 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004112 tok->text = nasm_strdup(numbuf);
4113 tok->type = TOK_NUMBER;
4114}
4115
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004116Preproc nasmpp = {
4117 pp_reset,
4118 pp_getline,
4119 pp_cleanup
4120};