blob: ba4778c470882b877a9be5c0008983f0db44755d [file] [log] [blame]
H. Peter Anvin65747262002-05-07 00:10:05 +00001/* -*- mode: c; c-file-style: "bsd" -*- */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002/* preproc.c macro preprocessor for the Netwide Assembler
3 *
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the licence given in the file "Licence"
7 * distributed in the NASM archive.
8 *
9 * initial version 18/iii/97 by Simon Tatham
10 */
11
H. Peter Anvin4836e332002-04-30 20:56:43 +000012/* Typical flow of text through preproc
13 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000014 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000015 *
16 * from a macro expansion
17 *
18 * or
19 * {
20 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000021 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000022 * }
23 *
24 * expand_mmac_params is used to expand %1 etc., unless a macro is being
25 * defined or a false conditional is being processed
26 * (%0, %1, %+1, %-1, %%foo
27 *
28 * do_directive checks for directives
29 *
30 * expand_smacro is used to expand single line macros
31 *
32 * expand_mmacro is used to expand multi-line macros
33 *
34 * detoken is used to convert the line back to text
35 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000036
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000037#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000038#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000039#include <stdlib.h>
40#include <stddef.h>
41#include <string.h>
42#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000043#include <limits.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000044#include <inttypes.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000045
46#include "nasm.h"
47#include "nasmlib.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000048#include "preproc.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000049
50typedef struct SMacro SMacro;
51typedef struct MMacro MMacro;
52typedef struct Context Context;
53typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000054typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000055typedef struct Line Line;
56typedef struct Include Include;
57typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000058typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000059
60/*
61 * Store the definition of a single-line macro.
62 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000063struct SMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000064 SMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +000065 char *name;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000066 int casesense;
67 int nparam;
68 int in_progress;
69 Token *expansion;
70};
71
72/*
H. Peter Anvin76690a12002-04-30 20:52:49 +000073 * Store the definition of a multi-line macro. This is also used to
74 * store the interiors of `%rep...%endrep' blocks, which are
75 * effectively self-re-invoking multi-line macros which simply
76 * don't have a name or bother to appear in the hash tables. %rep
77 * blocks are signified by having a NULL `name' field.
78 *
79 * In a MMacro describing a `%rep' block, the `in_progress' field
80 * isn't merely boolean, but gives the number of repeats left to
81 * run.
82 *
83 * The `next' field is used for storing MMacros in hash tables; the
84 * `next_active' field is for stacking them on istk entries.
85 *
86 * When a MMacro is being expanded, `params', `iline', `nparam',
87 * `paramlen', `rotate' and `unique' are local to the invocation.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000088 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000089struct MMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000090 MMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +000091 char *name;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000092 int casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -070093 int nparam_min, nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +000094 int plus; /* is the last parameter greedy? */
95 int nolist; /* is this macro listing-inhibited? */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000096 int in_progress;
H. Peter Anvine2c80182005-01-15 22:15:51 +000097 Token *dlist; /* All defaults as one list */
98 Token **defaults; /* Parameter default pointers */
99 int ndefs; /* number of default parameters */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000100 Line *expansion;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000101
102 MMacro *next_active;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000103 MMacro *rep_nest; /* used for nesting %rep */
104 Token **params; /* actual parameters */
105 Token *iline; /* invocation line */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000106 int nparam, rotate, *paramlen;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000107 uint32_t unique;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000108 int lineno; /* Current line number on expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000109};
110
111/*
112 * The context stack is composed of a linked list of these.
113 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000114struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000115 Context *next;
116 SMacro *localmac;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000117 char *name;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000118 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000119};
120
121/*
122 * This is the internal form which we break input lines up into.
123 * Typically stored in linked lists.
124 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000125 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
126 * necessarily used as-is, but is intended to denote the number of
127 * the substituted parameter. So in the definition
128 *
129 * %define a(x,y) ( (x) & ~(y) )
130 *
131 * the token representing `x' will have its type changed to
132 * TOK_SMAC_PARAM, but the one representing `y' will be
133 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000134 *
135 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
136 * which doesn't need quotes around it. Used in the pre-include
137 * mechanism as an alternative to trying to find a sensible type of
138 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000139 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000140enum pp_token_type {
141 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
142 TOK_PREPROC_ID, TOK_STRING,
143 TOK_NUMBER, TOK_SMAC_END, TOK_OTHER, TOK_SMAC_PARAM,
144 TOK_INTERNAL_STRING
145};
146
H. Peter Anvine2c80182005-01-15 22:15:51 +0000147struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000148 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000149 char *text;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000150 SMacro *mac; /* associated macro for TOK_SMAC_END */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000151 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000152};
153
154/*
155 * Multi-line macro definitions are stored as a linked list of
156 * these, which is essentially a container to allow several linked
157 * lists of Tokens.
158 *
159 * Note that in this module, linked lists are treated as stacks
160 * wherever possible. For this reason, Lines are _pushed_ on to the
161 * `expansion' field in MMacro structures, so that the linked list,
162 * if walked, would give the macro lines in reverse order; this
163 * means that we can walk the list when expanding a macro, and thus
164 * push the lines on to the `expansion' field in _istk_ in reverse
165 * order (so that when popped back off they are in the right
166 * order). It may seem cockeyed, and it relies on my design having
167 * an even number of steps in, but it works...
168 *
169 * Some of these structures, rather than being actual lines, are
170 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000171 * This is for use in the cycle-tracking and %rep-handling code.
172 * Such structures have `finishes' non-NULL, and `first' NULL. All
173 * others have `finishes' NULL, but `first' may still be NULL if
174 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000175 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000176struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000177 Line *next;
178 MMacro *finishes;
179 Token *first;
180};
181
182/*
183 * To handle an arbitrary level of file inclusion, we maintain a
184 * stack (ie linked list) of these things.
185 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000186struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000187 Include *next;
188 FILE *fp;
189 Cond *conds;
190 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000191 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000192 int lineno, lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000193 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000194};
195
196/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000197 * Include search path. This is simply a list of strings which get
198 * prepended, in turn, to the name of an include file, in an
199 * attempt to find the file if it's not in the current directory.
200 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000201struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000202 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000203 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000204};
205
206/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000207 * Conditional assembly: we maintain a separate stack of these for
208 * each level of file inclusion. (The only reason we keep the
209 * stacks separate is to ensure that a stray `%endif' in a file
210 * included from within the true branch of a `%if' won't terminate
211 * it and cause confusion: instead, rightly, it'll cause an error.)
212 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000213struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000214 Cond *next;
215 int state;
216};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000217enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000218 /*
219 * These states are for use just after %if or %elif: IF_TRUE
220 * means the condition has evaluated to truth so we are
221 * currently emitting, whereas IF_FALSE means we are not
222 * currently emitting but will start doing so if a %else comes
223 * up. In these states, all directives are admissible: %elif,
224 * %else and %endif. (And of course %if.)
225 */
226 COND_IF_TRUE, COND_IF_FALSE,
227 /*
228 * These states come up after a %else: ELSE_TRUE means we're
229 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
230 * any %elif or %else will cause an error.
231 */
232 COND_ELSE_TRUE, COND_ELSE_FALSE,
233 /*
234 * This state means that we're not emitting now, and also that
235 * nothing until %endif will be emitted at all. It's for use in
236 * two circumstances: (i) when we've had our moment of emission
237 * and have now started seeing %elifs, and (ii) when the
238 * condition construct in question is contained within a
239 * non-emitting branch of a larger condition construct.
240 */
241 COND_NEVER
242};
243#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
244
Ed Beroset3ab3f412002-06-11 03:31:49 +0000245/*
246 * These defines are used as the possible return values for do_directive
247 */
248#define NO_DIRECTIVE_FOUND 0
249#define DIRECTIVE_FOUND 1
250
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000251/*
252 * Condition codes. Note that we use c_ prefix not C_ because C_ is
253 * used in nasm.h for the "real" condition codes. At _this_ level,
254 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
255 * ones, so we need a different enum...
256 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000257static const char *conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000258 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
259 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000260 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000261};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000262enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000263 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
264 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
H. Peter Anvince9be342007-09-12 00:22:29 +0000265 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000266};
267static int inverse_ccs[] = {
268 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
269 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 +0000270 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000271};
272
H. Peter Anvin76690a12002-04-30 20:52:49 +0000273/*
274 * Directive names.
275 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000276/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000277static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000278{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000279 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000280}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000281
282/* For TASM compatibility we need to be able to recognise TASM compatible
283 * conditional compilation directives. Using the NASM pre-processor does
284 * not work, so we look for them specifically from the following list and
285 * then jam in the equivalent NASM directive into the input stream.
286 */
287
288#ifndef MAX
289# define MAX(a,b) ( ((a) > (b)) ? (a) : (b))
290#endif
291
H. Peter Anvine2c80182005-01-15 22:15:51 +0000292enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000293 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
294 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
295};
296
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000297static const char *tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000298 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
299 "ifndef", "include", "local"
300};
301
302static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000303static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000304static int ArgOffset = 8;
305static int LocalOffset = 4;
306
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000307static Context *cstk;
308static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000309static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000310
H. Peter Anvine2c80182005-01-15 22:15:51 +0000311static efunc _error; /* Pointer to client-provided error reporting function */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000312static evalfunc evaluate;
313
H. Peter Anvine2c80182005-01-15 22:15:51 +0000314static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000315
Keith Kaniosb7a89542007-04-12 02:40:54 +0000316static uint32_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000317
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000318static Line *predef = NULL;
319
320static ListGen *list;
321
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000322/*
323 * The number of hash values we use for the macro lookup tables.
H. Peter Anvineba20a72002-04-30 20:53:55 +0000324 * FIXME: We should *really* be able to configure this at run time,
325 * or even have the hash table automatically expanding when necessary.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000326 */
327#define NHASH 31
328
329/*
330 * The current set of multi-line macros we have defined.
331 */
332static MMacro *mmacros[NHASH];
333
334/*
335 * The current set of single-line macros we have defined.
336 */
337static SMacro *smacros[NHASH];
338
339/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000340 * The multi-line macro we are currently defining, or the %rep
341 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000342 */
343static MMacro *defining;
344
345/*
346 * The number of macro parameters to allocate space for at a time.
347 */
348#define PARAM_DELTA 16
349
350/*
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000351 * The standard macro set: defined as `static char *stdmac[]'. Also
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000352 * gives our position in the macro set, when we're processing it.
353 */
354#include "macros.c"
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000355static const char **stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000356
357/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000358 * The extra standard macros that come from the object format, if
359 * any.
360 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000361static const char **extrastdmac = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000362int any_extrastdmac;
363
364/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000365 * Tokens are allocated in blocks to improve speed
366 */
367#define TOKEN_BLOCKSIZE 4096
368static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000369struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000370 Blocks *next;
371 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000372};
373
374static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000375
376/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000377 * Forward declarations.
378 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000379static Token *expand_mmac_params(Token * tline);
380static Token *expand_smacro(Token * tline);
381static Token *expand_id(Token * tline);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000382static Context *get_ctx(char *name, int all_contexts);
Keith Kaniosb7a89542007-04-12 02:40:54 +0000383static void make_tok_num(Token * tok, int32_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000384static void error(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000385static void *new_Block(size_t size);
386static void delete_Blocks(void);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000387static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000388static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000389
390/*
391 * Macros for safe checking of token pointers, avoid *(NULL)
392 */
393#define tok_type_(x,t) ((x) && (x)->type == (t))
394#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
395#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
396#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000397
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000398/* Handle TASM specific directives, which do not contain a % in
399 * front of them. We do it here because I could not find any other
400 * place to do it for the moment, and it is a hack (ideally it would
401 * be nice to be able to use the NASM pre-processor to do it).
402 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000403static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000404{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000405 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000406 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000407
408 /* Skip whitespace */
409 while (isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000410 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000411
412 /* Binary search for the directive name */
413 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000414 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000415 len = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000416 while (!isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000417 len++;
418 if (len) {
419 oldchar = p[len];
420 p[len] = 0;
421 while (j - i > 1) {
422 k = (j + i) / 2;
423 m = nasm_stricmp(p, tasm_directives[k]);
424 if (m == 0) {
425 /* We have found a directive, so jam a % in front of it
426 * so that NASM will then recognise it as one if it's own.
427 */
428 p[len] = oldchar;
429 len = strlen(p);
430 oldline = line;
431 line = nasm_malloc(len + 2);
432 line[0] = '%';
433 if (k == TM_IFDIFI) {
434 /* NASM does not recognise IFDIFI, so we convert it to
435 * %ifdef BOGUS. This is not used in NASM comaptible
436 * code, but does need to parse for the TASM macro
437 * package.
438 */
439 strcpy(line + 1, "ifdef BOGUS");
440 } else {
441 memcpy(line + 1, p, len + 1);
442 }
443 nasm_free(oldline);
444 return line;
445 } else if (m < 0) {
446 j = k;
447 } else
448 i = k;
449 }
450 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000451 }
452 return line;
453}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000454
H. Peter Anvin76690a12002-04-30 20:52:49 +0000455/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000456 * The pre-preprocessing stage... This function translates line
457 * number indications as they emerge from GNU cpp (`# lineno "file"
458 * flags') into NASM preprocessor line number indications (`%line
459 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000460 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000461static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000462{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000463 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000464 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000465
H. Peter Anvine2c80182005-01-15 22:15:51 +0000466 if (line[0] == '#' && line[1] == ' ') {
467 oldline = line;
468 fname = oldline + 2;
469 lineno = atoi(fname);
470 fname += strspn(fname, "0123456789 ");
471 if (*fname == '"')
472 fname++;
473 fnlen = strcspn(fname, "\"");
474 line = nasm_malloc(20 + fnlen);
475 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
476 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000477 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000478 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000479 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000480 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000481}
482
483/*
484 * The hash function for macro lookups. Note that due to some
485 * macros having case-insensitive names, the hash function must be
486 * invariant under case changes. We implement this by applying a
487 * perfectly normal hash function to the uppercase of the string.
488 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000489static int hash(char *s)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000490{
491 unsigned int h = 0;
492 int i = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000493 /*
494 * Powers of three, mod 31.
495 */
496 static const int multipliers[] = {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000497 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10,
498 30, 28, 22, 4, 12, 5, 15, 14, 11, 2, 6, 18, 23, 7, 21
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000499 };
H. Peter Anvineba20a72002-04-30 20:53:55 +0000500
H. Peter Anvine2c80182005-01-15 22:15:51 +0000501 while (*s) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000502 h += multipliers[i] * (uint8_t)(toupper(*s));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503 s++;
504 if (++i >= elements(multipliers))
505 i = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000506 }
507 h %= NHASH;
508 return h;
509}
510
511/*
512 * Free a linked list of tokens.
513 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000514static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000515{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000516 while (list) {
517 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000518 }
519}
520
521/*
522 * Free a linked list of lines.
523 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000524static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000525{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000526 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000527 while (list) {
528 l = list;
529 list = list->next;
530 free_tlist(l->first);
531 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000532 }
533}
534
535/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000536 * Free an MMacro
537 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000538static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000539{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000540 nasm_free(m->name);
541 free_tlist(m->dlist);
542 nasm_free(m->defaults);
543 free_llist(m->expansion);
544 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000545}
546
547/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000548 * Pop the context stack.
549 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000550static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000551{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000552 Context *c = cstk;
553 SMacro *smac, *s;
554
555 cstk = cstk->next;
556 smac = c->localmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000557 while (smac) {
558 s = smac;
559 smac = smac->next;
560 nasm_free(s->name);
561 free_tlist(s->expansion);
562 nasm_free(s);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000563 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000564 nasm_free(c->name);
565 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000566}
567
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000568#define BUF_DELTA 512
569/*
570 * Read a line from the top file in istk, handling multiple CR/LFs
571 * at the end of the line read, and handling spurious ^Zs. Will
572 * return lines from the standard macro set if this has not already
573 * been done.
574 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000575static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000576{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000577 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000578 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000579
H. Peter Anvine2c80182005-01-15 22:15:51 +0000580 if (stdmacpos) {
581 if (*stdmacpos) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000582 char *ret = nasm_strdup(*stdmacpos++);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000583 if (!*stdmacpos && any_extrastdmac) {
584 stdmacpos = extrastdmac;
585 any_extrastdmac = FALSE;
586 return ret;
587 }
588 /*
589 * Nasty hack: here we push the contents of `predef' on
590 * to the top-level expansion stack, since this is the
591 * most convenient way to implement the pre-include and
592 * pre-define features.
593 */
594 if (!*stdmacpos) {
595 Line *pd, *l;
596 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000597
H. Peter Anvine2c80182005-01-15 22:15:51 +0000598 for (pd = predef; pd; pd = pd->next) {
599 head = NULL;
600 tail = &head;
601 for (t = pd->first; t; t = t->next) {
602 *tail = new_Token(NULL, t->type, t->text, 0);
603 tail = &(*tail)->next;
604 }
605 l = nasm_malloc(sizeof(Line));
606 l->next = istk->expansion;
607 l->first = head;
608 l->finishes = FALSE;
609 istk->expansion = l;
610 }
611 }
612 return ret;
613 } else {
614 stdmacpos = NULL;
615 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000616 }
617
618 bufsize = BUF_DELTA;
619 buffer = nasm_malloc(BUF_DELTA);
620 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000621 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000622 while (1) {
623 q = fgets(p, bufsize - (p - buffer), istk->fp);
624 if (!q)
625 break;
626 p += strlen(p);
627 if (p > buffer && p[-1] == '\n') {
628 /* Convert backslash-CRLF line continuation sequences into
629 nothing at all (for DOS and Windows) */
630 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
631 p -= 3;
632 *p = 0;
633 continued_count++;
634 }
635 /* Also convert backslash-LF line continuation sequences into
636 nothing at all (for Unix) */
637 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
638 p -= 2;
639 *p = 0;
640 continued_count++;
641 } else {
642 break;
643 }
644 }
645 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000646 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000647 bufsize += BUF_DELTA;
648 buffer = nasm_realloc(buffer, bufsize);
649 p = buffer + offset; /* prevent stale-pointer problems */
650 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000651 }
652
H. Peter Anvine2c80182005-01-15 22:15:51 +0000653 if (!q && p == buffer) {
654 nasm_free(buffer);
655 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000656 }
657
H. Peter Anvine2c80182005-01-15 22:15:51 +0000658 src_set_linnum(src_get_linnum() + istk->lineinc +
659 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000660
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000661 /*
662 * Play safe: remove CRs as well as LFs, if any of either are
663 * present at the end of the line.
664 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000665 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000666 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000667
668 /*
669 * Handle spurious ^Z, which may be inserted into source files
670 * by some file transfer utilities.
671 */
672 buffer[strcspn(buffer, "\032")] = '\0';
673
H. Peter Anvin734b1882002-04-30 21:01:08 +0000674 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000675
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000676 return buffer;
677}
678
679/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000680 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000681 * don't need to parse the value out of e.g. numeric tokens: we
682 * simply split one string into many.
683 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000684static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000685{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000686 char *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000687 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000688 Token *list = NULL;
689 Token *t, **tail = &list;
690
H. Peter Anvine2c80182005-01-15 22:15:51 +0000691 while (*line) {
692 p = line;
693 if (*p == '%') {
694 p++;
695 if (isdigit(*p) ||
696 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
697 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
698 do {
699 p++;
700 }
701 while (isdigit(*p));
702 type = TOK_PREPROC_ID;
703 } else if (*p == '{') {
704 p++;
705 while (*p && *p != '}') {
706 p[-1] = *p;
707 p++;
708 }
709 p[-1] = '\0';
710 if (*p)
711 p++;
712 type = TOK_PREPROC_ID;
713 } else if (isidchar(*p) ||
714 ((*p == '!' || *p == '%' || *p == '$') &&
715 isidchar(p[1]))) {
716 do {
717 p++;
718 }
719 while (isidchar(*p));
720 type = TOK_PREPROC_ID;
721 } else {
722 type = TOK_OTHER;
723 if (*p == '%')
724 p++;
725 }
726 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
727 type = TOK_ID;
728 p++;
729 while (*p && isidchar(*p))
730 p++;
731 } else if (*p == '\'' || *p == '"') {
732 /*
733 * A string token.
734 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000735 char c = *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000736 p++;
737 type = TOK_STRING;
738 while (*p && *p != c)
739 p++;
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000740
H. Peter Anvine2c80182005-01-15 22:15:51 +0000741 if (*p) {
742 p++;
743 } else {
744 error(ERR_WARNING, "unterminated string");
745 /* Handling unterminated strings by UNV */
746 /* type = -1; */
747 }
748 } else if (isnumstart(*p)) {
749 /*
750 * A number token.
751 */
752 type = TOK_NUMBER;
753 p++;
754 while (*p && isnumchar(*p))
755 p++;
756 } else if (isspace(*p)) {
757 type = TOK_WHITESPACE;
758 p++;
759 while (*p && isspace(*p))
760 p++;
761 /*
762 * Whitespace just before end-of-line is discarded by
763 * pretending it's a comment; whitespace just before a
764 * comment gets lumped into the comment.
765 */
766 if (!*p || *p == ';') {
767 type = TOK_COMMENT;
768 while (*p)
769 p++;
770 }
771 } else if (*p == ';') {
772 type = TOK_COMMENT;
773 while (*p)
774 p++;
775 } else {
776 /*
777 * Anything else is an operator of some kind. We check
778 * for all the double-character operators (>>, <<, //,
779 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000780 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000781 */
782 type = TOK_OTHER;
783 if ((p[0] == '>' && p[1] == '>') ||
784 (p[0] == '<' && p[1] == '<') ||
785 (p[0] == '/' && p[1] == '/') ||
786 (p[0] == '<' && p[1] == '=') ||
787 (p[0] == '>' && p[1] == '=') ||
788 (p[0] == '=' && p[1] == '=') ||
789 (p[0] == '!' && p[1] == '=') ||
790 (p[0] == '<' && p[1] == '>') ||
791 (p[0] == '&' && p[1] == '&') ||
792 (p[0] == '|' && p[1] == '|') ||
793 (p[0] == '^' && p[1] == '^')) {
794 p++;
795 }
796 p++;
797 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000798
H. Peter Anvine2c80182005-01-15 22:15:51 +0000799 /* Handling unterminated string by UNV */
800 /*if (type == -1)
801 {
802 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
803 t->text[p-line] = *line;
804 tail = &t->next;
805 }
806 else */
807 if (type != TOK_COMMENT) {
808 *tail = t = new_Token(NULL, type, line, p - line);
809 tail = &t->next;
810 }
811 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000812 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000813 return list;
814}
815
H. Peter Anvince616072002-04-30 21:02:23 +0000816/*
817 * this function allocates a new managed block of memory and
818 * returns a pointer to the block. The managed blocks are
819 * deleted only all at once by the delete_Blocks function.
820 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000821static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +0000822{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000823 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000824
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000825 /* first, get to the end of the linked list */
826 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000827 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000828 /* now allocate the requested chunk */
829 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000830
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000831 /* now allocate a new block for the next request */
832 b->next = nasm_malloc(sizeof(Blocks));
833 /* and initialize the contents of the new block */
834 b->next->next = NULL;
835 b->next->chunk = NULL;
836 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000837}
838
839/*
840 * this function deletes all managed blocks of memory
841 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000842static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +0000843{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000844 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +0000845
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000846 /*
847 * keep in mind that the first block, pointed to by blocks
848 * is a static and not dynamically allocated, so we don't
849 * free it.
850 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000851 while (b) {
852 if (b->chunk)
853 nasm_free(b->chunk);
854 a = b;
855 b = b->next;
856 if (a != &blocks)
857 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000858 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000859}
H. Peter Anvin734b1882002-04-30 21:01:08 +0000860
861/*
862 * this function creates a new Token and passes a pointer to it
863 * back to the caller. It sets the type and text elements, and
864 * also the mac and next elements to NULL.
865 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000866static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +0000867{
868 Token *t;
869 int i;
870
H. Peter Anvine2c80182005-01-15 22:15:51 +0000871 if (freeTokens == NULL) {
872 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
873 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
874 freeTokens[i].next = &freeTokens[i + 1];
875 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000876 }
877 t = freeTokens;
878 freeTokens = t->next;
879 t->next = next;
880 t->mac = NULL;
881 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000882 if (type == TOK_WHITESPACE || text == NULL) {
883 t->text = NULL;
884 } else {
885 if (txtlen == 0)
886 txtlen = strlen(text);
887 t->text = nasm_malloc(1 + txtlen);
888 strncpy(t->text, text, txtlen);
889 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +0000890 }
891 return t;
892}
893
H. Peter Anvine2c80182005-01-15 22:15:51 +0000894static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +0000895{
896 Token *next = t->next;
897 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +0000898 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000899 freeTokens = t;
900 return next;
901}
902
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000903/*
904 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000905 * If expand_locals is not zero, identifiers of the form "%$*xxx"
906 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000907 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000908static char *detoken(Token * tlist, int expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000909{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000910 Token *t;
911 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000912 char *line, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000913
914 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000915 for (t = tlist; t; t = t->next) {
916 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000917 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000918 nasm_free(t->text);
919 if (p)
920 t->text = nasm_strdup(p);
921 else
922 t->text = NULL;
923 }
924 /* Expand local macros here and not during preprocessing */
925 if (expand_locals &&
926 t->type == TOK_PREPROC_ID && t->text &&
927 t->text[0] == '%' && t->text[1] == '$') {
928 Context *ctx = get_ctx(t->text, FALSE);
929 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000930 char buffer[40];
931 char *p, *q = t->text + 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000932
H. Peter Anvine2c80182005-01-15 22:15:51 +0000933 q += strspn(q, "$");
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000934 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 p = nasm_strcat(buffer, q);
936 nasm_free(t->text);
937 t->text = p;
938 }
939 }
940 if (t->type == TOK_WHITESPACE) {
941 len++;
942 } else if (t->text) {
943 len += strlen(t->text);
944 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000945 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000946 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000947 for (t = tlist; t; t = t->next) {
948 if (t->type == TOK_WHITESPACE) {
949 *p = ' ';
950 p++;
951 *p = '\0';
952 } else if (t->text) {
953 strcpy(p, t->text);
954 p += strlen(p);
955 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000956 }
957 *p = '\0';
958 return line;
959}
960
961/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000962 * A scanner, suitable for use by the expression evaluator, which
963 * operates on a line of Tokens. Expects a pointer to a pointer to
964 * the first token in the line to be passed in as its private_data
965 * field.
966 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000968{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000969 Token **tlineptr = private_data;
970 Token *tline;
971
H. Peter Anvine2c80182005-01-15 22:15:51 +0000972 do {
973 tline = *tlineptr;
974 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000975 }
976 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000977 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000978
979 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000980 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000981
982 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +0000983 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +0000984 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000986
H. Peter Anvine2c80182005-01-15 22:15:51 +0000987 if (tline->type == TOK_ID) {
988 tokval->t_charptr = tline->text;
989 if (tline->text[0] == '$') {
990 tokval->t_charptr++;
991 return tokval->t_type = TOKEN_ID;
992 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000993
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 /*
995 * This is the only special case we actually need to worry
996 * about in this restricted context.
997 */
998 if (!nasm_stricmp(tline->text, "seg"))
999 return tokval->t_type = TOKEN_SEG;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001000
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 return tokval->t_type = TOKEN_ID;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001002 }
1003
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 if (tline->type == TOK_NUMBER) {
1005 int rn_error;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001006
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 tokval->t_integer = readnum(tline->text, &rn_error);
1008 if (rn_error)
1009 return tokval->t_type = TOKEN_ERRNUM;
1010 tokval->t_charptr = NULL;
1011 return tokval->t_type = TOKEN_NUM;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001012 }
1013
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014 if (tline->type == TOK_STRING) {
1015 int rn_warn;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001016 char q, *r;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 int l;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001018
H. Peter Anvine2c80182005-01-15 22:15:51 +00001019 r = tline->text;
1020 q = *r++;
1021 l = strlen(r);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001022
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 if (l == 0 || r[l - 1] != q)
1024 return tokval->t_type = TOKEN_ERRNUM;
1025 tokval->t_integer = readstrnum(r, l - 1, &rn_warn);
1026 if (rn_warn)
1027 error(ERR_WARNING | ERR_PASS1, "character constant too long");
1028 tokval->t_charptr = NULL;
1029 return tokval->t_type = TOKEN_NUM;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001030 }
1031
H. Peter Anvine2c80182005-01-15 22:15:51 +00001032 if (tline->type == TOK_OTHER) {
1033 if (!strcmp(tline->text, "<<"))
1034 return tokval->t_type = TOKEN_SHL;
1035 if (!strcmp(tline->text, ">>"))
1036 return tokval->t_type = TOKEN_SHR;
1037 if (!strcmp(tline->text, "//"))
1038 return tokval->t_type = TOKEN_SDIV;
1039 if (!strcmp(tline->text, "%%"))
1040 return tokval->t_type = TOKEN_SMOD;
1041 if (!strcmp(tline->text, "=="))
1042 return tokval->t_type = TOKEN_EQ;
1043 if (!strcmp(tline->text, "<>"))
1044 return tokval->t_type = TOKEN_NE;
1045 if (!strcmp(tline->text, "!="))
1046 return tokval->t_type = TOKEN_NE;
1047 if (!strcmp(tline->text, "<="))
1048 return tokval->t_type = TOKEN_LE;
1049 if (!strcmp(tline->text, ">="))
1050 return tokval->t_type = TOKEN_GE;
1051 if (!strcmp(tline->text, "&&"))
1052 return tokval->t_type = TOKEN_DBL_AND;
1053 if (!strcmp(tline->text, "^^"))
1054 return tokval->t_type = TOKEN_DBL_XOR;
1055 if (!strcmp(tline->text, "||"))
1056 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001057 }
1058
1059 /*
1060 * We have no other options: just return the first character of
1061 * the token text.
1062 */
1063 return tokval->t_type = tline->text[0];
1064}
1065
1066/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001067 * Compare a string to the name of an existing macro; this is a
1068 * simple wrapper which calls either strcmp or nasm_stricmp
1069 * depending on the value of the `casesense' parameter.
1070 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001071static int mstrcmp(char *p, char *q, int casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001072{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001073 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001074}
1075
1076/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001077 * Return the Context structure associated with a %$ token. Return
1078 * NULL, having _already_ reported an error condition, if the
1079 * context stack isn't deep enough for the supplied number of $
1080 * signs.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001081 * If all_contexts == TRUE, contexts that enclose current are
1082 * also scanned for such smacro, until it is found; if not -
1083 * only the context that directly results from the number of $'s
1084 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001085 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001086static Context *get_ctx(char *name, int all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001087{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001088 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001089 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001090 int i;
1091
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001092 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001094
H. Peter Anvine2c80182005-01-15 22:15:51 +00001095 if (!cstk) {
1096 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1097 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001098 }
1099
H. Peter Anvine2c80182005-01-15 22:15:51 +00001100 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1101 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001102/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001103 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001104 if (!ctx) {
1105 error(ERR_NONFATAL, "`%s': context stack is only"
1106 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1107 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001108 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001109 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001110 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001111
H. Peter Anvine2c80182005-01-15 22:15:51 +00001112 do {
1113 /* Search for this smacro in found context */
1114 m = ctx->localmac;
1115 while (m) {
1116 if (!mstrcmp(m->name, name, m->casesense))
1117 return ctx;
1118 m = m->next;
1119 }
1120 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001121 }
1122 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001123 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001124}
1125
1126/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001127 * Open an include file. This routine must always return a valid
1128 * file pointer if it returns - it's responsible for throwing an
1129 * ERR_FATAL and bombing out completely if not. It should also try
1130 * the include path one by one until it finds the file or reaches
1131 * the end of the path.
1132 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001133static FILE *inc_fopen(char *file)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001134{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001135 FILE *fp;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001136 char *prefix = "", *combine;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001137 IncPath *ip = ipath;
H. Peter Anvin620515a2002-04-30 20:57:38 +00001138 static int namelen = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001139 int len = strlen(file);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001140
H. Peter Anvine2c80182005-01-15 22:15:51 +00001141 while (1) {
1142 combine = nasm_malloc(strlen(prefix) + len + 1);
1143 strcpy(combine, prefix);
1144 strcat(combine, file);
1145 fp = fopen(combine, "r");
1146 if (pass == 0 && fp) {
1147 namelen += strlen(combine) + 1;
1148 if (namelen > 62) {
1149 printf(" \\\n ");
1150 namelen = 2;
1151 }
1152 printf(" %s", combine);
1153 }
1154 nasm_free(combine);
1155 if (fp)
1156 return fp;
1157 if (!ip)
1158 break;
1159 prefix = ip->path;
1160 ip = ip->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001161 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001162
H. Peter Anvin734b1882002-04-30 21:01:08 +00001163 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001164 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001165}
1166
1167/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001168 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001169 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001170 * return TRUE if _any_ single-line macro of that name is defined.
1171 * Otherwise, will return TRUE if a single-line macro with either
1172 * `nparam' or no parameters is defined.
1173 *
1174 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001175 * defined, or nparam is -1, the address of the definition structure
1176 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001177 * is NULL, no action will be taken regarding its contents, and no
1178 * error will occur.
1179 *
1180 * Note that this is also called with nparam zero to resolve
1181 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001182 *
1183 * If you already know which context macro belongs to, you can pass
1184 * the context pointer as first parameter; if you won't but name begins
1185 * with %$ the context will be automatically computed. If all_contexts
1186 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001187 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001188static int
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001189smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001190 int nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001191{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001192 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001193
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001194 if (ctx)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001195 m = ctx->localmac;
1196 else if (name[0] == '%' && name[1] == '$') {
1197 if (cstk)
1198 ctx = get_ctx(name, FALSE);
1199 if (!ctx)
1200 return FALSE; /* got to return _something_ */
1201 m = ctx->localmac;
1202 } else
1203 m = smacros[hash(name)];
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001204
H. Peter Anvine2c80182005-01-15 22:15:51 +00001205 while (m) {
1206 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1207 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam)) {
1208 if (defn) {
1209 if (nparam == m->nparam || nparam == -1)
1210 *defn = m;
1211 else
1212 *defn = NULL;
1213 }
1214 return TRUE;
1215 }
1216 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001217 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001218
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001219 return FALSE;
1220}
1221
1222/*
1223 * Count and mark off the parameters in a multi-line macro call.
1224 * This is called both from within the multi-line macro expansion
1225 * code, and also to mark off the default parameters when provided
1226 * in a %macro definition line.
1227 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001228static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001229{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001230 int paramsize, brace;
1231
1232 *nparam = paramsize = 0;
1233 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001234 while (t) {
1235 if (*nparam >= paramsize) {
1236 paramsize += PARAM_DELTA;
1237 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1238 }
1239 skip_white_(t);
1240 brace = FALSE;
1241 if (tok_is_(t, "{"))
1242 brace = TRUE;
1243 (*params)[(*nparam)++] = t;
1244 while (tok_isnt_(t, brace ? "}" : ","))
1245 t = t->next;
1246 if (t) { /* got a comma/brace */
1247 t = t->next;
1248 if (brace) {
1249 /*
1250 * Now we've found the closing brace, look further
1251 * for the comma.
1252 */
1253 skip_white_(t);
1254 if (tok_isnt_(t, ",")) {
1255 error(ERR_NONFATAL,
1256 "braces do not enclose all of macro parameter");
1257 while (tok_isnt_(t, ","))
1258 t = t->next;
1259 }
1260 if (t)
1261 t = t->next; /* eat the comma */
1262 }
1263 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001264 }
1265}
1266
1267/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001268 * Determine whether one of the various `if' conditions is true or
1269 * not.
1270 *
1271 * We must free the tline we get passed.
1272 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001273static int if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001274{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001275 enum pp_conditional i = PP_COND(ct);
1276 int j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001277 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001278 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001279 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001280 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001281
1282 origline = tline;
1283
H. Peter Anvine2c80182005-01-15 22:15:51 +00001284 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001285 case PPC_IFCTX:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001286 j = FALSE; /* have we matched yet? */
1287 while (cstk && tline) {
1288 skip_white_(tline);
1289 if (!tline || tline->type != TOK_ID) {
1290 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001291 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 free_tlist(origline);
1293 return -1;
1294 }
1295 if (!nasm_stricmp(tline->text, cstk->name))
1296 j = TRUE;
1297 tline = tline->next;
1298 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001299 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001300
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001301 case PPC_IFDEF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001302 j = FALSE; /* have we matched yet? */
1303 while (tline) {
1304 skip_white_(tline);
1305 if (!tline || (tline->type != TOK_ID &&
1306 (tline->type != TOK_PREPROC_ID ||
1307 tline->text[1] != '$'))) {
1308 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001309 "`%s' expects macro identifiers", pp_directives[ct]);
1310 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 }
1312 if (smacro_defined(NULL, tline->text, 0, NULL, 1))
1313 j = TRUE;
1314 tline = tline->next;
1315 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001316 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001317
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001318 case PPC_IFIDN:
1319 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001320 tline = expand_smacro(tline);
1321 t = tt = tline;
1322 while (tok_isnt_(tt, ","))
1323 tt = tt->next;
1324 if (!tt) {
1325 error(ERR_NONFATAL,
1326 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001327 pp_directives[ct]);
1328 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001329 }
1330 tt = tt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001331 j = TRUE; /* assume equality unless proved not */
1332 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1333 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1334 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001335 pp_directives[ct]);
1336 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001337 }
1338 if (t->type == TOK_WHITESPACE) {
1339 t = t->next;
1340 continue;
1341 }
1342 if (tt->type == TOK_WHITESPACE) {
1343 tt = tt->next;
1344 continue;
1345 }
1346 if (tt->type != t->type) {
1347 j = FALSE; /* found mismatching tokens */
1348 break;
1349 }
1350 /* Unify surrounding quotes for strings */
1351 if (t->type == TOK_STRING) {
1352 tt->text[0] = t->text[0];
1353 tt->text[strlen(tt->text) - 1] = t->text[0];
1354 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001355 if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001356 j = FALSE; /* found mismatching tokens */
1357 break;
1358 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001359
H. Peter Anvine2c80182005-01-15 22:15:51 +00001360 t = t->next;
1361 tt = tt->next;
1362 }
1363 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1364 j = FALSE; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001365 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001366
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001367 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001368 {
1369 int found = 0;
1370 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001371
H. Peter Anvine2c80182005-01-15 22:15:51 +00001372 tline = tline->next;
1373 skip_white_(tline);
1374 tline = expand_id(tline);
1375 if (!tok_type_(tline, TOK_ID)) {
1376 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001377 "`%s' expects a macro name", pp_directives[ct]);
1378 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001379 }
1380 searching.name = nasm_strdup(tline->text);
1381 searching.casesense = (i == PP_MACRO);
1382 searching.plus = FALSE;
1383 searching.nolist = FALSE;
1384 searching.in_progress = FALSE;
1385 searching.rep_nest = NULL;
1386 searching.nparam_min = 0;
1387 searching.nparam_max = INT_MAX;
1388 tline = expand_smacro(tline->next);
1389 skip_white_(tline);
1390 if (!tline) {
1391 } else if (!tok_type_(tline, TOK_NUMBER)) {
1392 error(ERR_NONFATAL,
1393 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001394 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001395 } else {
1396 searching.nparam_min = searching.nparam_max =
1397 readnum(tline->text, &j);
1398 if (j)
1399 error(ERR_NONFATAL,
1400 "unable to parse parameter count `%s'",
1401 tline->text);
1402 }
1403 if (tline && tok_is_(tline->next, "-")) {
1404 tline = tline->next->next;
1405 if (tok_is_(tline, "*"))
1406 searching.nparam_max = INT_MAX;
1407 else if (!tok_type_(tline, TOK_NUMBER))
1408 error(ERR_NONFATAL,
1409 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001410 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001411 else {
1412 searching.nparam_max = readnum(tline->text, &j);
1413 if (j)
1414 error(ERR_NONFATAL,
1415 "unable to parse parameter count `%s'",
1416 tline->text);
1417 if (searching.nparam_min > searching.nparam_max)
1418 error(ERR_NONFATAL,
1419 "minimum parameter count exceeds maximum");
1420 }
1421 }
1422 if (tline && tok_is_(tline->next, "+")) {
1423 tline = tline->next;
1424 searching.plus = TRUE;
1425 }
1426 mmac = mmacros[hash(searching.name)];
1427 while (mmac) {
1428 if (!strcmp(mmac->name, searching.name) &&
1429 (mmac->nparam_min <= searching.nparam_max
1430 || searching.plus)
1431 && (searching.nparam_min <= mmac->nparam_max
1432 || mmac->plus)) {
1433 found = TRUE;
1434 break;
1435 }
1436 mmac = mmac->next;
1437 }
1438 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001439 j = found;
1440 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001441 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001442
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001443 case PPC_IFID:
1444 needtype = TOK_ID;
1445 goto iftype;
1446 case PPC_IFNUM:
1447 needtype = TOK_NUMBER;
1448 goto iftype;
1449 case PPC_IFSTR:
1450 needtype = TOK_STRING;
1451 goto iftype;
1452
1453 iftype:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001454 tline = expand_smacro(tline);
1455 t = tline;
1456 while (tok_type_(t, TOK_WHITESPACE))
1457 t = t->next;
1458 j = FALSE; /* placate optimiser */
1459 if (t)
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001460 j = t->type == needtype;
1461 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001462
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001463 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001464 t = tline = expand_smacro(tline);
1465 tptr = &t;
1466 tokval.t_type = TOKEN_INVALID;
1467 evalresult = evaluate(ppscan, tptr, &tokval,
1468 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001469 if (!evalresult)
1470 return -1;
1471 if (tokval.t_type)
1472 error(ERR_WARNING,
1473 "trailing garbage after expression ignored");
1474 if (!is_simple(evalresult)) {
1475 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001476 "non-constant value given to `%s'", pp_directives[ct]);
1477 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001478 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001479 j = reloc_value(evalresult) != 0;
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001480 return j;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001481
H. Peter Anvine2c80182005-01-15 22:15:51 +00001482 default:
1483 error(ERR_FATAL,
1484 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001485 pp_directives[ct]);
1486 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001487 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001488
1489 free_tlist(origline);
1490 return j ^ PP_NEGATIVE(ct);
1491
1492fail:
1493 free_tlist(origline);
1494 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001495}
1496
1497/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001498 * Expand macros in a string. Used in %error and %include directives.
Keith Kaniosb7a89542007-04-12 02:40:54 +00001499 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001500 * The returned variable should ALWAYS be freed after usage.
1501 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001502void expand_macros_in_string(char **p)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001503{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001504 Token *line = tokenize(*p);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001505 line = expand_smacro(line);
1506 *p = detoken(line, FALSE);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001507}
1508
Ed Beroset3ab3f412002-06-11 03:31:49 +00001509/**
1510 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001511 * Find out if a line contains a preprocessor directive, and deal
1512 * with it if so.
1513 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001514 * If a directive _is_ found, it is the responsibility of this routine
1515 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001516 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001517 * @param tline a pointer to the current tokeninzed line linked list
1518 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001519 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001520 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001522{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001523 enum preproc_token i;
1524 int j;
1525 int nparam, nolist;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001526 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001527 int offset;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001528 char *p, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001529 Include *inc;
1530 Context *ctx;
1531 Cond *cond;
1532 SMacro *smac, **smhead;
1533 MMacro *mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001534 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1535 Line *l;
1536 struct tokenval tokval;
1537 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001538 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001539
1540 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001541
H. Peter Anvineba20a72002-04-30 20:53:55 +00001542 skip_white_(tline);
1543 if (!tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001544 (tline->text[1] == '%' || tline->text[1] == '$'
1545 || tline->text[1] == '!'))
1546 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001547
H. Peter Anvin4169a472007-09-12 01:29:43 +00001548 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001549
1550 /*
1551 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001552 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001553 * we should ignore all directives except for condition
1554 * directives.
1555 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001556 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001557 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1558 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001559 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001560
1561 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001562 * If we're defining a macro or reading a %rep block, we should
1563 * ignore all directives except for %macro/%imacro (which
1564 * generate an error), %endm/%endmacro, and (only if we're in a
H. Peter Anvineba20a72002-04-30 20:53:55 +00001565 * %rep block) %endrep. If we're in a %rep block, another %rep
1566 * causes an error, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001567 */
1568 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001569 i != PP_ENDMACRO && i != PP_ENDM &&
1570 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1571 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001572 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001573
H. Peter Anvin4169a472007-09-12 01:29:43 +00001574 switch (i) {
1575 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001576 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1577 tline->text);
1578 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001579
H. Peter Anvine2c80182005-01-15 22:15:51 +00001580 case PP_STACKSIZE:
1581 /* Directive to tell NASM what the default stack size is. The
1582 * default is for a 16-bit stack, and this can be overriden with
1583 * %stacksize large.
1584 * the following form:
1585 *
1586 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1587 */
1588 tline = tline->next;
1589 if (tline && tline->type == TOK_WHITESPACE)
1590 tline = tline->next;
1591 if (!tline || tline->type != TOK_ID) {
1592 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1593 free_tlist(origline);
1594 return DIRECTIVE_FOUND;
1595 }
1596 if (nasm_stricmp(tline->text, "flat") == 0) {
1597 /* All subsequent ARG directives are for a 32-bit stack */
1598 StackSize = 4;
1599 StackPointer = "ebp";
1600 ArgOffset = 8;
1601 LocalOffset = 4;
1602 } else if (nasm_stricmp(tline->text, "large") == 0) {
1603 /* All subsequent ARG directives are for a 16-bit stack,
1604 * far function call.
1605 */
1606 StackSize = 2;
1607 StackPointer = "bp";
1608 ArgOffset = 4;
1609 LocalOffset = 2;
1610 } else if (nasm_stricmp(tline->text, "small") == 0) {
1611 /* All subsequent ARG directives are for a 16-bit stack,
1612 * far function call. We don't support near functions.
1613 */
1614 StackSize = 2;
1615 StackPointer = "bp";
1616 ArgOffset = 6;
1617 LocalOffset = 2;
1618 } else {
1619 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1620 free_tlist(origline);
1621 return DIRECTIVE_FOUND;
1622 }
1623 free_tlist(origline);
1624 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001625
H. Peter Anvine2c80182005-01-15 22:15:51 +00001626 case PP_ARG:
1627 /* TASM like ARG directive to define arguments to functions, in
1628 * the following form:
1629 *
1630 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1631 */
1632 offset = ArgOffset;
1633 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001634 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001636
H. Peter Anvine2c80182005-01-15 22:15:51 +00001637 /* Find the argument name */
1638 tline = tline->next;
1639 if (tline && tline->type == TOK_WHITESPACE)
1640 tline = tline->next;
1641 if (!tline || tline->type != TOK_ID) {
1642 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1643 free_tlist(origline);
1644 return DIRECTIVE_FOUND;
1645 }
1646 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001647
H. Peter Anvine2c80182005-01-15 22:15:51 +00001648 /* Find the argument size type */
1649 tline = tline->next;
1650 if (!tline || tline->type != TOK_OTHER
1651 || tline->text[0] != ':') {
1652 error(ERR_NONFATAL,
1653 "Syntax error processing `%%arg' directive");
1654 free_tlist(origline);
1655 return DIRECTIVE_FOUND;
1656 }
1657 tline = tline->next;
1658 if (!tline || tline->type != TOK_ID) {
1659 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1660 free_tlist(origline);
1661 return DIRECTIVE_FOUND;
1662 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001663
H. Peter Anvine2c80182005-01-15 22:15:51 +00001664 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001665 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001666 tt = expand_smacro(tt);
1667 if (nasm_stricmp(tt->text, "byte") == 0) {
1668 size = MAX(StackSize, 1);
1669 } else if (nasm_stricmp(tt->text, "word") == 0) {
1670 size = MAX(StackSize, 2);
1671 } else if (nasm_stricmp(tt->text, "dword") == 0) {
1672 size = MAX(StackSize, 4);
1673 } else if (nasm_stricmp(tt->text, "qword") == 0) {
1674 size = MAX(StackSize, 8);
1675 } else if (nasm_stricmp(tt->text, "tword") == 0) {
1676 size = MAX(StackSize, 10);
1677 } else {
1678 error(ERR_NONFATAL,
1679 "Invalid size type for `%%arg' missing directive");
1680 free_tlist(tt);
1681 free_tlist(origline);
1682 return DIRECTIVE_FOUND;
1683 }
1684 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001685
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 /* Now define the macro for the argument */
1687 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1688 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001689 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001691
H. Peter Anvine2c80182005-01-15 22:15:51 +00001692 /* Move to the next argument in the list */
1693 tline = tline->next;
1694 if (tline && tline->type == TOK_WHITESPACE)
1695 tline = tline->next;
1696 }
1697 while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1698 free_tlist(origline);
1699 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001700
H. Peter Anvine2c80182005-01-15 22:15:51 +00001701 case PP_LOCAL:
1702 /* TASM like LOCAL directive to define local variables for a
1703 * function, in the following form:
1704 *
1705 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1706 *
1707 * The '= LocalSize' at the end is ignored by NASM, but is
1708 * required by TASM to define the local parameter size (and used
1709 * by the TASM macro package).
1710 */
1711 offset = LocalOffset;
1712 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001713 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001714 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001715
H. Peter Anvine2c80182005-01-15 22:15:51 +00001716 /* Find the argument name */
1717 tline = tline->next;
1718 if (tline && tline->type == TOK_WHITESPACE)
1719 tline = tline->next;
1720 if (!tline || tline->type != TOK_ID) {
1721 error(ERR_NONFATAL,
1722 "`%%local' missing argument parameter");
1723 free_tlist(origline);
1724 return DIRECTIVE_FOUND;
1725 }
1726 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001727
H. Peter Anvine2c80182005-01-15 22:15:51 +00001728 /* Find the argument size type */
1729 tline = tline->next;
1730 if (!tline || tline->type != TOK_OTHER
1731 || tline->text[0] != ':') {
1732 error(ERR_NONFATAL,
1733 "Syntax error processing `%%local' directive");
1734 free_tlist(origline);
1735 return DIRECTIVE_FOUND;
1736 }
1737 tline = tline->next;
1738 if (!tline || tline->type != TOK_ID) {
1739 error(ERR_NONFATAL,
1740 "`%%local' missing size type parameter");
1741 free_tlist(origline);
1742 return DIRECTIVE_FOUND;
1743 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001744
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001746 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001747 tt = expand_smacro(tt);
1748 if (nasm_stricmp(tt->text, "byte") == 0) {
1749 size = MAX(StackSize, 1);
1750 } else if (nasm_stricmp(tt->text, "word") == 0) {
1751 size = MAX(StackSize, 2);
1752 } else if (nasm_stricmp(tt->text, "dword") == 0) {
1753 size = MAX(StackSize, 4);
1754 } else if (nasm_stricmp(tt->text, "qword") == 0) {
1755 size = MAX(StackSize, 8);
1756 } else if (nasm_stricmp(tt->text, "tword") == 0) {
1757 size = MAX(StackSize, 10);
1758 } else {
1759 error(ERR_NONFATAL,
1760 "Invalid size type for `%%local' missing directive");
1761 free_tlist(tt);
1762 free_tlist(origline);
1763 return DIRECTIVE_FOUND;
1764 }
1765 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001766
H. Peter Anvine2c80182005-01-15 22:15:51 +00001767 /* Now define the macro for the argument */
1768 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
1769 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001770 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001771 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001772
H. Peter Anvine2c80182005-01-15 22:15:51 +00001773 /* Now define the assign to setup the enter_c macro correctly */
1774 snprintf(directive, sizeof(directive),
1775 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001776 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001777
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778 /* Move to the next argument in the list */
1779 tline = tline->next;
1780 if (tline && tline->type == TOK_WHITESPACE)
1781 tline = tline->next;
1782 }
1783 while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1784 free_tlist(origline);
1785 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001786
H. Peter Anvine2c80182005-01-15 22:15:51 +00001787 case PP_CLEAR:
1788 if (tline->next)
1789 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
1790 for (j = 0; j < NHASH; j++) {
1791 while (mmacros[j]) {
1792 MMacro *m = mmacros[j];
1793 mmacros[j] = m->next;
1794 free_mmacro(m);
1795 }
1796 while (smacros[j]) {
1797 SMacro *s = smacros[j];
1798 smacros[j] = smacros[j]->next;
1799 nasm_free(s->name);
1800 free_tlist(s->expansion);
1801 nasm_free(s);
1802 }
1803 }
1804 free_tlist(origline);
1805 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001806
H. Peter Anvine2c80182005-01-15 22:15:51 +00001807 case PP_INCLUDE:
1808 tline = tline->next;
1809 skip_white_(tline);
1810 if (!tline || (tline->type != TOK_STRING &&
1811 tline->type != TOK_INTERNAL_STRING)) {
1812 error(ERR_NONFATAL, "`%%include' expects a file name");
1813 free_tlist(origline);
1814 return DIRECTIVE_FOUND; /* but we did _something_ */
1815 }
1816 if (tline->next)
1817 error(ERR_WARNING,
1818 "trailing garbage after `%%include' ignored");
1819 if (tline->type != TOK_INTERNAL_STRING) {
1820 p = tline->text + 1; /* point past the quote to the name */
1821 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
1822 } else
1823 p = tline->text; /* internal_string is easier */
1824 expand_macros_in_string(&p);
1825 inc = nasm_malloc(sizeof(Include));
1826 inc->next = istk;
1827 inc->conds = NULL;
1828 inc->fp = inc_fopen(p);
1829 inc->fname = src_set_fname(p);
1830 inc->lineno = src_set_linnum(0);
1831 inc->lineinc = 1;
1832 inc->expansion = NULL;
1833 inc->mstk = NULL;
1834 istk = inc;
1835 list->uplevel(LIST_INCLUDE);
1836 free_tlist(origline);
1837 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001838
H. Peter Anvine2c80182005-01-15 22:15:51 +00001839 case PP_PUSH:
1840 tline = tline->next;
1841 skip_white_(tline);
1842 tline = expand_id(tline);
1843 if (!tok_type_(tline, TOK_ID)) {
1844 error(ERR_NONFATAL, "`%%push' expects a context identifier");
1845 free_tlist(origline);
1846 return DIRECTIVE_FOUND; /* but we did _something_ */
1847 }
1848 if (tline->next)
1849 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
1850 ctx = nasm_malloc(sizeof(Context));
1851 ctx->next = cstk;
1852 ctx->localmac = NULL;
1853 ctx->name = nasm_strdup(tline->text);
1854 ctx->number = unique++;
1855 cstk = ctx;
1856 free_tlist(origline);
1857 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001858
H. Peter Anvine2c80182005-01-15 22:15:51 +00001859 case PP_REPL:
1860 tline = tline->next;
1861 skip_white_(tline);
1862 tline = expand_id(tline);
1863 if (!tok_type_(tline, TOK_ID)) {
1864 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
1865 free_tlist(origline);
1866 return DIRECTIVE_FOUND; /* but we did _something_ */
1867 }
1868 if (tline->next)
1869 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
1870 if (!cstk)
1871 error(ERR_NONFATAL, "`%%repl': context stack is empty");
1872 else {
1873 nasm_free(cstk->name);
1874 cstk->name = nasm_strdup(tline->text);
1875 }
1876 free_tlist(origline);
1877 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001878
H. Peter Anvine2c80182005-01-15 22:15:51 +00001879 case PP_POP:
1880 if (tline->next)
1881 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
1882 if (!cstk)
1883 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
1884 else
1885 ctx_pop();
1886 free_tlist(origline);
1887 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001888
H. Peter Anvine2c80182005-01-15 22:15:51 +00001889 case PP_ERROR:
1890 tline->next = expand_smacro(tline->next);
1891 tline = tline->next;
1892 skip_white_(tline);
1893 if (tok_type_(tline, TOK_STRING)) {
1894 p = tline->text + 1; /* point past the quote to the name */
1895 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
1896 expand_macros_in_string(&p);
1897 error(ERR_NONFATAL, "%s", p);
1898 nasm_free(p);
1899 } else {
1900 p = detoken(tline, FALSE);
1901 error(ERR_WARNING, "%s", p);
1902 nasm_free(p);
1903 }
1904 free_tlist(origline);
1905 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001906
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001907 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001908 if (istk->conds && !emitting(istk->conds->state))
1909 j = COND_NEVER;
1910 else {
1911 j = if_condition(tline->next, i);
1912 tline->next = NULL; /* it got freed */
1913 free_tlist(origline);
1914 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
1915 }
1916 cond = nasm_malloc(sizeof(Cond));
1917 cond->next = istk->conds;
1918 cond->state = j;
1919 istk->conds = cond;
1920 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001921
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001922 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001923 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00001924 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001925 if (emitting(istk->conds->state)
1926 || istk->conds->state == COND_NEVER)
1927 istk->conds->state = COND_NEVER;
1928 else {
1929 /*
1930 * IMPORTANT: In the case of %if, we will already have
1931 * called expand_mmac_params(); however, if we're
1932 * processing an %elif we must have been in a
1933 * non-emitting mode, which would have inhibited
1934 * the normal invocation of expand_mmac_params(). Therefore,
1935 * we have to do it explicitly here.
1936 */
1937 j = if_condition(expand_mmac_params(tline->next), i);
1938 tline->next = NULL; /* it got freed */
1939 free_tlist(origline);
1940 istk->conds->state =
1941 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
1942 }
1943 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001944
H. Peter Anvine2c80182005-01-15 22:15:51 +00001945 case PP_ELSE:
1946 if (tline->next)
1947 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
1948 if (!istk->conds)
1949 error(ERR_FATAL, "`%%else': no matching `%%if'");
1950 if (emitting(istk->conds->state)
1951 || istk->conds->state == COND_NEVER)
1952 istk->conds->state = COND_ELSE_FALSE;
1953 else
1954 istk->conds->state = COND_ELSE_TRUE;
1955 free_tlist(origline);
1956 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001957
H. Peter Anvine2c80182005-01-15 22:15:51 +00001958 case PP_ENDIF:
1959 if (tline->next)
1960 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
1961 if (!istk->conds)
1962 error(ERR_FATAL, "`%%endif': no matching `%%if'");
1963 cond = istk->conds;
1964 istk->conds = cond->next;
1965 nasm_free(cond);
1966 free_tlist(origline);
1967 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001968
H. Peter Anvine2c80182005-01-15 22:15:51 +00001969 case PP_MACRO:
1970 case PP_IMACRO:
1971 if (defining)
1972 error(ERR_FATAL,
1973 "`%%%smacro': already defining a macro",
1974 (i == PP_IMACRO ? "i" : ""));
1975 tline = tline->next;
1976 skip_white_(tline);
1977 tline = expand_id(tline);
1978 if (!tok_type_(tline, TOK_ID)) {
1979 error(ERR_NONFATAL,
1980 "`%%%smacro' expects a macro name",
1981 (i == PP_IMACRO ? "i" : ""));
1982 return DIRECTIVE_FOUND;
1983 }
1984 defining = nasm_malloc(sizeof(MMacro));
1985 defining->name = nasm_strdup(tline->text);
1986 defining->casesense = (i == PP_MACRO);
1987 defining->plus = FALSE;
1988 defining->nolist = FALSE;
1989 defining->in_progress = FALSE;
1990 defining->rep_nest = NULL;
1991 tline = expand_smacro(tline->next);
1992 skip_white_(tline);
1993 if (!tok_type_(tline, TOK_NUMBER)) {
1994 error(ERR_NONFATAL,
1995 "`%%%smacro' expects a parameter count",
1996 (i == PP_IMACRO ? "i" : ""));
1997 defining->nparam_min = defining->nparam_max = 0;
1998 } else {
1999 defining->nparam_min = defining->nparam_max =
2000 readnum(tline->text, &j);
2001 if (j)
2002 error(ERR_NONFATAL,
2003 "unable to parse parameter count `%s'", tline->text);
2004 }
2005 if (tline && tok_is_(tline->next, "-")) {
2006 tline = tline->next->next;
2007 if (tok_is_(tline, "*"))
2008 defining->nparam_max = INT_MAX;
2009 else if (!tok_type_(tline, TOK_NUMBER))
2010 error(ERR_NONFATAL,
2011 "`%%%smacro' expects a parameter count after `-'",
2012 (i == PP_IMACRO ? "i" : ""));
2013 else {
2014 defining->nparam_max = readnum(tline->text, &j);
2015 if (j)
2016 error(ERR_NONFATAL,
2017 "unable to parse parameter count `%s'",
2018 tline->text);
2019 if (defining->nparam_min > defining->nparam_max)
2020 error(ERR_NONFATAL,
2021 "minimum parameter count exceeds maximum");
2022 }
2023 }
2024 if (tline && tok_is_(tline->next, "+")) {
2025 tline = tline->next;
2026 defining->plus = TRUE;
2027 }
2028 if (tline && tok_type_(tline->next, TOK_ID) &&
2029 !nasm_stricmp(tline->next->text, ".nolist")) {
2030 tline = tline->next;
2031 defining->nolist = TRUE;
2032 }
2033 mmac = mmacros[hash(defining->name)];
2034 while (mmac) {
2035 if (!strcmp(mmac->name, defining->name) &&
2036 (mmac->nparam_min <= defining->nparam_max
2037 || defining->plus)
2038 && (defining->nparam_min <= mmac->nparam_max
2039 || mmac->plus)) {
2040 error(ERR_WARNING,
2041 "redefining multi-line macro `%s'", defining->name);
2042 break;
2043 }
2044 mmac = mmac->next;
2045 }
2046 /*
2047 * Handle default parameters.
2048 */
2049 if (tline && tline->next) {
2050 defining->dlist = tline->next;
2051 tline->next = NULL;
2052 count_mmac_params(defining->dlist, &defining->ndefs,
2053 &defining->defaults);
2054 } else {
2055 defining->dlist = NULL;
2056 defining->defaults = NULL;
2057 }
2058 defining->expansion = NULL;
2059 free_tlist(origline);
2060 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002061
H. Peter Anvine2c80182005-01-15 22:15:51 +00002062 case PP_ENDM:
2063 case PP_ENDMACRO:
2064 if (!defining) {
2065 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2066 return DIRECTIVE_FOUND;
2067 }
2068 k = hash(defining->name);
2069 defining->next = mmacros[k];
2070 mmacros[k] = defining;
2071 defining = NULL;
2072 free_tlist(origline);
2073 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002074
H. Peter Anvine2c80182005-01-15 22:15:51 +00002075 case PP_ROTATE:
2076 if (tline->next && tline->next->type == TOK_WHITESPACE)
2077 tline = tline->next;
2078 if (tline->next == NULL) {
2079 free_tlist(origline);
2080 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2081 return DIRECTIVE_FOUND;
2082 }
2083 t = expand_smacro(tline->next);
2084 tline->next = NULL;
2085 free_tlist(origline);
2086 tline = t;
2087 tptr = &t;
2088 tokval.t_type = TOKEN_INVALID;
2089 evalresult =
2090 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2091 free_tlist(tline);
2092 if (!evalresult)
2093 return DIRECTIVE_FOUND;
2094 if (tokval.t_type)
2095 error(ERR_WARNING,
2096 "trailing garbage after expression ignored");
2097 if (!is_simple(evalresult)) {
2098 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2099 return DIRECTIVE_FOUND;
2100 }
2101 mmac = istk->mstk;
2102 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2103 mmac = mmac->next_active;
2104 if (!mmac) {
2105 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2106 } else if (mmac->nparam == 0) {
2107 error(ERR_NONFATAL,
2108 "`%%rotate' invoked within macro without parameters");
2109 } else {
2110 mmac->rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002111
H. Peter Anvine2c80182005-01-15 22:15:51 +00002112 if (mmac->rotate < 0)
2113 mmac->rotate =
2114 mmac->nparam - (-mmac->rotate) % mmac->nparam;
2115 mmac->rotate %= mmac->nparam;
2116 }
2117 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002118
H. Peter Anvine2c80182005-01-15 22:15:51 +00002119 case PP_REP:
2120 nolist = FALSE;
2121 do {
2122 tline = tline->next;
2123 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002124
H. Peter Anvine2c80182005-01-15 22:15:51 +00002125 if (tok_type_(tline, TOK_ID) &&
2126 nasm_stricmp(tline->text, ".nolist") == 0) {
2127 nolist = TRUE;
2128 do {
2129 tline = tline->next;
2130 } while (tok_type_(tline, TOK_WHITESPACE));
2131 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002132
H. Peter Anvine2c80182005-01-15 22:15:51 +00002133 if (tline) {
2134 t = expand_smacro(tline);
2135 tptr = &t;
2136 tokval.t_type = TOKEN_INVALID;
2137 evalresult =
2138 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2139 if (!evalresult) {
2140 free_tlist(origline);
2141 return DIRECTIVE_FOUND;
2142 }
2143 if (tokval.t_type)
2144 error(ERR_WARNING,
2145 "trailing garbage after expression ignored");
2146 if (!is_simple(evalresult)) {
2147 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2148 return DIRECTIVE_FOUND;
2149 }
2150 i = (int)reloc_value(evalresult) + 1;
2151 } else {
2152 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2153 i = 0;
2154 }
2155 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002156
H. Peter Anvine2c80182005-01-15 22:15:51 +00002157 tmp_defining = defining;
2158 defining = nasm_malloc(sizeof(MMacro));
2159 defining->name = NULL; /* flags this macro as a %rep block */
2160 defining->casesense = 0;
2161 defining->plus = FALSE;
2162 defining->nolist = nolist;
2163 defining->in_progress = i;
2164 defining->nparam_min = defining->nparam_max = 0;
2165 defining->defaults = NULL;
2166 defining->dlist = NULL;
2167 defining->expansion = NULL;
2168 defining->next_active = istk->mstk;
2169 defining->rep_nest = tmp_defining;
2170 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002171
H. Peter Anvine2c80182005-01-15 22:15:51 +00002172 case PP_ENDREP:
2173 if (!defining || defining->name) {
2174 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2175 return DIRECTIVE_FOUND;
2176 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002177
H. Peter Anvine2c80182005-01-15 22:15:51 +00002178 /*
2179 * Now we have a "macro" defined - although it has no name
2180 * and we won't be entering it in the hash tables - we must
2181 * push a macro-end marker for it on to istk->expansion.
2182 * After that, it will take care of propagating itself (a
2183 * macro-end marker line for a macro which is really a %rep
2184 * block will cause the macro to be re-expanded, complete
2185 * with another macro-end marker to ensure the process
2186 * continues) until the whole expansion is forcibly removed
2187 * from istk->expansion by a %exitrep.
2188 */
2189 l = nasm_malloc(sizeof(Line));
2190 l->next = istk->expansion;
2191 l->finishes = defining;
2192 l->first = NULL;
2193 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002194
H. Peter Anvine2c80182005-01-15 22:15:51 +00002195 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002196
H. Peter Anvine2c80182005-01-15 22:15:51 +00002197 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2198 tmp_defining = defining;
2199 defining = defining->rep_nest;
2200 free_tlist(origline);
2201 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002202
H. Peter Anvine2c80182005-01-15 22:15:51 +00002203 case PP_EXITREP:
2204 /*
2205 * We must search along istk->expansion until we hit a
2206 * macro-end marker for a macro with no name. Then we set
2207 * its `in_progress' flag to 0.
2208 */
2209 for (l = istk->expansion; l; l = l->next)
2210 if (l->finishes && !l->finishes->name)
2211 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002212
H. Peter Anvine2c80182005-01-15 22:15:51 +00002213 if (l)
2214 l->finishes->in_progress = 0;
2215 else
2216 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2217 free_tlist(origline);
2218 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002219
H. Peter Anvine2c80182005-01-15 22:15:51 +00002220 case PP_XDEFINE:
2221 case PP_IXDEFINE:
2222 case PP_DEFINE:
2223 case PP_IDEFINE:
2224 tline = tline->next;
2225 skip_white_(tline);
2226 tline = expand_id(tline);
2227 if (!tline || (tline->type != TOK_ID &&
2228 (tline->type != TOK_PREPROC_ID ||
2229 tline->text[1] != '$'))) {
2230 error(ERR_NONFATAL,
2231 "`%%%s%sdefine' expects a macro identifier",
2232 ((i == PP_IDEFINE || i == PP_IXDEFINE) ? "i" : ""),
2233 ((i == PP_XDEFINE || i == PP_IXDEFINE) ? "x" : ""));
2234 free_tlist(origline);
2235 return DIRECTIVE_FOUND;
2236 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002237
H. Peter Anvine2c80182005-01-15 22:15:51 +00002238 ctx = get_ctx(tline->text, FALSE);
2239 if (!ctx)
2240 smhead = &smacros[hash(tline->text)];
2241 else
2242 smhead = &ctx->localmac;
2243 mname = tline->text;
2244 last = tline;
2245 param_start = tline = tline->next;
2246 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002247
H. Peter Anvine2c80182005-01-15 22:15:51 +00002248 /* Expand the macro definition now for %xdefine and %ixdefine */
2249 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2250 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002251
H. Peter Anvine2c80182005-01-15 22:15:51 +00002252 if (tok_is_(tline, "(")) {
2253 /*
2254 * This macro has parameters.
2255 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002256
H. Peter Anvine2c80182005-01-15 22:15:51 +00002257 tline = tline->next;
2258 while (1) {
2259 skip_white_(tline);
2260 if (!tline) {
2261 error(ERR_NONFATAL, "parameter identifier expected");
2262 free_tlist(origline);
2263 return DIRECTIVE_FOUND;
2264 }
2265 if (tline->type != TOK_ID) {
2266 error(ERR_NONFATAL,
2267 "`%s': parameter identifier expected",
2268 tline->text);
2269 free_tlist(origline);
2270 return DIRECTIVE_FOUND;
2271 }
2272 tline->type = TOK_SMAC_PARAM + nparam++;
2273 tline = tline->next;
2274 skip_white_(tline);
2275 if (tok_is_(tline, ",")) {
2276 tline = tline->next;
2277 continue;
2278 }
2279 if (!tok_is_(tline, ")")) {
2280 error(ERR_NONFATAL,
2281 "`)' expected to terminate macro template");
2282 free_tlist(origline);
2283 return DIRECTIVE_FOUND;
2284 }
2285 break;
2286 }
2287 last = tline;
2288 tline = tline->next;
2289 }
2290 if (tok_type_(tline, TOK_WHITESPACE))
2291 last = tline, tline = tline->next;
2292 macro_start = NULL;
2293 last->next = NULL;
2294 t = tline;
2295 while (t) {
2296 if (t->type == TOK_ID) {
2297 for (tt = param_start; tt; tt = tt->next)
2298 if (tt->type >= TOK_SMAC_PARAM &&
2299 !strcmp(tt->text, t->text))
2300 t->type = tt->type;
2301 }
2302 tt = t->next;
2303 t->next = macro_start;
2304 macro_start = t;
2305 t = tt;
2306 }
2307 /*
2308 * Good. We now have a macro name, a parameter count, and a
2309 * token list (in reverse order) for an expansion. We ought
2310 * to be OK just to create an SMacro, store it, and let
2311 * free_tlist have the rest of the line (which we have
2312 * carefully re-terminated after chopping off the expansion
2313 * from the end).
2314 */
2315 if (smacro_defined(ctx, mname, nparam, &smac, i == PP_DEFINE)) {
2316 if (!smac) {
2317 error(ERR_WARNING,
2318 "single-line macro `%s' defined both with and"
2319 " without parameters", mname);
2320 free_tlist(origline);
2321 free_tlist(macro_start);
2322 return DIRECTIVE_FOUND;
2323 } else {
2324 /*
2325 * We're redefining, so we have to take over an
2326 * existing SMacro structure. This means freeing
2327 * what was already in it.
2328 */
2329 nasm_free(smac->name);
2330 free_tlist(smac->expansion);
2331 }
2332 } else {
2333 smac = nasm_malloc(sizeof(SMacro));
2334 smac->next = *smhead;
2335 *smhead = smac;
2336 }
2337 smac->name = nasm_strdup(mname);
2338 smac->casesense = ((i == PP_DEFINE) || (i == PP_XDEFINE));
2339 smac->nparam = nparam;
2340 smac->expansion = macro_start;
2341 smac->in_progress = FALSE;
2342 free_tlist(origline);
2343 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002344
H. Peter Anvine2c80182005-01-15 22:15:51 +00002345 case PP_UNDEF:
2346 tline = tline->next;
2347 skip_white_(tline);
2348 tline = expand_id(tline);
2349 if (!tline || (tline->type != TOK_ID &&
2350 (tline->type != TOK_PREPROC_ID ||
2351 tline->text[1] != '$'))) {
2352 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2353 free_tlist(origline);
2354 return DIRECTIVE_FOUND;
2355 }
2356 if (tline->next) {
2357 error(ERR_WARNING,
2358 "trailing garbage after macro name ignored");
2359 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002360
H. Peter Anvine2c80182005-01-15 22:15:51 +00002361 /* Find the context that symbol belongs to */
2362 ctx = get_ctx(tline->text, FALSE);
2363 if (!ctx)
2364 smhead = &smacros[hash(tline->text)];
2365 else
2366 smhead = &ctx->localmac;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002367
H. Peter Anvine2c80182005-01-15 22:15:51 +00002368 mname = tline->text;
2369 last = tline;
2370 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002371
H. Peter Anvine2c80182005-01-15 22:15:51 +00002372 /*
2373 * We now have a macro name... go hunt for it.
2374 */
2375 while (smacro_defined(ctx, mname, -1, &smac, 1)) {
2376 /* Defined, so we need to find its predecessor and nuke it */
2377 SMacro **s;
2378 for (s = smhead; *s && *s != smac; s = &(*s)->next) ;
2379 if (*s) {
2380 *s = smac->next;
2381 nasm_free(smac->name);
2382 free_tlist(smac->expansion);
2383 nasm_free(smac);
2384 }
2385 }
2386 free_tlist(origline);
2387 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002388
H. Peter Anvine2c80182005-01-15 22:15:51 +00002389 case PP_STRLEN:
2390 tline = tline->next;
2391 skip_white_(tline);
2392 tline = expand_id(tline);
2393 if (!tline || (tline->type != TOK_ID &&
2394 (tline->type != TOK_PREPROC_ID ||
2395 tline->text[1] != '$'))) {
2396 error(ERR_NONFATAL,
2397 "`%%strlen' expects a macro identifier as first parameter");
2398 free_tlist(origline);
2399 return DIRECTIVE_FOUND;
2400 }
2401 ctx = get_ctx(tline->text, FALSE);
2402 if (!ctx)
2403 smhead = &smacros[hash(tline->text)];
2404 else
2405 smhead = &ctx->localmac;
2406 mname = tline->text;
2407 last = tline;
2408 tline = expand_smacro(tline->next);
2409 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002410
H. Peter Anvine2c80182005-01-15 22:15:51 +00002411 t = tline;
2412 while (tok_type_(t, TOK_WHITESPACE))
2413 t = t->next;
2414 /* t should now point to the string */
2415 if (t->type != TOK_STRING) {
2416 error(ERR_NONFATAL,
2417 "`%%strlen` requires string as second parameter");
2418 free_tlist(tline);
2419 free_tlist(origline);
2420 return DIRECTIVE_FOUND;
2421 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002422
H. Peter Anvine2c80182005-01-15 22:15:51 +00002423 macro_start = nasm_malloc(sizeof(*macro_start));
2424 macro_start->next = NULL;
2425 make_tok_num(macro_start, strlen(t->text) - 2);
2426 macro_start->mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002427
H. Peter Anvine2c80182005-01-15 22:15:51 +00002428 /*
2429 * We now have a macro name, an implicit parameter count of
2430 * zero, and a numeric token to use as an expansion. Create
2431 * and store an SMacro.
2432 */
2433 if (smacro_defined(ctx, mname, 0, &smac, i == PP_STRLEN)) {
2434 if (!smac)
2435 error(ERR_WARNING,
2436 "single-line macro `%s' defined both with and"
2437 " without parameters", mname);
2438 else {
2439 /*
2440 * We're redefining, so we have to take over an
2441 * existing SMacro structure. This means freeing
2442 * what was already in it.
2443 */
2444 nasm_free(smac->name);
2445 free_tlist(smac->expansion);
2446 }
2447 } else {
2448 smac = nasm_malloc(sizeof(SMacro));
2449 smac->next = *smhead;
2450 *smhead = smac;
2451 }
2452 smac->name = nasm_strdup(mname);
2453 smac->casesense = (i == PP_STRLEN);
2454 smac->nparam = 0;
2455 smac->expansion = macro_start;
2456 smac->in_progress = FALSE;
2457 free_tlist(tline);
2458 free_tlist(origline);
2459 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002460
H. Peter Anvine2c80182005-01-15 22:15:51 +00002461 case PP_SUBSTR:
2462 tline = tline->next;
2463 skip_white_(tline);
2464 tline = expand_id(tline);
2465 if (!tline || (tline->type != TOK_ID &&
2466 (tline->type != TOK_PREPROC_ID ||
2467 tline->text[1] != '$'))) {
2468 error(ERR_NONFATAL,
2469 "`%%substr' expects a macro identifier as first parameter");
2470 free_tlist(origline);
2471 return DIRECTIVE_FOUND;
2472 }
2473 ctx = get_ctx(tline->text, FALSE);
2474 if (!ctx)
2475 smhead = &smacros[hash(tline->text)];
2476 else
2477 smhead = &ctx->localmac;
2478 mname = tline->text;
2479 last = tline;
2480 tline = expand_smacro(tline->next);
2481 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002482
H. Peter Anvine2c80182005-01-15 22:15:51 +00002483 t = tline->next;
2484 while (tok_type_(t, TOK_WHITESPACE))
2485 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002486
H. Peter Anvine2c80182005-01-15 22:15:51 +00002487 /* t should now point to the string */
2488 if (t->type != TOK_STRING) {
2489 error(ERR_NONFATAL,
2490 "`%%substr` requires string as second parameter");
2491 free_tlist(tline);
2492 free_tlist(origline);
2493 return DIRECTIVE_FOUND;
2494 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002495
H. Peter Anvine2c80182005-01-15 22:15:51 +00002496 tt = t->next;
2497 tptr = &tt;
2498 tokval.t_type = TOKEN_INVALID;
2499 evalresult =
2500 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2501 if (!evalresult) {
2502 free_tlist(tline);
2503 free_tlist(origline);
2504 return DIRECTIVE_FOUND;
2505 }
2506 if (!is_simple(evalresult)) {
2507 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2508 free_tlist(tline);
2509 free_tlist(origline);
2510 return DIRECTIVE_FOUND;
2511 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002512
H. Peter Anvine2c80182005-01-15 22:15:51 +00002513 macro_start = nasm_malloc(sizeof(*macro_start));
2514 macro_start->next = NULL;
2515 macro_start->text = nasm_strdup("'''");
2516 if (evalresult->value > 0
2517 && evalresult->value < strlen(t->text) - 1) {
2518 macro_start->text[1] = t->text[evalresult->value];
2519 } else {
2520 macro_start->text[2] = '\0';
2521 }
2522 macro_start->type = TOK_STRING;
2523 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002524
H. Peter Anvine2c80182005-01-15 22:15:51 +00002525 /*
2526 * We now have a macro name, an implicit parameter count of
2527 * zero, and a numeric token to use as an expansion. Create
2528 * and store an SMacro.
2529 */
2530 if (smacro_defined(ctx, mname, 0, &smac, i == PP_SUBSTR)) {
2531 if (!smac)
2532 error(ERR_WARNING,
2533 "single-line macro `%s' defined both with and"
2534 " without parameters", mname);
2535 else {
2536 /*
2537 * We're redefining, so we have to take over an
2538 * existing SMacro structure. This means freeing
2539 * what was already in it.
2540 */
2541 nasm_free(smac->name);
2542 free_tlist(smac->expansion);
2543 }
2544 } else {
2545 smac = nasm_malloc(sizeof(SMacro));
2546 smac->next = *smhead;
2547 *smhead = smac;
2548 }
2549 smac->name = nasm_strdup(mname);
2550 smac->casesense = (i == PP_SUBSTR);
2551 smac->nparam = 0;
2552 smac->expansion = macro_start;
2553 smac->in_progress = FALSE;
2554 free_tlist(tline);
2555 free_tlist(origline);
2556 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002557
H. Peter Anvine2c80182005-01-15 22:15:51 +00002558 case PP_ASSIGN:
2559 case PP_IASSIGN:
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,
2567 "`%%%sassign' expects a macro identifier",
2568 (i == PP_IASSIGN ? "i" : ""));
2569 free_tlist(origline);
2570 return DIRECTIVE_FOUND;
2571 }
2572 ctx = get_ctx(tline->text, FALSE);
2573 if (!ctx)
2574 smhead = &smacros[hash(tline->text)];
2575 else
2576 smhead = &ctx->localmac;
2577 mname = tline->text;
2578 last = tline;
2579 tline = expand_smacro(tline->next);
2580 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002581
H. Peter Anvine2c80182005-01-15 22:15:51 +00002582 t = tline;
2583 tptr = &t;
2584 tokval.t_type = TOKEN_INVALID;
2585 evalresult =
2586 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2587 free_tlist(tline);
2588 if (!evalresult) {
2589 free_tlist(origline);
2590 return DIRECTIVE_FOUND;
2591 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002592
H. Peter Anvine2c80182005-01-15 22:15:51 +00002593 if (tokval.t_type)
2594 error(ERR_WARNING,
2595 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00002596
H. Peter Anvine2c80182005-01-15 22:15:51 +00002597 if (!is_simple(evalresult)) {
2598 error(ERR_NONFATAL,
2599 "non-constant value given to `%%%sassign'",
2600 (i == PP_IASSIGN ? "i" : ""));
2601 free_tlist(origline);
2602 return DIRECTIVE_FOUND;
2603 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002604
H. Peter Anvine2c80182005-01-15 22:15:51 +00002605 macro_start = nasm_malloc(sizeof(*macro_start));
2606 macro_start->next = NULL;
2607 make_tok_num(macro_start, reloc_value(evalresult));
2608 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002609
H. Peter Anvine2c80182005-01-15 22:15:51 +00002610 /*
2611 * We now have a macro name, an implicit parameter count of
2612 * zero, and a numeric token to use as an expansion. Create
2613 * and store an SMacro.
2614 */
2615 if (smacro_defined(ctx, mname, 0, &smac, i == PP_ASSIGN)) {
2616 if (!smac)
2617 error(ERR_WARNING,
2618 "single-line macro `%s' defined both with and"
2619 " without parameters", mname);
2620 else {
2621 /*
2622 * We're redefining, so we have to take over an
2623 * existing SMacro structure. This means freeing
2624 * what was already in it.
2625 */
2626 nasm_free(smac->name);
2627 free_tlist(smac->expansion);
2628 }
2629 } else {
2630 smac = nasm_malloc(sizeof(SMacro));
2631 smac->next = *smhead;
2632 *smhead = smac;
2633 }
2634 smac->name = nasm_strdup(mname);
2635 smac->casesense = (i == PP_ASSIGN);
2636 smac->nparam = 0;
2637 smac->expansion = macro_start;
2638 smac->in_progress = FALSE;
2639 free_tlist(origline);
2640 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002641
H. Peter Anvine2c80182005-01-15 22:15:51 +00002642 case PP_LINE:
2643 /*
2644 * Syntax is `%line nnn[+mmm] [filename]'
2645 */
2646 tline = tline->next;
2647 skip_white_(tline);
2648 if (!tok_type_(tline, TOK_NUMBER)) {
2649 error(ERR_NONFATAL, "`%%line' expects line number");
2650 free_tlist(origline);
2651 return DIRECTIVE_FOUND;
2652 }
2653 k = readnum(tline->text, &j);
2654 m = 1;
2655 tline = tline->next;
2656 if (tok_is_(tline, "+")) {
2657 tline = tline->next;
2658 if (!tok_type_(tline, TOK_NUMBER)) {
2659 error(ERR_NONFATAL, "`%%line' expects line increment");
2660 free_tlist(origline);
2661 return DIRECTIVE_FOUND;
2662 }
2663 m = readnum(tline->text, &j);
2664 tline = tline->next;
2665 }
2666 skip_white_(tline);
2667 src_set_linnum(k);
2668 istk->lineinc = m;
2669 if (tline) {
2670 nasm_free(src_set_fname(detoken(tline, FALSE)));
2671 }
2672 free_tlist(origline);
2673 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002674
H. Peter Anvine2c80182005-01-15 22:15:51 +00002675 default:
2676 error(ERR_FATAL,
2677 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00002678 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002679 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002680 }
Ed Beroset3ab3f412002-06-11 03:31:49 +00002681 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002682}
2683
2684/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002685 * Ensure that a macro parameter contains a condition code and
2686 * nothing else. Return the condition code index if so, or -1
2687 * otherwise.
2688 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002689static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002690{
H. Peter Anvin76690a12002-04-30 20:52:49 +00002691 Token *tt;
2692 int i, j, k, m;
2693
H. Peter Anvineba20a72002-04-30 20:53:55 +00002694 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002695 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002696 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002697 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002698 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002699 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002700 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002701
2702 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00002703 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002704 while (j - i > 1) {
2705 k = (j + i) / 2;
2706 m = nasm_stricmp(t->text, conditions[k]);
2707 if (m == 0) {
2708 i = k;
2709 j = -2;
2710 break;
2711 } else if (m < 0) {
2712 j = k;
2713 } else
2714 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002715 }
2716 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002717 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002718 return i;
2719}
2720
2721/*
2722 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2723 * %-n) and MMacro-local identifiers (%%foo).
2724 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002725static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002726{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002727 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002728
2729 tail = &thead;
2730 thead = NULL;
2731
H. Peter Anvine2c80182005-01-15 22:15:51 +00002732 while (tline) {
2733 if (tline->type == TOK_PREPROC_ID &&
2734 (((tline->text[1] == '+' || tline->text[1] == '-')
2735 && tline->text[2]) || tline->text[1] == '%'
2736 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002737 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002738 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002739 char tmpbuf[30];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002740 int n, i;
2741 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002742
H. Peter Anvine2c80182005-01-15 22:15:51 +00002743 t = tline;
2744 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002745
H. Peter Anvine2c80182005-01-15 22:15:51 +00002746 mac = istk->mstk;
2747 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2748 mac = mac->next_active;
2749 if (!mac)
2750 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2751 else
2752 switch (t->text[1]) {
2753 /*
2754 * We have to make a substitution of one of the
2755 * forms %1, %-1, %+1, %%foo, %0.
2756 */
2757 case '0':
2758 type = TOK_NUMBER;
2759 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
2760 text = nasm_strdup(tmpbuf);
2761 break;
2762 case '%':
2763 type = TOK_ID;
Keith Kanios93f2e9a2007-04-14 00:10:59 +00002764 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu32".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002765 mac->unique);
2766 text = nasm_strcat(tmpbuf, t->text + 2);
2767 break;
2768 case '-':
2769 n = atoi(t->text + 2) - 1;
2770 if (n >= mac->nparam)
2771 tt = NULL;
2772 else {
2773 if (mac->nparam > 1)
2774 n = (n + mac->rotate) % mac->nparam;
2775 tt = mac->params[n];
2776 }
2777 cc = find_cc(tt);
2778 if (cc == -1) {
2779 error(ERR_NONFATAL,
2780 "macro parameter %d is not a condition code",
2781 n + 1);
2782 text = NULL;
2783 } else {
2784 type = TOK_ID;
2785 if (inverse_ccs[cc] == -1) {
2786 error(ERR_NONFATAL,
2787 "condition code `%s' is not invertible",
2788 conditions[cc]);
2789 text = NULL;
2790 } else
2791 text =
2792 nasm_strdup(conditions[inverse_ccs[cc]]);
2793 }
2794 break;
2795 case '+':
2796 n = atoi(t->text + 2) - 1;
2797 if (n >= mac->nparam)
2798 tt = NULL;
2799 else {
2800 if (mac->nparam > 1)
2801 n = (n + mac->rotate) % mac->nparam;
2802 tt = mac->params[n];
2803 }
2804 cc = find_cc(tt);
2805 if (cc == -1) {
2806 error(ERR_NONFATAL,
2807 "macro parameter %d is not a condition code",
2808 n + 1);
2809 text = NULL;
2810 } else {
2811 type = TOK_ID;
2812 text = nasm_strdup(conditions[cc]);
2813 }
2814 break;
2815 default:
2816 n = atoi(t->text + 1) - 1;
2817 if (n >= mac->nparam)
2818 tt = NULL;
2819 else {
2820 if (mac->nparam > 1)
2821 n = (n + mac->rotate) % mac->nparam;
2822 tt = mac->params[n];
2823 }
2824 if (tt) {
2825 for (i = 0; i < mac->paramlen[n]; i++) {
2826 *tail = new_Token(NULL, tt->type, tt->text, 0);
2827 tail = &(*tail)->next;
2828 tt = tt->next;
2829 }
2830 }
2831 text = NULL; /* we've done it here */
2832 break;
2833 }
2834 if (!text) {
2835 delete_Token(t);
2836 } else {
2837 *tail = t;
2838 tail = &t->next;
2839 t->type = type;
2840 nasm_free(t->text);
2841 t->text = text;
2842 t->mac = NULL;
2843 }
2844 continue;
2845 } else {
2846 t = *tail = tline;
2847 tline = tline->next;
2848 t->mac = NULL;
2849 tail = &t->next;
2850 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002851 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00002852 *tail = NULL;
2853 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002854 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002855 switch (t->type) {
2856 case TOK_WHITESPACE:
2857 if (tt->type == TOK_WHITESPACE) {
2858 t->next = delete_Token(tt);
2859 }
2860 break;
2861 case TOK_ID:
2862 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002863 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002864 nasm_free(t->text);
2865 t->text = tmp;
2866 t->next = delete_Token(tt);
2867 }
2868 break;
2869 case TOK_NUMBER:
2870 if (tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002871 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002872 nasm_free(t->text);
2873 t->text = tmp;
2874 t->next = delete_Token(tt);
2875 }
2876 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002877 default:
2878 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002879 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002880
H. Peter Anvin76690a12002-04-30 20:52:49 +00002881 return thead;
2882}
2883
2884/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002885 * Expand all single-line macro calls made in the given line.
2886 * Return the expanded version of the line. The original is deemed
2887 * to be destroyed in the process. (In reality we'll just move
2888 * Tokens from input to output a lot of the time, rather than
2889 * actually bothering to destroy and replicate.)
2890 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002891static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002892{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002893 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002894 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002895 Token **params;
2896 int *paramsize;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002897 int nparam, sparam, brackets, rescan;
2898 Token *org_tline = tline;
2899 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002900 char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002901
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002902 /*
2903 * Trick: we should avoid changing the start token pointer since it can
2904 * be contained in "next" field of other token. Because of this
2905 * we allocate a copy of first token and work with it; at the end of
2906 * routine we copy it back
2907 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002908 if (org_tline) {
2909 tline =
2910 new_Token(org_tline->next, org_tline->type, org_tline->text,
2911 0);
2912 tline->mac = org_tline->mac;
2913 nasm_free(org_tline->text);
2914 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002915 }
2916
H. Peter Anvin734b1882002-04-30 21:01:08 +00002917 again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002918 tail = &thead;
2919 thead = NULL;
2920
H. Peter Anvine2c80182005-01-15 22:15:51 +00002921 while (tline) { /* main token loop */
2922 if ((mname = tline->text)) {
2923 /* if this token is a local macro, look in local context */
2924 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
2925 ctx = get_ctx(mname, TRUE);
2926 else
2927 ctx = NULL;
2928 if (!ctx)
2929 head = smacros[hash(mname)];
2930 else
2931 head = ctx->localmac;
2932 /*
2933 * We've hit an identifier. As in is_mmacro below, we first
2934 * check whether the identifier is a single-line macro at
2935 * all, then think about checking for parameters if
2936 * necessary.
2937 */
2938 for (m = head; m; m = m->next)
2939 if (!mstrcmp(m->name, mname, m->casesense))
2940 break;
2941 if (m) {
2942 mstart = tline;
2943 params = NULL;
2944 paramsize = NULL;
2945 if (m->nparam == 0) {
2946 /*
2947 * Simple case: the macro is parameterless. Discard the
2948 * one token that the macro call took, and push the
2949 * expansion back on the to-do stack.
2950 */
2951 if (!m->expansion) {
2952 if (!strcmp("__FILE__", m->name)) {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002953 int32_t num = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002954 src_get(&num, &(tline->text));
2955 nasm_quote(&(tline->text));
2956 tline->type = TOK_STRING;
2957 continue;
2958 }
2959 if (!strcmp("__LINE__", m->name)) {
2960 nasm_free(tline->text);
2961 make_tok_num(tline, src_get_linnum());
2962 continue;
2963 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002964 if (!strcmp("__BITS__", m->name)) {
2965 nasm_free(tline->text);
2966 make_tok_num(tline, globalbits);
2967 continue;
2968 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002969 tline = delete_Token(tline);
2970 continue;
2971 }
2972 } else {
2973 /*
2974 * Complicated case: at least one macro with this name
2975 * exists and takes parameters. We must find the
2976 * parameters in the call, count them, find the SMacro
2977 * that corresponds to that form of the macro call, and
2978 * substitute for the parameters when we expand. What a
2979 * pain.
2980 */
2981 /*tline = tline->next;
2982 skip_white_(tline); */
2983 do {
2984 t = tline->next;
2985 while (tok_type_(t, TOK_SMAC_END)) {
2986 t->mac->in_progress = FALSE;
2987 t->text = NULL;
2988 t = tline->next = delete_Token(t);
2989 }
2990 tline = t;
2991 } while (tok_type_(tline, TOK_WHITESPACE));
2992 if (!tok_is_(tline, "(")) {
2993 /*
2994 * This macro wasn't called with parameters: ignore
2995 * the call. (Behaviour borrowed from gnu cpp.)
2996 */
2997 tline = mstart;
2998 m = NULL;
2999 } else {
3000 int paren = 0;
3001 int white = 0;
3002 brackets = 0;
3003 nparam = 0;
3004 sparam = PARAM_DELTA;
3005 params = nasm_malloc(sparam * sizeof(Token *));
3006 params[0] = tline->next;
3007 paramsize = nasm_malloc(sparam * sizeof(int));
3008 paramsize[0] = 0;
3009 while (TRUE) { /* parameter loop */
3010 /*
3011 * For some unusual expansions
3012 * which concatenates function call
3013 */
3014 t = tline->next;
3015 while (tok_type_(t, TOK_SMAC_END)) {
3016 t->mac->in_progress = FALSE;
3017 t->text = NULL;
3018 t = tline->next = delete_Token(t);
3019 }
3020 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003021
H. Peter Anvine2c80182005-01-15 22:15:51 +00003022 if (!tline) {
3023 error(ERR_NONFATAL,
3024 "macro call expects terminating `)'");
3025 break;
3026 }
3027 if (tline->type == TOK_WHITESPACE
3028 && brackets <= 0) {
3029 if (paramsize[nparam])
3030 white++;
3031 else
3032 params[nparam] = tline->next;
3033 continue; /* parameter loop */
3034 }
3035 if (tline->type == TOK_OTHER
3036 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003037 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003038 if (ch == ',' && !paren && brackets <= 0) {
3039 if (++nparam >= sparam) {
3040 sparam += PARAM_DELTA;
3041 params = nasm_realloc(params,
3042 sparam *
3043 sizeof(Token
3044 *));
3045 paramsize =
3046 nasm_realloc(paramsize,
3047 sparam *
3048 sizeof(int));
3049 }
3050 params[nparam] = tline->next;
3051 paramsize[nparam] = 0;
3052 white = 0;
3053 continue; /* parameter loop */
3054 }
3055 if (ch == '{' &&
3056 (brackets > 0 || (brackets == 0 &&
3057 !paramsize[nparam])))
3058 {
3059 if (!(brackets++)) {
3060 params[nparam] = tline->next;
3061 continue; /* parameter loop */
3062 }
3063 }
3064 if (ch == '}' && brackets > 0)
3065 if (--brackets == 0) {
3066 brackets = -1;
3067 continue; /* parameter loop */
3068 }
3069 if (ch == '(' && !brackets)
3070 paren++;
3071 if (ch == ')' && brackets <= 0)
3072 if (--paren < 0)
3073 break;
3074 }
3075 if (brackets < 0) {
3076 brackets = 0;
3077 error(ERR_NONFATAL, "braces do not "
3078 "enclose all of macro parameter");
3079 }
3080 paramsize[nparam] += white + 1;
3081 white = 0;
3082 } /* parameter loop */
3083 nparam++;
3084 while (m && (m->nparam != nparam ||
3085 mstrcmp(m->name, mname,
3086 m->casesense)))
3087 m = m->next;
3088 if (!m)
3089 error(ERR_WARNING | ERR_WARN_MNP,
3090 "macro `%s' exists, "
3091 "but not taking %d parameters",
3092 mstart->text, nparam);
3093 }
3094 }
3095 if (m && m->in_progress)
3096 m = NULL;
3097 if (!m) { /* in progess or didn't find '(' or wrong nparam */
3098 /*
3099 * Design question: should we handle !tline, which
3100 * indicates missing ')' here, or expand those
3101 * macros anyway, which requires the (t) test a few
3102 * lines down?
3103 */
3104 nasm_free(params);
3105 nasm_free(paramsize);
3106 tline = mstart;
3107 } else {
3108 /*
3109 * Expand the macro: we are placed on the last token of the
3110 * call, so that we can easily split the call from the
3111 * following tokens. We also start by pushing an SMAC_END
3112 * token for the cycle removal.
3113 */
3114 t = tline;
3115 if (t) {
3116 tline = t->next;
3117 t->next = NULL;
3118 }
3119 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3120 tt->mac = m;
3121 m->in_progress = TRUE;
3122 tline = tt;
3123 for (t = m->expansion; t; t = t->next) {
3124 if (t->type >= TOK_SMAC_PARAM) {
3125 Token *pcopy = tline, **ptail = &pcopy;
3126 Token *ttt, *pt;
3127 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003128
H. Peter Anvine2c80182005-01-15 22:15:51 +00003129 ttt = params[t->type - TOK_SMAC_PARAM];
3130 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3131 --i >= 0;) {
3132 pt = *ptail =
3133 new_Token(tline, ttt->type, ttt->text,
3134 0);
3135 ptail = &pt->next;
3136 ttt = ttt->next;
3137 }
3138 tline = pcopy;
3139 } else {
3140 tt = new_Token(tline, t->type, t->text, 0);
3141 tline = tt;
3142 }
3143 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003144
H. Peter Anvine2c80182005-01-15 22:15:51 +00003145 /*
3146 * Having done that, get rid of the macro call, and clean
3147 * up the parameters.
3148 */
3149 nasm_free(params);
3150 nasm_free(paramsize);
3151 free_tlist(mstart);
3152 continue; /* main token loop */
3153 }
3154 }
3155 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003156
H. Peter Anvine2c80182005-01-15 22:15:51 +00003157 if (tline->type == TOK_SMAC_END) {
3158 tline->mac->in_progress = FALSE;
3159 tline = delete_Token(tline);
3160 } else {
3161 t = *tail = tline;
3162 tline = tline->next;
3163 t->mac = NULL;
3164 t->next = NULL;
3165 tail = &t->next;
3166 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003167 }
3168
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003169 /*
3170 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003171 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003172 * TOK_IDs should be concatenated.
3173 * Also we look for %+ tokens and concatenate the tokens before and after
3174 * them (without white spaces in between).
3175 */
3176 t = thead;
3177 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003178 while (t) {
3179 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3180 t = t->next;
3181 if (!t || !t->next)
3182 break;
3183 if (t->next->type == TOK_ID ||
3184 t->next->type == TOK_PREPROC_ID ||
3185 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003186 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003187 nasm_free(t->text);
3188 t->next = delete_Token(t->next);
3189 t->text = p;
3190 rescan = 1;
3191 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3192 t->next->next->type == TOK_PREPROC_ID &&
3193 strcmp(t->next->next->text, "%+") == 0) {
3194 /* free the next whitespace, the %+ token and next whitespace */
3195 int i;
3196 for (i = 1; i <= 3; i++) {
3197 if (!t->next
3198 || (i != 2 && t->next->type != TOK_WHITESPACE))
3199 break;
3200 t->next = delete_Token(t->next);
3201 } /* endfor */
3202 } else
3203 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003204 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003205 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003206 if (rescan) {
3207 tline = thead;
3208 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003209 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003210
H. Peter Anvine2c80182005-01-15 22:15:51 +00003211 if (org_tline) {
3212 if (thead) {
3213 *org_tline = *thead;
3214 /* since we just gave text to org_line, don't free it */
3215 thead->text = NULL;
3216 delete_Token(thead);
3217 } else {
3218 /* the expression expanded to empty line;
3219 we can't return NULL for some reasons
3220 we just set the line to a single WHITESPACE token. */
3221 memset(org_tline, 0, sizeof(*org_tline));
3222 org_tline->text = NULL;
3223 org_tline->type = TOK_WHITESPACE;
3224 }
3225 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003226 }
3227
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003228 return thead;
3229}
3230
3231/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003232 * Similar to expand_smacro but used exclusively with macro identifiers
3233 * right before they are fetched in. The reason is that there can be
3234 * identifiers consisting of several subparts. We consider that if there
3235 * are more than one element forming the name, user wants a expansion,
3236 * otherwise it will be left as-is. Example:
3237 *
3238 * %define %$abc cde
3239 *
3240 * the identifier %$abc will be left as-is so that the handler for %define
3241 * will suck it and define the corresponding value. Other case:
3242 *
3243 * %define _%$abc cde
3244 *
3245 * In this case user wants name to be expanded *before* %define starts
3246 * working, so we'll expand %$abc into something (if it has a value;
3247 * otherwise it will be left as-is) then concatenate all successive
3248 * PP_IDs into one.
3249 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003250static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003251{
3252 Token *cur, *oldnext = NULL;
3253
H. Peter Anvin734b1882002-04-30 21:01:08 +00003254 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003255 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003256
3257 cur = tline;
3258 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003259 (cur->next->type == TOK_ID ||
3260 cur->next->type == TOK_PREPROC_ID
3261 || cur->next->type == TOK_NUMBER))
3262 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003263
3264 /* If identifier consists of just one token, don't expand */
3265 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003266 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003267
H. Peter Anvine2c80182005-01-15 22:15:51 +00003268 if (cur) {
3269 oldnext = cur->next; /* Detach the tail past identifier */
3270 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003271 }
3272
H. Peter Anvin734b1882002-04-30 21:01:08 +00003273 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003274
H. Peter Anvine2c80182005-01-15 22:15:51 +00003275 if (cur) {
3276 /* expand_smacro possibly changhed tline; re-scan for EOL */
3277 cur = tline;
3278 while (cur && cur->next)
3279 cur = cur->next;
3280 if (cur)
3281 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003282 }
3283
3284 return tline;
3285}
3286
3287/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003288 * Determine whether the given line constitutes a multi-line macro
3289 * call, and return the MMacro structure called if so. Doesn't have
3290 * to check for an initial label - that's taken care of in
3291 * expand_mmacro - but must check numbers of parameters. Guaranteed
3292 * to be called with tline->type == TOK_ID, so the putative macro
3293 * name is easy to find.
3294 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003295static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003296{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003297 MMacro *head, *m;
3298 Token **params;
3299 int nparam;
3300
3301 head = mmacros[hash(tline->text)];
3302
3303 /*
3304 * Efficiency: first we see if any macro exists with the given
3305 * name. If not, we can return NULL immediately. _Then_ we
3306 * count the parameters, and then we look further along the
3307 * list if necessary to find the proper MMacro.
3308 */
3309 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003310 if (!mstrcmp(m->name, tline->text, m->casesense))
3311 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003312 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003313 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003314
3315 /*
3316 * OK, we have a potential macro. Count and demarcate the
3317 * parameters.
3318 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003319 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003320
3321 /*
3322 * So we know how many parameters we've got. Find the MMacro
3323 * structure that handles this number.
3324 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003325 while (m) {
3326 if (m->nparam_min <= nparam
3327 && (m->plus || nparam <= m->nparam_max)) {
3328 /*
3329 * This one is right. Just check if cycle removal
3330 * prohibits us using it before we actually celebrate...
3331 */
3332 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003333#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003334 error(ERR_NONFATAL,
3335 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003336#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003337 nasm_free(params);
3338 return NULL;
3339 }
3340 /*
3341 * It's right, and we can use it. Add its default
3342 * parameters to the end of our list if necessary.
3343 */
3344 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3345 params =
3346 nasm_realloc(params,
3347 ((m->nparam_min + m->ndefs +
3348 1) * sizeof(*params)));
3349 while (nparam < m->nparam_min + m->ndefs) {
3350 params[nparam] = m->defaults[nparam - m->nparam_min];
3351 nparam++;
3352 }
3353 }
3354 /*
3355 * If we've gone over the maximum parameter count (and
3356 * we're in Plus mode), ignore parameters beyond
3357 * nparam_max.
3358 */
3359 if (m->plus && nparam > m->nparam_max)
3360 nparam = m->nparam_max;
3361 /*
3362 * Then terminate the parameter list, and leave.
3363 */
3364 if (!params) { /* need this special case */
3365 params = nasm_malloc(sizeof(*params));
3366 nparam = 0;
3367 }
3368 params[nparam] = NULL;
3369 *params_array = params;
3370 return m;
3371 }
3372 /*
3373 * This one wasn't right: look for the next one with the
3374 * same name.
3375 */
3376 for (m = m->next; m; m = m->next)
3377 if (!mstrcmp(m->name, tline->text, m->casesense))
3378 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003379 }
3380
3381 /*
3382 * After all that, we didn't find one with the right number of
3383 * parameters. Issue a warning, and fail to expand the macro.
3384 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003385 error(ERR_WARNING | ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003386 "macro `%s' exists, but not taking %d parameters",
3387 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003388 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003389 return NULL;
3390}
3391
3392/*
3393 * Expand the multi-line macro call made by the given line, if
3394 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003395 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003396 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003397static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003398{
3399 Token *startline = tline;
3400 Token *label = NULL;
3401 int dont_prepend = 0;
3402 Token **params, *t, *tt;
3403 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003404 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003405 int i, nparam, *paramlen;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003406
3407 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003408 skip_white_(t);
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003409/* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3410 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003411 return 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003412 m = is_mmacro(t, &params);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003413 if (!m) {
3414 Token *last;
3415 /*
3416 * We have an id which isn't a macro call. We'll assume
3417 * it might be a label; we'll also check to see if a
3418 * colon follows it. Then, if there's another id after
3419 * that lot, we'll check it again for macro-hood.
3420 */
3421 label = last = t;
3422 t = t->next;
3423 if (tok_type_(t, TOK_WHITESPACE))
3424 last = t, t = t->next;
3425 if (tok_is_(t, ":")) {
3426 dont_prepend = 1;
3427 last = t, t = t->next;
3428 if (tok_type_(t, TOK_WHITESPACE))
3429 last = t, t = t->next;
3430 }
3431 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3432 return 0;
3433 last->next = NULL;
3434 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003435 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003436
3437 /*
3438 * Fix up the parameters: this involves stripping leading and
3439 * trailing whitespace, then stripping braces if they are
3440 * present.
3441 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003442 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003443 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003444
H. Peter Anvine2c80182005-01-15 22:15:51 +00003445 for (i = 0; params[i]; i++) {
3446 int brace = FALSE;
3447 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003448
H. Peter Anvine2c80182005-01-15 22:15:51 +00003449 t = params[i];
3450 skip_white_(t);
3451 if (tok_is_(t, "{"))
3452 t = t->next, brace = TRUE, comma = FALSE;
3453 params[i] = t;
3454 paramlen[i] = 0;
3455 while (t) {
3456 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3457 break; /* ... because we have hit a comma */
3458 if (comma && t->type == TOK_WHITESPACE
3459 && tok_is_(t->next, ","))
3460 break; /* ... or a space then a comma */
3461 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3462 break; /* ... or a brace */
3463 t = t->next;
3464 paramlen[i]++;
3465 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003466 }
3467
3468 /*
3469 * OK, we have a MMacro structure together with a set of
3470 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003471 * copies of each Line on to istk->expansion. Substitution of
3472 * parameter tokens and macro-local tokens doesn't get done
3473 * until the single-line macro substitution process; this is
3474 * because delaying them allows us to change the semantics
3475 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003476 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003477 * First, push an end marker on to istk->expansion, mark this
3478 * macro as in progress, and set up its invocation-specific
3479 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003480 */
3481 ll = nasm_malloc(sizeof(Line));
3482 ll->next = istk->expansion;
3483 ll->finishes = m;
3484 ll->first = NULL;
3485 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003486
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003487 m->in_progress = TRUE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003488 m->params = params;
3489 m->iline = tline;
3490 m->nparam = nparam;
3491 m->rotate = 0;
3492 m->paramlen = paramlen;
3493 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003494 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003495
3496 m->next_active = istk->mstk;
3497 istk->mstk = m;
3498
H. Peter Anvine2c80182005-01-15 22:15:51 +00003499 for (l = m->expansion; l; l = l->next) {
3500 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003501
H. Peter Anvine2c80182005-01-15 22:15:51 +00003502 ll = nasm_malloc(sizeof(Line));
3503 ll->finishes = NULL;
3504 ll->next = istk->expansion;
3505 istk->expansion = ll;
3506 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003507
H. Peter Anvine2c80182005-01-15 22:15:51 +00003508 for (t = l->first; t; t = t->next) {
3509 Token *x = t;
3510 if (t->type == TOK_PREPROC_ID &&
3511 t->text[1] == '0' && t->text[2] == '0') {
3512 dont_prepend = -1;
3513 x = label;
3514 if (!x)
3515 continue;
3516 }
3517 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3518 tail = &tt->next;
3519 }
3520 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003521 }
3522
3523 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00003524 * If we had a label, push it on as the first line of
3525 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003526 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003527 if (label) {
3528 if (dont_prepend < 0)
3529 free_tlist(startline);
3530 else {
3531 ll = nasm_malloc(sizeof(Line));
3532 ll->finishes = NULL;
3533 ll->next = istk->expansion;
3534 istk->expansion = ll;
3535 ll->first = startline;
3536 if (!dont_prepend) {
3537 while (label->next)
3538 label = label->next;
3539 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3540 }
3541 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003542 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003543
H. Peter Anvin734b1882002-04-30 21:01:08 +00003544 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003545
H. Peter Anvineba20a72002-04-30 20:53:55 +00003546 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003547}
3548
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003549/*
3550 * Since preprocessor always operate only on the line that didn't
3551 * arrived yet, we should always use ERR_OFFBY1. Also since user
3552 * won't want to see same error twice (preprocessing is done once
3553 * per pass) we will want to show errors only during pass one.
3554 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003555static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003556{
3557 va_list arg;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003558 char buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003559
3560 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003561 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003562 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003563
H. Peter Anvin734b1882002-04-30 21:01:08 +00003564 va_start(arg, fmt);
Ed Beroset19f927a2004-12-15 17:07:03 +00003565 vsnprintf(buff, sizeof(buff), fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003566 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003567
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003568 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003569 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3570 istk->mstk->lineno, buff);
3571 else
3572 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003573}
3574
H. Peter Anvin734b1882002-04-30 21:01:08 +00003575static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003576pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003577 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003578{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003579 int h;
3580
H. Peter Anvin99941bf2002-05-14 17:44:03 +00003581 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003582 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003583 istk = nasm_malloc(sizeof(Include));
3584 istk->next = NULL;
3585 istk->conds = NULL;
3586 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003587 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003588 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00003589 istk->fname = NULL;
3590 src_set_fname(nasm_strdup(file));
3591 src_set_linnum(0);
3592 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003593 if (!istk->fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003594 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3595 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003596 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003597 for (h = 0; h < NHASH; h++) {
3598 mmacros[h] = NULL;
3599 smacros[h] = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003600 }
3601 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003602 if (tasm_compatible_mode) {
3603 stdmacpos = stdmac;
3604 } else {
3605 stdmacpos = &stdmac[TASM_MACRO_COUNT];
3606 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003607 any_extrastdmac = (extrastdmac != NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003608 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003609 evaluate = eval;
3610 pass = apass;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003611}
3612
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003613static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003614{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003615 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003616 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003617
H. Peter Anvine2c80182005-01-15 22:15:51 +00003618 while (1) {
3619 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003620 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00003621 * buffer or from the input file.
3622 */
3623 tline = NULL;
3624 while (istk->expansion && istk->expansion->finishes) {
3625 Line *l = istk->expansion;
3626 if (!l->finishes->name && l->finishes->in_progress > 1) {
3627 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003628
H. Peter Anvine2c80182005-01-15 22:15:51 +00003629 /*
3630 * This is a macro-end marker for a macro with no
3631 * name, which means it's not really a macro at all
3632 * but a %rep block, and the `in_progress' field is
3633 * more than 1, meaning that we still need to
3634 * repeat. (1 means the natural last repetition; 0
3635 * means termination by %exitrep.) We have
3636 * therefore expanded up to the %endrep, and must
3637 * push the whole block on to the expansion buffer
3638 * again. We don't bother to remove the macro-end
3639 * marker: we'd only have to generate another one
3640 * if we did.
3641 */
3642 l->finishes->in_progress--;
3643 for (l = l->finishes->expansion; l; l = l->next) {
3644 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003645
H. Peter Anvine2c80182005-01-15 22:15:51 +00003646 ll = nasm_malloc(sizeof(Line));
3647 ll->next = istk->expansion;
3648 ll->finishes = NULL;
3649 ll->first = NULL;
3650 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003651
H. Peter Anvine2c80182005-01-15 22:15:51 +00003652 for (t = l->first; t; t = t->next) {
3653 if (t->text || t->type == TOK_WHITESPACE) {
3654 tt = *tail =
3655 new_Token(NULL, t->type, t->text, 0);
3656 tail = &tt->next;
3657 }
3658 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003659
H. Peter Anvine2c80182005-01-15 22:15:51 +00003660 istk->expansion = ll;
3661 }
3662 } else {
3663 /*
3664 * Check whether a `%rep' was started and not ended
3665 * within this macro expansion. This can happen and
3666 * should be detected. It's a fatal error because
3667 * I'm too confused to work out how to recover
3668 * sensibly from it.
3669 */
3670 if (defining) {
3671 if (defining->name)
3672 error(ERR_PANIC,
3673 "defining with name in expansion");
3674 else if (istk->mstk->name)
3675 error(ERR_FATAL,
3676 "`%%rep' without `%%endrep' within"
3677 " expansion of macro `%s'",
3678 istk->mstk->name);
3679 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00003680
H. Peter Anvine2c80182005-01-15 22:15:51 +00003681 /*
3682 * FIXME: investigate the relationship at this point between
3683 * istk->mstk and l->finishes
3684 */
3685 {
3686 MMacro *m = istk->mstk;
3687 istk->mstk = m->next_active;
3688 if (m->name) {
3689 /*
3690 * This was a real macro call, not a %rep, and
3691 * therefore the parameter information needs to
3692 * be freed.
3693 */
3694 nasm_free(m->params);
3695 free_tlist(m->iline);
3696 nasm_free(m->paramlen);
3697 l->finishes->in_progress = FALSE;
3698 } else
3699 free_mmacro(m);
3700 }
3701 istk->expansion = l->next;
3702 nasm_free(l);
3703 list->downlevel(LIST_MACRO);
3704 }
3705 }
3706 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003707
H. Peter Anvine2c80182005-01-15 22:15:51 +00003708 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003709 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003710 Line *l = istk->expansion;
3711 if (istk->mstk)
3712 istk->mstk->lineno++;
3713 tline = l->first;
3714 istk->expansion = l->next;
3715 nasm_free(l);
3716 p = detoken(tline, FALSE);
3717 list->line(LIST_MACRO, p);
3718 nasm_free(p);
3719 break;
3720 }
3721 line = read_line();
3722 if (line) { /* from the current input file */
3723 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00003724 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003725 nasm_free(line);
3726 break;
3727 }
3728 /*
3729 * The current file has ended; work down the istk
3730 */
3731 {
3732 Include *i = istk;
3733 fclose(i->fp);
3734 if (i->conds)
3735 error(ERR_FATAL,
3736 "expected `%%endif' before end of file");
3737 /* only set line and file name if there's a next node */
3738 if (i->next) {
3739 src_set_linnum(i->lineno);
3740 nasm_free(src_set_fname(i->fname));
3741 }
3742 istk = i->next;
3743 list->downlevel(LIST_INCLUDE);
3744 nasm_free(i);
3745 if (!istk)
3746 return NULL;
3747 }
3748 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003749
H. Peter Anvine2c80182005-01-15 22:15:51 +00003750 /*
3751 * We must expand MMacro parameters and MMacro-local labels
3752 * _before_ we plunge into directive processing, to cope
3753 * with things like `%define something %1' such as STRUC
3754 * uses. Unless we're _defining_ a MMacro, in which case
3755 * those tokens should be left alone to go into the
3756 * definition; and unless we're in a non-emitting
3757 * condition, in which case we don't want to meddle with
3758 * anything.
3759 */
3760 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
3761 tline = expand_mmac_params(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003762
H. Peter Anvine2c80182005-01-15 22:15:51 +00003763 /*
3764 * Check the line to see if it's a preprocessor directive.
3765 */
3766 if (do_directive(tline) == DIRECTIVE_FOUND) {
3767 continue;
3768 } else if (defining) {
3769 /*
3770 * We're defining a multi-line macro. We emit nothing
3771 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00003772 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003773 */
3774 Line *l = nasm_malloc(sizeof(Line));
3775 l->next = defining->expansion;
3776 l->first = tline;
3777 l->finishes = FALSE;
3778 defining->expansion = l;
3779 continue;
3780 } else if (istk->conds && !emitting(istk->conds->state)) {
3781 /*
3782 * We're in a non-emitting branch of a condition block.
3783 * Emit nothing at all, not even a blank line: when we
3784 * emerge from the condition we'll give a line-number
3785 * directive so we keep our place correctly.
3786 */
3787 free_tlist(tline);
3788 continue;
3789 } else if (istk->mstk && !istk->mstk->in_progress) {
3790 /*
3791 * We're in a %rep block which has been terminated, so
3792 * we're walking through to the %endrep without
3793 * emitting anything. Emit nothing at all, not even a
3794 * blank line: when we emerge from the %rep block we'll
3795 * give a line-number directive so we keep our place
3796 * correctly.
3797 */
3798 free_tlist(tline);
3799 continue;
3800 } else {
3801 tline = expand_smacro(tline);
3802 if (!expand_mmacro(tline)) {
3803 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003804 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003805 */
3806 line = detoken(tline, TRUE);
3807 free_tlist(tline);
3808 break;
3809 } else {
3810 continue; /* expand_mmacro calls free_tlist */
3811 }
3812 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003813 }
3814
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003815 return line;
3816}
3817
H. Peter Anvine2c80182005-01-15 22:15:51 +00003818static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003819{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003820 int h;
3821
H. Peter Anvine2c80182005-01-15 22:15:51 +00003822 if (defining) {
3823 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
3824 defining->name);
3825 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003826 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003827 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003828 ctx_pop();
3829 for (h = 0; h < NHASH; h++) {
3830 while (mmacros[h]) {
3831 MMacro *m = mmacros[h];
3832 mmacros[h] = mmacros[h]->next;
3833 free_mmacro(m);
3834 }
3835 while (smacros[h]) {
3836 SMacro *s = smacros[h];
3837 smacros[h] = smacros[h]->next;
3838 nasm_free(s->name);
3839 free_tlist(s->expansion);
3840 nasm_free(s);
3841 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003842 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003843 while (istk) {
3844 Include *i = istk;
3845 istk = istk->next;
3846 fclose(i->fp);
3847 nasm_free(i->fname);
3848 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003849 }
3850 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003851 ctx_pop();
3852 if (pass == 0) {
3853 free_llist(predef);
3854 delete_Blocks();
3855 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003856}
3857
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003858void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003859{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003860 IncPath *i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003861/* by alexfru: order of path inclusion fixed (was reverse order) */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003862 i = nasm_malloc(sizeof(IncPath));
3863 i->path = nasm_strdup(path);
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003864 i->next = NULL;
3865
H. Peter Anvine2c80182005-01-15 22:15:51 +00003866 if (ipath != NULL) {
3867 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003868 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003869 j = j->next;
3870 j->next = i;
3871 } else {
3872 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003873 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003874}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003875
3876/*
3877 * added by alexfru:
3878 *
3879 * This function is used to "export" the include paths, e.g.
3880 * the paths specified in the '-I' command switch.
3881 * The need for such exporting is due to the 'incbin' directive,
3882 * which includes raw binary files (unlike '%include', which
3883 * includes text source files). It would be real nice to be
3884 * able to specify paths to search for incbin'ned files also.
3885 * So, this is a simple workaround.
3886 *
3887 * The function use is simple:
3888 *
3889 * The 1st call (with NULL argument) returns a pointer to the 1st path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003890 * (char** type) or NULL if none include paths available.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003891 *
3892 * All subsequent calls take as argument the value returned by this
3893 * function last. The return value is either the next path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003894 * (char** type) or NULL if the end of the paths list is reached.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003895 *
3896 * It is maybe not the best way to do things, but I didn't want
3897 * to export too much, just one or two functions and no types or
3898 * variables exported.
3899 *
3900 * Can't say I like the current situation with e.g. this path list either,
3901 * it seems to be never deallocated after creation...
3902 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003903char **pp_get_include_path_ptr(char **pPrevPath)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003904{
3905/* This macro returns offset of a member of a structure */
3906#define GetMemberOffset(StructType,MemberName)\
3907 ((size_t)&((StructType*)0)->MemberName)
3908 IncPath *i;
3909
H. Peter Anvine2c80182005-01-15 22:15:51 +00003910 if (pPrevPath == NULL) {
3911 if (ipath != NULL)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003912 return &ipath->path;
3913 else
3914 return NULL;
3915 }
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003916 i = (IncPath *) ((char *)pPrevPath - GetMemberOffset(IncPath, path));
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003917 i = i->next;
3918 if (i != NULL)
3919 return &i->path;
3920 else
3921 return NULL;
3922#undef GetMemberOffset
3923}
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003924
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003925void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003926{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003927 Token *inc, *space, *name;
3928 Line *l;
3929
H. Peter Anvin734b1882002-04-30 21:01:08 +00003930 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
3931 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
3932 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003933
3934 l = nasm_malloc(sizeof(Line));
3935 l->next = predef;
3936 l->first = inc;
3937 l->finishes = FALSE;
3938 predef = l;
3939}
3940
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003941void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003942{
3943 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003944 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003945 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003946
3947 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00003948 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
3949 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003950 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003951 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00003952 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003953 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003954 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003955
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003956 l = nasm_malloc(sizeof(Line));
3957 l->next = predef;
3958 l->first = def;
3959 l->finishes = FALSE;
3960 predef = l;
3961}
3962
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003963void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00003964{
3965 Token *def, *space;
3966 Line *l;
3967
H. Peter Anvin734b1882002-04-30 21:01:08 +00003968 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
3969 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00003970 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00003971
3972 l = nasm_malloc(sizeof(Line));
3973 l->next = predef;
3974 l->first = def;
3975 l->finishes = FALSE;
3976 predef = l;
3977}
3978
Keith Kaniosb7a89542007-04-12 02:40:54 +00003979/*
3980 * Added by Keith Kanios:
3981 *
3982 * This function is used to assist with "runtime" preprocessor
3983 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
3984 *
3985 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
3986 * PASS A VALID STRING TO THIS FUNCTION!!!!!
3987 */
3988
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003989void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00003990{
3991 Token *def;
3992
3993 def = tokenize(definition);
3994 if(do_directive(def) == NO_DIRECTIVE_FOUND)
3995 free_tlist(def);
3996
3997}
3998
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003999void pp_extra_stdmac(const char **macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004000{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004001 extrastdmac = macros;
4002}
4003
Keith Kaniosb7a89542007-04-12 02:40:54 +00004004static void make_tok_num(Token * tok, int32_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004005{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004006 char numbuf[20];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00004007 snprintf(numbuf, sizeof(numbuf), "%"PRId32"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004008 tok->text = nasm_strdup(numbuf);
4009 tok->type = TOK_NUMBER;
4010}
4011
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004012Preproc nasmpp = {
4013 pp_reset,
4014 pp_getline,
4015 pp_cleanup
4016};