blob: d45881b8eb745f9bcaf16b05cf3b628ee2790d6e [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 Anvin6c81f0a2008-05-25 21:46:17 -0700158 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
159 TOK_INTERNAL_STRING,
160 TOK_PREPROC_Q, TOK_PREPROC_QQ,
161 TOK_SMAC_PARAM /* MUST BE LAST IN THE LIST!!! */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000162};
163
H. Peter Anvine2c80182005-01-15 22:15:51 +0000164struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000165 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000166 char *text;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000167 SMacro *mac; /* associated macro for TOK_SMAC_END */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000168 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000169};
170
171/*
172 * Multi-line macro definitions are stored as a linked list of
173 * these, which is essentially a container to allow several linked
174 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700175 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000176 * Note that in this module, linked lists are treated as stacks
177 * wherever possible. For this reason, Lines are _pushed_ on to the
178 * `expansion' field in MMacro structures, so that the linked list,
179 * if walked, would give the macro lines in reverse order; this
180 * means that we can walk the list when expanding a macro, and thus
181 * push the lines on to the `expansion' field in _istk_ in reverse
182 * order (so that when popped back off they are in the right
183 * order). It may seem cockeyed, and it relies on my design having
184 * an even number of steps in, but it works...
185 *
186 * Some of these structures, rather than being actual lines, are
187 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000188 * This is for use in the cycle-tracking and %rep-handling code.
189 * Such structures have `finishes' non-NULL, and `first' NULL. All
190 * others have `finishes' NULL, but `first' may still be NULL if
191 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000192 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000193struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000194 Line *next;
195 MMacro *finishes;
196 Token *first;
197};
198
199/*
200 * To handle an arbitrary level of file inclusion, we maintain a
201 * stack (ie linked list) of these things.
202 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000203struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000204 Include *next;
205 FILE *fp;
206 Cond *conds;
207 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000208 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000209 int lineno, lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000210 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000211};
212
213/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000214 * Include search path. This is simply a list of strings which get
215 * prepended, in turn, to the name of an include file, in an
216 * attempt to find the file if it's not in the current directory.
217 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000218struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000219 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000220 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000221};
222
223/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000224 * Conditional assembly: we maintain a separate stack of these for
225 * each level of file inclusion. (The only reason we keep the
226 * stacks separate is to ensure that a stray `%endif' in a file
227 * included from within the true branch of a `%if' won't terminate
228 * it and cause confusion: instead, rightly, it'll cause an error.)
229 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000230struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000231 Cond *next;
232 int state;
233};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000234enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000235 /*
236 * These states are for use just after %if or %elif: IF_TRUE
237 * means the condition has evaluated to truth so we are
238 * currently emitting, whereas IF_FALSE means we are not
239 * currently emitting but will start doing so if a %else comes
240 * up. In these states, all directives are admissible: %elif,
241 * %else and %endif. (And of course %if.)
242 */
243 COND_IF_TRUE, COND_IF_FALSE,
244 /*
245 * These states come up after a %else: ELSE_TRUE means we're
246 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
247 * any %elif or %else will cause an error.
248 */
249 COND_ELSE_TRUE, COND_ELSE_FALSE,
250 /*
251 * This state means that we're not emitting now, and also that
252 * nothing until %endif will be emitted at all. It's for use in
253 * two circumstances: (i) when we've had our moment of emission
254 * and have now started seeing %elifs, and (ii) when the
255 * condition construct in question is contained within a
256 * non-emitting branch of a larger condition construct.
257 */
258 COND_NEVER
259};
260#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
261
H. Peter Anvin70653092007-10-19 14:42:29 -0700262/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000263 * These defines are used as the possible return values for do_directive
264 */
265#define NO_DIRECTIVE_FOUND 0
266#define DIRECTIVE_FOUND 1
267
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000268/*
269 * Condition codes. Note that we use c_ prefix not C_ because C_ is
270 * used in nasm.h for the "real" condition codes. At _this_ level,
271 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
272 * ones, so we need a different enum...
273 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700274static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000275 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
276 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000277 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000278};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700279enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000280 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
281 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 -0700282 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
283 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000284};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700285static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000286 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
287 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 +0000288 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000289};
290
H. Peter Anvin76690a12002-04-30 20:52:49 +0000291/*
292 * Directive names.
293 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000294/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000295static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000296{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000297 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000298}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000299
300/* For TASM compatibility we need to be able to recognise TASM compatible
301 * conditional compilation directives. Using the NASM pre-processor does
302 * not work, so we look for them specifically from the following list and
303 * then jam in the equivalent NASM directive into the input stream.
304 */
305
H. Peter Anvine2c80182005-01-15 22:15:51 +0000306enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000307 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
308 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
309};
310
H. Peter Anvin476d2862007-10-02 22:04:15 -0700311static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000312 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
313 "ifndef", "include", "local"
314};
315
316static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000317static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000318static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800319static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000320
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000321static Context *cstk;
322static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000323static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000324
H. Peter Anvine2c80182005-01-15 22:15:51 +0000325static efunc _error; /* Pointer to client-provided error reporting function */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000326static evalfunc evaluate;
327
H. Peter Anvine2c80182005-01-15 22:15:51 +0000328static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000329
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700330static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000331
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000332static Line *predef = NULL;
333
334static ListGen *list;
335
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000336/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000337 * The current set of multi-line macros we have defined.
338 */
H. Peter Anvin97a23472007-09-16 17:57:25 -0700339static struct hash_table *mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000340
341/*
342 * The current set of single-line macros we have defined.
343 */
H. Peter Anvin97a23472007-09-16 17:57:25 -0700344static struct hash_table *smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000345
346/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000347 * The multi-line macro we are currently defining, or the %rep
348 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000349 */
350static MMacro *defining;
351
352/*
353 * The number of macro parameters to allocate space for at a time.
354 */
355#define PARAM_DELTA 16
356
357/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700358 * The standard macro set: defined in macros.c in the array nasm_stdmac.
359 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000360 */
H. Peter Anvin188ce762008-02-16 13:58:45 -0800361static const char * const *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000362
363/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000364 * The extra standard macros that come from the object format, if
365 * any.
366 */
H. Peter Anvin188ce762008-02-16 13:58:45 -0800367static const char * const *extrastdmac = NULL;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700368bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000369
370/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000371 * Tokens are allocated in blocks to improve speed
372 */
373#define TOKEN_BLOCKSIZE 4096
374static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000375struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000376 Blocks *next;
377 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000378};
379
380static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000381
382/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000383 * Forward declarations.
384 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000385static Token *expand_mmac_params(Token * tline);
386static Token *expand_smacro(Token * tline);
387static Token *expand_id(Token * tline);
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700388static Context *get_ctx(char *name, bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700389static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000390static void error(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000391static void *new_Block(size_t size);
392static void delete_Blocks(void);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000393static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000394static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000395
396/*
397 * Macros for safe checking of token pointers, avoid *(NULL)
398 */
399#define tok_type_(x,t) ((x) && (x)->type == (t))
400#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
401#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
402#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000403
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000404/* Handle TASM specific directives, which do not contain a % in
405 * front of them. We do it here because I could not find any other
406 * place to do it for the moment, and it is a hack (ideally it would
407 * be nice to be able to use the NASM pre-processor to do it).
408 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000409static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000410{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000411 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000412 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000413
414 /* Skip whitespace */
415 while (isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000416 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000417
418 /* Binary search for the directive name */
419 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000420 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000421 len = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000422 while (!isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000423 len++;
424 if (len) {
425 oldchar = p[len];
426 p[len] = 0;
427 while (j - i > 1) {
428 k = (j + i) / 2;
429 m = nasm_stricmp(p, tasm_directives[k]);
430 if (m == 0) {
431 /* We have found a directive, so jam a % in front of it
432 * so that NASM will then recognise it as one if it's own.
433 */
434 p[len] = oldchar;
435 len = strlen(p);
436 oldline = line;
437 line = nasm_malloc(len + 2);
438 line[0] = '%';
439 if (k == TM_IFDIFI) {
440 /* NASM does not recognise IFDIFI, so we convert it to
441 * %ifdef BOGUS. This is not used in NASM comaptible
442 * code, but does need to parse for the TASM macro
443 * package.
444 */
445 strcpy(line + 1, "ifdef BOGUS");
446 } else {
447 memcpy(line + 1, p, len + 1);
448 }
449 nasm_free(oldline);
450 return line;
451 } else if (m < 0) {
452 j = k;
453 } else
454 i = k;
455 }
456 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000457 }
458 return line;
459}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000460
H. Peter Anvin76690a12002-04-30 20:52:49 +0000461/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000462 * The pre-preprocessing stage... This function translates line
463 * number indications as they emerge from GNU cpp (`# lineno "file"
464 * flags') into NASM preprocessor line number indications (`%line
465 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000466 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000467static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000468{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000469 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000470 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000471
H. Peter Anvine2c80182005-01-15 22:15:51 +0000472 if (line[0] == '#' && line[1] == ' ') {
473 oldline = line;
474 fname = oldline + 2;
475 lineno = atoi(fname);
476 fname += strspn(fname, "0123456789 ");
477 if (*fname == '"')
478 fname++;
479 fnlen = strcspn(fname, "\"");
480 line = nasm_malloc(20 + fnlen);
481 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
482 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000483 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000484 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000485 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000486 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000487}
488
489/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000490 * Free a linked list of tokens.
491 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000492static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000493{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000494 while (list) {
495 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000496 }
497}
498
499/*
500 * Free a linked list of lines.
501 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000502static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000503{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000504 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000505 while (list) {
506 l = list;
507 list = list->next;
508 free_tlist(l->first);
509 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000510 }
511}
512
513/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000514 * Free an MMacro
515 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000516static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000517{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000518 nasm_free(m->name);
519 free_tlist(m->dlist);
520 nasm_free(m->defaults);
521 free_llist(m->expansion);
522 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000523}
524
525/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700526 * Free all currently defined macros, and free the hash tables
527 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700528static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700529{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700530 SMacro *s;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700531 const char *key;
532 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700533
H. Peter Anvin072771e2008-05-22 13:17:51 -0700534 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700535 nasm_free((void *)key);
536 while (s) {
537 SMacro *ns = s->next;
538 nasm_free(s->name);
539 free_tlist(s->expansion);
540 nasm_free(s);
541 s = ns;
542 }
543 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700544 hash_free(smt);
545}
546
547static void free_mmacro_table(struct hash_table *mmt)
548{
549 MMacro *m;
550 const char *key;
551 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700552
553 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700554 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700555 nasm_free((void *)key);
556 while (m) {
557 MMacro *nm = m->next;
558 free_mmacro(m);
559 m = nm;
560 }
561 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700562 hash_free(mmt);
563}
564
565static void free_macros(void)
566{
567 free_smacro_table(smacros);
568 free_mmacro_table(mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700569}
570
571/*
572 * Initialize the hash tables
573 */
574static void init_macros(void)
575{
H. Peter Anvin072771e2008-05-22 13:17:51 -0700576 smacros = hash_init(HASH_LARGE);
577 mmacros = hash_init(HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700578}
579
580/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000581 * Pop the context stack.
582 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000583static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000584{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000585 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000586
587 cstk = cstk->next;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700588 free_smacro_table(c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000589 nasm_free(c->name);
590 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000591}
592
H. Peter Anvin072771e2008-05-22 13:17:51 -0700593/*
594 * Search for a key in the hash index; adding it if necessary
595 * (in which case we initialize the data pointer to NULL.)
596 */
597static void **
598hash_findi_add(struct hash_table *hash, const char *str)
599{
600 struct hash_insert hi;
601 void **r;
602 char *strx;
603
604 r = hash_findi(hash, str, &hi);
605 if (r)
606 return r;
607
608 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
609 return hash_add(&hi, strx, NULL);
610}
611
612/*
613 * Like hash_findi, but returns the data element rather than a pointer
614 * to it. Used only when not adding a new element, hence no third
615 * argument.
616 */
617static void *
618hash_findix(struct hash_table *hash, const char *str)
619{
620 void **p;
621
622 p = hash_findi(hash, str, NULL);
623 return p ? *p : NULL;
624}
625
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000626#define BUF_DELTA 512
627/*
628 * Read a line from the top file in istk, handling multiple CR/LFs
629 * at the end of the line read, and handling spurious ^Zs. Will
630 * return lines from the standard macro set if this has not already
631 * been done.
632 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000633static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000634{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000635 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000636 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000637
H. Peter Anvine2c80182005-01-15 22:15:51 +0000638 if (stdmacpos) {
639 if (*stdmacpos) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000640 char *ret = nasm_strdup(*stdmacpos++);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000641 if (!*stdmacpos && any_extrastdmac) {
642 stdmacpos = extrastdmac;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700643 any_extrastdmac = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000644 return ret;
645 }
646 /*
647 * Nasty hack: here we push the contents of `predef' on
648 * to the top-level expansion stack, since this is the
649 * most convenient way to implement the pre-include and
650 * pre-define features.
651 */
652 if (!*stdmacpos) {
653 Line *pd, *l;
654 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000655
H. Peter Anvine2c80182005-01-15 22:15:51 +0000656 for (pd = predef; pd; pd = pd->next) {
657 head = NULL;
658 tail = &head;
659 for (t = pd->first; t; t = t->next) {
660 *tail = new_Token(NULL, t->type, t->text, 0);
661 tail = &(*tail)->next;
662 }
663 l = nasm_malloc(sizeof(Line));
664 l->next = istk->expansion;
665 l->first = head;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700666 l->finishes = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000667 istk->expansion = l;
668 }
669 }
670 return ret;
671 } else {
672 stdmacpos = NULL;
673 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000674 }
675
676 bufsize = BUF_DELTA;
677 buffer = nasm_malloc(BUF_DELTA);
678 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000679 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000680 while (1) {
681 q = fgets(p, bufsize - (p - buffer), istk->fp);
682 if (!q)
683 break;
684 p += strlen(p);
685 if (p > buffer && p[-1] == '\n') {
686 /* Convert backslash-CRLF line continuation sequences into
687 nothing at all (for DOS and Windows) */
688 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
689 p -= 3;
690 *p = 0;
691 continued_count++;
692 }
693 /* Also convert backslash-LF line continuation sequences into
694 nothing at all (for Unix) */
695 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
696 p -= 2;
697 *p = 0;
698 continued_count++;
699 } else {
700 break;
701 }
702 }
703 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000704 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000705 bufsize += BUF_DELTA;
706 buffer = nasm_realloc(buffer, bufsize);
707 p = buffer + offset; /* prevent stale-pointer problems */
708 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000709 }
710
H. Peter Anvine2c80182005-01-15 22:15:51 +0000711 if (!q && p == buffer) {
712 nasm_free(buffer);
713 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000714 }
715
H. Peter Anvine2c80182005-01-15 22:15:51 +0000716 src_set_linnum(src_get_linnum() + istk->lineinc +
717 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000718
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000719 /*
720 * Play safe: remove CRs as well as LFs, if any of either are
721 * present at the end of the line.
722 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000723 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000724 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000725
726 /*
727 * Handle spurious ^Z, which may be inserted into source files
728 * by some file transfer utilities.
729 */
730 buffer[strcspn(buffer, "\032")] = '\0';
731
H. Peter Anvin734b1882002-04-30 21:01:08 +0000732 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000733
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000734 return buffer;
735}
736
737/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000738 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000739 * don't need to parse the value out of e.g. numeric tokens: we
740 * simply split one string into many.
741 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000742static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000743{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000744 char *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000745 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000746 Token *list = NULL;
747 Token *t, **tail = &list;
748
H. Peter Anvine2c80182005-01-15 22:15:51 +0000749 while (*line) {
750 p = line;
751 if (*p == '%') {
752 p++;
753 if (isdigit(*p) ||
754 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
755 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
756 do {
757 p++;
758 }
759 while (isdigit(*p));
760 type = TOK_PREPROC_ID;
761 } else if (*p == '{') {
762 p++;
763 while (*p && *p != '}') {
764 p[-1] = *p;
765 p++;
766 }
767 p[-1] = '\0';
768 if (*p)
769 p++;
770 type = TOK_PREPROC_ID;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700771 } else if (*p == '?') {
772 type = TOK_PREPROC_Q; /* %? */
773 p++;
774 if (*p == '?') {
775 type = TOK_PREPROC_QQ; /* %?? */
776 p++;
777 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000778 } else if (isidchar(*p) ||
779 ((*p == '!' || *p == '%' || *p == '$') &&
780 isidchar(p[1]))) {
781 do {
782 p++;
783 }
784 while (isidchar(*p));
785 type = TOK_PREPROC_ID;
786 } else {
787 type = TOK_OTHER;
788 if (*p == '%')
789 p++;
790 }
791 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
792 type = TOK_ID;
793 p++;
794 while (*p && isidchar(*p))
795 p++;
796 } else if (*p == '\'' || *p == '"') {
797 /*
798 * A string token.
799 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000800 char c = *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000801 p++;
802 type = TOK_STRING;
803 while (*p && *p != c)
804 p++;
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000805
H. Peter Anvine2c80182005-01-15 22:15:51 +0000806 if (*p) {
807 p++;
808 } else {
809 error(ERR_WARNING, "unterminated string");
810 /* Handling unterminated strings by UNV */
811 /* type = -1; */
812 }
813 } else if (isnumstart(*p)) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700814 bool is_hex = false;
815 bool is_float = false;
816 bool has_e = false;
817 char c, *r;
818
H. Peter Anvine2c80182005-01-15 22:15:51 +0000819 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700820 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000821 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700822
823 if (*p == '$') {
824 p++;
825 is_hex = true;
826 }
827
828 for (;;) {
829 c = *p++;
830
831 if (!is_hex && (c == 'e' || c == 'E')) {
832 has_e = true;
833 if (*p == '+' || *p == '-') {
834 /* e can only be followed by +/- if it is either a
835 prefixed hex number or a floating-point number */
836 p++;
837 is_float = true;
838 }
839 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
840 is_hex = true;
841 } else if (c == 'P' || c == 'p') {
842 is_float = true;
843 if (*p == '+' || *p == '-')
844 p++;
845 } else if (isnumchar(c) || c == '_')
846 ; /* just advance */
847 else if (c == '.') {
848 /* we need to deal with consequences of the legacy
849 parser, like "1.nolist" being two tokens
850 (TOK_NUMBER, TOK_ID) here; at least give it
851 a shot for now. In the future, we probably need
852 a flex-based scanner with proper pattern matching
853 to do it as well as it can be done. Nothing in
854 the world is going to help the person who wants
855 0x123.p16 interpreted as two tokens, though. */
856 r = p;
857 while (*r == '_')
858 r++;
859
860 if (isdigit(*r) || (is_hex && isxdigit(*r)) ||
861 (!is_hex && (*r == 'e' || *r == 'E')) ||
862 (*r == 'p' || *r == 'P')) {
863 p = r;
864 is_float = true;
865 } else
866 break; /* Terminate the token */
867 } else
868 break;
869 }
870 p--; /* Point to first character beyond number */
871
872 if (has_e && !is_hex) {
873 /* 1e13 is floating-point, but 1e13h is not */
874 is_float = true;
875 }
876
877 type = is_float ? TOK_FLOAT : TOK_NUMBER;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000878 } else if (isspace(*p)) {
879 type = TOK_WHITESPACE;
880 p++;
881 while (*p && isspace(*p))
882 p++;
883 /*
884 * Whitespace just before end-of-line is discarded by
885 * pretending it's a comment; whitespace just before a
886 * comment gets lumped into the comment.
887 */
888 if (!*p || *p == ';') {
889 type = TOK_COMMENT;
890 while (*p)
891 p++;
892 }
893 } else if (*p == ';') {
894 type = TOK_COMMENT;
895 while (*p)
896 p++;
897 } else {
898 /*
899 * Anything else is an operator of some kind. We check
900 * for all the double-character operators (>>, <<, //,
901 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000902 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000903 */
904 type = TOK_OTHER;
905 if ((p[0] == '>' && p[1] == '>') ||
906 (p[0] == '<' && p[1] == '<') ||
907 (p[0] == '/' && p[1] == '/') ||
908 (p[0] == '<' && p[1] == '=') ||
909 (p[0] == '>' && p[1] == '=') ||
910 (p[0] == '=' && p[1] == '=') ||
911 (p[0] == '!' && p[1] == '=') ||
912 (p[0] == '<' && p[1] == '>') ||
913 (p[0] == '&' && p[1] == '&') ||
914 (p[0] == '|' && p[1] == '|') ||
915 (p[0] == '^' && p[1] == '^')) {
916 p++;
917 }
918 p++;
919 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000920
H. Peter Anvine2c80182005-01-15 22:15:51 +0000921 /* Handling unterminated string by UNV */
922 /*if (type == -1)
923 {
924 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
925 t->text[p-line] = *line;
926 tail = &t->next;
927 }
928 else */
929 if (type != TOK_COMMENT) {
930 *tail = t = new_Token(NULL, type, line, p - line);
931 tail = &t->next;
932 }
933 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000934 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000935 return list;
936}
937
H. Peter Anvince616072002-04-30 21:02:23 +0000938/*
939 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -0700940 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +0000941 * deleted only all at once by the delete_Blocks function.
942 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000943static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +0000944{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000945 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000947 /* first, get to the end of the linked list */
948 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000949 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000950 /* now allocate the requested chunk */
951 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000952
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000953 /* now allocate a new block for the next request */
954 b->next = nasm_malloc(sizeof(Blocks));
955 /* and initialize the contents of the new block */
956 b->next->next = NULL;
957 b->next->chunk = NULL;
958 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000959}
960
961/*
962 * this function deletes all managed blocks of memory
963 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000964static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +0000965{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +0000967
H. Peter Anvin70653092007-10-19 14:42:29 -0700968 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000969 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -0700970 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000971 * free it.
972 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973 while (b) {
974 if (b->chunk)
975 nasm_free(b->chunk);
976 a = b;
977 b = b->next;
978 if (a != &blocks)
979 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000980 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000981}
H. Peter Anvin734b1882002-04-30 21:01:08 +0000982
983/*
H. Peter Anvin70653092007-10-19 14:42:29 -0700984 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +0000985 * back to the caller. It sets the type and text elements, and
986 * also the mac and next elements to NULL.
987 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000988static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +0000989{
990 Token *t;
991 int i;
992
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 if (freeTokens == NULL) {
994 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
995 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
996 freeTokens[i].next = &freeTokens[i + 1];
997 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000998 }
999 t = freeTokens;
1000 freeTokens = t->next;
1001 t->next = next;
1002 t->mac = NULL;
1003 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 if (type == TOK_WHITESPACE || text == NULL) {
1005 t->text = NULL;
1006 } else {
1007 if (txtlen == 0)
1008 txtlen = strlen(text);
1009 t->text = nasm_malloc(1 + txtlen);
1010 strncpy(t->text, text, txtlen);
1011 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001012 }
1013 return t;
1014}
1015
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001017{
1018 Token *next = t->next;
1019 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001020 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001021 freeTokens = t;
1022 return next;
1023}
1024
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001025/*
1026 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001027 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1028 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001029 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001030static char *detoken(Token * tlist, int expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001031{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001032 Token *t;
1033 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001034 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001035 const char *q;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001036
1037 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001038 for (t = tlist; t; t = t->next) {
1039 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001040 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001041 nasm_free(t->text);
1042 if (p)
1043 t->text = nasm_strdup(p);
1044 else
1045 t->text = NULL;
1046 }
1047 /* Expand local macros here and not during preprocessing */
1048 if (expand_locals &&
1049 t->type == TOK_PREPROC_ID && t->text &&
1050 t->text[0] == '%' && t->text[1] == '$') {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001051 Context *ctx = get_ctx(t->text, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001052 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001053 char buffer[40];
1054 char *p, *q = t->text + 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001055
H. Peter Anvine2c80182005-01-15 22:15:51 +00001056 q += strspn(q, "$");
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001057 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001058 p = nasm_strcat(buffer, q);
1059 nasm_free(t->text);
1060 t->text = p;
1061 }
1062 }
1063 if (t->type == TOK_WHITESPACE) {
1064 len++;
1065 } else if (t->text) {
1066 len += strlen(t->text);
1067 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001068 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001069 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001070 for (t = tlist; t; t = t->next) {
1071 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001072 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001073 } else if (t->text) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001074 q = t->text;
1075 while (*q)
1076 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001077 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001078 }
1079 *p = '\0';
1080 return line;
1081}
1082
1083/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001084 * A scanner, suitable for use by the expression evaluator, which
1085 * operates on a line of Tokens. Expects a pointer to a pointer to
1086 * the first token in the line to be passed in as its private_data
1087 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001088 *
1089 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001090 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001091static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001092{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001093 Token **tlineptr = private_data;
1094 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001095 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001096
H. Peter Anvine2c80182005-01-15 22:15:51 +00001097 do {
1098 tline = *tlineptr;
1099 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001100 }
1101 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001102 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001103
1104 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001105 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001106
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001107 tokval->t_charptr = tline->text;
1108
H. Peter Anvin76690a12002-04-30 20:52:49 +00001109 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001110 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001111 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001112 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001113
H. Peter Anvine2c80182005-01-15 22:15:51 +00001114 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001115 p = tokval->t_charptr = tline->text;
1116 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001117 tokval->t_charptr++;
1118 return tokval->t_type = TOKEN_ID;
1119 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001120
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001121 for (r = p, s = ourcopy; *r; r++) {
Philipp Thomas76ec8e72008-05-21 08:53:21 -07001122 if (r >= p+MAX_KEYWORD)
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001123 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1124 *s++ = tolower(*r);
1125 }
1126 *s = '\0';
1127 /* right, so we have an identifier sitting in temp storage. now,
1128 * is it actually a register or instruction name, or what? */
1129 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001130 }
1131
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 if (tline->type == TOK_NUMBER) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001133 bool rn_error;
1134 tokval->t_integer = readnum(tline->text, &rn_error);
1135 if (rn_error)
1136 return tokval->t_type = TOKEN_ERRNUM; /* some malformation occurred */
1137 tokval->t_charptr = tline->text;
1138 return tokval->t_type = TOKEN_NUM;
1139 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001140
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001141 if (tline->type == TOK_FLOAT) {
1142 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001143 }
1144
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145 if (tline->type == TOK_STRING) {
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001146 bool rn_warn;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001147 char q, *r;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001148 int l;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001149
H. Peter Anvine2c80182005-01-15 22:15:51 +00001150 r = tline->text;
1151 q = *r++;
1152 l = strlen(r);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001153
H. Peter Anvine2c80182005-01-15 22:15:51 +00001154 if (l == 0 || r[l - 1] != q)
1155 return tokval->t_type = TOKEN_ERRNUM;
1156 tokval->t_integer = readstrnum(r, l - 1, &rn_warn);
1157 if (rn_warn)
1158 error(ERR_WARNING | ERR_PASS1, "character constant too long");
1159 tokval->t_charptr = NULL;
1160 return tokval->t_type = TOKEN_NUM;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001161 }
1162
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163 if (tline->type == TOK_OTHER) {
1164 if (!strcmp(tline->text, "<<"))
1165 return tokval->t_type = TOKEN_SHL;
1166 if (!strcmp(tline->text, ">>"))
1167 return tokval->t_type = TOKEN_SHR;
1168 if (!strcmp(tline->text, "//"))
1169 return tokval->t_type = TOKEN_SDIV;
1170 if (!strcmp(tline->text, "%%"))
1171 return tokval->t_type = TOKEN_SMOD;
1172 if (!strcmp(tline->text, "=="))
1173 return tokval->t_type = TOKEN_EQ;
1174 if (!strcmp(tline->text, "<>"))
1175 return tokval->t_type = TOKEN_NE;
1176 if (!strcmp(tline->text, "!="))
1177 return tokval->t_type = TOKEN_NE;
1178 if (!strcmp(tline->text, "<="))
1179 return tokval->t_type = TOKEN_LE;
1180 if (!strcmp(tline->text, ">="))
1181 return tokval->t_type = TOKEN_GE;
1182 if (!strcmp(tline->text, "&&"))
1183 return tokval->t_type = TOKEN_DBL_AND;
1184 if (!strcmp(tline->text, "^^"))
1185 return tokval->t_type = TOKEN_DBL_XOR;
1186 if (!strcmp(tline->text, "||"))
1187 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001188 }
1189
1190 /*
1191 * We have no other options: just return the first character of
1192 * the token text.
1193 */
1194 return tokval->t_type = tline->text[0];
1195}
1196
1197/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001198 * Compare a string to the name of an existing macro; this is a
1199 * simple wrapper which calls either strcmp or nasm_stricmp
1200 * depending on the value of the `casesense' parameter.
1201 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001202static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001203{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001204 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001205}
1206
1207/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001208 * Return the Context structure associated with a %$ token. Return
1209 * NULL, having _already_ reported an error condition, if the
1210 * context stack isn't deep enough for the supplied number of $
1211 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001212 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001213 * also scanned for such smacro, until it is found; if not -
1214 * only the context that directly results from the number of $'s
1215 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001216 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001217static Context *get_ctx(char *name, bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001218{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001219 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001220 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001221 int i;
1222
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001223 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001224 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001225
H. Peter Anvine2c80182005-01-15 22:15:51 +00001226 if (!cstk) {
1227 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1228 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001229 }
1230
H. Peter Anvine2c80182005-01-15 22:15:51 +00001231 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1232 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001233/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001234 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001235 if (!ctx) {
1236 error(ERR_NONFATAL, "`%s': context stack is only"
1237 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1238 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001239 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001240 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001241 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001242
H. Peter Anvine2c80182005-01-15 22:15:51 +00001243 do {
1244 /* Search for this smacro in found context */
H. Peter Anvin072771e2008-05-22 13:17:51 -07001245 m = hash_findix(ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001246 while (m) {
1247 if (!mstrcmp(m->name, name, m->casesense))
1248 return ctx;
1249 m = m->next;
1250 }
1251 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001252 }
1253 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001254 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001255}
1256
1257/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001258 * Open an include file. This routine must always return a valid
1259 * file pointer if it returns - it's responsible for throwing an
1260 * ERR_FATAL and bombing out completely if not. It should also try
1261 * the include path one by one until it finds the file or reaches
1262 * the end of the path.
1263 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001264static FILE *inc_fopen(char *file)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001265{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001266 FILE *fp;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001267 char *prefix = "", *combine;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001268 IncPath *ip = ipath;
H. Peter Anvin620515a2002-04-30 20:57:38 +00001269 static int namelen = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001270 int len = strlen(file);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001271
H. Peter Anvine2c80182005-01-15 22:15:51 +00001272 while (1) {
1273 combine = nasm_malloc(strlen(prefix) + len + 1);
1274 strcpy(combine, prefix);
1275 strcat(combine, file);
1276 fp = fopen(combine, "r");
1277 if (pass == 0 && fp) {
1278 namelen += strlen(combine) + 1;
1279 if (namelen > 62) {
1280 printf(" \\\n ");
1281 namelen = 2;
1282 }
1283 printf(" %s", combine);
1284 }
1285 nasm_free(combine);
1286 if (fp)
1287 return fp;
1288 if (!ip)
1289 break;
1290 prefix = ip->path;
1291 ip = ip->next;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001292
1293 if (!prefix) {
1294 /* -MG given and file not found */
1295 if (pass == 0) {
1296 namelen += strlen(file) + 1;
1297 if (namelen > 62) {
1298 printf(" \\\n ");
1299 namelen = 2;
1300 }
1301 printf(" %s", file);
1302 }
1303 return NULL;
1304 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001305 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001306
H. Peter Anvin734b1882002-04-30 21:01:08 +00001307 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001308 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001309}
1310
1311/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001312 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001313 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001314 * return true if _any_ single-line macro of that name is defined.
1315 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001316 * `nparam' or no parameters is defined.
1317 *
1318 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001319 * defined, or nparam is -1, the address of the definition structure
1320 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001321 * is NULL, no action will be taken regarding its contents, and no
1322 * error will occur.
1323 *
1324 * Note that this is also called with nparam zero to resolve
1325 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001326 *
1327 * If you already know which context macro belongs to, you can pass
1328 * the context pointer as first parameter; if you won't but name begins
1329 * with %$ the context will be automatically computed. If all_contexts
1330 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001331 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001332static bool
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001333smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001334 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001335{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001336 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001337
H. Peter Anvin97a23472007-09-16 17:57:25 -07001338 if (ctx) {
H. Peter Anvin072771e2008-05-22 13:17:51 -07001339 m = (SMacro *) hash_findix(ctx->localmac, name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001340 } else if (name[0] == '%' && name[1] == '$') {
1341 if (cstk)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001342 ctx = get_ctx(name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001344 return false; /* got to return _something_ */
H. Peter Anvin072771e2008-05-22 13:17:51 -07001345 m = (SMacro *) hash_findix(ctx->localmac, name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001346 } else {
1347 m = (SMacro *) hash_findix(smacros, name);
1348 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001349
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350 while (m) {
1351 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001352 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001353 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001354 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001355 *defn = m;
1356 else
1357 *defn = NULL;
1358 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001359 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001360 }
1361 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001362 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001363
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001364 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001365}
1366
1367/*
1368 * Count and mark off the parameters in a multi-line macro call.
1369 * This is called both from within the multi-line macro expansion
1370 * code, and also to mark off the default parameters when provided
1371 * in a %macro definition line.
1372 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001373static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001374{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001375 int paramsize, brace;
1376
1377 *nparam = paramsize = 0;
1378 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001379 while (t) {
1380 if (*nparam >= paramsize) {
1381 paramsize += PARAM_DELTA;
1382 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1383 }
1384 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001385 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001386 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001387 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001388 (*params)[(*nparam)++] = t;
1389 while (tok_isnt_(t, brace ? "}" : ","))
1390 t = t->next;
1391 if (t) { /* got a comma/brace */
1392 t = t->next;
1393 if (brace) {
1394 /*
1395 * Now we've found the closing brace, look further
1396 * for the comma.
1397 */
1398 skip_white_(t);
1399 if (tok_isnt_(t, ",")) {
1400 error(ERR_NONFATAL,
1401 "braces do not enclose all of macro parameter");
1402 while (tok_isnt_(t, ","))
1403 t = t->next;
1404 }
1405 if (t)
1406 t = t->next; /* eat the comma */
1407 }
1408 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001409 }
1410}
1411
1412/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001413 * Determine whether one of the various `if' conditions is true or
1414 * not.
1415 *
1416 * We must free the tline we get passed.
1417 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001418static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001419{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001420 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001421 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001422 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001423 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001424 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001425 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001426
1427 origline = tline;
1428
H. Peter Anvine2c80182005-01-15 22:15:51 +00001429 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001430 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001431 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001432 while (cstk && tline) {
1433 skip_white_(tline);
1434 if (!tline || tline->type != TOK_ID) {
1435 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001436 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 free_tlist(origline);
1438 return -1;
1439 }
1440 if (!nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001441 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001442 tline = tline->next;
1443 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001444 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001445
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001446 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001447 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001448 while (tline) {
1449 skip_white_(tline);
1450 if (!tline || (tline->type != TOK_ID &&
1451 (tline->type != TOK_PREPROC_ID ||
1452 tline->text[1] != '$'))) {
1453 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001454 "`%s' expects macro identifiers", pp_directives[ct]);
1455 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001456 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001457 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001458 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001459 tline = tline->next;
1460 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001461 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001462
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001463 case PPC_IFIDN:
1464 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001465 tline = expand_smacro(tline);
1466 t = tt = tline;
1467 while (tok_isnt_(tt, ","))
1468 tt = tt->next;
1469 if (!tt) {
1470 error(ERR_NONFATAL,
1471 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001472 pp_directives[ct]);
1473 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001474 }
1475 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001476 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001477 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1478 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1479 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001480 pp_directives[ct]);
1481 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001482 }
1483 if (t->type == TOK_WHITESPACE) {
1484 t = t->next;
1485 continue;
1486 }
1487 if (tt->type == TOK_WHITESPACE) {
1488 tt = tt->next;
1489 continue;
1490 }
1491 if (tt->type != t->type) {
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 }
1495 /* Unify surrounding quotes for strings */
1496 if (t->type == TOK_STRING) {
1497 tt->text[0] = t->text[0];
1498 tt->text[strlen(tt->text) - 1] = t->text[0];
1499 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001500 if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001501 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001502 break;
1503 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001504
H. Peter Anvine2c80182005-01-15 22:15:51 +00001505 t = t->next;
1506 tt = tt->next;
1507 }
1508 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001509 j = false; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001510 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001511
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001512 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001513 {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001514 bool found = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001515 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001516
H. Peter Anvine2c80182005-01-15 22:15:51 +00001517 tline = tline->next;
1518 skip_white_(tline);
1519 tline = expand_id(tline);
1520 if (!tok_type_(tline, TOK_ID)) {
1521 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001522 "`%s' expects a macro name", pp_directives[ct]);
1523 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001524 }
1525 searching.name = nasm_strdup(tline->text);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001526 searching.casesense = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001527 searching.plus = false;
1528 searching.nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001529 searching.in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001530 searching.rep_nest = NULL;
1531 searching.nparam_min = 0;
1532 searching.nparam_max = INT_MAX;
1533 tline = expand_smacro(tline->next);
1534 skip_white_(tline);
1535 if (!tline) {
1536 } else if (!tok_type_(tline, TOK_NUMBER)) {
1537 error(ERR_NONFATAL,
1538 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001539 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001540 } else {
1541 searching.nparam_min = searching.nparam_max =
1542 readnum(tline->text, &j);
1543 if (j)
1544 error(ERR_NONFATAL,
1545 "unable to parse parameter count `%s'",
1546 tline->text);
1547 }
1548 if (tline && tok_is_(tline->next, "-")) {
1549 tline = tline->next->next;
1550 if (tok_is_(tline, "*"))
1551 searching.nparam_max = INT_MAX;
1552 else if (!tok_type_(tline, TOK_NUMBER))
1553 error(ERR_NONFATAL,
1554 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001555 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001556 else {
1557 searching.nparam_max = readnum(tline->text, &j);
1558 if (j)
1559 error(ERR_NONFATAL,
1560 "unable to parse parameter count `%s'",
1561 tline->text);
1562 if (searching.nparam_min > searching.nparam_max)
1563 error(ERR_NONFATAL,
1564 "minimum parameter count exceeds maximum");
1565 }
1566 }
1567 if (tline && tok_is_(tline->next, "+")) {
1568 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001569 searching.plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001570 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07001571 mmac = (MMacro *) hash_findix(mmacros, searching.name);
1572 while (mmac) {
1573 if (!strcmp(mmac->name, searching.name) &&
1574 (mmac->nparam_min <= searching.nparam_max
1575 || searching.plus)
1576 && (searching.nparam_min <= mmac->nparam_max
1577 || mmac->plus)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001578 found = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001579 break;
1580 }
1581 mmac = mmac->next;
1582 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001583 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001584 j = found;
1585 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001586 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001587
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001588 case PPC_IFID:
1589 needtype = TOK_ID;
1590 goto iftype;
1591 case PPC_IFNUM:
1592 needtype = TOK_NUMBER;
1593 goto iftype;
1594 case PPC_IFSTR:
1595 needtype = TOK_STRING;
1596 goto iftype;
1597
1598 iftype:
H. Peter Anvin134b9462008-02-16 17:01:40 -08001599 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001600
H. Peter Anvin927c92b2008-02-16 13:44:52 -08001601 while (tok_type_(t, TOK_WHITESPACE) ||
1602 (needtype == TOK_NUMBER &&
1603 tok_type_(t, TOK_OTHER) &&
1604 (t->text[0] == '-' || t->text[0] == '+') &&
1605 !t->text[1]))
1606 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001607
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001608 j = tok_type_(t, needtype);
1609 break;
1610
1611 case PPC_IFTOKEN:
1612 t = tline = expand_smacro(tline);
1613 while (tok_type_(t, TOK_WHITESPACE))
1614 t = t->next;
1615
1616 j = false;
1617 if (t) {
1618 t = t->next; /* Skip the actual token */
1619 while (tok_type_(t, TOK_WHITESPACE))
1620 t = t->next;
1621 j = !t; /* Should be nothing left */
1622 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001623 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001624
H. Peter Anvin134b9462008-02-16 17:01:40 -08001625 case PPC_IFEMPTY:
1626 t = tline = expand_smacro(tline);
1627 while (tok_type_(t, TOK_WHITESPACE))
1628 t = t->next;
1629
1630 j = !t; /* Should be empty */
1631 break;
1632
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001633 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001634 t = tline = expand_smacro(tline);
1635 tptr = &t;
1636 tokval.t_type = TOKEN_INVALID;
1637 evalresult = evaluate(ppscan, tptr, &tokval,
1638 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 if (!evalresult)
1640 return -1;
1641 if (tokval.t_type)
1642 error(ERR_WARNING,
1643 "trailing garbage after expression ignored");
1644 if (!is_simple(evalresult)) {
1645 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001646 "non-constant value given to `%s'", pp_directives[ct]);
1647 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001648 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001649 j = reloc_value(evalresult) != 0;
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001650 return j;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001651
H. Peter Anvine2c80182005-01-15 22:15:51 +00001652 default:
1653 error(ERR_FATAL,
1654 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001655 pp_directives[ct]);
1656 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001657 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001658
1659 free_tlist(origline);
1660 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001661
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001662fail:
1663 free_tlist(origline);
1664 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001665}
1666
1667/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001668 * Expand macros in a string. Used in %error and %include directives.
Keith Kaniosb7a89542007-04-12 02:40:54 +00001669 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001670 * The returned variable should ALWAYS be freed after usage.
1671 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001672void expand_macros_in_string(char **p)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001673{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001674 Token *line = tokenize(*p);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001675 line = expand_smacro(line);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001676 *p = detoken(line, false);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001677}
1678
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001679/*
1680 * Common code for defining an smacro
1681 */
1682static bool define_smacro(Context *ctx, char *mname, bool casesense,
1683 int nparam, Token *expansion)
1684{
1685 SMacro *smac, **smhead;
H. Peter Anvin70653092007-10-19 14:42:29 -07001686
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001687 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1688 if (!smac) {
1689 error(ERR_WARNING,
1690 "single-line macro `%s' defined both with and"
1691 " without parameters", mname);
1692
1693 /* Some instances of the old code considered this a failure,
1694 some others didn't. What is the right thing to do here? */
1695 free_tlist(expansion);
1696 return false; /* Failure */
1697 } else {
1698 /*
1699 * We're redefining, so we have to take over an
1700 * existing SMacro structure. This means freeing
1701 * what was already in it.
1702 */
1703 nasm_free(smac->name);
1704 free_tlist(smac->expansion);
1705 }
1706 } else {
H. Peter Anvin072771e2008-05-22 13:17:51 -07001707 smhead = (SMacro **) hash_findi_add(ctx ? ctx->localmac : smacros,
1708 mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001709 smac = nasm_malloc(sizeof(SMacro));
1710 smac->next = *smhead;
1711 *smhead = smac;
1712 }
1713 smac->name = nasm_strdup(mname);
1714 smac->casesense = casesense;
1715 smac->nparam = nparam;
1716 smac->expansion = expansion;
1717 smac->in_progress = false;
1718 return true; /* Success */
1719}
1720
1721/*
1722 * Undefine an smacro
1723 */
1724static void undef_smacro(Context *ctx, const char *mname)
1725{
1726 SMacro **smhead, *s, **sp;
1727
H. Peter Anvin072771e2008-05-22 13:17:51 -07001728 smhead = (SMacro **)hash_findi(ctx ? ctx->localmac : smacros, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001729
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001730 if (smhead) {
1731 /*
1732 * We now have a macro name... go hunt for it.
1733 */
1734 sp = smhead;
1735 while ((s = *sp) != NULL) {
1736 if (!mstrcmp(s->name, mname, s->casesense)) {
1737 *sp = s->next;
1738 nasm_free(s->name);
1739 free_tlist(s->expansion);
1740 nasm_free(s);
1741 } else {
1742 sp = &s->next;
1743 }
1744 }
1745 }
1746}
1747
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001748/*
1749 * Decode a size directive
1750 */
1751static int parse_size(const char *str) {
1752 static const char *size_names[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001753 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001754 static const int sizes[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001755 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001756
1757 return sizes[bsii(str, size_names, elements(size_names))+1];
1758}
1759
Ed Beroset3ab3f412002-06-11 03:31:49 +00001760/**
1761 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001762 * Find out if a line contains a preprocessor directive, and deal
1763 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07001764 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001765 * If a directive _is_ found, it is the responsibility of this routine
1766 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001767 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001768 * @param tline a pointer to the current tokeninzed line linked list
1769 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07001770 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001771 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001772static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001773{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001774 enum preproc_token i;
1775 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07001776 bool err;
1777 int nparam;
1778 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001779 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001780 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001781 int offset;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001782 char *p, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001783 Include *inc;
1784 Context *ctx;
1785 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001786 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001787 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1788 Line *l;
1789 struct tokenval tokval;
1790 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001791 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001792 int64_t count;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001793
1794 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001795
H. Peter Anvineba20a72002-04-30 20:53:55 +00001796 skip_white_(tline);
1797 if (!tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001798 (tline->text[1] == '%' || tline->text[1] == '$'
1799 || tline->text[1] == '!'))
1800 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001801
H. Peter Anvin4169a472007-09-12 01:29:43 +00001802 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001803
1804 /*
1805 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001806 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001807 * we should ignore all directives except for condition
1808 * directives.
1809 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001810 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001811 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1812 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001813 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001814
1815 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001816 * If we're defining a macro or reading a %rep block, we should
1817 * ignore all directives except for %macro/%imacro (which
1818 * generate an error), %endm/%endmacro, and (only if we're in a
H. Peter Anvineba20a72002-04-30 20:53:55 +00001819 * %rep block) %endrep. If we're in a %rep block, another %rep
1820 * causes an error, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001821 */
1822 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823 i != PP_ENDMACRO && i != PP_ENDM &&
1824 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1825 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001826 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001827
H. Peter Anvin4169a472007-09-12 01:29:43 +00001828 switch (i) {
1829 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001830 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1831 tline->text);
1832 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001833
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 case PP_STACKSIZE:
1835 /* Directive to tell NASM what the default stack size is. The
1836 * default is for a 16-bit stack, and this can be overriden with
1837 * %stacksize large.
1838 * the following form:
1839 *
1840 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1841 */
1842 tline = tline->next;
1843 if (tline && tline->type == TOK_WHITESPACE)
1844 tline = tline->next;
1845 if (!tline || tline->type != TOK_ID) {
1846 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1847 free_tlist(origline);
1848 return DIRECTIVE_FOUND;
1849 }
1850 if (nasm_stricmp(tline->text, "flat") == 0) {
1851 /* All subsequent ARG directives are for a 32-bit stack */
1852 StackSize = 4;
1853 StackPointer = "ebp";
1854 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001855 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08001856 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
1857 /* All subsequent ARG directives are for a 64-bit stack */
1858 StackSize = 8;
1859 StackPointer = "rbp";
1860 ArgOffset = 8;
1861 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001862 } else if (nasm_stricmp(tline->text, "large") == 0) {
1863 /* All subsequent ARG directives are for a 16-bit stack,
1864 * far function call.
1865 */
1866 StackSize = 2;
1867 StackPointer = "bp";
1868 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001869 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001870 } else if (nasm_stricmp(tline->text, "small") == 0) {
1871 /* All subsequent ARG directives are for a 16-bit stack,
1872 * far function call. We don't support near functions.
1873 */
1874 StackSize = 2;
1875 StackPointer = "bp";
1876 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001877 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001878 } else {
1879 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1880 free_tlist(origline);
1881 return DIRECTIVE_FOUND;
1882 }
1883 free_tlist(origline);
1884 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001885
H. Peter Anvine2c80182005-01-15 22:15:51 +00001886 case PP_ARG:
1887 /* TASM like ARG directive to define arguments to functions, in
1888 * the following form:
1889 *
1890 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1891 */
1892 offset = ArgOffset;
1893 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001894 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001895 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001896
H. Peter Anvine2c80182005-01-15 22:15:51 +00001897 /* Find the argument name */
1898 tline = tline->next;
1899 if (tline && tline->type == TOK_WHITESPACE)
1900 tline = tline->next;
1901 if (!tline || tline->type != TOK_ID) {
1902 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1903 free_tlist(origline);
1904 return DIRECTIVE_FOUND;
1905 }
1906 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001907
H. Peter Anvine2c80182005-01-15 22:15:51 +00001908 /* Find the argument size type */
1909 tline = tline->next;
1910 if (!tline || tline->type != TOK_OTHER
1911 || tline->text[0] != ':') {
1912 error(ERR_NONFATAL,
1913 "Syntax error processing `%%arg' directive");
1914 free_tlist(origline);
1915 return DIRECTIVE_FOUND;
1916 }
1917 tline = tline->next;
1918 if (!tline || tline->type != TOK_ID) {
1919 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1920 free_tlist(origline);
1921 return DIRECTIVE_FOUND;
1922 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001923
H. Peter Anvine2c80182005-01-15 22:15:51 +00001924 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001925 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001926 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001927 size = parse_size(tt->text);
1928 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001929 error(ERR_NONFATAL,
1930 "Invalid size type for `%%arg' missing directive");
1931 free_tlist(tt);
1932 free_tlist(origline);
1933 return DIRECTIVE_FOUND;
1934 }
1935 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001936
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001937 /* Round up to even stack slots */
1938 size = (size+StackSize-1) & ~(StackSize-1);
1939
H. Peter Anvine2c80182005-01-15 22:15:51 +00001940 /* Now define the macro for the argument */
1941 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1942 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001943 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001944 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001945
H. Peter Anvine2c80182005-01-15 22:15:51 +00001946 /* Move to the next argument in the list */
1947 tline = tline->next;
1948 if (tline && tline->type == TOK_WHITESPACE)
1949 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001950 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1951 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001952 free_tlist(origline);
1953 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001954
H. Peter Anvine2c80182005-01-15 22:15:51 +00001955 case PP_LOCAL:
1956 /* TASM like LOCAL directive to define local variables for a
1957 * function, in the following form:
1958 *
1959 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1960 *
1961 * The '= LocalSize' at the end is ignored by NASM, but is
1962 * required by TASM to define the local parameter size (and used
1963 * by the TASM macro package).
1964 */
1965 offset = LocalOffset;
1966 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001967 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001968 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001969
H. Peter Anvine2c80182005-01-15 22:15:51 +00001970 /* Find the argument name */
1971 tline = tline->next;
1972 if (tline && tline->type == TOK_WHITESPACE)
1973 tline = tline->next;
1974 if (!tline || tline->type != TOK_ID) {
1975 error(ERR_NONFATAL,
1976 "`%%local' missing argument parameter");
1977 free_tlist(origline);
1978 return DIRECTIVE_FOUND;
1979 }
1980 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001981
H. Peter Anvine2c80182005-01-15 22:15:51 +00001982 /* Find the argument size type */
1983 tline = tline->next;
1984 if (!tline || tline->type != TOK_OTHER
1985 || tline->text[0] != ':') {
1986 error(ERR_NONFATAL,
1987 "Syntax error processing `%%local' directive");
1988 free_tlist(origline);
1989 return DIRECTIVE_FOUND;
1990 }
1991 tline = tline->next;
1992 if (!tline || tline->type != TOK_ID) {
1993 error(ERR_NONFATAL,
1994 "`%%local' missing size type parameter");
1995 free_tlist(origline);
1996 return DIRECTIVE_FOUND;
1997 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001998
H. Peter Anvine2c80182005-01-15 22:15:51 +00001999 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002000 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002001 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002002 size = parse_size(tt->text);
2003 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002004 error(ERR_NONFATAL,
2005 "Invalid size type for `%%local' missing directive");
2006 free_tlist(tt);
2007 free_tlist(origline);
2008 return DIRECTIVE_FOUND;
2009 }
2010 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002011
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002012 /* Round up to even stack slots */
2013 size = (size+StackSize-1) & ~(StackSize-1);
2014
2015 offset += size; /* Negative offset, increment before */
2016
2017 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002018 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2019 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002020 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002021
H. Peter Anvine2c80182005-01-15 22:15:51 +00002022 /* Now define the assign to setup the enter_c macro correctly */
2023 snprintf(directive, sizeof(directive),
2024 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002025 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002026
H. Peter Anvine2c80182005-01-15 22:15:51 +00002027 /* Move to the next argument in the list */
2028 tline = tline->next;
2029 if (tline && tline->type == TOK_WHITESPACE)
2030 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002031 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2032 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002033 free_tlist(origline);
2034 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002035
H. Peter Anvine2c80182005-01-15 22:15:51 +00002036 case PP_CLEAR:
2037 if (tline->next)
2038 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002039 free_macros();
2040 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002041 free_tlist(origline);
2042 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002043
H. Peter Anvine2c80182005-01-15 22:15:51 +00002044 case PP_INCLUDE:
2045 tline = tline->next;
2046 skip_white_(tline);
2047 if (!tline || (tline->type != TOK_STRING &&
2048 tline->type != TOK_INTERNAL_STRING)) {
2049 error(ERR_NONFATAL, "`%%include' expects a file name");
2050 free_tlist(origline);
2051 return DIRECTIVE_FOUND; /* but we did _something_ */
2052 }
2053 if (tline->next)
2054 error(ERR_WARNING,
2055 "trailing garbage after `%%include' ignored");
2056 if (tline->type != TOK_INTERNAL_STRING) {
2057 p = tline->text + 1; /* point past the quote to the name */
2058 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2059 } else
2060 p = tline->text; /* internal_string is easier */
2061 expand_macros_in_string(&p);
2062 inc = nasm_malloc(sizeof(Include));
2063 inc->next = istk;
2064 inc->conds = NULL;
2065 inc->fp = inc_fopen(p);
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002066 if (!inc->fp && pass == 0) {
2067 /* -MG given but file not found */
2068 nasm_free(inc);
2069 } else {
2070 inc->fname = src_set_fname(p);
2071 inc->lineno = src_set_linnum(0);
2072 inc->lineinc = 1;
2073 inc->expansion = NULL;
2074 inc->mstk = NULL;
2075 istk = inc;
2076 list->uplevel(LIST_INCLUDE);
2077 }
2078 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002079 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002080
H. Peter Anvine2c80182005-01-15 22:15:51 +00002081 case PP_PUSH:
2082 tline = tline->next;
2083 skip_white_(tline);
2084 tline = expand_id(tline);
2085 if (!tok_type_(tline, TOK_ID)) {
2086 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2087 free_tlist(origline);
2088 return DIRECTIVE_FOUND; /* but we did _something_ */
2089 }
2090 if (tline->next)
2091 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2092 ctx = nasm_malloc(sizeof(Context));
2093 ctx->next = cstk;
H. Peter Anvin072771e2008-05-22 13:17:51 -07002094 ctx->localmac = hash_init(HASH_SMALL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002095 ctx->name = nasm_strdup(tline->text);
2096 ctx->number = unique++;
2097 cstk = ctx;
2098 free_tlist(origline);
2099 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002100
H. Peter Anvine2c80182005-01-15 22:15:51 +00002101 case PP_REPL:
2102 tline = tline->next;
2103 skip_white_(tline);
2104 tline = expand_id(tline);
2105 if (!tok_type_(tline, TOK_ID)) {
2106 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2107 free_tlist(origline);
2108 return DIRECTIVE_FOUND; /* but we did _something_ */
2109 }
2110 if (tline->next)
2111 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2112 if (!cstk)
2113 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2114 else {
2115 nasm_free(cstk->name);
2116 cstk->name = nasm_strdup(tline->text);
2117 }
2118 free_tlist(origline);
2119 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002120
H. Peter Anvine2c80182005-01-15 22:15:51 +00002121 case PP_POP:
2122 if (tline->next)
2123 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2124 if (!cstk)
2125 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2126 else
2127 ctx_pop();
2128 free_tlist(origline);
2129 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002130
H. Peter Anvine2c80182005-01-15 22:15:51 +00002131 case PP_ERROR:
2132 tline->next = expand_smacro(tline->next);
2133 tline = tline->next;
2134 skip_white_(tline);
2135 if (tok_type_(tline, TOK_STRING)) {
2136 p = tline->text + 1; /* point past the quote to the name */
2137 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2138 expand_macros_in_string(&p);
2139 error(ERR_NONFATAL, "%s", p);
2140 nasm_free(p);
2141 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002142 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002143 error(ERR_WARNING, "%s", p);
2144 nasm_free(p);
2145 }
2146 free_tlist(origline);
2147 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002148
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002149 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002150 if (istk->conds && !emitting(istk->conds->state))
2151 j = COND_NEVER;
2152 else {
2153 j = if_condition(tline->next, i);
2154 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002155 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2156 }
2157 cond = nasm_malloc(sizeof(Cond));
2158 cond->next = istk->conds;
2159 cond->state = j;
2160 istk->conds = cond;
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002161 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002162 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002163
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002164 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002165 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002166 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002167 if (emitting(istk->conds->state)
2168 || istk->conds->state == COND_NEVER)
2169 istk->conds->state = COND_NEVER;
2170 else {
2171 /*
2172 * IMPORTANT: In the case of %if, we will already have
2173 * called expand_mmac_params(); however, if we're
2174 * processing an %elif we must have been in a
2175 * non-emitting mode, which would have inhibited
2176 * the normal invocation of expand_mmac_params(). Therefore,
2177 * we have to do it explicitly here.
2178 */
2179 j = if_condition(expand_mmac_params(tline->next), i);
2180 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002181 istk->conds->state =
2182 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2183 }
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002184 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002185 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002186
H. Peter Anvine2c80182005-01-15 22:15:51 +00002187 case PP_ELSE:
2188 if (tline->next)
2189 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2190 if (!istk->conds)
2191 error(ERR_FATAL, "`%%else': no matching `%%if'");
2192 if (emitting(istk->conds->state)
2193 || istk->conds->state == COND_NEVER)
2194 istk->conds->state = COND_ELSE_FALSE;
2195 else
2196 istk->conds->state = COND_ELSE_TRUE;
2197 free_tlist(origline);
2198 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002199
H. Peter Anvine2c80182005-01-15 22:15:51 +00002200 case PP_ENDIF:
2201 if (tline->next)
2202 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2203 if (!istk->conds)
2204 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2205 cond = istk->conds;
2206 istk->conds = cond->next;
2207 nasm_free(cond);
2208 free_tlist(origline);
2209 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002210
H. Peter Anvine2c80182005-01-15 22:15:51 +00002211 case PP_MACRO:
2212 case PP_IMACRO:
2213 if (defining)
2214 error(ERR_FATAL,
2215 "`%%%smacro': already defining a macro",
2216 (i == PP_IMACRO ? "i" : ""));
2217 tline = tline->next;
2218 skip_white_(tline);
2219 tline = expand_id(tline);
2220 if (!tok_type_(tline, TOK_ID)) {
2221 error(ERR_NONFATAL,
2222 "`%%%smacro' expects a macro name",
2223 (i == PP_IMACRO ? "i" : ""));
2224 return DIRECTIVE_FOUND;
2225 }
2226 defining = nasm_malloc(sizeof(MMacro));
2227 defining->name = nasm_strdup(tline->text);
2228 defining->casesense = (i == PP_MACRO);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002229 defining->plus = false;
2230 defining->nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002231 defining->in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002232 defining->rep_nest = NULL;
2233 tline = expand_smacro(tline->next);
2234 skip_white_(tline);
2235 if (!tok_type_(tline, TOK_NUMBER)) {
2236 error(ERR_NONFATAL,
2237 "`%%%smacro' expects a parameter count",
2238 (i == PP_IMACRO ? "i" : ""));
2239 defining->nparam_min = defining->nparam_max = 0;
2240 } else {
2241 defining->nparam_min = defining->nparam_max =
H. Peter Anvin70055962007-10-11 00:05:31 -07002242 readnum(tline->text, &err);
2243 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002244 error(ERR_NONFATAL,
2245 "unable to parse parameter count `%s'", tline->text);
2246 }
2247 if (tline && tok_is_(tline->next, "-")) {
2248 tline = tline->next->next;
2249 if (tok_is_(tline, "*"))
2250 defining->nparam_max = INT_MAX;
2251 else if (!tok_type_(tline, TOK_NUMBER))
2252 error(ERR_NONFATAL,
2253 "`%%%smacro' expects a parameter count after `-'",
2254 (i == PP_IMACRO ? "i" : ""));
2255 else {
H. Peter Anvin70055962007-10-11 00:05:31 -07002256 defining->nparam_max = readnum(tline->text, &err);
2257 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002258 error(ERR_NONFATAL,
2259 "unable to parse parameter count `%s'",
2260 tline->text);
2261 if (defining->nparam_min > defining->nparam_max)
2262 error(ERR_NONFATAL,
2263 "minimum parameter count exceeds maximum");
2264 }
2265 }
2266 if (tline && tok_is_(tline->next, "+")) {
2267 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002268 defining->plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002269 }
2270 if (tline && tok_type_(tline->next, TOK_ID) &&
2271 !nasm_stricmp(tline->next->text, ".nolist")) {
2272 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002273 defining->nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002274 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07002275 mmac = (MMacro *) hash_findix(mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002276 while (mmac) {
2277 if (!strcmp(mmac->name, defining->name) &&
2278 (mmac->nparam_min <= defining->nparam_max
2279 || defining->plus)
2280 && (defining->nparam_min <= mmac->nparam_max
2281 || mmac->plus)) {
2282 error(ERR_WARNING,
2283 "redefining multi-line macro `%s'", defining->name);
2284 break;
2285 }
2286 mmac = mmac->next;
2287 }
2288 /*
2289 * Handle default parameters.
2290 */
2291 if (tline && tline->next) {
2292 defining->dlist = tline->next;
2293 tline->next = NULL;
2294 count_mmac_params(defining->dlist, &defining->ndefs,
2295 &defining->defaults);
2296 } else {
2297 defining->dlist = NULL;
2298 defining->defaults = NULL;
2299 }
2300 defining->expansion = NULL;
2301 free_tlist(origline);
2302 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002303
H. Peter Anvine2c80182005-01-15 22:15:51 +00002304 case PP_ENDM:
2305 case PP_ENDMACRO:
2306 if (!defining) {
2307 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2308 return DIRECTIVE_FOUND;
2309 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07002310 mmhead = (MMacro **) hash_findi_add(mmacros, defining->name);
2311 defining->next = *mmhead;
2312 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002313 defining = NULL;
2314 free_tlist(origline);
2315 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002316
H. Peter Anvine2c80182005-01-15 22:15:51 +00002317 case PP_ROTATE:
2318 if (tline->next && tline->next->type == TOK_WHITESPACE)
2319 tline = tline->next;
2320 if (tline->next == NULL) {
2321 free_tlist(origline);
2322 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2323 return DIRECTIVE_FOUND;
2324 }
2325 t = expand_smacro(tline->next);
2326 tline->next = NULL;
2327 free_tlist(origline);
2328 tline = t;
2329 tptr = &t;
2330 tokval.t_type = TOKEN_INVALID;
2331 evalresult =
2332 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2333 free_tlist(tline);
2334 if (!evalresult)
2335 return DIRECTIVE_FOUND;
2336 if (tokval.t_type)
2337 error(ERR_WARNING,
2338 "trailing garbage after expression ignored");
2339 if (!is_simple(evalresult)) {
2340 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2341 return DIRECTIVE_FOUND;
2342 }
2343 mmac = istk->mstk;
2344 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2345 mmac = mmac->next_active;
2346 if (!mmac) {
2347 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2348 } else if (mmac->nparam == 0) {
2349 error(ERR_NONFATAL,
2350 "`%%rotate' invoked within macro without parameters");
2351 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002352 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002353
H. Peter Anvin25a99342007-09-22 17:45:45 -07002354 rotate %= (int)mmac->nparam;
2355 if (rotate < 0)
2356 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002357
H. Peter Anvin25a99342007-09-22 17:45:45 -07002358 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002359 }
2360 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002361
H. Peter Anvine2c80182005-01-15 22:15:51 +00002362 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002363 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002364 do {
2365 tline = tline->next;
2366 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002367
H. Peter Anvine2c80182005-01-15 22:15:51 +00002368 if (tok_type_(tline, TOK_ID) &&
2369 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002370 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002371 do {
2372 tline = tline->next;
2373 } while (tok_type_(tline, TOK_WHITESPACE));
2374 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002375
H. Peter Anvine2c80182005-01-15 22:15:51 +00002376 if (tline) {
2377 t = expand_smacro(tline);
2378 tptr = &t;
2379 tokval.t_type = TOKEN_INVALID;
2380 evalresult =
2381 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2382 if (!evalresult) {
2383 free_tlist(origline);
2384 return DIRECTIVE_FOUND;
2385 }
2386 if (tokval.t_type)
2387 error(ERR_WARNING,
2388 "trailing garbage after expression ignored");
2389 if (!is_simple(evalresult)) {
2390 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2391 return DIRECTIVE_FOUND;
2392 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002393 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002394 } else {
2395 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002396 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002397 }
2398 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002399
H. Peter Anvine2c80182005-01-15 22:15:51 +00002400 tmp_defining = defining;
2401 defining = nasm_malloc(sizeof(MMacro));
2402 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002403 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002404 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002405 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002406 defining->in_progress = count;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002407 defining->nparam_min = defining->nparam_max = 0;
2408 defining->defaults = NULL;
2409 defining->dlist = NULL;
2410 defining->expansion = NULL;
2411 defining->next_active = istk->mstk;
2412 defining->rep_nest = tmp_defining;
2413 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002414
H. Peter Anvine2c80182005-01-15 22:15:51 +00002415 case PP_ENDREP:
2416 if (!defining || defining->name) {
2417 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2418 return DIRECTIVE_FOUND;
2419 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002420
H. Peter Anvine2c80182005-01-15 22:15:51 +00002421 /*
2422 * Now we have a "macro" defined - although it has no name
2423 * and we won't be entering it in the hash tables - we must
2424 * push a macro-end marker for it on to istk->expansion.
2425 * After that, it will take care of propagating itself (a
2426 * macro-end marker line for a macro which is really a %rep
2427 * block will cause the macro to be re-expanded, complete
2428 * with another macro-end marker to ensure the process
2429 * continues) until the whole expansion is forcibly removed
2430 * from istk->expansion by a %exitrep.
2431 */
2432 l = nasm_malloc(sizeof(Line));
2433 l->next = istk->expansion;
2434 l->finishes = defining;
2435 l->first = NULL;
2436 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002437
H. Peter Anvine2c80182005-01-15 22:15:51 +00002438 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002439
H. Peter Anvine2c80182005-01-15 22:15:51 +00002440 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2441 tmp_defining = defining;
2442 defining = defining->rep_nest;
2443 free_tlist(origline);
2444 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002445
H. Peter Anvine2c80182005-01-15 22:15:51 +00002446 case PP_EXITREP:
2447 /*
2448 * We must search along istk->expansion until we hit a
2449 * macro-end marker for a macro with no name. Then we set
2450 * its `in_progress' flag to 0.
2451 */
2452 for (l = istk->expansion; l; l = l->next)
2453 if (l->finishes && !l->finishes->name)
2454 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002455
H. Peter Anvine2c80182005-01-15 22:15:51 +00002456 if (l)
2457 l->finishes->in_progress = 0;
2458 else
2459 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2460 free_tlist(origline);
2461 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002462
H. Peter Anvine2c80182005-01-15 22:15:51 +00002463 case PP_XDEFINE:
2464 case PP_IXDEFINE:
2465 case PP_DEFINE:
2466 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002467 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002468
H. Peter Anvine2c80182005-01-15 22:15:51 +00002469 tline = tline->next;
2470 skip_white_(tline);
2471 tline = expand_id(tline);
2472 if (!tline || (tline->type != TOK_ID &&
2473 (tline->type != TOK_PREPROC_ID ||
2474 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002475 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2476 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002477 free_tlist(origline);
2478 return DIRECTIVE_FOUND;
2479 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002480
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002481 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002482
H. Peter Anvine2c80182005-01-15 22:15:51 +00002483 mname = tline->text;
2484 last = tline;
2485 param_start = tline = tline->next;
2486 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002487
H. Peter Anvine2c80182005-01-15 22:15:51 +00002488 /* Expand the macro definition now for %xdefine and %ixdefine */
2489 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2490 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002491
H. Peter Anvine2c80182005-01-15 22:15:51 +00002492 if (tok_is_(tline, "(")) {
2493 /*
2494 * This macro has parameters.
2495 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002496
H. Peter Anvine2c80182005-01-15 22:15:51 +00002497 tline = tline->next;
2498 while (1) {
2499 skip_white_(tline);
2500 if (!tline) {
2501 error(ERR_NONFATAL, "parameter identifier expected");
2502 free_tlist(origline);
2503 return DIRECTIVE_FOUND;
2504 }
2505 if (tline->type != TOK_ID) {
2506 error(ERR_NONFATAL,
2507 "`%s': parameter identifier expected",
2508 tline->text);
2509 free_tlist(origline);
2510 return DIRECTIVE_FOUND;
2511 }
2512 tline->type = TOK_SMAC_PARAM + nparam++;
2513 tline = tline->next;
2514 skip_white_(tline);
2515 if (tok_is_(tline, ",")) {
2516 tline = tline->next;
2517 continue;
2518 }
2519 if (!tok_is_(tline, ")")) {
2520 error(ERR_NONFATAL,
2521 "`)' expected to terminate macro template");
2522 free_tlist(origline);
2523 return DIRECTIVE_FOUND;
2524 }
2525 break;
2526 }
2527 last = tline;
2528 tline = tline->next;
2529 }
2530 if (tok_type_(tline, TOK_WHITESPACE))
2531 last = tline, tline = tline->next;
2532 macro_start = NULL;
2533 last->next = NULL;
2534 t = tline;
2535 while (t) {
2536 if (t->type == TOK_ID) {
2537 for (tt = param_start; tt; tt = tt->next)
2538 if (tt->type >= TOK_SMAC_PARAM &&
2539 !strcmp(tt->text, t->text))
2540 t->type = tt->type;
2541 }
2542 tt = t->next;
2543 t->next = macro_start;
2544 macro_start = t;
2545 t = tt;
2546 }
2547 /*
2548 * Good. We now have a macro name, a parameter count, and a
2549 * token list (in reverse order) for an expansion. We ought
2550 * to be OK just to create an SMacro, store it, and let
2551 * free_tlist have the rest of the line (which we have
2552 * carefully re-terminated after chopping off the expansion
2553 * from the end).
2554 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002555 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002556 free_tlist(origline);
2557 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002558
H. Peter Anvine2c80182005-01-15 22:15:51 +00002559 case PP_UNDEF:
2560 tline = tline->next;
2561 skip_white_(tline);
2562 tline = expand_id(tline);
2563 if (!tline || (tline->type != TOK_ID &&
2564 (tline->type != TOK_PREPROC_ID ||
2565 tline->text[1] != '$'))) {
2566 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2567 free_tlist(origline);
2568 return DIRECTIVE_FOUND;
2569 }
2570 if (tline->next) {
2571 error(ERR_WARNING,
2572 "trailing garbage after macro name ignored");
2573 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002574
H. Peter Anvine2c80182005-01-15 22:15:51 +00002575 /* Find the context that symbol belongs to */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002576 ctx = get_ctx(tline->text, false);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002577 undef_smacro(ctx, tline->text);
2578 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002579 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002580
H. Peter Anvine2c80182005-01-15 22:15:51 +00002581 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002582 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07002583
H. Peter Anvine2c80182005-01-15 22:15:51 +00002584 tline = tline->next;
2585 skip_white_(tline);
2586 tline = expand_id(tline);
2587 if (!tline || (tline->type != TOK_ID &&
2588 (tline->type != TOK_PREPROC_ID ||
2589 tline->text[1] != '$'))) {
2590 error(ERR_NONFATAL,
2591 "`%%strlen' expects a macro identifier as first parameter");
2592 free_tlist(origline);
2593 return DIRECTIVE_FOUND;
2594 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002595 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002596
H. Peter Anvine2c80182005-01-15 22:15:51 +00002597 mname = tline->text;
2598 last = tline;
2599 tline = expand_smacro(tline->next);
2600 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002601
H. Peter Anvine2c80182005-01-15 22:15:51 +00002602 t = tline;
2603 while (tok_type_(t, TOK_WHITESPACE))
2604 t = t->next;
2605 /* t should now point to the string */
2606 if (t->type != TOK_STRING) {
2607 error(ERR_NONFATAL,
2608 "`%%strlen` requires string as second parameter");
2609 free_tlist(tline);
2610 free_tlist(origline);
2611 return DIRECTIVE_FOUND;
2612 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002613
H. Peter Anvine2c80182005-01-15 22:15:51 +00002614 macro_start = nasm_malloc(sizeof(*macro_start));
2615 macro_start->next = NULL;
2616 make_tok_num(macro_start, strlen(t->text) - 2);
2617 macro_start->mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002618
H. Peter Anvine2c80182005-01-15 22:15:51 +00002619 /*
2620 * We now have a macro name, an implicit parameter count of
2621 * zero, and a numeric token to use as an expansion. Create
2622 * and store an SMacro.
2623 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002624 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002625 free_tlist(tline);
2626 free_tlist(origline);
2627 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002628
H. Peter Anvine2c80182005-01-15 22:15:51 +00002629 case PP_SUBSTR:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002630 casesense = true;
2631
H. Peter Anvine2c80182005-01-15 22:15:51 +00002632 tline = tline->next;
2633 skip_white_(tline);
2634 tline = expand_id(tline);
2635 if (!tline || (tline->type != TOK_ID &&
2636 (tline->type != TOK_PREPROC_ID ||
2637 tline->text[1] != '$'))) {
2638 error(ERR_NONFATAL,
2639 "`%%substr' expects a macro identifier as first parameter");
2640 free_tlist(origline);
2641 return DIRECTIVE_FOUND;
2642 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002643 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002644
H. Peter Anvine2c80182005-01-15 22:15:51 +00002645 mname = tline->text;
2646 last = tline;
2647 tline = expand_smacro(tline->next);
2648 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002649
H. Peter Anvine2c80182005-01-15 22:15:51 +00002650 t = tline->next;
2651 while (tok_type_(t, TOK_WHITESPACE))
2652 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002653
H. Peter Anvine2c80182005-01-15 22:15:51 +00002654 /* t should now point to the string */
2655 if (t->type != TOK_STRING) {
2656 error(ERR_NONFATAL,
2657 "`%%substr` requires string as second parameter");
2658 free_tlist(tline);
2659 free_tlist(origline);
2660 return DIRECTIVE_FOUND;
2661 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002662
H. Peter Anvine2c80182005-01-15 22:15:51 +00002663 tt = t->next;
2664 tptr = &tt;
2665 tokval.t_type = TOKEN_INVALID;
2666 evalresult =
2667 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2668 if (!evalresult) {
2669 free_tlist(tline);
2670 free_tlist(origline);
2671 return DIRECTIVE_FOUND;
2672 }
2673 if (!is_simple(evalresult)) {
2674 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2675 free_tlist(tline);
2676 free_tlist(origline);
2677 return DIRECTIVE_FOUND;
2678 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002679
H. Peter Anvine2c80182005-01-15 22:15:51 +00002680 macro_start = nasm_malloc(sizeof(*macro_start));
2681 macro_start->next = NULL;
2682 macro_start->text = nasm_strdup("'''");
2683 if (evalresult->value > 0
Charles Crayne192d5b52007-10-18 19:02:42 -07002684 && evalresult->value < (int) strlen(t->text) - 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002685 macro_start->text[1] = t->text[evalresult->value];
2686 } else {
2687 macro_start->text[2] = '\0';
2688 }
2689 macro_start->type = TOK_STRING;
2690 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002691
H. Peter Anvine2c80182005-01-15 22:15:51 +00002692 /*
2693 * We now have a macro name, an implicit parameter count of
2694 * zero, and a numeric token to use as an expansion. Create
2695 * and store an SMacro.
2696 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002697 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002698 free_tlist(tline);
2699 free_tlist(origline);
2700 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002701
H. Peter Anvine2c80182005-01-15 22:15:51 +00002702 case PP_ASSIGN:
2703 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002704 casesense = (i == PP_ASSIGN);
2705
H. Peter Anvine2c80182005-01-15 22:15:51 +00002706 tline = tline->next;
2707 skip_white_(tline);
2708 tline = expand_id(tline);
2709 if (!tline || (tline->type != TOK_ID &&
2710 (tline->type != TOK_PREPROC_ID ||
2711 tline->text[1] != '$'))) {
2712 error(ERR_NONFATAL,
2713 "`%%%sassign' expects a macro identifier",
2714 (i == PP_IASSIGN ? "i" : ""));
2715 free_tlist(origline);
2716 return DIRECTIVE_FOUND;
2717 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002718 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002719
H. Peter Anvine2c80182005-01-15 22:15:51 +00002720 mname = tline->text;
2721 last = tline;
2722 tline = expand_smacro(tline->next);
2723 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002724
H. Peter Anvine2c80182005-01-15 22:15:51 +00002725 t = tline;
2726 tptr = &t;
2727 tokval.t_type = TOKEN_INVALID;
2728 evalresult =
2729 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2730 free_tlist(tline);
2731 if (!evalresult) {
2732 free_tlist(origline);
2733 return DIRECTIVE_FOUND;
2734 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002735
H. Peter Anvine2c80182005-01-15 22:15:51 +00002736 if (tokval.t_type)
2737 error(ERR_WARNING,
2738 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00002739
H. Peter Anvine2c80182005-01-15 22:15:51 +00002740 if (!is_simple(evalresult)) {
2741 error(ERR_NONFATAL,
2742 "non-constant value given to `%%%sassign'",
2743 (i == PP_IASSIGN ? "i" : ""));
2744 free_tlist(origline);
2745 return DIRECTIVE_FOUND;
2746 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002747
H. Peter Anvine2c80182005-01-15 22:15:51 +00002748 macro_start = nasm_malloc(sizeof(*macro_start));
2749 macro_start->next = NULL;
2750 make_tok_num(macro_start, reloc_value(evalresult));
2751 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002752
H. Peter Anvine2c80182005-01-15 22:15:51 +00002753 /*
2754 * We now have a macro name, an implicit parameter count of
2755 * zero, and a numeric token to use as an expansion. Create
2756 * and store an SMacro.
2757 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002758 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002759 free_tlist(origline);
2760 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002761
H. Peter Anvine2c80182005-01-15 22:15:51 +00002762 case PP_LINE:
2763 /*
2764 * Syntax is `%line nnn[+mmm] [filename]'
2765 */
2766 tline = tline->next;
2767 skip_white_(tline);
2768 if (!tok_type_(tline, TOK_NUMBER)) {
2769 error(ERR_NONFATAL, "`%%line' expects line number");
2770 free_tlist(origline);
2771 return DIRECTIVE_FOUND;
2772 }
H. Peter Anvin70055962007-10-11 00:05:31 -07002773 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002774 m = 1;
2775 tline = tline->next;
2776 if (tok_is_(tline, "+")) {
2777 tline = tline->next;
2778 if (!tok_type_(tline, TOK_NUMBER)) {
2779 error(ERR_NONFATAL, "`%%line' expects line increment");
2780 free_tlist(origline);
2781 return DIRECTIVE_FOUND;
2782 }
H. Peter Anvin70055962007-10-11 00:05:31 -07002783 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002784 tline = tline->next;
2785 }
2786 skip_white_(tline);
2787 src_set_linnum(k);
2788 istk->lineinc = m;
2789 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002790 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002791 }
2792 free_tlist(origline);
2793 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002794
H. Peter Anvine2c80182005-01-15 22:15:51 +00002795 default:
2796 error(ERR_FATAL,
2797 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00002798 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002799 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002800 }
Ed Beroset3ab3f412002-06-11 03:31:49 +00002801 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002802}
2803
2804/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002805 * Ensure that a macro parameter contains a condition code and
2806 * nothing else. Return the condition code index if so, or -1
2807 * otherwise.
2808 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002809static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002810{
H. Peter Anvin76690a12002-04-30 20:52:49 +00002811 Token *tt;
2812 int i, j, k, m;
2813
H. Peter Anvin25a99342007-09-22 17:45:45 -07002814 if (!t)
2815 return -1; /* Probably a %+ without a space */
2816
H. Peter Anvineba20a72002-04-30 20:53:55 +00002817 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002818 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002819 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002820 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002821 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002822 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002823 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002824
2825 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00002826 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002827 while (j - i > 1) {
2828 k = (j + i) / 2;
2829 m = nasm_stricmp(t->text, conditions[k]);
2830 if (m == 0) {
2831 i = k;
2832 j = -2;
2833 break;
2834 } else if (m < 0) {
2835 j = k;
2836 } else
2837 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002838 }
2839 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002840 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002841 return i;
2842}
2843
2844/*
2845 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2846 * %-n) and MMacro-local identifiers (%%foo).
2847 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002848static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002849{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002850 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002851
2852 tail = &thead;
2853 thead = NULL;
2854
H. Peter Anvine2c80182005-01-15 22:15:51 +00002855 while (tline) {
2856 if (tline->type == TOK_PREPROC_ID &&
2857 (((tline->text[1] == '+' || tline->text[1] == '-')
2858 && tline->text[2]) || tline->text[1] == '%'
2859 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002860 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002861 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002862 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07002863 unsigned int n;
2864 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002865 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002866
H. Peter Anvine2c80182005-01-15 22:15:51 +00002867 t = tline;
2868 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002869
H. Peter Anvine2c80182005-01-15 22:15:51 +00002870 mac = istk->mstk;
2871 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2872 mac = mac->next_active;
2873 if (!mac)
2874 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2875 else
2876 switch (t->text[1]) {
2877 /*
2878 * We have to make a substitution of one of the
2879 * forms %1, %-1, %+1, %%foo, %0.
2880 */
2881 case '0':
2882 type = TOK_NUMBER;
2883 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
2884 text = nasm_strdup(tmpbuf);
2885 break;
2886 case '%':
2887 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002888 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002889 mac->unique);
2890 text = nasm_strcat(tmpbuf, t->text + 2);
2891 break;
2892 case '-':
2893 n = atoi(t->text + 2) - 1;
2894 if (n >= mac->nparam)
2895 tt = NULL;
2896 else {
2897 if (mac->nparam > 1)
2898 n = (n + mac->rotate) % mac->nparam;
2899 tt = mac->params[n];
2900 }
2901 cc = find_cc(tt);
2902 if (cc == -1) {
2903 error(ERR_NONFATAL,
2904 "macro parameter %d is not a condition code",
2905 n + 1);
2906 text = NULL;
2907 } else {
2908 type = TOK_ID;
2909 if (inverse_ccs[cc] == -1) {
2910 error(ERR_NONFATAL,
2911 "condition code `%s' is not invertible",
2912 conditions[cc]);
2913 text = NULL;
2914 } else
2915 text =
2916 nasm_strdup(conditions[inverse_ccs[cc]]);
2917 }
2918 break;
2919 case '+':
2920 n = atoi(t->text + 2) - 1;
2921 if (n >= mac->nparam)
2922 tt = NULL;
2923 else {
2924 if (mac->nparam > 1)
2925 n = (n + mac->rotate) % mac->nparam;
2926 tt = mac->params[n];
2927 }
2928 cc = find_cc(tt);
2929 if (cc == -1) {
2930 error(ERR_NONFATAL,
2931 "macro parameter %d is not a condition code",
2932 n + 1);
2933 text = NULL;
2934 } else {
2935 type = TOK_ID;
2936 text = nasm_strdup(conditions[cc]);
2937 }
2938 break;
2939 default:
2940 n = atoi(t->text + 1) - 1;
2941 if (n >= mac->nparam)
2942 tt = NULL;
2943 else {
2944 if (mac->nparam > 1)
2945 n = (n + mac->rotate) % mac->nparam;
2946 tt = mac->params[n];
2947 }
2948 if (tt) {
2949 for (i = 0; i < mac->paramlen[n]; i++) {
2950 *tail = new_Token(NULL, tt->type, tt->text, 0);
2951 tail = &(*tail)->next;
2952 tt = tt->next;
2953 }
2954 }
2955 text = NULL; /* we've done it here */
2956 break;
2957 }
2958 if (!text) {
2959 delete_Token(t);
2960 } else {
2961 *tail = t;
2962 tail = &t->next;
2963 t->type = type;
2964 nasm_free(t->text);
2965 t->text = text;
2966 t->mac = NULL;
2967 }
2968 continue;
2969 } else {
2970 t = *tail = tline;
2971 tline = tline->next;
2972 t->mac = NULL;
2973 tail = &t->next;
2974 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002975 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00002976 *tail = NULL;
2977 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002978 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002979 switch (t->type) {
2980 case TOK_WHITESPACE:
2981 if (tt->type == TOK_WHITESPACE) {
2982 t->next = delete_Token(tt);
2983 }
2984 break;
2985 case TOK_ID:
2986 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002987 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002988 nasm_free(t->text);
2989 t->text = tmp;
2990 t->next = delete_Token(tt);
2991 }
2992 break;
2993 case TOK_NUMBER:
2994 if (tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002995 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002996 nasm_free(t->text);
2997 t->text = tmp;
2998 t->next = delete_Token(tt);
2999 }
3000 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003001 default:
3002 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003003 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003004
H. Peter Anvin76690a12002-04-30 20:52:49 +00003005 return thead;
3006}
3007
3008/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003009 * Expand all single-line macro calls made in the given line.
3010 * Return the expanded version of the line. The original is deemed
3011 * to be destroyed in the process. (In reality we'll just move
3012 * Tokens from input to output a lot of the time, rather than
3013 * actually bothering to destroy and replicate.)
3014 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003015#define DEADMAN_LIMIT (1 << 20)
3016
H. Peter Anvine2c80182005-01-15 22:15:51 +00003017static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003018{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003019 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003020 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003021 Token **params;
3022 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003023 unsigned int nparam, sparam;
3024 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003025 Token *org_tline = tline;
3026 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003027 char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003028 int deadman = DEADMAN_LIMIT;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003029
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003030 /*
3031 * Trick: we should avoid changing the start token pointer since it can
3032 * be contained in "next" field of other token. Because of this
3033 * we allocate a copy of first token and work with it; at the end of
3034 * routine we copy it back
3035 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003036 if (org_tline) {
3037 tline =
3038 new_Token(org_tline->next, org_tline->type, org_tline->text,
3039 0);
3040 tline->mac = org_tline->mac;
3041 nasm_free(org_tline->text);
3042 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003043 }
3044
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003045again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003046 tail = &thead;
3047 thead = NULL;
3048
H. Peter Anvine2c80182005-01-15 22:15:51 +00003049 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003050 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003051 error(ERR_NONFATAL, "interminable macro recursion");
3052 break;
3053 }
3054
H. Peter Anvine2c80182005-01-15 22:15:51 +00003055 if ((mname = tline->text)) {
3056 /* if this token is a local macro, look in local context */
3057 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003058 ctx = get_ctx(mname, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003059 else
3060 ctx = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07003061
3062 head = (SMacro *) hash_findix(ctx ? ctx->localmac : smacros,
3063 mname);
3064
H. Peter Anvine2c80182005-01-15 22:15:51 +00003065 /*
3066 * We've hit an identifier. As in is_mmacro below, we first
3067 * check whether the identifier is a single-line macro at
3068 * all, then think about checking for parameters if
3069 * necessary.
3070 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003071 for (m = head; m; m = m->next)
3072 if (!mstrcmp(m->name, mname, m->casesense))
3073 break;
3074 if (m) {
3075 mstart = tline;
3076 params = NULL;
3077 paramsize = NULL;
3078 if (m->nparam == 0) {
3079 /*
3080 * Simple case: the macro is parameterless. Discard the
3081 * one token that the macro call took, and push the
3082 * expansion back on the to-do stack.
3083 */
3084 if (!m->expansion) {
3085 if (!strcmp("__FILE__", m->name)) {
3086 int32_t num = 0;
3087 src_get(&num, &(tline->text));
3088 nasm_quote(&(tline->text));
3089 tline->type = TOK_STRING;
3090 continue;
3091 }
3092 if (!strcmp("__LINE__", m->name)) {
3093 nasm_free(tline->text);
3094 make_tok_num(tline, src_get_linnum());
3095 continue;
3096 }
3097 if (!strcmp("__BITS__", m->name)) {
3098 nasm_free(tline->text);
3099 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003100 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003101 }
3102 tline = delete_Token(tline);
3103 continue;
3104 }
3105 } else {
3106 /*
3107 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003108 * exists and takes parameters. We must find the
3109 * parameters in the call, count them, find the SMacro
3110 * that corresponds to that form of the macro call, and
3111 * substitute for the parameters when we expand. What a
3112 * pain.
3113 */
3114 /*tline = tline->next;
3115 skip_white_(tline); */
3116 do {
3117 t = tline->next;
3118 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003119 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003120 t->text = NULL;
3121 t = tline->next = delete_Token(t);
3122 }
3123 tline = t;
3124 } while (tok_type_(tline, TOK_WHITESPACE));
3125 if (!tok_is_(tline, "(")) {
3126 /*
3127 * This macro wasn't called with parameters: ignore
3128 * the call. (Behaviour borrowed from gnu cpp.)
3129 */
3130 tline = mstart;
3131 m = NULL;
3132 } else {
3133 int paren = 0;
3134 int white = 0;
3135 brackets = 0;
3136 nparam = 0;
3137 sparam = PARAM_DELTA;
3138 params = nasm_malloc(sparam * sizeof(Token *));
3139 params[0] = tline->next;
3140 paramsize = nasm_malloc(sparam * sizeof(int));
3141 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003142 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003143 /*
3144 * For some unusual expansions
3145 * which concatenates function call
3146 */
3147 t = tline->next;
3148 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003149 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003150 t->text = NULL;
3151 t = tline->next = delete_Token(t);
3152 }
3153 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003154
H. Peter Anvine2c80182005-01-15 22:15:51 +00003155 if (!tline) {
3156 error(ERR_NONFATAL,
3157 "macro call expects terminating `)'");
3158 break;
3159 }
3160 if (tline->type == TOK_WHITESPACE
3161 && brackets <= 0) {
3162 if (paramsize[nparam])
3163 white++;
3164 else
3165 params[nparam] = tline->next;
3166 continue; /* parameter loop */
3167 }
3168 if (tline->type == TOK_OTHER
3169 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003170 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003171 if (ch == ',' && !paren && brackets <= 0) {
3172 if (++nparam >= sparam) {
3173 sparam += PARAM_DELTA;
3174 params = nasm_realloc(params,
3175 sparam *
3176 sizeof(Token
3177 *));
3178 paramsize =
3179 nasm_realloc(paramsize,
3180 sparam *
3181 sizeof(int));
3182 }
3183 params[nparam] = tline->next;
3184 paramsize[nparam] = 0;
3185 white = 0;
3186 continue; /* parameter loop */
3187 }
3188 if (ch == '{' &&
3189 (brackets > 0 || (brackets == 0 &&
3190 !paramsize[nparam])))
3191 {
3192 if (!(brackets++)) {
3193 params[nparam] = tline->next;
3194 continue; /* parameter loop */
3195 }
3196 }
3197 if (ch == '}' && brackets > 0)
3198 if (--brackets == 0) {
3199 brackets = -1;
3200 continue; /* parameter loop */
3201 }
3202 if (ch == '(' && !brackets)
3203 paren++;
3204 if (ch == ')' && brackets <= 0)
3205 if (--paren < 0)
3206 break;
3207 }
3208 if (brackets < 0) {
3209 brackets = 0;
3210 error(ERR_NONFATAL, "braces do not "
3211 "enclose all of macro parameter");
3212 }
3213 paramsize[nparam] += white + 1;
3214 white = 0;
3215 } /* parameter loop */
3216 nparam++;
3217 while (m && (m->nparam != nparam ||
3218 mstrcmp(m->name, mname,
3219 m->casesense)))
3220 m = m->next;
3221 if (!m)
3222 error(ERR_WARNING | ERR_WARN_MNP,
3223 "macro `%s' exists, "
3224 "but not taking %d parameters",
3225 mstart->text, nparam);
3226 }
3227 }
3228 if (m && m->in_progress)
3229 m = NULL;
3230 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003231 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003232 * Design question: should we handle !tline, which
3233 * indicates missing ')' here, or expand those
3234 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003235 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003236 */
3237 nasm_free(params);
3238 nasm_free(paramsize);
3239 tline = mstart;
3240 } else {
3241 /*
3242 * Expand the macro: we are placed on the last token of the
3243 * call, so that we can easily split the call from the
3244 * following tokens. We also start by pushing an SMAC_END
3245 * token for the cycle removal.
3246 */
3247 t = tline;
3248 if (t) {
3249 tline = t->next;
3250 t->next = NULL;
3251 }
3252 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3253 tt->mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003254 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003255 tline = tt;
3256 for (t = m->expansion; t; t = t->next) {
3257 if (t->type >= TOK_SMAC_PARAM) {
3258 Token *pcopy = tline, **ptail = &pcopy;
3259 Token *ttt, *pt;
3260 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003261
H. Peter Anvine2c80182005-01-15 22:15:51 +00003262 ttt = params[t->type - TOK_SMAC_PARAM];
3263 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3264 --i >= 0;) {
3265 pt = *ptail =
3266 new_Token(tline, ttt->type, ttt->text,
3267 0);
3268 ptail = &pt->next;
3269 ttt = ttt->next;
3270 }
3271 tline = pcopy;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -07003272 } else if (t->type == TOK_PREPROC_Q) {
3273 tt = new_Token(tline, TOK_ID, mname, 0);
3274 tline = tt;
3275 } else if (t->type == TOK_PREPROC_QQ) {
3276 tt = new_Token(tline, TOK_ID, m->name, 0);
3277 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003278 } else {
3279 tt = new_Token(tline, t->type, t->text, 0);
3280 tline = tt;
3281 }
3282 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003283
H. Peter Anvine2c80182005-01-15 22:15:51 +00003284 /*
3285 * Having done that, get rid of the macro call, and clean
3286 * up the parameters.
3287 */
3288 nasm_free(params);
3289 nasm_free(paramsize);
3290 free_tlist(mstart);
3291 continue; /* main token loop */
3292 }
3293 }
3294 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003295
H. Peter Anvine2c80182005-01-15 22:15:51 +00003296 if (tline->type == TOK_SMAC_END) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003297 tline->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003298 tline = delete_Token(tline);
3299 } else {
3300 t = *tail = tline;
3301 tline = tline->next;
3302 t->mac = NULL;
3303 t->next = NULL;
3304 tail = &t->next;
3305 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003306 }
3307
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003308 /*
3309 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003310 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003311 * TOK_IDs should be concatenated.
3312 * Also we look for %+ tokens and concatenate the tokens before and after
3313 * them (without white spaces in between).
3314 */
3315 t = thead;
3316 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003317 while (t) {
3318 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3319 t = t->next;
3320 if (!t || !t->next)
3321 break;
3322 if (t->next->type == TOK_ID ||
3323 t->next->type == TOK_PREPROC_ID ||
3324 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003325 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003326 nasm_free(t->text);
3327 t->next = delete_Token(t->next);
3328 t->text = p;
3329 rescan = 1;
3330 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3331 t->next->next->type == TOK_PREPROC_ID &&
3332 strcmp(t->next->next->text, "%+") == 0) {
3333 /* free the next whitespace, the %+ token and next whitespace */
3334 int i;
3335 for (i = 1; i <= 3; i++) {
3336 if (!t->next
3337 || (i != 2 && t->next->type != TOK_WHITESPACE))
3338 break;
3339 t->next = delete_Token(t->next);
3340 } /* endfor */
3341 } else
3342 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003343 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003344 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003345 if (rescan) {
3346 tline = thead;
3347 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003348 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003349
H. Peter Anvine2c80182005-01-15 22:15:51 +00003350 if (org_tline) {
3351 if (thead) {
3352 *org_tline = *thead;
3353 /* since we just gave text to org_line, don't free it */
3354 thead->text = NULL;
3355 delete_Token(thead);
3356 } else {
3357 /* the expression expanded to empty line;
3358 we can't return NULL for some reasons
3359 we just set the line to a single WHITESPACE token. */
3360 memset(org_tline, 0, sizeof(*org_tline));
3361 org_tline->text = NULL;
3362 org_tline->type = TOK_WHITESPACE;
3363 }
3364 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003365 }
3366
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003367 return thead;
3368}
3369
3370/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003371 * Similar to expand_smacro but used exclusively with macro identifiers
3372 * right before they are fetched in. The reason is that there can be
3373 * identifiers consisting of several subparts. We consider that if there
3374 * are more than one element forming the name, user wants a expansion,
3375 * otherwise it will be left as-is. Example:
3376 *
3377 * %define %$abc cde
3378 *
3379 * the identifier %$abc will be left as-is so that the handler for %define
3380 * will suck it and define the corresponding value. Other case:
3381 *
3382 * %define _%$abc cde
3383 *
3384 * In this case user wants name to be expanded *before* %define starts
3385 * working, so we'll expand %$abc into something (if it has a value;
3386 * otherwise it will be left as-is) then concatenate all successive
3387 * PP_IDs into one.
3388 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003389static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003390{
3391 Token *cur, *oldnext = NULL;
3392
H. Peter Anvin734b1882002-04-30 21:01:08 +00003393 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003394 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003395
3396 cur = tline;
3397 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003398 (cur->next->type == TOK_ID ||
3399 cur->next->type == TOK_PREPROC_ID
3400 || cur->next->type == TOK_NUMBER))
3401 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003402
3403 /* If identifier consists of just one token, don't expand */
3404 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003405 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003406
H. Peter Anvine2c80182005-01-15 22:15:51 +00003407 if (cur) {
3408 oldnext = cur->next; /* Detach the tail past identifier */
3409 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003410 }
3411
H. Peter Anvin734b1882002-04-30 21:01:08 +00003412 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003413
H. Peter Anvine2c80182005-01-15 22:15:51 +00003414 if (cur) {
3415 /* expand_smacro possibly changhed tline; re-scan for EOL */
3416 cur = tline;
3417 while (cur && cur->next)
3418 cur = cur->next;
3419 if (cur)
3420 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003421 }
3422
3423 return tline;
3424}
3425
3426/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003427 * Determine whether the given line constitutes a multi-line macro
3428 * call, and return the MMacro structure called if so. Doesn't have
3429 * to check for an initial label - that's taken care of in
3430 * expand_mmacro - but must check numbers of parameters. Guaranteed
3431 * to be called with tline->type == TOK_ID, so the putative macro
3432 * name is easy to find.
3433 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003434static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003435{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003436 MMacro *head, *m;
3437 Token **params;
3438 int nparam;
3439
H. Peter Anvin97a23472007-09-16 17:57:25 -07003440 head = (MMacro *) hash_findix(mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003441
3442 /*
3443 * Efficiency: first we see if any macro exists with the given
3444 * name. If not, we can return NULL immediately. _Then_ we
3445 * count the parameters, and then we look further along the
3446 * list if necessary to find the proper MMacro.
3447 */
3448 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003449 if (!mstrcmp(m->name, tline->text, m->casesense))
3450 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003451 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003452 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003453
3454 /*
3455 * OK, we have a potential macro. Count and demarcate the
3456 * parameters.
3457 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003458 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003459
3460 /*
3461 * So we know how many parameters we've got. Find the MMacro
3462 * structure that handles this number.
3463 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003464 while (m) {
3465 if (m->nparam_min <= nparam
3466 && (m->plus || nparam <= m->nparam_max)) {
3467 /*
3468 * This one is right. Just check if cycle removal
3469 * prohibits us using it before we actually celebrate...
3470 */
3471 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003472#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003473 error(ERR_NONFATAL,
3474 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003475#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003476 nasm_free(params);
3477 return NULL;
3478 }
3479 /*
3480 * It's right, and we can use it. Add its default
3481 * parameters to the end of our list if necessary.
3482 */
3483 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3484 params =
3485 nasm_realloc(params,
3486 ((m->nparam_min + m->ndefs +
3487 1) * sizeof(*params)));
3488 while (nparam < m->nparam_min + m->ndefs) {
3489 params[nparam] = m->defaults[nparam - m->nparam_min];
3490 nparam++;
3491 }
3492 }
3493 /*
3494 * If we've gone over the maximum parameter count (and
3495 * we're in Plus mode), ignore parameters beyond
3496 * nparam_max.
3497 */
3498 if (m->plus && nparam > m->nparam_max)
3499 nparam = m->nparam_max;
3500 /*
3501 * Then terminate the parameter list, and leave.
3502 */
3503 if (!params) { /* need this special case */
3504 params = nasm_malloc(sizeof(*params));
3505 nparam = 0;
3506 }
3507 params[nparam] = NULL;
3508 *params_array = params;
3509 return m;
3510 }
3511 /*
3512 * This one wasn't right: look for the next one with the
3513 * same name.
3514 */
3515 for (m = m->next; m; m = m->next)
3516 if (!mstrcmp(m->name, tline->text, m->casesense))
3517 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003518 }
3519
3520 /*
3521 * After all that, we didn't find one with the right number of
3522 * parameters. Issue a warning, and fail to expand the macro.
3523 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003524 error(ERR_WARNING | ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003525 "macro `%s' exists, but not taking %d parameters",
3526 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003527 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003528 return NULL;
3529}
3530
3531/*
3532 * Expand the multi-line macro call made by the given line, if
3533 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003534 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003535 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003536static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003537{
3538 Token *startline = tline;
3539 Token *label = NULL;
3540 int dont_prepend = 0;
3541 Token **params, *t, *tt;
3542 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003543 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003544 int i, nparam, *paramlen;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003545
3546 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003547 skip_white_(t);
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003548/* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3549 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003550 return 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003551 m = is_mmacro(t, &params);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003552 if (!m) {
3553 Token *last;
3554 /*
3555 * We have an id which isn't a macro call. We'll assume
3556 * it might be a label; we'll also check to see if a
3557 * colon follows it. Then, if there's another id after
3558 * that lot, we'll check it again for macro-hood.
3559 */
3560 label = last = t;
3561 t = t->next;
3562 if (tok_type_(t, TOK_WHITESPACE))
3563 last = t, t = t->next;
3564 if (tok_is_(t, ":")) {
3565 dont_prepend = 1;
3566 last = t, t = t->next;
3567 if (tok_type_(t, TOK_WHITESPACE))
3568 last = t, t = t->next;
3569 }
3570 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3571 return 0;
3572 last->next = NULL;
3573 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003574 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003575
3576 /*
3577 * Fix up the parameters: this involves stripping leading and
3578 * trailing whitespace, then stripping braces if they are
3579 * present.
3580 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003581 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003582 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003583
H. Peter Anvine2c80182005-01-15 22:15:51 +00003584 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003585 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003586 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003587
H. Peter Anvine2c80182005-01-15 22:15:51 +00003588 t = params[i];
3589 skip_white_(t);
3590 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003591 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003592 params[i] = t;
3593 paramlen[i] = 0;
3594 while (t) {
3595 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3596 break; /* ... because we have hit a comma */
3597 if (comma && t->type == TOK_WHITESPACE
3598 && tok_is_(t->next, ","))
3599 break; /* ... or a space then a comma */
3600 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3601 break; /* ... or a brace */
3602 t = t->next;
3603 paramlen[i]++;
3604 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003605 }
3606
3607 /*
3608 * OK, we have a MMacro structure together with a set of
3609 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003610 * copies of each Line on to istk->expansion. Substitution of
3611 * parameter tokens and macro-local tokens doesn't get done
3612 * until the single-line macro substitution process; this is
3613 * because delaying them allows us to change the semantics
3614 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003615 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003616 * First, push an end marker on to istk->expansion, mark this
3617 * macro as in progress, and set up its invocation-specific
3618 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003619 */
3620 ll = nasm_malloc(sizeof(Line));
3621 ll->next = istk->expansion;
3622 ll->finishes = m;
3623 ll->first = NULL;
3624 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003625
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003626 m->in_progress = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003627 m->params = params;
3628 m->iline = tline;
3629 m->nparam = nparam;
3630 m->rotate = 0;
3631 m->paramlen = paramlen;
3632 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003633 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003634
3635 m->next_active = istk->mstk;
3636 istk->mstk = m;
3637
H. Peter Anvine2c80182005-01-15 22:15:51 +00003638 for (l = m->expansion; l; l = l->next) {
3639 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003640
H. Peter Anvine2c80182005-01-15 22:15:51 +00003641 ll = nasm_malloc(sizeof(Line));
3642 ll->finishes = NULL;
3643 ll->next = istk->expansion;
3644 istk->expansion = ll;
3645 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003646
H. Peter Anvine2c80182005-01-15 22:15:51 +00003647 for (t = l->first; t; t = t->next) {
3648 Token *x = t;
3649 if (t->type == TOK_PREPROC_ID &&
3650 t->text[1] == '0' && t->text[2] == '0') {
3651 dont_prepend = -1;
3652 x = label;
3653 if (!x)
3654 continue;
3655 }
3656 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3657 tail = &tt->next;
3658 }
3659 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003660 }
3661
3662 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00003663 * If we had a label, push it on as the first line of
3664 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003665 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003666 if (label) {
3667 if (dont_prepend < 0)
3668 free_tlist(startline);
3669 else {
3670 ll = nasm_malloc(sizeof(Line));
3671 ll->finishes = NULL;
3672 ll->next = istk->expansion;
3673 istk->expansion = ll;
3674 ll->first = startline;
3675 if (!dont_prepend) {
3676 while (label->next)
3677 label = label->next;
3678 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3679 }
3680 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003681 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003682
H. Peter Anvin734b1882002-04-30 21:01:08 +00003683 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003684
H. Peter Anvineba20a72002-04-30 20:53:55 +00003685 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003686}
3687
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003688/*
3689 * Since preprocessor always operate only on the line that didn't
3690 * arrived yet, we should always use ERR_OFFBY1. Also since user
3691 * won't want to see same error twice (preprocessing is done once
3692 * per pass) we will want to show errors only during pass one.
3693 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003694static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003695{
3696 va_list arg;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003697 char buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003698
3699 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003700 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003701 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003702
H. Peter Anvin734b1882002-04-30 21:01:08 +00003703 va_start(arg, fmt);
Ed Beroset19f927a2004-12-15 17:07:03 +00003704 vsnprintf(buff, sizeof(buff), fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003705 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003706
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003707 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003708 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3709 istk->mstk->lineno, buff);
3710 else
3711 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003712}
3713
H. Peter Anvin734b1882002-04-30 21:01:08 +00003714static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003715pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003716 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003717{
H. Peter Anvin99941bf2002-05-14 17:44:03 +00003718 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003719 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003720 istk = nasm_malloc(sizeof(Include));
3721 istk->next = NULL;
3722 istk->conds = NULL;
3723 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003724 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003725 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00003726 istk->fname = NULL;
3727 src_set_fname(nasm_strdup(file));
3728 src_set_linnum(0);
3729 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003730 if (!istk->fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003731 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3732 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003733 defining = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003734 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003735 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003736 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07003737 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003738 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07003739 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003740 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003741 any_extrastdmac = (extrastdmac != NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003742 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003743 evaluate = eval;
3744 pass = apass;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003745}
3746
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003747static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003748{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003749 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003750 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003751
H. Peter Anvine2c80182005-01-15 22:15:51 +00003752 while (1) {
3753 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003754 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00003755 * buffer or from the input file.
3756 */
3757 tline = NULL;
3758 while (istk->expansion && istk->expansion->finishes) {
3759 Line *l = istk->expansion;
3760 if (!l->finishes->name && l->finishes->in_progress > 1) {
3761 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003762
H. Peter Anvine2c80182005-01-15 22:15:51 +00003763 /*
3764 * This is a macro-end marker for a macro with no
3765 * name, which means it's not really a macro at all
3766 * but a %rep block, and the `in_progress' field is
3767 * more than 1, meaning that we still need to
3768 * repeat. (1 means the natural last repetition; 0
3769 * means termination by %exitrep.) We have
3770 * therefore expanded up to the %endrep, and must
3771 * push the whole block on to the expansion buffer
3772 * again. We don't bother to remove the macro-end
3773 * marker: we'd only have to generate another one
3774 * if we did.
3775 */
3776 l->finishes->in_progress--;
3777 for (l = l->finishes->expansion; l; l = l->next) {
3778 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003779
H. Peter Anvine2c80182005-01-15 22:15:51 +00003780 ll = nasm_malloc(sizeof(Line));
3781 ll->next = istk->expansion;
3782 ll->finishes = NULL;
3783 ll->first = NULL;
3784 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003785
H. Peter Anvine2c80182005-01-15 22:15:51 +00003786 for (t = l->first; t; t = t->next) {
3787 if (t->text || t->type == TOK_WHITESPACE) {
3788 tt = *tail =
3789 new_Token(NULL, t->type, t->text, 0);
3790 tail = &tt->next;
3791 }
3792 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003793
H. Peter Anvine2c80182005-01-15 22:15:51 +00003794 istk->expansion = ll;
3795 }
3796 } else {
3797 /*
3798 * Check whether a `%rep' was started and not ended
3799 * within this macro expansion. This can happen and
3800 * should be detected. It's a fatal error because
3801 * I'm too confused to work out how to recover
3802 * sensibly from it.
3803 */
3804 if (defining) {
3805 if (defining->name)
3806 error(ERR_PANIC,
3807 "defining with name in expansion");
3808 else if (istk->mstk->name)
3809 error(ERR_FATAL,
3810 "`%%rep' without `%%endrep' within"
3811 " expansion of macro `%s'",
3812 istk->mstk->name);
3813 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00003814
H. Peter Anvine2c80182005-01-15 22:15:51 +00003815 /*
3816 * FIXME: investigate the relationship at this point between
3817 * istk->mstk and l->finishes
3818 */
3819 {
3820 MMacro *m = istk->mstk;
3821 istk->mstk = m->next_active;
3822 if (m->name) {
3823 /*
3824 * This was a real macro call, not a %rep, and
3825 * therefore the parameter information needs to
3826 * be freed.
3827 */
3828 nasm_free(m->params);
3829 free_tlist(m->iline);
3830 nasm_free(m->paramlen);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003831 l->finishes->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003832 } else
3833 free_mmacro(m);
3834 }
3835 istk->expansion = l->next;
3836 nasm_free(l);
3837 list->downlevel(LIST_MACRO);
3838 }
3839 }
3840 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003841
H. Peter Anvine2c80182005-01-15 22:15:51 +00003842 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003843 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003844 Line *l = istk->expansion;
3845 if (istk->mstk)
3846 istk->mstk->lineno++;
3847 tline = l->first;
3848 istk->expansion = l->next;
3849 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003850 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003851 list->line(LIST_MACRO, p);
3852 nasm_free(p);
3853 break;
3854 }
3855 line = read_line();
3856 if (line) { /* from the current input file */
3857 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00003858 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003859 nasm_free(line);
3860 break;
3861 }
3862 /*
3863 * The current file has ended; work down the istk
3864 */
3865 {
3866 Include *i = istk;
3867 fclose(i->fp);
3868 if (i->conds)
3869 error(ERR_FATAL,
3870 "expected `%%endif' before end of file");
3871 /* only set line and file name if there's a next node */
3872 if (i->next) {
3873 src_set_linnum(i->lineno);
3874 nasm_free(src_set_fname(i->fname));
3875 }
3876 istk = i->next;
3877 list->downlevel(LIST_INCLUDE);
3878 nasm_free(i);
3879 if (!istk)
3880 return NULL;
3881 }
3882 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003883
H. Peter Anvine2c80182005-01-15 22:15:51 +00003884 /*
3885 * We must expand MMacro parameters and MMacro-local labels
3886 * _before_ we plunge into directive processing, to cope
3887 * with things like `%define something %1' such as STRUC
3888 * uses. Unless we're _defining_ a MMacro, in which case
3889 * those tokens should be left alone to go into the
3890 * definition; and unless we're in a non-emitting
3891 * condition, in which case we don't want to meddle with
3892 * anything.
3893 */
3894 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
3895 tline = expand_mmac_params(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003896
H. Peter Anvine2c80182005-01-15 22:15:51 +00003897 /*
3898 * Check the line to see if it's a preprocessor directive.
3899 */
3900 if (do_directive(tline) == DIRECTIVE_FOUND) {
3901 continue;
3902 } else if (defining) {
3903 /*
3904 * We're defining a multi-line macro. We emit nothing
3905 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00003906 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003907 */
3908 Line *l = nasm_malloc(sizeof(Line));
3909 l->next = defining->expansion;
3910 l->first = tline;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003911 l->finishes = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003912 defining->expansion = l;
3913 continue;
3914 } else if (istk->conds && !emitting(istk->conds->state)) {
3915 /*
3916 * We're in a non-emitting branch of a condition block.
3917 * Emit nothing at all, not even a blank line: when we
3918 * emerge from the condition we'll give a line-number
3919 * directive so we keep our place correctly.
3920 */
3921 free_tlist(tline);
3922 continue;
3923 } else if (istk->mstk && !istk->mstk->in_progress) {
3924 /*
3925 * We're in a %rep block which has been terminated, so
3926 * we're walking through to the %endrep without
3927 * emitting anything. Emit nothing at all, not even a
3928 * blank line: when we emerge from the %rep block we'll
3929 * give a line-number directive so we keep our place
3930 * correctly.
3931 */
3932 free_tlist(tline);
3933 continue;
3934 } else {
3935 tline = expand_smacro(tline);
3936 if (!expand_mmacro(tline)) {
3937 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003938 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003939 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003940 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003941 free_tlist(tline);
3942 break;
3943 } else {
3944 continue; /* expand_mmacro calls free_tlist */
3945 }
3946 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003947 }
3948
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003949 return line;
3950}
3951
H. Peter Anvine2c80182005-01-15 22:15:51 +00003952static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003953{
H. Peter Anvine2c80182005-01-15 22:15:51 +00003954 if (defining) {
3955 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
3956 defining->name);
3957 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003958 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003959 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003960 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07003961 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00003962 while (istk) {
3963 Include *i = istk;
3964 istk = istk->next;
3965 fclose(i->fp);
3966 nasm_free(i->fname);
3967 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003968 }
3969 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003970 ctx_pop();
3971 if (pass == 0) {
3972 free_llist(predef);
3973 delete_Blocks();
3974 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003975}
3976
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003977void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003978{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003979 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07003980
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003981 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07003982 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003983 i->next = NULL;
3984
H. Peter Anvine2c80182005-01-15 22:15:51 +00003985 if (ipath != NULL) {
3986 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003987 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003988 j = j->next;
3989 j->next = i;
3990 } else {
3991 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003992 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003993}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003994
3995/*
3996 * added by alexfru:
3997 *
3998 * This function is used to "export" the include paths, e.g.
3999 * the paths specified in the '-I' command switch.
4000 * The need for such exporting is due to the 'incbin' directive,
4001 * which includes raw binary files (unlike '%include', which
4002 * includes text source files). It would be real nice to be
4003 * able to specify paths to search for incbin'ned files also.
4004 * So, this is a simple workaround.
4005 *
4006 * The function use is simple:
4007 *
4008 * The 1st call (with NULL argument) returns a pointer to the 1st path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004009 * (char** type) or NULL if none include paths available.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004010 *
4011 * All subsequent calls take as argument the value returned by this
4012 * function last. The return value is either the next path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004013 * (char** type) or NULL if the end of the paths list is reached.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004014 *
4015 * It is maybe not the best way to do things, but I didn't want
4016 * to export too much, just one or two functions and no types or
4017 * variables exported.
4018 *
4019 * Can't say I like the current situation with e.g. this path list either,
4020 * it seems to be never deallocated after creation...
4021 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004022char **pp_get_include_path_ptr(char **pPrevPath)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004023{
4024/* This macro returns offset of a member of a structure */
4025#define GetMemberOffset(StructType,MemberName)\
4026 ((size_t)&((StructType*)0)->MemberName)
4027 IncPath *i;
4028
H. Peter Anvine2c80182005-01-15 22:15:51 +00004029 if (pPrevPath == NULL) {
4030 if (ipath != NULL)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004031 return &ipath->path;
4032 else
4033 return NULL;
4034 }
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004035 i = (IncPath *) ((char *)pPrevPath - GetMemberOffset(IncPath, path));
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004036 i = i->next;
4037 if (i != NULL)
4038 return &i->path;
4039 else
4040 return NULL;
4041#undef GetMemberOffset
4042}
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004043
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004044void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004045{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004046 Token *inc, *space, *name;
4047 Line *l;
4048
H. Peter Anvin734b1882002-04-30 21:01:08 +00004049 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4050 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4051 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004052
4053 l = nasm_malloc(sizeof(Line));
4054 l->next = predef;
4055 l->first = inc;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004056 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004057 predef = l;
4058}
4059
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004060void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004061{
4062 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004063 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004064 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004065
4066 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004067 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4068 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004069 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004070 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004071 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004072 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004073 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004074
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004075 l = nasm_malloc(sizeof(Line));
4076 l->next = predef;
4077 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004078 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004079 predef = l;
4080}
4081
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004082void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004083{
4084 Token *def, *space;
4085 Line *l;
4086
H. Peter Anvin734b1882002-04-30 21:01:08 +00004087 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4088 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004089 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004090
4091 l = nasm_malloc(sizeof(Line));
4092 l->next = predef;
4093 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004094 l->finishes = false;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004095 predef = l;
4096}
4097
Keith Kaniosb7a89542007-04-12 02:40:54 +00004098/*
4099 * Added by Keith Kanios:
4100 *
4101 * This function is used to assist with "runtime" preprocessor
4102 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4103 *
4104 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4105 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4106 */
4107
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004108void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004109{
4110 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004111
Keith Kaniosb7a89542007-04-12 02:40:54 +00004112 def = tokenize(definition);
4113 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4114 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004115
Keith Kaniosb7a89542007-04-12 02:40:54 +00004116}
4117
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004118void pp_extra_stdmac(const char **macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004119{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004120 extrastdmac = macros;
4121}
4122
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004123static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004124{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004125 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004126 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004127 tok->text = nasm_strdup(numbuf);
4128 tok->type = TOK_NUMBER;
4129}
4130
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004131Preproc nasmpp = {
4132 pp_reset,
4133 pp_getline,
4134 pp_cleanup
4135};