blob: 5d97d1eceb879681f7366071c78ec7e80aee3b28 [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 Anvinfe501952007-10-02 21:53:51 -070037#include "compiler.h"
38
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000039#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000040#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000041#include <stdlib.h>
42#include <stddef.h>
43#include <string.h>
44#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000045#include <limits.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000046#include <inttypes.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000047
48#include "nasm.h"
49#include "nasmlib.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000050#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070051#include "hashtbl.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000052
53typedef struct SMacro SMacro;
54typedef struct MMacro MMacro;
55typedef struct Context Context;
56typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000057typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000058typedef struct Line Line;
59typedef struct Include Include;
60typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000061typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000062
63/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070064 * Note on the storage of both SMacro and MMacros: the hash table
65 * indexes them case-insensitively, and we then have to go through a
66 * linked list of potential case aliases (and, for MMacros, parameter
67 * ranges); this is to preserve the matching semantics of the earlier
68 * code. If the number of case aliases for a specific macro is a
69 * performance issue, you may want to reconsider your coding style.
70 */
71
72/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000073 * Store the definition of a single-line macro.
74 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000075struct SMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000076 SMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +000077 char *name;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000078 int casesense;
H. Peter Anvin25a99342007-09-22 17:45:45 -070079 unsigned int nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000080 int in_progress;
81 Token *expansion;
82};
83
84/*
H. Peter Anvin76690a12002-04-30 20:52:49 +000085 * Store the definition of a multi-line macro. This is also used to
86 * store the interiors of `%rep...%endrep' blocks, which are
87 * effectively self-re-invoking multi-line macros which simply
88 * don't have a name or bother to appear in the hash tables. %rep
89 * blocks are signified by having a NULL `name' field.
90 *
91 * In a MMacro describing a `%rep' block, the `in_progress' field
92 * isn't merely boolean, but gives the number of repeats left to
93 * run.
94 *
95 * The `next' field is used for storing MMacros in hash tables; the
96 * `next_active' field is for stacking them on istk entries.
97 *
98 * When a MMacro is being expanded, `params', `iline', `nparam',
99 * `paramlen', `rotate' and `unique' are local to the invocation.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000100 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000101struct MMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000102 MMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000103 char *name;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000104 int casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -0700105 int nparam_min, nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000106 int plus; /* is the last parameter greedy? */
107 int nolist; /* is this macro listing-inhibited? */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000108 int in_progress;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000109 Token *dlist; /* All defaults as one list */
110 Token **defaults; /* Parameter default pointers */
111 int ndefs; /* number of default parameters */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000112 Line *expansion;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000113
114 MMacro *next_active;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000115 MMacro *rep_nest; /* used for nesting %rep */
116 Token **params; /* actual parameters */
117 Token *iline; /* invocation line */
H. Peter Anvin25a99342007-09-22 17:45:45 -0700118 unsigned int nparam, rotate;
119 int *paramlen;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000120 uint32_t unique;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000121 int lineno; /* Current line number on expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000122};
123
124/*
125 * The context stack is composed of a linked list of these.
126 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000127struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000128 Context *next;
129 SMacro *localmac;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000130 char *name;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000131 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000132};
133
134/*
135 * This is the internal form which we break input lines up into.
136 * Typically stored in linked lists.
137 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000138 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
139 * necessarily used as-is, but is intended to denote the number of
140 * the substituted parameter. So in the definition
141 *
142 * %define a(x,y) ( (x) & ~(y) )
143 *
144 * the token representing `x' will have its type changed to
145 * TOK_SMAC_PARAM, but the one representing `y' will be
146 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000147 *
148 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
149 * which doesn't need quotes around it. Used in the pre-include
150 * mechanism as an alternative to trying to find a sensible type of
151 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000152 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000153enum pp_token_type {
154 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
155 TOK_PREPROC_ID, TOK_STRING,
156 TOK_NUMBER, TOK_SMAC_END, TOK_OTHER, TOK_SMAC_PARAM,
157 TOK_INTERNAL_STRING
158};
159
H. Peter Anvine2c80182005-01-15 22:15:51 +0000160struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000161 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000162 char *text;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000163 SMacro *mac; /* associated macro for TOK_SMAC_END */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000164 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000165};
166
167/*
168 * Multi-line macro definitions are stored as a linked list of
169 * these, which is essentially a container to allow several linked
170 * lists of Tokens.
171 *
172 * Note that in this module, linked lists are treated as stacks
173 * wherever possible. For this reason, Lines are _pushed_ on to the
174 * `expansion' field in MMacro structures, so that the linked list,
175 * if walked, would give the macro lines in reverse order; this
176 * means that we can walk the list when expanding a macro, and thus
177 * push the lines on to the `expansion' field in _istk_ in reverse
178 * order (so that when popped back off they are in the right
179 * order). It may seem cockeyed, and it relies on my design having
180 * an even number of steps in, but it works...
181 *
182 * Some of these structures, rather than being actual lines, are
183 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000184 * This is for use in the cycle-tracking and %rep-handling code.
185 * Such structures have `finishes' non-NULL, and `first' NULL. All
186 * others have `finishes' NULL, but `first' may still be NULL if
187 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000188 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000189struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000190 Line *next;
191 MMacro *finishes;
192 Token *first;
193};
194
195/*
196 * To handle an arbitrary level of file inclusion, we maintain a
197 * stack (ie linked list) of these things.
198 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000199struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000200 Include *next;
201 FILE *fp;
202 Cond *conds;
203 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000204 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000205 int lineno, lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000206 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000207};
208
209/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000210 * Include search path. This is simply a list of strings which get
211 * prepended, in turn, to the name of an include file, in an
212 * attempt to find the file if it's not in the current directory.
213 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000214struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000215 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000216 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000217};
218
219/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000220 * Conditional assembly: we maintain a separate stack of these for
221 * each level of file inclusion. (The only reason we keep the
222 * stacks separate is to ensure that a stray `%endif' in a file
223 * included from within the true branch of a `%if' won't terminate
224 * it and cause confusion: instead, rightly, it'll cause an error.)
225 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000226struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000227 Cond *next;
228 int state;
229};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000230enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000231 /*
232 * These states are for use just after %if or %elif: IF_TRUE
233 * means the condition has evaluated to truth so we are
234 * currently emitting, whereas IF_FALSE means we are not
235 * currently emitting but will start doing so if a %else comes
236 * up. In these states, all directives are admissible: %elif,
237 * %else and %endif. (And of course %if.)
238 */
239 COND_IF_TRUE, COND_IF_FALSE,
240 /*
241 * These states come up after a %else: ELSE_TRUE means we're
242 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
243 * any %elif or %else will cause an error.
244 */
245 COND_ELSE_TRUE, COND_ELSE_FALSE,
246 /*
247 * This state means that we're not emitting now, and also that
248 * nothing until %endif will be emitted at all. It's for use in
249 * two circumstances: (i) when we've had our moment of emission
250 * and have now started seeing %elifs, and (ii) when the
251 * condition construct in question is contained within a
252 * non-emitting branch of a larger condition construct.
253 */
254 COND_NEVER
255};
256#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
257
Ed Beroset3ab3f412002-06-11 03:31:49 +0000258/*
259 * These defines are used as the possible return values for do_directive
260 */
261#define NO_DIRECTIVE_FOUND 0
262#define DIRECTIVE_FOUND 1
263
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000264/*
265 * Condition codes. Note that we use c_ prefix not C_ because C_ is
266 * used in nasm.h for the "real" condition codes. At _this_ level,
267 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
268 * ones, so we need a different enum...
269 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000270static const char *conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000271 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
272 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000273 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000274};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000275enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000276 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
277 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 +0000278 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 +0000279};
280static int inverse_ccs[] = {
281 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
282 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 +0000283 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000284};
285
H. Peter Anvin76690a12002-04-30 20:52:49 +0000286/*
287 * Directive names.
288 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000289/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000290static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000291{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000292 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000293}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000294
295/* For TASM compatibility we need to be able to recognise TASM compatible
296 * conditional compilation directives. Using the NASM pre-processor does
297 * not work, so we look for them specifically from the following list and
298 * then jam in the equivalent NASM directive into the input stream.
299 */
300
301#ifndef MAX
302# define MAX(a,b) ( ((a) > (b)) ? (a) : (b))
303#endif
304
H. Peter Anvine2c80182005-01-15 22:15:51 +0000305enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000306 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
307 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
308};
309
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000310static const char *tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000311 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
312 "ifndef", "include", "local"
313};
314
315static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000316static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000317static int ArgOffset = 8;
318static int LocalOffset = 4;
319
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000320static Context *cstk;
321static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000322static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000323
H. Peter Anvine2c80182005-01-15 22:15:51 +0000324static efunc _error; /* Pointer to client-provided error reporting function */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000325static evalfunc evaluate;
326
H. Peter Anvine2c80182005-01-15 22:15:51 +0000327static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000328
Keith Kaniosb7a89542007-04-12 02:40:54 +0000329static uint32_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000330
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000331static Line *predef = NULL;
332
333static ListGen *list;
334
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000335/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000336 * The current set of multi-line macros we have defined.
337 */
H. Peter Anvin97a23472007-09-16 17:57:25 -0700338static struct hash_table *mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000339
340/*
341 * The current set of single-line macros we have defined.
342 */
H. Peter Anvin97a23472007-09-16 17:57:25 -0700343static struct hash_table *smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000344
345/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000346 * The multi-line macro we are currently defining, or the %rep
347 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000348 */
349static MMacro *defining;
350
351/*
352 * The number of macro parameters to allocate space for at a time.
353 */
354#define PARAM_DELTA 16
355
356/*
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000357 * The standard macro set: defined as `static char *stdmac[]'. Also
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000358 * gives our position in the macro set, when we're processing it.
359 */
360#include "macros.c"
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000361static const char **stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000362
363/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000364 * The extra standard macros that come from the object format, if
365 * any.
366 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000367static const char **extrastdmac = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000368int any_extrastdmac;
369
370/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000371 * Tokens are allocated in blocks to improve speed
372 */
373#define TOKEN_BLOCKSIZE 4096
374static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000375struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000376 Blocks *next;
377 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000378};
379
380static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000381
382/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000383 * Forward declarations.
384 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000385static Token *expand_mmac_params(Token * tline);
386static Token *expand_smacro(Token * tline);
387static Token *expand_id(Token * tline);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000388static Context *get_ctx(char *name, int all_contexts);
Keith Kaniosb7a89542007-04-12 02:40:54 +0000389static void make_tok_num(Token * tok, int32_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000390static void error(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000391static void *new_Block(size_t size);
392static void delete_Blocks(void);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000393static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000394static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000395
396/*
397 * Macros for safe checking of token pointers, avoid *(NULL)
398 */
399#define tok_type_(x,t) ((x) && (x)->type == (t))
400#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
401#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
402#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000403
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000404/* Handle TASM specific directives, which do not contain a % in
405 * front of them. We do it here because I could not find any other
406 * place to do it for the moment, and it is a hack (ideally it would
407 * be nice to be able to use the NASM pre-processor to do it).
408 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000409static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000410{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000411 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000412 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000413
414 /* Skip whitespace */
415 while (isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000416 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000417
418 /* Binary search for the directive name */
419 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000420 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000421 len = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000422 while (!isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000423 len++;
424 if (len) {
425 oldchar = p[len];
426 p[len] = 0;
427 while (j - i > 1) {
428 k = (j + i) / 2;
429 m = nasm_stricmp(p, tasm_directives[k]);
430 if (m == 0) {
431 /* We have found a directive, so jam a % in front of it
432 * so that NASM will then recognise it as one if it's own.
433 */
434 p[len] = oldchar;
435 len = strlen(p);
436 oldline = line;
437 line = nasm_malloc(len + 2);
438 line[0] = '%';
439 if (k == TM_IFDIFI) {
440 /* NASM does not recognise IFDIFI, so we convert it to
441 * %ifdef BOGUS. This is not used in NASM comaptible
442 * code, but does need to parse for the TASM macro
443 * package.
444 */
445 strcpy(line + 1, "ifdef BOGUS");
446 } else {
447 memcpy(line + 1, p, len + 1);
448 }
449 nasm_free(oldline);
450 return line;
451 } else if (m < 0) {
452 j = k;
453 } else
454 i = k;
455 }
456 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000457 }
458 return line;
459}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000460
H. Peter Anvin76690a12002-04-30 20:52:49 +0000461/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000462 * The pre-preprocessing stage... This function translates line
463 * number indications as they emerge from GNU cpp (`# lineno "file"
464 * flags') into NASM preprocessor line number indications (`%line
465 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000466 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000467static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000468{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000469 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000470 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000471
H. Peter Anvine2c80182005-01-15 22:15:51 +0000472 if (line[0] == '#' && line[1] == ' ') {
473 oldline = line;
474 fname = oldline + 2;
475 lineno = atoi(fname);
476 fname += strspn(fname, "0123456789 ");
477 if (*fname == '"')
478 fname++;
479 fnlen = strcspn(fname, "\"");
480 line = nasm_malloc(20 + fnlen);
481 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
482 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000483 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000484 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000485 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000486 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000487}
488
489/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000490 * Free a linked list of tokens.
491 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000492static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000493{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000494 while (list) {
495 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000496 }
497}
498
499/*
500 * Free a linked list of lines.
501 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000502static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000503{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000504 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000505 while (list) {
506 l = list;
507 list = list->next;
508 free_tlist(l->first);
509 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000510 }
511}
512
513/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000514 * Free an MMacro
515 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000516static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000517{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000518 nasm_free(m->name);
519 free_tlist(m->dlist);
520 nasm_free(m->defaults);
521 free_llist(m->expansion);
522 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000523}
524
525/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700526 * Free all currently defined macros, and free the hash tables
527 */
528static void free_macros(void)
529{
530 struct hash_tbl_node *it;
531 const char *key;
532 SMacro *s;
533 MMacro *m;
534
535 it = NULL;
536 while ((s = hash_iterate(smacros, &it, &key)) != NULL) {
537 nasm_free((void *)key);
538 while (s) {
539 SMacro *ns = s->next;
540 nasm_free(s->name);
541 free_tlist(s->expansion);
542 nasm_free(s);
543 s = ns;
544 }
545 }
546 hash_free(smacros);
547
548 it = NULL;
549 while ((m = hash_iterate(mmacros, &it, &key)) != NULL) {
550 nasm_free((void *)key);
551 while (m) {
552 MMacro *nm = m->next;
553 free_mmacro(m);
554 m = nm;
555 }
556 }
557 hash_free(mmacros);
558}
559
560/*
561 * Initialize the hash tables
562 */
563static void init_macros(void)
564{
565 smacros = hash_init();
566 mmacros = hash_init();
567}
568
569/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000570 * Pop the context stack.
571 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000572static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000573{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000574 Context *c = cstk;
575 SMacro *smac, *s;
576
577 cstk = cstk->next;
578 smac = c->localmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000579 while (smac) {
580 s = smac;
581 smac = smac->next;
582 nasm_free(s->name);
583 free_tlist(s->expansion);
584 nasm_free(s);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000585 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000586 nasm_free(c->name);
587 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000588}
589
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000590#define BUF_DELTA 512
591/*
592 * Read a line from the top file in istk, handling multiple CR/LFs
593 * at the end of the line read, and handling spurious ^Zs. Will
594 * return lines from the standard macro set if this has not already
595 * been done.
596 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000597static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000598{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000599 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000600 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000601
H. Peter Anvine2c80182005-01-15 22:15:51 +0000602 if (stdmacpos) {
603 if (*stdmacpos) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000604 char *ret = nasm_strdup(*stdmacpos++);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000605 if (!*stdmacpos && any_extrastdmac) {
606 stdmacpos = extrastdmac;
607 any_extrastdmac = FALSE;
608 return ret;
609 }
610 /*
611 * Nasty hack: here we push the contents of `predef' on
612 * to the top-level expansion stack, since this is the
613 * most convenient way to implement the pre-include and
614 * pre-define features.
615 */
616 if (!*stdmacpos) {
617 Line *pd, *l;
618 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000619
H. Peter Anvine2c80182005-01-15 22:15:51 +0000620 for (pd = predef; pd; pd = pd->next) {
621 head = NULL;
622 tail = &head;
623 for (t = pd->first; t; t = t->next) {
624 *tail = new_Token(NULL, t->type, t->text, 0);
625 tail = &(*tail)->next;
626 }
627 l = nasm_malloc(sizeof(Line));
628 l->next = istk->expansion;
629 l->first = head;
630 l->finishes = FALSE;
631 istk->expansion = l;
632 }
633 }
634 return ret;
635 } else {
636 stdmacpos = NULL;
637 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000638 }
639
640 bufsize = BUF_DELTA;
641 buffer = nasm_malloc(BUF_DELTA);
642 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000643 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000644 while (1) {
645 q = fgets(p, bufsize - (p - buffer), istk->fp);
646 if (!q)
647 break;
648 p += strlen(p);
649 if (p > buffer && p[-1] == '\n') {
650 /* Convert backslash-CRLF line continuation sequences into
651 nothing at all (for DOS and Windows) */
652 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
653 p -= 3;
654 *p = 0;
655 continued_count++;
656 }
657 /* Also convert backslash-LF line continuation sequences into
658 nothing at all (for Unix) */
659 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
660 p -= 2;
661 *p = 0;
662 continued_count++;
663 } else {
664 break;
665 }
666 }
667 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000668 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000669 bufsize += BUF_DELTA;
670 buffer = nasm_realloc(buffer, bufsize);
671 p = buffer + offset; /* prevent stale-pointer problems */
672 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000673 }
674
H. Peter Anvine2c80182005-01-15 22:15:51 +0000675 if (!q && p == buffer) {
676 nasm_free(buffer);
677 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000678 }
679
H. Peter Anvine2c80182005-01-15 22:15:51 +0000680 src_set_linnum(src_get_linnum() + istk->lineinc +
681 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000682
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000683 /*
684 * Play safe: remove CRs as well as LFs, if any of either are
685 * present at the end of the line.
686 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000687 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000688 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000689
690 /*
691 * Handle spurious ^Z, which may be inserted into source files
692 * by some file transfer utilities.
693 */
694 buffer[strcspn(buffer, "\032")] = '\0';
695
H. Peter Anvin734b1882002-04-30 21:01:08 +0000696 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000697
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000698 return buffer;
699}
700
701/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000702 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000703 * don't need to parse the value out of e.g. numeric tokens: we
704 * simply split one string into many.
705 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000706static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000707{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000708 char *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000709 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000710 Token *list = NULL;
711 Token *t, **tail = &list;
712
H. Peter Anvine2c80182005-01-15 22:15:51 +0000713 while (*line) {
714 p = line;
715 if (*p == '%') {
716 p++;
717 if (isdigit(*p) ||
718 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
719 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
720 do {
721 p++;
722 }
723 while (isdigit(*p));
724 type = TOK_PREPROC_ID;
725 } else if (*p == '{') {
726 p++;
727 while (*p && *p != '}') {
728 p[-1] = *p;
729 p++;
730 }
731 p[-1] = '\0';
732 if (*p)
733 p++;
734 type = TOK_PREPROC_ID;
735 } else if (isidchar(*p) ||
736 ((*p == '!' || *p == '%' || *p == '$') &&
737 isidchar(p[1]))) {
738 do {
739 p++;
740 }
741 while (isidchar(*p));
742 type = TOK_PREPROC_ID;
743 } else {
744 type = TOK_OTHER;
745 if (*p == '%')
746 p++;
747 }
748 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
749 type = TOK_ID;
750 p++;
751 while (*p && isidchar(*p))
752 p++;
753 } else if (*p == '\'' || *p == '"') {
754 /*
755 * A string token.
756 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000757 char c = *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000758 p++;
759 type = TOK_STRING;
760 while (*p && *p != c)
761 p++;
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000762
H. Peter Anvine2c80182005-01-15 22:15:51 +0000763 if (*p) {
764 p++;
765 } else {
766 error(ERR_WARNING, "unterminated string");
767 /* Handling unterminated strings by UNV */
768 /* type = -1; */
769 }
770 } else if (isnumstart(*p)) {
771 /*
772 * A number token.
773 */
774 type = TOK_NUMBER;
775 p++;
776 while (*p && isnumchar(*p))
777 p++;
778 } else if (isspace(*p)) {
779 type = TOK_WHITESPACE;
780 p++;
781 while (*p && isspace(*p))
782 p++;
783 /*
784 * Whitespace just before end-of-line is discarded by
785 * pretending it's a comment; whitespace just before a
786 * comment gets lumped into the comment.
787 */
788 if (!*p || *p == ';') {
789 type = TOK_COMMENT;
790 while (*p)
791 p++;
792 }
793 } else if (*p == ';') {
794 type = TOK_COMMENT;
795 while (*p)
796 p++;
797 } else {
798 /*
799 * Anything else is an operator of some kind. We check
800 * for all the double-character operators (>>, <<, //,
801 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000802 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 */
804 type = TOK_OTHER;
805 if ((p[0] == '>' && p[1] == '>') ||
806 (p[0] == '<' && p[1] == '<') ||
807 (p[0] == '/' && p[1] == '/') ||
808 (p[0] == '<' && p[1] == '=') ||
809 (p[0] == '>' && p[1] == '=') ||
810 (p[0] == '=' && p[1] == '=') ||
811 (p[0] == '!' && p[1] == '=') ||
812 (p[0] == '<' && p[1] == '>') ||
813 (p[0] == '&' && p[1] == '&') ||
814 (p[0] == '|' && p[1] == '|') ||
815 (p[0] == '^' && p[1] == '^')) {
816 p++;
817 }
818 p++;
819 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000820
H. Peter Anvine2c80182005-01-15 22:15:51 +0000821 /* Handling unterminated string by UNV */
822 /*if (type == -1)
823 {
824 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
825 t->text[p-line] = *line;
826 tail = &t->next;
827 }
828 else */
829 if (type != TOK_COMMENT) {
830 *tail = t = new_Token(NULL, type, line, p - line);
831 tail = &t->next;
832 }
833 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000834 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000835 return list;
836}
837
H. Peter Anvince616072002-04-30 21:02:23 +0000838/*
839 * this function allocates a new managed block of memory and
840 * returns a pointer to the block. The managed blocks are
841 * deleted only all at once by the delete_Blocks function.
842 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000843static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +0000844{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000845 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000846
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000847 /* first, get to the end of the linked list */
848 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000849 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000850 /* now allocate the requested chunk */
851 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000852
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000853 /* now allocate a new block for the next request */
854 b->next = nasm_malloc(sizeof(Blocks));
855 /* and initialize the contents of the new block */
856 b->next->next = NULL;
857 b->next->chunk = NULL;
858 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000859}
860
861/*
862 * this function deletes all managed blocks of memory
863 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000864static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +0000865{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000866 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +0000867
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000868 /*
869 * keep in mind that the first block, pointed to by blocks
870 * is a static and not dynamically allocated, so we don't
871 * free it.
872 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000873 while (b) {
874 if (b->chunk)
875 nasm_free(b->chunk);
876 a = b;
877 b = b->next;
878 if (a != &blocks)
879 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000880 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000881}
H. Peter Anvin734b1882002-04-30 21:01:08 +0000882
883/*
884 * this function creates a new Token and passes a pointer to it
885 * back to the caller. It sets the type and text elements, and
886 * also the mac and next elements to NULL.
887 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000888static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +0000889{
890 Token *t;
891 int i;
892
H. Peter Anvine2c80182005-01-15 22:15:51 +0000893 if (freeTokens == NULL) {
894 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
895 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
896 freeTokens[i].next = &freeTokens[i + 1];
897 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000898 }
899 t = freeTokens;
900 freeTokens = t->next;
901 t->next = next;
902 t->mac = NULL;
903 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000904 if (type == TOK_WHITESPACE || text == NULL) {
905 t->text = NULL;
906 } else {
907 if (txtlen == 0)
908 txtlen = strlen(text);
909 t->text = nasm_malloc(1 + txtlen);
910 strncpy(t->text, text, txtlen);
911 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +0000912 }
913 return t;
914}
915
H. Peter Anvine2c80182005-01-15 22:15:51 +0000916static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +0000917{
918 Token *next = t->next;
919 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +0000920 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000921 freeTokens = t;
922 return next;
923}
924
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000925/*
926 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000927 * If expand_locals is not zero, identifiers of the form "%$*xxx"
928 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000929 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000930static char *detoken(Token * tlist, int expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000931{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000932 Token *t;
933 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000934 char *line, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000935
936 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000937 for (t = tlist; t; t = t->next) {
938 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000939 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 nasm_free(t->text);
941 if (p)
942 t->text = nasm_strdup(p);
943 else
944 t->text = NULL;
945 }
946 /* Expand local macros here and not during preprocessing */
947 if (expand_locals &&
948 t->type == TOK_PREPROC_ID && t->text &&
949 t->text[0] == '%' && t->text[1] == '$') {
950 Context *ctx = get_ctx(t->text, FALSE);
951 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000952 char buffer[40];
953 char *p, *q = t->text + 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000954
H. Peter Anvine2c80182005-01-15 22:15:51 +0000955 q += strspn(q, "$");
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000956 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000957 p = nasm_strcat(buffer, q);
958 nasm_free(t->text);
959 t->text = p;
960 }
961 }
962 if (t->type == TOK_WHITESPACE) {
963 len++;
964 } else if (t->text) {
965 len += strlen(t->text);
966 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000967 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000968 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000969 for (t = tlist; t; t = t->next) {
970 if (t->type == TOK_WHITESPACE) {
971 *p = ' ';
972 p++;
973 *p = '\0';
974 } else if (t->text) {
975 strcpy(p, t->text);
976 p += strlen(p);
977 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000978 }
979 *p = '\0';
980 return line;
981}
982
983/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000984 * A scanner, suitable for use by the expression evaluator, which
985 * operates on a line of Tokens. Expects a pointer to a pointer to
986 * the first token in the line to be passed in as its private_data
987 * field.
988 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000989static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000990{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000991 Token **tlineptr = private_data;
992 Token *tline;
993
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 do {
995 tline = *tlineptr;
996 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000997 }
998 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001000
1001 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001003
1004 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001006 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001008
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 if (tline->type == TOK_ID) {
1010 tokval->t_charptr = tline->text;
1011 if (tline->text[0] == '$') {
1012 tokval->t_charptr++;
1013 return tokval->t_type = TOKEN_ID;
1014 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001015
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 /*
1017 * This is the only special case we actually need to worry
1018 * about in this restricted context.
1019 */
1020 if (!nasm_stricmp(tline->text, "seg"))
1021 return tokval->t_type = TOKEN_SEG;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001022
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 return tokval->t_type = TOKEN_ID;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001024 }
1025
H. Peter Anvine2c80182005-01-15 22:15:51 +00001026 if (tline->type == TOK_NUMBER) {
1027 int rn_error;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001028
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 tokval->t_integer = readnum(tline->text, &rn_error);
1030 if (rn_error)
1031 return tokval->t_type = TOKEN_ERRNUM;
1032 tokval->t_charptr = NULL;
1033 return tokval->t_type = TOKEN_NUM;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001034 }
1035
H. Peter Anvine2c80182005-01-15 22:15:51 +00001036 if (tline->type == TOK_STRING) {
1037 int rn_warn;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001038 char q, *r;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001039 int l;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001040
H. Peter Anvine2c80182005-01-15 22:15:51 +00001041 r = tline->text;
1042 q = *r++;
1043 l = strlen(r);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001044
H. Peter Anvine2c80182005-01-15 22:15:51 +00001045 if (l == 0 || r[l - 1] != q)
1046 return tokval->t_type = TOKEN_ERRNUM;
1047 tokval->t_integer = readstrnum(r, l - 1, &rn_warn);
1048 if (rn_warn)
1049 error(ERR_WARNING | ERR_PASS1, "character constant too long");
1050 tokval->t_charptr = NULL;
1051 return tokval->t_type = TOKEN_NUM;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001052 }
1053
H. Peter Anvine2c80182005-01-15 22:15:51 +00001054 if (tline->type == TOK_OTHER) {
1055 if (!strcmp(tline->text, "<<"))
1056 return tokval->t_type = TOKEN_SHL;
1057 if (!strcmp(tline->text, ">>"))
1058 return tokval->t_type = TOKEN_SHR;
1059 if (!strcmp(tline->text, "//"))
1060 return tokval->t_type = TOKEN_SDIV;
1061 if (!strcmp(tline->text, "%%"))
1062 return tokval->t_type = TOKEN_SMOD;
1063 if (!strcmp(tline->text, "=="))
1064 return tokval->t_type = TOKEN_EQ;
1065 if (!strcmp(tline->text, "<>"))
1066 return tokval->t_type = TOKEN_NE;
1067 if (!strcmp(tline->text, "!="))
1068 return tokval->t_type = TOKEN_NE;
1069 if (!strcmp(tline->text, "<="))
1070 return tokval->t_type = TOKEN_LE;
1071 if (!strcmp(tline->text, ">="))
1072 return tokval->t_type = TOKEN_GE;
1073 if (!strcmp(tline->text, "&&"))
1074 return tokval->t_type = TOKEN_DBL_AND;
1075 if (!strcmp(tline->text, "^^"))
1076 return tokval->t_type = TOKEN_DBL_XOR;
1077 if (!strcmp(tline->text, "||"))
1078 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001079 }
1080
1081 /*
1082 * We have no other options: just return the first character of
1083 * the token text.
1084 */
1085 return tokval->t_type = tline->text[0];
1086}
1087
1088/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001089 * Compare a string to the name of an existing macro; this is a
1090 * simple wrapper which calls either strcmp or nasm_stricmp
1091 * depending on the value of the `casesense' parameter.
1092 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001093static int mstrcmp(char *p, char *q, int casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001094{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001095 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001096}
1097
1098/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001099 * Return the Context structure associated with a %$ token. Return
1100 * NULL, having _already_ reported an error condition, if the
1101 * context stack isn't deep enough for the supplied number of $
1102 * signs.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001103 * If all_contexts == TRUE, contexts that enclose current are
1104 * also scanned for such smacro, until it is found; if not -
1105 * only the context that directly results from the number of $'s
1106 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001107 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001108static Context *get_ctx(char *name, int all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001109{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001110 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001111 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001112 int i;
1113
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001114 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001115 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001116
H. Peter Anvine2c80182005-01-15 22:15:51 +00001117 if (!cstk) {
1118 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1119 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001120 }
1121
H. Peter Anvine2c80182005-01-15 22:15:51 +00001122 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1123 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001124/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001125 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001126 if (!ctx) {
1127 error(ERR_NONFATAL, "`%s': context stack is only"
1128 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1129 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001130 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001131 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001133
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 do {
1135 /* Search for this smacro in found context */
1136 m = ctx->localmac;
1137 while (m) {
1138 if (!mstrcmp(m->name, name, m->casesense))
1139 return ctx;
1140 m = m->next;
1141 }
1142 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001143 }
1144 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001145 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001146}
1147
1148/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001149 * Open an include file. This routine must always return a valid
1150 * file pointer if it returns - it's responsible for throwing an
1151 * ERR_FATAL and bombing out completely if not. It should also try
1152 * the include path one by one until it finds the file or reaches
1153 * the end of the path.
1154 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001155static FILE *inc_fopen(char *file)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001156{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001157 FILE *fp;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001158 char *prefix = "", *combine;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001159 IncPath *ip = ipath;
H. Peter Anvin620515a2002-04-30 20:57:38 +00001160 static int namelen = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001161 int len = strlen(file);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001162
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163 while (1) {
1164 combine = nasm_malloc(strlen(prefix) + len + 1);
1165 strcpy(combine, prefix);
1166 strcat(combine, file);
1167 fp = fopen(combine, "r");
1168 if (pass == 0 && fp) {
1169 namelen += strlen(combine) + 1;
1170 if (namelen > 62) {
1171 printf(" \\\n ");
1172 namelen = 2;
1173 }
1174 printf(" %s", combine);
1175 }
1176 nasm_free(combine);
1177 if (fp)
1178 return fp;
1179 if (!ip)
1180 break;
1181 prefix = ip->path;
1182 ip = ip->next;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001183
1184 if (!prefix) {
1185 /* -MG given and file not found */
1186 if (pass == 0) {
1187 namelen += strlen(file) + 1;
1188 if (namelen > 62) {
1189 printf(" \\\n ");
1190 namelen = 2;
1191 }
1192 printf(" %s", file);
1193 }
1194 return NULL;
1195 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001196 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001197
H. Peter Anvin734b1882002-04-30 21:01:08 +00001198 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001199 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001200}
1201
1202/*
H. Peter Anvin97a23472007-09-16 17:57:25 -07001203 * Search for a key in the hash index; adding it if necessary
1204 * (in which case we initialize the data pointer to NULL.)
1205 */
1206static void **
1207hash_findi_add(struct hash_table *hash, const char *str)
1208{
1209 struct hash_insert hi;
1210 void **r;
1211 char *strx;
1212
1213 r = hash_findi(hash, str, &hi);
1214 if (r)
1215 return r;
1216
1217 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
1218 return hash_add(&hi, strx, NULL);
1219}
1220
1221/*
1222 * Like hash_findi, but returns the data element rather than a pointer
1223 * to it. Used only when not adding a new element, hence no third
1224 * argument.
1225 */
1226static void *
1227hash_findix(struct hash_table *hash, const char *str)
1228{
1229 void **p;
1230
1231 p = hash_findi(hash, str, NULL);
1232 return p ? *p : NULL;
1233}
1234
1235/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001236 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001237 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001238 * return TRUE if _any_ single-line macro of that name is defined.
1239 * Otherwise, will return TRUE if a single-line macro with either
1240 * `nparam' or no parameters is defined.
1241 *
1242 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001243 * defined, or nparam is -1, the address of the definition structure
1244 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001245 * is NULL, no action will be taken regarding its contents, and no
1246 * error will occur.
1247 *
1248 * Note that this is also called with nparam zero to resolve
1249 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001250 *
1251 * If you already know which context macro belongs to, you can pass
1252 * the context pointer as first parameter; if you won't but name begins
1253 * with %$ the context will be automatically computed. If all_contexts
1254 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001255 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001256static int
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001257smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001258 int nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001259{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001260 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001261
H. Peter Anvin97a23472007-09-16 17:57:25 -07001262 if (ctx) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 m = ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001264 } else if (name[0] == '%' && name[1] == '$') {
1265 if (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001266 ctx = get_ctx(name, FALSE);
1267 if (!ctx)
1268 return FALSE; /* got to return _something_ */
1269 m = ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001270 } else {
1271 m = (SMacro *) hash_findix(smacros, name);
1272 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001273
H. Peter Anvine2c80182005-01-15 22:15:51 +00001274 while (m) {
1275 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1276 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam)) {
1277 if (defn) {
1278 if (nparam == m->nparam || nparam == -1)
1279 *defn = m;
1280 else
1281 *defn = NULL;
1282 }
1283 return TRUE;
1284 }
1285 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001286 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001287
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001288 return FALSE;
1289}
1290
1291/*
1292 * Count and mark off the parameters in a multi-line macro call.
1293 * This is called both from within the multi-line macro expansion
1294 * code, and also to mark off the default parameters when provided
1295 * in a %macro definition line.
1296 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001297static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001298{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001299 int paramsize, brace;
1300
1301 *nparam = paramsize = 0;
1302 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 while (t) {
1304 if (*nparam >= paramsize) {
1305 paramsize += PARAM_DELTA;
1306 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1307 }
1308 skip_white_(t);
1309 brace = FALSE;
1310 if (tok_is_(t, "{"))
1311 brace = TRUE;
1312 (*params)[(*nparam)++] = t;
1313 while (tok_isnt_(t, brace ? "}" : ","))
1314 t = t->next;
1315 if (t) { /* got a comma/brace */
1316 t = t->next;
1317 if (brace) {
1318 /*
1319 * Now we've found the closing brace, look further
1320 * for the comma.
1321 */
1322 skip_white_(t);
1323 if (tok_isnt_(t, ",")) {
1324 error(ERR_NONFATAL,
1325 "braces do not enclose all of macro parameter");
1326 while (tok_isnt_(t, ","))
1327 t = t->next;
1328 }
1329 if (t)
1330 t = t->next; /* eat the comma */
1331 }
1332 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001333 }
1334}
1335
1336/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001337 * Determine whether one of the various `if' conditions is true or
1338 * not.
1339 *
1340 * We must free the tline we get passed.
1341 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001342static int if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001343{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001344 enum pp_conditional i = PP_COND(ct);
1345 int j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001346 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001347 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001348 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001349 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001350
1351 origline = tline;
1352
H. Peter Anvine2c80182005-01-15 22:15:51 +00001353 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001354 case PPC_IFCTX:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001355 j = FALSE; /* have we matched yet? */
1356 while (cstk && tline) {
1357 skip_white_(tline);
1358 if (!tline || tline->type != TOK_ID) {
1359 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001360 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361 free_tlist(origline);
1362 return -1;
1363 }
1364 if (!nasm_stricmp(tline->text, cstk->name))
1365 j = TRUE;
1366 tline = tline->next;
1367 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001368 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001369
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001370 case PPC_IFDEF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001371 j = FALSE; /* have we matched yet? */
1372 while (tline) {
1373 skip_white_(tline);
1374 if (!tline || (tline->type != TOK_ID &&
1375 (tline->type != TOK_PREPROC_ID ||
1376 tline->text[1] != '$'))) {
1377 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001378 "`%s' expects macro identifiers", pp_directives[ct]);
1379 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 }
1381 if (smacro_defined(NULL, tline->text, 0, NULL, 1))
1382 j = TRUE;
1383 tline = tline->next;
1384 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001385 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001386
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001387 case PPC_IFIDN:
1388 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001389 tline = expand_smacro(tline);
1390 t = tt = tline;
1391 while (tok_isnt_(tt, ","))
1392 tt = tt->next;
1393 if (!tt) {
1394 error(ERR_NONFATAL,
1395 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001396 pp_directives[ct]);
1397 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001398 }
1399 tt = tt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001400 j = TRUE; /* assume equality unless proved not */
1401 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1402 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1403 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001404 pp_directives[ct]);
1405 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001406 }
1407 if (t->type == TOK_WHITESPACE) {
1408 t = t->next;
1409 continue;
1410 }
1411 if (tt->type == TOK_WHITESPACE) {
1412 tt = tt->next;
1413 continue;
1414 }
1415 if (tt->type != t->type) {
1416 j = FALSE; /* found mismatching tokens */
1417 break;
1418 }
1419 /* Unify surrounding quotes for strings */
1420 if (t->type == TOK_STRING) {
1421 tt->text[0] = t->text[0];
1422 tt->text[strlen(tt->text) - 1] = t->text[0];
1423 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001424 if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001425 j = FALSE; /* found mismatching tokens */
1426 break;
1427 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001428
H. Peter Anvine2c80182005-01-15 22:15:51 +00001429 t = t->next;
1430 tt = tt->next;
1431 }
1432 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1433 j = FALSE; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001434 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001435
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001436 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 {
1438 int found = 0;
1439 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001440
H. Peter Anvine2c80182005-01-15 22:15:51 +00001441 tline = tline->next;
1442 skip_white_(tline);
1443 tline = expand_id(tline);
1444 if (!tok_type_(tline, TOK_ID)) {
1445 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001446 "`%s' expects a macro name", pp_directives[ct]);
1447 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001448 }
1449 searching.name = nasm_strdup(tline->text);
1450 searching.casesense = (i == PP_MACRO);
1451 searching.plus = FALSE;
1452 searching.nolist = FALSE;
1453 searching.in_progress = FALSE;
1454 searching.rep_nest = NULL;
1455 searching.nparam_min = 0;
1456 searching.nparam_max = INT_MAX;
1457 tline = expand_smacro(tline->next);
1458 skip_white_(tline);
1459 if (!tline) {
1460 } else if (!tok_type_(tline, TOK_NUMBER)) {
1461 error(ERR_NONFATAL,
1462 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001463 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001464 } else {
1465 searching.nparam_min = searching.nparam_max =
1466 readnum(tline->text, &j);
1467 if (j)
1468 error(ERR_NONFATAL,
1469 "unable to parse parameter count `%s'",
1470 tline->text);
1471 }
1472 if (tline && tok_is_(tline->next, "-")) {
1473 tline = tline->next->next;
1474 if (tok_is_(tline, "*"))
1475 searching.nparam_max = INT_MAX;
1476 else if (!tok_type_(tline, TOK_NUMBER))
1477 error(ERR_NONFATAL,
1478 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001479 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001480 else {
1481 searching.nparam_max = readnum(tline->text, &j);
1482 if (j)
1483 error(ERR_NONFATAL,
1484 "unable to parse parameter count `%s'",
1485 tline->text);
1486 if (searching.nparam_min > searching.nparam_max)
1487 error(ERR_NONFATAL,
1488 "minimum parameter count exceeds maximum");
1489 }
1490 }
1491 if (tline && tok_is_(tline->next, "+")) {
1492 tline = tline->next;
1493 searching.plus = TRUE;
1494 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07001495 mmac = (MMacro *) hash_findix(mmacros, searching.name);
1496 while (mmac) {
1497 if (!strcmp(mmac->name, searching.name) &&
1498 (mmac->nparam_min <= searching.nparam_max
1499 || searching.plus)
1500 && (searching.nparam_min <= mmac->nparam_max
1501 || mmac->plus)) {
1502 found = TRUE;
1503 break;
1504 }
1505 mmac = mmac->next;
1506 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001507 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001508 j = found;
1509 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001510 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001511
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001512 case PPC_IFID:
1513 needtype = TOK_ID;
1514 goto iftype;
1515 case PPC_IFNUM:
1516 needtype = TOK_NUMBER;
1517 goto iftype;
1518 case PPC_IFSTR:
1519 needtype = TOK_STRING;
1520 goto iftype;
1521
1522 iftype:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001523 tline = expand_smacro(tline);
1524 t = tline;
1525 while (tok_type_(t, TOK_WHITESPACE))
1526 t = t->next;
1527 j = FALSE; /* placate optimiser */
1528 if (t)
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001529 j = t->type == needtype;
1530 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001531
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001532 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001533 t = tline = expand_smacro(tline);
1534 tptr = &t;
1535 tokval.t_type = TOKEN_INVALID;
1536 evalresult = evaluate(ppscan, tptr, &tokval,
1537 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001538 if (!evalresult)
1539 return -1;
1540 if (tokval.t_type)
1541 error(ERR_WARNING,
1542 "trailing garbage after expression ignored");
1543 if (!is_simple(evalresult)) {
1544 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001545 "non-constant value given to `%s'", pp_directives[ct]);
1546 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001547 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001548 j = reloc_value(evalresult) != 0;
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001549 return j;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001550
H. Peter Anvine2c80182005-01-15 22:15:51 +00001551 default:
1552 error(ERR_FATAL,
1553 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001554 pp_directives[ct]);
1555 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001556 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001557
1558 free_tlist(origline);
1559 return j ^ PP_NEGATIVE(ct);
1560
1561fail:
1562 free_tlist(origline);
1563 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001564}
1565
1566/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001567 * Expand macros in a string. Used in %error and %include directives.
Keith Kaniosb7a89542007-04-12 02:40:54 +00001568 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001569 * The returned variable should ALWAYS be freed after usage.
1570 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001571void expand_macros_in_string(char **p)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001572{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001573 Token *line = tokenize(*p);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001574 line = expand_smacro(line);
1575 *p = detoken(line, FALSE);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001576}
1577
Ed Beroset3ab3f412002-06-11 03:31:49 +00001578/**
1579 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001580 * Find out if a line contains a preprocessor directive, and deal
1581 * with it if so.
1582 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001583 * If a directive _is_ found, it is the responsibility of this routine
1584 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001585 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001586 * @param tline a pointer to the current tokeninzed line linked list
1587 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001588 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001589 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001590static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001591{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001592 enum preproc_token i;
1593 int j;
1594 int nparam, nolist;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001595 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001596 int offset;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001597 char *p, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001598 Include *inc;
1599 Context *ctx;
1600 Cond *cond;
1601 SMacro *smac, **smhead;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001602 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001603 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1604 Line *l;
1605 struct tokenval tokval;
1606 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001607 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001608
1609 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001610
H. Peter Anvineba20a72002-04-30 20:53:55 +00001611 skip_white_(tline);
1612 if (!tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001613 (tline->text[1] == '%' || tline->text[1] == '$'
1614 || tline->text[1] == '!'))
1615 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001616
H. Peter Anvin4169a472007-09-12 01:29:43 +00001617 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001618
1619 /*
1620 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001621 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001622 * we should ignore all directives except for condition
1623 * directives.
1624 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001625 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001626 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1627 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001628 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001629
1630 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001631 * If we're defining a macro or reading a %rep block, we should
1632 * ignore all directives except for %macro/%imacro (which
1633 * generate an error), %endm/%endmacro, and (only if we're in a
H. Peter Anvineba20a72002-04-30 20:53:55 +00001634 * %rep block) %endrep. If we're in a %rep block, another %rep
1635 * causes an error, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001636 */
1637 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001638 i != PP_ENDMACRO && i != PP_ENDM &&
1639 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1640 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001641 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001642
H. Peter Anvin4169a472007-09-12 01:29:43 +00001643 switch (i) {
1644 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1646 tline->text);
1647 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001648
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 case PP_STACKSIZE:
1650 /* Directive to tell NASM what the default stack size is. The
1651 * default is for a 16-bit stack, and this can be overriden with
1652 * %stacksize large.
1653 * the following form:
1654 *
1655 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1656 */
1657 tline = tline->next;
1658 if (tline && tline->type == TOK_WHITESPACE)
1659 tline = tline->next;
1660 if (!tline || tline->type != TOK_ID) {
1661 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1662 free_tlist(origline);
1663 return DIRECTIVE_FOUND;
1664 }
1665 if (nasm_stricmp(tline->text, "flat") == 0) {
1666 /* All subsequent ARG directives are for a 32-bit stack */
1667 StackSize = 4;
1668 StackPointer = "ebp";
1669 ArgOffset = 8;
1670 LocalOffset = 4;
1671 } else if (nasm_stricmp(tline->text, "large") == 0) {
1672 /* All subsequent ARG directives are for a 16-bit stack,
1673 * far function call.
1674 */
1675 StackSize = 2;
1676 StackPointer = "bp";
1677 ArgOffset = 4;
1678 LocalOffset = 2;
1679 } else if (nasm_stricmp(tline->text, "small") == 0) {
1680 /* All subsequent ARG directives are for a 16-bit stack,
1681 * far function call. We don't support near functions.
1682 */
1683 StackSize = 2;
1684 StackPointer = "bp";
1685 ArgOffset = 6;
1686 LocalOffset = 2;
1687 } else {
1688 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1689 free_tlist(origline);
1690 return DIRECTIVE_FOUND;
1691 }
1692 free_tlist(origline);
1693 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001694
H. Peter Anvine2c80182005-01-15 22:15:51 +00001695 case PP_ARG:
1696 /* TASM like ARG directive to define arguments to functions, in
1697 * the following form:
1698 *
1699 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1700 */
1701 offset = ArgOffset;
1702 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001703 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001704 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001705
H. Peter Anvine2c80182005-01-15 22:15:51 +00001706 /* Find the argument name */
1707 tline = tline->next;
1708 if (tline && tline->type == TOK_WHITESPACE)
1709 tline = tline->next;
1710 if (!tline || tline->type != TOK_ID) {
1711 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1712 free_tlist(origline);
1713 return DIRECTIVE_FOUND;
1714 }
1715 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001716
H. Peter Anvine2c80182005-01-15 22:15:51 +00001717 /* Find the argument size type */
1718 tline = tline->next;
1719 if (!tline || tline->type != TOK_OTHER
1720 || tline->text[0] != ':') {
1721 error(ERR_NONFATAL,
1722 "Syntax error processing `%%arg' directive");
1723 free_tlist(origline);
1724 return DIRECTIVE_FOUND;
1725 }
1726 tline = tline->next;
1727 if (!tline || tline->type != TOK_ID) {
1728 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1729 free_tlist(origline);
1730 return DIRECTIVE_FOUND;
1731 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001732
H. Peter Anvine2c80182005-01-15 22:15:51 +00001733 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001734 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001735 tt = expand_smacro(tt);
1736 if (nasm_stricmp(tt->text, "byte") == 0) {
1737 size = MAX(StackSize, 1);
1738 } else if (nasm_stricmp(tt->text, "word") == 0) {
1739 size = MAX(StackSize, 2);
1740 } else if (nasm_stricmp(tt->text, "dword") == 0) {
1741 size = MAX(StackSize, 4);
1742 } else if (nasm_stricmp(tt->text, "qword") == 0) {
1743 size = MAX(StackSize, 8);
1744 } else if (nasm_stricmp(tt->text, "tword") == 0) {
1745 size = MAX(StackSize, 10);
1746 } else {
1747 error(ERR_NONFATAL,
1748 "Invalid size type for `%%arg' missing directive");
1749 free_tlist(tt);
1750 free_tlist(origline);
1751 return DIRECTIVE_FOUND;
1752 }
1753 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001754
H. Peter Anvine2c80182005-01-15 22:15:51 +00001755 /* Now define the macro for the argument */
1756 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1757 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001758 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001759 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001760
H. Peter Anvine2c80182005-01-15 22:15:51 +00001761 /* Move to the next argument in the list */
1762 tline = tline->next;
1763 if (tline && tline->type == TOK_WHITESPACE)
1764 tline = tline->next;
1765 }
1766 while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1767 free_tlist(origline);
1768 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001769
H. Peter Anvine2c80182005-01-15 22:15:51 +00001770 case PP_LOCAL:
1771 /* TASM like LOCAL directive to define local variables for a
1772 * function, in the following form:
1773 *
1774 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1775 *
1776 * The '= LocalSize' at the end is ignored by NASM, but is
1777 * required by TASM to define the local parameter size (and used
1778 * by the TASM macro package).
1779 */
1780 offset = LocalOffset;
1781 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001782 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001783 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001784
H. Peter Anvine2c80182005-01-15 22:15:51 +00001785 /* Find the argument name */
1786 tline = tline->next;
1787 if (tline && tline->type == TOK_WHITESPACE)
1788 tline = tline->next;
1789 if (!tline || tline->type != TOK_ID) {
1790 error(ERR_NONFATAL,
1791 "`%%local' missing argument parameter");
1792 free_tlist(origline);
1793 return DIRECTIVE_FOUND;
1794 }
1795 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001796
H. Peter Anvine2c80182005-01-15 22:15:51 +00001797 /* Find the argument size type */
1798 tline = tline->next;
1799 if (!tline || tline->type != TOK_OTHER
1800 || tline->text[0] != ':') {
1801 error(ERR_NONFATAL,
1802 "Syntax error processing `%%local' directive");
1803 free_tlist(origline);
1804 return DIRECTIVE_FOUND;
1805 }
1806 tline = tline->next;
1807 if (!tline || tline->type != TOK_ID) {
1808 error(ERR_NONFATAL,
1809 "`%%local' missing size type parameter");
1810 free_tlist(origline);
1811 return DIRECTIVE_FOUND;
1812 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001813
H. Peter Anvine2c80182005-01-15 22:15:51 +00001814 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001815 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001816 tt = expand_smacro(tt);
1817 if (nasm_stricmp(tt->text, "byte") == 0) {
1818 size = MAX(StackSize, 1);
1819 } else if (nasm_stricmp(tt->text, "word") == 0) {
1820 size = MAX(StackSize, 2);
1821 } else if (nasm_stricmp(tt->text, "dword") == 0) {
1822 size = MAX(StackSize, 4);
1823 } else if (nasm_stricmp(tt->text, "qword") == 0) {
1824 size = MAX(StackSize, 8);
1825 } else if (nasm_stricmp(tt->text, "tword") == 0) {
1826 size = MAX(StackSize, 10);
1827 } else {
1828 error(ERR_NONFATAL,
1829 "Invalid size type for `%%local' missing directive");
1830 free_tlist(tt);
1831 free_tlist(origline);
1832 return DIRECTIVE_FOUND;
1833 }
1834 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001835
H. Peter Anvine2c80182005-01-15 22:15:51 +00001836 /* Now define the macro for the argument */
1837 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
1838 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001839 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001840 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001841
H. Peter Anvine2c80182005-01-15 22:15:51 +00001842 /* Now define the assign to setup the enter_c macro correctly */
1843 snprintf(directive, sizeof(directive),
1844 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001845 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001846
H. Peter Anvine2c80182005-01-15 22:15:51 +00001847 /* Move to the next argument in the list */
1848 tline = tline->next;
1849 if (tline && tline->type == TOK_WHITESPACE)
1850 tline = tline->next;
1851 }
1852 while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1853 free_tlist(origline);
1854 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001855
H. Peter Anvine2c80182005-01-15 22:15:51 +00001856 case PP_CLEAR:
1857 if (tline->next)
1858 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07001859 free_macros();
1860 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001861 free_tlist(origline);
1862 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001863
H. Peter Anvine2c80182005-01-15 22:15:51 +00001864 case PP_INCLUDE:
1865 tline = tline->next;
1866 skip_white_(tline);
1867 if (!tline || (tline->type != TOK_STRING &&
1868 tline->type != TOK_INTERNAL_STRING)) {
1869 error(ERR_NONFATAL, "`%%include' expects a file name");
1870 free_tlist(origline);
1871 return DIRECTIVE_FOUND; /* but we did _something_ */
1872 }
1873 if (tline->next)
1874 error(ERR_WARNING,
1875 "trailing garbage after `%%include' ignored");
1876 if (tline->type != TOK_INTERNAL_STRING) {
1877 p = tline->text + 1; /* point past the quote to the name */
1878 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
1879 } else
1880 p = tline->text; /* internal_string is easier */
1881 expand_macros_in_string(&p);
1882 inc = nasm_malloc(sizeof(Include));
1883 inc->next = istk;
1884 inc->conds = NULL;
1885 inc->fp = inc_fopen(p);
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001886 if (!inc->fp && pass == 0) {
1887 /* -MG given but file not found */
1888 nasm_free(inc);
1889 } else {
1890 inc->fname = src_set_fname(p);
1891 inc->lineno = src_set_linnum(0);
1892 inc->lineinc = 1;
1893 inc->expansion = NULL;
1894 inc->mstk = NULL;
1895 istk = inc;
1896 list->uplevel(LIST_INCLUDE);
1897 }
1898 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001899 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001900
H. Peter Anvine2c80182005-01-15 22:15:51 +00001901 case PP_PUSH:
1902 tline = tline->next;
1903 skip_white_(tline);
1904 tline = expand_id(tline);
1905 if (!tok_type_(tline, TOK_ID)) {
1906 error(ERR_NONFATAL, "`%%push' expects a context identifier");
1907 free_tlist(origline);
1908 return DIRECTIVE_FOUND; /* but we did _something_ */
1909 }
1910 if (tline->next)
1911 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
1912 ctx = nasm_malloc(sizeof(Context));
1913 ctx->next = cstk;
1914 ctx->localmac = NULL;
1915 ctx->name = nasm_strdup(tline->text);
1916 ctx->number = unique++;
1917 cstk = ctx;
1918 free_tlist(origline);
1919 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001920
H. Peter Anvine2c80182005-01-15 22:15:51 +00001921 case PP_REPL:
1922 tline = tline->next;
1923 skip_white_(tline);
1924 tline = expand_id(tline);
1925 if (!tok_type_(tline, TOK_ID)) {
1926 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
1927 free_tlist(origline);
1928 return DIRECTIVE_FOUND; /* but we did _something_ */
1929 }
1930 if (tline->next)
1931 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
1932 if (!cstk)
1933 error(ERR_NONFATAL, "`%%repl': context stack is empty");
1934 else {
1935 nasm_free(cstk->name);
1936 cstk->name = nasm_strdup(tline->text);
1937 }
1938 free_tlist(origline);
1939 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001940
H. Peter Anvine2c80182005-01-15 22:15:51 +00001941 case PP_POP:
1942 if (tline->next)
1943 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
1944 if (!cstk)
1945 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
1946 else
1947 ctx_pop();
1948 free_tlist(origline);
1949 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001950
H. Peter Anvine2c80182005-01-15 22:15:51 +00001951 case PP_ERROR:
1952 tline->next = expand_smacro(tline->next);
1953 tline = tline->next;
1954 skip_white_(tline);
1955 if (tok_type_(tline, TOK_STRING)) {
1956 p = tline->text + 1; /* point past the quote to the name */
1957 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
1958 expand_macros_in_string(&p);
1959 error(ERR_NONFATAL, "%s", p);
1960 nasm_free(p);
1961 } else {
1962 p = detoken(tline, FALSE);
1963 error(ERR_WARNING, "%s", p);
1964 nasm_free(p);
1965 }
1966 free_tlist(origline);
1967 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001968
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001969 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001970 if (istk->conds && !emitting(istk->conds->state))
1971 j = COND_NEVER;
1972 else {
1973 j = if_condition(tline->next, i);
1974 tline->next = NULL; /* it got freed */
1975 free_tlist(origline);
1976 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
1977 }
1978 cond = nasm_malloc(sizeof(Cond));
1979 cond->next = istk->conds;
1980 cond->state = j;
1981 istk->conds = cond;
1982 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001983
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001984 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001985 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00001986 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001987 if (emitting(istk->conds->state)
1988 || istk->conds->state == COND_NEVER)
1989 istk->conds->state = COND_NEVER;
1990 else {
1991 /*
1992 * IMPORTANT: In the case of %if, we will already have
1993 * called expand_mmac_params(); however, if we're
1994 * processing an %elif we must have been in a
1995 * non-emitting mode, which would have inhibited
1996 * the normal invocation of expand_mmac_params(). Therefore,
1997 * we have to do it explicitly here.
1998 */
1999 j = if_condition(expand_mmac_params(tline->next), i);
2000 tline->next = NULL; /* it got freed */
2001 free_tlist(origline);
2002 istk->conds->state =
2003 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2004 }
2005 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002006
H. Peter Anvine2c80182005-01-15 22:15:51 +00002007 case PP_ELSE:
2008 if (tline->next)
2009 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2010 if (!istk->conds)
2011 error(ERR_FATAL, "`%%else': no matching `%%if'");
2012 if (emitting(istk->conds->state)
2013 || istk->conds->state == COND_NEVER)
2014 istk->conds->state = COND_ELSE_FALSE;
2015 else
2016 istk->conds->state = COND_ELSE_TRUE;
2017 free_tlist(origline);
2018 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002019
H. Peter Anvine2c80182005-01-15 22:15:51 +00002020 case PP_ENDIF:
2021 if (tline->next)
2022 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2023 if (!istk->conds)
2024 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2025 cond = istk->conds;
2026 istk->conds = cond->next;
2027 nasm_free(cond);
2028 free_tlist(origline);
2029 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002030
H. Peter Anvine2c80182005-01-15 22:15:51 +00002031 case PP_MACRO:
2032 case PP_IMACRO:
2033 if (defining)
2034 error(ERR_FATAL,
2035 "`%%%smacro': already defining a macro",
2036 (i == PP_IMACRO ? "i" : ""));
2037 tline = tline->next;
2038 skip_white_(tline);
2039 tline = expand_id(tline);
2040 if (!tok_type_(tline, TOK_ID)) {
2041 error(ERR_NONFATAL,
2042 "`%%%smacro' expects a macro name",
2043 (i == PP_IMACRO ? "i" : ""));
2044 return DIRECTIVE_FOUND;
2045 }
2046 defining = nasm_malloc(sizeof(MMacro));
2047 defining->name = nasm_strdup(tline->text);
2048 defining->casesense = (i == PP_MACRO);
2049 defining->plus = FALSE;
2050 defining->nolist = FALSE;
2051 defining->in_progress = FALSE;
2052 defining->rep_nest = NULL;
2053 tline = expand_smacro(tline->next);
2054 skip_white_(tline);
2055 if (!tok_type_(tline, TOK_NUMBER)) {
2056 error(ERR_NONFATAL,
2057 "`%%%smacro' expects a parameter count",
2058 (i == PP_IMACRO ? "i" : ""));
2059 defining->nparam_min = defining->nparam_max = 0;
2060 } else {
2061 defining->nparam_min = defining->nparam_max =
2062 readnum(tline->text, &j);
2063 if (j)
2064 error(ERR_NONFATAL,
2065 "unable to parse parameter count `%s'", tline->text);
2066 }
2067 if (tline && tok_is_(tline->next, "-")) {
2068 tline = tline->next->next;
2069 if (tok_is_(tline, "*"))
2070 defining->nparam_max = INT_MAX;
2071 else if (!tok_type_(tline, TOK_NUMBER))
2072 error(ERR_NONFATAL,
2073 "`%%%smacro' expects a parameter count after `-'",
2074 (i == PP_IMACRO ? "i" : ""));
2075 else {
2076 defining->nparam_max = readnum(tline->text, &j);
2077 if (j)
2078 error(ERR_NONFATAL,
2079 "unable to parse parameter count `%s'",
2080 tline->text);
2081 if (defining->nparam_min > defining->nparam_max)
2082 error(ERR_NONFATAL,
2083 "minimum parameter count exceeds maximum");
2084 }
2085 }
2086 if (tline && tok_is_(tline->next, "+")) {
2087 tline = tline->next;
2088 defining->plus = TRUE;
2089 }
2090 if (tline && tok_type_(tline->next, TOK_ID) &&
2091 !nasm_stricmp(tline->next->text, ".nolist")) {
2092 tline = tline->next;
2093 defining->nolist = TRUE;
2094 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07002095 mmac = (MMacro *) hash_findix(mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002096 while (mmac) {
2097 if (!strcmp(mmac->name, defining->name) &&
2098 (mmac->nparam_min <= defining->nparam_max
2099 || defining->plus)
2100 && (defining->nparam_min <= mmac->nparam_max
2101 || mmac->plus)) {
2102 error(ERR_WARNING,
2103 "redefining multi-line macro `%s'", defining->name);
2104 break;
2105 }
2106 mmac = mmac->next;
2107 }
2108 /*
2109 * Handle default parameters.
2110 */
2111 if (tline && tline->next) {
2112 defining->dlist = tline->next;
2113 tline->next = NULL;
2114 count_mmac_params(defining->dlist, &defining->ndefs,
2115 &defining->defaults);
2116 } else {
2117 defining->dlist = NULL;
2118 defining->defaults = NULL;
2119 }
2120 defining->expansion = NULL;
2121 free_tlist(origline);
2122 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002123
H. Peter Anvine2c80182005-01-15 22:15:51 +00002124 case PP_ENDM:
2125 case PP_ENDMACRO:
2126 if (!defining) {
2127 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2128 return DIRECTIVE_FOUND;
2129 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07002130 mmhead = (MMacro **) hash_findi_add(mmacros, defining->name);
2131 defining->next = *mmhead;
2132 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002133 defining = NULL;
2134 free_tlist(origline);
2135 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002136
H. Peter Anvine2c80182005-01-15 22:15:51 +00002137 case PP_ROTATE:
2138 if (tline->next && tline->next->type == TOK_WHITESPACE)
2139 tline = tline->next;
2140 if (tline->next == NULL) {
2141 free_tlist(origline);
2142 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2143 return DIRECTIVE_FOUND;
2144 }
2145 t = expand_smacro(tline->next);
2146 tline->next = NULL;
2147 free_tlist(origline);
2148 tline = t;
2149 tptr = &t;
2150 tokval.t_type = TOKEN_INVALID;
2151 evalresult =
2152 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2153 free_tlist(tline);
2154 if (!evalresult)
2155 return DIRECTIVE_FOUND;
2156 if (tokval.t_type)
2157 error(ERR_WARNING,
2158 "trailing garbage after expression ignored");
2159 if (!is_simple(evalresult)) {
2160 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2161 return DIRECTIVE_FOUND;
2162 }
2163 mmac = istk->mstk;
2164 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2165 mmac = mmac->next_active;
2166 if (!mmac) {
2167 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2168 } else if (mmac->nparam == 0) {
2169 error(ERR_NONFATAL,
2170 "`%%rotate' invoked within macro without parameters");
2171 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002172 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002173
H. Peter Anvin25a99342007-09-22 17:45:45 -07002174 rotate %= (int)mmac->nparam;
2175 if (rotate < 0)
2176 rotate += mmac->nparam;
2177
2178 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002179 }
2180 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002181
H. Peter Anvine2c80182005-01-15 22:15:51 +00002182 case PP_REP:
2183 nolist = FALSE;
2184 do {
2185 tline = tline->next;
2186 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002187
H. Peter Anvine2c80182005-01-15 22:15:51 +00002188 if (tok_type_(tline, TOK_ID) &&
2189 nasm_stricmp(tline->text, ".nolist") == 0) {
2190 nolist = TRUE;
2191 do {
2192 tline = tline->next;
2193 } while (tok_type_(tline, TOK_WHITESPACE));
2194 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002195
H. Peter Anvine2c80182005-01-15 22:15:51 +00002196 if (tline) {
2197 t = expand_smacro(tline);
2198 tptr = &t;
2199 tokval.t_type = TOKEN_INVALID;
2200 evalresult =
2201 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2202 if (!evalresult) {
2203 free_tlist(origline);
2204 return DIRECTIVE_FOUND;
2205 }
2206 if (tokval.t_type)
2207 error(ERR_WARNING,
2208 "trailing garbage after expression ignored");
2209 if (!is_simple(evalresult)) {
2210 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2211 return DIRECTIVE_FOUND;
2212 }
2213 i = (int)reloc_value(evalresult) + 1;
2214 } else {
2215 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2216 i = 0;
2217 }
2218 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002219
H. Peter Anvine2c80182005-01-15 22:15:51 +00002220 tmp_defining = defining;
2221 defining = nasm_malloc(sizeof(MMacro));
2222 defining->name = NULL; /* flags this macro as a %rep block */
2223 defining->casesense = 0;
2224 defining->plus = FALSE;
2225 defining->nolist = nolist;
2226 defining->in_progress = i;
2227 defining->nparam_min = defining->nparam_max = 0;
2228 defining->defaults = NULL;
2229 defining->dlist = NULL;
2230 defining->expansion = NULL;
2231 defining->next_active = istk->mstk;
2232 defining->rep_nest = tmp_defining;
2233 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002234
H. Peter Anvine2c80182005-01-15 22:15:51 +00002235 case PP_ENDREP:
2236 if (!defining || defining->name) {
2237 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2238 return DIRECTIVE_FOUND;
2239 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002240
H. Peter Anvine2c80182005-01-15 22:15:51 +00002241 /*
2242 * Now we have a "macro" defined - although it has no name
2243 * and we won't be entering it in the hash tables - we must
2244 * push a macro-end marker for it on to istk->expansion.
2245 * After that, it will take care of propagating itself (a
2246 * macro-end marker line for a macro which is really a %rep
2247 * block will cause the macro to be re-expanded, complete
2248 * with another macro-end marker to ensure the process
2249 * continues) until the whole expansion is forcibly removed
2250 * from istk->expansion by a %exitrep.
2251 */
2252 l = nasm_malloc(sizeof(Line));
2253 l->next = istk->expansion;
2254 l->finishes = defining;
2255 l->first = NULL;
2256 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002257
H. Peter Anvine2c80182005-01-15 22:15:51 +00002258 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002259
H. Peter Anvine2c80182005-01-15 22:15:51 +00002260 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2261 tmp_defining = defining;
2262 defining = defining->rep_nest;
2263 free_tlist(origline);
2264 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002265
H. Peter Anvine2c80182005-01-15 22:15:51 +00002266 case PP_EXITREP:
2267 /*
2268 * We must search along istk->expansion until we hit a
2269 * macro-end marker for a macro with no name. Then we set
2270 * its `in_progress' flag to 0.
2271 */
2272 for (l = istk->expansion; l; l = l->next)
2273 if (l->finishes && !l->finishes->name)
2274 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002275
H. Peter Anvine2c80182005-01-15 22:15:51 +00002276 if (l)
2277 l->finishes->in_progress = 0;
2278 else
2279 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2280 free_tlist(origline);
2281 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002282
H. Peter Anvine2c80182005-01-15 22:15:51 +00002283 case PP_XDEFINE:
2284 case PP_IXDEFINE:
2285 case PP_DEFINE:
2286 case PP_IDEFINE:
2287 tline = tline->next;
2288 skip_white_(tline);
2289 tline = expand_id(tline);
2290 if (!tline || (tline->type != TOK_ID &&
2291 (tline->type != TOK_PREPROC_ID ||
2292 tline->text[1] != '$'))) {
2293 error(ERR_NONFATAL,
2294 "`%%%s%sdefine' expects a macro identifier",
2295 ((i == PP_IDEFINE || i == PP_IXDEFINE) ? "i" : ""),
2296 ((i == PP_XDEFINE || i == PP_IXDEFINE) ? "x" : ""));
2297 free_tlist(origline);
2298 return DIRECTIVE_FOUND;
2299 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002300
H. Peter Anvine2c80182005-01-15 22:15:51 +00002301 ctx = get_ctx(tline->text, FALSE);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002302
H. Peter Anvine2c80182005-01-15 22:15:51 +00002303 mname = tline->text;
2304 last = tline;
2305 param_start = tline = tline->next;
2306 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002307
H. Peter Anvine2c80182005-01-15 22:15:51 +00002308 /* Expand the macro definition now for %xdefine and %ixdefine */
2309 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2310 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002311
H. Peter Anvine2c80182005-01-15 22:15:51 +00002312 if (tok_is_(tline, "(")) {
2313 /*
2314 * This macro has parameters.
2315 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002316
H. Peter Anvine2c80182005-01-15 22:15:51 +00002317 tline = tline->next;
2318 while (1) {
2319 skip_white_(tline);
2320 if (!tline) {
2321 error(ERR_NONFATAL, "parameter identifier expected");
2322 free_tlist(origline);
2323 return DIRECTIVE_FOUND;
2324 }
2325 if (tline->type != TOK_ID) {
2326 error(ERR_NONFATAL,
2327 "`%s': parameter identifier expected",
2328 tline->text);
2329 free_tlist(origline);
2330 return DIRECTIVE_FOUND;
2331 }
2332 tline->type = TOK_SMAC_PARAM + nparam++;
2333 tline = tline->next;
2334 skip_white_(tline);
2335 if (tok_is_(tline, ",")) {
2336 tline = tline->next;
2337 continue;
2338 }
2339 if (!tok_is_(tline, ")")) {
2340 error(ERR_NONFATAL,
2341 "`)' expected to terminate macro template");
2342 free_tlist(origline);
2343 return DIRECTIVE_FOUND;
2344 }
2345 break;
2346 }
2347 last = tline;
2348 tline = tline->next;
2349 }
2350 if (tok_type_(tline, TOK_WHITESPACE))
2351 last = tline, tline = tline->next;
2352 macro_start = NULL;
2353 last->next = NULL;
2354 t = tline;
2355 while (t) {
2356 if (t->type == TOK_ID) {
2357 for (tt = param_start; tt; tt = tt->next)
2358 if (tt->type >= TOK_SMAC_PARAM &&
2359 !strcmp(tt->text, t->text))
2360 t->type = tt->type;
2361 }
2362 tt = t->next;
2363 t->next = macro_start;
2364 macro_start = t;
2365 t = tt;
2366 }
2367 /*
2368 * Good. We now have a macro name, a parameter count, and a
2369 * token list (in reverse order) for an expansion. We ought
2370 * to be OK just to create an SMacro, store it, and let
2371 * free_tlist have the rest of the line (which we have
2372 * carefully re-terminated after chopping off the expansion
2373 * from the end).
2374 */
2375 if (smacro_defined(ctx, mname, nparam, &smac, i == PP_DEFINE)) {
2376 if (!smac) {
2377 error(ERR_WARNING,
2378 "single-line macro `%s' defined both with and"
2379 " without parameters", mname);
2380 free_tlist(origline);
2381 free_tlist(macro_start);
2382 return DIRECTIVE_FOUND;
2383 } else {
2384 /*
2385 * We're redefining, so we have to take over an
2386 * existing SMacro structure. This means freeing
2387 * what was already in it.
2388 */
2389 nasm_free(smac->name);
2390 free_tlist(smac->expansion);
2391 }
2392 } else {
H. Peter Anvin97a23472007-09-16 17:57:25 -07002393 if (!ctx)
2394 smhead = (SMacro **) hash_findi_add(smacros, mname);
2395 else
2396 smhead = &ctx->localmac;
2397
H. Peter Anvine2c80182005-01-15 22:15:51 +00002398 smac = nasm_malloc(sizeof(SMacro));
2399 smac->next = *smhead;
2400 *smhead = smac;
2401 }
2402 smac->name = nasm_strdup(mname);
2403 smac->casesense = ((i == PP_DEFINE) || (i == PP_XDEFINE));
2404 smac->nparam = nparam;
2405 smac->expansion = macro_start;
2406 smac->in_progress = FALSE;
2407 free_tlist(origline);
2408 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002409
H. Peter Anvine2c80182005-01-15 22:15:51 +00002410 case PP_UNDEF:
2411 tline = tline->next;
2412 skip_white_(tline);
2413 tline = expand_id(tline);
2414 if (!tline || (tline->type != TOK_ID &&
2415 (tline->type != TOK_PREPROC_ID ||
2416 tline->text[1] != '$'))) {
2417 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2418 free_tlist(origline);
2419 return DIRECTIVE_FOUND;
2420 }
2421 if (tline->next) {
2422 error(ERR_WARNING,
2423 "trailing garbage after macro name ignored");
2424 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002425
H. Peter Anvine2c80182005-01-15 22:15:51 +00002426 /* Find the context that symbol belongs to */
2427 ctx = get_ctx(tline->text, FALSE);
2428 if (!ctx)
H. Peter Anvin97a23472007-09-16 17:57:25 -07002429 smhead = (SMacro **) hash_findi(smacros, tline->text, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002430 else
2431 smhead = &ctx->localmac;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002432
H. Peter Anvin97a23472007-09-16 17:57:25 -07002433 if (smhead) {
2434 SMacro *s, **sp;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002435
H. Peter Anvin97a23472007-09-16 17:57:25 -07002436 mname = tline->text;
2437 last = tline;
2438 last->next = NULL;
2439
2440 /*
2441 * We now have a macro name... go hunt for it.
2442 */
H. Peter Anvine373efd2007-09-24 21:33:17 -07002443 sp = smhead;
2444 while ((s = *sp) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -07002445 if (!mstrcmp(s->name, tline->text, s->casesense)) {
2446 *sp = s->next;
2447 nasm_free(s->name);
2448 free_tlist(s->expansion);
2449 nasm_free(s);
H. Peter Anvine373efd2007-09-24 21:33:17 -07002450 } else {
2451 sp = &s->next;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002452 }
2453 }
2454 free_tlist(origline);
2455 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002456 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002457
H. Peter Anvine2c80182005-01-15 22:15:51 +00002458 case PP_STRLEN:
2459 tline = tline->next;
2460 skip_white_(tline);
2461 tline = expand_id(tline);
2462 if (!tline || (tline->type != TOK_ID &&
2463 (tline->type != TOK_PREPROC_ID ||
2464 tline->text[1] != '$'))) {
2465 error(ERR_NONFATAL,
2466 "`%%strlen' expects a macro identifier as first parameter");
2467 free_tlist(origline);
2468 return DIRECTIVE_FOUND;
2469 }
2470 ctx = get_ctx(tline->text, FALSE);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002471
H. Peter Anvine2c80182005-01-15 22:15:51 +00002472 mname = tline->text;
2473 last = tline;
2474 tline = expand_smacro(tline->next);
2475 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002476
H. Peter Anvine2c80182005-01-15 22:15:51 +00002477 t = tline;
2478 while (tok_type_(t, TOK_WHITESPACE))
2479 t = t->next;
2480 /* t should now point to the string */
2481 if (t->type != TOK_STRING) {
2482 error(ERR_NONFATAL,
2483 "`%%strlen` requires string as second parameter");
2484 free_tlist(tline);
2485 free_tlist(origline);
2486 return DIRECTIVE_FOUND;
2487 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002488
H. Peter Anvine2c80182005-01-15 22:15:51 +00002489 macro_start = nasm_malloc(sizeof(*macro_start));
2490 macro_start->next = NULL;
2491 make_tok_num(macro_start, strlen(t->text) - 2);
2492 macro_start->mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002493
H. Peter Anvine2c80182005-01-15 22:15:51 +00002494 /*
2495 * We now have a macro name, an implicit parameter count of
2496 * zero, and a numeric token to use as an expansion. Create
2497 * and store an SMacro.
2498 */
2499 if (smacro_defined(ctx, mname, 0, &smac, i == PP_STRLEN)) {
2500 if (!smac)
2501 error(ERR_WARNING,
2502 "single-line macro `%s' defined both with and"
2503 " without parameters", mname);
2504 else {
2505 /*
2506 * We're redefining, so we have to take over an
2507 * existing SMacro structure. This means freeing
2508 * what was already in it.
2509 */
2510 nasm_free(smac->name);
2511 free_tlist(smac->expansion);
2512 }
2513 } else {
H. Peter Anvin97a23472007-09-16 17:57:25 -07002514 if (!ctx)
2515 smhead = (SMacro **) hash_findi_add(smacros, mname);
2516 else
2517 smhead = &ctx->localmac;
2518
H. Peter Anvine2c80182005-01-15 22:15:51 +00002519 smac = nasm_malloc(sizeof(SMacro));
2520 smac->next = *smhead;
2521 *smhead = smac;
2522 }
2523 smac->name = nasm_strdup(mname);
2524 smac->casesense = (i == PP_STRLEN);
2525 smac->nparam = 0;
2526 smac->expansion = macro_start;
2527 smac->in_progress = FALSE;
2528 free_tlist(tline);
2529 free_tlist(origline);
2530 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002531
H. Peter Anvine2c80182005-01-15 22:15:51 +00002532 case PP_SUBSTR:
2533 tline = tline->next;
2534 skip_white_(tline);
2535 tline = expand_id(tline);
2536 if (!tline || (tline->type != TOK_ID &&
2537 (tline->type != TOK_PREPROC_ID ||
2538 tline->text[1] != '$'))) {
2539 error(ERR_NONFATAL,
2540 "`%%substr' expects a macro identifier as first parameter");
2541 free_tlist(origline);
2542 return DIRECTIVE_FOUND;
2543 }
2544 ctx = get_ctx(tline->text, FALSE);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002545
H. Peter Anvine2c80182005-01-15 22:15:51 +00002546 mname = tline->text;
2547 last = tline;
2548 tline = expand_smacro(tline->next);
2549 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002550
H. Peter Anvine2c80182005-01-15 22:15:51 +00002551 t = tline->next;
2552 while (tok_type_(t, TOK_WHITESPACE))
2553 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002554
H. Peter Anvine2c80182005-01-15 22:15:51 +00002555 /* t should now point to the string */
2556 if (t->type != TOK_STRING) {
2557 error(ERR_NONFATAL,
2558 "`%%substr` requires string as second parameter");
2559 free_tlist(tline);
2560 free_tlist(origline);
2561 return DIRECTIVE_FOUND;
2562 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002563
H. Peter Anvine2c80182005-01-15 22:15:51 +00002564 tt = t->next;
2565 tptr = &tt;
2566 tokval.t_type = TOKEN_INVALID;
2567 evalresult =
2568 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2569 if (!evalresult) {
2570 free_tlist(tline);
2571 free_tlist(origline);
2572 return DIRECTIVE_FOUND;
2573 }
2574 if (!is_simple(evalresult)) {
2575 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2576 free_tlist(tline);
2577 free_tlist(origline);
2578 return DIRECTIVE_FOUND;
2579 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002580
H. Peter Anvine2c80182005-01-15 22:15:51 +00002581 macro_start = nasm_malloc(sizeof(*macro_start));
2582 macro_start->next = NULL;
2583 macro_start->text = nasm_strdup("'''");
2584 if (evalresult->value > 0
2585 && evalresult->value < strlen(t->text) - 1) {
2586 macro_start->text[1] = t->text[evalresult->value];
2587 } else {
2588 macro_start->text[2] = '\0';
2589 }
2590 macro_start->type = TOK_STRING;
2591 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002592
H. Peter Anvine2c80182005-01-15 22:15:51 +00002593 /*
2594 * We now have a macro name, an implicit parameter count of
2595 * zero, and a numeric token to use as an expansion. Create
2596 * and store an SMacro.
2597 */
2598 if (smacro_defined(ctx, mname, 0, &smac, i == PP_SUBSTR)) {
2599 if (!smac)
2600 error(ERR_WARNING,
2601 "single-line macro `%s' defined both with and"
2602 " without parameters", mname);
2603 else {
2604 /*
2605 * We're redefining, so we have to take over an
2606 * existing SMacro structure. This means freeing
2607 * what was already in it.
2608 */
2609 nasm_free(smac->name);
2610 free_tlist(smac->expansion);
2611 }
2612 } else {
H. Peter Anvin97a23472007-09-16 17:57:25 -07002613 if (!ctx)
2614 smhead = (SMacro **) hash_findi_add(smacros, tline->text);
2615 else
2616 smhead = &ctx->localmac;
2617
H. Peter Anvine2c80182005-01-15 22:15:51 +00002618 smac = nasm_malloc(sizeof(SMacro));
2619 smac->next = *smhead;
2620 *smhead = smac;
2621 }
2622 smac->name = nasm_strdup(mname);
2623 smac->casesense = (i == PP_SUBSTR);
2624 smac->nparam = 0;
2625 smac->expansion = macro_start;
2626 smac->in_progress = FALSE;
2627 free_tlist(tline);
2628 free_tlist(origline);
2629 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002630
H. Peter Anvine2c80182005-01-15 22:15:51 +00002631 case PP_ASSIGN:
2632 case PP_IASSIGN:
2633 tline = tline->next;
2634 skip_white_(tline);
2635 tline = expand_id(tline);
2636 if (!tline || (tline->type != TOK_ID &&
2637 (tline->type != TOK_PREPROC_ID ||
2638 tline->text[1] != '$'))) {
2639 error(ERR_NONFATAL,
2640 "`%%%sassign' expects a macro identifier",
2641 (i == PP_IASSIGN ? "i" : ""));
2642 free_tlist(origline);
2643 return DIRECTIVE_FOUND;
2644 }
2645 ctx = get_ctx(tline->text, FALSE);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002646
H. Peter Anvine2c80182005-01-15 22:15:51 +00002647 mname = tline->text;
2648 last = tline;
2649 tline = expand_smacro(tline->next);
2650 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002651
H. Peter Anvine2c80182005-01-15 22:15:51 +00002652 t = tline;
2653 tptr = &t;
2654 tokval.t_type = TOKEN_INVALID;
2655 evalresult =
2656 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2657 free_tlist(tline);
2658 if (!evalresult) {
2659 free_tlist(origline);
2660 return DIRECTIVE_FOUND;
2661 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002662
H. Peter Anvine2c80182005-01-15 22:15:51 +00002663 if (tokval.t_type)
2664 error(ERR_WARNING,
2665 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00002666
H. Peter Anvine2c80182005-01-15 22:15:51 +00002667 if (!is_simple(evalresult)) {
2668 error(ERR_NONFATAL,
2669 "non-constant value given to `%%%sassign'",
2670 (i == PP_IASSIGN ? "i" : ""));
2671 free_tlist(origline);
2672 return DIRECTIVE_FOUND;
2673 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002674
H. Peter Anvine2c80182005-01-15 22:15:51 +00002675 macro_start = nasm_malloc(sizeof(*macro_start));
2676 macro_start->next = NULL;
2677 make_tok_num(macro_start, reloc_value(evalresult));
2678 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002679
H. Peter Anvine2c80182005-01-15 22:15:51 +00002680 /*
2681 * We now have a macro name, an implicit parameter count of
2682 * zero, and a numeric token to use as an expansion. Create
2683 * and store an SMacro.
2684 */
2685 if (smacro_defined(ctx, mname, 0, &smac, i == PP_ASSIGN)) {
2686 if (!smac)
2687 error(ERR_WARNING,
2688 "single-line macro `%s' defined both with and"
2689 " without parameters", mname);
2690 else {
2691 /*
2692 * We're redefining, so we have to take over an
2693 * existing SMacro structure. This means freeing
2694 * what was already in it.
2695 */
2696 nasm_free(smac->name);
2697 free_tlist(smac->expansion);
2698 }
2699 } else {
H. Peter Anvin97a23472007-09-16 17:57:25 -07002700 if (!ctx)
2701 smhead = (SMacro **) hash_findi_add(smacros, mname);
2702 else
2703 smhead = &ctx->localmac;
2704
H. Peter Anvine2c80182005-01-15 22:15:51 +00002705 smac = nasm_malloc(sizeof(SMacro));
2706 smac->next = *smhead;
2707 *smhead = smac;
2708 }
2709 smac->name = nasm_strdup(mname);
2710 smac->casesense = (i == PP_ASSIGN);
2711 smac->nparam = 0;
2712 smac->expansion = macro_start;
2713 smac->in_progress = FALSE;
2714 free_tlist(origline);
2715 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002716
H. Peter Anvine2c80182005-01-15 22:15:51 +00002717 case PP_LINE:
2718 /*
2719 * Syntax is `%line nnn[+mmm] [filename]'
2720 */
2721 tline = tline->next;
2722 skip_white_(tline);
2723 if (!tok_type_(tline, TOK_NUMBER)) {
2724 error(ERR_NONFATAL, "`%%line' expects line number");
2725 free_tlist(origline);
2726 return DIRECTIVE_FOUND;
2727 }
2728 k = readnum(tline->text, &j);
2729 m = 1;
2730 tline = tline->next;
2731 if (tok_is_(tline, "+")) {
2732 tline = tline->next;
2733 if (!tok_type_(tline, TOK_NUMBER)) {
2734 error(ERR_NONFATAL, "`%%line' expects line increment");
2735 free_tlist(origline);
2736 return DIRECTIVE_FOUND;
2737 }
2738 m = readnum(tline->text, &j);
2739 tline = tline->next;
2740 }
2741 skip_white_(tline);
2742 src_set_linnum(k);
2743 istk->lineinc = m;
2744 if (tline) {
2745 nasm_free(src_set_fname(detoken(tline, FALSE)));
2746 }
2747 free_tlist(origline);
2748 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002749
H. Peter Anvine2c80182005-01-15 22:15:51 +00002750 default:
2751 error(ERR_FATAL,
2752 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00002753 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002754 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002755 }
Ed Beroset3ab3f412002-06-11 03:31:49 +00002756 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002757}
2758
2759/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002760 * Ensure that a macro parameter contains a condition code and
2761 * nothing else. Return the condition code index if so, or -1
2762 * otherwise.
2763 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002764static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002765{
H. Peter Anvin76690a12002-04-30 20:52:49 +00002766 Token *tt;
2767 int i, j, k, m;
2768
H. Peter Anvin25a99342007-09-22 17:45:45 -07002769 if (!t)
2770 return -1; /* Probably a %+ without a space */
2771
H. Peter Anvineba20a72002-04-30 20:53:55 +00002772 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002773 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002774 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002775 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002776 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002777 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002778 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002779
2780 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00002781 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002782 while (j - i > 1) {
2783 k = (j + i) / 2;
2784 m = nasm_stricmp(t->text, conditions[k]);
2785 if (m == 0) {
2786 i = k;
2787 j = -2;
2788 break;
2789 } else if (m < 0) {
2790 j = k;
2791 } else
2792 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002793 }
2794 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002795 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002796 return i;
2797}
2798
2799/*
2800 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2801 * %-n) and MMacro-local identifiers (%%foo).
2802 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002803static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002804{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002805 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002806
2807 tail = &thead;
2808 thead = NULL;
2809
H. Peter Anvine2c80182005-01-15 22:15:51 +00002810 while (tline) {
2811 if (tline->type == TOK_PREPROC_ID &&
2812 (((tline->text[1] == '+' || tline->text[1] == '-')
2813 && tline->text[2]) || tline->text[1] == '%'
2814 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002815 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002816 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002817 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07002818 unsigned int n;
2819 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002820 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002821
H. Peter Anvine2c80182005-01-15 22:15:51 +00002822 t = tline;
2823 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002824
H. Peter Anvine2c80182005-01-15 22:15:51 +00002825 mac = istk->mstk;
2826 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2827 mac = mac->next_active;
2828 if (!mac)
2829 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2830 else
2831 switch (t->text[1]) {
2832 /*
2833 * We have to make a substitution of one of the
2834 * forms %1, %-1, %+1, %%foo, %0.
2835 */
2836 case '0':
2837 type = TOK_NUMBER;
2838 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
2839 text = nasm_strdup(tmpbuf);
2840 break;
2841 case '%':
2842 type = TOK_ID;
Keith Kanios93f2e9a2007-04-14 00:10:59 +00002843 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu32".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002844 mac->unique);
2845 text = nasm_strcat(tmpbuf, t->text + 2);
2846 break;
2847 case '-':
2848 n = atoi(t->text + 2) - 1;
2849 if (n >= mac->nparam)
2850 tt = NULL;
2851 else {
2852 if (mac->nparam > 1)
2853 n = (n + mac->rotate) % mac->nparam;
2854 tt = mac->params[n];
2855 }
2856 cc = find_cc(tt);
2857 if (cc == -1) {
2858 error(ERR_NONFATAL,
2859 "macro parameter %d is not a condition code",
2860 n + 1);
2861 text = NULL;
2862 } else {
2863 type = TOK_ID;
2864 if (inverse_ccs[cc] == -1) {
2865 error(ERR_NONFATAL,
2866 "condition code `%s' is not invertible",
2867 conditions[cc]);
2868 text = NULL;
2869 } else
2870 text =
2871 nasm_strdup(conditions[inverse_ccs[cc]]);
2872 }
2873 break;
2874 case '+':
2875 n = atoi(t->text + 2) - 1;
2876 if (n >= mac->nparam)
2877 tt = NULL;
2878 else {
2879 if (mac->nparam > 1)
2880 n = (n + mac->rotate) % mac->nparam;
2881 tt = mac->params[n];
2882 }
2883 cc = find_cc(tt);
2884 if (cc == -1) {
2885 error(ERR_NONFATAL,
2886 "macro parameter %d is not a condition code",
2887 n + 1);
2888 text = NULL;
2889 } else {
2890 type = TOK_ID;
2891 text = nasm_strdup(conditions[cc]);
2892 }
2893 break;
2894 default:
2895 n = atoi(t->text + 1) - 1;
2896 if (n >= mac->nparam)
2897 tt = NULL;
2898 else {
2899 if (mac->nparam > 1)
2900 n = (n + mac->rotate) % mac->nparam;
2901 tt = mac->params[n];
2902 }
2903 if (tt) {
2904 for (i = 0; i < mac->paramlen[n]; i++) {
2905 *tail = new_Token(NULL, tt->type, tt->text, 0);
2906 tail = &(*tail)->next;
2907 tt = tt->next;
2908 }
2909 }
2910 text = NULL; /* we've done it here */
2911 break;
2912 }
2913 if (!text) {
2914 delete_Token(t);
2915 } else {
2916 *tail = t;
2917 tail = &t->next;
2918 t->type = type;
2919 nasm_free(t->text);
2920 t->text = text;
2921 t->mac = NULL;
2922 }
2923 continue;
2924 } else {
2925 t = *tail = tline;
2926 tline = tline->next;
2927 t->mac = NULL;
2928 tail = &t->next;
2929 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002930 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00002931 *tail = NULL;
2932 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002933 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002934 switch (t->type) {
2935 case TOK_WHITESPACE:
2936 if (tt->type == TOK_WHITESPACE) {
2937 t->next = delete_Token(tt);
2938 }
2939 break;
2940 case TOK_ID:
2941 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002942 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002943 nasm_free(t->text);
2944 t->text = tmp;
2945 t->next = delete_Token(tt);
2946 }
2947 break;
2948 case TOK_NUMBER:
2949 if (tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002950 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002951 nasm_free(t->text);
2952 t->text = tmp;
2953 t->next = delete_Token(tt);
2954 }
2955 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002956 default:
2957 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002958 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002959
H. Peter Anvin76690a12002-04-30 20:52:49 +00002960 return thead;
2961}
2962
2963/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002964 * Expand all single-line macro calls made in the given line.
2965 * Return the expanded version of the line. The original is deemed
2966 * to be destroyed in the process. (In reality we'll just move
2967 * Tokens from input to output a lot of the time, rather than
2968 * actually bothering to destroy and replicate.)
2969 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002970static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002971{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002972 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002973 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002974 Token **params;
2975 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07002976 unsigned int nparam, sparam;
2977 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002978 Token *org_tline = tline;
2979 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002980 char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002981
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002982 /*
2983 * Trick: we should avoid changing the start token pointer since it can
2984 * be contained in "next" field of other token. Because of this
2985 * we allocate a copy of first token and work with it; at the end of
2986 * routine we copy it back
2987 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002988 if (org_tline) {
2989 tline =
2990 new_Token(org_tline->next, org_tline->type, org_tline->text,
2991 0);
2992 tline->mac = org_tline->mac;
2993 nasm_free(org_tline->text);
2994 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002995 }
2996
H. Peter Anvin734b1882002-04-30 21:01:08 +00002997 again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002998 tail = &thead;
2999 thead = NULL;
3000
H. Peter Anvine2c80182005-01-15 22:15:51 +00003001 while (tline) { /* main token loop */
3002 if ((mname = tline->text)) {
3003 /* if this token is a local macro, look in local context */
3004 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
3005 ctx = get_ctx(mname, TRUE);
3006 else
3007 ctx = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003008 if (!ctx) {
3009 head = (SMacro *) hash_findix(smacros, mname);
3010 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003011 head = ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003012 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003013 /*
3014 * We've hit an identifier. As in is_mmacro below, we first
3015 * check whether the identifier is a single-line macro at
3016 * all, then think about checking for parameters if
3017 * necessary.
3018 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003019 for (m = head; m; m = m->next)
3020 if (!mstrcmp(m->name, mname, m->casesense))
3021 break;
3022 if (m) {
3023 mstart = tline;
3024 params = NULL;
3025 paramsize = NULL;
3026 if (m->nparam == 0) {
3027 /*
3028 * Simple case: the macro is parameterless. Discard the
3029 * one token that the macro call took, and push the
3030 * expansion back on the to-do stack.
3031 */
3032 if (!m->expansion) {
3033 if (!strcmp("__FILE__", m->name)) {
3034 int32_t num = 0;
3035 src_get(&num, &(tline->text));
3036 nasm_quote(&(tline->text));
3037 tline->type = TOK_STRING;
3038 continue;
3039 }
3040 if (!strcmp("__LINE__", m->name)) {
3041 nasm_free(tline->text);
3042 make_tok_num(tline, src_get_linnum());
3043 continue;
3044 }
3045 if (!strcmp("__BITS__", m->name)) {
3046 nasm_free(tline->text);
3047 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003048 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003049 }
3050 tline = delete_Token(tline);
3051 continue;
3052 }
3053 } else {
3054 /*
3055 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003056 * exists and takes parameters. We must find the
3057 * parameters in the call, count them, find the SMacro
3058 * that corresponds to that form of the macro call, and
3059 * substitute for the parameters when we expand. What a
3060 * pain.
3061 */
3062 /*tline = tline->next;
3063 skip_white_(tline); */
3064 do {
3065 t = tline->next;
3066 while (tok_type_(t, TOK_SMAC_END)) {
3067 t->mac->in_progress = FALSE;
3068 t->text = NULL;
3069 t = tline->next = delete_Token(t);
3070 }
3071 tline = t;
3072 } while (tok_type_(tline, TOK_WHITESPACE));
3073 if (!tok_is_(tline, "(")) {
3074 /*
3075 * This macro wasn't called with parameters: ignore
3076 * the call. (Behaviour borrowed from gnu cpp.)
3077 */
3078 tline = mstart;
3079 m = NULL;
3080 } else {
3081 int paren = 0;
3082 int white = 0;
3083 brackets = 0;
3084 nparam = 0;
3085 sparam = PARAM_DELTA;
3086 params = nasm_malloc(sparam * sizeof(Token *));
3087 params[0] = tline->next;
3088 paramsize = nasm_malloc(sparam * sizeof(int));
3089 paramsize[0] = 0;
3090 while (TRUE) { /* parameter loop */
3091 /*
3092 * For some unusual expansions
3093 * which concatenates function call
3094 */
3095 t = tline->next;
3096 while (tok_type_(t, TOK_SMAC_END)) {
3097 t->mac->in_progress = FALSE;
3098 t->text = NULL;
3099 t = tline->next = delete_Token(t);
3100 }
3101 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003102
H. Peter Anvine2c80182005-01-15 22:15:51 +00003103 if (!tline) {
3104 error(ERR_NONFATAL,
3105 "macro call expects terminating `)'");
3106 break;
3107 }
3108 if (tline->type == TOK_WHITESPACE
3109 && brackets <= 0) {
3110 if (paramsize[nparam])
3111 white++;
3112 else
3113 params[nparam] = tline->next;
3114 continue; /* parameter loop */
3115 }
3116 if (tline->type == TOK_OTHER
3117 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003118 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003119 if (ch == ',' && !paren && brackets <= 0) {
3120 if (++nparam >= sparam) {
3121 sparam += PARAM_DELTA;
3122 params = nasm_realloc(params,
3123 sparam *
3124 sizeof(Token
3125 *));
3126 paramsize =
3127 nasm_realloc(paramsize,
3128 sparam *
3129 sizeof(int));
3130 }
3131 params[nparam] = tline->next;
3132 paramsize[nparam] = 0;
3133 white = 0;
3134 continue; /* parameter loop */
3135 }
3136 if (ch == '{' &&
3137 (brackets > 0 || (brackets == 0 &&
3138 !paramsize[nparam])))
3139 {
3140 if (!(brackets++)) {
3141 params[nparam] = tline->next;
3142 continue; /* parameter loop */
3143 }
3144 }
3145 if (ch == '}' && brackets > 0)
3146 if (--brackets == 0) {
3147 brackets = -1;
3148 continue; /* parameter loop */
3149 }
3150 if (ch == '(' && !brackets)
3151 paren++;
3152 if (ch == ')' && brackets <= 0)
3153 if (--paren < 0)
3154 break;
3155 }
3156 if (brackets < 0) {
3157 brackets = 0;
3158 error(ERR_NONFATAL, "braces do not "
3159 "enclose all of macro parameter");
3160 }
3161 paramsize[nparam] += white + 1;
3162 white = 0;
3163 } /* parameter loop */
3164 nparam++;
3165 while (m && (m->nparam != nparam ||
3166 mstrcmp(m->name, mname,
3167 m->casesense)))
3168 m = m->next;
3169 if (!m)
3170 error(ERR_WARNING | ERR_WARN_MNP,
3171 "macro `%s' exists, "
3172 "but not taking %d parameters",
3173 mstart->text, nparam);
3174 }
3175 }
3176 if (m && m->in_progress)
3177 m = NULL;
3178 if (!m) { /* in progess or didn't find '(' or wrong nparam */
3179 /*
3180 * Design question: should we handle !tline, which
3181 * indicates missing ')' here, or expand those
3182 * macros anyway, which requires the (t) test a few
3183 * lines down?
3184 */
3185 nasm_free(params);
3186 nasm_free(paramsize);
3187 tline = mstart;
3188 } else {
3189 /*
3190 * Expand the macro: we are placed on the last token of the
3191 * call, so that we can easily split the call from the
3192 * following tokens. We also start by pushing an SMAC_END
3193 * token for the cycle removal.
3194 */
3195 t = tline;
3196 if (t) {
3197 tline = t->next;
3198 t->next = NULL;
3199 }
3200 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3201 tt->mac = m;
3202 m->in_progress = TRUE;
3203 tline = tt;
3204 for (t = m->expansion; t; t = t->next) {
3205 if (t->type >= TOK_SMAC_PARAM) {
3206 Token *pcopy = tline, **ptail = &pcopy;
3207 Token *ttt, *pt;
3208 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003209
H. Peter Anvine2c80182005-01-15 22:15:51 +00003210 ttt = params[t->type - TOK_SMAC_PARAM];
3211 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3212 --i >= 0;) {
3213 pt = *ptail =
3214 new_Token(tline, ttt->type, ttt->text,
3215 0);
3216 ptail = &pt->next;
3217 ttt = ttt->next;
3218 }
3219 tline = pcopy;
3220 } else {
3221 tt = new_Token(tline, t->type, t->text, 0);
3222 tline = tt;
3223 }
3224 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003225
H. Peter Anvine2c80182005-01-15 22:15:51 +00003226 /*
3227 * Having done that, get rid of the macro call, and clean
3228 * up the parameters.
3229 */
3230 nasm_free(params);
3231 nasm_free(paramsize);
3232 free_tlist(mstart);
3233 continue; /* main token loop */
3234 }
3235 }
3236 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003237
H. Peter Anvine2c80182005-01-15 22:15:51 +00003238 if (tline->type == TOK_SMAC_END) {
3239 tline->mac->in_progress = FALSE;
3240 tline = delete_Token(tline);
3241 } else {
3242 t = *tail = tline;
3243 tline = tline->next;
3244 t->mac = NULL;
3245 t->next = NULL;
3246 tail = &t->next;
3247 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003248 }
3249
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003250 /*
3251 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003252 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003253 * TOK_IDs should be concatenated.
3254 * Also we look for %+ tokens and concatenate the tokens before and after
3255 * them (without white spaces in between).
3256 */
3257 t = thead;
3258 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003259 while (t) {
3260 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3261 t = t->next;
3262 if (!t || !t->next)
3263 break;
3264 if (t->next->type == TOK_ID ||
3265 t->next->type == TOK_PREPROC_ID ||
3266 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003267 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003268 nasm_free(t->text);
3269 t->next = delete_Token(t->next);
3270 t->text = p;
3271 rescan = 1;
3272 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3273 t->next->next->type == TOK_PREPROC_ID &&
3274 strcmp(t->next->next->text, "%+") == 0) {
3275 /* free the next whitespace, the %+ token and next whitespace */
3276 int i;
3277 for (i = 1; i <= 3; i++) {
3278 if (!t->next
3279 || (i != 2 && t->next->type != TOK_WHITESPACE))
3280 break;
3281 t->next = delete_Token(t->next);
3282 } /* endfor */
3283 } else
3284 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003285 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003286 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003287 if (rescan) {
3288 tline = thead;
3289 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003290 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003291
H. Peter Anvine2c80182005-01-15 22:15:51 +00003292 if (org_tline) {
3293 if (thead) {
3294 *org_tline = *thead;
3295 /* since we just gave text to org_line, don't free it */
3296 thead->text = NULL;
3297 delete_Token(thead);
3298 } else {
3299 /* the expression expanded to empty line;
3300 we can't return NULL for some reasons
3301 we just set the line to a single WHITESPACE token. */
3302 memset(org_tline, 0, sizeof(*org_tline));
3303 org_tline->text = NULL;
3304 org_tline->type = TOK_WHITESPACE;
3305 }
3306 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003307 }
3308
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003309 return thead;
3310}
3311
3312/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003313 * Similar to expand_smacro but used exclusively with macro identifiers
3314 * right before they are fetched in. The reason is that there can be
3315 * identifiers consisting of several subparts. We consider that if there
3316 * are more than one element forming the name, user wants a expansion,
3317 * otherwise it will be left as-is. Example:
3318 *
3319 * %define %$abc cde
3320 *
3321 * the identifier %$abc will be left as-is so that the handler for %define
3322 * will suck it and define the corresponding value. Other case:
3323 *
3324 * %define _%$abc cde
3325 *
3326 * In this case user wants name to be expanded *before* %define starts
3327 * working, so we'll expand %$abc into something (if it has a value;
3328 * otherwise it will be left as-is) then concatenate all successive
3329 * PP_IDs into one.
3330 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003331static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003332{
3333 Token *cur, *oldnext = NULL;
3334
H. Peter Anvin734b1882002-04-30 21:01:08 +00003335 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003336 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003337
3338 cur = tline;
3339 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003340 (cur->next->type == TOK_ID ||
3341 cur->next->type == TOK_PREPROC_ID
3342 || cur->next->type == TOK_NUMBER))
3343 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003344
3345 /* If identifier consists of just one token, don't expand */
3346 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003347 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003348
H. Peter Anvine2c80182005-01-15 22:15:51 +00003349 if (cur) {
3350 oldnext = cur->next; /* Detach the tail past identifier */
3351 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003352 }
3353
H. Peter Anvin734b1882002-04-30 21:01:08 +00003354 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003355
H. Peter Anvine2c80182005-01-15 22:15:51 +00003356 if (cur) {
3357 /* expand_smacro possibly changhed tline; re-scan for EOL */
3358 cur = tline;
3359 while (cur && cur->next)
3360 cur = cur->next;
3361 if (cur)
3362 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003363 }
3364
3365 return tline;
3366}
3367
3368/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003369 * Determine whether the given line constitutes a multi-line macro
3370 * call, and return the MMacro structure called if so. Doesn't have
3371 * to check for an initial label - that's taken care of in
3372 * expand_mmacro - but must check numbers of parameters. Guaranteed
3373 * to be called with tline->type == TOK_ID, so the putative macro
3374 * name is easy to find.
3375 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003376static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003377{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003378 MMacro *head, *m;
3379 Token **params;
3380 int nparam;
3381
H. Peter Anvin97a23472007-09-16 17:57:25 -07003382 head = (MMacro *) hash_findix(mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003383
3384 /*
3385 * Efficiency: first we see if any macro exists with the given
3386 * name. If not, we can return NULL immediately. _Then_ we
3387 * count the parameters, and then we look further along the
3388 * list if necessary to find the proper MMacro.
3389 */
3390 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003391 if (!mstrcmp(m->name, tline->text, m->casesense))
3392 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003393 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003394 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003395
3396 /*
3397 * OK, we have a potential macro. Count and demarcate the
3398 * parameters.
3399 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003400 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003401
3402 /*
3403 * So we know how many parameters we've got. Find the MMacro
3404 * structure that handles this number.
3405 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003406 while (m) {
3407 if (m->nparam_min <= nparam
3408 && (m->plus || nparam <= m->nparam_max)) {
3409 /*
3410 * This one is right. Just check if cycle removal
3411 * prohibits us using it before we actually celebrate...
3412 */
3413 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003414#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003415 error(ERR_NONFATAL,
3416 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003417#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003418 nasm_free(params);
3419 return NULL;
3420 }
3421 /*
3422 * It's right, and we can use it. Add its default
3423 * parameters to the end of our list if necessary.
3424 */
3425 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3426 params =
3427 nasm_realloc(params,
3428 ((m->nparam_min + m->ndefs +
3429 1) * sizeof(*params)));
3430 while (nparam < m->nparam_min + m->ndefs) {
3431 params[nparam] = m->defaults[nparam - m->nparam_min];
3432 nparam++;
3433 }
3434 }
3435 /*
3436 * If we've gone over the maximum parameter count (and
3437 * we're in Plus mode), ignore parameters beyond
3438 * nparam_max.
3439 */
3440 if (m->plus && nparam > m->nparam_max)
3441 nparam = m->nparam_max;
3442 /*
3443 * Then terminate the parameter list, and leave.
3444 */
3445 if (!params) { /* need this special case */
3446 params = nasm_malloc(sizeof(*params));
3447 nparam = 0;
3448 }
3449 params[nparam] = NULL;
3450 *params_array = params;
3451 return m;
3452 }
3453 /*
3454 * This one wasn't right: look for the next one with the
3455 * same name.
3456 */
3457 for (m = m->next; m; m = m->next)
3458 if (!mstrcmp(m->name, tline->text, m->casesense))
3459 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003460 }
3461
3462 /*
3463 * After all that, we didn't find one with the right number of
3464 * parameters. Issue a warning, and fail to expand the macro.
3465 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003466 error(ERR_WARNING | ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003467 "macro `%s' exists, but not taking %d parameters",
3468 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003469 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003470 return NULL;
3471}
3472
3473/*
3474 * Expand the multi-line macro call made by the given line, if
3475 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003476 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003477 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003478static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003479{
3480 Token *startline = tline;
3481 Token *label = NULL;
3482 int dont_prepend = 0;
3483 Token **params, *t, *tt;
3484 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003485 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003486 int i, nparam, *paramlen;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003487
3488 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003489 skip_white_(t);
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003490/* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3491 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003492 return 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003493 m = is_mmacro(t, &params);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003494 if (!m) {
3495 Token *last;
3496 /*
3497 * We have an id which isn't a macro call. We'll assume
3498 * it might be a label; we'll also check to see if a
3499 * colon follows it. Then, if there's another id after
3500 * that lot, we'll check it again for macro-hood.
3501 */
3502 label = last = t;
3503 t = t->next;
3504 if (tok_type_(t, TOK_WHITESPACE))
3505 last = t, t = t->next;
3506 if (tok_is_(t, ":")) {
3507 dont_prepend = 1;
3508 last = t, t = t->next;
3509 if (tok_type_(t, TOK_WHITESPACE))
3510 last = t, t = t->next;
3511 }
3512 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3513 return 0;
3514 last->next = NULL;
3515 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003516 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003517
3518 /*
3519 * Fix up the parameters: this involves stripping leading and
3520 * trailing whitespace, then stripping braces if they are
3521 * present.
3522 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003523 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003524 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003525
H. Peter Anvine2c80182005-01-15 22:15:51 +00003526 for (i = 0; params[i]; i++) {
3527 int brace = FALSE;
3528 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003529
H. Peter Anvine2c80182005-01-15 22:15:51 +00003530 t = params[i];
3531 skip_white_(t);
3532 if (tok_is_(t, "{"))
3533 t = t->next, brace = TRUE, comma = FALSE;
3534 params[i] = t;
3535 paramlen[i] = 0;
3536 while (t) {
3537 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3538 break; /* ... because we have hit a comma */
3539 if (comma && t->type == TOK_WHITESPACE
3540 && tok_is_(t->next, ","))
3541 break; /* ... or a space then a comma */
3542 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3543 break; /* ... or a brace */
3544 t = t->next;
3545 paramlen[i]++;
3546 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003547 }
3548
3549 /*
3550 * OK, we have a MMacro structure together with a set of
3551 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003552 * copies of each Line on to istk->expansion. Substitution of
3553 * parameter tokens and macro-local tokens doesn't get done
3554 * until the single-line macro substitution process; this is
3555 * because delaying them allows us to change the semantics
3556 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003557 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003558 * First, push an end marker on to istk->expansion, mark this
3559 * macro as in progress, and set up its invocation-specific
3560 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003561 */
3562 ll = nasm_malloc(sizeof(Line));
3563 ll->next = istk->expansion;
3564 ll->finishes = m;
3565 ll->first = NULL;
3566 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003567
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003568 m->in_progress = TRUE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003569 m->params = params;
3570 m->iline = tline;
3571 m->nparam = nparam;
3572 m->rotate = 0;
3573 m->paramlen = paramlen;
3574 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003575 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003576
3577 m->next_active = istk->mstk;
3578 istk->mstk = m;
3579
H. Peter Anvine2c80182005-01-15 22:15:51 +00003580 for (l = m->expansion; l; l = l->next) {
3581 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003582
H. Peter Anvine2c80182005-01-15 22:15:51 +00003583 ll = nasm_malloc(sizeof(Line));
3584 ll->finishes = NULL;
3585 ll->next = istk->expansion;
3586 istk->expansion = ll;
3587 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003588
H. Peter Anvine2c80182005-01-15 22:15:51 +00003589 for (t = l->first; t; t = t->next) {
3590 Token *x = t;
3591 if (t->type == TOK_PREPROC_ID &&
3592 t->text[1] == '0' && t->text[2] == '0') {
3593 dont_prepend = -1;
3594 x = label;
3595 if (!x)
3596 continue;
3597 }
3598 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3599 tail = &tt->next;
3600 }
3601 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003602 }
3603
3604 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00003605 * If we had a label, push it on as the first line of
3606 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003607 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003608 if (label) {
3609 if (dont_prepend < 0)
3610 free_tlist(startline);
3611 else {
3612 ll = nasm_malloc(sizeof(Line));
3613 ll->finishes = NULL;
3614 ll->next = istk->expansion;
3615 istk->expansion = ll;
3616 ll->first = startline;
3617 if (!dont_prepend) {
3618 while (label->next)
3619 label = label->next;
3620 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3621 }
3622 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003623 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003624
H. Peter Anvin734b1882002-04-30 21:01:08 +00003625 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003626
H. Peter Anvineba20a72002-04-30 20:53:55 +00003627 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003628}
3629
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003630/*
3631 * Since preprocessor always operate only on the line that didn't
3632 * arrived yet, we should always use ERR_OFFBY1. Also since user
3633 * won't want to see same error twice (preprocessing is done once
3634 * per pass) we will want to show errors only during pass one.
3635 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003636static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003637{
3638 va_list arg;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003639 char buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003640
3641 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003642 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003643 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003644
H. Peter Anvin734b1882002-04-30 21:01:08 +00003645 va_start(arg, fmt);
Ed Beroset19f927a2004-12-15 17:07:03 +00003646 vsnprintf(buff, sizeof(buff), fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003647 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003648
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003649 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003650 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3651 istk->mstk->lineno, buff);
3652 else
3653 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003654}
3655
H. Peter Anvin734b1882002-04-30 21:01:08 +00003656static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003657pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003658 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003659{
H. Peter Anvin99941bf2002-05-14 17:44:03 +00003660 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003661 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003662 istk = nasm_malloc(sizeof(Include));
3663 istk->next = NULL;
3664 istk->conds = NULL;
3665 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003666 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003667 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00003668 istk->fname = NULL;
3669 src_set_fname(nasm_strdup(file));
3670 src_set_linnum(0);
3671 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003672 if (!istk->fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003673 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3674 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003675 defining = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003676 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003677 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003678 if (tasm_compatible_mode) {
3679 stdmacpos = stdmac;
3680 } else {
3681 stdmacpos = &stdmac[TASM_MACRO_COUNT];
3682 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003683 any_extrastdmac = (extrastdmac != NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003684 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003685 evaluate = eval;
3686 pass = apass;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003687}
3688
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003689static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003690{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003691 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003692 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003693
H. Peter Anvine2c80182005-01-15 22:15:51 +00003694 while (1) {
3695 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003696 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00003697 * buffer or from the input file.
3698 */
3699 tline = NULL;
3700 while (istk->expansion && istk->expansion->finishes) {
3701 Line *l = istk->expansion;
3702 if (!l->finishes->name && l->finishes->in_progress > 1) {
3703 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003704
H. Peter Anvine2c80182005-01-15 22:15:51 +00003705 /*
3706 * This is a macro-end marker for a macro with no
3707 * name, which means it's not really a macro at all
3708 * but a %rep block, and the `in_progress' field is
3709 * more than 1, meaning that we still need to
3710 * repeat. (1 means the natural last repetition; 0
3711 * means termination by %exitrep.) We have
3712 * therefore expanded up to the %endrep, and must
3713 * push the whole block on to the expansion buffer
3714 * again. We don't bother to remove the macro-end
3715 * marker: we'd only have to generate another one
3716 * if we did.
3717 */
3718 l->finishes->in_progress--;
3719 for (l = l->finishes->expansion; l; l = l->next) {
3720 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003721
H. Peter Anvine2c80182005-01-15 22:15:51 +00003722 ll = nasm_malloc(sizeof(Line));
3723 ll->next = istk->expansion;
3724 ll->finishes = NULL;
3725 ll->first = NULL;
3726 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003727
H. Peter Anvine2c80182005-01-15 22:15:51 +00003728 for (t = l->first; t; t = t->next) {
3729 if (t->text || t->type == TOK_WHITESPACE) {
3730 tt = *tail =
3731 new_Token(NULL, t->type, t->text, 0);
3732 tail = &tt->next;
3733 }
3734 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003735
H. Peter Anvine2c80182005-01-15 22:15:51 +00003736 istk->expansion = ll;
3737 }
3738 } else {
3739 /*
3740 * Check whether a `%rep' was started and not ended
3741 * within this macro expansion. This can happen and
3742 * should be detected. It's a fatal error because
3743 * I'm too confused to work out how to recover
3744 * sensibly from it.
3745 */
3746 if (defining) {
3747 if (defining->name)
3748 error(ERR_PANIC,
3749 "defining with name in expansion");
3750 else if (istk->mstk->name)
3751 error(ERR_FATAL,
3752 "`%%rep' without `%%endrep' within"
3753 " expansion of macro `%s'",
3754 istk->mstk->name);
3755 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00003756
H. Peter Anvine2c80182005-01-15 22:15:51 +00003757 /*
3758 * FIXME: investigate the relationship at this point between
3759 * istk->mstk and l->finishes
3760 */
3761 {
3762 MMacro *m = istk->mstk;
3763 istk->mstk = m->next_active;
3764 if (m->name) {
3765 /*
3766 * This was a real macro call, not a %rep, and
3767 * therefore the parameter information needs to
3768 * be freed.
3769 */
3770 nasm_free(m->params);
3771 free_tlist(m->iline);
3772 nasm_free(m->paramlen);
3773 l->finishes->in_progress = FALSE;
3774 } else
3775 free_mmacro(m);
3776 }
3777 istk->expansion = l->next;
3778 nasm_free(l);
3779 list->downlevel(LIST_MACRO);
3780 }
3781 }
3782 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003783
H. Peter Anvine2c80182005-01-15 22:15:51 +00003784 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003785 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003786 Line *l = istk->expansion;
3787 if (istk->mstk)
3788 istk->mstk->lineno++;
3789 tline = l->first;
3790 istk->expansion = l->next;
3791 nasm_free(l);
3792 p = detoken(tline, FALSE);
3793 list->line(LIST_MACRO, p);
3794 nasm_free(p);
3795 break;
3796 }
3797 line = read_line();
3798 if (line) { /* from the current input file */
3799 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00003800 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003801 nasm_free(line);
3802 break;
3803 }
3804 /*
3805 * The current file has ended; work down the istk
3806 */
3807 {
3808 Include *i = istk;
3809 fclose(i->fp);
3810 if (i->conds)
3811 error(ERR_FATAL,
3812 "expected `%%endif' before end of file");
3813 /* only set line and file name if there's a next node */
3814 if (i->next) {
3815 src_set_linnum(i->lineno);
3816 nasm_free(src_set_fname(i->fname));
3817 }
3818 istk = i->next;
3819 list->downlevel(LIST_INCLUDE);
3820 nasm_free(i);
3821 if (!istk)
3822 return NULL;
3823 }
3824 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003825
H. Peter Anvine2c80182005-01-15 22:15:51 +00003826 /*
3827 * We must expand MMacro parameters and MMacro-local labels
3828 * _before_ we plunge into directive processing, to cope
3829 * with things like `%define something %1' such as STRUC
3830 * uses. Unless we're _defining_ a MMacro, in which case
3831 * those tokens should be left alone to go into the
3832 * definition; and unless we're in a non-emitting
3833 * condition, in which case we don't want to meddle with
3834 * anything.
3835 */
3836 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
3837 tline = expand_mmac_params(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003838
H. Peter Anvine2c80182005-01-15 22:15:51 +00003839 /*
3840 * Check the line to see if it's a preprocessor directive.
3841 */
3842 if (do_directive(tline) == DIRECTIVE_FOUND) {
3843 continue;
3844 } else if (defining) {
3845 /*
3846 * We're defining a multi-line macro. We emit nothing
3847 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00003848 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003849 */
3850 Line *l = nasm_malloc(sizeof(Line));
3851 l->next = defining->expansion;
3852 l->first = tline;
3853 l->finishes = FALSE;
3854 defining->expansion = l;
3855 continue;
3856 } else if (istk->conds && !emitting(istk->conds->state)) {
3857 /*
3858 * We're in a non-emitting branch of a condition block.
3859 * Emit nothing at all, not even a blank line: when we
3860 * emerge from the condition we'll give a line-number
3861 * directive so we keep our place correctly.
3862 */
3863 free_tlist(tline);
3864 continue;
3865 } else if (istk->mstk && !istk->mstk->in_progress) {
3866 /*
3867 * We're in a %rep block which has been terminated, so
3868 * we're walking through to the %endrep without
3869 * emitting anything. Emit nothing at all, not even a
3870 * blank line: when we emerge from the %rep block we'll
3871 * give a line-number directive so we keep our place
3872 * correctly.
3873 */
3874 free_tlist(tline);
3875 continue;
3876 } else {
3877 tline = expand_smacro(tline);
3878 if (!expand_mmacro(tline)) {
3879 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003880 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003881 */
3882 line = detoken(tline, TRUE);
3883 free_tlist(tline);
3884 break;
3885 } else {
3886 continue; /* expand_mmacro calls free_tlist */
3887 }
3888 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003889 }
3890
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003891 return line;
3892}
3893
H. Peter Anvine2c80182005-01-15 22:15:51 +00003894static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003895{
H. Peter Anvine2c80182005-01-15 22:15:51 +00003896 if (defining) {
3897 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
3898 defining->name);
3899 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003900 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003901 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003902 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07003903 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00003904 while (istk) {
3905 Include *i = istk;
3906 istk = istk->next;
3907 fclose(i->fp);
3908 nasm_free(i->fname);
3909 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003910 }
3911 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003912 ctx_pop();
3913 if (pass == 0) {
3914 free_llist(predef);
3915 delete_Blocks();
3916 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003917}
3918
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003919void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003920{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003921 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07003922
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003923 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07003924 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003925 i->next = NULL;
3926
H. Peter Anvine2c80182005-01-15 22:15:51 +00003927 if (ipath != NULL) {
3928 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003929 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003930 j = j->next;
3931 j->next = i;
3932 } else {
3933 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003934 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003935}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003936
3937/*
3938 * added by alexfru:
3939 *
3940 * This function is used to "export" the include paths, e.g.
3941 * the paths specified in the '-I' command switch.
3942 * The need for such exporting is due to the 'incbin' directive,
3943 * which includes raw binary files (unlike '%include', which
3944 * includes text source files). It would be real nice to be
3945 * able to specify paths to search for incbin'ned files also.
3946 * So, this is a simple workaround.
3947 *
3948 * The function use is simple:
3949 *
3950 * The 1st call (with NULL argument) returns a pointer to the 1st path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003951 * (char** type) or NULL if none include paths available.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003952 *
3953 * All subsequent calls take as argument the value returned by this
3954 * function last. The return value is either the next path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003955 * (char** type) or NULL if the end of the paths list is reached.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003956 *
3957 * It is maybe not the best way to do things, but I didn't want
3958 * to export too much, just one or two functions and no types or
3959 * variables exported.
3960 *
3961 * Can't say I like the current situation with e.g. this path list either,
3962 * it seems to be never deallocated after creation...
3963 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003964char **pp_get_include_path_ptr(char **pPrevPath)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003965{
3966/* This macro returns offset of a member of a structure */
3967#define GetMemberOffset(StructType,MemberName)\
3968 ((size_t)&((StructType*)0)->MemberName)
3969 IncPath *i;
3970
H. Peter Anvine2c80182005-01-15 22:15:51 +00003971 if (pPrevPath == NULL) {
3972 if (ipath != NULL)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003973 return &ipath->path;
3974 else
3975 return NULL;
3976 }
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003977 i = (IncPath *) ((char *)pPrevPath - GetMemberOffset(IncPath, path));
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003978 i = i->next;
3979 if (i != NULL)
3980 return &i->path;
3981 else
3982 return NULL;
3983#undef GetMemberOffset
3984}
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003985
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003986void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003987{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003988 Token *inc, *space, *name;
3989 Line *l;
3990
H. Peter Anvin734b1882002-04-30 21:01:08 +00003991 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
3992 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
3993 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003994
3995 l = nasm_malloc(sizeof(Line));
3996 l->next = predef;
3997 l->first = inc;
3998 l->finishes = FALSE;
3999 predef = l;
4000}
4001
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004002void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004003{
4004 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004005 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004006 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004007
4008 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004009 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4010 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004011 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004012 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004013 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004014 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004015 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004016
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004017 l = nasm_malloc(sizeof(Line));
4018 l->next = predef;
4019 l->first = def;
4020 l->finishes = FALSE;
4021 predef = l;
4022}
4023
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004024void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004025{
4026 Token *def, *space;
4027 Line *l;
4028
H. Peter Anvin734b1882002-04-30 21:01:08 +00004029 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4030 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004031 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004032
4033 l = nasm_malloc(sizeof(Line));
4034 l->next = predef;
4035 l->first = def;
4036 l->finishes = FALSE;
4037 predef = l;
4038}
4039
Keith Kaniosb7a89542007-04-12 02:40:54 +00004040/*
4041 * Added by Keith Kanios:
4042 *
4043 * This function is used to assist with "runtime" preprocessor
4044 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4045 *
4046 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4047 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4048 */
4049
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004050void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004051{
4052 Token *def;
4053
4054 def = tokenize(definition);
4055 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4056 free_tlist(def);
4057
4058}
4059
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004060void pp_extra_stdmac(const char **macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004061{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004062 extrastdmac = macros;
4063}
4064
Keith Kaniosb7a89542007-04-12 02:40:54 +00004065static void make_tok_num(Token * tok, int32_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004066{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004067 char numbuf[20];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00004068 snprintf(numbuf, sizeof(numbuf), "%"PRId32"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004069 tok->text = nasm_strdup(numbuf);
4070 tok->type = TOK_NUMBER;
4071}
4072
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004073Preproc nasmpp = {
4074 pp_reset,
4075 pp_getline,
4076 pp_cleanup
4077};