blob: b8b6ff2fee104c5406915ebd15cd4c64b0ef3676 [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
Beroset095e6a22007-12-29 09:44:23 -05005 * redistributable under the license given in the file "LICENSE"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 * 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
H. Peter Anvine2c80182005-01-15 22:15:51 +0000303enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000304 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
305 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
306};
307
H. Peter Anvin476d2862007-10-02 22:04:15 -0700308static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000309 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
310 "ifndef", "include", "local"
311};
312
313static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000314static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000315static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800316static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000317
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000318static Context *cstk;
319static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000320static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000321
H. Peter Anvine2c80182005-01-15 22:15:51 +0000322static efunc _error; /* Pointer to client-provided error reporting function */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000323static evalfunc evaluate;
324
H. Peter Anvine2c80182005-01-15 22:15:51 +0000325static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000326
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700327static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000328
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000329static Line *predef = NULL;
330
331static ListGen *list;
332
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000333/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000334 * The current set of multi-line macros we have defined.
335 */
H. Peter Anvin97a23472007-09-16 17:57:25 -0700336static struct hash_table *mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000337
338/*
339 * The current set of single-line macros we have defined.
340 */
H. Peter Anvin97a23472007-09-16 17:57:25 -0700341static struct hash_table *smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000342
343/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000344 * The multi-line macro we are currently defining, or the %rep
345 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000346 */
347static MMacro *defining;
348
349/*
350 * The number of macro parameters to allocate space for at a time.
351 */
352#define PARAM_DELTA 16
353
354/*
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000355 * The standard macro set: defined as `static char *stdmac[]'. Also
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000356 * gives our position in the macro set, when we're processing it.
357 */
358#include "macros.c"
H. Peter Anvin188ce762008-02-16 13:58:45 -0800359static const char * const *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000360
361/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000362 * The extra standard macros that come from the object format, if
363 * any.
364 */
H. Peter Anvin188ce762008-02-16 13:58:45 -0800365static const char * const *extrastdmac = NULL;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700366bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000367
368/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000369 * Tokens are allocated in blocks to improve speed
370 */
371#define TOKEN_BLOCKSIZE 4096
372static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000373struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000374 Blocks *next;
375 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000376};
377
378static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000379
380/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000381 * Forward declarations.
382 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000383static Token *expand_mmac_params(Token * tline);
384static Token *expand_smacro(Token * tline);
385static Token *expand_id(Token * tline);
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700386static Context *get_ctx(char *name, bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700387static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000388static void error(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000389static void *new_Block(size_t size);
390static void delete_Blocks(void);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000391static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000392static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000393
394/*
395 * Macros for safe checking of token pointers, avoid *(NULL)
396 */
397#define tok_type_(x,t) ((x) && (x)->type == (t))
398#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
399#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
400#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000401
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000402/* Handle TASM specific directives, which do not contain a % in
403 * front of them. We do it here because I could not find any other
404 * place to do it for the moment, and it is a hack (ideally it would
405 * be nice to be able to use the NASM pre-processor to do it).
406 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000407static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000408{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000409 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000410 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000411
412 /* Skip whitespace */
413 while (isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000414 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000415
416 /* Binary search for the directive name */
417 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000418 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000419 len = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000420 while (!isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000421 len++;
422 if (len) {
423 oldchar = p[len];
424 p[len] = 0;
425 while (j - i > 1) {
426 k = (j + i) / 2;
427 m = nasm_stricmp(p, tasm_directives[k]);
428 if (m == 0) {
429 /* We have found a directive, so jam a % in front of it
430 * so that NASM will then recognise it as one if it's own.
431 */
432 p[len] = oldchar;
433 len = strlen(p);
434 oldline = line;
435 line = nasm_malloc(len + 2);
436 line[0] = '%';
437 if (k == TM_IFDIFI) {
438 /* NASM does not recognise IFDIFI, so we convert it to
439 * %ifdef BOGUS. This is not used in NASM comaptible
440 * code, but does need to parse for the TASM macro
441 * package.
442 */
443 strcpy(line + 1, "ifdef BOGUS");
444 } else {
445 memcpy(line + 1, p, len + 1);
446 }
447 nasm_free(oldline);
448 return line;
449 } else if (m < 0) {
450 j = k;
451 } else
452 i = k;
453 }
454 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000455 }
456 return line;
457}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000458
H. Peter Anvin76690a12002-04-30 20:52:49 +0000459/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000460 * The pre-preprocessing stage... This function translates line
461 * number indications as they emerge from GNU cpp (`# lineno "file"
462 * flags') into NASM preprocessor line number indications (`%line
463 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000464 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000465static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000466{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000467 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000468 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000469
H. Peter Anvine2c80182005-01-15 22:15:51 +0000470 if (line[0] == '#' && line[1] == ' ') {
471 oldline = line;
472 fname = oldline + 2;
473 lineno = atoi(fname);
474 fname += strspn(fname, "0123456789 ");
475 if (*fname == '"')
476 fname++;
477 fnlen = strcspn(fname, "\"");
478 line = nasm_malloc(20 + fnlen);
479 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
480 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000481 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000482 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000483 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000484 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000485}
486
487/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000488 * Free a linked list of tokens.
489 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000490static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000491{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000492 while (list) {
493 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000494 }
495}
496
497/*
498 * Free a linked list of lines.
499 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000501{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000502 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503 while (list) {
504 l = list;
505 list = list->next;
506 free_tlist(l->first);
507 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000508 }
509}
510
511/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000512 * Free an MMacro
513 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000514static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000515{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000516 nasm_free(m->name);
517 free_tlist(m->dlist);
518 nasm_free(m->defaults);
519 free_llist(m->expansion);
520 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000521}
522
523/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700524 * Free all currently defined macros, and free the hash tables
525 */
526static void free_macros(void)
527{
528 struct hash_tbl_node *it;
529 const char *key;
530 SMacro *s;
531 MMacro *m;
532
533 it = NULL;
534 while ((s = hash_iterate(smacros, &it, &key)) != NULL) {
535 nasm_free((void *)key);
536 while (s) {
537 SMacro *ns = s->next;
538 nasm_free(s->name);
539 free_tlist(s->expansion);
540 nasm_free(s);
541 s = ns;
542 }
543 }
544 hash_free(smacros);
545
546 it = NULL;
547 while ((m = hash_iterate(mmacros, &it, &key)) != NULL) {
548 nasm_free((void *)key);
549 while (m) {
550 MMacro *nm = m->next;
551 free_mmacro(m);
552 m = nm;
553 }
554 }
555 hash_free(mmacros);
556}
557
558/*
559 * Initialize the hash tables
560 */
561static void init_macros(void)
562{
563 smacros = hash_init();
564 mmacros = hash_init();
565}
566
567/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000568 * Pop the context stack.
569 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000571{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000572 Context *c = cstk;
573 SMacro *smac, *s;
574
575 cstk = cstk->next;
576 smac = c->localmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000577 while (smac) {
578 s = smac;
579 smac = smac->next;
580 nasm_free(s->name);
581 free_tlist(s->expansion);
582 nasm_free(s);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000583 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000584 nasm_free(c->name);
585 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000586}
587
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000588#define BUF_DELTA 512
589/*
590 * Read a line from the top file in istk, handling multiple CR/LFs
591 * at the end of the line read, and handling spurious ^Zs. Will
592 * return lines from the standard macro set if this has not already
593 * been done.
594 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000595static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000596{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000597 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000598 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000599
H. Peter Anvine2c80182005-01-15 22:15:51 +0000600 if (stdmacpos) {
601 if (*stdmacpos) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000602 char *ret = nasm_strdup(*stdmacpos++);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000603 if (!*stdmacpos && any_extrastdmac) {
604 stdmacpos = extrastdmac;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700605 any_extrastdmac = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000606 return ret;
607 }
608 /*
609 * Nasty hack: here we push the contents of `predef' on
610 * to the top-level expansion stack, since this is the
611 * most convenient way to implement the pre-include and
612 * pre-define features.
613 */
614 if (!*stdmacpos) {
615 Line *pd, *l;
616 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000617
H. Peter Anvine2c80182005-01-15 22:15:51 +0000618 for (pd = predef; pd; pd = pd->next) {
619 head = NULL;
620 tail = &head;
621 for (t = pd->first; t; t = t->next) {
622 *tail = new_Token(NULL, t->type, t->text, 0);
623 tail = &(*tail)->next;
624 }
625 l = nasm_malloc(sizeof(Line));
626 l->next = istk->expansion;
627 l->first = head;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700628 l->finishes = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000629 istk->expansion = l;
630 }
631 }
632 return ret;
633 } else {
634 stdmacpos = NULL;
635 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000636 }
637
638 bufsize = BUF_DELTA;
639 buffer = nasm_malloc(BUF_DELTA);
640 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000641 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000642 while (1) {
643 q = fgets(p, bufsize - (p - buffer), istk->fp);
644 if (!q)
645 break;
646 p += strlen(p);
647 if (p > buffer && p[-1] == '\n') {
648 /* Convert backslash-CRLF line continuation sequences into
649 nothing at all (for DOS and Windows) */
650 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
651 p -= 3;
652 *p = 0;
653 continued_count++;
654 }
655 /* Also convert backslash-LF line continuation sequences into
656 nothing at all (for Unix) */
657 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
658 p -= 2;
659 *p = 0;
660 continued_count++;
661 } else {
662 break;
663 }
664 }
665 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000666 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000667 bufsize += BUF_DELTA;
668 buffer = nasm_realloc(buffer, bufsize);
669 p = buffer + offset; /* prevent stale-pointer problems */
670 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000671 }
672
H. Peter Anvine2c80182005-01-15 22:15:51 +0000673 if (!q && p == buffer) {
674 nasm_free(buffer);
675 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000676 }
677
H. Peter Anvine2c80182005-01-15 22:15:51 +0000678 src_set_linnum(src_get_linnum() + istk->lineinc +
679 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000680
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000681 /*
682 * Play safe: remove CRs as well as LFs, if any of either are
683 * present at the end of the line.
684 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000685 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000686 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000687
688 /*
689 * Handle spurious ^Z, which may be inserted into source files
690 * by some file transfer utilities.
691 */
692 buffer[strcspn(buffer, "\032")] = '\0';
693
H. Peter Anvin734b1882002-04-30 21:01:08 +0000694 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000695
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000696 return buffer;
697}
698
699/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000700 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000701 * don't need to parse the value out of e.g. numeric tokens: we
702 * simply split one string into many.
703 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000704static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000705{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000706 char *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000707 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000708 Token *list = NULL;
709 Token *t, **tail = &list;
710
H. Peter Anvine2c80182005-01-15 22:15:51 +0000711 while (*line) {
712 p = line;
713 if (*p == '%') {
714 p++;
715 if (isdigit(*p) ||
716 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
717 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
718 do {
719 p++;
720 }
721 while (isdigit(*p));
722 type = TOK_PREPROC_ID;
723 } else if (*p == '{') {
724 p++;
725 while (*p && *p != '}') {
726 p[-1] = *p;
727 p++;
728 }
729 p[-1] = '\0';
730 if (*p)
731 p++;
732 type = TOK_PREPROC_ID;
733 } else if (isidchar(*p) ||
734 ((*p == '!' || *p == '%' || *p == '$') &&
735 isidchar(p[1]))) {
736 do {
737 p++;
738 }
739 while (isidchar(*p));
740 type = TOK_PREPROC_ID;
741 } else {
742 type = TOK_OTHER;
743 if (*p == '%')
744 p++;
745 }
746 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
747 type = TOK_ID;
748 p++;
749 while (*p && isidchar(*p))
750 p++;
751 } else if (*p == '\'' || *p == '"') {
752 /*
753 * A string token.
754 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000755 char c = *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000756 p++;
757 type = TOK_STRING;
758 while (*p && *p != c)
759 p++;
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000760
H. Peter Anvine2c80182005-01-15 22:15:51 +0000761 if (*p) {
762 p++;
763 } else {
764 error(ERR_WARNING, "unterminated string");
765 /* Handling unterminated strings by UNV */
766 /* type = -1; */
767 }
768 } else if (isnumstart(*p)) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700769 bool is_hex = false;
770 bool is_float = false;
771 bool has_e = false;
772 char c, *r;
773
H. Peter Anvine2c80182005-01-15 22:15:51 +0000774 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700775 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000776 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700777
778 if (*p == '$') {
779 p++;
780 is_hex = true;
781 }
782
783 for (;;) {
784 c = *p++;
785
786 if (!is_hex && (c == 'e' || c == 'E')) {
787 has_e = true;
788 if (*p == '+' || *p == '-') {
789 /* e can only be followed by +/- if it is either a
790 prefixed hex number or a floating-point number */
791 p++;
792 is_float = true;
793 }
794 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
795 is_hex = true;
796 } else if (c == 'P' || c == 'p') {
797 is_float = true;
798 if (*p == '+' || *p == '-')
799 p++;
800 } else if (isnumchar(c) || c == '_')
801 ; /* just advance */
802 else if (c == '.') {
803 /* we need to deal with consequences of the legacy
804 parser, like "1.nolist" being two tokens
805 (TOK_NUMBER, TOK_ID) here; at least give it
806 a shot for now. In the future, we probably need
807 a flex-based scanner with proper pattern matching
808 to do it as well as it can be done. Nothing in
809 the world is going to help the person who wants
810 0x123.p16 interpreted as two tokens, though. */
811 r = p;
812 while (*r == '_')
813 r++;
814
815 if (isdigit(*r) || (is_hex && isxdigit(*r)) ||
816 (!is_hex && (*r == 'e' || *r == 'E')) ||
817 (*r == 'p' || *r == 'P')) {
818 p = r;
819 is_float = true;
820 } else
821 break; /* Terminate the token */
822 } else
823 break;
824 }
825 p--; /* Point to first character beyond number */
826
827 if (has_e && !is_hex) {
828 /* 1e13 is floating-point, but 1e13h is not */
829 is_float = true;
830 }
831
832 type = is_float ? TOK_FLOAT : TOK_NUMBER;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000833 } else if (isspace(*p)) {
834 type = TOK_WHITESPACE;
835 p++;
836 while (*p && isspace(*p))
837 p++;
838 /*
839 * Whitespace just before end-of-line is discarded by
840 * pretending it's a comment; whitespace just before a
841 * comment gets lumped into the comment.
842 */
843 if (!*p || *p == ';') {
844 type = TOK_COMMENT;
845 while (*p)
846 p++;
847 }
848 } else if (*p == ';') {
849 type = TOK_COMMENT;
850 while (*p)
851 p++;
852 } else {
853 /*
854 * Anything else is an operator of some kind. We check
855 * for all the double-character operators (>>, <<, //,
856 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000857 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000858 */
859 type = TOK_OTHER;
860 if ((p[0] == '>' && p[1] == '>') ||
861 (p[0] == '<' && p[1] == '<') ||
862 (p[0] == '/' && p[1] == '/') ||
863 (p[0] == '<' && p[1] == '=') ||
864 (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++;
872 }
873 p++;
874 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000875
H. Peter Anvine2c80182005-01-15 22:15:51 +0000876 /* Handling unterminated string by UNV */
877 /*if (type == -1)
878 {
879 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
880 t->text[p-line] = *line;
881 tail = &t->next;
882 }
883 else */
884 if (type != TOK_COMMENT) {
885 *tail = t = new_Token(NULL, type, line, p - line);
886 tail = &t->next;
887 }
888 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000889 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000890 return list;
891}
892
H. Peter Anvince616072002-04-30 21:02:23 +0000893/*
894 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -0700895 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +0000896 * deleted only all at once by the delete_Blocks function.
897 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000898static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +0000899{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000900 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000901
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000902 /* first, get to the end of the linked list */
903 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000904 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000905 /* now allocate the requested chunk */
906 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000907
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000908 /* now allocate a new block for the next request */
909 b->next = nasm_malloc(sizeof(Blocks));
910 /* and initialize the contents of the new block */
911 b->next->next = NULL;
912 b->next->chunk = NULL;
913 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000914}
915
916/*
917 * this function deletes all managed blocks of memory
918 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000919static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +0000920{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000921 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +0000922
H. Peter Anvin70653092007-10-19 14:42:29 -0700923 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000924 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -0700925 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000926 * free it.
927 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000928 while (b) {
929 if (b->chunk)
930 nasm_free(b->chunk);
931 a = b;
932 b = b->next;
933 if (a != &blocks)
934 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000935 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000936}
H. Peter Anvin734b1882002-04-30 21:01:08 +0000937
938/*
H. Peter Anvin70653092007-10-19 14:42:29 -0700939 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +0000940 * back to the caller. It sets the type and text elements, and
941 * also the mac and next elements to NULL.
942 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000943static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +0000944{
945 Token *t;
946 int i;
947
H. Peter Anvine2c80182005-01-15 22:15:51 +0000948 if (freeTokens == NULL) {
949 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
950 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
951 freeTokens[i].next = &freeTokens[i + 1];
952 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000953 }
954 t = freeTokens;
955 freeTokens = t->next;
956 t->next = next;
957 t->mac = NULL;
958 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000959 if (type == TOK_WHITESPACE || text == NULL) {
960 t->text = NULL;
961 } else {
962 if (txtlen == 0)
963 txtlen = strlen(text);
964 t->text = nasm_malloc(1 + txtlen);
965 strncpy(t->text, text, txtlen);
966 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +0000967 }
968 return t;
969}
970
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +0000972{
973 Token *next = t->next;
974 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +0000975 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000976 freeTokens = t;
977 return next;
978}
979
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000980/*
981 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000982 * If expand_locals is not zero, identifiers of the form "%$*xxx"
983 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000984 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000985static char *detoken(Token * tlist, int expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000986{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000987 Token *t;
988 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000989 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -0800990 const char *q;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000991
992 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 for (t = tlist; t; t = t->next) {
994 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000995 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 nasm_free(t->text);
997 if (p)
998 t->text = nasm_strdup(p);
999 else
1000 t->text = NULL;
1001 }
1002 /* Expand local macros here and not during preprocessing */
1003 if (expand_locals &&
1004 t->type == TOK_PREPROC_ID && t->text &&
1005 t->text[0] == '%' && t->text[1] == '$') {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001006 Context *ctx = get_ctx(t->text, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001008 char buffer[40];
1009 char *p, *q = t->text + 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001010
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 q += strspn(q, "$");
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001012 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 p = nasm_strcat(buffer, q);
1014 nasm_free(t->text);
1015 t->text = p;
1016 }
1017 }
1018 if (t->type == TOK_WHITESPACE) {
1019 len++;
1020 } else if (t->text) {
1021 len += strlen(t->text);
1022 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001023 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001024 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001025 for (t = tlist; t; t = t->next) {
1026 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001027 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001028 } else if (t->text) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001029 q = t->text;
1030 while (*q)
1031 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001032 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001033 }
1034 *p = '\0';
1035 return line;
1036}
1037
1038/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001039 * A scanner, suitable for use by the expression evaluator, which
1040 * operates on a line of Tokens. Expects a pointer to a pointer to
1041 * the first token in the line to be passed in as its private_data
1042 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001043 *
1044 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001045 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001046static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001047{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001048 Token **tlineptr = private_data;
1049 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001050 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001051
H. Peter Anvine2c80182005-01-15 22:15:51 +00001052 do {
1053 tline = *tlineptr;
1054 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001055 }
1056 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001058
1059 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001060 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001061
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001062 tokval->t_charptr = tline->text;
1063
H. Peter Anvin76690a12002-04-30 20:52:49 +00001064 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001065 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001066 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001067 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001068
H. Peter Anvine2c80182005-01-15 22:15:51 +00001069 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001070 p = tokval->t_charptr = tline->text;
1071 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 tokval->t_charptr++;
1073 return tokval->t_type = TOKEN_ID;
1074 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001075
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001076 for (r = p, s = ourcopy; *r; r++) {
1077 if (r > p+MAX_KEYWORD)
1078 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1079 *s++ = tolower(*r);
1080 }
1081 *s = '\0';
1082 /* right, so we have an identifier sitting in temp storage. now,
1083 * is it actually a register or instruction name, or what? */
1084 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001085 }
1086
H. Peter Anvine2c80182005-01-15 22:15:51 +00001087 if (tline->type == TOK_NUMBER) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001088 bool rn_error;
1089 tokval->t_integer = readnum(tline->text, &rn_error);
1090 if (rn_error)
1091 return tokval->t_type = TOKEN_ERRNUM; /* some malformation occurred */
1092 tokval->t_charptr = tline->text;
1093 return tokval->t_type = TOKEN_NUM;
1094 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001095
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001096 if (tline->type == TOK_FLOAT) {
1097 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001098 }
1099
H. Peter Anvine2c80182005-01-15 22:15:51 +00001100 if (tline->type == TOK_STRING) {
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001101 bool rn_warn;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001102 char q, *r;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001103 int l;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001104
H. Peter Anvine2c80182005-01-15 22:15:51 +00001105 r = tline->text;
1106 q = *r++;
1107 l = strlen(r);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001108
H. Peter Anvine2c80182005-01-15 22:15:51 +00001109 if (l == 0 || r[l - 1] != q)
1110 return tokval->t_type = TOKEN_ERRNUM;
1111 tokval->t_integer = readstrnum(r, l - 1, &rn_warn);
1112 if (rn_warn)
1113 error(ERR_WARNING | ERR_PASS1, "character constant too long");
1114 tokval->t_charptr = NULL;
1115 return tokval->t_type = TOKEN_NUM;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001116 }
1117
H. Peter Anvine2c80182005-01-15 22:15:51 +00001118 if (tline->type == TOK_OTHER) {
1119 if (!strcmp(tline->text, "<<"))
1120 return tokval->t_type = TOKEN_SHL;
1121 if (!strcmp(tline->text, ">>"))
1122 return tokval->t_type = TOKEN_SHR;
1123 if (!strcmp(tline->text, "//"))
1124 return tokval->t_type = TOKEN_SDIV;
1125 if (!strcmp(tline->text, "%%"))
1126 return tokval->t_type = TOKEN_SMOD;
1127 if (!strcmp(tline->text, "=="))
1128 return tokval->t_type = TOKEN_EQ;
1129 if (!strcmp(tline->text, "<>"))
1130 return tokval->t_type = TOKEN_NE;
1131 if (!strcmp(tline->text, "!="))
1132 return tokval->t_type = TOKEN_NE;
1133 if (!strcmp(tline->text, "<="))
1134 return tokval->t_type = TOKEN_LE;
1135 if (!strcmp(tline->text, ">="))
1136 return tokval->t_type = TOKEN_GE;
1137 if (!strcmp(tline->text, "&&"))
1138 return tokval->t_type = TOKEN_DBL_AND;
1139 if (!strcmp(tline->text, "^^"))
1140 return tokval->t_type = TOKEN_DBL_XOR;
1141 if (!strcmp(tline->text, "||"))
1142 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001143 }
1144
1145 /*
1146 * We have no other options: just return the first character of
1147 * the token text.
1148 */
1149 return tokval->t_type = tline->text[0];
1150}
1151
1152/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001153 * Compare a string to the name of an existing macro; this is a
1154 * simple wrapper which calls either strcmp or nasm_stricmp
1155 * depending on the value of the `casesense' parameter.
1156 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001157static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001158{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001159 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001160}
1161
1162/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001163 * Return the Context structure associated with a %$ token. Return
1164 * NULL, having _already_ reported an error condition, if the
1165 * context stack isn't deep enough for the supplied number of $
1166 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001167 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001168 * also scanned for such smacro, until it is found; if not -
1169 * only the context that directly results from the number of $'s
1170 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001171 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001172static Context *get_ctx(char *name, bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001173{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001174 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001175 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001176 int i;
1177
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001178 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001179 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001180
H. Peter Anvine2c80182005-01-15 22:15:51 +00001181 if (!cstk) {
1182 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1183 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001184 }
1185
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1187 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001188/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001189 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001190 if (!ctx) {
1191 error(ERR_NONFATAL, "`%s': context stack is only"
1192 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1193 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001194 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001195 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001196 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001197
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198 do {
1199 /* Search for this smacro in found context */
1200 m = ctx->localmac;
1201 while (m) {
1202 if (!mstrcmp(m->name, name, m->casesense))
1203 return ctx;
1204 m = m->next;
1205 }
1206 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001207 }
1208 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001209 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001210}
1211
1212/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001213 * Open an include file. This routine must always return a valid
1214 * file pointer if it returns - it's responsible for throwing an
1215 * ERR_FATAL and bombing out completely if not. It should also try
1216 * the include path one by one until it finds the file or reaches
1217 * the end of the path.
1218 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001219static FILE *inc_fopen(char *file)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001220{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001221 FILE *fp;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001222 char *prefix = "", *combine;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001223 IncPath *ip = ipath;
H. Peter Anvin620515a2002-04-30 20:57:38 +00001224 static int namelen = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001225 int len = strlen(file);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001226
H. Peter Anvine2c80182005-01-15 22:15:51 +00001227 while (1) {
1228 combine = nasm_malloc(strlen(prefix) + len + 1);
1229 strcpy(combine, prefix);
1230 strcat(combine, file);
1231 fp = fopen(combine, "r");
1232 if (pass == 0 && fp) {
1233 namelen += strlen(combine) + 1;
1234 if (namelen > 62) {
1235 printf(" \\\n ");
1236 namelen = 2;
1237 }
1238 printf(" %s", combine);
1239 }
1240 nasm_free(combine);
1241 if (fp)
1242 return fp;
1243 if (!ip)
1244 break;
1245 prefix = ip->path;
1246 ip = ip->next;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001247
1248 if (!prefix) {
1249 /* -MG given and file not found */
1250 if (pass == 0) {
1251 namelen += strlen(file) + 1;
1252 if (namelen > 62) {
1253 printf(" \\\n ");
1254 namelen = 2;
1255 }
1256 printf(" %s", file);
1257 }
1258 return NULL;
1259 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001260 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001261
H. Peter Anvin734b1882002-04-30 21:01:08 +00001262 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001264}
1265
1266/*
H. Peter Anvin97a23472007-09-16 17:57:25 -07001267 * Search for a key in the hash index; adding it if necessary
1268 * (in which case we initialize the data pointer to NULL.)
1269 */
1270static void **
1271hash_findi_add(struct hash_table *hash, const char *str)
1272{
1273 struct hash_insert hi;
1274 void **r;
1275 char *strx;
1276
1277 r = hash_findi(hash, str, &hi);
1278 if (r)
1279 return r;
1280
1281 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
1282 return hash_add(&hi, strx, NULL);
1283}
1284
1285/*
1286 * Like hash_findi, but returns the data element rather than a pointer
1287 * to it. Used only when not adding a new element, hence no third
1288 * argument.
1289 */
1290static void *
1291hash_findix(struct hash_table *hash, const char *str)
1292{
1293 void **p;
1294
1295 p = hash_findi(hash, str, NULL);
1296 return p ? *p : NULL;
1297}
1298
1299/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001300 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001301 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001302 * return true if _any_ single-line macro of that name is defined.
1303 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001304 * `nparam' or no parameters is defined.
1305 *
1306 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001307 * defined, or nparam is -1, the address of the definition structure
1308 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001309 * is NULL, no action will be taken regarding its contents, and no
1310 * error will occur.
1311 *
1312 * Note that this is also called with nparam zero to resolve
1313 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001314 *
1315 * If you already know which context macro belongs to, you can pass
1316 * the context pointer as first parameter; if you won't but name begins
1317 * with %$ the context will be automatically computed. If all_contexts
1318 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001319 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001320static bool
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001321smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001322 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001323{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001324 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001325
H. Peter Anvin97a23472007-09-16 17:57:25 -07001326 if (ctx) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 m = ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001328 } else if (name[0] == '%' && name[1] == '$') {
1329 if (cstk)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001330 ctx = get_ctx(name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001331 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001332 return false; /* got to return _something_ */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 m = ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001334 } else {
1335 m = (SMacro *) hash_findix(smacros, name);
1336 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001337
H. Peter Anvine2c80182005-01-15 22:15:51 +00001338 while (m) {
1339 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001340 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001342 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 *defn = m;
1344 else
1345 *defn = NULL;
1346 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001347 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001348 }
1349 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001350 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001351
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001352 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001353}
1354
1355/*
1356 * Count and mark off the parameters in a multi-line macro call.
1357 * This is called both from within the multi-line macro expansion
1358 * code, and also to mark off the default parameters when provided
1359 * in a %macro definition line.
1360 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001362{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001363 int paramsize, brace;
1364
1365 *nparam = paramsize = 0;
1366 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001367 while (t) {
1368 if (*nparam >= paramsize) {
1369 paramsize += PARAM_DELTA;
1370 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1371 }
1372 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001373 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001374 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001375 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001376 (*params)[(*nparam)++] = t;
1377 while (tok_isnt_(t, brace ? "}" : ","))
1378 t = t->next;
1379 if (t) { /* got a comma/brace */
1380 t = t->next;
1381 if (brace) {
1382 /*
1383 * Now we've found the closing brace, look further
1384 * for the comma.
1385 */
1386 skip_white_(t);
1387 if (tok_isnt_(t, ",")) {
1388 error(ERR_NONFATAL,
1389 "braces do not enclose all of macro parameter");
1390 while (tok_isnt_(t, ","))
1391 t = t->next;
1392 }
1393 if (t)
1394 t = t->next; /* eat the comma */
1395 }
1396 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001397 }
1398}
1399
1400/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001401 * Determine whether one of the various `if' conditions is true or
1402 * not.
1403 *
1404 * We must free the tline we get passed.
1405 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001406static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001407{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001408 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001409 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001410 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001411 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001412 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001413 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001414
1415 origline = tline;
1416
H. Peter Anvine2c80182005-01-15 22:15:51 +00001417 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001418 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001419 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001420 while (cstk && tline) {
1421 skip_white_(tline);
1422 if (!tline || tline->type != TOK_ID) {
1423 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001424 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001425 free_tlist(origline);
1426 return -1;
1427 }
1428 if (!nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001429 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 tline = tline->next;
1431 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001432 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001433
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001434 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001435 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001436 while (tline) {
1437 skip_white_(tline);
1438 if (!tline || (tline->type != TOK_ID &&
1439 (tline->type != TOK_PREPROC_ID ||
1440 tline->text[1] != '$'))) {
1441 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001442 "`%s' expects macro identifiers", pp_directives[ct]);
1443 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001444 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001445 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001446 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001447 tline = tline->next;
1448 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001449 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001450
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001451 case PPC_IFIDN:
1452 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001453 tline = expand_smacro(tline);
1454 t = tt = tline;
1455 while (tok_isnt_(tt, ","))
1456 tt = tt->next;
1457 if (!tt) {
1458 error(ERR_NONFATAL,
1459 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001460 pp_directives[ct]);
1461 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 }
1463 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001464 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001465 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1466 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1467 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001468 pp_directives[ct]);
1469 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001470 }
1471 if (t->type == TOK_WHITESPACE) {
1472 t = t->next;
1473 continue;
1474 }
1475 if (tt->type == TOK_WHITESPACE) {
1476 tt = tt->next;
1477 continue;
1478 }
1479 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001480 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001481 break;
1482 }
1483 /* Unify surrounding quotes for strings */
1484 if (t->type == TOK_STRING) {
1485 tt->text[0] = t->text[0];
1486 tt->text[strlen(tt->text) - 1] = t->text[0];
1487 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001488 if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001489 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001490 break;
1491 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001492
H. Peter Anvine2c80182005-01-15 22:15:51 +00001493 t = t->next;
1494 tt = tt->next;
1495 }
1496 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001497 j = false; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001498 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001499
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001500 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001501 {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001502 bool found = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001503 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001504
H. Peter Anvine2c80182005-01-15 22:15:51 +00001505 tline = tline->next;
1506 skip_white_(tline);
1507 tline = expand_id(tline);
1508 if (!tok_type_(tline, TOK_ID)) {
1509 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001510 "`%s' expects a macro name", pp_directives[ct]);
1511 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001512 }
1513 searching.name = nasm_strdup(tline->text);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001514 searching.casesense = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001515 searching.plus = false;
1516 searching.nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001517 searching.in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001518 searching.rep_nest = NULL;
1519 searching.nparam_min = 0;
1520 searching.nparam_max = INT_MAX;
1521 tline = expand_smacro(tline->next);
1522 skip_white_(tline);
1523 if (!tline) {
1524 } else if (!tok_type_(tline, TOK_NUMBER)) {
1525 error(ERR_NONFATAL,
1526 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001527 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001528 } else {
1529 searching.nparam_min = searching.nparam_max =
1530 readnum(tline->text, &j);
1531 if (j)
1532 error(ERR_NONFATAL,
1533 "unable to parse parameter count `%s'",
1534 tline->text);
1535 }
1536 if (tline && tok_is_(tline->next, "-")) {
1537 tline = tline->next->next;
1538 if (tok_is_(tline, "*"))
1539 searching.nparam_max = INT_MAX;
1540 else if (!tok_type_(tline, TOK_NUMBER))
1541 error(ERR_NONFATAL,
1542 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001543 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001544 else {
1545 searching.nparam_max = readnum(tline->text, &j);
1546 if (j)
1547 error(ERR_NONFATAL,
1548 "unable to parse parameter count `%s'",
1549 tline->text);
1550 if (searching.nparam_min > searching.nparam_max)
1551 error(ERR_NONFATAL,
1552 "minimum parameter count exceeds maximum");
1553 }
1554 }
1555 if (tline && tok_is_(tline->next, "+")) {
1556 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001557 searching.plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001558 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07001559 mmac = (MMacro *) hash_findix(mmacros, searching.name);
1560 while (mmac) {
1561 if (!strcmp(mmac->name, searching.name) &&
1562 (mmac->nparam_min <= searching.nparam_max
1563 || searching.plus)
1564 && (searching.nparam_min <= mmac->nparam_max
1565 || mmac->plus)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001566 found = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001567 break;
1568 }
1569 mmac = mmac->next;
1570 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001571 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001572 j = found;
1573 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001574 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001575
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001576 case PPC_IFID:
1577 needtype = TOK_ID;
1578 goto iftype;
1579 case PPC_IFNUM:
1580 needtype = TOK_NUMBER;
1581 goto iftype;
1582 case PPC_IFSTR:
1583 needtype = TOK_STRING;
1584 goto iftype;
1585
1586 iftype:
H. Peter Anvin927c92b2008-02-16 13:44:52 -08001587 tline = expand_smacro(tline);
1588 t = tline;
1589
1590 while (tok_type_(t, TOK_WHITESPACE) ||
1591 (needtype == TOK_NUMBER &&
1592 tok_type_(t, TOK_OTHER) &&
1593 (t->text[0] == '-' || t->text[0] == '+') &&
1594 !t->text[1]))
1595 t = t->next;
1596
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001597 j = tok_type_(t, needtype);
1598 break;
1599
1600 case PPC_IFTOKEN:
1601 t = tline = expand_smacro(tline);
1602 while (tok_type_(t, TOK_WHITESPACE))
1603 t = t->next;
1604
1605 j = false;
1606 if (t) {
1607 t = t->next; /* Skip the actual token */
1608 while (tok_type_(t, TOK_WHITESPACE))
1609 t = t->next;
1610 j = !t; /* Should be nothing left */
1611 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001612 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001613
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001614 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 t = tline = expand_smacro(tline);
1616 tptr = &t;
1617 tokval.t_type = TOKEN_INVALID;
1618 evalresult = evaluate(ppscan, tptr, &tokval,
1619 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001620 if (!evalresult)
1621 return -1;
1622 if (tokval.t_type)
1623 error(ERR_WARNING,
1624 "trailing garbage after expression ignored");
1625 if (!is_simple(evalresult)) {
1626 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001627 "non-constant value given to `%s'", pp_directives[ct]);
1628 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001629 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001630 j = reloc_value(evalresult) != 0;
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001631 return j;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001632
H. Peter Anvine2c80182005-01-15 22:15:51 +00001633 default:
1634 error(ERR_FATAL,
1635 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001636 pp_directives[ct]);
1637 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001638 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001639
1640 free_tlist(origline);
1641 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001642
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001643fail:
1644 free_tlist(origline);
1645 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001646}
1647
1648/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001649 * Expand macros in a string. Used in %error and %include directives.
Keith Kaniosb7a89542007-04-12 02:40:54 +00001650 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001651 * The returned variable should ALWAYS be freed after usage.
1652 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001653void expand_macros_in_string(char **p)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001654{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001655 Token *line = tokenize(*p);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001656 line = expand_smacro(line);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001657 *p = detoken(line, false);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001658}
1659
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001660/*
1661 * Common code for defining an smacro
1662 */
1663static bool define_smacro(Context *ctx, char *mname, bool casesense,
1664 int nparam, Token *expansion)
1665{
1666 SMacro *smac, **smhead;
H. Peter Anvin70653092007-10-19 14:42:29 -07001667
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001668 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1669 if (!smac) {
1670 error(ERR_WARNING,
1671 "single-line macro `%s' defined both with and"
1672 " without parameters", mname);
1673
1674 /* Some instances of the old code considered this a failure,
1675 some others didn't. What is the right thing to do here? */
1676 free_tlist(expansion);
1677 return false; /* Failure */
1678 } else {
1679 /*
1680 * We're redefining, so we have to take over an
1681 * existing SMacro structure. This means freeing
1682 * what was already in it.
1683 */
1684 nasm_free(smac->name);
1685 free_tlist(smac->expansion);
1686 }
1687 } else {
1688 if (!ctx)
1689 smhead = (SMacro **) hash_findi_add(smacros, mname);
1690 else
1691 smhead = &ctx->localmac;
H. Peter Anvin70653092007-10-19 14:42:29 -07001692
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001693 smac = nasm_malloc(sizeof(SMacro));
1694 smac->next = *smhead;
1695 *smhead = smac;
1696 }
1697 smac->name = nasm_strdup(mname);
1698 smac->casesense = casesense;
1699 smac->nparam = nparam;
1700 smac->expansion = expansion;
1701 smac->in_progress = false;
1702 return true; /* Success */
1703}
1704
1705/*
1706 * Undefine an smacro
1707 */
1708static void undef_smacro(Context *ctx, const char *mname)
1709{
1710 SMacro **smhead, *s, **sp;
1711
1712 if (!ctx)
1713 smhead = (SMacro **) hash_findi(smacros, mname, NULL);
1714 else
1715 smhead = &ctx->localmac;
H. Peter Anvin70653092007-10-19 14:42:29 -07001716
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001717 if (smhead) {
1718 /*
1719 * We now have a macro name... go hunt for it.
1720 */
1721 sp = smhead;
1722 while ((s = *sp) != NULL) {
1723 if (!mstrcmp(s->name, mname, s->casesense)) {
1724 *sp = s->next;
1725 nasm_free(s->name);
1726 free_tlist(s->expansion);
1727 nasm_free(s);
1728 } else {
1729 sp = &s->next;
1730 }
1731 }
1732 }
1733}
1734
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001735/*
1736 * Decode a size directive
1737 */
1738static int parse_size(const char *str) {
1739 static const char *size_names[] =
1740 { "byte", "dword", "oword", "qword", "tword", "word" };
1741 static const int sizes[] =
1742 { 0, 1, 4, 16, 8, 10, 2 };
1743
1744 return sizes[bsii(str, size_names, elements(size_names))+1];
1745}
1746
Ed Beroset3ab3f412002-06-11 03:31:49 +00001747/**
1748 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001749 * Find out if a line contains a preprocessor directive, and deal
1750 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07001751 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001752 * If a directive _is_ found, it is the responsibility of this routine
1753 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001754 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001755 * @param tline a pointer to the current tokeninzed line linked list
1756 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07001757 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001758 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001759static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001760{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001761 enum preproc_token i;
1762 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07001763 bool err;
1764 int nparam;
1765 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001766 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001767 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001768 int offset;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001769 char *p, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001770 Include *inc;
1771 Context *ctx;
1772 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001773 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001774 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1775 Line *l;
1776 struct tokenval tokval;
1777 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001779 int64_t count;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001780
1781 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001782
H. Peter Anvineba20a72002-04-30 20:53:55 +00001783 skip_white_(tline);
1784 if (!tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001785 (tline->text[1] == '%' || tline->text[1] == '$'
1786 || tline->text[1] == '!'))
1787 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001788
H. Peter Anvin4169a472007-09-12 01:29:43 +00001789 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001790
1791 /*
1792 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001793 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001794 * we should ignore all directives except for condition
1795 * directives.
1796 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001797 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001798 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1799 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001800 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001801
1802 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001803 * If we're defining a macro or reading a %rep block, we should
1804 * ignore all directives except for %macro/%imacro (which
1805 * generate an error), %endm/%endmacro, and (only if we're in a
H. Peter Anvineba20a72002-04-30 20:53:55 +00001806 * %rep block) %endrep. If we're in a %rep block, another %rep
1807 * causes an error, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001808 */
1809 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001810 i != PP_ENDMACRO && i != PP_ENDM &&
1811 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1812 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001813 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001814
H. Peter Anvin4169a472007-09-12 01:29:43 +00001815 switch (i) {
1816 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1818 tline->text);
1819 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001820
H. Peter Anvine2c80182005-01-15 22:15:51 +00001821 case PP_STACKSIZE:
1822 /* Directive to tell NASM what the default stack size is. The
1823 * default is for a 16-bit stack, and this can be overriden with
1824 * %stacksize large.
1825 * the following form:
1826 *
1827 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1828 */
1829 tline = tline->next;
1830 if (tline && tline->type == TOK_WHITESPACE)
1831 tline = tline->next;
1832 if (!tline || tline->type != TOK_ID) {
1833 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1834 free_tlist(origline);
1835 return DIRECTIVE_FOUND;
1836 }
1837 if (nasm_stricmp(tline->text, "flat") == 0) {
1838 /* All subsequent ARG directives are for a 32-bit stack */
1839 StackSize = 4;
1840 StackPointer = "ebp";
1841 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001842 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08001843 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
1844 /* All subsequent ARG directives are for a 64-bit stack */
1845 StackSize = 8;
1846 StackPointer = "rbp";
1847 ArgOffset = 8;
1848 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001849 } else if (nasm_stricmp(tline->text, "large") == 0) {
1850 /* All subsequent ARG directives are for a 16-bit stack,
1851 * far function call.
1852 */
1853 StackSize = 2;
1854 StackPointer = "bp";
1855 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001856 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001857 } else if (nasm_stricmp(tline->text, "small") == 0) {
1858 /* All subsequent ARG directives are for a 16-bit stack,
1859 * far function call. We don't support near functions.
1860 */
1861 StackSize = 2;
1862 StackPointer = "bp";
1863 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001864 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001865 } else {
1866 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1867 free_tlist(origline);
1868 return DIRECTIVE_FOUND;
1869 }
1870 free_tlist(origline);
1871 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001872
H. Peter Anvine2c80182005-01-15 22:15:51 +00001873 case PP_ARG:
1874 /* TASM like ARG directive to define arguments to functions, in
1875 * the following form:
1876 *
1877 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1878 */
1879 offset = ArgOffset;
1880 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001881 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001882 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001883
H. Peter Anvine2c80182005-01-15 22:15:51 +00001884 /* Find the argument name */
1885 tline = tline->next;
1886 if (tline && tline->type == TOK_WHITESPACE)
1887 tline = tline->next;
1888 if (!tline || tline->type != TOK_ID) {
1889 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1890 free_tlist(origline);
1891 return DIRECTIVE_FOUND;
1892 }
1893 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001894
H. Peter Anvine2c80182005-01-15 22:15:51 +00001895 /* Find the argument size type */
1896 tline = tline->next;
1897 if (!tline || tline->type != TOK_OTHER
1898 || tline->text[0] != ':') {
1899 error(ERR_NONFATAL,
1900 "Syntax error processing `%%arg' directive");
1901 free_tlist(origline);
1902 return DIRECTIVE_FOUND;
1903 }
1904 tline = tline->next;
1905 if (!tline || tline->type != TOK_ID) {
1906 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1907 free_tlist(origline);
1908 return DIRECTIVE_FOUND;
1909 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001910
H. Peter Anvine2c80182005-01-15 22:15:51 +00001911 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001912 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001913 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001914 size = parse_size(tt->text);
1915 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001916 error(ERR_NONFATAL,
1917 "Invalid size type for `%%arg' missing directive");
1918 free_tlist(tt);
1919 free_tlist(origline);
1920 return DIRECTIVE_FOUND;
1921 }
1922 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001923
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001924 /* Round up to even stack slots */
1925 size = (size+StackSize-1) & ~(StackSize-1);
1926
H. Peter Anvine2c80182005-01-15 22:15:51 +00001927 /* Now define the macro for the argument */
1928 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1929 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001930 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001931 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001932
H. Peter Anvine2c80182005-01-15 22:15:51 +00001933 /* Move to the next argument in the list */
1934 tline = tline->next;
1935 if (tline && tline->type == TOK_WHITESPACE)
1936 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001937 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1938 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001939 free_tlist(origline);
1940 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001941
H. Peter Anvine2c80182005-01-15 22:15:51 +00001942 case PP_LOCAL:
1943 /* TASM like LOCAL directive to define local variables for a
1944 * function, in the following form:
1945 *
1946 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1947 *
1948 * The '= LocalSize' at the end is ignored by NASM, but is
1949 * required by TASM to define the local parameter size (and used
1950 * by the TASM macro package).
1951 */
1952 offset = LocalOffset;
1953 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001954 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001955 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001956
H. Peter Anvine2c80182005-01-15 22:15:51 +00001957 /* Find the argument name */
1958 tline = tline->next;
1959 if (tline && tline->type == TOK_WHITESPACE)
1960 tline = tline->next;
1961 if (!tline || tline->type != TOK_ID) {
1962 error(ERR_NONFATAL,
1963 "`%%local' missing argument parameter");
1964 free_tlist(origline);
1965 return DIRECTIVE_FOUND;
1966 }
1967 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001968
H. Peter Anvine2c80182005-01-15 22:15:51 +00001969 /* Find the argument size type */
1970 tline = tline->next;
1971 if (!tline || tline->type != TOK_OTHER
1972 || tline->text[0] != ':') {
1973 error(ERR_NONFATAL,
1974 "Syntax error processing `%%local' directive");
1975 free_tlist(origline);
1976 return DIRECTIVE_FOUND;
1977 }
1978 tline = tline->next;
1979 if (!tline || tline->type != TOK_ID) {
1980 error(ERR_NONFATAL,
1981 "`%%local' missing size type parameter");
1982 free_tlist(origline);
1983 return DIRECTIVE_FOUND;
1984 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001985
H. Peter Anvine2c80182005-01-15 22:15:51 +00001986 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001987 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001988 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001989 size = parse_size(tt->text);
1990 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001991 error(ERR_NONFATAL,
1992 "Invalid size type for `%%local' missing directive");
1993 free_tlist(tt);
1994 free_tlist(origline);
1995 return DIRECTIVE_FOUND;
1996 }
1997 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001998
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001999 /* Round up to even stack slots */
2000 size = (size+StackSize-1) & ~(StackSize-1);
2001
2002 offset += size; /* Negative offset, increment before */
2003
2004 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002005 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2006 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002007 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002008
H. Peter Anvine2c80182005-01-15 22:15:51 +00002009 /* Now define the assign to setup the enter_c macro correctly */
2010 snprintf(directive, sizeof(directive),
2011 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002012 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002013
H. Peter Anvine2c80182005-01-15 22:15:51 +00002014 /* Move to the next argument in the list */
2015 tline = tline->next;
2016 if (tline && tline->type == TOK_WHITESPACE)
2017 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002018 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2019 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002020 free_tlist(origline);
2021 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002022
H. Peter Anvine2c80182005-01-15 22:15:51 +00002023 case PP_CLEAR:
2024 if (tline->next)
2025 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002026 free_macros();
2027 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002028 free_tlist(origline);
2029 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002030
H. Peter Anvine2c80182005-01-15 22:15:51 +00002031 case PP_INCLUDE:
2032 tline = tline->next;
2033 skip_white_(tline);
2034 if (!tline || (tline->type != TOK_STRING &&
2035 tline->type != TOK_INTERNAL_STRING)) {
2036 error(ERR_NONFATAL, "`%%include' expects a file name");
2037 free_tlist(origline);
2038 return DIRECTIVE_FOUND; /* but we did _something_ */
2039 }
2040 if (tline->next)
2041 error(ERR_WARNING,
2042 "trailing garbage after `%%include' ignored");
2043 if (tline->type != TOK_INTERNAL_STRING) {
2044 p = tline->text + 1; /* point past the quote to the name */
2045 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2046 } else
2047 p = tline->text; /* internal_string is easier */
2048 expand_macros_in_string(&p);
2049 inc = nasm_malloc(sizeof(Include));
2050 inc->next = istk;
2051 inc->conds = NULL;
2052 inc->fp = inc_fopen(p);
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002053 if (!inc->fp && pass == 0) {
2054 /* -MG given but file not found */
2055 nasm_free(inc);
2056 } else {
2057 inc->fname = src_set_fname(p);
2058 inc->lineno = src_set_linnum(0);
2059 inc->lineinc = 1;
2060 inc->expansion = NULL;
2061 inc->mstk = NULL;
2062 istk = inc;
2063 list->uplevel(LIST_INCLUDE);
2064 }
2065 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002066 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002067
H. Peter Anvine2c80182005-01-15 22:15:51 +00002068 case PP_PUSH:
2069 tline = tline->next;
2070 skip_white_(tline);
2071 tline = expand_id(tline);
2072 if (!tok_type_(tline, TOK_ID)) {
2073 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2074 free_tlist(origline);
2075 return DIRECTIVE_FOUND; /* but we did _something_ */
2076 }
2077 if (tline->next)
2078 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2079 ctx = nasm_malloc(sizeof(Context));
2080 ctx->next = cstk;
2081 ctx->localmac = NULL;
2082 ctx->name = nasm_strdup(tline->text);
2083 ctx->number = unique++;
2084 cstk = ctx;
2085 free_tlist(origline);
2086 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002087
H. Peter Anvine2c80182005-01-15 22:15:51 +00002088 case PP_REPL:
2089 tline = tline->next;
2090 skip_white_(tline);
2091 tline = expand_id(tline);
2092 if (!tok_type_(tline, TOK_ID)) {
2093 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2094 free_tlist(origline);
2095 return DIRECTIVE_FOUND; /* but we did _something_ */
2096 }
2097 if (tline->next)
2098 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2099 if (!cstk)
2100 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2101 else {
2102 nasm_free(cstk->name);
2103 cstk->name = nasm_strdup(tline->text);
2104 }
2105 free_tlist(origline);
2106 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002107
H. Peter Anvine2c80182005-01-15 22:15:51 +00002108 case PP_POP:
2109 if (tline->next)
2110 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2111 if (!cstk)
2112 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2113 else
2114 ctx_pop();
2115 free_tlist(origline);
2116 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002117
H. Peter Anvine2c80182005-01-15 22:15:51 +00002118 case PP_ERROR:
2119 tline->next = expand_smacro(tline->next);
2120 tline = tline->next;
2121 skip_white_(tline);
2122 if (tok_type_(tline, TOK_STRING)) {
2123 p = tline->text + 1; /* point past the quote to the name */
2124 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2125 expand_macros_in_string(&p);
2126 error(ERR_NONFATAL, "%s", p);
2127 nasm_free(p);
2128 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002129 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002130 error(ERR_WARNING, "%s", p);
2131 nasm_free(p);
2132 }
2133 free_tlist(origline);
2134 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002135
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002136 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002137 if (istk->conds && !emitting(istk->conds->state))
2138 j = COND_NEVER;
2139 else {
2140 j = if_condition(tline->next, i);
2141 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002142 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2143 }
2144 cond = nasm_malloc(sizeof(Cond));
2145 cond->next = istk->conds;
2146 cond->state = j;
2147 istk->conds = cond;
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002148 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002149 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002150
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002151 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002152 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002153 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002154 if (emitting(istk->conds->state)
2155 || istk->conds->state == COND_NEVER)
2156 istk->conds->state = COND_NEVER;
2157 else {
2158 /*
2159 * IMPORTANT: In the case of %if, we will already have
2160 * called expand_mmac_params(); however, if we're
2161 * processing an %elif we must have been in a
2162 * non-emitting mode, which would have inhibited
2163 * the normal invocation of expand_mmac_params(). Therefore,
2164 * we have to do it explicitly here.
2165 */
2166 j = if_condition(expand_mmac_params(tline->next), i);
2167 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002168 istk->conds->state =
2169 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2170 }
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002171 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002172 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002173
H. Peter Anvine2c80182005-01-15 22:15:51 +00002174 case PP_ELSE:
2175 if (tline->next)
2176 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2177 if (!istk->conds)
2178 error(ERR_FATAL, "`%%else': no matching `%%if'");
2179 if (emitting(istk->conds->state)
2180 || istk->conds->state == COND_NEVER)
2181 istk->conds->state = COND_ELSE_FALSE;
2182 else
2183 istk->conds->state = COND_ELSE_TRUE;
2184 free_tlist(origline);
2185 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002186
H. Peter Anvine2c80182005-01-15 22:15:51 +00002187 case PP_ENDIF:
2188 if (tline->next)
2189 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2190 if (!istk->conds)
2191 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2192 cond = istk->conds;
2193 istk->conds = cond->next;
2194 nasm_free(cond);
2195 free_tlist(origline);
2196 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002197
H. Peter Anvine2c80182005-01-15 22:15:51 +00002198 case PP_MACRO:
2199 case PP_IMACRO:
2200 if (defining)
2201 error(ERR_FATAL,
2202 "`%%%smacro': already defining a macro",
2203 (i == PP_IMACRO ? "i" : ""));
2204 tline = tline->next;
2205 skip_white_(tline);
2206 tline = expand_id(tline);
2207 if (!tok_type_(tline, TOK_ID)) {
2208 error(ERR_NONFATAL,
2209 "`%%%smacro' expects a macro name",
2210 (i == PP_IMACRO ? "i" : ""));
2211 return DIRECTIVE_FOUND;
2212 }
2213 defining = nasm_malloc(sizeof(MMacro));
2214 defining->name = nasm_strdup(tline->text);
2215 defining->casesense = (i == PP_MACRO);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002216 defining->plus = false;
2217 defining->nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002218 defining->in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002219 defining->rep_nest = NULL;
2220 tline = expand_smacro(tline->next);
2221 skip_white_(tline);
2222 if (!tok_type_(tline, TOK_NUMBER)) {
2223 error(ERR_NONFATAL,
2224 "`%%%smacro' expects a parameter count",
2225 (i == PP_IMACRO ? "i" : ""));
2226 defining->nparam_min = defining->nparam_max = 0;
2227 } else {
2228 defining->nparam_min = defining->nparam_max =
H. Peter Anvin70055962007-10-11 00:05:31 -07002229 readnum(tline->text, &err);
2230 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002231 error(ERR_NONFATAL,
2232 "unable to parse parameter count `%s'", tline->text);
2233 }
2234 if (tline && tok_is_(tline->next, "-")) {
2235 tline = tline->next->next;
2236 if (tok_is_(tline, "*"))
2237 defining->nparam_max = INT_MAX;
2238 else if (!tok_type_(tline, TOK_NUMBER))
2239 error(ERR_NONFATAL,
2240 "`%%%smacro' expects a parameter count after `-'",
2241 (i == PP_IMACRO ? "i" : ""));
2242 else {
H. Peter Anvin70055962007-10-11 00:05:31 -07002243 defining->nparam_max = readnum(tline->text, &err);
2244 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002245 error(ERR_NONFATAL,
2246 "unable to parse parameter count `%s'",
2247 tline->text);
2248 if (defining->nparam_min > defining->nparam_max)
2249 error(ERR_NONFATAL,
2250 "minimum parameter count exceeds maximum");
2251 }
2252 }
2253 if (tline && tok_is_(tline->next, "+")) {
2254 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002255 defining->plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002256 }
2257 if (tline && tok_type_(tline->next, TOK_ID) &&
2258 !nasm_stricmp(tline->next->text, ".nolist")) {
2259 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002260 defining->nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002261 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07002262 mmac = (MMacro *) hash_findix(mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002263 while (mmac) {
2264 if (!strcmp(mmac->name, defining->name) &&
2265 (mmac->nparam_min <= defining->nparam_max
2266 || defining->plus)
2267 && (defining->nparam_min <= mmac->nparam_max
2268 || mmac->plus)) {
2269 error(ERR_WARNING,
2270 "redefining multi-line macro `%s'", defining->name);
2271 break;
2272 }
2273 mmac = mmac->next;
2274 }
2275 /*
2276 * Handle default parameters.
2277 */
2278 if (tline && tline->next) {
2279 defining->dlist = tline->next;
2280 tline->next = NULL;
2281 count_mmac_params(defining->dlist, &defining->ndefs,
2282 &defining->defaults);
2283 } else {
2284 defining->dlist = NULL;
2285 defining->defaults = NULL;
2286 }
2287 defining->expansion = NULL;
2288 free_tlist(origline);
2289 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002290
H. Peter Anvine2c80182005-01-15 22:15:51 +00002291 case PP_ENDM:
2292 case PP_ENDMACRO:
2293 if (!defining) {
2294 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2295 return DIRECTIVE_FOUND;
2296 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07002297 mmhead = (MMacro **) hash_findi_add(mmacros, defining->name);
2298 defining->next = *mmhead;
2299 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002300 defining = NULL;
2301 free_tlist(origline);
2302 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002303
H. Peter Anvine2c80182005-01-15 22:15:51 +00002304 case PP_ROTATE:
2305 if (tline->next && tline->next->type == TOK_WHITESPACE)
2306 tline = tline->next;
2307 if (tline->next == NULL) {
2308 free_tlist(origline);
2309 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2310 return DIRECTIVE_FOUND;
2311 }
2312 t = expand_smacro(tline->next);
2313 tline->next = NULL;
2314 free_tlist(origline);
2315 tline = t;
2316 tptr = &t;
2317 tokval.t_type = TOKEN_INVALID;
2318 evalresult =
2319 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2320 free_tlist(tline);
2321 if (!evalresult)
2322 return DIRECTIVE_FOUND;
2323 if (tokval.t_type)
2324 error(ERR_WARNING,
2325 "trailing garbage after expression ignored");
2326 if (!is_simple(evalresult)) {
2327 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2328 return DIRECTIVE_FOUND;
2329 }
2330 mmac = istk->mstk;
2331 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2332 mmac = mmac->next_active;
2333 if (!mmac) {
2334 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2335 } else if (mmac->nparam == 0) {
2336 error(ERR_NONFATAL,
2337 "`%%rotate' invoked within macro without parameters");
2338 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002339 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002340
H. Peter Anvin25a99342007-09-22 17:45:45 -07002341 rotate %= (int)mmac->nparam;
2342 if (rotate < 0)
2343 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002344
H. Peter Anvin25a99342007-09-22 17:45:45 -07002345 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002346 }
2347 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002348
H. Peter Anvine2c80182005-01-15 22:15:51 +00002349 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002350 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002351 do {
2352 tline = tline->next;
2353 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002354
H. Peter Anvine2c80182005-01-15 22:15:51 +00002355 if (tok_type_(tline, TOK_ID) &&
2356 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002357 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002358 do {
2359 tline = tline->next;
2360 } while (tok_type_(tline, TOK_WHITESPACE));
2361 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002362
H. Peter Anvine2c80182005-01-15 22:15:51 +00002363 if (tline) {
2364 t = expand_smacro(tline);
2365 tptr = &t;
2366 tokval.t_type = TOKEN_INVALID;
2367 evalresult =
2368 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2369 if (!evalresult) {
2370 free_tlist(origline);
2371 return DIRECTIVE_FOUND;
2372 }
2373 if (tokval.t_type)
2374 error(ERR_WARNING,
2375 "trailing garbage after expression ignored");
2376 if (!is_simple(evalresult)) {
2377 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2378 return DIRECTIVE_FOUND;
2379 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002380 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002381 } else {
2382 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002383 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 }
2385 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002386
H. Peter Anvine2c80182005-01-15 22:15:51 +00002387 tmp_defining = defining;
2388 defining = nasm_malloc(sizeof(MMacro));
2389 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002390 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002391 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002392 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002393 defining->in_progress = count;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002394 defining->nparam_min = defining->nparam_max = 0;
2395 defining->defaults = NULL;
2396 defining->dlist = NULL;
2397 defining->expansion = NULL;
2398 defining->next_active = istk->mstk;
2399 defining->rep_nest = tmp_defining;
2400 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002401
H. Peter Anvine2c80182005-01-15 22:15:51 +00002402 case PP_ENDREP:
2403 if (!defining || defining->name) {
2404 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2405 return DIRECTIVE_FOUND;
2406 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002407
H. Peter Anvine2c80182005-01-15 22:15:51 +00002408 /*
2409 * Now we have a "macro" defined - although it has no name
2410 * and we won't be entering it in the hash tables - we must
2411 * push a macro-end marker for it on to istk->expansion.
2412 * After that, it will take care of propagating itself (a
2413 * macro-end marker line for a macro which is really a %rep
2414 * block will cause the macro to be re-expanded, complete
2415 * with another macro-end marker to ensure the process
2416 * continues) until the whole expansion is forcibly removed
2417 * from istk->expansion by a %exitrep.
2418 */
2419 l = nasm_malloc(sizeof(Line));
2420 l->next = istk->expansion;
2421 l->finishes = defining;
2422 l->first = NULL;
2423 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002424
H. Peter Anvine2c80182005-01-15 22:15:51 +00002425 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002426
H. Peter Anvine2c80182005-01-15 22:15:51 +00002427 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2428 tmp_defining = defining;
2429 defining = defining->rep_nest;
2430 free_tlist(origline);
2431 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002432
H. Peter Anvine2c80182005-01-15 22:15:51 +00002433 case PP_EXITREP:
2434 /*
2435 * We must search along istk->expansion until we hit a
2436 * macro-end marker for a macro with no name. Then we set
2437 * its `in_progress' flag to 0.
2438 */
2439 for (l = istk->expansion; l; l = l->next)
2440 if (l->finishes && !l->finishes->name)
2441 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002442
H. Peter Anvine2c80182005-01-15 22:15:51 +00002443 if (l)
2444 l->finishes->in_progress = 0;
2445 else
2446 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2447 free_tlist(origline);
2448 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002449
H. Peter Anvine2c80182005-01-15 22:15:51 +00002450 case PP_XDEFINE:
2451 case PP_IXDEFINE:
2452 case PP_DEFINE:
2453 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002454 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002455
H. Peter Anvine2c80182005-01-15 22:15:51 +00002456 tline = tline->next;
2457 skip_white_(tline);
2458 tline = expand_id(tline);
2459 if (!tline || (tline->type != TOK_ID &&
2460 (tline->type != TOK_PREPROC_ID ||
2461 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002462 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2463 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002464 free_tlist(origline);
2465 return DIRECTIVE_FOUND;
2466 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002467
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002468 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002469
H. Peter Anvine2c80182005-01-15 22:15:51 +00002470 mname = tline->text;
2471 last = tline;
2472 param_start = tline = tline->next;
2473 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002474
H. Peter Anvine2c80182005-01-15 22:15:51 +00002475 /* Expand the macro definition now for %xdefine and %ixdefine */
2476 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2477 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002478
H. Peter Anvine2c80182005-01-15 22:15:51 +00002479 if (tok_is_(tline, "(")) {
2480 /*
2481 * This macro has parameters.
2482 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002483
H. Peter Anvine2c80182005-01-15 22:15:51 +00002484 tline = tline->next;
2485 while (1) {
2486 skip_white_(tline);
2487 if (!tline) {
2488 error(ERR_NONFATAL, "parameter identifier expected");
2489 free_tlist(origline);
2490 return DIRECTIVE_FOUND;
2491 }
2492 if (tline->type != TOK_ID) {
2493 error(ERR_NONFATAL,
2494 "`%s': parameter identifier expected",
2495 tline->text);
2496 free_tlist(origline);
2497 return DIRECTIVE_FOUND;
2498 }
2499 tline->type = TOK_SMAC_PARAM + nparam++;
2500 tline = tline->next;
2501 skip_white_(tline);
2502 if (tok_is_(tline, ",")) {
2503 tline = tline->next;
2504 continue;
2505 }
2506 if (!tok_is_(tline, ")")) {
2507 error(ERR_NONFATAL,
2508 "`)' expected to terminate macro template");
2509 free_tlist(origline);
2510 return DIRECTIVE_FOUND;
2511 }
2512 break;
2513 }
2514 last = tline;
2515 tline = tline->next;
2516 }
2517 if (tok_type_(tline, TOK_WHITESPACE))
2518 last = tline, tline = tline->next;
2519 macro_start = NULL;
2520 last->next = NULL;
2521 t = tline;
2522 while (t) {
2523 if (t->type == TOK_ID) {
2524 for (tt = param_start; tt; tt = tt->next)
2525 if (tt->type >= TOK_SMAC_PARAM &&
2526 !strcmp(tt->text, t->text))
2527 t->type = tt->type;
2528 }
2529 tt = t->next;
2530 t->next = macro_start;
2531 macro_start = t;
2532 t = tt;
2533 }
2534 /*
2535 * Good. We now have a macro name, a parameter count, and a
2536 * token list (in reverse order) for an expansion. We ought
2537 * to be OK just to create an SMacro, store it, and let
2538 * free_tlist have the rest of the line (which we have
2539 * carefully re-terminated after chopping off the expansion
2540 * from the end).
2541 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002542 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002543 free_tlist(origline);
2544 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002545
H. Peter Anvine2c80182005-01-15 22:15:51 +00002546 case PP_UNDEF:
2547 tline = tline->next;
2548 skip_white_(tline);
2549 tline = expand_id(tline);
2550 if (!tline || (tline->type != TOK_ID &&
2551 (tline->type != TOK_PREPROC_ID ||
2552 tline->text[1] != '$'))) {
2553 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2554 free_tlist(origline);
2555 return DIRECTIVE_FOUND;
2556 }
2557 if (tline->next) {
2558 error(ERR_WARNING,
2559 "trailing garbage after macro name ignored");
2560 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002561
H. Peter Anvine2c80182005-01-15 22:15:51 +00002562 /* Find the context that symbol belongs to */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002563 ctx = get_ctx(tline->text, false);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002564 undef_smacro(ctx, tline->text);
2565 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002566 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002567
H. Peter Anvine2c80182005-01-15 22:15:51 +00002568 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002569 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07002570
H. Peter Anvine2c80182005-01-15 22:15:51 +00002571 tline = tline->next;
2572 skip_white_(tline);
2573 tline = expand_id(tline);
2574 if (!tline || (tline->type != TOK_ID &&
2575 (tline->type != TOK_PREPROC_ID ||
2576 tline->text[1] != '$'))) {
2577 error(ERR_NONFATAL,
2578 "`%%strlen' expects a macro identifier as first parameter");
2579 free_tlist(origline);
2580 return DIRECTIVE_FOUND;
2581 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002582 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002583
H. Peter Anvine2c80182005-01-15 22:15:51 +00002584 mname = tline->text;
2585 last = tline;
2586 tline = expand_smacro(tline->next);
2587 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002588
H. Peter Anvine2c80182005-01-15 22:15:51 +00002589 t = tline;
2590 while (tok_type_(t, TOK_WHITESPACE))
2591 t = t->next;
2592 /* t should now point to the string */
2593 if (t->type != TOK_STRING) {
2594 error(ERR_NONFATAL,
2595 "`%%strlen` requires string as second parameter");
2596 free_tlist(tline);
2597 free_tlist(origline);
2598 return DIRECTIVE_FOUND;
2599 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002600
H. Peter Anvine2c80182005-01-15 22:15:51 +00002601 macro_start = nasm_malloc(sizeof(*macro_start));
2602 macro_start->next = NULL;
2603 make_tok_num(macro_start, strlen(t->text) - 2);
2604 macro_start->mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002605
H. Peter Anvine2c80182005-01-15 22:15:51 +00002606 /*
2607 * We now have a macro name, an implicit parameter count of
2608 * zero, and a numeric token to use as an expansion. Create
2609 * and store an SMacro.
2610 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002611 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002612 free_tlist(tline);
2613 free_tlist(origline);
2614 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002615
H. Peter Anvine2c80182005-01-15 22:15:51 +00002616 case PP_SUBSTR:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002617 casesense = true;
2618
H. Peter Anvine2c80182005-01-15 22:15:51 +00002619 tline = tline->next;
2620 skip_white_(tline);
2621 tline = expand_id(tline);
2622 if (!tline || (tline->type != TOK_ID &&
2623 (tline->type != TOK_PREPROC_ID ||
2624 tline->text[1] != '$'))) {
2625 error(ERR_NONFATAL,
2626 "`%%substr' expects a macro identifier as first parameter");
2627 free_tlist(origline);
2628 return DIRECTIVE_FOUND;
2629 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002630 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002631
H. Peter Anvine2c80182005-01-15 22:15:51 +00002632 mname = tline->text;
2633 last = tline;
2634 tline = expand_smacro(tline->next);
2635 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002636
H. Peter Anvine2c80182005-01-15 22:15:51 +00002637 t = tline->next;
2638 while (tok_type_(t, TOK_WHITESPACE))
2639 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002640
H. Peter Anvine2c80182005-01-15 22:15:51 +00002641 /* t should now point to the string */
2642 if (t->type != TOK_STRING) {
2643 error(ERR_NONFATAL,
2644 "`%%substr` requires string as second parameter");
2645 free_tlist(tline);
2646 free_tlist(origline);
2647 return DIRECTIVE_FOUND;
2648 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002649
H. Peter Anvine2c80182005-01-15 22:15:51 +00002650 tt = t->next;
2651 tptr = &tt;
2652 tokval.t_type = TOKEN_INVALID;
2653 evalresult =
2654 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2655 if (!evalresult) {
2656 free_tlist(tline);
2657 free_tlist(origline);
2658 return DIRECTIVE_FOUND;
2659 }
2660 if (!is_simple(evalresult)) {
2661 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2662 free_tlist(tline);
2663 free_tlist(origline);
2664 return DIRECTIVE_FOUND;
2665 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002666
H. Peter Anvine2c80182005-01-15 22:15:51 +00002667 macro_start = nasm_malloc(sizeof(*macro_start));
2668 macro_start->next = NULL;
2669 macro_start->text = nasm_strdup("'''");
2670 if (evalresult->value > 0
Charles Crayne192d5b52007-10-18 19:02:42 -07002671 && evalresult->value < (int) strlen(t->text) - 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002672 macro_start->text[1] = t->text[evalresult->value];
2673 } else {
2674 macro_start->text[2] = '\0';
2675 }
2676 macro_start->type = TOK_STRING;
2677 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002678
H. Peter Anvine2c80182005-01-15 22:15:51 +00002679 /*
2680 * We now have a macro name, an implicit parameter count of
2681 * zero, and a numeric token to use as an expansion. Create
2682 * and store an SMacro.
2683 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002684 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002685 free_tlist(tline);
2686 free_tlist(origline);
2687 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002688
H. Peter Anvine2c80182005-01-15 22:15:51 +00002689 case PP_ASSIGN:
2690 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002691 casesense = (i == PP_ASSIGN);
2692
H. Peter Anvine2c80182005-01-15 22:15:51 +00002693 tline = tline->next;
2694 skip_white_(tline);
2695 tline = expand_id(tline);
2696 if (!tline || (tline->type != TOK_ID &&
2697 (tline->type != TOK_PREPROC_ID ||
2698 tline->text[1] != '$'))) {
2699 error(ERR_NONFATAL,
2700 "`%%%sassign' expects a macro identifier",
2701 (i == PP_IASSIGN ? "i" : ""));
2702 free_tlist(origline);
2703 return DIRECTIVE_FOUND;
2704 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002705 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002706
H. Peter Anvine2c80182005-01-15 22:15:51 +00002707 mname = tline->text;
2708 last = tline;
2709 tline = expand_smacro(tline->next);
2710 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002711
H. Peter Anvine2c80182005-01-15 22:15:51 +00002712 t = tline;
2713 tptr = &t;
2714 tokval.t_type = TOKEN_INVALID;
2715 evalresult =
2716 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2717 free_tlist(tline);
2718 if (!evalresult) {
2719 free_tlist(origline);
2720 return DIRECTIVE_FOUND;
2721 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002722
H. Peter Anvine2c80182005-01-15 22:15:51 +00002723 if (tokval.t_type)
2724 error(ERR_WARNING,
2725 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00002726
H. Peter Anvine2c80182005-01-15 22:15:51 +00002727 if (!is_simple(evalresult)) {
2728 error(ERR_NONFATAL,
2729 "non-constant value given to `%%%sassign'",
2730 (i == PP_IASSIGN ? "i" : ""));
2731 free_tlist(origline);
2732 return DIRECTIVE_FOUND;
2733 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002734
H. Peter Anvine2c80182005-01-15 22:15:51 +00002735 macro_start = nasm_malloc(sizeof(*macro_start));
2736 macro_start->next = NULL;
2737 make_tok_num(macro_start, reloc_value(evalresult));
2738 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002739
H. Peter Anvine2c80182005-01-15 22:15:51 +00002740 /*
2741 * We now have a macro name, an implicit parameter count of
2742 * zero, and a numeric token to use as an expansion. Create
2743 * and store an SMacro.
2744 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002745 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002746 free_tlist(origline);
2747 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002748
H. Peter Anvine2c80182005-01-15 22:15:51 +00002749 case PP_LINE:
2750 /*
2751 * Syntax is `%line nnn[+mmm] [filename]'
2752 */
2753 tline = tline->next;
2754 skip_white_(tline);
2755 if (!tok_type_(tline, TOK_NUMBER)) {
2756 error(ERR_NONFATAL, "`%%line' expects line number");
2757 free_tlist(origline);
2758 return DIRECTIVE_FOUND;
2759 }
H. Peter Anvin70055962007-10-11 00:05:31 -07002760 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002761 m = 1;
2762 tline = tline->next;
2763 if (tok_is_(tline, "+")) {
2764 tline = tline->next;
2765 if (!tok_type_(tline, TOK_NUMBER)) {
2766 error(ERR_NONFATAL, "`%%line' expects line increment");
2767 free_tlist(origline);
2768 return DIRECTIVE_FOUND;
2769 }
H. Peter Anvin70055962007-10-11 00:05:31 -07002770 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002771 tline = tline->next;
2772 }
2773 skip_white_(tline);
2774 src_set_linnum(k);
2775 istk->lineinc = m;
2776 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002777 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002778 }
2779 free_tlist(origline);
2780 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002781
H. Peter Anvine2c80182005-01-15 22:15:51 +00002782 default:
2783 error(ERR_FATAL,
2784 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00002785 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002786 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002787 }
Ed Beroset3ab3f412002-06-11 03:31:49 +00002788 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002789}
2790
2791/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002792 * Ensure that a macro parameter contains a condition code and
2793 * nothing else. Return the condition code index if so, or -1
2794 * otherwise.
2795 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002796static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002797{
H. Peter Anvin76690a12002-04-30 20:52:49 +00002798 Token *tt;
2799 int i, j, k, m;
2800
H. Peter Anvin25a99342007-09-22 17:45:45 -07002801 if (!t)
2802 return -1; /* Probably a %+ without a space */
2803
H. Peter Anvineba20a72002-04-30 20:53:55 +00002804 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002805 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002806 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002807 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002808 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002809 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002810 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002811
2812 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00002813 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002814 while (j - i > 1) {
2815 k = (j + i) / 2;
2816 m = nasm_stricmp(t->text, conditions[k]);
2817 if (m == 0) {
2818 i = k;
2819 j = -2;
2820 break;
2821 } else if (m < 0) {
2822 j = k;
2823 } else
2824 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002825 }
2826 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002827 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002828 return i;
2829}
2830
2831/*
2832 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2833 * %-n) and MMacro-local identifiers (%%foo).
2834 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002835static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002836{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002837 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002838
2839 tail = &thead;
2840 thead = NULL;
2841
H. Peter Anvine2c80182005-01-15 22:15:51 +00002842 while (tline) {
2843 if (tline->type == TOK_PREPROC_ID &&
2844 (((tline->text[1] == '+' || tline->text[1] == '-')
2845 && tline->text[2]) || tline->text[1] == '%'
2846 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002847 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002848 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002849 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07002850 unsigned int n;
2851 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002852 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002853
H. Peter Anvine2c80182005-01-15 22:15:51 +00002854 t = tline;
2855 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002856
H. Peter Anvine2c80182005-01-15 22:15:51 +00002857 mac = istk->mstk;
2858 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2859 mac = mac->next_active;
2860 if (!mac)
2861 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2862 else
2863 switch (t->text[1]) {
2864 /*
2865 * We have to make a substitution of one of the
2866 * forms %1, %-1, %+1, %%foo, %0.
2867 */
2868 case '0':
2869 type = TOK_NUMBER;
2870 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
2871 text = nasm_strdup(tmpbuf);
2872 break;
2873 case '%':
2874 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002875 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002876 mac->unique);
2877 text = nasm_strcat(tmpbuf, t->text + 2);
2878 break;
2879 case '-':
2880 n = atoi(t->text + 2) - 1;
2881 if (n >= mac->nparam)
2882 tt = NULL;
2883 else {
2884 if (mac->nparam > 1)
2885 n = (n + mac->rotate) % mac->nparam;
2886 tt = mac->params[n];
2887 }
2888 cc = find_cc(tt);
2889 if (cc == -1) {
2890 error(ERR_NONFATAL,
2891 "macro parameter %d is not a condition code",
2892 n + 1);
2893 text = NULL;
2894 } else {
2895 type = TOK_ID;
2896 if (inverse_ccs[cc] == -1) {
2897 error(ERR_NONFATAL,
2898 "condition code `%s' is not invertible",
2899 conditions[cc]);
2900 text = NULL;
2901 } else
2902 text =
2903 nasm_strdup(conditions[inverse_ccs[cc]]);
2904 }
2905 break;
2906 case '+':
2907 n = atoi(t->text + 2) - 1;
2908 if (n >= mac->nparam)
2909 tt = NULL;
2910 else {
2911 if (mac->nparam > 1)
2912 n = (n + mac->rotate) % mac->nparam;
2913 tt = mac->params[n];
2914 }
2915 cc = find_cc(tt);
2916 if (cc == -1) {
2917 error(ERR_NONFATAL,
2918 "macro parameter %d is not a condition code",
2919 n + 1);
2920 text = NULL;
2921 } else {
2922 type = TOK_ID;
2923 text = nasm_strdup(conditions[cc]);
2924 }
2925 break;
2926 default:
2927 n = atoi(t->text + 1) - 1;
2928 if (n >= mac->nparam)
2929 tt = NULL;
2930 else {
2931 if (mac->nparam > 1)
2932 n = (n + mac->rotate) % mac->nparam;
2933 tt = mac->params[n];
2934 }
2935 if (tt) {
2936 for (i = 0; i < mac->paramlen[n]; i++) {
2937 *tail = new_Token(NULL, tt->type, tt->text, 0);
2938 tail = &(*tail)->next;
2939 tt = tt->next;
2940 }
2941 }
2942 text = NULL; /* we've done it here */
2943 break;
2944 }
2945 if (!text) {
2946 delete_Token(t);
2947 } else {
2948 *tail = t;
2949 tail = &t->next;
2950 t->type = type;
2951 nasm_free(t->text);
2952 t->text = text;
2953 t->mac = NULL;
2954 }
2955 continue;
2956 } else {
2957 t = *tail = tline;
2958 tline = tline->next;
2959 t->mac = NULL;
2960 tail = &t->next;
2961 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002962 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00002963 *tail = NULL;
2964 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002965 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002966 switch (t->type) {
2967 case TOK_WHITESPACE:
2968 if (tt->type == TOK_WHITESPACE) {
2969 t->next = delete_Token(tt);
2970 }
2971 break;
2972 case TOK_ID:
2973 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002974 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002975 nasm_free(t->text);
2976 t->text = tmp;
2977 t->next = delete_Token(tt);
2978 }
2979 break;
2980 case TOK_NUMBER:
2981 if (tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002982 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002983 nasm_free(t->text);
2984 t->text = tmp;
2985 t->next = delete_Token(tt);
2986 }
2987 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002988 default:
2989 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002990 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002991
H. Peter Anvin76690a12002-04-30 20:52:49 +00002992 return thead;
2993}
2994
2995/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002996 * Expand all single-line macro calls made in the given line.
2997 * Return the expanded version of the line. The original is deemed
2998 * to be destroyed in the process. (In reality we'll just move
2999 * Tokens from input to output a lot of the time, rather than
3000 * actually bothering to destroy and replicate.)
3001 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003002#define DEADMAN_LIMIT (1 << 20)
3003
H. Peter Anvine2c80182005-01-15 22:15:51 +00003004static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003005{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003006 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003007 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003008 Token **params;
3009 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003010 unsigned int nparam, sparam;
3011 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003012 Token *org_tline = tline;
3013 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003014 char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003015 int deadman = DEADMAN_LIMIT;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003016
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003017 /*
3018 * Trick: we should avoid changing the start token pointer since it can
3019 * be contained in "next" field of other token. Because of this
3020 * we allocate a copy of first token and work with it; at the end of
3021 * routine we copy it back
3022 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003023 if (org_tline) {
3024 tline =
3025 new_Token(org_tline->next, org_tline->type, org_tline->text,
3026 0);
3027 tline->mac = org_tline->mac;
3028 nasm_free(org_tline->text);
3029 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003030 }
3031
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003032again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003033 tail = &thead;
3034 thead = NULL;
3035
H. Peter Anvine2c80182005-01-15 22:15:51 +00003036 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003037 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003038 error(ERR_NONFATAL, "interminable macro recursion");
3039 break;
3040 }
3041
H. Peter Anvine2c80182005-01-15 22:15:51 +00003042 if ((mname = tline->text)) {
3043 /* if this token is a local macro, look in local context */
3044 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003045 ctx = get_ctx(mname, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003046 else
3047 ctx = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003048 if (!ctx) {
3049 head = (SMacro *) hash_findix(smacros, mname);
3050 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003051 head = ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003052 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003053 /*
3054 * We've hit an identifier. As in is_mmacro below, we first
3055 * check whether the identifier is a single-line macro at
3056 * all, then think about checking for parameters if
3057 * necessary.
3058 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003059 for (m = head; m; m = m->next)
3060 if (!mstrcmp(m->name, mname, m->casesense))
3061 break;
3062 if (m) {
3063 mstart = tline;
3064 params = NULL;
3065 paramsize = NULL;
3066 if (m->nparam == 0) {
3067 /*
3068 * Simple case: the macro is parameterless. Discard the
3069 * one token that the macro call took, and push the
3070 * expansion back on the to-do stack.
3071 */
3072 if (!m->expansion) {
3073 if (!strcmp("__FILE__", m->name)) {
3074 int32_t num = 0;
3075 src_get(&num, &(tline->text));
3076 nasm_quote(&(tline->text));
3077 tline->type = TOK_STRING;
3078 continue;
3079 }
3080 if (!strcmp("__LINE__", m->name)) {
3081 nasm_free(tline->text);
3082 make_tok_num(tline, src_get_linnum());
3083 continue;
3084 }
3085 if (!strcmp("__BITS__", m->name)) {
3086 nasm_free(tline->text);
3087 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003088 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003089 }
3090 tline = delete_Token(tline);
3091 continue;
3092 }
3093 } else {
3094 /*
3095 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003096 * exists and takes parameters. We must find the
3097 * parameters in the call, count them, find the SMacro
3098 * that corresponds to that form of the macro call, and
3099 * substitute for the parameters when we expand. What a
3100 * pain.
3101 */
3102 /*tline = tline->next;
3103 skip_white_(tline); */
3104 do {
3105 t = tline->next;
3106 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003107 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003108 t->text = NULL;
3109 t = tline->next = delete_Token(t);
3110 }
3111 tline = t;
3112 } while (tok_type_(tline, TOK_WHITESPACE));
3113 if (!tok_is_(tline, "(")) {
3114 /*
3115 * This macro wasn't called with parameters: ignore
3116 * the call. (Behaviour borrowed from gnu cpp.)
3117 */
3118 tline = mstart;
3119 m = NULL;
3120 } else {
3121 int paren = 0;
3122 int white = 0;
3123 brackets = 0;
3124 nparam = 0;
3125 sparam = PARAM_DELTA;
3126 params = nasm_malloc(sparam * sizeof(Token *));
3127 params[0] = tline->next;
3128 paramsize = nasm_malloc(sparam * sizeof(int));
3129 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003130 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003131 /*
3132 * For some unusual expansions
3133 * which concatenates function call
3134 */
3135 t = tline->next;
3136 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003137 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003138 t->text = NULL;
3139 t = tline->next = delete_Token(t);
3140 }
3141 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003142
H. Peter Anvine2c80182005-01-15 22:15:51 +00003143 if (!tline) {
3144 error(ERR_NONFATAL,
3145 "macro call expects terminating `)'");
3146 break;
3147 }
3148 if (tline->type == TOK_WHITESPACE
3149 && brackets <= 0) {
3150 if (paramsize[nparam])
3151 white++;
3152 else
3153 params[nparam] = tline->next;
3154 continue; /* parameter loop */
3155 }
3156 if (tline->type == TOK_OTHER
3157 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003158 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003159 if (ch == ',' && !paren && brackets <= 0) {
3160 if (++nparam >= sparam) {
3161 sparam += PARAM_DELTA;
3162 params = nasm_realloc(params,
3163 sparam *
3164 sizeof(Token
3165 *));
3166 paramsize =
3167 nasm_realloc(paramsize,
3168 sparam *
3169 sizeof(int));
3170 }
3171 params[nparam] = tline->next;
3172 paramsize[nparam] = 0;
3173 white = 0;
3174 continue; /* parameter loop */
3175 }
3176 if (ch == '{' &&
3177 (brackets > 0 || (brackets == 0 &&
3178 !paramsize[nparam])))
3179 {
3180 if (!(brackets++)) {
3181 params[nparam] = tline->next;
3182 continue; /* parameter loop */
3183 }
3184 }
3185 if (ch == '}' && brackets > 0)
3186 if (--brackets == 0) {
3187 brackets = -1;
3188 continue; /* parameter loop */
3189 }
3190 if (ch == '(' && !brackets)
3191 paren++;
3192 if (ch == ')' && brackets <= 0)
3193 if (--paren < 0)
3194 break;
3195 }
3196 if (brackets < 0) {
3197 brackets = 0;
3198 error(ERR_NONFATAL, "braces do not "
3199 "enclose all of macro parameter");
3200 }
3201 paramsize[nparam] += white + 1;
3202 white = 0;
3203 } /* parameter loop */
3204 nparam++;
3205 while (m && (m->nparam != nparam ||
3206 mstrcmp(m->name, mname,
3207 m->casesense)))
3208 m = m->next;
3209 if (!m)
3210 error(ERR_WARNING | ERR_WARN_MNP,
3211 "macro `%s' exists, "
3212 "but not taking %d parameters",
3213 mstart->text, nparam);
3214 }
3215 }
3216 if (m && m->in_progress)
3217 m = NULL;
3218 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003219 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003220 * Design question: should we handle !tline, which
3221 * indicates missing ')' here, or expand those
3222 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003223 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003224 */
3225 nasm_free(params);
3226 nasm_free(paramsize);
3227 tline = mstart;
3228 } else {
3229 /*
3230 * Expand the macro: we are placed on the last token of the
3231 * call, so that we can easily split the call from the
3232 * following tokens. We also start by pushing an SMAC_END
3233 * token for the cycle removal.
3234 */
3235 t = tline;
3236 if (t) {
3237 tline = t->next;
3238 t->next = NULL;
3239 }
3240 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3241 tt->mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003242 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003243 tline = tt;
3244 for (t = m->expansion; t; t = t->next) {
3245 if (t->type >= TOK_SMAC_PARAM) {
3246 Token *pcopy = tline, **ptail = &pcopy;
3247 Token *ttt, *pt;
3248 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003249
H. Peter Anvine2c80182005-01-15 22:15:51 +00003250 ttt = params[t->type - TOK_SMAC_PARAM];
3251 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3252 --i >= 0;) {
3253 pt = *ptail =
3254 new_Token(tline, ttt->type, ttt->text,
3255 0);
3256 ptail = &pt->next;
3257 ttt = ttt->next;
3258 }
3259 tline = pcopy;
3260 } else {
3261 tt = new_Token(tline, t->type, t->text, 0);
3262 tline = tt;
3263 }
3264 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003265
H. Peter Anvine2c80182005-01-15 22:15:51 +00003266 /*
3267 * Having done that, get rid of the macro call, and clean
3268 * up the parameters.
3269 */
3270 nasm_free(params);
3271 nasm_free(paramsize);
3272 free_tlist(mstart);
3273 continue; /* main token loop */
3274 }
3275 }
3276 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003277
H. Peter Anvine2c80182005-01-15 22:15:51 +00003278 if (tline->type == TOK_SMAC_END) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003279 tline->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003280 tline = delete_Token(tline);
3281 } else {
3282 t = *tail = tline;
3283 tline = tline->next;
3284 t->mac = NULL;
3285 t->next = NULL;
3286 tail = &t->next;
3287 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003288 }
3289
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003290 /*
3291 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003292 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003293 * TOK_IDs should be concatenated.
3294 * Also we look for %+ tokens and concatenate the tokens before and after
3295 * them (without white spaces in between).
3296 */
3297 t = thead;
3298 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003299 while (t) {
3300 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3301 t = t->next;
3302 if (!t || !t->next)
3303 break;
3304 if (t->next->type == TOK_ID ||
3305 t->next->type == TOK_PREPROC_ID ||
3306 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003307 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003308 nasm_free(t->text);
3309 t->next = delete_Token(t->next);
3310 t->text = p;
3311 rescan = 1;
3312 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3313 t->next->next->type == TOK_PREPROC_ID &&
3314 strcmp(t->next->next->text, "%+") == 0) {
3315 /* free the next whitespace, the %+ token and next whitespace */
3316 int i;
3317 for (i = 1; i <= 3; i++) {
3318 if (!t->next
3319 || (i != 2 && t->next->type != TOK_WHITESPACE))
3320 break;
3321 t->next = delete_Token(t->next);
3322 } /* endfor */
3323 } else
3324 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003325 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003326 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003327 if (rescan) {
3328 tline = thead;
3329 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003330 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003331
H. Peter Anvine2c80182005-01-15 22:15:51 +00003332 if (org_tline) {
3333 if (thead) {
3334 *org_tline = *thead;
3335 /* since we just gave text to org_line, don't free it */
3336 thead->text = NULL;
3337 delete_Token(thead);
3338 } else {
3339 /* the expression expanded to empty line;
3340 we can't return NULL for some reasons
3341 we just set the line to a single WHITESPACE token. */
3342 memset(org_tline, 0, sizeof(*org_tline));
3343 org_tline->text = NULL;
3344 org_tline->type = TOK_WHITESPACE;
3345 }
3346 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003347 }
3348
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003349 return thead;
3350}
3351
3352/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003353 * Similar to expand_smacro but used exclusively with macro identifiers
3354 * right before they are fetched in. The reason is that there can be
3355 * identifiers consisting of several subparts. We consider that if there
3356 * are more than one element forming the name, user wants a expansion,
3357 * otherwise it will be left as-is. Example:
3358 *
3359 * %define %$abc cde
3360 *
3361 * the identifier %$abc will be left as-is so that the handler for %define
3362 * will suck it and define the corresponding value. Other case:
3363 *
3364 * %define _%$abc cde
3365 *
3366 * In this case user wants name to be expanded *before* %define starts
3367 * working, so we'll expand %$abc into something (if it has a value;
3368 * otherwise it will be left as-is) then concatenate all successive
3369 * PP_IDs into one.
3370 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003371static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003372{
3373 Token *cur, *oldnext = NULL;
3374
H. Peter Anvin734b1882002-04-30 21:01:08 +00003375 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003376 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003377
3378 cur = tline;
3379 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003380 (cur->next->type == TOK_ID ||
3381 cur->next->type == TOK_PREPROC_ID
3382 || cur->next->type == TOK_NUMBER))
3383 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003384
3385 /* If identifier consists of just one token, don't expand */
3386 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003387 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003388
H. Peter Anvine2c80182005-01-15 22:15:51 +00003389 if (cur) {
3390 oldnext = cur->next; /* Detach the tail past identifier */
3391 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003392 }
3393
H. Peter Anvin734b1882002-04-30 21:01:08 +00003394 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003395
H. Peter Anvine2c80182005-01-15 22:15:51 +00003396 if (cur) {
3397 /* expand_smacro possibly changhed tline; re-scan for EOL */
3398 cur = tline;
3399 while (cur && cur->next)
3400 cur = cur->next;
3401 if (cur)
3402 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003403 }
3404
3405 return tline;
3406}
3407
3408/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003409 * Determine whether the given line constitutes a multi-line macro
3410 * call, and return the MMacro structure called if so. Doesn't have
3411 * to check for an initial label - that's taken care of in
3412 * expand_mmacro - but must check numbers of parameters. Guaranteed
3413 * to be called with tline->type == TOK_ID, so the putative macro
3414 * name is easy to find.
3415 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003416static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003417{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003418 MMacro *head, *m;
3419 Token **params;
3420 int nparam;
3421
H. Peter Anvin97a23472007-09-16 17:57:25 -07003422 head = (MMacro *) hash_findix(mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003423
3424 /*
3425 * Efficiency: first we see if any macro exists with the given
3426 * name. If not, we can return NULL immediately. _Then_ we
3427 * count the parameters, and then we look further along the
3428 * list if necessary to find the proper MMacro.
3429 */
3430 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003431 if (!mstrcmp(m->name, tline->text, m->casesense))
3432 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003433 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003434 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003435
3436 /*
3437 * OK, we have a potential macro. Count and demarcate the
3438 * parameters.
3439 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003440 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003441
3442 /*
3443 * So we know how many parameters we've got. Find the MMacro
3444 * structure that handles this number.
3445 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003446 while (m) {
3447 if (m->nparam_min <= nparam
3448 && (m->plus || nparam <= m->nparam_max)) {
3449 /*
3450 * This one is right. Just check if cycle removal
3451 * prohibits us using it before we actually celebrate...
3452 */
3453 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003454#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003455 error(ERR_NONFATAL,
3456 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003457#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003458 nasm_free(params);
3459 return NULL;
3460 }
3461 /*
3462 * It's right, and we can use it. Add its default
3463 * parameters to the end of our list if necessary.
3464 */
3465 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3466 params =
3467 nasm_realloc(params,
3468 ((m->nparam_min + m->ndefs +
3469 1) * sizeof(*params)));
3470 while (nparam < m->nparam_min + m->ndefs) {
3471 params[nparam] = m->defaults[nparam - m->nparam_min];
3472 nparam++;
3473 }
3474 }
3475 /*
3476 * If we've gone over the maximum parameter count (and
3477 * we're in Plus mode), ignore parameters beyond
3478 * nparam_max.
3479 */
3480 if (m->plus && nparam > m->nparam_max)
3481 nparam = m->nparam_max;
3482 /*
3483 * Then terminate the parameter list, and leave.
3484 */
3485 if (!params) { /* need this special case */
3486 params = nasm_malloc(sizeof(*params));
3487 nparam = 0;
3488 }
3489 params[nparam] = NULL;
3490 *params_array = params;
3491 return m;
3492 }
3493 /*
3494 * This one wasn't right: look for the next one with the
3495 * same name.
3496 */
3497 for (m = m->next; m; m = m->next)
3498 if (!mstrcmp(m->name, tline->text, m->casesense))
3499 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003500 }
3501
3502 /*
3503 * After all that, we didn't find one with the right number of
3504 * parameters. Issue a warning, and fail to expand the macro.
3505 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003506 error(ERR_WARNING | ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003507 "macro `%s' exists, but not taking %d parameters",
3508 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003509 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003510 return NULL;
3511}
3512
3513/*
3514 * Expand the multi-line macro call made by the given line, if
3515 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003516 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003517 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003518static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003519{
3520 Token *startline = tline;
3521 Token *label = NULL;
3522 int dont_prepend = 0;
3523 Token **params, *t, *tt;
3524 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003525 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003526 int i, nparam, *paramlen;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003527
3528 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003529 skip_white_(t);
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003530/* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
3531 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003532 return 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003533 m = is_mmacro(t, &params);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003534 if (!m) {
3535 Token *last;
3536 /*
3537 * We have an id which isn't a macro call. We'll assume
3538 * it might be a label; we'll also check to see if a
3539 * colon follows it. Then, if there's another id after
3540 * that lot, we'll check it again for macro-hood.
3541 */
3542 label = last = t;
3543 t = t->next;
3544 if (tok_type_(t, TOK_WHITESPACE))
3545 last = t, t = t->next;
3546 if (tok_is_(t, ":")) {
3547 dont_prepend = 1;
3548 last = t, t = t->next;
3549 if (tok_type_(t, TOK_WHITESPACE))
3550 last = t, t = t->next;
3551 }
3552 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3553 return 0;
3554 last->next = NULL;
3555 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003556 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003557
3558 /*
3559 * Fix up the parameters: this involves stripping leading and
3560 * trailing whitespace, then stripping braces if they are
3561 * present.
3562 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003563 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003564 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003565
H. Peter Anvine2c80182005-01-15 22:15:51 +00003566 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003567 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003568 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003569
H. Peter Anvine2c80182005-01-15 22:15:51 +00003570 t = params[i];
3571 skip_white_(t);
3572 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003573 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003574 params[i] = t;
3575 paramlen[i] = 0;
3576 while (t) {
3577 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3578 break; /* ... because we have hit a comma */
3579 if (comma && t->type == TOK_WHITESPACE
3580 && tok_is_(t->next, ","))
3581 break; /* ... or a space then a comma */
3582 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3583 break; /* ... or a brace */
3584 t = t->next;
3585 paramlen[i]++;
3586 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003587 }
3588
3589 /*
3590 * OK, we have a MMacro structure together with a set of
3591 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003592 * copies of each Line on to istk->expansion. Substitution of
3593 * parameter tokens and macro-local tokens doesn't get done
3594 * until the single-line macro substitution process; this is
3595 * because delaying them allows us to change the semantics
3596 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003597 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003598 * First, push an end marker on to istk->expansion, mark this
3599 * macro as in progress, and set up its invocation-specific
3600 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003601 */
3602 ll = nasm_malloc(sizeof(Line));
3603 ll->next = istk->expansion;
3604 ll->finishes = m;
3605 ll->first = NULL;
3606 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003607
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003608 m->in_progress = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003609 m->params = params;
3610 m->iline = tline;
3611 m->nparam = nparam;
3612 m->rotate = 0;
3613 m->paramlen = paramlen;
3614 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003615 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003616
3617 m->next_active = istk->mstk;
3618 istk->mstk = m;
3619
H. Peter Anvine2c80182005-01-15 22:15:51 +00003620 for (l = m->expansion; l; l = l->next) {
3621 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003622
H. Peter Anvine2c80182005-01-15 22:15:51 +00003623 ll = nasm_malloc(sizeof(Line));
3624 ll->finishes = NULL;
3625 ll->next = istk->expansion;
3626 istk->expansion = ll;
3627 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003628
H. Peter Anvine2c80182005-01-15 22:15:51 +00003629 for (t = l->first; t; t = t->next) {
3630 Token *x = t;
3631 if (t->type == TOK_PREPROC_ID &&
3632 t->text[1] == '0' && t->text[2] == '0') {
3633 dont_prepend = -1;
3634 x = label;
3635 if (!x)
3636 continue;
3637 }
3638 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3639 tail = &tt->next;
3640 }
3641 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003642 }
3643
3644 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00003645 * If we had a label, push it on as the first line of
3646 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003647 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003648 if (label) {
3649 if (dont_prepend < 0)
3650 free_tlist(startline);
3651 else {
3652 ll = nasm_malloc(sizeof(Line));
3653 ll->finishes = NULL;
3654 ll->next = istk->expansion;
3655 istk->expansion = ll;
3656 ll->first = startline;
3657 if (!dont_prepend) {
3658 while (label->next)
3659 label = label->next;
3660 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3661 }
3662 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003663 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003664
H. Peter Anvin734b1882002-04-30 21:01:08 +00003665 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003666
H. Peter Anvineba20a72002-04-30 20:53:55 +00003667 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003668}
3669
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003670/*
3671 * Since preprocessor always operate only on the line that didn't
3672 * arrived yet, we should always use ERR_OFFBY1. Also since user
3673 * won't want to see same error twice (preprocessing is done once
3674 * per pass) we will want to show errors only during pass one.
3675 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003676static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003677{
3678 va_list arg;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003679 char buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003680
3681 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003682 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003683 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003684
H. Peter Anvin734b1882002-04-30 21:01:08 +00003685 va_start(arg, fmt);
Ed Beroset19f927a2004-12-15 17:07:03 +00003686 vsnprintf(buff, sizeof(buff), fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003687 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003688
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003689 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003690 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3691 istk->mstk->lineno, buff);
3692 else
3693 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003694}
3695
H. Peter Anvin734b1882002-04-30 21:01:08 +00003696static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003697pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003698 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003699{
H. Peter Anvin99941bf2002-05-14 17:44:03 +00003700 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003701 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003702 istk = nasm_malloc(sizeof(Include));
3703 istk->next = NULL;
3704 istk->conds = NULL;
3705 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003706 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003707 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00003708 istk->fname = NULL;
3709 src_set_fname(nasm_strdup(file));
3710 src_set_linnum(0);
3711 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003712 if (!istk->fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003713 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3714 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003715 defining = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003716 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003717 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003718 if (tasm_compatible_mode) {
3719 stdmacpos = stdmac;
3720 } else {
3721 stdmacpos = &stdmac[TASM_MACRO_COUNT];
3722 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003723 any_extrastdmac = (extrastdmac != NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003724 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003725 evaluate = eval;
3726 pass = apass;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003727}
3728
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003729static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003730{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003731 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003732 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003733
H. Peter Anvine2c80182005-01-15 22:15:51 +00003734 while (1) {
3735 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003736 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00003737 * buffer or from the input file.
3738 */
3739 tline = NULL;
3740 while (istk->expansion && istk->expansion->finishes) {
3741 Line *l = istk->expansion;
3742 if (!l->finishes->name && l->finishes->in_progress > 1) {
3743 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003744
H. Peter Anvine2c80182005-01-15 22:15:51 +00003745 /*
3746 * This is a macro-end marker for a macro with no
3747 * name, which means it's not really a macro at all
3748 * but a %rep block, and the `in_progress' field is
3749 * more than 1, meaning that we still need to
3750 * repeat. (1 means the natural last repetition; 0
3751 * means termination by %exitrep.) We have
3752 * therefore expanded up to the %endrep, and must
3753 * push the whole block on to the expansion buffer
3754 * again. We don't bother to remove the macro-end
3755 * marker: we'd only have to generate another one
3756 * if we did.
3757 */
3758 l->finishes->in_progress--;
3759 for (l = l->finishes->expansion; l; l = l->next) {
3760 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003761
H. Peter Anvine2c80182005-01-15 22:15:51 +00003762 ll = nasm_malloc(sizeof(Line));
3763 ll->next = istk->expansion;
3764 ll->finishes = NULL;
3765 ll->first = NULL;
3766 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003767
H. Peter Anvine2c80182005-01-15 22:15:51 +00003768 for (t = l->first; t; t = t->next) {
3769 if (t->text || t->type == TOK_WHITESPACE) {
3770 tt = *tail =
3771 new_Token(NULL, t->type, t->text, 0);
3772 tail = &tt->next;
3773 }
3774 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003775
H. Peter Anvine2c80182005-01-15 22:15:51 +00003776 istk->expansion = ll;
3777 }
3778 } else {
3779 /*
3780 * Check whether a `%rep' was started and not ended
3781 * within this macro expansion. This can happen and
3782 * should be detected. It's a fatal error because
3783 * I'm too confused to work out how to recover
3784 * sensibly from it.
3785 */
3786 if (defining) {
3787 if (defining->name)
3788 error(ERR_PANIC,
3789 "defining with name in expansion");
3790 else if (istk->mstk->name)
3791 error(ERR_FATAL,
3792 "`%%rep' without `%%endrep' within"
3793 " expansion of macro `%s'",
3794 istk->mstk->name);
3795 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00003796
H. Peter Anvine2c80182005-01-15 22:15:51 +00003797 /*
3798 * FIXME: investigate the relationship at this point between
3799 * istk->mstk and l->finishes
3800 */
3801 {
3802 MMacro *m = istk->mstk;
3803 istk->mstk = m->next_active;
3804 if (m->name) {
3805 /*
3806 * This was a real macro call, not a %rep, and
3807 * therefore the parameter information needs to
3808 * be freed.
3809 */
3810 nasm_free(m->params);
3811 free_tlist(m->iline);
3812 nasm_free(m->paramlen);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003813 l->finishes->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003814 } else
3815 free_mmacro(m);
3816 }
3817 istk->expansion = l->next;
3818 nasm_free(l);
3819 list->downlevel(LIST_MACRO);
3820 }
3821 }
3822 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003823
H. Peter Anvine2c80182005-01-15 22:15:51 +00003824 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003825 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003826 Line *l = istk->expansion;
3827 if (istk->mstk)
3828 istk->mstk->lineno++;
3829 tline = l->first;
3830 istk->expansion = l->next;
3831 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003832 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003833 list->line(LIST_MACRO, p);
3834 nasm_free(p);
3835 break;
3836 }
3837 line = read_line();
3838 if (line) { /* from the current input file */
3839 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00003840 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003841 nasm_free(line);
3842 break;
3843 }
3844 /*
3845 * The current file has ended; work down the istk
3846 */
3847 {
3848 Include *i = istk;
3849 fclose(i->fp);
3850 if (i->conds)
3851 error(ERR_FATAL,
3852 "expected `%%endif' before end of file");
3853 /* only set line and file name if there's a next node */
3854 if (i->next) {
3855 src_set_linnum(i->lineno);
3856 nasm_free(src_set_fname(i->fname));
3857 }
3858 istk = i->next;
3859 list->downlevel(LIST_INCLUDE);
3860 nasm_free(i);
3861 if (!istk)
3862 return NULL;
3863 }
3864 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003865
H. Peter Anvine2c80182005-01-15 22:15:51 +00003866 /*
3867 * We must expand MMacro parameters and MMacro-local labels
3868 * _before_ we plunge into directive processing, to cope
3869 * with things like `%define something %1' such as STRUC
3870 * uses. Unless we're _defining_ a MMacro, in which case
3871 * those tokens should be left alone to go into the
3872 * definition; and unless we're in a non-emitting
3873 * condition, in which case we don't want to meddle with
3874 * anything.
3875 */
3876 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
3877 tline = expand_mmac_params(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003878
H. Peter Anvine2c80182005-01-15 22:15:51 +00003879 /*
3880 * Check the line to see if it's a preprocessor directive.
3881 */
3882 if (do_directive(tline) == DIRECTIVE_FOUND) {
3883 continue;
3884 } else if (defining) {
3885 /*
3886 * We're defining a multi-line macro. We emit nothing
3887 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00003888 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003889 */
3890 Line *l = nasm_malloc(sizeof(Line));
3891 l->next = defining->expansion;
3892 l->first = tline;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003893 l->finishes = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003894 defining->expansion = l;
3895 continue;
3896 } else if (istk->conds && !emitting(istk->conds->state)) {
3897 /*
3898 * We're in a non-emitting branch of a condition block.
3899 * Emit nothing at all, not even a blank line: when we
3900 * emerge from the condition we'll give a line-number
3901 * directive so we keep our place correctly.
3902 */
3903 free_tlist(tline);
3904 continue;
3905 } else if (istk->mstk && !istk->mstk->in_progress) {
3906 /*
3907 * We're in a %rep block which has been terminated, so
3908 * we're walking through to the %endrep without
3909 * emitting anything. Emit nothing at all, not even a
3910 * blank line: when we emerge from the %rep block we'll
3911 * give a line-number directive so we keep our place
3912 * correctly.
3913 */
3914 free_tlist(tline);
3915 continue;
3916 } else {
3917 tline = expand_smacro(tline);
3918 if (!expand_mmacro(tline)) {
3919 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003920 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003921 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003922 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003923 free_tlist(tline);
3924 break;
3925 } else {
3926 continue; /* expand_mmacro calls free_tlist */
3927 }
3928 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003929 }
3930
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003931 return line;
3932}
3933
H. Peter Anvine2c80182005-01-15 22:15:51 +00003934static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003935{
H. Peter Anvine2c80182005-01-15 22:15:51 +00003936 if (defining) {
3937 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
3938 defining->name);
3939 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003940 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003941 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003942 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07003943 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00003944 while (istk) {
3945 Include *i = istk;
3946 istk = istk->next;
3947 fclose(i->fp);
3948 nasm_free(i->fname);
3949 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003950 }
3951 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003952 ctx_pop();
3953 if (pass == 0) {
3954 free_llist(predef);
3955 delete_Blocks();
3956 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003957}
3958
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003959void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003960{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003961 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07003962
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003963 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07003964 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003965 i->next = NULL;
3966
H. Peter Anvine2c80182005-01-15 22:15:51 +00003967 if (ipath != NULL) {
3968 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003969 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003970 j = j->next;
3971 j->next = i;
3972 } else {
3973 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003974 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003975}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003976
3977/*
3978 * added by alexfru:
3979 *
3980 * This function is used to "export" the include paths, e.g.
3981 * the paths specified in the '-I' command switch.
3982 * The need for such exporting is due to the 'incbin' directive,
3983 * which includes raw binary files (unlike '%include', which
3984 * includes text source files). It would be real nice to be
3985 * able to specify paths to search for incbin'ned files also.
3986 * So, this is a simple workaround.
3987 *
3988 * The function use is simple:
3989 *
3990 * The 1st call (with NULL argument) returns a pointer to the 1st path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003991 * (char** type) or NULL if none include paths available.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003992 *
3993 * All subsequent calls take as argument the value returned by this
3994 * function last. The return value is either the next path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003995 * (char** type) or NULL if the end of the paths list is reached.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003996 *
3997 * It is maybe not the best way to do things, but I didn't want
3998 * to export too much, just one or two functions and no types or
3999 * variables exported.
4000 *
4001 * Can't say I like the current situation with e.g. this path list either,
4002 * it seems to be never deallocated after creation...
4003 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004004char **pp_get_include_path_ptr(char **pPrevPath)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004005{
4006/* This macro returns offset of a member of a structure */
4007#define GetMemberOffset(StructType,MemberName)\
4008 ((size_t)&((StructType*)0)->MemberName)
4009 IncPath *i;
4010
H. Peter Anvine2c80182005-01-15 22:15:51 +00004011 if (pPrevPath == NULL) {
4012 if (ipath != NULL)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004013 return &ipath->path;
4014 else
4015 return NULL;
4016 }
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004017 i = (IncPath *) ((char *)pPrevPath - GetMemberOffset(IncPath, path));
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004018 i = i->next;
4019 if (i != NULL)
4020 return &i->path;
4021 else
4022 return NULL;
4023#undef GetMemberOffset
4024}
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004025
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004026void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004027{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004028 Token *inc, *space, *name;
4029 Line *l;
4030
H. Peter Anvin734b1882002-04-30 21:01:08 +00004031 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4032 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4033 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004034
4035 l = nasm_malloc(sizeof(Line));
4036 l->next = predef;
4037 l->first = inc;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004038 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004039 predef = l;
4040}
4041
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004042void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004043{
4044 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004045 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004046 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004047
4048 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004049 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4050 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004051 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004052 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004053 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004054 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004055 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004056
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004057 l = nasm_malloc(sizeof(Line));
4058 l->next = predef;
4059 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004060 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004061 predef = l;
4062}
4063
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004064void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004065{
4066 Token *def, *space;
4067 Line *l;
4068
H. Peter Anvin734b1882002-04-30 21:01:08 +00004069 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4070 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004071 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004072
4073 l = nasm_malloc(sizeof(Line));
4074 l->next = predef;
4075 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004076 l->finishes = false;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004077 predef = l;
4078}
4079
Keith Kaniosb7a89542007-04-12 02:40:54 +00004080/*
4081 * Added by Keith Kanios:
4082 *
4083 * This function is used to assist with "runtime" preprocessor
4084 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4085 *
4086 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4087 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4088 */
4089
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004090void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004091{
4092 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004093
Keith Kaniosb7a89542007-04-12 02:40:54 +00004094 def = tokenize(definition);
4095 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4096 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004097
Keith Kaniosb7a89542007-04-12 02:40:54 +00004098}
4099
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004100void pp_extra_stdmac(const char **macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004101{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004102 extrastdmac = macros;
4103}
4104
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004105static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004106{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004107 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004108 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004109 tok->text = nasm_strdup(numbuf);
4110 tok->type = TOK_NUMBER;
4111}
4112
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004113Preproc nasmpp = {
4114 pp_reset,
4115 pp_getline,
4116 pp_cleanup
4117};