blob: fe9a67e143ea65eba58fa087de44ab65d563f5b7 [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 Anvina4835d42008-05-20 14:21:29 -070053#include "tables.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000054
55typedef struct SMacro SMacro;
56typedef struct MMacro MMacro;
57typedef struct Context Context;
58typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000059typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000060typedef struct Line Line;
61typedef struct Include Include;
62typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000063typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000064
65/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070066 * Note on the storage of both SMacro and MMacros: the hash table
67 * indexes them case-insensitively, and we then have to go through a
68 * linked list of potential case aliases (and, for MMacros, parameter
69 * ranges); this is to preserve the matching semantics of the earlier
70 * code. If the number of case aliases for a specific macro is a
71 * performance issue, you may want to reconsider your coding style.
72 */
73
74/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000075 * Store the definition of a single-line macro.
76 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000077struct SMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000078 SMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +000079 char *name;
H. Peter Anvin70055962007-10-11 00:05:31 -070080 bool casesense;
H. Peter Anvin16ed4382007-10-11 10:06:19 -070081 bool in_progress;
H. Peter Anvin70055962007-10-11 00:05:31 -070082 unsigned int nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000083 Token *expansion;
84};
85
86/*
H. Peter Anvin76690a12002-04-30 20:52:49 +000087 * Store the definition of a multi-line macro. This is also used to
88 * store the interiors of `%rep...%endrep' blocks, which are
89 * effectively self-re-invoking multi-line macros which simply
90 * don't have a name or bother to appear in the hash tables. %rep
91 * blocks are signified by having a NULL `name' field.
92 *
93 * In a MMacro describing a `%rep' block, the `in_progress' field
94 * isn't merely boolean, but gives the number of repeats left to
95 * run.
96 *
97 * The `next' field is used for storing MMacros in hash tables; the
98 * `next_active' field is for stacking them on istk entries.
99 *
100 * When a MMacro is being expanded, `params', `iline', `nparam',
101 * `paramlen', `rotate' and `unique' are local to the invocation.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000102 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000103struct MMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000104 MMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000105 char *name;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -0700106 int nparam_min, nparam_max;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700107 bool casesense;
108 bool plus; /* is the last parameter greedy? */
109 bool nolist; /* is this macro listing-inhibited? */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700110 int64_t in_progress;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000111 Token *dlist; /* All defaults as one list */
112 Token **defaults; /* Parameter default pointers */
113 int ndefs; /* number of default parameters */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000114 Line *expansion;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000115
116 MMacro *next_active;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000117 MMacro *rep_nest; /* used for nesting %rep */
118 Token **params; /* actual parameters */
119 Token *iline; /* invocation line */
H. Peter Anvin25a99342007-09-22 17:45:45 -0700120 unsigned int nparam, rotate;
121 int *paramlen;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700122 uint64_t unique;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000123 int lineno; /* Current line number on expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000124};
125
126/*
127 * The context stack is composed of a linked list of these.
128 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000129struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000130 Context *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000131 char *name;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700132 struct hash_table localmac;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000133 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000134};
135
136/*
137 * This is the internal form which we break input lines up into.
138 * Typically stored in linked lists.
139 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000140 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
141 * necessarily used as-is, but is intended to denote the number of
142 * the substituted parameter. So in the definition
143 *
144 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700145 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000146 * the token representing `x' will have its type changed to
147 * TOK_SMAC_PARAM, but the one representing `y' will be
148 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000149 *
150 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
151 * which doesn't need quotes around it. Used in the pre-include
152 * mechanism as an alternative to trying to find a sensible type of
153 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000154 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000155enum pp_token_type {
156 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
157 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700158 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
159 TOK_INTERNAL_STRING,
160 TOK_PREPROC_Q, TOK_PREPROC_QQ,
H. Peter Anvin5b76fa22008-05-26 11:14:38 -0700161 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
162 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000163};
164
H. Peter Anvine2c80182005-01-15 22:15:51 +0000165struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000166 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000167 char *text;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000168 SMacro *mac; /* associated macro for TOK_SMAC_END */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000169 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000170};
171
172/*
173 * Multi-line macro definitions are stored as a linked list of
174 * these, which is essentially a container to allow several linked
175 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700176 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000177 * Note that in this module, linked lists are treated as stacks
178 * wherever possible. For this reason, Lines are _pushed_ on to the
179 * `expansion' field in MMacro structures, so that the linked list,
180 * if walked, would give the macro lines in reverse order; this
181 * means that we can walk the list when expanding a macro, and thus
182 * push the lines on to the `expansion' field in _istk_ in reverse
183 * order (so that when popped back off they are in the right
184 * order). It may seem cockeyed, and it relies on my design having
185 * an even number of steps in, but it works...
186 *
187 * Some of these structures, rather than being actual lines, are
188 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000189 * This is for use in the cycle-tracking and %rep-handling code.
190 * Such structures have `finishes' non-NULL, and `first' NULL. All
191 * others have `finishes' NULL, but `first' may still be NULL if
192 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000193 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000194struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000195 Line *next;
196 MMacro *finishes;
197 Token *first;
198};
199
200/*
201 * To handle an arbitrary level of file inclusion, we maintain a
202 * stack (ie linked list) of these things.
203 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000204struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000205 Include *next;
206 FILE *fp;
207 Cond *conds;
208 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000209 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000210 int lineno, lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000211 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000212};
213
214/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000215 * Include search path. This is simply a list of strings which get
216 * prepended, in turn, to the name of an include file, in an
217 * attempt to find the file if it's not in the current directory.
218 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000219struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000220 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000221 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000222};
223
224/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000225 * Conditional assembly: we maintain a separate stack of these for
226 * each level of file inclusion. (The only reason we keep the
227 * stacks separate is to ensure that a stray `%endif' in a file
228 * included from within the true branch of a `%if' won't terminate
229 * it and cause confusion: instead, rightly, it'll cause an error.)
230 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000231struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000232 Cond *next;
233 int state;
234};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000235enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000236 /*
237 * These states are for use just after %if or %elif: IF_TRUE
238 * means the condition has evaluated to truth so we are
239 * currently emitting, whereas IF_FALSE means we are not
240 * currently emitting but will start doing so if a %else comes
241 * up. In these states, all directives are admissible: %elif,
242 * %else and %endif. (And of course %if.)
243 */
244 COND_IF_TRUE, COND_IF_FALSE,
245 /*
246 * These states come up after a %else: ELSE_TRUE means we're
247 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
248 * any %elif or %else will cause an error.
249 */
250 COND_ELSE_TRUE, COND_ELSE_FALSE,
251 /*
252 * This state means that we're not emitting now, and also that
253 * nothing until %endif will be emitted at all. It's for use in
254 * two circumstances: (i) when we've had our moment of emission
255 * and have now started seeing %elifs, and (ii) when the
256 * condition construct in question is contained within a
257 * non-emitting branch of a larger condition construct.
258 */
259 COND_NEVER
260};
261#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
262
H. Peter Anvin70653092007-10-19 14:42:29 -0700263/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000264 * These defines are used as the possible return values for do_directive
265 */
266#define NO_DIRECTIVE_FOUND 0
267#define DIRECTIVE_FOUND 1
268
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000269/*
270 * Condition codes. Note that we use c_ prefix not C_ because C_ is
271 * used in nasm.h for the "real" condition codes. At _this_ level,
272 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
273 * ones, so we need a different enum...
274 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700275static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000276 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
277 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000278 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000279};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700280enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000281 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
282 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 -0700283 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
284 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000285};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700286static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000287 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
288 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 +0000289 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000290};
291
H. Peter Anvin76690a12002-04-30 20:52:49 +0000292/*
293 * Directive names.
294 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000295/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000296static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000297{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000298 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000299}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000300
301/* For TASM compatibility we need to be able to recognise TASM compatible
302 * conditional compilation directives. Using the NASM pre-processor does
303 * not work, so we look for them specifically from the following list and
304 * then jam in the equivalent NASM directive into the input stream.
305 */
306
H. Peter Anvine2c80182005-01-15 22:15:51 +0000307enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000308 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
309 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
310};
311
H. Peter Anvin476d2862007-10-02 22:04:15 -0700312static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000313 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
314 "ifndef", "include", "local"
315};
316
317static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000318static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000319static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800320static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000321
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000322static Context *cstk;
323static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000324static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000325
H. Peter Anvine2c80182005-01-15 22:15:51 +0000326static efunc _error; /* Pointer to client-provided error reporting function */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000327static evalfunc evaluate;
328
H. Peter Anvine2c80182005-01-15 22:15:51 +0000329static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000330
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700331static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000332
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000333static Line *predef = NULL;
334
335static ListGen *list;
336
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000337/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000338 * The current set of multi-line macros we have defined.
339 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700340static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341
342/*
343 * The current set of single-line macros we have defined.
344 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700345static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000346
347/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000348 * The multi-line macro we are currently defining, or the %rep
349 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000350 */
351static MMacro *defining;
352
353/*
354 * The number of macro parameters to allocate space for at a time.
355 */
356#define PARAM_DELTA 16
357
358/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700359 * The standard macro set: defined in macros.c in the array nasm_stdmac.
360 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000361 */
H. Peter Anvin188ce762008-02-16 13:58:45 -0800362static const char * const *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000363
364/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000365 * The extra standard macros that come from the object format, if
366 * any.
367 */
H. Peter Anvin188ce762008-02-16 13:58:45 -0800368static const char * const *extrastdmac = NULL;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700369bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000370
371/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000372 * Tokens are allocated in blocks to improve speed
373 */
374#define TOKEN_BLOCKSIZE 4096
375static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000376struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000377 Blocks *next;
378 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000379};
380
381static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000382
383/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000384 * Forward declarations.
385 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000386static Token *expand_mmac_params(Token * tline);
387static Token *expand_smacro(Token * tline);
388static Token *expand_id(Token * tline);
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700389static Context *get_ctx(char *name, bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700390static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000391static void error(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000392static void *new_Block(size_t size);
393static void delete_Blocks(void);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000394static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000395static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000396
397/*
398 * Macros for safe checking of token pointers, avoid *(NULL)
399 */
400#define tok_type_(x,t) ((x) && (x)->type == (t))
401#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
402#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
403#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000404
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000405/* Handle TASM specific directives, which do not contain a % in
406 * front of them. We do it here because I could not find any other
407 * place to do it for the moment, and it is a hack (ideally it would
408 * be nice to be able to use the NASM pre-processor to do it).
409 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000410static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000411{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000412 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000413 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000414
415 /* Skip whitespace */
416 while (isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000417 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000418
419 /* Binary search for the directive name */
420 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000421 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000422 len = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000423 while (!isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000424 len++;
425 if (len) {
426 oldchar = p[len];
427 p[len] = 0;
428 while (j - i > 1) {
429 k = (j + i) / 2;
430 m = nasm_stricmp(p, tasm_directives[k]);
431 if (m == 0) {
432 /* We have found a directive, so jam a % in front of it
433 * so that NASM will then recognise it as one if it's own.
434 */
435 p[len] = oldchar;
436 len = strlen(p);
437 oldline = line;
438 line = nasm_malloc(len + 2);
439 line[0] = '%';
440 if (k == TM_IFDIFI) {
441 /* NASM does not recognise IFDIFI, so we convert it to
442 * %ifdef BOGUS. This is not used in NASM comaptible
443 * code, but does need to parse for the TASM macro
444 * package.
445 */
446 strcpy(line + 1, "ifdef BOGUS");
447 } else {
448 memcpy(line + 1, p, len + 1);
449 }
450 nasm_free(oldline);
451 return line;
452 } else if (m < 0) {
453 j = k;
454 } else
455 i = k;
456 }
457 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000458 }
459 return line;
460}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000461
H. Peter Anvin76690a12002-04-30 20:52:49 +0000462/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000463 * The pre-preprocessing stage... This function translates line
464 * number indications as they emerge from GNU cpp (`# lineno "file"
465 * flags') into NASM preprocessor line number indications (`%line
466 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000467 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000468static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000469{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000470 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000471 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000472
H. Peter Anvine2c80182005-01-15 22:15:51 +0000473 if (line[0] == '#' && line[1] == ' ') {
474 oldline = line;
475 fname = oldline + 2;
476 lineno = atoi(fname);
477 fname += strspn(fname, "0123456789 ");
478 if (*fname == '"')
479 fname++;
480 fnlen = strcspn(fname, "\"");
481 line = nasm_malloc(20 + fnlen);
482 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
483 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000484 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000485 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000486 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000487 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000488}
489
490/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000491 * Free a linked list of tokens.
492 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000493static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000494{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000495 while (list) {
496 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000497 }
498}
499
500/*
501 * Free a linked list of lines.
502 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000504{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000505 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000506 while (list) {
507 l = list;
508 list = list->next;
509 free_tlist(l->first);
510 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000511 }
512}
513
514/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000515 * Free an MMacro
516 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000517static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000518{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000519 nasm_free(m->name);
520 free_tlist(m->dlist);
521 nasm_free(m->defaults);
522 free_llist(m->expansion);
523 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000524}
525
526/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700527 * Free all currently defined macros, and free the hash tables
528 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700529static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700530{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700531 SMacro *s;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700532 const char *key;
533 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700534
H. Peter Anvin072771e2008-05-22 13:17:51 -0700535 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700536 nasm_free((void *)key);
537 while (s) {
538 SMacro *ns = s->next;
539 nasm_free(s->name);
540 free_tlist(s->expansion);
541 nasm_free(s);
542 s = ns;
543 }
544 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700545 hash_free(smt);
546}
547
548static void free_mmacro_table(struct hash_table *mmt)
549{
550 MMacro *m;
551 const char *key;
552 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700553
554 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700555 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700556 nasm_free((void *)key);
557 while (m) {
558 MMacro *nm = m->next;
559 free_mmacro(m);
560 m = nm;
561 }
562 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700563 hash_free(mmt);
564}
565
566static void free_macros(void)
567{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700568 free_smacro_table(&smacros);
569 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700570}
571
572/*
573 * Initialize the hash tables
574 */
575static void init_macros(void)
576{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700577 hash_init(&smacros, HASH_LARGE);
578 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700579}
580
581/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000582 * Pop the context stack.
583 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000584static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000585{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000586 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000587
588 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700589 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000590 nasm_free(c->name);
591 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000592}
593
H. Peter Anvin072771e2008-05-22 13:17:51 -0700594/*
595 * Search for a key in the hash index; adding it if necessary
596 * (in which case we initialize the data pointer to NULL.)
597 */
598static void **
599hash_findi_add(struct hash_table *hash, const char *str)
600{
601 struct hash_insert hi;
602 void **r;
603 char *strx;
604
605 r = hash_findi(hash, str, &hi);
606 if (r)
607 return r;
608
609 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
610 return hash_add(&hi, strx, NULL);
611}
612
613/*
614 * Like hash_findi, but returns the data element rather than a pointer
615 * to it. Used only when not adding a new element, hence no third
616 * argument.
617 */
618static void *
619hash_findix(struct hash_table *hash, const char *str)
620{
621 void **p;
622
623 p = hash_findi(hash, str, NULL);
624 return p ? *p : NULL;
625}
626
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000627#define BUF_DELTA 512
628/*
629 * Read a line from the top file in istk, handling multiple CR/LFs
630 * at the end of the line read, and handling spurious ^Zs. Will
631 * return lines from the standard macro set if this has not already
632 * been done.
633 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000634static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000635{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000636 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000637 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000638
H. Peter Anvine2c80182005-01-15 22:15:51 +0000639 if (stdmacpos) {
640 if (*stdmacpos) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000641 char *ret = nasm_strdup(*stdmacpos++);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000642 if (!*stdmacpos && any_extrastdmac) {
643 stdmacpos = extrastdmac;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700644 any_extrastdmac = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000645 return ret;
646 }
647 /*
648 * Nasty hack: here we push the contents of `predef' on
649 * to the top-level expansion stack, since this is the
650 * most convenient way to implement the pre-include and
651 * pre-define features.
652 */
653 if (!*stdmacpos) {
654 Line *pd, *l;
655 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000656
H. Peter Anvine2c80182005-01-15 22:15:51 +0000657 for (pd = predef; pd; pd = pd->next) {
658 head = NULL;
659 tail = &head;
660 for (t = pd->first; t; t = t->next) {
661 *tail = new_Token(NULL, t->type, t->text, 0);
662 tail = &(*tail)->next;
663 }
664 l = nasm_malloc(sizeof(Line));
665 l->next = istk->expansion;
666 l->first = head;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700667 l->finishes = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000668 istk->expansion = l;
669 }
670 }
671 return ret;
672 } else {
673 stdmacpos = NULL;
674 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000675 }
676
677 bufsize = BUF_DELTA;
678 buffer = nasm_malloc(BUF_DELTA);
679 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000680 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000681 while (1) {
682 q = fgets(p, bufsize - (p - buffer), istk->fp);
683 if (!q)
684 break;
685 p += strlen(p);
686 if (p > buffer && p[-1] == '\n') {
687 /* Convert backslash-CRLF line continuation sequences into
688 nothing at all (for DOS and Windows) */
689 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
690 p -= 3;
691 *p = 0;
692 continued_count++;
693 }
694 /* Also convert backslash-LF line continuation sequences into
695 nothing at all (for Unix) */
696 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
697 p -= 2;
698 *p = 0;
699 continued_count++;
700 } else {
701 break;
702 }
703 }
704 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000705 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000706 bufsize += BUF_DELTA;
707 buffer = nasm_realloc(buffer, bufsize);
708 p = buffer + offset; /* prevent stale-pointer problems */
709 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000710 }
711
H. Peter Anvine2c80182005-01-15 22:15:51 +0000712 if (!q && p == buffer) {
713 nasm_free(buffer);
714 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000715 }
716
H. Peter Anvine2c80182005-01-15 22:15:51 +0000717 src_set_linnum(src_get_linnum() + istk->lineinc +
718 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000719
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000720 /*
721 * Play safe: remove CRs as well as LFs, if any of either are
722 * present at the end of the line.
723 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000724 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000725 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000726
727 /*
728 * Handle spurious ^Z, which may be inserted into source files
729 * by some file transfer utilities.
730 */
731 buffer[strcspn(buffer, "\032")] = '\0';
732
H. Peter Anvin734b1882002-04-30 21:01:08 +0000733 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000734
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000735 return buffer;
736}
737
738/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000739 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000740 * don't need to parse the value out of e.g. numeric tokens: we
741 * simply split one string into many.
742 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000743static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000744{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000745 char *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000746 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000747 Token *list = NULL;
748 Token *t, **tail = &list;
749
H. Peter Anvine2c80182005-01-15 22:15:51 +0000750 while (*line) {
751 p = line;
752 if (*p == '%') {
753 p++;
754 if (isdigit(*p) ||
755 ((*p == '-' || *p == '+') && isdigit(p[1])) ||
756 ((*p == '+') && (isspace(p[1]) || !p[1]))) {
757 do {
758 p++;
759 }
760 while (isdigit(*p));
761 type = TOK_PREPROC_ID;
762 } else if (*p == '{') {
763 p++;
764 while (*p && *p != '}') {
765 p[-1] = *p;
766 p++;
767 }
768 p[-1] = '\0';
769 if (*p)
770 p++;
771 type = TOK_PREPROC_ID;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700772 } else if (*p == '?') {
773 type = TOK_PREPROC_Q; /* %? */
774 p++;
775 if (*p == '?') {
776 type = TOK_PREPROC_QQ; /* %?? */
777 p++;
778 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000779 } else if (isidchar(*p) ||
780 ((*p == '!' || *p == '%' || *p == '$') &&
781 isidchar(p[1]))) {
782 do {
783 p++;
784 }
785 while (isidchar(*p));
786 type = TOK_PREPROC_ID;
787 } else {
788 type = TOK_OTHER;
789 if (*p == '%')
790 p++;
791 }
792 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
793 type = TOK_ID;
794 p++;
795 while (*p && isidchar(*p))
796 p++;
797 } else if (*p == '\'' || *p == '"') {
798 /*
799 * A string token.
800 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000801 char c = *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000802 p++;
803 type = TOK_STRING;
804 while (*p && *p != c)
805 p++;
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000806
H. Peter Anvine2c80182005-01-15 22:15:51 +0000807 if (*p) {
808 p++;
809 } else {
810 error(ERR_WARNING, "unterminated string");
811 /* Handling unterminated strings by UNV */
812 /* type = -1; */
813 }
814 } else if (isnumstart(*p)) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700815 bool is_hex = false;
816 bool is_float = false;
817 bool has_e = false;
818 char c, *r;
819
H. Peter Anvine2c80182005-01-15 22:15:51 +0000820 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700821 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000822 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700823
824 if (*p == '$') {
825 p++;
826 is_hex = true;
827 }
828
829 for (;;) {
830 c = *p++;
831
832 if (!is_hex && (c == 'e' || c == 'E')) {
833 has_e = true;
834 if (*p == '+' || *p == '-') {
835 /* e can only be followed by +/- if it is either a
836 prefixed hex number or a floating-point number */
837 p++;
838 is_float = true;
839 }
840 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
841 is_hex = true;
842 } else if (c == 'P' || c == 'p') {
843 is_float = true;
844 if (*p == '+' || *p == '-')
845 p++;
846 } else if (isnumchar(c) || c == '_')
847 ; /* just advance */
848 else if (c == '.') {
849 /* we need to deal with consequences of the legacy
850 parser, like "1.nolist" being two tokens
851 (TOK_NUMBER, TOK_ID) here; at least give it
852 a shot for now. In the future, we probably need
853 a flex-based scanner with proper pattern matching
854 to do it as well as it can be done. Nothing in
855 the world is going to help the person who wants
856 0x123.p16 interpreted as two tokens, though. */
857 r = p;
858 while (*r == '_')
859 r++;
860
861 if (isdigit(*r) || (is_hex && isxdigit(*r)) ||
862 (!is_hex && (*r == 'e' || *r == 'E')) ||
863 (*r == 'p' || *r == 'P')) {
864 p = r;
865 is_float = true;
866 } else
867 break; /* Terminate the token */
868 } else
869 break;
870 }
871 p--; /* Point to first character beyond number */
872
873 if (has_e && !is_hex) {
874 /* 1e13 is floating-point, but 1e13h is not */
875 is_float = true;
876 }
877
878 type = is_float ? TOK_FLOAT : TOK_NUMBER;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000879 } else if (isspace(*p)) {
880 type = TOK_WHITESPACE;
881 p++;
882 while (*p && isspace(*p))
883 p++;
884 /*
885 * Whitespace just before end-of-line is discarded by
886 * pretending it's a comment; whitespace just before a
887 * comment gets lumped into the comment.
888 */
889 if (!*p || *p == ';') {
890 type = TOK_COMMENT;
891 while (*p)
892 p++;
893 }
894 } else if (*p == ';') {
895 type = TOK_COMMENT;
896 while (*p)
897 p++;
898 } else {
899 /*
900 * Anything else is an operator of some kind. We check
901 * for all the double-character operators (>>, <<, //,
902 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000903 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000904 */
905 type = TOK_OTHER;
906 if ((p[0] == '>' && p[1] == '>') ||
907 (p[0] == '<' && p[1] == '<') ||
908 (p[0] == '/' && p[1] == '/') ||
909 (p[0] == '<' && p[1] == '=') ||
910 (p[0] == '>' && p[1] == '=') ||
911 (p[0] == '=' && p[1] == '=') ||
912 (p[0] == '!' && p[1] == '=') ||
913 (p[0] == '<' && p[1] == '>') ||
914 (p[0] == '&' && p[1] == '&') ||
915 (p[0] == '|' && p[1] == '|') ||
916 (p[0] == '^' && p[1] == '^')) {
917 p++;
918 }
919 p++;
920 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000921
H. Peter Anvine2c80182005-01-15 22:15:51 +0000922 /* Handling unterminated string by UNV */
923 /*if (type == -1)
924 {
925 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
926 t->text[p-line] = *line;
927 tail = &t->next;
928 }
929 else */
930 if (type != TOK_COMMENT) {
931 *tail = t = new_Token(NULL, type, line, p - line);
932 tail = &t->next;
933 }
934 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000935 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000936 return list;
937}
938
H. Peter Anvince616072002-04-30 21:02:23 +0000939/*
940 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -0700941 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +0000942 * deleted only all at once by the delete_Blocks function.
943 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000944static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +0000945{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000946 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000947
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000948 /* first, get to the end of the linked list */
949 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000950 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000951 /* now allocate the requested chunk */
952 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000954 /* now allocate a new block for the next request */
955 b->next = nasm_malloc(sizeof(Blocks));
956 /* and initialize the contents of the new block */
957 b->next->next = NULL;
958 b->next->chunk = NULL;
959 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000960}
961
962/*
963 * this function deletes all managed blocks of memory
964 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000965static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +0000966{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +0000968
H. Peter Anvin70653092007-10-19 14:42:29 -0700969 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000970 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -0700971 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000972 * free it.
973 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 while (b) {
975 if (b->chunk)
976 nasm_free(b->chunk);
977 a = b;
978 b = b->next;
979 if (a != &blocks)
980 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +0000981 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000982}
H. Peter Anvin734b1882002-04-30 21:01:08 +0000983
984/*
H. Peter Anvin70653092007-10-19 14:42:29 -0700985 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +0000986 * back to the caller. It sets the type and text elements, and
987 * also the mac and next elements to NULL.
988 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000989static Token *new_Token(Token * next, enum pp_token_type type, char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +0000990{
991 Token *t;
992 int i;
993
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 if (freeTokens == NULL) {
995 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
996 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
997 freeTokens[i].next = &freeTokens[i + 1];
998 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000999 }
1000 t = freeTokens;
1001 freeTokens = t->next;
1002 t->next = next;
1003 t->mac = NULL;
1004 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 if (type == TOK_WHITESPACE || text == NULL) {
1006 t->text = NULL;
1007 } else {
1008 if (txtlen == 0)
1009 txtlen = strlen(text);
1010 t->text = nasm_malloc(1 + txtlen);
1011 strncpy(t->text, text, txtlen);
1012 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001013 }
1014 return t;
1015}
1016
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001018{
1019 Token *next = t->next;
1020 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001021 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001022 freeTokens = t;
1023 return next;
1024}
1025
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001026/*
1027 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001028 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1029 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001030 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001031static char *detoken(Token * tlist, int expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001032{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001033 Token *t;
1034 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001035 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001036 const char *q;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001037
1038 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001039 for (t = tlist; t; t = t->next) {
1040 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001041 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001042 nasm_free(t->text);
1043 if (p)
1044 t->text = nasm_strdup(p);
1045 else
1046 t->text = NULL;
1047 }
1048 /* Expand local macros here and not during preprocessing */
1049 if (expand_locals &&
1050 t->type == TOK_PREPROC_ID && t->text &&
1051 t->text[0] == '%' && t->text[1] == '$') {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001052 Context *ctx = get_ctx(t->text, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001053 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001054 char buffer[40];
1055 char *p, *q = t->text + 2;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001056
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 q += strspn(q, "$");
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001058 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 p = nasm_strcat(buffer, q);
1060 nasm_free(t->text);
1061 t->text = p;
1062 }
1063 }
1064 if (t->type == TOK_WHITESPACE) {
1065 len++;
1066 } else if (t->text) {
1067 len += strlen(t->text);
1068 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001069 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001070 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071 for (t = tlist; t; t = t->next) {
1072 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001073 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 } else if (t->text) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001075 q = t->text;
1076 while (*q)
1077 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001078 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001079 }
1080 *p = '\0';
1081 return line;
1082}
1083
1084/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001085 * A scanner, suitable for use by the expression evaluator, which
1086 * operates on a line of Tokens. Expects a pointer to a pointer to
1087 * the first token in the line to be passed in as its private_data
1088 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001089 *
1090 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001091 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001093{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001094 Token **tlineptr = private_data;
1095 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001096 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001097
H. Peter Anvine2c80182005-01-15 22:15:51 +00001098 do {
1099 tline = *tlineptr;
1100 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001101 }
1102 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001103 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001104
1105 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001106 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001107
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001108 tokval->t_charptr = tline->text;
1109
H. Peter Anvin76690a12002-04-30 20:52:49 +00001110 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001112 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001113 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001114
H. Peter Anvine2c80182005-01-15 22:15:51 +00001115 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001116 p = tokval->t_charptr = tline->text;
1117 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001118 tokval->t_charptr++;
1119 return tokval->t_type = TOKEN_ID;
1120 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001121
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001122 for (r = p, s = ourcopy; *r; r++) {
Philipp Thomas76ec8e72008-05-21 08:53:21 -07001123 if (r >= p+MAX_KEYWORD)
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001124 return tokval->t_type = TOKEN_ID; /* Not a keyword */
1125 *s++ = tolower(*r);
1126 }
1127 *s = '\0';
1128 /* right, so we have an identifier sitting in temp storage. now,
1129 * is it actually a register or instruction name, or what? */
1130 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001131 }
1132
H. Peter Anvine2c80182005-01-15 22:15:51 +00001133 if (tline->type == TOK_NUMBER) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001134 bool rn_error;
1135 tokval->t_integer = readnum(tline->text, &rn_error);
1136 if (rn_error)
1137 return tokval->t_type = TOKEN_ERRNUM; /* some malformation occurred */
1138 tokval->t_charptr = tline->text;
1139 return tokval->t_type = TOKEN_NUM;
1140 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001141
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001142 if (tline->type == TOK_FLOAT) {
1143 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001144 }
1145
H. Peter Anvine2c80182005-01-15 22:15:51 +00001146 if (tline->type == TOK_STRING) {
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001147 bool rn_warn;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001148 char q, *r;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 int l;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001150
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151 r = tline->text;
1152 q = *r++;
1153 l = strlen(r);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001154
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155 if (l == 0 || r[l - 1] != q)
1156 return tokval->t_type = TOKEN_ERRNUM;
1157 tokval->t_integer = readstrnum(r, l - 1, &rn_warn);
1158 if (rn_warn)
1159 error(ERR_WARNING | ERR_PASS1, "character constant too long");
1160 tokval->t_charptr = NULL;
1161 return tokval->t_type = TOKEN_NUM;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001162 }
1163
H. Peter Anvine2c80182005-01-15 22:15:51 +00001164 if (tline->type == TOK_OTHER) {
1165 if (!strcmp(tline->text, "<<"))
1166 return tokval->t_type = TOKEN_SHL;
1167 if (!strcmp(tline->text, ">>"))
1168 return tokval->t_type = TOKEN_SHR;
1169 if (!strcmp(tline->text, "//"))
1170 return tokval->t_type = TOKEN_SDIV;
1171 if (!strcmp(tline->text, "%%"))
1172 return tokval->t_type = TOKEN_SMOD;
1173 if (!strcmp(tline->text, "=="))
1174 return tokval->t_type = TOKEN_EQ;
1175 if (!strcmp(tline->text, "<>"))
1176 return tokval->t_type = TOKEN_NE;
1177 if (!strcmp(tline->text, "!="))
1178 return tokval->t_type = TOKEN_NE;
1179 if (!strcmp(tline->text, "<="))
1180 return tokval->t_type = TOKEN_LE;
1181 if (!strcmp(tline->text, ">="))
1182 return tokval->t_type = TOKEN_GE;
1183 if (!strcmp(tline->text, "&&"))
1184 return tokval->t_type = TOKEN_DBL_AND;
1185 if (!strcmp(tline->text, "^^"))
1186 return tokval->t_type = TOKEN_DBL_XOR;
1187 if (!strcmp(tline->text, "||"))
1188 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001189 }
1190
1191 /*
1192 * We have no other options: just return the first character of
1193 * the token text.
1194 */
1195 return tokval->t_type = tline->text[0];
1196}
1197
1198/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001199 * Compare a string to the name of an existing macro; this is a
1200 * simple wrapper which calls either strcmp or nasm_stricmp
1201 * depending on the value of the `casesense' parameter.
1202 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001203static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001204{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001205 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001206}
1207
1208/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001209 * Return the Context structure associated with a %$ token. Return
1210 * NULL, having _already_ reported an error condition, if the
1211 * context stack isn't deep enough for the supplied number of $
1212 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001213 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001214 * also scanned for such smacro, until it is found; if not -
1215 * only the context that directly results from the number of $'s
1216 * in variable's name.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001217 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001218static Context *get_ctx(char *name, bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001219{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001220 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001221 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001222 int i;
1223
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001224 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001225 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001226
H. Peter Anvine2c80182005-01-15 22:15:51 +00001227 if (!cstk) {
1228 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1229 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001230 }
1231
H. Peter Anvine2c80182005-01-15 22:15:51 +00001232 for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) {
1233 ctx = ctx->next;
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00001234/* i--; Lino - 02/25/02 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001235 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001236 if (!ctx) {
1237 error(ERR_NONFATAL, "`%s': context stack is only"
1238 " %d level%s deep", name, i - 1, (i == 2 ? "" : "s"));
1239 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001240 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001241 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001242 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001243
H. Peter Anvine2c80182005-01-15 22:15:51 +00001244 do {
1245 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001246 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001247 while (m) {
1248 if (!mstrcmp(m->name, name, m->casesense))
1249 return ctx;
1250 m = m->next;
1251 }
1252 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001253 }
1254 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001255 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001256}
1257
1258/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001259 * Open an include file. This routine must always return a valid
1260 * file pointer if it returns - it's responsible for throwing an
1261 * ERR_FATAL and bombing out completely if not. It should also try
1262 * the include path one by one until it finds the file or reaches
1263 * the end of the path.
1264 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001265static FILE *inc_fopen(char *file)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001266{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001267 FILE *fp;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001268 char *prefix = "", *combine;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001269 IncPath *ip = ipath;
H. Peter Anvin620515a2002-04-30 20:57:38 +00001270 static int namelen = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001271 int len = strlen(file);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001272
H. Peter Anvine2c80182005-01-15 22:15:51 +00001273 while (1) {
1274 combine = nasm_malloc(strlen(prefix) + len + 1);
1275 strcpy(combine, prefix);
1276 strcat(combine, file);
1277 fp = fopen(combine, "r");
1278 if (pass == 0 && fp) {
1279 namelen += strlen(combine) + 1;
1280 if (namelen > 62) {
1281 printf(" \\\n ");
1282 namelen = 2;
1283 }
1284 printf(" %s", combine);
1285 }
1286 nasm_free(combine);
1287 if (fp)
1288 return fp;
1289 if (!ip)
1290 break;
1291 prefix = ip->path;
1292 ip = ip->next;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001293
1294 if (!prefix) {
1295 /* -MG given and file not found */
1296 if (pass == 0) {
1297 namelen += strlen(file) + 1;
1298 if (namelen > 62) {
1299 printf(" \\\n ");
1300 namelen = 2;
1301 }
1302 printf(" %s", file);
1303 }
1304 return NULL;
1305 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001306 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001307
H. Peter Anvin734b1882002-04-30 21:01:08 +00001308 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001309 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001310}
1311
1312/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001313 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001314 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001315 * return true if _any_ single-line macro of that name is defined.
1316 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001317 * `nparam' or no parameters is defined.
1318 *
1319 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001320 * defined, or nparam is -1, the address of the definition structure
1321 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001322 * is NULL, no action will be taken regarding its contents, and no
1323 * error will occur.
1324 *
1325 * Note that this is also called with nparam zero to resolve
1326 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001327 *
1328 * If you already know which context macro belongs to, you can pass
1329 * the context pointer as first parameter; if you won't but name begins
1330 * with %$ the context will be automatically computed. If all_contexts
1331 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001332 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001333static bool
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001334smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001335 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001336{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001337 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001338 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001339
H. Peter Anvin97a23472007-09-16 17:57:25 -07001340 if (ctx) {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001341 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001342 } else if (name[0] == '%' && name[1] == '$') {
1343 if (cstk)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001344 ctx = get_ctx(name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001345 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001346 return false; /* got to return _something_ */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001347 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001348 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001349 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001350 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001351 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001352
H. Peter Anvine2c80182005-01-15 22:15:51 +00001353 while (m) {
1354 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001355 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001356 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001357 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001358 *defn = m;
1359 else
1360 *defn = NULL;
1361 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001362 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001363 }
1364 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001365 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001366
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001367 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001368}
1369
1370/*
1371 * Count and mark off the parameters in a multi-line macro call.
1372 * This is called both from within the multi-line macro expansion
1373 * code, and also to mark off the default parameters when provided
1374 * in a %macro definition line.
1375 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001376static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001377{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001378 int paramsize, brace;
1379
1380 *nparam = paramsize = 0;
1381 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001382 while (t) {
1383 if (*nparam >= paramsize) {
1384 paramsize += PARAM_DELTA;
1385 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1386 }
1387 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001388 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001389 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001390 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001391 (*params)[(*nparam)++] = t;
1392 while (tok_isnt_(t, brace ? "}" : ","))
1393 t = t->next;
1394 if (t) { /* got a comma/brace */
1395 t = t->next;
1396 if (brace) {
1397 /*
1398 * Now we've found the closing brace, look further
1399 * for the comma.
1400 */
1401 skip_white_(t);
1402 if (tok_isnt_(t, ",")) {
1403 error(ERR_NONFATAL,
1404 "braces do not enclose all of macro parameter");
1405 while (tok_isnt_(t, ","))
1406 t = t->next;
1407 }
1408 if (t)
1409 t = t->next; /* eat the comma */
1410 }
1411 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001412 }
1413}
1414
1415/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001416 * Determine whether one of the various `if' conditions is true or
1417 * not.
1418 *
1419 * We must free the tline we get passed.
1420 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001421static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001422{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001423 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001424 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001425 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001426 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001427 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001428 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001429
1430 origline = tline;
1431
H. Peter Anvine2c80182005-01-15 22:15:51 +00001432 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001433 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001434 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001435 while (cstk && tline) {
1436 skip_white_(tline);
1437 if (!tline || tline->type != TOK_ID) {
1438 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001439 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001440 free_tlist(origline);
1441 return -1;
1442 }
1443 if (!nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001444 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001445 tline = tline->next;
1446 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001447 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001448
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001449 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001450 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001451 while (tline) {
1452 skip_white_(tline);
1453 if (!tline || (tline->type != TOK_ID &&
1454 (tline->type != TOK_PREPROC_ID ||
1455 tline->text[1] != '$'))) {
1456 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001457 "`%s' expects macro identifiers", pp_directives[ct]);
1458 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001459 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001460 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001461 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 tline = tline->next;
1463 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001464 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001465
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001466 case PPC_IFIDN:
1467 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001468 tline = expand_smacro(tline);
1469 t = tt = tline;
1470 while (tok_isnt_(tt, ","))
1471 tt = tt->next;
1472 if (!tt) {
1473 error(ERR_NONFATAL,
1474 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001475 pp_directives[ct]);
1476 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001477 }
1478 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001479 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001480 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1481 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1482 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001483 pp_directives[ct]);
1484 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001485 }
1486 if (t->type == TOK_WHITESPACE) {
1487 t = t->next;
1488 continue;
1489 }
1490 if (tt->type == TOK_WHITESPACE) {
1491 tt = tt->next;
1492 continue;
1493 }
1494 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001495 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001496 break;
1497 }
1498 /* Unify surrounding quotes for strings */
1499 if (t->type == TOK_STRING) {
1500 tt->text[0] = t->text[0];
1501 tt->text[strlen(tt->text) - 1] = t->text[0];
1502 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001503 if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001504 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001505 break;
1506 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001507
H. Peter Anvine2c80182005-01-15 22:15:51 +00001508 t = t->next;
1509 tt = tt->next;
1510 }
1511 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001512 j = false; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001513 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001514
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001515 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001516 {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001517 bool found = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001518 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001519
H. Peter Anvine2c80182005-01-15 22:15:51 +00001520 tline = tline->next;
1521 skip_white_(tline);
1522 tline = expand_id(tline);
1523 if (!tok_type_(tline, TOK_ID)) {
1524 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001525 "`%s' expects a macro name", pp_directives[ct]);
1526 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001527 }
1528 searching.name = nasm_strdup(tline->text);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001529 searching.casesense = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001530 searching.plus = false;
1531 searching.nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001532 searching.in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001533 searching.rep_nest = NULL;
1534 searching.nparam_min = 0;
1535 searching.nparam_max = INT_MAX;
1536 tline = expand_smacro(tline->next);
1537 skip_white_(tline);
1538 if (!tline) {
1539 } else if (!tok_type_(tline, TOK_NUMBER)) {
1540 error(ERR_NONFATAL,
1541 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001542 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001543 } else {
1544 searching.nparam_min = searching.nparam_max =
1545 readnum(tline->text, &j);
1546 if (j)
1547 error(ERR_NONFATAL,
1548 "unable to parse parameter count `%s'",
1549 tline->text);
1550 }
1551 if (tline && tok_is_(tline->next, "-")) {
1552 tline = tline->next->next;
1553 if (tok_is_(tline, "*"))
1554 searching.nparam_max = INT_MAX;
1555 else if (!tok_type_(tline, TOK_NUMBER))
1556 error(ERR_NONFATAL,
1557 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001558 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001559 else {
1560 searching.nparam_max = readnum(tline->text, &j);
1561 if (j)
1562 error(ERR_NONFATAL,
1563 "unable to parse parameter count `%s'",
1564 tline->text);
1565 if (searching.nparam_min > searching.nparam_max)
1566 error(ERR_NONFATAL,
1567 "minimum parameter count exceeds maximum");
1568 }
1569 }
1570 if (tline && tok_is_(tline->next, "+")) {
1571 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001572 searching.plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001573 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001574 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001575 while (mmac) {
1576 if (!strcmp(mmac->name, searching.name) &&
1577 (mmac->nparam_min <= searching.nparam_max
1578 || searching.plus)
1579 && (searching.nparam_min <= mmac->nparam_max
1580 || mmac->plus)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001581 found = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001582 break;
1583 }
1584 mmac = mmac->next;
1585 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001586 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001587 j = found;
1588 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001589 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001590
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001591 case PPC_IFID:
1592 needtype = TOK_ID;
1593 goto iftype;
1594 case PPC_IFNUM:
1595 needtype = TOK_NUMBER;
1596 goto iftype;
1597 case PPC_IFSTR:
1598 needtype = TOK_STRING;
1599 goto iftype;
1600
1601 iftype:
H. Peter Anvin134b9462008-02-16 17:01:40 -08001602 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001603
H. Peter Anvin927c92b2008-02-16 13:44:52 -08001604 while (tok_type_(t, TOK_WHITESPACE) ||
1605 (needtype == TOK_NUMBER &&
1606 tok_type_(t, TOK_OTHER) &&
1607 (t->text[0] == '-' || t->text[0] == '+') &&
1608 !t->text[1]))
1609 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001610
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001611 j = tok_type_(t, needtype);
1612 break;
1613
1614 case PPC_IFTOKEN:
1615 t = tline = expand_smacro(tline);
1616 while (tok_type_(t, TOK_WHITESPACE))
1617 t = t->next;
1618
1619 j = false;
1620 if (t) {
1621 t = t->next; /* Skip the actual token */
1622 while (tok_type_(t, TOK_WHITESPACE))
1623 t = t->next;
1624 j = !t; /* Should be nothing left */
1625 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001626 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001627
H. Peter Anvin134b9462008-02-16 17:01:40 -08001628 case PPC_IFEMPTY:
1629 t = tline = expand_smacro(tline);
1630 while (tok_type_(t, TOK_WHITESPACE))
1631 t = t->next;
1632
1633 j = !t; /* Should be empty */
1634 break;
1635
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001636 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001637 t = tline = expand_smacro(tline);
1638 tptr = &t;
1639 tokval.t_type = TOKEN_INVALID;
1640 evalresult = evaluate(ppscan, tptr, &tokval,
1641 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001642 if (!evalresult)
1643 return -1;
1644 if (tokval.t_type)
1645 error(ERR_WARNING,
1646 "trailing garbage after expression ignored");
1647 if (!is_simple(evalresult)) {
1648 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001649 "non-constant value given to `%s'", pp_directives[ct]);
1650 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001651 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001652 j = reloc_value(evalresult) != 0;
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001653 return j;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001654
H. Peter Anvine2c80182005-01-15 22:15:51 +00001655 default:
1656 error(ERR_FATAL,
1657 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001658 pp_directives[ct]);
1659 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001660 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001661
1662 free_tlist(origline);
1663 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001664
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001665fail:
1666 free_tlist(origline);
1667 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001668}
1669
1670/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001671 * Expand macros in a string. Used in %error and %include directives.
Keith Kaniosb7a89542007-04-12 02:40:54 +00001672 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001673 * The returned variable should ALWAYS be freed after usage.
1674 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001675void expand_macros_in_string(char **p)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001676{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001677 Token *line = tokenize(*p);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001678 line = expand_smacro(line);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001679 *p = detoken(line, false);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001680}
1681
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001682/*
1683 * Common code for defining an smacro
1684 */
1685static bool define_smacro(Context *ctx, char *mname, bool casesense,
1686 int nparam, Token *expansion)
1687{
1688 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001689 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001690
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001691 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1692 if (!smac) {
1693 error(ERR_WARNING,
1694 "single-line macro `%s' defined both with and"
1695 " without parameters", mname);
1696
1697 /* Some instances of the old code considered this a failure,
1698 some others didn't. What is the right thing to do here? */
1699 free_tlist(expansion);
1700 return false; /* Failure */
1701 } else {
1702 /*
1703 * We're redefining, so we have to take over an
1704 * existing SMacro structure. This means freeing
1705 * what was already in it.
1706 */
1707 nasm_free(smac->name);
1708 free_tlist(smac->expansion);
1709 }
1710 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001711 smtbl = ctx ? &ctx->localmac : &smacros;
1712 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001713 smac = nasm_malloc(sizeof(SMacro));
1714 smac->next = *smhead;
1715 *smhead = smac;
1716 }
1717 smac->name = nasm_strdup(mname);
1718 smac->casesense = casesense;
1719 smac->nparam = nparam;
1720 smac->expansion = expansion;
1721 smac->in_progress = false;
1722 return true; /* Success */
1723}
1724
1725/*
1726 * Undefine an smacro
1727 */
1728static void undef_smacro(Context *ctx, const char *mname)
1729{
1730 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001731 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001732
H. Peter Anvin166c2472008-05-28 12:28:58 -07001733 smtbl = ctx ? &ctx->localmac : &smacros;
1734 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001735
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001736 if (smhead) {
1737 /*
1738 * We now have a macro name... go hunt for it.
1739 */
1740 sp = smhead;
1741 while ((s = *sp) != NULL) {
1742 if (!mstrcmp(s->name, mname, s->casesense)) {
1743 *sp = s->next;
1744 nasm_free(s->name);
1745 free_tlist(s->expansion);
1746 nasm_free(s);
1747 } else {
1748 sp = &s->next;
1749 }
1750 }
1751 }
1752}
1753
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001754/*
1755 * Decode a size directive
1756 */
1757static int parse_size(const char *str) {
1758 static const char *size_names[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001759 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001760 static const int sizes[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001761 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001762
1763 return sizes[bsii(str, size_names, elements(size_names))+1];
1764}
1765
Ed Beroset3ab3f412002-06-11 03:31:49 +00001766/**
1767 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001768 * Find out if a line contains a preprocessor directive, and deal
1769 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07001770 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001771 * If a directive _is_ found, it is the responsibility of this routine
1772 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001773 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001774 * @param tline a pointer to the current tokeninzed line linked list
1775 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07001776 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001777 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001779{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001780 enum preproc_token i;
1781 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07001782 bool err;
1783 int nparam;
1784 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001785 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001786 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001787 int offset;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001788 char *p, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001789 Include *inc;
1790 Context *ctx;
1791 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001792 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001793 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1794 Line *l;
1795 struct tokenval tokval;
1796 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001797 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001798 int64_t count;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001799
1800 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001801
H. Peter Anvineba20a72002-04-30 20:53:55 +00001802 skip_white_(tline);
1803 if (!tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001804 (tline->text[1] == '%' || tline->text[1] == '$'
1805 || tline->text[1] == '!'))
1806 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001807
H. Peter Anvin4169a472007-09-12 01:29:43 +00001808 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001809
1810 /*
1811 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001812 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001813 * we should ignore all directives except for condition
1814 * directives.
1815 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001816 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1818 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001819 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001820
1821 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001822 * If we're defining a macro or reading a %rep block, we should
1823 * ignore all directives except for %macro/%imacro (which
1824 * generate an error), %endm/%endmacro, and (only if we're in a
H. Peter Anvineba20a72002-04-30 20:53:55 +00001825 * %rep block) %endrep. If we're in a %rep block, another %rep
1826 * causes an error, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001827 */
1828 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001829 i != PP_ENDMACRO && i != PP_ENDM &&
1830 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1831 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001832 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001833
H. Peter Anvin4169a472007-09-12 01:29:43 +00001834 switch (i) {
1835 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001836 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1837 tline->text);
1838 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001839
H. Peter Anvine2c80182005-01-15 22:15:51 +00001840 case PP_STACKSIZE:
1841 /* Directive to tell NASM what the default stack size is. The
1842 * default is for a 16-bit stack, and this can be overriden with
1843 * %stacksize large.
1844 * the following form:
1845 *
1846 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1847 */
1848 tline = tline->next;
1849 if (tline && tline->type == TOK_WHITESPACE)
1850 tline = tline->next;
1851 if (!tline || tline->type != TOK_ID) {
1852 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1853 free_tlist(origline);
1854 return DIRECTIVE_FOUND;
1855 }
1856 if (nasm_stricmp(tline->text, "flat") == 0) {
1857 /* All subsequent ARG directives are for a 32-bit stack */
1858 StackSize = 4;
1859 StackPointer = "ebp";
1860 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001861 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08001862 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
1863 /* All subsequent ARG directives are for a 64-bit stack */
1864 StackSize = 8;
1865 StackPointer = "rbp";
1866 ArgOffset = 8;
1867 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001868 } else if (nasm_stricmp(tline->text, "large") == 0) {
1869 /* All subsequent ARG directives are for a 16-bit stack,
1870 * far function call.
1871 */
1872 StackSize = 2;
1873 StackPointer = "bp";
1874 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001875 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001876 } else if (nasm_stricmp(tline->text, "small") == 0) {
1877 /* All subsequent ARG directives are for a 16-bit stack,
1878 * far function call. We don't support near functions.
1879 */
1880 StackSize = 2;
1881 StackPointer = "bp";
1882 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001883 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001884 } else {
1885 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1886 free_tlist(origline);
1887 return DIRECTIVE_FOUND;
1888 }
1889 free_tlist(origline);
1890 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001891
H. Peter Anvine2c80182005-01-15 22:15:51 +00001892 case PP_ARG:
1893 /* TASM like ARG directive to define arguments to functions, in
1894 * the following form:
1895 *
1896 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1897 */
1898 offset = ArgOffset;
1899 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001900 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001901 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001902
H. Peter Anvine2c80182005-01-15 22:15:51 +00001903 /* Find the argument name */
1904 tline = tline->next;
1905 if (tline && tline->type == TOK_WHITESPACE)
1906 tline = tline->next;
1907 if (!tline || tline->type != TOK_ID) {
1908 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1909 free_tlist(origline);
1910 return DIRECTIVE_FOUND;
1911 }
1912 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001913
H. Peter Anvine2c80182005-01-15 22:15:51 +00001914 /* Find the argument size type */
1915 tline = tline->next;
1916 if (!tline || tline->type != TOK_OTHER
1917 || tline->text[0] != ':') {
1918 error(ERR_NONFATAL,
1919 "Syntax error processing `%%arg' directive");
1920 free_tlist(origline);
1921 return DIRECTIVE_FOUND;
1922 }
1923 tline = tline->next;
1924 if (!tline || tline->type != TOK_ID) {
1925 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1926 free_tlist(origline);
1927 return DIRECTIVE_FOUND;
1928 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001929
H. Peter Anvine2c80182005-01-15 22:15:51 +00001930 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001931 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001932 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001933 size = parse_size(tt->text);
1934 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001935 error(ERR_NONFATAL,
1936 "Invalid size type for `%%arg' missing directive");
1937 free_tlist(tt);
1938 free_tlist(origline);
1939 return DIRECTIVE_FOUND;
1940 }
1941 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001942
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001943 /* Round up to even stack slots */
1944 size = (size+StackSize-1) & ~(StackSize-1);
1945
H. Peter Anvine2c80182005-01-15 22:15:51 +00001946 /* Now define the macro for the argument */
1947 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1948 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001949 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001950 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001951
H. Peter Anvine2c80182005-01-15 22:15:51 +00001952 /* Move to the next argument in the list */
1953 tline = tline->next;
1954 if (tline && tline->type == TOK_WHITESPACE)
1955 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001956 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1957 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001958 free_tlist(origline);
1959 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001960
H. Peter Anvine2c80182005-01-15 22:15:51 +00001961 case PP_LOCAL:
1962 /* TASM like LOCAL directive to define local variables for a
1963 * function, in the following form:
1964 *
1965 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1966 *
1967 * The '= LocalSize' at the end is ignored by NASM, but is
1968 * required by TASM to define the local parameter size (and used
1969 * by the TASM macro package).
1970 */
1971 offset = LocalOffset;
1972 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001973 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001974 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001975
H. Peter Anvine2c80182005-01-15 22:15:51 +00001976 /* Find the argument name */
1977 tline = tline->next;
1978 if (tline && tline->type == TOK_WHITESPACE)
1979 tline = tline->next;
1980 if (!tline || tline->type != TOK_ID) {
1981 error(ERR_NONFATAL,
1982 "`%%local' missing argument parameter");
1983 free_tlist(origline);
1984 return DIRECTIVE_FOUND;
1985 }
1986 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001987
H. Peter Anvine2c80182005-01-15 22:15:51 +00001988 /* Find the argument size type */
1989 tline = tline->next;
1990 if (!tline || tline->type != TOK_OTHER
1991 || tline->text[0] != ':') {
1992 error(ERR_NONFATAL,
1993 "Syntax error processing `%%local' directive");
1994 free_tlist(origline);
1995 return DIRECTIVE_FOUND;
1996 }
1997 tline = tline->next;
1998 if (!tline || tline->type != TOK_ID) {
1999 error(ERR_NONFATAL,
2000 "`%%local' missing size type parameter");
2001 free_tlist(origline);
2002 return DIRECTIVE_FOUND;
2003 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002004
H. Peter Anvine2c80182005-01-15 22:15:51 +00002005 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002006 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002007 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002008 size = parse_size(tt->text);
2009 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002010 error(ERR_NONFATAL,
2011 "Invalid size type for `%%local' missing directive");
2012 free_tlist(tt);
2013 free_tlist(origline);
2014 return DIRECTIVE_FOUND;
2015 }
2016 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002017
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002018 /* Round up to even stack slots */
2019 size = (size+StackSize-1) & ~(StackSize-1);
2020
2021 offset += size; /* Negative offset, increment before */
2022
2023 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002024 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2025 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002026 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002027
H. Peter Anvine2c80182005-01-15 22:15:51 +00002028 /* Now define the assign to setup the enter_c macro correctly */
2029 snprintf(directive, sizeof(directive),
2030 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002031 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002032
H. Peter Anvine2c80182005-01-15 22:15:51 +00002033 /* Move to the next argument in the list */
2034 tline = tline->next;
2035 if (tline && tline->type == TOK_WHITESPACE)
2036 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002037 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2038 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002039 free_tlist(origline);
2040 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002041
H. Peter Anvine2c80182005-01-15 22:15:51 +00002042 case PP_CLEAR:
2043 if (tline->next)
2044 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002045 free_macros();
2046 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002047 free_tlist(origline);
2048 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002049
H. Peter Anvine2c80182005-01-15 22:15:51 +00002050 case PP_INCLUDE:
2051 tline = tline->next;
2052 skip_white_(tline);
2053 if (!tline || (tline->type != TOK_STRING &&
2054 tline->type != TOK_INTERNAL_STRING)) {
2055 error(ERR_NONFATAL, "`%%include' expects a file name");
2056 free_tlist(origline);
2057 return DIRECTIVE_FOUND; /* but we did _something_ */
2058 }
2059 if (tline->next)
2060 error(ERR_WARNING,
2061 "trailing garbage after `%%include' ignored");
2062 if (tline->type != TOK_INTERNAL_STRING) {
2063 p = tline->text + 1; /* point past the quote to the name */
2064 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2065 } else
2066 p = tline->text; /* internal_string is easier */
2067 expand_macros_in_string(&p);
2068 inc = nasm_malloc(sizeof(Include));
2069 inc->next = istk;
2070 inc->conds = NULL;
2071 inc->fp = inc_fopen(p);
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002072 if (!inc->fp && pass == 0) {
2073 /* -MG given but file not found */
2074 nasm_free(inc);
2075 } else {
2076 inc->fname = src_set_fname(p);
2077 inc->lineno = src_set_linnum(0);
2078 inc->lineinc = 1;
2079 inc->expansion = NULL;
2080 inc->mstk = NULL;
2081 istk = inc;
2082 list->uplevel(LIST_INCLUDE);
2083 }
2084 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002085 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002086
H. Peter Anvine2c80182005-01-15 22:15:51 +00002087 case PP_PUSH:
2088 tline = tline->next;
2089 skip_white_(tline);
2090 tline = expand_id(tline);
2091 if (!tok_type_(tline, TOK_ID)) {
2092 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2093 free_tlist(origline);
2094 return DIRECTIVE_FOUND; /* but we did _something_ */
2095 }
2096 if (tline->next)
2097 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2098 ctx = nasm_malloc(sizeof(Context));
2099 ctx->next = cstk;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002100 hash_init(&ctx->localmac, HASH_SMALL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002101 ctx->name = nasm_strdup(tline->text);
2102 ctx->number = unique++;
2103 cstk = ctx;
2104 free_tlist(origline);
2105 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002106
H. Peter Anvine2c80182005-01-15 22:15:51 +00002107 case PP_REPL:
2108 tline = tline->next;
2109 skip_white_(tline);
2110 tline = expand_id(tline);
2111 if (!tok_type_(tline, TOK_ID)) {
2112 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2113 free_tlist(origline);
2114 return DIRECTIVE_FOUND; /* but we did _something_ */
2115 }
2116 if (tline->next)
2117 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2118 if (!cstk)
2119 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2120 else {
2121 nasm_free(cstk->name);
2122 cstk->name = nasm_strdup(tline->text);
2123 }
2124 free_tlist(origline);
2125 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002126
H. Peter Anvine2c80182005-01-15 22:15:51 +00002127 case PP_POP:
2128 if (tline->next)
2129 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2130 if (!cstk)
2131 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2132 else
2133 ctx_pop();
2134 free_tlist(origline);
2135 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002136
H. Peter Anvine2c80182005-01-15 22:15:51 +00002137 case PP_ERROR:
2138 tline->next = expand_smacro(tline->next);
2139 tline = tline->next;
2140 skip_white_(tline);
2141 if (tok_type_(tline, TOK_STRING)) {
2142 p = tline->text + 1; /* point past the quote to the name */
2143 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2144 expand_macros_in_string(&p);
2145 error(ERR_NONFATAL, "%s", p);
2146 nasm_free(p);
2147 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002148 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002149 error(ERR_WARNING, "%s", p);
2150 nasm_free(p);
2151 }
2152 free_tlist(origline);
2153 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002154
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002155 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002156 if (istk->conds && !emitting(istk->conds->state))
2157 j = COND_NEVER;
2158 else {
2159 j = if_condition(tline->next, i);
2160 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002161 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2162 }
2163 cond = nasm_malloc(sizeof(Cond));
2164 cond->next = istk->conds;
2165 cond->state = j;
2166 istk->conds = cond;
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002167 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002168 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002169
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002170 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002171 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002172 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002173 if (emitting(istk->conds->state)
2174 || istk->conds->state == COND_NEVER)
2175 istk->conds->state = COND_NEVER;
2176 else {
2177 /*
2178 * IMPORTANT: In the case of %if, we will already have
2179 * called expand_mmac_params(); however, if we're
2180 * processing an %elif we must have been in a
2181 * non-emitting mode, which would have inhibited
2182 * the normal invocation of expand_mmac_params(). Therefore,
2183 * we have to do it explicitly here.
2184 */
2185 j = if_condition(expand_mmac_params(tline->next), i);
2186 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002187 istk->conds->state =
2188 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2189 }
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002190 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002191 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002192
H. Peter Anvine2c80182005-01-15 22:15:51 +00002193 case PP_ELSE:
2194 if (tline->next)
2195 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2196 if (!istk->conds)
2197 error(ERR_FATAL, "`%%else': no matching `%%if'");
2198 if (emitting(istk->conds->state)
2199 || istk->conds->state == COND_NEVER)
2200 istk->conds->state = COND_ELSE_FALSE;
2201 else
2202 istk->conds->state = COND_ELSE_TRUE;
2203 free_tlist(origline);
2204 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002205
H. Peter Anvine2c80182005-01-15 22:15:51 +00002206 case PP_ENDIF:
2207 if (tline->next)
2208 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2209 if (!istk->conds)
2210 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2211 cond = istk->conds;
2212 istk->conds = cond->next;
2213 nasm_free(cond);
2214 free_tlist(origline);
2215 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002216
H. Peter Anvine2c80182005-01-15 22:15:51 +00002217 case PP_MACRO:
2218 case PP_IMACRO:
2219 if (defining)
2220 error(ERR_FATAL,
2221 "`%%%smacro': already defining a macro",
2222 (i == PP_IMACRO ? "i" : ""));
2223 tline = tline->next;
2224 skip_white_(tline);
2225 tline = expand_id(tline);
2226 if (!tok_type_(tline, TOK_ID)) {
2227 error(ERR_NONFATAL,
2228 "`%%%smacro' expects a macro name",
2229 (i == PP_IMACRO ? "i" : ""));
2230 return DIRECTIVE_FOUND;
2231 }
2232 defining = nasm_malloc(sizeof(MMacro));
2233 defining->name = nasm_strdup(tline->text);
2234 defining->casesense = (i == PP_MACRO);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002235 defining->plus = false;
2236 defining->nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002237 defining->in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002238 defining->rep_nest = NULL;
2239 tline = expand_smacro(tline->next);
2240 skip_white_(tline);
2241 if (!tok_type_(tline, TOK_NUMBER)) {
2242 error(ERR_NONFATAL,
2243 "`%%%smacro' expects a parameter count",
2244 (i == PP_IMACRO ? "i" : ""));
2245 defining->nparam_min = defining->nparam_max = 0;
2246 } else {
2247 defining->nparam_min = defining->nparam_max =
H. Peter Anvin70055962007-10-11 00:05:31 -07002248 readnum(tline->text, &err);
2249 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002250 error(ERR_NONFATAL,
2251 "unable to parse parameter count `%s'", tline->text);
2252 }
2253 if (tline && tok_is_(tline->next, "-")) {
2254 tline = tline->next->next;
2255 if (tok_is_(tline, "*"))
2256 defining->nparam_max = INT_MAX;
2257 else if (!tok_type_(tline, TOK_NUMBER))
2258 error(ERR_NONFATAL,
2259 "`%%%smacro' expects a parameter count after `-'",
2260 (i == PP_IMACRO ? "i" : ""));
2261 else {
H. Peter Anvin70055962007-10-11 00:05:31 -07002262 defining->nparam_max = readnum(tline->text, &err);
2263 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002264 error(ERR_NONFATAL,
2265 "unable to parse parameter count `%s'",
2266 tline->text);
2267 if (defining->nparam_min > defining->nparam_max)
2268 error(ERR_NONFATAL,
2269 "minimum parameter count exceeds maximum");
2270 }
2271 }
2272 if (tline && tok_is_(tline->next, "+")) {
2273 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002274 defining->plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002275 }
2276 if (tline && tok_type_(tline->next, TOK_ID) &&
2277 !nasm_stricmp(tline->next->text, ".nolist")) {
2278 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002279 defining->nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002280 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002281 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002282 while (mmac) {
2283 if (!strcmp(mmac->name, defining->name) &&
2284 (mmac->nparam_min <= defining->nparam_max
2285 || defining->plus)
2286 && (defining->nparam_min <= mmac->nparam_max
2287 || mmac->plus)) {
2288 error(ERR_WARNING,
2289 "redefining multi-line macro `%s'", defining->name);
2290 break;
2291 }
2292 mmac = mmac->next;
2293 }
2294 /*
2295 * Handle default parameters.
2296 */
2297 if (tline && tline->next) {
2298 defining->dlist = tline->next;
2299 tline->next = NULL;
2300 count_mmac_params(defining->dlist, &defining->ndefs,
2301 &defining->defaults);
2302 } else {
2303 defining->dlist = NULL;
2304 defining->defaults = NULL;
2305 }
2306 defining->expansion = NULL;
2307 free_tlist(origline);
2308 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002309
H. Peter Anvine2c80182005-01-15 22:15:51 +00002310 case PP_ENDM:
2311 case PP_ENDMACRO:
2312 if (!defining) {
2313 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2314 return DIRECTIVE_FOUND;
2315 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002316 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002317 defining->next = *mmhead;
2318 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002319 defining = NULL;
2320 free_tlist(origline);
2321 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002322
H. Peter Anvine2c80182005-01-15 22:15:51 +00002323 case PP_ROTATE:
2324 if (tline->next && tline->next->type == TOK_WHITESPACE)
2325 tline = tline->next;
2326 if (tline->next == NULL) {
2327 free_tlist(origline);
2328 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2329 return DIRECTIVE_FOUND;
2330 }
2331 t = expand_smacro(tline->next);
2332 tline->next = NULL;
2333 free_tlist(origline);
2334 tline = t;
2335 tptr = &t;
2336 tokval.t_type = TOKEN_INVALID;
2337 evalresult =
2338 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2339 free_tlist(tline);
2340 if (!evalresult)
2341 return DIRECTIVE_FOUND;
2342 if (tokval.t_type)
2343 error(ERR_WARNING,
2344 "trailing garbage after expression ignored");
2345 if (!is_simple(evalresult)) {
2346 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2347 return DIRECTIVE_FOUND;
2348 }
2349 mmac = istk->mstk;
2350 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2351 mmac = mmac->next_active;
2352 if (!mmac) {
2353 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2354 } else if (mmac->nparam == 0) {
2355 error(ERR_NONFATAL,
2356 "`%%rotate' invoked within macro without parameters");
2357 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002358 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002359
H. Peter Anvin25a99342007-09-22 17:45:45 -07002360 rotate %= (int)mmac->nparam;
2361 if (rotate < 0)
2362 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002363
H. Peter Anvin25a99342007-09-22 17:45:45 -07002364 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002365 }
2366 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002367
H. Peter Anvine2c80182005-01-15 22:15:51 +00002368 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002369 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002370 do {
2371 tline = tline->next;
2372 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002373
H. Peter Anvine2c80182005-01-15 22:15:51 +00002374 if (tok_type_(tline, TOK_ID) &&
2375 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002376 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002377 do {
2378 tline = tline->next;
2379 } while (tok_type_(tline, TOK_WHITESPACE));
2380 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002381
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 if (tline) {
2383 t = expand_smacro(tline);
2384 tptr = &t;
2385 tokval.t_type = TOKEN_INVALID;
2386 evalresult =
2387 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2388 if (!evalresult) {
2389 free_tlist(origline);
2390 return DIRECTIVE_FOUND;
2391 }
2392 if (tokval.t_type)
2393 error(ERR_WARNING,
2394 "trailing garbage after expression ignored");
2395 if (!is_simple(evalresult)) {
2396 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2397 return DIRECTIVE_FOUND;
2398 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002399 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002400 } else {
2401 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002402 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002403 }
2404 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002405
H. Peter Anvine2c80182005-01-15 22:15:51 +00002406 tmp_defining = defining;
2407 defining = nasm_malloc(sizeof(MMacro));
2408 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002409 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002410 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002411 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002412 defining->in_progress = count;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002413 defining->nparam_min = defining->nparam_max = 0;
2414 defining->defaults = NULL;
2415 defining->dlist = NULL;
2416 defining->expansion = NULL;
2417 defining->next_active = istk->mstk;
2418 defining->rep_nest = tmp_defining;
2419 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002420
H. Peter Anvine2c80182005-01-15 22:15:51 +00002421 case PP_ENDREP:
2422 if (!defining || defining->name) {
2423 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2424 return DIRECTIVE_FOUND;
2425 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002426
H. Peter Anvine2c80182005-01-15 22:15:51 +00002427 /*
2428 * Now we have a "macro" defined - although it has no name
2429 * and we won't be entering it in the hash tables - we must
2430 * push a macro-end marker for it on to istk->expansion.
2431 * After that, it will take care of propagating itself (a
2432 * macro-end marker line for a macro which is really a %rep
2433 * block will cause the macro to be re-expanded, complete
2434 * with another macro-end marker to ensure the process
2435 * continues) until the whole expansion is forcibly removed
2436 * from istk->expansion by a %exitrep.
2437 */
2438 l = nasm_malloc(sizeof(Line));
2439 l->next = istk->expansion;
2440 l->finishes = defining;
2441 l->first = NULL;
2442 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002443
H. Peter Anvine2c80182005-01-15 22:15:51 +00002444 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002445
H. Peter Anvine2c80182005-01-15 22:15:51 +00002446 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2447 tmp_defining = defining;
2448 defining = defining->rep_nest;
2449 free_tlist(origline);
2450 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002451
H. Peter Anvine2c80182005-01-15 22:15:51 +00002452 case PP_EXITREP:
2453 /*
2454 * We must search along istk->expansion until we hit a
2455 * macro-end marker for a macro with no name. Then we set
2456 * its `in_progress' flag to 0.
2457 */
2458 for (l = istk->expansion; l; l = l->next)
2459 if (l->finishes && !l->finishes->name)
2460 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002461
H. Peter Anvine2c80182005-01-15 22:15:51 +00002462 if (l)
2463 l->finishes->in_progress = 0;
2464 else
2465 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2466 free_tlist(origline);
2467 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002468
H. Peter Anvine2c80182005-01-15 22:15:51 +00002469 case PP_XDEFINE:
2470 case PP_IXDEFINE:
2471 case PP_DEFINE:
2472 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002473 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002474
H. Peter Anvine2c80182005-01-15 22:15:51 +00002475 tline = tline->next;
2476 skip_white_(tline);
2477 tline = expand_id(tline);
2478 if (!tline || (tline->type != TOK_ID &&
2479 (tline->type != TOK_PREPROC_ID ||
2480 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002481 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2482 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002483 free_tlist(origline);
2484 return DIRECTIVE_FOUND;
2485 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002486
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002487 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002488
H. Peter Anvine2c80182005-01-15 22:15:51 +00002489 mname = tline->text;
2490 last = tline;
2491 param_start = tline = tline->next;
2492 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002493
H. Peter Anvine2c80182005-01-15 22:15:51 +00002494 /* Expand the macro definition now for %xdefine and %ixdefine */
2495 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2496 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002497
H. Peter Anvine2c80182005-01-15 22:15:51 +00002498 if (tok_is_(tline, "(")) {
2499 /*
2500 * This macro has parameters.
2501 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002502
H. Peter Anvine2c80182005-01-15 22:15:51 +00002503 tline = tline->next;
2504 while (1) {
2505 skip_white_(tline);
2506 if (!tline) {
2507 error(ERR_NONFATAL, "parameter identifier expected");
2508 free_tlist(origline);
2509 return DIRECTIVE_FOUND;
2510 }
2511 if (tline->type != TOK_ID) {
2512 error(ERR_NONFATAL,
2513 "`%s': parameter identifier expected",
2514 tline->text);
2515 free_tlist(origline);
2516 return DIRECTIVE_FOUND;
2517 }
2518 tline->type = TOK_SMAC_PARAM + nparam++;
2519 tline = tline->next;
2520 skip_white_(tline);
2521 if (tok_is_(tline, ",")) {
2522 tline = tline->next;
2523 continue;
2524 }
2525 if (!tok_is_(tline, ")")) {
2526 error(ERR_NONFATAL,
2527 "`)' expected to terminate macro template");
2528 free_tlist(origline);
2529 return DIRECTIVE_FOUND;
2530 }
2531 break;
2532 }
2533 last = tline;
2534 tline = tline->next;
2535 }
2536 if (tok_type_(tline, TOK_WHITESPACE))
2537 last = tline, tline = tline->next;
2538 macro_start = NULL;
2539 last->next = NULL;
2540 t = tline;
2541 while (t) {
2542 if (t->type == TOK_ID) {
2543 for (tt = param_start; tt; tt = tt->next)
2544 if (tt->type >= TOK_SMAC_PARAM &&
2545 !strcmp(tt->text, t->text))
2546 t->type = tt->type;
2547 }
2548 tt = t->next;
2549 t->next = macro_start;
2550 macro_start = t;
2551 t = tt;
2552 }
2553 /*
2554 * Good. We now have a macro name, a parameter count, and a
2555 * token list (in reverse order) for an expansion. We ought
2556 * to be OK just to create an SMacro, store it, and let
2557 * free_tlist have the rest of the line (which we have
2558 * carefully re-terminated after chopping off the expansion
2559 * from the end).
2560 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002561 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002562 free_tlist(origline);
2563 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002564
H. Peter Anvine2c80182005-01-15 22:15:51 +00002565 case PP_UNDEF:
2566 tline = tline->next;
2567 skip_white_(tline);
2568 tline = expand_id(tline);
2569 if (!tline || (tline->type != TOK_ID &&
2570 (tline->type != TOK_PREPROC_ID ||
2571 tline->text[1] != '$'))) {
2572 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2573 free_tlist(origline);
2574 return DIRECTIVE_FOUND;
2575 }
2576 if (tline->next) {
2577 error(ERR_WARNING,
2578 "trailing garbage after macro name ignored");
2579 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002580
H. Peter Anvine2c80182005-01-15 22:15:51 +00002581 /* Find the context that symbol belongs to */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002582 ctx = get_ctx(tline->text, false);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002583 undef_smacro(ctx, tline->text);
2584 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002585 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002586
H. Peter Anvine2c80182005-01-15 22:15:51 +00002587 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002588 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07002589
H. Peter Anvine2c80182005-01-15 22:15:51 +00002590 tline = tline->next;
2591 skip_white_(tline);
2592 tline = expand_id(tline);
2593 if (!tline || (tline->type != TOK_ID &&
2594 (tline->type != TOK_PREPROC_ID ||
2595 tline->text[1] != '$'))) {
2596 error(ERR_NONFATAL,
2597 "`%%strlen' expects a macro identifier as first parameter");
2598 free_tlist(origline);
2599 return DIRECTIVE_FOUND;
2600 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002601 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002602
H. Peter Anvine2c80182005-01-15 22:15:51 +00002603 mname = tline->text;
2604 last = tline;
2605 tline = expand_smacro(tline->next);
2606 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002607
H. Peter Anvine2c80182005-01-15 22:15:51 +00002608 t = tline;
2609 while (tok_type_(t, TOK_WHITESPACE))
2610 t = t->next;
2611 /* t should now point to the string */
2612 if (t->type != TOK_STRING) {
2613 error(ERR_NONFATAL,
2614 "`%%strlen` requires string as second parameter");
2615 free_tlist(tline);
2616 free_tlist(origline);
2617 return DIRECTIVE_FOUND;
2618 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002619
H. Peter Anvine2c80182005-01-15 22:15:51 +00002620 macro_start = nasm_malloc(sizeof(*macro_start));
2621 macro_start->next = NULL;
2622 make_tok_num(macro_start, strlen(t->text) - 2);
2623 macro_start->mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002624
H. Peter Anvine2c80182005-01-15 22:15:51 +00002625 /*
2626 * We now have a macro name, an implicit parameter count of
2627 * zero, and a numeric token to use as an expansion. Create
2628 * and store an SMacro.
2629 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002630 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002631 free_tlist(tline);
2632 free_tlist(origline);
2633 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002634
H. Peter Anvine2c80182005-01-15 22:15:51 +00002635 case PP_SUBSTR:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002636 casesense = true;
2637
H. Peter Anvine2c80182005-01-15 22:15:51 +00002638 tline = tline->next;
2639 skip_white_(tline);
2640 tline = expand_id(tline);
2641 if (!tline || (tline->type != TOK_ID &&
2642 (tline->type != TOK_PREPROC_ID ||
2643 tline->text[1] != '$'))) {
2644 error(ERR_NONFATAL,
2645 "`%%substr' expects a macro identifier as first parameter");
2646 free_tlist(origline);
2647 return DIRECTIVE_FOUND;
2648 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002649 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002650
H. Peter Anvine2c80182005-01-15 22:15:51 +00002651 mname = tline->text;
2652 last = tline;
2653 tline = expand_smacro(tline->next);
2654 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002655
H. Peter Anvine2c80182005-01-15 22:15:51 +00002656 t = tline->next;
2657 while (tok_type_(t, TOK_WHITESPACE))
2658 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002659
H. Peter Anvine2c80182005-01-15 22:15:51 +00002660 /* t should now point to the string */
2661 if (t->type != TOK_STRING) {
2662 error(ERR_NONFATAL,
2663 "`%%substr` requires string as second parameter");
2664 free_tlist(tline);
2665 free_tlist(origline);
2666 return DIRECTIVE_FOUND;
2667 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002668
H. Peter Anvine2c80182005-01-15 22:15:51 +00002669 tt = t->next;
2670 tptr = &tt;
2671 tokval.t_type = TOKEN_INVALID;
2672 evalresult =
2673 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2674 if (!evalresult) {
2675 free_tlist(tline);
2676 free_tlist(origline);
2677 return DIRECTIVE_FOUND;
2678 }
2679 if (!is_simple(evalresult)) {
2680 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2681 free_tlist(tline);
2682 free_tlist(origline);
2683 return DIRECTIVE_FOUND;
2684 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002685
H. Peter Anvine2c80182005-01-15 22:15:51 +00002686 macro_start = nasm_malloc(sizeof(*macro_start));
2687 macro_start->next = NULL;
2688 macro_start->text = nasm_strdup("'''");
2689 if (evalresult->value > 0
Charles Crayne192d5b52007-10-18 19:02:42 -07002690 && evalresult->value < (int) strlen(t->text) - 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002691 macro_start->text[1] = t->text[evalresult->value];
2692 } else {
2693 macro_start->text[2] = '\0';
2694 }
2695 macro_start->type = TOK_STRING;
2696 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002697
H. Peter Anvine2c80182005-01-15 22:15:51 +00002698 /*
2699 * We now have a macro name, an implicit parameter count of
2700 * zero, and a numeric token to use as an expansion. Create
2701 * and store an SMacro.
2702 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002703 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002704 free_tlist(tline);
2705 free_tlist(origline);
2706 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002707
H. Peter Anvine2c80182005-01-15 22:15:51 +00002708 case PP_ASSIGN:
2709 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002710 casesense = (i == PP_ASSIGN);
2711
H. Peter Anvine2c80182005-01-15 22:15:51 +00002712 tline = tline->next;
2713 skip_white_(tline);
2714 tline = expand_id(tline);
2715 if (!tline || (tline->type != TOK_ID &&
2716 (tline->type != TOK_PREPROC_ID ||
2717 tline->text[1] != '$'))) {
2718 error(ERR_NONFATAL,
2719 "`%%%sassign' expects a macro identifier",
2720 (i == PP_IASSIGN ? "i" : ""));
2721 free_tlist(origline);
2722 return DIRECTIVE_FOUND;
2723 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002724 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002725
H. Peter Anvine2c80182005-01-15 22:15:51 +00002726 mname = tline->text;
2727 last = tline;
2728 tline = expand_smacro(tline->next);
2729 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002730
H. Peter Anvine2c80182005-01-15 22:15:51 +00002731 t = tline;
2732 tptr = &t;
2733 tokval.t_type = TOKEN_INVALID;
2734 evalresult =
2735 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2736 free_tlist(tline);
2737 if (!evalresult) {
2738 free_tlist(origline);
2739 return DIRECTIVE_FOUND;
2740 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002741
H. Peter Anvine2c80182005-01-15 22:15:51 +00002742 if (tokval.t_type)
2743 error(ERR_WARNING,
2744 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00002745
H. Peter Anvine2c80182005-01-15 22:15:51 +00002746 if (!is_simple(evalresult)) {
2747 error(ERR_NONFATAL,
2748 "non-constant value given to `%%%sassign'",
2749 (i == PP_IASSIGN ? "i" : ""));
2750 free_tlist(origline);
2751 return DIRECTIVE_FOUND;
2752 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002753
H. Peter Anvine2c80182005-01-15 22:15:51 +00002754 macro_start = nasm_malloc(sizeof(*macro_start));
2755 macro_start->next = NULL;
2756 make_tok_num(macro_start, reloc_value(evalresult));
2757 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002758
H. Peter Anvine2c80182005-01-15 22:15:51 +00002759 /*
2760 * We now have a macro name, an implicit parameter count of
2761 * zero, and a numeric token to use as an expansion. Create
2762 * and store an SMacro.
2763 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002764 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002765 free_tlist(origline);
2766 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002767
H. Peter Anvine2c80182005-01-15 22:15:51 +00002768 case PP_LINE:
2769 /*
2770 * Syntax is `%line nnn[+mmm] [filename]'
2771 */
2772 tline = tline->next;
2773 skip_white_(tline);
2774 if (!tok_type_(tline, TOK_NUMBER)) {
2775 error(ERR_NONFATAL, "`%%line' expects line number");
2776 free_tlist(origline);
2777 return DIRECTIVE_FOUND;
2778 }
H. Peter Anvin70055962007-10-11 00:05:31 -07002779 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002780 m = 1;
2781 tline = tline->next;
2782 if (tok_is_(tline, "+")) {
2783 tline = tline->next;
2784 if (!tok_type_(tline, TOK_NUMBER)) {
2785 error(ERR_NONFATAL, "`%%line' expects line increment");
2786 free_tlist(origline);
2787 return DIRECTIVE_FOUND;
2788 }
H. Peter Anvin70055962007-10-11 00:05:31 -07002789 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002790 tline = tline->next;
2791 }
2792 skip_white_(tline);
2793 src_set_linnum(k);
2794 istk->lineinc = m;
2795 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002796 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002797 }
2798 free_tlist(origline);
2799 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002800
H. Peter Anvine2c80182005-01-15 22:15:51 +00002801 default:
2802 error(ERR_FATAL,
2803 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00002804 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002805 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002806 }
Ed Beroset3ab3f412002-06-11 03:31:49 +00002807 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002808}
2809
2810/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002811 * Ensure that a macro parameter contains a condition code and
2812 * nothing else. Return the condition code index if so, or -1
2813 * otherwise.
2814 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002815static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002816{
H. Peter Anvin76690a12002-04-30 20:52:49 +00002817 Token *tt;
2818 int i, j, k, m;
2819
H. Peter Anvin25a99342007-09-22 17:45:45 -07002820 if (!t)
2821 return -1; /* Probably a %+ without a space */
2822
H. Peter Anvineba20a72002-04-30 20:53:55 +00002823 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002824 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002825 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002826 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002827 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002828 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002829 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002830
2831 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00002832 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002833 while (j - i > 1) {
2834 k = (j + i) / 2;
2835 m = nasm_stricmp(t->text, conditions[k]);
2836 if (m == 0) {
2837 i = k;
2838 j = -2;
2839 break;
2840 } else if (m < 0) {
2841 j = k;
2842 } else
2843 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002844 }
2845 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002846 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002847 return i;
2848}
2849
2850/*
2851 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2852 * %-n) and MMacro-local identifiers (%%foo).
2853 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002854static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002855{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002856 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002857
2858 tail = &thead;
2859 thead = NULL;
2860
H. Peter Anvine2c80182005-01-15 22:15:51 +00002861 while (tline) {
2862 if (tline->type == TOK_PREPROC_ID &&
2863 (((tline->text[1] == '+' || tline->text[1] == '-')
2864 && tline->text[2]) || tline->text[1] == '%'
2865 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002866 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002867 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002868 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07002869 unsigned int n;
2870 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002871 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002872
H. Peter Anvine2c80182005-01-15 22:15:51 +00002873 t = tline;
2874 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002875
H. Peter Anvine2c80182005-01-15 22:15:51 +00002876 mac = istk->mstk;
2877 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2878 mac = mac->next_active;
2879 if (!mac)
2880 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2881 else
2882 switch (t->text[1]) {
2883 /*
2884 * We have to make a substitution of one of the
2885 * forms %1, %-1, %+1, %%foo, %0.
2886 */
2887 case '0':
2888 type = TOK_NUMBER;
2889 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
2890 text = nasm_strdup(tmpbuf);
2891 break;
2892 case '%':
2893 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002894 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002895 mac->unique);
2896 text = nasm_strcat(tmpbuf, t->text + 2);
2897 break;
2898 case '-':
2899 n = atoi(t->text + 2) - 1;
2900 if (n >= mac->nparam)
2901 tt = NULL;
2902 else {
2903 if (mac->nparam > 1)
2904 n = (n + mac->rotate) % mac->nparam;
2905 tt = mac->params[n];
2906 }
2907 cc = find_cc(tt);
2908 if (cc == -1) {
2909 error(ERR_NONFATAL,
2910 "macro parameter %d is not a condition code",
2911 n + 1);
2912 text = NULL;
2913 } else {
2914 type = TOK_ID;
2915 if (inverse_ccs[cc] == -1) {
2916 error(ERR_NONFATAL,
2917 "condition code `%s' is not invertible",
2918 conditions[cc]);
2919 text = NULL;
2920 } else
2921 text =
2922 nasm_strdup(conditions[inverse_ccs[cc]]);
2923 }
2924 break;
2925 case '+':
2926 n = atoi(t->text + 2) - 1;
2927 if (n >= mac->nparam)
2928 tt = NULL;
2929 else {
2930 if (mac->nparam > 1)
2931 n = (n + mac->rotate) % mac->nparam;
2932 tt = mac->params[n];
2933 }
2934 cc = find_cc(tt);
2935 if (cc == -1) {
2936 error(ERR_NONFATAL,
2937 "macro parameter %d is not a condition code",
2938 n + 1);
2939 text = NULL;
2940 } else {
2941 type = TOK_ID;
2942 text = nasm_strdup(conditions[cc]);
2943 }
2944 break;
2945 default:
2946 n = atoi(t->text + 1) - 1;
2947 if (n >= mac->nparam)
2948 tt = NULL;
2949 else {
2950 if (mac->nparam > 1)
2951 n = (n + mac->rotate) % mac->nparam;
2952 tt = mac->params[n];
2953 }
2954 if (tt) {
2955 for (i = 0; i < mac->paramlen[n]; i++) {
2956 *tail = new_Token(NULL, tt->type, tt->text, 0);
2957 tail = &(*tail)->next;
2958 tt = tt->next;
2959 }
2960 }
2961 text = NULL; /* we've done it here */
2962 break;
2963 }
2964 if (!text) {
2965 delete_Token(t);
2966 } else {
2967 *tail = t;
2968 tail = &t->next;
2969 t->type = type;
2970 nasm_free(t->text);
2971 t->text = text;
2972 t->mac = NULL;
2973 }
2974 continue;
2975 } else {
2976 t = *tail = tline;
2977 tline = tline->next;
2978 t->mac = NULL;
2979 tail = &t->next;
2980 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002981 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00002982 *tail = NULL;
2983 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002984 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002985 switch (t->type) {
2986 case TOK_WHITESPACE:
2987 if (tt->type == TOK_WHITESPACE) {
2988 t->next = delete_Token(tt);
2989 }
2990 break;
2991 case TOK_ID:
2992 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002993 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002994 nasm_free(t->text);
2995 t->text = tmp;
2996 t->next = delete_Token(tt);
2997 }
2998 break;
2999 case TOK_NUMBER:
3000 if (tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003001 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003002 nasm_free(t->text);
3003 t->text = tmp;
3004 t->next = delete_Token(tt);
3005 }
3006 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003007 default:
3008 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003009 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003010
H. Peter Anvin76690a12002-04-30 20:52:49 +00003011 return thead;
3012}
3013
3014/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003015 * Expand all single-line macro calls made in the given line.
3016 * Return the expanded version of the line. The original is deemed
3017 * to be destroyed in the process. (In reality we'll just move
3018 * Tokens from input to output a lot of the time, rather than
3019 * actually bothering to destroy and replicate.)
3020 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003021#define DEADMAN_LIMIT (1 << 20)
3022
H. Peter Anvine2c80182005-01-15 22:15:51 +00003023static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003024{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003025 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003026 struct hash_table *smtbl;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003027 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003028 Token **params;
3029 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003030 unsigned int nparam, sparam;
3031 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003032 Token *org_tline = tline;
3033 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003034 char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003035 int deadman = DEADMAN_LIMIT;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003036
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003037 /*
3038 * Trick: we should avoid changing the start token pointer since it can
3039 * be contained in "next" field of other token. Because of this
3040 * we allocate a copy of first token and work with it; at the end of
3041 * routine we copy it back
3042 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003043 if (org_tline) {
3044 tline =
3045 new_Token(org_tline->next, org_tline->type, org_tline->text,
3046 0);
3047 tline->mac = org_tline->mac;
3048 nasm_free(org_tline->text);
3049 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003050 }
3051
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003052again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003053 tail = &thead;
3054 thead = NULL;
3055
H. Peter Anvine2c80182005-01-15 22:15:51 +00003056 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003057 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003058 error(ERR_NONFATAL, "interminable macro recursion");
3059 break;
3060 }
3061
H. Peter Anvine2c80182005-01-15 22:15:51 +00003062 if ((mname = tline->text)) {
3063 /* if this token is a local macro, look in local context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07003064 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003065 ctx = get_ctx(mname, true);
H. Peter Anvin166c2472008-05-28 12:28:58 -07003066 smtbl = &ctx->localmac;
3067 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003068 ctx = NULL;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003069 smtbl = &smacros;
3070 }
H. Peter Anvin072771e2008-05-22 13:17:51 -07003071
H. Peter Anvin166c2472008-05-28 12:28:58 -07003072 head = (SMacro *) hash_findix(smtbl, mname);
H. Peter Anvin072771e2008-05-22 13:17:51 -07003073
H. Peter Anvine2c80182005-01-15 22:15:51 +00003074 /*
3075 * We've hit an identifier. As in is_mmacro below, we first
3076 * check whether the identifier is a single-line macro at
3077 * all, then think about checking for parameters if
3078 * necessary.
3079 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003080 for (m = head; m; m = m->next)
3081 if (!mstrcmp(m->name, mname, m->casesense))
3082 break;
3083 if (m) {
3084 mstart = tline;
3085 params = NULL;
3086 paramsize = NULL;
3087 if (m->nparam == 0) {
3088 /*
3089 * Simple case: the macro is parameterless. Discard the
3090 * one token that the macro call took, and push the
3091 * expansion back on the to-do stack.
3092 */
3093 if (!m->expansion) {
3094 if (!strcmp("__FILE__", m->name)) {
3095 int32_t num = 0;
3096 src_get(&num, &(tline->text));
3097 nasm_quote(&(tline->text));
3098 tline->type = TOK_STRING;
3099 continue;
3100 }
3101 if (!strcmp("__LINE__", m->name)) {
3102 nasm_free(tline->text);
3103 make_tok_num(tline, src_get_linnum());
3104 continue;
3105 }
3106 if (!strcmp("__BITS__", m->name)) {
3107 nasm_free(tline->text);
3108 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003109 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003110 }
3111 tline = delete_Token(tline);
3112 continue;
3113 }
3114 } else {
3115 /*
3116 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003117 * exists and takes parameters. We must find the
3118 * parameters in the call, count them, find the SMacro
3119 * that corresponds to that form of the macro call, and
3120 * substitute for the parameters when we expand. What a
3121 * pain.
3122 */
3123 /*tline = tline->next;
3124 skip_white_(tline); */
3125 do {
3126 t = tline->next;
3127 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003128 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003129 t->text = NULL;
3130 t = tline->next = delete_Token(t);
3131 }
3132 tline = t;
3133 } while (tok_type_(tline, TOK_WHITESPACE));
3134 if (!tok_is_(tline, "(")) {
3135 /*
3136 * This macro wasn't called with parameters: ignore
3137 * the call. (Behaviour borrowed from gnu cpp.)
3138 */
3139 tline = mstart;
3140 m = NULL;
3141 } else {
3142 int paren = 0;
3143 int white = 0;
3144 brackets = 0;
3145 nparam = 0;
3146 sparam = PARAM_DELTA;
3147 params = nasm_malloc(sparam * sizeof(Token *));
3148 params[0] = tline->next;
3149 paramsize = nasm_malloc(sparam * sizeof(int));
3150 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003151 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003152 /*
3153 * For some unusual expansions
3154 * which concatenates function call
3155 */
3156 t = tline->next;
3157 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003158 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003159 t->text = NULL;
3160 t = tline->next = delete_Token(t);
3161 }
3162 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003163
H. Peter Anvine2c80182005-01-15 22:15:51 +00003164 if (!tline) {
3165 error(ERR_NONFATAL,
3166 "macro call expects terminating `)'");
3167 break;
3168 }
3169 if (tline->type == TOK_WHITESPACE
3170 && brackets <= 0) {
3171 if (paramsize[nparam])
3172 white++;
3173 else
3174 params[nparam] = tline->next;
3175 continue; /* parameter loop */
3176 }
3177 if (tline->type == TOK_OTHER
3178 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003179 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003180 if (ch == ',' && !paren && brackets <= 0) {
3181 if (++nparam >= sparam) {
3182 sparam += PARAM_DELTA;
3183 params = nasm_realloc(params,
3184 sparam *
3185 sizeof(Token
3186 *));
3187 paramsize =
3188 nasm_realloc(paramsize,
3189 sparam *
3190 sizeof(int));
3191 }
3192 params[nparam] = tline->next;
3193 paramsize[nparam] = 0;
3194 white = 0;
3195 continue; /* parameter loop */
3196 }
3197 if (ch == '{' &&
3198 (brackets > 0 || (brackets == 0 &&
3199 !paramsize[nparam])))
3200 {
3201 if (!(brackets++)) {
3202 params[nparam] = tline->next;
3203 continue; /* parameter loop */
3204 }
3205 }
3206 if (ch == '}' && brackets > 0)
3207 if (--brackets == 0) {
3208 brackets = -1;
3209 continue; /* parameter loop */
3210 }
3211 if (ch == '(' && !brackets)
3212 paren++;
3213 if (ch == ')' && brackets <= 0)
3214 if (--paren < 0)
3215 break;
3216 }
3217 if (brackets < 0) {
3218 brackets = 0;
3219 error(ERR_NONFATAL, "braces do not "
3220 "enclose all of macro parameter");
3221 }
3222 paramsize[nparam] += white + 1;
3223 white = 0;
3224 } /* parameter loop */
3225 nparam++;
3226 while (m && (m->nparam != nparam ||
3227 mstrcmp(m->name, mname,
3228 m->casesense)))
3229 m = m->next;
3230 if (!m)
3231 error(ERR_WARNING | ERR_WARN_MNP,
3232 "macro `%s' exists, "
3233 "but not taking %d parameters",
3234 mstart->text, nparam);
3235 }
3236 }
3237 if (m && m->in_progress)
3238 m = NULL;
3239 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003240 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003241 * Design question: should we handle !tline, which
3242 * indicates missing ')' here, or expand those
3243 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003244 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003245 */
3246 nasm_free(params);
3247 nasm_free(paramsize);
3248 tline = mstart;
3249 } else {
3250 /*
3251 * Expand the macro: we are placed on the last token of the
3252 * call, so that we can easily split the call from the
3253 * following tokens. We also start by pushing an SMAC_END
3254 * token for the cycle removal.
3255 */
3256 t = tline;
3257 if (t) {
3258 tline = t->next;
3259 t->next = NULL;
3260 }
3261 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3262 tt->mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003263 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003264 tline = tt;
3265 for (t = m->expansion; t; t = t->next) {
3266 if (t->type >= TOK_SMAC_PARAM) {
3267 Token *pcopy = tline, **ptail = &pcopy;
3268 Token *ttt, *pt;
3269 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003270
H. Peter Anvine2c80182005-01-15 22:15:51 +00003271 ttt = params[t->type - TOK_SMAC_PARAM];
3272 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3273 --i >= 0;) {
3274 pt = *ptail =
3275 new_Token(tline, ttt->type, ttt->text,
3276 0);
3277 ptail = &pt->next;
3278 ttt = ttt->next;
3279 }
3280 tline = pcopy;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -07003281 } else if (t->type == TOK_PREPROC_Q) {
3282 tt = new_Token(tline, TOK_ID, mname, 0);
3283 tline = tt;
3284 } else if (t->type == TOK_PREPROC_QQ) {
3285 tt = new_Token(tline, TOK_ID, m->name, 0);
3286 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003287 } else {
3288 tt = new_Token(tline, t->type, t->text, 0);
3289 tline = tt;
3290 }
3291 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003292
H. Peter Anvine2c80182005-01-15 22:15:51 +00003293 /*
3294 * Having done that, get rid of the macro call, and clean
3295 * up the parameters.
3296 */
3297 nasm_free(params);
3298 nasm_free(paramsize);
3299 free_tlist(mstart);
3300 continue; /* main token loop */
3301 }
3302 }
3303 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003304
H. Peter Anvine2c80182005-01-15 22:15:51 +00003305 if (tline->type == TOK_SMAC_END) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003306 tline->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003307 tline = delete_Token(tline);
3308 } else {
3309 t = *tail = tline;
3310 tline = tline->next;
3311 t->mac = NULL;
3312 t->next = NULL;
3313 tail = &t->next;
3314 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003315 }
3316
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003317 /*
3318 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003319 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003320 * TOK_IDs should be concatenated.
3321 * Also we look for %+ tokens and concatenate the tokens before and after
3322 * them (without white spaces in between).
3323 */
3324 t = thead;
3325 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003326 while (t) {
3327 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3328 t = t->next;
3329 if (!t || !t->next)
3330 break;
3331 if (t->next->type == TOK_ID ||
3332 t->next->type == TOK_PREPROC_ID ||
3333 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003334 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003335 nasm_free(t->text);
3336 t->next = delete_Token(t->next);
3337 t->text = p;
3338 rescan = 1;
3339 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3340 t->next->next->type == TOK_PREPROC_ID &&
3341 strcmp(t->next->next->text, "%+") == 0) {
3342 /* free the next whitespace, the %+ token and next whitespace */
3343 int i;
3344 for (i = 1; i <= 3; i++) {
3345 if (!t->next
3346 || (i != 2 && t->next->type != TOK_WHITESPACE))
3347 break;
3348 t->next = delete_Token(t->next);
3349 } /* endfor */
3350 } else
3351 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003352 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003353 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003354 if (rescan) {
3355 tline = thead;
3356 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003357 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003358
H. Peter Anvine2c80182005-01-15 22:15:51 +00003359 if (org_tline) {
3360 if (thead) {
3361 *org_tline = *thead;
3362 /* since we just gave text to org_line, don't free it */
3363 thead->text = NULL;
3364 delete_Token(thead);
3365 } else {
3366 /* the expression expanded to empty line;
3367 we can't return NULL for some reasons
3368 we just set the line to a single WHITESPACE token. */
3369 memset(org_tline, 0, sizeof(*org_tline));
3370 org_tline->text = NULL;
3371 org_tline->type = TOK_WHITESPACE;
3372 }
3373 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003374 }
3375
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003376 return thead;
3377}
3378
3379/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003380 * Similar to expand_smacro but used exclusively with macro identifiers
3381 * right before they are fetched in. The reason is that there can be
3382 * identifiers consisting of several subparts. We consider that if there
3383 * are more than one element forming the name, user wants a expansion,
3384 * otherwise it will be left as-is. Example:
3385 *
3386 * %define %$abc cde
3387 *
3388 * the identifier %$abc will be left as-is so that the handler for %define
3389 * will suck it and define the corresponding value. Other case:
3390 *
3391 * %define _%$abc cde
3392 *
3393 * In this case user wants name to be expanded *before* %define starts
3394 * working, so we'll expand %$abc into something (if it has a value;
3395 * otherwise it will be left as-is) then concatenate all successive
3396 * PP_IDs into one.
3397 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003398static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003399{
3400 Token *cur, *oldnext = NULL;
3401
H. Peter Anvin734b1882002-04-30 21:01:08 +00003402 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003403 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003404
3405 cur = tline;
3406 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003407 (cur->next->type == TOK_ID ||
3408 cur->next->type == TOK_PREPROC_ID
3409 || cur->next->type == TOK_NUMBER))
3410 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003411
3412 /* If identifier consists of just one token, don't expand */
3413 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003414 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003415
H. Peter Anvine2c80182005-01-15 22:15:51 +00003416 if (cur) {
3417 oldnext = cur->next; /* Detach the tail past identifier */
3418 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003419 }
3420
H. Peter Anvin734b1882002-04-30 21:01:08 +00003421 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003422
H. Peter Anvine2c80182005-01-15 22:15:51 +00003423 if (cur) {
3424 /* expand_smacro possibly changhed tline; re-scan for EOL */
3425 cur = tline;
3426 while (cur && cur->next)
3427 cur = cur->next;
3428 if (cur)
3429 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003430 }
3431
3432 return tline;
3433}
3434
3435/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003436 * Determine whether the given line constitutes a multi-line macro
3437 * call, and return the MMacro structure called if so. Doesn't have
3438 * to check for an initial label - that's taken care of in
3439 * expand_mmacro - but must check numbers of parameters. Guaranteed
3440 * to be called with tline->type == TOK_ID, so the putative macro
3441 * name is easy to find.
3442 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003443static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003444{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003445 MMacro *head, *m;
3446 Token **params;
3447 int nparam;
3448
H. Peter Anvin166c2472008-05-28 12:28:58 -07003449 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003450
3451 /*
3452 * Efficiency: first we see if any macro exists with the given
3453 * name. If not, we can return NULL immediately. _Then_ we
3454 * count the parameters, and then we look further along the
3455 * list if necessary to find the proper MMacro.
3456 */
3457 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003458 if (!mstrcmp(m->name, tline->text, m->casesense))
3459 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003460 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003461 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003462
3463 /*
3464 * OK, we have a potential macro. Count and demarcate the
3465 * parameters.
3466 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003467 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003468
3469 /*
3470 * So we know how many parameters we've got. Find the MMacro
3471 * structure that handles this number.
3472 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003473 while (m) {
3474 if (m->nparam_min <= nparam
3475 && (m->plus || nparam <= m->nparam_max)) {
3476 /*
3477 * This one is right. Just check if cycle removal
3478 * prohibits us using it before we actually celebrate...
3479 */
3480 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003481#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003482 error(ERR_NONFATAL,
3483 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003484#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003485 nasm_free(params);
3486 return NULL;
3487 }
3488 /*
3489 * It's right, and we can use it. Add its default
3490 * parameters to the end of our list if necessary.
3491 */
3492 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3493 params =
3494 nasm_realloc(params,
3495 ((m->nparam_min + m->ndefs +
3496 1) * sizeof(*params)));
3497 while (nparam < m->nparam_min + m->ndefs) {
3498 params[nparam] = m->defaults[nparam - m->nparam_min];
3499 nparam++;
3500 }
3501 }
3502 /*
3503 * If we've gone over the maximum parameter count (and
3504 * we're in Plus mode), ignore parameters beyond
3505 * nparam_max.
3506 */
3507 if (m->plus && nparam > m->nparam_max)
3508 nparam = m->nparam_max;
3509 /*
3510 * Then terminate the parameter list, and leave.
3511 */
3512 if (!params) { /* need this special case */
3513 params = nasm_malloc(sizeof(*params));
3514 nparam = 0;
3515 }
3516 params[nparam] = NULL;
3517 *params_array = params;
3518 return m;
3519 }
3520 /*
3521 * This one wasn't right: look for the next one with the
3522 * same name.
3523 */
3524 for (m = m->next; m; m = m->next)
3525 if (!mstrcmp(m->name, tline->text, m->casesense))
3526 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003527 }
3528
3529 /*
3530 * After all that, we didn't find one with the right number of
3531 * parameters. Issue a warning, and fail to expand the macro.
3532 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003533 error(ERR_WARNING | ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003534 "macro `%s' exists, but not taking %d parameters",
3535 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003536 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003537 return NULL;
3538}
3539
3540/*
3541 * Expand the multi-line macro call made by the given line, if
3542 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003543 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003544 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003545static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003546{
3547 Token *startline = tline;
3548 Token *label = NULL;
3549 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003550 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003551 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003552 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003553 int i, nparam, *paramlen;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003554
3555 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003556 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07003557 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003558 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003559 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003560 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003561 m = is_mmacro(t, &params);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003562 if (!m) {
3563 Token *last;
3564 /*
3565 * We have an id which isn't a macro call. We'll assume
3566 * it might be a label; we'll also check to see if a
3567 * colon follows it. Then, if there's another id after
3568 * that lot, we'll check it again for macro-hood.
3569 */
3570 label = last = t;
3571 t = t->next;
3572 if (tok_type_(t, TOK_WHITESPACE))
3573 last = t, t = t->next;
3574 if (tok_is_(t, ":")) {
3575 dont_prepend = 1;
3576 last = t, t = t->next;
3577 if (tok_type_(t, TOK_WHITESPACE))
3578 last = t, t = t->next;
3579 }
3580 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3581 return 0;
3582 last->next = NULL;
3583 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003584 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003585
3586 /*
3587 * Fix up the parameters: this involves stripping leading and
3588 * trailing whitespace, then stripping braces if they are
3589 * present.
3590 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003591 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003592 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003593
H. Peter Anvine2c80182005-01-15 22:15:51 +00003594 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003595 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003596 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003597
H. Peter Anvine2c80182005-01-15 22:15:51 +00003598 t = params[i];
3599 skip_white_(t);
3600 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003601 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003602 params[i] = t;
3603 paramlen[i] = 0;
3604 while (t) {
3605 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3606 break; /* ... because we have hit a comma */
3607 if (comma && t->type == TOK_WHITESPACE
3608 && tok_is_(t->next, ","))
3609 break; /* ... or a space then a comma */
3610 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3611 break; /* ... or a brace */
3612 t = t->next;
3613 paramlen[i]++;
3614 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003615 }
3616
3617 /*
3618 * OK, we have a MMacro structure together with a set of
3619 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003620 * copies of each Line on to istk->expansion. Substitution of
3621 * parameter tokens and macro-local tokens doesn't get done
3622 * until the single-line macro substitution process; this is
3623 * because delaying them allows us to change the semantics
3624 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003625 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003626 * First, push an end marker on to istk->expansion, mark this
3627 * macro as in progress, and set up its invocation-specific
3628 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003629 */
3630 ll = nasm_malloc(sizeof(Line));
3631 ll->next = istk->expansion;
3632 ll->finishes = m;
3633 ll->first = NULL;
3634 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003635
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003636 m->in_progress = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003637 m->params = params;
3638 m->iline = tline;
3639 m->nparam = nparam;
3640 m->rotate = 0;
3641 m->paramlen = paramlen;
3642 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003643 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003644
3645 m->next_active = istk->mstk;
3646 istk->mstk = m;
3647
H. Peter Anvine2c80182005-01-15 22:15:51 +00003648 for (l = m->expansion; l; l = l->next) {
3649 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003650
H. Peter Anvine2c80182005-01-15 22:15:51 +00003651 ll = nasm_malloc(sizeof(Line));
3652 ll->finishes = NULL;
3653 ll->next = istk->expansion;
3654 istk->expansion = ll;
3655 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003656
H. Peter Anvine2c80182005-01-15 22:15:51 +00003657 for (t = l->first; t; t = t->next) {
3658 Token *x = t;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003659 switch (t->type) {
3660 case TOK_PREPROC_Q:
3661 tt = *tail = new_Token(NULL, TOK_ID, mtok->text, 0);
3662 break;
3663 case TOK_PREPROC_QQ:
3664 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
3665 break;
3666 case TOK_PREPROC_ID:
3667 if (t->text[1] == '0' && t->text[2] == '0') {
3668 dont_prepend = -1;
3669 x = label;
3670 if (!x)
3671 continue;
3672 }
3673 /* fall through */
3674 default:
3675 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3676 break;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003677 }
H. Peter Anvince2233b2008-05-25 21:57:00 -07003678 tail = &tt->next;
3679 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003680 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003681 }
3682
3683 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00003684 * If we had a label, push it on as the first line of
3685 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003686 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003687 if (label) {
3688 if (dont_prepend < 0)
3689 free_tlist(startline);
3690 else {
3691 ll = nasm_malloc(sizeof(Line));
3692 ll->finishes = NULL;
3693 ll->next = istk->expansion;
3694 istk->expansion = ll;
3695 ll->first = startline;
3696 if (!dont_prepend) {
3697 while (label->next)
3698 label = label->next;
3699 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3700 }
3701 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003702 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003703
H. Peter Anvin734b1882002-04-30 21:01:08 +00003704 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003705
H. Peter Anvineba20a72002-04-30 20:53:55 +00003706 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003707}
3708
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003709/*
3710 * Since preprocessor always operate only on the line that didn't
3711 * arrived yet, we should always use ERR_OFFBY1. Also since user
3712 * won't want to see same error twice (preprocessing is done once
3713 * per pass) we will want to show errors only during pass one.
3714 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003715static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003716{
3717 va_list arg;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003718 char buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003719
3720 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003721 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003722 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003723
H. Peter Anvin734b1882002-04-30 21:01:08 +00003724 va_start(arg, fmt);
Ed Beroset19f927a2004-12-15 17:07:03 +00003725 vsnprintf(buff, sizeof(buff), fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003726 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003727
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003728 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003729 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3730 istk->mstk->lineno, buff);
3731 else
3732 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003733}
3734
H. Peter Anvin734b1882002-04-30 21:01:08 +00003735static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003736pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003737 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003738{
H. Peter Anvin99941bf2002-05-14 17:44:03 +00003739 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003740 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003741 istk = nasm_malloc(sizeof(Include));
3742 istk->next = NULL;
3743 istk->conds = NULL;
3744 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003745 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003746 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00003747 istk->fname = NULL;
3748 src_set_fname(nasm_strdup(file));
3749 src_set_linnum(0);
3750 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003751 if (!istk->fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003752 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3753 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003754 defining = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003755 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003756 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003757 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07003758 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003759 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07003760 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003761 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003762 any_extrastdmac = (extrastdmac != NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003763 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003764 evaluate = eval;
3765 pass = apass;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003766}
3767
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003768static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003769{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003770 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003771 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003772
H. Peter Anvine2c80182005-01-15 22:15:51 +00003773 while (1) {
3774 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003775 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00003776 * buffer or from the input file.
3777 */
3778 tline = NULL;
3779 while (istk->expansion && istk->expansion->finishes) {
3780 Line *l = istk->expansion;
3781 if (!l->finishes->name && l->finishes->in_progress > 1) {
3782 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003783
H. Peter Anvine2c80182005-01-15 22:15:51 +00003784 /*
3785 * This is a macro-end marker for a macro with no
3786 * name, which means it's not really a macro at all
3787 * but a %rep block, and the `in_progress' field is
3788 * more than 1, meaning that we still need to
3789 * repeat. (1 means the natural last repetition; 0
3790 * means termination by %exitrep.) We have
3791 * therefore expanded up to the %endrep, and must
3792 * push the whole block on to the expansion buffer
3793 * again. We don't bother to remove the macro-end
3794 * marker: we'd only have to generate another one
3795 * if we did.
3796 */
3797 l->finishes->in_progress--;
3798 for (l = l->finishes->expansion; l; l = l->next) {
3799 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003800
H. Peter Anvine2c80182005-01-15 22:15:51 +00003801 ll = nasm_malloc(sizeof(Line));
3802 ll->next = istk->expansion;
3803 ll->finishes = NULL;
3804 ll->first = NULL;
3805 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003806
H. Peter Anvine2c80182005-01-15 22:15:51 +00003807 for (t = l->first; t; t = t->next) {
3808 if (t->text || t->type == TOK_WHITESPACE) {
3809 tt = *tail =
3810 new_Token(NULL, t->type, t->text, 0);
3811 tail = &tt->next;
3812 }
3813 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003814
H. Peter Anvine2c80182005-01-15 22:15:51 +00003815 istk->expansion = ll;
3816 }
3817 } else {
3818 /*
3819 * Check whether a `%rep' was started and not ended
3820 * within this macro expansion. This can happen and
3821 * should be detected. It's a fatal error because
3822 * I'm too confused to work out how to recover
3823 * sensibly from it.
3824 */
3825 if (defining) {
3826 if (defining->name)
3827 error(ERR_PANIC,
3828 "defining with name in expansion");
3829 else if (istk->mstk->name)
3830 error(ERR_FATAL,
3831 "`%%rep' without `%%endrep' within"
3832 " expansion of macro `%s'",
3833 istk->mstk->name);
3834 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00003835
H. Peter Anvine2c80182005-01-15 22:15:51 +00003836 /*
3837 * FIXME: investigate the relationship at this point between
3838 * istk->mstk and l->finishes
3839 */
3840 {
3841 MMacro *m = istk->mstk;
3842 istk->mstk = m->next_active;
3843 if (m->name) {
3844 /*
3845 * This was a real macro call, not a %rep, and
3846 * therefore the parameter information needs to
3847 * be freed.
3848 */
3849 nasm_free(m->params);
3850 free_tlist(m->iline);
3851 nasm_free(m->paramlen);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003852 l->finishes->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003853 } else
3854 free_mmacro(m);
3855 }
3856 istk->expansion = l->next;
3857 nasm_free(l);
3858 list->downlevel(LIST_MACRO);
3859 }
3860 }
3861 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003862
H. Peter Anvine2c80182005-01-15 22:15:51 +00003863 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003864 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003865 Line *l = istk->expansion;
3866 if (istk->mstk)
3867 istk->mstk->lineno++;
3868 tline = l->first;
3869 istk->expansion = l->next;
3870 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003871 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003872 list->line(LIST_MACRO, p);
3873 nasm_free(p);
3874 break;
3875 }
3876 line = read_line();
3877 if (line) { /* from the current input file */
3878 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00003879 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003880 nasm_free(line);
3881 break;
3882 }
3883 /*
3884 * The current file has ended; work down the istk
3885 */
3886 {
3887 Include *i = istk;
3888 fclose(i->fp);
3889 if (i->conds)
3890 error(ERR_FATAL,
3891 "expected `%%endif' before end of file");
3892 /* only set line and file name if there's a next node */
3893 if (i->next) {
3894 src_set_linnum(i->lineno);
3895 nasm_free(src_set_fname(i->fname));
3896 }
3897 istk = i->next;
3898 list->downlevel(LIST_INCLUDE);
3899 nasm_free(i);
3900 if (!istk)
3901 return NULL;
3902 }
3903 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003904
H. Peter Anvine2c80182005-01-15 22:15:51 +00003905 /*
3906 * We must expand MMacro parameters and MMacro-local labels
3907 * _before_ we plunge into directive processing, to cope
3908 * with things like `%define something %1' such as STRUC
3909 * uses. Unless we're _defining_ a MMacro, in which case
3910 * those tokens should be left alone to go into the
3911 * definition; and unless we're in a non-emitting
3912 * condition, in which case we don't want to meddle with
3913 * anything.
3914 */
3915 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
3916 tline = expand_mmac_params(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003917
H. Peter Anvine2c80182005-01-15 22:15:51 +00003918 /*
3919 * Check the line to see if it's a preprocessor directive.
3920 */
3921 if (do_directive(tline) == DIRECTIVE_FOUND) {
3922 continue;
3923 } else if (defining) {
3924 /*
3925 * We're defining a multi-line macro. We emit nothing
3926 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00003927 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003928 */
3929 Line *l = nasm_malloc(sizeof(Line));
3930 l->next = defining->expansion;
3931 l->first = tline;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003932 l->finishes = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003933 defining->expansion = l;
3934 continue;
3935 } else if (istk->conds && !emitting(istk->conds->state)) {
3936 /*
3937 * We're in a non-emitting branch of a condition block.
3938 * Emit nothing at all, not even a blank line: when we
3939 * emerge from the condition we'll give a line-number
3940 * directive so we keep our place correctly.
3941 */
3942 free_tlist(tline);
3943 continue;
3944 } else if (istk->mstk && !istk->mstk->in_progress) {
3945 /*
3946 * We're in a %rep block which has been terminated, so
3947 * we're walking through to the %endrep without
3948 * emitting anything. Emit nothing at all, not even a
3949 * blank line: when we emerge from the %rep block we'll
3950 * give a line-number directive so we keep our place
3951 * correctly.
3952 */
3953 free_tlist(tline);
3954 continue;
3955 } else {
3956 tline = expand_smacro(tline);
3957 if (!expand_mmacro(tline)) {
3958 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003959 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003960 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003961 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003962 free_tlist(tline);
3963 break;
3964 } else {
3965 continue; /* expand_mmacro calls free_tlist */
3966 }
3967 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003968 }
3969
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003970 return line;
3971}
3972
H. Peter Anvine2c80182005-01-15 22:15:51 +00003973static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003974{
H. Peter Anvine2c80182005-01-15 22:15:51 +00003975 if (defining) {
3976 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
3977 defining->name);
3978 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003979 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003980 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003981 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07003982 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00003983 while (istk) {
3984 Include *i = istk;
3985 istk = istk->next;
3986 fclose(i->fp);
3987 nasm_free(i->fname);
3988 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003989 }
3990 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003991 ctx_pop();
3992 if (pass == 0) {
3993 free_llist(predef);
3994 delete_Blocks();
3995 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003996}
3997
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003998void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003999{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004000 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004001
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004002 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004003 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004004 i->next = NULL;
4005
H. Peter Anvine2c80182005-01-15 22:15:51 +00004006 if (ipath != NULL) {
4007 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004008 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004009 j = j->next;
4010 j->next = i;
4011 } else {
4012 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004013 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004014}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004015
4016/*
4017 * added by alexfru:
4018 *
4019 * This function is used to "export" the include paths, e.g.
4020 * the paths specified in the '-I' command switch.
4021 * The need for such exporting is due to the 'incbin' directive,
4022 * which includes raw binary files (unlike '%include', which
4023 * includes text source files). It would be real nice to be
4024 * able to specify paths to search for incbin'ned files also.
4025 * So, this is a simple workaround.
4026 *
4027 * The function use is simple:
4028 *
4029 * The 1st call (with NULL argument) returns a pointer to the 1st path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004030 * (char** type) or NULL if none include paths available.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004031 *
4032 * All subsequent calls take as argument the value returned by this
4033 * function last. The return value is either the next path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004034 * (char** type) or NULL if the end of the paths list is reached.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004035 *
4036 * It is maybe not the best way to do things, but I didn't want
4037 * to export too much, just one or two functions and no types or
4038 * variables exported.
4039 *
4040 * Can't say I like the current situation with e.g. this path list either,
4041 * it seems to be never deallocated after creation...
4042 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004043char **pp_get_include_path_ptr(char **pPrevPath)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004044{
4045/* This macro returns offset of a member of a structure */
4046#define GetMemberOffset(StructType,MemberName)\
4047 ((size_t)&((StructType*)0)->MemberName)
4048 IncPath *i;
4049
H. Peter Anvine2c80182005-01-15 22:15:51 +00004050 if (pPrevPath == NULL) {
4051 if (ipath != NULL)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004052 return &ipath->path;
4053 else
4054 return NULL;
4055 }
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004056 i = (IncPath *) ((char *)pPrevPath - GetMemberOffset(IncPath, path));
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004057 i = i->next;
4058 if (i != NULL)
4059 return &i->path;
4060 else
4061 return NULL;
4062#undef GetMemberOffset
4063}
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004064
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004065void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004066{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004067 Token *inc, *space, *name;
4068 Line *l;
4069
H. Peter Anvin734b1882002-04-30 21:01:08 +00004070 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4071 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4072 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004073
4074 l = nasm_malloc(sizeof(Line));
4075 l->next = predef;
4076 l->first = inc;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004077 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004078 predef = l;
4079}
4080
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004081void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004082{
4083 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004084 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004085 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004086
4087 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004088 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4089 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004090 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004091 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004092 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004093 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004094 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004095
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004096 l = nasm_malloc(sizeof(Line));
4097 l->next = predef;
4098 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004099 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004100 predef = l;
4101}
4102
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004103void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004104{
4105 Token *def, *space;
4106 Line *l;
4107
H. Peter Anvin734b1882002-04-30 21:01:08 +00004108 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4109 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004110 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004111
4112 l = nasm_malloc(sizeof(Line));
4113 l->next = predef;
4114 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004115 l->finishes = false;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004116 predef = l;
4117}
4118
Keith Kaniosb7a89542007-04-12 02:40:54 +00004119/*
4120 * Added by Keith Kanios:
4121 *
4122 * This function is used to assist with "runtime" preprocessor
4123 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4124 *
4125 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4126 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4127 */
4128
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004129void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004130{
4131 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004132
Keith Kaniosb7a89542007-04-12 02:40:54 +00004133 def = tokenize(definition);
4134 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4135 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004136
Keith Kaniosb7a89542007-04-12 02:40:54 +00004137}
4138
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004139void pp_extra_stdmac(const char **macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004140{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004141 extrastdmac = macros;
4142}
4143
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004144static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004145{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004146 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004147 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004148 tok->text = nasm_strdup(numbuf);
4149 tok->type = TOK_NUMBER;
4150}
4151
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004152Preproc nasmpp = {
4153 pp_reset,
4154 pp_getline,
4155 pp_cleanup
4156};