blob: 8598beed1570aba308a2b0f35c8d353c4c7784be [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;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700131 struct hash_table *localmac;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000132 char *name;
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 Anvin97a23472007-09-16 17:57:25 -0700340static struct hash_table *mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341
342/*
343 * The current set of single-line macros we have defined.
344 */
H. Peter Anvin97a23472007-09-16 17:57:25 -0700345static struct hash_table *smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000346
347/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000348 * The multi-line macro we are currently defining, or the %rep
349 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000350 */
351static MMacro *defining;
352
353/*
354 * The number of macro parameters to allocate space for at a time.
355 */
356#define PARAM_DELTA 16
357
358/*
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{
568 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 Anvin072771e2008-05-22 13:17:51 -0700577 smacros = hash_init(HASH_LARGE);
578 mmacros = hash_init(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 Anvin072771e2008-05-22 13:17:51 -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 Anvin072771e2008-05-22 13:17:51 -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 Anvind7ed89e2002-04-30 20:52:08 +00001337 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001338
H. Peter Anvin97a23472007-09-16 17:57:25 -07001339 if (ctx) {
H. Peter Anvin072771e2008-05-22 13:17:51 -07001340 m = (SMacro *) hash_findix(ctx->localmac, name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001341 } else if (name[0] == '%' && name[1] == '$') {
1342 if (cstk)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001343 ctx = get_ctx(name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001344 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001345 return false; /* got to return _something_ */
H. Peter Anvin072771e2008-05-22 13:17:51 -07001346 m = (SMacro *) hash_findix(ctx->localmac, name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001347 } else {
1348 m = (SMacro *) hash_findix(smacros, name);
1349 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001350
H. Peter Anvine2c80182005-01-15 22:15:51 +00001351 while (m) {
1352 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001353 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001354 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001355 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001356 *defn = m;
1357 else
1358 *defn = NULL;
1359 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001360 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361 }
1362 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001363 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001364
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001365 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001366}
1367
1368/*
1369 * Count and mark off the parameters in a multi-line macro call.
1370 * This is called both from within the multi-line macro expansion
1371 * code, and also to mark off the default parameters when provided
1372 * in a %macro definition line.
1373 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001374static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001375{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001376 int paramsize, brace;
1377
1378 *nparam = paramsize = 0;
1379 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 while (t) {
1381 if (*nparam >= paramsize) {
1382 paramsize += PARAM_DELTA;
1383 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1384 }
1385 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001386 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001387 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001388 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001389 (*params)[(*nparam)++] = t;
1390 while (tok_isnt_(t, brace ? "}" : ","))
1391 t = t->next;
1392 if (t) { /* got a comma/brace */
1393 t = t->next;
1394 if (brace) {
1395 /*
1396 * Now we've found the closing brace, look further
1397 * for the comma.
1398 */
1399 skip_white_(t);
1400 if (tok_isnt_(t, ",")) {
1401 error(ERR_NONFATAL,
1402 "braces do not enclose all of macro parameter");
1403 while (tok_isnt_(t, ","))
1404 t = t->next;
1405 }
1406 if (t)
1407 t = t->next; /* eat the comma */
1408 }
1409 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001410 }
1411}
1412
1413/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001414 * Determine whether one of the various `if' conditions is true or
1415 * not.
1416 *
1417 * We must free the tline we get passed.
1418 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001419static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001420{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001421 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001422 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001423 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001424 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001425 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001426 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001427
1428 origline = tline;
1429
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001431 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001432 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433 while (cstk && tline) {
1434 skip_white_(tline);
1435 if (!tline || tline->type != TOK_ID) {
1436 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001437 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001438 free_tlist(origline);
1439 return -1;
1440 }
1441 if (!nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001442 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001443 tline = tline->next;
1444 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001445 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001446
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001447 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001448 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001449 while (tline) {
1450 skip_white_(tline);
1451 if (!tline || (tline->type != TOK_ID &&
1452 (tline->type != TOK_PREPROC_ID ||
1453 tline->text[1] != '$'))) {
1454 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001455 "`%s' expects macro identifiers", pp_directives[ct]);
1456 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001457 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001458 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001459 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001460 tline = tline->next;
1461 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001462 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001463
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001464 case PPC_IFIDN:
1465 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001466 tline = expand_smacro(tline);
1467 t = tt = tline;
1468 while (tok_isnt_(tt, ","))
1469 tt = tt->next;
1470 if (!tt) {
1471 error(ERR_NONFATAL,
1472 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001473 pp_directives[ct]);
1474 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001475 }
1476 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001477 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001478 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1479 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1480 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001481 pp_directives[ct]);
1482 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001483 }
1484 if (t->type == TOK_WHITESPACE) {
1485 t = t->next;
1486 continue;
1487 }
1488 if (tt->type == TOK_WHITESPACE) {
1489 tt = tt->next;
1490 continue;
1491 }
1492 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001493 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001494 break;
1495 }
1496 /* Unify surrounding quotes for strings */
1497 if (t->type == TOK_STRING) {
1498 tt->text[0] = t->text[0];
1499 tt->text[strlen(tt->text) - 1] = t->text[0];
1500 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001501 if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001502 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001503 break;
1504 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001505
H. Peter Anvine2c80182005-01-15 22:15:51 +00001506 t = t->next;
1507 tt = tt->next;
1508 }
1509 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001510 j = false; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001511 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001512
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001513 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001514 {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001515 bool found = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001516 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001517
H. Peter Anvine2c80182005-01-15 22:15:51 +00001518 tline = tline->next;
1519 skip_white_(tline);
1520 tline = expand_id(tline);
1521 if (!tok_type_(tline, TOK_ID)) {
1522 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001523 "`%s' expects a macro name", pp_directives[ct]);
1524 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001525 }
1526 searching.name = nasm_strdup(tline->text);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001527 searching.casesense = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001528 searching.plus = false;
1529 searching.nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001530 searching.in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001531 searching.rep_nest = NULL;
1532 searching.nparam_min = 0;
1533 searching.nparam_max = INT_MAX;
1534 tline = expand_smacro(tline->next);
1535 skip_white_(tline);
1536 if (!tline) {
1537 } else if (!tok_type_(tline, TOK_NUMBER)) {
1538 error(ERR_NONFATAL,
1539 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001540 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001541 } else {
1542 searching.nparam_min = searching.nparam_max =
1543 readnum(tline->text, &j);
1544 if (j)
1545 error(ERR_NONFATAL,
1546 "unable to parse parameter count `%s'",
1547 tline->text);
1548 }
1549 if (tline && tok_is_(tline->next, "-")) {
1550 tline = tline->next->next;
1551 if (tok_is_(tline, "*"))
1552 searching.nparam_max = INT_MAX;
1553 else if (!tok_type_(tline, TOK_NUMBER))
1554 error(ERR_NONFATAL,
1555 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001556 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001557 else {
1558 searching.nparam_max = readnum(tline->text, &j);
1559 if (j)
1560 error(ERR_NONFATAL,
1561 "unable to parse parameter count `%s'",
1562 tline->text);
1563 if (searching.nparam_min > searching.nparam_max)
1564 error(ERR_NONFATAL,
1565 "minimum parameter count exceeds maximum");
1566 }
1567 }
1568 if (tline && tok_is_(tline->next, "+")) {
1569 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001570 searching.plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001571 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07001572 mmac = (MMacro *) hash_findix(mmacros, searching.name);
1573 while (mmac) {
1574 if (!strcmp(mmac->name, searching.name) &&
1575 (mmac->nparam_min <= searching.nparam_max
1576 || searching.plus)
1577 && (searching.nparam_min <= mmac->nparam_max
1578 || mmac->plus)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001579 found = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001580 break;
1581 }
1582 mmac = mmac->next;
1583 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001584 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001585 j = found;
1586 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001588
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001589 case PPC_IFID:
1590 needtype = TOK_ID;
1591 goto iftype;
1592 case PPC_IFNUM:
1593 needtype = TOK_NUMBER;
1594 goto iftype;
1595 case PPC_IFSTR:
1596 needtype = TOK_STRING;
1597 goto iftype;
1598
1599 iftype:
H. Peter Anvin134b9462008-02-16 17:01:40 -08001600 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001601
H. Peter Anvin927c92b2008-02-16 13:44:52 -08001602 while (tok_type_(t, TOK_WHITESPACE) ||
1603 (needtype == TOK_NUMBER &&
1604 tok_type_(t, TOK_OTHER) &&
1605 (t->text[0] == '-' || t->text[0] == '+') &&
1606 !t->text[1]))
1607 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001608
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001609 j = tok_type_(t, needtype);
1610 break;
1611
1612 case PPC_IFTOKEN:
1613 t = tline = expand_smacro(tline);
1614 while (tok_type_(t, TOK_WHITESPACE))
1615 t = t->next;
1616
1617 j = false;
1618 if (t) {
1619 t = t->next; /* Skip the actual token */
1620 while (tok_type_(t, TOK_WHITESPACE))
1621 t = t->next;
1622 j = !t; /* Should be nothing left */
1623 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001624 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001625
H. Peter Anvin134b9462008-02-16 17:01:40 -08001626 case PPC_IFEMPTY:
1627 t = tline = expand_smacro(tline);
1628 while (tok_type_(t, TOK_WHITESPACE))
1629 t = t->next;
1630
1631 j = !t; /* Should be empty */
1632 break;
1633
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001634 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 t = tline = expand_smacro(tline);
1636 tptr = &t;
1637 tokval.t_type = TOKEN_INVALID;
1638 evalresult = evaluate(ppscan, tptr, &tokval,
1639 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001640 if (!evalresult)
1641 return -1;
1642 if (tokval.t_type)
1643 error(ERR_WARNING,
1644 "trailing garbage after expression ignored");
1645 if (!is_simple(evalresult)) {
1646 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001647 "non-constant value given to `%s'", pp_directives[ct]);
1648 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001650 j = reloc_value(evalresult) != 0;
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001651 return j;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001652
H. Peter Anvine2c80182005-01-15 22:15:51 +00001653 default:
1654 error(ERR_FATAL,
1655 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001656 pp_directives[ct]);
1657 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001658 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001659
1660 free_tlist(origline);
1661 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001662
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001663fail:
1664 free_tlist(origline);
1665 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001666}
1667
1668/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001669 * Expand macros in a string. Used in %error and %include directives.
Keith Kaniosb7a89542007-04-12 02:40:54 +00001670 * First tokenize the string, apply "expand_smacro" and then de-tokenize back.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001671 * The returned variable should ALWAYS be freed after usage.
1672 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001673void expand_macros_in_string(char **p)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001674{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001675 Token *line = tokenize(*p);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001676 line = expand_smacro(line);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001677 *p = detoken(line, false);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001678}
1679
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001680/*
1681 * Common code for defining an smacro
1682 */
1683static bool define_smacro(Context *ctx, char *mname, bool casesense,
1684 int nparam, Token *expansion)
1685{
1686 SMacro *smac, **smhead;
H. Peter Anvin70653092007-10-19 14:42:29 -07001687
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001688 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1689 if (!smac) {
1690 error(ERR_WARNING,
1691 "single-line macro `%s' defined both with and"
1692 " without parameters", mname);
1693
1694 /* Some instances of the old code considered this a failure,
1695 some others didn't. What is the right thing to do here? */
1696 free_tlist(expansion);
1697 return false; /* Failure */
1698 } else {
1699 /*
1700 * We're redefining, so we have to take over an
1701 * existing SMacro structure. This means freeing
1702 * what was already in it.
1703 */
1704 nasm_free(smac->name);
1705 free_tlist(smac->expansion);
1706 }
1707 } else {
H. Peter Anvin072771e2008-05-22 13:17:51 -07001708 smhead = (SMacro **) hash_findi_add(ctx ? ctx->localmac : smacros,
1709 mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001710 smac = nasm_malloc(sizeof(SMacro));
1711 smac->next = *smhead;
1712 *smhead = smac;
1713 }
1714 smac->name = nasm_strdup(mname);
1715 smac->casesense = casesense;
1716 smac->nparam = nparam;
1717 smac->expansion = expansion;
1718 smac->in_progress = false;
1719 return true; /* Success */
1720}
1721
1722/*
1723 * Undefine an smacro
1724 */
1725static void undef_smacro(Context *ctx, const char *mname)
1726{
1727 SMacro **smhead, *s, **sp;
1728
H. Peter Anvin072771e2008-05-22 13:17:51 -07001729 smhead = (SMacro **)hash_findi(ctx ? ctx->localmac : smacros, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001730
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001731 if (smhead) {
1732 /*
1733 * We now have a macro name... go hunt for it.
1734 */
1735 sp = smhead;
1736 while ((s = *sp) != NULL) {
1737 if (!mstrcmp(s->name, mname, s->casesense)) {
1738 *sp = s->next;
1739 nasm_free(s->name);
1740 free_tlist(s->expansion);
1741 nasm_free(s);
1742 } else {
1743 sp = &s->next;
1744 }
1745 }
1746 }
1747}
1748
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001749/*
1750 * Decode a size directive
1751 */
1752static int parse_size(const char *str) {
1753 static const char *size_names[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001754 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001755 static const int sizes[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001756 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001757
1758 return sizes[bsii(str, size_names, elements(size_names))+1];
1759}
1760
Ed Beroset3ab3f412002-06-11 03:31:49 +00001761/**
1762 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001763 * Find out if a line contains a preprocessor directive, and deal
1764 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07001765 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001766 * If a directive _is_ found, it is the responsibility of this routine
1767 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001768 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001769 * @param tline a pointer to the current tokeninzed line linked list
1770 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07001771 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001772 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001773static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001774{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001775 enum preproc_token i;
1776 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07001777 bool err;
1778 int nparam;
1779 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001780 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001781 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001782 int offset;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001783 char *p, *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001784 Include *inc;
1785 Context *ctx;
1786 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001787 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001788 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1789 Line *l;
1790 struct tokenval tokval;
1791 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001792 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001793 int64_t count;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001794
1795 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001796
H. Peter Anvineba20a72002-04-30 20:53:55 +00001797 skip_white_(tline);
1798 if (!tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001799 (tline->text[1] == '%' || tline->text[1] == '$'
1800 || tline->text[1] == '!'))
1801 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001802
H. Peter Anvin4169a472007-09-12 01:29:43 +00001803 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001804
1805 /*
1806 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00001807 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001808 * we should ignore all directives except for condition
1809 * directives.
1810 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001811 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
1813 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001814 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001815
1816 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001817 * If we're defining a macro or reading a %rep block, we should
1818 * ignore all directives except for %macro/%imacro (which
1819 * generate an error), %endm/%endmacro, and (only if we're in a
H. Peter Anvineba20a72002-04-30 20:53:55 +00001820 * %rep block) %endrep. If we're in a %rep block, another %rep
1821 * causes an error, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001822 */
1823 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001824 i != PP_ENDMACRO && i != PP_ENDM &&
1825 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
1826 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001827 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001828
H. Peter Anvin4169a472007-09-12 01:29:43 +00001829 switch (i) {
1830 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
1832 tline->text);
1833 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001834
H. Peter Anvine2c80182005-01-15 22:15:51 +00001835 case PP_STACKSIZE:
1836 /* Directive to tell NASM what the default stack size is. The
1837 * default is for a 16-bit stack, and this can be overriden with
1838 * %stacksize large.
1839 * the following form:
1840 *
1841 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1842 */
1843 tline = tline->next;
1844 if (tline && tline->type == TOK_WHITESPACE)
1845 tline = tline->next;
1846 if (!tline || tline->type != TOK_ID) {
1847 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
1848 free_tlist(origline);
1849 return DIRECTIVE_FOUND;
1850 }
1851 if (nasm_stricmp(tline->text, "flat") == 0) {
1852 /* All subsequent ARG directives are for a 32-bit stack */
1853 StackSize = 4;
1854 StackPointer = "ebp";
1855 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001856 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08001857 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
1858 /* All subsequent ARG directives are for a 64-bit stack */
1859 StackSize = 8;
1860 StackPointer = "rbp";
1861 ArgOffset = 8;
1862 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001863 } else if (nasm_stricmp(tline->text, "large") == 0) {
1864 /* All subsequent ARG directives are for a 16-bit stack,
1865 * far function call.
1866 */
1867 StackSize = 2;
1868 StackPointer = "bp";
1869 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001870 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001871 } else if (nasm_stricmp(tline->text, "small") == 0) {
1872 /* All subsequent ARG directives are for a 16-bit stack,
1873 * far function call. We don't support near functions.
1874 */
1875 StackSize = 2;
1876 StackPointer = "bp";
1877 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001878 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001879 } else {
1880 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
1881 free_tlist(origline);
1882 return DIRECTIVE_FOUND;
1883 }
1884 free_tlist(origline);
1885 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001886
H. Peter Anvine2c80182005-01-15 22:15:51 +00001887 case PP_ARG:
1888 /* TASM like ARG directive to define arguments to functions, in
1889 * the following form:
1890 *
1891 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
1892 */
1893 offset = ArgOffset;
1894 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001895 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001896 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001897
H. Peter Anvine2c80182005-01-15 22:15:51 +00001898 /* Find the argument name */
1899 tline = tline->next;
1900 if (tline && tline->type == TOK_WHITESPACE)
1901 tline = tline->next;
1902 if (!tline || tline->type != TOK_ID) {
1903 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
1904 free_tlist(origline);
1905 return DIRECTIVE_FOUND;
1906 }
1907 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001908
H. Peter Anvine2c80182005-01-15 22:15:51 +00001909 /* Find the argument size type */
1910 tline = tline->next;
1911 if (!tline || tline->type != TOK_OTHER
1912 || tline->text[0] != ':') {
1913 error(ERR_NONFATAL,
1914 "Syntax error processing `%%arg' directive");
1915 free_tlist(origline);
1916 return DIRECTIVE_FOUND;
1917 }
1918 tline = tline->next;
1919 if (!tline || tline->type != TOK_ID) {
1920 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
1921 free_tlist(origline);
1922 return DIRECTIVE_FOUND;
1923 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001924
H. Peter Anvine2c80182005-01-15 22:15:51 +00001925 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001926 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001927 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001928 size = parse_size(tt->text);
1929 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001930 error(ERR_NONFATAL,
1931 "Invalid size type for `%%arg' missing directive");
1932 free_tlist(tt);
1933 free_tlist(origline);
1934 return DIRECTIVE_FOUND;
1935 }
1936 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001937
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001938 /* Round up to even stack slots */
1939 size = (size+StackSize-1) & ~(StackSize-1);
1940
H. Peter Anvine2c80182005-01-15 22:15:51 +00001941 /* Now define the macro for the argument */
1942 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
1943 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001944 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001945 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001946
H. Peter Anvine2c80182005-01-15 22:15:51 +00001947 /* Move to the next argument in the list */
1948 tline = tline->next;
1949 if (tline && tline->type == TOK_WHITESPACE)
1950 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001951 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
1952 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001953 free_tlist(origline);
1954 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001955
H. Peter Anvine2c80182005-01-15 22:15:51 +00001956 case PP_LOCAL:
1957 /* TASM like LOCAL directive to define local variables for a
1958 * function, in the following form:
1959 *
1960 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
1961 *
1962 * The '= LocalSize' at the end is ignored by NASM, but is
1963 * required by TASM to define the local parameter size (and used
1964 * by the TASM macro package).
1965 */
1966 offset = LocalOffset;
1967 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001968 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001969 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001970
H. Peter Anvine2c80182005-01-15 22:15:51 +00001971 /* Find the argument name */
1972 tline = tline->next;
1973 if (tline && tline->type == TOK_WHITESPACE)
1974 tline = tline->next;
1975 if (!tline || tline->type != TOK_ID) {
1976 error(ERR_NONFATAL,
1977 "`%%local' missing argument parameter");
1978 free_tlist(origline);
1979 return DIRECTIVE_FOUND;
1980 }
1981 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001982
H. Peter Anvine2c80182005-01-15 22:15:51 +00001983 /* Find the argument size type */
1984 tline = tline->next;
1985 if (!tline || tline->type != TOK_OTHER
1986 || tline->text[0] != ':') {
1987 error(ERR_NONFATAL,
1988 "Syntax error processing `%%local' directive");
1989 free_tlist(origline);
1990 return DIRECTIVE_FOUND;
1991 }
1992 tline = tline->next;
1993 if (!tline || tline->type != TOK_ID) {
1994 error(ERR_NONFATAL,
1995 "`%%local' missing size type parameter");
1996 free_tlist(origline);
1997 return DIRECTIVE_FOUND;
1998 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001999
H. Peter Anvine2c80182005-01-15 22:15:51 +00002000 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002001 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002002 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002003 size = parse_size(tt->text);
2004 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002005 error(ERR_NONFATAL,
2006 "Invalid size type for `%%local' missing directive");
2007 free_tlist(tt);
2008 free_tlist(origline);
2009 return DIRECTIVE_FOUND;
2010 }
2011 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002012
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002013 /* Round up to even stack slots */
2014 size = (size+StackSize-1) & ~(StackSize-1);
2015
2016 offset += size; /* Negative offset, increment before */
2017
2018 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002019 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2020 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002021 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002022
H. Peter Anvine2c80182005-01-15 22:15:51 +00002023 /* Now define the assign to setup the enter_c macro correctly */
2024 snprintf(directive, sizeof(directive),
2025 "%%assign %%$localsize %%$localsize+%d", size);
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 /* Move to the next argument in the list */
2029 tline = tline->next;
2030 if (tline && tline->type == TOK_WHITESPACE)
2031 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002032 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2033 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002034 free_tlist(origline);
2035 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002036
H. Peter Anvine2c80182005-01-15 22:15:51 +00002037 case PP_CLEAR:
2038 if (tline->next)
2039 error(ERR_WARNING, "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002040 free_macros();
2041 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002042 free_tlist(origline);
2043 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002044
H. Peter Anvine2c80182005-01-15 22:15:51 +00002045 case PP_INCLUDE:
2046 tline = tline->next;
2047 skip_white_(tline);
2048 if (!tline || (tline->type != TOK_STRING &&
2049 tline->type != TOK_INTERNAL_STRING)) {
2050 error(ERR_NONFATAL, "`%%include' expects a file name");
2051 free_tlist(origline);
2052 return DIRECTIVE_FOUND; /* but we did _something_ */
2053 }
2054 if (tline->next)
2055 error(ERR_WARNING,
2056 "trailing garbage after `%%include' ignored");
2057 if (tline->type != TOK_INTERNAL_STRING) {
2058 p = tline->text + 1; /* point past the quote to the name */
2059 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2060 } else
2061 p = tline->text; /* internal_string is easier */
2062 expand_macros_in_string(&p);
2063 inc = nasm_malloc(sizeof(Include));
2064 inc->next = istk;
2065 inc->conds = NULL;
2066 inc->fp = inc_fopen(p);
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002067 if (!inc->fp && pass == 0) {
2068 /* -MG given but file not found */
2069 nasm_free(inc);
2070 } else {
2071 inc->fname = src_set_fname(p);
2072 inc->lineno = src_set_linnum(0);
2073 inc->lineinc = 1;
2074 inc->expansion = NULL;
2075 inc->mstk = NULL;
2076 istk = inc;
2077 list->uplevel(LIST_INCLUDE);
2078 }
2079 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002080 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002081
H. Peter Anvine2c80182005-01-15 22:15:51 +00002082 case PP_PUSH:
2083 tline = tline->next;
2084 skip_white_(tline);
2085 tline = expand_id(tline);
2086 if (!tok_type_(tline, TOK_ID)) {
2087 error(ERR_NONFATAL, "`%%push' expects a context identifier");
2088 free_tlist(origline);
2089 return DIRECTIVE_FOUND; /* but we did _something_ */
2090 }
2091 if (tline->next)
2092 error(ERR_WARNING, "trailing garbage after `%%push' ignored");
2093 ctx = nasm_malloc(sizeof(Context));
2094 ctx->next = cstk;
H. Peter Anvin072771e2008-05-22 13:17:51 -07002095 ctx->localmac = hash_init(HASH_SMALL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002096 ctx->name = nasm_strdup(tline->text);
2097 ctx->number = unique++;
2098 cstk = ctx;
2099 free_tlist(origline);
2100 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002101
H. Peter Anvine2c80182005-01-15 22:15:51 +00002102 case PP_REPL:
2103 tline = tline->next;
2104 skip_white_(tline);
2105 tline = expand_id(tline);
2106 if (!tok_type_(tline, TOK_ID)) {
2107 error(ERR_NONFATAL, "`%%repl' expects a context identifier");
2108 free_tlist(origline);
2109 return DIRECTIVE_FOUND; /* but we did _something_ */
2110 }
2111 if (tline->next)
2112 error(ERR_WARNING, "trailing garbage after `%%repl' ignored");
2113 if (!cstk)
2114 error(ERR_NONFATAL, "`%%repl': context stack is empty");
2115 else {
2116 nasm_free(cstk->name);
2117 cstk->name = nasm_strdup(tline->text);
2118 }
2119 free_tlist(origline);
2120 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002121
H. Peter Anvine2c80182005-01-15 22:15:51 +00002122 case PP_POP:
2123 if (tline->next)
2124 error(ERR_WARNING, "trailing garbage after `%%pop' ignored");
2125 if (!cstk)
2126 error(ERR_NONFATAL, "`%%pop': context stack is already empty");
2127 else
2128 ctx_pop();
2129 free_tlist(origline);
2130 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002131
H. Peter Anvine2c80182005-01-15 22:15:51 +00002132 case PP_ERROR:
2133 tline->next = expand_smacro(tline->next);
2134 tline = tline->next;
2135 skip_white_(tline);
2136 if (tok_type_(tline, TOK_STRING)) {
2137 p = tline->text + 1; /* point past the quote to the name */
2138 p[strlen(p) - 1] = '\0'; /* remove the trailing quote */
2139 expand_macros_in_string(&p);
2140 error(ERR_NONFATAL, "%s", p);
2141 nasm_free(p);
2142 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002143 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002144 error(ERR_WARNING, "%s", p);
2145 nasm_free(p);
2146 }
2147 free_tlist(origline);
2148 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002149
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002150 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002151 if (istk->conds && !emitting(istk->conds->state))
2152 j = COND_NEVER;
2153 else {
2154 j = if_condition(tline->next, i);
2155 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002156 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2157 }
2158 cond = nasm_malloc(sizeof(Cond));
2159 cond->next = istk->conds;
2160 cond->state = j;
2161 istk->conds = cond;
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002162 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002163 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002164
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002165 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002166 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002167 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002168 if (emitting(istk->conds->state)
2169 || istk->conds->state == COND_NEVER)
2170 istk->conds->state = COND_NEVER;
2171 else {
2172 /*
2173 * IMPORTANT: In the case of %if, we will already have
2174 * called expand_mmac_params(); however, if we're
2175 * processing an %elif we must have been in a
2176 * non-emitting mode, which would have inhibited
2177 * the normal invocation of expand_mmac_params(). Therefore,
2178 * we have to do it explicitly here.
2179 */
2180 j = if_condition(expand_mmac_params(tline->next), i);
2181 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002182 istk->conds->state =
2183 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2184 }
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002185 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002186 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002187
H. Peter Anvine2c80182005-01-15 22:15:51 +00002188 case PP_ELSE:
2189 if (tline->next)
2190 error(ERR_WARNING, "trailing garbage after `%%else' ignored");
2191 if (!istk->conds)
2192 error(ERR_FATAL, "`%%else': no matching `%%if'");
2193 if (emitting(istk->conds->state)
2194 || istk->conds->state == COND_NEVER)
2195 istk->conds->state = COND_ELSE_FALSE;
2196 else
2197 istk->conds->state = COND_ELSE_TRUE;
2198 free_tlist(origline);
2199 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002200
H. Peter Anvine2c80182005-01-15 22:15:51 +00002201 case PP_ENDIF:
2202 if (tline->next)
2203 error(ERR_WARNING, "trailing garbage after `%%endif' ignored");
2204 if (!istk->conds)
2205 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2206 cond = istk->conds;
2207 istk->conds = cond->next;
2208 nasm_free(cond);
2209 free_tlist(origline);
2210 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002211
H. Peter Anvine2c80182005-01-15 22:15:51 +00002212 case PP_MACRO:
2213 case PP_IMACRO:
2214 if (defining)
2215 error(ERR_FATAL,
2216 "`%%%smacro': already defining a macro",
2217 (i == PP_IMACRO ? "i" : ""));
2218 tline = tline->next;
2219 skip_white_(tline);
2220 tline = expand_id(tline);
2221 if (!tok_type_(tline, TOK_ID)) {
2222 error(ERR_NONFATAL,
2223 "`%%%smacro' expects a macro name",
2224 (i == PP_IMACRO ? "i" : ""));
2225 return DIRECTIVE_FOUND;
2226 }
2227 defining = nasm_malloc(sizeof(MMacro));
2228 defining->name = nasm_strdup(tline->text);
2229 defining->casesense = (i == PP_MACRO);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002230 defining->plus = false;
2231 defining->nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002232 defining->in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002233 defining->rep_nest = NULL;
2234 tline = expand_smacro(tline->next);
2235 skip_white_(tline);
2236 if (!tok_type_(tline, TOK_NUMBER)) {
2237 error(ERR_NONFATAL,
2238 "`%%%smacro' expects a parameter count",
2239 (i == PP_IMACRO ? "i" : ""));
2240 defining->nparam_min = defining->nparam_max = 0;
2241 } else {
2242 defining->nparam_min = defining->nparam_max =
H. Peter Anvin70055962007-10-11 00:05:31 -07002243 readnum(tline->text, &err);
2244 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002245 error(ERR_NONFATAL,
2246 "unable to parse parameter count `%s'", tline->text);
2247 }
2248 if (tline && tok_is_(tline->next, "-")) {
2249 tline = tline->next->next;
2250 if (tok_is_(tline, "*"))
2251 defining->nparam_max = INT_MAX;
2252 else if (!tok_type_(tline, TOK_NUMBER))
2253 error(ERR_NONFATAL,
2254 "`%%%smacro' expects a parameter count after `-'",
2255 (i == PP_IMACRO ? "i" : ""));
2256 else {
H. Peter Anvin70055962007-10-11 00:05:31 -07002257 defining->nparam_max = readnum(tline->text, &err);
2258 if (err)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002259 error(ERR_NONFATAL,
2260 "unable to parse parameter count `%s'",
2261 tline->text);
2262 if (defining->nparam_min > defining->nparam_max)
2263 error(ERR_NONFATAL,
2264 "minimum parameter count exceeds maximum");
2265 }
2266 }
2267 if (tline && tok_is_(tline->next, "+")) {
2268 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002269 defining->plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002270 }
2271 if (tline && tok_type_(tline->next, TOK_ID) &&
2272 !nasm_stricmp(tline->next->text, ".nolist")) {
2273 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002274 defining->nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002275 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07002276 mmac = (MMacro *) hash_findix(mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002277 while (mmac) {
2278 if (!strcmp(mmac->name, defining->name) &&
2279 (mmac->nparam_min <= defining->nparam_max
2280 || defining->plus)
2281 && (defining->nparam_min <= mmac->nparam_max
2282 || mmac->plus)) {
2283 error(ERR_WARNING,
2284 "redefining multi-line macro `%s'", defining->name);
2285 break;
2286 }
2287 mmac = mmac->next;
2288 }
2289 /*
2290 * Handle default parameters.
2291 */
2292 if (tline && tline->next) {
2293 defining->dlist = tline->next;
2294 tline->next = NULL;
2295 count_mmac_params(defining->dlist, &defining->ndefs,
2296 &defining->defaults);
2297 } else {
2298 defining->dlist = NULL;
2299 defining->defaults = NULL;
2300 }
2301 defining->expansion = NULL;
2302 free_tlist(origline);
2303 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002304
H. Peter Anvine2c80182005-01-15 22:15:51 +00002305 case PP_ENDM:
2306 case PP_ENDMACRO:
2307 if (!defining) {
2308 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2309 return DIRECTIVE_FOUND;
2310 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07002311 mmhead = (MMacro **) hash_findi_add(mmacros, defining->name);
2312 defining->next = *mmhead;
2313 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002314 defining = NULL;
2315 free_tlist(origline);
2316 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002317
H. Peter Anvine2c80182005-01-15 22:15:51 +00002318 case PP_ROTATE:
2319 if (tline->next && tline->next->type == TOK_WHITESPACE)
2320 tline = tline->next;
2321 if (tline->next == NULL) {
2322 free_tlist(origline);
2323 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2324 return DIRECTIVE_FOUND;
2325 }
2326 t = expand_smacro(tline->next);
2327 tline->next = NULL;
2328 free_tlist(origline);
2329 tline = t;
2330 tptr = &t;
2331 tokval.t_type = TOKEN_INVALID;
2332 evalresult =
2333 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2334 free_tlist(tline);
2335 if (!evalresult)
2336 return DIRECTIVE_FOUND;
2337 if (tokval.t_type)
2338 error(ERR_WARNING,
2339 "trailing garbage after expression ignored");
2340 if (!is_simple(evalresult)) {
2341 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2342 return DIRECTIVE_FOUND;
2343 }
2344 mmac = istk->mstk;
2345 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2346 mmac = mmac->next_active;
2347 if (!mmac) {
2348 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2349 } else if (mmac->nparam == 0) {
2350 error(ERR_NONFATAL,
2351 "`%%rotate' invoked within macro without parameters");
2352 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002353 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002354
H. Peter Anvin25a99342007-09-22 17:45:45 -07002355 rotate %= (int)mmac->nparam;
2356 if (rotate < 0)
2357 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002358
H. Peter Anvin25a99342007-09-22 17:45:45 -07002359 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002360 }
2361 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002362
H. Peter Anvine2c80182005-01-15 22:15:51 +00002363 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002364 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002365 do {
2366 tline = tline->next;
2367 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002368
H. Peter Anvine2c80182005-01-15 22:15:51 +00002369 if (tok_type_(tline, TOK_ID) &&
2370 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002371 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002372 do {
2373 tline = tline->next;
2374 } while (tok_type_(tline, TOK_WHITESPACE));
2375 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002376
H. Peter Anvine2c80182005-01-15 22:15:51 +00002377 if (tline) {
2378 t = expand_smacro(tline);
2379 tptr = &t;
2380 tokval.t_type = TOKEN_INVALID;
2381 evalresult =
2382 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2383 if (!evalresult) {
2384 free_tlist(origline);
2385 return DIRECTIVE_FOUND;
2386 }
2387 if (tokval.t_type)
2388 error(ERR_WARNING,
2389 "trailing garbage after expression ignored");
2390 if (!is_simple(evalresult)) {
2391 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2392 return DIRECTIVE_FOUND;
2393 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002394 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002395 } else {
2396 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002397 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002398 }
2399 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002400
H. Peter Anvine2c80182005-01-15 22:15:51 +00002401 tmp_defining = defining;
2402 defining = nasm_malloc(sizeof(MMacro));
2403 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002404 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002405 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002406 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002407 defining->in_progress = count;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002408 defining->nparam_min = defining->nparam_max = 0;
2409 defining->defaults = NULL;
2410 defining->dlist = NULL;
2411 defining->expansion = NULL;
2412 defining->next_active = istk->mstk;
2413 defining->rep_nest = tmp_defining;
2414 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002415
H. Peter Anvine2c80182005-01-15 22:15:51 +00002416 case PP_ENDREP:
2417 if (!defining || defining->name) {
2418 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2419 return DIRECTIVE_FOUND;
2420 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002421
H. Peter Anvine2c80182005-01-15 22:15:51 +00002422 /*
2423 * Now we have a "macro" defined - although it has no name
2424 * and we won't be entering it in the hash tables - we must
2425 * push a macro-end marker for it on to istk->expansion.
2426 * After that, it will take care of propagating itself (a
2427 * macro-end marker line for a macro which is really a %rep
2428 * block will cause the macro to be re-expanded, complete
2429 * with another macro-end marker to ensure the process
2430 * continues) until the whole expansion is forcibly removed
2431 * from istk->expansion by a %exitrep.
2432 */
2433 l = nasm_malloc(sizeof(Line));
2434 l->next = istk->expansion;
2435 l->finishes = defining;
2436 l->first = NULL;
2437 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002438
H. Peter Anvine2c80182005-01-15 22:15:51 +00002439 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002440
H. Peter Anvine2c80182005-01-15 22:15:51 +00002441 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2442 tmp_defining = defining;
2443 defining = defining->rep_nest;
2444 free_tlist(origline);
2445 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002446
H. Peter Anvine2c80182005-01-15 22:15:51 +00002447 case PP_EXITREP:
2448 /*
2449 * We must search along istk->expansion until we hit a
2450 * macro-end marker for a macro with no name. Then we set
2451 * its `in_progress' flag to 0.
2452 */
2453 for (l = istk->expansion; l; l = l->next)
2454 if (l->finishes && !l->finishes->name)
2455 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002456
H. Peter Anvine2c80182005-01-15 22:15:51 +00002457 if (l)
2458 l->finishes->in_progress = 0;
2459 else
2460 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2461 free_tlist(origline);
2462 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002463
H. Peter Anvine2c80182005-01-15 22:15:51 +00002464 case PP_XDEFINE:
2465 case PP_IXDEFINE:
2466 case PP_DEFINE:
2467 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002468 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002469
H. Peter Anvine2c80182005-01-15 22:15:51 +00002470 tline = tline->next;
2471 skip_white_(tline);
2472 tline = expand_id(tline);
2473 if (!tline || (tline->type != TOK_ID &&
2474 (tline->type != TOK_PREPROC_ID ||
2475 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002476 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2477 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002478 free_tlist(origline);
2479 return DIRECTIVE_FOUND;
2480 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002481
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002482 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002483
H. Peter Anvine2c80182005-01-15 22:15:51 +00002484 mname = tline->text;
2485 last = tline;
2486 param_start = tline = tline->next;
2487 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002488
H. Peter Anvine2c80182005-01-15 22:15:51 +00002489 /* Expand the macro definition now for %xdefine and %ixdefine */
2490 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2491 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002492
H. Peter Anvine2c80182005-01-15 22:15:51 +00002493 if (tok_is_(tline, "(")) {
2494 /*
2495 * This macro has parameters.
2496 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002497
H. Peter Anvine2c80182005-01-15 22:15:51 +00002498 tline = tline->next;
2499 while (1) {
2500 skip_white_(tline);
2501 if (!tline) {
2502 error(ERR_NONFATAL, "parameter identifier expected");
2503 free_tlist(origline);
2504 return DIRECTIVE_FOUND;
2505 }
2506 if (tline->type != TOK_ID) {
2507 error(ERR_NONFATAL,
2508 "`%s': parameter identifier expected",
2509 tline->text);
2510 free_tlist(origline);
2511 return DIRECTIVE_FOUND;
2512 }
2513 tline->type = TOK_SMAC_PARAM + nparam++;
2514 tline = tline->next;
2515 skip_white_(tline);
2516 if (tok_is_(tline, ",")) {
2517 tline = tline->next;
2518 continue;
2519 }
2520 if (!tok_is_(tline, ")")) {
2521 error(ERR_NONFATAL,
2522 "`)' expected to terminate macro template");
2523 free_tlist(origline);
2524 return DIRECTIVE_FOUND;
2525 }
2526 break;
2527 }
2528 last = tline;
2529 tline = tline->next;
2530 }
2531 if (tok_type_(tline, TOK_WHITESPACE))
2532 last = tline, tline = tline->next;
2533 macro_start = NULL;
2534 last->next = NULL;
2535 t = tline;
2536 while (t) {
2537 if (t->type == TOK_ID) {
2538 for (tt = param_start; tt; tt = tt->next)
2539 if (tt->type >= TOK_SMAC_PARAM &&
2540 !strcmp(tt->text, t->text))
2541 t->type = tt->type;
2542 }
2543 tt = t->next;
2544 t->next = macro_start;
2545 macro_start = t;
2546 t = tt;
2547 }
2548 /*
2549 * Good. We now have a macro name, a parameter count, and a
2550 * token list (in reverse order) for an expansion. We ought
2551 * to be OK just to create an SMacro, store it, and let
2552 * free_tlist have the rest of the line (which we have
2553 * carefully re-terminated after chopping off the expansion
2554 * from the end).
2555 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002556 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002557 free_tlist(origline);
2558 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002559
H. Peter Anvine2c80182005-01-15 22:15:51 +00002560 case PP_UNDEF:
2561 tline = tline->next;
2562 skip_white_(tline);
2563 tline = expand_id(tline);
2564 if (!tline || (tline->type != TOK_ID &&
2565 (tline->type != TOK_PREPROC_ID ||
2566 tline->text[1] != '$'))) {
2567 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2568 free_tlist(origline);
2569 return DIRECTIVE_FOUND;
2570 }
2571 if (tline->next) {
2572 error(ERR_WARNING,
2573 "trailing garbage after macro name ignored");
2574 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002575
H. Peter Anvine2c80182005-01-15 22:15:51 +00002576 /* Find the context that symbol belongs to */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002577 ctx = get_ctx(tline->text, false);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002578 undef_smacro(ctx, tline->text);
2579 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002580 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002581
H. Peter Anvine2c80182005-01-15 22:15:51 +00002582 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002583 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07002584
H. Peter Anvine2c80182005-01-15 22:15:51 +00002585 tline = tline->next;
2586 skip_white_(tline);
2587 tline = expand_id(tline);
2588 if (!tline || (tline->type != TOK_ID &&
2589 (tline->type != TOK_PREPROC_ID ||
2590 tline->text[1] != '$'))) {
2591 error(ERR_NONFATAL,
2592 "`%%strlen' expects a macro identifier as first parameter");
2593 free_tlist(origline);
2594 return DIRECTIVE_FOUND;
2595 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002596 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002597
H. Peter Anvine2c80182005-01-15 22:15:51 +00002598 mname = tline->text;
2599 last = tline;
2600 tline = expand_smacro(tline->next);
2601 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002602
H. Peter Anvine2c80182005-01-15 22:15:51 +00002603 t = tline;
2604 while (tok_type_(t, TOK_WHITESPACE))
2605 t = t->next;
2606 /* t should now point to the string */
2607 if (t->type != TOK_STRING) {
2608 error(ERR_NONFATAL,
2609 "`%%strlen` requires string as second parameter");
2610 free_tlist(tline);
2611 free_tlist(origline);
2612 return DIRECTIVE_FOUND;
2613 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002614
H. Peter Anvine2c80182005-01-15 22:15:51 +00002615 macro_start = nasm_malloc(sizeof(*macro_start));
2616 macro_start->next = NULL;
2617 make_tok_num(macro_start, strlen(t->text) - 2);
2618 macro_start->mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002619
H. Peter Anvine2c80182005-01-15 22:15:51 +00002620 /*
2621 * We now have a macro name, an implicit parameter count of
2622 * zero, and a numeric token to use as an expansion. Create
2623 * and store an SMacro.
2624 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002625 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002626 free_tlist(tline);
2627 free_tlist(origline);
2628 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002629
H. Peter Anvine2c80182005-01-15 22:15:51 +00002630 case PP_SUBSTR:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002631 casesense = true;
2632
H. Peter Anvine2c80182005-01-15 22:15:51 +00002633 tline = tline->next;
2634 skip_white_(tline);
2635 tline = expand_id(tline);
2636 if (!tline || (tline->type != TOK_ID &&
2637 (tline->type != TOK_PREPROC_ID ||
2638 tline->text[1] != '$'))) {
2639 error(ERR_NONFATAL,
2640 "`%%substr' expects a macro identifier as first parameter");
2641 free_tlist(origline);
2642 return DIRECTIVE_FOUND;
2643 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002644 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002645
H. Peter Anvine2c80182005-01-15 22:15:51 +00002646 mname = tline->text;
2647 last = tline;
2648 tline = expand_smacro(tline->next);
2649 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002650
H. Peter Anvine2c80182005-01-15 22:15:51 +00002651 t = tline->next;
2652 while (tok_type_(t, TOK_WHITESPACE))
2653 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002654
H. Peter Anvine2c80182005-01-15 22:15:51 +00002655 /* t should now point to the string */
2656 if (t->type != TOK_STRING) {
2657 error(ERR_NONFATAL,
2658 "`%%substr` requires string as second parameter");
2659 free_tlist(tline);
2660 free_tlist(origline);
2661 return DIRECTIVE_FOUND;
2662 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002663
H. Peter Anvine2c80182005-01-15 22:15:51 +00002664 tt = t->next;
2665 tptr = &tt;
2666 tokval.t_type = TOKEN_INVALID;
2667 evalresult =
2668 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2669 if (!evalresult) {
2670 free_tlist(tline);
2671 free_tlist(origline);
2672 return DIRECTIVE_FOUND;
2673 }
2674 if (!is_simple(evalresult)) {
2675 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
2676 free_tlist(tline);
2677 free_tlist(origline);
2678 return DIRECTIVE_FOUND;
2679 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002680
H. Peter Anvine2c80182005-01-15 22:15:51 +00002681 macro_start = nasm_malloc(sizeof(*macro_start));
2682 macro_start->next = NULL;
2683 macro_start->text = nasm_strdup("'''");
2684 if (evalresult->value > 0
Charles Crayne192d5b52007-10-18 19:02:42 -07002685 && evalresult->value < (int) strlen(t->text) - 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002686 macro_start->text[1] = t->text[evalresult->value];
2687 } else {
2688 macro_start->text[2] = '\0';
2689 }
2690 macro_start->type = TOK_STRING;
2691 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002692
H. Peter Anvine2c80182005-01-15 22:15:51 +00002693 /*
2694 * We now have a macro name, an implicit parameter count of
2695 * zero, and a numeric token to use as an expansion. Create
2696 * and store an SMacro.
2697 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002698 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002699 free_tlist(tline);
2700 free_tlist(origline);
2701 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002702
H. Peter Anvine2c80182005-01-15 22:15:51 +00002703 case PP_ASSIGN:
2704 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002705 casesense = (i == PP_ASSIGN);
2706
H. Peter Anvine2c80182005-01-15 22:15:51 +00002707 tline = tline->next;
2708 skip_white_(tline);
2709 tline = expand_id(tline);
2710 if (!tline || (tline->type != TOK_ID &&
2711 (tline->type != TOK_PREPROC_ID ||
2712 tline->text[1] != '$'))) {
2713 error(ERR_NONFATAL,
2714 "`%%%sassign' expects a macro identifier",
2715 (i == PP_IASSIGN ? "i" : ""));
2716 free_tlist(origline);
2717 return DIRECTIVE_FOUND;
2718 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002719 ctx = get_ctx(tline->text, false);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002720
H. Peter Anvine2c80182005-01-15 22:15:51 +00002721 mname = tline->text;
2722 last = tline;
2723 tline = expand_smacro(tline->next);
2724 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002725
H. Peter Anvine2c80182005-01-15 22:15:51 +00002726 t = tline;
2727 tptr = &t;
2728 tokval.t_type = TOKEN_INVALID;
2729 evalresult =
2730 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2731 free_tlist(tline);
2732 if (!evalresult) {
2733 free_tlist(origline);
2734 return DIRECTIVE_FOUND;
2735 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002736
H. Peter Anvine2c80182005-01-15 22:15:51 +00002737 if (tokval.t_type)
2738 error(ERR_WARNING,
2739 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00002740
H. Peter Anvine2c80182005-01-15 22:15:51 +00002741 if (!is_simple(evalresult)) {
2742 error(ERR_NONFATAL,
2743 "non-constant value given to `%%%sassign'",
2744 (i == PP_IASSIGN ? "i" : ""));
2745 free_tlist(origline);
2746 return DIRECTIVE_FOUND;
2747 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002748
H. Peter Anvine2c80182005-01-15 22:15:51 +00002749 macro_start = nasm_malloc(sizeof(*macro_start));
2750 macro_start->next = NULL;
2751 make_tok_num(macro_start, reloc_value(evalresult));
2752 macro_start->mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002753
H. Peter Anvine2c80182005-01-15 22:15:51 +00002754 /*
2755 * We now have a macro name, an implicit parameter count of
2756 * zero, and a numeric token to use as an expansion. Create
2757 * and store an SMacro.
2758 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002759 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002760 free_tlist(origline);
2761 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002762
H. Peter Anvine2c80182005-01-15 22:15:51 +00002763 case PP_LINE:
2764 /*
2765 * Syntax is `%line nnn[+mmm] [filename]'
2766 */
2767 tline = tline->next;
2768 skip_white_(tline);
2769 if (!tok_type_(tline, TOK_NUMBER)) {
2770 error(ERR_NONFATAL, "`%%line' expects line number");
2771 free_tlist(origline);
2772 return DIRECTIVE_FOUND;
2773 }
H. Peter Anvin70055962007-10-11 00:05:31 -07002774 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002775 m = 1;
2776 tline = tline->next;
2777 if (tok_is_(tline, "+")) {
2778 tline = tline->next;
2779 if (!tok_type_(tline, TOK_NUMBER)) {
2780 error(ERR_NONFATAL, "`%%line' expects line increment");
2781 free_tlist(origline);
2782 return DIRECTIVE_FOUND;
2783 }
H. Peter Anvin70055962007-10-11 00:05:31 -07002784 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002785 tline = tline->next;
2786 }
2787 skip_white_(tline);
2788 src_set_linnum(k);
2789 istk->lineinc = m;
2790 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002791 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002792 }
2793 free_tlist(origline);
2794 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002795
H. Peter Anvine2c80182005-01-15 22:15:51 +00002796 default:
2797 error(ERR_FATAL,
2798 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00002799 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002800 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002801 }
Ed Beroset3ab3f412002-06-11 03:31:49 +00002802 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002803}
2804
2805/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002806 * Ensure that a macro parameter contains a condition code and
2807 * nothing else. Return the condition code index if so, or -1
2808 * otherwise.
2809 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002810static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002811{
H. Peter Anvin76690a12002-04-30 20:52:49 +00002812 Token *tt;
2813 int i, j, k, m;
2814
H. Peter Anvin25a99342007-09-22 17:45:45 -07002815 if (!t)
2816 return -1; /* Probably a %+ without a space */
2817
H. Peter Anvineba20a72002-04-30 20:53:55 +00002818 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002819 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002820 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002821 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002822 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002823 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002824 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002825
2826 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00002827 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002828 while (j - i > 1) {
2829 k = (j + i) / 2;
2830 m = nasm_stricmp(t->text, conditions[k]);
2831 if (m == 0) {
2832 i = k;
2833 j = -2;
2834 break;
2835 } else if (m < 0) {
2836 j = k;
2837 } else
2838 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002839 }
2840 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002841 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002842 return i;
2843}
2844
2845/*
2846 * Expand MMacro-local things: parameter references (%0, %n, %+n,
2847 * %-n) and MMacro-local identifiers (%%foo).
2848 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002849static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002850{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002851 Token *t, *tt, **tail, *thead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002852
2853 tail = &thead;
2854 thead = NULL;
2855
H. Peter Anvine2c80182005-01-15 22:15:51 +00002856 while (tline) {
2857 if (tline->type == TOK_PREPROC_ID &&
2858 (((tline->text[1] == '+' || tline->text[1] == '-')
2859 && tline->text[2]) || tline->text[1] == '%'
2860 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002861 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002862 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002863 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07002864 unsigned int n;
2865 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002866 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002867
H. Peter Anvine2c80182005-01-15 22:15:51 +00002868 t = tline;
2869 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002870
H. Peter Anvine2c80182005-01-15 22:15:51 +00002871 mac = istk->mstk;
2872 while (mac && !mac->name) /* avoid mistaking %reps for macros */
2873 mac = mac->next_active;
2874 if (!mac)
2875 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
2876 else
2877 switch (t->text[1]) {
2878 /*
2879 * We have to make a substitution of one of the
2880 * forms %1, %-1, %+1, %%foo, %0.
2881 */
2882 case '0':
2883 type = TOK_NUMBER;
2884 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
2885 text = nasm_strdup(tmpbuf);
2886 break;
2887 case '%':
2888 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002889 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002890 mac->unique);
2891 text = nasm_strcat(tmpbuf, t->text + 2);
2892 break;
2893 case '-':
2894 n = atoi(t->text + 2) - 1;
2895 if (n >= mac->nparam)
2896 tt = NULL;
2897 else {
2898 if (mac->nparam > 1)
2899 n = (n + mac->rotate) % mac->nparam;
2900 tt = mac->params[n];
2901 }
2902 cc = find_cc(tt);
2903 if (cc == -1) {
2904 error(ERR_NONFATAL,
2905 "macro parameter %d is not a condition code",
2906 n + 1);
2907 text = NULL;
2908 } else {
2909 type = TOK_ID;
2910 if (inverse_ccs[cc] == -1) {
2911 error(ERR_NONFATAL,
2912 "condition code `%s' is not invertible",
2913 conditions[cc]);
2914 text = NULL;
2915 } else
2916 text =
2917 nasm_strdup(conditions[inverse_ccs[cc]]);
2918 }
2919 break;
2920 case '+':
2921 n = atoi(t->text + 2) - 1;
2922 if (n >= mac->nparam)
2923 tt = NULL;
2924 else {
2925 if (mac->nparam > 1)
2926 n = (n + mac->rotate) % mac->nparam;
2927 tt = mac->params[n];
2928 }
2929 cc = find_cc(tt);
2930 if (cc == -1) {
2931 error(ERR_NONFATAL,
2932 "macro parameter %d is not a condition code",
2933 n + 1);
2934 text = NULL;
2935 } else {
2936 type = TOK_ID;
2937 text = nasm_strdup(conditions[cc]);
2938 }
2939 break;
2940 default:
2941 n = atoi(t->text + 1) - 1;
2942 if (n >= mac->nparam)
2943 tt = NULL;
2944 else {
2945 if (mac->nparam > 1)
2946 n = (n + mac->rotate) % mac->nparam;
2947 tt = mac->params[n];
2948 }
2949 if (tt) {
2950 for (i = 0; i < mac->paramlen[n]; i++) {
2951 *tail = new_Token(NULL, tt->type, tt->text, 0);
2952 tail = &(*tail)->next;
2953 tt = tt->next;
2954 }
2955 }
2956 text = NULL; /* we've done it here */
2957 break;
2958 }
2959 if (!text) {
2960 delete_Token(t);
2961 } else {
2962 *tail = t;
2963 tail = &t->next;
2964 t->type = type;
2965 nasm_free(t->text);
2966 t->text = text;
2967 t->mac = NULL;
2968 }
2969 continue;
2970 } else {
2971 t = *tail = tline;
2972 tline = tline->next;
2973 t->mac = NULL;
2974 tail = &t->next;
2975 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002976 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00002977 *tail = NULL;
2978 t = thead;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002979 for (; t && (tt = t->next) != NULL; t = t->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00002980 switch (t->type) {
2981 case TOK_WHITESPACE:
2982 if (tt->type == TOK_WHITESPACE) {
2983 t->next = delete_Token(tt);
2984 }
2985 break;
2986 case TOK_ID:
2987 if (tt->type == TOK_ID || tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002988 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002989 nasm_free(t->text);
2990 t->text = tmp;
2991 t->next = delete_Token(tt);
2992 }
2993 break;
2994 case TOK_NUMBER:
2995 if (tt->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002996 char *tmp = nasm_strcat(t->text, tt->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002997 nasm_free(t->text);
2998 t->text = tmp;
2999 t->next = delete_Token(tt);
3000 }
3001 break;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003002 default:
3003 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003004 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003005
H. Peter Anvin76690a12002-04-30 20:52:49 +00003006 return thead;
3007}
3008
3009/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003010 * Expand all single-line macro calls made in the given line.
3011 * Return the expanded version of the line. The original is deemed
3012 * to be destroyed in the process. (In reality we'll just move
3013 * Tokens from input to output a lot of the time, rather than
3014 * actually bothering to destroy and replicate.)
3015 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003016#define DEADMAN_LIMIT (1 << 20)
3017
H. Peter Anvine2c80182005-01-15 22:15:51 +00003018static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003019{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003020 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003021 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003022 Token **params;
3023 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003024 unsigned int nparam, sparam;
3025 int brackets, rescan;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003026 Token *org_tline = tline;
3027 Context *ctx;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003028 char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003029 int deadman = DEADMAN_LIMIT;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003030
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003031 /*
3032 * Trick: we should avoid changing the start token pointer since it can
3033 * be contained in "next" field of other token. Because of this
3034 * we allocate a copy of first token and work with it; at the end of
3035 * routine we copy it back
3036 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003037 if (org_tline) {
3038 tline =
3039 new_Token(org_tline->next, org_tline->type, org_tline->text,
3040 0);
3041 tline->mac = org_tline->mac;
3042 nasm_free(org_tline->text);
3043 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003044 }
3045
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003046again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003047 tail = &thead;
3048 thead = NULL;
3049
H. Peter Anvine2c80182005-01-15 22:15:51 +00003050 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003051 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003052 error(ERR_NONFATAL, "interminable macro recursion");
3053 break;
3054 }
3055
H. Peter Anvine2c80182005-01-15 22:15:51 +00003056 if ((mname = tline->text)) {
3057 /* if this token is a local macro, look in local context */
3058 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003059 ctx = get_ctx(mname, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003060 else
3061 ctx = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -07003062
3063 head = (SMacro *) hash_findix(ctx ? ctx->localmac : smacros,
3064 mname);
3065
H. Peter Anvine2c80182005-01-15 22:15:51 +00003066 /*
3067 * We've hit an identifier. As in is_mmacro below, we first
3068 * check whether the identifier is a single-line macro at
3069 * all, then think about checking for parameters if
3070 * necessary.
3071 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003072 for (m = head; m; m = m->next)
3073 if (!mstrcmp(m->name, mname, m->casesense))
3074 break;
3075 if (m) {
3076 mstart = tline;
3077 params = NULL;
3078 paramsize = NULL;
3079 if (m->nparam == 0) {
3080 /*
3081 * Simple case: the macro is parameterless. Discard the
3082 * one token that the macro call took, and push the
3083 * expansion back on the to-do stack.
3084 */
3085 if (!m->expansion) {
3086 if (!strcmp("__FILE__", m->name)) {
3087 int32_t num = 0;
3088 src_get(&num, &(tline->text));
3089 nasm_quote(&(tline->text));
3090 tline->type = TOK_STRING;
3091 continue;
3092 }
3093 if (!strcmp("__LINE__", m->name)) {
3094 nasm_free(tline->text);
3095 make_tok_num(tline, src_get_linnum());
3096 continue;
3097 }
3098 if (!strcmp("__BITS__", m->name)) {
3099 nasm_free(tline->text);
3100 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003101 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003102 }
3103 tline = delete_Token(tline);
3104 continue;
3105 }
3106 } else {
3107 /*
3108 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003109 * exists and takes parameters. We must find the
3110 * parameters in the call, count them, find the SMacro
3111 * that corresponds to that form of the macro call, and
3112 * substitute for the parameters when we expand. What a
3113 * pain.
3114 */
3115 /*tline = tline->next;
3116 skip_white_(tline); */
3117 do {
3118 t = tline->next;
3119 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003120 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003121 t->text = NULL;
3122 t = tline->next = delete_Token(t);
3123 }
3124 tline = t;
3125 } while (tok_type_(tline, TOK_WHITESPACE));
3126 if (!tok_is_(tline, "(")) {
3127 /*
3128 * This macro wasn't called with parameters: ignore
3129 * the call. (Behaviour borrowed from gnu cpp.)
3130 */
3131 tline = mstart;
3132 m = NULL;
3133 } else {
3134 int paren = 0;
3135 int white = 0;
3136 brackets = 0;
3137 nparam = 0;
3138 sparam = PARAM_DELTA;
3139 params = nasm_malloc(sparam * sizeof(Token *));
3140 params[0] = tline->next;
3141 paramsize = nasm_malloc(sparam * sizeof(int));
3142 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003143 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003144 /*
3145 * For some unusual expansions
3146 * which concatenates function call
3147 */
3148 t = tline->next;
3149 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003150 t->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003151 t->text = NULL;
3152 t = tline->next = delete_Token(t);
3153 }
3154 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003155
H. Peter Anvine2c80182005-01-15 22:15:51 +00003156 if (!tline) {
3157 error(ERR_NONFATAL,
3158 "macro call expects terminating `)'");
3159 break;
3160 }
3161 if (tline->type == TOK_WHITESPACE
3162 && brackets <= 0) {
3163 if (paramsize[nparam])
3164 white++;
3165 else
3166 params[nparam] = tline->next;
3167 continue; /* parameter loop */
3168 }
3169 if (tline->type == TOK_OTHER
3170 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003171 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003172 if (ch == ',' && !paren && brackets <= 0) {
3173 if (++nparam >= sparam) {
3174 sparam += PARAM_DELTA;
3175 params = nasm_realloc(params,
3176 sparam *
3177 sizeof(Token
3178 *));
3179 paramsize =
3180 nasm_realloc(paramsize,
3181 sparam *
3182 sizeof(int));
3183 }
3184 params[nparam] = tline->next;
3185 paramsize[nparam] = 0;
3186 white = 0;
3187 continue; /* parameter loop */
3188 }
3189 if (ch == '{' &&
3190 (brackets > 0 || (brackets == 0 &&
3191 !paramsize[nparam])))
3192 {
3193 if (!(brackets++)) {
3194 params[nparam] = tline->next;
3195 continue; /* parameter loop */
3196 }
3197 }
3198 if (ch == '}' && brackets > 0)
3199 if (--brackets == 0) {
3200 brackets = -1;
3201 continue; /* parameter loop */
3202 }
3203 if (ch == '(' && !brackets)
3204 paren++;
3205 if (ch == ')' && brackets <= 0)
3206 if (--paren < 0)
3207 break;
3208 }
3209 if (brackets < 0) {
3210 brackets = 0;
3211 error(ERR_NONFATAL, "braces do not "
3212 "enclose all of macro parameter");
3213 }
3214 paramsize[nparam] += white + 1;
3215 white = 0;
3216 } /* parameter loop */
3217 nparam++;
3218 while (m && (m->nparam != nparam ||
3219 mstrcmp(m->name, mname,
3220 m->casesense)))
3221 m = m->next;
3222 if (!m)
3223 error(ERR_WARNING | ERR_WARN_MNP,
3224 "macro `%s' exists, "
3225 "but not taking %d parameters",
3226 mstart->text, nparam);
3227 }
3228 }
3229 if (m && m->in_progress)
3230 m = NULL;
3231 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003232 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003233 * Design question: should we handle !tline, which
3234 * indicates missing ')' here, or expand those
3235 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003236 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003237 */
3238 nasm_free(params);
3239 nasm_free(paramsize);
3240 tline = mstart;
3241 } else {
3242 /*
3243 * Expand the macro: we are placed on the last token of the
3244 * call, so that we can easily split the call from the
3245 * following tokens. We also start by pushing an SMAC_END
3246 * token for the cycle removal.
3247 */
3248 t = tline;
3249 if (t) {
3250 tline = t->next;
3251 t->next = NULL;
3252 }
3253 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
3254 tt->mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003255 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003256 tline = tt;
3257 for (t = m->expansion; t; t = t->next) {
3258 if (t->type >= TOK_SMAC_PARAM) {
3259 Token *pcopy = tline, **ptail = &pcopy;
3260 Token *ttt, *pt;
3261 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003262
H. Peter Anvine2c80182005-01-15 22:15:51 +00003263 ttt = params[t->type - TOK_SMAC_PARAM];
3264 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3265 --i >= 0;) {
3266 pt = *ptail =
3267 new_Token(tline, ttt->type, ttt->text,
3268 0);
3269 ptail = &pt->next;
3270 ttt = ttt->next;
3271 }
3272 tline = pcopy;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -07003273 } else if (t->type == TOK_PREPROC_Q) {
3274 tt = new_Token(tline, TOK_ID, mname, 0);
3275 tline = tt;
3276 } else if (t->type == TOK_PREPROC_QQ) {
3277 tt = new_Token(tline, TOK_ID, m->name, 0);
3278 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003279 } else {
3280 tt = new_Token(tline, t->type, t->text, 0);
3281 tline = tt;
3282 }
3283 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003284
H. Peter Anvine2c80182005-01-15 22:15:51 +00003285 /*
3286 * Having done that, get rid of the macro call, and clean
3287 * up the parameters.
3288 */
3289 nasm_free(params);
3290 nasm_free(paramsize);
3291 free_tlist(mstart);
3292 continue; /* main token loop */
3293 }
3294 }
3295 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003296
H. Peter Anvine2c80182005-01-15 22:15:51 +00003297 if (tline->type == TOK_SMAC_END) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003298 tline->mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003299 tline = delete_Token(tline);
3300 } else {
3301 t = *tail = tline;
3302 tline = tline->next;
3303 t->mac = NULL;
3304 t->next = NULL;
3305 tail = &t->next;
3306 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003307 }
3308
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003309 /*
3310 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003311 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003312 * TOK_IDs should be concatenated.
3313 * Also we look for %+ tokens and concatenate the tokens before and after
3314 * them (without white spaces in between).
3315 */
3316 t = thead;
3317 rescan = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003318 while (t) {
3319 while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID)
3320 t = t->next;
3321 if (!t || !t->next)
3322 break;
3323 if (t->next->type == TOK_ID ||
3324 t->next->type == TOK_PREPROC_ID ||
3325 t->next->type == TOK_NUMBER) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003326 char *p = nasm_strcat(t->text, t->next->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003327 nasm_free(t->text);
3328 t->next = delete_Token(t->next);
3329 t->text = p;
3330 rescan = 1;
3331 } else if (t->next->type == TOK_WHITESPACE && t->next->next &&
3332 t->next->next->type == TOK_PREPROC_ID &&
3333 strcmp(t->next->next->text, "%+") == 0) {
3334 /* free the next whitespace, the %+ token and next whitespace */
3335 int i;
3336 for (i = 1; i <= 3; i++) {
3337 if (!t->next
3338 || (i != 2 && t->next->type != TOK_WHITESPACE))
3339 break;
3340 t->next = delete_Token(t->next);
3341 } /* endfor */
3342 } else
3343 t = t->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003344 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003345 /* If we concatenaded something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003346 if (rescan) {
3347 tline = thead;
3348 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003349 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003350
H. Peter Anvine2c80182005-01-15 22:15:51 +00003351 if (org_tline) {
3352 if (thead) {
3353 *org_tline = *thead;
3354 /* since we just gave text to org_line, don't free it */
3355 thead->text = NULL;
3356 delete_Token(thead);
3357 } else {
3358 /* the expression expanded to empty line;
3359 we can't return NULL for some reasons
3360 we just set the line to a single WHITESPACE token. */
3361 memset(org_tline, 0, sizeof(*org_tline));
3362 org_tline->text = NULL;
3363 org_tline->type = TOK_WHITESPACE;
3364 }
3365 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003366 }
3367
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003368 return thead;
3369}
3370
3371/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003372 * Similar to expand_smacro but used exclusively with macro identifiers
3373 * right before they are fetched in. The reason is that there can be
3374 * identifiers consisting of several subparts. We consider that if there
3375 * are more than one element forming the name, user wants a expansion,
3376 * otherwise it will be left as-is. Example:
3377 *
3378 * %define %$abc cde
3379 *
3380 * the identifier %$abc will be left as-is so that the handler for %define
3381 * will suck it and define the corresponding value. Other case:
3382 *
3383 * %define _%$abc cde
3384 *
3385 * In this case user wants name to be expanded *before* %define starts
3386 * working, so we'll expand %$abc into something (if it has a value;
3387 * otherwise it will be left as-is) then concatenate all successive
3388 * PP_IDs into one.
3389 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003390static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003391{
3392 Token *cur, *oldnext = NULL;
3393
H. Peter Anvin734b1882002-04-30 21:01:08 +00003394 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003395 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003396
3397 cur = tline;
3398 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003399 (cur->next->type == TOK_ID ||
3400 cur->next->type == TOK_PREPROC_ID
3401 || cur->next->type == TOK_NUMBER))
3402 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003403
3404 /* If identifier consists of just one token, don't expand */
3405 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003406 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003407
H. Peter Anvine2c80182005-01-15 22:15:51 +00003408 if (cur) {
3409 oldnext = cur->next; /* Detach the tail past identifier */
3410 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003411 }
3412
H. Peter Anvin734b1882002-04-30 21:01:08 +00003413 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003414
H. Peter Anvine2c80182005-01-15 22:15:51 +00003415 if (cur) {
3416 /* expand_smacro possibly changhed tline; re-scan for EOL */
3417 cur = tline;
3418 while (cur && cur->next)
3419 cur = cur->next;
3420 if (cur)
3421 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003422 }
3423
3424 return tline;
3425}
3426
3427/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003428 * Determine whether the given line constitutes a multi-line macro
3429 * call, and return the MMacro structure called if so. Doesn't have
3430 * to check for an initial label - that's taken care of in
3431 * expand_mmacro - but must check numbers of parameters. Guaranteed
3432 * to be called with tline->type == TOK_ID, so the putative macro
3433 * name is easy to find.
3434 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003435static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003436{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003437 MMacro *head, *m;
3438 Token **params;
3439 int nparam;
3440
H. Peter Anvin97a23472007-09-16 17:57:25 -07003441 head = (MMacro *) hash_findix(mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003442
3443 /*
3444 * Efficiency: first we see if any macro exists with the given
3445 * name. If not, we can return NULL immediately. _Then_ we
3446 * count the parameters, and then we look further along the
3447 * list if necessary to find the proper MMacro.
3448 */
3449 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003450 if (!mstrcmp(m->name, tline->text, m->casesense))
3451 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003452 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003453 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003454
3455 /*
3456 * OK, we have a potential macro. Count and demarcate the
3457 * parameters.
3458 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003459 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003460
3461 /*
3462 * So we know how many parameters we've got. Find the MMacro
3463 * structure that handles this number.
3464 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003465 while (m) {
3466 if (m->nparam_min <= nparam
3467 && (m->plus || nparam <= m->nparam_max)) {
3468 /*
3469 * This one is right. Just check if cycle removal
3470 * prohibits us using it before we actually celebrate...
3471 */
3472 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00003473#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00003474 error(ERR_NONFATAL,
3475 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00003476#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00003477 nasm_free(params);
3478 return NULL;
3479 }
3480 /*
3481 * It's right, and we can use it. Add its default
3482 * parameters to the end of our list if necessary.
3483 */
3484 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
3485 params =
3486 nasm_realloc(params,
3487 ((m->nparam_min + m->ndefs +
3488 1) * sizeof(*params)));
3489 while (nparam < m->nparam_min + m->ndefs) {
3490 params[nparam] = m->defaults[nparam - m->nparam_min];
3491 nparam++;
3492 }
3493 }
3494 /*
3495 * If we've gone over the maximum parameter count (and
3496 * we're in Plus mode), ignore parameters beyond
3497 * nparam_max.
3498 */
3499 if (m->plus && nparam > m->nparam_max)
3500 nparam = m->nparam_max;
3501 /*
3502 * Then terminate the parameter list, and leave.
3503 */
3504 if (!params) { /* need this special case */
3505 params = nasm_malloc(sizeof(*params));
3506 nparam = 0;
3507 }
3508 params[nparam] = NULL;
3509 *params_array = params;
3510 return m;
3511 }
3512 /*
3513 * This one wasn't right: look for the next one with the
3514 * same name.
3515 */
3516 for (m = m->next; m; m = m->next)
3517 if (!mstrcmp(m->name, tline->text, m->casesense))
3518 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003519 }
3520
3521 /*
3522 * After all that, we didn't find one with the right number of
3523 * parameters. Issue a warning, and fail to expand the macro.
3524 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003525 error(ERR_WARNING | ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003526 "macro `%s' exists, but not taking %d parameters",
3527 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003528 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003529 return NULL;
3530}
3531
3532/*
3533 * Expand the multi-line macro call made by the given line, if
3534 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00003535 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003536 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003537static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003538{
3539 Token *startline = tline;
3540 Token *label = NULL;
3541 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003542 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003543 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003544 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003545 int i, nparam, *paramlen;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003546
3547 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003548 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07003549 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00003550 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003551 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003552 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003553 m = is_mmacro(t, &params);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003554 if (!m) {
3555 Token *last;
3556 /*
3557 * We have an id which isn't a macro call. We'll assume
3558 * it might be a label; we'll also check to see if a
3559 * colon follows it. Then, if there's another id after
3560 * that lot, we'll check it again for macro-hood.
3561 */
3562 label = last = t;
3563 t = t->next;
3564 if (tok_type_(t, TOK_WHITESPACE))
3565 last = t, t = t->next;
3566 if (tok_is_(t, ":")) {
3567 dont_prepend = 1;
3568 last = t, t = t->next;
3569 if (tok_type_(t, TOK_WHITESPACE))
3570 last = t, t = t->next;
3571 }
3572 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
3573 return 0;
3574 last->next = NULL;
3575 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003576 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003577
3578 /*
3579 * Fix up the parameters: this involves stripping leading and
3580 * trailing whitespace, then stripping braces if they are
3581 * present.
3582 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003583 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003584 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003585
H. Peter Anvine2c80182005-01-15 22:15:51 +00003586 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003587 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003588 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003589
H. Peter Anvine2c80182005-01-15 22:15:51 +00003590 t = params[i];
3591 skip_white_(t);
3592 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003593 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003594 params[i] = t;
3595 paramlen[i] = 0;
3596 while (t) {
3597 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
3598 break; /* ... because we have hit a comma */
3599 if (comma && t->type == TOK_WHITESPACE
3600 && tok_is_(t->next, ","))
3601 break; /* ... or a space then a comma */
3602 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
3603 break; /* ... or a brace */
3604 t = t->next;
3605 paramlen[i]++;
3606 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003607 }
3608
3609 /*
3610 * OK, we have a MMacro structure together with a set of
3611 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00003612 * copies of each Line on to istk->expansion. Substitution of
3613 * parameter tokens and macro-local tokens doesn't get done
3614 * until the single-line macro substitution process; this is
3615 * because delaying them allows us to change the semantics
3616 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003617 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00003618 * First, push an end marker on to istk->expansion, mark this
3619 * macro as in progress, and set up its invocation-specific
3620 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003621 */
3622 ll = nasm_malloc(sizeof(Line));
3623 ll->next = istk->expansion;
3624 ll->finishes = m;
3625 ll->first = NULL;
3626 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003627
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003628 m->in_progress = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003629 m->params = params;
3630 m->iline = tline;
3631 m->nparam = nparam;
3632 m->rotate = 0;
3633 m->paramlen = paramlen;
3634 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003635 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003636
3637 m->next_active = istk->mstk;
3638 istk->mstk = m;
3639
H. Peter Anvine2c80182005-01-15 22:15:51 +00003640 for (l = m->expansion; l; l = l->next) {
3641 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003642
H. Peter Anvine2c80182005-01-15 22:15:51 +00003643 ll = nasm_malloc(sizeof(Line));
3644 ll->finishes = NULL;
3645 ll->next = istk->expansion;
3646 istk->expansion = ll;
3647 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003648
H. Peter Anvine2c80182005-01-15 22:15:51 +00003649 for (t = l->first; t; t = t->next) {
3650 Token *x = t;
H. Peter Anvince2233b2008-05-25 21:57:00 -07003651 switch (t->type) {
3652 case TOK_PREPROC_Q:
3653 tt = *tail = new_Token(NULL, TOK_ID, mtok->text, 0);
3654 break;
3655 case TOK_PREPROC_QQ:
3656 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
3657 break;
3658 case TOK_PREPROC_ID:
3659 if (t->text[1] == '0' && t->text[2] == '0') {
3660 dont_prepend = -1;
3661 x = label;
3662 if (!x)
3663 continue;
3664 }
3665 /* fall through */
3666 default:
3667 tt = *tail = new_Token(NULL, x->type, x->text, 0);
3668 break;
3669 }
3670 tail = &tt->next;
3671 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003672 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003673 }
3674
3675 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00003676 * If we had a label, push it on as the first line of
3677 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003678 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003679 if (label) {
3680 if (dont_prepend < 0)
3681 free_tlist(startline);
3682 else {
3683 ll = nasm_malloc(sizeof(Line));
3684 ll->finishes = NULL;
3685 ll->next = istk->expansion;
3686 istk->expansion = ll;
3687 ll->first = startline;
3688 if (!dont_prepend) {
3689 while (label->next)
3690 label = label->next;
3691 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
3692 }
3693 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003694 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003695
H. Peter Anvin734b1882002-04-30 21:01:08 +00003696 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003697
H. Peter Anvineba20a72002-04-30 20:53:55 +00003698 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003699}
3700
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003701/*
3702 * Since preprocessor always operate only on the line that didn't
3703 * arrived yet, we should always use ERR_OFFBY1. Also since user
3704 * won't want to see same error twice (preprocessing is done once
3705 * per pass) we will want to show errors only during pass one.
3706 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003707static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003708{
3709 va_list arg;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003710 char buff[1024];
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003711
3712 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003713 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003714 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003715
H. Peter Anvin734b1882002-04-30 21:01:08 +00003716 va_start(arg, fmt);
Ed Beroset19f927a2004-12-15 17:07:03 +00003717 vsnprintf(buff, sizeof(buff), fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003718 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003719
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00003720 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003721 _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name,
3722 istk->mstk->lineno, buff);
3723 else
3724 _error(severity | ERR_PASS1, "%s", buff);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003725}
3726
H. Peter Anvin734b1882002-04-30 21:01:08 +00003727static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003728pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003729 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003730{
H. Peter Anvin99941bf2002-05-14 17:44:03 +00003731 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003732 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003733 istk = nasm_malloc(sizeof(Include));
3734 istk->next = NULL;
3735 istk->conds = NULL;
3736 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003737 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003738 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00003739 istk->fname = NULL;
3740 src_set_fname(nasm_strdup(file));
3741 src_set_linnum(0);
3742 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003743 if (!istk->fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003744 error(ERR_FATAL | ERR_NOFILE, "unable to open input file `%s'",
3745 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003746 defining = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003747 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003748 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003749 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07003750 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003751 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07003752 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003753 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003754 any_extrastdmac = (extrastdmac != NULL);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003755 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003756 evaluate = eval;
3757 pass = apass;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003758}
3759
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003760static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003761{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003762 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003763 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003764
H. Peter Anvine2c80182005-01-15 22:15:51 +00003765 while (1) {
3766 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003767 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00003768 * buffer or from the input file.
3769 */
3770 tline = NULL;
3771 while (istk->expansion && istk->expansion->finishes) {
3772 Line *l = istk->expansion;
3773 if (!l->finishes->name && l->finishes->in_progress > 1) {
3774 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003775
H. Peter Anvine2c80182005-01-15 22:15:51 +00003776 /*
3777 * This is a macro-end marker for a macro with no
3778 * name, which means it's not really a macro at all
3779 * but a %rep block, and the `in_progress' field is
3780 * more than 1, meaning that we still need to
3781 * repeat. (1 means the natural last repetition; 0
3782 * means termination by %exitrep.) We have
3783 * therefore expanded up to the %endrep, and must
3784 * push the whole block on to the expansion buffer
3785 * again. We don't bother to remove the macro-end
3786 * marker: we'd only have to generate another one
3787 * if we did.
3788 */
3789 l->finishes->in_progress--;
3790 for (l = l->finishes->expansion; l; l = l->next) {
3791 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003792
H. Peter Anvine2c80182005-01-15 22:15:51 +00003793 ll = nasm_malloc(sizeof(Line));
3794 ll->next = istk->expansion;
3795 ll->finishes = NULL;
3796 ll->first = NULL;
3797 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003798
H. Peter Anvine2c80182005-01-15 22:15:51 +00003799 for (t = l->first; t; t = t->next) {
3800 if (t->text || t->type == TOK_WHITESPACE) {
3801 tt = *tail =
3802 new_Token(NULL, t->type, t->text, 0);
3803 tail = &tt->next;
3804 }
3805 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003806
H. Peter Anvine2c80182005-01-15 22:15:51 +00003807 istk->expansion = ll;
3808 }
3809 } else {
3810 /*
3811 * Check whether a `%rep' was started and not ended
3812 * within this macro expansion. This can happen and
3813 * should be detected. It's a fatal error because
3814 * I'm too confused to work out how to recover
3815 * sensibly from it.
3816 */
3817 if (defining) {
3818 if (defining->name)
3819 error(ERR_PANIC,
3820 "defining with name in expansion");
3821 else if (istk->mstk->name)
3822 error(ERR_FATAL,
3823 "`%%rep' without `%%endrep' within"
3824 " expansion of macro `%s'",
3825 istk->mstk->name);
3826 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00003827
H. Peter Anvine2c80182005-01-15 22:15:51 +00003828 /*
3829 * FIXME: investigate the relationship at this point between
3830 * istk->mstk and l->finishes
3831 */
3832 {
3833 MMacro *m = istk->mstk;
3834 istk->mstk = m->next_active;
3835 if (m->name) {
3836 /*
3837 * This was a real macro call, not a %rep, and
3838 * therefore the parameter information needs to
3839 * be freed.
3840 */
3841 nasm_free(m->params);
3842 free_tlist(m->iline);
3843 nasm_free(m->paramlen);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003844 l->finishes->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003845 } else
3846 free_mmacro(m);
3847 }
3848 istk->expansion = l->next;
3849 nasm_free(l);
3850 list->downlevel(LIST_MACRO);
3851 }
3852 }
3853 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00003854
H. Peter Anvine2c80182005-01-15 22:15:51 +00003855 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003856 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003857 Line *l = istk->expansion;
3858 if (istk->mstk)
3859 istk->mstk->lineno++;
3860 tline = l->first;
3861 istk->expansion = l->next;
3862 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003863 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003864 list->line(LIST_MACRO, p);
3865 nasm_free(p);
3866 break;
3867 }
3868 line = read_line();
3869 if (line) { /* from the current input file */
3870 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00003871 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003872 nasm_free(line);
3873 break;
3874 }
3875 /*
3876 * The current file has ended; work down the istk
3877 */
3878 {
3879 Include *i = istk;
3880 fclose(i->fp);
3881 if (i->conds)
3882 error(ERR_FATAL,
3883 "expected `%%endif' before end of file");
3884 /* only set line and file name if there's a next node */
3885 if (i->next) {
3886 src_set_linnum(i->lineno);
3887 nasm_free(src_set_fname(i->fname));
3888 }
3889 istk = i->next;
3890 list->downlevel(LIST_INCLUDE);
3891 nasm_free(i);
3892 if (!istk)
3893 return NULL;
3894 }
3895 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003896
H. Peter Anvine2c80182005-01-15 22:15:51 +00003897 /*
3898 * We must expand MMacro parameters and MMacro-local labels
3899 * _before_ we plunge into directive processing, to cope
3900 * with things like `%define something %1' such as STRUC
3901 * uses. Unless we're _defining_ a MMacro, in which case
3902 * those tokens should be left alone to go into the
3903 * definition; and unless we're in a non-emitting
3904 * condition, in which case we don't want to meddle with
3905 * anything.
3906 */
3907 if (!defining && !(istk->conds && !emitting(istk->conds->state)))
3908 tline = expand_mmac_params(tline);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003909
H. Peter Anvine2c80182005-01-15 22:15:51 +00003910 /*
3911 * Check the line to see if it's a preprocessor directive.
3912 */
3913 if (do_directive(tline) == DIRECTIVE_FOUND) {
3914 continue;
3915 } else if (defining) {
3916 /*
3917 * We're defining a multi-line macro. We emit nothing
3918 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00003919 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003920 */
3921 Line *l = nasm_malloc(sizeof(Line));
3922 l->next = defining->expansion;
3923 l->first = tline;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003924 l->finishes = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003925 defining->expansion = l;
3926 continue;
3927 } else if (istk->conds && !emitting(istk->conds->state)) {
3928 /*
3929 * We're in a non-emitting branch of a condition block.
3930 * Emit nothing at all, not even a blank line: when we
3931 * emerge from the condition we'll give a line-number
3932 * directive so we keep our place correctly.
3933 */
3934 free_tlist(tline);
3935 continue;
3936 } else if (istk->mstk && !istk->mstk->in_progress) {
3937 /*
3938 * We're in a %rep block which has been terminated, so
3939 * we're walking through to the %endrep without
3940 * emitting anything. Emit nothing at all, not even a
3941 * blank line: when we emerge from the %rep block we'll
3942 * give a line-number directive so we keep our place
3943 * correctly.
3944 */
3945 free_tlist(tline);
3946 continue;
3947 } else {
3948 tline = expand_smacro(tline);
3949 if (!expand_mmacro(tline)) {
3950 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00003951 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003952 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003953 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003954 free_tlist(tline);
3955 break;
3956 } else {
3957 continue; /* expand_mmacro calls free_tlist */
3958 }
3959 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003960 }
3961
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003962 return line;
3963}
3964
H. Peter Anvine2c80182005-01-15 22:15:51 +00003965static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003966{
H. Peter Anvine2c80182005-01-15 22:15:51 +00003967 if (defining) {
3968 error(ERR_NONFATAL, "end of file while still defining macro `%s'",
3969 defining->name);
3970 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003971 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003972 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003973 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07003974 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00003975 while (istk) {
3976 Include *i = istk;
3977 istk = istk->next;
3978 fclose(i->fp);
3979 nasm_free(i->fname);
3980 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003981 }
3982 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003983 ctx_pop();
3984 if (pass == 0) {
3985 free_llist(predef);
3986 delete_Blocks();
3987 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003988}
3989
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003990void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003991{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003992 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07003993
H. Peter Anvin6768eb72002-04-30 20:52:26 +00003994 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07003995 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00003996 i->next = NULL;
3997
H. Peter Anvine2c80182005-01-15 22:15:51 +00003998 if (ipath != NULL) {
3999 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004000 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004001 j = j->next;
4002 j->next = i;
4003 } else {
4004 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004005 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004006}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004007
4008/*
4009 * added by alexfru:
4010 *
4011 * This function is used to "export" the include paths, e.g.
4012 * the paths specified in the '-I' command switch.
4013 * The need for such exporting is due to the 'incbin' directive,
4014 * which includes raw binary files (unlike '%include', which
4015 * includes text source files). It would be real nice to be
4016 * able to specify paths to search for incbin'ned files also.
4017 * So, this is a simple workaround.
4018 *
4019 * The function use is simple:
4020 *
4021 * The 1st call (with NULL argument) returns a pointer to the 1st path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004022 * (char** type) or NULL if none include paths available.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004023 *
4024 * All subsequent calls take as argument the value returned by this
4025 * function last. The return value is either the next path
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004026 * (char** type) or NULL if the end of the paths list is reached.
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004027 *
4028 * It is maybe not the best way to do things, but I didn't want
4029 * to export too much, just one or two functions and no types or
4030 * variables exported.
4031 *
4032 * Can't say I like the current situation with e.g. this path list either,
4033 * it seems to be never deallocated after creation...
4034 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004035char **pp_get_include_path_ptr(char **pPrevPath)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004036{
4037/* This macro returns offset of a member of a structure */
4038#define GetMemberOffset(StructType,MemberName)\
4039 ((size_t)&((StructType*)0)->MemberName)
4040 IncPath *i;
4041
H. Peter Anvine2c80182005-01-15 22:15:51 +00004042 if (pPrevPath == NULL) {
4043 if (ipath != NULL)
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004044 return &ipath->path;
4045 else
4046 return NULL;
4047 }
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004048 i = (IncPath *) ((char *)pPrevPath - GetMemberOffset(IncPath, path));
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004049 i = i->next;
4050 if (i != NULL)
4051 return &i->path;
4052 else
4053 return NULL;
4054#undef GetMemberOffset
4055}
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004056
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004057void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004058{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004059 Token *inc, *space, *name;
4060 Line *l;
4061
H. Peter Anvin734b1882002-04-30 21:01:08 +00004062 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4063 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4064 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004065
4066 l = nasm_malloc(sizeof(Line));
4067 l->next = predef;
4068 l->first = inc;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004069 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004070 predef = l;
4071}
4072
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004073void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004074{
4075 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004076 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004077 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004078
4079 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004080 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4081 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004082 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004083 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004084 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004085 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004086 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004087
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004088 l = nasm_malloc(sizeof(Line));
4089 l->next = predef;
4090 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004091 l->finishes = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004092 predef = l;
4093}
4094
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004095void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004096{
4097 Token *def, *space;
4098 Line *l;
4099
H. Peter Anvin734b1882002-04-30 21:01:08 +00004100 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4101 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004102 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004103
4104 l = nasm_malloc(sizeof(Line));
4105 l->next = predef;
4106 l->first = def;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004107 l->finishes = false;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004108 predef = l;
4109}
4110
Keith Kaniosb7a89542007-04-12 02:40:54 +00004111/*
4112 * Added by Keith Kanios:
4113 *
4114 * This function is used to assist with "runtime" preprocessor
4115 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4116 *
4117 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4118 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4119 */
4120
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004121void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004122{
4123 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004124
Keith Kaniosb7a89542007-04-12 02:40:54 +00004125 def = tokenize(definition);
4126 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4127 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004128
Keith Kaniosb7a89542007-04-12 02:40:54 +00004129}
4130
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004131void pp_extra_stdmac(const char **macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004132{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004133 extrastdmac = macros;
4134}
4135
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004136static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004137{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004138 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004139 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004140 tok->text = nasm_strdup(numbuf);
4141 tok->type = TOK_NUMBER;
4142}
4143
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004144Preproc nasmpp = {
4145 pp_reset,
4146 pp_getline,
4147 pp_cleanup
4148};