blob: b3e32d1e372d3591b0e0d9d5d8e1f6fa3e18e412 [file] [log] [blame]
H. Peter Anvin65747262002-05-07 00:10:05 +00001/* -*- mode: c; c-file-style: "bsd" -*- */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002/* preproc.c macro preprocessor for the Netwide Assembler
3 *
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the licence given in the file "Licence"
7 * distributed in the NASM archive.
8 *
9 * initial version 18/iii/97 by Simon Tatham
10 */
11
H. Peter Anvin4836e332002-04-30 20:56:43 +000012/* Typical flow of text through preproc
13 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000014 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000015 *
16 * from a macro expansion
17 *
18 * or
19 * {
20 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000021 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000022 * }
23 *
24 * expand_mmac_params is used to expand %1 etc., unless a macro is being
25 * defined or a false conditional is being processed
26 * (%0, %1, %+1, %-1, %%foo
27 *
28 * do_directive checks for directives
29 *
30 * expand_smacro is used to expand single line macros
31 *
32 * expand_mmacro is used to expand multi-line macros
33 *
34 * detoken is used to convert the line back to text
35 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000036
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000037#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000038#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000039#include <stdlib.h>
40#include <stddef.h>
41#include <string.h>
42#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000043#include <limits.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000044#include <inttypes.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000045
46#include "nasm.h"
47#include "nasmlib.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000048#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070049#include "hashtbl.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000050
51typedef struct SMacro SMacro;
52typedef struct MMacro MMacro;
53typedef struct Context Context;
54typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000055typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000056typedef struct Line Line;
57typedef struct Include Include;
58typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000059typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000060
61/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070062 * Note on the storage of both SMacro and MMacros: the hash table
63 * indexes them case-insensitively, and we then have to go through a
64 * linked list of potential case aliases (and, for MMacros, parameter
65 * ranges); this is to preserve the matching semantics of the earlier
66 * code. If the number of case aliases for a specific macro is a
67 * performance issue, you may want to reconsider your coding style.
68 */
69
70/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000071 * Store the definition of a single-line macro.
72 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000073struct SMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000074 SMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +000075 char *name;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000076 int casesense;
H. Peter Anvin25a99342007-09-22 17:45:45 -070077 unsigned int nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000078 int in_progress;
79 Token *expansion;
80};
81
82/*
H. Peter Anvin76690a12002-04-30 20:52:49 +000083 * Store the definition of a multi-line macro. This is also used to
84 * store the interiors of `%rep...%endrep' blocks, which are
85 * effectively self-re-invoking multi-line macros which simply
86 * don't have a name or bother to appear in the hash tables. %rep
87 * blocks are signified by having a NULL `name' field.
88 *
89 * In a MMacro describing a `%rep' block, the `in_progress' field
90 * isn't merely boolean, but gives the number of repeats left to
91 * run.
92 *
93 * The `next' field is used for storing MMacros in hash tables; the
94 * `next_active' field is for stacking them on istk entries.
95 *
96 * When a MMacro is being expanded, `params', `iline', `nparam',
97 * `paramlen', `rotate' and `unique' are local to the invocation.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000098 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000099struct MMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000100 MMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000101 char *name;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000102 int casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -0700103 int nparam_min, nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000104 int plus; /* is the last parameter greedy? */
105 int nolist; /* is this macro listing-inhibited? */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000106 int in_progress;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107 Token *dlist; /* All defaults as one list */
108 Token **defaults; /* Parameter default pointers */
109 int ndefs; /* number of default parameters */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000110 Line *expansion;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000111
112 MMacro *next_active;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000113 MMacro *rep_nest; /* used for nesting %rep */
114 Token **params; /* actual parameters */
115 Token *iline; /* invocation line */
H. Peter Anvin25a99342007-09-22 17:45:45 -0700116 unsigned int nparam, rotate;
117 int *paramlen;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000118 uint32_t unique;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000119 int lineno; /* Current line number on expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000120};
121
122/*
123 * The context stack is composed of a linked list of these.
124 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000125struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000126 Context *next;
127 SMacro *localmac;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000128 char *name;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000129 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000130};
131
132/*
133 * This is the internal form which we break input lines up into.
134 * Typically stored in linked lists.
135 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000136 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
137 * necessarily used as-is, but is intended to denote the number of
138 * the substituted parameter. So in the definition
139 *
140 * %define a(x,y) ( (x) & ~(y) )
141 *
142 * the token representing `x' will have its type changed to
143 * TOK_SMAC_PARAM, but the one representing `y' will be
144 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000145 *
146 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
147 * which doesn't need quotes around it. Used in the pre-include
148 * mechanism as an alternative to trying to find a sensible type of
149 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000150 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000151enum pp_token_type {
152 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
153 TOK_PREPROC_ID, TOK_STRING,
154 TOK_NUMBER, TOK_SMAC_END, TOK_OTHER, TOK_SMAC_PARAM,
155 TOK_INTERNAL_STRING
156};
157
H. Peter Anvine2c80182005-01-15 22:15:51 +0000158struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000159 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000160 char *text;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000161 SMacro *mac; /* associated macro for TOK_SMAC_END */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000162 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000163};
164
165/*
166 * Multi-line macro definitions are stored as a linked list of
167 * these, which is essentially a container to allow several linked
168 * lists of Tokens.
169 *
170 * Note that in this module, linked lists are treated as stacks
171 * wherever possible. For this reason, Lines are _pushed_ on to the
172 * `expansion' field in MMacro structures, so that the linked list,
173 * if walked, would give the macro lines in reverse order; this
174 * means that we can walk the list when expanding a macro, and thus
175 * push the lines on to the `expansion' field in _istk_ in reverse
176 * order (so that when popped back off they are in the right
177 * order). It may seem cockeyed, and it relies on my design having
178 * an even number of steps in, but it works...
179 *
180 * Some of these structures, rather than being actual lines, are
181 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000182 * This is for use in the cycle-tracking and %rep-handling code.
183 * Such structures have `finishes' non-NULL, and `first' NULL. All
184 * others have `finishes' NULL, but `first' may still be NULL if
185 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000186 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000187struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000188 Line *next;
189 MMacro *finishes;
190 Token *first;
191};
192
193/*
194 * To handle an arbitrary level of file inclusion, we maintain a
195 * stack (ie linked list) of these things.
196 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000197struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000198 Include *next;
199 FILE *fp;
200 Cond *conds;
201 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000202 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000203 int lineno, lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000204 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000205};
206
207/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000208 * Include search path. This is simply a list of strings which get
209 * prepended, in turn, to the name of an include file, in an
210 * attempt to find the file if it's not in the current directory.
211 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000212struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000213 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000214 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000215};
216
217/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000218 * Conditional assembly: we maintain a separate stack of these for
219 * each level of file inclusion. (The only reason we keep the
220 * stacks separate is to ensure that a stray `%endif' in a file
221 * included from within the true branch of a `%if' won't terminate
222 * it and cause confusion: instead, rightly, it'll cause an error.)
223 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000224struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000225 Cond *next;
226 int state;
227};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000228enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000229 /*
230 * These states are for use just after %if or %elif: IF_TRUE
231 * means the condition has evaluated to truth so we are
232 * currently emitting, whereas IF_FALSE means we are not
233 * currently emitting but will start doing so if a %else comes
234 * up. In these states, all directives are admissible: %elif,
235 * %else and %endif. (And of course %if.)
236 */
237 COND_IF_TRUE, COND_IF_FALSE,
238 /*
239 * These states come up after a %else: ELSE_TRUE means we're
240 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
241 * any %elif or %else will cause an error.
242 */
243 COND_ELSE_TRUE, COND_ELSE_FALSE,
244 /*
245 * This state means that we're not emitting now, and also that
246 * nothing until %endif will be emitted at all. It's for use in
247 * two circumstances: (i) when we've had our moment of emission
248 * and have now started seeing %elifs, and (ii) when the
249 * condition construct in question is contained within a
250 * non-emitting branch of a larger condition construct.
251 */
252 COND_NEVER
253};
254#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
255
Ed Beroset3ab3f412002-06-11 03:31:49 +0000256/*
257 * These defines are used as the possible return values for do_directive
258 */
259#define NO_DIRECTIVE_FOUND 0
260#define DIRECTIVE_FOUND 1
261
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000262/*
263 * Condition codes. Note that we use c_ prefix not C_ because C_ is
264 * used in nasm.h for the "real" condition codes. At _this_ level,
265 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
266 * ones, so we need a different enum...
267 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000268static const char *conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000269 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
270 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000271 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000272};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000273enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000274 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
275 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 +0000276 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 +0000277};
278static int inverse_ccs[] = {
279 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
280 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 +0000281 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000282};
283
H. Peter Anvin76690a12002-04-30 20:52:49 +0000284/*
285 * Directive names.
286 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000287/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000288static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000289{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000290 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000291}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000292
293/* For TASM compatibility we need to be able to recognise TASM compatible
294 * conditional compilation directives. Using the NASM pre-processor does
295 * not work, so we look for them specifically from the following list and
296 * then jam in the equivalent NASM directive into the input stream.
297 */
298
299#ifndef MAX
300# define MAX(a,b) ( ((a) > (b)) ? (a) : (b))
301#endif
302
H. Peter Anvine2c80182005-01-15 22:15:51 +0000303enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000304 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
305 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
306};
307
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000308static const char *tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000309 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
310 "ifndef", "include", "local"
311};
312
313static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000314static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000315static int ArgOffset = 8;
316static int LocalOffset = 4;
317
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000318static Context *cstk;
319static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000320static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000321
H. Peter Anvine2c80182005-01-15 22:15:51 +0000322static efunc _error; /* Pointer to client-provided error reporting function */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000323static evalfunc evaluate;
324
H. Peter Anvine2c80182005-01-15 22:15:51 +0000325static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000326
Keith Kaniosb7a89542007-04-12 02:40:54 +0000327static uint32_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000328
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000329static Line *predef = NULL;
330
331static ListGen *list;
332
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000333/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000334 * The current set of multi-line macros we have defined.
335 */
H. Peter Anvin97a23472007-09-16 17:57:25 -0700336static struct hash_table *mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000337
338/*
339 * The current set of single-line macros we have defined.
340 */
H. Peter Anvin97a23472007-09-16 17:57:25 -0700341static struct hash_table *smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000342
343/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000344 * The multi-line macro we are currently defining, or the %rep
345 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000346 */
347static MMacro *defining;
348
349/*
350 * The number of macro parameters to allocate space for at a time.
351 */
352#define PARAM_DELTA 16
353
354/*
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000355 * The standard macro set: defined as `static char *stdmac[]'. Also
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000356 * gives our position in the macro set, when we're processing it.
357 */
358#include "macros.c"
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000359static const char **stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000360
361/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000362 * The extra standard macros that come from the object format, if
363 * any.
364 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000365static const char **extrastdmac = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000366int any_extrastdmac;
367
368/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000369 * Tokens are allocated in blocks to improve speed
370 */
371#define TOKEN_BLOCKSIZE 4096
372static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000373struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000374 Blocks *next;
375 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000376};
377
378static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000379
380/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000381 * Forward declarations.
382 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000383static Token *expand_mmac_params(Token * tline);
384static Token *expand_smacro(Token * tline);
385static Token *expand_id(Token * tline);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000386static Context *get_ctx(char *name, int all_contexts);
Keith Kaniosb7a89542007-04-12 02:40:54 +0000387static void make_tok_num(Token * tok, int32_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000388static void error(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000389static void *new_Block(size_t size);
390static void delete_Blocks(void);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000391static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000392static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000393
394/*
395 * Macros for safe checking of token pointers, avoid *(NULL)
396 */
397#define tok_type_(x,t) ((x) && (x)->type == (t))
398#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
399#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
400#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000401
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000402/* Handle TASM specific directives, which do not contain a % in
403 * front of them. We do it here because I could not find any other
404 * place to do it for the moment, and it is a hack (ideally it would
405 * be nice to be able to use the NASM pre-processor to do it).
406 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000407static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000408{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000409 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000410 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000411
412 /* Skip whitespace */
413 while (isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000414 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000415
416 /* Binary search for the directive name */
417 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000418 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000419 len = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000420 while (!isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000421 len++;
422 if (len) {
423 oldchar = p[len];
424 p[len] = 0;
425 while (j - i > 1) {
426 k = (j + i) / 2;
427 m = nasm_stricmp(p, tasm_directives[k]);
428 if (m == 0) {
429 /* We have found a directive, so jam a % in front of it
430 * so that NASM will then recognise it as one if it's own.
431 */
432 p[len] = oldchar;
433 len = strlen(p);
434 oldline = line;
435 line = nasm_malloc(len + 2);
436 line[0] = '%';
437 if (k == TM_IFDIFI) {
438 /* NASM does not recognise IFDIFI, so we convert it to
439 * %ifdef BOGUS. This is not used in NASM comaptible
440 * code, but does need to parse for the TASM macro
441 * package.
442 */
443 strcpy(line + 1, "ifdef BOGUS");
444 } else {
445 memcpy(line + 1, p, len + 1);
446 }
447 nasm_free(oldline);
448 return line;
449 } else if (m < 0) {
450 j = k;
451 } else
452 i = k;
453 }
454 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000455 }
456 return line;
457}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000458
H. Peter Anvin76690a12002-04-30 20:52:49 +0000459/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000460 * The pre-preprocessing stage... This function translates line
461 * number indications as they emerge from GNU cpp (`# lineno "file"
462 * flags') into NASM preprocessor line number indications (`%line
463 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000464 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000465static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000466{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000467 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000468 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000469
H. Peter Anvine2c80182005-01-15 22:15:51 +0000470 if (line[0] == '#' && line[1] == ' ') {
471 oldline = line;
472 fname = oldline + 2;
473 lineno = atoi(fname);
474 fname += strspn(fname, "0123456789 ");
475 if (*fname == '"')
476 fname++;
477 fnlen = strcspn(fname, "\"");
478 line = nasm_malloc(20 + fnlen);
479 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
480 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000481 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000482 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000483 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000484 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000485}
486
487/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000488 * Free a linked list of tokens.
489 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000490static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000491{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000492 while (list) {
493 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000494 }
495}
496
497/*
498 * Free a linked list of lines.
499 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000501{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000502 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503 while (list) {
504 l = list;
505 list = list->next;
506 free_tlist(l->first);
507 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000508 }
509}
510
511/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000512 * Free an MMacro
513 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000514static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000515{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000516 nasm_free(m->name);
517 free_tlist(m->dlist);
518 nasm_free(m->defaults);
519 free_llist(m->expansion);
520 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000521}
522
523/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700524 * Free all currently defined macros, and free the hash tables
525 */
526static void free_macros(void)
527{
528 struct hash_tbl_node *it;
529 const char *key;
530 SMacro *s;
531 MMacro *m;
532
533 it = NULL;
534 while ((s = hash_iterate(smacros, &it, &key)) != NULL) {
535 nasm_free((void *)key);
536 while (s) {
537 SMacro *ns = s->next;
538 nasm_free(s->name);
539 free_tlist(s->expansion);
540 nasm_free(s);
541 s = ns;
542 }
543 }
544 hash_free(smacros);
545
546 it = NULL;
547 while ((m = hash_iterate(mmacros, &it, &key)) != NULL) {
548 nasm_free((void *)key);
549 while (m) {
550 MMacro *nm = m->next;
551 free_mmacro(m);
552 m = nm;
553 }
554 }
555 hash_free(mmacros);
556}
557
558/*
559 * Initialize the hash tables
560 */
561static void init_macros(void)
562{
563 smacros = hash_init();
564 mmacros = hash_init();
565}
566
567/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000568 * Pop the context stack.
569 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000571{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000572 Context *c = cstk;
573 SMacro *smac, *s;
574
575 cstk = cstk->next;
576 smac = c->localmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000577 while (smac) {
578 s = smac;
579 smac = smac->next;
580 nasm_free(s->name);
581 free_tlist(s->expansion);
582 nasm_free(s);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000583 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000584 nasm_free(c->name);
585 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000586}
587
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000588#define BUF_DELTA 512
589/*
590 * Read a line from the top file in istk, handling multiple CR/LFs
591 * at the end of the line read, and handling spurious ^Zs. Will
592 * return lines from the standard macro set if this has not already
593 * been done.
594 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000595static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000596{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000597 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000598 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000599
H. Peter Anvine2c80182005-01-15 22:15:51 +0000600 if (stdmacpos) {
601 if (*stdmacpos) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000602 char *ret = nasm_strdup(*stdmacpos++);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000603 if (!*stdmacpos && any_extrastdmac) {
604 stdmacpos = extrastdmac;
605 any_extrastdmac = FALSE;
606 return ret;
607 }
608 /*
609 * Nasty hack: here we push the contents of `predef' on
610 * to the top-level expansion stack, since this is the
611 * most convenient way to implement the pre-include and
612 * pre-define features.
613 */
614 if (!*stdmacpos) {
615 Line *pd, *l;
616 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000617
H. Peter Anvine2c80182005-01-15 22:15:51 +0000618 for (pd = predef; pd; pd = pd->next) {
619 head = NULL;
620 tail = &head;
621 for (t = pd->first; t; t = t->next) {
622 *tail = new_Token(NULL, t->type, t->text, 0);
623 tail = &(*tail)->next;
624 }
625 l = nasm_malloc(sizeof(Line));
626 l->next = istk->expansion;
627 l->first = head;
628 l->finishes = FALSE;
629 istk->expansion = l;
630 }
631 }
632 return ret;
633 } else {
634 stdmacpos = NULL;
635 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000636 }
637
638 bufsize = BUF_DELTA;
639 buffer = nasm_malloc(BUF_DELTA);
640 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000641 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000642 while (1) {
643 q = fgets(p, bufsize - (p - buffer), istk->fp);
644 if (!q)
645 break;
646 p += strlen(p);
647 if (p > buffer && p[-1] == '\n') {
648 /* Convert backslash-CRLF line continuation sequences into
649 nothing at all (for DOS and Windows) */
650 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
651 p -= 3;
652 *p = 0;
653 continued_count++;
654 }
655 /* Also convert backslash-LF line continuation sequences into
656 nothing at all (for Unix) */
657 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
658 p -= 2;
659 *p = 0;
660 continued_count++;
661 } else {
662 break;
663 }
664 }
665 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000666 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000667 bufsize += BUF_DELTA;
668 buffer = nasm_realloc(buffer, bufsize);
669 p = buffer + offset; /* prevent stale-pointer problems */
670 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000671 }
672
H. Peter Anvine2c80182005-01-15 22:15:51 +0000673 if (!q && p == buffer) {
674 nasm_free(buffer);
675 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000676 }
677
H. Peter Anvine2c80182005-01-15 22:15:51 +0000678 src_set_linnum(src_get_linnum() + istk->lineinc +
679 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000680
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000681 /*
682 * Play safe: remove CRs as well as LFs, if any of either are
683 * present at the end of the line.
684 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000685 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000686 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000687
688 /*
689 * Handle spurious ^Z, which may be inserted into source files
690 * by some file transfer utilities.
691 */
692 buffer[strcspn(buffer, "\032")] = '\0';
693
H. Peter Anvin734b1882002-04-30 21:01:08 +0000694 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000695
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000696 return buffer;
697}
698
699/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000700 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000701 * don't need to parse the value out of e.g. numeric tokens: we
702 * simply split one string into many.
703 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000704static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000705{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000706 char *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000707 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000708 Token *list = NULL;
709 Token *t, **tail = &list;
710
H. Peter Anvine2c80182005-01-15 22:15:51 +0000711 while (*line) {
712 p = line;
713 if (*p == '%') {
714 p++;
715 if (isdigit(*p) ||
716 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
717 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
718 do {
719 p++;
720 }
721 while (isdigit(*p));
722 type = TOK_PREPROC_ID;
723 } else if (*p == '{') {
724 p++;
725 while (*p && *p != '}') {
726 p[-1] = *p;
727 p++;
728 }
729 p[-1] = '\0';
730 if (*p)
731 p++;
732 type = TOK_PREPROC_ID;
733 } else if (isidchar(*p) ||
734 ((*p == '!' || *p == '%' || *p == '$') &&
735 isidchar(p[1]))) {
736 do {
737 p++;
738 }
739 while (isidchar(*p));
740 type = TOK_PREPROC_ID;
741 } else {
742 type = TOK_OTHER;
743 if (*p == '%')
744 p++;
745 }
746 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
747 type = TOK_ID;
748 p++;
749 while (*p && isidchar(*p))
750 p++;
751 } else if (*p == '\'' || *p == '"') {
752 /*
753 * A string token.
754 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000755 char c = *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000756 p++;
757 type = TOK_STRING;
758 while (*p && *p != c)
759 p++;
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000760
H. Peter Anvine2c80182005-01-15 22:15:51 +0000761 if (*p) {
762 p++;
763 } else {
764 error(ERR_WARNING, "unterminated string");
765 /* Handling unterminated strings by UNV */
766 /* type = -1; */
767 }
768 } else if (isnumstart(*p)) {
769 /*
770 * A number token.
771 */
772 type = TOK_NUMBER;
773 p++;
774 while (*p && isnumchar(*p))
775 p++;
776 } else if (isspace(*p)) {
777 type = TOK_WHITESPACE;
778 p++;
779 while (*p && isspace(*p))
780 p++;
781 /*
782 * Whitespace just before end-of-line is discarded by
783 * pretending it's a comment; whitespace just before a
784 * comment gets lumped into the comment.
785 */
786 if (!*p || *p == ';') {
787 type = TOK_COMMENT;
788 while (*p)
789 p++;
790 }
791 } else if (*p == ';') {
792 type = TOK_COMMENT;
793 while (*p)
794 p++;
795 } else {
796 /*
797 * Anything else is an operator of some kind. We check
798 * for all the double-character operators (>>, <<, //,
799 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000800 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000801 */
802 type = TOK_OTHER;
803 if ((p[0] == '>' && p[1] == '>') ||
804 (p[0] == '<' && p[1] == '<') ||
805 (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++;
815 }
816 p++;
817 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000818
H. Peter Anvine2c80182005-01-15 22:15:51 +0000819 /* Handling unterminated string by UNV */
820 /*if (type == -1)
821 {
822 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
823 t->text[p-line] = *line;
824 tail = &t->next;
825 }
826 else */
827 if (type != TOK_COMMENT) {
828 *tail = t = new_Token(NULL, type, line, p - line);
829 tail = &t->next;
830 }
831 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000832 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000833 return list;
834}
835
H. Peter Anvince616072002-04-30 21:02:23 +0000836/*
837 * this function allocates a new managed block of memory and
838 * returns a pointer to the block. The managed blocks are
839 * deleted only all at once by the delete_Blocks function.
840 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000841static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +0000842{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000843 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000844
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000845 /* first, get to the end of the linked list */
846 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000847 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000848 /* now allocate the requested chunk */
849 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000851 /* now allocate a new block for the next request */
852 b->next = nasm_malloc(sizeof(Blocks));
853 /* and initialize the contents of the new block */
854 b->next->next = NULL;
855 b->next->chunk = NULL;
856 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000857}
858
859/*
860 * this function deletes all managed blocks of memory
861 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000862static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +0000863{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000864 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +0000865
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000866 /*
867 * keep in mind that the first block, pointed to by blocks
868 * is a static and not dynamically allocated, so we don't
869 * free it.
870 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000871 while (b) {
872 if (b->chunk)
873 nasm_free(b->chunk);
874 a = b;
875 b = b->next;
876 if (a != &blocks)
877 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000878 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000879}
H. Peter Anvin734b1882002-04-30 21:01:08 +0000880
881/*
882 * this function creates a new Token and passes a pointer to it
883 * back to the caller. It sets the type and text elements, and
884 * also the mac and next elements to NULL.
885 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000886static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +0000887{
888 Token *t;
889 int i;
890
H. Peter Anvine2c80182005-01-15 22:15:51 +0000891 if (freeTokens == NULL) {
892 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
893 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
894 freeTokens[i].next = &freeTokens[i + 1];
895 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000896 }
897 t = freeTokens;
898 freeTokens = t->next;
899 t->next = next;
900 t->mac = NULL;
901 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000902 if (type == TOK_WHITESPACE || text == NULL) {
903 t->text = NULL;
904 } else {
905 if (txtlen == 0)
906 txtlen = strlen(text);
907 t->text = nasm_malloc(1 + txtlen);
908 strncpy(t->text, text, txtlen);
909 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +0000910 }
911 return t;
912}
913
H. Peter Anvine2c80182005-01-15 22:15:51 +0000914static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +0000915{
916 Token *next = t->next;
917 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +0000918 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000919 freeTokens = t;
920 return next;
921}
922
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000923/*
924 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000925 * If expand_locals is not zero, identifiers of the form "%$*xxx"
926 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000927 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000928static char *detoken(Token * tlist, int expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000929{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000930 Token *t;
931 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000932 char *line, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000933
934 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 for (t = tlist; t; t = t->next) {
936 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000937 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000938 nasm_free(t->text);
939 if (p)
940 t->text = nasm_strdup(p);
941 else
942 t->text = NULL;
943 }
944 /* Expand local macros here and not during preprocessing */
945 if (expand_locals &&
946 t->type == TOK_PREPROC_ID && t->text &&
947 t->text[0] == '%' && t->text[1] == '$') {
948 Context *ctx = get_ctx(t->text, FALSE);
949 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000950 char buffer[40];
951 char *p, *q = t->text + 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000952
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953 q += strspn(q, "$");
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000954 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000955 p = nasm_strcat(buffer, q);
956 nasm_free(t->text);
957 t->text = p;
958 }
959 }
960 if (t->type == TOK_WHITESPACE) {
961 len++;
962 } else if (t->text) {
963 len += strlen(t->text);
964 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000965 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000966 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967 for (t = tlist; t; t = t->next) {
968 if (t->type == TOK_WHITESPACE) {
969 *p = ' ';
970 p++;
971 *p = '\0';
972 } else if (t->text) {
973 strcpy(p, t->text);
974 p += strlen(p);
975 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000976 }
977 *p = '\0';
978 return line;
979}
980
981/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000982 * A scanner, suitable for use by the expression evaluator, which
983 * operates on a line of Tokens. Expects a pointer to a pointer to
984 * the first token in the line to be passed in as its private_data
985 * field.
986 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000987static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000988{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000989 Token **tlineptr = private_data;
990 Token *tline;
991
H. Peter Anvine2c80182005-01-15 22:15:51 +0000992 do {
993 tline = *tlineptr;
994 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000995 }
996 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000998
999 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001001
1002 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001004 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001006
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 if (tline->type == TOK_ID) {
1008 tokval->t_charptr = tline->text;
1009 if (tline->text[0] == '$') {
1010 tokval->t_charptr++;
1011 return tokval->t_type = TOKEN_ID;
1012 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001013
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014 /*
1015 * This is the only special case we actually need to worry
1016 * about in this restricted context.
1017 */
1018 if (!nasm_stricmp(tline->text, "seg"))
1019 return tokval->t_type = TOKEN_SEG;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001020
H. Peter Anvine2c80182005-01-15 22:15:51 +00001021 return tokval->t_type = TOKEN_ID;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001022 }
1023
H. Peter Anvine2c80182005-01-15 22:15:51 +00001024 if (tline->type == TOK_NUMBER) {
1025 int rn_error;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001026
H. Peter Anvine2c80182005-01-15 22:15:51 +00001027 tokval->t_integer = readnum(tline->text, &rn_error);
1028 if (rn_error)
1029 return tokval->t_type = TOKEN_ERRNUM;
1030 tokval->t_charptr = NULL;
1031 return tokval->t_type = TOKEN_NUM;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001032 }
1033
H. Peter Anvine2c80182005-01-15 22:15:51 +00001034 if (tline->type == TOK_STRING) {
1035 int rn_warn;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001036 char q, *r;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001037 int l;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001038
H. Peter Anvine2c80182005-01-15 22:15:51 +00001039 r = tline->text;
1040 q = *r++;
1041 l = strlen(r);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001042
H. Peter Anvine2c80182005-01-15 22:15:51 +00001043 if (l == 0 || r[l - 1] != q)
1044 return tokval->t_type = TOKEN_ERRNUM;
1045 tokval->t_integer = readstrnum(r, l - 1, &rn_warn);
1046 if (rn_warn)
1047 error(ERR_WARNING | ERR_PASS1, "character constant too long");
1048 tokval->t_charptr = NULL;
1049 return tokval->t_type = TOKEN_NUM;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001050 }
1051
H. Peter Anvine2c80182005-01-15 22:15:51 +00001052 if (tline->type == TOK_OTHER) {
1053 if (!strcmp(tline->text, "<<"))
1054 return tokval->t_type = TOKEN_SHL;
1055 if (!strcmp(tline->text, ">>"))
1056 return tokval->t_type = TOKEN_SHR;
1057 if (!strcmp(tline->text, "//"))
1058 return tokval->t_type = TOKEN_SDIV;
1059 if (!strcmp(tline->text, "%%"))
1060 return tokval->t_type = TOKEN_SMOD;
1061 if (!strcmp(tline->text, "=="))
1062 return tokval->t_type = TOKEN_EQ;
1063 if (!strcmp(tline->text, "<>"))
1064 return tokval->t_type = TOKEN_NE;
1065 if (!strcmp(tline->text, "!="))
1066 return tokval->t_type = TOKEN_NE;
1067 if (!strcmp(tline->text, "<="))
1068 return tokval->t_type = TOKEN_LE;
1069 if (!strcmp(tline->text, ">="))
1070 return tokval->t_type = TOKEN_GE;
1071 if (!strcmp(tline->text, "&&"))
1072 return tokval->t_type = TOKEN_DBL_AND;
1073 if (!strcmp(tline->text, "^^"))
1074 return tokval->t_type = TOKEN_DBL_XOR;
1075 if (!strcmp(tline->text, "||"))
1076 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001077 }
1078
1079 /*
1080 * We have no other options: just return the first character of
1081 * the token text.
1082 */
1083 return tokval->t_type = tline->text[0];
1084}
1085
1086/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001087 * Compare a string to the name of an existing macro; this is a
1088 * simple wrapper which calls either strcmp or nasm_stricmp
1089 * depending on the value of the `casesense' parameter.
1090 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001091static int mstrcmp(char *p, char *q, int casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001092{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001093 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001094}
1095
1096/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001097 * Return the Context structure associated with a %$ token. Return
1098 * NULL, having _already_ reported an error condition, if the
1099 * context stack isn't deep enough for the supplied number of $
1100 * signs.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001101 * If all_contexts == TRUE, contexts that enclose current are
1102 * also scanned for such smacro, until it is found; if not -
1103 * only the context that directly results from the number of $'s
1104 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001105 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001106static Context *get_ctx(char *name, int all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001107{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001108 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001109 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001110 int i;
1111
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001112 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001113 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001114
H. Peter Anvine2c80182005-01-15 22:15:51 +00001115 if (!cstk) {
1116 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1117 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001118 }
1119
H. Peter Anvine2c80182005-01-15 22:15:51 +00001120 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1121 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001122/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001123 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124 if (!ctx) {
1125 error(ERR_NONFATAL, "`%s': context stack is only"
1126 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1127 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001128 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001129 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001130 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001131
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 do {
1133 /* Search for this smacro in found context */
1134 m = ctx->localmac;
1135 while (m) {
1136 if (!mstrcmp(m->name, name, m->casesense))
1137 return ctx;
1138 m = m->next;
1139 }
1140 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001141 }
1142 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001143 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001144}
1145
1146/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001147 * Open an include file. This routine must always return a valid
1148 * file pointer if it returns - it's responsible for throwing an
1149 * ERR_FATAL and bombing out completely if not. It should also try
1150 * the include path one by one until it finds the file or reaches
1151 * the end of the path.
1152 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001153static FILE *inc_fopen(char *file)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001154{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001155 FILE *fp;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001156 char *prefix = "", *combine;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001157 IncPath *ip = ipath;
H. Peter Anvin620515a2002-04-30 20:57:38 +00001158 static int namelen = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001159 int len = strlen(file);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001160
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161 while (1) {
1162 combine = nasm_malloc(strlen(prefix) + len + 1);
1163 strcpy(combine, prefix);
1164 strcat(combine, file);
1165 fp = fopen(combine, "r");
1166 if (pass == 0 && fp) {
1167 namelen += strlen(combine) + 1;
1168 if (namelen > 62) {
1169 printf(" \\\n ");
1170 namelen = 2;
1171 }
1172 printf(" %s", combine);
1173 }
1174 nasm_free(combine);
1175 if (fp)
1176 return fp;
1177 if (!ip)
1178 break;
1179 prefix = ip->path;
1180 ip = ip->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001181 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001182
H. Peter Anvin734b1882002-04-30 21:01:08 +00001183 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001184 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001185}
1186
1187/*
H. Peter Anvin97a23472007-09-16 17:57:25 -07001188 * Search for a key in the hash index; adding it if necessary
1189 * (in which case we initialize the data pointer to NULL.)
1190 */
1191static void **
1192hash_findi_add(struct hash_table *hash, const char *str)
1193{
1194 struct hash_insert hi;
1195 void **r;
1196 char *strx;
1197
1198 r = hash_findi(hash, str, &hi);
1199 if (r)
1200 return r;
1201
1202 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
1203 return hash_add(&hi, strx, NULL);
1204}
1205
1206/*
1207 * Like hash_findi, but returns the data element rather than a pointer
1208 * to it. Used only when not adding a new element, hence no third
1209 * argument.
1210 */
1211static void *
1212hash_findix(struct hash_table *hash, const char *str)
1213{
1214 void **p;
1215
1216 p = hash_findi(hash, str, NULL);
1217 return p ? *p : NULL;
1218}
1219
1220/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001221 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001222 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001223 * return TRUE if _any_ single-line macro of that name is defined.
1224 * Otherwise, will return TRUE if a single-line macro with either
1225 * `nparam' or no parameters is defined.
1226 *
1227 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001228 * defined, or nparam is -1, the address of the definition structure
1229 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001230 * is NULL, no action will be taken regarding its contents, and no
1231 * error will occur.
1232 *
1233 * Note that this is also called with nparam zero to resolve
1234 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001235 *
1236 * If you already know which context macro belongs to, you can pass
1237 * the context pointer as first parameter; if you won't but name begins
1238 * with %$ the context will be automatically computed. If all_contexts
1239 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001240 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001241static int
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001242smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001243 int nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001244{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001245 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001246
H. Peter Anvin97a23472007-09-16 17:57:25 -07001247 if (ctx) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001248 m = ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001249 } else if (name[0] == '%' && name[1] == '$') {
1250 if (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001251 ctx = get_ctx(name, FALSE);
1252 if (!ctx)
1253 return FALSE; /* got to return _something_ */
1254 m = ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001255 } else {
1256 m = (SMacro *) hash_findix(smacros, name);
1257 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001258
H. Peter Anvine2c80182005-01-15 22:15:51 +00001259 while (m) {
1260 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1261 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam)) {
1262 if (defn) {
1263 if (nparam == m->nparam || nparam == -1)
1264 *defn = m;
1265 else
1266 *defn = NULL;
1267 }
1268 return TRUE;
1269 }
1270 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001271 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001272
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001273 return FALSE;
1274}
1275
1276/*
1277 * Count and mark off the parameters in a multi-line macro call.
1278 * This is called both from within the multi-line macro expansion
1279 * code, and also to mark off the default parameters when provided
1280 * in a %macro definition line.
1281 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001282static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001283{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001284 int paramsize, brace;
1285
1286 *nparam = paramsize = 0;
1287 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001288 while (t) {
1289 if (*nparam >= paramsize) {
1290 paramsize += PARAM_DELTA;
1291 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1292 }
1293 skip_white_(t);
1294 brace = FALSE;
1295 if (tok_is_(t, "{"))
1296 brace = TRUE;
1297 (*params)[(*nparam)++] = t;
1298 while (tok_isnt_(t, brace ? "}" : ","))
1299 t = t->next;
1300 if (t) { /* got a comma/brace */
1301 t = t->next;
1302 if (brace) {
1303 /*
1304 * Now we've found the closing brace, look further
1305 * for the comma.
1306 */
1307 skip_white_(t);
1308 if (tok_isnt_(t, ",")) {
1309 error(ERR_NONFATAL,
1310 "braces do not enclose all of macro parameter");
1311 while (tok_isnt_(t, ","))
1312 t = t->next;
1313 }
1314 if (t)
1315 t = t->next; /* eat the comma */
1316 }
1317 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001318 }
1319}
1320
1321/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001322 * Determine whether one of the various `if' conditions is true or
1323 * not.
1324 *
1325 * We must free the tline we get passed.
1326 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001327static int if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001328{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001329 enum pp_conditional i = PP_COND(ct);
1330 int j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001331 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001332 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001333 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001334 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001335
1336 origline = tline;
1337
H. Peter Anvine2c80182005-01-15 22:15:51 +00001338 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001339 case PPC_IFCTX:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340 j = FALSE; /* have we matched yet? */
1341 while (cstk && tline) {
1342 skip_white_(tline);
1343 if (!tline || tline->type != TOK_ID) {
1344 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001345 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001346 free_tlist(origline);
1347 return -1;
1348 }
1349 if (!nasm_stricmp(tline->text, cstk->name))
1350 j = TRUE;
1351 tline = tline->next;
1352 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001353 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001354
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001355 case PPC_IFDEF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001356 j = FALSE; /* have we matched yet? */
1357 while (tline) {
1358 skip_white_(tline);
1359 if (!tline || (tline->type != TOK_ID &&
1360 (tline->type != TOK_PREPROC_ID ||
1361 tline->text[1] != '$'))) {
1362 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001363 "`%s' expects macro identifiers", pp_directives[ct]);
1364 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 }
1366 if (smacro_defined(NULL, tline->text, 0, NULL, 1))
1367 j = TRUE;
1368 tline = tline->next;
1369 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001370 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001371
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001372 case PPC_IFIDN:
1373 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001374 tline = expand_smacro(tline);
1375 t = tt = tline;
1376 while (tok_isnt_(tt, ","))
1377 tt = tt->next;
1378 if (!tt) {
1379 error(ERR_NONFATAL,
1380 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001381 pp_directives[ct]);
1382 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001383 }
1384 tt = tt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001385 j = TRUE; /* assume equality unless proved not */
1386 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1387 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1388 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001389 pp_directives[ct]);
1390 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001391 }
1392 if (t->type == TOK_WHITESPACE) {
1393 t = t->next;
1394 continue;
1395 }
1396 if (tt->type == TOK_WHITESPACE) {
1397 tt = tt->next;
1398 continue;
1399 }
1400 if (tt->type != t->type) {
1401 j = FALSE; /* found mismatching tokens */
1402 break;
1403 }
1404 /* Unify surrounding quotes for strings */
1405 if (t->type == TOK_STRING) {
1406 tt->text[0] = t->text[0];
1407 tt->text[strlen(tt->text) - 1] = t->text[0];
1408 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001409 if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001410 j = FALSE; /* found mismatching tokens */
1411 break;
1412 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001413
H. Peter Anvine2c80182005-01-15 22:15:51 +00001414 t = t->next;
1415 tt = tt->next;
1416 }
1417 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1418 j = FALSE; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001419 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001420
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001421 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001422 {
1423 int found = 0;
1424 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001425
H. Peter Anvine2c80182005-01-15 22:15:51 +00001426 tline = tline->next;
1427 skip_white_(tline);
1428 tline = expand_id(tline);
1429 if (!tok_type_(tline, TOK_ID)) {
1430 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001431 "`%s' expects a macro name", pp_directives[ct]);
1432 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433 }
1434 searching.name = nasm_strdup(tline->text);
1435 searching.casesense = (i == PP_MACRO);
1436 searching.plus = FALSE;
1437 searching.nolist = FALSE;
1438 searching.in_progress = FALSE;
1439 searching.rep_nest = NULL;
1440 searching.nparam_min = 0;
1441 searching.nparam_max = INT_MAX;
1442 tline = expand_smacro(tline->next);
1443 skip_white_(tline);
1444 if (!tline) {
1445 } else if (!tok_type_(tline, TOK_NUMBER)) {
1446 error(ERR_NONFATAL,
1447 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001448 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001449 } else {
1450 searching.nparam_min = searching.nparam_max =
1451 readnum(tline->text, &j);
1452 if (j)
1453 error(ERR_NONFATAL,
1454 "unable to parse parameter count `%s'",
1455 tline->text);
1456 }
1457 if (tline && tok_is_(tline->next, "-")) {
1458 tline = tline->next->next;
1459 if (tok_is_(tline, "*"))
1460 searching.nparam_max = INT_MAX;
1461 else if (!tok_type_(tline, TOK_NUMBER))
1462 error(ERR_NONFATAL,
1463 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001464 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001465 else {
1466 searching.nparam_max = readnum(tline->text, &j);
1467 if (j)
1468 error(ERR_NONFATAL,
1469 "unable to parse parameter count `%s'",
1470 tline->text);
1471 if (searching.nparam_min > searching.nparam_max)
1472 error(ERR_NONFATAL,
1473 "minimum parameter count exceeds maximum");
1474 }
1475 }
1476 if (tline && tok_is_(tline->next, "+")) {
1477 tline = tline->next;
1478 searching.plus = TRUE;
1479 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07001480 mmac = (MMacro *) hash_findix(mmacros, searching.name);
1481 while (mmac) {
1482 if (!strcmp(mmac->name, searching.name) &&
1483 (mmac->nparam_min <= searching.nparam_max
1484 || searching.plus)
1485 && (searching.nparam_min <= mmac->nparam_max
1486 || mmac->plus)) {
1487 found = TRUE;
1488 break;
1489 }
1490 mmac = mmac->next;
1491 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001492 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001493 j = found;
1494 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001495 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001496
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001497 case PPC_IFID:
1498 needtype = TOK_ID;
1499 goto iftype;
1500 case PPC_IFNUM:
1501 needtype = TOK_NUMBER;
1502 goto iftype;
1503 case PPC_IFSTR:
1504 needtype = TOK_STRING;
1505 goto iftype;
1506
1507 iftype:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001508 tline = expand_smacro(tline);
1509 t = tline;
1510 while (tok_type_(t, TOK_WHITESPACE))
1511 t = t->next;
1512 j = FALSE; /* placate optimiser */
1513 if (t)
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001514 j = t->type == needtype;
1515 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001516
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001517 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001518 t = tline = expand_smacro(tline);
1519 tptr = &t;
1520 tokval.t_type = TOKEN_INVALID;
1521 evalresult = evaluate(ppscan, tptr, &tokval,
1522 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001523 if (!evalresult)
1524 return -1;
1525 if (tokval.t_type)
1526 error(ERR_WARNING,
1527 "trailing garbage after expression ignored");
1528 if (!is_simple(evalresult)) {
1529 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001530 "non-constant value given to `%s'", pp_directives[ct]);
1531 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001532 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001533 j = reloc_value(evalresult) != 0;
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001534 return j;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001535
H. Peter Anvine2c80182005-01-15 22:15:51 +00001536 default:
1537 error(ERR_FATAL,
1538 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001539 pp_directives[ct]);
1540 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001541 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001542
1543 free_tlist(origline);
1544 return j ^ PP_NEGATIVE(ct);
1545
1546fail:
1547 free_tlist(origline);
1548 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001549}
1550
1551/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001552 * Expand macros in a string. Used in %error and %include directives.
Keith Kaniosb7a89542007-04-12 02:40:54 +00001553 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001554 * The returned variable should ALWAYS be freed after usage.
1555 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001556void expand_macros_in_string(char **p)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001557{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001558 Token *line = tokenize(*p);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001559 line = expand_smacro(line);
1560 *p = detoken(line, FALSE);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001561}
1562
Ed Beroset3ab3f412002-06-11 03:31:49 +00001563/**
1564 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001565 * Find out if a line contains a preprocessor directive, and deal
1566 * with it if so.
1567 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001568 * If a directive _is_ found, it is the responsibility of this routine
1569 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001570 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001571 * @param tline a pointer to the current tokeninzed line linked list
1572 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001573 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001574 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001575static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001576{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001577 enum preproc_token i;
1578 int j;
1579 int nparam, nolist;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001580 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001581 int offset;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001582 char *p, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001583 Include *inc;
1584 Context *ctx;
1585 Cond *cond;
1586 SMacro *smac, **smhead;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001587 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001588 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1589 Line *l;
1590 struct tokenval tokval;
1591 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001592 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001593
1594 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001595
H. Peter Anvineba20a72002-04-30 20:53:55 +00001596 skip_white_(tline);
1597 if (!tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001598 (tline->text[1] == '%' || tline->text[1] == '$'
1599 || tline->text[1] == '!'))
1600 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001601
H. Peter Anvin4169a472007-09-12 01:29:43 +00001602 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001603
1604 /*
1605 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001606 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001607 * we should ignore all directives except for condition
1608 * directives.
1609 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001610 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001611 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1612 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001613 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001614
1615 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001616 * If we're defining a macro or reading a %rep block, we should
1617 * ignore all directives except for %macro/%imacro (which
1618 * generate an error), %endm/%endmacro, and (only if we're in a
H. Peter Anvineba20a72002-04-30 20:53:55 +00001619 * %rep block) %endrep. If we're in a %rep block, another %rep
1620 * causes an error, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001621 */
1622 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001623 i != PP_ENDMACRO && i != PP_ENDM &&
1624 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1625 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001626 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001627
H. Peter Anvin4169a472007-09-12 01:29:43 +00001628 switch (i) {
1629 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001630 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1631 tline->text);
1632 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001633
H. Peter Anvine2c80182005-01-15 22:15:51 +00001634 case PP_STACKSIZE:
1635 /* Directive to tell NASM what the default stack size is. The
1636 * default is for a 16-bit stack, and this can be overriden with
1637 * %stacksize large.
1638 * the following form:
1639 *
1640 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1641 */
1642 tline = tline->next;
1643 if (tline && tline->type == TOK_WHITESPACE)
1644 tline = tline->next;
1645 if (!tline || tline->type != TOK_ID) {
1646 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1647 free_tlist(origline);
1648 return DIRECTIVE_FOUND;
1649 }
1650 if (nasm_stricmp(tline->text, "flat") == 0) {
1651 /* All subsequent ARG directives are for a 32-bit stack */
1652 StackSize = 4;
1653 StackPointer = "ebp";
1654 ArgOffset = 8;
1655 LocalOffset = 4;
1656 } else if (nasm_stricmp(tline->text, "large") == 0) {
1657 /* All subsequent ARG directives are for a 16-bit stack,
1658 * far function call.
1659 */
1660 StackSize = 2;
1661 StackPointer = "bp";
1662 ArgOffset = 4;
1663 LocalOffset = 2;
1664 } else if (nasm_stricmp(tline->text, "small") == 0) {
1665 /* All subsequent ARG directives are for a 16-bit stack,
1666 * far function call. We don't support near functions.
1667 */
1668 StackSize = 2;
1669 StackPointer = "bp";
1670 ArgOffset = 6;
1671 LocalOffset = 2;
1672 } else {
1673 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1674 free_tlist(origline);
1675 return DIRECTIVE_FOUND;
1676 }
1677 free_tlist(origline);
1678 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001679
H. Peter Anvine2c80182005-01-15 22:15:51 +00001680 case PP_ARG:
1681 /* TASM like ARG directive to define arguments to functions, in
1682 * the following form:
1683 *
1684 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1685 */
1686 offset = ArgOffset;
1687 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001688 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001689 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001690
H. Peter Anvine2c80182005-01-15 22:15:51 +00001691 /* Find the argument name */
1692 tline = tline->next;
1693 if (tline && tline->type == TOK_WHITESPACE)
1694 tline = tline->next;
1695 if (!tline || tline->type != TOK_ID) {
1696 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1697 free_tlist(origline);
1698 return DIRECTIVE_FOUND;
1699 }
1700 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001701
H. Peter Anvine2c80182005-01-15 22:15:51 +00001702 /* Find the argument size type */
1703 tline = tline->next;
1704 if (!tline || tline->type != TOK_OTHER
1705 || tline->text[0] != ':') {
1706 error(ERR_NONFATAL,
1707 "Syntax error processing `%%arg' directive");
1708 free_tlist(origline);
1709 return DIRECTIVE_FOUND;
1710 }
1711 tline = tline->next;
1712 if (!tline || tline->type != TOK_ID) {
1713 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1714 free_tlist(origline);
1715 return DIRECTIVE_FOUND;
1716 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001717
H. Peter Anvine2c80182005-01-15 22:15:51 +00001718 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001719 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001720 tt = expand_smacro(tt);
1721 if (nasm_stricmp(tt->text, "byte") == 0) {
1722 size = MAX(StackSize, 1);
1723 } else if (nasm_stricmp(tt->text, "word") == 0) {
1724 size = MAX(StackSize, 2);
1725 } else if (nasm_stricmp(tt->text, "dword") == 0) {
1726 size = MAX(StackSize, 4);
1727 } else if (nasm_stricmp(tt->text, "qword") == 0) {
1728 size = MAX(StackSize, 8);
1729 } else if (nasm_stricmp(tt->text, "tword") == 0) {
1730 size = MAX(StackSize, 10);
1731 } else {
1732 error(ERR_NONFATAL,
1733 "Invalid size type for `%%arg' missing directive");
1734 free_tlist(tt);
1735 free_tlist(origline);
1736 return DIRECTIVE_FOUND;
1737 }
1738 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001739
H. Peter Anvine2c80182005-01-15 22:15:51 +00001740 /* Now define the macro for the argument */
1741 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1742 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001743 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001744 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001745
H. Peter Anvine2c80182005-01-15 22:15:51 +00001746 /* Move to the next argument in the list */
1747 tline = tline->next;
1748 if (tline && tline->type == TOK_WHITESPACE)
1749 tline = tline->next;
1750 }
1751 while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1752 free_tlist(origline);
1753 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001754
H. Peter Anvine2c80182005-01-15 22:15:51 +00001755 case PP_LOCAL:
1756 /* TASM like LOCAL directive to define local variables for a
1757 * function, in the following form:
1758 *
1759 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1760 *
1761 * The '= LocalSize' at the end is ignored by NASM, but is
1762 * required by TASM to define the local parameter size (and used
1763 * by the TASM macro package).
1764 */
1765 offset = LocalOffset;
1766 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001767 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001768 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001769
H. Peter Anvine2c80182005-01-15 22:15:51 +00001770 /* Find the argument name */
1771 tline = tline->next;
1772 if (tline && tline->type == TOK_WHITESPACE)
1773 tline = tline->next;
1774 if (!tline || tline->type != TOK_ID) {
1775 error(ERR_NONFATAL,
1776 "`%%local' missing argument parameter");
1777 free_tlist(origline);
1778 return DIRECTIVE_FOUND;
1779 }
1780 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001781
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782 /* Find the argument size type */
1783 tline = tline->next;
1784 if (!tline || tline->type != TOK_OTHER
1785 || tline->text[0] != ':') {
1786 error(ERR_NONFATAL,
1787 "Syntax error processing `%%local' directive");
1788 free_tlist(origline);
1789 return DIRECTIVE_FOUND;
1790 }
1791 tline = tline->next;
1792 if (!tline || tline->type != TOK_ID) {
1793 error(ERR_NONFATAL,
1794 "`%%local' missing size type parameter");
1795 free_tlist(origline);
1796 return DIRECTIVE_FOUND;
1797 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001798
H. Peter Anvine2c80182005-01-15 22:15:51 +00001799 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001800 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001801 tt = expand_smacro(tt);
1802 if (nasm_stricmp(tt->text, "byte") == 0) {
1803 size = MAX(StackSize, 1);
1804 } else if (nasm_stricmp(tt->text, "word") == 0) {
1805 size = MAX(StackSize, 2);
1806 } else if (nasm_stricmp(tt->text, "dword") == 0) {
1807 size = MAX(StackSize, 4);
1808 } else if (nasm_stricmp(tt->text, "qword") == 0) {
1809 size = MAX(StackSize, 8);
1810 } else if (nasm_stricmp(tt->text, "tword") == 0) {
1811 size = MAX(StackSize, 10);
1812 } else {
1813 error(ERR_NONFATAL,
1814 "Invalid size type for `%%local' missing directive");
1815 free_tlist(tt);
1816 free_tlist(origline);
1817 return DIRECTIVE_FOUND;
1818 }
1819 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001820
H. Peter Anvine2c80182005-01-15 22:15:51 +00001821 /* Now define the macro for the argument */
1822 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
1823 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001824 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001825 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001826
H. Peter Anvine2c80182005-01-15 22:15:51 +00001827 /* Now define the assign to setup the enter_c macro correctly */
1828 snprintf(directive, sizeof(directive),
1829 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001830 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001831
H. Peter Anvine2c80182005-01-15 22:15:51 +00001832 /* Move to the next argument in the list */
1833 tline = tline->next;
1834 if (tline && tline->type == TOK_WHITESPACE)
1835 tline = tline->next;
1836 }
1837 while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1838 free_tlist(origline);
1839 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001840
H. Peter Anvine2c80182005-01-15 22:15:51 +00001841 case PP_CLEAR:
1842 if (tline->next)
1843 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07001844 free_macros();
1845 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001846 free_tlist(origline);
1847 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001848
H. Peter Anvine2c80182005-01-15 22:15:51 +00001849 case PP_INCLUDE:
1850 tline = tline->next;
1851 skip_white_(tline);
1852 if (!tline || (tline->type != TOK_STRING &&
1853 tline->type != TOK_INTERNAL_STRING)) {
1854 error(ERR_NONFATAL, "`%%include' expects a file name");
1855 free_tlist(origline);
1856 return DIRECTIVE_FOUND; /* but we did _something_ */
1857 }
1858 if (tline->next)
1859 error(ERR_WARNING,
1860 "trailing garbage after `%%include' ignored");
1861 if (tline->type != TOK_INTERNAL_STRING) {
1862 p = tline->text + 1; /* point past the quote to the name */
1863 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
1864 } else
1865 p = tline->text; /* internal_string is easier */
1866 expand_macros_in_string(&p);
1867 inc = nasm_malloc(sizeof(Include));
1868 inc->next = istk;
1869 inc->conds = NULL;
1870 inc->fp = inc_fopen(p);
1871 inc->fname = src_set_fname(p);
1872 inc->lineno = src_set_linnum(0);
1873 inc->lineinc = 1;
1874 inc->expansion = NULL;
1875 inc->mstk = NULL;
1876 istk = inc;
1877 list->uplevel(LIST_INCLUDE);
1878 free_tlist(origline);
1879 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001880
H. Peter Anvine2c80182005-01-15 22:15:51 +00001881 case PP_PUSH:
1882 tline = tline->next;
1883 skip_white_(tline);
1884 tline = expand_id(tline);
1885 if (!tok_type_(tline, TOK_ID)) {
1886 error(ERR_NONFATAL, "`%%push' expects a context identifier");
1887 free_tlist(origline);
1888 return DIRECTIVE_FOUND; /* but we did _something_ */
1889 }
1890 if (tline->next)
1891 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
1892 ctx = nasm_malloc(sizeof(Context));
1893 ctx->next = cstk;
1894 ctx->localmac = NULL;
1895 ctx->name = nasm_strdup(tline->text);
1896 ctx->number = unique++;
1897 cstk = ctx;
1898 free_tlist(origline);
1899 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001900
H. Peter Anvine2c80182005-01-15 22:15:51 +00001901 case PP_REPL:
1902 tline = tline->next;
1903 skip_white_(tline);
1904 tline = expand_id(tline);
1905 if (!tok_type_(tline, TOK_ID)) {
1906 error(ERR_NONFATAL, "`%%repl' 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 `%%repl' ignored");
1912 if (!cstk)
1913 error(ERR_NONFATAL, "`%%repl': context stack is empty");
1914 else {
1915 nasm_free(cstk->name);
1916 cstk->name = nasm_strdup(tline->text);
1917 }
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_POP:
1922 if (tline->next)
1923 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
1924 if (!cstk)
1925 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
1926 else
1927 ctx_pop();
1928 free_tlist(origline);
1929 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001930
H. Peter Anvine2c80182005-01-15 22:15:51 +00001931 case PP_ERROR:
1932 tline->next = expand_smacro(tline->next);
1933 tline = tline->next;
1934 skip_white_(tline);
1935 if (tok_type_(tline, TOK_STRING)) {
1936 p = tline->text + 1; /* point past the quote to the name */
1937 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
1938 expand_macros_in_string(&p);
1939 error(ERR_NONFATAL, "%s", p);
1940 nasm_free(p);
1941 } else {
1942 p = detoken(tline, FALSE);
1943 error(ERR_WARNING, "%s", p);
1944 nasm_free(p);
1945 }
1946 free_tlist(origline);
1947 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001948
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001949 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001950 if (istk->conds && !emitting(istk->conds->state))
1951 j = COND_NEVER;
1952 else {
1953 j = if_condition(tline->next, i);
1954 tline->next = NULL; /* it got freed */
1955 free_tlist(origline);
1956 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
1957 }
1958 cond = nasm_malloc(sizeof(Cond));
1959 cond->next = istk->conds;
1960 cond->state = j;
1961 istk->conds = cond;
1962 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001963
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001964 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001965 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00001966 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001967 if (emitting(istk->conds->state)
1968 || istk->conds->state == COND_NEVER)
1969 istk->conds->state = COND_NEVER;
1970 else {
1971 /*
1972 * IMPORTANT: In the case of %if, we will already have
1973 * called expand_mmac_params(); however, if we're
1974 * processing an %elif we must have been in a
1975 * non-emitting mode, which would have inhibited
1976 * the normal invocation of expand_mmac_params(). Therefore,
1977 * we have to do it explicitly here.
1978 */
1979 j = if_condition(expand_mmac_params(tline->next), i);
1980 tline->next = NULL; /* it got freed */
1981 free_tlist(origline);
1982 istk->conds->state =
1983 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
1984 }
1985 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001986
H. Peter Anvine2c80182005-01-15 22:15:51 +00001987 case PP_ELSE:
1988 if (tline->next)
1989 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
1990 if (!istk->conds)
1991 error(ERR_FATAL, "`%%else': no matching `%%if'");
1992 if (emitting(istk->conds->state)
1993 || istk->conds->state == COND_NEVER)
1994 istk->conds->state = COND_ELSE_FALSE;
1995 else
1996 istk->conds->state = COND_ELSE_TRUE;
1997 free_tlist(origline);
1998 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001999
H. Peter Anvine2c80182005-01-15 22:15:51 +00002000 case PP_ENDIF:
2001 if (tline->next)
2002 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2003 if (!istk->conds)
2004 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2005 cond = istk->conds;
2006 istk->conds = cond->next;
2007 nasm_free(cond);
2008 free_tlist(origline);
2009 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002010
H. Peter Anvine2c80182005-01-15 22:15:51 +00002011 case PP_MACRO:
2012 case PP_IMACRO:
2013 if (defining)
2014 error(ERR_FATAL,
2015 "`%%%smacro': already defining a macro",
2016 (i == PP_IMACRO ? "i" : ""));
2017 tline = tline->next;
2018 skip_white_(tline);
2019 tline = expand_id(tline);
2020 if (!tok_type_(tline, TOK_ID)) {
2021 error(ERR_NONFATAL,
2022 "`%%%smacro' expects a macro name",
2023 (i == PP_IMACRO ? "i" : ""));
2024 return DIRECTIVE_FOUND;
2025 }
2026 defining = nasm_malloc(sizeof(MMacro));
2027 defining->name = nasm_strdup(tline->text);
2028 defining->casesense = (i == PP_MACRO);
2029 defining->plus = FALSE;
2030 defining->nolist = FALSE;
2031 defining->in_progress = FALSE;
2032 defining->rep_nest = NULL;
2033 tline = expand_smacro(tline->next);
2034 skip_white_(tline);
2035 if (!tok_type_(tline, TOK_NUMBER)) {
2036 error(ERR_NONFATAL,
2037 "`%%%smacro' expects a parameter count",
2038 (i == PP_IMACRO ? "i" : ""));
2039 defining->nparam_min = defining->nparam_max = 0;
2040 } else {
2041 defining->nparam_min = defining->nparam_max =
2042 readnum(tline->text, &j);
2043 if (j)
2044 error(ERR_NONFATAL,
2045 "unable to parse parameter count `%s'", tline->text);
2046 }
2047 if (tline && tok_is_(tline->next, "-")) {
2048 tline = tline->next->next;
2049 if (tok_is_(tline, "*"))
2050 defining->nparam_max = INT_MAX;
2051 else if (!tok_type_(tline, TOK_NUMBER))
2052 error(ERR_NONFATAL,
2053 "`%%%smacro' expects a parameter count after `-'",
2054 (i == PP_IMACRO ? "i" : ""));
2055 else {
2056 defining->nparam_max = readnum(tline->text, &j);
2057 if (j)
2058 error(ERR_NONFATAL,
2059 "unable to parse parameter count `%s'",
2060 tline->text);
2061 if (defining->nparam_min > defining->nparam_max)
2062 error(ERR_NONFATAL,
2063 "minimum parameter count exceeds maximum");
2064 }
2065 }
2066 if (tline && tok_is_(tline->next, "+")) {
2067 tline = tline->next;
2068 defining->plus = TRUE;
2069 }
2070 if (tline && tok_type_(tline->next, TOK_ID) &&
2071 !nasm_stricmp(tline->next->text, ".nolist")) {
2072 tline = tline->next;
2073 defining->nolist = TRUE;
2074 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07002075 mmac = (MMacro *) hash_findix(mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002076 while (mmac) {
2077 if (!strcmp(mmac->name, defining->name) &&
2078 (mmac->nparam_min <= defining->nparam_max
2079 || defining->plus)
2080 && (defining->nparam_min <= mmac->nparam_max
2081 || mmac->plus)) {
2082 error(ERR_WARNING,
2083 "redefining multi-line macro `%s'", defining->name);
2084 break;
2085 }
2086 mmac = mmac->next;
2087 }
2088 /*
2089 * Handle default parameters.
2090 */
2091 if (tline && tline->next) {
2092 defining->dlist = tline->next;
2093 tline->next = NULL;
2094 count_mmac_params(defining->dlist, &defining->ndefs,
2095 &defining->defaults);
2096 } else {
2097 defining->dlist = NULL;
2098 defining->defaults = NULL;
2099 }
2100 defining->expansion = NULL;
2101 free_tlist(origline);
2102 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002103
H. Peter Anvine2c80182005-01-15 22:15:51 +00002104 case PP_ENDM:
2105 case PP_ENDMACRO:
2106 if (!defining) {
2107 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2108 return DIRECTIVE_FOUND;
2109 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07002110 mmhead = (MMacro **) hash_findi_add(mmacros, defining->name);
2111 defining->next = *mmhead;
2112 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002113 defining = NULL;
2114 free_tlist(origline);
2115 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002116
H. Peter Anvine2c80182005-01-15 22:15:51 +00002117 case PP_ROTATE:
2118 if (tline->next && tline->next->type == TOK_WHITESPACE)
2119 tline = tline->next;
2120 if (tline->next == NULL) {
2121 free_tlist(origline);
2122 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2123 return DIRECTIVE_FOUND;
2124 }
2125 t = expand_smacro(tline->next);
2126 tline->next = NULL;
2127 free_tlist(origline);
2128 tline = t;
2129 tptr = &t;
2130 tokval.t_type = TOKEN_INVALID;
2131 evalresult =
2132 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2133 free_tlist(tline);
2134 if (!evalresult)
2135 return DIRECTIVE_FOUND;
2136 if (tokval.t_type)
2137 error(ERR_WARNING,
2138 "trailing garbage after expression ignored");
2139 if (!is_simple(evalresult)) {
2140 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2141 return DIRECTIVE_FOUND;
2142 }
2143 mmac = istk->mstk;
2144 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2145 mmac = mmac->next_active;
2146 if (!mmac) {
2147 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2148 } else if (mmac->nparam == 0) {
2149 error(ERR_NONFATAL,
2150 "`%%rotate' invoked within macro without parameters");
2151 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002152 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002153
H. Peter Anvin25a99342007-09-22 17:45:45 -07002154 rotate %= (int)mmac->nparam;
2155 if (rotate < 0)
2156 rotate += mmac->nparam;
2157
2158 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002159 }
2160 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002161
H. Peter Anvine2c80182005-01-15 22:15:51 +00002162 case PP_REP:
2163 nolist = FALSE;
2164 do {
2165 tline = tline->next;
2166 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002167
H. Peter Anvine2c80182005-01-15 22:15:51 +00002168 if (tok_type_(tline, TOK_ID) &&
2169 nasm_stricmp(tline->text, ".nolist") == 0) {
2170 nolist = TRUE;
2171 do {
2172 tline = tline->next;
2173 } while (tok_type_(tline, TOK_WHITESPACE));
2174 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002175
H. Peter Anvine2c80182005-01-15 22:15:51 +00002176 if (tline) {
2177 t = expand_smacro(tline);
2178 tptr = &t;
2179 tokval.t_type = TOKEN_INVALID;
2180 evalresult =
2181 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2182 if (!evalresult) {
2183 free_tlist(origline);
2184 return DIRECTIVE_FOUND;
2185 }
2186 if (tokval.t_type)
2187 error(ERR_WARNING,
2188 "trailing garbage after expression ignored");
2189 if (!is_simple(evalresult)) {
2190 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2191 return DIRECTIVE_FOUND;
2192 }
2193 i = (int)reloc_value(evalresult) + 1;
2194 } else {
2195 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2196 i = 0;
2197 }
2198 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002199
H. Peter Anvine2c80182005-01-15 22:15:51 +00002200 tmp_defining = defining;
2201 defining = nasm_malloc(sizeof(MMacro));
2202 defining->name = NULL; /* flags this macro as a %rep block */
2203 defining->casesense = 0;
2204 defining->plus = FALSE;
2205 defining->nolist = nolist;
2206 defining->in_progress = i;
2207 defining->nparam_min = defining->nparam_max = 0;
2208 defining->defaults = NULL;
2209 defining->dlist = NULL;
2210 defining->expansion = NULL;
2211 defining->next_active = istk->mstk;
2212 defining->rep_nest = tmp_defining;
2213 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002214
H. Peter Anvine2c80182005-01-15 22:15:51 +00002215 case PP_ENDREP:
2216 if (!defining || defining->name) {
2217 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2218 return DIRECTIVE_FOUND;
2219 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002220
H. Peter Anvine2c80182005-01-15 22:15:51 +00002221 /*
2222 * Now we have a "macro" defined - although it has no name
2223 * and we won't be entering it in the hash tables - we must
2224 * push a macro-end marker for it on to istk->expansion.
2225 * After that, it will take care of propagating itself (a
2226 * macro-end marker line for a macro which is really a %rep
2227 * block will cause the macro to be re-expanded, complete
2228 * with another macro-end marker to ensure the process
2229 * continues) until the whole expansion is forcibly removed
2230 * from istk->expansion by a %exitrep.
2231 */
2232 l = nasm_malloc(sizeof(Line));
2233 l->next = istk->expansion;
2234 l->finishes = defining;
2235 l->first = NULL;
2236 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002237
H. Peter Anvine2c80182005-01-15 22:15:51 +00002238 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002239
H. Peter Anvine2c80182005-01-15 22:15:51 +00002240 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2241 tmp_defining = defining;
2242 defining = defining->rep_nest;
2243 free_tlist(origline);
2244 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002245
H. Peter Anvine2c80182005-01-15 22:15:51 +00002246 case PP_EXITREP:
2247 /*
2248 * We must search along istk->expansion until we hit a
2249 * macro-end marker for a macro with no name. Then we set
2250 * its `in_progress' flag to 0.
2251 */
2252 for (l = istk->expansion; l; l = l->next)
2253 if (l->finishes && !l->finishes->name)
2254 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002255
H. Peter Anvine2c80182005-01-15 22:15:51 +00002256 if (l)
2257 l->finishes->in_progress = 0;
2258 else
2259 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2260 free_tlist(origline);
2261 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002262
H. Peter Anvine2c80182005-01-15 22:15:51 +00002263 case PP_XDEFINE:
2264 case PP_IXDEFINE:
2265 case PP_DEFINE:
2266 case PP_IDEFINE:
2267 tline = tline->next;
2268 skip_white_(tline);
2269 tline = expand_id(tline);
2270 if (!tline || (tline->type != TOK_ID &&
2271 (tline->type != TOK_PREPROC_ID ||
2272 tline->text[1] != '$'))) {
2273 error(ERR_NONFATAL,
2274 "`%%%s%sdefine' expects a macro identifier",
2275 ((i == PP_IDEFINE || i == PP_IXDEFINE) ? "i" : ""),
2276 ((i == PP_XDEFINE || i == PP_IXDEFINE) ? "x" : ""));
2277 free_tlist(origline);
2278 return DIRECTIVE_FOUND;
2279 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002280
H. Peter Anvine2c80182005-01-15 22:15:51 +00002281 ctx = get_ctx(tline->text, FALSE);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002282
H. Peter Anvine2c80182005-01-15 22:15:51 +00002283 mname = tline->text;
2284 last = tline;
2285 param_start = tline = tline->next;
2286 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002287
H. Peter Anvine2c80182005-01-15 22:15:51 +00002288 /* Expand the macro definition now for %xdefine and %ixdefine */
2289 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2290 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002291
H. Peter Anvine2c80182005-01-15 22:15:51 +00002292 if (tok_is_(tline, "(")) {
2293 /*
2294 * This macro has parameters.
2295 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002296
H. Peter Anvine2c80182005-01-15 22:15:51 +00002297 tline = tline->next;
2298 while (1) {
2299 skip_white_(tline);
2300 if (!tline) {
2301 error(ERR_NONFATAL, "parameter identifier expected");
2302 free_tlist(origline);
2303 return DIRECTIVE_FOUND;
2304 }
2305 if (tline->type != TOK_ID) {
2306 error(ERR_NONFATAL,
2307 "`%s': parameter identifier expected",
2308 tline->text);
2309 free_tlist(origline);
2310 return DIRECTIVE_FOUND;
2311 }
2312 tline->type = TOK_SMAC_PARAM + nparam++;
2313 tline = tline->next;
2314 skip_white_(tline);
2315 if (tok_is_(tline, ",")) {
2316 tline = tline->next;
2317 continue;
2318 }
2319 if (!tok_is_(tline, ")")) {
2320 error(ERR_NONFATAL,
2321 "`)' expected to terminate macro template");
2322 free_tlist(origline);
2323 return DIRECTIVE_FOUND;
2324 }
2325 break;
2326 }
2327 last = tline;
2328 tline = tline->next;
2329 }
2330 if (tok_type_(tline, TOK_WHITESPACE))
2331 last = tline, tline = tline->next;
2332 macro_start = NULL;
2333 last->next = NULL;
2334 t = tline;
2335 while (t) {
2336 if (t->type == TOK_ID) {
2337 for (tt = param_start; tt; tt = tt->next)
2338 if (tt->type >= TOK_SMAC_PARAM &&
2339 !strcmp(tt->text, t->text))
2340 t->type = tt->type;
2341 }
2342 tt = t->next;
2343 t->next = macro_start;
2344 macro_start = t;
2345 t = tt;
2346 }
2347 /*
2348 * Good. We now have a macro name, a parameter count, and a
2349 * token list (in reverse order) for an expansion. We ought
2350 * to be OK just to create an SMacro, store it, and let
2351 * free_tlist have the rest of the line (which we have
2352 * carefully re-terminated after chopping off the expansion
2353 * from the end).
2354 */
2355 if (smacro_defined(ctx, mname, nparam, &smac, i == PP_DEFINE)) {
2356 if (!smac) {
2357 error(ERR_WARNING,
2358 "single-line macro `%s' defined both with and"
2359 " without parameters", mname);
2360 free_tlist(origline);
2361 free_tlist(macro_start);
2362 return DIRECTIVE_FOUND;
2363 } else {
2364 /*
2365 * We're redefining, so we have to take over an
2366 * existing SMacro structure. This means freeing
2367 * what was already in it.
2368 */
2369 nasm_free(smac->name);
2370 free_tlist(smac->expansion);
2371 }
2372 } else {
H. Peter Anvin97a23472007-09-16 17:57:25 -07002373 if (!ctx)
2374 smhead = (SMacro **) hash_findi_add(smacros, mname);
2375 else
2376 smhead = &ctx->localmac;
2377
H. Peter Anvine2c80182005-01-15 22:15:51 +00002378 smac = nasm_malloc(sizeof(SMacro));
2379 smac->next = *smhead;
2380 *smhead = smac;
2381 }
2382 smac->name = nasm_strdup(mname);
2383 smac->casesense = ((i == PP_DEFINE) || (i == PP_XDEFINE));
2384 smac->nparam = nparam;
2385 smac->expansion = macro_start;
2386 smac->in_progress = FALSE;
2387 free_tlist(origline);
2388 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002389
H. Peter Anvine2c80182005-01-15 22:15:51 +00002390 case PP_UNDEF:
2391 tline = tline->next;
2392 skip_white_(tline);
2393 tline = expand_id(tline);
2394 if (!tline || (tline->type != TOK_ID &&
2395 (tline->type != TOK_PREPROC_ID ||
2396 tline->text[1] != '$'))) {
2397 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2398 free_tlist(origline);
2399 return DIRECTIVE_FOUND;
2400 }
2401 if (tline->next) {
2402 error(ERR_WARNING,
2403 "trailing garbage after macro name ignored");
2404 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002405
H. Peter Anvine2c80182005-01-15 22:15:51 +00002406 /* Find the context that symbol belongs to */
2407 ctx = get_ctx(tline->text, FALSE);
2408 if (!ctx)
H. Peter Anvin97a23472007-09-16 17:57:25 -07002409 smhead = (SMacro **) hash_findi(smacros, tline->text, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002410 else
2411 smhead = &ctx->localmac;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002412
H. Peter Anvin97a23472007-09-16 17:57:25 -07002413 if (smhead) {
2414 SMacro *s, **sp;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002415
H. Peter Anvin97a23472007-09-16 17:57:25 -07002416 mname = tline->text;
2417 last = tline;
2418 last->next = NULL;
2419
2420 /*
2421 * We now have a macro name... go hunt for it.
2422 */
2423 for (sp = smhead; *sp; sp = &(*sp)->next) {
2424 s = *sp;
2425 if (!mstrcmp(s->name, tline->text, s->casesense)) {
2426 *sp = s->next;
2427 nasm_free(s->name);
2428 free_tlist(s->expansion);
2429 nasm_free(s);
2430 }
2431 }
2432 free_tlist(origline);
2433 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002434 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002435
H. Peter Anvine2c80182005-01-15 22:15:51 +00002436 case PP_STRLEN:
2437 tline = tline->next;
2438 skip_white_(tline);
2439 tline = expand_id(tline);
2440 if (!tline || (tline->type != TOK_ID &&
2441 (tline->type != TOK_PREPROC_ID ||
2442 tline->text[1] != '$'))) {
2443 error(ERR_NONFATAL,
2444 "`%%strlen' expects a macro identifier as first parameter");
2445 free_tlist(origline);
2446 return DIRECTIVE_FOUND;
2447 }
2448 ctx = get_ctx(tline->text, FALSE);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002449
H. Peter Anvine2c80182005-01-15 22:15:51 +00002450 mname = tline->text;
2451 last = tline;
2452 tline = expand_smacro(tline->next);
2453 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002454
H. Peter Anvine2c80182005-01-15 22:15:51 +00002455 t = tline;
2456 while (tok_type_(t, TOK_WHITESPACE))
2457 t = t->next;
2458 /* t should now point to the string */
2459 if (t->type != TOK_STRING) {
2460 error(ERR_NONFATAL,
2461 "`%%strlen` requires string as second parameter");
2462 free_tlist(tline);
2463 free_tlist(origline);
2464 return DIRECTIVE_FOUND;
2465 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002466
H. Peter Anvine2c80182005-01-15 22:15:51 +00002467 macro_start = nasm_malloc(sizeof(*macro_start));
2468 macro_start->next = NULL;
2469 make_tok_num(macro_start, strlen(t->text) - 2);
2470 macro_start->mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002471
H. Peter Anvine2c80182005-01-15 22:15:51 +00002472 /*
2473 * We now have a macro name, an implicit parameter count of
2474 * zero, and a numeric token to use as an expansion. Create
2475 * and store an SMacro.
2476 */
2477 if (smacro_defined(ctx, mname, 0, &smac, i == PP_STRLEN)) {
2478 if (!smac)
2479 error(ERR_WARNING,
2480 "single-line macro `%s' defined both with and"
2481 " without parameters", mname);
2482 else {
2483 /*
2484 * We're redefining, so we have to take over an
2485 * existing SMacro structure. This means freeing
2486 * what was already in it.
2487 */
2488 nasm_free(smac->name);
2489 free_tlist(smac->expansion);
2490 }
2491 } else {
H. Peter Anvin97a23472007-09-16 17:57:25 -07002492 if (!ctx)
2493 smhead = (SMacro **) hash_findi_add(smacros, mname);
2494 else
2495 smhead = &ctx->localmac;
2496
H. Peter Anvine2c80182005-01-15 22:15:51 +00002497 smac = nasm_malloc(sizeof(SMacro));
2498 smac->next = *smhead;
2499 *smhead = smac;
2500 }
2501 smac->name = nasm_strdup(mname);
2502 smac->casesense = (i == PP_STRLEN);
2503 smac->nparam = 0;
2504 smac->expansion = macro_start;
2505 smac->in_progress = FALSE;
2506 free_tlist(tline);
2507 free_tlist(origline);
2508 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002509
H. Peter Anvine2c80182005-01-15 22:15:51 +00002510 case PP_SUBSTR:
2511 tline = tline->next;
2512 skip_white_(tline);
2513 tline = expand_id(tline);
2514 if (!tline || (tline->type != TOK_ID &&
2515 (tline->type != TOK_PREPROC_ID ||
2516 tline->text[1] != '$'))) {
2517 error(ERR_NONFATAL,
2518 "`%%substr' expects a macro identifier as first parameter");
2519 free_tlist(origline);
2520 return DIRECTIVE_FOUND;
2521 }
2522 ctx = get_ctx(tline->text, FALSE);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002523
H. Peter Anvine2c80182005-01-15 22:15:51 +00002524 mname = tline->text;
2525 last = tline;
2526 tline = expand_smacro(tline->next);
2527 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002528
H. Peter Anvine2c80182005-01-15 22:15:51 +00002529 t = tline->next;
2530 while (tok_type_(t, TOK_WHITESPACE))
2531 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002532
H. Peter Anvine2c80182005-01-15 22:15:51 +00002533 /* t should now point to the string */
2534 if (t->type != TOK_STRING) {
2535 error(ERR_NONFATAL,
2536 "`%%substr` requires string as second parameter");
2537 free_tlist(tline);
2538 free_tlist(origline);
2539 return DIRECTIVE_FOUND;
2540 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002541
H. Peter Anvine2c80182005-01-15 22:15:51 +00002542 tt = t->next;
2543 tptr = &tt;
2544 tokval.t_type = TOKEN_INVALID;
2545 evalresult =
2546 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2547 if (!evalresult) {
2548 free_tlist(tline);
2549 free_tlist(origline);
2550 return DIRECTIVE_FOUND;
2551 }
2552 if (!is_simple(evalresult)) {
2553 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2554 free_tlist(tline);
2555 free_tlist(origline);
2556 return DIRECTIVE_FOUND;
2557 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002558
H. Peter Anvine2c80182005-01-15 22:15:51 +00002559 macro_start = nasm_malloc(sizeof(*macro_start));
2560 macro_start->next = NULL;
2561 macro_start->text = nasm_strdup("'''");
2562 if (evalresult->value > 0
2563 && evalresult->value < strlen(t->text) - 1) {
2564 macro_start->text[1] = t->text[evalresult->value];
2565 } else {
2566 macro_start->text[2] = '\0';
2567 }
2568 macro_start->type = TOK_STRING;
2569 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002570
H. Peter Anvine2c80182005-01-15 22:15:51 +00002571 /*
2572 * We now have a macro name, an implicit parameter count of
2573 * zero, and a numeric token to use as an expansion. Create
2574 * and store an SMacro.
2575 */
2576 if (smacro_defined(ctx, mname, 0, &smac, i == PP_SUBSTR)) {
2577 if (!smac)
2578 error(ERR_WARNING,
2579 "single-line macro `%s' defined both with and"
2580 " without parameters", mname);
2581 else {
2582 /*
2583 * We're redefining, so we have to take over an
2584 * existing SMacro structure. This means freeing
2585 * what was already in it.
2586 */
2587 nasm_free(smac->name);
2588 free_tlist(smac->expansion);
2589 }
2590 } else {
H. Peter Anvin97a23472007-09-16 17:57:25 -07002591 if (!ctx)
2592 smhead = (SMacro **) hash_findi_add(smacros, tline->text);
2593 else
2594 smhead = &ctx->localmac;
2595
H. Peter Anvine2c80182005-01-15 22:15:51 +00002596 smac = nasm_malloc(sizeof(SMacro));
2597 smac->next = *smhead;
2598 *smhead = smac;
2599 }
2600 smac->name = nasm_strdup(mname);
2601 smac->casesense = (i == PP_SUBSTR);
2602 smac->nparam = 0;
2603 smac->expansion = macro_start;
2604 smac->in_progress = FALSE;
2605 free_tlist(tline);
2606 free_tlist(origline);
2607 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002608
H. Peter Anvine2c80182005-01-15 22:15:51 +00002609 case PP_ASSIGN:
2610 case PP_IASSIGN:
2611 tline = tline->next;
2612 skip_white_(tline);
2613 tline = expand_id(tline);
2614 if (!tline || (tline->type != TOK_ID &&
2615 (tline->type != TOK_PREPROC_ID ||
2616 tline->text[1] != '$'))) {
2617 error(ERR_NONFATAL,
2618 "`%%%sassign' expects a macro identifier",
2619 (i == PP_IASSIGN ? "i" : ""));
2620 free_tlist(origline);
2621 return DIRECTIVE_FOUND;
2622 }
2623 ctx = get_ctx(tline->text, FALSE);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002624
H. Peter Anvine2c80182005-01-15 22:15:51 +00002625 mname = tline->text;
2626 last = tline;
2627 tline = expand_smacro(tline->next);
2628 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002629
H. Peter Anvine2c80182005-01-15 22:15:51 +00002630 t = tline;
2631 tptr = &t;
2632 tokval.t_type = TOKEN_INVALID;
2633 evalresult =
2634 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2635 free_tlist(tline);
2636 if (!evalresult) {
2637 free_tlist(origline);
2638 return DIRECTIVE_FOUND;
2639 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002640
H. Peter Anvine2c80182005-01-15 22:15:51 +00002641 if (tokval.t_type)
2642 error(ERR_WARNING,
2643 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00002644
H. Peter Anvine2c80182005-01-15 22:15:51 +00002645 if (!is_simple(evalresult)) {
2646 error(ERR_NONFATAL,
2647 "non-constant value given to `%%%sassign'",
2648 (i == PP_IASSIGN ? "i" : ""));
2649 free_tlist(origline);
2650 return DIRECTIVE_FOUND;
2651 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002652
H. Peter Anvine2c80182005-01-15 22:15:51 +00002653 macro_start = nasm_malloc(sizeof(*macro_start));
2654 macro_start->next = NULL;
2655 make_tok_num(macro_start, reloc_value(evalresult));
2656 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002657
H. Peter Anvine2c80182005-01-15 22:15:51 +00002658 /*
2659 * We now have a macro name, an implicit parameter count of
2660 * zero, and a numeric token to use as an expansion. Create
2661 * and store an SMacro.
2662 */
2663 if (smacro_defined(ctx, mname, 0, &smac, i == PP_ASSIGN)) {
2664 if (!smac)
2665 error(ERR_WARNING,
2666 "single-line macro `%s' defined both with and"
2667 " without parameters", mname);
2668 else {
2669 /*
2670 * We're redefining, so we have to take over an
2671 * existing SMacro structure. This means freeing
2672 * what was already in it.
2673 */
2674 nasm_free(smac->name);
2675 free_tlist(smac->expansion);
2676 }
2677 } else {
H. Peter Anvin97a23472007-09-16 17:57:25 -07002678 if (!ctx)
2679 smhead = (SMacro **) hash_findi_add(smacros, mname);
2680 else
2681 smhead = &ctx->localmac;
2682
H. Peter Anvine2c80182005-01-15 22:15:51 +00002683 smac = nasm_malloc(sizeof(SMacro));
2684 smac->next = *smhead;
2685 *smhead = smac;
2686 }
2687 smac->name = nasm_strdup(mname);
2688 smac->casesense = (i == PP_ASSIGN);
2689 smac->nparam = 0;
2690 smac->expansion = macro_start;
2691 smac->in_progress = FALSE;
2692 free_tlist(origline);
2693 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002694
H. Peter Anvine2c80182005-01-15 22:15:51 +00002695 case PP_LINE:
2696 /*
2697 * Syntax is `%line nnn[+mmm] [filename]'
2698 */
2699 tline = tline->next;
2700 skip_white_(tline);
2701 if (!tok_type_(tline, TOK_NUMBER)) {
2702 error(ERR_NONFATAL, "`%%line' expects line number");
2703 free_tlist(origline);
2704 return DIRECTIVE_FOUND;
2705 }
2706 k = readnum(tline->text, &j);
2707 m = 1;
2708 tline = tline->next;
2709 if (tok_is_(tline, "+")) {
2710 tline = tline->next;
2711 if (!tok_type_(tline, TOK_NUMBER)) {
2712 error(ERR_NONFATAL, "`%%line' expects line increment");
2713 free_tlist(origline);
2714 return DIRECTIVE_FOUND;
2715 }
2716 m = readnum(tline->text, &j);
2717 tline = tline->next;
2718 }
2719 skip_white_(tline);
2720 src_set_linnum(k);
2721 istk->lineinc = m;
2722 if (tline) {
2723 nasm_free(src_set_fname(detoken(tline, FALSE)));
2724 }
2725 free_tlist(origline);
2726 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002727
H. Peter Anvine2c80182005-01-15 22:15:51 +00002728 default:
2729 error(ERR_FATAL,
2730 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00002731 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002732 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002733 }
Ed Beroset3ab3f412002-06-11 03:31:49 +00002734 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002735}
2736
2737/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002738 * Ensure that a macro parameter contains a condition code and
2739 * nothing else. Return the condition code index if so, or -1
2740 * otherwise.
2741 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002742static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002743{
H. Peter Anvin76690a12002-04-30 20:52:49 +00002744 Token *tt;
2745 int i, j, k, m;
2746
H. Peter Anvin25a99342007-09-22 17:45:45 -07002747 if (!t)
2748 return -1; /* Probably a %+ without a space */
2749
H. Peter Anvineba20a72002-04-30 20:53:55 +00002750 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002751 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002752 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002753 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002754 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002755 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002756 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002757
2758 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00002759 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002760 while (j - i > 1) {
2761 k = (j + i) / 2;
2762 m = nasm_stricmp(t->text, conditions[k]);
2763 if (m == 0) {
2764 i = k;
2765 j = -2;
2766 break;
2767 } else if (m < 0) {
2768 j = k;
2769 } else
2770 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002771 }
2772 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002773 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002774 return i;
2775}
2776
2777/*
2778 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2779 * %-n) and MMacro-local identifiers (%%foo).
2780 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002781static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002782{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002783 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002784
2785 tail = &thead;
2786 thead = NULL;
2787
H. Peter Anvine2c80182005-01-15 22:15:51 +00002788 while (tline) {
2789 if (tline->type == TOK_PREPROC_ID &&
2790 (((tline->text[1] == '+' || tline->text[1] == '-')
2791 && tline->text[2]) || tline->text[1] == '%'
2792 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002793 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002794 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002795 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07002796 unsigned int n;
2797 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002798 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002799
H. Peter Anvine2c80182005-01-15 22:15:51 +00002800 t = tline;
2801 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002802
H. Peter Anvine2c80182005-01-15 22:15:51 +00002803 mac = istk->mstk;
2804 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2805 mac = mac->next_active;
2806 if (!mac)
2807 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2808 else
2809 switch (t->text[1]) {
2810 /*
2811 * We have to make a substitution of one of the
2812 * forms %1, %-1, %+1, %%foo, %0.
2813 */
2814 case '0':
2815 type = TOK_NUMBER;
2816 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
2817 text = nasm_strdup(tmpbuf);
2818 break;
2819 case '%':
2820 type = TOK_ID;
Keith Kanios93f2e9a2007-04-14 00:10:59 +00002821 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu32".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002822 mac->unique);
2823 text = nasm_strcat(tmpbuf, t->text + 2);
2824 break;
2825 case '-':
2826 n = atoi(t->text + 2) - 1;
2827 if (n >= mac->nparam)
2828 tt = NULL;
2829 else {
2830 if (mac->nparam > 1)
2831 n = (n + mac->rotate) % mac->nparam;
2832 tt = mac->params[n];
2833 }
2834 cc = find_cc(tt);
2835 if (cc == -1) {
2836 error(ERR_NONFATAL,
2837 "macro parameter %d is not a condition code",
2838 n + 1);
2839 text = NULL;
2840 } else {
2841 type = TOK_ID;
2842 if (inverse_ccs[cc] == -1) {
2843 error(ERR_NONFATAL,
2844 "condition code `%s' is not invertible",
2845 conditions[cc]);
2846 text = NULL;
2847 } else
2848 text =
2849 nasm_strdup(conditions[inverse_ccs[cc]]);
2850 }
2851 break;
2852 case '+':
2853 n = atoi(t->text + 2) - 1;
2854 if (n >= mac->nparam)
2855 tt = NULL;
2856 else {
2857 if (mac->nparam > 1)
2858 n = (n + mac->rotate) % mac->nparam;
2859 tt = mac->params[n];
2860 }
2861 cc = find_cc(tt);
2862 if (cc == -1) {
2863 error(ERR_NONFATAL,
2864 "macro parameter %d is not a condition code",
2865 n + 1);
2866 text = NULL;
2867 } else {
2868 type = TOK_ID;
2869 text = nasm_strdup(conditions[cc]);
2870 }
2871 break;
2872 default:
2873 n = atoi(t->text + 1) - 1;
2874 if (n >= mac->nparam)
2875 tt = NULL;
2876 else {
2877 if (mac->nparam > 1)
2878 n = (n + mac->rotate) % mac->nparam;
2879 tt = mac->params[n];
2880 }
2881 if (tt) {
2882 for (i = 0; i < mac->paramlen[n]; i++) {
2883 *tail = new_Token(NULL, tt->type, tt->text, 0);
2884 tail = &(*tail)->next;
2885 tt = tt->next;
2886 }
2887 }
2888 text = NULL; /* we've done it here */
2889 break;
2890 }
2891 if (!text) {
2892 delete_Token(t);
2893 } else {
2894 *tail = t;
2895 tail = &t->next;
2896 t->type = type;
2897 nasm_free(t->text);
2898 t->text = text;
2899 t->mac = NULL;
2900 }
2901 continue;
2902 } else {
2903 t = *tail = tline;
2904 tline = tline->next;
2905 t->mac = NULL;
2906 tail = &t->next;
2907 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002908 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00002909 *tail = NULL;
2910 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002911 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002912 switch (t->type) {
2913 case TOK_WHITESPACE:
2914 if (tt->type == TOK_WHITESPACE) {
2915 t->next = delete_Token(tt);
2916 }
2917 break;
2918 case TOK_ID:
2919 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002920 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002921 nasm_free(t->text);
2922 t->text = tmp;
2923 t->next = delete_Token(tt);
2924 }
2925 break;
2926 case TOK_NUMBER:
2927 if (tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002928 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002929 nasm_free(t->text);
2930 t->text = tmp;
2931 t->next = delete_Token(tt);
2932 }
2933 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002934 default:
2935 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002936 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002937
H. Peter Anvin76690a12002-04-30 20:52:49 +00002938 return thead;
2939}
2940
2941/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002942 * Expand all single-line macro calls made in the given line.
2943 * Return the expanded version of the line. The original is deemed
2944 * to be destroyed in the process. (In reality we'll just move
2945 * Tokens from input to output a lot of the time, rather than
2946 * actually bothering to destroy and replicate.)
2947 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002948static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002949{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002950 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002951 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002952 Token **params;
2953 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07002954 unsigned int nparam, sparam;
2955 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002956 Token *org_tline = tline;
2957 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002958 char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002959
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002960 /*
2961 * Trick: we should avoid changing the start token pointer since it can
2962 * be contained in "next" field of other token. Because of this
2963 * we allocate a copy of first token and work with it; at the end of
2964 * routine we copy it back
2965 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002966 if (org_tline) {
2967 tline =
2968 new_Token(org_tline->next, org_tline->type, org_tline->text,
2969 0);
2970 tline->mac = org_tline->mac;
2971 nasm_free(org_tline->text);
2972 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002973 }
2974
H. Peter Anvin734b1882002-04-30 21:01:08 +00002975 again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002976 tail = &thead;
2977 thead = NULL;
2978
H. Peter Anvine2c80182005-01-15 22:15:51 +00002979 while (tline) { /* main token loop */
2980 if ((mname = tline->text)) {
2981 /* if this token is a local macro, look in local context */
2982 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
2983 ctx = get_ctx(mname, TRUE);
2984 else
2985 ctx = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002986 if (!ctx) {
2987 head = (SMacro *) hash_findix(smacros, mname);
2988 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002989 head = ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002990 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002991 /*
2992 * We've hit an identifier. As in is_mmacro below, we first
2993 * check whether the identifier is a single-line macro at
2994 * all, then think about checking for parameters if
2995 * necessary.
2996 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07002997 for (m = head; m; m = m->next)
2998 if (!mstrcmp(m->name, mname, m->casesense))
2999 break;
3000 if (m) {
3001 mstart = tline;
3002 params = NULL;
3003 paramsize = NULL;
3004 if (m->nparam == 0) {
3005 /*
3006 * Simple case: the macro is parameterless. Discard the
3007 * one token that the macro call took, and push the
3008 * expansion back on the to-do stack.
3009 */
3010 if (!m->expansion) {
3011 if (!strcmp("__FILE__", m->name)) {
3012 int32_t num = 0;
3013 src_get(&num, &(tline->text));
3014 nasm_quote(&(tline->text));
3015 tline->type = TOK_STRING;
3016 continue;
3017 }
3018 if (!strcmp("__LINE__", m->name)) {
3019 nasm_free(tline->text);
3020 make_tok_num(tline, src_get_linnum());
3021 continue;
3022 }
3023 if (!strcmp("__BITS__", m->name)) {
3024 nasm_free(tline->text);
3025 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003026 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003027 }
3028 tline = delete_Token(tline);
3029 continue;
3030 }
3031 } else {
3032 /*
3033 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003034 * exists and takes parameters. We must find the
3035 * parameters in the call, count them, find the SMacro
3036 * that corresponds to that form of the macro call, and
3037 * substitute for the parameters when we expand. What a
3038 * pain.
3039 */
3040 /*tline = tline->next;
3041 skip_white_(tline); */
3042 do {
3043 t = tline->next;
3044 while (tok_type_(t, TOK_SMAC_END)) {
3045 t->mac->in_progress = FALSE;
3046 t->text = NULL;
3047 t = tline->next = delete_Token(t);
3048 }
3049 tline = t;
3050 } while (tok_type_(tline, TOK_WHITESPACE));
3051 if (!tok_is_(tline, "(")) {
3052 /*
3053 * This macro wasn't called with parameters: ignore
3054 * the call. (Behaviour borrowed from gnu cpp.)
3055 */
3056 tline = mstart;
3057 m = NULL;
3058 } else {
3059 int paren = 0;
3060 int white = 0;
3061 brackets = 0;
3062 nparam = 0;
3063 sparam = PARAM_DELTA;
3064 params = nasm_malloc(sparam * sizeof(Token *));
3065 params[0] = tline->next;
3066 paramsize = nasm_malloc(sparam * sizeof(int));
3067 paramsize[0] = 0;
3068 while (TRUE) { /* parameter loop */
3069 /*
3070 * For some unusual expansions
3071 * which concatenates function call
3072 */
3073 t = tline->next;
3074 while (tok_type_(t, TOK_SMAC_END)) {
3075 t->mac->in_progress = FALSE;
3076 t->text = NULL;
3077 t = tline->next = delete_Token(t);
3078 }
3079 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003080
H. Peter Anvine2c80182005-01-15 22:15:51 +00003081 if (!tline) {
3082 error(ERR_NONFATAL,
3083 "macro call expects terminating `)'");
3084 break;
3085 }
3086 if (tline->type == TOK_WHITESPACE
3087 && brackets <= 0) {
3088 if (paramsize[nparam])
3089 white++;
3090 else
3091 params[nparam] = tline->next;
3092 continue; /* parameter loop */
3093 }
3094 if (tline->type == TOK_OTHER
3095 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003096 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003097 if (ch == ',' && !paren && brackets <= 0) {
3098 if (++nparam >= sparam) {
3099 sparam += PARAM_DELTA;
3100 params = nasm_realloc(params,
3101 sparam *
3102 sizeof(Token
3103 *));
3104 paramsize =
3105 nasm_realloc(paramsize,
3106 sparam *
3107 sizeof(int));
3108 }
3109 params[nparam] = tline->next;
3110 paramsize[nparam] = 0;
3111 white = 0;
3112 continue; /* parameter loop */
3113 }
3114 if (ch == '{' &&
3115 (brackets > 0 || (brackets == 0 &&
3116 !paramsize[nparam])))
3117 {
3118 if (!(brackets++)) {
3119 params[nparam] = tline->next;
3120 continue; /* parameter loop */
3121 }
3122 }
3123 if (ch == '}' && brackets > 0)
3124 if (--brackets == 0) {
3125 brackets = -1;
3126 continue; /* parameter loop */
3127 }
3128 if (ch == '(' && !brackets)
3129 paren++;
3130 if (ch == ')' && brackets <= 0)
3131 if (--paren < 0)
3132 break;
3133 }
3134 if (brackets < 0) {
3135 brackets = 0;
3136 error(ERR_NONFATAL, "braces do not "
3137 "enclose all of macro parameter");
3138 }
3139 paramsize[nparam] += white + 1;
3140 white = 0;
3141 } /* parameter loop */
3142 nparam++;
3143 while (m && (m->nparam != nparam ||
3144 mstrcmp(m->name, mname,
3145 m->casesense)))
3146 m = m->next;
3147 if (!m)
3148 error(ERR_WARNING | ERR_WARN_MNP,
3149 "macro `%s' exists, "
3150 "but not taking %d parameters",
3151 mstart->text, nparam);
3152 }
3153 }
3154 if (m && m->in_progress)
3155 m = NULL;
3156 if (!m) { /* in progess or didn't find '(' or wrong nparam */
3157 /*
3158 * Design question: should we handle !tline, which
3159 * indicates missing ')' here, or expand those
3160 * macros anyway, which requires the (t) test a few
3161 * lines down?
3162 */
3163 nasm_free(params);
3164 nasm_free(paramsize);
3165 tline = mstart;
3166 } else {
3167 /*
3168 * Expand the macro: we are placed on the last token of the
3169 * call, so that we can easily split the call from the
3170 * following tokens. We also start by pushing an SMAC_END
3171 * token for the cycle removal.
3172 */
3173 t = tline;
3174 if (t) {
3175 tline = t->next;
3176 t->next = NULL;
3177 }
3178 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3179 tt->mac = m;
3180 m->in_progress = TRUE;
3181 tline = tt;
3182 for (t = m->expansion; t; t = t->next) {
3183 if (t->type >= TOK_SMAC_PARAM) {
3184 Token *pcopy = tline, **ptail = &pcopy;
3185 Token *ttt, *pt;
3186 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003187
H. Peter Anvine2c80182005-01-15 22:15:51 +00003188 ttt = params[t->type - TOK_SMAC_PARAM];
3189 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3190 --i >= 0;) {
3191 pt = *ptail =
3192 new_Token(tline, ttt->type, ttt->text,
3193 0);
3194 ptail = &pt->next;
3195 ttt = ttt->next;
3196 }
3197 tline = pcopy;
3198 } else {
3199 tt = new_Token(tline, t->type, t->text, 0);
3200 tline = tt;
3201 }
3202 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003203
H. Peter Anvine2c80182005-01-15 22:15:51 +00003204 /*
3205 * Having done that, get rid of the macro call, and clean
3206 * up the parameters.
3207 */
3208 nasm_free(params);
3209 nasm_free(paramsize);
3210 free_tlist(mstart);
3211 continue; /* main token loop */
3212 }
3213 }
3214 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003215
H. Peter Anvine2c80182005-01-15 22:15:51 +00003216 if (tline->type == TOK_SMAC_END) {
3217 tline->mac->in_progress = FALSE;
3218 tline = delete_Token(tline);
3219 } else {
3220 t = *tail = tline;
3221 tline = tline->next;
3222 t->mac = NULL;
3223 t->next = NULL;
3224 tail = &t->next;
3225 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003226 }
3227
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003228 /*
3229 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003230 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003231 * TOK_IDs should be concatenated.
3232 * Also we look for %+ tokens and concatenate the tokens before and after
3233 * them (without white spaces in between).
3234 */
3235 t = thead;
3236 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003237 while (t) {
3238 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3239 t = t->next;
3240 if (!t || !t->next)
3241 break;
3242 if (t->next->type == TOK_ID ||
3243 t->next->type == TOK_PREPROC_ID ||
3244 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003245 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003246 nasm_free(t->text);
3247 t->next = delete_Token(t->next);
3248 t->text = p;
3249 rescan = 1;
3250 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3251 t->next->next->type == TOK_PREPROC_ID &&
3252 strcmp(t->next->next->text, "%+") == 0) {
3253 /* free the next whitespace, the %+ token and next whitespace */
3254 int i;
3255 for (i = 1; i <= 3; i++) {
3256 if (!t->next
3257 || (i != 2 && t->next->type != TOK_WHITESPACE))
3258 break;
3259 t->next = delete_Token(t->next);
3260 } /* endfor */
3261 } else
3262 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003263 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003264 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003265 if (rescan) {
3266 tline = thead;
3267 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003268 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003269
H. Peter Anvine2c80182005-01-15 22:15:51 +00003270 if (org_tline) {
3271 if (thead) {
3272 *org_tline = *thead;
3273 /* since we just gave text to org_line, don't free it */
3274 thead->text = NULL;
3275 delete_Token(thead);
3276 } else {
3277 /* the expression expanded to empty line;
3278 we can't return NULL for some reasons
3279 we just set the line to a single WHITESPACE token. */
3280 memset(org_tline, 0, sizeof(*org_tline));
3281 org_tline->text = NULL;
3282 org_tline->type = TOK_WHITESPACE;
3283 }
3284 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003285 }
3286
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003287 return thead;
3288}
3289
3290/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003291 * Similar to expand_smacro but used exclusively with macro identifiers
3292 * right before they are fetched in. The reason is that there can be
3293 * identifiers consisting of several subparts. We consider that if there
3294 * are more than one element forming the name, user wants a expansion,
3295 * otherwise it will be left as-is. Example:
3296 *
3297 * %define %$abc cde
3298 *
3299 * the identifier %$abc will be left as-is so that the handler for %define
3300 * will suck it and define the corresponding value. Other case:
3301 *
3302 * %define _%$abc cde
3303 *
3304 * In this case user wants name to be expanded *before* %define starts
3305 * working, so we'll expand %$abc into something (if it has a value;
3306 * otherwise it will be left as-is) then concatenate all successive
3307 * PP_IDs into one.
3308 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003309static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003310{
3311 Token *cur, *oldnext = NULL;
3312
H. Peter Anvin734b1882002-04-30 21:01:08 +00003313 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003314 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003315
3316 cur = tline;
3317 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003318 (cur->next->type == TOK_ID ||
3319 cur->next->type == TOK_PREPROC_ID
3320 || cur->next->type == TOK_NUMBER))
3321 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003322
3323 /* If identifier consists of just one token, don't expand */
3324 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003325 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003326
H. Peter Anvine2c80182005-01-15 22:15:51 +00003327 if (cur) {
3328 oldnext = cur->next; /* Detach the tail past identifier */
3329 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003330 }
3331
H. Peter Anvin734b1882002-04-30 21:01:08 +00003332 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003333
H. Peter Anvine2c80182005-01-15 22:15:51 +00003334 if (cur) {
3335 /* expand_smacro possibly changhed tline; re-scan for EOL */
3336 cur = tline;
3337 while (cur && cur->next)
3338 cur = cur->next;
3339 if (cur)
3340 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003341 }
3342
3343 return tline;
3344}
3345
3346/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003347 * Determine whether the given line constitutes a multi-line macro
3348 * call, and return the MMacro structure called if so. Doesn't have
3349 * to check for an initial label - that's taken care of in
3350 * expand_mmacro - but must check numbers of parameters. Guaranteed
3351 * to be called with tline->type == TOK_ID, so the putative macro
3352 * name is easy to find.
3353 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003354static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003355{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003356 MMacro *head, *m;
3357 Token **params;
3358 int nparam;
3359
H. Peter Anvin97a23472007-09-16 17:57:25 -07003360 head = (MMacro *) hash_findix(mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003361
3362 /*
3363 * Efficiency: first we see if any macro exists with the given
3364 * name. If not, we can return NULL immediately. _Then_ we
3365 * count the parameters, and then we look further along the
3366 * list if necessary to find the proper MMacro.
3367 */
3368 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003369 if (!mstrcmp(m->name, tline->text, m->casesense))
3370 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003371 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003372 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003373
3374 /*
3375 * OK, we have a potential macro. Count and demarcate the
3376 * parameters.
3377 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003378 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003379
3380 /*
3381 * So we know how many parameters we've got. Find the MMacro
3382 * structure that handles this number.
3383 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003384 while (m) {
3385 if (m->nparam_min <= nparam
3386 && (m->plus || nparam <= m->nparam_max)) {
3387 /*
3388 * This one is right. Just check if cycle removal
3389 * prohibits us using it before we actually celebrate...
3390 */
3391 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003392#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003393 error(ERR_NONFATAL,
3394 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003395#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003396 nasm_free(params);
3397 return NULL;
3398 }
3399 /*
3400 * It's right, and we can use it. Add its default
3401 * parameters to the end of our list if necessary.
3402 */
3403 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3404 params =
3405 nasm_realloc(params,
3406 ((m->nparam_min + m->ndefs +
3407 1) * sizeof(*params)));
3408 while (nparam < m->nparam_min + m->ndefs) {
3409 params[nparam] = m->defaults[nparam - m->nparam_min];
3410 nparam++;
3411 }
3412 }
3413 /*
3414 * If we've gone over the maximum parameter count (and
3415 * we're in Plus mode), ignore parameters beyond
3416 * nparam_max.
3417 */
3418 if (m->plus && nparam > m->nparam_max)
3419 nparam = m->nparam_max;
3420 /*
3421 * Then terminate the parameter list, and leave.
3422 */
3423 if (!params) { /* need this special case */
3424 params = nasm_malloc(sizeof(*params));
3425 nparam = 0;
3426 }
3427 params[nparam] = NULL;
3428 *params_array = params;
3429 return m;
3430 }
3431 /*
3432 * This one wasn't right: look for the next one with the
3433 * same name.
3434 */
3435 for (m = m->next; m; m = m->next)
3436 if (!mstrcmp(m->name, tline->text, m->casesense))
3437 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003438 }
3439
3440 /*
3441 * After all that, we didn't find one with the right number of
3442 * parameters. Issue a warning, and fail to expand the macro.
3443 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003444 error(ERR_WARNING | ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003445 "macro `%s' exists, but not taking %d parameters",
3446 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003447 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003448 return NULL;
3449}
3450
3451/*
3452 * Expand the multi-line macro call made by the given line, if
3453 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003454 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003455 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003456static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003457{
3458 Token *startline = tline;
3459 Token *label = NULL;
3460 int dont_prepend = 0;
3461 Token **params, *t, *tt;
3462 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003463 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003464 int i, nparam, *paramlen;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003465
3466 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003467 skip_white_(t);
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003468/* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3469 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003470 return 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003471 m = is_mmacro(t, &params);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003472 if (!m) {
3473 Token *last;
3474 /*
3475 * We have an id which isn't a macro call. We'll assume
3476 * it might be a label; we'll also check to see if a
3477 * colon follows it. Then, if there's another id after
3478 * that lot, we'll check it again for macro-hood.
3479 */
3480 label = last = t;
3481 t = t->next;
3482 if (tok_type_(t, TOK_WHITESPACE))
3483 last = t, t = t->next;
3484 if (tok_is_(t, ":")) {
3485 dont_prepend = 1;
3486 last = t, t = t->next;
3487 if (tok_type_(t, TOK_WHITESPACE))
3488 last = t, t = t->next;
3489 }
3490 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3491 return 0;
3492 last->next = NULL;
3493 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003494 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003495
3496 /*
3497 * Fix up the parameters: this involves stripping leading and
3498 * trailing whitespace, then stripping braces if they are
3499 * present.
3500 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003501 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003502 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003503
H. Peter Anvine2c80182005-01-15 22:15:51 +00003504 for (i = 0; params[i]; i++) {
3505 int brace = FALSE;
3506 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003507
H. Peter Anvine2c80182005-01-15 22:15:51 +00003508 t = params[i];
3509 skip_white_(t);
3510 if (tok_is_(t, "{"))
3511 t = t->next, brace = TRUE, comma = FALSE;
3512 params[i] = t;
3513 paramlen[i] = 0;
3514 while (t) {
3515 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3516 break; /* ... because we have hit a comma */
3517 if (comma && t->type == TOK_WHITESPACE
3518 && tok_is_(t->next, ","))
3519 break; /* ... or a space then a comma */
3520 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3521 break; /* ... or a brace */
3522 t = t->next;
3523 paramlen[i]++;
3524 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003525 }
3526
3527 /*
3528 * OK, we have a MMacro structure together with a set of
3529 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003530 * copies of each Line on to istk->expansion. Substitution of
3531 * parameter tokens and macro-local tokens doesn't get done
3532 * until the single-line macro substitution process; this is
3533 * because delaying them allows us to change the semantics
3534 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003535 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003536 * First, push an end marker on to istk->expansion, mark this
3537 * macro as in progress, and set up its invocation-specific
3538 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003539 */
3540 ll = nasm_malloc(sizeof(Line));
3541 ll->next = istk->expansion;
3542 ll->finishes = m;
3543 ll->first = NULL;
3544 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003545
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003546 m->in_progress = TRUE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003547 m->params = params;
3548 m->iline = tline;
3549 m->nparam = nparam;
3550 m->rotate = 0;
3551 m->paramlen = paramlen;
3552 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003553 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003554
3555 m->next_active = istk->mstk;
3556 istk->mstk = m;
3557
H. Peter Anvine2c80182005-01-15 22:15:51 +00003558 for (l = m->expansion; l; l = l->next) {
3559 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003560
H. Peter Anvine2c80182005-01-15 22:15:51 +00003561 ll = nasm_malloc(sizeof(Line));
3562 ll->finishes = NULL;
3563 ll->next = istk->expansion;
3564 istk->expansion = ll;
3565 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003566
H. Peter Anvine2c80182005-01-15 22:15:51 +00003567 for (t = l->first; t; t = t->next) {
3568 Token *x = t;
3569 if (t->type == TOK_PREPROC_ID &&
3570 t->text[1] == '0' && t->text[2] == '0') {
3571 dont_prepend = -1;
3572 x = label;
3573 if (!x)
3574 continue;
3575 }
3576 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3577 tail = &tt->next;
3578 }
3579 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003580 }
3581
3582 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00003583 * If we had a label, push it on as the first line of
3584 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003585 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003586 if (label) {
3587 if (dont_prepend < 0)
3588 free_tlist(startline);
3589 else {
3590 ll = nasm_malloc(sizeof(Line));
3591 ll->finishes = NULL;
3592 ll->next = istk->expansion;
3593 istk->expansion = ll;
3594 ll->first = startline;
3595 if (!dont_prepend) {
3596 while (label->next)
3597 label = label->next;
3598 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3599 }
3600 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003601 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003602
H. Peter Anvin734b1882002-04-30 21:01:08 +00003603 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003604
H. Peter Anvineba20a72002-04-30 20:53:55 +00003605 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003606}
3607
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003608/*
3609 * Since preprocessor always operate only on the line that didn't
3610 * arrived yet, we should always use ERR_OFFBY1. Also since user
3611 * won't want to see same error twice (preprocessing is done once
3612 * per pass) we will want to show errors only during pass one.
3613 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003614static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003615{
3616 va_list arg;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003617 char buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003618
3619 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003620 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003621 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003622
H. Peter Anvin734b1882002-04-30 21:01:08 +00003623 va_start(arg, fmt);
Ed Beroset19f927a2004-12-15 17:07:03 +00003624 vsnprintf(buff, sizeof(buff), fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003625 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003626
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003627 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003628 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3629 istk->mstk->lineno, buff);
3630 else
3631 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003632}
3633
H. Peter Anvin734b1882002-04-30 21:01:08 +00003634static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003635pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003636 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003637{
H. Peter Anvin99941bf2002-05-14 17:44:03 +00003638 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003639 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003640 istk = nasm_malloc(sizeof(Include));
3641 istk->next = NULL;
3642 istk->conds = NULL;
3643 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003644 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003645 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00003646 istk->fname = NULL;
3647 src_set_fname(nasm_strdup(file));
3648 src_set_linnum(0);
3649 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003650 if (!istk->fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003651 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3652 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003653 defining = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003654 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003655 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003656 if (tasm_compatible_mode) {
3657 stdmacpos = stdmac;
3658 } else {
3659 stdmacpos = &stdmac[TASM_MACRO_COUNT];
3660 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003661 any_extrastdmac = (extrastdmac != NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003662 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003663 evaluate = eval;
3664 pass = apass;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003665}
3666
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003667static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003668{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003669 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003670 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003671
H. Peter Anvine2c80182005-01-15 22:15:51 +00003672 while (1) {
3673 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003674 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00003675 * buffer or from the input file.
3676 */
3677 tline = NULL;
3678 while (istk->expansion && istk->expansion->finishes) {
3679 Line *l = istk->expansion;
3680 if (!l->finishes->name && l->finishes->in_progress > 1) {
3681 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003682
H. Peter Anvine2c80182005-01-15 22:15:51 +00003683 /*
3684 * This is a macro-end marker for a macro with no
3685 * name, which means it's not really a macro at all
3686 * but a %rep block, and the `in_progress' field is
3687 * more than 1, meaning that we still need to
3688 * repeat. (1 means the natural last repetition; 0
3689 * means termination by %exitrep.) We have
3690 * therefore expanded up to the %endrep, and must
3691 * push the whole block on to the expansion buffer
3692 * again. We don't bother to remove the macro-end
3693 * marker: we'd only have to generate another one
3694 * if we did.
3695 */
3696 l->finishes->in_progress--;
3697 for (l = l->finishes->expansion; l; l = l->next) {
3698 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003699
H. Peter Anvine2c80182005-01-15 22:15:51 +00003700 ll = nasm_malloc(sizeof(Line));
3701 ll->next = istk->expansion;
3702 ll->finishes = NULL;
3703 ll->first = NULL;
3704 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003705
H. Peter Anvine2c80182005-01-15 22:15:51 +00003706 for (t = l->first; t; t = t->next) {
3707 if (t->text || t->type == TOK_WHITESPACE) {
3708 tt = *tail =
3709 new_Token(NULL, t->type, t->text, 0);
3710 tail = &tt->next;
3711 }
3712 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003713
H. Peter Anvine2c80182005-01-15 22:15:51 +00003714 istk->expansion = ll;
3715 }
3716 } else {
3717 /*
3718 * Check whether a `%rep' was started and not ended
3719 * within this macro expansion. This can happen and
3720 * should be detected. It's a fatal error because
3721 * I'm too confused to work out how to recover
3722 * sensibly from it.
3723 */
3724 if (defining) {
3725 if (defining->name)
3726 error(ERR_PANIC,
3727 "defining with name in expansion");
3728 else if (istk->mstk->name)
3729 error(ERR_FATAL,
3730 "`%%rep' without `%%endrep' within"
3731 " expansion of macro `%s'",
3732 istk->mstk->name);
3733 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00003734
H. Peter Anvine2c80182005-01-15 22:15:51 +00003735 /*
3736 * FIXME: investigate the relationship at this point between
3737 * istk->mstk and l->finishes
3738 */
3739 {
3740 MMacro *m = istk->mstk;
3741 istk->mstk = m->next_active;
3742 if (m->name) {
3743 /*
3744 * This was a real macro call, not a %rep, and
3745 * therefore the parameter information needs to
3746 * be freed.
3747 */
3748 nasm_free(m->params);
3749 free_tlist(m->iline);
3750 nasm_free(m->paramlen);
3751 l->finishes->in_progress = FALSE;
3752 } else
3753 free_mmacro(m);
3754 }
3755 istk->expansion = l->next;
3756 nasm_free(l);
3757 list->downlevel(LIST_MACRO);
3758 }
3759 }
3760 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003761
H. Peter Anvine2c80182005-01-15 22:15:51 +00003762 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003763 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003764 Line *l = istk->expansion;
3765 if (istk->mstk)
3766 istk->mstk->lineno++;
3767 tline = l->first;
3768 istk->expansion = l->next;
3769 nasm_free(l);
3770 p = detoken(tline, FALSE);
3771 list->line(LIST_MACRO, p);
3772 nasm_free(p);
3773 break;
3774 }
3775 line = read_line();
3776 if (line) { /* from the current input file */
3777 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00003778 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003779 nasm_free(line);
3780 break;
3781 }
3782 /*
3783 * The current file has ended; work down the istk
3784 */
3785 {
3786 Include *i = istk;
3787 fclose(i->fp);
3788 if (i->conds)
3789 error(ERR_FATAL,
3790 "expected `%%endif' before end of file");
3791 /* only set line and file name if there's a next node */
3792 if (i->next) {
3793 src_set_linnum(i->lineno);
3794 nasm_free(src_set_fname(i->fname));
3795 }
3796 istk = i->next;
3797 list->downlevel(LIST_INCLUDE);
3798 nasm_free(i);
3799 if (!istk)
3800 return NULL;
3801 }
3802 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003803
H. Peter Anvine2c80182005-01-15 22:15:51 +00003804 /*
3805 * We must expand MMacro parameters and MMacro-local labels
3806 * _before_ we plunge into directive processing, to cope
3807 * with things like `%define something %1' such as STRUC
3808 * uses. Unless we're _defining_ a MMacro, in which case
3809 * those tokens should be left alone to go into the
3810 * definition; and unless we're in a non-emitting
3811 * condition, in which case we don't want to meddle with
3812 * anything.
3813 */
3814 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
3815 tline = expand_mmac_params(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003816
H. Peter Anvine2c80182005-01-15 22:15:51 +00003817 /*
3818 * Check the line to see if it's a preprocessor directive.
3819 */
3820 if (do_directive(tline) == DIRECTIVE_FOUND) {
3821 continue;
3822 } else if (defining) {
3823 /*
3824 * We're defining a multi-line macro. We emit nothing
3825 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00003826 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003827 */
3828 Line *l = nasm_malloc(sizeof(Line));
3829 l->next = defining->expansion;
3830 l->first = tline;
3831 l->finishes = FALSE;
3832 defining->expansion = l;
3833 continue;
3834 } else if (istk->conds && !emitting(istk->conds->state)) {
3835 /*
3836 * We're in a non-emitting branch of a condition block.
3837 * Emit nothing at all, not even a blank line: when we
3838 * emerge from the condition we'll give a line-number
3839 * directive so we keep our place correctly.
3840 */
3841 free_tlist(tline);
3842 continue;
3843 } else if (istk->mstk && !istk->mstk->in_progress) {
3844 /*
3845 * We're in a %rep block which has been terminated, so
3846 * we're walking through to the %endrep without
3847 * emitting anything. Emit nothing at all, not even a
3848 * blank line: when we emerge from the %rep block we'll
3849 * give a line-number directive so we keep our place
3850 * correctly.
3851 */
3852 free_tlist(tline);
3853 continue;
3854 } else {
3855 tline = expand_smacro(tline);
3856 if (!expand_mmacro(tline)) {
3857 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003858 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003859 */
3860 line = detoken(tline, TRUE);
3861 free_tlist(tline);
3862 break;
3863 } else {
3864 continue; /* expand_mmacro calls free_tlist */
3865 }
3866 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003867 }
3868
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003869 return line;
3870}
3871
H. Peter Anvine2c80182005-01-15 22:15:51 +00003872static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003873{
H. Peter Anvine2c80182005-01-15 22:15:51 +00003874 if (defining) {
3875 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
3876 defining->name);
3877 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003878 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003879 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003880 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07003881 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00003882 while (istk) {
3883 Include *i = istk;
3884 istk = istk->next;
3885 fclose(i->fp);
3886 nasm_free(i->fname);
3887 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003888 }
3889 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003890 ctx_pop();
3891 if (pass == 0) {
3892 free_llist(predef);
3893 delete_Blocks();
3894 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003895}
3896
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003897void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003898{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003899 IncPath *i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003900/* by alexfru: order of path inclusion fixed (was reverse order) */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003901 i = nasm_malloc(sizeof(IncPath));
3902 i->path = nasm_strdup(path);
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003903 i->next = NULL;
3904
H. Peter Anvine2c80182005-01-15 22:15:51 +00003905 if (ipath != NULL) {
3906 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003907 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003908 j = j->next;
3909 j->next = i;
3910 } else {
3911 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003912 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003913}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003914
3915/*
3916 * added by alexfru:
3917 *
3918 * This function is used to "export" the include paths, e.g.
3919 * the paths specified in the '-I' command switch.
3920 * The need for such exporting is due to the 'incbin' directive,
3921 * which includes raw binary files (unlike '%include', which
3922 * includes text source files). It would be real nice to be
3923 * able to specify paths to search for incbin'ned files also.
3924 * So, this is a simple workaround.
3925 *
3926 * The function use is simple:
3927 *
3928 * The 1st call (with NULL argument) returns a pointer to the 1st path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003929 * (char** type) or NULL if none include paths available.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003930 *
3931 * All subsequent calls take as argument the value returned by this
3932 * function last. The return value is either the next path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003933 * (char** type) or NULL if the end of the paths list is reached.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003934 *
3935 * It is maybe not the best way to do things, but I didn't want
3936 * to export too much, just one or two functions and no types or
3937 * variables exported.
3938 *
3939 * Can't say I like the current situation with e.g. this path list either,
3940 * it seems to be never deallocated after creation...
3941 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003942char **pp_get_include_path_ptr(char **pPrevPath)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003943{
3944/* This macro returns offset of a member of a structure */
3945#define GetMemberOffset(StructType,MemberName)\
3946 ((size_t)&((StructType*)0)->MemberName)
3947 IncPath *i;
3948
H. Peter Anvine2c80182005-01-15 22:15:51 +00003949 if (pPrevPath == NULL) {
3950 if (ipath != NULL)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003951 return &ipath->path;
3952 else
3953 return NULL;
3954 }
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003955 i = (IncPath *) ((char *)pPrevPath - GetMemberOffset(IncPath, path));
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003956 i = i->next;
3957 if (i != NULL)
3958 return &i->path;
3959 else
3960 return NULL;
3961#undef GetMemberOffset
3962}
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003963
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003964void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003965{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003966 Token *inc, *space, *name;
3967 Line *l;
3968
H. Peter Anvin734b1882002-04-30 21:01:08 +00003969 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
3970 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
3971 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003972
3973 l = nasm_malloc(sizeof(Line));
3974 l->next = predef;
3975 l->first = inc;
3976 l->finishes = FALSE;
3977 predef = l;
3978}
3979
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003980void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003981{
3982 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003983 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003984 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003985
3986 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00003987 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
3988 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003989 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003990 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00003991 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003992 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003993 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003994
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003995 l = nasm_malloc(sizeof(Line));
3996 l->next = predef;
3997 l->first = def;
3998 l->finishes = FALSE;
3999 predef = l;
4000}
4001
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004002void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004003{
4004 Token *def, *space;
4005 Line *l;
4006
H. Peter Anvin734b1882002-04-30 21:01:08 +00004007 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4008 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004009 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004010
4011 l = nasm_malloc(sizeof(Line));
4012 l->next = predef;
4013 l->first = def;
4014 l->finishes = FALSE;
4015 predef = l;
4016}
4017
Keith Kaniosb7a89542007-04-12 02:40:54 +00004018/*
4019 * Added by Keith Kanios:
4020 *
4021 * This function is used to assist with "runtime" preprocessor
4022 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4023 *
4024 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4025 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4026 */
4027
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004028void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004029{
4030 Token *def;
4031
4032 def = tokenize(definition);
4033 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4034 free_tlist(def);
4035
4036}
4037
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004038void pp_extra_stdmac(const char **macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004039{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004040 extrastdmac = macros;
4041}
4042
Keith Kaniosb7a89542007-04-12 02:40:54 +00004043static void make_tok_num(Token * tok, int32_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004044{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004045 char numbuf[20];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00004046 snprintf(numbuf, sizeof(numbuf), "%"PRId32"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004047 tok->text = nasm_strdup(numbuf);
4048 tok->type = TOK_NUMBER;
4049}
4050
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004051Preproc nasmpp = {
4052 pp_reset,
4053 pp_getline,
4054 pp_cleanup
4055};