blob: 9bc6f71192cf637962c4c3052375055f5ba88dd4 [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 Anvinc2df2822007-10-24 15:29:28 -070051#include "stdscan.h"
52#include "tokens.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000053
54typedef struct SMacro SMacro;
55typedef struct MMacro MMacro;
56typedef struct Context Context;
57typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000058typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000059typedef struct Line Line;
60typedef struct Include Include;
61typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000062typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000063
64/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070065 * Note on the storage of both SMacro and MMacros: the hash table
66 * indexes them case-insensitively, and we then have to go through a
67 * linked list of potential case aliases (and, for MMacros, parameter
68 * ranges); this is to preserve the matching semantics of the earlier
69 * code. If the number of case aliases for a specific macro is a
70 * performance issue, you may want to reconsider your coding style.
71 */
72
73/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000074 * Store the definition of a single-line macro.
75 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000076struct SMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000077 SMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +000078 char *name;
H. Peter Anvin70055962007-10-11 00:05:31 -070079 bool casesense;
H. Peter Anvin16ed4382007-10-11 10:06:19 -070080 bool in_progress;
H. Peter Anvin70055962007-10-11 00:05:31 -070081 unsigned int nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000082 Token *expansion;
83};
84
85/*
H. Peter Anvin76690a12002-04-30 20:52:49 +000086 * Store the definition of a multi-line macro. This is also used to
87 * store the interiors of `%rep...%endrep' blocks, which are
88 * effectively self-re-invoking multi-line macros which simply
89 * don't have a name or bother to appear in the hash tables. %rep
90 * blocks are signified by having a NULL `name' field.
91 *
92 * In a MMacro describing a `%rep' block, the `in_progress' field
93 * isn't merely boolean, but gives the number of repeats left to
94 * run.
95 *
96 * The `next' field is used for storing MMacros in hash tables; the
97 * `next_active' field is for stacking them on istk entries.
98 *
99 * When a MMacro is being expanded, `params', `iline', `nparam',
100 * `paramlen', `rotate' and `unique' are local to the invocation.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000101 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000102struct MMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000103 MMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000104 char *name;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -0700105 int nparam_min, nparam_max;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700106 bool casesense;
107 bool plus; /* is the last parameter greedy? */
108 bool nolist; /* is this macro listing-inhibited? */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700109 int64_t in_progress;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000110 Token *dlist; /* All defaults as one list */
111 Token **defaults; /* Parameter default pointers */
112 int ndefs; /* number of default parameters */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000113 Line *expansion;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000114
115 MMacro *next_active;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000116 MMacro *rep_nest; /* used for nesting %rep */
117 Token **params; /* actual parameters */
118 Token *iline; /* invocation line */
H. Peter Anvin25a99342007-09-22 17:45:45 -0700119 unsigned int nparam, rotate;
120 int *paramlen;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700121 uint64_t unique;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000122 int lineno; /* Current line number on expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000123};
124
125/*
126 * The context stack is composed of a linked list of these.
127 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000128struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000129 Context *next;
130 SMacro *localmac;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000131 char *name;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000132 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000133};
134
135/*
136 * This is the internal form which we break input lines up into.
137 * Typically stored in linked lists.
138 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000139 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
140 * necessarily used as-is, but is intended to denote the number of
141 * the substituted parameter. So in the definition
142 *
143 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700144 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000145 * the token representing `x' will have its type changed to
146 * TOK_SMAC_PARAM, but the one representing `y' will be
147 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000148 *
149 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
150 * which doesn't need quotes around it. Used in the pre-include
151 * mechanism as an alternative to trying to find a sensible type of
152 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000153 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000154enum pp_token_type {
155 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
156 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700157 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER, TOK_SMAC_PARAM,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000158 TOK_INTERNAL_STRING
159};
160
H. Peter Anvine2c80182005-01-15 22:15:51 +0000161struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000162 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000163 char *text;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000164 SMacro *mac; /* associated macro for TOK_SMAC_END */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000165 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000166};
167
168/*
169 * Multi-line macro definitions are stored as a linked list of
170 * these, which is essentially a container to allow several linked
171 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700172 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000173 * Note that in this module, linked lists are treated as stacks
174 * wherever possible. For this reason, Lines are _pushed_ on to the
175 * `expansion' field in MMacro structures, so that the linked list,
176 * if walked, would give the macro lines in reverse order; this
177 * means that we can walk the list when expanding a macro, and thus
178 * push the lines on to the `expansion' field in _istk_ in reverse
179 * order (so that when popped back off they are in the right
180 * order). It may seem cockeyed, and it relies on my design having
181 * an even number of steps in, but it works...
182 *
183 * Some of these structures, rather than being actual lines, are
184 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000185 * This is for use in the cycle-tracking and %rep-handling code.
186 * Such structures have `finishes' non-NULL, and `first' NULL. All
187 * others have `finishes' NULL, but `first' may still be NULL if
188 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000189 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000190struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000191 Line *next;
192 MMacro *finishes;
193 Token *first;
194};
195
196/*
197 * To handle an arbitrary level of file inclusion, we maintain a
198 * stack (ie linked list) of these things.
199 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000200struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000201 Include *next;
202 FILE *fp;
203 Cond *conds;
204 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000205 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000206 int lineno, lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000207 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000208};
209
210/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000211 * Include search path. This is simply a list of strings which get
212 * prepended, in turn, to the name of an include file, in an
213 * attempt to find the file if it's not in the current directory.
214 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000215struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000216 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000217 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000218};
219
220/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000221 * Conditional assembly: we maintain a separate stack of these for
222 * each level of file inclusion. (The only reason we keep the
223 * stacks separate is to ensure that a stray `%endif' in a file
224 * included from within the true branch of a `%if' won't terminate
225 * it and cause confusion: instead, rightly, it'll cause an error.)
226 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000227struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000228 Cond *next;
229 int state;
230};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000231enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000232 /*
233 * These states are for use just after %if or %elif: IF_TRUE
234 * means the condition has evaluated to truth so we are
235 * currently emitting, whereas IF_FALSE means we are not
236 * currently emitting but will start doing so if a %else comes
237 * up. In these states, all directives are admissible: %elif,
238 * %else and %endif. (And of course %if.)
239 */
240 COND_IF_TRUE, COND_IF_FALSE,
241 /*
242 * These states come up after a %else: ELSE_TRUE means we're
243 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
244 * any %elif or %else will cause an error.
245 */
246 COND_ELSE_TRUE, COND_ELSE_FALSE,
247 /*
248 * This state means that we're not emitting now, and also that
249 * nothing until %endif will be emitted at all. It's for use in
250 * two circumstances: (i) when we've had our moment of emission
251 * and have now started seeing %elifs, and (ii) when the
252 * condition construct in question is contained within a
253 * non-emitting branch of a larger condition construct.
254 */
255 COND_NEVER
256};
257#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
258
H. Peter Anvin70653092007-10-19 14:42:29 -0700259/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000260 * These defines are used as the possible return values for do_directive
261 */
262#define NO_DIRECTIVE_FOUND 0
263#define DIRECTIVE_FOUND 1
264
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000265/*
266 * Condition codes. Note that we use c_ prefix not C_ because C_ is
267 * used in nasm.h for the "real" condition codes. At _this_ level,
268 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
269 * ones, so we need a different enum...
270 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700271static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000272 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
273 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000274 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000275};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700276enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000277 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
278 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 -0700279 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
280 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000281};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700282static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000283 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
284 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 +0000285 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000286};
287
H. Peter Anvin76690a12002-04-30 20:52:49 +0000288/*
289 * Directive names.
290 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000291/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000292static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000293{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000294 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000295}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000296
297/* For TASM compatibility we need to be able to recognise TASM compatible
298 * conditional compilation directives. Using the NASM pre-processor does
299 * not work, so we look for them specifically from the following list and
300 * then jam in the equivalent NASM directive into the input stream.
301 */
302
303#ifndef MAX
304# define MAX(a,b) ( ((a) > (b)) ? (a) : (b))
305#endif
306
H. Peter Anvine2c80182005-01-15 22:15:51 +0000307enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000308 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
309 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
310};
311
H. Peter Anvin476d2862007-10-02 22:04:15 -0700312static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000313 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
314 "ifndef", "include", "local"
315};
316
317static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000318static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000319static int ArgOffset = 8;
320static int LocalOffset = 4;
321
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000322static Context *cstk;
323static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000324static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000325
H. Peter Anvine2c80182005-01-15 22:15:51 +0000326static efunc _error; /* Pointer to client-provided error reporting function */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000327static evalfunc evaluate;
328
H. Peter Anvine2c80182005-01-15 22:15:51 +0000329static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000330
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700331static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000332
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000333static Line *predef = NULL;
334
335static ListGen *list;
336
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000337/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000338 * The current set of multi-line macros we have defined.
339 */
H. Peter Anvin97a23472007-09-16 17:57:25 -0700340static struct hash_table *mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341
342/*
343 * The current set of single-line macros we have defined.
344 */
H. Peter Anvin97a23472007-09-16 17:57:25 -0700345static struct hash_table *smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000346
347/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000348 * The multi-line macro we are currently defining, or the %rep
349 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000350 */
351static MMacro *defining;
352
353/*
354 * The number of macro parameters to allocate space for at a time.
355 */
356#define PARAM_DELTA 16
357
358/*
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000359 * The standard macro set: defined as `static char *stdmac[]'. Also
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000360 * gives our position in the macro set, when we're processing it.
361 */
362#include "macros.c"
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000363static const char **stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000364
365/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000366 * The extra standard macros that come from the object format, if
367 * any.
368 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000369static const char **extrastdmac = NULL;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700370bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000371
372/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000373 * Tokens are allocated in blocks to improve speed
374 */
375#define TOKEN_BLOCKSIZE 4096
376static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000377struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000378 Blocks *next;
379 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000380};
381
382static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000383
384/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000385 * Forward declarations.
386 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000387static Token *expand_mmac_params(Token * tline);
388static Token *expand_smacro(Token * tline);
389static Token *expand_id(Token * tline);
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700390static Context *get_ctx(char *name, bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700391static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000392static void error(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000393static void *new_Block(size_t size);
394static void delete_Blocks(void);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000395static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000396static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000397
398/*
399 * Macros for safe checking of token pointers, avoid *(NULL)
400 */
401#define tok_type_(x,t) ((x) && (x)->type == (t))
402#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
403#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
404#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000405
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000406/* Handle TASM specific directives, which do not contain a % in
407 * front of them. We do it here because I could not find any other
408 * place to do it for the moment, and it is a hack (ideally it would
409 * be nice to be able to use the NASM pre-processor to do it).
410 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000411static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000412{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000413 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000414 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000415
416 /* Skip whitespace */
417 while (isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000418 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000419
420 /* Binary search for the directive name */
421 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000422 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000423 len = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000424 while (!isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000425 len++;
426 if (len) {
427 oldchar = p[len];
428 p[len] = 0;
429 while (j - i > 1) {
430 k = (j + i) / 2;
431 m = nasm_stricmp(p, tasm_directives[k]);
432 if (m == 0) {
433 /* We have found a directive, so jam a % in front of it
434 * so that NASM will then recognise it as one if it's own.
435 */
436 p[len] = oldchar;
437 len = strlen(p);
438 oldline = line;
439 line = nasm_malloc(len + 2);
440 line[0] = '%';
441 if (k == TM_IFDIFI) {
442 /* NASM does not recognise IFDIFI, so we convert it to
443 * %ifdef BOGUS. This is not used in NASM comaptible
444 * code, but does need to parse for the TASM macro
445 * package.
446 */
447 strcpy(line + 1, "ifdef BOGUS");
448 } else {
449 memcpy(line + 1, p, len + 1);
450 }
451 nasm_free(oldline);
452 return line;
453 } else if (m < 0) {
454 j = k;
455 } else
456 i = k;
457 }
458 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000459 }
460 return line;
461}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000462
H. Peter Anvin76690a12002-04-30 20:52:49 +0000463/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000464 * The pre-preprocessing stage... This function translates line
465 * number indications as they emerge from GNU cpp (`# lineno "file"
466 * flags') into NASM preprocessor line number indications (`%line
467 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000468 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000469static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000470{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000471 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000472 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000473
H. Peter Anvine2c80182005-01-15 22:15:51 +0000474 if (line[0] == '#' && line[1] == ' ') {
475 oldline = line;
476 fname = oldline + 2;
477 lineno = atoi(fname);
478 fname += strspn(fname, "0123456789 ");
479 if (*fname == '"')
480 fname++;
481 fnlen = strcspn(fname, "\"");
482 line = nasm_malloc(20 + fnlen);
483 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
484 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000485 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000486 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000487 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000488 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000489}
490
491/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000492 * Free a linked list of tokens.
493 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000494static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000495{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000496 while (list) {
497 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000498 }
499}
500
501/*
502 * Free a linked list of lines.
503 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000504static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000505{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000506 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000507 while (list) {
508 l = list;
509 list = list->next;
510 free_tlist(l->first);
511 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000512 }
513}
514
515/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000516 * Free an MMacro
517 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000518static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000519{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000520 nasm_free(m->name);
521 free_tlist(m->dlist);
522 nasm_free(m->defaults);
523 free_llist(m->expansion);
524 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000525}
526
527/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700528 * Free all currently defined macros, and free the hash tables
529 */
530static void free_macros(void)
531{
532 struct hash_tbl_node *it;
533 const char *key;
534 SMacro *s;
535 MMacro *m;
536
537 it = NULL;
538 while ((s = hash_iterate(smacros, &it, &key)) != NULL) {
539 nasm_free((void *)key);
540 while (s) {
541 SMacro *ns = s->next;
542 nasm_free(s->name);
543 free_tlist(s->expansion);
544 nasm_free(s);
545 s = ns;
546 }
547 }
548 hash_free(smacros);
549
550 it = NULL;
551 while ((m = hash_iterate(mmacros, &it, &key)) != NULL) {
552 nasm_free((void *)key);
553 while (m) {
554 MMacro *nm = m->next;
555 free_mmacro(m);
556 m = nm;
557 }
558 }
559 hash_free(mmacros);
560}
561
562/*
563 * Initialize the hash tables
564 */
565static void init_macros(void)
566{
567 smacros = hash_init();
568 mmacros = hash_init();
569}
570
571/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000572 * Pop the context stack.
573 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000574static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000575{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000576 Context *c = cstk;
577 SMacro *smac, *s;
578
579 cstk = cstk->next;
580 smac = c->localmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000581 while (smac) {
582 s = smac;
583 smac = smac->next;
584 nasm_free(s->name);
585 free_tlist(s->expansion);
586 nasm_free(s);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000587 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000588 nasm_free(c->name);
589 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000590}
591
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000592#define BUF_DELTA 512
593/*
594 * Read a line from the top file in istk, handling multiple CR/LFs
595 * at the end of the line read, and handling spurious ^Zs. Will
596 * return lines from the standard macro set if this has not already
597 * been done.
598 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000599static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000600{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000601 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000602 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000603
H. Peter Anvine2c80182005-01-15 22:15:51 +0000604 if (stdmacpos) {
605 if (*stdmacpos) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000606 char *ret = nasm_strdup(*stdmacpos++);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000607 if (!*stdmacpos && any_extrastdmac) {
608 stdmacpos = extrastdmac;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700609 any_extrastdmac = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000610 return ret;
611 }
612 /*
613 * Nasty hack: here we push the contents of `predef' on
614 * to the top-level expansion stack, since this is the
615 * most convenient way to implement the pre-include and
616 * pre-define features.
617 */
618 if (!*stdmacpos) {
619 Line *pd, *l;
620 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000621
H. Peter Anvine2c80182005-01-15 22:15:51 +0000622 for (pd = predef; pd; pd = pd->next) {
623 head = NULL;
624 tail = &head;
625 for (t = pd->first; t; t = t->next) {
626 *tail = new_Token(NULL, t->type, t->text, 0);
627 tail = &(*tail)->next;
628 }
629 l = nasm_malloc(sizeof(Line));
630 l->next = istk->expansion;
631 l->first = head;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700632 l->finishes = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000633 istk->expansion = l;
634 }
635 }
636 return ret;
637 } else {
638 stdmacpos = NULL;
639 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000640 }
641
642 bufsize = BUF_DELTA;
643 buffer = nasm_malloc(BUF_DELTA);
644 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000645 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000646 while (1) {
647 q = fgets(p, bufsize - (p - buffer), istk->fp);
648 if (!q)
649 break;
650 p += strlen(p);
651 if (p > buffer && p[-1] == '\n') {
652 /* Convert backslash-CRLF line continuation sequences into
653 nothing at all (for DOS and Windows) */
654 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
655 p -= 3;
656 *p = 0;
657 continued_count++;
658 }
659 /* Also convert backslash-LF line continuation sequences into
660 nothing at all (for Unix) */
661 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
662 p -= 2;
663 *p = 0;
664 continued_count++;
665 } else {
666 break;
667 }
668 }
669 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000670 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000671 bufsize += BUF_DELTA;
672 buffer = nasm_realloc(buffer, bufsize);
673 p = buffer + offset; /* prevent stale-pointer problems */
674 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000675 }
676
H. Peter Anvine2c80182005-01-15 22:15:51 +0000677 if (!q && p == buffer) {
678 nasm_free(buffer);
679 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000680 }
681
H. Peter Anvine2c80182005-01-15 22:15:51 +0000682 src_set_linnum(src_get_linnum() + istk->lineinc +
683 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000684
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000685 /*
686 * Play safe: remove CRs as well as LFs, if any of either are
687 * present at the end of the line.
688 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000689 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000690 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000691
692 /*
693 * Handle spurious ^Z, which may be inserted into source files
694 * by some file transfer utilities.
695 */
696 buffer[strcspn(buffer, "\032")] = '\0';
697
H. Peter Anvin734b1882002-04-30 21:01:08 +0000698 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000699
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000700 return buffer;
701}
702
703/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000704 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000705 * don't need to parse the value out of e.g. numeric tokens: we
706 * simply split one string into many.
707 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000708static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000709{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000710 char *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000711 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000712 Token *list = NULL;
713 Token *t, **tail = &list;
714
H. Peter Anvine2c80182005-01-15 22:15:51 +0000715 while (*line) {
716 p = line;
717 if (*p == '%') {
718 p++;
719 if (isdigit(*p) ||
720 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
721 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
722 do {
723 p++;
724 }
725 while (isdigit(*p));
726 type = TOK_PREPROC_ID;
727 } else if (*p == '{') {
728 p++;
729 while (*p && *p != '}') {
730 p[-1] = *p;
731 p++;
732 }
733 p[-1] = '\0';
734 if (*p)
735 p++;
736 type = TOK_PREPROC_ID;
737 } else if (isidchar(*p) ||
738 ((*p == '!' || *p == '%' || *p == '$') &&
739 isidchar(p[1]))) {
740 do {
741 p++;
742 }
743 while (isidchar(*p));
744 type = TOK_PREPROC_ID;
745 } else {
746 type = TOK_OTHER;
747 if (*p == '%')
748 p++;
749 }
750 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
751 type = TOK_ID;
752 p++;
753 while (*p && isidchar(*p))
754 p++;
755 } else if (*p == '\'' || *p == '"') {
756 /*
757 * A string token.
758 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000759 char c = *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000760 p++;
761 type = TOK_STRING;
762 while (*p && *p != c)
763 p++;
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000764
H. Peter Anvine2c80182005-01-15 22:15:51 +0000765 if (*p) {
766 p++;
767 } else {
768 error(ERR_WARNING, "unterminated string");
769 /* Handling unterminated strings by UNV */
770 /* type = -1; */
771 }
772 } else if (isnumstart(*p)) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700773 bool is_hex = false;
774 bool is_float = false;
775 bool has_e = false;
776 char c, *r;
777
H. Peter Anvine2c80182005-01-15 22:15:51 +0000778 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700779 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000780 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700781
782 if (*p == '$') {
783 p++;
784 is_hex = true;
785 }
786
787 for (;;) {
788 c = *p++;
789
790 if (!is_hex && (c == 'e' || c == 'E')) {
791 has_e = true;
792 if (*p == '+' || *p == '-') {
793 /* e can only be followed by +/- if it is either a
794 prefixed hex number or a floating-point number */
795 p++;
796 is_float = true;
797 }
798 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
799 is_hex = true;
800 } else if (c == 'P' || c == 'p') {
801 is_float = true;
802 if (*p == '+' || *p == '-')
803 p++;
804 } else if (isnumchar(c) || c == '_')
805 ; /* just advance */
806 else if (c == '.') {
807 /* we need to deal with consequences of the legacy
808 parser, like "1.nolist" being two tokens
809 (TOK_NUMBER, TOK_ID) here; at least give it
810 a shot for now. In the future, we probably need
811 a flex-based scanner with proper pattern matching
812 to do it as well as it can be done. Nothing in
813 the world is going to help the person who wants
814 0x123.p16 interpreted as two tokens, though. */
815 r = p;
816 while (*r == '_')
817 r++;
818
819 if (isdigit(*r) || (is_hex && isxdigit(*r)) ||
820 (!is_hex && (*r == 'e' || *r == 'E')) ||
821 (*r == 'p' || *r == 'P')) {
822 p = r;
823 is_float = true;
824 } else
825 break; /* Terminate the token */
826 } else
827 break;
828 }
829 p--; /* Point to first character beyond number */
830
831 if (has_e && !is_hex) {
832 /* 1e13 is floating-point, but 1e13h is not */
833 is_float = true;
834 }
835
836 type = is_float ? TOK_FLOAT : TOK_NUMBER;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000837 } else if (isspace(*p)) {
838 type = TOK_WHITESPACE;
839 p++;
840 while (*p && isspace(*p))
841 p++;
842 /*
843 * Whitespace just before end-of-line is discarded by
844 * pretending it's a comment; whitespace just before a
845 * comment gets lumped into the comment.
846 */
847 if (!*p || *p == ';') {
848 type = TOK_COMMENT;
849 while (*p)
850 p++;
851 }
852 } else if (*p == ';') {
853 type = TOK_COMMENT;
854 while (*p)
855 p++;
856 } else {
857 /*
858 * Anything else is an operator of some kind. We check
859 * for all the double-character operators (>>, <<, //,
860 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000861 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000862 */
863 type = TOK_OTHER;
864 if ((p[0] == '>' && p[1] == '>') ||
865 (p[0] == '<' && p[1] == '<') ||
866 (p[0] == '/' && p[1] == '/') ||
867 (p[0] == '<' && p[1] == '=') ||
868 (p[0] == '>' && p[1] == '=') ||
869 (p[0] == '=' && p[1] == '=') ||
870 (p[0] == '!' && p[1] == '=') ||
871 (p[0] == '<' && p[1] == '>') ||
872 (p[0] == '&' && p[1] == '&') ||
873 (p[0] == '|' && p[1] == '|') ||
874 (p[0] == '^' && p[1] == '^')) {
875 p++;
876 }
877 p++;
878 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000879
H. Peter Anvine2c80182005-01-15 22:15:51 +0000880 /* Handling unterminated string by UNV */
881 /*if (type == -1)
882 {
883 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
884 t->text[p-line] = *line;
885 tail = &t->next;
886 }
887 else */
888 if (type != TOK_COMMENT) {
889 *tail = t = new_Token(NULL, type, line, p - line);
890 tail = &t->next;
891 }
892 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000893 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000894 return list;
895}
896
H. Peter Anvince616072002-04-30 21:02:23 +0000897/*
898 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -0700899 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +0000900 * deleted only all at once by the delete_Blocks function.
901 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000902static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +0000903{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000904 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000905
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000906 /* first, get to the end of the linked list */
907 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000908 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000909 /* now allocate the requested chunk */
910 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000911
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000912 /* now allocate a new block for the next request */
913 b->next = nasm_malloc(sizeof(Blocks));
914 /* and initialize the contents of the new block */
915 b->next->next = NULL;
916 b->next->chunk = NULL;
917 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000918}
919
920/*
921 * this function deletes all managed blocks of memory
922 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000923static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +0000924{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000925 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +0000926
H. Peter Anvin70653092007-10-19 14:42:29 -0700927 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000928 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -0700929 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000930 * free it.
931 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000932 while (b) {
933 if (b->chunk)
934 nasm_free(b->chunk);
935 a = b;
936 b = b->next;
937 if (a != &blocks)
938 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000939 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940}
H. Peter Anvin734b1882002-04-30 21:01:08 +0000941
942/*
H. Peter Anvin70653092007-10-19 14:42:29 -0700943 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +0000944 * back to the caller. It sets the type and text elements, and
945 * also the mac and next elements to NULL.
946 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000947static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +0000948{
949 Token *t;
950 int i;
951
H. Peter Anvine2c80182005-01-15 22:15:51 +0000952 if (freeTokens == NULL) {
953 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
954 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
955 freeTokens[i].next = &freeTokens[i + 1];
956 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000957 }
958 t = freeTokens;
959 freeTokens = t->next;
960 t->next = next;
961 t->mac = NULL;
962 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000963 if (type == TOK_WHITESPACE || text == NULL) {
964 t->text = NULL;
965 } else {
966 if (txtlen == 0)
967 txtlen = strlen(text);
968 t->text = nasm_malloc(1 + txtlen);
969 strncpy(t->text, text, txtlen);
970 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +0000971 }
972 return t;
973}
974
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +0000976{
977 Token *next = t->next;
978 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +0000979 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000980 freeTokens = t;
981 return next;
982}
983
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000984/*
985 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000986 * If expand_locals is not zero, identifiers of the form "%$*xxx"
987 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000988 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000989static char *detoken(Token * tlist, int expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000990{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000991 Token *t;
992 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000993 char *line, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000994
995 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 for (t = tlist; t; t = t->next) {
997 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000998 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 nasm_free(t->text);
1000 if (p)
1001 t->text = nasm_strdup(p);
1002 else
1003 t->text = NULL;
1004 }
1005 /* Expand local macros here and not during preprocessing */
1006 if (expand_locals &&
1007 t->type == TOK_PREPROC_ID && t->text &&
1008 t->text[0] == '%' && t->text[1] == '$') {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001009 Context *ctx = get_ctx(t->text, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001011 char buffer[40];
1012 char *p, *q = t->text + 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001013
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014 q += strspn(q, "$");
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001015 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 p = nasm_strcat(buffer, q);
1017 nasm_free(t->text);
1018 t->text = p;
1019 }
1020 }
1021 if (t->type == TOK_WHITESPACE) {
1022 len++;
1023 } else if (t->text) {
1024 len += strlen(t->text);
1025 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001026 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001027 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001028 for (t = tlist; t; t = t->next) {
1029 if (t->type == TOK_WHITESPACE) {
1030 *p = ' ';
1031 p++;
1032 *p = '\0';
1033 } else if (t->text) {
1034 strcpy(p, t->text);
1035 p += strlen(p);
1036 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001037 }
1038 *p = '\0';
1039 return line;
1040}
1041
1042/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001043 * A scanner, suitable for use by the expression evaluator, which
1044 * operates on a line of Tokens. Expects a pointer to a pointer to
1045 * the first token in the line to be passed in as its private_data
1046 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001047 *
1048 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001049 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001050static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001051{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001052 Token **tlineptr = private_data;
1053 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001054 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001055
H. Peter Anvine2c80182005-01-15 22:15:51 +00001056 do {
1057 tline = *tlineptr;
1058 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001059 }
1060 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001061 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001062
1063 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001065
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001066 tokval->t_charptr = tline->text;
1067
H. Peter Anvin76690a12002-04-30 20:52:49 +00001068 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001069 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001070 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001072
H. Peter Anvine2c80182005-01-15 22:15:51 +00001073 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001074 p = tokval->t_charptr = tline->text;
1075 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001076 tokval->t_charptr++;
1077 return tokval->t_type = TOKEN_ID;
1078 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001079
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001080 for (r = p, s = ourcopy; *r; r++) {
1081 if (r > p+MAX_KEYWORD)
1082 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1083 *s++ = tolower(*r);
1084 }
1085 *s = '\0';
1086 /* right, so we have an identifier sitting in temp storage. now,
1087 * is it actually a register or instruction name, or what? */
1088 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001089 }
1090
H. Peter Anvine2c80182005-01-15 22:15:51 +00001091 if (tline->type == TOK_NUMBER) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001092 bool rn_error;
1093 tokval->t_integer = readnum(tline->text, &rn_error);
1094 if (rn_error)
1095 return tokval->t_type = TOKEN_ERRNUM; /* some malformation occurred */
1096 tokval->t_charptr = tline->text;
1097 return tokval->t_type = TOKEN_NUM;
1098 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001099
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001100 if (tline->type == TOK_FLOAT) {
1101 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001102 }
1103
H. Peter Anvine2c80182005-01-15 22:15:51 +00001104 if (tline->type == TOK_STRING) {
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001105 bool rn_warn;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001106 char q, *r;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001107 int l;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001108
H. Peter Anvine2c80182005-01-15 22:15:51 +00001109 r = tline->text;
1110 q = *r++;
1111 l = strlen(r);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001112
H. Peter Anvine2c80182005-01-15 22:15:51 +00001113 if (l == 0 || r[l - 1] != q)
1114 return tokval->t_type = TOKEN_ERRNUM;
1115 tokval->t_integer = readstrnum(r, l - 1, &rn_warn);
1116 if (rn_warn)
1117 error(ERR_WARNING | ERR_PASS1, "character constant too long");
1118 tokval->t_charptr = NULL;
1119 return tokval->t_type = TOKEN_NUM;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001120 }
1121
H. Peter Anvine2c80182005-01-15 22:15:51 +00001122 if (tline->type == TOK_OTHER) {
1123 if (!strcmp(tline->text, "<<"))
1124 return tokval->t_type = TOKEN_SHL;
1125 if (!strcmp(tline->text, ">>"))
1126 return tokval->t_type = TOKEN_SHR;
1127 if (!strcmp(tline->text, "//"))
1128 return tokval->t_type = TOKEN_SDIV;
1129 if (!strcmp(tline->text, "%%"))
1130 return tokval->t_type = TOKEN_SMOD;
1131 if (!strcmp(tline->text, "=="))
1132 return tokval->t_type = TOKEN_EQ;
1133 if (!strcmp(tline->text, "<>"))
1134 return tokval->t_type = TOKEN_NE;
1135 if (!strcmp(tline->text, "!="))
1136 return tokval->t_type = TOKEN_NE;
1137 if (!strcmp(tline->text, "<="))
1138 return tokval->t_type = TOKEN_LE;
1139 if (!strcmp(tline->text, ">="))
1140 return tokval->t_type = TOKEN_GE;
1141 if (!strcmp(tline->text, "&&"))
1142 return tokval->t_type = TOKEN_DBL_AND;
1143 if (!strcmp(tline->text, "^^"))
1144 return tokval->t_type = TOKEN_DBL_XOR;
1145 if (!strcmp(tline->text, "||"))
1146 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001147 }
1148
1149 /*
1150 * We have no other options: just return the first character of
1151 * the token text.
1152 */
1153 return tokval->t_type = tline->text[0];
1154}
1155
1156/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001157 * Compare a string to the name of an existing macro; this is a
1158 * simple wrapper which calls either strcmp or nasm_stricmp
1159 * depending on the value of the `casesense' parameter.
1160 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001161static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001162{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001163 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001164}
1165
1166/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001167 * Return the Context structure associated with a %$ token. Return
1168 * NULL, having _already_ reported an error condition, if the
1169 * context stack isn't deep enough for the supplied number of $
1170 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001171 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001172 * also scanned for such smacro, until it is found; if not -
1173 * only the context that directly results from the number of $'s
1174 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001175 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001176static Context *get_ctx(char *name, bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001177{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001178 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001179 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001180 int i;
1181
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001182 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001183 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001184
H. Peter Anvine2c80182005-01-15 22:15:51 +00001185 if (!cstk) {
1186 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1187 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001188 }
1189
H. Peter Anvine2c80182005-01-15 22:15:51 +00001190 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1191 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001192/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001193 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001194 if (!ctx) {
1195 error(ERR_NONFATAL, "`%s': context stack is only"
1196 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1197 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001198 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001199 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001200 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001201
H. Peter Anvine2c80182005-01-15 22:15:51 +00001202 do {
1203 /* Search for this smacro in found context */
1204 m = ctx->localmac;
1205 while (m) {
1206 if (!mstrcmp(m->name, name, m->casesense))
1207 return ctx;
1208 m = m->next;
1209 }
1210 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001211 }
1212 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001213 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001214}
1215
1216/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001217 * Open an include file. This routine must always return a valid
1218 * file pointer if it returns - it's responsible for throwing an
1219 * ERR_FATAL and bombing out completely if not. It should also try
1220 * the include path one by one until it finds the file or reaches
1221 * the end of the path.
1222 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001223static FILE *inc_fopen(char *file)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001224{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001225 FILE *fp;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001226 char *prefix = "", *combine;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001227 IncPath *ip = ipath;
H. Peter Anvin620515a2002-04-30 20:57:38 +00001228 static int namelen = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001229 int len = strlen(file);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001230
H. Peter Anvine2c80182005-01-15 22:15:51 +00001231 while (1) {
1232 combine = nasm_malloc(strlen(prefix) + len + 1);
1233 strcpy(combine, prefix);
1234 strcat(combine, file);
1235 fp = fopen(combine, "r");
1236 if (pass == 0 && fp) {
1237 namelen += strlen(combine) + 1;
1238 if (namelen > 62) {
1239 printf(" \\\n ");
1240 namelen = 2;
1241 }
1242 printf(" %s", combine);
1243 }
1244 nasm_free(combine);
1245 if (fp)
1246 return fp;
1247 if (!ip)
1248 break;
1249 prefix = ip->path;
1250 ip = ip->next;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001251
1252 if (!prefix) {
1253 /* -MG given and file not found */
1254 if (pass == 0) {
1255 namelen += strlen(file) + 1;
1256 if (namelen > 62) {
1257 printf(" \\\n ");
1258 namelen = 2;
1259 }
1260 printf(" %s", file);
1261 }
1262 return NULL;
1263 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001264 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001265
H. Peter Anvin734b1882002-04-30 21:01:08 +00001266 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001267 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001268}
1269
1270/*
H. Peter Anvin97a23472007-09-16 17:57:25 -07001271 * Search for a key in the hash index; adding it if necessary
1272 * (in which case we initialize the data pointer to NULL.)
1273 */
1274static void **
1275hash_findi_add(struct hash_table *hash, const char *str)
1276{
1277 struct hash_insert hi;
1278 void **r;
1279 char *strx;
1280
1281 r = hash_findi(hash, str, &hi);
1282 if (r)
1283 return r;
1284
1285 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
1286 return hash_add(&hi, strx, NULL);
1287}
1288
1289/*
1290 * Like hash_findi, but returns the data element rather than a pointer
1291 * to it. Used only when not adding a new element, hence no third
1292 * argument.
1293 */
1294static void *
1295hash_findix(struct hash_table *hash, const char *str)
1296{
1297 void **p;
1298
1299 p = hash_findi(hash, str, NULL);
1300 return p ? *p : NULL;
1301}
1302
1303/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001304 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001305 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001306 * return true if _any_ single-line macro of that name is defined.
1307 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001308 * `nparam' or no parameters is defined.
1309 *
1310 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001311 * defined, or nparam is -1, the address of the definition structure
1312 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001313 * is NULL, no action will be taken regarding its contents, and no
1314 * error will occur.
1315 *
1316 * Note that this is also called with nparam zero to resolve
1317 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001318 *
1319 * If you already know which context macro belongs to, you can pass
1320 * the context pointer as first parameter; if you won't but name begins
1321 * with %$ the context will be automatically computed. If all_contexts
1322 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001323 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001324static bool
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001325smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001326 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001327{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001328 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001329
H. Peter Anvin97a23472007-09-16 17:57:25 -07001330 if (ctx) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001331 m = ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001332 } else if (name[0] == '%' && name[1] == '$') {
1333 if (cstk)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001334 ctx = get_ctx(name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001336 return false; /* got to return _something_ */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001337 m = ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001338 } else {
1339 m = (SMacro *) hash_findix(smacros, name);
1340 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001341
H. Peter Anvine2c80182005-01-15 22:15:51 +00001342 while (m) {
1343 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001344 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001345 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001346 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001347 *defn = m;
1348 else
1349 *defn = NULL;
1350 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001351 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001352 }
1353 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001354 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001355
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001356 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001357}
1358
1359/*
1360 * Count and mark off the parameters in a multi-line macro call.
1361 * This is called both from within the multi-line macro expansion
1362 * code, and also to mark off the default parameters when provided
1363 * in a %macro definition line.
1364 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001366{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001367 int paramsize, brace;
1368
1369 *nparam = paramsize = 0;
1370 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001371 while (t) {
1372 if (*nparam >= paramsize) {
1373 paramsize += PARAM_DELTA;
1374 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1375 }
1376 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001377 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001378 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001379 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 (*params)[(*nparam)++] = t;
1381 while (tok_isnt_(t, brace ? "}" : ","))
1382 t = t->next;
1383 if (t) { /* got a comma/brace */
1384 t = t->next;
1385 if (brace) {
1386 /*
1387 * Now we've found the closing brace, look further
1388 * for the comma.
1389 */
1390 skip_white_(t);
1391 if (tok_isnt_(t, ",")) {
1392 error(ERR_NONFATAL,
1393 "braces do not enclose all of macro parameter");
1394 while (tok_isnt_(t, ","))
1395 t = t->next;
1396 }
1397 if (t)
1398 t = t->next; /* eat the comma */
1399 }
1400 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001401 }
1402}
1403
1404/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001405 * Determine whether one of the various `if' conditions is true or
1406 * not.
1407 *
1408 * We must free the tline we get passed.
1409 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001410static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001411{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001412 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001413 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001414 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001415 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001416 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001417 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001418
1419 origline = tline;
1420
H. Peter Anvine2c80182005-01-15 22:15:51 +00001421 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001422 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001423 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001424 while (cstk && tline) {
1425 skip_white_(tline);
1426 if (!tline || tline->type != TOK_ID) {
1427 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001428 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001429 free_tlist(origline);
1430 return -1;
1431 }
1432 if (!nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001433 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001434 tline = tline->next;
1435 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001436 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001437
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001438 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001439 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001440 while (tline) {
1441 skip_white_(tline);
1442 if (!tline || (tline->type != TOK_ID &&
1443 (tline->type != TOK_PREPROC_ID ||
1444 tline->text[1] != '$'))) {
1445 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001446 "`%s' expects macro identifiers", pp_directives[ct]);
1447 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001448 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001449 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001450 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001451 tline = tline->next;
1452 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001453 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001454
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001455 case PPC_IFIDN:
1456 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001457 tline = expand_smacro(tline);
1458 t = tt = tline;
1459 while (tok_isnt_(tt, ","))
1460 tt = tt->next;
1461 if (!tt) {
1462 error(ERR_NONFATAL,
1463 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001464 pp_directives[ct]);
1465 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001466 }
1467 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001468 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001469 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1470 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1471 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001472 pp_directives[ct]);
1473 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001474 }
1475 if (t->type == TOK_WHITESPACE) {
1476 t = t->next;
1477 continue;
1478 }
1479 if (tt->type == TOK_WHITESPACE) {
1480 tt = tt->next;
1481 continue;
1482 }
1483 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001484 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001485 break;
1486 }
1487 /* Unify surrounding quotes for strings */
1488 if (t->type == TOK_STRING) {
1489 tt->text[0] = t->text[0];
1490 tt->text[strlen(tt->text) - 1] = t->text[0];
1491 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001492 if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001493 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001494 break;
1495 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001496
H. Peter Anvine2c80182005-01-15 22:15:51 +00001497 t = t->next;
1498 tt = tt->next;
1499 }
1500 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001501 j = false; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001502 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001503
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001504 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001505 {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001506 bool found = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001507 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001508
H. Peter Anvine2c80182005-01-15 22:15:51 +00001509 tline = tline->next;
1510 skip_white_(tline);
1511 tline = expand_id(tline);
1512 if (!tok_type_(tline, TOK_ID)) {
1513 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001514 "`%s' expects a macro name", pp_directives[ct]);
1515 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001516 }
1517 searching.name = nasm_strdup(tline->text);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001518 searching.casesense = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001519 searching.plus = false;
1520 searching.nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001521 searching.in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001522 searching.rep_nest = NULL;
1523 searching.nparam_min = 0;
1524 searching.nparam_max = INT_MAX;
1525 tline = expand_smacro(tline->next);
1526 skip_white_(tline);
1527 if (!tline) {
1528 } else if (!tok_type_(tline, TOK_NUMBER)) {
1529 error(ERR_NONFATAL,
1530 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001531 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001532 } else {
1533 searching.nparam_min = searching.nparam_max =
1534 readnum(tline->text, &j);
1535 if (j)
1536 error(ERR_NONFATAL,
1537 "unable to parse parameter count `%s'",
1538 tline->text);
1539 }
1540 if (tline && tok_is_(tline->next, "-")) {
1541 tline = tline->next->next;
1542 if (tok_is_(tline, "*"))
1543 searching.nparam_max = INT_MAX;
1544 else if (!tok_type_(tline, TOK_NUMBER))
1545 error(ERR_NONFATAL,
1546 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001547 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001548 else {
1549 searching.nparam_max = readnum(tline->text, &j);
1550 if (j)
1551 error(ERR_NONFATAL,
1552 "unable to parse parameter count `%s'",
1553 tline->text);
1554 if (searching.nparam_min > searching.nparam_max)
1555 error(ERR_NONFATAL,
1556 "minimum parameter count exceeds maximum");
1557 }
1558 }
1559 if (tline && tok_is_(tline->next, "+")) {
1560 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001561 searching.plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001562 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07001563 mmac = (MMacro *) hash_findix(mmacros, searching.name);
1564 while (mmac) {
1565 if (!strcmp(mmac->name, searching.name) &&
1566 (mmac->nparam_min <= searching.nparam_max
1567 || searching.plus)
1568 && (searching.nparam_min <= mmac->nparam_max
1569 || mmac->plus)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001570 found = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001571 break;
1572 }
1573 mmac = mmac->next;
1574 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001575 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001576 j = found;
1577 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001578 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001579
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001580 case PPC_IFID:
1581 needtype = TOK_ID;
1582 goto iftype;
1583 case PPC_IFNUM:
1584 needtype = TOK_NUMBER;
1585 goto iftype;
1586 case PPC_IFSTR:
1587 needtype = TOK_STRING;
1588 goto iftype;
1589
1590 iftype:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001591 tline = expand_smacro(tline);
1592 t = tline;
1593 while (tok_type_(t, TOK_WHITESPACE))
1594 t = t->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001595 j = false; /* placate optimiser */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001596 if (t)
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001597 j = t->type == needtype;
1598 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001599
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001600 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001601 t = tline = expand_smacro(tline);
1602 tptr = &t;
1603 tokval.t_type = TOKEN_INVALID;
1604 evalresult = evaluate(ppscan, tptr, &tokval,
1605 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001606 if (!evalresult)
1607 return -1;
1608 if (tokval.t_type)
1609 error(ERR_WARNING,
1610 "trailing garbage after expression ignored");
1611 if (!is_simple(evalresult)) {
1612 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001613 "non-constant value given to `%s'", pp_directives[ct]);
1614 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001616 j = reloc_value(evalresult) != 0;
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001617 return j;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001618
H. Peter Anvine2c80182005-01-15 22:15:51 +00001619 default:
1620 error(ERR_FATAL,
1621 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001622 pp_directives[ct]);
1623 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001624 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001625
1626 free_tlist(origline);
1627 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001628
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001629fail:
1630 free_tlist(origline);
1631 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001632}
1633
1634/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001635 * Expand macros in a string. Used in %error and %include directives.
Keith Kaniosb7a89542007-04-12 02:40:54 +00001636 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001637 * The returned variable should ALWAYS be freed after usage.
1638 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001639void expand_macros_in_string(char **p)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001640{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001641 Token *line = tokenize(*p);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001642 line = expand_smacro(line);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001643 *p = detoken(line, false);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001644}
1645
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001646/*
1647 * Common code for defining an smacro
1648 */
1649static bool define_smacro(Context *ctx, char *mname, bool casesense,
1650 int nparam, Token *expansion)
1651{
1652 SMacro *smac, **smhead;
H. Peter Anvin70653092007-10-19 14:42:29 -07001653
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001654 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1655 if (!smac) {
1656 error(ERR_WARNING,
1657 "single-line macro `%s' defined both with and"
1658 " without parameters", mname);
1659
1660 /* Some instances of the old code considered this a failure,
1661 some others didn't. What is the right thing to do here? */
1662 free_tlist(expansion);
1663 return false; /* Failure */
1664 } else {
1665 /*
1666 * We're redefining, so we have to take over an
1667 * existing SMacro structure. This means freeing
1668 * what was already in it.
1669 */
1670 nasm_free(smac->name);
1671 free_tlist(smac->expansion);
1672 }
1673 } else {
1674 if (!ctx)
1675 smhead = (SMacro **) hash_findi_add(smacros, mname);
1676 else
1677 smhead = &ctx->localmac;
H. Peter Anvin70653092007-10-19 14:42:29 -07001678
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001679 smac = nasm_malloc(sizeof(SMacro));
1680 smac->next = *smhead;
1681 *smhead = smac;
1682 }
1683 smac->name = nasm_strdup(mname);
1684 smac->casesense = casesense;
1685 smac->nparam = nparam;
1686 smac->expansion = expansion;
1687 smac->in_progress = false;
1688 return true; /* Success */
1689}
1690
1691/*
1692 * Undefine an smacro
1693 */
1694static void undef_smacro(Context *ctx, const char *mname)
1695{
1696 SMacro **smhead, *s, **sp;
1697
1698 if (!ctx)
1699 smhead = (SMacro **) hash_findi(smacros, mname, NULL);
1700 else
1701 smhead = &ctx->localmac;
H. Peter Anvin70653092007-10-19 14:42:29 -07001702
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001703 if (smhead) {
1704 /*
1705 * We now have a macro name... go hunt for it.
1706 */
1707 sp = smhead;
1708 while ((s = *sp) != NULL) {
1709 if (!mstrcmp(s->name, mname, s->casesense)) {
1710 *sp = s->next;
1711 nasm_free(s->name);
1712 free_tlist(s->expansion);
1713 nasm_free(s);
1714 } else {
1715 sp = &s->next;
1716 }
1717 }
1718 }
1719}
1720
Ed Beroset3ab3f412002-06-11 03:31:49 +00001721/**
1722 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001723 * Find out if a line contains a preprocessor directive, and deal
1724 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07001725 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001726 * If a directive _is_ found, it is the responsibility of this routine
1727 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001728 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001729 * @param tline a pointer to the current tokeninzed line linked list
1730 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07001731 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001732 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001733static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001734{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001735 enum preproc_token i;
1736 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07001737 bool err;
1738 int nparam;
1739 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001740 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001741 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001742 int offset;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001743 char *p, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001744 Include *inc;
1745 Context *ctx;
1746 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001747 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001748 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1749 Line *l;
1750 struct tokenval tokval;
1751 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001752 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001753 int64_t count;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001754
1755 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001756
H. Peter Anvineba20a72002-04-30 20:53:55 +00001757 skip_white_(tline);
1758 if (!tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001759 (tline->text[1] == '%' || tline->text[1] == '$'
1760 || tline->text[1] == '!'))
1761 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001762
H. Peter Anvin4169a472007-09-12 01:29:43 +00001763 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001764
1765 /*
1766 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001767 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001768 * we should ignore all directives except for condition
1769 * directives.
1770 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001771 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001772 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1773 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001774 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001775
1776 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001777 * If we're defining a macro or reading a %rep block, we should
1778 * ignore all directives except for %macro/%imacro (which
1779 * generate an error), %endm/%endmacro, and (only if we're in a
H. Peter Anvineba20a72002-04-30 20:53:55 +00001780 * %rep block) %endrep. If we're in a %rep block, another %rep
1781 * causes an error, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001782 */
1783 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001784 i != PP_ENDMACRO && i != PP_ENDM &&
1785 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1786 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001787 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001788
H. Peter Anvin4169a472007-09-12 01:29:43 +00001789 switch (i) {
1790 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001791 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1792 tline->text);
1793 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001794
H. Peter Anvine2c80182005-01-15 22:15:51 +00001795 case PP_STACKSIZE:
1796 /* Directive to tell NASM what the default stack size is. The
1797 * default is for a 16-bit stack, and this can be overriden with
1798 * %stacksize large.
1799 * the following form:
1800 *
1801 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1802 */
1803 tline = tline->next;
1804 if (tline && tline->type == TOK_WHITESPACE)
1805 tline = tline->next;
1806 if (!tline || tline->type != TOK_ID) {
1807 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1808 free_tlist(origline);
1809 return DIRECTIVE_FOUND;
1810 }
1811 if (nasm_stricmp(tline->text, "flat") == 0) {
1812 /* All subsequent ARG directives are for a 32-bit stack */
1813 StackSize = 4;
1814 StackPointer = "ebp";
1815 ArgOffset = 8;
1816 LocalOffset = 4;
1817 } else if (nasm_stricmp(tline->text, "large") == 0) {
1818 /* All subsequent ARG directives are for a 16-bit stack,
1819 * far function call.
1820 */
1821 StackSize = 2;
1822 StackPointer = "bp";
1823 ArgOffset = 4;
1824 LocalOffset = 2;
1825 } else if (nasm_stricmp(tline->text, "small") == 0) {
1826 /* All subsequent ARG directives are for a 16-bit stack,
1827 * far function call. We don't support near functions.
1828 */
1829 StackSize = 2;
1830 StackPointer = "bp";
1831 ArgOffset = 6;
1832 LocalOffset = 2;
1833 } else {
1834 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1835 free_tlist(origline);
1836 return DIRECTIVE_FOUND;
1837 }
1838 free_tlist(origline);
1839 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001840
H. Peter Anvine2c80182005-01-15 22:15:51 +00001841 case PP_ARG:
1842 /* TASM like ARG directive to define arguments to functions, in
1843 * the following form:
1844 *
1845 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1846 */
1847 offset = ArgOffset;
1848 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001849 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001850 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001851
H. Peter Anvine2c80182005-01-15 22:15:51 +00001852 /* Find the argument name */
1853 tline = tline->next;
1854 if (tline && tline->type == TOK_WHITESPACE)
1855 tline = tline->next;
1856 if (!tline || tline->type != TOK_ID) {
1857 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1858 free_tlist(origline);
1859 return DIRECTIVE_FOUND;
1860 }
1861 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001862
H. Peter Anvine2c80182005-01-15 22:15:51 +00001863 /* Find the argument size type */
1864 tline = tline->next;
1865 if (!tline || tline->type != TOK_OTHER
1866 || tline->text[0] != ':') {
1867 error(ERR_NONFATAL,
1868 "Syntax error processing `%%arg' directive");
1869 free_tlist(origline);
1870 return DIRECTIVE_FOUND;
1871 }
1872 tline = tline->next;
1873 if (!tline || tline->type != TOK_ID) {
1874 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1875 free_tlist(origline);
1876 return DIRECTIVE_FOUND;
1877 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001878
H. Peter Anvine2c80182005-01-15 22:15:51 +00001879 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001880 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001881 tt = expand_smacro(tt);
1882 if (nasm_stricmp(tt->text, "byte") == 0) {
1883 size = MAX(StackSize, 1);
1884 } else if (nasm_stricmp(tt->text, "word") == 0) {
1885 size = MAX(StackSize, 2);
1886 } else if (nasm_stricmp(tt->text, "dword") == 0) {
1887 size = MAX(StackSize, 4);
1888 } else if (nasm_stricmp(tt->text, "qword") == 0) {
1889 size = MAX(StackSize, 8);
1890 } else if (nasm_stricmp(tt->text, "tword") == 0) {
1891 size = MAX(StackSize, 10);
1892 } else {
1893 error(ERR_NONFATAL,
1894 "Invalid size type for `%%arg' missing directive");
1895 free_tlist(tt);
1896 free_tlist(origline);
1897 return DIRECTIVE_FOUND;
1898 }
1899 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001900
H. Peter Anvine2c80182005-01-15 22:15:51 +00001901 /* Now define the macro for the argument */
1902 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1903 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001904 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001905 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001906
H. Peter Anvine2c80182005-01-15 22:15:51 +00001907 /* Move to the next argument in the list */
1908 tline = tline->next;
1909 if (tline && tline->type == TOK_WHITESPACE)
1910 tline = tline->next;
1911 }
1912 while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1913 free_tlist(origline);
1914 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001915
H. Peter Anvine2c80182005-01-15 22:15:51 +00001916 case PP_LOCAL:
1917 /* TASM like LOCAL directive to define local variables for a
1918 * function, in the following form:
1919 *
1920 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1921 *
1922 * The '= LocalSize' at the end is ignored by NASM, but is
1923 * required by TASM to define the local parameter size (and used
1924 * by the TASM macro package).
1925 */
1926 offset = LocalOffset;
1927 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001928 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001929 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001930
H. Peter Anvine2c80182005-01-15 22:15:51 +00001931 /* Find the argument name */
1932 tline = tline->next;
1933 if (tline && tline->type == TOK_WHITESPACE)
1934 tline = tline->next;
1935 if (!tline || tline->type != TOK_ID) {
1936 error(ERR_NONFATAL,
1937 "`%%local' missing argument parameter");
1938 free_tlist(origline);
1939 return DIRECTIVE_FOUND;
1940 }
1941 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001942
H. Peter Anvine2c80182005-01-15 22:15:51 +00001943 /* Find the argument size type */
1944 tline = tline->next;
1945 if (!tline || tline->type != TOK_OTHER
1946 || tline->text[0] != ':') {
1947 error(ERR_NONFATAL,
1948 "Syntax error processing `%%local' directive");
1949 free_tlist(origline);
1950 return DIRECTIVE_FOUND;
1951 }
1952 tline = tline->next;
1953 if (!tline || tline->type != TOK_ID) {
1954 error(ERR_NONFATAL,
1955 "`%%local' missing size type parameter");
1956 free_tlist(origline);
1957 return DIRECTIVE_FOUND;
1958 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001959
H. Peter Anvine2c80182005-01-15 22:15:51 +00001960 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001961 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001962 tt = expand_smacro(tt);
1963 if (nasm_stricmp(tt->text, "byte") == 0) {
1964 size = MAX(StackSize, 1);
1965 } else if (nasm_stricmp(tt->text, "word") == 0) {
1966 size = MAX(StackSize, 2);
1967 } else if (nasm_stricmp(tt->text, "dword") == 0) {
1968 size = MAX(StackSize, 4);
1969 } else if (nasm_stricmp(tt->text, "qword") == 0) {
1970 size = MAX(StackSize, 8);
1971 } else if (nasm_stricmp(tt->text, "tword") == 0) {
1972 size = MAX(StackSize, 10);
1973 } else {
1974 error(ERR_NONFATAL,
1975 "Invalid size type for `%%local' missing directive");
1976 free_tlist(tt);
1977 free_tlist(origline);
1978 return DIRECTIVE_FOUND;
1979 }
1980 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001981
H. Peter Anvine2c80182005-01-15 22:15:51 +00001982 /* Now define the macro for the argument */
1983 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
1984 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001985 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001986 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001987
H. Peter Anvine2c80182005-01-15 22:15:51 +00001988 /* Now define the assign to setup the enter_c macro correctly */
1989 snprintf(directive, sizeof(directive),
1990 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001991 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001992
H. Peter Anvine2c80182005-01-15 22:15:51 +00001993 /* Move to the next argument in the list */
1994 tline = tline->next;
1995 if (tline && tline->type == TOK_WHITESPACE)
1996 tline = tline->next;
1997 }
1998 while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1999 free_tlist(origline);
2000 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002001
H. Peter Anvine2c80182005-01-15 22:15:51 +00002002 case PP_CLEAR:
2003 if (tline->next)
2004 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002005 free_macros();
2006 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002007 free_tlist(origline);
2008 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002009
H. Peter Anvine2c80182005-01-15 22:15:51 +00002010 case PP_INCLUDE:
2011 tline = tline->next;
2012 skip_white_(tline);
2013 if (!tline || (tline->type != TOK_STRING &&
2014 tline->type != TOK_INTERNAL_STRING)) {
2015 error(ERR_NONFATAL, "`%%include' expects a file name");
2016 free_tlist(origline);
2017 return DIRECTIVE_FOUND; /* but we did _something_ */
2018 }
2019 if (tline->next)
2020 error(ERR_WARNING,
2021 "trailing garbage after `%%include' ignored");
2022 if (tline->type != TOK_INTERNAL_STRING) {
2023 p = tline->text + 1; /* point past the quote to the name */
2024 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2025 } else
2026 p = tline->text; /* internal_string is easier */
2027 expand_macros_in_string(&p);
2028 inc = nasm_malloc(sizeof(Include));
2029 inc->next = istk;
2030 inc->conds = NULL;
2031 inc->fp = inc_fopen(p);
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002032 if (!inc->fp && pass == 0) {
2033 /* -MG given but file not found */
2034 nasm_free(inc);
2035 } else {
2036 inc->fname = src_set_fname(p);
2037 inc->lineno = src_set_linnum(0);
2038 inc->lineinc = 1;
2039 inc->expansion = NULL;
2040 inc->mstk = NULL;
2041 istk = inc;
2042 list->uplevel(LIST_INCLUDE);
2043 }
2044 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002045 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002046
H. Peter Anvine2c80182005-01-15 22:15:51 +00002047 case PP_PUSH:
2048 tline = tline->next;
2049 skip_white_(tline);
2050 tline = expand_id(tline);
2051 if (!tok_type_(tline, TOK_ID)) {
2052 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2053 free_tlist(origline);
2054 return DIRECTIVE_FOUND; /* but we did _something_ */
2055 }
2056 if (tline->next)
2057 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2058 ctx = nasm_malloc(sizeof(Context));
2059 ctx->next = cstk;
2060 ctx->localmac = NULL;
2061 ctx->name = nasm_strdup(tline->text);
2062 ctx->number = unique++;
2063 cstk = ctx;
2064 free_tlist(origline);
2065 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002066
H. Peter Anvine2c80182005-01-15 22:15:51 +00002067 case PP_REPL:
2068 tline = tline->next;
2069 skip_white_(tline);
2070 tline = expand_id(tline);
2071 if (!tok_type_(tline, TOK_ID)) {
2072 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2073 free_tlist(origline);
2074 return DIRECTIVE_FOUND; /* but we did _something_ */
2075 }
2076 if (tline->next)
2077 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2078 if (!cstk)
2079 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2080 else {
2081 nasm_free(cstk->name);
2082 cstk->name = nasm_strdup(tline->text);
2083 }
2084 free_tlist(origline);
2085 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002086
H. Peter Anvine2c80182005-01-15 22:15:51 +00002087 case PP_POP:
2088 if (tline->next)
2089 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2090 if (!cstk)
2091 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2092 else
2093 ctx_pop();
2094 free_tlist(origline);
2095 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002096
H. Peter Anvine2c80182005-01-15 22:15:51 +00002097 case PP_ERROR:
2098 tline->next = expand_smacro(tline->next);
2099 tline = tline->next;
2100 skip_white_(tline);
2101 if (tok_type_(tline, TOK_STRING)) {
2102 p = tline->text + 1; /* point past the quote to the name */
2103 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2104 expand_macros_in_string(&p);
2105 error(ERR_NONFATAL, "%s", p);
2106 nasm_free(p);
2107 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002108 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002109 error(ERR_WARNING, "%s", p);
2110 nasm_free(p);
2111 }
2112 free_tlist(origline);
2113 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002114
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002115 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002116 if (istk->conds && !emitting(istk->conds->state))
2117 j = COND_NEVER;
2118 else {
2119 j = if_condition(tline->next, i);
2120 tline->next = NULL; /* it got freed */
2121 free_tlist(origline);
2122 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2123 }
2124 cond = nasm_malloc(sizeof(Cond));
2125 cond->next = istk->conds;
2126 cond->state = j;
2127 istk->conds = cond;
2128 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002129
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002130 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002131 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002132 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002133 if (emitting(istk->conds->state)
2134 || istk->conds->state == COND_NEVER)
2135 istk->conds->state = COND_NEVER;
2136 else {
2137 /*
2138 * IMPORTANT: In the case of %if, we will already have
2139 * called expand_mmac_params(); however, if we're
2140 * processing an %elif we must have been in a
2141 * non-emitting mode, which would have inhibited
2142 * the normal invocation of expand_mmac_params(). Therefore,
2143 * we have to do it explicitly here.
2144 */
2145 j = if_condition(expand_mmac_params(tline->next), i);
2146 tline->next = NULL; /* it got freed */
2147 free_tlist(origline);
2148 istk->conds->state =
2149 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2150 }
2151 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002152
H. Peter Anvine2c80182005-01-15 22:15:51 +00002153 case PP_ELSE:
2154 if (tline->next)
2155 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2156 if (!istk->conds)
2157 error(ERR_FATAL, "`%%else': no matching `%%if'");
2158 if (emitting(istk->conds->state)
2159 || istk->conds->state == COND_NEVER)
2160 istk->conds->state = COND_ELSE_FALSE;
2161 else
2162 istk->conds->state = COND_ELSE_TRUE;
2163 free_tlist(origline);
2164 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002165
H. Peter Anvine2c80182005-01-15 22:15:51 +00002166 case PP_ENDIF:
2167 if (tline->next)
2168 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2169 if (!istk->conds)
2170 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2171 cond = istk->conds;
2172 istk->conds = cond->next;
2173 nasm_free(cond);
2174 free_tlist(origline);
2175 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002176
H. Peter Anvine2c80182005-01-15 22:15:51 +00002177 case PP_MACRO:
2178 case PP_IMACRO:
2179 if (defining)
2180 error(ERR_FATAL,
2181 "`%%%smacro': already defining a macro",
2182 (i == PP_IMACRO ? "i" : ""));
2183 tline = tline->next;
2184 skip_white_(tline);
2185 tline = expand_id(tline);
2186 if (!tok_type_(tline, TOK_ID)) {
2187 error(ERR_NONFATAL,
2188 "`%%%smacro' expects a macro name",
2189 (i == PP_IMACRO ? "i" : ""));
2190 return DIRECTIVE_FOUND;
2191 }
2192 defining = nasm_malloc(sizeof(MMacro));
2193 defining->name = nasm_strdup(tline->text);
2194 defining->casesense = (i == PP_MACRO);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002195 defining->plus = false;
2196 defining->nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002197 defining->in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002198 defining->rep_nest = NULL;
2199 tline = expand_smacro(tline->next);
2200 skip_white_(tline);
2201 if (!tok_type_(tline, TOK_NUMBER)) {
2202 error(ERR_NONFATAL,
2203 "`%%%smacro' expects a parameter count",
2204 (i == PP_IMACRO ? "i" : ""));
2205 defining->nparam_min = defining->nparam_max = 0;
2206 } else {
2207 defining->nparam_min = defining->nparam_max =
H. Peter Anvin70055962007-10-11 00:05:31 -07002208 readnum(tline->text, &err);
2209 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002210 error(ERR_NONFATAL,
2211 "unable to parse parameter count `%s'", tline->text);
2212 }
2213 if (tline && tok_is_(tline->next, "-")) {
2214 tline = tline->next->next;
2215 if (tok_is_(tline, "*"))
2216 defining->nparam_max = INT_MAX;
2217 else if (!tok_type_(tline, TOK_NUMBER))
2218 error(ERR_NONFATAL,
2219 "`%%%smacro' expects a parameter count after `-'",
2220 (i == PP_IMACRO ? "i" : ""));
2221 else {
H. Peter Anvin70055962007-10-11 00:05:31 -07002222 defining->nparam_max = readnum(tline->text, &err);
2223 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002224 error(ERR_NONFATAL,
2225 "unable to parse parameter count `%s'",
2226 tline->text);
2227 if (defining->nparam_min > defining->nparam_max)
2228 error(ERR_NONFATAL,
2229 "minimum parameter count exceeds maximum");
2230 }
2231 }
2232 if (tline && tok_is_(tline->next, "+")) {
2233 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002234 defining->plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002235 }
2236 if (tline && tok_type_(tline->next, TOK_ID) &&
2237 !nasm_stricmp(tline->next->text, ".nolist")) {
2238 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002239 defining->nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002240 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07002241 mmac = (MMacro *) hash_findix(mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002242 while (mmac) {
2243 if (!strcmp(mmac->name, defining->name) &&
2244 (mmac->nparam_min <= defining->nparam_max
2245 || defining->plus)
2246 && (defining->nparam_min <= mmac->nparam_max
2247 || mmac->plus)) {
2248 error(ERR_WARNING,
2249 "redefining multi-line macro `%s'", defining->name);
2250 break;
2251 }
2252 mmac = mmac->next;
2253 }
2254 /*
2255 * Handle default parameters.
2256 */
2257 if (tline && tline->next) {
2258 defining->dlist = tline->next;
2259 tline->next = NULL;
2260 count_mmac_params(defining->dlist, &defining->ndefs,
2261 &defining->defaults);
2262 } else {
2263 defining->dlist = NULL;
2264 defining->defaults = NULL;
2265 }
2266 defining->expansion = NULL;
2267 free_tlist(origline);
2268 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002269
H. Peter Anvine2c80182005-01-15 22:15:51 +00002270 case PP_ENDM:
2271 case PP_ENDMACRO:
2272 if (!defining) {
2273 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2274 return DIRECTIVE_FOUND;
2275 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07002276 mmhead = (MMacro **) hash_findi_add(mmacros, defining->name);
2277 defining->next = *mmhead;
2278 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002279 defining = NULL;
2280 free_tlist(origline);
2281 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002282
H. Peter Anvine2c80182005-01-15 22:15:51 +00002283 case PP_ROTATE:
2284 if (tline->next && tline->next->type == TOK_WHITESPACE)
2285 tline = tline->next;
2286 if (tline->next == NULL) {
2287 free_tlist(origline);
2288 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2289 return DIRECTIVE_FOUND;
2290 }
2291 t = expand_smacro(tline->next);
2292 tline->next = NULL;
2293 free_tlist(origline);
2294 tline = t;
2295 tptr = &t;
2296 tokval.t_type = TOKEN_INVALID;
2297 evalresult =
2298 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2299 free_tlist(tline);
2300 if (!evalresult)
2301 return DIRECTIVE_FOUND;
2302 if (tokval.t_type)
2303 error(ERR_WARNING,
2304 "trailing garbage after expression ignored");
2305 if (!is_simple(evalresult)) {
2306 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2307 return DIRECTIVE_FOUND;
2308 }
2309 mmac = istk->mstk;
2310 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2311 mmac = mmac->next_active;
2312 if (!mmac) {
2313 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2314 } else if (mmac->nparam == 0) {
2315 error(ERR_NONFATAL,
2316 "`%%rotate' invoked within macro without parameters");
2317 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002318 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002319
H. Peter Anvin25a99342007-09-22 17:45:45 -07002320 rotate %= (int)mmac->nparam;
2321 if (rotate < 0)
2322 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002323
H. Peter Anvin25a99342007-09-22 17:45:45 -07002324 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002325 }
2326 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002327
H. Peter Anvine2c80182005-01-15 22:15:51 +00002328 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002329 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002330 do {
2331 tline = tline->next;
2332 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002333
H. Peter Anvine2c80182005-01-15 22:15:51 +00002334 if (tok_type_(tline, TOK_ID) &&
2335 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002336 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002337 do {
2338 tline = tline->next;
2339 } while (tok_type_(tline, TOK_WHITESPACE));
2340 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002341
H. Peter Anvine2c80182005-01-15 22:15:51 +00002342 if (tline) {
2343 t = expand_smacro(tline);
2344 tptr = &t;
2345 tokval.t_type = TOKEN_INVALID;
2346 evalresult =
2347 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2348 if (!evalresult) {
2349 free_tlist(origline);
2350 return DIRECTIVE_FOUND;
2351 }
2352 if (tokval.t_type)
2353 error(ERR_WARNING,
2354 "trailing garbage after expression ignored");
2355 if (!is_simple(evalresult)) {
2356 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2357 return DIRECTIVE_FOUND;
2358 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002359 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002360 } else {
2361 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002362 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002363 }
2364 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002365
H. Peter Anvine2c80182005-01-15 22:15:51 +00002366 tmp_defining = defining;
2367 defining = nasm_malloc(sizeof(MMacro));
2368 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002369 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002370 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002371 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002372 defining->in_progress = count;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002373 defining->nparam_min = defining->nparam_max = 0;
2374 defining->defaults = NULL;
2375 defining->dlist = NULL;
2376 defining->expansion = NULL;
2377 defining->next_active = istk->mstk;
2378 defining->rep_nest = tmp_defining;
2379 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002380
H. Peter Anvine2c80182005-01-15 22:15:51 +00002381 case PP_ENDREP:
2382 if (!defining || defining->name) {
2383 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2384 return DIRECTIVE_FOUND;
2385 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002386
H. Peter Anvine2c80182005-01-15 22:15:51 +00002387 /*
2388 * Now we have a "macro" defined - although it has no name
2389 * and we won't be entering it in the hash tables - we must
2390 * push a macro-end marker for it on to istk->expansion.
2391 * After that, it will take care of propagating itself (a
2392 * macro-end marker line for a macro which is really a %rep
2393 * block will cause the macro to be re-expanded, complete
2394 * with another macro-end marker to ensure the process
2395 * continues) until the whole expansion is forcibly removed
2396 * from istk->expansion by a %exitrep.
2397 */
2398 l = nasm_malloc(sizeof(Line));
2399 l->next = istk->expansion;
2400 l->finishes = defining;
2401 l->first = NULL;
2402 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002403
H. Peter Anvine2c80182005-01-15 22:15:51 +00002404 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002405
H. Peter Anvine2c80182005-01-15 22:15:51 +00002406 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2407 tmp_defining = defining;
2408 defining = defining->rep_nest;
2409 free_tlist(origline);
2410 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002411
H. Peter Anvine2c80182005-01-15 22:15:51 +00002412 case PP_EXITREP:
2413 /*
2414 * We must search along istk->expansion until we hit a
2415 * macro-end marker for a macro with no name. Then we set
2416 * its `in_progress' flag to 0.
2417 */
2418 for (l = istk->expansion; l; l = l->next)
2419 if (l->finishes && !l->finishes->name)
2420 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002421
H. Peter Anvine2c80182005-01-15 22:15:51 +00002422 if (l)
2423 l->finishes->in_progress = 0;
2424 else
2425 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2426 free_tlist(origline);
2427 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002428
H. Peter Anvine2c80182005-01-15 22:15:51 +00002429 case PP_XDEFINE:
2430 case PP_IXDEFINE:
2431 case PP_DEFINE:
2432 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002433 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002434
H. Peter Anvine2c80182005-01-15 22:15:51 +00002435 tline = tline->next;
2436 skip_white_(tline);
2437 tline = expand_id(tline);
2438 if (!tline || (tline->type != TOK_ID &&
2439 (tline->type != TOK_PREPROC_ID ||
2440 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002441 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2442 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002443 free_tlist(origline);
2444 return DIRECTIVE_FOUND;
2445 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002446
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002447 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002448
H. Peter Anvine2c80182005-01-15 22:15:51 +00002449 mname = tline->text;
2450 last = tline;
2451 param_start = tline = tline->next;
2452 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002453
H. Peter Anvine2c80182005-01-15 22:15:51 +00002454 /* Expand the macro definition now for %xdefine and %ixdefine */
2455 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2456 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002457
H. Peter Anvine2c80182005-01-15 22:15:51 +00002458 if (tok_is_(tline, "(")) {
2459 /*
2460 * This macro has parameters.
2461 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002462
H. Peter Anvine2c80182005-01-15 22:15:51 +00002463 tline = tline->next;
2464 while (1) {
2465 skip_white_(tline);
2466 if (!tline) {
2467 error(ERR_NONFATAL, "parameter identifier expected");
2468 free_tlist(origline);
2469 return DIRECTIVE_FOUND;
2470 }
2471 if (tline->type != TOK_ID) {
2472 error(ERR_NONFATAL,
2473 "`%s': parameter identifier expected",
2474 tline->text);
2475 free_tlist(origline);
2476 return DIRECTIVE_FOUND;
2477 }
2478 tline->type = TOK_SMAC_PARAM + nparam++;
2479 tline = tline->next;
2480 skip_white_(tline);
2481 if (tok_is_(tline, ",")) {
2482 tline = tline->next;
2483 continue;
2484 }
2485 if (!tok_is_(tline, ")")) {
2486 error(ERR_NONFATAL,
2487 "`)' expected to terminate macro template");
2488 free_tlist(origline);
2489 return DIRECTIVE_FOUND;
2490 }
2491 break;
2492 }
2493 last = tline;
2494 tline = tline->next;
2495 }
2496 if (tok_type_(tline, TOK_WHITESPACE))
2497 last = tline, tline = tline->next;
2498 macro_start = NULL;
2499 last->next = NULL;
2500 t = tline;
2501 while (t) {
2502 if (t->type == TOK_ID) {
2503 for (tt = param_start; tt; tt = tt->next)
2504 if (tt->type >= TOK_SMAC_PARAM &&
2505 !strcmp(tt->text, t->text))
2506 t->type = tt->type;
2507 }
2508 tt = t->next;
2509 t->next = macro_start;
2510 macro_start = t;
2511 t = tt;
2512 }
2513 /*
2514 * Good. We now have a macro name, a parameter count, and a
2515 * token list (in reverse order) for an expansion. We ought
2516 * to be OK just to create an SMacro, store it, and let
2517 * free_tlist have the rest of the line (which we have
2518 * carefully re-terminated after chopping off the expansion
2519 * from the end).
2520 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002521 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002522 free_tlist(origline);
2523 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002524
H. Peter Anvine2c80182005-01-15 22:15:51 +00002525 case PP_UNDEF:
2526 tline = tline->next;
2527 skip_white_(tline);
2528 tline = expand_id(tline);
2529 if (!tline || (tline->type != TOK_ID &&
2530 (tline->type != TOK_PREPROC_ID ||
2531 tline->text[1] != '$'))) {
2532 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2533 free_tlist(origline);
2534 return DIRECTIVE_FOUND;
2535 }
2536 if (tline->next) {
2537 error(ERR_WARNING,
2538 "trailing garbage after macro name ignored");
2539 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002540
H. Peter Anvine2c80182005-01-15 22:15:51 +00002541 /* Find the context that symbol belongs to */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002542 ctx = get_ctx(tline->text, false);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002543 undef_smacro(ctx, tline->text);
2544 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002545 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002546
H. Peter Anvine2c80182005-01-15 22:15:51 +00002547 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002548 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07002549
H. Peter Anvine2c80182005-01-15 22:15:51 +00002550 tline = tline->next;
2551 skip_white_(tline);
2552 tline = expand_id(tline);
2553 if (!tline || (tline->type != TOK_ID &&
2554 (tline->type != TOK_PREPROC_ID ||
2555 tline->text[1] != '$'))) {
2556 error(ERR_NONFATAL,
2557 "`%%strlen' expects a macro identifier as first parameter");
2558 free_tlist(origline);
2559 return DIRECTIVE_FOUND;
2560 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002561 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002562
H. Peter Anvine2c80182005-01-15 22:15:51 +00002563 mname = tline->text;
2564 last = tline;
2565 tline = expand_smacro(tline->next);
2566 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002567
H. Peter Anvine2c80182005-01-15 22:15:51 +00002568 t = tline;
2569 while (tok_type_(t, TOK_WHITESPACE))
2570 t = t->next;
2571 /* t should now point to the string */
2572 if (t->type != TOK_STRING) {
2573 error(ERR_NONFATAL,
2574 "`%%strlen` requires string as second parameter");
2575 free_tlist(tline);
2576 free_tlist(origline);
2577 return DIRECTIVE_FOUND;
2578 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002579
H. Peter Anvine2c80182005-01-15 22:15:51 +00002580 macro_start = nasm_malloc(sizeof(*macro_start));
2581 macro_start->next = NULL;
2582 make_tok_num(macro_start, strlen(t->text) - 2);
2583 macro_start->mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002584
H. Peter Anvine2c80182005-01-15 22:15:51 +00002585 /*
2586 * We now have a macro name, an implicit parameter count of
2587 * zero, and a numeric token to use as an expansion. Create
2588 * and store an SMacro.
2589 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002590 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002591 free_tlist(tline);
2592 free_tlist(origline);
2593 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002594
H. Peter Anvine2c80182005-01-15 22:15:51 +00002595 case PP_SUBSTR:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002596 casesense = true;
2597
H. Peter Anvine2c80182005-01-15 22:15:51 +00002598 tline = tline->next;
2599 skip_white_(tline);
2600 tline = expand_id(tline);
2601 if (!tline || (tline->type != TOK_ID &&
2602 (tline->type != TOK_PREPROC_ID ||
2603 tline->text[1] != '$'))) {
2604 error(ERR_NONFATAL,
2605 "`%%substr' expects a macro identifier as first parameter");
2606 free_tlist(origline);
2607 return DIRECTIVE_FOUND;
2608 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002609 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002610
H. Peter Anvine2c80182005-01-15 22:15:51 +00002611 mname = tline->text;
2612 last = tline;
2613 tline = expand_smacro(tline->next);
2614 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002615
H. Peter Anvine2c80182005-01-15 22:15:51 +00002616 t = tline->next;
2617 while (tok_type_(t, TOK_WHITESPACE))
2618 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002619
H. Peter Anvine2c80182005-01-15 22:15:51 +00002620 /* t should now point to the string */
2621 if (t->type != TOK_STRING) {
2622 error(ERR_NONFATAL,
2623 "`%%substr` requires string as second parameter");
2624 free_tlist(tline);
2625 free_tlist(origline);
2626 return DIRECTIVE_FOUND;
2627 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002628
H. Peter Anvine2c80182005-01-15 22:15:51 +00002629 tt = t->next;
2630 tptr = &tt;
2631 tokval.t_type = TOKEN_INVALID;
2632 evalresult =
2633 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2634 if (!evalresult) {
2635 free_tlist(tline);
2636 free_tlist(origline);
2637 return DIRECTIVE_FOUND;
2638 }
2639 if (!is_simple(evalresult)) {
2640 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2641 free_tlist(tline);
2642 free_tlist(origline);
2643 return DIRECTIVE_FOUND;
2644 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002645
H. Peter Anvine2c80182005-01-15 22:15:51 +00002646 macro_start = nasm_malloc(sizeof(*macro_start));
2647 macro_start->next = NULL;
2648 macro_start->text = nasm_strdup("'''");
2649 if (evalresult->value > 0
Charles Crayne192d5b52007-10-18 19:02:42 -07002650 && evalresult->value < (int) strlen(t->text) - 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002651 macro_start->text[1] = t->text[evalresult->value];
2652 } else {
2653 macro_start->text[2] = '\0';
2654 }
2655 macro_start->type = TOK_STRING;
2656 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002657
H. Peter Anvine2c80182005-01-15 22:15:51 +00002658 /*
2659 * We now have a macro name, an implicit parameter count of
2660 * zero, and a numeric token to use as an expansion. Create
2661 * and store an SMacro.
2662 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002663 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002664 free_tlist(tline);
2665 free_tlist(origline);
2666 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002667
H. Peter Anvine2c80182005-01-15 22:15:51 +00002668 case PP_ASSIGN:
2669 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002670 casesense = (i == PP_ASSIGN);
2671
H. Peter Anvine2c80182005-01-15 22:15:51 +00002672 tline = tline->next;
2673 skip_white_(tline);
2674 tline = expand_id(tline);
2675 if (!tline || (tline->type != TOK_ID &&
2676 (tline->type != TOK_PREPROC_ID ||
2677 tline->text[1] != '$'))) {
2678 error(ERR_NONFATAL,
2679 "`%%%sassign' expects a macro identifier",
2680 (i == PP_IASSIGN ? "i" : ""));
2681 free_tlist(origline);
2682 return DIRECTIVE_FOUND;
2683 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002684 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002685
H. Peter Anvine2c80182005-01-15 22:15:51 +00002686 mname = tline->text;
2687 last = tline;
2688 tline = expand_smacro(tline->next);
2689 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002690
H. Peter Anvine2c80182005-01-15 22:15:51 +00002691 t = tline;
2692 tptr = &t;
2693 tokval.t_type = TOKEN_INVALID;
2694 evalresult =
2695 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2696 free_tlist(tline);
2697 if (!evalresult) {
2698 free_tlist(origline);
2699 return DIRECTIVE_FOUND;
2700 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002701
H. Peter Anvine2c80182005-01-15 22:15:51 +00002702 if (tokval.t_type)
2703 error(ERR_WARNING,
2704 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00002705
H. Peter Anvine2c80182005-01-15 22:15:51 +00002706 if (!is_simple(evalresult)) {
2707 error(ERR_NONFATAL,
2708 "non-constant value given to `%%%sassign'",
2709 (i == PP_IASSIGN ? "i" : ""));
2710 free_tlist(origline);
2711 return DIRECTIVE_FOUND;
2712 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002713
H. Peter Anvine2c80182005-01-15 22:15:51 +00002714 macro_start = nasm_malloc(sizeof(*macro_start));
2715 macro_start->next = NULL;
2716 make_tok_num(macro_start, reloc_value(evalresult));
2717 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002718
H. Peter Anvine2c80182005-01-15 22:15:51 +00002719 /*
2720 * We now have a macro name, an implicit parameter count of
2721 * zero, and a numeric token to use as an expansion. Create
2722 * and store an SMacro.
2723 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002724 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002725 free_tlist(origline);
2726 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002727
H. Peter Anvine2c80182005-01-15 22:15:51 +00002728 case PP_LINE:
2729 /*
2730 * Syntax is `%line nnn[+mmm] [filename]'
2731 */
2732 tline = tline->next;
2733 skip_white_(tline);
2734 if (!tok_type_(tline, TOK_NUMBER)) {
2735 error(ERR_NONFATAL, "`%%line' expects line number");
2736 free_tlist(origline);
2737 return DIRECTIVE_FOUND;
2738 }
H. Peter Anvin70055962007-10-11 00:05:31 -07002739 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002740 m = 1;
2741 tline = tline->next;
2742 if (tok_is_(tline, "+")) {
2743 tline = tline->next;
2744 if (!tok_type_(tline, TOK_NUMBER)) {
2745 error(ERR_NONFATAL, "`%%line' expects line increment");
2746 free_tlist(origline);
2747 return DIRECTIVE_FOUND;
2748 }
H. Peter Anvin70055962007-10-11 00:05:31 -07002749 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002750 tline = tline->next;
2751 }
2752 skip_white_(tline);
2753 src_set_linnum(k);
2754 istk->lineinc = m;
2755 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002756 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002757 }
2758 free_tlist(origline);
2759 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002760
H. Peter Anvine2c80182005-01-15 22:15:51 +00002761 default:
2762 error(ERR_FATAL,
2763 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00002764 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002765 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002766 }
Ed Beroset3ab3f412002-06-11 03:31:49 +00002767 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002768}
2769
2770/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002771 * Ensure that a macro parameter contains a condition code and
2772 * nothing else. Return the condition code index if so, or -1
2773 * otherwise.
2774 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002775static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002776{
H. Peter Anvin76690a12002-04-30 20:52:49 +00002777 Token *tt;
2778 int i, j, k, m;
2779
H. Peter Anvin25a99342007-09-22 17:45:45 -07002780 if (!t)
2781 return -1; /* Probably a %+ without a space */
2782
H. Peter Anvineba20a72002-04-30 20:53:55 +00002783 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002784 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002785 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002786 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002787 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002788 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002789 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002790
2791 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00002792 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002793 while (j - i > 1) {
2794 k = (j + i) / 2;
2795 m = nasm_stricmp(t->text, conditions[k]);
2796 if (m == 0) {
2797 i = k;
2798 j = -2;
2799 break;
2800 } else if (m < 0) {
2801 j = k;
2802 } else
2803 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002804 }
2805 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002806 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002807 return i;
2808}
2809
2810/*
2811 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2812 * %-n) and MMacro-local identifiers (%%foo).
2813 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002814static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002815{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002816 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002817
2818 tail = &thead;
2819 thead = NULL;
2820
H. Peter Anvine2c80182005-01-15 22:15:51 +00002821 while (tline) {
2822 if (tline->type == TOK_PREPROC_ID &&
2823 (((tline->text[1] == '+' || tline->text[1] == '-')
2824 && tline->text[2]) || tline->text[1] == '%'
2825 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002826 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002827 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002828 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07002829 unsigned int n;
2830 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002831 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002832
H. Peter Anvine2c80182005-01-15 22:15:51 +00002833 t = tline;
2834 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002835
H. Peter Anvine2c80182005-01-15 22:15:51 +00002836 mac = istk->mstk;
2837 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2838 mac = mac->next_active;
2839 if (!mac)
2840 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2841 else
2842 switch (t->text[1]) {
2843 /*
2844 * We have to make a substitution of one of the
2845 * forms %1, %-1, %+1, %%foo, %0.
2846 */
2847 case '0':
2848 type = TOK_NUMBER;
2849 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
2850 text = nasm_strdup(tmpbuf);
2851 break;
2852 case '%':
2853 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002854 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002855 mac->unique);
2856 text = nasm_strcat(tmpbuf, t->text + 2);
2857 break;
2858 case '-':
2859 n = atoi(t->text + 2) - 1;
2860 if (n >= mac->nparam)
2861 tt = NULL;
2862 else {
2863 if (mac->nparam > 1)
2864 n = (n + mac->rotate) % mac->nparam;
2865 tt = mac->params[n];
2866 }
2867 cc = find_cc(tt);
2868 if (cc == -1) {
2869 error(ERR_NONFATAL,
2870 "macro parameter %d is not a condition code",
2871 n + 1);
2872 text = NULL;
2873 } else {
2874 type = TOK_ID;
2875 if (inverse_ccs[cc] == -1) {
2876 error(ERR_NONFATAL,
2877 "condition code `%s' is not invertible",
2878 conditions[cc]);
2879 text = NULL;
2880 } else
2881 text =
2882 nasm_strdup(conditions[inverse_ccs[cc]]);
2883 }
2884 break;
2885 case '+':
2886 n = atoi(t->text + 2) - 1;
2887 if (n >= mac->nparam)
2888 tt = NULL;
2889 else {
2890 if (mac->nparam > 1)
2891 n = (n + mac->rotate) % mac->nparam;
2892 tt = mac->params[n];
2893 }
2894 cc = find_cc(tt);
2895 if (cc == -1) {
2896 error(ERR_NONFATAL,
2897 "macro parameter %d is not a condition code",
2898 n + 1);
2899 text = NULL;
2900 } else {
2901 type = TOK_ID;
2902 text = nasm_strdup(conditions[cc]);
2903 }
2904 break;
2905 default:
2906 n = atoi(t->text + 1) - 1;
2907 if (n >= mac->nparam)
2908 tt = NULL;
2909 else {
2910 if (mac->nparam > 1)
2911 n = (n + mac->rotate) % mac->nparam;
2912 tt = mac->params[n];
2913 }
2914 if (tt) {
2915 for (i = 0; i < mac->paramlen[n]; i++) {
2916 *tail = new_Token(NULL, tt->type, tt->text, 0);
2917 tail = &(*tail)->next;
2918 tt = tt->next;
2919 }
2920 }
2921 text = NULL; /* we've done it here */
2922 break;
2923 }
2924 if (!text) {
2925 delete_Token(t);
2926 } else {
2927 *tail = t;
2928 tail = &t->next;
2929 t->type = type;
2930 nasm_free(t->text);
2931 t->text = text;
2932 t->mac = NULL;
2933 }
2934 continue;
2935 } else {
2936 t = *tail = tline;
2937 tline = tline->next;
2938 t->mac = NULL;
2939 tail = &t->next;
2940 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002941 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00002942 *tail = NULL;
2943 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002944 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002945 switch (t->type) {
2946 case TOK_WHITESPACE:
2947 if (tt->type == TOK_WHITESPACE) {
2948 t->next = delete_Token(tt);
2949 }
2950 break;
2951 case TOK_ID:
2952 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002953 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002954 nasm_free(t->text);
2955 t->text = tmp;
2956 t->next = delete_Token(tt);
2957 }
2958 break;
2959 case TOK_NUMBER:
2960 if (tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002961 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002962 nasm_free(t->text);
2963 t->text = tmp;
2964 t->next = delete_Token(tt);
2965 }
2966 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002967 default:
2968 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002969 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002970
H. Peter Anvin76690a12002-04-30 20:52:49 +00002971 return thead;
2972}
2973
2974/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002975 * Expand all single-line macro calls made in the given line.
2976 * Return the expanded version of the line. The original is deemed
2977 * to be destroyed in the process. (In reality we'll just move
2978 * Tokens from input to output a lot of the time, rather than
2979 * actually bothering to destroy and replicate.)
2980 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002981static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002982{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002983 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002984 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002985 Token **params;
2986 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07002987 unsigned int nparam, sparam;
2988 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002989 Token *org_tline = tline;
2990 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002991 char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002992
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002993 /*
2994 * Trick: we should avoid changing the start token pointer since it can
2995 * be contained in "next" field of other token. Because of this
2996 * we allocate a copy of first token and work with it; at the end of
2997 * routine we copy it back
2998 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002999 if (org_tline) {
3000 tline =
3001 new_Token(org_tline->next, org_tline->type, org_tline->text,
3002 0);
3003 tline->mac = org_tline->mac;
3004 nasm_free(org_tline->text);
3005 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003006 }
3007
H. Peter Anvin734b1882002-04-30 21:01:08 +00003008 again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003009 tail = &thead;
3010 thead = NULL;
3011
H. Peter Anvine2c80182005-01-15 22:15:51 +00003012 while (tline) { /* main token loop */
3013 if ((mname = tline->text)) {
3014 /* if this token is a local macro, look in local context */
3015 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003016 ctx = get_ctx(mname, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003017 else
3018 ctx = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003019 if (!ctx) {
3020 head = (SMacro *) hash_findix(smacros, mname);
3021 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003022 head = ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003023 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003024 /*
3025 * We've hit an identifier. As in is_mmacro below, we first
3026 * check whether the identifier is a single-line macro at
3027 * all, then think about checking for parameters if
3028 * necessary.
3029 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003030 for (m = head; m; m = m->next)
3031 if (!mstrcmp(m->name, mname, m->casesense))
3032 break;
3033 if (m) {
3034 mstart = tline;
3035 params = NULL;
3036 paramsize = NULL;
3037 if (m->nparam == 0) {
3038 /*
3039 * Simple case: the macro is parameterless. Discard the
3040 * one token that the macro call took, and push the
3041 * expansion back on the to-do stack.
3042 */
3043 if (!m->expansion) {
3044 if (!strcmp("__FILE__", m->name)) {
3045 int32_t num = 0;
3046 src_get(&num, &(tline->text));
3047 nasm_quote(&(tline->text));
3048 tline->type = TOK_STRING;
3049 continue;
3050 }
3051 if (!strcmp("__LINE__", m->name)) {
3052 nasm_free(tline->text);
3053 make_tok_num(tline, src_get_linnum());
3054 continue;
3055 }
3056 if (!strcmp("__BITS__", m->name)) {
3057 nasm_free(tline->text);
3058 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003059 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003060 }
3061 tline = delete_Token(tline);
3062 continue;
3063 }
3064 } else {
3065 /*
3066 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003067 * exists and takes parameters. We must find the
3068 * parameters in the call, count them, find the SMacro
3069 * that corresponds to that form of the macro call, and
3070 * substitute for the parameters when we expand. What a
3071 * pain.
3072 */
3073 /*tline = tline->next;
3074 skip_white_(tline); */
3075 do {
3076 t = tline->next;
3077 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003078 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003079 t->text = NULL;
3080 t = tline->next = delete_Token(t);
3081 }
3082 tline = t;
3083 } while (tok_type_(tline, TOK_WHITESPACE));
3084 if (!tok_is_(tline, "(")) {
3085 /*
3086 * This macro wasn't called with parameters: ignore
3087 * the call. (Behaviour borrowed from gnu cpp.)
3088 */
3089 tline = mstart;
3090 m = NULL;
3091 } else {
3092 int paren = 0;
3093 int white = 0;
3094 brackets = 0;
3095 nparam = 0;
3096 sparam = PARAM_DELTA;
3097 params = nasm_malloc(sparam * sizeof(Token *));
3098 params[0] = tline->next;
3099 paramsize = nasm_malloc(sparam * sizeof(int));
3100 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003101 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003102 /*
3103 * For some unusual expansions
3104 * which concatenates function call
3105 */
3106 t = tline->next;
3107 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003108 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003109 t->text = NULL;
3110 t = tline->next = delete_Token(t);
3111 }
3112 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003113
H. Peter Anvine2c80182005-01-15 22:15:51 +00003114 if (!tline) {
3115 error(ERR_NONFATAL,
3116 "macro call expects terminating `)'");
3117 break;
3118 }
3119 if (tline->type == TOK_WHITESPACE
3120 && brackets <= 0) {
3121 if (paramsize[nparam])
3122 white++;
3123 else
3124 params[nparam] = tline->next;
3125 continue; /* parameter loop */
3126 }
3127 if (tline->type == TOK_OTHER
3128 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003129 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003130 if (ch == ',' && !paren && brackets <= 0) {
3131 if (++nparam >= sparam) {
3132 sparam += PARAM_DELTA;
3133 params = nasm_realloc(params,
3134 sparam *
3135 sizeof(Token
3136 *));
3137 paramsize =
3138 nasm_realloc(paramsize,
3139 sparam *
3140 sizeof(int));
3141 }
3142 params[nparam] = tline->next;
3143 paramsize[nparam] = 0;
3144 white = 0;
3145 continue; /* parameter loop */
3146 }
3147 if (ch == '{' &&
3148 (brackets > 0 || (brackets == 0 &&
3149 !paramsize[nparam])))
3150 {
3151 if (!(brackets++)) {
3152 params[nparam] = tline->next;
3153 continue; /* parameter loop */
3154 }
3155 }
3156 if (ch == '}' && brackets > 0)
3157 if (--brackets == 0) {
3158 brackets = -1;
3159 continue; /* parameter loop */
3160 }
3161 if (ch == '(' && !brackets)
3162 paren++;
3163 if (ch == ')' && brackets <= 0)
3164 if (--paren < 0)
3165 break;
3166 }
3167 if (brackets < 0) {
3168 brackets = 0;
3169 error(ERR_NONFATAL, "braces do not "
3170 "enclose all of macro parameter");
3171 }
3172 paramsize[nparam] += white + 1;
3173 white = 0;
3174 } /* parameter loop */
3175 nparam++;
3176 while (m && (m->nparam != nparam ||
3177 mstrcmp(m->name, mname,
3178 m->casesense)))
3179 m = m->next;
3180 if (!m)
3181 error(ERR_WARNING | ERR_WARN_MNP,
3182 "macro `%s' exists, "
3183 "but not taking %d parameters",
3184 mstart->text, nparam);
3185 }
3186 }
3187 if (m && m->in_progress)
3188 m = NULL;
3189 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003190 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003191 * Design question: should we handle !tline, which
3192 * indicates missing ')' here, or expand those
3193 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003194 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003195 */
3196 nasm_free(params);
3197 nasm_free(paramsize);
3198 tline = mstart;
3199 } else {
3200 /*
3201 * Expand the macro: we are placed on the last token of the
3202 * call, so that we can easily split the call from the
3203 * following tokens. We also start by pushing an SMAC_END
3204 * token for the cycle removal.
3205 */
3206 t = tline;
3207 if (t) {
3208 tline = t->next;
3209 t->next = NULL;
3210 }
3211 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3212 tt->mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003213 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003214 tline = tt;
3215 for (t = m->expansion; t; t = t->next) {
3216 if (t->type >= TOK_SMAC_PARAM) {
3217 Token *pcopy = tline, **ptail = &pcopy;
3218 Token *ttt, *pt;
3219 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003220
H. Peter Anvine2c80182005-01-15 22:15:51 +00003221 ttt = params[t->type - TOK_SMAC_PARAM];
3222 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3223 --i >= 0;) {
3224 pt = *ptail =
3225 new_Token(tline, ttt->type, ttt->text,
3226 0);
3227 ptail = &pt->next;
3228 ttt = ttt->next;
3229 }
3230 tline = pcopy;
3231 } else {
3232 tt = new_Token(tline, t->type, t->text, 0);
3233 tline = tt;
3234 }
3235 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003236
H. Peter Anvine2c80182005-01-15 22:15:51 +00003237 /*
3238 * Having done that, get rid of the macro call, and clean
3239 * up the parameters.
3240 */
3241 nasm_free(params);
3242 nasm_free(paramsize);
3243 free_tlist(mstart);
3244 continue; /* main token loop */
3245 }
3246 }
3247 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003248
H. Peter Anvine2c80182005-01-15 22:15:51 +00003249 if (tline->type == TOK_SMAC_END) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003250 tline->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003251 tline = delete_Token(tline);
3252 } else {
3253 t = *tail = tline;
3254 tline = tline->next;
3255 t->mac = NULL;
3256 t->next = NULL;
3257 tail = &t->next;
3258 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003259 }
3260
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003261 /*
3262 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003263 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003264 * TOK_IDs should be concatenated.
3265 * Also we look for %+ tokens and concatenate the tokens before and after
3266 * them (without white spaces in between).
3267 */
3268 t = thead;
3269 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003270 while (t) {
3271 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3272 t = t->next;
3273 if (!t || !t->next)
3274 break;
3275 if (t->next->type == TOK_ID ||
3276 t->next->type == TOK_PREPROC_ID ||
3277 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003278 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003279 nasm_free(t->text);
3280 t->next = delete_Token(t->next);
3281 t->text = p;
3282 rescan = 1;
3283 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3284 t->next->next->type == TOK_PREPROC_ID &&
3285 strcmp(t->next->next->text, "%+") == 0) {
3286 /* free the next whitespace, the %+ token and next whitespace */
3287 int i;
3288 for (i = 1; i <= 3; i++) {
3289 if (!t->next
3290 || (i != 2 && t->next->type != TOK_WHITESPACE))
3291 break;
3292 t->next = delete_Token(t->next);
3293 } /* endfor */
3294 } else
3295 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003296 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003297 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003298 if (rescan) {
3299 tline = thead;
3300 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003301 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003302
H. Peter Anvine2c80182005-01-15 22:15:51 +00003303 if (org_tline) {
3304 if (thead) {
3305 *org_tline = *thead;
3306 /* since we just gave text to org_line, don't free it */
3307 thead->text = NULL;
3308 delete_Token(thead);
3309 } else {
3310 /* the expression expanded to empty line;
3311 we can't return NULL for some reasons
3312 we just set the line to a single WHITESPACE token. */
3313 memset(org_tline, 0, sizeof(*org_tline));
3314 org_tline->text = NULL;
3315 org_tline->type = TOK_WHITESPACE;
3316 }
3317 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003318 }
3319
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003320 return thead;
3321}
3322
3323/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003324 * Similar to expand_smacro but used exclusively with macro identifiers
3325 * right before they are fetched in. The reason is that there can be
3326 * identifiers consisting of several subparts. We consider that if there
3327 * are more than one element forming the name, user wants a expansion,
3328 * otherwise it will be left as-is. Example:
3329 *
3330 * %define %$abc cde
3331 *
3332 * the identifier %$abc will be left as-is so that the handler for %define
3333 * will suck it and define the corresponding value. Other case:
3334 *
3335 * %define _%$abc cde
3336 *
3337 * In this case user wants name to be expanded *before* %define starts
3338 * working, so we'll expand %$abc into something (if it has a value;
3339 * otherwise it will be left as-is) then concatenate all successive
3340 * PP_IDs into one.
3341 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003342static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003343{
3344 Token *cur, *oldnext = NULL;
3345
H. Peter Anvin734b1882002-04-30 21:01:08 +00003346 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003347 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003348
3349 cur = tline;
3350 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003351 (cur->next->type == TOK_ID ||
3352 cur->next->type == TOK_PREPROC_ID
3353 || cur->next->type == TOK_NUMBER))
3354 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003355
3356 /* If identifier consists of just one token, don't expand */
3357 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003358 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003359
H. Peter Anvine2c80182005-01-15 22:15:51 +00003360 if (cur) {
3361 oldnext = cur->next; /* Detach the tail past identifier */
3362 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003363 }
3364
H. Peter Anvin734b1882002-04-30 21:01:08 +00003365 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003366
H. Peter Anvine2c80182005-01-15 22:15:51 +00003367 if (cur) {
3368 /* expand_smacro possibly changhed tline; re-scan for EOL */
3369 cur = tline;
3370 while (cur && cur->next)
3371 cur = cur->next;
3372 if (cur)
3373 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003374 }
3375
3376 return tline;
3377}
3378
3379/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003380 * Determine whether the given line constitutes a multi-line macro
3381 * call, and return the MMacro structure called if so. Doesn't have
3382 * to check for an initial label - that's taken care of in
3383 * expand_mmacro - but must check numbers of parameters. Guaranteed
3384 * to be called with tline->type == TOK_ID, so the putative macro
3385 * name is easy to find.
3386 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003387static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003388{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003389 MMacro *head, *m;
3390 Token **params;
3391 int nparam;
3392
H. Peter Anvin97a23472007-09-16 17:57:25 -07003393 head = (MMacro *) hash_findix(mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003394
3395 /*
3396 * Efficiency: first we see if any macro exists with the given
3397 * name. If not, we can return NULL immediately. _Then_ we
3398 * count the parameters, and then we look further along the
3399 * list if necessary to find the proper MMacro.
3400 */
3401 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003402 if (!mstrcmp(m->name, tline->text, m->casesense))
3403 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003404 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003405 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003406
3407 /*
3408 * OK, we have a potential macro. Count and demarcate the
3409 * parameters.
3410 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003411 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003412
3413 /*
3414 * So we know how many parameters we've got. Find the MMacro
3415 * structure that handles this number.
3416 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003417 while (m) {
3418 if (m->nparam_min <= nparam
3419 && (m->plus || nparam <= m->nparam_max)) {
3420 /*
3421 * This one is right. Just check if cycle removal
3422 * prohibits us using it before we actually celebrate...
3423 */
3424 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003425#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003426 error(ERR_NONFATAL,
3427 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003428#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003429 nasm_free(params);
3430 return NULL;
3431 }
3432 /*
3433 * It's right, and we can use it. Add its default
3434 * parameters to the end of our list if necessary.
3435 */
3436 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3437 params =
3438 nasm_realloc(params,
3439 ((m->nparam_min + m->ndefs +
3440 1) * sizeof(*params)));
3441 while (nparam < m->nparam_min + m->ndefs) {
3442 params[nparam] = m->defaults[nparam - m->nparam_min];
3443 nparam++;
3444 }
3445 }
3446 /*
3447 * If we've gone over the maximum parameter count (and
3448 * we're in Plus mode), ignore parameters beyond
3449 * nparam_max.
3450 */
3451 if (m->plus && nparam > m->nparam_max)
3452 nparam = m->nparam_max;
3453 /*
3454 * Then terminate the parameter list, and leave.
3455 */
3456 if (!params) { /* need this special case */
3457 params = nasm_malloc(sizeof(*params));
3458 nparam = 0;
3459 }
3460 params[nparam] = NULL;
3461 *params_array = params;
3462 return m;
3463 }
3464 /*
3465 * This one wasn't right: look for the next one with the
3466 * same name.
3467 */
3468 for (m = m->next; m; m = m->next)
3469 if (!mstrcmp(m->name, tline->text, m->casesense))
3470 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003471 }
3472
3473 /*
3474 * After all that, we didn't find one with the right number of
3475 * parameters. Issue a warning, and fail to expand the macro.
3476 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003477 error(ERR_WARNING | ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003478 "macro `%s' exists, but not taking %d parameters",
3479 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003480 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003481 return NULL;
3482}
3483
3484/*
3485 * Expand the multi-line macro call made by the given line, if
3486 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003487 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003488 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003489static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003490{
3491 Token *startline = tline;
3492 Token *label = NULL;
3493 int dont_prepend = 0;
3494 Token **params, *t, *tt;
3495 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003496 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003497 int i, nparam, *paramlen;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003498
3499 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003500 skip_white_(t);
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003501/* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3502 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003503 return 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003504 m = is_mmacro(t, &params);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003505 if (!m) {
3506 Token *last;
3507 /*
3508 * We have an id which isn't a macro call. We'll assume
3509 * it might be a label; we'll also check to see if a
3510 * colon follows it. Then, if there's another id after
3511 * that lot, we'll check it again for macro-hood.
3512 */
3513 label = last = t;
3514 t = t->next;
3515 if (tok_type_(t, TOK_WHITESPACE))
3516 last = t, t = t->next;
3517 if (tok_is_(t, ":")) {
3518 dont_prepend = 1;
3519 last = t, t = t->next;
3520 if (tok_type_(t, TOK_WHITESPACE))
3521 last = t, t = t->next;
3522 }
3523 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3524 return 0;
3525 last->next = NULL;
3526 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003527 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003528
3529 /*
3530 * Fix up the parameters: this involves stripping leading and
3531 * trailing whitespace, then stripping braces if they are
3532 * present.
3533 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003534 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003535 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003536
H. Peter Anvine2c80182005-01-15 22:15:51 +00003537 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003538 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003539 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003540
H. Peter Anvine2c80182005-01-15 22:15:51 +00003541 t = params[i];
3542 skip_white_(t);
3543 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003544 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003545 params[i] = t;
3546 paramlen[i] = 0;
3547 while (t) {
3548 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3549 break; /* ... because we have hit a comma */
3550 if (comma && t->type == TOK_WHITESPACE
3551 && tok_is_(t->next, ","))
3552 break; /* ... or a space then a comma */
3553 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3554 break; /* ... or a brace */
3555 t = t->next;
3556 paramlen[i]++;
3557 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003558 }
3559
3560 /*
3561 * OK, we have a MMacro structure together with a set of
3562 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003563 * copies of each Line on to istk->expansion. Substitution of
3564 * parameter tokens and macro-local tokens doesn't get done
3565 * until the single-line macro substitution process; this is
3566 * because delaying them allows us to change the semantics
3567 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003568 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003569 * First, push an end marker on to istk->expansion, mark this
3570 * macro as in progress, and set up its invocation-specific
3571 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003572 */
3573 ll = nasm_malloc(sizeof(Line));
3574 ll->next = istk->expansion;
3575 ll->finishes = m;
3576 ll->first = NULL;
3577 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003578
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003579 m->in_progress = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003580 m->params = params;
3581 m->iline = tline;
3582 m->nparam = nparam;
3583 m->rotate = 0;
3584 m->paramlen = paramlen;
3585 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003586 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003587
3588 m->next_active = istk->mstk;
3589 istk->mstk = m;
3590
H. Peter Anvine2c80182005-01-15 22:15:51 +00003591 for (l = m->expansion; l; l = l->next) {
3592 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003593
H. Peter Anvine2c80182005-01-15 22:15:51 +00003594 ll = nasm_malloc(sizeof(Line));
3595 ll->finishes = NULL;
3596 ll->next = istk->expansion;
3597 istk->expansion = ll;
3598 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003599
H. Peter Anvine2c80182005-01-15 22:15:51 +00003600 for (t = l->first; t; t = t->next) {
3601 Token *x = t;
3602 if (t->type == TOK_PREPROC_ID &&
3603 t->text[1] == '0' && t->text[2] == '0') {
3604 dont_prepend = -1;
3605 x = label;
3606 if (!x)
3607 continue;
3608 }
3609 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3610 tail = &tt->next;
3611 }
3612 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003613 }
3614
3615 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00003616 * If we had a label, push it on as the first line of
3617 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003618 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003619 if (label) {
3620 if (dont_prepend < 0)
3621 free_tlist(startline);
3622 else {
3623 ll = nasm_malloc(sizeof(Line));
3624 ll->finishes = NULL;
3625 ll->next = istk->expansion;
3626 istk->expansion = ll;
3627 ll->first = startline;
3628 if (!dont_prepend) {
3629 while (label->next)
3630 label = label->next;
3631 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3632 }
3633 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003634 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003635
H. Peter Anvin734b1882002-04-30 21:01:08 +00003636 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003637
H. Peter Anvineba20a72002-04-30 20:53:55 +00003638 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003639}
3640
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003641/*
3642 * Since preprocessor always operate only on the line that didn't
3643 * arrived yet, we should always use ERR_OFFBY1. Also since user
3644 * won't want to see same error twice (preprocessing is done once
3645 * per pass) we will want to show errors only during pass one.
3646 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003647static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003648{
3649 va_list arg;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003650 char buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003651
3652 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003653 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003654 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003655
H. Peter Anvin734b1882002-04-30 21:01:08 +00003656 va_start(arg, fmt);
Ed Beroset19f927a2004-12-15 17:07:03 +00003657 vsnprintf(buff, sizeof(buff), fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003658 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003659
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003660 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003661 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3662 istk->mstk->lineno, buff);
3663 else
3664 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003665}
3666
H. Peter Anvin734b1882002-04-30 21:01:08 +00003667static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003668pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003669 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003670{
H. Peter Anvin99941bf2002-05-14 17:44:03 +00003671 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003672 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003673 istk = nasm_malloc(sizeof(Include));
3674 istk->next = NULL;
3675 istk->conds = NULL;
3676 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003677 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003678 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00003679 istk->fname = NULL;
3680 src_set_fname(nasm_strdup(file));
3681 src_set_linnum(0);
3682 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003683 if (!istk->fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003684 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3685 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003686 defining = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003687 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003688 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003689 if (tasm_compatible_mode) {
3690 stdmacpos = stdmac;
3691 } else {
3692 stdmacpos = &stdmac[TASM_MACRO_COUNT];
3693 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003694 any_extrastdmac = (extrastdmac != NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003695 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003696 evaluate = eval;
3697 pass = apass;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003698}
3699
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003700static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003701{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003702 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003703 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003704
H. Peter Anvine2c80182005-01-15 22:15:51 +00003705 while (1) {
3706 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003707 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00003708 * buffer or from the input file.
3709 */
3710 tline = NULL;
3711 while (istk->expansion && istk->expansion->finishes) {
3712 Line *l = istk->expansion;
3713 if (!l->finishes->name && l->finishes->in_progress > 1) {
3714 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003715
H. Peter Anvine2c80182005-01-15 22:15:51 +00003716 /*
3717 * This is a macro-end marker for a macro with no
3718 * name, which means it's not really a macro at all
3719 * but a %rep block, and the `in_progress' field is
3720 * more than 1, meaning that we still need to
3721 * repeat. (1 means the natural last repetition; 0
3722 * means termination by %exitrep.) We have
3723 * therefore expanded up to the %endrep, and must
3724 * push the whole block on to the expansion buffer
3725 * again. We don't bother to remove the macro-end
3726 * marker: we'd only have to generate another one
3727 * if we did.
3728 */
3729 l->finishes->in_progress--;
3730 for (l = l->finishes->expansion; l; l = l->next) {
3731 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003732
H. Peter Anvine2c80182005-01-15 22:15:51 +00003733 ll = nasm_malloc(sizeof(Line));
3734 ll->next = istk->expansion;
3735 ll->finishes = NULL;
3736 ll->first = NULL;
3737 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003738
H. Peter Anvine2c80182005-01-15 22:15:51 +00003739 for (t = l->first; t; t = t->next) {
3740 if (t->text || t->type == TOK_WHITESPACE) {
3741 tt = *tail =
3742 new_Token(NULL, t->type, t->text, 0);
3743 tail = &tt->next;
3744 }
3745 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003746
H. Peter Anvine2c80182005-01-15 22:15:51 +00003747 istk->expansion = ll;
3748 }
3749 } else {
3750 /*
3751 * Check whether a `%rep' was started and not ended
3752 * within this macro expansion. This can happen and
3753 * should be detected. It's a fatal error because
3754 * I'm too confused to work out how to recover
3755 * sensibly from it.
3756 */
3757 if (defining) {
3758 if (defining->name)
3759 error(ERR_PANIC,
3760 "defining with name in expansion");
3761 else if (istk->mstk->name)
3762 error(ERR_FATAL,
3763 "`%%rep' without `%%endrep' within"
3764 " expansion of macro `%s'",
3765 istk->mstk->name);
3766 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00003767
H. Peter Anvine2c80182005-01-15 22:15:51 +00003768 /*
3769 * FIXME: investigate the relationship at this point between
3770 * istk->mstk and l->finishes
3771 */
3772 {
3773 MMacro *m = istk->mstk;
3774 istk->mstk = m->next_active;
3775 if (m->name) {
3776 /*
3777 * This was a real macro call, not a %rep, and
3778 * therefore the parameter information needs to
3779 * be freed.
3780 */
3781 nasm_free(m->params);
3782 free_tlist(m->iline);
3783 nasm_free(m->paramlen);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003784 l->finishes->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003785 } else
3786 free_mmacro(m);
3787 }
3788 istk->expansion = l->next;
3789 nasm_free(l);
3790 list->downlevel(LIST_MACRO);
3791 }
3792 }
3793 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003794
H. Peter Anvine2c80182005-01-15 22:15:51 +00003795 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003796 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003797 Line *l = istk->expansion;
3798 if (istk->mstk)
3799 istk->mstk->lineno++;
3800 tline = l->first;
3801 istk->expansion = l->next;
3802 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003803 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003804 list->line(LIST_MACRO, p);
3805 nasm_free(p);
3806 break;
3807 }
3808 line = read_line();
3809 if (line) { /* from the current input file */
3810 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00003811 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003812 nasm_free(line);
3813 break;
3814 }
3815 /*
3816 * The current file has ended; work down the istk
3817 */
3818 {
3819 Include *i = istk;
3820 fclose(i->fp);
3821 if (i->conds)
3822 error(ERR_FATAL,
3823 "expected `%%endif' before end of file");
3824 /* only set line and file name if there's a next node */
3825 if (i->next) {
3826 src_set_linnum(i->lineno);
3827 nasm_free(src_set_fname(i->fname));
3828 }
3829 istk = i->next;
3830 list->downlevel(LIST_INCLUDE);
3831 nasm_free(i);
3832 if (!istk)
3833 return NULL;
3834 }
3835 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003836
H. Peter Anvine2c80182005-01-15 22:15:51 +00003837 /*
3838 * We must expand MMacro parameters and MMacro-local labels
3839 * _before_ we plunge into directive processing, to cope
3840 * with things like `%define something %1' such as STRUC
3841 * uses. Unless we're _defining_ a MMacro, in which case
3842 * those tokens should be left alone to go into the
3843 * definition; and unless we're in a non-emitting
3844 * condition, in which case we don't want to meddle with
3845 * anything.
3846 */
3847 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
3848 tline = expand_mmac_params(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003849
H. Peter Anvine2c80182005-01-15 22:15:51 +00003850 /*
3851 * Check the line to see if it's a preprocessor directive.
3852 */
3853 if (do_directive(tline) == DIRECTIVE_FOUND) {
3854 continue;
3855 } else if (defining) {
3856 /*
3857 * We're defining a multi-line macro. We emit nothing
3858 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00003859 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003860 */
3861 Line *l = nasm_malloc(sizeof(Line));
3862 l->next = defining->expansion;
3863 l->first = tline;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003864 l->finishes = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003865 defining->expansion = l;
3866 continue;
3867 } else if (istk->conds && !emitting(istk->conds->state)) {
3868 /*
3869 * We're in a non-emitting branch of a condition block.
3870 * Emit nothing at all, not even a blank line: when we
3871 * emerge from the condition we'll give a line-number
3872 * directive so we keep our place correctly.
3873 */
3874 free_tlist(tline);
3875 continue;
3876 } else if (istk->mstk && !istk->mstk->in_progress) {
3877 /*
3878 * We're in a %rep block which has been terminated, so
3879 * we're walking through to the %endrep without
3880 * emitting anything. Emit nothing at all, not even a
3881 * blank line: when we emerge from the %rep block we'll
3882 * give a line-number directive so we keep our place
3883 * correctly.
3884 */
3885 free_tlist(tline);
3886 continue;
3887 } else {
3888 tline = expand_smacro(tline);
3889 if (!expand_mmacro(tline)) {
3890 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003891 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003892 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003893 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003894 free_tlist(tline);
3895 break;
3896 } else {
3897 continue; /* expand_mmacro calls free_tlist */
3898 }
3899 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003900 }
3901
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003902 return line;
3903}
3904
H. Peter Anvine2c80182005-01-15 22:15:51 +00003905static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003906{
H. Peter Anvine2c80182005-01-15 22:15:51 +00003907 if (defining) {
3908 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
3909 defining->name);
3910 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003911 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003912 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003913 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07003914 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00003915 while (istk) {
3916 Include *i = istk;
3917 istk = istk->next;
3918 fclose(i->fp);
3919 nasm_free(i->fname);
3920 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003921 }
3922 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003923 ctx_pop();
3924 if (pass == 0) {
3925 free_llist(predef);
3926 delete_Blocks();
3927 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003928}
3929
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003930void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003931{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003932 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07003933
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003934 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07003935 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003936 i->next = NULL;
3937
H. Peter Anvine2c80182005-01-15 22:15:51 +00003938 if (ipath != NULL) {
3939 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003940 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003941 j = j->next;
3942 j->next = i;
3943 } else {
3944 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003945 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003946}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003947
3948/*
3949 * added by alexfru:
3950 *
3951 * This function is used to "export" the include paths, e.g.
3952 * the paths specified in the '-I' command switch.
3953 * The need for such exporting is due to the 'incbin' directive,
3954 * which includes raw binary files (unlike '%include', which
3955 * includes text source files). It would be real nice to be
3956 * able to specify paths to search for incbin'ned files also.
3957 * So, this is a simple workaround.
3958 *
3959 * The function use is simple:
3960 *
3961 * The 1st call (with NULL argument) returns a pointer to the 1st path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003962 * (char** type) or NULL if none include paths available.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003963 *
3964 * All subsequent calls take as argument the value returned by this
3965 * function last. The return value is either the next path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003966 * (char** type) or NULL if the end of the paths list is reached.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003967 *
3968 * It is maybe not the best way to do things, but I didn't want
3969 * to export too much, just one or two functions and no types or
3970 * variables exported.
3971 *
3972 * Can't say I like the current situation with e.g. this path list either,
3973 * it seems to be never deallocated after creation...
3974 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003975char **pp_get_include_path_ptr(char **pPrevPath)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003976{
3977/* This macro returns offset of a member of a structure */
3978#define GetMemberOffset(StructType,MemberName)\
3979 ((size_t)&((StructType*)0)->MemberName)
3980 IncPath *i;
3981
H. Peter Anvine2c80182005-01-15 22:15:51 +00003982 if (pPrevPath == NULL) {
3983 if (ipath != NULL)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003984 return &ipath->path;
3985 else
3986 return NULL;
3987 }
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003988 i = (IncPath *) ((char *)pPrevPath - GetMemberOffset(IncPath, path));
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003989 i = i->next;
3990 if (i != NULL)
3991 return &i->path;
3992 else
3993 return NULL;
3994#undef GetMemberOffset
3995}
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003996
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003997void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003998{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003999 Token *inc, *space, *name;
4000 Line *l;
4001
H. Peter Anvin734b1882002-04-30 21:01:08 +00004002 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4003 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4004 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004005
4006 l = nasm_malloc(sizeof(Line));
4007 l->next = predef;
4008 l->first = inc;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004009 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004010 predef = l;
4011}
4012
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004013void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004014{
4015 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004016 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004017 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004018
4019 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004020 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4021 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004022 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004023 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004024 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004025 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004026 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004027
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004028 l = nasm_malloc(sizeof(Line));
4029 l->next = predef;
4030 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004031 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004032 predef = l;
4033}
4034
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004035void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004036{
4037 Token *def, *space;
4038 Line *l;
4039
H. Peter Anvin734b1882002-04-30 21:01:08 +00004040 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4041 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004042 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004043
4044 l = nasm_malloc(sizeof(Line));
4045 l->next = predef;
4046 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004047 l->finishes = false;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004048 predef = l;
4049}
4050
Keith Kaniosb7a89542007-04-12 02:40:54 +00004051/*
4052 * Added by Keith Kanios:
4053 *
4054 * This function is used to assist with "runtime" preprocessor
4055 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4056 *
4057 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4058 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4059 */
4060
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004061void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004062{
4063 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004064
Keith Kaniosb7a89542007-04-12 02:40:54 +00004065 def = tokenize(definition);
4066 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4067 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004068
Keith Kaniosb7a89542007-04-12 02:40:54 +00004069}
4070
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004071void pp_extra_stdmac(const char **macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004072{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004073 extrastdmac = macros;
4074}
4075
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004076static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004077{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004078 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004079 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004080 tok->text = nasm_strdup(numbuf);
4081 tok->type = TOK_NUMBER;
4082}
4083
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004084Preproc nasmpp = {
4085 pp_reset,
4086 pp_getline,
4087 pp_cleanup
4088};