blob: bcef30ec1f5b3d01fd29ca4dba8bd9f84d068c36 [file] [log] [blame]
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001/* preproc.c macro preprocessor for the Netwide Assembler
2 *
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
7 *
8 * initial version 18/iii/97 by Simon Tatham
9 */
10
H. Peter Anvin4836e332002-04-30 20:56:43 +000011/* Typical flow of text through preproc
12 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000013 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000014 *
15 * from a macro expansion
16 *
17 * or
18 * {
19 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000020 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000021 * }
22 *
23 * expand_mmac_params is used to expand %1 etc., unless a macro is being
24 * defined or a false conditional is being processed
25 * (%0, %1, %+1, %-1, %%foo
26 *
27 * do_directive checks for directives
28 *
29 * expand_smacro is used to expand single line macros
30 *
31 * expand_mmacro is used to expand multi-line macros
32 *
33 * detoken is used to convert the line back to text
34 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000035
H. Peter Anvinfe501952007-10-02 21:53:51 -070036#include "compiler.h"
37
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000038#include <stdio.h>
H. Peter Anvinaf535c12002-04-30 20:59:21 +000039#include <stdarg.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000040#include <stdlib.h>
41#include <stddef.h>
42#include <string.h>
43#include <ctype.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000044#include <limits.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000045#include <inttypes.h>
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000046
47#include "nasm.h"
48#include "nasmlib.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000049#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070050#include "hashtbl.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000051
52typedef struct SMacro SMacro;
53typedef struct MMacro MMacro;
54typedef struct Context Context;
55typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000056typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000057typedef struct Line Line;
58typedef struct Include Include;
59typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000060typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000061
62/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070063 * Note on the storage of both SMacro and MMacros: the hash table
64 * indexes them case-insensitively, and we then have to go through a
65 * linked list of potential case aliases (and, for MMacros, parameter
66 * ranges); this is to preserve the matching semantics of the earlier
67 * code. If the number of case aliases for a specific macro is a
68 * performance issue, you may want to reconsider your coding style.
69 */
70
71/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000072 * Store the definition of a single-line macro.
73 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000074struct SMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000075 SMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +000076 char *name;
H. Peter Anvin70055962007-10-11 00:05:31 -070077 bool casesense;
H. Peter Anvin16ed4382007-10-11 10:06:19 -070078 bool in_progress;
H. Peter Anvin70055962007-10-11 00:05:31 -070079 unsigned int nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000080 Token *expansion;
81};
82
83/*
H. Peter Anvin76690a12002-04-30 20:52:49 +000084 * Store the definition of a multi-line macro. This is also used to
85 * store the interiors of `%rep...%endrep' blocks, which are
86 * effectively self-re-invoking multi-line macros which simply
87 * don't have a name or bother to appear in the hash tables. %rep
88 * blocks are signified by having a NULL `name' field.
89 *
90 * In a MMacro describing a `%rep' block, the `in_progress' field
91 * isn't merely boolean, but gives the number of repeats left to
92 * run.
93 *
94 * The `next' field is used for storing MMacros in hash tables; the
95 * `next_active' field is for stacking them on istk entries.
96 *
97 * When a MMacro is being expanded, `params', `iline', `nparam',
98 * `paramlen', `rotate' and `unique' are local to the invocation.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000099 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000100struct MMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000101 MMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000102 char *name;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -0700103 int nparam_min, nparam_max;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700104 bool casesense;
105 bool plus; /* is the last parameter greedy? */
106 bool nolist; /* is this macro listing-inhibited? */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700107 int64_t in_progress;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000108 Token *dlist; /* All defaults as one list */
109 Token **defaults; /* Parameter default pointers */
110 int ndefs; /* number of default parameters */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000111 Line *expansion;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000112
113 MMacro *next_active;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000114 MMacro *rep_nest; /* used for nesting %rep */
115 Token **params; /* actual parameters */
116 Token *iline; /* invocation line */
H. Peter Anvin25a99342007-09-22 17:45:45 -0700117 unsigned int nparam, rotate;
118 int *paramlen;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700119 uint64_t unique;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000120 int lineno; /* Current line number on expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000121};
122
123/*
124 * The context stack is composed of a linked list of these.
125 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000126struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000127 Context *next;
128 SMacro *localmac;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000129 char *name;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000130 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000131};
132
133/*
134 * This is the internal form which we break input lines up into.
135 * Typically stored in linked lists.
136 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000137 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
138 * necessarily used as-is, but is intended to denote the number of
139 * the substituted parameter. So in the definition
140 *
141 * %define a(x,y) ( (x) & ~(y) )
142 *
143 * the token representing `x' will have its type changed to
144 * TOK_SMAC_PARAM, but the one representing `y' will be
145 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000146 *
147 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
148 * which doesn't need quotes around it. Used in the pre-include
149 * mechanism as an alternative to trying to find a sensible type of
150 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000151 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000152enum pp_token_type {
153 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
154 TOK_PREPROC_ID, TOK_STRING,
155 TOK_NUMBER, TOK_SMAC_END, TOK_OTHER, TOK_SMAC_PARAM,
156 TOK_INTERNAL_STRING
157};
158
H. Peter Anvine2c80182005-01-15 22:15:51 +0000159struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000160 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000161 char *text;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000162 SMacro *mac; /* associated macro for TOK_SMAC_END */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000163 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000164};
165
166/*
167 * Multi-line macro definitions are stored as a linked list of
168 * these, which is essentially a container to allow several linked
169 * lists of Tokens.
170 *
171 * Note that in this module, linked lists are treated as stacks
172 * wherever possible. For this reason, Lines are _pushed_ on to the
173 * `expansion' field in MMacro structures, so that the linked list,
174 * if walked, would give the macro lines in reverse order; this
175 * means that we can walk the list when expanding a macro, and thus
176 * push the lines on to the `expansion' field in _istk_ in reverse
177 * order (so that when popped back off they are in the right
178 * order). It may seem cockeyed, and it relies on my design having
179 * an even number of steps in, but it works...
180 *
181 * Some of these structures, rather than being actual lines, are
182 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000183 * This is for use in the cycle-tracking and %rep-handling code.
184 * Such structures have `finishes' non-NULL, and `first' NULL. All
185 * others have `finishes' NULL, but `first' may still be NULL if
186 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000187 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000188struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000189 Line *next;
190 MMacro *finishes;
191 Token *first;
192};
193
194/*
195 * To handle an arbitrary level of file inclusion, we maintain a
196 * stack (ie linked list) of these things.
197 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000198struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000199 Include *next;
200 FILE *fp;
201 Cond *conds;
202 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000203 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000204 int lineno, lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000205 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000206};
207
208/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000209 * Include search path. This is simply a list of strings which get
210 * prepended, in turn, to the name of an include file, in an
211 * attempt to find the file if it's not in the current directory.
212 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000213struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000214 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000215 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000216};
217
218/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000219 * Conditional assembly: we maintain a separate stack of these for
220 * each level of file inclusion. (The only reason we keep the
221 * stacks separate is to ensure that a stray `%endif' in a file
222 * included from within the true branch of a `%if' won't terminate
223 * it and cause confusion: instead, rightly, it'll cause an error.)
224 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000225struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000226 Cond *next;
227 int state;
228};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000229enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000230 /*
231 * These states are for use just after %if or %elif: IF_TRUE
232 * means the condition has evaluated to truth so we are
233 * currently emitting, whereas IF_FALSE means we are not
234 * currently emitting but will start doing so if a %else comes
235 * up. In these states, all directives are admissible: %elif,
236 * %else and %endif. (And of course %if.)
237 */
238 COND_IF_TRUE, COND_IF_FALSE,
239 /*
240 * These states come up after a %else: ELSE_TRUE means we're
241 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
242 * any %elif or %else will cause an error.
243 */
244 COND_ELSE_TRUE, COND_ELSE_FALSE,
245 /*
246 * This state means that we're not emitting now, and also that
247 * nothing until %endif will be emitted at all. It's for use in
248 * two circumstances: (i) when we've had our moment of emission
249 * and have now started seeing %elifs, and (ii) when the
250 * condition construct in question is contained within a
251 * non-emitting branch of a larger condition construct.
252 */
253 COND_NEVER
254};
255#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
256
Ed Beroset3ab3f412002-06-11 03:31:49 +0000257/*
258 * These defines are used as the possible return values for do_directive
259 */
260#define NO_DIRECTIVE_FOUND 0
261#define DIRECTIVE_FOUND 1
262
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000263/*
264 * Condition codes. Note that we use c_ prefix not C_ because C_ is
265 * used in nasm.h for the "real" condition codes. At _this_ level,
266 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
267 * ones, so we need a different enum...
268 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700269static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000270 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
271 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000272 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000273};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700274enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000275 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
276 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
H. Peter Anvin476d2862007-10-02 22:04:15 -0700277 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
278 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000279};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700280static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000281 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
282 c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
H. Peter Anvince9be342007-09-12 00:22:29 +0000283 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000284};
285
H. Peter Anvin76690a12002-04-30 20:52:49 +0000286/*
287 * Directive names.
288 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000289/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000290static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000291{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000292 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000293}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000294
295/* For TASM compatibility we need to be able to recognise TASM compatible
296 * conditional compilation directives. Using the NASM pre-processor does
297 * not work, so we look for them specifically from the following list and
298 * then jam in the equivalent NASM directive into the input stream.
299 */
300
301#ifndef MAX
302# define MAX(a,b) ( ((a) > (b)) ? (a) : (b))
303#endif
304
H. Peter Anvine2c80182005-01-15 22:15:51 +0000305enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000306 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
307 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
308};
309
H. Peter Anvin476d2862007-10-02 22:04:15 -0700310static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000311 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
312 "ifndef", "include", "local"
313};
314
315static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000316static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000317static int ArgOffset = 8;
318static int LocalOffset = 4;
319
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000320static Context *cstk;
321static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000322static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000323
H. Peter Anvine2c80182005-01-15 22:15:51 +0000324static efunc _error; /* Pointer to client-provided error reporting function */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000325static evalfunc evaluate;
326
H. Peter Anvine2c80182005-01-15 22:15:51 +0000327static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000328
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700329static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000330
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000331static Line *predef = NULL;
332
333static ListGen *list;
334
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000335/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000336 * The current set of multi-line macros we have defined.
337 */
H. Peter Anvin97a23472007-09-16 17:57:25 -0700338static struct hash_table *mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000339
340/*
341 * The current set of single-line macros we have defined.
342 */
H. Peter Anvin97a23472007-09-16 17:57:25 -0700343static struct hash_table *smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000344
345/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000346 * The multi-line macro we are currently defining, or the %rep
347 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000348 */
349static MMacro *defining;
350
351/*
352 * The number of macro parameters to allocate space for at a time.
353 */
354#define PARAM_DELTA 16
355
356/*
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000357 * The standard macro set: defined as `static char *stdmac[]'. Also
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000358 * gives our position in the macro set, when we're processing it.
359 */
360#include "macros.c"
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000361static const char **stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000362
363/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000364 * The extra standard macros that come from the object format, if
365 * any.
366 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000367static const char **extrastdmac = NULL;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700368bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000369
370/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000371 * Tokens are allocated in blocks to improve speed
372 */
373#define TOKEN_BLOCKSIZE 4096
374static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000375struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000376 Blocks *next;
377 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000378};
379
380static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000381
382/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000383 * Forward declarations.
384 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000385static Token *expand_mmac_params(Token * tline);
386static Token *expand_smacro(Token * tline);
387static Token *expand_id(Token * tline);
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700388static Context *get_ctx(char *name, bool all_contexts);
Keith Kaniosb7a89542007-04-12 02:40:54 +0000389static void make_tok_num(Token * tok, int32_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000390static void error(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000391static void *new_Block(size_t size);
392static void delete_Blocks(void);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000393static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000394static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000395
396/*
397 * Macros for safe checking of token pointers, avoid *(NULL)
398 */
399#define tok_type_(x,t) ((x) && (x)->type == (t))
400#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
401#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
402#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000403
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000404/* Handle TASM specific directives, which do not contain a % in
405 * front of them. We do it here because I could not find any other
406 * place to do it for the moment, and it is a hack (ideally it would
407 * be nice to be able to use the NASM pre-processor to do it).
408 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000409static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000410{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000411 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000412 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000413
414 /* Skip whitespace */
415 while (isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000416 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000417
418 /* Binary search for the directive name */
419 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000420 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000421 len = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000422 while (!isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000423 len++;
424 if (len) {
425 oldchar = p[len];
426 p[len] = 0;
427 while (j - i > 1) {
428 k = (j + i) / 2;
429 m = nasm_stricmp(p, tasm_directives[k]);
430 if (m == 0) {
431 /* We have found a directive, so jam a % in front of it
432 * so that NASM will then recognise it as one if it's own.
433 */
434 p[len] = oldchar;
435 len = strlen(p);
436 oldline = line;
437 line = nasm_malloc(len + 2);
438 line[0] = '%';
439 if (k == TM_IFDIFI) {
440 /* NASM does not recognise IFDIFI, so we convert it to
441 * %ifdef BOGUS. This is not used in NASM comaptible
442 * code, but does need to parse for the TASM macro
443 * package.
444 */
445 strcpy(line + 1, "ifdef BOGUS");
446 } else {
447 memcpy(line + 1, p, len + 1);
448 }
449 nasm_free(oldline);
450 return line;
451 } else if (m < 0) {
452 j = k;
453 } else
454 i = k;
455 }
456 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000457 }
458 return line;
459}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000460
H. Peter Anvin76690a12002-04-30 20:52:49 +0000461/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000462 * The pre-preprocessing stage... This function translates line
463 * number indications as they emerge from GNU cpp (`# lineno "file"
464 * flags') into NASM preprocessor line number indications (`%line
465 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000466 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000467static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000468{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000469 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000470 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000471
H. Peter Anvine2c80182005-01-15 22:15:51 +0000472 if (line[0] == '#' && line[1] == ' ') {
473 oldline = line;
474 fname = oldline + 2;
475 lineno = atoi(fname);
476 fname += strspn(fname, "0123456789 ");
477 if (*fname == '"')
478 fname++;
479 fnlen = strcspn(fname, "\"");
480 line = nasm_malloc(20 + fnlen);
481 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
482 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000483 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000484 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000485 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000486 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000487}
488
489/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000490 * Free a linked list of tokens.
491 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000492static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000493{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000494 while (list) {
495 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000496 }
497}
498
499/*
500 * Free a linked list of lines.
501 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000502static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000503{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000504 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000505 while (list) {
506 l = list;
507 list = list->next;
508 free_tlist(l->first);
509 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000510 }
511}
512
513/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000514 * Free an MMacro
515 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000516static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000517{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000518 nasm_free(m->name);
519 free_tlist(m->dlist);
520 nasm_free(m->defaults);
521 free_llist(m->expansion);
522 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000523}
524
525/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700526 * Free all currently defined macros, and free the hash tables
527 */
528static void free_macros(void)
529{
530 struct hash_tbl_node *it;
531 const char *key;
532 SMacro *s;
533 MMacro *m;
534
535 it = NULL;
536 while ((s = hash_iterate(smacros, &it, &key)) != NULL) {
537 nasm_free((void *)key);
538 while (s) {
539 SMacro *ns = s->next;
540 nasm_free(s->name);
541 free_tlist(s->expansion);
542 nasm_free(s);
543 s = ns;
544 }
545 }
546 hash_free(smacros);
547
548 it = NULL;
549 while ((m = hash_iterate(mmacros, &it, &key)) != NULL) {
550 nasm_free((void *)key);
551 while (m) {
552 MMacro *nm = m->next;
553 free_mmacro(m);
554 m = nm;
555 }
556 }
557 hash_free(mmacros);
558}
559
560/*
561 * Initialize the hash tables
562 */
563static void init_macros(void)
564{
565 smacros = hash_init();
566 mmacros = hash_init();
567}
568
569/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000570 * Pop the context stack.
571 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000572static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000573{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000574 Context *c = cstk;
575 SMacro *smac, *s;
576
577 cstk = cstk->next;
578 smac = c->localmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000579 while (smac) {
580 s = smac;
581 smac = smac->next;
582 nasm_free(s->name);
583 free_tlist(s->expansion);
584 nasm_free(s);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000585 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000586 nasm_free(c->name);
587 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000588}
589
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000590#define BUF_DELTA 512
591/*
592 * Read a line from the top file in istk, handling multiple CR/LFs
593 * at the end of the line read, and handling spurious ^Zs. Will
594 * return lines from the standard macro set if this has not already
595 * been done.
596 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000597static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000598{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000599 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000600 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000601
H. Peter Anvine2c80182005-01-15 22:15:51 +0000602 if (stdmacpos) {
603 if (*stdmacpos) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000604 char *ret = nasm_strdup(*stdmacpos++);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000605 if (!*stdmacpos && any_extrastdmac) {
606 stdmacpos = extrastdmac;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700607 any_extrastdmac = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000608 return ret;
609 }
610 /*
611 * Nasty hack: here we push the contents of `predef' on
612 * to the top-level expansion stack, since this is the
613 * most convenient way to implement the pre-include and
614 * pre-define features.
615 */
616 if (!*stdmacpos) {
617 Line *pd, *l;
618 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000619
H. Peter Anvine2c80182005-01-15 22:15:51 +0000620 for (pd = predef; pd; pd = pd->next) {
621 head = NULL;
622 tail = &head;
623 for (t = pd->first; t; t = t->next) {
624 *tail = new_Token(NULL, t->type, t->text, 0);
625 tail = &(*tail)->next;
626 }
627 l = nasm_malloc(sizeof(Line));
628 l->next = istk->expansion;
629 l->first = head;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700630 l->finishes = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000631 istk->expansion = l;
632 }
633 }
634 return ret;
635 } else {
636 stdmacpos = NULL;
637 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000638 }
639
640 bufsize = BUF_DELTA;
641 buffer = nasm_malloc(BUF_DELTA);
642 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000643 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000644 while (1) {
645 q = fgets(p, bufsize - (p - buffer), istk->fp);
646 if (!q)
647 break;
648 p += strlen(p);
649 if (p > buffer && p[-1] == '\n') {
650 /* Convert backslash-CRLF line continuation sequences into
651 nothing at all (for DOS and Windows) */
652 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
653 p -= 3;
654 *p = 0;
655 continued_count++;
656 }
657 /* Also convert backslash-LF line continuation sequences into
658 nothing at all (for Unix) */
659 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
660 p -= 2;
661 *p = 0;
662 continued_count++;
663 } else {
664 break;
665 }
666 }
667 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000668 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000669 bufsize += BUF_DELTA;
670 buffer = nasm_realloc(buffer, bufsize);
671 p = buffer + offset; /* prevent stale-pointer problems */
672 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000673 }
674
H. Peter Anvine2c80182005-01-15 22:15:51 +0000675 if (!q && p == buffer) {
676 nasm_free(buffer);
677 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000678 }
679
H. Peter Anvine2c80182005-01-15 22:15:51 +0000680 src_set_linnum(src_get_linnum() + istk->lineinc +
681 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000682
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000683 /*
684 * Play safe: remove CRs as well as LFs, if any of either are
685 * present at the end of the line.
686 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000687 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000688 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000689
690 /*
691 * Handle spurious ^Z, which may be inserted into source files
692 * by some file transfer utilities.
693 */
694 buffer[strcspn(buffer, "\032")] = '\0';
695
H. Peter Anvin734b1882002-04-30 21:01:08 +0000696 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000697
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000698 return buffer;
699}
700
701/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000702 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000703 * don't need to parse the value out of e.g. numeric tokens: we
704 * simply split one string into many.
705 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000706static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000707{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000708 char *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000709 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000710 Token *list = NULL;
711 Token *t, **tail = &list;
712
H. Peter Anvine2c80182005-01-15 22:15:51 +0000713 while (*line) {
714 p = line;
715 if (*p == '%') {
716 p++;
717 if (isdigit(*p) ||
718 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
719 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
720 do {
721 p++;
722 }
723 while (isdigit(*p));
724 type = TOK_PREPROC_ID;
725 } else if (*p == '{') {
726 p++;
727 while (*p && *p != '}') {
728 p[-1] = *p;
729 p++;
730 }
731 p[-1] = '\0';
732 if (*p)
733 p++;
734 type = TOK_PREPROC_ID;
735 } else if (isidchar(*p) ||
736 ((*p == '!' || *p == '%' || *p == '$') &&
737 isidchar(p[1]))) {
738 do {
739 p++;
740 }
741 while (isidchar(*p));
742 type = TOK_PREPROC_ID;
743 } else {
744 type = TOK_OTHER;
745 if (*p == '%')
746 p++;
747 }
748 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
749 type = TOK_ID;
750 p++;
751 while (*p && isidchar(*p))
752 p++;
753 } else if (*p == '\'' || *p == '"') {
754 /*
755 * A string token.
756 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000757 char c = *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000758 p++;
759 type = TOK_STRING;
760 while (*p && *p != c)
761 p++;
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000762
H. Peter Anvine2c80182005-01-15 22:15:51 +0000763 if (*p) {
764 p++;
765 } else {
766 error(ERR_WARNING, "unterminated string");
767 /* Handling unterminated strings by UNV */
768 /* type = -1; */
769 }
770 } else if (isnumstart(*p)) {
771 /*
772 * A number token.
773 */
774 type = TOK_NUMBER;
775 p++;
776 while (*p && isnumchar(*p))
777 p++;
778 } else if (isspace(*p)) {
779 type = TOK_WHITESPACE;
780 p++;
781 while (*p && isspace(*p))
782 p++;
783 /*
784 * Whitespace just before end-of-line is discarded by
785 * pretending it's a comment; whitespace just before a
786 * comment gets lumped into the comment.
787 */
788 if (!*p || *p == ';') {
789 type = TOK_COMMENT;
790 while (*p)
791 p++;
792 }
793 } else if (*p == ';') {
794 type = TOK_COMMENT;
795 while (*p)
796 p++;
797 } else {
798 /*
799 * Anything else is an operator of some kind. We check
800 * for all the double-character operators (>>, <<, //,
801 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000802 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 */
804 type = TOK_OTHER;
805 if ((p[0] == '>' && p[1] == '>') ||
806 (p[0] == '<' && p[1] == '<') ||
807 (p[0] == '/' && p[1] == '/') ||
808 (p[0] == '<' && p[1] == '=') ||
809 (p[0] == '>' && p[1] == '=') ||
810 (p[0] == '=' && p[1] == '=') ||
811 (p[0] == '!' && p[1] == '=') ||
812 (p[0] == '<' && p[1] == '>') ||
813 (p[0] == '&' && p[1] == '&') ||
814 (p[0] == '|' && p[1] == '|') ||
815 (p[0] == '^' && p[1] == '^')) {
816 p++;
817 }
818 p++;
819 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000820
H. Peter Anvine2c80182005-01-15 22:15:51 +0000821 /* Handling unterminated string by UNV */
822 /*if (type == -1)
823 {
824 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
825 t->text[p-line] = *line;
826 tail = &t->next;
827 }
828 else */
829 if (type != TOK_COMMENT) {
830 *tail = t = new_Token(NULL, type, line, p - line);
831 tail = &t->next;
832 }
833 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000834 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000835 return list;
836}
837
H. Peter Anvince616072002-04-30 21:02:23 +0000838/*
839 * this function allocates a new managed block of memory and
840 * returns a pointer to the block. The managed blocks are
841 * deleted only all at once by the delete_Blocks function.
842 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000843static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +0000844{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000845 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000846
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000847 /* first, get to the end of the linked list */
848 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000849 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000850 /* now allocate the requested chunk */
851 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000852
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000853 /* now allocate a new block for the next request */
854 b->next = nasm_malloc(sizeof(Blocks));
855 /* and initialize the contents of the new block */
856 b->next->next = NULL;
857 b->next->chunk = NULL;
858 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000859}
860
861/*
862 * this function deletes all managed blocks of memory
863 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000864static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +0000865{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000866 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +0000867
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000868 /*
869 * keep in mind that the first block, pointed to by blocks
870 * is a static and not dynamically allocated, so we don't
871 * free it.
872 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000873 while (b) {
874 if (b->chunk)
875 nasm_free(b->chunk);
876 a = b;
877 b = b->next;
878 if (a != &blocks)
879 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000880 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000881}
H. Peter Anvin734b1882002-04-30 21:01:08 +0000882
883/*
884 * this function creates a new Token and passes a pointer to it
885 * back to the caller. It sets the type and text elements, and
886 * also the mac and next elements to NULL.
887 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000888static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +0000889{
890 Token *t;
891 int i;
892
H. Peter Anvine2c80182005-01-15 22:15:51 +0000893 if (freeTokens == NULL) {
894 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
895 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
896 freeTokens[i].next = &freeTokens[i + 1];
897 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000898 }
899 t = freeTokens;
900 freeTokens = t->next;
901 t->next = next;
902 t->mac = NULL;
903 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000904 if (type == TOK_WHITESPACE || text == NULL) {
905 t->text = NULL;
906 } else {
907 if (txtlen == 0)
908 txtlen = strlen(text);
909 t->text = nasm_malloc(1 + txtlen);
910 strncpy(t->text, text, txtlen);
911 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +0000912 }
913 return t;
914}
915
H. Peter Anvine2c80182005-01-15 22:15:51 +0000916static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +0000917{
918 Token *next = t->next;
919 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +0000920 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000921 freeTokens = t;
922 return next;
923}
924
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000925/*
926 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000927 * If expand_locals is not zero, identifiers of the form "%$*xxx"
928 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000929 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000930static char *detoken(Token * tlist, int expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000931{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000932 Token *t;
933 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000934 char *line, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000935
936 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000937 for (t = tlist; t; t = t->next) {
938 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000939 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 nasm_free(t->text);
941 if (p)
942 t->text = nasm_strdup(p);
943 else
944 t->text = NULL;
945 }
946 /* Expand local macros here and not during preprocessing */
947 if (expand_locals &&
948 t->type == TOK_PREPROC_ID && t->text &&
949 t->text[0] == '%' && t->text[1] == '$') {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700950 Context *ctx = get_ctx(t->text, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000951 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000952 char buffer[40];
953 char *p, *q = t->text + 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000954
H. Peter Anvine2c80182005-01-15 22:15:51 +0000955 q += strspn(q, "$");
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000956 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000957 p = nasm_strcat(buffer, q);
958 nasm_free(t->text);
959 t->text = p;
960 }
961 }
962 if (t->type == TOK_WHITESPACE) {
963 len++;
964 } else if (t->text) {
965 len += strlen(t->text);
966 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000967 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000968 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000969 for (t = tlist; t; t = t->next) {
970 if (t->type == TOK_WHITESPACE) {
971 *p = ' ';
972 p++;
973 *p = '\0';
974 } else if (t->text) {
975 strcpy(p, t->text);
976 p += strlen(p);
977 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000978 }
979 *p = '\0';
980 return line;
981}
982
983/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000984 * A scanner, suitable for use by the expression evaluator, which
985 * operates on a line of Tokens. Expects a pointer to a pointer to
986 * the first token in the line to be passed in as its private_data
987 * field.
988 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000989static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000990{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000991 Token **tlineptr = private_data;
992 Token *tline;
993
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 do {
995 tline = *tlineptr;
996 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000997 }
998 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001000
1001 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001003
1004 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001006 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001008
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 if (tline->type == TOK_ID) {
1010 tokval->t_charptr = tline->text;
1011 if (tline->text[0] == '$') {
1012 tokval->t_charptr++;
1013 return tokval->t_type = TOKEN_ID;
1014 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001015
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 /*
1017 * This is the only special case we actually need to worry
1018 * about in this restricted context.
1019 */
1020 if (!nasm_stricmp(tline->text, "seg"))
1021 return tokval->t_type = TOKEN_SEG;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001022
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 return tokval->t_type = TOKEN_ID;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001024 }
1025
H. Peter Anvine2c80182005-01-15 22:15:51 +00001026 if (tline->type == TOK_NUMBER) {
H. Peter Anvin70055962007-10-11 00:05:31 -07001027 bool rn_error;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001028
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 tokval->t_integer = readnum(tline->text, &rn_error);
1030 if (rn_error)
1031 return tokval->t_type = TOKEN_ERRNUM;
1032 tokval->t_charptr = NULL;
1033 return tokval->t_type = TOKEN_NUM;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001034 }
1035
H. Peter Anvine2c80182005-01-15 22:15:51 +00001036 if (tline->type == TOK_STRING) {
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001037 bool rn_warn;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001038 char q, *r;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001039 int l;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001040
H. Peter Anvine2c80182005-01-15 22:15:51 +00001041 r = tline->text;
1042 q = *r++;
1043 l = strlen(r);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001044
H. Peter Anvine2c80182005-01-15 22:15:51 +00001045 if (l == 0 || r[l - 1] != q)
1046 return tokval->t_type = TOKEN_ERRNUM;
1047 tokval->t_integer = readstrnum(r, l - 1, &rn_warn);
1048 if (rn_warn)
1049 error(ERR_WARNING | ERR_PASS1, "character constant too long");
1050 tokval->t_charptr = NULL;
1051 return tokval->t_type = TOKEN_NUM;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001052 }
1053
H. Peter Anvine2c80182005-01-15 22:15:51 +00001054 if (tline->type == TOK_OTHER) {
1055 if (!strcmp(tline->text, "<<"))
1056 return tokval->t_type = TOKEN_SHL;
1057 if (!strcmp(tline->text, ">>"))
1058 return tokval->t_type = TOKEN_SHR;
1059 if (!strcmp(tline->text, "//"))
1060 return tokval->t_type = TOKEN_SDIV;
1061 if (!strcmp(tline->text, "%%"))
1062 return tokval->t_type = TOKEN_SMOD;
1063 if (!strcmp(tline->text, "=="))
1064 return tokval->t_type = TOKEN_EQ;
1065 if (!strcmp(tline->text, "<>"))
1066 return tokval->t_type = TOKEN_NE;
1067 if (!strcmp(tline->text, "!="))
1068 return tokval->t_type = TOKEN_NE;
1069 if (!strcmp(tline->text, "<="))
1070 return tokval->t_type = TOKEN_LE;
1071 if (!strcmp(tline->text, ">="))
1072 return tokval->t_type = TOKEN_GE;
1073 if (!strcmp(tline->text, "&&"))
1074 return tokval->t_type = TOKEN_DBL_AND;
1075 if (!strcmp(tline->text, "^^"))
1076 return tokval->t_type = TOKEN_DBL_XOR;
1077 if (!strcmp(tline->text, "||"))
1078 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001079 }
1080
1081 /*
1082 * We have no other options: just return the first character of
1083 * the token text.
1084 */
1085 return tokval->t_type = tline->text[0];
1086}
1087
1088/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001089 * Compare a string to the name of an existing macro; this is a
1090 * simple wrapper which calls either strcmp or nasm_stricmp
1091 * depending on the value of the `casesense' parameter.
1092 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001093static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001094{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001095 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001096}
1097
1098/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001099 * Return the Context structure associated with a %$ token. Return
1100 * NULL, having _already_ reported an error condition, if the
1101 * context stack isn't deep enough for the supplied number of $
1102 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001103 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001104 * also scanned for such smacro, until it is found; if not -
1105 * only the context that directly results from the number of $'s
1106 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001107 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001108static Context *get_ctx(char *name, bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001109{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001110 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001111 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001112 int i;
1113
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001114 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001115 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001116
H. Peter Anvine2c80182005-01-15 22:15:51 +00001117 if (!cstk) {
1118 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1119 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001120 }
1121
H. Peter Anvine2c80182005-01-15 22:15:51 +00001122 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1123 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001124/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001125 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001126 if (!ctx) {
1127 error(ERR_NONFATAL, "`%s': context stack is only"
1128 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1129 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001130 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001131 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001133
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 do {
1135 /* Search for this smacro in found context */
1136 m = ctx->localmac;
1137 while (m) {
1138 if (!mstrcmp(m->name, name, m->casesense))
1139 return ctx;
1140 m = m->next;
1141 }
1142 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001143 }
1144 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001145 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001146}
1147
1148/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001149 * Open an include file. This routine must always return a valid
1150 * file pointer if it returns - it's responsible for throwing an
1151 * ERR_FATAL and bombing out completely if not. It should also try
1152 * the include path one by one until it finds the file or reaches
1153 * the end of the path.
1154 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001155static FILE *inc_fopen(char *file)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001156{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001157 FILE *fp;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001158 char *prefix = "", *combine;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001159 IncPath *ip = ipath;
H. Peter Anvin620515a2002-04-30 20:57:38 +00001160 static int namelen = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001161 int len = strlen(file);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001162
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163 while (1) {
1164 combine = nasm_malloc(strlen(prefix) + len + 1);
1165 strcpy(combine, prefix);
1166 strcat(combine, file);
1167 fp = fopen(combine, "r");
1168 if (pass == 0 && fp) {
1169 namelen += strlen(combine) + 1;
1170 if (namelen > 62) {
1171 printf(" \\\n ");
1172 namelen = 2;
1173 }
1174 printf(" %s", combine);
1175 }
1176 nasm_free(combine);
1177 if (fp)
1178 return fp;
1179 if (!ip)
1180 break;
1181 prefix = ip->path;
1182 ip = ip->next;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001183
1184 if (!prefix) {
1185 /* -MG given and file not found */
1186 if (pass == 0) {
1187 namelen += strlen(file) + 1;
1188 if (namelen > 62) {
1189 printf(" \\\n ");
1190 namelen = 2;
1191 }
1192 printf(" %s", file);
1193 }
1194 return NULL;
1195 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001196 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001197
H. Peter Anvin734b1882002-04-30 21:01:08 +00001198 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001199 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001200}
1201
1202/*
H. Peter Anvin97a23472007-09-16 17:57:25 -07001203 * Search for a key in the hash index; adding it if necessary
1204 * (in which case we initialize the data pointer to NULL.)
1205 */
1206static void **
1207hash_findi_add(struct hash_table *hash, const char *str)
1208{
1209 struct hash_insert hi;
1210 void **r;
1211 char *strx;
1212
1213 r = hash_findi(hash, str, &hi);
1214 if (r)
1215 return r;
1216
1217 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
1218 return hash_add(&hi, strx, NULL);
1219}
1220
1221/*
1222 * Like hash_findi, but returns the data element rather than a pointer
1223 * to it. Used only when not adding a new element, hence no third
1224 * argument.
1225 */
1226static void *
1227hash_findix(struct hash_table *hash, const char *str)
1228{
1229 void **p;
1230
1231 p = hash_findi(hash, str, NULL);
1232 return p ? *p : NULL;
1233}
1234
1235/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001236 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001237 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001238 * return true if _any_ single-line macro of that name is defined.
1239 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001240 * `nparam' or no parameters is defined.
1241 *
1242 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001243 * defined, or nparam is -1, the address of the definition structure
1244 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001245 * is NULL, no action will be taken regarding its contents, and no
1246 * error will occur.
1247 *
1248 * Note that this is also called with nparam zero to resolve
1249 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001250 *
1251 * If you already know which context macro belongs to, you can pass
1252 * the context pointer as first parameter; if you won't but name begins
1253 * with %$ the context will be automatically computed. If all_contexts
1254 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001255 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001256static bool
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001257smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001258 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001259{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001260 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001261
H. Peter Anvin97a23472007-09-16 17:57:25 -07001262 if (ctx) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 m = ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001264 } else if (name[0] == '%' && name[1] == '$') {
1265 if (cstk)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001266 ctx = get_ctx(name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001267 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001268 return false; /* got to return _something_ */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001269 m = ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001270 } else {
1271 m = (SMacro *) hash_findix(smacros, name);
1272 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001273
H. Peter Anvine2c80182005-01-15 22:15:51 +00001274 while (m) {
1275 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
1276 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam)) {
1277 if (defn) {
1278 if (nparam == m->nparam || nparam == -1)
1279 *defn = m;
1280 else
1281 *defn = NULL;
1282 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001283 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001284 }
1285 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001286 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001287
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001288 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001289}
1290
1291/*
1292 * Count and mark off the parameters in a multi-line macro call.
1293 * This is called both from within the multi-line macro expansion
1294 * code, and also to mark off the default parameters when provided
1295 * in a %macro definition line.
1296 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001297static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001298{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001299 int paramsize, brace;
1300
1301 *nparam = paramsize = 0;
1302 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 while (t) {
1304 if (*nparam >= paramsize) {
1305 paramsize += PARAM_DELTA;
1306 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1307 }
1308 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001309 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001311 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001312 (*params)[(*nparam)++] = t;
1313 while (tok_isnt_(t, brace ? "}" : ","))
1314 t = t->next;
1315 if (t) { /* got a comma/brace */
1316 t = t->next;
1317 if (brace) {
1318 /*
1319 * Now we've found the closing brace, look further
1320 * for the comma.
1321 */
1322 skip_white_(t);
1323 if (tok_isnt_(t, ",")) {
1324 error(ERR_NONFATAL,
1325 "braces do not enclose all of macro parameter");
1326 while (tok_isnt_(t, ","))
1327 t = t->next;
1328 }
1329 if (t)
1330 t = t->next; /* eat the comma */
1331 }
1332 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001333 }
1334}
1335
1336/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001337 * Determine whether one of the various `if' conditions is true or
1338 * not.
1339 *
1340 * We must free the tline we get passed.
1341 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001342static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001343{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001344 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001345 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001346 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001347 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001348 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001349 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001350
1351 origline = tline;
1352
H. Peter Anvine2c80182005-01-15 22:15:51 +00001353 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001354 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001355 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001356 while (cstk && tline) {
1357 skip_white_(tline);
1358 if (!tline || tline->type != TOK_ID) {
1359 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001360 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361 free_tlist(origline);
1362 return -1;
1363 }
1364 if (!nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001365 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001366 tline = tline->next;
1367 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001368 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001369
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001370 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001371 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001372 while (tline) {
1373 skip_white_(tline);
1374 if (!tline || (tline->type != TOK_ID &&
1375 (tline->type != TOK_PREPROC_ID ||
1376 tline->text[1] != '$'))) {
1377 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001378 "`%s' expects macro identifiers", pp_directives[ct]);
1379 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001381 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001382 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001383 tline = tline->next;
1384 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001385 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001386
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001387 case PPC_IFIDN:
1388 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001389 tline = expand_smacro(tline);
1390 t = tt = tline;
1391 while (tok_isnt_(tt, ","))
1392 tt = tt->next;
1393 if (!tt) {
1394 error(ERR_NONFATAL,
1395 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001396 pp_directives[ct]);
1397 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001398 }
1399 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001400 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001401 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1402 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1403 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001404 pp_directives[ct]);
1405 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001406 }
1407 if (t->type == TOK_WHITESPACE) {
1408 t = t->next;
1409 continue;
1410 }
1411 if (tt->type == TOK_WHITESPACE) {
1412 tt = tt->next;
1413 continue;
1414 }
1415 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001416 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001417 break;
1418 }
1419 /* Unify surrounding quotes for strings */
1420 if (t->type == TOK_STRING) {
1421 tt->text[0] = t->text[0];
1422 tt->text[strlen(tt->text) - 1] = t->text[0];
1423 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001424 if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001425 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001426 break;
1427 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001428
H. Peter Anvine2c80182005-01-15 22:15:51 +00001429 t = t->next;
1430 tt = tt->next;
1431 }
1432 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001433 j = false; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001434 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001435
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001436 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001438 bool found = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001439 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001440
H. Peter Anvine2c80182005-01-15 22:15:51 +00001441 tline = tline->next;
1442 skip_white_(tline);
1443 tline = expand_id(tline);
1444 if (!tok_type_(tline, TOK_ID)) {
1445 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001446 "`%s' expects a macro name", pp_directives[ct]);
1447 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001448 }
1449 searching.name = nasm_strdup(tline->text);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001450 searching.casesense = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001451 searching.plus = false;
1452 searching.nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001453 searching.in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001454 searching.rep_nest = NULL;
1455 searching.nparam_min = 0;
1456 searching.nparam_max = INT_MAX;
1457 tline = expand_smacro(tline->next);
1458 skip_white_(tline);
1459 if (!tline) {
1460 } else if (!tok_type_(tline, TOK_NUMBER)) {
1461 error(ERR_NONFATAL,
1462 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001463 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001464 } else {
1465 searching.nparam_min = searching.nparam_max =
1466 readnum(tline->text, &j);
1467 if (j)
1468 error(ERR_NONFATAL,
1469 "unable to parse parameter count `%s'",
1470 tline->text);
1471 }
1472 if (tline && tok_is_(tline->next, "-")) {
1473 tline = tline->next->next;
1474 if (tok_is_(tline, "*"))
1475 searching.nparam_max = INT_MAX;
1476 else if (!tok_type_(tline, TOK_NUMBER))
1477 error(ERR_NONFATAL,
1478 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001479 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001480 else {
1481 searching.nparam_max = readnum(tline->text, &j);
1482 if (j)
1483 error(ERR_NONFATAL,
1484 "unable to parse parameter count `%s'",
1485 tline->text);
1486 if (searching.nparam_min > searching.nparam_max)
1487 error(ERR_NONFATAL,
1488 "minimum parameter count exceeds maximum");
1489 }
1490 }
1491 if (tline && tok_is_(tline->next, "+")) {
1492 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001493 searching.plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001494 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07001495 mmac = (MMacro *) hash_findix(mmacros, searching.name);
1496 while (mmac) {
1497 if (!strcmp(mmac->name, searching.name) &&
1498 (mmac->nparam_min <= searching.nparam_max
1499 || searching.plus)
1500 && (searching.nparam_min <= mmac->nparam_max
1501 || mmac->plus)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001502 found = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001503 break;
1504 }
1505 mmac = mmac->next;
1506 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001507 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001508 j = found;
1509 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001510 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001511
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001512 case PPC_IFID:
1513 needtype = TOK_ID;
1514 goto iftype;
1515 case PPC_IFNUM:
1516 needtype = TOK_NUMBER;
1517 goto iftype;
1518 case PPC_IFSTR:
1519 needtype = TOK_STRING;
1520 goto iftype;
1521
1522 iftype:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001523 tline = expand_smacro(tline);
1524 t = tline;
1525 while (tok_type_(t, TOK_WHITESPACE))
1526 t = t->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001527 j = false; /* placate optimiser */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001528 if (t)
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001529 j = t->type == needtype;
1530 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001531
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001532 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001533 t = tline = expand_smacro(tline);
1534 tptr = &t;
1535 tokval.t_type = TOKEN_INVALID;
1536 evalresult = evaluate(ppscan, tptr, &tokval,
1537 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001538 if (!evalresult)
1539 return -1;
1540 if (tokval.t_type)
1541 error(ERR_WARNING,
1542 "trailing garbage after expression ignored");
1543 if (!is_simple(evalresult)) {
1544 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001545 "non-constant value given to `%s'", pp_directives[ct]);
1546 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001547 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001548 j = reloc_value(evalresult) != 0;
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001549 return j;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001550
H. Peter Anvine2c80182005-01-15 22:15:51 +00001551 default:
1552 error(ERR_FATAL,
1553 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001554 pp_directives[ct]);
1555 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001556 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001557
1558 free_tlist(origline);
1559 return j ^ PP_NEGATIVE(ct);
1560
1561fail:
1562 free_tlist(origline);
1563 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001564}
1565
1566/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001567 * Expand macros in a string. Used in %error and %include directives.
Keith Kaniosb7a89542007-04-12 02:40:54 +00001568 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001569 * The returned variable should ALWAYS be freed after usage.
1570 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001571void expand_macros_in_string(char **p)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001572{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001573 Token *line = tokenize(*p);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001574 line = expand_smacro(line);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001575 *p = detoken(line, false);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001576}
1577
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001578/*
1579 * Common code for defining an smacro
1580 */
1581static bool define_smacro(Context *ctx, char *mname, bool casesense,
1582 int nparam, Token *expansion)
1583{
1584 SMacro *smac, **smhead;
1585
1586 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1587 if (!smac) {
1588 error(ERR_WARNING,
1589 "single-line macro `%s' defined both with and"
1590 " without parameters", mname);
1591
1592 /* Some instances of the old code considered this a failure,
1593 some others didn't. What is the right thing to do here? */
1594 free_tlist(expansion);
1595 return false; /* Failure */
1596 } else {
1597 /*
1598 * We're redefining, so we have to take over an
1599 * existing SMacro structure. This means freeing
1600 * what was already in it.
1601 */
1602 nasm_free(smac->name);
1603 free_tlist(smac->expansion);
1604 }
1605 } else {
1606 if (!ctx)
1607 smhead = (SMacro **) hash_findi_add(smacros, mname);
1608 else
1609 smhead = &ctx->localmac;
1610
1611 smac = nasm_malloc(sizeof(SMacro));
1612 smac->next = *smhead;
1613 *smhead = smac;
1614 }
1615 smac->name = nasm_strdup(mname);
1616 smac->casesense = casesense;
1617 smac->nparam = nparam;
1618 smac->expansion = expansion;
1619 smac->in_progress = false;
1620 return true; /* Success */
1621}
1622
1623/*
1624 * Undefine an smacro
1625 */
1626static void undef_smacro(Context *ctx, const char *mname)
1627{
1628 SMacro **smhead, *s, **sp;
1629
1630 if (!ctx)
1631 smhead = (SMacro **) hash_findi(smacros, mname, NULL);
1632 else
1633 smhead = &ctx->localmac;
1634
1635 if (smhead) {
1636 /*
1637 * We now have a macro name... go hunt for it.
1638 */
1639 sp = smhead;
1640 while ((s = *sp) != NULL) {
1641 if (!mstrcmp(s->name, mname, s->casesense)) {
1642 *sp = s->next;
1643 nasm_free(s->name);
1644 free_tlist(s->expansion);
1645 nasm_free(s);
1646 } else {
1647 sp = &s->next;
1648 }
1649 }
1650 }
1651}
1652
Ed Beroset3ab3f412002-06-11 03:31:49 +00001653/**
1654 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001655 * Find out if a line contains a preprocessor directive, and deal
1656 * with it if so.
1657 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001658 * If a directive _is_ found, it is the responsibility of this routine
1659 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001660 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001661 * @param tline a pointer to the current tokeninzed line linked list
1662 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001663 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001664 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001665static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001666{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001667 enum preproc_token i;
1668 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07001669 bool err;
1670 int nparam;
1671 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001672 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001673 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001674 int offset;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001675 char *p, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001676 Include *inc;
1677 Context *ctx;
1678 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001679 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001680 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1681 Line *l;
1682 struct tokenval tokval;
1683 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001685 int64_t count;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001686
1687 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001688
H. Peter Anvineba20a72002-04-30 20:53:55 +00001689 skip_white_(tline);
1690 if (!tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001691 (tline->text[1] == '%' || tline->text[1] == '$'
1692 || tline->text[1] == '!'))
1693 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001694
H. Peter Anvin4169a472007-09-12 01:29:43 +00001695 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001696
1697 /*
1698 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001699 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001700 * we should ignore all directives except for condition
1701 * directives.
1702 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001703 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001704 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1705 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001706 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001707
1708 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001709 * If we're defining a macro or reading a %rep block, we should
1710 * ignore all directives except for %macro/%imacro (which
1711 * generate an error), %endm/%endmacro, and (only if we're in a
H. Peter Anvineba20a72002-04-30 20:53:55 +00001712 * %rep block) %endrep. If we're in a %rep block, another %rep
1713 * causes an error, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001714 */
1715 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001716 i != PP_ENDMACRO && i != PP_ENDM &&
1717 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1718 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001719 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001720
H. Peter Anvin4169a472007-09-12 01:29:43 +00001721 switch (i) {
1722 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001723 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1724 tline->text);
1725 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001726
H. Peter Anvine2c80182005-01-15 22:15:51 +00001727 case PP_STACKSIZE:
1728 /* Directive to tell NASM what the default stack size is. The
1729 * default is for a 16-bit stack, and this can be overriden with
1730 * %stacksize large.
1731 * the following form:
1732 *
1733 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1734 */
1735 tline = tline->next;
1736 if (tline && tline->type == TOK_WHITESPACE)
1737 tline = tline->next;
1738 if (!tline || tline->type != TOK_ID) {
1739 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1740 free_tlist(origline);
1741 return DIRECTIVE_FOUND;
1742 }
1743 if (nasm_stricmp(tline->text, "flat") == 0) {
1744 /* All subsequent ARG directives are for a 32-bit stack */
1745 StackSize = 4;
1746 StackPointer = "ebp";
1747 ArgOffset = 8;
1748 LocalOffset = 4;
1749 } else if (nasm_stricmp(tline->text, "large") == 0) {
1750 /* All subsequent ARG directives are for a 16-bit stack,
1751 * far function call.
1752 */
1753 StackSize = 2;
1754 StackPointer = "bp";
1755 ArgOffset = 4;
1756 LocalOffset = 2;
1757 } else if (nasm_stricmp(tline->text, "small") == 0) {
1758 /* All subsequent ARG directives are for a 16-bit stack,
1759 * far function call. We don't support near functions.
1760 */
1761 StackSize = 2;
1762 StackPointer = "bp";
1763 ArgOffset = 6;
1764 LocalOffset = 2;
1765 } else {
1766 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1767 free_tlist(origline);
1768 return DIRECTIVE_FOUND;
1769 }
1770 free_tlist(origline);
1771 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001772
H. Peter Anvine2c80182005-01-15 22:15:51 +00001773 case PP_ARG:
1774 /* TASM like ARG directive to define arguments to functions, in
1775 * the following form:
1776 *
1777 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1778 */
1779 offset = ArgOffset;
1780 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001781 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001783
H. Peter Anvine2c80182005-01-15 22:15:51 +00001784 /* Find the argument name */
1785 tline = tline->next;
1786 if (tline && tline->type == TOK_WHITESPACE)
1787 tline = tline->next;
1788 if (!tline || tline->type != TOK_ID) {
1789 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1790 free_tlist(origline);
1791 return DIRECTIVE_FOUND;
1792 }
1793 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +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 `%%arg' 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, "`%%arg' missing size type parameter");
1807 free_tlist(origline);
1808 return DIRECTIVE_FOUND;
1809 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001810
H. Peter Anvine2c80182005-01-15 22:15:51 +00001811 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001812 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001813 tt = expand_smacro(tt);
1814 if (nasm_stricmp(tt->text, "byte") == 0) {
1815 size = MAX(StackSize, 1);
1816 } else if (nasm_stricmp(tt->text, "word") == 0) {
1817 size = MAX(StackSize, 2);
1818 } else if (nasm_stricmp(tt->text, "dword") == 0) {
1819 size = MAX(StackSize, 4);
1820 } else if (nasm_stricmp(tt->text, "qword") == 0) {
1821 size = MAX(StackSize, 8);
1822 } else if (nasm_stricmp(tt->text, "tword") == 0) {
1823 size = MAX(StackSize, 10);
1824 } else {
1825 error(ERR_NONFATAL,
1826 "Invalid size type for `%%arg' missing directive");
1827 free_tlist(tt);
1828 free_tlist(origline);
1829 return DIRECTIVE_FOUND;
1830 }
1831 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001832
H. Peter Anvine2c80182005-01-15 22:15:51 +00001833 /* Now define the macro for the argument */
1834 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1835 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001836 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001837 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001838
H. Peter Anvine2c80182005-01-15 22:15:51 +00001839 /* Move to the next argument in the list */
1840 tline = tline->next;
1841 if (tline && tline->type == TOK_WHITESPACE)
1842 tline = tline->next;
1843 }
1844 while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1845 free_tlist(origline);
1846 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001847
H. Peter Anvine2c80182005-01-15 22:15:51 +00001848 case PP_LOCAL:
1849 /* TASM like LOCAL directive to define local variables for a
1850 * function, in the following form:
1851 *
1852 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1853 *
1854 * The '= LocalSize' at the end is ignored by NASM, but is
1855 * required by TASM to define the local parameter size (and used
1856 * by the TASM macro package).
1857 */
1858 offset = LocalOffset;
1859 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001860 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001861 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001862
H. Peter Anvine2c80182005-01-15 22:15:51 +00001863 /* Find the argument name */
1864 tline = tline->next;
1865 if (tline && tline->type == TOK_WHITESPACE)
1866 tline = tline->next;
1867 if (!tline || tline->type != TOK_ID) {
1868 error(ERR_NONFATAL,
1869 "`%%local' missing argument parameter");
1870 free_tlist(origline);
1871 return DIRECTIVE_FOUND;
1872 }
1873 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001874
H. Peter Anvine2c80182005-01-15 22:15:51 +00001875 /* Find the argument size type */
1876 tline = tline->next;
1877 if (!tline || tline->type != TOK_OTHER
1878 || tline->text[0] != ':') {
1879 error(ERR_NONFATAL,
1880 "Syntax error processing `%%local' directive");
1881 free_tlist(origline);
1882 return DIRECTIVE_FOUND;
1883 }
1884 tline = tline->next;
1885 if (!tline || tline->type != TOK_ID) {
1886 error(ERR_NONFATAL,
1887 "`%%local' missing size type parameter");
1888 free_tlist(origline);
1889 return DIRECTIVE_FOUND;
1890 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001891
H. Peter Anvine2c80182005-01-15 22:15:51 +00001892 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001893 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001894 tt = expand_smacro(tt);
1895 if (nasm_stricmp(tt->text, "byte") == 0) {
1896 size = MAX(StackSize, 1);
1897 } else if (nasm_stricmp(tt->text, "word") == 0) {
1898 size = MAX(StackSize, 2);
1899 } else if (nasm_stricmp(tt->text, "dword") == 0) {
1900 size = MAX(StackSize, 4);
1901 } else if (nasm_stricmp(tt->text, "qword") == 0) {
1902 size = MAX(StackSize, 8);
1903 } else if (nasm_stricmp(tt->text, "tword") == 0) {
1904 size = MAX(StackSize, 10);
1905 } else {
1906 error(ERR_NONFATAL,
1907 "Invalid size type for `%%local' missing directive");
1908 free_tlist(tt);
1909 free_tlist(origline);
1910 return DIRECTIVE_FOUND;
1911 }
1912 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001913
H. Peter Anvine2c80182005-01-15 22:15:51 +00001914 /* Now define the macro for the argument */
1915 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
1916 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001917 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001918 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001919
H. Peter Anvine2c80182005-01-15 22:15:51 +00001920 /* Now define the assign to setup the enter_c macro correctly */
1921 snprintf(directive, sizeof(directive),
1922 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001923 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001924
H. Peter Anvine2c80182005-01-15 22:15:51 +00001925 /* Move to the next argument in the list */
1926 tline = tline->next;
1927 if (tline && tline->type == TOK_WHITESPACE)
1928 tline = tline->next;
1929 }
1930 while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1931 free_tlist(origline);
1932 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001933
H. Peter Anvine2c80182005-01-15 22:15:51 +00001934 case PP_CLEAR:
1935 if (tline->next)
1936 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07001937 free_macros();
1938 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001939 free_tlist(origline);
1940 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001941
H. Peter Anvine2c80182005-01-15 22:15:51 +00001942 case PP_INCLUDE:
1943 tline = tline->next;
1944 skip_white_(tline);
1945 if (!tline || (tline->type != TOK_STRING &&
1946 tline->type != TOK_INTERNAL_STRING)) {
1947 error(ERR_NONFATAL, "`%%include' expects a file name");
1948 free_tlist(origline);
1949 return DIRECTIVE_FOUND; /* but we did _something_ */
1950 }
1951 if (tline->next)
1952 error(ERR_WARNING,
1953 "trailing garbage after `%%include' ignored");
1954 if (tline->type != TOK_INTERNAL_STRING) {
1955 p = tline->text + 1; /* point past the quote to the name */
1956 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
1957 } else
1958 p = tline->text; /* internal_string is easier */
1959 expand_macros_in_string(&p);
1960 inc = nasm_malloc(sizeof(Include));
1961 inc->next = istk;
1962 inc->conds = NULL;
1963 inc->fp = inc_fopen(p);
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001964 if (!inc->fp && pass == 0) {
1965 /* -MG given but file not found */
1966 nasm_free(inc);
1967 } else {
1968 inc->fname = src_set_fname(p);
1969 inc->lineno = src_set_linnum(0);
1970 inc->lineinc = 1;
1971 inc->expansion = NULL;
1972 inc->mstk = NULL;
1973 istk = inc;
1974 list->uplevel(LIST_INCLUDE);
1975 }
1976 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001977 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001978
H. Peter Anvine2c80182005-01-15 22:15:51 +00001979 case PP_PUSH:
1980 tline = tline->next;
1981 skip_white_(tline);
1982 tline = expand_id(tline);
1983 if (!tok_type_(tline, TOK_ID)) {
1984 error(ERR_NONFATAL, "`%%push' expects a context identifier");
1985 free_tlist(origline);
1986 return DIRECTIVE_FOUND; /* but we did _something_ */
1987 }
1988 if (tline->next)
1989 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
1990 ctx = nasm_malloc(sizeof(Context));
1991 ctx->next = cstk;
1992 ctx->localmac = NULL;
1993 ctx->name = nasm_strdup(tline->text);
1994 ctx->number = unique++;
1995 cstk = ctx;
1996 free_tlist(origline);
1997 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001998
H. Peter Anvine2c80182005-01-15 22:15:51 +00001999 case PP_REPL:
2000 tline = tline->next;
2001 skip_white_(tline);
2002 tline = expand_id(tline);
2003 if (!tok_type_(tline, TOK_ID)) {
2004 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2005 free_tlist(origline);
2006 return DIRECTIVE_FOUND; /* but we did _something_ */
2007 }
2008 if (tline->next)
2009 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2010 if (!cstk)
2011 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2012 else {
2013 nasm_free(cstk->name);
2014 cstk->name = nasm_strdup(tline->text);
2015 }
2016 free_tlist(origline);
2017 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002018
H. Peter Anvine2c80182005-01-15 22:15:51 +00002019 case PP_POP:
2020 if (tline->next)
2021 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2022 if (!cstk)
2023 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2024 else
2025 ctx_pop();
2026 free_tlist(origline);
2027 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002028
H. Peter Anvine2c80182005-01-15 22:15:51 +00002029 case PP_ERROR:
2030 tline->next = expand_smacro(tline->next);
2031 tline = tline->next;
2032 skip_white_(tline);
2033 if (tok_type_(tline, TOK_STRING)) {
2034 p = tline->text + 1; /* point past the quote to the name */
2035 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2036 expand_macros_in_string(&p);
2037 error(ERR_NONFATAL, "%s", p);
2038 nasm_free(p);
2039 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002040 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002041 error(ERR_WARNING, "%s", p);
2042 nasm_free(p);
2043 }
2044 free_tlist(origline);
2045 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002046
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002047 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002048 if (istk->conds && !emitting(istk->conds->state))
2049 j = COND_NEVER;
2050 else {
2051 j = if_condition(tline->next, i);
2052 tline->next = NULL; /* it got freed */
2053 free_tlist(origline);
2054 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2055 }
2056 cond = nasm_malloc(sizeof(Cond));
2057 cond->next = istk->conds;
2058 cond->state = j;
2059 istk->conds = cond;
2060 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002061
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002062 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002063 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002064 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002065 if (emitting(istk->conds->state)
2066 || istk->conds->state == COND_NEVER)
2067 istk->conds->state = COND_NEVER;
2068 else {
2069 /*
2070 * IMPORTANT: In the case of %if, we will already have
2071 * called expand_mmac_params(); however, if we're
2072 * processing an %elif we must have been in a
2073 * non-emitting mode, which would have inhibited
2074 * the normal invocation of expand_mmac_params(). Therefore,
2075 * we have to do it explicitly here.
2076 */
2077 j = if_condition(expand_mmac_params(tline->next), i);
2078 tline->next = NULL; /* it got freed */
2079 free_tlist(origline);
2080 istk->conds->state =
2081 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2082 }
2083 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002084
H. Peter Anvine2c80182005-01-15 22:15:51 +00002085 case PP_ELSE:
2086 if (tline->next)
2087 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2088 if (!istk->conds)
2089 error(ERR_FATAL, "`%%else': no matching `%%if'");
2090 if (emitting(istk->conds->state)
2091 || istk->conds->state == COND_NEVER)
2092 istk->conds->state = COND_ELSE_FALSE;
2093 else
2094 istk->conds->state = COND_ELSE_TRUE;
2095 free_tlist(origline);
2096 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002097
H. Peter Anvine2c80182005-01-15 22:15:51 +00002098 case PP_ENDIF:
2099 if (tline->next)
2100 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2101 if (!istk->conds)
2102 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2103 cond = istk->conds;
2104 istk->conds = cond->next;
2105 nasm_free(cond);
2106 free_tlist(origline);
2107 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002108
H. Peter Anvine2c80182005-01-15 22:15:51 +00002109 case PP_MACRO:
2110 case PP_IMACRO:
2111 if (defining)
2112 error(ERR_FATAL,
2113 "`%%%smacro': already defining a macro",
2114 (i == PP_IMACRO ? "i" : ""));
2115 tline = tline->next;
2116 skip_white_(tline);
2117 tline = expand_id(tline);
2118 if (!tok_type_(tline, TOK_ID)) {
2119 error(ERR_NONFATAL,
2120 "`%%%smacro' expects a macro name",
2121 (i == PP_IMACRO ? "i" : ""));
2122 return DIRECTIVE_FOUND;
2123 }
2124 defining = nasm_malloc(sizeof(MMacro));
2125 defining->name = nasm_strdup(tline->text);
2126 defining->casesense = (i == PP_MACRO);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002127 defining->plus = false;
2128 defining->nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002129 defining->in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002130 defining->rep_nest = NULL;
2131 tline = expand_smacro(tline->next);
2132 skip_white_(tline);
2133 if (!tok_type_(tline, TOK_NUMBER)) {
2134 error(ERR_NONFATAL,
2135 "`%%%smacro' expects a parameter count",
2136 (i == PP_IMACRO ? "i" : ""));
2137 defining->nparam_min = defining->nparam_max = 0;
2138 } else {
2139 defining->nparam_min = defining->nparam_max =
H. Peter Anvin70055962007-10-11 00:05:31 -07002140 readnum(tline->text, &err);
2141 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002142 error(ERR_NONFATAL,
2143 "unable to parse parameter count `%s'", tline->text);
2144 }
2145 if (tline && tok_is_(tline->next, "-")) {
2146 tline = tline->next->next;
2147 if (tok_is_(tline, "*"))
2148 defining->nparam_max = INT_MAX;
2149 else if (!tok_type_(tline, TOK_NUMBER))
2150 error(ERR_NONFATAL,
2151 "`%%%smacro' expects a parameter count after `-'",
2152 (i == PP_IMACRO ? "i" : ""));
2153 else {
H. Peter Anvin70055962007-10-11 00:05:31 -07002154 defining->nparam_max = readnum(tline->text, &err);
2155 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002156 error(ERR_NONFATAL,
2157 "unable to parse parameter count `%s'",
2158 tline->text);
2159 if (defining->nparam_min > defining->nparam_max)
2160 error(ERR_NONFATAL,
2161 "minimum parameter count exceeds maximum");
2162 }
2163 }
2164 if (tline && tok_is_(tline->next, "+")) {
2165 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002166 defining->plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002167 }
2168 if (tline && tok_type_(tline->next, TOK_ID) &&
2169 !nasm_stricmp(tline->next->text, ".nolist")) {
2170 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002171 defining->nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002172 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07002173 mmac = (MMacro *) hash_findix(mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002174 while (mmac) {
2175 if (!strcmp(mmac->name, defining->name) &&
2176 (mmac->nparam_min <= defining->nparam_max
2177 || defining->plus)
2178 && (defining->nparam_min <= mmac->nparam_max
2179 || mmac->plus)) {
2180 error(ERR_WARNING,
2181 "redefining multi-line macro `%s'", defining->name);
2182 break;
2183 }
2184 mmac = mmac->next;
2185 }
2186 /*
2187 * Handle default parameters.
2188 */
2189 if (tline && tline->next) {
2190 defining->dlist = tline->next;
2191 tline->next = NULL;
2192 count_mmac_params(defining->dlist, &defining->ndefs,
2193 &defining->defaults);
2194 } else {
2195 defining->dlist = NULL;
2196 defining->defaults = NULL;
2197 }
2198 defining->expansion = NULL;
2199 free_tlist(origline);
2200 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002201
H. Peter Anvine2c80182005-01-15 22:15:51 +00002202 case PP_ENDM:
2203 case PP_ENDMACRO:
2204 if (!defining) {
2205 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2206 return DIRECTIVE_FOUND;
2207 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07002208 mmhead = (MMacro **) hash_findi_add(mmacros, defining->name);
2209 defining->next = *mmhead;
2210 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002211 defining = NULL;
2212 free_tlist(origline);
2213 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002214
H. Peter Anvine2c80182005-01-15 22:15:51 +00002215 case PP_ROTATE:
2216 if (tline->next && tline->next->type == TOK_WHITESPACE)
2217 tline = tline->next;
2218 if (tline->next == NULL) {
2219 free_tlist(origline);
2220 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2221 return DIRECTIVE_FOUND;
2222 }
2223 t = expand_smacro(tline->next);
2224 tline->next = NULL;
2225 free_tlist(origline);
2226 tline = t;
2227 tptr = &t;
2228 tokval.t_type = TOKEN_INVALID;
2229 evalresult =
2230 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2231 free_tlist(tline);
2232 if (!evalresult)
2233 return DIRECTIVE_FOUND;
2234 if (tokval.t_type)
2235 error(ERR_WARNING,
2236 "trailing garbage after expression ignored");
2237 if (!is_simple(evalresult)) {
2238 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2239 return DIRECTIVE_FOUND;
2240 }
2241 mmac = istk->mstk;
2242 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2243 mmac = mmac->next_active;
2244 if (!mmac) {
2245 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2246 } else if (mmac->nparam == 0) {
2247 error(ERR_NONFATAL,
2248 "`%%rotate' invoked within macro without parameters");
2249 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002250 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002251
H. Peter Anvin25a99342007-09-22 17:45:45 -07002252 rotate %= (int)mmac->nparam;
2253 if (rotate < 0)
2254 rotate += mmac->nparam;
2255
2256 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002257 }
2258 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002259
H. Peter Anvine2c80182005-01-15 22:15:51 +00002260 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002261 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002262 do {
2263 tline = tline->next;
2264 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002265
H. Peter Anvine2c80182005-01-15 22:15:51 +00002266 if (tok_type_(tline, TOK_ID) &&
2267 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002268 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002269 do {
2270 tline = tline->next;
2271 } while (tok_type_(tline, TOK_WHITESPACE));
2272 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002273
H. Peter Anvine2c80182005-01-15 22:15:51 +00002274 if (tline) {
2275 t = expand_smacro(tline);
2276 tptr = &t;
2277 tokval.t_type = TOKEN_INVALID;
2278 evalresult =
2279 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2280 if (!evalresult) {
2281 free_tlist(origline);
2282 return DIRECTIVE_FOUND;
2283 }
2284 if (tokval.t_type)
2285 error(ERR_WARNING,
2286 "trailing garbage after expression ignored");
2287 if (!is_simple(evalresult)) {
2288 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2289 return DIRECTIVE_FOUND;
2290 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002291 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002292 } else {
2293 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002294 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002295 }
2296 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002297
H. Peter Anvine2c80182005-01-15 22:15:51 +00002298 tmp_defining = defining;
2299 defining = nasm_malloc(sizeof(MMacro));
2300 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002301 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002302 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002303 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002304 defining->in_progress = count;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002305 defining->nparam_min = defining->nparam_max = 0;
2306 defining->defaults = NULL;
2307 defining->dlist = NULL;
2308 defining->expansion = NULL;
2309 defining->next_active = istk->mstk;
2310 defining->rep_nest = tmp_defining;
2311 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002312
H. Peter Anvine2c80182005-01-15 22:15:51 +00002313 case PP_ENDREP:
2314 if (!defining || defining->name) {
2315 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2316 return DIRECTIVE_FOUND;
2317 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002318
H. Peter Anvine2c80182005-01-15 22:15:51 +00002319 /*
2320 * Now we have a "macro" defined - although it has no name
2321 * and we won't be entering it in the hash tables - we must
2322 * push a macro-end marker for it on to istk->expansion.
2323 * After that, it will take care of propagating itself (a
2324 * macro-end marker line for a macro which is really a %rep
2325 * block will cause the macro to be re-expanded, complete
2326 * with another macro-end marker to ensure the process
2327 * continues) until the whole expansion is forcibly removed
2328 * from istk->expansion by a %exitrep.
2329 */
2330 l = nasm_malloc(sizeof(Line));
2331 l->next = istk->expansion;
2332 l->finishes = defining;
2333 l->first = NULL;
2334 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002335
H. Peter Anvine2c80182005-01-15 22:15:51 +00002336 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002337
H. Peter Anvine2c80182005-01-15 22:15:51 +00002338 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2339 tmp_defining = defining;
2340 defining = defining->rep_nest;
2341 free_tlist(origline);
2342 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002343
H. Peter Anvine2c80182005-01-15 22:15:51 +00002344 case PP_EXITREP:
2345 /*
2346 * We must search along istk->expansion until we hit a
2347 * macro-end marker for a macro with no name. Then we set
2348 * its `in_progress' flag to 0.
2349 */
2350 for (l = istk->expansion; l; l = l->next)
2351 if (l->finishes && !l->finishes->name)
2352 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002353
H. Peter Anvine2c80182005-01-15 22:15:51 +00002354 if (l)
2355 l->finishes->in_progress = 0;
2356 else
2357 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2358 free_tlist(origline);
2359 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002360
H. Peter Anvine2c80182005-01-15 22:15:51 +00002361 case PP_XDEFINE:
2362 case PP_IXDEFINE:
2363 case PP_DEFINE:
2364 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002365 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002366
H. Peter Anvine2c80182005-01-15 22:15:51 +00002367 tline = tline->next;
2368 skip_white_(tline);
2369 tline = expand_id(tline);
2370 if (!tline || (tline->type != TOK_ID &&
2371 (tline->type != TOK_PREPROC_ID ||
2372 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002373 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2374 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002375 free_tlist(origline);
2376 return DIRECTIVE_FOUND;
2377 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002378
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002379 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002380
H. Peter Anvine2c80182005-01-15 22:15:51 +00002381 mname = tline->text;
2382 last = tline;
2383 param_start = tline = tline->next;
2384 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002385
H. Peter Anvine2c80182005-01-15 22:15:51 +00002386 /* Expand the macro definition now for %xdefine and %ixdefine */
2387 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2388 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002389
H. Peter Anvine2c80182005-01-15 22:15:51 +00002390 if (tok_is_(tline, "(")) {
2391 /*
2392 * This macro has parameters.
2393 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002394
H. Peter Anvine2c80182005-01-15 22:15:51 +00002395 tline = tline->next;
2396 while (1) {
2397 skip_white_(tline);
2398 if (!tline) {
2399 error(ERR_NONFATAL, "parameter identifier expected");
2400 free_tlist(origline);
2401 return DIRECTIVE_FOUND;
2402 }
2403 if (tline->type != TOK_ID) {
2404 error(ERR_NONFATAL,
2405 "`%s': parameter identifier expected",
2406 tline->text);
2407 free_tlist(origline);
2408 return DIRECTIVE_FOUND;
2409 }
2410 tline->type = TOK_SMAC_PARAM + nparam++;
2411 tline = tline->next;
2412 skip_white_(tline);
2413 if (tok_is_(tline, ",")) {
2414 tline = tline->next;
2415 continue;
2416 }
2417 if (!tok_is_(tline, ")")) {
2418 error(ERR_NONFATAL,
2419 "`)' expected to terminate macro template");
2420 free_tlist(origline);
2421 return DIRECTIVE_FOUND;
2422 }
2423 break;
2424 }
2425 last = tline;
2426 tline = tline->next;
2427 }
2428 if (tok_type_(tline, TOK_WHITESPACE))
2429 last = tline, tline = tline->next;
2430 macro_start = NULL;
2431 last->next = NULL;
2432 t = tline;
2433 while (t) {
2434 if (t->type == TOK_ID) {
2435 for (tt = param_start; tt; tt = tt->next)
2436 if (tt->type >= TOK_SMAC_PARAM &&
2437 !strcmp(tt->text, t->text))
2438 t->type = tt->type;
2439 }
2440 tt = t->next;
2441 t->next = macro_start;
2442 macro_start = t;
2443 t = tt;
2444 }
2445 /*
2446 * Good. We now have a macro name, a parameter count, and a
2447 * token list (in reverse order) for an expansion. We ought
2448 * to be OK just to create an SMacro, store it, and let
2449 * free_tlist have the rest of the line (which we have
2450 * carefully re-terminated after chopping off the expansion
2451 * from the end).
2452 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002453 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002454 free_tlist(origline);
2455 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002456
H. Peter Anvine2c80182005-01-15 22:15:51 +00002457 case PP_UNDEF:
2458 tline = tline->next;
2459 skip_white_(tline);
2460 tline = expand_id(tline);
2461 if (!tline || (tline->type != TOK_ID &&
2462 (tline->type != TOK_PREPROC_ID ||
2463 tline->text[1] != '$'))) {
2464 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2465 free_tlist(origline);
2466 return DIRECTIVE_FOUND;
2467 }
2468 if (tline->next) {
2469 error(ERR_WARNING,
2470 "trailing garbage after macro name ignored");
2471 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002472
H. Peter Anvine2c80182005-01-15 22:15:51 +00002473 /* Find the context that symbol belongs to */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002474 ctx = get_ctx(tline->text, false);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002475 undef_smacro(ctx, tline->text);
2476 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002477 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002478
H. Peter Anvine2c80182005-01-15 22:15:51 +00002479 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002480 casesense = true;
2481
H. Peter Anvine2c80182005-01-15 22:15:51 +00002482 tline = tline->next;
2483 skip_white_(tline);
2484 tline = expand_id(tline);
2485 if (!tline || (tline->type != TOK_ID &&
2486 (tline->type != TOK_PREPROC_ID ||
2487 tline->text[1] != '$'))) {
2488 error(ERR_NONFATAL,
2489 "`%%strlen' expects a macro identifier as first parameter");
2490 free_tlist(origline);
2491 return DIRECTIVE_FOUND;
2492 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002493 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002494
H. Peter Anvine2c80182005-01-15 22:15:51 +00002495 mname = tline->text;
2496 last = tline;
2497 tline = expand_smacro(tline->next);
2498 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002499
H. Peter Anvine2c80182005-01-15 22:15:51 +00002500 t = tline;
2501 while (tok_type_(t, TOK_WHITESPACE))
2502 t = t->next;
2503 /* t should now point to the string */
2504 if (t->type != TOK_STRING) {
2505 error(ERR_NONFATAL,
2506 "`%%strlen` requires string as second parameter");
2507 free_tlist(tline);
2508 free_tlist(origline);
2509 return DIRECTIVE_FOUND;
2510 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002511
H. Peter Anvine2c80182005-01-15 22:15:51 +00002512 macro_start = nasm_malloc(sizeof(*macro_start));
2513 macro_start->next = NULL;
2514 make_tok_num(macro_start, strlen(t->text) - 2);
2515 macro_start->mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002516
H. Peter Anvine2c80182005-01-15 22:15:51 +00002517 /*
2518 * We now have a macro name, an implicit parameter count of
2519 * zero, and a numeric token to use as an expansion. Create
2520 * and store an SMacro.
2521 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002522 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002523 free_tlist(tline);
2524 free_tlist(origline);
2525 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002526
H. Peter Anvine2c80182005-01-15 22:15:51 +00002527 case PP_SUBSTR:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002528 casesense = true;
2529
H. Peter Anvine2c80182005-01-15 22:15:51 +00002530 tline = tline->next;
2531 skip_white_(tline);
2532 tline = expand_id(tline);
2533 if (!tline || (tline->type != TOK_ID &&
2534 (tline->type != TOK_PREPROC_ID ||
2535 tline->text[1] != '$'))) {
2536 error(ERR_NONFATAL,
2537 "`%%substr' expects a macro identifier as first parameter");
2538 free_tlist(origline);
2539 return DIRECTIVE_FOUND;
2540 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002541 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002542
H. Peter Anvine2c80182005-01-15 22:15:51 +00002543 mname = tline->text;
2544 last = tline;
2545 tline = expand_smacro(tline->next);
2546 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002547
H. Peter Anvine2c80182005-01-15 22:15:51 +00002548 t = tline->next;
2549 while (tok_type_(t, TOK_WHITESPACE))
2550 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002551
H. Peter Anvine2c80182005-01-15 22:15:51 +00002552 /* t should now point to the string */
2553 if (t->type != TOK_STRING) {
2554 error(ERR_NONFATAL,
2555 "`%%substr` requires string as second parameter");
2556 free_tlist(tline);
2557 free_tlist(origline);
2558 return DIRECTIVE_FOUND;
2559 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002560
H. Peter Anvine2c80182005-01-15 22:15:51 +00002561 tt = t->next;
2562 tptr = &tt;
2563 tokval.t_type = TOKEN_INVALID;
2564 evalresult =
2565 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2566 if (!evalresult) {
2567 free_tlist(tline);
2568 free_tlist(origline);
2569 return DIRECTIVE_FOUND;
2570 }
2571 if (!is_simple(evalresult)) {
2572 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2573 free_tlist(tline);
2574 free_tlist(origline);
2575 return DIRECTIVE_FOUND;
2576 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002577
H. Peter Anvine2c80182005-01-15 22:15:51 +00002578 macro_start = nasm_malloc(sizeof(*macro_start));
2579 macro_start->next = NULL;
2580 macro_start->text = nasm_strdup("'''");
2581 if (evalresult->value > 0
2582 && evalresult->value < strlen(t->text) - 1) {
2583 macro_start->text[1] = t->text[evalresult->value];
2584 } else {
2585 macro_start->text[2] = '\0';
2586 }
2587 macro_start->type = TOK_STRING;
2588 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002589
H. Peter Anvine2c80182005-01-15 22:15:51 +00002590 /*
2591 * We now have a macro name, an implicit parameter count of
2592 * zero, and a numeric token to use as an expansion. Create
2593 * and store an SMacro.
2594 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002595 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002596 free_tlist(tline);
2597 free_tlist(origline);
2598 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002599
H. Peter Anvine2c80182005-01-15 22:15:51 +00002600 case PP_ASSIGN:
2601 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002602 casesense = (i == PP_ASSIGN);
2603
H. Peter Anvine2c80182005-01-15 22:15:51 +00002604 tline = tline->next;
2605 skip_white_(tline);
2606 tline = expand_id(tline);
2607 if (!tline || (tline->type != TOK_ID &&
2608 (tline->type != TOK_PREPROC_ID ||
2609 tline->text[1] != '$'))) {
2610 error(ERR_NONFATAL,
2611 "`%%%sassign' expects a macro identifier",
2612 (i == PP_IASSIGN ? "i" : ""));
2613 free_tlist(origline);
2614 return DIRECTIVE_FOUND;
2615 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002616 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002617
H. Peter Anvine2c80182005-01-15 22:15:51 +00002618 mname = tline->text;
2619 last = tline;
2620 tline = expand_smacro(tline->next);
2621 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002622
H. Peter Anvine2c80182005-01-15 22:15:51 +00002623 t = tline;
2624 tptr = &t;
2625 tokval.t_type = TOKEN_INVALID;
2626 evalresult =
2627 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2628 free_tlist(tline);
2629 if (!evalresult) {
2630 free_tlist(origline);
2631 return DIRECTIVE_FOUND;
2632 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002633
H. Peter Anvine2c80182005-01-15 22:15:51 +00002634 if (tokval.t_type)
2635 error(ERR_WARNING,
2636 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00002637
H. Peter Anvine2c80182005-01-15 22:15:51 +00002638 if (!is_simple(evalresult)) {
2639 error(ERR_NONFATAL,
2640 "non-constant value given to `%%%sassign'",
2641 (i == PP_IASSIGN ? "i" : ""));
2642 free_tlist(origline);
2643 return DIRECTIVE_FOUND;
2644 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002645
H. Peter Anvine2c80182005-01-15 22:15:51 +00002646 macro_start = nasm_malloc(sizeof(*macro_start));
2647 macro_start->next = NULL;
2648 make_tok_num(macro_start, reloc_value(evalresult));
2649 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002650
H. Peter Anvine2c80182005-01-15 22:15:51 +00002651 /*
2652 * We now have a macro name, an implicit parameter count of
2653 * zero, and a numeric token to use as an expansion. Create
2654 * and store an SMacro.
2655 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002656 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002657 free_tlist(origline);
2658 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002659
H. Peter Anvine2c80182005-01-15 22:15:51 +00002660 case PP_LINE:
2661 /*
2662 * Syntax is `%line nnn[+mmm] [filename]'
2663 */
2664 tline = tline->next;
2665 skip_white_(tline);
2666 if (!tok_type_(tline, TOK_NUMBER)) {
2667 error(ERR_NONFATAL, "`%%line' expects line number");
2668 free_tlist(origline);
2669 return DIRECTIVE_FOUND;
2670 }
H. Peter Anvin70055962007-10-11 00:05:31 -07002671 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002672 m = 1;
2673 tline = tline->next;
2674 if (tok_is_(tline, "+")) {
2675 tline = tline->next;
2676 if (!tok_type_(tline, TOK_NUMBER)) {
2677 error(ERR_NONFATAL, "`%%line' expects line increment");
2678 free_tlist(origline);
2679 return DIRECTIVE_FOUND;
2680 }
H. Peter Anvin70055962007-10-11 00:05:31 -07002681 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002682 tline = tline->next;
2683 }
2684 skip_white_(tline);
2685 src_set_linnum(k);
2686 istk->lineinc = m;
2687 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002688 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002689 }
2690 free_tlist(origline);
2691 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002692
H. Peter Anvine2c80182005-01-15 22:15:51 +00002693 default:
2694 error(ERR_FATAL,
2695 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00002696 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002697 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002698 }
Ed Beroset3ab3f412002-06-11 03:31:49 +00002699 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002700}
2701
2702/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002703 * Ensure that a macro parameter contains a condition code and
2704 * nothing else. Return the condition code index if so, or -1
2705 * otherwise.
2706 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002707static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002708{
H. Peter Anvin76690a12002-04-30 20:52:49 +00002709 Token *tt;
2710 int i, j, k, m;
2711
H. Peter Anvin25a99342007-09-22 17:45:45 -07002712 if (!t)
2713 return -1; /* Probably a %+ without a space */
2714
H. Peter Anvineba20a72002-04-30 20:53:55 +00002715 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002716 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002717 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002718 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002719 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002720 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002721 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002722
2723 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00002724 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002725 while (j - i > 1) {
2726 k = (j + i) / 2;
2727 m = nasm_stricmp(t->text, conditions[k]);
2728 if (m == 0) {
2729 i = k;
2730 j = -2;
2731 break;
2732 } else if (m < 0) {
2733 j = k;
2734 } else
2735 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002736 }
2737 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002738 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002739 return i;
2740}
2741
2742/*
2743 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2744 * %-n) and MMacro-local identifiers (%%foo).
2745 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002746static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002747{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002748 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002749
2750 tail = &thead;
2751 thead = NULL;
2752
H. Peter Anvine2c80182005-01-15 22:15:51 +00002753 while (tline) {
2754 if (tline->type == TOK_PREPROC_ID &&
2755 (((tline->text[1] == '+' || tline->text[1] == '-')
2756 && tline->text[2]) || tline->text[1] == '%'
2757 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002758 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002759 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002760 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07002761 unsigned int n;
2762 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002763 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002764
H. Peter Anvine2c80182005-01-15 22:15:51 +00002765 t = tline;
2766 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002767
H. Peter Anvine2c80182005-01-15 22:15:51 +00002768 mac = istk->mstk;
2769 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2770 mac = mac->next_active;
2771 if (!mac)
2772 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2773 else
2774 switch (t->text[1]) {
2775 /*
2776 * We have to make a substitution of one of the
2777 * forms %1, %-1, %+1, %%foo, %0.
2778 */
2779 case '0':
2780 type = TOK_NUMBER;
2781 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
2782 text = nasm_strdup(tmpbuf);
2783 break;
2784 case '%':
2785 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002786 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002787 mac->unique);
2788 text = nasm_strcat(tmpbuf, t->text + 2);
2789 break;
2790 case '-':
2791 n = atoi(t->text + 2) - 1;
2792 if (n >= mac->nparam)
2793 tt = NULL;
2794 else {
2795 if (mac->nparam > 1)
2796 n = (n + mac->rotate) % mac->nparam;
2797 tt = mac->params[n];
2798 }
2799 cc = find_cc(tt);
2800 if (cc == -1) {
2801 error(ERR_NONFATAL,
2802 "macro parameter %d is not a condition code",
2803 n + 1);
2804 text = NULL;
2805 } else {
2806 type = TOK_ID;
2807 if (inverse_ccs[cc] == -1) {
2808 error(ERR_NONFATAL,
2809 "condition code `%s' is not invertible",
2810 conditions[cc]);
2811 text = NULL;
2812 } else
2813 text =
2814 nasm_strdup(conditions[inverse_ccs[cc]]);
2815 }
2816 break;
2817 case '+':
2818 n = atoi(t->text + 2) - 1;
2819 if (n >= mac->nparam)
2820 tt = NULL;
2821 else {
2822 if (mac->nparam > 1)
2823 n = (n + mac->rotate) % mac->nparam;
2824 tt = mac->params[n];
2825 }
2826 cc = find_cc(tt);
2827 if (cc == -1) {
2828 error(ERR_NONFATAL,
2829 "macro parameter %d is not a condition code",
2830 n + 1);
2831 text = NULL;
2832 } else {
2833 type = TOK_ID;
2834 text = nasm_strdup(conditions[cc]);
2835 }
2836 break;
2837 default:
2838 n = atoi(t->text + 1) - 1;
2839 if (n >= mac->nparam)
2840 tt = NULL;
2841 else {
2842 if (mac->nparam > 1)
2843 n = (n + mac->rotate) % mac->nparam;
2844 tt = mac->params[n];
2845 }
2846 if (tt) {
2847 for (i = 0; i < mac->paramlen[n]; i++) {
2848 *tail = new_Token(NULL, tt->type, tt->text, 0);
2849 tail = &(*tail)->next;
2850 tt = tt->next;
2851 }
2852 }
2853 text = NULL; /* we've done it here */
2854 break;
2855 }
2856 if (!text) {
2857 delete_Token(t);
2858 } else {
2859 *tail = t;
2860 tail = &t->next;
2861 t->type = type;
2862 nasm_free(t->text);
2863 t->text = text;
2864 t->mac = NULL;
2865 }
2866 continue;
2867 } else {
2868 t = *tail = tline;
2869 tline = tline->next;
2870 t->mac = NULL;
2871 tail = &t->next;
2872 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002873 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00002874 *tail = NULL;
2875 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002876 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002877 switch (t->type) {
2878 case TOK_WHITESPACE:
2879 if (tt->type == TOK_WHITESPACE) {
2880 t->next = delete_Token(tt);
2881 }
2882 break;
2883 case TOK_ID:
2884 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002885 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002886 nasm_free(t->text);
2887 t->text = tmp;
2888 t->next = delete_Token(tt);
2889 }
2890 break;
2891 case TOK_NUMBER:
2892 if (tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002893 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002894 nasm_free(t->text);
2895 t->text = tmp;
2896 t->next = delete_Token(tt);
2897 }
2898 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002899 default:
2900 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002901 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002902
H. Peter Anvin76690a12002-04-30 20:52:49 +00002903 return thead;
2904}
2905
2906/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002907 * Expand all single-line macro calls made in the given line.
2908 * Return the expanded version of the line. The original is deemed
2909 * to be destroyed in the process. (In reality we'll just move
2910 * Tokens from input to output a lot of the time, rather than
2911 * actually bothering to destroy and replicate.)
2912 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002913static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002914{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002915 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002916 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002917 Token **params;
2918 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07002919 unsigned int nparam, sparam;
2920 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002921 Token *org_tline = tline;
2922 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002923 char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002924
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002925 /*
2926 * Trick: we should avoid changing the start token pointer since it can
2927 * be contained in "next" field of other token. Because of this
2928 * we allocate a copy of first token and work with it; at the end of
2929 * routine we copy it back
2930 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002931 if (org_tline) {
2932 tline =
2933 new_Token(org_tline->next, org_tline->type, org_tline->text,
2934 0);
2935 tline->mac = org_tline->mac;
2936 nasm_free(org_tline->text);
2937 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002938 }
2939
H. Peter Anvin734b1882002-04-30 21:01:08 +00002940 again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002941 tail = &thead;
2942 thead = NULL;
2943
H. Peter Anvine2c80182005-01-15 22:15:51 +00002944 while (tline) { /* main token loop */
2945 if ((mname = tline->text)) {
2946 /* if this token is a local macro, look in local context */
2947 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002948 ctx = get_ctx(mname, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002949 else
2950 ctx = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002951 if (!ctx) {
2952 head = (SMacro *) hash_findix(smacros, mname);
2953 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002954 head = ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002955 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002956 /*
2957 * We've hit an identifier. As in is_mmacro below, we first
2958 * check whether the identifier is a single-line macro at
2959 * all, then think about checking for parameters if
2960 * necessary.
2961 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07002962 for (m = head; m; m = m->next)
2963 if (!mstrcmp(m->name, mname, m->casesense))
2964 break;
2965 if (m) {
2966 mstart = tline;
2967 params = NULL;
2968 paramsize = NULL;
2969 if (m->nparam == 0) {
2970 /*
2971 * Simple case: the macro is parameterless. Discard the
2972 * one token that the macro call took, and push the
2973 * expansion back on the to-do stack.
2974 */
2975 if (!m->expansion) {
2976 if (!strcmp("__FILE__", m->name)) {
2977 int32_t num = 0;
2978 src_get(&num, &(tline->text));
2979 nasm_quote(&(tline->text));
2980 tline->type = TOK_STRING;
2981 continue;
2982 }
2983 if (!strcmp("__LINE__", m->name)) {
2984 nasm_free(tline->text);
2985 make_tok_num(tline, src_get_linnum());
2986 continue;
2987 }
2988 if (!strcmp("__BITS__", m->name)) {
2989 nasm_free(tline->text);
2990 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002991 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002992 }
2993 tline = delete_Token(tline);
2994 continue;
2995 }
2996 } else {
2997 /*
2998 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00002999 * exists and takes parameters. We must find the
3000 * parameters in the call, count them, find the SMacro
3001 * that corresponds to that form of the macro call, and
3002 * substitute for the parameters when we expand. What a
3003 * pain.
3004 */
3005 /*tline = tline->next;
3006 skip_white_(tline); */
3007 do {
3008 t = tline->next;
3009 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003010 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003011 t->text = NULL;
3012 t = tline->next = delete_Token(t);
3013 }
3014 tline = t;
3015 } while (tok_type_(tline, TOK_WHITESPACE));
3016 if (!tok_is_(tline, "(")) {
3017 /*
3018 * This macro wasn't called with parameters: ignore
3019 * the call. (Behaviour borrowed from gnu cpp.)
3020 */
3021 tline = mstart;
3022 m = NULL;
3023 } else {
3024 int paren = 0;
3025 int white = 0;
3026 brackets = 0;
3027 nparam = 0;
3028 sparam = PARAM_DELTA;
3029 params = nasm_malloc(sparam * sizeof(Token *));
3030 params[0] = tline->next;
3031 paramsize = nasm_malloc(sparam * sizeof(int));
3032 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003033 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003034 /*
3035 * For some unusual expansions
3036 * which concatenates function call
3037 */
3038 t = tline->next;
3039 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003040 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003041 t->text = NULL;
3042 t = tline->next = delete_Token(t);
3043 }
3044 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003045
H. Peter Anvine2c80182005-01-15 22:15:51 +00003046 if (!tline) {
3047 error(ERR_NONFATAL,
3048 "macro call expects terminating `)'");
3049 break;
3050 }
3051 if (tline->type == TOK_WHITESPACE
3052 && brackets <= 0) {
3053 if (paramsize[nparam])
3054 white++;
3055 else
3056 params[nparam] = tline->next;
3057 continue; /* parameter loop */
3058 }
3059 if (tline->type == TOK_OTHER
3060 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003061 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003062 if (ch == ',' && !paren && brackets <= 0) {
3063 if (++nparam >= sparam) {
3064 sparam += PARAM_DELTA;
3065 params = nasm_realloc(params,
3066 sparam *
3067 sizeof(Token
3068 *));
3069 paramsize =
3070 nasm_realloc(paramsize,
3071 sparam *
3072 sizeof(int));
3073 }
3074 params[nparam] = tline->next;
3075 paramsize[nparam] = 0;
3076 white = 0;
3077 continue; /* parameter loop */
3078 }
3079 if (ch == '{' &&
3080 (brackets > 0 || (brackets == 0 &&
3081 !paramsize[nparam])))
3082 {
3083 if (!(brackets++)) {
3084 params[nparam] = tline->next;
3085 continue; /* parameter loop */
3086 }
3087 }
3088 if (ch == '}' && brackets > 0)
3089 if (--brackets == 0) {
3090 brackets = -1;
3091 continue; /* parameter loop */
3092 }
3093 if (ch == '(' && !brackets)
3094 paren++;
3095 if (ch == ')' && brackets <= 0)
3096 if (--paren < 0)
3097 break;
3098 }
3099 if (brackets < 0) {
3100 brackets = 0;
3101 error(ERR_NONFATAL, "braces do not "
3102 "enclose all of macro parameter");
3103 }
3104 paramsize[nparam] += white + 1;
3105 white = 0;
3106 } /* parameter loop */
3107 nparam++;
3108 while (m && (m->nparam != nparam ||
3109 mstrcmp(m->name, mname,
3110 m->casesense)))
3111 m = m->next;
3112 if (!m)
3113 error(ERR_WARNING | ERR_WARN_MNP,
3114 "macro `%s' exists, "
3115 "but not taking %d parameters",
3116 mstart->text, nparam);
3117 }
3118 }
3119 if (m && m->in_progress)
3120 m = NULL;
3121 if (!m) { /* in progess or didn't find '(' or wrong nparam */
3122 /*
3123 * Design question: should we handle !tline, which
3124 * indicates missing ')' here, or expand those
3125 * macros anyway, which requires the (t) test a few
3126 * lines down?
3127 */
3128 nasm_free(params);
3129 nasm_free(paramsize);
3130 tline = mstart;
3131 } else {
3132 /*
3133 * Expand the macro: we are placed on the last token of the
3134 * call, so that we can easily split the call from the
3135 * following tokens. We also start by pushing an SMAC_END
3136 * token for the cycle removal.
3137 */
3138 t = tline;
3139 if (t) {
3140 tline = t->next;
3141 t->next = NULL;
3142 }
3143 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3144 tt->mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003145 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003146 tline = tt;
3147 for (t = m->expansion; t; t = t->next) {
3148 if (t->type >= TOK_SMAC_PARAM) {
3149 Token *pcopy = tline, **ptail = &pcopy;
3150 Token *ttt, *pt;
3151 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003152
H. Peter Anvine2c80182005-01-15 22:15:51 +00003153 ttt = params[t->type - TOK_SMAC_PARAM];
3154 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3155 --i >= 0;) {
3156 pt = *ptail =
3157 new_Token(tline, ttt->type, ttt->text,
3158 0);
3159 ptail = &pt->next;
3160 ttt = ttt->next;
3161 }
3162 tline = pcopy;
3163 } else {
3164 tt = new_Token(tline, t->type, t->text, 0);
3165 tline = tt;
3166 }
3167 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003168
H. Peter Anvine2c80182005-01-15 22:15:51 +00003169 /*
3170 * Having done that, get rid of the macro call, and clean
3171 * up the parameters.
3172 */
3173 nasm_free(params);
3174 nasm_free(paramsize);
3175 free_tlist(mstart);
3176 continue; /* main token loop */
3177 }
3178 }
3179 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003180
H. Peter Anvine2c80182005-01-15 22:15:51 +00003181 if (tline->type == TOK_SMAC_END) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003182 tline->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003183 tline = delete_Token(tline);
3184 } else {
3185 t = *tail = tline;
3186 tline = tline->next;
3187 t->mac = NULL;
3188 t->next = NULL;
3189 tail = &t->next;
3190 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003191 }
3192
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003193 /*
3194 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003195 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003196 * TOK_IDs should be concatenated.
3197 * Also we look for %+ tokens and concatenate the tokens before and after
3198 * them (without white spaces in between).
3199 */
3200 t = thead;
3201 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003202 while (t) {
3203 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3204 t = t->next;
3205 if (!t || !t->next)
3206 break;
3207 if (t->next->type == TOK_ID ||
3208 t->next->type == TOK_PREPROC_ID ||
3209 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003210 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003211 nasm_free(t->text);
3212 t->next = delete_Token(t->next);
3213 t->text = p;
3214 rescan = 1;
3215 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3216 t->next->next->type == TOK_PREPROC_ID &&
3217 strcmp(t->next->next->text, "%+") == 0) {
3218 /* free the next whitespace, the %+ token and next whitespace */
3219 int i;
3220 for (i = 1; i <= 3; i++) {
3221 if (!t->next
3222 || (i != 2 && t->next->type != TOK_WHITESPACE))
3223 break;
3224 t->next = delete_Token(t->next);
3225 } /* endfor */
3226 } else
3227 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003228 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003229 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003230 if (rescan) {
3231 tline = thead;
3232 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003233 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003234
H. Peter Anvine2c80182005-01-15 22:15:51 +00003235 if (org_tline) {
3236 if (thead) {
3237 *org_tline = *thead;
3238 /* since we just gave text to org_line, don't free it */
3239 thead->text = NULL;
3240 delete_Token(thead);
3241 } else {
3242 /* the expression expanded to empty line;
3243 we can't return NULL for some reasons
3244 we just set the line to a single WHITESPACE token. */
3245 memset(org_tline, 0, sizeof(*org_tline));
3246 org_tline->text = NULL;
3247 org_tline->type = TOK_WHITESPACE;
3248 }
3249 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003250 }
3251
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003252 return thead;
3253}
3254
3255/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003256 * Similar to expand_smacro but used exclusively with macro identifiers
3257 * right before they are fetched in. The reason is that there can be
3258 * identifiers consisting of several subparts. We consider that if there
3259 * are more than one element forming the name, user wants a expansion,
3260 * otherwise it will be left as-is. Example:
3261 *
3262 * %define %$abc cde
3263 *
3264 * the identifier %$abc will be left as-is so that the handler for %define
3265 * will suck it and define the corresponding value. Other case:
3266 *
3267 * %define _%$abc cde
3268 *
3269 * In this case user wants name to be expanded *before* %define starts
3270 * working, so we'll expand %$abc into something (if it has a value;
3271 * otherwise it will be left as-is) then concatenate all successive
3272 * PP_IDs into one.
3273 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003274static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003275{
3276 Token *cur, *oldnext = NULL;
3277
H. Peter Anvin734b1882002-04-30 21:01:08 +00003278 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003279 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003280
3281 cur = tline;
3282 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003283 (cur->next->type == TOK_ID ||
3284 cur->next->type == TOK_PREPROC_ID
3285 || cur->next->type == TOK_NUMBER))
3286 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003287
3288 /* If identifier consists of just one token, don't expand */
3289 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003290 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003291
H. Peter Anvine2c80182005-01-15 22:15:51 +00003292 if (cur) {
3293 oldnext = cur->next; /* Detach the tail past identifier */
3294 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003295 }
3296
H. Peter Anvin734b1882002-04-30 21:01:08 +00003297 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003298
H. Peter Anvine2c80182005-01-15 22:15:51 +00003299 if (cur) {
3300 /* expand_smacro possibly changhed tline; re-scan for EOL */
3301 cur = tline;
3302 while (cur && cur->next)
3303 cur = cur->next;
3304 if (cur)
3305 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003306 }
3307
3308 return tline;
3309}
3310
3311/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003312 * Determine whether the given line constitutes a multi-line macro
3313 * call, and return the MMacro structure called if so. Doesn't have
3314 * to check for an initial label - that's taken care of in
3315 * expand_mmacro - but must check numbers of parameters. Guaranteed
3316 * to be called with tline->type == TOK_ID, so the putative macro
3317 * name is easy to find.
3318 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003319static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003320{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003321 MMacro *head, *m;
3322 Token **params;
3323 int nparam;
3324
H. Peter Anvin97a23472007-09-16 17:57:25 -07003325 head = (MMacro *) hash_findix(mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003326
3327 /*
3328 * Efficiency: first we see if any macro exists with the given
3329 * name. If not, we can return NULL immediately. _Then_ we
3330 * count the parameters, and then we look further along the
3331 * list if necessary to find the proper MMacro.
3332 */
3333 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003334 if (!mstrcmp(m->name, tline->text, m->casesense))
3335 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003336 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003337 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003338
3339 /*
3340 * OK, we have a potential macro. Count and demarcate the
3341 * parameters.
3342 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003343 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003344
3345 /*
3346 * So we know how many parameters we've got. Find the MMacro
3347 * structure that handles this number.
3348 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003349 while (m) {
3350 if (m->nparam_min <= nparam
3351 && (m->plus || nparam <= m->nparam_max)) {
3352 /*
3353 * This one is right. Just check if cycle removal
3354 * prohibits us using it before we actually celebrate...
3355 */
3356 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003357#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003358 error(ERR_NONFATAL,
3359 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003360#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003361 nasm_free(params);
3362 return NULL;
3363 }
3364 /*
3365 * It's right, and we can use it. Add its default
3366 * parameters to the end of our list if necessary.
3367 */
3368 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3369 params =
3370 nasm_realloc(params,
3371 ((m->nparam_min + m->ndefs +
3372 1) * sizeof(*params)));
3373 while (nparam < m->nparam_min + m->ndefs) {
3374 params[nparam] = m->defaults[nparam - m->nparam_min];
3375 nparam++;
3376 }
3377 }
3378 /*
3379 * If we've gone over the maximum parameter count (and
3380 * we're in Plus mode), ignore parameters beyond
3381 * nparam_max.
3382 */
3383 if (m->plus && nparam > m->nparam_max)
3384 nparam = m->nparam_max;
3385 /*
3386 * Then terminate the parameter list, and leave.
3387 */
3388 if (!params) { /* need this special case */
3389 params = nasm_malloc(sizeof(*params));
3390 nparam = 0;
3391 }
3392 params[nparam] = NULL;
3393 *params_array = params;
3394 return m;
3395 }
3396 /*
3397 * This one wasn't right: look for the next one with the
3398 * same name.
3399 */
3400 for (m = m->next; m; m = m->next)
3401 if (!mstrcmp(m->name, tline->text, m->casesense))
3402 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003403 }
3404
3405 /*
3406 * After all that, we didn't find one with the right number of
3407 * parameters. Issue a warning, and fail to expand the macro.
3408 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003409 error(ERR_WARNING | ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003410 "macro `%s' exists, but not taking %d parameters",
3411 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003412 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003413 return NULL;
3414}
3415
3416/*
3417 * Expand the multi-line macro call made by the given line, if
3418 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003419 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003420 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003421static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003422{
3423 Token *startline = tline;
3424 Token *label = NULL;
3425 int dont_prepend = 0;
3426 Token **params, *t, *tt;
3427 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003428 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003429 int i, nparam, *paramlen;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003430
3431 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003432 skip_white_(t);
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003433/* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3434 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003435 return 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003436 m = is_mmacro(t, &params);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003437 if (!m) {
3438 Token *last;
3439 /*
3440 * We have an id which isn't a macro call. We'll assume
3441 * it might be a label; we'll also check to see if a
3442 * colon follows it. Then, if there's another id after
3443 * that lot, we'll check it again for macro-hood.
3444 */
3445 label = last = t;
3446 t = t->next;
3447 if (tok_type_(t, TOK_WHITESPACE))
3448 last = t, t = t->next;
3449 if (tok_is_(t, ":")) {
3450 dont_prepend = 1;
3451 last = t, t = t->next;
3452 if (tok_type_(t, TOK_WHITESPACE))
3453 last = t, t = t->next;
3454 }
3455 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3456 return 0;
3457 last->next = NULL;
3458 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003459 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003460
3461 /*
3462 * Fix up the parameters: this involves stripping leading and
3463 * trailing whitespace, then stripping braces if they are
3464 * present.
3465 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003466 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003467 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003468
H. Peter Anvine2c80182005-01-15 22:15:51 +00003469 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003470 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003471 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003472
H. Peter Anvine2c80182005-01-15 22:15:51 +00003473 t = params[i];
3474 skip_white_(t);
3475 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003476 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003477 params[i] = t;
3478 paramlen[i] = 0;
3479 while (t) {
3480 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3481 break; /* ... because we have hit a comma */
3482 if (comma && t->type == TOK_WHITESPACE
3483 && tok_is_(t->next, ","))
3484 break; /* ... or a space then a comma */
3485 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3486 break; /* ... or a brace */
3487 t = t->next;
3488 paramlen[i]++;
3489 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003490 }
3491
3492 /*
3493 * OK, we have a MMacro structure together with a set of
3494 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003495 * copies of each Line on to istk->expansion. Substitution of
3496 * parameter tokens and macro-local tokens doesn't get done
3497 * until the single-line macro substitution process; this is
3498 * because delaying them allows us to change the semantics
3499 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003500 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003501 * First, push an end marker on to istk->expansion, mark this
3502 * macro as in progress, and set up its invocation-specific
3503 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003504 */
3505 ll = nasm_malloc(sizeof(Line));
3506 ll->next = istk->expansion;
3507 ll->finishes = m;
3508 ll->first = NULL;
3509 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003510
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003511 m->in_progress = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003512 m->params = params;
3513 m->iline = tline;
3514 m->nparam = nparam;
3515 m->rotate = 0;
3516 m->paramlen = paramlen;
3517 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003518 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003519
3520 m->next_active = istk->mstk;
3521 istk->mstk = m;
3522
H. Peter Anvine2c80182005-01-15 22:15:51 +00003523 for (l = m->expansion; l; l = l->next) {
3524 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003525
H. Peter Anvine2c80182005-01-15 22:15:51 +00003526 ll = nasm_malloc(sizeof(Line));
3527 ll->finishes = NULL;
3528 ll->next = istk->expansion;
3529 istk->expansion = ll;
3530 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003531
H. Peter Anvine2c80182005-01-15 22:15:51 +00003532 for (t = l->first; t; t = t->next) {
3533 Token *x = t;
3534 if (t->type == TOK_PREPROC_ID &&
3535 t->text[1] == '0' && t->text[2] == '0') {
3536 dont_prepend = -1;
3537 x = label;
3538 if (!x)
3539 continue;
3540 }
3541 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3542 tail = &tt->next;
3543 }
3544 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003545 }
3546
3547 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00003548 * If we had a label, push it on as the first line of
3549 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003550 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003551 if (label) {
3552 if (dont_prepend < 0)
3553 free_tlist(startline);
3554 else {
3555 ll = nasm_malloc(sizeof(Line));
3556 ll->finishes = NULL;
3557 ll->next = istk->expansion;
3558 istk->expansion = ll;
3559 ll->first = startline;
3560 if (!dont_prepend) {
3561 while (label->next)
3562 label = label->next;
3563 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3564 }
3565 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003566 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003567
H. Peter Anvin734b1882002-04-30 21:01:08 +00003568 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003569
H. Peter Anvineba20a72002-04-30 20:53:55 +00003570 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003571}
3572
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003573/*
3574 * Since preprocessor always operate only on the line that didn't
3575 * arrived yet, we should always use ERR_OFFBY1. Also since user
3576 * won't want to see same error twice (preprocessing is done once
3577 * per pass) we will want to show errors only during pass one.
3578 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003579static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003580{
3581 va_list arg;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003582 char buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003583
3584 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003585 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003586 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003587
H. Peter Anvin734b1882002-04-30 21:01:08 +00003588 va_start(arg, fmt);
Ed Beroset19f927a2004-12-15 17:07:03 +00003589 vsnprintf(buff, sizeof(buff), fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003590 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003591
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003592 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003593 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3594 istk->mstk->lineno, buff);
3595 else
3596 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003597}
3598
H. Peter Anvin734b1882002-04-30 21:01:08 +00003599static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003600pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003601 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003602{
H. Peter Anvin99941bf2002-05-14 17:44:03 +00003603 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003604 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003605 istk = nasm_malloc(sizeof(Include));
3606 istk->next = NULL;
3607 istk->conds = NULL;
3608 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003609 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003610 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00003611 istk->fname = NULL;
3612 src_set_fname(nasm_strdup(file));
3613 src_set_linnum(0);
3614 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003615 if (!istk->fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003616 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3617 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003618 defining = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003619 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003620 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003621 if (tasm_compatible_mode) {
3622 stdmacpos = stdmac;
3623 } else {
3624 stdmacpos = &stdmac[TASM_MACRO_COUNT];
3625 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003626 any_extrastdmac = (extrastdmac != NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003627 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003628 evaluate = eval;
3629 pass = apass;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003630}
3631
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003632static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003633{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003634 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003635 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003636
H. Peter Anvine2c80182005-01-15 22:15:51 +00003637 while (1) {
3638 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003639 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00003640 * buffer or from the input file.
3641 */
3642 tline = NULL;
3643 while (istk->expansion && istk->expansion->finishes) {
3644 Line *l = istk->expansion;
3645 if (!l->finishes->name && l->finishes->in_progress > 1) {
3646 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003647
H. Peter Anvine2c80182005-01-15 22:15:51 +00003648 /*
3649 * This is a macro-end marker for a macro with no
3650 * name, which means it's not really a macro at all
3651 * but a %rep block, and the `in_progress' field is
3652 * more than 1, meaning that we still need to
3653 * repeat. (1 means the natural last repetition; 0
3654 * means termination by %exitrep.) We have
3655 * therefore expanded up to the %endrep, and must
3656 * push the whole block on to the expansion buffer
3657 * again. We don't bother to remove the macro-end
3658 * marker: we'd only have to generate another one
3659 * if we did.
3660 */
3661 l->finishes->in_progress--;
3662 for (l = l->finishes->expansion; l; l = l->next) {
3663 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003664
H. Peter Anvine2c80182005-01-15 22:15:51 +00003665 ll = nasm_malloc(sizeof(Line));
3666 ll->next = istk->expansion;
3667 ll->finishes = NULL;
3668 ll->first = NULL;
3669 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003670
H. Peter Anvine2c80182005-01-15 22:15:51 +00003671 for (t = l->first; t; t = t->next) {
3672 if (t->text || t->type == TOK_WHITESPACE) {
3673 tt = *tail =
3674 new_Token(NULL, t->type, t->text, 0);
3675 tail = &tt->next;
3676 }
3677 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003678
H. Peter Anvine2c80182005-01-15 22:15:51 +00003679 istk->expansion = ll;
3680 }
3681 } else {
3682 /*
3683 * Check whether a `%rep' was started and not ended
3684 * within this macro expansion. This can happen and
3685 * should be detected. It's a fatal error because
3686 * I'm too confused to work out how to recover
3687 * sensibly from it.
3688 */
3689 if (defining) {
3690 if (defining->name)
3691 error(ERR_PANIC,
3692 "defining with name in expansion");
3693 else if (istk->mstk->name)
3694 error(ERR_FATAL,
3695 "`%%rep' without `%%endrep' within"
3696 " expansion of macro `%s'",
3697 istk->mstk->name);
3698 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00003699
H. Peter Anvine2c80182005-01-15 22:15:51 +00003700 /*
3701 * FIXME: investigate the relationship at this point between
3702 * istk->mstk and l->finishes
3703 */
3704 {
3705 MMacro *m = istk->mstk;
3706 istk->mstk = m->next_active;
3707 if (m->name) {
3708 /*
3709 * This was a real macro call, not a %rep, and
3710 * therefore the parameter information needs to
3711 * be freed.
3712 */
3713 nasm_free(m->params);
3714 free_tlist(m->iline);
3715 nasm_free(m->paramlen);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003716 l->finishes->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003717 } else
3718 free_mmacro(m);
3719 }
3720 istk->expansion = l->next;
3721 nasm_free(l);
3722 list->downlevel(LIST_MACRO);
3723 }
3724 }
3725 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003726
H. Peter Anvine2c80182005-01-15 22:15:51 +00003727 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003728 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003729 Line *l = istk->expansion;
3730 if (istk->mstk)
3731 istk->mstk->lineno++;
3732 tline = l->first;
3733 istk->expansion = l->next;
3734 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003735 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003736 list->line(LIST_MACRO, p);
3737 nasm_free(p);
3738 break;
3739 }
3740 line = read_line();
3741 if (line) { /* from the current input file */
3742 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00003743 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003744 nasm_free(line);
3745 break;
3746 }
3747 /*
3748 * The current file has ended; work down the istk
3749 */
3750 {
3751 Include *i = istk;
3752 fclose(i->fp);
3753 if (i->conds)
3754 error(ERR_FATAL,
3755 "expected `%%endif' before end of file");
3756 /* only set line and file name if there's a next node */
3757 if (i->next) {
3758 src_set_linnum(i->lineno);
3759 nasm_free(src_set_fname(i->fname));
3760 }
3761 istk = i->next;
3762 list->downlevel(LIST_INCLUDE);
3763 nasm_free(i);
3764 if (!istk)
3765 return NULL;
3766 }
3767 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003768
H. Peter Anvine2c80182005-01-15 22:15:51 +00003769 /*
3770 * We must expand MMacro parameters and MMacro-local labels
3771 * _before_ we plunge into directive processing, to cope
3772 * with things like `%define something %1' such as STRUC
3773 * uses. Unless we're _defining_ a MMacro, in which case
3774 * those tokens should be left alone to go into the
3775 * definition; and unless we're in a non-emitting
3776 * condition, in which case we don't want to meddle with
3777 * anything.
3778 */
3779 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
3780 tline = expand_mmac_params(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003781
H. Peter Anvine2c80182005-01-15 22:15:51 +00003782 /*
3783 * Check the line to see if it's a preprocessor directive.
3784 */
3785 if (do_directive(tline) == DIRECTIVE_FOUND) {
3786 continue;
3787 } else if (defining) {
3788 /*
3789 * We're defining a multi-line macro. We emit nothing
3790 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00003791 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003792 */
3793 Line *l = nasm_malloc(sizeof(Line));
3794 l->next = defining->expansion;
3795 l->first = tline;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003796 l->finishes = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003797 defining->expansion = l;
3798 continue;
3799 } else if (istk->conds && !emitting(istk->conds->state)) {
3800 /*
3801 * We're in a non-emitting branch of a condition block.
3802 * Emit nothing at all, not even a blank line: when we
3803 * emerge from the condition we'll give a line-number
3804 * directive so we keep our place correctly.
3805 */
3806 free_tlist(tline);
3807 continue;
3808 } else if (istk->mstk && !istk->mstk->in_progress) {
3809 /*
3810 * We're in a %rep block which has been terminated, so
3811 * we're walking through to the %endrep without
3812 * emitting anything. Emit nothing at all, not even a
3813 * blank line: when we emerge from the %rep block we'll
3814 * give a line-number directive so we keep our place
3815 * correctly.
3816 */
3817 free_tlist(tline);
3818 continue;
3819 } else {
3820 tline = expand_smacro(tline);
3821 if (!expand_mmacro(tline)) {
3822 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003823 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003824 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003825 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003826 free_tlist(tline);
3827 break;
3828 } else {
3829 continue; /* expand_mmacro calls free_tlist */
3830 }
3831 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003832 }
3833
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003834 return line;
3835}
3836
H. Peter Anvine2c80182005-01-15 22:15:51 +00003837static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003838{
H. Peter Anvine2c80182005-01-15 22:15:51 +00003839 if (defining) {
3840 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
3841 defining->name);
3842 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003843 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003844 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003845 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07003846 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00003847 while (istk) {
3848 Include *i = istk;
3849 istk = istk->next;
3850 fclose(i->fp);
3851 nasm_free(i->fname);
3852 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003853 }
3854 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003855 ctx_pop();
3856 if (pass == 0) {
3857 free_llist(predef);
3858 delete_Blocks();
3859 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003860}
3861
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003862void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003863{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003864 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07003865
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003866 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07003867 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003868 i->next = NULL;
3869
H. Peter Anvine2c80182005-01-15 22:15:51 +00003870 if (ipath != NULL) {
3871 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003872 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003873 j = j->next;
3874 j->next = i;
3875 } else {
3876 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003877 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003878}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003879
3880/*
3881 * added by alexfru:
3882 *
3883 * This function is used to "export" the include paths, e.g.
3884 * the paths specified in the '-I' command switch.
3885 * The need for such exporting is due to the 'incbin' directive,
3886 * which includes raw binary files (unlike '%include', which
3887 * includes text source files). It would be real nice to be
3888 * able to specify paths to search for incbin'ned files also.
3889 * So, this is a simple workaround.
3890 *
3891 * The function use is simple:
3892 *
3893 * The 1st call (with NULL argument) returns a pointer to the 1st path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003894 * (char** type) or NULL if none include paths available.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003895 *
3896 * All subsequent calls take as argument the value returned by this
3897 * function last. The return value is either the next path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003898 * (char** type) or NULL if the end of the paths list is reached.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003899 *
3900 * It is maybe not the best way to do things, but I didn't want
3901 * to export too much, just one or two functions and no types or
3902 * variables exported.
3903 *
3904 * Can't say I like the current situation with e.g. this path list either,
3905 * it seems to be never deallocated after creation...
3906 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003907char **pp_get_include_path_ptr(char **pPrevPath)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003908{
3909/* This macro returns offset of a member of a structure */
3910#define GetMemberOffset(StructType,MemberName)\
3911 ((size_t)&((StructType*)0)->MemberName)
3912 IncPath *i;
3913
H. Peter Anvine2c80182005-01-15 22:15:51 +00003914 if (pPrevPath == NULL) {
3915 if (ipath != NULL)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003916 return &ipath->path;
3917 else
3918 return NULL;
3919 }
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003920 i = (IncPath *) ((char *)pPrevPath - GetMemberOffset(IncPath, path));
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003921 i = i->next;
3922 if (i != NULL)
3923 return &i->path;
3924 else
3925 return NULL;
3926#undef GetMemberOffset
3927}
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003928
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003929void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003930{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003931 Token *inc, *space, *name;
3932 Line *l;
3933
H. Peter Anvin734b1882002-04-30 21:01:08 +00003934 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
3935 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
3936 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003937
3938 l = nasm_malloc(sizeof(Line));
3939 l->next = predef;
3940 l->first = inc;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003941 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003942 predef = l;
3943}
3944
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003945void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003946{
3947 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003948 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003949 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003950
3951 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00003952 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
3953 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003954 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003955 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00003956 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003957 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003958 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003959
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003960 l = nasm_malloc(sizeof(Line));
3961 l->next = predef;
3962 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003963 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003964 predef = l;
3965}
3966
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003967void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00003968{
3969 Token *def, *space;
3970 Line *l;
3971
H. Peter Anvin734b1882002-04-30 21:01:08 +00003972 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
3973 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00003974 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00003975
3976 l = nasm_malloc(sizeof(Line));
3977 l->next = predef;
3978 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003979 l->finishes = false;
H. Peter Anvin620515a2002-04-30 20:57:38 +00003980 predef = l;
3981}
3982
Keith Kaniosb7a89542007-04-12 02:40:54 +00003983/*
3984 * Added by Keith Kanios:
3985 *
3986 * This function is used to assist with "runtime" preprocessor
3987 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
3988 *
3989 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
3990 * PASS A VALID STRING TO THIS FUNCTION!!!!!
3991 */
3992
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003993void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00003994{
3995 Token *def;
3996
3997 def = tokenize(definition);
3998 if(do_directive(def) == NO_DIRECTIVE_FOUND)
3999 free_tlist(def);
4000
4001}
4002
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004003void pp_extra_stdmac(const char **macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004004{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004005 extrastdmac = macros;
4006}
4007
Keith Kaniosb7a89542007-04-12 02:40:54 +00004008static void make_tok_num(Token * tok, int32_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004009{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004010 char numbuf[20];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00004011 snprintf(numbuf, sizeof(numbuf), "%"PRId32"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004012 tok->text = nasm_strdup(numbuf);
4013 tok->type = TOK_NUMBER;
4014}
4015
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004016Preproc nasmpp = {
4017 pp_reset,
4018 pp_getline,
4019 pp_cleanup
4020};