blob: 1ee07ad6bb711d432798a735ca7a35075feb9f03 [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 Anvin37a321f2007-09-24 13:41:58 -07001181
1182 if (!prefix) {
1183 /* -MG given and file not found */
1184 if (pass == 0) {
1185 namelen += strlen(file) + 1;
1186 if (namelen > 62) {
1187 printf(" \\\n ");
1188 namelen = 2;
1189 }
1190 printf(" %s", file);
1191 }
1192 return NULL;
1193 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001194 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001195
H. Peter Anvin734b1882002-04-30 21:01:08 +00001196 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001197 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001198}
1199
1200/*
H. Peter Anvin97a23472007-09-16 17:57:25 -07001201 * Search for a key in the hash index; adding it if necessary
1202 * (in which case we initialize the data pointer to NULL.)
1203 */
1204static void **
1205hash_findi_add(struct hash_table *hash, const char *str)
1206{
1207 struct hash_insert hi;
1208 void **r;
1209 char *strx;
1210
1211 r = hash_findi(hash, str, &hi);
1212 if (r)
1213 return r;
1214
1215 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
1216 return hash_add(&hi, strx, NULL);
1217}
1218
1219/*
1220 * Like hash_findi, but returns the data element rather than a pointer
1221 * to it. Used only when not adding a new element, hence no third
1222 * argument.
1223 */
1224static void *
1225hash_findix(struct hash_table *hash, const char *str)
1226{
1227 void **p;
1228
1229 p = hash_findi(hash, str, NULL);
1230 return p ? *p : NULL;
1231}
1232
1233/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001234 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001235 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001236 * return TRUE if _any_ single-line macro of that name is defined.
1237 * Otherwise, will return TRUE if a single-line macro with either
1238 * `nparam' or no parameters is defined.
1239 *
1240 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001241 * defined, or nparam is -1, the address of the definition structure
1242 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001243 * is NULL, no action will be taken regarding its contents, and no
1244 * error will occur.
1245 *
1246 * Note that this is also called with nparam zero to resolve
1247 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001248 *
1249 * If you already know which context macro belongs to, you can pass
1250 * the context pointer as first parameter; if you won't but name begins
1251 * with %$ the context will be automatically computed. If all_contexts
1252 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001253 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001254static int
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001255smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001256 int nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001257{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001258 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001259
H. Peter Anvin97a23472007-09-16 17:57:25 -07001260 if (ctx) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001261 m = ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001262 } else if (name[0] == '%' && name[1] == '$') {
1263 if (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001264 ctx = get_ctx(name, FALSE);
1265 if (!ctx)
1266 return FALSE; /* got to return _something_ */
1267 m = ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001268 } else {
1269 m = (SMacro *) hash_findix(smacros, name);
1270 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001271
H. Peter Anvine2c80182005-01-15 22:15:51 +00001272 while (m) {
1273 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1274 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam)) {
1275 if (defn) {
1276 if (nparam == m->nparam || nparam == -1)
1277 *defn = m;
1278 else
1279 *defn = NULL;
1280 }
1281 return TRUE;
1282 }
1283 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001284 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001285
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001286 return FALSE;
1287}
1288
1289/*
1290 * Count and mark off the parameters in a multi-line macro call.
1291 * This is called both from within the multi-line macro expansion
1292 * code, and also to mark off the default parameters when provided
1293 * in a %macro definition line.
1294 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001295static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001296{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001297 int paramsize, brace;
1298
1299 *nparam = paramsize = 0;
1300 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001301 while (t) {
1302 if (*nparam >= paramsize) {
1303 paramsize += PARAM_DELTA;
1304 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1305 }
1306 skip_white_(t);
1307 brace = FALSE;
1308 if (tok_is_(t, "{"))
1309 brace = TRUE;
1310 (*params)[(*nparam)++] = t;
1311 while (tok_isnt_(t, brace ? "}" : ","))
1312 t = t->next;
1313 if (t) { /* got a comma/brace */
1314 t = t->next;
1315 if (brace) {
1316 /*
1317 * Now we've found the closing brace, look further
1318 * for the comma.
1319 */
1320 skip_white_(t);
1321 if (tok_isnt_(t, ",")) {
1322 error(ERR_NONFATAL,
1323 "braces do not enclose all of macro parameter");
1324 while (tok_isnt_(t, ","))
1325 t = t->next;
1326 }
1327 if (t)
1328 t = t->next; /* eat the comma */
1329 }
1330 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001331 }
1332}
1333
1334/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001335 * Determine whether one of the various `if' conditions is true or
1336 * not.
1337 *
1338 * We must free the tline we get passed.
1339 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001340static int if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001341{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001342 enum pp_conditional i = PP_COND(ct);
1343 int j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001344 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001345 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001346 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001347 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001348
1349 origline = tline;
1350
H. Peter Anvine2c80182005-01-15 22:15:51 +00001351 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001352 case PPC_IFCTX:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001353 j = FALSE; /* have we matched yet? */
1354 while (cstk && tline) {
1355 skip_white_(tline);
1356 if (!tline || tline->type != TOK_ID) {
1357 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001358 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 free_tlist(origline);
1360 return -1;
1361 }
1362 if (!nasm_stricmp(tline->text, cstk->name))
1363 j = TRUE;
1364 tline = tline->next;
1365 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001366 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001367
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001368 case PPC_IFDEF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001369 j = FALSE; /* have we matched yet? */
1370 while (tline) {
1371 skip_white_(tline);
1372 if (!tline || (tline->type != TOK_ID &&
1373 (tline->type != TOK_PREPROC_ID ||
1374 tline->text[1] != '$'))) {
1375 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001376 "`%s' expects macro identifiers", pp_directives[ct]);
1377 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001378 }
1379 if (smacro_defined(NULL, tline->text, 0, NULL, 1))
1380 j = TRUE;
1381 tline = tline->next;
1382 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001383 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001384
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001385 case PPC_IFIDN:
1386 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001387 tline = expand_smacro(tline);
1388 t = tt = tline;
1389 while (tok_isnt_(tt, ","))
1390 tt = tt->next;
1391 if (!tt) {
1392 error(ERR_NONFATAL,
1393 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001394 pp_directives[ct]);
1395 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001396 }
1397 tt = tt->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001398 j = TRUE; /* assume equality unless proved not */
1399 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1400 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1401 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001402 pp_directives[ct]);
1403 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001404 }
1405 if (t->type == TOK_WHITESPACE) {
1406 t = t->next;
1407 continue;
1408 }
1409 if (tt->type == TOK_WHITESPACE) {
1410 tt = tt->next;
1411 continue;
1412 }
1413 if (tt->type != t->type) {
1414 j = FALSE; /* found mismatching tokens */
1415 break;
1416 }
1417 /* Unify surrounding quotes for strings */
1418 if (t->type == TOK_STRING) {
1419 tt->text[0] = t->text[0];
1420 tt->text[strlen(tt->text) - 1] = t->text[0];
1421 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001422 if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001423 j = FALSE; /* found mismatching tokens */
1424 break;
1425 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001426
H. Peter Anvine2c80182005-01-15 22:15:51 +00001427 t = t->next;
1428 tt = tt->next;
1429 }
1430 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
1431 j = FALSE; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001432 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001433
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001434 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001435 {
1436 int found = 0;
1437 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001438
H. Peter Anvine2c80182005-01-15 22:15:51 +00001439 tline = tline->next;
1440 skip_white_(tline);
1441 tline = expand_id(tline);
1442 if (!tok_type_(tline, TOK_ID)) {
1443 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001444 "`%s' expects a macro name", pp_directives[ct]);
1445 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001446 }
1447 searching.name = nasm_strdup(tline->text);
1448 searching.casesense = (i == PP_MACRO);
1449 searching.plus = FALSE;
1450 searching.nolist = FALSE;
1451 searching.in_progress = FALSE;
1452 searching.rep_nest = NULL;
1453 searching.nparam_min = 0;
1454 searching.nparam_max = INT_MAX;
1455 tline = expand_smacro(tline->next);
1456 skip_white_(tline);
1457 if (!tline) {
1458 } else if (!tok_type_(tline, TOK_NUMBER)) {
1459 error(ERR_NONFATAL,
1460 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001461 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 } else {
1463 searching.nparam_min = searching.nparam_max =
1464 readnum(tline->text, &j);
1465 if (j)
1466 error(ERR_NONFATAL,
1467 "unable to parse parameter count `%s'",
1468 tline->text);
1469 }
1470 if (tline && tok_is_(tline->next, "-")) {
1471 tline = tline->next->next;
1472 if (tok_is_(tline, "*"))
1473 searching.nparam_max = INT_MAX;
1474 else if (!tok_type_(tline, TOK_NUMBER))
1475 error(ERR_NONFATAL,
1476 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001477 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001478 else {
1479 searching.nparam_max = readnum(tline->text, &j);
1480 if (j)
1481 error(ERR_NONFATAL,
1482 "unable to parse parameter count `%s'",
1483 tline->text);
1484 if (searching.nparam_min > searching.nparam_max)
1485 error(ERR_NONFATAL,
1486 "minimum parameter count exceeds maximum");
1487 }
1488 }
1489 if (tline && tok_is_(tline->next, "+")) {
1490 tline = tline->next;
1491 searching.plus = TRUE;
1492 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07001493 mmac = (MMacro *) hash_findix(mmacros, searching.name);
1494 while (mmac) {
1495 if (!strcmp(mmac->name, searching.name) &&
1496 (mmac->nparam_min <= searching.nparam_max
1497 || searching.plus)
1498 && (searching.nparam_min <= mmac->nparam_max
1499 || mmac->plus)) {
1500 found = TRUE;
1501 break;
1502 }
1503 mmac = mmac->next;
1504 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001505 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001506 j = found;
1507 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001508 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001509
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001510 case PPC_IFID:
1511 needtype = TOK_ID;
1512 goto iftype;
1513 case PPC_IFNUM:
1514 needtype = TOK_NUMBER;
1515 goto iftype;
1516 case PPC_IFSTR:
1517 needtype = TOK_STRING;
1518 goto iftype;
1519
1520 iftype:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 tline = expand_smacro(tline);
1522 t = tline;
1523 while (tok_type_(t, TOK_WHITESPACE))
1524 t = t->next;
1525 j = FALSE; /* placate optimiser */
1526 if (t)
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001527 j = t->type == needtype;
1528 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001529
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001530 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001531 t = tline = expand_smacro(tline);
1532 tptr = &t;
1533 tokval.t_type = TOKEN_INVALID;
1534 evalresult = evaluate(ppscan, tptr, &tokval,
1535 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001536 if (!evalresult)
1537 return -1;
1538 if (tokval.t_type)
1539 error(ERR_WARNING,
1540 "trailing garbage after expression ignored");
1541 if (!is_simple(evalresult)) {
1542 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001543 "non-constant value given to `%s'", pp_directives[ct]);
1544 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001545 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001546 j = reloc_value(evalresult) != 0;
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001547 return j;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001548
H. Peter Anvine2c80182005-01-15 22:15:51 +00001549 default:
1550 error(ERR_FATAL,
1551 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001552 pp_directives[ct]);
1553 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001554 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001555
1556 free_tlist(origline);
1557 return j ^ PP_NEGATIVE(ct);
1558
1559fail:
1560 free_tlist(origline);
1561 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001562}
1563
1564/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001565 * Expand macros in a string. Used in %error and %include directives.
Keith Kaniosb7a89542007-04-12 02:40:54 +00001566 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001567 * The returned variable should ALWAYS be freed after usage.
1568 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001569void expand_macros_in_string(char **p)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001570{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001571 Token *line = tokenize(*p);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001572 line = expand_smacro(line);
1573 *p = detoken(line, FALSE);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001574}
1575
Ed Beroset3ab3f412002-06-11 03:31:49 +00001576/**
1577 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001578 * Find out if a line contains a preprocessor directive, and deal
1579 * with it if so.
1580 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001581 * If a directive _is_ found, it is the responsibility of this routine
1582 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001583 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001584 * @param tline a pointer to the current tokeninzed line linked list
1585 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001586 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001587 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001588static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001589{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001590 enum preproc_token i;
1591 int j;
1592 int nparam, nolist;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001593 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001594 int offset;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001595 char *p, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001596 Include *inc;
1597 Context *ctx;
1598 Cond *cond;
1599 SMacro *smac, **smhead;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001600 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001601 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1602 Line *l;
1603 struct tokenval tokval;
1604 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001605 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001606
1607 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001608
H. Peter Anvineba20a72002-04-30 20:53:55 +00001609 skip_white_(tline);
1610 if (!tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001611 (tline->text[1] == '%' || tline->text[1] == '$'
1612 || tline->text[1] == '!'))
1613 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001614
H. Peter Anvin4169a472007-09-12 01:29:43 +00001615 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001616
1617 /*
1618 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001619 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001620 * we should ignore all directives except for condition
1621 * directives.
1622 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001623 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001624 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1625 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001626 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001627
1628 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001629 * If we're defining a macro or reading a %rep block, we should
1630 * ignore all directives except for %macro/%imacro (which
1631 * generate an error), %endm/%endmacro, and (only if we're in a
H. Peter Anvineba20a72002-04-30 20:53:55 +00001632 * %rep block) %endrep. If we're in a %rep block, another %rep
1633 * causes an error, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001634 */
1635 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001636 i != PP_ENDMACRO && i != PP_ENDM &&
1637 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1638 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001639 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001640
H. Peter Anvin4169a472007-09-12 01:29:43 +00001641 switch (i) {
1642 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001643 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1644 tline->text);
1645 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001646
H. Peter Anvine2c80182005-01-15 22:15:51 +00001647 case PP_STACKSIZE:
1648 /* Directive to tell NASM what the default stack size is. The
1649 * default is for a 16-bit stack, and this can be overriden with
1650 * %stacksize large.
1651 * the following form:
1652 *
1653 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1654 */
1655 tline = tline->next;
1656 if (tline && tline->type == TOK_WHITESPACE)
1657 tline = tline->next;
1658 if (!tline || tline->type != TOK_ID) {
1659 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1660 free_tlist(origline);
1661 return DIRECTIVE_FOUND;
1662 }
1663 if (nasm_stricmp(tline->text, "flat") == 0) {
1664 /* All subsequent ARG directives are for a 32-bit stack */
1665 StackSize = 4;
1666 StackPointer = "ebp";
1667 ArgOffset = 8;
1668 LocalOffset = 4;
1669 } else if (nasm_stricmp(tline->text, "large") == 0) {
1670 /* All subsequent ARG directives are for a 16-bit stack,
1671 * far function call.
1672 */
1673 StackSize = 2;
1674 StackPointer = "bp";
1675 ArgOffset = 4;
1676 LocalOffset = 2;
1677 } else if (nasm_stricmp(tline->text, "small") == 0) {
1678 /* All subsequent ARG directives are for a 16-bit stack,
1679 * far function call. We don't support near functions.
1680 */
1681 StackSize = 2;
1682 StackPointer = "bp";
1683 ArgOffset = 6;
1684 LocalOffset = 2;
1685 } else {
1686 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1687 free_tlist(origline);
1688 return DIRECTIVE_FOUND;
1689 }
1690 free_tlist(origline);
1691 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001692
H. Peter Anvine2c80182005-01-15 22:15:51 +00001693 case PP_ARG:
1694 /* TASM like ARG directive to define arguments to functions, in
1695 * the following form:
1696 *
1697 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1698 */
1699 offset = ArgOffset;
1700 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001701 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001702 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001703
H. Peter Anvine2c80182005-01-15 22:15:51 +00001704 /* Find the argument name */
1705 tline = tline->next;
1706 if (tline && tline->type == TOK_WHITESPACE)
1707 tline = tline->next;
1708 if (!tline || tline->type != TOK_ID) {
1709 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1710 free_tlist(origline);
1711 return DIRECTIVE_FOUND;
1712 }
1713 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001714
H. Peter Anvine2c80182005-01-15 22:15:51 +00001715 /* Find the argument size type */
1716 tline = tline->next;
1717 if (!tline || tline->type != TOK_OTHER
1718 || tline->text[0] != ':') {
1719 error(ERR_NONFATAL,
1720 "Syntax error processing `%%arg' directive");
1721 free_tlist(origline);
1722 return DIRECTIVE_FOUND;
1723 }
1724 tline = tline->next;
1725 if (!tline || tline->type != TOK_ID) {
1726 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1727 free_tlist(origline);
1728 return DIRECTIVE_FOUND;
1729 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001730
H. Peter Anvine2c80182005-01-15 22:15:51 +00001731 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001732 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001733 tt = expand_smacro(tt);
1734 if (nasm_stricmp(tt->text, "byte") == 0) {
1735 size = MAX(StackSize, 1);
1736 } else if (nasm_stricmp(tt->text, "word") == 0) {
1737 size = MAX(StackSize, 2);
1738 } else if (nasm_stricmp(tt->text, "dword") == 0) {
1739 size = MAX(StackSize, 4);
1740 } else if (nasm_stricmp(tt->text, "qword") == 0) {
1741 size = MAX(StackSize, 8);
1742 } else if (nasm_stricmp(tt->text, "tword") == 0) {
1743 size = MAX(StackSize, 10);
1744 } else {
1745 error(ERR_NONFATAL,
1746 "Invalid size type for `%%arg' missing directive");
1747 free_tlist(tt);
1748 free_tlist(origline);
1749 return DIRECTIVE_FOUND;
1750 }
1751 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001752
H. Peter Anvine2c80182005-01-15 22:15:51 +00001753 /* Now define the macro for the argument */
1754 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1755 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001756 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001757 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001758
H. Peter Anvine2c80182005-01-15 22:15:51 +00001759 /* Move to the next argument in the list */
1760 tline = tline->next;
1761 if (tline && tline->type == TOK_WHITESPACE)
1762 tline = tline->next;
1763 }
1764 while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1765 free_tlist(origline);
1766 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001767
H. Peter Anvine2c80182005-01-15 22:15:51 +00001768 case PP_LOCAL:
1769 /* TASM like LOCAL directive to define local variables for a
1770 * function, in the following form:
1771 *
1772 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1773 *
1774 * The '= LocalSize' at the end is ignored by NASM, but is
1775 * required by TASM to define the local parameter size (and used
1776 * by the TASM macro package).
1777 */
1778 offset = LocalOffset;
1779 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001780 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001781 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001782
H. Peter Anvine2c80182005-01-15 22:15:51 +00001783 /* Find the argument name */
1784 tline = tline->next;
1785 if (tline && tline->type == TOK_WHITESPACE)
1786 tline = tline->next;
1787 if (!tline || tline->type != TOK_ID) {
1788 error(ERR_NONFATAL,
1789 "`%%local' missing argument parameter");
1790 free_tlist(origline);
1791 return DIRECTIVE_FOUND;
1792 }
1793 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001794
H. Peter Anvine2c80182005-01-15 22:15:51 +00001795 /* Find the argument size type */
1796 tline = tline->next;
1797 if (!tline || tline->type != TOK_OTHER
1798 || tline->text[0] != ':') {
1799 error(ERR_NONFATAL,
1800 "Syntax error processing `%%local' directive");
1801 free_tlist(origline);
1802 return DIRECTIVE_FOUND;
1803 }
1804 tline = tline->next;
1805 if (!tline || tline->type != TOK_ID) {
1806 error(ERR_NONFATAL,
1807 "`%%local' missing size type parameter");
1808 free_tlist(origline);
1809 return DIRECTIVE_FOUND;
1810 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001811
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001813 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001814 tt = expand_smacro(tt);
1815 if (nasm_stricmp(tt->text, "byte") == 0) {
1816 size = MAX(StackSize, 1);
1817 } else if (nasm_stricmp(tt->text, "word") == 0) {
1818 size = MAX(StackSize, 2);
1819 } else if (nasm_stricmp(tt->text, "dword") == 0) {
1820 size = MAX(StackSize, 4);
1821 } else if (nasm_stricmp(tt->text, "qword") == 0) {
1822 size = MAX(StackSize, 8);
1823 } else if (nasm_stricmp(tt->text, "tword") == 0) {
1824 size = MAX(StackSize, 10);
1825 } else {
1826 error(ERR_NONFATAL,
1827 "Invalid size type for `%%local' missing directive");
1828 free_tlist(tt);
1829 free_tlist(origline);
1830 return DIRECTIVE_FOUND;
1831 }
1832 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001833
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 /* Now define the macro for the argument */
1835 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
1836 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001837 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001838 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001839
H. Peter Anvine2c80182005-01-15 22:15:51 +00001840 /* Now define the assign to setup the enter_c macro correctly */
1841 snprintf(directive, sizeof(directive),
1842 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001843 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001844
H. Peter Anvine2c80182005-01-15 22:15:51 +00001845 /* Move to the next argument in the list */
1846 tline = tline->next;
1847 if (tline && tline->type == TOK_WHITESPACE)
1848 tline = tline->next;
1849 }
1850 while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1851 free_tlist(origline);
1852 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001853
H. Peter Anvine2c80182005-01-15 22:15:51 +00001854 case PP_CLEAR:
1855 if (tline->next)
1856 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07001857 free_macros();
1858 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001859 free_tlist(origline);
1860 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001861
H. Peter Anvine2c80182005-01-15 22:15:51 +00001862 case PP_INCLUDE:
1863 tline = tline->next;
1864 skip_white_(tline);
1865 if (!tline || (tline->type != TOK_STRING &&
1866 tline->type != TOK_INTERNAL_STRING)) {
1867 error(ERR_NONFATAL, "`%%include' expects a file name");
1868 free_tlist(origline);
1869 return DIRECTIVE_FOUND; /* but we did _something_ */
1870 }
1871 if (tline->next)
1872 error(ERR_WARNING,
1873 "trailing garbage after `%%include' ignored");
1874 if (tline->type != TOK_INTERNAL_STRING) {
1875 p = tline->text + 1; /* point past the quote to the name */
1876 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
1877 } else
1878 p = tline->text; /* internal_string is easier */
1879 expand_macros_in_string(&p);
1880 inc = nasm_malloc(sizeof(Include));
1881 inc->next = istk;
1882 inc->conds = NULL;
1883 inc->fp = inc_fopen(p);
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001884 if (!inc->fp && pass == 0) {
1885 /* -MG given but file not found */
1886 nasm_free(inc);
1887 } else {
1888 inc->fname = src_set_fname(p);
1889 inc->lineno = src_set_linnum(0);
1890 inc->lineinc = 1;
1891 inc->expansion = NULL;
1892 inc->mstk = NULL;
1893 istk = inc;
1894 list->uplevel(LIST_INCLUDE);
1895 }
1896 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001897 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001898
H. Peter Anvine2c80182005-01-15 22:15:51 +00001899 case PP_PUSH:
1900 tline = tline->next;
1901 skip_white_(tline);
1902 tline = expand_id(tline);
1903 if (!tok_type_(tline, TOK_ID)) {
1904 error(ERR_NONFATAL, "`%%push' expects a context identifier");
1905 free_tlist(origline);
1906 return DIRECTIVE_FOUND; /* but we did _something_ */
1907 }
1908 if (tline->next)
1909 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
1910 ctx = nasm_malloc(sizeof(Context));
1911 ctx->next = cstk;
1912 ctx->localmac = NULL;
1913 ctx->name = nasm_strdup(tline->text);
1914 ctx->number = unique++;
1915 cstk = ctx;
1916 free_tlist(origline);
1917 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001918
H. Peter Anvine2c80182005-01-15 22:15:51 +00001919 case PP_REPL:
1920 tline = tline->next;
1921 skip_white_(tline);
1922 tline = expand_id(tline);
1923 if (!tok_type_(tline, TOK_ID)) {
1924 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
1925 free_tlist(origline);
1926 return DIRECTIVE_FOUND; /* but we did _something_ */
1927 }
1928 if (tline->next)
1929 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
1930 if (!cstk)
1931 error(ERR_NONFATAL, "`%%repl': context stack is empty");
1932 else {
1933 nasm_free(cstk->name);
1934 cstk->name = nasm_strdup(tline->text);
1935 }
1936 free_tlist(origline);
1937 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001938
H. Peter Anvine2c80182005-01-15 22:15:51 +00001939 case PP_POP:
1940 if (tline->next)
1941 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
1942 if (!cstk)
1943 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
1944 else
1945 ctx_pop();
1946 free_tlist(origline);
1947 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001948
H. Peter Anvine2c80182005-01-15 22:15:51 +00001949 case PP_ERROR:
1950 tline->next = expand_smacro(tline->next);
1951 tline = tline->next;
1952 skip_white_(tline);
1953 if (tok_type_(tline, TOK_STRING)) {
1954 p = tline->text + 1; /* point past the quote to the name */
1955 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
1956 expand_macros_in_string(&p);
1957 error(ERR_NONFATAL, "%s", p);
1958 nasm_free(p);
1959 } else {
1960 p = detoken(tline, FALSE);
1961 error(ERR_WARNING, "%s", p);
1962 nasm_free(p);
1963 }
1964 free_tlist(origline);
1965 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001966
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001967 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001968 if (istk->conds && !emitting(istk->conds->state))
1969 j = COND_NEVER;
1970 else {
1971 j = if_condition(tline->next, i);
1972 tline->next = NULL; /* it got freed */
1973 free_tlist(origline);
1974 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
1975 }
1976 cond = nasm_malloc(sizeof(Cond));
1977 cond->next = istk->conds;
1978 cond->state = j;
1979 istk->conds = cond;
1980 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001981
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001982 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001983 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00001984 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001985 if (emitting(istk->conds->state)
1986 || istk->conds->state == COND_NEVER)
1987 istk->conds->state = COND_NEVER;
1988 else {
1989 /*
1990 * IMPORTANT: In the case of %if, we will already have
1991 * called expand_mmac_params(); however, if we're
1992 * processing an %elif we must have been in a
1993 * non-emitting mode, which would have inhibited
1994 * the normal invocation of expand_mmac_params(). Therefore,
1995 * we have to do it explicitly here.
1996 */
1997 j = if_condition(expand_mmac_params(tline->next), i);
1998 tline->next = NULL; /* it got freed */
1999 free_tlist(origline);
2000 istk->conds->state =
2001 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2002 }
2003 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002004
H. Peter Anvine2c80182005-01-15 22:15:51 +00002005 case PP_ELSE:
2006 if (tline->next)
2007 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2008 if (!istk->conds)
2009 error(ERR_FATAL, "`%%else': no matching `%%if'");
2010 if (emitting(istk->conds->state)
2011 || istk->conds->state == COND_NEVER)
2012 istk->conds->state = COND_ELSE_FALSE;
2013 else
2014 istk->conds->state = COND_ELSE_TRUE;
2015 free_tlist(origline);
2016 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002017
H. Peter Anvine2c80182005-01-15 22:15:51 +00002018 case PP_ENDIF:
2019 if (tline->next)
2020 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2021 if (!istk->conds)
2022 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2023 cond = istk->conds;
2024 istk->conds = cond->next;
2025 nasm_free(cond);
2026 free_tlist(origline);
2027 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002028
H. Peter Anvine2c80182005-01-15 22:15:51 +00002029 case PP_MACRO:
2030 case PP_IMACRO:
2031 if (defining)
2032 error(ERR_FATAL,
2033 "`%%%smacro': already defining a macro",
2034 (i == PP_IMACRO ? "i" : ""));
2035 tline = tline->next;
2036 skip_white_(tline);
2037 tline = expand_id(tline);
2038 if (!tok_type_(tline, TOK_ID)) {
2039 error(ERR_NONFATAL,
2040 "`%%%smacro' expects a macro name",
2041 (i == PP_IMACRO ? "i" : ""));
2042 return DIRECTIVE_FOUND;
2043 }
2044 defining = nasm_malloc(sizeof(MMacro));
2045 defining->name = nasm_strdup(tline->text);
2046 defining->casesense = (i == PP_MACRO);
2047 defining->plus = FALSE;
2048 defining->nolist = FALSE;
2049 defining->in_progress = FALSE;
2050 defining->rep_nest = NULL;
2051 tline = expand_smacro(tline->next);
2052 skip_white_(tline);
2053 if (!tok_type_(tline, TOK_NUMBER)) {
2054 error(ERR_NONFATAL,
2055 "`%%%smacro' expects a parameter count",
2056 (i == PP_IMACRO ? "i" : ""));
2057 defining->nparam_min = defining->nparam_max = 0;
2058 } else {
2059 defining->nparam_min = defining->nparam_max =
2060 readnum(tline->text, &j);
2061 if (j)
2062 error(ERR_NONFATAL,
2063 "unable to parse parameter count `%s'", tline->text);
2064 }
2065 if (tline && tok_is_(tline->next, "-")) {
2066 tline = tline->next->next;
2067 if (tok_is_(tline, "*"))
2068 defining->nparam_max = INT_MAX;
2069 else if (!tok_type_(tline, TOK_NUMBER))
2070 error(ERR_NONFATAL,
2071 "`%%%smacro' expects a parameter count after `-'",
2072 (i == PP_IMACRO ? "i" : ""));
2073 else {
2074 defining->nparam_max = readnum(tline->text, &j);
2075 if (j)
2076 error(ERR_NONFATAL,
2077 "unable to parse parameter count `%s'",
2078 tline->text);
2079 if (defining->nparam_min > defining->nparam_max)
2080 error(ERR_NONFATAL,
2081 "minimum parameter count exceeds maximum");
2082 }
2083 }
2084 if (tline && tok_is_(tline->next, "+")) {
2085 tline = tline->next;
2086 defining->plus = TRUE;
2087 }
2088 if (tline && tok_type_(tline->next, TOK_ID) &&
2089 !nasm_stricmp(tline->next->text, ".nolist")) {
2090 tline = tline->next;
2091 defining->nolist = TRUE;
2092 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07002093 mmac = (MMacro *) hash_findix(mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002094 while (mmac) {
2095 if (!strcmp(mmac->name, defining->name) &&
2096 (mmac->nparam_min <= defining->nparam_max
2097 || defining->plus)
2098 && (defining->nparam_min <= mmac->nparam_max
2099 || mmac->plus)) {
2100 error(ERR_WARNING,
2101 "redefining multi-line macro `%s'", defining->name);
2102 break;
2103 }
2104 mmac = mmac->next;
2105 }
2106 /*
2107 * Handle default parameters.
2108 */
2109 if (tline && tline->next) {
2110 defining->dlist = tline->next;
2111 tline->next = NULL;
2112 count_mmac_params(defining->dlist, &defining->ndefs,
2113 &defining->defaults);
2114 } else {
2115 defining->dlist = NULL;
2116 defining->defaults = NULL;
2117 }
2118 defining->expansion = NULL;
2119 free_tlist(origline);
2120 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002121
H. Peter Anvine2c80182005-01-15 22:15:51 +00002122 case PP_ENDM:
2123 case PP_ENDMACRO:
2124 if (!defining) {
2125 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2126 return DIRECTIVE_FOUND;
2127 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07002128 mmhead = (MMacro **) hash_findi_add(mmacros, defining->name);
2129 defining->next = *mmhead;
2130 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002131 defining = NULL;
2132 free_tlist(origline);
2133 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002134
H. Peter Anvine2c80182005-01-15 22:15:51 +00002135 case PP_ROTATE:
2136 if (tline->next && tline->next->type == TOK_WHITESPACE)
2137 tline = tline->next;
2138 if (tline->next == NULL) {
2139 free_tlist(origline);
2140 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2141 return DIRECTIVE_FOUND;
2142 }
2143 t = expand_smacro(tline->next);
2144 tline->next = NULL;
2145 free_tlist(origline);
2146 tline = t;
2147 tptr = &t;
2148 tokval.t_type = TOKEN_INVALID;
2149 evalresult =
2150 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2151 free_tlist(tline);
2152 if (!evalresult)
2153 return DIRECTIVE_FOUND;
2154 if (tokval.t_type)
2155 error(ERR_WARNING,
2156 "trailing garbage after expression ignored");
2157 if (!is_simple(evalresult)) {
2158 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2159 return DIRECTIVE_FOUND;
2160 }
2161 mmac = istk->mstk;
2162 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2163 mmac = mmac->next_active;
2164 if (!mmac) {
2165 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2166 } else if (mmac->nparam == 0) {
2167 error(ERR_NONFATAL,
2168 "`%%rotate' invoked within macro without parameters");
2169 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002170 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002171
H. Peter Anvin25a99342007-09-22 17:45:45 -07002172 rotate %= (int)mmac->nparam;
2173 if (rotate < 0)
2174 rotate += mmac->nparam;
2175
2176 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002177 }
2178 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002179
H. Peter Anvine2c80182005-01-15 22:15:51 +00002180 case PP_REP:
2181 nolist = FALSE;
2182 do {
2183 tline = tline->next;
2184 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002185
H. Peter Anvine2c80182005-01-15 22:15:51 +00002186 if (tok_type_(tline, TOK_ID) &&
2187 nasm_stricmp(tline->text, ".nolist") == 0) {
2188 nolist = TRUE;
2189 do {
2190 tline = tline->next;
2191 } while (tok_type_(tline, TOK_WHITESPACE));
2192 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002193
H. Peter Anvine2c80182005-01-15 22:15:51 +00002194 if (tline) {
2195 t = expand_smacro(tline);
2196 tptr = &t;
2197 tokval.t_type = TOKEN_INVALID;
2198 evalresult =
2199 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2200 if (!evalresult) {
2201 free_tlist(origline);
2202 return DIRECTIVE_FOUND;
2203 }
2204 if (tokval.t_type)
2205 error(ERR_WARNING,
2206 "trailing garbage after expression ignored");
2207 if (!is_simple(evalresult)) {
2208 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2209 return DIRECTIVE_FOUND;
2210 }
2211 i = (int)reloc_value(evalresult) + 1;
2212 } else {
2213 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
2214 i = 0;
2215 }
2216 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002217
H. Peter Anvine2c80182005-01-15 22:15:51 +00002218 tmp_defining = defining;
2219 defining = nasm_malloc(sizeof(MMacro));
2220 defining->name = NULL; /* flags this macro as a %rep block */
2221 defining->casesense = 0;
2222 defining->plus = FALSE;
2223 defining->nolist = nolist;
2224 defining->in_progress = i;
2225 defining->nparam_min = defining->nparam_max = 0;
2226 defining->defaults = NULL;
2227 defining->dlist = NULL;
2228 defining->expansion = NULL;
2229 defining->next_active = istk->mstk;
2230 defining->rep_nest = tmp_defining;
2231 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002232
H. Peter Anvine2c80182005-01-15 22:15:51 +00002233 case PP_ENDREP:
2234 if (!defining || defining->name) {
2235 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2236 return DIRECTIVE_FOUND;
2237 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002238
H. Peter Anvine2c80182005-01-15 22:15:51 +00002239 /*
2240 * Now we have a "macro" defined - although it has no name
2241 * and we won't be entering it in the hash tables - we must
2242 * push a macro-end marker for it on to istk->expansion.
2243 * After that, it will take care of propagating itself (a
2244 * macro-end marker line for a macro which is really a %rep
2245 * block will cause the macro to be re-expanded, complete
2246 * with another macro-end marker to ensure the process
2247 * continues) until the whole expansion is forcibly removed
2248 * from istk->expansion by a %exitrep.
2249 */
2250 l = nasm_malloc(sizeof(Line));
2251 l->next = istk->expansion;
2252 l->finishes = defining;
2253 l->first = NULL;
2254 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002255
H. Peter Anvine2c80182005-01-15 22:15:51 +00002256 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002257
H. Peter Anvine2c80182005-01-15 22:15:51 +00002258 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2259 tmp_defining = defining;
2260 defining = defining->rep_nest;
2261 free_tlist(origline);
2262 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002263
H. Peter Anvine2c80182005-01-15 22:15:51 +00002264 case PP_EXITREP:
2265 /*
2266 * We must search along istk->expansion until we hit a
2267 * macro-end marker for a macro with no name. Then we set
2268 * its `in_progress' flag to 0.
2269 */
2270 for (l = istk->expansion; l; l = l->next)
2271 if (l->finishes && !l->finishes->name)
2272 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002273
H. Peter Anvine2c80182005-01-15 22:15:51 +00002274 if (l)
2275 l->finishes->in_progress = 0;
2276 else
2277 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2278 free_tlist(origline);
2279 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002280
H. Peter Anvine2c80182005-01-15 22:15:51 +00002281 case PP_XDEFINE:
2282 case PP_IXDEFINE:
2283 case PP_DEFINE:
2284 case PP_IDEFINE:
2285 tline = tline->next;
2286 skip_white_(tline);
2287 tline = expand_id(tline);
2288 if (!tline || (tline->type != TOK_ID &&
2289 (tline->type != TOK_PREPROC_ID ||
2290 tline->text[1] != '$'))) {
2291 error(ERR_NONFATAL,
2292 "`%%%s%sdefine' expects a macro identifier",
2293 ((i == PP_IDEFINE || i == PP_IXDEFINE) ? "i" : ""),
2294 ((i == PP_XDEFINE || i == PP_IXDEFINE) ? "x" : ""));
2295 free_tlist(origline);
2296 return DIRECTIVE_FOUND;
2297 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002298
H. Peter Anvine2c80182005-01-15 22:15:51 +00002299 ctx = get_ctx(tline->text, FALSE);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002300
H. Peter Anvine2c80182005-01-15 22:15:51 +00002301 mname = tline->text;
2302 last = tline;
2303 param_start = tline = tline->next;
2304 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002305
H. Peter Anvine2c80182005-01-15 22:15:51 +00002306 /* Expand the macro definition now for %xdefine and %ixdefine */
2307 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2308 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002309
H. Peter Anvine2c80182005-01-15 22:15:51 +00002310 if (tok_is_(tline, "(")) {
2311 /*
2312 * This macro has parameters.
2313 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002314
H. Peter Anvine2c80182005-01-15 22:15:51 +00002315 tline = tline->next;
2316 while (1) {
2317 skip_white_(tline);
2318 if (!tline) {
2319 error(ERR_NONFATAL, "parameter identifier expected");
2320 free_tlist(origline);
2321 return DIRECTIVE_FOUND;
2322 }
2323 if (tline->type != TOK_ID) {
2324 error(ERR_NONFATAL,
2325 "`%s': parameter identifier expected",
2326 tline->text);
2327 free_tlist(origline);
2328 return DIRECTIVE_FOUND;
2329 }
2330 tline->type = TOK_SMAC_PARAM + nparam++;
2331 tline = tline->next;
2332 skip_white_(tline);
2333 if (tok_is_(tline, ",")) {
2334 tline = tline->next;
2335 continue;
2336 }
2337 if (!tok_is_(tline, ")")) {
2338 error(ERR_NONFATAL,
2339 "`)' expected to terminate macro template");
2340 free_tlist(origline);
2341 return DIRECTIVE_FOUND;
2342 }
2343 break;
2344 }
2345 last = tline;
2346 tline = tline->next;
2347 }
2348 if (tok_type_(tline, TOK_WHITESPACE))
2349 last = tline, tline = tline->next;
2350 macro_start = NULL;
2351 last->next = NULL;
2352 t = tline;
2353 while (t) {
2354 if (t->type == TOK_ID) {
2355 for (tt = param_start; tt; tt = tt->next)
2356 if (tt->type >= TOK_SMAC_PARAM &&
2357 !strcmp(tt->text, t->text))
2358 t->type = tt->type;
2359 }
2360 tt = t->next;
2361 t->next = macro_start;
2362 macro_start = t;
2363 t = tt;
2364 }
2365 /*
2366 * Good. We now have a macro name, a parameter count, and a
2367 * token list (in reverse order) for an expansion. We ought
2368 * to be OK just to create an SMacro, store it, and let
2369 * free_tlist have the rest of the line (which we have
2370 * carefully re-terminated after chopping off the expansion
2371 * from the end).
2372 */
2373 if (smacro_defined(ctx, mname, nparam, &smac, i == PP_DEFINE)) {
2374 if (!smac) {
2375 error(ERR_WARNING,
2376 "single-line macro `%s' defined both with and"
2377 " without parameters", mname);
2378 free_tlist(origline);
2379 free_tlist(macro_start);
2380 return DIRECTIVE_FOUND;
2381 } else {
2382 /*
2383 * We're redefining, so we have to take over an
2384 * existing SMacro structure. This means freeing
2385 * what was already in it.
2386 */
2387 nasm_free(smac->name);
2388 free_tlist(smac->expansion);
2389 }
2390 } else {
H. Peter Anvin97a23472007-09-16 17:57:25 -07002391 if (!ctx)
2392 smhead = (SMacro **) hash_findi_add(smacros, mname);
2393 else
2394 smhead = &ctx->localmac;
2395
H. Peter Anvine2c80182005-01-15 22:15:51 +00002396 smac = nasm_malloc(sizeof(SMacro));
2397 smac->next = *smhead;
2398 *smhead = smac;
2399 }
2400 smac->name = nasm_strdup(mname);
2401 smac->casesense = ((i == PP_DEFINE) || (i == PP_XDEFINE));
2402 smac->nparam = nparam;
2403 smac->expansion = macro_start;
2404 smac->in_progress = FALSE;
2405 free_tlist(origline);
2406 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002407
H. Peter Anvine2c80182005-01-15 22:15:51 +00002408 case PP_UNDEF:
2409 tline = tline->next;
2410 skip_white_(tline);
2411 tline = expand_id(tline);
2412 if (!tline || (tline->type != TOK_ID &&
2413 (tline->type != TOK_PREPROC_ID ||
2414 tline->text[1] != '$'))) {
2415 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2416 free_tlist(origline);
2417 return DIRECTIVE_FOUND;
2418 }
2419 if (tline->next) {
2420 error(ERR_WARNING,
2421 "trailing garbage after macro name ignored");
2422 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002423
H. Peter Anvine2c80182005-01-15 22:15:51 +00002424 /* Find the context that symbol belongs to */
2425 ctx = get_ctx(tline->text, FALSE);
2426 if (!ctx)
H. Peter Anvin97a23472007-09-16 17:57:25 -07002427 smhead = (SMacro **) hash_findi(smacros, tline->text, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002428 else
2429 smhead = &ctx->localmac;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002430
H. Peter Anvin97a23472007-09-16 17:57:25 -07002431 if (smhead) {
2432 SMacro *s, **sp;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002433
H. Peter Anvin97a23472007-09-16 17:57:25 -07002434 mname = tline->text;
2435 last = tline;
2436 last->next = NULL;
2437
2438 /*
2439 * We now have a macro name... go hunt for it.
2440 */
H. Peter Anvine373efd2007-09-24 21:33:17 -07002441 sp = smhead;
2442 while ((s = *sp) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -07002443 if (!mstrcmp(s->name, tline->text, s->casesense)) {
2444 *sp = s->next;
2445 nasm_free(s->name);
2446 free_tlist(s->expansion);
2447 nasm_free(s);
H. Peter Anvine373efd2007-09-24 21:33:17 -07002448 } else {
2449 sp = &s->next;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002450 }
2451 }
2452 free_tlist(origline);
2453 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002454 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002455
H. Peter Anvine2c80182005-01-15 22:15:51 +00002456 case PP_STRLEN:
2457 tline = tline->next;
2458 skip_white_(tline);
2459 tline = expand_id(tline);
2460 if (!tline || (tline->type != TOK_ID &&
2461 (tline->type != TOK_PREPROC_ID ||
2462 tline->text[1] != '$'))) {
2463 error(ERR_NONFATAL,
2464 "`%%strlen' expects a macro identifier as first parameter");
2465 free_tlist(origline);
2466 return DIRECTIVE_FOUND;
2467 }
2468 ctx = get_ctx(tline->text, FALSE);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002469
H. Peter Anvine2c80182005-01-15 22:15:51 +00002470 mname = tline->text;
2471 last = tline;
2472 tline = expand_smacro(tline->next);
2473 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002474
H. Peter Anvine2c80182005-01-15 22:15:51 +00002475 t = tline;
2476 while (tok_type_(t, TOK_WHITESPACE))
2477 t = t->next;
2478 /* t should now point to the string */
2479 if (t->type != TOK_STRING) {
2480 error(ERR_NONFATAL,
2481 "`%%strlen` requires string as second parameter");
2482 free_tlist(tline);
2483 free_tlist(origline);
2484 return DIRECTIVE_FOUND;
2485 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002486
H. Peter Anvine2c80182005-01-15 22:15:51 +00002487 macro_start = nasm_malloc(sizeof(*macro_start));
2488 macro_start->next = NULL;
2489 make_tok_num(macro_start, strlen(t->text) - 2);
2490 macro_start->mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002491
H. Peter Anvine2c80182005-01-15 22:15:51 +00002492 /*
2493 * We now have a macro name, an implicit parameter count of
2494 * zero, and a numeric token to use as an expansion. Create
2495 * and store an SMacro.
2496 */
2497 if (smacro_defined(ctx, mname, 0, &smac, i == PP_STRLEN)) {
2498 if (!smac)
2499 error(ERR_WARNING,
2500 "single-line macro `%s' defined both with and"
2501 " without parameters", mname);
2502 else {
2503 /*
2504 * We're redefining, so we have to take over an
2505 * existing SMacro structure. This means freeing
2506 * what was already in it.
2507 */
2508 nasm_free(smac->name);
2509 free_tlist(smac->expansion);
2510 }
2511 } else {
H. Peter Anvin97a23472007-09-16 17:57:25 -07002512 if (!ctx)
2513 smhead = (SMacro **) hash_findi_add(smacros, mname);
2514 else
2515 smhead = &ctx->localmac;
2516
H. Peter Anvine2c80182005-01-15 22:15:51 +00002517 smac = nasm_malloc(sizeof(SMacro));
2518 smac->next = *smhead;
2519 *smhead = smac;
2520 }
2521 smac->name = nasm_strdup(mname);
2522 smac->casesense = (i == PP_STRLEN);
2523 smac->nparam = 0;
2524 smac->expansion = macro_start;
2525 smac->in_progress = FALSE;
2526 free_tlist(tline);
2527 free_tlist(origline);
2528 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002529
H. Peter Anvine2c80182005-01-15 22:15:51 +00002530 case PP_SUBSTR:
2531 tline = tline->next;
2532 skip_white_(tline);
2533 tline = expand_id(tline);
2534 if (!tline || (tline->type != TOK_ID &&
2535 (tline->type != TOK_PREPROC_ID ||
2536 tline->text[1] != '$'))) {
2537 error(ERR_NONFATAL,
2538 "`%%substr' expects a macro identifier as first parameter");
2539 free_tlist(origline);
2540 return DIRECTIVE_FOUND;
2541 }
2542 ctx = get_ctx(tline->text, FALSE);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002543
H. Peter Anvine2c80182005-01-15 22:15:51 +00002544 mname = tline->text;
2545 last = tline;
2546 tline = expand_smacro(tline->next);
2547 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002548
H. Peter Anvine2c80182005-01-15 22:15:51 +00002549 t = tline->next;
2550 while (tok_type_(t, TOK_WHITESPACE))
2551 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002552
H. Peter Anvine2c80182005-01-15 22:15:51 +00002553 /* t should now point to the string */
2554 if (t->type != TOK_STRING) {
2555 error(ERR_NONFATAL,
2556 "`%%substr` requires string as second parameter");
2557 free_tlist(tline);
2558 free_tlist(origline);
2559 return DIRECTIVE_FOUND;
2560 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002561
H. Peter Anvine2c80182005-01-15 22:15:51 +00002562 tt = t->next;
2563 tptr = &tt;
2564 tokval.t_type = TOKEN_INVALID;
2565 evalresult =
2566 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2567 if (!evalresult) {
2568 free_tlist(tline);
2569 free_tlist(origline);
2570 return DIRECTIVE_FOUND;
2571 }
2572 if (!is_simple(evalresult)) {
2573 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2574 free_tlist(tline);
2575 free_tlist(origline);
2576 return DIRECTIVE_FOUND;
2577 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002578
H. Peter Anvine2c80182005-01-15 22:15:51 +00002579 macro_start = nasm_malloc(sizeof(*macro_start));
2580 macro_start->next = NULL;
2581 macro_start->text = nasm_strdup("'''");
2582 if (evalresult->value > 0
2583 && evalresult->value < strlen(t->text) - 1) {
2584 macro_start->text[1] = t->text[evalresult->value];
2585 } else {
2586 macro_start->text[2] = '\0';
2587 }
2588 macro_start->type = TOK_STRING;
2589 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002590
H. Peter Anvine2c80182005-01-15 22:15:51 +00002591 /*
2592 * We now have a macro name, an implicit parameter count of
2593 * zero, and a numeric token to use as an expansion. Create
2594 * and store an SMacro.
2595 */
2596 if (smacro_defined(ctx, mname, 0, &smac, i == PP_SUBSTR)) {
2597 if (!smac)
2598 error(ERR_WARNING,
2599 "single-line macro `%s' defined both with and"
2600 " without parameters", mname);
2601 else {
2602 /*
2603 * We're redefining, so we have to take over an
2604 * existing SMacro structure. This means freeing
2605 * what was already in it.
2606 */
2607 nasm_free(smac->name);
2608 free_tlist(smac->expansion);
2609 }
2610 } else {
H. Peter Anvin97a23472007-09-16 17:57:25 -07002611 if (!ctx)
2612 smhead = (SMacro **) hash_findi_add(smacros, tline->text);
2613 else
2614 smhead = &ctx->localmac;
2615
H. Peter Anvine2c80182005-01-15 22:15:51 +00002616 smac = nasm_malloc(sizeof(SMacro));
2617 smac->next = *smhead;
2618 *smhead = smac;
2619 }
2620 smac->name = nasm_strdup(mname);
2621 smac->casesense = (i == PP_SUBSTR);
2622 smac->nparam = 0;
2623 smac->expansion = macro_start;
2624 smac->in_progress = FALSE;
2625 free_tlist(tline);
2626 free_tlist(origline);
2627 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002628
H. Peter Anvine2c80182005-01-15 22:15:51 +00002629 case PP_ASSIGN:
2630 case PP_IASSIGN:
2631 tline = tline->next;
2632 skip_white_(tline);
2633 tline = expand_id(tline);
2634 if (!tline || (tline->type != TOK_ID &&
2635 (tline->type != TOK_PREPROC_ID ||
2636 tline->text[1] != '$'))) {
2637 error(ERR_NONFATAL,
2638 "`%%%sassign' expects a macro identifier",
2639 (i == PP_IASSIGN ? "i" : ""));
2640 free_tlist(origline);
2641 return DIRECTIVE_FOUND;
2642 }
2643 ctx = get_ctx(tline->text, FALSE);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002644
H. Peter Anvine2c80182005-01-15 22:15:51 +00002645 mname = tline->text;
2646 last = tline;
2647 tline = expand_smacro(tline->next);
2648 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002649
H. Peter Anvine2c80182005-01-15 22:15:51 +00002650 t = tline;
2651 tptr = &t;
2652 tokval.t_type = TOKEN_INVALID;
2653 evalresult =
2654 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2655 free_tlist(tline);
2656 if (!evalresult) {
2657 free_tlist(origline);
2658 return DIRECTIVE_FOUND;
2659 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002660
H. Peter Anvine2c80182005-01-15 22:15:51 +00002661 if (tokval.t_type)
2662 error(ERR_WARNING,
2663 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00002664
H. Peter Anvine2c80182005-01-15 22:15:51 +00002665 if (!is_simple(evalresult)) {
2666 error(ERR_NONFATAL,
2667 "non-constant value given to `%%%sassign'",
2668 (i == PP_IASSIGN ? "i" : ""));
2669 free_tlist(origline);
2670 return DIRECTIVE_FOUND;
2671 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002672
H. Peter Anvine2c80182005-01-15 22:15:51 +00002673 macro_start = nasm_malloc(sizeof(*macro_start));
2674 macro_start->next = NULL;
2675 make_tok_num(macro_start, reloc_value(evalresult));
2676 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002677
H. Peter Anvine2c80182005-01-15 22:15:51 +00002678 /*
2679 * We now have a macro name, an implicit parameter count of
2680 * zero, and a numeric token to use as an expansion. Create
2681 * and store an SMacro.
2682 */
2683 if (smacro_defined(ctx, mname, 0, &smac, i == PP_ASSIGN)) {
2684 if (!smac)
2685 error(ERR_WARNING,
2686 "single-line macro `%s' defined both with and"
2687 " without parameters", mname);
2688 else {
2689 /*
2690 * We're redefining, so we have to take over an
2691 * existing SMacro structure. This means freeing
2692 * what was already in it.
2693 */
2694 nasm_free(smac->name);
2695 free_tlist(smac->expansion);
2696 }
2697 } else {
H. Peter Anvin97a23472007-09-16 17:57:25 -07002698 if (!ctx)
2699 smhead = (SMacro **) hash_findi_add(smacros, mname);
2700 else
2701 smhead = &ctx->localmac;
2702
H. Peter Anvine2c80182005-01-15 22:15:51 +00002703 smac = nasm_malloc(sizeof(SMacro));
2704 smac->next = *smhead;
2705 *smhead = smac;
2706 }
2707 smac->name = nasm_strdup(mname);
2708 smac->casesense = (i == PP_ASSIGN);
2709 smac->nparam = 0;
2710 smac->expansion = macro_start;
2711 smac->in_progress = FALSE;
2712 free_tlist(origline);
2713 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002714
H. Peter Anvine2c80182005-01-15 22:15:51 +00002715 case PP_LINE:
2716 /*
2717 * Syntax is `%line nnn[+mmm] [filename]'
2718 */
2719 tline = tline->next;
2720 skip_white_(tline);
2721 if (!tok_type_(tline, TOK_NUMBER)) {
2722 error(ERR_NONFATAL, "`%%line' expects line number");
2723 free_tlist(origline);
2724 return DIRECTIVE_FOUND;
2725 }
2726 k = readnum(tline->text, &j);
2727 m = 1;
2728 tline = tline->next;
2729 if (tok_is_(tline, "+")) {
2730 tline = tline->next;
2731 if (!tok_type_(tline, TOK_NUMBER)) {
2732 error(ERR_NONFATAL, "`%%line' expects line increment");
2733 free_tlist(origline);
2734 return DIRECTIVE_FOUND;
2735 }
2736 m = readnum(tline->text, &j);
2737 tline = tline->next;
2738 }
2739 skip_white_(tline);
2740 src_set_linnum(k);
2741 istk->lineinc = m;
2742 if (tline) {
2743 nasm_free(src_set_fname(detoken(tline, FALSE)));
2744 }
2745 free_tlist(origline);
2746 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002747
H. Peter Anvine2c80182005-01-15 22:15:51 +00002748 default:
2749 error(ERR_FATAL,
2750 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00002751 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002752 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002753 }
Ed Beroset3ab3f412002-06-11 03:31:49 +00002754 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002755}
2756
2757/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002758 * Ensure that a macro parameter contains a condition code and
2759 * nothing else. Return the condition code index if so, or -1
2760 * otherwise.
2761 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002762static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002763{
H. Peter Anvin76690a12002-04-30 20:52:49 +00002764 Token *tt;
2765 int i, j, k, m;
2766
H. Peter Anvin25a99342007-09-22 17:45:45 -07002767 if (!t)
2768 return -1; /* Probably a %+ without a space */
2769
H. Peter Anvineba20a72002-04-30 20:53:55 +00002770 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002771 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002772 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002773 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002774 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002775 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002776 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002777
2778 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00002779 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002780 while (j - i > 1) {
2781 k = (j + i) / 2;
2782 m = nasm_stricmp(t->text, conditions[k]);
2783 if (m == 0) {
2784 i = k;
2785 j = -2;
2786 break;
2787 } else if (m < 0) {
2788 j = k;
2789 } else
2790 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002791 }
2792 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002793 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002794 return i;
2795}
2796
2797/*
2798 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2799 * %-n) and MMacro-local identifiers (%%foo).
2800 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002801static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002802{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002803 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002804
2805 tail = &thead;
2806 thead = NULL;
2807
H. Peter Anvine2c80182005-01-15 22:15:51 +00002808 while (tline) {
2809 if (tline->type == TOK_PREPROC_ID &&
2810 (((tline->text[1] == '+' || tline->text[1] == '-')
2811 && tline->text[2]) || tline->text[1] == '%'
2812 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002813 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002814 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002815 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07002816 unsigned int n;
2817 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002818 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002819
H. Peter Anvine2c80182005-01-15 22:15:51 +00002820 t = tline;
2821 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002822
H. Peter Anvine2c80182005-01-15 22:15:51 +00002823 mac = istk->mstk;
2824 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2825 mac = mac->next_active;
2826 if (!mac)
2827 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2828 else
2829 switch (t->text[1]) {
2830 /*
2831 * We have to make a substitution of one of the
2832 * forms %1, %-1, %+1, %%foo, %0.
2833 */
2834 case '0':
2835 type = TOK_NUMBER;
2836 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
2837 text = nasm_strdup(tmpbuf);
2838 break;
2839 case '%':
2840 type = TOK_ID;
Keith Kanios93f2e9a2007-04-14 00:10:59 +00002841 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu32".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002842 mac->unique);
2843 text = nasm_strcat(tmpbuf, t->text + 2);
2844 break;
2845 case '-':
2846 n = atoi(t->text + 2) - 1;
2847 if (n >= mac->nparam)
2848 tt = NULL;
2849 else {
2850 if (mac->nparam > 1)
2851 n = (n + mac->rotate) % mac->nparam;
2852 tt = mac->params[n];
2853 }
2854 cc = find_cc(tt);
2855 if (cc == -1) {
2856 error(ERR_NONFATAL,
2857 "macro parameter %d is not a condition code",
2858 n + 1);
2859 text = NULL;
2860 } else {
2861 type = TOK_ID;
2862 if (inverse_ccs[cc] == -1) {
2863 error(ERR_NONFATAL,
2864 "condition code `%s' is not invertible",
2865 conditions[cc]);
2866 text = NULL;
2867 } else
2868 text =
2869 nasm_strdup(conditions[inverse_ccs[cc]]);
2870 }
2871 break;
2872 case '+':
2873 n = atoi(t->text + 2) - 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 cc = find_cc(tt);
2882 if (cc == -1) {
2883 error(ERR_NONFATAL,
2884 "macro parameter %d is not a condition code",
2885 n + 1);
2886 text = NULL;
2887 } else {
2888 type = TOK_ID;
2889 text = nasm_strdup(conditions[cc]);
2890 }
2891 break;
2892 default:
2893 n = atoi(t->text + 1) - 1;
2894 if (n >= mac->nparam)
2895 tt = NULL;
2896 else {
2897 if (mac->nparam > 1)
2898 n = (n + mac->rotate) % mac->nparam;
2899 tt = mac->params[n];
2900 }
2901 if (tt) {
2902 for (i = 0; i < mac->paramlen[n]; i++) {
2903 *tail = new_Token(NULL, tt->type, tt->text, 0);
2904 tail = &(*tail)->next;
2905 tt = tt->next;
2906 }
2907 }
2908 text = NULL; /* we've done it here */
2909 break;
2910 }
2911 if (!text) {
2912 delete_Token(t);
2913 } else {
2914 *tail = t;
2915 tail = &t->next;
2916 t->type = type;
2917 nasm_free(t->text);
2918 t->text = text;
2919 t->mac = NULL;
2920 }
2921 continue;
2922 } else {
2923 t = *tail = tline;
2924 tline = tline->next;
2925 t->mac = NULL;
2926 tail = &t->next;
2927 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002928 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00002929 *tail = NULL;
2930 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002931 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002932 switch (t->type) {
2933 case TOK_WHITESPACE:
2934 if (tt->type == TOK_WHITESPACE) {
2935 t->next = delete_Token(tt);
2936 }
2937 break;
2938 case TOK_ID:
2939 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002940 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002941 nasm_free(t->text);
2942 t->text = tmp;
2943 t->next = delete_Token(tt);
2944 }
2945 break;
2946 case TOK_NUMBER:
2947 if (tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002948 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002949 nasm_free(t->text);
2950 t->text = tmp;
2951 t->next = delete_Token(tt);
2952 }
2953 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002954 default:
2955 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002956 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002957
H. Peter Anvin76690a12002-04-30 20:52:49 +00002958 return thead;
2959}
2960
2961/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002962 * Expand all single-line macro calls made in the given line.
2963 * Return the expanded version of the line. The original is deemed
2964 * to be destroyed in the process. (In reality we'll just move
2965 * Tokens from input to output a lot of the time, rather than
2966 * actually bothering to destroy and replicate.)
2967 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002968static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002969{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002970 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002971 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002972 Token **params;
2973 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07002974 unsigned int nparam, sparam;
2975 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002976 Token *org_tline = tline;
2977 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002978 char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002979
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002980 /*
2981 * Trick: we should avoid changing the start token pointer since it can
2982 * be contained in "next" field of other token. Because of this
2983 * we allocate a copy of first token and work with it; at the end of
2984 * routine we copy it back
2985 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002986 if (org_tline) {
2987 tline =
2988 new_Token(org_tline->next, org_tline->type, org_tline->text,
2989 0);
2990 tline->mac = org_tline->mac;
2991 nasm_free(org_tline->text);
2992 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002993 }
2994
H. Peter Anvin734b1882002-04-30 21:01:08 +00002995 again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002996 tail = &thead;
2997 thead = NULL;
2998
H. Peter Anvine2c80182005-01-15 22:15:51 +00002999 while (tline) { /* main token loop */
3000 if ((mname = tline->text)) {
3001 /* if this token is a local macro, look in local context */
3002 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
3003 ctx = get_ctx(mname, TRUE);
3004 else
3005 ctx = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003006 if (!ctx) {
3007 head = (SMacro *) hash_findix(smacros, mname);
3008 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003009 head = ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003010 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003011 /*
3012 * We've hit an identifier. As in is_mmacro below, we first
3013 * check whether the identifier is a single-line macro at
3014 * all, then think about checking for parameters if
3015 * necessary.
3016 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003017 for (m = head; m; m = m->next)
3018 if (!mstrcmp(m->name, mname, m->casesense))
3019 break;
3020 if (m) {
3021 mstart = tline;
3022 params = NULL;
3023 paramsize = NULL;
3024 if (m->nparam == 0) {
3025 /*
3026 * Simple case: the macro is parameterless. Discard the
3027 * one token that the macro call took, and push the
3028 * expansion back on the to-do stack.
3029 */
3030 if (!m->expansion) {
3031 if (!strcmp("__FILE__", m->name)) {
3032 int32_t num = 0;
3033 src_get(&num, &(tline->text));
3034 nasm_quote(&(tline->text));
3035 tline->type = TOK_STRING;
3036 continue;
3037 }
3038 if (!strcmp("__LINE__", m->name)) {
3039 nasm_free(tline->text);
3040 make_tok_num(tline, src_get_linnum());
3041 continue;
3042 }
3043 if (!strcmp("__BITS__", m->name)) {
3044 nasm_free(tline->text);
3045 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003046 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003047 }
3048 tline = delete_Token(tline);
3049 continue;
3050 }
3051 } else {
3052 /*
3053 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003054 * exists and takes parameters. We must find the
3055 * parameters in the call, count them, find the SMacro
3056 * that corresponds to that form of the macro call, and
3057 * substitute for the parameters when we expand. What a
3058 * pain.
3059 */
3060 /*tline = tline->next;
3061 skip_white_(tline); */
3062 do {
3063 t = tline->next;
3064 while (tok_type_(t, TOK_SMAC_END)) {
3065 t->mac->in_progress = FALSE;
3066 t->text = NULL;
3067 t = tline->next = delete_Token(t);
3068 }
3069 tline = t;
3070 } while (tok_type_(tline, TOK_WHITESPACE));
3071 if (!tok_is_(tline, "(")) {
3072 /*
3073 * This macro wasn't called with parameters: ignore
3074 * the call. (Behaviour borrowed from gnu cpp.)
3075 */
3076 tline = mstart;
3077 m = NULL;
3078 } else {
3079 int paren = 0;
3080 int white = 0;
3081 brackets = 0;
3082 nparam = 0;
3083 sparam = PARAM_DELTA;
3084 params = nasm_malloc(sparam * sizeof(Token *));
3085 params[0] = tline->next;
3086 paramsize = nasm_malloc(sparam * sizeof(int));
3087 paramsize[0] = 0;
3088 while (TRUE) { /* parameter loop */
3089 /*
3090 * For some unusual expansions
3091 * which concatenates function call
3092 */
3093 t = tline->next;
3094 while (tok_type_(t, TOK_SMAC_END)) {
3095 t->mac->in_progress = FALSE;
3096 t->text = NULL;
3097 t = tline->next = delete_Token(t);
3098 }
3099 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003100
H. Peter Anvine2c80182005-01-15 22:15:51 +00003101 if (!tline) {
3102 error(ERR_NONFATAL,
3103 "macro call expects terminating `)'");
3104 break;
3105 }
3106 if (tline->type == TOK_WHITESPACE
3107 && brackets <= 0) {
3108 if (paramsize[nparam])
3109 white++;
3110 else
3111 params[nparam] = tline->next;
3112 continue; /* parameter loop */
3113 }
3114 if (tline->type == TOK_OTHER
3115 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003116 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003117 if (ch == ',' && !paren && brackets <= 0) {
3118 if (++nparam >= sparam) {
3119 sparam += PARAM_DELTA;
3120 params = nasm_realloc(params,
3121 sparam *
3122 sizeof(Token
3123 *));
3124 paramsize =
3125 nasm_realloc(paramsize,
3126 sparam *
3127 sizeof(int));
3128 }
3129 params[nparam] = tline->next;
3130 paramsize[nparam] = 0;
3131 white = 0;
3132 continue; /* parameter loop */
3133 }
3134 if (ch == '{' &&
3135 (brackets > 0 || (brackets == 0 &&
3136 !paramsize[nparam])))
3137 {
3138 if (!(brackets++)) {
3139 params[nparam] = tline->next;
3140 continue; /* parameter loop */
3141 }
3142 }
3143 if (ch == '}' && brackets > 0)
3144 if (--brackets == 0) {
3145 brackets = -1;
3146 continue; /* parameter loop */
3147 }
3148 if (ch == '(' && !brackets)
3149 paren++;
3150 if (ch == ')' && brackets <= 0)
3151 if (--paren < 0)
3152 break;
3153 }
3154 if (brackets < 0) {
3155 brackets = 0;
3156 error(ERR_NONFATAL, "braces do not "
3157 "enclose all of macro parameter");
3158 }
3159 paramsize[nparam] += white + 1;
3160 white = 0;
3161 } /* parameter loop */
3162 nparam++;
3163 while (m && (m->nparam != nparam ||
3164 mstrcmp(m->name, mname,
3165 m->casesense)))
3166 m = m->next;
3167 if (!m)
3168 error(ERR_WARNING | ERR_WARN_MNP,
3169 "macro `%s' exists, "
3170 "but not taking %d parameters",
3171 mstart->text, nparam);
3172 }
3173 }
3174 if (m && m->in_progress)
3175 m = NULL;
3176 if (!m) { /* in progess or didn't find '(' or wrong nparam */
3177 /*
3178 * Design question: should we handle !tline, which
3179 * indicates missing ')' here, or expand those
3180 * macros anyway, which requires the (t) test a few
3181 * lines down?
3182 */
3183 nasm_free(params);
3184 nasm_free(paramsize);
3185 tline = mstart;
3186 } else {
3187 /*
3188 * Expand the macro: we are placed on the last token of the
3189 * call, so that we can easily split the call from the
3190 * following tokens. We also start by pushing an SMAC_END
3191 * token for the cycle removal.
3192 */
3193 t = tline;
3194 if (t) {
3195 tline = t->next;
3196 t->next = NULL;
3197 }
3198 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3199 tt->mac = m;
3200 m->in_progress = TRUE;
3201 tline = tt;
3202 for (t = m->expansion; t; t = t->next) {
3203 if (t->type >= TOK_SMAC_PARAM) {
3204 Token *pcopy = tline, **ptail = &pcopy;
3205 Token *ttt, *pt;
3206 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003207
H. Peter Anvine2c80182005-01-15 22:15:51 +00003208 ttt = params[t->type - TOK_SMAC_PARAM];
3209 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3210 --i >= 0;) {
3211 pt = *ptail =
3212 new_Token(tline, ttt->type, ttt->text,
3213 0);
3214 ptail = &pt->next;
3215 ttt = ttt->next;
3216 }
3217 tline = pcopy;
3218 } else {
3219 tt = new_Token(tline, t->type, t->text, 0);
3220 tline = tt;
3221 }
3222 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003223
H. Peter Anvine2c80182005-01-15 22:15:51 +00003224 /*
3225 * Having done that, get rid of the macro call, and clean
3226 * up the parameters.
3227 */
3228 nasm_free(params);
3229 nasm_free(paramsize);
3230 free_tlist(mstart);
3231 continue; /* main token loop */
3232 }
3233 }
3234 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003235
H. Peter Anvine2c80182005-01-15 22:15:51 +00003236 if (tline->type == TOK_SMAC_END) {
3237 tline->mac->in_progress = FALSE;
3238 tline = delete_Token(tline);
3239 } else {
3240 t = *tail = tline;
3241 tline = tline->next;
3242 t->mac = NULL;
3243 t->next = NULL;
3244 tail = &t->next;
3245 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003246 }
3247
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003248 /*
3249 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003250 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003251 * TOK_IDs should be concatenated.
3252 * Also we look for %+ tokens and concatenate the tokens before and after
3253 * them (without white spaces in between).
3254 */
3255 t = thead;
3256 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003257 while (t) {
3258 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3259 t = t->next;
3260 if (!t || !t->next)
3261 break;
3262 if (t->next->type == TOK_ID ||
3263 t->next->type == TOK_PREPROC_ID ||
3264 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003265 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003266 nasm_free(t->text);
3267 t->next = delete_Token(t->next);
3268 t->text = p;
3269 rescan = 1;
3270 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3271 t->next->next->type == TOK_PREPROC_ID &&
3272 strcmp(t->next->next->text, "%+") == 0) {
3273 /* free the next whitespace, the %+ token and next whitespace */
3274 int i;
3275 for (i = 1; i <= 3; i++) {
3276 if (!t->next
3277 || (i != 2 && t->next->type != TOK_WHITESPACE))
3278 break;
3279 t->next = delete_Token(t->next);
3280 } /* endfor */
3281 } else
3282 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003283 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003284 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003285 if (rescan) {
3286 tline = thead;
3287 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003288 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003289
H. Peter Anvine2c80182005-01-15 22:15:51 +00003290 if (org_tline) {
3291 if (thead) {
3292 *org_tline = *thead;
3293 /* since we just gave text to org_line, don't free it */
3294 thead->text = NULL;
3295 delete_Token(thead);
3296 } else {
3297 /* the expression expanded to empty line;
3298 we can't return NULL for some reasons
3299 we just set the line to a single WHITESPACE token. */
3300 memset(org_tline, 0, sizeof(*org_tline));
3301 org_tline->text = NULL;
3302 org_tline->type = TOK_WHITESPACE;
3303 }
3304 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003305 }
3306
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003307 return thead;
3308}
3309
3310/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003311 * Similar to expand_smacro but used exclusively with macro identifiers
3312 * right before they are fetched in. The reason is that there can be
3313 * identifiers consisting of several subparts. We consider that if there
3314 * are more than one element forming the name, user wants a expansion,
3315 * otherwise it will be left as-is. Example:
3316 *
3317 * %define %$abc cde
3318 *
3319 * the identifier %$abc will be left as-is so that the handler for %define
3320 * will suck it and define the corresponding value. Other case:
3321 *
3322 * %define _%$abc cde
3323 *
3324 * In this case user wants name to be expanded *before* %define starts
3325 * working, so we'll expand %$abc into something (if it has a value;
3326 * otherwise it will be left as-is) then concatenate all successive
3327 * PP_IDs into one.
3328 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003329static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003330{
3331 Token *cur, *oldnext = NULL;
3332
H. Peter Anvin734b1882002-04-30 21:01:08 +00003333 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003334 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003335
3336 cur = tline;
3337 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003338 (cur->next->type == TOK_ID ||
3339 cur->next->type == TOK_PREPROC_ID
3340 || cur->next->type == TOK_NUMBER))
3341 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003342
3343 /* If identifier consists of just one token, don't expand */
3344 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003345 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003346
H. Peter Anvine2c80182005-01-15 22:15:51 +00003347 if (cur) {
3348 oldnext = cur->next; /* Detach the tail past identifier */
3349 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003350 }
3351
H. Peter Anvin734b1882002-04-30 21:01:08 +00003352 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003353
H. Peter Anvine2c80182005-01-15 22:15:51 +00003354 if (cur) {
3355 /* expand_smacro possibly changhed tline; re-scan for EOL */
3356 cur = tline;
3357 while (cur && cur->next)
3358 cur = cur->next;
3359 if (cur)
3360 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003361 }
3362
3363 return tline;
3364}
3365
3366/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003367 * Determine whether the given line constitutes a multi-line macro
3368 * call, and return the MMacro structure called if so. Doesn't have
3369 * to check for an initial label - that's taken care of in
3370 * expand_mmacro - but must check numbers of parameters. Guaranteed
3371 * to be called with tline->type == TOK_ID, so the putative macro
3372 * name is easy to find.
3373 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003374static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003375{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003376 MMacro *head, *m;
3377 Token **params;
3378 int nparam;
3379
H. Peter Anvin97a23472007-09-16 17:57:25 -07003380 head = (MMacro *) hash_findix(mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003381
3382 /*
3383 * Efficiency: first we see if any macro exists with the given
3384 * name. If not, we can return NULL immediately. _Then_ we
3385 * count the parameters, and then we look further along the
3386 * list if necessary to find the proper MMacro.
3387 */
3388 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003389 if (!mstrcmp(m->name, tline->text, m->casesense))
3390 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003391 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003392 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003393
3394 /*
3395 * OK, we have a potential macro. Count and demarcate the
3396 * parameters.
3397 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003398 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003399
3400 /*
3401 * So we know how many parameters we've got. Find the MMacro
3402 * structure that handles this number.
3403 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003404 while (m) {
3405 if (m->nparam_min <= nparam
3406 && (m->plus || nparam <= m->nparam_max)) {
3407 /*
3408 * This one is right. Just check if cycle removal
3409 * prohibits us using it before we actually celebrate...
3410 */
3411 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003412#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003413 error(ERR_NONFATAL,
3414 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003415#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003416 nasm_free(params);
3417 return NULL;
3418 }
3419 /*
3420 * It's right, and we can use it. Add its default
3421 * parameters to the end of our list if necessary.
3422 */
3423 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3424 params =
3425 nasm_realloc(params,
3426 ((m->nparam_min + m->ndefs +
3427 1) * sizeof(*params)));
3428 while (nparam < m->nparam_min + m->ndefs) {
3429 params[nparam] = m->defaults[nparam - m->nparam_min];
3430 nparam++;
3431 }
3432 }
3433 /*
3434 * If we've gone over the maximum parameter count (and
3435 * we're in Plus mode), ignore parameters beyond
3436 * nparam_max.
3437 */
3438 if (m->plus && nparam > m->nparam_max)
3439 nparam = m->nparam_max;
3440 /*
3441 * Then terminate the parameter list, and leave.
3442 */
3443 if (!params) { /* need this special case */
3444 params = nasm_malloc(sizeof(*params));
3445 nparam = 0;
3446 }
3447 params[nparam] = NULL;
3448 *params_array = params;
3449 return m;
3450 }
3451 /*
3452 * This one wasn't right: look for the next one with the
3453 * same name.
3454 */
3455 for (m = m->next; m; m = m->next)
3456 if (!mstrcmp(m->name, tline->text, m->casesense))
3457 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003458 }
3459
3460 /*
3461 * After all that, we didn't find one with the right number of
3462 * parameters. Issue a warning, and fail to expand the macro.
3463 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003464 error(ERR_WARNING | ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003465 "macro `%s' exists, but not taking %d parameters",
3466 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003467 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003468 return NULL;
3469}
3470
3471/*
3472 * Expand the multi-line macro call made by the given line, if
3473 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003474 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003475 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003476static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003477{
3478 Token *startline = tline;
3479 Token *label = NULL;
3480 int dont_prepend = 0;
3481 Token **params, *t, *tt;
3482 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003483 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003484 int i, nparam, *paramlen;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003485
3486 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003487 skip_white_(t);
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003488/* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3489 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003490 return 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003491 m = is_mmacro(t, &params);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003492 if (!m) {
3493 Token *last;
3494 /*
3495 * We have an id which isn't a macro call. We'll assume
3496 * it might be a label; we'll also check to see if a
3497 * colon follows it. Then, if there's another id after
3498 * that lot, we'll check it again for macro-hood.
3499 */
3500 label = last = t;
3501 t = t->next;
3502 if (tok_type_(t, TOK_WHITESPACE))
3503 last = t, t = t->next;
3504 if (tok_is_(t, ":")) {
3505 dont_prepend = 1;
3506 last = t, t = t->next;
3507 if (tok_type_(t, TOK_WHITESPACE))
3508 last = t, t = t->next;
3509 }
3510 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3511 return 0;
3512 last->next = NULL;
3513 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003514 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003515
3516 /*
3517 * Fix up the parameters: this involves stripping leading and
3518 * trailing whitespace, then stripping braces if they are
3519 * present.
3520 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003521 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003522 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003523
H. Peter Anvine2c80182005-01-15 22:15:51 +00003524 for (i = 0; params[i]; i++) {
3525 int brace = FALSE;
3526 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003527
H. Peter Anvine2c80182005-01-15 22:15:51 +00003528 t = params[i];
3529 skip_white_(t);
3530 if (tok_is_(t, "{"))
3531 t = t->next, brace = TRUE, comma = FALSE;
3532 params[i] = t;
3533 paramlen[i] = 0;
3534 while (t) {
3535 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3536 break; /* ... because we have hit a comma */
3537 if (comma && t->type == TOK_WHITESPACE
3538 && tok_is_(t->next, ","))
3539 break; /* ... or a space then a comma */
3540 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3541 break; /* ... or a brace */
3542 t = t->next;
3543 paramlen[i]++;
3544 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003545 }
3546
3547 /*
3548 * OK, we have a MMacro structure together with a set of
3549 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003550 * copies of each Line on to istk->expansion. Substitution of
3551 * parameter tokens and macro-local tokens doesn't get done
3552 * until the single-line macro substitution process; this is
3553 * because delaying them allows us to change the semantics
3554 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003555 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003556 * First, push an end marker on to istk->expansion, mark this
3557 * macro as in progress, and set up its invocation-specific
3558 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003559 */
3560 ll = nasm_malloc(sizeof(Line));
3561 ll->next = istk->expansion;
3562 ll->finishes = m;
3563 ll->first = NULL;
3564 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003565
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003566 m->in_progress = TRUE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003567 m->params = params;
3568 m->iline = tline;
3569 m->nparam = nparam;
3570 m->rotate = 0;
3571 m->paramlen = paramlen;
3572 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003573 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003574
3575 m->next_active = istk->mstk;
3576 istk->mstk = m;
3577
H. Peter Anvine2c80182005-01-15 22:15:51 +00003578 for (l = m->expansion; l; l = l->next) {
3579 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003580
H. Peter Anvine2c80182005-01-15 22:15:51 +00003581 ll = nasm_malloc(sizeof(Line));
3582 ll->finishes = NULL;
3583 ll->next = istk->expansion;
3584 istk->expansion = ll;
3585 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003586
H. Peter Anvine2c80182005-01-15 22:15:51 +00003587 for (t = l->first; t; t = t->next) {
3588 Token *x = t;
3589 if (t->type == TOK_PREPROC_ID &&
3590 t->text[1] == '0' && t->text[2] == '0') {
3591 dont_prepend = -1;
3592 x = label;
3593 if (!x)
3594 continue;
3595 }
3596 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3597 tail = &tt->next;
3598 }
3599 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003600 }
3601
3602 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00003603 * If we had a label, push it on as the first line of
3604 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003605 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003606 if (label) {
3607 if (dont_prepend < 0)
3608 free_tlist(startline);
3609 else {
3610 ll = nasm_malloc(sizeof(Line));
3611 ll->finishes = NULL;
3612 ll->next = istk->expansion;
3613 istk->expansion = ll;
3614 ll->first = startline;
3615 if (!dont_prepend) {
3616 while (label->next)
3617 label = label->next;
3618 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3619 }
3620 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003621 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003622
H. Peter Anvin734b1882002-04-30 21:01:08 +00003623 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003624
H. Peter Anvineba20a72002-04-30 20:53:55 +00003625 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003626}
3627
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003628/*
3629 * Since preprocessor always operate only on the line that didn't
3630 * arrived yet, we should always use ERR_OFFBY1. Also since user
3631 * won't want to see same error twice (preprocessing is done once
3632 * per pass) we will want to show errors only during pass one.
3633 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003634static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003635{
3636 va_list arg;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003637 char buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003638
3639 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003640 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003641 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003642
H. Peter Anvin734b1882002-04-30 21:01:08 +00003643 va_start(arg, fmt);
Ed Beroset19f927a2004-12-15 17:07:03 +00003644 vsnprintf(buff, sizeof(buff), fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003645 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003646
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003647 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003648 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3649 istk->mstk->lineno, buff);
3650 else
3651 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003652}
3653
H. Peter Anvin734b1882002-04-30 21:01:08 +00003654static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003655pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003656 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003657{
H. Peter Anvin99941bf2002-05-14 17:44:03 +00003658 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003659 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003660 istk = nasm_malloc(sizeof(Include));
3661 istk->next = NULL;
3662 istk->conds = NULL;
3663 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003664 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003665 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00003666 istk->fname = NULL;
3667 src_set_fname(nasm_strdup(file));
3668 src_set_linnum(0);
3669 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003670 if (!istk->fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003671 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3672 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003673 defining = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003674 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003675 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003676 if (tasm_compatible_mode) {
3677 stdmacpos = stdmac;
3678 } else {
3679 stdmacpos = &stdmac[TASM_MACRO_COUNT];
3680 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003681 any_extrastdmac = (extrastdmac != NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003682 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003683 evaluate = eval;
3684 pass = apass;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003685}
3686
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003687static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003688{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003689 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003690 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003691
H. Peter Anvine2c80182005-01-15 22:15:51 +00003692 while (1) {
3693 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003694 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00003695 * buffer or from the input file.
3696 */
3697 tline = NULL;
3698 while (istk->expansion && istk->expansion->finishes) {
3699 Line *l = istk->expansion;
3700 if (!l->finishes->name && l->finishes->in_progress > 1) {
3701 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003702
H. Peter Anvine2c80182005-01-15 22:15:51 +00003703 /*
3704 * This is a macro-end marker for a macro with no
3705 * name, which means it's not really a macro at all
3706 * but a %rep block, and the `in_progress' field is
3707 * more than 1, meaning that we still need to
3708 * repeat. (1 means the natural last repetition; 0
3709 * means termination by %exitrep.) We have
3710 * therefore expanded up to the %endrep, and must
3711 * push the whole block on to the expansion buffer
3712 * again. We don't bother to remove the macro-end
3713 * marker: we'd only have to generate another one
3714 * if we did.
3715 */
3716 l->finishes->in_progress--;
3717 for (l = l->finishes->expansion; l; l = l->next) {
3718 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003719
H. Peter Anvine2c80182005-01-15 22:15:51 +00003720 ll = nasm_malloc(sizeof(Line));
3721 ll->next = istk->expansion;
3722 ll->finishes = NULL;
3723 ll->first = NULL;
3724 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003725
H. Peter Anvine2c80182005-01-15 22:15:51 +00003726 for (t = l->first; t; t = t->next) {
3727 if (t->text || t->type == TOK_WHITESPACE) {
3728 tt = *tail =
3729 new_Token(NULL, t->type, t->text, 0);
3730 tail = &tt->next;
3731 }
3732 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003733
H. Peter Anvine2c80182005-01-15 22:15:51 +00003734 istk->expansion = ll;
3735 }
3736 } else {
3737 /*
3738 * Check whether a `%rep' was started and not ended
3739 * within this macro expansion. This can happen and
3740 * should be detected. It's a fatal error because
3741 * I'm too confused to work out how to recover
3742 * sensibly from it.
3743 */
3744 if (defining) {
3745 if (defining->name)
3746 error(ERR_PANIC,
3747 "defining with name in expansion");
3748 else if (istk->mstk->name)
3749 error(ERR_FATAL,
3750 "`%%rep' without `%%endrep' within"
3751 " expansion of macro `%s'",
3752 istk->mstk->name);
3753 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00003754
H. Peter Anvine2c80182005-01-15 22:15:51 +00003755 /*
3756 * FIXME: investigate the relationship at this point between
3757 * istk->mstk and l->finishes
3758 */
3759 {
3760 MMacro *m = istk->mstk;
3761 istk->mstk = m->next_active;
3762 if (m->name) {
3763 /*
3764 * This was a real macro call, not a %rep, and
3765 * therefore the parameter information needs to
3766 * be freed.
3767 */
3768 nasm_free(m->params);
3769 free_tlist(m->iline);
3770 nasm_free(m->paramlen);
3771 l->finishes->in_progress = FALSE;
3772 } else
3773 free_mmacro(m);
3774 }
3775 istk->expansion = l->next;
3776 nasm_free(l);
3777 list->downlevel(LIST_MACRO);
3778 }
3779 }
3780 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003781
H. Peter Anvine2c80182005-01-15 22:15:51 +00003782 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003783 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003784 Line *l = istk->expansion;
3785 if (istk->mstk)
3786 istk->mstk->lineno++;
3787 tline = l->first;
3788 istk->expansion = l->next;
3789 nasm_free(l);
3790 p = detoken(tline, FALSE);
3791 list->line(LIST_MACRO, p);
3792 nasm_free(p);
3793 break;
3794 }
3795 line = read_line();
3796 if (line) { /* from the current input file */
3797 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00003798 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003799 nasm_free(line);
3800 break;
3801 }
3802 /*
3803 * The current file has ended; work down the istk
3804 */
3805 {
3806 Include *i = istk;
3807 fclose(i->fp);
3808 if (i->conds)
3809 error(ERR_FATAL,
3810 "expected `%%endif' before end of file");
3811 /* only set line and file name if there's a next node */
3812 if (i->next) {
3813 src_set_linnum(i->lineno);
3814 nasm_free(src_set_fname(i->fname));
3815 }
3816 istk = i->next;
3817 list->downlevel(LIST_INCLUDE);
3818 nasm_free(i);
3819 if (!istk)
3820 return NULL;
3821 }
3822 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003823
H. Peter Anvine2c80182005-01-15 22:15:51 +00003824 /*
3825 * We must expand MMacro parameters and MMacro-local labels
3826 * _before_ we plunge into directive processing, to cope
3827 * with things like `%define something %1' such as STRUC
3828 * uses. Unless we're _defining_ a MMacro, in which case
3829 * those tokens should be left alone to go into the
3830 * definition; and unless we're in a non-emitting
3831 * condition, in which case we don't want to meddle with
3832 * anything.
3833 */
3834 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
3835 tline = expand_mmac_params(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003836
H. Peter Anvine2c80182005-01-15 22:15:51 +00003837 /*
3838 * Check the line to see if it's a preprocessor directive.
3839 */
3840 if (do_directive(tline) == DIRECTIVE_FOUND) {
3841 continue;
3842 } else if (defining) {
3843 /*
3844 * We're defining a multi-line macro. We emit nothing
3845 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00003846 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003847 */
3848 Line *l = nasm_malloc(sizeof(Line));
3849 l->next = defining->expansion;
3850 l->first = tline;
3851 l->finishes = FALSE;
3852 defining->expansion = l;
3853 continue;
3854 } else if (istk->conds && !emitting(istk->conds->state)) {
3855 /*
3856 * We're in a non-emitting branch of a condition block.
3857 * Emit nothing at all, not even a blank line: when we
3858 * emerge from the condition we'll give a line-number
3859 * directive so we keep our place correctly.
3860 */
3861 free_tlist(tline);
3862 continue;
3863 } else if (istk->mstk && !istk->mstk->in_progress) {
3864 /*
3865 * We're in a %rep block which has been terminated, so
3866 * we're walking through to the %endrep without
3867 * emitting anything. Emit nothing at all, not even a
3868 * blank line: when we emerge from the %rep block we'll
3869 * give a line-number directive so we keep our place
3870 * correctly.
3871 */
3872 free_tlist(tline);
3873 continue;
3874 } else {
3875 tline = expand_smacro(tline);
3876 if (!expand_mmacro(tline)) {
3877 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003878 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003879 */
3880 line = detoken(tline, TRUE);
3881 free_tlist(tline);
3882 break;
3883 } else {
3884 continue; /* expand_mmacro calls free_tlist */
3885 }
3886 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003887 }
3888
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003889 return line;
3890}
3891
H. Peter Anvine2c80182005-01-15 22:15:51 +00003892static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003893{
H. Peter Anvine2c80182005-01-15 22:15:51 +00003894 if (defining) {
3895 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
3896 defining->name);
3897 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003898 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003899 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003900 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07003901 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00003902 while (istk) {
3903 Include *i = istk;
3904 istk = istk->next;
3905 fclose(i->fp);
3906 nasm_free(i->fname);
3907 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003908 }
3909 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003910 ctx_pop();
3911 if (pass == 0) {
3912 free_llist(predef);
3913 delete_Blocks();
3914 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003915}
3916
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003917void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003918{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003919 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07003920
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003921 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07003922 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003923 i->next = NULL;
3924
H. Peter Anvine2c80182005-01-15 22:15:51 +00003925 if (ipath != NULL) {
3926 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003927 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003928 j = j->next;
3929 j->next = i;
3930 } else {
3931 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003932 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003933}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003934
3935/*
3936 * added by alexfru:
3937 *
3938 * This function is used to "export" the include paths, e.g.
3939 * the paths specified in the '-I' command switch.
3940 * The need for such exporting is due to the 'incbin' directive,
3941 * which includes raw binary files (unlike '%include', which
3942 * includes text source files). It would be real nice to be
3943 * able to specify paths to search for incbin'ned files also.
3944 * So, this is a simple workaround.
3945 *
3946 * The function use is simple:
3947 *
3948 * The 1st call (with NULL argument) returns a pointer to the 1st path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003949 * (char** type) or NULL if none include paths available.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003950 *
3951 * All subsequent calls take as argument the value returned by this
3952 * function last. The return value is either the next path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003953 * (char** type) or NULL if the end of the paths list is reached.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003954 *
3955 * It is maybe not the best way to do things, but I didn't want
3956 * to export too much, just one or two functions and no types or
3957 * variables exported.
3958 *
3959 * Can't say I like the current situation with e.g. this path list either,
3960 * it seems to be never deallocated after creation...
3961 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003962char **pp_get_include_path_ptr(char **pPrevPath)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003963{
3964/* This macro returns offset of a member of a structure */
3965#define GetMemberOffset(StructType,MemberName)\
3966 ((size_t)&((StructType*)0)->MemberName)
3967 IncPath *i;
3968
H. Peter Anvine2c80182005-01-15 22:15:51 +00003969 if (pPrevPath == NULL) {
3970 if (ipath != NULL)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003971 return &ipath->path;
3972 else
3973 return NULL;
3974 }
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003975 i = (IncPath *) ((char *)pPrevPath - GetMemberOffset(IncPath, path));
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003976 i = i->next;
3977 if (i != NULL)
3978 return &i->path;
3979 else
3980 return NULL;
3981#undef GetMemberOffset
3982}
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003983
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003984void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003985{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003986 Token *inc, *space, *name;
3987 Line *l;
3988
H. Peter Anvin734b1882002-04-30 21:01:08 +00003989 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
3990 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
3991 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003992
3993 l = nasm_malloc(sizeof(Line));
3994 l->next = predef;
3995 l->first = inc;
3996 l->finishes = FALSE;
3997 predef = l;
3998}
3999
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004000void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004001{
4002 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004003 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004004 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004005
4006 equals = strchr(definition, '=');
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, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004009 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004010 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004011 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004012 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004013 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004014
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004015 l = nasm_malloc(sizeof(Line));
4016 l->next = predef;
4017 l->first = def;
4018 l->finishes = FALSE;
4019 predef = l;
4020}
4021
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004022void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004023{
4024 Token *def, *space;
4025 Line *l;
4026
H. Peter Anvin734b1882002-04-30 21:01:08 +00004027 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4028 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004029 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004030
4031 l = nasm_malloc(sizeof(Line));
4032 l->next = predef;
4033 l->first = def;
4034 l->finishes = FALSE;
4035 predef = l;
4036}
4037
Keith Kaniosb7a89542007-04-12 02:40:54 +00004038/*
4039 * Added by Keith Kanios:
4040 *
4041 * This function is used to assist with "runtime" preprocessor
4042 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4043 *
4044 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4045 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4046 */
4047
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004048void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004049{
4050 Token *def;
4051
4052 def = tokenize(definition);
4053 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4054 free_tlist(def);
4055
4056}
4057
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004058void pp_extra_stdmac(const char **macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004059{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004060 extrastdmac = macros;
4061}
4062
Keith Kaniosb7a89542007-04-12 02:40:54 +00004063static void make_tok_num(Token * tok, int32_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004064{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004065 char numbuf[20];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00004066 snprintf(numbuf, sizeof(numbuf), "%"PRId32"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004067 tok->text = nasm_strdup(numbuf);
4068 tok->type = TOK_NUMBER;
4069}
4070
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004071Preproc nasmpp = {
4072 pp_reset,
4073 pp_getline,
4074 pp_cleanup
4075};