blob: fa2a9530f045c7808fe2ff01252ce9c446349809 [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;
Keith Kaniosb7a89542007-04-12 02:40:54 +000093 int64_t 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 Anvine2c80182005-01-15 22:15:51 +00001481 default:
1482 error(ERR_FATAL,
1483 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001484 pp_directives[ct]);
1485 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001486 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001487
1488 free_tlist(origline);
1489 return j ^ PP_NEGATIVE(ct);
1490
1491fail:
1492 free_tlist(origline);
1493 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001494}
1495
1496/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001497 * Expand macros in a string. Used in %error and %include directives.
Keith Kaniosb7a89542007-04-12 02:40:54 +00001498 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001499 * The returned variable should ALWAYS be freed after usage.
1500 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001501void expand_macros_in_string(char **p)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001502{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001503 Token *line = tokenize(*p);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001504 line = expand_smacro(line);
1505 *p = detoken(line, FALSE);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001506}
1507
Ed Beroset3ab3f412002-06-11 03:31:49 +00001508/**
1509 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001510 * Find out if a line contains a preprocessor directive, and deal
1511 * with it if so.
1512 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001513 * If a directive _is_ found, it is the responsibility of this routine
1514 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001515 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001516 * @param tline a pointer to the current tokeninzed line linked list
1517 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001518 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001519 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001520static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001521{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001522 enum preproc_token i;
1523 int j;
1524 int nparam, nolist;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001525 int64_t k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001526 int offset;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001527 char *p, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001528 Include *inc;
1529 Context *ctx;
1530 Cond *cond;
1531 SMacro *smac, **smhead;
1532 MMacro *mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001533 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1534 Line *l;
1535 struct tokenval tokval;
1536 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001537 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001538
1539 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001540
H. Peter Anvineba20a72002-04-30 20:53:55 +00001541 skip_white_(tline);
1542 if (!tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001543 (tline->text[1] == '%' || tline->text[1] == '$'
1544 || tline->text[1] == '!'))
1545 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001546
H. Peter Anvin4169a472007-09-12 01:29:43 +00001547 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001548
1549 /*
1550 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001551 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001552 * we should ignore all directives except for condition
1553 * directives.
1554 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001555 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001556 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1557 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001558 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001559
1560 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001561 * If we're defining a macro or reading a %rep block, we should
1562 * ignore all directives except for %macro/%imacro (which
1563 * generate an error), %endm/%endmacro, and (only if we're in a
H. Peter Anvineba20a72002-04-30 20:53:55 +00001564 * %rep block) %endrep. If we're in a %rep block, another %rep
1565 * causes an error, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001566 */
1567 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001568 i != PP_ENDMACRO && i != PP_ENDM &&
1569 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1570 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001571 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001572
H. Peter Anvin4169a472007-09-12 01:29:43 +00001573 switch (i) {
1574 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001575 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1576 tline->text);
1577 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001578
H. Peter Anvine2c80182005-01-15 22:15:51 +00001579 case PP_STACKSIZE:
1580 /* Directive to tell NASM what the default stack size is. The
1581 * default is for a 16-bit stack, and this can be overriden with
1582 * %stacksize large.
1583 * the following form:
1584 *
1585 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1586 */
1587 tline = tline->next;
1588 if (tline && tline->type == TOK_WHITESPACE)
1589 tline = tline->next;
1590 if (!tline || tline->type != TOK_ID) {
1591 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1592 free_tlist(origline);
1593 return DIRECTIVE_FOUND;
1594 }
1595 if (nasm_stricmp(tline->text, "flat") == 0) {
1596 /* All subsequent ARG directives are for a 32-bit stack */
1597 StackSize = 4;
1598 StackPointer = "ebp";
1599 ArgOffset = 8;
1600 LocalOffset = 4;
1601 } else if (nasm_stricmp(tline->text, "large") == 0) {
1602 /* All subsequent ARG directives are for a 16-bit stack,
1603 * far function call.
1604 */
1605 StackSize = 2;
1606 StackPointer = "bp";
1607 ArgOffset = 4;
1608 LocalOffset = 2;
1609 } else if (nasm_stricmp(tline->text, "small") == 0) {
1610 /* All subsequent ARG directives are for a 16-bit stack,
1611 * far function call. We don't support near functions.
1612 */
1613 StackSize = 2;
1614 StackPointer = "bp";
1615 ArgOffset = 6;
1616 LocalOffset = 2;
1617 } else {
1618 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1619 free_tlist(origline);
1620 return DIRECTIVE_FOUND;
1621 }
1622 free_tlist(origline);
1623 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001624
H. Peter Anvine2c80182005-01-15 22:15:51 +00001625 case PP_ARG:
1626 /* TASM like ARG directive to define arguments to functions, in
1627 * the following form:
1628 *
1629 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1630 */
1631 offset = ArgOffset;
1632 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001633 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001634 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001635
H. Peter Anvine2c80182005-01-15 22:15:51 +00001636 /* Find the argument name */
1637 tline = tline->next;
1638 if (tline && tline->type == TOK_WHITESPACE)
1639 tline = tline->next;
1640 if (!tline || tline->type != TOK_ID) {
1641 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1642 free_tlist(origline);
1643 return DIRECTIVE_FOUND;
1644 }
1645 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001646
H. Peter Anvine2c80182005-01-15 22:15:51 +00001647 /* Find the argument size type */
1648 tline = tline->next;
1649 if (!tline || tline->type != TOK_OTHER
1650 || tline->text[0] != ':') {
1651 error(ERR_NONFATAL,
1652 "Syntax error processing `%%arg' directive");
1653 free_tlist(origline);
1654 return DIRECTIVE_FOUND;
1655 }
1656 tline = tline->next;
1657 if (!tline || tline->type != TOK_ID) {
1658 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1659 free_tlist(origline);
1660 return DIRECTIVE_FOUND;
1661 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001662
H. Peter Anvine2c80182005-01-15 22:15:51 +00001663 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001664 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001665 tt = expand_smacro(tt);
1666 if (nasm_stricmp(tt->text, "byte") == 0) {
1667 size = MAX(StackSize, 1);
1668 } else if (nasm_stricmp(tt->text, "word") == 0) {
1669 size = MAX(StackSize, 2);
1670 } else if (nasm_stricmp(tt->text, "dword") == 0) {
1671 size = MAX(StackSize, 4);
1672 } else if (nasm_stricmp(tt->text, "qword") == 0) {
1673 size = MAX(StackSize, 8);
1674 } else if (nasm_stricmp(tt->text, "tword") == 0) {
1675 size = MAX(StackSize, 10);
1676 } else {
1677 error(ERR_NONFATAL,
1678 "Invalid size type for `%%arg' missing directive");
1679 free_tlist(tt);
1680 free_tlist(origline);
1681 return DIRECTIVE_FOUND;
1682 }
1683 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001684
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 /* Now define the macro for the argument */
1686 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1687 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001688 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001689 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001690
H. Peter Anvine2c80182005-01-15 22:15:51 +00001691 /* Move to the next argument in the list */
1692 tline = tline->next;
1693 if (tline && tline->type == TOK_WHITESPACE)
1694 tline = tline->next;
1695 }
1696 while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1697 free_tlist(origline);
1698 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001699
H. Peter Anvine2c80182005-01-15 22:15:51 +00001700 case PP_LOCAL:
1701 /* TASM like LOCAL directive to define local variables for a
1702 * function, in the following form:
1703 *
1704 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1705 *
1706 * The '= LocalSize' at the end is ignored by NASM, but is
1707 * required by TASM to define the local parameter size (and used
1708 * by the TASM macro package).
1709 */
1710 offset = LocalOffset;
1711 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001712 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001713 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001714
H. Peter Anvine2c80182005-01-15 22:15:51 +00001715 /* Find the argument name */
1716 tline = tline->next;
1717 if (tline && tline->type == TOK_WHITESPACE)
1718 tline = tline->next;
1719 if (!tline || tline->type != TOK_ID) {
1720 error(ERR_NONFATAL,
1721 "`%%local' missing argument parameter");
1722 free_tlist(origline);
1723 return DIRECTIVE_FOUND;
1724 }
1725 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001726
H. Peter Anvine2c80182005-01-15 22:15:51 +00001727 /* Find the argument size type */
1728 tline = tline->next;
1729 if (!tline || tline->type != TOK_OTHER
1730 || tline->text[0] != ':') {
1731 error(ERR_NONFATAL,
1732 "Syntax error processing `%%local' directive");
1733 free_tlist(origline);
1734 return DIRECTIVE_FOUND;
1735 }
1736 tline = tline->next;
1737 if (!tline || tline->type != TOK_ID) {
1738 error(ERR_NONFATAL,
1739 "`%%local' missing size type parameter");
1740 free_tlist(origline);
1741 return DIRECTIVE_FOUND;
1742 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001743
H. Peter Anvine2c80182005-01-15 22:15:51 +00001744 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001745 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001746 tt = expand_smacro(tt);
1747 if (nasm_stricmp(tt->text, "byte") == 0) {
1748 size = MAX(StackSize, 1);
1749 } else if (nasm_stricmp(tt->text, "word") == 0) {
1750 size = MAX(StackSize, 2);
1751 } else if (nasm_stricmp(tt->text, "dword") == 0) {
1752 size = MAX(StackSize, 4);
1753 } else if (nasm_stricmp(tt->text, "qword") == 0) {
1754 size = MAX(StackSize, 8);
1755 } else if (nasm_stricmp(tt->text, "tword") == 0) {
1756 size = MAX(StackSize, 10);
1757 } else {
1758 error(ERR_NONFATAL,
1759 "Invalid size type for `%%local' missing directive");
1760 free_tlist(tt);
1761 free_tlist(origline);
1762 return DIRECTIVE_FOUND;
1763 }
1764 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001765
H. Peter Anvine2c80182005-01-15 22:15:51 +00001766 /* Now define the macro for the argument */
1767 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
1768 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001769 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001770 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001771
H. Peter Anvine2c80182005-01-15 22:15:51 +00001772 /* Now define the assign to setup the enter_c macro correctly */
1773 snprintf(directive, sizeof(directive),
1774 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001775 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001776
H. Peter Anvine2c80182005-01-15 22:15:51 +00001777 /* Move to the next argument in the list */
1778 tline = tline->next;
1779 if (tline && tline->type == TOK_WHITESPACE)
1780 tline = tline->next;
1781 }
1782 while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1783 free_tlist(origline);
1784 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001785
H. Peter Anvine2c80182005-01-15 22:15:51 +00001786 case PP_CLEAR:
1787 if (tline->next)
1788 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
1789 for (j = 0; j < NHASH; j++) {
1790 while (mmacros[j]) {
1791 MMacro *m = mmacros[j];
1792 mmacros[j] = m->next;
1793 free_mmacro(m);
1794 }
1795 while (smacros[j]) {
1796 SMacro *s = smacros[j];
1797 smacros[j] = smacros[j]->next;
1798 nasm_free(s->name);
1799 free_tlist(s->expansion);
1800 nasm_free(s);
1801 }
1802 }
1803 free_tlist(origline);
1804 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001805
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 case PP_INCLUDE:
1807 tline = tline->next;
1808 skip_white_(tline);
1809 if (!tline || (tline->type != TOK_STRING &&
1810 tline->type != TOK_INTERNAL_STRING)) {
1811 error(ERR_NONFATAL, "`%%include' expects a file name");
1812 free_tlist(origline);
1813 return DIRECTIVE_FOUND; /* but we did _something_ */
1814 }
1815 if (tline->next)
1816 error(ERR_WARNING,
1817 "trailing garbage after `%%include' ignored");
1818 if (tline->type != TOK_INTERNAL_STRING) {
1819 p = tline->text + 1; /* point past the quote to the name */
1820 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
1821 } else
1822 p = tline->text; /* internal_string is easier */
1823 expand_macros_in_string(&p);
1824 inc = nasm_malloc(sizeof(Include));
1825 inc->next = istk;
1826 inc->conds = NULL;
1827 inc->fp = inc_fopen(p);
1828 inc->fname = src_set_fname(p);
1829 inc->lineno = src_set_linnum(0);
1830 inc->lineinc = 1;
1831 inc->expansion = NULL;
1832 inc->mstk = NULL;
1833 istk = inc;
1834 list->uplevel(LIST_INCLUDE);
1835 free_tlist(origline);
1836 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001837
H. Peter Anvine2c80182005-01-15 22:15:51 +00001838 case PP_PUSH:
1839 tline = tline->next;
1840 skip_white_(tline);
1841 tline = expand_id(tline);
1842 if (!tok_type_(tline, TOK_ID)) {
1843 error(ERR_NONFATAL, "`%%push' expects a context identifier");
1844 free_tlist(origline);
1845 return DIRECTIVE_FOUND; /* but we did _something_ */
1846 }
1847 if (tline->next)
1848 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
1849 ctx = nasm_malloc(sizeof(Context));
1850 ctx->next = cstk;
1851 ctx->localmac = NULL;
1852 ctx->name = nasm_strdup(tline->text);
1853 ctx->number = unique++;
1854 cstk = ctx;
1855 free_tlist(origline);
1856 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001857
H. Peter Anvine2c80182005-01-15 22:15:51 +00001858 case PP_REPL:
1859 tline = tline->next;
1860 skip_white_(tline);
1861 tline = expand_id(tline);
1862 if (!tok_type_(tline, TOK_ID)) {
1863 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
1864 free_tlist(origline);
1865 return DIRECTIVE_FOUND; /* but we did _something_ */
1866 }
1867 if (tline->next)
1868 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
1869 if (!cstk)
1870 error(ERR_NONFATAL, "`%%repl': context stack is empty");
1871 else {
1872 nasm_free(cstk->name);
1873 cstk->name = nasm_strdup(tline->text);
1874 }
1875 free_tlist(origline);
1876 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001877
H. Peter Anvine2c80182005-01-15 22:15:51 +00001878 case PP_POP:
1879 if (tline->next)
1880 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
1881 if (!cstk)
1882 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
1883 else
1884 ctx_pop();
1885 free_tlist(origline);
1886 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001887
H. Peter Anvine2c80182005-01-15 22:15:51 +00001888 case PP_ERROR:
1889 tline->next = expand_smacro(tline->next);
1890 tline = tline->next;
1891 skip_white_(tline);
1892 if (tok_type_(tline, TOK_STRING)) {
1893 p = tline->text + 1; /* point past the quote to the name */
1894 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
1895 expand_macros_in_string(&p);
1896 error(ERR_NONFATAL, "%s", p);
1897 nasm_free(p);
1898 } else {
1899 p = detoken(tline, FALSE);
1900 error(ERR_WARNING, "%s", p);
1901 nasm_free(p);
1902 }
1903 free_tlist(origline);
1904 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001905
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001906 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001907 if (istk->conds && !emitting(istk->conds->state))
1908 j = COND_NEVER;
1909 else {
1910 j = if_condition(tline->next, i);
1911 tline->next = NULL; /* it got freed */
1912 free_tlist(origline);
1913 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
1914 }
1915 cond = nasm_malloc(sizeof(Cond));
1916 cond->next = istk->conds;
1917 cond->state = j;
1918 istk->conds = cond;
1919 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001920
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001921 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001922 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00001923 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001924 if (emitting(istk->conds->state)
1925 || istk->conds->state == COND_NEVER)
1926 istk->conds->state = COND_NEVER;
1927 else {
1928 /*
1929 * IMPORTANT: In the case of %if, we will already have
1930 * called expand_mmac_params(); however, if we're
1931 * processing an %elif we must have been in a
1932 * non-emitting mode, which would have inhibited
1933 * the normal invocation of expand_mmac_params(). Therefore,
1934 * we have to do it explicitly here.
1935 */
1936 j = if_condition(expand_mmac_params(tline->next), i);
1937 tline->next = NULL; /* it got freed */
1938 free_tlist(origline);
1939 istk->conds->state =
1940 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
1941 }
1942 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001943
H. Peter Anvine2c80182005-01-15 22:15:51 +00001944 case PP_ELSE:
1945 if (tline->next)
1946 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
1947 if (!istk->conds)
1948 error(ERR_FATAL, "`%%else': no matching `%%if'");
1949 if (emitting(istk->conds->state)
1950 || istk->conds->state == COND_NEVER)
1951 istk->conds->state = COND_ELSE_FALSE;
1952 else
1953 istk->conds->state = COND_ELSE_TRUE;
1954 free_tlist(origline);
1955 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001956
H. Peter Anvine2c80182005-01-15 22:15:51 +00001957 case PP_ENDIF:
1958 if (tline->next)
1959 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
1960 if (!istk->conds)
1961 error(ERR_FATAL, "`%%endif': no matching `%%if'");
1962 cond = istk->conds;
1963 istk->conds = cond->next;
1964 nasm_free(cond);
1965 free_tlist(origline);
1966 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001967
H. Peter Anvine2c80182005-01-15 22:15:51 +00001968 case PP_MACRO:
1969 case PP_IMACRO:
1970 if (defining)
1971 error(ERR_FATAL,
1972 "`%%%smacro': already defining a macro",
1973 (i == PP_IMACRO ? "i" : ""));
1974 tline = tline->next;
1975 skip_white_(tline);
1976 tline = expand_id(tline);
1977 if (!tok_type_(tline, TOK_ID)) {
1978 error(ERR_NONFATAL,
1979 "`%%%smacro' expects a macro name",
1980 (i == PP_IMACRO ? "i" : ""));
1981 return DIRECTIVE_FOUND;
1982 }
1983 defining = nasm_malloc(sizeof(MMacro));
1984 defining->name = nasm_strdup(tline->text);
1985 defining->casesense = (i == PP_MACRO);
1986 defining->plus = FALSE;
1987 defining->nolist = FALSE;
1988 defining->in_progress = FALSE;
1989 defining->rep_nest = NULL;
1990 tline = expand_smacro(tline->next);
1991 skip_white_(tline);
1992 if (!tok_type_(tline, TOK_NUMBER)) {
1993 error(ERR_NONFATAL,
1994 "`%%%smacro' expects a parameter count",
1995 (i == PP_IMACRO ? "i" : ""));
1996 defining->nparam_min = defining->nparam_max = 0;
1997 } else {
1998 defining->nparam_min = defining->nparam_max =
1999 readnum(tline->text, &j);
2000 if (j)
2001 error(ERR_NONFATAL,
2002 "unable to parse parameter count `%s'", tline->text);
2003 }
2004 if (tline && tok_is_(tline->next, "-")) {
2005 tline = tline->next->next;
2006 if (tok_is_(tline, "*"))
2007 defining->nparam_max = INT_MAX;
2008 else if (!tok_type_(tline, TOK_NUMBER))
2009 error(ERR_NONFATAL,
2010 "`%%%smacro' expects a parameter count after `-'",
2011 (i == PP_IMACRO ? "i" : ""));
2012 else {
2013 defining->nparam_max = readnum(tline->text, &j);
2014 if (j)
2015 error(ERR_NONFATAL,
2016 "unable to parse parameter count `%s'",
2017 tline->text);
2018 if (defining->nparam_min > defining->nparam_max)
2019 error(ERR_NONFATAL,
2020 "minimum parameter count exceeds maximum");
2021 }
2022 }
2023 if (tline && tok_is_(tline->next, "+")) {
2024 tline = tline->next;
2025 defining->plus = TRUE;
2026 }
2027 if (tline && tok_type_(tline->next, TOK_ID) &&
2028 !nasm_stricmp(tline->next->text, ".nolist")) {
2029 tline = tline->next;
2030 defining->nolist = TRUE;
2031 }
2032 mmac = mmacros[hash(defining->name)];
2033 while (mmac) {
2034 if (!strcmp(mmac->name, defining->name) &&
2035 (mmac->nparam_min <= defining->nparam_max
2036 || defining->plus)
2037 && (defining->nparam_min <= mmac->nparam_max
2038 || mmac->plus)) {
2039 error(ERR_WARNING,
2040 "redefining multi-line macro `%s'", defining->name);
2041 break;
2042 }
2043 mmac = mmac->next;
2044 }
2045 /*
2046 * Handle default parameters.
2047 */
2048 if (tline && tline->next) {
2049 defining->dlist = tline->next;
2050 tline->next = NULL;
2051 count_mmac_params(defining->dlist, &defining->ndefs,
2052 &defining->defaults);
2053 } else {
2054 defining->dlist = NULL;
2055 defining->defaults = NULL;
2056 }
2057 defining->expansion = NULL;
2058 free_tlist(origline);
2059 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002060
H. Peter Anvine2c80182005-01-15 22:15:51 +00002061 case PP_ENDM:
2062 case PP_ENDMACRO:
2063 if (!defining) {
2064 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2065 return DIRECTIVE_FOUND;
2066 }
2067 k = hash(defining->name);
2068 defining->next = mmacros[k];
2069 mmacros[k] = defining;
2070 defining = NULL;
2071 free_tlist(origline);
2072 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002073
H. Peter Anvine2c80182005-01-15 22:15:51 +00002074 case PP_ROTATE:
2075 if (tline->next && tline->next->type == TOK_WHITESPACE)
2076 tline = tline->next;
2077 if (tline->next == NULL) {
2078 free_tlist(origline);
2079 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2080 return DIRECTIVE_FOUND;
2081 }
2082 t = expand_smacro(tline->next);
2083 tline->next = NULL;
2084 free_tlist(origline);
2085 tline = t;
2086 tptr = &t;
2087 tokval.t_type = TOKEN_INVALID;
2088 evalresult =
2089 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2090 free_tlist(tline);
2091 if (!evalresult)
2092 return DIRECTIVE_FOUND;
2093 if (tokval.t_type)
2094 error(ERR_WARNING,
2095 "trailing garbage after expression ignored");
2096 if (!is_simple(evalresult)) {
2097 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2098 return DIRECTIVE_FOUND;
2099 }
2100 mmac = istk->mstk;
2101 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2102 mmac = mmac->next_active;
2103 if (!mmac) {
2104 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2105 } else if (mmac->nparam == 0) {
2106 error(ERR_NONFATAL,
2107 "`%%rotate' invoked within macro without parameters");
2108 } else {
2109 mmac->rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002110
H. Peter Anvine2c80182005-01-15 22:15:51 +00002111 if (mmac->rotate < 0)
2112 mmac->rotate =
2113 mmac->nparam - (-mmac->rotate) % mmac->nparam;
2114 mmac->rotate %= mmac->nparam;
2115 }
2116 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002117
H. Peter Anvine2c80182005-01-15 22:15:51 +00002118 case PP_REP:
2119 nolist = FALSE;
2120 do {
2121 tline = tline->next;
2122 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002123
H. Peter Anvine2c80182005-01-15 22:15:51 +00002124 if (tok_type_(tline, TOK_ID) &&
2125 nasm_stricmp(tline->text, ".nolist") == 0) {
2126 nolist = TRUE;
2127 do {
2128 tline = tline->next;
2129 } while (tok_type_(tline, TOK_WHITESPACE));
2130 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002131
H. Peter Anvine2c80182005-01-15 22:15:51 +00002132 if (tline) {
2133 t = expand_smacro(tline);
2134 tptr = &t;
2135 tokval.t_type = TOKEN_INVALID;
2136 evalresult =
2137 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2138 if (!evalresult) {
2139 free_tlist(origline);
2140 return DIRECTIVE_FOUND;
2141 }
2142 if (tokval.t_type)
2143 error(ERR_WARNING,
2144 "trailing garbage after expression ignored");
2145 if (!is_simple(evalresult)) {
2146 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2147 return DIRECTIVE_FOUND;
2148 }
2149 i = (int)reloc_value(evalresult) + 1;
2150 } else {
2151 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2152 i = 0;
2153 }
2154 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002155
H. Peter Anvine2c80182005-01-15 22:15:51 +00002156 tmp_defining = defining;
2157 defining = nasm_malloc(sizeof(MMacro));
2158 defining->name = NULL; /* flags this macro as a %rep block */
2159 defining->casesense = 0;
2160 defining->plus = FALSE;
2161 defining->nolist = nolist;
2162 defining->in_progress = i;
2163 defining->nparam_min = defining->nparam_max = 0;
2164 defining->defaults = NULL;
2165 defining->dlist = NULL;
2166 defining->expansion = NULL;
2167 defining->next_active = istk->mstk;
2168 defining->rep_nest = tmp_defining;
2169 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002170
H. Peter Anvine2c80182005-01-15 22:15:51 +00002171 case PP_ENDREP:
2172 if (!defining || defining->name) {
2173 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2174 return DIRECTIVE_FOUND;
2175 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002176
H. Peter Anvine2c80182005-01-15 22:15:51 +00002177 /*
2178 * Now we have a "macro" defined - although it has no name
2179 * and we won't be entering it in the hash tables - we must
2180 * push a macro-end marker for it on to istk->expansion.
2181 * After that, it will take care of propagating itself (a
2182 * macro-end marker line for a macro which is really a %rep
2183 * block will cause the macro to be re-expanded, complete
2184 * with another macro-end marker to ensure the process
2185 * continues) until the whole expansion is forcibly removed
2186 * from istk->expansion by a %exitrep.
2187 */
2188 l = nasm_malloc(sizeof(Line));
2189 l->next = istk->expansion;
2190 l->finishes = defining;
2191 l->first = NULL;
2192 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002193
H. Peter Anvine2c80182005-01-15 22:15:51 +00002194 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002195
H. Peter Anvine2c80182005-01-15 22:15:51 +00002196 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2197 tmp_defining = defining;
2198 defining = defining->rep_nest;
2199 free_tlist(origline);
2200 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002201
H. Peter Anvine2c80182005-01-15 22:15:51 +00002202 case PP_EXITREP:
2203 /*
2204 * We must search along istk->expansion until we hit a
2205 * macro-end marker for a macro with no name. Then we set
2206 * its `in_progress' flag to 0.
2207 */
2208 for (l = istk->expansion; l; l = l->next)
2209 if (l->finishes && !l->finishes->name)
2210 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002211
H. Peter Anvine2c80182005-01-15 22:15:51 +00002212 if (l)
2213 l->finishes->in_progress = 0;
2214 else
2215 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2216 free_tlist(origline);
2217 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002218
H. Peter Anvine2c80182005-01-15 22:15:51 +00002219 case PP_XDEFINE:
2220 case PP_IXDEFINE:
2221 case PP_DEFINE:
2222 case PP_IDEFINE:
2223 tline = tline->next;
2224 skip_white_(tline);
2225 tline = expand_id(tline);
2226 if (!tline || (tline->type != TOK_ID &&
2227 (tline->type != TOK_PREPROC_ID ||
2228 tline->text[1] != '$'))) {
2229 error(ERR_NONFATAL,
2230 "`%%%s%sdefine' expects a macro identifier",
2231 ((i == PP_IDEFINE || i == PP_IXDEFINE) ? "i" : ""),
2232 ((i == PP_XDEFINE || i == PP_IXDEFINE) ? "x" : ""));
2233 free_tlist(origline);
2234 return DIRECTIVE_FOUND;
2235 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002236
H. Peter Anvine2c80182005-01-15 22:15:51 +00002237 ctx = get_ctx(tline->text, FALSE);
2238 if (!ctx)
2239 smhead = &smacros[hash(tline->text)];
2240 else
2241 smhead = &ctx->localmac;
2242 mname = tline->text;
2243 last = tline;
2244 param_start = tline = tline->next;
2245 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002246
H. Peter Anvine2c80182005-01-15 22:15:51 +00002247 /* Expand the macro definition now for %xdefine and %ixdefine */
2248 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2249 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002250
H. Peter Anvine2c80182005-01-15 22:15:51 +00002251 if (tok_is_(tline, "(")) {
2252 /*
2253 * This macro has parameters.
2254 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002255
H. Peter Anvine2c80182005-01-15 22:15:51 +00002256 tline = tline->next;
2257 while (1) {
2258 skip_white_(tline);
2259 if (!tline) {
2260 error(ERR_NONFATAL, "parameter identifier expected");
2261 free_tlist(origline);
2262 return DIRECTIVE_FOUND;
2263 }
2264 if (tline->type != TOK_ID) {
2265 error(ERR_NONFATAL,
2266 "`%s': parameter identifier expected",
2267 tline->text);
2268 free_tlist(origline);
2269 return DIRECTIVE_FOUND;
2270 }
2271 tline->type = TOK_SMAC_PARAM + nparam++;
2272 tline = tline->next;
2273 skip_white_(tline);
2274 if (tok_is_(tline, ",")) {
2275 tline = tline->next;
2276 continue;
2277 }
2278 if (!tok_is_(tline, ")")) {
2279 error(ERR_NONFATAL,
2280 "`)' expected to terminate macro template");
2281 free_tlist(origline);
2282 return DIRECTIVE_FOUND;
2283 }
2284 break;
2285 }
2286 last = tline;
2287 tline = tline->next;
2288 }
2289 if (tok_type_(tline, TOK_WHITESPACE))
2290 last = tline, tline = tline->next;
2291 macro_start = NULL;
2292 last->next = NULL;
2293 t = tline;
2294 while (t) {
2295 if (t->type == TOK_ID) {
2296 for (tt = param_start; tt; tt = tt->next)
2297 if (tt->type >= TOK_SMAC_PARAM &&
2298 !strcmp(tt->text, t->text))
2299 t->type = tt->type;
2300 }
2301 tt = t->next;
2302 t->next = macro_start;
2303 macro_start = t;
2304 t = tt;
2305 }
2306 /*
2307 * Good. We now have a macro name, a parameter count, and a
2308 * token list (in reverse order) for an expansion. We ought
2309 * to be OK just to create an SMacro, store it, and let
2310 * free_tlist have the rest of the line (which we have
2311 * carefully re-terminated after chopping off the expansion
2312 * from the end).
2313 */
2314 if (smacro_defined(ctx, mname, nparam, &smac, i == PP_DEFINE)) {
2315 if (!smac) {
2316 error(ERR_WARNING,
2317 "single-line macro `%s' defined both with and"
2318 " without parameters", mname);
2319 free_tlist(origline);
2320 free_tlist(macro_start);
2321 return DIRECTIVE_FOUND;
2322 } else {
2323 /*
2324 * We're redefining, so we have to take over an
2325 * existing SMacro structure. This means freeing
2326 * what was already in it.
2327 */
2328 nasm_free(smac->name);
2329 free_tlist(smac->expansion);
2330 }
2331 } else {
2332 smac = nasm_malloc(sizeof(SMacro));
2333 smac->next = *smhead;
2334 *smhead = smac;
2335 }
2336 smac->name = nasm_strdup(mname);
2337 smac->casesense = ((i == PP_DEFINE) || (i == PP_XDEFINE));
2338 smac->nparam = nparam;
2339 smac->expansion = macro_start;
2340 smac->in_progress = FALSE;
2341 free_tlist(origline);
2342 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002343
H. Peter Anvine2c80182005-01-15 22:15:51 +00002344 case PP_UNDEF:
2345 tline = tline->next;
2346 skip_white_(tline);
2347 tline = expand_id(tline);
2348 if (!tline || (tline->type != TOK_ID &&
2349 (tline->type != TOK_PREPROC_ID ||
2350 tline->text[1] != '$'))) {
2351 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2352 free_tlist(origline);
2353 return DIRECTIVE_FOUND;
2354 }
2355 if (tline->next) {
2356 error(ERR_WARNING,
2357 "trailing garbage after macro name ignored");
2358 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002359
H. Peter Anvine2c80182005-01-15 22:15:51 +00002360 /* Find the context that symbol belongs to */
2361 ctx = get_ctx(tline->text, FALSE);
2362 if (!ctx)
2363 smhead = &smacros[hash(tline->text)];
2364 else
2365 smhead = &ctx->localmac;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002366
H. Peter Anvine2c80182005-01-15 22:15:51 +00002367 mname = tline->text;
2368 last = tline;
2369 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002370
H. Peter Anvine2c80182005-01-15 22:15:51 +00002371 /*
2372 * We now have a macro name... go hunt for it.
2373 */
2374 while (smacro_defined(ctx, mname, -1, &smac, 1)) {
2375 /* Defined, so we need to find its predecessor and nuke it */
2376 SMacro **s;
2377 for (s = smhead; *s && *s != smac; s = &(*s)->next) ;
2378 if (*s) {
2379 *s = smac->next;
2380 nasm_free(smac->name);
2381 free_tlist(smac->expansion);
2382 nasm_free(smac);
2383 }
2384 }
2385 free_tlist(origline);
2386 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002387
H. Peter Anvine2c80182005-01-15 22:15:51 +00002388 case PP_STRLEN:
2389 tline = tline->next;
2390 skip_white_(tline);
2391 tline = expand_id(tline);
2392 if (!tline || (tline->type != TOK_ID &&
2393 (tline->type != TOK_PREPROC_ID ||
2394 tline->text[1] != '$'))) {
2395 error(ERR_NONFATAL,
2396 "`%%strlen' expects a macro identifier as first parameter");
2397 free_tlist(origline);
2398 return DIRECTIVE_FOUND;
2399 }
2400 ctx = get_ctx(tline->text, FALSE);
2401 if (!ctx)
2402 smhead = &smacros[hash(tline->text)];
2403 else
2404 smhead = &ctx->localmac;
2405 mname = tline->text;
2406 last = tline;
2407 tline = expand_smacro(tline->next);
2408 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002409
H. Peter Anvine2c80182005-01-15 22:15:51 +00002410 t = tline;
2411 while (tok_type_(t, TOK_WHITESPACE))
2412 t = t->next;
2413 /* t should now point to the string */
2414 if (t->type != TOK_STRING) {
2415 error(ERR_NONFATAL,
2416 "`%%strlen` requires string as second parameter");
2417 free_tlist(tline);
2418 free_tlist(origline);
2419 return DIRECTIVE_FOUND;
2420 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002421
H. Peter Anvine2c80182005-01-15 22:15:51 +00002422 macro_start = nasm_malloc(sizeof(*macro_start));
2423 macro_start->next = NULL;
2424 make_tok_num(macro_start, strlen(t->text) - 2);
2425 macro_start->mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002426
H. Peter Anvine2c80182005-01-15 22:15:51 +00002427 /*
2428 * We now have a macro name, an implicit parameter count of
2429 * zero, and a numeric token to use as an expansion. Create
2430 * and store an SMacro.
2431 */
2432 if (smacro_defined(ctx, mname, 0, &smac, i == PP_STRLEN)) {
2433 if (!smac)
2434 error(ERR_WARNING,
2435 "single-line macro `%s' defined both with and"
2436 " without parameters", mname);
2437 else {
2438 /*
2439 * We're redefining, so we have to take over an
2440 * existing SMacro structure. This means freeing
2441 * what was already in it.
2442 */
2443 nasm_free(smac->name);
2444 free_tlist(smac->expansion);
2445 }
2446 } else {
2447 smac = nasm_malloc(sizeof(SMacro));
2448 smac->next = *smhead;
2449 *smhead = smac;
2450 }
2451 smac->name = nasm_strdup(mname);
2452 smac->casesense = (i == PP_STRLEN);
2453 smac->nparam = 0;
2454 smac->expansion = macro_start;
2455 smac->in_progress = FALSE;
2456 free_tlist(tline);
2457 free_tlist(origline);
2458 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002459
H. Peter Anvine2c80182005-01-15 22:15:51 +00002460 case PP_SUBSTR:
2461 tline = tline->next;
2462 skip_white_(tline);
2463 tline = expand_id(tline);
2464 if (!tline || (tline->type != TOK_ID &&
2465 (tline->type != TOK_PREPROC_ID ||
2466 tline->text[1] != '$'))) {
2467 error(ERR_NONFATAL,
2468 "`%%substr' expects a macro identifier as first parameter");
2469 free_tlist(origline);
2470 return DIRECTIVE_FOUND;
2471 }
2472 ctx = get_ctx(tline->text, FALSE);
2473 if (!ctx)
2474 smhead = &smacros[hash(tline->text)];
2475 else
2476 smhead = &ctx->localmac;
2477 mname = tline->text;
2478 last = tline;
2479 tline = expand_smacro(tline->next);
2480 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002481
H. Peter Anvine2c80182005-01-15 22:15:51 +00002482 t = tline->next;
2483 while (tok_type_(t, TOK_WHITESPACE))
2484 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002485
H. Peter Anvine2c80182005-01-15 22:15:51 +00002486 /* t should now point to the string */
2487 if (t->type != TOK_STRING) {
2488 error(ERR_NONFATAL,
2489 "`%%substr` requires string as second parameter");
2490 free_tlist(tline);
2491 free_tlist(origline);
2492 return DIRECTIVE_FOUND;
2493 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002494
H. Peter Anvine2c80182005-01-15 22:15:51 +00002495 tt = t->next;
2496 tptr = &tt;
2497 tokval.t_type = TOKEN_INVALID;
2498 evalresult =
2499 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2500 if (!evalresult) {
2501 free_tlist(tline);
2502 free_tlist(origline);
2503 return DIRECTIVE_FOUND;
2504 }
2505 if (!is_simple(evalresult)) {
2506 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2507 free_tlist(tline);
2508 free_tlist(origline);
2509 return DIRECTIVE_FOUND;
2510 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002511
H. Peter Anvine2c80182005-01-15 22:15:51 +00002512 macro_start = nasm_malloc(sizeof(*macro_start));
2513 macro_start->next = NULL;
2514 macro_start->text = nasm_strdup("'''");
2515 if (evalresult->value > 0
2516 && evalresult->value < strlen(t->text) - 1) {
2517 macro_start->text[1] = t->text[evalresult->value];
2518 } else {
2519 macro_start->text[2] = '\0';
2520 }
2521 macro_start->type = TOK_STRING;
2522 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002523
H. Peter Anvine2c80182005-01-15 22:15:51 +00002524 /*
2525 * We now have a macro name, an implicit parameter count of
2526 * zero, and a numeric token to use as an expansion. Create
2527 * and store an SMacro.
2528 */
2529 if (smacro_defined(ctx, mname, 0, &smac, i == PP_SUBSTR)) {
2530 if (!smac)
2531 error(ERR_WARNING,
2532 "single-line macro `%s' defined both with and"
2533 " without parameters", mname);
2534 else {
2535 /*
2536 * We're redefining, so we have to take over an
2537 * existing SMacro structure. This means freeing
2538 * what was already in it.
2539 */
2540 nasm_free(smac->name);
2541 free_tlist(smac->expansion);
2542 }
2543 } else {
2544 smac = nasm_malloc(sizeof(SMacro));
2545 smac->next = *smhead;
2546 *smhead = smac;
2547 }
2548 smac->name = nasm_strdup(mname);
2549 smac->casesense = (i == PP_SUBSTR);
2550 smac->nparam = 0;
2551 smac->expansion = macro_start;
2552 smac->in_progress = FALSE;
2553 free_tlist(tline);
2554 free_tlist(origline);
2555 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002556
H. Peter Anvine2c80182005-01-15 22:15:51 +00002557 case PP_ASSIGN:
2558 case PP_IASSIGN:
2559 tline = tline->next;
2560 skip_white_(tline);
2561 tline = expand_id(tline);
2562 if (!tline || (tline->type != TOK_ID &&
2563 (tline->type != TOK_PREPROC_ID ||
2564 tline->text[1] != '$'))) {
2565 error(ERR_NONFATAL,
2566 "`%%%sassign' expects a macro identifier",
2567 (i == PP_IASSIGN ? "i" : ""));
2568 free_tlist(origline);
2569 return DIRECTIVE_FOUND;
2570 }
2571 ctx = get_ctx(tline->text, FALSE);
2572 if (!ctx)
2573 smhead = &smacros[hash(tline->text)];
2574 else
2575 smhead = &ctx->localmac;
2576 mname = tline->text;
2577 last = tline;
2578 tline = expand_smacro(tline->next);
2579 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002580
H. Peter Anvine2c80182005-01-15 22:15:51 +00002581 t = tline;
2582 tptr = &t;
2583 tokval.t_type = TOKEN_INVALID;
2584 evalresult =
2585 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2586 free_tlist(tline);
2587 if (!evalresult) {
2588 free_tlist(origline);
2589 return DIRECTIVE_FOUND;
2590 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002591
H. Peter Anvine2c80182005-01-15 22:15:51 +00002592 if (tokval.t_type)
2593 error(ERR_WARNING,
2594 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00002595
H. Peter Anvine2c80182005-01-15 22:15:51 +00002596 if (!is_simple(evalresult)) {
2597 error(ERR_NONFATAL,
2598 "non-constant value given to `%%%sassign'",
2599 (i == PP_IASSIGN ? "i" : ""));
2600 free_tlist(origline);
2601 return DIRECTIVE_FOUND;
2602 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002603
H. Peter Anvine2c80182005-01-15 22:15:51 +00002604 macro_start = nasm_malloc(sizeof(*macro_start));
2605 macro_start->next = NULL;
2606 make_tok_num(macro_start, reloc_value(evalresult));
2607 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002608
H. Peter Anvine2c80182005-01-15 22:15:51 +00002609 /*
2610 * We now have a macro name, an implicit parameter count of
2611 * zero, and a numeric token to use as an expansion. Create
2612 * and store an SMacro.
2613 */
2614 if (smacro_defined(ctx, mname, 0, &smac, i == PP_ASSIGN)) {
2615 if (!smac)
2616 error(ERR_WARNING,
2617 "single-line macro `%s' defined both with and"
2618 " without parameters", mname);
2619 else {
2620 /*
2621 * We're redefining, so we have to take over an
2622 * existing SMacro structure. This means freeing
2623 * what was already in it.
2624 */
2625 nasm_free(smac->name);
2626 free_tlist(smac->expansion);
2627 }
2628 } else {
2629 smac = nasm_malloc(sizeof(SMacro));
2630 smac->next = *smhead;
2631 *smhead = smac;
2632 }
2633 smac->name = nasm_strdup(mname);
2634 smac->casesense = (i == PP_ASSIGN);
2635 smac->nparam = 0;
2636 smac->expansion = macro_start;
2637 smac->in_progress = FALSE;
2638 free_tlist(origline);
2639 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002640
H. Peter Anvine2c80182005-01-15 22:15:51 +00002641 case PP_LINE:
2642 /*
2643 * Syntax is `%line nnn[+mmm] [filename]'
2644 */
2645 tline = tline->next;
2646 skip_white_(tline);
2647 if (!tok_type_(tline, TOK_NUMBER)) {
2648 error(ERR_NONFATAL, "`%%line' expects line number");
2649 free_tlist(origline);
2650 return DIRECTIVE_FOUND;
2651 }
2652 k = readnum(tline->text, &j);
2653 m = 1;
2654 tline = tline->next;
2655 if (tok_is_(tline, "+")) {
2656 tline = tline->next;
2657 if (!tok_type_(tline, TOK_NUMBER)) {
2658 error(ERR_NONFATAL, "`%%line' expects line increment");
2659 free_tlist(origline);
2660 return DIRECTIVE_FOUND;
2661 }
2662 m = readnum(tline->text, &j);
2663 tline = tline->next;
2664 }
2665 skip_white_(tline);
2666 src_set_linnum(k);
2667 istk->lineinc = m;
2668 if (tline) {
2669 nasm_free(src_set_fname(detoken(tline, FALSE)));
2670 }
2671 free_tlist(origline);
2672 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002673
H. Peter Anvine2c80182005-01-15 22:15:51 +00002674 default:
2675 error(ERR_FATAL,
2676 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00002677 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002678 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002679 }
Ed Beroset3ab3f412002-06-11 03:31:49 +00002680 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002681}
2682
2683/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002684 * Ensure that a macro parameter contains a condition code and
2685 * nothing else. Return the condition code index if so, or -1
2686 * otherwise.
2687 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002688static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002689{
H. Peter Anvin76690a12002-04-30 20:52:49 +00002690 Token *tt;
2691 int i, j, k, m;
2692
H. Peter Anvineba20a72002-04-30 20:53:55 +00002693 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002694 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002695 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002696 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002697 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002698 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002699 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002700
2701 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00002702 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002703 while (j - i > 1) {
2704 k = (j + i) / 2;
2705 m = nasm_stricmp(t->text, conditions[k]);
2706 if (m == 0) {
2707 i = k;
2708 j = -2;
2709 break;
2710 } else if (m < 0) {
2711 j = k;
2712 } else
2713 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002714 }
2715 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002716 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002717 return i;
2718}
2719
2720/*
2721 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2722 * %-n) and MMacro-local identifiers (%%foo).
2723 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002724static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002725{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002726 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002727
2728 tail = &thead;
2729 thead = NULL;
2730
H. Peter Anvine2c80182005-01-15 22:15:51 +00002731 while (tline) {
2732 if (tline->type == TOK_PREPROC_ID &&
2733 (((tline->text[1] == '+' || tline->text[1] == '-')
2734 && tline->text[2]) || tline->text[1] == '%'
2735 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002736 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002737 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002738 char tmpbuf[30];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002739 int n, i;
2740 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002741
H. Peter Anvine2c80182005-01-15 22:15:51 +00002742 t = tline;
2743 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002744
H. Peter Anvine2c80182005-01-15 22:15:51 +00002745 mac = istk->mstk;
2746 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2747 mac = mac->next_active;
2748 if (!mac)
2749 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2750 else
2751 switch (t->text[1]) {
2752 /*
2753 * We have to make a substitution of one of the
2754 * forms %1, %-1, %+1, %%foo, %0.
2755 */
2756 case '0':
2757 type = TOK_NUMBER;
2758 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
2759 text = nasm_strdup(tmpbuf);
2760 break;
2761 case '%':
2762 type = TOK_ID;
Keith Kanios93f2e9a2007-04-14 00:10:59 +00002763 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu32".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002764 mac->unique);
2765 text = nasm_strcat(tmpbuf, t->text + 2);
2766 break;
2767 case '-':
2768 n = atoi(t->text + 2) - 1;
2769 if (n >= mac->nparam)
2770 tt = NULL;
2771 else {
2772 if (mac->nparam > 1)
2773 n = (n + mac->rotate) % mac->nparam;
2774 tt = mac->params[n];
2775 }
2776 cc = find_cc(tt);
2777 if (cc == -1) {
2778 error(ERR_NONFATAL,
2779 "macro parameter %d is not a condition code",
2780 n + 1);
2781 text = NULL;
2782 } else {
2783 type = TOK_ID;
2784 if (inverse_ccs[cc] == -1) {
2785 error(ERR_NONFATAL,
2786 "condition code `%s' is not invertible",
2787 conditions[cc]);
2788 text = NULL;
2789 } else
2790 text =
2791 nasm_strdup(conditions[inverse_ccs[cc]]);
2792 }
2793 break;
2794 case '+':
2795 n = atoi(t->text + 2) - 1;
2796 if (n >= mac->nparam)
2797 tt = NULL;
2798 else {
2799 if (mac->nparam > 1)
2800 n = (n + mac->rotate) % mac->nparam;
2801 tt = mac->params[n];
2802 }
2803 cc = find_cc(tt);
2804 if (cc == -1) {
2805 error(ERR_NONFATAL,
2806 "macro parameter %d is not a condition code",
2807 n + 1);
2808 text = NULL;
2809 } else {
2810 type = TOK_ID;
2811 text = nasm_strdup(conditions[cc]);
2812 }
2813 break;
2814 default:
2815 n = atoi(t->text + 1) - 1;
2816 if (n >= mac->nparam)
2817 tt = NULL;
2818 else {
2819 if (mac->nparam > 1)
2820 n = (n + mac->rotate) % mac->nparam;
2821 tt = mac->params[n];
2822 }
2823 if (tt) {
2824 for (i = 0; i < mac->paramlen[n]; i++) {
2825 *tail = new_Token(NULL, tt->type, tt->text, 0);
2826 tail = &(*tail)->next;
2827 tt = tt->next;
2828 }
2829 }
2830 text = NULL; /* we've done it here */
2831 break;
2832 }
2833 if (!text) {
2834 delete_Token(t);
2835 } else {
2836 *tail = t;
2837 tail = &t->next;
2838 t->type = type;
2839 nasm_free(t->text);
2840 t->text = text;
2841 t->mac = NULL;
2842 }
2843 continue;
2844 } else {
2845 t = *tail = tline;
2846 tline = tline->next;
2847 t->mac = NULL;
2848 tail = &t->next;
2849 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002850 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00002851 *tail = NULL;
2852 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002853 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002854 switch (t->type) {
2855 case TOK_WHITESPACE:
2856 if (tt->type == TOK_WHITESPACE) {
2857 t->next = delete_Token(tt);
2858 }
2859 break;
2860 case TOK_ID:
2861 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002862 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002863 nasm_free(t->text);
2864 t->text = tmp;
2865 t->next = delete_Token(tt);
2866 }
2867 break;
2868 case TOK_NUMBER:
2869 if (tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002870 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002871 nasm_free(t->text);
2872 t->text = tmp;
2873 t->next = delete_Token(tt);
2874 }
2875 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002876 default:
2877 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002878 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002879
H. Peter Anvin76690a12002-04-30 20:52:49 +00002880 return thead;
2881}
2882
2883/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002884 * Expand all single-line macro calls made in the given line.
2885 * Return the expanded version of the line. The original is deemed
2886 * to be destroyed in the process. (In reality we'll just move
2887 * Tokens from input to output a lot of the time, rather than
2888 * actually bothering to destroy and replicate.)
2889 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002890static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002891{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002892 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002893 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002894 Token **params;
2895 int *paramsize;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002896 int nparam, sparam, brackets, rescan;
2897 Token *org_tline = tline;
2898 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002899 char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002900
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002901 /*
2902 * Trick: we should avoid changing the start token pointer since it can
2903 * be contained in "next" field of other token. Because of this
2904 * we allocate a copy of first token and work with it; at the end of
2905 * routine we copy it back
2906 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002907 if (org_tline) {
2908 tline =
2909 new_Token(org_tline->next, org_tline->type, org_tline->text,
2910 0);
2911 tline->mac = org_tline->mac;
2912 nasm_free(org_tline->text);
2913 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002914 }
2915
H. Peter Anvin734b1882002-04-30 21:01:08 +00002916 again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002917 tail = &thead;
2918 thead = NULL;
2919
H. Peter Anvine2c80182005-01-15 22:15:51 +00002920 while (tline) { /* main token loop */
2921 if ((mname = tline->text)) {
2922 /* if this token is a local macro, look in local context */
2923 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
2924 ctx = get_ctx(mname, TRUE);
2925 else
2926 ctx = NULL;
2927 if (!ctx)
2928 head = smacros[hash(mname)];
2929 else
2930 head = ctx->localmac;
2931 /*
2932 * We've hit an identifier. As in is_mmacro below, we first
2933 * check whether the identifier is a single-line macro at
2934 * all, then think about checking for parameters if
2935 * necessary.
2936 */
2937 for (m = head; m; m = m->next)
2938 if (!mstrcmp(m->name, mname, m->casesense))
2939 break;
2940 if (m) {
2941 mstart = tline;
2942 params = NULL;
2943 paramsize = NULL;
2944 if (m->nparam == 0) {
2945 /*
2946 * Simple case: the macro is parameterless. Discard the
2947 * one token that the macro call took, and push the
2948 * expansion back on the to-do stack.
2949 */
2950 if (!m->expansion) {
2951 if (!strcmp("__FILE__", m->name)) {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002952 int32_t num = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002953 src_get(&num, &(tline->text));
2954 nasm_quote(&(tline->text));
2955 tline->type = TOK_STRING;
2956 continue;
2957 }
2958 if (!strcmp("__LINE__", m->name)) {
2959 nasm_free(tline->text);
2960 make_tok_num(tline, src_get_linnum());
2961 continue;
2962 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002963 if (!strcmp("__BITS__", m->name)) {
2964 nasm_free(tline->text);
2965 make_tok_num(tline, globalbits);
2966 continue;
2967 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002968 tline = delete_Token(tline);
2969 continue;
2970 }
2971 } else {
2972 /*
2973 * Complicated case: at least one macro with this name
2974 * exists and takes parameters. We must find the
2975 * parameters in the call, count them, find the SMacro
2976 * that corresponds to that form of the macro call, and
2977 * substitute for the parameters when we expand. What a
2978 * pain.
2979 */
2980 /*tline = tline->next;
2981 skip_white_(tline); */
2982 do {
2983 t = tline->next;
2984 while (tok_type_(t, TOK_SMAC_END)) {
2985 t->mac->in_progress = FALSE;
2986 t->text = NULL;
2987 t = tline->next = delete_Token(t);
2988 }
2989 tline = t;
2990 } while (tok_type_(tline, TOK_WHITESPACE));
2991 if (!tok_is_(tline, "(")) {
2992 /*
2993 * This macro wasn't called with parameters: ignore
2994 * the call. (Behaviour borrowed from gnu cpp.)
2995 */
2996 tline = mstart;
2997 m = NULL;
2998 } else {
2999 int paren = 0;
3000 int white = 0;
3001 brackets = 0;
3002 nparam = 0;
3003 sparam = PARAM_DELTA;
3004 params = nasm_malloc(sparam * sizeof(Token *));
3005 params[0] = tline->next;
3006 paramsize = nasm_malloc(sparam * sizeof(int));
3007 paramsize[0] = 0;
3008 while (TRUE) { /* parameter loop */
3009 /*
3010 * For some unusual expansions
3011 * which concatenates function call
3012 */
3013 t = tline->next;
3014 while (tok_type_(t, TOK_SMAC_END)) {
3015 t->mac->in_progress = FALSE;
3016 t->text = NULL;
3017 t = tline->next = delete_Token(t);
3018 }
3019 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003020
H. Peter Anvine2c80182005-01-15 22:15:51 +00003021 if (!tline) {
3022 error(ERR_NONFATAL,
3023 "macro call expects terminating `)'");
3024 break;
3025 }
3026 if (tline->type == TOK_WHITESPACE
3027 && brackets <= 0) {
3028 if (paramsize[nparam])
3029 white++;
3030 else
3031 params[nparam] = tline->next;
3032 continue; /* parameter loop */
3033 }
3034 if (tline->type == TOK_OTHER
3035 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003036 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003037 if (ch == ',' && !paren && brackets <= 0) {
3038 if (++nparam >= sparam) {
3039 sparam += PARAM_DELTA;
3040 params = nasm_realloc(params,
3041 sparam *
3042 sizeof(Token
3043 *));
3044 paramsize =
3045 nasm_realloc(paramsize,
3046 sparam *
3047 sizeof(int));
3048 }
3049 params[nparam] = tline->next;
3050 paramsize[nparam] = 0;
3051 white = 0;
3052 continue; /* parameter loop */
3053 }
3054 if (ch == '{' &&
3055 (brackets > 0 || (brackets == 0 &&
3056 !paramsize[nparam])))
3057 {
3058 if (!(brackets++)) {
3059 params[nparam] = tline->next;
3060 continue; /* parameter loop */
3061 }
3062 }
3063 if (ch == '}' && brackets > 0)
3064 if (--brackets == 0) {
3065 brackets = -1;
3066 continue; /* parameter loop */
3067 }
3068 if (ch == '(' && !brackets)
3069 paren++;
3070 if (ch == ')' && brackets <= 0)
3071 if (--paren < 0)
3072 break;
3073 }
3074 if (brackets < 0) {
3075 brackets = 0;
3076 error(ERR_NONFATAL, "braces do not "
3077 "enclose all of macro parameter");
3078 }
3079 paramsize[nparam] += white + 1;
3080 white = 0;
3081 } /* parameter loop */
3082 nparam++;
3083 while (m && (m->nparam != nparam ||
3084 mstrcmp(m->name, mname,
3085 m->casesense)))
3086 m = m->next;
3087 if (!m)
3088 error(ERR_WARNING | ERR_WARN_MNP,
3089 "macro `%s' exists, "
3090 "but not taking %d parameters",
3091 mstart->text, nparam);
3092 }
3093 }
3094 if (m && m->in_progress)
3095 m = NULL;
3096 if (!m) { /* in progess or didn't find '(' or wrong nparam */
3097 /*
3098 * Design question: should we handle !tline, which
3099 * indicates missing ')' here, or expand those
3100 * macros anyway, which requires the (t) test a few
3101 * lines down?
3102 */
3103 nasm_free(params);
3104 nasm_free(paramsize);
3105 tline = mstart;
3106 } else {
3107 /*
3108 * Expand the macro: we are placed on the last token of the
3109 * call, so that we can easily split the call from the
3110 * following tokens. We also start by pushing an SMAC_END
3111 * token for the cycle removal.
3112 */
3113 t = tline;
3114 if (t) {
3115 tline = t->next;
3116 t->next = NULL;
3117 }
3118 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3119 tt->mac = m;
3120 m->in_progress = TRUE;
3121 tline = tt;
3122 for (t = m->expansion; t; t = t->next) {
3123 if (t->type >= TOK_SMAC_PARAM) {
3124 Token *pcopy = tline, **ptail = &pcopy;
3125 Token *ttt, *pt;
3126 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003127
H. Peter Anvine2c80182005-01-15 22:15:51 +00003128 ttt = params[t->type - TOK_SMAC_PARAM];
3129 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3130 --i >= 0;) {
3131 pt = *ptail =
3132 new_Token(tline, ttt->type, ttt->text,
3133 0);
3134 ptail = &pt->next;
3135 ttt = ttt->next;
3136 }
3137 tline = pcopy;
3138 } else {
3139 tt = new_Token(tline, t->type, t->text, 0);
3140 tline = tt;
3141 }
3142 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003143
H. Peter Anvine2c80182005-01-15 22:15:51 +00003144 /*
3145 * Having done that, get rid of the macro call, and clean
3146 * up the parameters.
3147 */
3148 nasm_free(params);
3149 nasm_free(paramsize);
3150 free_tlist(mstart);
3151 continue; /* main token loop */
3152 }
3153 }
3154 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003155
H. Peter Anvine2c80182005-01-15 22:15:51 +00003156 if (tline->type == TOK_SMAC_END) {
3157 tline->mac->in_progress = FALSE;
3158 tline = delete_Token(tline);
3159 } else {
3160 t = *tail = tline;
3161 tline = tline->next;
3162 t->mac = NULL;
3163 t->next = NULL;
3164 tail = &t->next;
3165 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003166 }
3167
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003168 /*
3169 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003170 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003171 * TOK_IDs should be concatenated.
3172 * Also we look for %+ tokens and concatenate the tokens before and after
3173 * them (without white spaces in between).
3174 */
3175 t = thead;
3176 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003177 while (t) {
3178 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3179 t = t->next;
3180 if (!t || !t->next)
3181 break;
3182 if (t->next->type == TOK_ID ||
3183 t->next->type == TOK_PREPROC_ID ||
3184 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003185 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003186 nasm_free(t->text);
3187 t->next = delete_Token(t->next);
3188 t->text = p;
3189 rescan = 1;
3190 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3191 t->next->next->type == TOK_PREPROC_ID &&
3192 strcmp(t->next->next->text, "%+") == 0) {
3193 /* free the next whitespace, the %+ token and next whitespace */
3194 int i;
3195 for (i = 1; i <= 3; i++) {
3196 if (!t->next
3197 || (i != 2 && t->next->type != TOK_WHITESPACE))
3198 break;
3199 t->next = delete_Token(t->next);
3200 } /* endfor */
3201 } else
3202 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003203 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003204 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003205 if (rescan) {
3206 tline = thead;
3207 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003208 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003209
H. Peter Anvine2c80182005-01-15 22:15:51 +00003210 if (org_tline) {
3211 if (thead) {
3212 *org_tline = *thead;
3213 /* since we just gave text to org_line, don't free it */
3214 thead->text = NULL;
3215 delete_Token(thead);
3216 } else {
3217 /* the expression expanded to empty line;
3218 we can't return NULL for some reasons
3219 we just set the line to a single WHITESPACE token. */
3220 memset(org_tline, 0, sizeof(*org_tline));
3221 org_tline->text = NULL;
3222 org_tline->type = TOK_WHITESPACE;
3223 }
3224 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003225 }
3226
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003227 return thead;
3228}
3229
3230/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003231 * Similar to expand_smacro but used exclusively with macro identifiers
3232 * right before they are fetched in. The reason is that there can be
3233 * identifiers consisting of several subparts. We consider that if there
3234 * are more than one element forming the name, user wants a expansion,
3235 * otherwise it will be left as-is. Example:
3236 *
3237 * %define %$abc cde
3238 *
3239 * the identifier %$abc will be left as-is so that the handler for %define
3240 * will suck it and define the corresponding value. Other case:
3241 *
3242 * %define _%$abc cde
3243 *
3244 * In this case user wants name to be expanded *before* %define starts
3245 * working, so we'll expand %$abc into something (if it has a value;
3246 * otherwise it will be left as-is) then concatenate all successive
3247 * PP_IDs into one.
3248 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003249static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003250{
3251 Token *cur, *oldnext = NULL;
3252
H. Peter Anvin734b1882002-04-30 21:01:08 +00003253 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003254 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003255
3256 cur = tline;
3257 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003258 (cur->next->type == TOK_ID ||
3259 cur->next->type == TOK_PREPROC_ID
3260 || cur->next->type == TOK_NUMBER))
3261 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003262
3263 /* If identifier consists of just one token, don't expand */
3264 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003265 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003266
H. Peter Anvine2c80182005-01-15 22:15:51 +00003267 if (cur) {
3268 oldnext = cur->next; /* Detach the tail past identifier */
3269 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003270 }
3271
H. Peter Anvin734b1882002-04-30 21:01:08 +00003272 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003273
H. Peter Anvine2c80182005-01-15 22:15:51 +00003274 if (cur) {
3275 /* expand_smacro possibly changhed tline; re-scan for EOL */
3276 cur = tline;
3277 while (cur && cur->next)
3278 cur = cur->next;
3279 if (cur)
3280 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003281 }
3282
3283 return tline;
3284}
3285
3286/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003287 * Determine whether the given line constitutes a multi-line macro
3288 * call, and return the MMacro structure called if so. Doesn't have
3289 * to check for an initial label - that's taken care of in
3290 * expand_mmacro - but must check numbers of parameters. Guaranteed
3291 * to be called with tline->type == TOK_ID, so the putative macro
3292 * name is easy to find.
3293 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003294static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003295{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003296 MMacro *head, *m;
3297 Token **params;
3298 int nparam;
3299
3300 head = mmacros[hash(tline->text)];
3301
3302 /*
3303 * Efficiency: first we see if any macro exists with the given
3304 * name. If not, we can return NULL immediately. _Then_ we
3305 * count the parameters, and then we look further along the
3306 * list if necessary to find the proper MMacro.
3307 */
3308 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003309 if (!mstrcmp(m->name, tline->text, m->casesense))
3310 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003311 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003312 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003313
3314 /*
3315 * OK, we have a potential macro. Count and demarcate the
3316 * parameters.
3317 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003318 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003319
3320 /*
3321 * So we know how many parameters we've got. Find the MMacro
3322 * structure that handles this number.
3323 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003324 while (m) {
3325 if (m->nparam_min <= nparam
3326 && (m->plus || nparam <= m->nparam_max)) {
3327 /*
3328 * This one is right. Just check if cycle removal
3329 * prohibits us using it before we actually celebrate...
3330 */
3331 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003332#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003333 error(ERR_NONFATAL,
3334 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003335#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003336 nasm_free(params);
3337 return NULL;
3338 }
3339 /*
3340 * It's right, and we can use it. Add its default
3341 * parameters to the end of our list if necessary.
3342 */
3343 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3344 params =
3345 nasm_realloc(params,
3346 ((m->nparam_min + m->ndefs +
3347 1) * sizeof(*params)));
3348 while (nparam < m->nparam_min + m->ndefs) {
3349 params[nparam] = m->defaults[nparam - m->nparam_min];
3350 nparam++;
3351 }
3352 }
3353 /*
3354 * If we've gone over the maximum parameter count (and
3355 * we're in Plus mode), ignore parameters beyond
3356 * nparam_max.
3357 */
3358 if (m->plus && nparam > m->nparam_max)
3359 nparam = m->nparam_max;
3360 /*
3361 * Then terminate the parameter list, and leave.
3362 */
3363 if (!params) { /* need this special case */
3364 params = nasm_malloc(sizeof(*params));
3365 nparam = 0;
3366 }
3367 params[nparam] = NULL;
3368 *params_array = params;
3369 return m;
3370 }
3371 /*
3372 * This one wasn't right: look for the next one with the
3373 * same name.
3374 */
3375 for (m = m->next; m; m = m->next)
3376 if (!mstrcmp(m->name, tline->text, m->casesense))
3377 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003378 }
3379
3380 /*
3381 * After all that, we didn't find one with the right number of
3382 * parameters. Issue a warning, and fail to expand the macro.
3383 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003384 error(ERR_WARNING | ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003385 "macro `%s' exists, but not taking %d parameters",
3386 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003387 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003388 return NULL;
3389}
3390
3391/*
3392 * Expand the multi-line macro call made by the given line, if
3393 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003394 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003395 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003396static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003397{
3398 Token *startline = tline;
3399 Token *label = NULL;
3400 int dont_prepend = 0;
3401 Token **params, *t, *tt;
3402 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003403 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003404 int i, nparam, *paramlen;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003405
3406 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003407 skip_white_(t);
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003408/* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3409 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003410 return 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003411 m = is_mmacro(t, &params);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003412 if (!m) {
3413 Token *last;
3414 /*
3415 * We have an id which isn't a macro call. We'll assume
3416 * it might be a label; we'll also check to see if a
3417 * colon follows it. Then, if there's another id after
3418 * that lot, we'll check it again for macro-hood.
3419 */
3420 label = last = t;
3421 t = t->next;
3422 if (tok_type_(t, TOK_WHITESPACE))
3423 last = t, t = t->next;
3424 if (tok_is_(t, ":")) {
3425 dont_prepend = 1;
3426 last = t, t = t->next;
3427 if (tok_type_(t, TOK_WHITESPACE))
3428 last = t, t = t->next;
3429 }
3430 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3431 return 0;
3432 last->next = NULL;
3433 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003434 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003435
3436 /*
3437 * Fix up the parameters: this involves stripping leading and
3438 * trailing whitespace, then stripping braces if they are
3439 * present.
3440 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003441 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003442 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003443
H. Peter Anvine2c80182005-01-15 22:15:51 +00003444 for (i = 0; params[i]; i++) {
3445 int brace = FALSE;
3446 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003447
H. Peter Anvine2c80182005-01-15 22:15:51 +00003448 t = params[i];
3449 skip_white_(t);
3450 if (tok_is_(t, "{"))
3451 t = t->next, brace = TRUE, comma = FALSE;
3452 params[i] = t;
3453 paramlen[i] = 0;
3454 while (t) {
3455 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3456 break; /* ... because we have hit a comma */
3457 if (comma && t->type == TOK_WHITESPACE
3458 && tok_is_(t->next, ","))
3459 break; /* ... or a space then a comma */
3460 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3461 break; /* ... or a brace */
3462 t = t->next;
3463 paramlen[i]++;
3464 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003465 }
3466
3467 /*
3468 * OK, we have a MMacro structure together with a set of
3469 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003470 * copies of each Line on to istk->expansion. Substitution of
3471 * parameter tokens and macro-local tokens doesn't get done
3472 * until the single-line macro substitution process; this is
3473 * because delaying them allows us to change the semantics
3474 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003475 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003476 * First, push an end marker on to istk->expansion, mark this
3477 * macro as in progress, and set up its invocation-specific
3478 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003479 */
3480 ll = nasm_malloc(sizeof(Line));
3481 ll->next = istk->expansion;
3482 ll->finishes = m;
3483 ll->first = NULL;
3484 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003485
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003486 m->in_progress = TRUE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003487 m->params = params;
3488 m->iline = tline;
3489 m->nparam = nparam;
3490 m->rotate = 0;
3491 m->paramlen = paramlen;
3492 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003493 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003494
3495 m->next_active = istk->mstk;
3496 istk->mstk = m;
3497
H. Peter Anvine2c80182005-01-15 22:15:51 +00003498 for (l = m->expansion; l; l = l->next) {
3499 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003500
H. Peter Anvine2c80182005-01-15 22:15:51 +00003501 ll = nasm_malloc(sizeof(Line));
3502 ll->finishes = NULL;
3503 ll->next = istk->expansion;
3504 istk->expansion = ll;
3505 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003506
H. Peter Anvine2c80182005-01-15 22:15:51 +00003507 for (t = l->first; t; t = t->next) {
3508 Token *x = t;
3509 if (t->type == TOK_PREPROC_ID &&
3510 t->text[1] == '0' && t->text[2] == '0') {
3511 dont_prepend = -1;
3512 x = label;
3513 if (!x)
3514 continue;
3515 }
3516 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3517 tail = &tt->next;
3518 }
3519 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003520 }
3521
3522 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00003523 * If we had a label, push it on as the first line of
3524 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003525 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003526 if (label) {
3527 if (dont_prepend < 0)
3528 free_tlist(startline);
3529 else {
3530 ll = nasm_malloc(sizeof(Line));
3531 ll->finishes = NULL;
3532 ll->next = istk->expansion;
3533 istk->expansion = ll;
3534 ll->first = startline;
3535 if (!dont_prepend) {
3536 while (label->next)
3537 label = label->next;
3538 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3539 }
3540 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003541 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003542
H. Peter Anvin734b1882002-04-30 21:01:08 +00003543 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003544
H. Peter Anvineba20a72002-04-30 20:53:55 +00003545 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003546}
3547
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003548/*
3549 * Since preprocessor always operate only on the line that didn't
3550 * arrived yet, we should always use ERR_OFFBY1. Also since user
3551 * won't want to see same error twice (preprocessing is done once
3552 * per pass) we will want to show errors only during pass one.
3553 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003554static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003555{
3556 va_list arg;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003557 char buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003558
3559 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003560 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003561 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003562
H. Peter Anvin734b1882002-04-30 21:01:08 +00003563 va_start(arg, fmt);
Ed Beroset19f927a2004-12-15 17:07:03 +00003564 vsnprintf(buff, sizeof(buff), fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003565 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003566
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003567 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003568 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3569 istk->mstk->lineno, buff);
3570 else
3571 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003572}
3573
H. Peter Anvin734b1882002-04-30 21:01:08 +00003574static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003575pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003576 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003577{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003578 int h;
3579
H. Peter Anvin99941bf2002-05-14 17:44:03 +00003580 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003581 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003582 istk = nasm_malloc(sizeof(Include));
3583 istk->next = NULL;
3584 istk->conds = NULL;
3585 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003586 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003587 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00003588 istk->fname = NULL;
3589 src_set_fname(nasm_strdup(file));
3590 src_set_linnum(0);
3591 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003592 if (!istk->fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003593 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3594 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003595 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003596 for (h = 0; h < NHASH; h++) {
3597 mmacros[h] = NULL;
3598 smacros[h] = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003599 }
3600 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003601 if (tasm_compatible_mode) {
3602 stdmacpos = stdmac;
3603 } else {
3604 stdmacpos = &stdmac[TASM_MACRO_COUNT];
3605 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003606 any_extrastdmac = (extrastdmac != NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003607 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003608 evaluate = eval;
3609 pass = apass;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003610}
3611
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003612static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003613{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003614 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003615 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003616
H. Peter Anvine2c80182005-01-15 22:15:51 +00003617 while (1) {
3618 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003619 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00003620 * buffer or from the input file.
3621 */
3622 tline = NULL;
3623 while (istk->expansion && istk->expansion->finishes) {
3624 Line *l = istk->expansion;
3625 if (!l->finishes->name && l->finishes->in_progress > 1) {
3626 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003627
H. Peter Anvine2c80182005-01-15 22:15:51 +00003628 /*
3629 * This is a macro-end marker for a macro with no
3630 * name, which means it's not really a macro at all
3631 * but a %rep block, and the `in_progress' field is
3632 * more than 1, meaning that we still need to
3633 * repeat. (1 means the natural last repetition; 0
3634 * means termination by %exitrep.) We have
3635 * therefore expanded up to the %endrep, and must
3636 * push the whole block on to the expansion buffer
3637 * again. We don't bother to remove the macro-end
3638 * marker: we'd only have to generate another one
3639 * if we did.
3640 */
3641 l->finishes->in_progress--;
3642 for (l = l->finishes->expansion; l; l = l->next) {
3643 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003644
H. Peter Anvine2c80182005-01-15 22:15:51 +00003645 ll = nasm_malloc(sizeof(Line));
3646 ll->next = istk->expansion;
3647 ll->finishes = NULL;
3648 ll->first = NULL;
3649 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003650
H. Peter Anvine2c80182005-01-15 22:15:51 +00003651 for (t = l->first; t; t = t->next) {
3652 if (t->text || t->type == TOK_WHITESPACE) {
3653 tt = *tail =
3654 new_Token(NULL, t->type, t->text, 0);
3655 tail = &tt->next;
3656 }
3657 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003658
H. Peter Anvine2c80182005-01-15 22:15:51 +00003659 istk->expansion = ll;
3660 }
3661 } else {
3662 /*
3663 * Check whether a `%rep' was started and not ended
3664 * within this macro expansion. This can happen and
3665 * should be detected. It's a fatal error because
3666 * I'm too confused to work out how to recover
3667 * sensibly from it.
3668 */
3669 if (defining) {
3670 if (defining->name)
3671 error(ERR_PANIC,
3672 "defining with name in expansion");
3673 else if (istk->mstk->name)
3674 error(ERR_FATAL,
3675 "`%%rep' without `%%endrep' within"
3676 " expansion of macro `%s'",
3677 istk->mstk->name);
3678 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00003679
H. Peter Anvine2c80182005-01-15 22:15:51 +00003680 /*
3681 * FIXME: investigate the relationship at this point between
3682 * istk->mstk and l->finishes
3683 */
3684 {
3685 MMacro *m = istk->mstk;
3686 istk->mstk = m->next_active;
3687 if (m->name) {
3688 /*
3689 * This was a real macro call, not a %rep, and
3690 * therefore the parameter information needs to
3691 * be freed.
3692 */
3693 nasm_free(m->params);
3694 free_tlist(m->iline);
3695 nasm_free(m->paramlen);
3696 l->finishes->in_progress = FALSE;
3697 } else
3698 free_mmacro(m);
3699 }
3700 istk->expansion = l->next;
3701 nasm_free(l);
3702 list->downlevel(LIST_MACRO);
3703 }
3704 }
3705 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003706
H. Peter Anvine2c80182005-01-15 22:15:51 +00003707 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003708 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003709 Line *l = istk->expansion;
3710 if (istk->mstk)
3711 istk->mstk->lineno++;
3712 tline = l->first;
3713 istk->expansion = l->next;
3714 nasm_free(l);
3715 p = detoken(tline, FALSE);
3716 list->line(LIST_MACRO, p);
3717 nasm_free(p);
3718 break;
3719 }
3720 line = read_line();
3721 if (line) { /* from the current input file */
3722 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00003723 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003724 nasm_free(line);
3725 break;
3726 }
3727 /*
3728 * The current file has ended; work down the istk
3729 */
3730 {
3731 Include *i = istk;
3732 fclose(i->fp);
3733 if (i->conds)
3734 error(ERR_FATAL,
3735 "expected `%%endif' before end of file");
3736 /* only set line and file name if there's a next node */
3737 if (i->next) {
3738 src_set_linnum(i->lineno);
3739 nasm_free(src_set_fname(i->fname));
3740 }
3741 istk = i->next;
3742 list->downlevel(LIST_INCLUDE);
3743 nasm_free(i);
3744 if (!istk)
3745 return NULL;
3746 }
3747 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003748
H. Peter Anvine2c80182005-01-15 22:15:51 +00003749 /*
3750 * We must expand MMacro parameters and MMacro-local labels
3751 * _before_ we plunge into directive processing, to cope
3752 * with things like `%define something %1' such as STRUC
3753 * uses. Unless we're _defining_ a MMacro, in which case
3754 * those tokens should be left alone to go into the
3755 * definition; and unless we're in a non-emitting
3756 * condition, in which case we don't want to meddle with
3757 * anything.
3758 */
3759 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
3760 tline = expand_mmac_params(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003761
H. Peter Anvine2c80182005-01-15 22:15:51 +00003762 /*
3763 * Check the line to see if it's a preprocessor directive.
3764 */
3765 if (do_directive(tline) == DIRECTIVE_FOUND) {
3766 continue;
3767 } else if (defining) {
3768 /*
3769 * We're defining a multi-line macro. We emit nothing
3770 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00003771 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003772 */
3773 Line *l = nasm_malloc(sizeof(Line));
3774 l->next = defining->expansion;
3775 l->first = tline;
3776 l->finishes = FALSE;
3777 defining->expansion = l;
3778 continue;
3779 } else if (istk->conds && !emitting(istk->conds->state)) {
3780 /*
3781 * We're in a non-emitting branch of a condition block.
3782 * Emit nothing at all, not even a blank line: when we
3783 * emerge from the condition we'll give a line-number
3784 * directive so we keep our place correctly.
3785 */
3786 free_tlist(tline);
3787 continue;
3788 } else if (istk->mstk && !istk->mstk->in_progress) {
3789 /*
3790 * We're in a %rep block which has been terminated, so
3791 * we're walking through to the %endrep without
3792 * emitting anything. Emit nothing at all, not even a
3793 * blank line: when we emerge from the %rep block we'll
3794 * give a line-number directive so we keep our place
3795 * correctly.
3796 */
3797 free_tlist(tline);
3798 continue;
3799 } else {
3800 tline = expand_smacro(tline);
3801 if (!expand_mmacro(tline)) {
3802 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003803 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003804 */
3805 line = detoken(tline, TRUE);
3806 free_tlist(tline);
3807 break;
3808 } else {
3809 continue; /* expand_mmacro calls free_tlist */
3810 }
3811 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003812 }
3813
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003814 return line;
3815}
3816
H. Peter Anvine2c80182005-01-15 22:15:51 +00003817static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003818{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003819 int h;
3820
H. Peter Anvine2c80182005-01-15 22:15:51 +00003821 if (defining) {
3822 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
3823 defining->name);
3824 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003825 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003826 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003827 ctx_pop();
3828 for (h = 0; h < NHASH; h++) {
3829 while (mmacros[h]) {
3830 MMacro *m = mmacros[h];
3831 mmacros[h] = mmacros[h]->next;
3832 free_mmacro(m);
3833 }
3834 while (smacros[h]) {
3835 SMacro *s = smacros[h];
3836 smacros[h] = smacros[h]->next;
3837 nasm_free(s->name);
3838 free_tlist(s->expansion);
3839 nasm_free(s);
3840 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003841 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003842 while (istk) {
3843 Include *i = istk;
3844 istk = istk->next;
3845 fclose(i->fp);
3846 nasm_free(i->fname);
3847 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003848 }
3849 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003850 ctx_pop();
3851 if (pass == 0) {
3852 free_llist(predef);
3853 delete_Blocks();
3854 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003855}
3856
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003857void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003858{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003859 IncPath *i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003860/* by alexfru: order of path inclusion fixed (was reverse order) */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003861 i = nasm_malloc(sizeof(IncPath));
3862 i->path = nasm_strdup(path);
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003863 i->next = NULL;
3864
H. Peter Anvine2c80182005-01-15 22:15:51 +00003865 if (ipath != NULL) {
3866 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003867 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003868 j = j->next;
3869 j->next = i;
3870 } else {
3871 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003872 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003873}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003874
3875/*
3876 * added by alexfru:
3877 *
3878 * This function is used to "export" the include paths, e.g.
3879 * the paths specified in the '-I' command switch.
3880 * The need for such exporting is due to the 'incbin' directive,
3881 * which includes raw binary files (unlike '%include', which
3882 * includes text source files). It would be real nice to be
3883 * able to specify paths to search for incbin'ned files also.
3884 * So, this is a simple workaround.
3885 *
3886 * The function use is simple:
3887 *
3888 * The 1st call (with NULL argument) returns a pointer to the 1st path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003889 * (char** type) or NULL if none include paths available.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003890 *
3891 * All subsequent calls take as argument the value returned by this
3892 * function last. The return value is either the next path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003893 * (char** type) or NULL if the end of the paths list is reached.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003894 *
3895 * It is maybe not the best way to do things, but I didn't want
3896 * to export too much, just one or two functions and no types or
3897 * variables exported.
3898 *
3899 * Can't say I like the current situation with e.g. this path list either,
3900 * it seems to be never deallocated after creation...
3901 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003902char **pp_get_include_path_ptr(char **pPrevPath)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003903{
3904/* This macro returns offset of a member of a structure */
3905#define GetMemberOffset(StructType,MemberName)\
3906 ((size_t)&((StructType*)0)->MemberName)
3907 IncPath *i;
3908
H. Peter Anvine2c80182005-01-15 22:15:51 +00003909 if (pPrevPath == NULL) {
3910 if (ipath != NULL)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003911 return &ipath->path;
3912 else
3913 return NULL;
3914 }
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003915 i = (IncPath *) ((char *)pPrevPath - GetMemberOffset(IncPath, path));
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003916 i = i->next;
3917 if (i != NULL)
3918 return &i->path;
3919 else
3920 return NULL;
3921#undef GetMemberOffset
3922}
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003923
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003924void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003925{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003926 Token *inc, *space, *name;
3927 Line *l;
3928
H. Peter Anvin734b1882002-04-30 21:01:08 +00003929 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
3930 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
3931 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003932
3933 l = nasm_malloc(sizeof(Line));
3934 l->next = predef;
3935 l->first = inc;
3936 l->finishes = FALSE;
3937 predef = l;
3938}
3939
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003940void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003941{
3942 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003943 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003944 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003945
3946 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00003947 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
3948 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003949 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003950 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00003951 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003952 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003953 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003954
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003955 l = nasm_malloc(sizeof(Line));
3956 l->next = predef;
3957 l->first = def;
3958 l->finishes = FALSE;
3959 predef = l;
3960}
3961
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003962void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00003963{
3964 Token *def, *space;
3965 Line *l;
3966
H. Peter Anvin734b1882002-04-30 21:01:08 +00003967 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
3968 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00003969 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00003970
3971 l = nasm_malloc(sizeof(Line));
3972 l->next = predef;
3973 l->first = def;
3974 l->finishes = FALSE;
3975 predef = l;
3976}
3977
Keith Kaniosb7a89542007-04-12 02:40:54 +00003978/*
3979 * Added by Keith Kanios:
3980 *
3981 * This function is used to assist with "runtime" preprocessor
3982 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
3983 *
3984 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
3985 * PASS A VALID STRING TO THIS FUNCTION!!!!!
3986 */
3987
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003988void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00003989{
3990 Token *def;
3991
3992 def = tokenize(definition);
3993 if(do_directive(def) == NO_DIRECTIVE_FOUND)
3994 free_tlist(def);
3995
3996}
3997
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003998void pp_extra_stdmac(const char **macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003999{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004000 extrastdmac = macros;
4001}
4002
Keith Kaniosb7a89542007-04-12 02:40:54 +00004003static void make_tok_num(Token * tok, int32_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004004{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004005 char numbuf[20];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00004006 snprintf(numbuf, sizeof(numbuf), "%"PRId32"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004007 tok->text = nasm_strdup(numbuf);
4008 tok->type = TOK_NUMBER;
4009}
4010
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004011Preproc nasmpp = {
4012 pp_reset,
4013 pp_getline,
4014 pp_cleanup
4015};