blob: 80e70c8fee58f7f185dcd29f2b1d138c6b03aad2 [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 Anvin8cad14b2008-06-01 17:23:51 -070051#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070052#include "stdscan.h"
53#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070054#include "tables.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000055
56typedef struct SMacro SMacro;
57typedef struct MMacro MMacro;
58typedef struct Context Context;
59typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000060typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000061typedef struct Line Line;
62typedef struct Include Include;
63typedef struct Cond Cond;
H. Peter Anvin6768eb72002-04-30 20:52:26 +000064typedef struct IncPath IncPath;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000065
66/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070067 * Note on the storage of both SMacro and MMacros: the hash table
68 * indexes them case-insensitively, and we then have to go through a
69 * linked list of potential case aliases (and, for MMacros, parameter
70 * ranges); this is to preserve the matching semantics of the earlier
71 * code. If the number of case aliases for a specific macro is a
72 * performance issue, you may want to reconsider your coding style.
73 */
74
75/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000076 * Store the definition of a single-line macro.
77 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000078struct SMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000079 SMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +000080 char *name;
H. Peter Anvin70055962007-10-11 00:05:31 -070081 bool casesense;
H. Peter Anvin16ed4382007-10-11 10:06:19 -070082 bool in_progress;
H. Peter Anvin70055962007-10-11 00:05:31 -070083 unsigned int nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000084 Token *expansion;
85};
86
87/*
H. Peter Anvin76690a12002-04-30 20:52:49 +000088 * Store the definition of a multi-line macro. This is also used to
89 * store the interiors of `%rep...%endrep' blocks, which are
90 * effectively self-re-invoking multi-line macros which simply
91 * don't have a name or bother to appear in the hash tables. %rep
92 * blocks are signified by having a NULL `name' field.
93 *
94 * In a MMacro describing a `%rep' block, the `in_progress' field
95 * isn't merely boolean, but gives the number of repeats left to
96 * run.
97 *
98 * The `next' field is used for storing MMacros in hash tables; the
99 * `next_active' field is for stacking them on istk entries.
100 *
101 * When a MMacro is being expanded, `params', `iline', `nparam',
102 * `paramlen', `rotate' and `unique' are local to the invocation.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000103 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000104struct MMacro {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 MMacro *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000106 char *name;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -0700107 int nparam_min, nparam_max;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700108 bool casesense;
109 bool plus; /* is the last parameter greedy? */
110 bool nolist; /* is this macro listing-inhibited? */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700111 int64_t in_progress;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000112 Token *dlist; /* All defaults as one list */
113 Token **defaults; /* Parameter default pointers */
114 int ndefs; /* number of default parameters */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000115 Line *expansion;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000116
117 MMacro *next_active;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000118 MMacro *rep_nest; /* used for nesting %rep */
119 Token **params; /* actual parameters */
120 Token *iline; /* invocation line */
H. Peter Anvin25a99342007-09-22 17:45:45 -0700121 unsigned int nparam, rotate;
122 int *paramlen;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700123 uint64_t unique;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000124 int lineno; /* Current line number on expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000125};
126
127/*
128 * The context stack is composed of a linked list of these.
129 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000130struct Context {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000131 Context *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000132 char *name;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700133 struct hash_table localmac;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000134 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000135};
136
137/*
138 * This is the internal form which we break input lines up into.
139 * Typically stored in linked lists.
140 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000141 * Note that `type' serves a double meaning: TOK_SMAC_PARAM is not
142 * necessarily used as-is, but is intended to denote the number of
143 * the substituted parameter. So in the definition
144 *
145 * %define a(x,y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700146 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000147 * the token representing `x' will have its type changed to
148 * TOK_SMAC_PARAM, but the one representing `y' will be
149 * TOK_SMAC_PARAM+1.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000150 *
151 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
152 * which doesn't need quotes around it. Used in the pre-include
153 * mechanism as an alternative to trying to find a sensible type of
154 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000155 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000156enum pp_token_type {
157 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
158 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700159 TOK_NUMBER, TOK_FLOAT, TOK_SMAC_END, TOK_OTHER,
160 TOK_INTERNAL_STRING,
161 TOK_PREPROC_Q, TOK_PREPROC_QQ,
H. Peter Anvind784a082009-04-20 14:01:18 -0700162 TOK_PASTE, /* %+ */
H. Peter Anvin9bb46df2009-04-07 21:59:24 -0700163 TOK_INDIRECT, /* %[...] */
H. Peter Anvin5b76fa22008-05-26 11:14:38 -0700164 TOK_SMAC_PARAM, /* MUST BE LAST IN THE LIST!!! */
165 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000166};
167
H. Peter Anvine2c80182005-01-15 22:15:51 +0000168struct Token {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000169 Token *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000170 char *text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -0700171 union {
172 SMacro *mac; /* associated macro for TOK_SMAC_END */
173 size_t len; /* scratch length field */
174 } a; /* Auxiliary data */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000175 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000176};
177
178/*
179 * Multi-line macro definitions are stored as a linked list of
180 * these, which is essentially a container to allow several linked
181 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700182 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000183 * Note that in this module, linked lists are treated as stacks
184 * wherever possible. For this reason, Lines are _pushed_ on to the
185 * `expansion' field in MMacro structures, so that the linked list,
186 * if walked, would give the macro lines in reverse order; this
187 * means that we can walk the list when expanding a macro, and thus
188 * push the lines on to the `expansion' field in _istk_ in reverse
189 * order (so that when popped back off they are in the right
190 * order). It may seem cockeyed, and it relies on my design having
191 * an even number of steps in, but it works...
192 *
193 * Some of these structures, rather than being actual lines, are
194 * markers delimiting the end of the expansion of a given macro.
H. Peter Anvin76690a12002-04-30 20:52:49 +0000195 * This is for use in the cycle-tracking and %rep-handling code.
196 * Such structures have `finishes' non-NULL, and `first' NULL. All
197 * others have `finishes' NULL, but `first' may still be NULL if
198 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000199 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000200struct Line {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000201 Line *next;
202 MMacro *finishes;
203 Token *first;
204};
205
206/*
207 * To handle an arbitrary level of file inclusion, we maintain a
208 * stack (ie linked list) of these things.
209 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000210struct Include {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000211 Include *next;
212 FILE *fp;
213 Cond *conds;
214 Line *expansion;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000215 char *fname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000216 int lineno, lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000217 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000218};
219
220/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000221 * Include search path. This is simply a list of strings which get
222 * prepended, in turn, to the name of an include file, in an
223 * attempt to find the file if it's not in the current directory.
224 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000225struct IncPath {
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000226 IncPath *next;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000227 char *path;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000228};
229
230/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000231 * Conditional assembly: we maintain a separate stack of these for
232 * each level of file inclusion. (The only reason we keep the
233 * stacks separate is to ensure that a stray `%endif' in a file
234 * included from within the true branch of a `%if' won't terminate
235 * it and cause confusion: instead, rightly, it'll cause an error.)
236 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000237struct Cond {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000238 Cond *next;
239 int state;
240};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000241enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000242 /*
243 * These states are for use just after %if or %elif: IF_TRUE
244 * means the condition has evaluated to truth so we are
245 * currently emitting, whereas IF_FALSE means we are not
246 * currently emitting but will start doing so if a %else comes
247 * up. In these states, all directives are admissible: %elif,
248 * %else and %endif. (And of course %if.)
249 */
250 COND_IF_TRUE, COND_IF_FALSE,
251 /*
252 * These states come up after a %else: ELSE_TRUE means we're
253 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
254 * any %elif or %else will cause an error.
255 */
256 COND_ELSE_TRUE, COND_ELSE_FALSE,
257 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200258 * These states mean that we're not emitting now, and also that
259 * nothing until %endif will be emitted at all. COND_DONE is
260 * used when we've had our moment of emission
261 * and have now started seeing %elifs. COND_NEVER is used when
262 * the condition construct in question is contained within a
263 * non-emitting branch of a larger condition construct,
264 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000265 */
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200266 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000267};
268#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
269
H. Peter Anvin70653092007-10-19 14:42:29 -0700270/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000271 * These defines are used as the possible return values for do_directive
272 */
273#define NO_DIRECTIVE_FOUND 0
274#define DIRECTIVE_FOUND 1
275
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000276/*
277 * Condition codes. Note that we use c_ prefix not C_ because C_ is
278 * used in nasm.h for the "real" condition codes. At _this_ level,
279 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
280 * ones, so we need a different enum...
281 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700282static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000283 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
284 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000285 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000286};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700287enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000288 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
289 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 -0700290 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
291 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000292};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700293static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000294 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
295 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 +0000296 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000297};
298
H. Peter Anvin76690a12002-04-30 20:52:49 +0000299/*
300 * Directive names.
301 */
H. Peter Anvin65747262002-05-07 00:10:05 +0000302/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
H. Peter Anvin9bf0aa72007-09-12 02:12:07 +0000303static int is_condition(enum preproc_token arg)
H. Peter Anvin65747262002-05-07 00:10:05 +0000304{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000305 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
H. Peter Anvin65747262002-05-07 00:10:05 +0000306}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000307
308/* For TASM compatibility we need to be able to recognise TASM compatible
309 * conditional compilation directives. Using the NASM pre-processor does
310 * not work, so we look for them specifically from the following list and
311 * then jam in the equivalent NASM directive into the input stream.
312 */
313
H. Peter Anvine2c80182005-01-15 22:15:51 +0000314enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000315 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
316 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
317};
318
H. Peter Anvin476d2862007-10-02 22:04:15 -0700319static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000320 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
321 "ifndef", "include", "local"
322};
323
324static int StackSize = 4;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000325static char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000326static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800327static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000328
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000329static Context *cstk;
330static Include *istk;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000331static IncPath *ipath = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000332
H. Peter Anvine2c80182005-01-15 22:15:51 +0000333static efunc _error; /* Pointer to client-provided error reporting function */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000334static evalfunc evaluate;
335
H. Peter Anvine2c80182005-01-15 22:15:51 +0000336static int pass; /* HACK: pass 0 = generate dependencies only */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700337static StrList **dephead, **deptail; /* Dependency list */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000338
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -0700339static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000340
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000341static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700342static bool do_predef;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000343
344static ListGen *list;
345
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000346/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000347 * The current set of multi-line macros we have defined.
348 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700349static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000350
351/*
352 * The current set of single-line macros we have defined.
353 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700354static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000355
356/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000357 * The multi-line macro we are currently defining, or the %rep
358 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000359 */
360static MMacro *defining;
361
Charles Crayned4200be2008-07-12 16:42:33 -0700362static uint64_t nested_mac_count;
363static uint64_t nested_rep_count;
364
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000365/*
366 * The number of macro parameters to allocate space for at a time.
367 */
368#define PARAM_DELTA 16
369
370/*
H. Peter Anvina4835d42008-05-20 14:21:29 -0700371 * The standard macro set: defined in macros.c in the array nasm_stdmac.
372 * This gives our position in the macro set, when we're processing it.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000373 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700374static macros_t *stdmacpos;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000375
376/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000377 * The extra standard macros that come from the object format, if
378 * any.
379 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700380static macros_t *extrastdmac = NULL;
H. Peter Anvincfb71762008-06-20 15:20:16 -0700381static bool any_extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000382
383/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000384 * Tokens are allocated in blocks to improve speed
385 */
386#define TOKEN_BLOCKSIZE 4096
387static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000388struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000389 Blocks *next;
390 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000391};
392
393static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000394
395/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000396 * Forward declarations.
397 */
H. Peter Anvin734b1882002-04-30 21:01:08 +0000398static Token *expand_mmac_params(Token * tline);
399static Token *expand_smacro(Token * tline);
400static Token *expand_id(Token * tline);
H. Peter Anvinf8ad5322009-02-21 17:55:08 -0800401static Context *get_ctx(const char *name, const char **namep,
402 bool all_contexts);
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700403static void make_tok_num(Token * tok, int64_t val);
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000404static void error(int severity, const char *fmt, ...);
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200405static void error_precond(int severity, const char *fmt, ...);
H. Peter Anvince616072002-04-30 21:02:23 +0000406static void *new_Block(size_t size);
407static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700408static Token *new_Token(Token * next, enum pp_token_type type,
409 const char *text, int txtlen);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000410static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000411
412/*
413 * Macros for safe checking of token pointers, avoid *(NULL)
414 */
415#define tok_type_(x,t) ((x) && (x)->type == (t))
416#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
417#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
418#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000419
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000420/* Handle TASM specific directives, which do not contain a % in
421 * front of them. We do it here because I could not find any other
422 * place to do it for the moment, and it is a hack (ideally it would
423 * be nice to be able to use the NASM pre-processor to do it).
424 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000425static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000426{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000427 int32_t i, j, k, m, len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000428 char *p = line, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000429
430 /* Skip whitespace */
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700431 while (nasm_isspace(*p) && *p != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000432 p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000433
434 /* Binary search for the directive name */
435 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +0000436 j = elements(tasm_directives);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000437 len = 0;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700438 while (!nasm_isspace(p[len]) && p[len] != 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000439 len++;
440 if (len) {
441 oldchar = p[len];
442 p[len] = 0;
443 while (j - i > 1) {
444 k = (j + i) / 2;
445 m = nasm_stricmp(p, tasm_directives[k]);
446 if (m == 0) {
447 /* We have found a directive, so jam a % in front of it
448 * so that NASM will then recognise it as one if it's own.
449 */
450 p[len] = oldchar;
451 len = strlen(p);
452 oldline = line;
453 line = nasm_malloc(len + 2);
454 line[0] = '%';
455 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700456 /*
457 * NASM does not recognise IFDIFI, so we convert
458 * it to %if 0. This is not used in NASM
459 * compatible code, but does need to parse for the
460 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000461 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700462 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000463 } else {
464 memcpy(line + 1, p, len + 1);
465 }
466 nasm_free(oldline);
467 return line;
468 } else if (m < 0) {
469 j = k;
470 } else
471 i = k;
472 }
473 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000474 }
475 return line;
476}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000477
H. Peter Anvin76690a12002-04-30 20:52:49 +0000478/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000479 * The pre-preprocessing stage... This function translates line
480 * number indications as they emerge from GNU cpp (`# lineno "file"
481 * flags') into NASM preprocessor line number indications (`%line
482 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000483 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000484static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000485{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000486 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000487 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000488
H. Peter Anvine2c80182005-01-15 22:15:51 +0000489 if (line[0] == '#' && line[1] == ' ') {
490 oldline = line;
491 fname = oldline + 2;
492 lineno = atoi(fname);
493 fname += strspn(fname, "0123456789 ");
494 if (*fname == '"')
495 fname++;
496 fnlen = strcspn(fname, "\"");
497 line = nasm_malloc(20 + fnlen);
498 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
499 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000500 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000501 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000502 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000503 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000504}
505
506/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000507 * Free a linked list of tokens.
508 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000509static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000510{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000511 while (list) {
512 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000513 }
514}
515
516/*
517 * Free a linked list of lines.
518 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000519static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000520{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000521 Line *l;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000522 while (list) {
523 l = list;
524 list = list->next;
525 free_tlist(l->first);
526 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000527 }
528}
529
530/*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000531 * Free an MMacro
532 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000533static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000534{
H. Peter Anvin734b1882002-04-30 21:01:08 +0000535 nasm_free(m->name);
536 free_tlist(m->dlist);
537 nasm_free(m->defaults);
538 free_llist(m->expansion);
539 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000540}
541
542/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700543 * Free all currently defined macros, and free the hash tables
544 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700545static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700546{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700547 SMacro *s;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700548 const char *key;
549 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700550
H. Peter Anvin072771e2008-05-22 13:17:51 -0700551 while ((s = hash_iterate(smt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700552 nasm_free((void *)key);
553 while (s) {
554 SMacro *ns = s->next;
555 nasm_free(s->name);
556 free_tlist(s->expansion);
557 nasm_free(s);
558 s = ns;
559 }
560 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700561 hash_free(smt);
562}
563
564static void free_mmacro_table(struct hash_table *mmt)
565{
566 MMacro *m;
567 const char *key;
568 struct hash_tbl_node *it = NULL;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700569
570 it = NULL;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700571 while ((m = hash_iterate(mmt, &it, &key)) != NULL) {
H. Peter Anvin97a23472007-09-16 17:57:25 -0700572 nasm_free((void *)key);
573 while (m) {
574 MMacro *nm = m->next;
575 free_mmacro(m);
576 m = nm;
577 }
578 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700579 hash_free(mmt);
580}
581
582static void free_macros(void)
583{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700584 free_smacro_table(&smacros);
585 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700586}
587
588/*
589 * Initialize the hash tables
590 */
591static void init_macros(void)
592{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700593 hash_init(&smacros, HASH_LARGE);
594 hash_init(&mmacros, HASH_LARGE);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700595}
596
597/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000598 * Pop the context stack.
599 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000600static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000601{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000602 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000603
604 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700605 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000606 nasm_free(c->name);
607 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000608}
609
H. Peter Anvin072771e2008-05-22 13:17:51 -0700610/*
611 * Search for a key in the hash index; adding it if necessary
612 * (in which case we initialize the data pointer to NULL.)
613 */
614static void **
615hash_findi_add(struct hash_table *hash, const char *str)
616{
617 struct hash_insert hi;
618 void **r;
619 char *strx;
620
621 r = hash_findi(hash, str, &hi);
622 if (r)
623 return r;
624
625 strx = nasm_strdup(str); /* Use a more efficient allocator here? */
626 return hash_add(&hi, strx, NULL);
627}
628
629/*
630 * Like hash_findi, but returns the data element rather than a pointer
631 * to it. Used only when not adding a new element, hence no third
632 * argument.
633 */
634static void *
635hash_findix(struct hash_table *hash, const char *str)
636{
637 void **p;
638
639 p = hash_findi(hash, str, NULL);
640 return p ? *p : NULL;
641}
642
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000643#define BUF_DELTA 512
644/*
645 * Read a line from the top file in istk, handling multiple CR/LFs
646 * at the end of the line read, and handling spurious ^Zs. Will
647 * return lines from the standard macro set if this has not already
648 * been done.
649 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000650static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000651{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000652 char *buffer, *p, *q;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000653 int bufsize, continued_count;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000654
H. Peter Anvine2c80182005-01-15 22:15:51 +0000655 if (stdmacpos) {
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700656 unsigned char c;
H. Peter Anvin7e50d232008-06-25 14:54:14 -0700657 const unsigned char *p = stdmacpos;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700658 char *ret, *q;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700659 size_t len = 0;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700660 while ((c = *p++)) {
661 if (c >= 0x80)
662 len += pp_directives_len[c-0x80]+1;
663 else
664 len++;
665 }
666 ret = nasm_malloc(len+1);
H. Peter Anvincda81632008-06-21 15:15:40 -0700667 q = ret;
668 while ((c = *stdmacpos++)) {
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700669 if (c >= 0x80) {
670 memcpy(q, pp_directives[c-0x80], pp_directives_len[c-0x80]);
671 q += pp_directives_len[c-0x80];
672 *q++ = ' ';
673 } else {
674 *q++ = c;
675 }
676 }
H. Peter Anvincda81632008-06-21 15:15:40 -0700677 stdmacpos = p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700678 *q = '\0';
679
H. Peter Anvind2456592008-06-19 15:04:18 -0700680 if (!*stdmacpos) {
681 /* This was the last of the standard macro chain... */
682 stdmacpos = NULL;
683 if (any_extrastdmac) {
684 stdmacpos = extrastdmac;
685 any_extrastdmac = false;
686 } else if (do_predef) {
687 Line *pd, *l;
688 Token *head, **tail, *t;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000689
H. Peter Anvind2456592008-06-19 15:04:18 -0700690 /*
691 * Nasty hack: here we push the contents of
692 * `predef' on to the top-level expansion stack,
693 * since this is the most convenient way to
694 * implement the pre-include and pre-define
695 * features.
696 */
697 for (pd = predef; pd; pd = pd->next) {
698 head = NULL;
699 tail = &head;
700 for (t = pd->first; t; t = t->next) {
701 *tail = new_Token(NULL, t->type, t->text, 0);
702 tail = &(*tail)->next;
703 }
704 l = nasm_malloc(sizeof(Line));
705 l->next = istk->expansion;
706 l->first = head;
H. Peter Anvin538002d2008-06-28 18:30:27 -0700707 l->finishes = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700708 istk->expansion = l;
709 }
710 do_predef = false;
711 }
712 }
713 return ret;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000714 }
715
716 bufsize = BUF_DELTA;
717 buffer = nasm_malloc(BUF_DELTA);
718 p = buffer;
H. Peter Anvin9f394642002-04-30 21:07:51 +0000719 continued_count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000720 while (1) {
721 q = fgets(p, bufsize - (p - buffer), istk->fp);
722 if (!q)
723 break;
724 p += strlen(p);
725 if (p > buffer && p[-1] == '\n') {
726 /* Convert backslash-CRLF line continuation sequences into
727 nothing at all (for DOS and Windows) */
728 if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) {
729 p -= 3;
730 *p = 0;
731 continued_count++;
732 }
733 /* Also convert backslash-LF line continuation sequences into
734 nothing at all (for Unix) */
735 else if (((p - 1) > buffer) && (p[-2] == '\\')) {
736 p -= 2;
737 *p = 0;
738 continued_count++;
739 } else {
740 break;
741 }
742 }
743 if (p - buffer > bufsize - 10) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000744 int32_t offset = p - buffer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000745 bufsize += BUF_DELTA;
746 buffer = nasm_realloc(buffer, bufsize);
747 p = buffer + offset; /* prevent stale-pointer problems */
748 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000749 }
750
H. Peter Anvine2c80182005-01-15 22:15:51 +0000751 if (!q && p == buffer) {
752 nasm_free(buffer);
753 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000754 }
755
H. Peter Anvine2c80182005-01-15 22:15:51 +0000756 src_set_linnum(src_get_linnum() + istk->lineinc +
757 (continued_count * istk->lineinc));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000758
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000759 /*
760 * Play safe: remove CRs as well as LFs, if any of either are
761 * present at the end of the line.
762 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000763 while (--p >= buffer && (*p == '\n' || *p == '\r'))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000764 *p = '\0';
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000765
766 /*
767 * Handle spurious ^Z, which may be inserted into source files
768 * by some file transfer utilities.
769 */
770 buffer[strcspn(buffer, "\032")] = '\0';
771
H. Peter Anvin734b1882002-04-30 21:01:08 +0000772 list->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000773
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000774 return buffer;
775}
776
777/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000778 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000779 * don't need to parse the value out of e.g. numeric tokens: we
780 * simply split one string into many.
781 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000782static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000783{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700784 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000785 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000786 Token *list = NULL;
787 Token *t, **tail = &list;
788
H. Peter Anvine2c80182005-01-15 22:15:51 +0000789 while (*line) {
790 p = line;
791 if (*p == '%') {
792 p++;
H. Peter Anvind784a082009-04-20 14:01:18 -0700793 if (*p == '+' && !nasm_isdigit(p[1])) {
794 p++;
795 type = TOK_PASTE;
796 } else if (nasm_isdigit(*p) ||
797 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000798 do {
799 p++;
800 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700801 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000802 type = TOK_PREPROC_ID;
803 } else if (*p == '{') {
804 p++;
805 while (*p && *p != '}') {
806 p[-1] = *p;
807 p++;
808 }
809 p[-1] = '\0';
810 if (*p)
811 p++;
812 type = TOK_PREPROC_ID;
H. Peter Anvin992fe752008-10-19 15:45:05 -0700813 } else if (*p == '[') {
814 int lvl = 1;
815 line += 2; /* Skip the leading %[ */
816 p++;
H. Peter Anvinec033012008-10-19 22:12:06 -0700817 while (lvl && (c = *p++)) {
H. Peter Anvinca544db2008-10-19 19:30:11 -0700818 switch (c) {
819 case ']':
820 lvl--;
821 break;
822 case '%':
H. Peter Anvinca544db2008-10-19 19:30:11 -0700823 if (*p == '[')
824 lvl++;
825 break;
826 case '\'':
827 case '\"':
828 case '`':
H. Peter Anvinec033012008-10-19 22:12:06 -0700829 p = nasm_skip_string(p)+1;
H. Peter Anvinca544db2008-10-19 19:30:11 -0700830 break;
831 default:
832 break;
H. Peter Anvin992fe752008-10-19 15:45:05 -0700833 }
H. Peter Anvin992fe752008-10-19 15:45:05 -0700834 }
H. Peter Anvinec033012008-10-19 22:12:06 -0700835 p--;
H. Peter Anvin992fe752008-10-19 15:45:05 -0700836 if (*p)
837 *p++ = '\0';
H. Peter Anvine1265812008-10-19 22:14:30 -0700838 if (lvl)
839 error(ERR_NONFATAL, "unterminated %[ construct");
H. Peter Anvin992fe752008-10-19 15:45:05 -0700840 type = TOK_INDIRECT;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700841 } else if (*p == '?') {
842 type = TOK_PREPROC_Q; /* %? */
843 p++;
844 if (*p == '?') {
845 type = TOK_PREPROC_QQ; /* %?? */
846 p++;
847 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000848 } else if (isidchar(*p) ||
849 ((*p == '!' || *p == '%' || *p == '$') &&
850 isidchar(p[1]))) {
851 do {
852 p++;
853 }
854 while (isidchar(*p));
855 type = TOK_PREPROC_ID;
856 } else {
857 type = TOK_OTHER;
858 if (*p == '%')
859 p++;
860 }
861 } else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) {
862 type = TOK_ID;
863 p++;
864 while (*p && isidchar(*p))
865 p++;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700866 } else if (*p == '\'' || *p == '"' || *p == '`') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000867 /*
868 * A string token.
869 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000870 type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -0700871 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000872
H. Peter Anvine2c80182005-01-15 22:15:51 +0000873 if (*p) {
874 p++;
875 } else {
H. Peter Anvin917a3492008-09-24 09:14:49 -0700876 error(ERR_WARNING|ERR_PASS1, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000877 /* Handling unterminated strings by UNV */
878 /* type = -1; */
879 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200880 } else if (p[0] == '$' && p[1] == '$') {
H. Peter Anvin8e1f8112009-04-17 14:20:44 -0700881 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +0200882 p += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000883 } else if (isnumstart(*p)) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700884 bool is_hex = false;
885 bool is_float = false;
886 bool has_e = false;
887 char c, *r;
888
H. Peter Anvine2c80182005-01-15 22:15:51 +0000889 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700890 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000891 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700892
893 if (*p == '$') {
894 p++;
895 is_hex = true;
896 }
897
898 for (;;) {
899 c = *p++;
900
901 if (!is_hex && (c == 'e' || c == 'E')) {
902 has_e = true;
903 if (*p == '+' || *p == '-') {
904 /* e can only be followed by +/- if it is either a
905 prefixed hex number or a floating-point number */
906 p++;
907 is_float = true;
908 }
909 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
910 is_hex = true;
911 } else if (c == 'P' || c == 'p') {
912 is_float = true;
913 if (*p == '+' || *p == '-')
914 p++;
915 } else if (isnumchar(c) || c == '_')
916 ; /* just advance */
917 else if (c == '.') {
918 /* we need to deal with consequences of the legacy
919 parser, like "1.nolist" being two tokens
920 (TOK_NUMBER, TOK_ID) here; at least give it
921 a shot for now. In the future, we probably need
922 a flex-based scanner with proper pattern matching
923 to do it as well as it can be done. Nothing in
924 the world is going to help the person who wants
925 0x123.p16 interpreted as two tokens, though. */
926 r = p;
927 while (*r == '_')
928 r++;
929
H. Peter Anvinf221b9e2008-06-21 11:03:51 -0700930 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700931 (!is_hex && (*r == 'e' || *r == 'E')) ||
932 (*r == 'p' || *r == 'P')) {
933 p = r;
934 is_float = true;
935 } else
936 break; /* Terminate the token */
937 } else
938 break;
939 }
940 p--; /* Point to first character beyond number */
941
H. Peter Anvin8e1f8112009-04-17 14:20:44 -0700942 if (p == line+1 && *line == '$') {
943 type = TOK_OTHER; /* TOKEN_HERE */
944 } else {
945 if (has_e && !is_hex) {
946 /* 1e13 is floating-point, but 1e13h is not */
947 is_float = true;
948 }
H. Peter Anvind784a082009-04-20 14:01:18 -0700949
H. Peter Anvin8e1f8112009-04-17 14:20:44 -0700950 type = is_float ? TOK_FLOAT : TOK_NUMBER;
H. Peter Anvinc2df2822007-10-24 15:29:28 -0700951 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700952 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953 type = TOK_WHITESPACE;
954 p++;
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700955 while (*p && nasm_isspace(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000956 p++;
957 /*
958 * Whitespace just before end-of-line is discarded by
959 * pretending it's a comment; whitespace just before a
960 * comment gets lumped into the comment.
961 */
962 if (!*p || *p == ';') {
963 type = TOK_COMMENT;
964 while (*p)
965 p++;
966 }
967 } else if (*p == ';') {
968 type = TOK_COMMENT;
969 while (*p)
970 p++;
971 } else {
972 /*
973 * Anything else is an operator of some kind. We check
974 * for all the double-character operators (>>, <<, //,
975 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000976 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000977 */
978 type = TOK_OTHER;
979 if ((p[0] == '>' && p[1] == '>') ||
980 (p[0] == '<' && p[1] == '<') ||
981 (p[0] == '/' && p[1] == '/') ||
982 (p[0] == '<' && p[1] == '=') ||
983 (p[0] == '>' && p[1] == '=') ||
984 (p[0] == '=' && p[1] == '=') ||
985 (p[0] == '!' && p[1] == '=') ||
986 (p[0] == '<' && p[1] == '>') ||
987 (p[0] == '&' && p[1] == '&') ||
988 (p[0] == '|' && p[1] == '|') ||
989 (p[0] == '^' && p[1] == '^')) {
990 p++;
991 }
992 p++;
993 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +0000994
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995 /* Handling unterminated string by UNV */
996 /*if (type == -1)
997 {
998 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
999 t->text[p-line] = *line;
1000 tail = &t->next;
1001 }
1002 else */
1003 if (type != TOK_COMMENT) {
1004 *tail = t = new_Token(NULL, type, line, p - line);
1005 tail = &t->next;
1006 }
1007 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001008 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001009 return list;
1010}
1011
H. Peter Anvince616072002-04-30 21:02:23 +00001012/*
1013 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001014 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001015 * deleted only all at once by the delete_Blocks function.
1016 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001018{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001019 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001020
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001021 /* first, get to the end of the linked list */
1022 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001024 /* now allocate the requested chunk */
1025 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001026
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001027 /* now allocate a new block for the next request */
1028 b->next = nasm_malloc(sizeof(Blocks));
1029 /* and initialize the contents of the new block */
1030 b->next->next = NULL;
1031 b->next->chunk = NULL;
1032 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001033}
1034
1035/*
1036 * this function deletes all managed blocks of memory
1037 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001038static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001039{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001040 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001041
H. Peter Anvin70653092007-10-19 14:42:29 -07001042 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001043 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001044 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001045 * free it.
1046 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001047 while (b) {
1048 if (b->chunk)
1049 nasm_free(b->chunk);
1050 a = b;
1051 b = b->next;
1052 if (a != &blocks)
1053 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001054 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001055}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001056
1057/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001058 * this function creates a new Token and passes a pointer to it
H. Peter Anvin734b1882002-04-30 21:01:08 +00001059 * back to the caller. It sets the type and text elements, and
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001060 * also the a.mac and next elements to NULL.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001061 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001062static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvinc751e862008-06-09 10:18:45 -07001063 const char *text, int txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001064{
1065 Token *t;
1066 int i;
1067
H. Peter Anvine2c80182005-01-15 22:15:51 +00001068 if (freeTokens == NULL) {
1069 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1070 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1071 freeTokens[i].next = &freeTokens[i + 1];
1072 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001073 }
1074 t = freeTokens;
1075 freeTokens = t->next;
1076 t->next = next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001077 t->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001078 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001079 if (type == TOK_WHITESPACE || text == NULL) {
1080 t->text = NULL;
1081 } else {
1082 if (txtlen == 0)
1083 txtlen = strlen(text);
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001084 t->text = nasm_malloc(txtlen+1);
1085 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001087 }
1088 return t;
1089}
1090
H. Peter Anvine2c80182005-01-15 22:15:51 +00001091static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001092{
1093 Token *next = t->next;
1094 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001095 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001096 freeTokens = t;
1097 return next;
1098}
1099
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001100/*
1101 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001102 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1103 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001104 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001105static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001106{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001107 Token *t;
1108 int len;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001109 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001110 const char *q;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001111
1112 len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001113 for (t = tlist; t; t = t->next) {
1114 if (t->type == TOK_PREPROC_ID && t->text[1] == '!') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001115 char *p = getenv(t->text + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001116 nasm_free(t->text);
1117 if (p)
1118 t->text = nasm_strdup(p);
1119 else
1120 t->text = NULL;
1121 }
1122 /* Expand local macros here and not during preprocessing */
1123 if (expand_locals &&
1124 t->type == TOK_PREPROC_ID && t->text &&
1125 t->text[0] == '%' && t->text[1] == '$') {
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001126 const char *q;
1127 char *p;
1128 Context *ctx = get_ctx(t->text, &q, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001130 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001131 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 p = nasm_strcat(buffer, q);
1133 nasm_free(t->text);
1134 t->text = p;
1135 }
1136 }
1137 if (t->type == TOK_WHITESPACE) {
1138 len++;
1139 } else if (t->text) {
1140 len += strlen(t->text);
1141 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001142 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001143 p = line = nasm_malloc(len + 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144 for (t = tlist; t; t = t->next) {
1145 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001146 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 } else if (t->text) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001148 q = t->text;
1149 while (*q)
1150 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001152 }
1153 *p = '\0';
1154 return line;
1155}
1156
1157/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001158 * A scanner, suitable for use by the expression evaluator, which
1159 * operates on a line of Tokens. Expects a pointer to a pointer to
1160 * the first token in the line to be passed in as its private_data
1161 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001162 *
1163 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001164 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001165static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001166{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001167 Token **tlineptr = private_data;
1168 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001169 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001170
H. Peter Anvine2c80182005-01-15 22:15:51 +00001171 do {
1172 tline = *tlineptr;
1173 *tlineptr = tline ? tline->next : NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001174 }
1175 while (tline && (tline->type == TOK_WHITESPACE ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001176 tline->type == TOK_COMMENT));
H. Peter Anvin76690a12002-04-30 20:52:49 +00001177
1178 if (!tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001179 return tokval->t_type = TOKEN_EOS;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001180
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001181 tokval->t_charptr = tline->text;
1182
H. Peter Anvin76690a12002-04-30 20:52:49 +00001183 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001184 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001185 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001187
H. Peter Anvine2c80182005-01-15 22:15:51 +00001188 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001189 p = tokval->t_charptr = tline->text;
1190 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001191 tokval->t_charptr++;
1192 return tokval->t_type = TOKEN_ID;
1193 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001194
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001195 for (r = p, s = ourcopy; *r; r++) {
Philipp Thomas76ec8e72008-05-21 08:53:21 -07001196 if (r >= p+MAX_KEYWORD)
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001197 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001198 *s++ = nasm_tolower(*r);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001199 }
1200 *s = '\0';
1201 /* right, so we have an identifier sitting in temp storage. now,
1202 * is it actually a register or instruction name, or what? */
1203 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001204 }
1205
H. Peter Anvine2c80182005-01-15 22:15:51 +00001206 if (tline->type == TOK_NUMBER) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001207 bool rn_error;
1208 tokval->t_integer = readnum(tline->text, &rn_error);
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001209 tokval->t_charptr = tline->text;
H. Peter Anvin11627042008-06-09 20:45:19 -07001210 if (rn_error)
1211 return tokval->t_type = TOKEN_ERRNUM;
1212 else
1213 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001214 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001215
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001216 if (tline->type == TOK_FLOAT) {
1217 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001218 }
1219
H. Peter Anvine2c80182005-01-15 22:15:51 +00001220 if (tline->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001221 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001222
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001223 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001224 tokval->t_charptr = tline->text;
1225 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001226
H. Peter Anvin11627042008-06-09 20:45:19 -07001227 if (ep[0] != bq || ep[1] != '\0')
1228 return tokval->t_type = TOKEN_ERRSTR;
1229 else
1230 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001231 }
1232
H. Peter Anvine2c80182005-01-15 22:15:51 +00001233 if (tline->type == TOK_OTHER) {
1234 if (!strcmp(tline->text, "<<"))
1235 return tokval->t_type = TOKEN_SHL;
1236 if (!strcmp(tline->text, ">>"))
1237 return tokval->t_type = TOKEN_SHR;
1238 if (!strcmp(tline->text, "//"))
1239 return tokval->t_type = TOKEN_SDIV;
1240 if (!strcmp(tline->text, "%%"))
1241 return tokval->t_type = TOKEN_SMOD;
1242 if (!strcmp(tline->text, "=="))
1243 return tokval->t_type = TOKEN_EQ;
1244 if (!strcmp(tline->text, "<>"))
1245 return tokval->t_type = TOKEN_NE;
1246 if (!strcmp(tline->text, "!="))
1247 return tokval->t_type = TOKEN_NE;
1248 if (!strcmp(tline->text, "<="))
1249 return tokval->t_type = TOKEN_LE;
1250 if (!strcmp(tline->text, ">="))
1251 return tokval->t_type = TOKEN_GE;
1252 if (!strcmp(tline->text, "&&"))
1253 return tokval->t_type = TOKEN_DBL_AND;
1254 if (!strcmp(tline->text, "^^"))
1255 return tokval->t_type = TOKEN_DBL_XOR;
1256 if (!strcmp(tline->text, "||"))
1257 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001258 }
1259
1260 /*
1261 * We have no other options: just return the first character of
1262 * the token text.
1263 */
1264 return tokval->t_type = tline->text[0];
1265}
1266
1267/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001268 * Compare a string to the name of an existing macro; this is a
1269 * simple wrapper which calls either strcmp or nasm_stricmp
1270 * depending on the value of the `casesense' parameter.
1271 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001272static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001273{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001274 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001275}
1276
1277/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001278 * Compare a string to the name of an existing macro; this is a
1279 * simple wrapper which calls either strcmp or nasm_stricmp
1280 * depending on the value of the `casesense' parameter.
1281 */
1282static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1283{
1284 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1285}
1286
1287/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001288 * Return the Context structure associated with a %$ token. Return
1289 * NULL, having _already_ reported an error condition, if the
1290 * context stack isn't deep enough for the supplied number of $
1291 * signs.
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001292 * If all_contexts == true, contexts that enclose current are
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001293 * also scanned for such smacro, until it is found; if not -
1294 * only the context that directly results from the number of $'s
1295 * in variable's name.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001296 *
1297 * If "namep" is non-NULL, set it to the pointer to the macro name
1298 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001299 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001300static Context *get_ctx(const char *name, const char **namep,
1301 bool all_contexts)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001302{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001303 Context *ctx;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001304 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001305 int i;
1306
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001307 if (namep)
1308 *namep = name;
1309
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001310 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001312
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 if (!cstk) {
1314 error(ERR_NONFATAL, "`%s': context stack is empty", name);
1315 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001316 }
1317
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001318 name += 2;
1319 ctx = cstk;
1320 i = 0;
1321 while (ctx && *name == '$') {
1322 name++;
1323 i++;
1324 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001325 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001326 if (!ctx) {
1327 error(ERR_NONFATAL, "`%s': context stack is only"
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001328 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001329 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001330 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001331
1332 if (namep)
1333 *namep = name;
1334
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001335 if (!all_contexts)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001337
H. Peter Anvine2c80182005-01-15 22:15:51 +00001338 do {
1339 /* Search for this smacro in found context */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001340 m = hash_findix(&ctx->localmac, name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 while (m) {
1342 if (!mstrcmp(m->name, name, m->casesense))
1343 return ctx;
1344 m = m->next;
1345 }
1346 ctx = ctx->next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001347 }
1348 while (ctx);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001349 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001350}
1351
1352/*
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001353 * Check to see if a file is already in a string list
1354 */
1355static bool in_list(const StrList *list, const char *str)
1356{
1357 while (list) {
1358 if (!strcmp(list->str, str))
1359 return true;
1360 list = list->next;
1361 }
1362 return false;
1363}
1364
1365/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001366 * Open an include file. This routine must always return a valid
1367 * file pointer if it returns - it's responsible for throwing an
1368 * ERR_FATAL and bombing out completely if not. It should also try
1369 * the include path one by one until it finds the file or reaches
1370 * the end of the path.
1371 */
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001372static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail,
H. Peter Anvin418ca702008-05-30 10:42:30 -07001373 bool missing_ok)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001374{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001375 FILE *fp;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001376 char *prefix = "";
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001377 IncPath *ip = ipath;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001378 int len = strlen(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001379 size_t prefix_len = 0;
1380 StrList *sl;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001381
H. Peter Anvine2c80182005-01-15 22:15:51 +00001382 while (1) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001383 sl = nasm_malloc(prefix_len+len+1+sizeof sl->next);
1384 memcpy(sl->str, prefix, prefix_len);
1385 memcpy(sl->str+prefix_len, file, len+1);
1386 fp = fopen(sl->str, "r");
H. Peter Anvin418ca702008-05-30 10:42:30 -07001387 if (fp && dhead && !in_list(*dhead, sl->str)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001388 sl->next = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001389 **dtail = sl;
1390 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001391 } else {
1392 nasm_free(sl);
1393 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001394 if (fp)
1395 return fp;
H. Peter Anvin418ca702008-05-30 10:42:30 -07001396 if (!ip) {
1397 if (!missing_ok)
1398 break;
1399 prefix = NULL;
1400 } else {
1401 prefix = ip->path;
1402 ip = ip->next;
1403 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001404 if (prefix) {
1405 prefix_len = strlen(prefix);
1406 } else {
1407 /* -MG given and file not found */
H. Peter Anvin418ca702008-05-30 10:42:30 -07001408 if (dhead && !in_list(*dhead, file)) {
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001409 sl = nasm_malloc(len+1+sizeof sl->next);
1410 sl->next = NULL;
1411 strcpy(sl->str, file);
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07001412 **dtail = sl;
1413 *dtail = &sl->next;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001414 }
H. Peter Anvin37a321f2007-09-24 13:41:58 -07001415 return NULL;
1416 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001417 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001418
H. Peter Anvin734b1882002-04-30 21:01:08 +00001419 error(ERR_FATAL, "unable to open include file `%s'", file);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001420 return NULL; /* never reached - placate compilers */
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001421}
1422
1423/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001424 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001425 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001426 * return true if _any_ single-line macro of that name is defined.
1427 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001428 * `nparam' or no parameters is defined.
1429 *
1430 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001431 * defined, or nparam is -1, the address of the definition structure
1432 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001433 * is NULL, no action will be taken regarding its contents, and no
1434 * error will occur.
1435 *
1436 * Note that this is also called with nparam zero to resolve
1437 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001438 *
1439 * If you already know which context macro belongs to, you can pass
1440 * the context pointer as first parameter; if you won't but name begins
1441 * with %$ the context will be automatically computed. If all_contexts
1442 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001443 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001444static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001445smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001446 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001447{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001448 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001449 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001450
H. Peter Anvin97a23472007-09-16 17:57:25 -07001451 if (ctx) {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001452 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001453 } else if (name[0] == '%' && name[1] == '$') {
1454 if (cstk)
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001455 ctx = get_ctx(name, &name, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001456 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001457 return false; /* got to return _something_ */
H. Peter Anvin166c2472008-05-28 12:28:58 -07001458 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001459 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001460 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001461 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001462 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001463
H. Peter Anvine2c80182005-01-15 22:15:51 +00001464 while (m) {
1465 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001466 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001467 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001468 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001469 *defn = m;
1470 else
1471 *defn = NULL;
1472 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001473 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001474 }
1475 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001476 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001477
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001478 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001479}
1480
1481/*
1482 * Count and mark off the parameters in a multi-line macro call.
1483 * This is called both from within the multi-line macro expansion
1484 * code, and also to mark off the default parameters when provided
1485 * in a %macro definition line.
1486 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001487static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001488{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001489 int paramsize, brace;
1490
1491 *nparam = paramsize = 0;
1492 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001493 while (t) {
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001494 /* +1: we need space for the final NULL */
1495 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001496 paramsize += PARAM_DELTA;
1497 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1498 }
1499 skip_white_(t);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001500 brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001501 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001502 brace = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001503 (*params)[(*nparam)++] = t;
1504 while (tok_isnt_(t, brace ? "}" : ","))
1505 t = t->next;
1506 if (t) { /* got a comma/brace */
1507 t = t->next;
1508 if (brace) {
1509 /*
1510 * Now we've found the closing brace, look further
1511 * for the comma.
1512 */
1513 skip_white_(t);
1514 if (tok_isnt_(t, ",")) {
1515 error(ERR_NONFATAL,
1516 "braces do not enclose all of macro parameter");
1517 while (tok_isnt_(t, ","))
1518 t = t->next;
1519 }
1520 if (t)
1521 t = t->next; /* eat the comma */
1522 }
1523 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001524 }
1525}
1526
1527/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001528 * Determine whether one of the various `if' conditions is true or
1529 * not.
1530 *
1531 * We must free the tline we get passed.
1532 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001533static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001534{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001535 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001536 bool j;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001537 Token *t, *tt, **tptr, *origline;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001538 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001539 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001540 enum pp_token_type needtype;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001541
1542 origline = tline;
1543
H. Peter Anvine2c80182005-01-15 22:15:51 +00001544 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001545 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001546 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001547 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001548 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001549 if (!tline)
1550 break;
1551 if (tline->type != TOK_ID) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001552 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001553 "`%s' expects context identifiers", pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001554 free_tlist(origline);
1555 return -1;
1556 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001557 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001558 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001559 tline = tline->next;
1560 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001561 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001562
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001563 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001564 j = false; /* have we matched yet? */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001565 while (tline) {
1566 skip_white_(tline);
1567 if (!tline || (tline->type != TOK_ID &&
1568 (tline->type != TOK_PREPROC_ID ||
1569 tline->text[1] != '$'))) {
1570 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001571 "`%s' expects macro identifiers", pp_directives[ct]);
1572 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001573 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001574 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001575 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001576 tline = tline->next;
1577 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001578 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001579
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001580 case PPC_IFIDN:
1581 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001582 tline = expand_smacro(tline);
1583 t = tt = tline;
1584 while (tok_isnt_(tt, ","))
1585 tt = tt->next;
1586 if (!tt) {
1587 error(ERR_NONFATAL,
1588 "`%s' expects two comma-separated arguments",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001589 pp_directives[ct]);
1590 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001591 }
1592 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001593 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001594 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1595 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
1596 error(ERR_NONFATAL, "`%s': more than one comma on line",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001597 pp_directives[ct]);
1598 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001599 }
1600 if (t->type == TOK_WHITESPACE) {
1601 t = t->next;
1602 continue;
1603 }
1604 if (tt->type == TOK_WHITESPACE) {
1605 tt = tt->next;
1606 continue;
1607 }
1608 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001609 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001610 break;
1611 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001612 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001613 if (t->type == TOK_STRING) {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07001614 size_t l1 = nasm_unquote(t->text, NULL);
1615 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001616
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001617 if (l1 != l2) {
1618 j = false;
1619 break;
1620 }
1621 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1622 j = false;
1623 break;
1624 }
1625 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001626 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001627 break;
1628 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001629
H. Peter Anvine2c80182005-01-15 22:15:51 +00001630 t = t->next;
1631 tt = tt->next;
1632 }
1633 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001634 j = false; /* trailing gunk on one end or other */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001635 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001636
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001637 case PPC_IFMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001638 {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001639 bool found = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001640 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001641
H. Peter Anvine2c80182005-01-15 22:15:51 +00001642 skip_white_(tline);
1643 tline = expand_id(tline);
1644 if (!tok_type_(tline, TOK_ID)) {
1645 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001646 "`%s' expects a macro name", pp_directives[ct]);
1647 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001648 }
1649 searching.name = nasm_strdup(tline->text);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001650 searching.casesense = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001651 searching.plus = false;
1652 searching.nolist = false;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001653 searching.in_progress = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001654 searching.rep_nest = NULL;
1655 searching.nparam_min = 0;
1656 searching.nparam_max = INT_MAX;
1657 tline = expand_smacro(tline->next);
1658 skip_white_(tline);
1659 if (!tline) {
1660 } else if (!tok_type_(tline, TOK_NUMBER)) {
1661 error(ERR_NONFATAL,
1662 "`%s' expects a parameter count or nothing",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001663 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001664 } else {
1665 searching.nparam_min = searching.nparam_max =
1666 readnum(tline->text, &j);
1667 if (j)
1668 error(ERR_NONFATAL,
1669 "unable to parse parameter count `%s'",
1670 tline->text);
1671 }
1672 if (tline && tok_is_(tline->next, "-")) {
1673 tline = tline->next->next;
1674 if (tok_is_(tline, "*"))
1675 searching.nparam_max = INT_MAX;
1676 else if (!tok_type_(tline, TOK_NUMBER))
1677 error(ERR_NONFATAL,
1678 "`%s' expects a parameter count after `-'",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001679 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001680 else {
1681 searching.nparam_max = readnum(tline->text, &j);
1682 if (j)
1683 error(ERR_NONFATAL,
1684 "unable to parse parameter count `%s'",
1685 tline->text);
1686 if (searching.nparam_min > searching.nparam_max)
1687 error(ERR_NONFATAL,
1688 "minimum parameter count exceeds maximum");
1689 }
1690 }
1691 if (tline && tok_is_(tline->next, "+")) {
1692 tline = tline->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001693 searching.plus = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001694 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001695 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001696 while (mmac) {
1697 if (!strcmp(mmac->name, searching.name) &&
1698 (mmac->nparam_min <= searching.nparam_max
1699 || searching.plus)
1700 && (searching.nparam_min <= mmac->nparam_max
1701 || mmac->plus)) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001702 found = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001703 break;
1704 }
1705 mmac = mmac->next;
1706 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001707 if(tline && tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001708 error(ERR_WARNING|ERR_PASS1,
1709 "trailing garbage after %%ifmacro ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001710 nasm_free(searching.name);
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001711 j = found;
1712 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001713 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001714
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001715 case PPC_IFID:
1716 needtype = TOK_ID;
1717 goto iftype;
1718 case PPC_IFNUM:
1719 needtype = TOK_NUMBER;
1720 goto iftype;
1721 case PPC_IFSTR:
1722 needtype = TOK_STRING;
1723 goto iftype;
1724
1725 iftype:
H. Peter Anvin134b9462008-02-16 17:01:40 -08001726 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001727
H. Peter Anvin927c92b2008-02-16 13:44:52 -08001728 while (tok_type_(t, TOK_WHITESPACE) ||
1729 (needtype == TOK_NUMBER &&
1730 tok_type_(t, TOK_OTHER) &&
1731 (t->text[0] == '-' || t->text[0] == '+') &&
1732 !t->text[1]))
1733 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001734
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001735 j = tok_type_(t, needtype);
1736 break;
1737
1738 case PPC_IFTOKEN:
1739 t = tline = expand_smacro(tline);
1740 while (tok_type_(t, TOK_WHITESPACE))
1741 t = t->next;
1742
1743 j = false;
1744 if (t) {
1745 t = t->next; /* Skip the actual token */
1746 while (tok_type_(t, TOK_WHITESPACE))
1747 t = t->next;
1748 j = !t; /* Should be nothing left */
1749 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001750 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001751
H. Peter Anvin134b9462008-02-16 17:01:40 -08001752 case PPC_IFEMPTY:
1753 t = tline = expand_smacro(tline);
1754 while (tok_type_(t, TOK_WHITESPACE))
1755 t = t->next;
1756
1757 j = !t; /* Should be empty */
1758 break;
1759
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001760 case PPC_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001761 t = tline = expand_smacro(tline);
1762 tptr = &t;
1763 tokval.t_type = TOKEN_INVALID;
1764 evalresult = evaluate(ppscan, tptr, &tokval,
1765 NULL, pass | CRITICAL, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001766 if (!evalresult)
1767 return -1;
1768 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001769 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001770 "trailing garbage after expression ignored");
1771 if (!is_simple(evalresult)) {
1772 error(ERR_NONFATAL,
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001773 "non-constant value given to `%s'", pp_directives[ct]);
1774 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001775 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00001776 j = reloc_value(evalresult) != 0;
H. Peter Anvin90e6e812008-07-16 14:38:24 -07001777 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00001778
H. Peter Anvine2c80182005-01-15 22:15:51 +00001779 default:
1780 error(ERR_FATAL,
1781 "preprocessor directive `%s' not yet implemented",
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001782 pp_directives[ct]);
1783 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001784 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001785
1786 free_tlist(origline);
1787 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07001788
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001789fail:
1790 free_tlist(origline);
1791 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001792}
1793
1794/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001795 * Common code for defining an smacro
1796 */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001797static bool define_smacro(Context *ctx, const char *mname, bool casesense,
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001798 int nparam, Token *expansion)
1799{
1800 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001801 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07001802
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001803 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
1804 if (!smac) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07001805 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001806 "single-line macro `%s' defined both with and"
1807 " without parameters", mname);
1808
1809 /* Some instances of the old code considered this a failure,
1810 some others didn't. What is the right thing to do here? */
1811 free_tlist(expansion);
1812 return false; /* Failure */
1813 } else {
1814 /*
1815 * We're redefining, so we have to take over an
1816 * existing SMacro structure. This means freeing
1817 * what was already in it.
1818 */
1819 nasm_free(smac->name);
1820 free_tlist(smac->expansion);
1821 }
1822 } else {
H. Peter Anvin166c2472008-05-28 12:28:58 -07001823 smtbl = ctx ? &ctx->localmac : &smacros;
1824 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001825 smac = nasm_malloc(sizeof(SMacro));
1826 smac->next = *smhead;
1827 *smhead = smac;
1828 }
1829 smac->name = nasm_strdup(mname);
1830 smac->casesense = casesense;
1831 smac->nparam = nparam;
1832 smac->expansion = expansion;
1833 smac->in_progress = false;
1834 return true; /* Success */
1835}
1836
1837/*
1838 * Undefine an smacro
1839 */
1840static void undef_smacro(Context *ctx, const char *mname)
1841{
1842 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001843 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001844
H. Peter Anvin166c2472008-05-28 12:28:58 -07001845 smtbl = ctx ? &ctx->localmac : &smacros;
1846 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07001847
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001848 if (smhead) {
1849 /*
1850 * We now have a macro name... go hunt for it.
1851 */
1852 sp = smhead;
1853 while ((s = *sp) != NULL) {
1854 if (!mstrcmp(s->name, mname, s->casesense)) {
1855 *sp = s->next;
1856 nasm_free(s->name);
1857 free_tlist(s->expansion);
1858 nasm_free(s);
1859 } else {
1860 sp = &s->next;
1861 }
1862 }
1863 }
1864}
1865
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001866/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07001867 * Parse a mmacro specification.
1868 */
1869static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
1870{
1871 bool err;
1872
1873 tline = tline->next;
1874 skip_white_(tline);
1875 tline = expand_id(tline);
1876 if (!tok_type_(tline, TOK_ID)) {
1877 error(ERR_NONFATAL, "`%s' expects a macro name", directive);
1878 return false;
1879 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001880
H. Peter Anvina26433d2008-07-16 14:40:01 -07001881 def->name = nasm_strdup(tline->text);
1882 def->plus = false;
1883 def->nolist = false;
1884 def->in_progress = 0;
1885 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001886 def->nparam_min = 0;
1887 def->nparam_max = 0;
1888
H. Peter Anvina26433d2008-07-16 14:40:01 -07001889 tline = expand_smacro(tline->next);
1890 skip_white_(tline);
1891 if (!tok_type_(tline, TOK_NUMBER)) {
1892 error(ERR_NONFATAL, "`%s' expects a parameter count", directive);
H. Peter Anvina26433d2008-07-16 14:40:01 -07001893 } else {
1894 def->nparam_min = def->nparam_max =
1895 readnum(tline->text, &err);
1896 if (err)
1897 error(ERR_NONFATAL,
1898 "unable to parse parameter count `%s'", tline->text);
1899 }
1900 if (tline && tok_is_(tline->next, "-")) {
1901 tline = tline->next->next;
1902 if (tok_is_(tline, "*")) {
1903 def->nparam_max = INT_MAX;
1904 } else if (!tok_type_(tline, TOK_NUMBER)) {
1905 error(ERR_NONFATAL,
1906 "`%s' expects a parameter count after `-'", directive);
1907 } else {
1908 def->nparam_max = readnum(tline->text, &err);
1909 if (err) {
1910 error(ERR_NONFATAL, "unable to parse parameter count `%s'",
1911 tline->text);
1912 }
1913 if (def->nparam_min > def->nparam_max) {
1914 error(ERR_NONFATAL, "minimum parameter count exceeds maximum");
1915 }
1916 }
1917 }
1918 if (tline && tok_is_(tline->next, "+")) {
1919 tline = tline->next;
1920 def->plus = true;
1921 }
1922 if (tline && tok_type_(tline->next, TOK_ID) &&
1923 !nasm_stricmp(tline->next->text, ".nolist")) {
1924 tline = tline->next;
1925 def->nolist = true;
1926 }
1927
1928 /*
1929 * Handle default parameters.
1930 */
1931 if (tline && tline->next) {
1932 def->dlist = tline->next;
1933 tline->next = NULL;
1934 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
1935 } else {
1936 def->dlist = NULL;
1937 def->defaults = NULL;
1938 }
1939 def->expansion = NULL;
1940
Victor van den Elzen22343c22008-08-06 14:47:54 +02001941 if(def->defaults &&
1942 def->ndefs > def->nparam_max - def->nparam_min &&
1943 !def->plus)
H. Peter Anvin917a3492008-09-24 09:14:49 -07001944 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MDP,
1945 "too many default macro parameters");
Victor van den Elzenb916ede2008-07-23 15:14:22 +02001946
H. Peter Anvina26433d2008-07-16 14:40:01 -07001947 return true;
1948}
1949
1950
1951/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001952 * Decode a size directive
1953 */
1954static int parse_size(const char *str) {
1955 static const char *size_names[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001956 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001957 static const int sizes[] =
H. Peter Anvindfb91802008-05-20 11:43:53 -07001958 { 0, 1, 4, 16, 8, 10, 2, 32 };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08001959
1960 return sizes[bsii(str, size_names, elements(size_names))+1];
1961}
1962
Ed Beroset3ab3f412002-06-11 03:31:49 +00001963/**
1964 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001965 * Find out if a line contains a preprocessor directive, and deal
1966 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07001967 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001968 * If a directive _is_ found, it is the responsibility of this routine
1969 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001970 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00001971 * @param tline a pointer to the current tokeninzed line linked list
1972 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07001973 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001974 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001975static int do_directive(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001976{
H. Peter Anvin4169a472007-09-12 01:29:43 +00001977 enum preproc_token i;
1978 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07001979 bool err;
1980 int nparam;
1981 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001982 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07001983 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001984 int offset;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001985 char *p, *pp;
1986 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001987 Include *inc;
1988 Context *ctx;
1989 Cond *cond;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001990 MMacro *mmac, **mmhead;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001991 Token *t, *tt, *param_start, *macro_start, *last, **tptr, *origline;
1992 Line *l;
1993 struct tokenval tokval;
1994 expr *evalresult;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001995 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07001996 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07001997 size_t len;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07001998 int severity;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001999
2000 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002001
H. Peter Anvineba20a72002-04-30 20:53:55 +00002002 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002003 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002004 (tline->text[1] == '%' || tline->text[1] == '$'
2005 || tline->text[1] == '!'))
2006 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002007
H. Peter Anvin4169a472007-09-12 01:29:43 +00002008 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002009
2010 /*
2011 * If we're in a non-emitting branch of a condition construct,
H. Peter Anvin76690a12002-04-30 20:52:49 +00002012 * or walking to the end of an already terminated %rep block,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002013 * we should ignore all directives except for condition
2014 * directives.
2015 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002016 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00002017 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2018 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002019 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002020
2021 /*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002022 * If we're defining a macro or reading a %rep block, we should
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002023 * ignore all directives except for %macro/%imacro (which nest),
2024 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2025 * If we're in a %rep block, another %rep nests, so should be let through.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002026 */
2027 if (defining && i != PP_MACRO && i != PP_IMACRO &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00002028 i != PP_ENDMACRO && i != PP_ENDM &&
2029 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2030 return NO_DIRECTIVE_FOUND;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002031 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002032
Charles Crayned4200be2008-07-12 16:42:33 -07002033 if (defining) {
2034 if (i == PP_MACRO || i == PP_IMACRO) {
2035 nested_mac_count++;
2036 return NO_DIRECTIVE_FOUND;
2037 } else if (nested_mac_count > 0) {
2038 if (i == PP_ENDMACRO) {
2039 nested_mac_count--;
2040 return NO_DIRECTIVE_FOUND;
2041 }
2042 }
2043 if (!defining->name) {
2044 if (i == PP_REP) {
2045 nested_rep_count++;
2046 return NO_DIRECTIVE_FOUND;
2047 } else if (nested_rep_count > 0) {
2048 if (i == PP_ENDREP) {
2049 nested_rep_count--;
2050 return NO_DIRECTIVE_FOUND;
2051 }
2052 }
2053 }
2054 }
2055
H. Peter Anvin4169a472007-09-12 01:29:43 +00002056 switch (i) {
2057 case PP_INVALID:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002058 error(ERR_NONFATAL, "unknown preprocessor directive `%s'",
2059 tline->text);
2060 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002061
H. Peter Anvine2c80182005-01-15 22:15:51 +00002062 case PP_STACKSIZE:
2063 /* Directive to tell NASM what the default stack size is. The
2064 * default is for a 16-bit stack, and this can be overriden with
2065 * %stacksize large.
2066 * the following form:
2067 *
2068 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2069 */
2070 tline = tline->next;
2071 if (tline && tline->type == TOK_WHITESPACE)
2072 tline = tline->next;
2073 if (!tline || tline->type != TOK_ID) {
2074 error(ERR_NONFATAL, "`%%stacksize' missing size parameter");
2075 free_tlist(origline);
2076 return DIRECTIVE_FOUND;
2077 }
2078 if (nasm_stricmp(tline->text, "flat") == 0) {
2079 /* All subsequent ARG directives are for a 32-bit stack */
2080 StackSize = 4;
2081 StackPointer = "ebp";
2082 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002083 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002084 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2085 /* All subsequent ARG directives are for a 64-bit stack */
2086 StackSize = 8;
2087 StackPointer = "rbp";
2088 ArgOffset = 8;
2089 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002090 } else if (nasm_stricmp(tline->text, "large") == 0) {
2091 /* All subsequent ARG directives are for a 16-bit stack,
2092 * far function call.
2093 */
2094 StackSize = 2;
2095 StackPointer = "bp";
2096 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002097 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002098 } else if (nasm_stricmp(tline->text, "small") == 0) {
2099 /* All subsequent ARG directives are for a 16-bit stack,
2100 * far function call. We don't support near functions.
2101 */
2102 StackSize = 2;
2103 StackPointer = "bp";
2104 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002105 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002106 } else {
2107 error(ERR_NONFATAL, "`%%stacksize' invalid size type");
2108 free_tlist(origline);
2109 return DIRECTIVE_FOUND;
2110 }
2111 free_tlist(origline);
2112 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002113
H. Peter Anvine2c80182005-01-15 22:15:51 +00002114 case PP_ARG:
2115 /* TASM like ARG directive to define arguments to functions, in
2116 * the following form:
2117 *
2118 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2119 */
2120 offset = ArgOffset;
2121 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002122 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002123 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002124
H. Peter Anvine2c80182005-01-15 22:15:51 +00002125 /* Find the argument name */
2126 tline = tline->next;
2127 if (tline && tline->type == TOK_WHITESPACE)
2128 tline = tline->next;
2129 if (!tline || tline->type != TOK_ID) {
2130 error(ERR_NONFATAL, "`%%arg' missing argument parameter");
2131 free_tlist(origline);
2132 return DIRECTIVE_FOUND;
2133 }
2134 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002135
H. Peter Anvine2c80182005-01-15 22:15:51 +00002136 /* Find the argument size type */
2137 tline = tline->next;
2138 if (!tline || tline->type != TOK_OTHER
2139 || tline->text[0] != ':') {
2140 error(ERR_NONFATAL,
2141 "Syntax error processing `%%arg' directive");
2142 free_tlist(origline);
2143 return DIRECTIVE_FOUND;
2144 }
2145 tline = tline->next;
2146 if (!tline || tline->type != TOK_ID) {
2147 error(ERR_NONFATAL, "`%%arg' missing size type parameter");
2148 free_tlist(origline);
2149 return DIRECTIVE_FOUND;
2150 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002151
H. Peter Anvine2c80182005-01-15 22:15:51 +00002152 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002153 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002154 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002155 size = parse_size(tt->text);
2156 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002157 error(ERR_NONFATAL,
2158 "Invalid size type for `%%arg' missing directive");
2159 free_tlist(tt);
2160 free_tlist(origline);
2161 return DIRECTIVE_FOUND;
2162 }
2163 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002164
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002165 /* Round up to even stack slots */
2166 size = (size+StackSize-1) & ~(StackSize-1);
2167
H. Peter Anvine2c80182005-01-15 22:15:51 +00002168 /* Now define the macro for the argument */
2169 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2170 arg, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002171 do_directive(tokenize(directive));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002172 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002173
H. Peter Anvine2c80182005-01-15 22:15:51 +00002174 /* Move to the next argument in the list */
2175 tline = tline->next;
2176 if (tline && tline->type == TOK_WHITESPACE)
2177 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002178 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2179 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002180 free_tlist(origline);
2181 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002182
H. Peter Anvine2c80182005-01-15 22:15:51 +00002183 case PP_LOCAL:
2184 /* TASM like LOCAL directive to define local variables for a
2185 * function, in the following form:
2186 *
2187 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2188 *
2189 * The '= LocalSize' at the end is ignored by NASM, but is
2190 * required by TASM to define the local parameter size (and used
2191 * by the TASM macro package).
2192 */
2193 offset = LocalOffset;
2194 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002195 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002196 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002197
H. Peter Anvine2c80182005-01-15 22:15:51 +00002198 /* Find the argument name */
2199 tline = tline->next;
2200 if (tline && tline->type == TOK_WHITESPACE)
2201 tline = tline->next;
2202 if (!tline || tline->type != TOK_ID) {
2203 error(ERR_NONFATAL,
2204 "`%%local' missing argument parameter");
2205 free_tlist(origline);
2206 return DIRECTIVE_FOUND;
2207 }
2208 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002209
H. Peter Anvine2c80182005-01-15 22:15:51 +00002210 /* Find the argument size type */
2211 tline = tline->next;
2212 if (!tline || tline->type != TOK_OTHER
2213 || tline->text[0] != ':') {
2214 error(ERR_NONFATAL,
2215 "Syntax error processing `%%local' directive");
2216 free_tlist(origline);
2217 return DIRECTIVE_FOUND;
2218 }
2219 tline = tline->next;
2220 if (!tline || tline->type != TOK_ID) {
2221 error(ERR_NONFATAL,
2222 "`%%local' missing size type parameter");
2223 free_tlist(origline);
2224 return DIRECTIVE_FOUND;
2225 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002226
H. Peter Anvine2c80182005-01-15 22:15:51 +00002227 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002228 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002229 tt = expand_smacro(tt);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002230 size = parse_size(tt->text);
2231 if (!size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002232 error(ERR_NONFATAL,
2233 "Invalid size type for `%%local' missing directive");
2234 free_tlist(tt);
2235 free_tlist(origline);
2236 return DIRECTIVE_FOUND;
2237 }
2238 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002239
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002240 /* Round up to even stack slots */
2241 size = (size+StackSize-1) & ~(StackSize-1);
2242
2243 offset += size; /* Negative offset, increment before */
2244
2245 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002246 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2247 local, StackPointer, offset);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002248 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002249
H. Peter Anvine2c80182005-01-15 22:15:51 +00002250 /* Now define the assign to setup the enter_c macro correctly */
2251 snprintf(directive, sizeof(directive),
2252 "%%assign %%$localsize %%$localsize+%d", size);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002253 do_directive(tokenize(directive));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002254
H. Peter Anvine2c80182005-01-15 22:15:51 +00002255 /* Move to the next argument in the list */
2256 tline = tline->next;
2257 if (tline && tline->type == TOK_WHITESPACE)
2258 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002259 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
2260 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002261 free_tlist(origline);
2262 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002263
H. Peter Anvine2c80182005-01-15 22:15:51 +00002264 case PP_CLEAR:
2265 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002266 error(ERR_WARNING|ERR_PASS1,
2267 "trailing garbage after `%%clear' ignored");
H. Peter Anvin97a23472007-09-16 17:57:25 -07002268 free_macros();
2269 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002270 free_tlist(origline);
2271 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002272
H. Peter Anvin418ca702008-05-30 10:42:30 -07002273 case PP_DEPEND:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002274 t = tline->next = expand_smacro(tline->next);
2275 skip_white_(t);
2276 if (!t || (t->type != TOK_STRING &&
2277 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin418ca702008-05-30 10:42:30 -07002278 error(ERR_NONFATAL, "`%%depend' expects a file name");
2279 free_tlist(origline);
2280 return DIRECTIVE_FOUND; /* but we did _something_ */
2281 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002282 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002283 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002284 "trailing garbage after `%%depend' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002285 p = t->text;
2286 if (t->type != TOK_INTERNAL_STRING)
2287 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002288 if (dephead && !in_list(*dephead, p)) {
2289 StrList *sl = nasm_malloc(strlen(p)+1+sizeof sl->next);
2290 sl->next = NULL;
2291 strcpy(sl->str, p);
2292 *deptail = sl;
2293 deptail = &sl->next;
2294 }
2295 free_tlist(origline);
2296 return DIRECTIVE_FOUND;
2297
2298 case PP_INCLUDE:
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002299 t = tline->next = expand_smacro(tline->next);
2300 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002301
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002302 if (!t || (t->type != TOK_STRING &&
2303 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002304 error(ERR_NONFATAL, "`%%include' expects a file name");
2305 free_tlist(origline);
2306 return DIRECTIVE_FOUND; /* but we did _something_ */
2307 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002308 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002309 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002310 "trailing garbage after `%%include' ignored");
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002311 p = t->text;
2312 if (t->type != TOK_INTERNAL_STRING)
2313 nasm_unquote(p, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002314 inc = nasm_malloc(sizeof(Include));
2315 inc->next = istk;
2316 inc->conds = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002317 inc->fp = inc_fopen(p, dephead, &deptail, pass == 0);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002318 if (!inc->fp) {
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002319 /* -MG given but file not found */
2320 nasm_free(inc);
2321 } else {
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002322 inc->fname = src_set_fname(nasm_strdup(p));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07002323 inc->lineno = src_set_linnum(0);
2324 inc->lineinc = 1;
2325 inc->expansion = NULL;
2326 inc->mstk = NULL;
2327 istk = inc;
2328 list->uplevel(LIST_INCLUDE);
2329 }
2330 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002331 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002332
H. Peter Anvind2456592008-06-19 15:04:18 -07002333 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002334 {
H. Peter Anvina70547f2008-07-19 21:44:26 -07002335 static macros_t *use_pkg;
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002336 const char *pkg_macro;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002337
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002338 tline = tline->next;
2339 skip_white_(tline);
2340 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002341
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002342 if (!tline || (tline->type != TOK_STRING &&
2343 tline->type != TOK_INTERNAL_STRING &&
2344 tline->type != TOK_ID)) {
H. Peter Anvin926fc402008-06-19 16:26:12 -07002345 error(ERR_NONFATAL, "`%%use' expects a package name");
H. Peter Anvind2456592008-06-19 15:04:18 -07002346 free_tlist(origline);
2347 return DIRECTIVE_FOUND; /* but we did _something_ */
2348 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002349 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002350 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvind2456592008-06-19 15:04:18 -07002351 "trailing garbage after `%%use' ignored");
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002352 if (tline->type == TOK_STRING)
2353 nasm_unquote(tline->text, NULL);
2354 use_pkg = nasm_stdmac_find_package(tline->text);
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002355 if (!use_pkg)
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002356 error(ERR_NONFATAL, "unknown `%%use' package: %s", tline->text);
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002357 /* The first string will be <%define>__USE_*__ */
H. Peter Anvin7e50d232008-06-25 14:54:14 -07002358 pkg_macro = (char *)use_pkg + 1;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002359 if (!smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
2360 /* Not already included, go ahead and include it */
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002361 stdmacpos = use_pkg;
2362 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002363 free_tlist(origline);
2364 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002365 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002366 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002367 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002368 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002369 tline = tline->next;
2370 skip_white_(tline);
2371 tline = expand_id(tline);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002372 if (tline) {
2373 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin42b56392008-10-24 16:24:21 -07002374 error(ERR_NONFATAL, "`%s' expects a context identifier",
2375 pp_directives[i]);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002376 free_tlist(origline);
2377 return DIRECTIVE_FOUND; /* but we did _something_ */
2378 }
2379 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002380 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin42b56392008-10-24 16:24:21 -07002381 "trailing garbage after `%s' ignored",
2382 pp_directives[i]);
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002383 p = nasm_strdup(tline->text);
2384 } else {
H. Peter Anvin42b56392008-10-24 16:24:21 -07002385 p = NULL; /* Anonymous */
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07002386 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002387
2388 if (i == PP_PUSH) {
2389 ctx = nasm_malloc(sizeof(Context));
2390 ctx->next = cstk;
2391 hash_init(&ctx->localmac, HASH_SMALL);
2392 ctx->name = p;
2393 ctx->number = unique++;
2394 cstk = ctx;
2395 } else {
2396 /* %pop or %repl */
2397 if (!cstk) {
2398 error(ERR_NONFATAL, "`%s': context stack is empty",
2399 pp_directives[i]);
2400 } else if (i == PP_POP) {
2401 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
2402 error(ERR_NONFATAL, "`%%pop' in wrong context: %s, "
2403 "expected %s",
2404 cstk->name ? cstk->name : "anonymous", p);
2405 else
2406 ctx_pop();
2407 } else {
2408 /* i == PP_REPL */
2409 nasm_free(cstk->name);
2410 cstk->name = p;
2411 p = NULL;
2412 }
2413 nasm_free(p);
2414 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002415 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002416 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002417 case PP_FATAL:
2418 severity = ERR_FATAL|ERR_NO_SEVERITY;
2419 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002420 case PP_ERROR:
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002421 severity = ERR_NONFATAL|ERR_NO_SEVERITY;
2422 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002423 case PP_WARNING:
H. Peter Anvin2f160432008-09-30 16:39:17 -07002424 severity = ERR_WARNING|ERR_NO_SEVERITY|ERR_WARN_USER;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002425 goto issue_error;
2426
2427 issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002428 {
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002429 /* Only error out if this is the final pass */
2430 if (pass != 2 && i != PP_FATAL)
2431 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002432
H. Peter Anvine2c80182005-01-15 22:15:51 +00002433 tline->next = expand_smacro(tline->next);
2434 tline = tline->next;
2435 skip_white_(tline);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002436 t = tline ? tline->next : NULL;
2437 skip_white_(t);
2438 if (tok_type_(tline, TOK_STRING) && !t) {
2439 /* The line contains only a quoted string */
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002440 p = tline->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002441 nasm_unquote(p, NULL);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002442 error(severity, "%s: %s", pp_directives[i], p);
2443 } else {
2444 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002445 p = detoken(tline, false);
H. Peter Anvin7df04172008-06-10 18:27:38 -07002446 error(severity, "%s: %s", pp_directives[i], p);
2447 nasm_free(p);
H. Peter Anvind2456592008-06-19 15:04:18 -07002448 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002449 free_tlist(origline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002450 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002451 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002452
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002453 CASE_PP_IF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002454 if (istk->conds && !emitting(istk->conds->state))
2455 j = COND_NEVER;
2456 else {
2457 j = if_condition(tline->next, i);
2458 tline->next = NULL; /* it got freed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002459 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2460 }
2461 cond = nasm_malloc(sizeof(Cond));
2462 cond->next = istk->conds;
2463 cond->state = j;
2464 istk->conds = cond;
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002465 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002466 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002467
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002468 CASE_PP_ELIF:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002469 if (!istk->conds)
H. Peter Anvin4169a472007-09-12 01:29:43 +00002470 error(ERR_FATAL, "`%s': no matching `%%if'", pp_directives[i]);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002471 switch(istk->conds->state) {
2472 case COND_IF_TRUE:
2473 istk->conds->state = COND_DONE;
2474 break;
2475
2476 case COND_DONE:
2477 case COND_NEVER:
2478 break;
2479
2480 case COND_ELSE_TRUE:
2481 case COND_ELSE_FALSE:
H. Peter Anvin917a3492008-09-24 09:14:49 -07002482 error_precond(ERR_WARNING|ERR_PASS1,
2483 "`%%elif' after `%%else' ignored");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002484 istk->conds->state = COND_NEVER;
2485 break;
2486
2487 case COND_IF_FALSE:
2488 /*
2489 * IMPORTANT: In the case of %if, we will already have
2490 * called expand_mmac_params(); however, if we're
2491 * processing an %elif we must have been in a
2492 * non-emitting mode, which would have inhibited
H. Peter Anvin67c63722008-10-26 23:49:00 -07002493 * the normal invocation of expand_mmac_params().
2494 * Therefore, we have to do it explicitly here.
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002495 */
H. Peter Anvin67c63722008-10-26 23:49:00 -07002496 j = if_condition(expand_mmac_params(tline->next), i);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002497 tline->next = NULL; /* it got freed */
2498 istk->conds->state =
2499 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2500 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002501 }
H. Peter Anvin7061ad72007-11-26 22:02:21 -08002502 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002503 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002504
H. Peter Anvine2c80182005-01-15 22:15:51 +00002505 case PP_ELSE:
2506 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002507 error_precond(ERR_WARNING|ERR_PASS1,
2508 "trailing garbage after `%%else' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002509 if (!istk->conds)
2510 error(ERR_FATAL, "`%%else': no matching `%%if'");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002511 switch(istk->conds->state) {
2512 case COND_IF_TRUE:
2513 case COND_DONE:
2514 istk->conds->state = COND_ELSE_FALSE;
2515 break;
2516
2517 case COND_NEVER:
2518 break;
2519
2520 case COND_IF_FALSE:
2521 istk->conds->state = COND_ELSE_TRUE;
2522 break;
2523
2524 case COND_ELSE_TRUE:
2525 case COND_ELSE_FALSE:
H. Peter Anvin917a3492008-09-24 09:14:49 -07002526 error_precond(ERR_WARNING|ERR_PASS1,
2527 "`%%else' after `%%else' ignored.");
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002528 istk->conds->state = COND_NEVER;
2529 break;
2530 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002531 free_tlist(origline);
2532 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002533
H. Peter Anvine2c80182005-01-15 22:15:51 +00002534 case PP_ENDIF:
2535 if (tline->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002536 error_precond(ERR_WARNING|ERR_PASS1,
2537 "trailing garbage after `%%endif' ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002538 if (!istk->conds)
2539 error(ERR_FATAL, "`%%endif': no matching `%%if'");
2540 cond = istk->conds;
2541 istk->conds = cond->next;
2542 nasm_free(cond);
2543 free_tlist(origline);
2544 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002545
H. Peter Anvine2c80182005-01-15 22:15:51 +00002546 case PP_MACRO:
2547 case PP_IMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002548 if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002549 error(ERR_FATAL,
2550 "`%%%smacro': already defining a macro",
2551 (i == PP_IMACRO ? "i" : ""));
H. Peter Anvina26433d2008-07-16 14:40:01 -07002552 return DIRECTIVE_FOUND;
2553 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002554 defining = nasm_malloc(sizeof(MMacro));
H. Peter Anvina26433d2008-07-16 14:40:01 -07002555 defining->casesense = (i == PP_MACRO);
2556 if (!parse_mmacro_spec(tline, defining, pp_directives[i])) {
2557 nasm_free(defining);
2558 defining = NULL;
2559 return DIRECTIVE_FOUND;
2560 }
2561
H. Peter Anvin166c2472008-05-28 12:28:58 -07002562 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002563 while (mmac) {
2564 if (!strcmp(mmac->name, defining->name) &&
2565 (mmac->nparam_min <= defining->nparam_max
2566 || defining->plus)
2567 && (defining->nparam_min <= mmac->nparam_max
2568 || mmac->plus)) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002569 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002570 "redefining multi-line macro `%s'", defining->name);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002571 return DIRECTIVE_FOUND;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002572 }
2573 mmac = mmac->next;
2574 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002575 free_tlist(origline);
2576 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002577
H. Peter Anvine2c80182005-01-15 22:15:51 +00002578 case PP_ENDM:
2579 case PP_ENDMACRO:
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02002580 if (! (defining && defining->name)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002581 error(ERR_NONFATAL, "`%s': not defining a macro", tline->text);
2582 return DIRECTIVE_FOUND;
2583 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07002584 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
H. Peter Anvin97a23472007-09-16 17:57:25 -07002585 defining->next = *mmhead;
2586 *mmhead = defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002587 defining = NULL;
2588 free_tlist(origline);
2589 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002590
H. Peter Anvina26433d2008-07-16 14:40:01 -07002591 case PP_UNMACRO:
2592 case PP_UNIMACRO:
2593 {
2594 MMacro **mmac_p;
2595 MMacro spec;
2596
2597 spec.casesense = (i == PP_UNMACRO);
2598 if (!parse_mmacro_spec(tline, &spec, pp_directives[i])) {
2599 return DIRECTIVE_FOUND;
2600 }
2601 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2602 while (mmac_p && *mmac_p) {
2603 mmac = *mmac_p;
2604 if (mmac->casesense == spec.casesense &&
2605 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2606 mmac->nparam_min == spec.nparam_min &&
2607 mmac->nparam_max == spec.nparam_max &&
2608 mmac->plus == spec.plus) {
2609 *mmac_p = mmac->next;
2610 free_mmacro(mmac);
2611 } else {
2612 mmac_p = &mmac->next;
2613 }
2614 }
2615 free_tlist(origline);
2616 free_tlist(spec.dlist);
2617 return DIRECTIVE_FOUND;
2618 }
2619
H. Peter Anvine2c80182005-01-15 22:15:51 +00002620 case PP_ROTATE:
2621 if (tline->next && tline->next->type == TOK_WHITESPACE)
2622 tline = tline->next;
2623 if (tline->next == NULL) {
2624 free_tlist(origline);
2625 error(ERR_NONFATAL, "`%%rotate' missing rotate count");
2626 return DIRECTIVE_FOUND;
2627 }
2628 t = expand_smacro(tline->next);
2629 tline->next = NULL;
2630 free_tlist(origline);
2631 tline = t;
2632 tptr = &t;
2633 tokval.t_type = TOKEN_INVALID;
2634 evalresult =
2635 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2636 free_tlist(tline);
2637 if (!evalresult)
2638 return DIRECTIVE_FOUND;
2639 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002640 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002641 "trailing garbage after expression ignored");
2642 if (!is_simple(evalresult)) {
2643 error(ERR_NONFATAL, "non-constant value given to `%%rotate'");
2644 return DIRECTIVE_FOUND;
2645 }
2646 mmac = istk->mstk;
2647 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2648 mmac = mmac->next_active;
2649 if (!mmac) {
2650 error(ERR_NONFATAL, "`%%rotate' invoked outside a macro call");
2651 } else if (mmac->nparam == 0) {
2652 error(ERR_NONFATAL,
2653 "`%%rotate' invoked within macro without parameters");
2654 } else {
H. Peter Anvin25a99342007-09-22 17:45:45 -07002655 int rotate = mmac->rotate + reloc_value(evalresult);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002656
H. Peter Anvin25a99342007-09-22 17:45:45 -07002657 rotate %= (int)mmac->nparam;
2658 if (rotate < 0)
2659 rotate += mmac->nparam;
H. Peter Anvin70653092007-10-19 14:42:29 -07002660
H. Peter Anvin25a99342007-09-22 17:45:45 -07002661 mmac->rotate = rotate;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002662 }
2663 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002664
H. Peter Anvine2c80182005-01-15 22:15:51 +00002665 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002666 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002667 do {
2668 tline = tline->next;
2669 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002670
H. Peter Anvine2c80182005-01-15 22:15:51 +00002671 if (tok_type_(tline, TOK_ID) &&
2672 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002673 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002674 do {
2675 tline = tline->next;
2676 } while (tok_type_(tline, TOK_WHITESPACE));
2677 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002678
H. Peter Anvine2c80182005-01-15 22:15:51 +00002679 if (tline) {
2680 t = expand_smacro(tline);
2681 tptr = &t;
2682 tokval.t_type = TOKEN_INVALID;
2683 evalresult =
2684 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
2685 if (!evalresult) {
2686 free_tlist(origline);
2687 return DIRECTIVE_FOUND;
2688 }
2689 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002690 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002691 "trailing garbage after expression ignored");
2692 if (!is_simple(evalresult)) {
2693 error(ERR_NONFATAL, "non-constant value given to `%%rep'");
2694 return DIRECTIVE_FOUND;
2695 }
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002696 count = reloc_value(evalresult) + 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002697 } else {
2698 error(ERR_NONFATAL, "`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002699 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002700 }
2701 free_tlist(origline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002702
H. Peter Anvine2c80182005-01-15 22:15:51 +00002703 tmp_defining = defining;
2704 defining = nasm_malloc(sizeof(MMacro));
2705 defining->name = NULL; /* flags this macro as a %rep block */
H. Peter Anvin70055962007-10-11 00:05:31 -07002706 defining->casesense = false;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002707 defining->plus = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002708 defining->nolist = nolist;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002709 defining->in_progress = count;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002710 defining->nparam_min = defining->nparam_max = 0;
2711 defining->defaults = NULL;
2712 defining->dlist = NULL;
2713 defining->expansion = NULL;
2714 defining->next_active = istk->mstk;
2715 defining->rep_nest = tmp_defining;
2716 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002717
H. Peter Anvine2c80182005-01-15 22:15:51 +00002718 case PP_ENDREP:
2719 if (!defining || defining->name) {
2720 error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'");
2721 return DIRECTIVE_FOUND;
2722 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002723
H. Peter Anvine2c80182005-01-15 22:15:51 +00002724 /*
2725 * Now we have a "macro" defined - although it has no name
2726 * and we won't be entering it in the hash tables - we must
2727 * push a macro-end marker for it on to istk->expansion.
2728 * After that, it will take care of propagating itself (a
2729 * macro-end marker line for a macro which is really a %rep
2730 * block will cause the macro to be re-expanded, complete
2731 * with another macro-end marker to ensure the process
2732 * continues) until the whole expansion is forcibly removed
2733 * from istk->expansion by a %exitrep.
2734 */
2735 l = nasm_malloc(sizeof(Line));
2736 l->next = istk->expansion;
2737 l->finishes = defining;
2738 l->first = NULL;
2739 istk->expansion = l;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002740
H. Peter Anvine2c80182005-01-15 22:15:51 +00002741 istk->mstk = defining;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002742
H. Peter Anvine2c80182005-01-15 22:15:51 +00002743 list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
2744 tmp_defining = defining;
2745 defining = defining->rep_nest;
2746 free_tlist(origline);
2747 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002748
H. Peter Anvine2c80182005-01-15 22:15:51 +00002749 case PP_EXITREP:
2750 /*
2751 * We must search along istk->expansion until we hit a
2752 * macro-end marker for a macro with no name. Then we set
2753 * its `in_progress' flag to 0.
2754 */
2755 for (l = istk->expansion; l; l = l->next)
2756 if (l->finishes && !l->finishes->name)
H. Peter Anvinca348b62008-07-23 10:49:26 -04002757 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002758
H. Peter Anvine2c80182005-01-15 22:15:51 +00002759 if (l)
Charles Crayned4200be2008-07-12 16:42:33 -07002760 l->finishes->in_progress = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002761 else
2762 error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block");
2763 free_tlist(origline);
2764 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002765
H. Peter Anvine2c80182005-01-15 22:15:51 +00002766 case PP_XDEFINE:
2767 case PP_IXDEFINE:
2768 case PP_DEFINE:
2769 case PP_IDEFINE:
H. Peter Anvin95e7f952007-10-11 13:38:38 -07002770 casesense = (i == PP_DEFINE || i == PP_XDEFINE);
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002771
H. Peter Anvine2c80182005-01-15 22:15:51 +00002772 tline = tline->next;
2773 skip_white_(tline);
2774 tline = expand_id(tline);
2775 if (!tline || (tline->type != TOK_ID &&
2776 (tline->type != TOK_PREPROC_ID ||
2777 tline->text[1] != '$'))) {
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002778 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2779 pp_directives[i]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002780 free_tlist(origline);
2781 return DIRECTIVE_FOUND;
2782 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002783
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002784 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002785 last = tline;
2786 param_start = tline = tline->next;
2787 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002788
H. Peter Anvine2c80182005-01-15 22:15:51 +00002789 /* Expand the macro definition now for %xdefine and %ixdefine */
2790 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
2791 tline = expand_smacro(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002792
H. Peter Anvine2c80182005-01-15 22:15:51 +00002793 if (tok_is_(tline, "(")) {
2794 /*
2795 * This macro has parameters.
2796 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00002797
H. Peter Anvine2c80182005-01-15 22:15:51 +00002798 tline = tline->next;
2799 while (1) {
2800 skip_white_(tline);
2801 if (!tline) {
2802 error(ERR_NONFATAL, "parameter identifier expected");
2803 free_tlist(origline);
2804 return DIRECTIVE_FOUND;
2805 }
2806 if (tline->type != TOK_ID) {
2807 error(ERR_NONFATAL,
2808 "`%s': parameter identifier expected",
2809 tline->text);
2810 free_tlist(origline);
2811 return DIRECTIVE_FOUND;
2812 }
2813 tline->type = TOK_SMAC_PARAM + nparam++;
2814 tline = tline->next;
2815 skip_white_(tline);
2816 if (tok_is_(tline, ",")) {
2817 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04002818 } else {
2819 if (!tok_is_(tline, ")")) {
2820 error(ERR_NONFATAL,
2821 "`)' expected to terminate macro template");
2822 free_tlist(origline);
2823 return DIRECTIVE_FOUND;
2824 }
2825 break;
2826 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002827 }
2828 last = tline;
2829 tline = tline->next;
2830 }
2831 if (tok_type_(tline, TOK_WHITESPACE))
2832 last = tline, tline = tline->next;
2833 macro_start = NULL;
2834 last->next = NULL;
2835 t = tline;
2836 while (t) {
2837 if (t->type == TOK_ID) {
2838 for (tt = param_start; tt; tt = tt->next)
2839 if (tt->type >= TOK_SMAC_PARAM &&
2840 !strcmp(tt->text, t->text))
2841 t->type = tt->type;
2842 }
2843 tt = t->next;
2844 t->next = macro_start;
2845 macro_start = t;
2846 t = tt;
2847 }
2848 /*
2849 * Good. We now have a macro name, a parameter count, and a
2850 * token list (in reverse order) for an expansion. We ought
2851 * to be OK just to create an SMacro, store it, and let
2852 * free_tlist have the rest of the line (which we have
2853 * carefully re-terminated after chopping off the expansion
2854 * from the end).
2855 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002856 define_smacro(ctx, mname, casesense, nparam, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002857 free_tlist(origline);
2858 return DIRECTIVE_FOUND;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002859
H. Peter Anvine2c80182005-01-15 22:15:51 +00002860 case PP_UNDEF:
2861 tline = tline->next;
2862 skip_white_(tline);
2863 tline = expand_id(tline);
2864 if (!tline || (tline->type != TOK_ID &&
2865 (tline->type != TOK_PREPROC_ID ||
2866 tline->text[1] != '$'))) {
2867 error(ERR_NONFATAL, "`%%undef' expects a macro identifier");
2868 free_tlist(origline);
2869 return DIRECTIVE_FOUND;
2870 }
2871 if (tline->next) {
H. Peter Anvin917a3492008-09-24 09:14:49 -07002872 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002873 "trailing garbage after macro name ignored");
2874 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002875
H. Peter Anvine2c80182005-01-15 22:15:51 +00002876 /* Find the context that symbol belongs to */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002877 ctx = get_ctx(tline->text, &mname, false);
2878 undef_smacro(ctx, mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002879 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002880 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002881
H. Peter Anvin9e200162008-06-04 17:23:14 -07002882 case PP_DEFSTR:
2883 case PP_IDEFSTR:
2884 casesense = (i == PP_DEFSTR);
2885
2886 tline = tline->next;
2887 skip_white_(tline);
2888 tline = expand_id(tline);
2889 if (!tline || (tline->type != TOK_ID &&
2890 (tline->type != TOK_PREPROC_ID ||
2891 tline->text[1] != '$'))) {
2892 error(ERR_NONFATAL, "`%s' expects a macro identifier",
2893 pp_directives[i]);
2894 free_tlist(origline);
2895 return DIRECTIVE_FOUND;
2896 }
2897
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002898 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin9e200162008-06-04 17:23:14 -07002899 last = tline;
2900 tline = expand_smacro(tline->next);
2901 last->next = NULL;
2902
2903 while (tok_type_(tline, TOK_WHITESPACE))
2904 tline = delete_Token(tline);
2905
2906 p = detoken(tline, false);
2907 macro_start = nasm_malloc(sizeof(*macro_start));
2908 macro_start->next = NULL;
2909 macro_start->text = nasm_quote(p, strlen(p));
2910 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002911 macro_start->a.mac = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07002912 nasm_free(p);
2913
2914 /*
2915 * We now have a macro name, an implicit parameter count of
2916 * zero, and a string token to use as an expansion. Create
2917 * and store an SMacro.
2918 */
2919 define_smacro(ctx, mname, casesense, 0, macro_start);
2920 free_tlist(origline);
2921 return DIRECTIVE_FOUND;
2922
H. Peter Anvin418ca702008-05-30 10:42:30 -07002923 case PP_PATHSEARCH:
2924 {
2925 FILE *fp;
2926 StrList *xsl = NULL;
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002927 StrList **xst = &xsl;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002928
2929 casesense = true;
2930
2931 tline = tline->next;
2932 skip_white_(tline);
2933 tline = expand_id(tline);
2934 if (!tline || (tline->type != TOK_ID &&
2935 (tline->type != TOK_PREPROC_ID ||
2936 tline->text[1] != '$'))) {
2937 error(ERR_NONFATAL,
2938 "`%%pathsearch' expects a macro identifier as first parameter");
2939 free_tlist(origline);
2940 return DIRECTIVE_FOUND;
2941 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002942 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002943 last = tline;
2944 tline = expand_smacro(tline->next);
2945 last->next = NULL;
2946
2947 t = tline;
2948 while (tok_type_(t, TOK_WHITESPACE))
2949 t = t->next;
2950
2951 if (!t || (t->type != TOK_STRING &&
2952 t->type != TOK_INTERNAL_STRING)) {
2953 error(ERR_NONFATAL, "`%%pathsearch' expects a file name");
2954 free_tlist(tline);
2955 free_tlist(origline);
2956 return DIRECTIVE_FOUND; /* but we did _something_ */
2957 }
2958 if (t->next)
H. Peter Anvin917a3492008-09-24 09:14:49 -07002959 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvin418ca702008-05-30 10:42:30 -07002960 "trailing garbage after `%%pathsearch' ignored");
H. Peter Anvin427cc912008-06-01 21:43:03 -07002961 p = t->text;
2962 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002963 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002964
H. Peter Anvin2b1c3b92008-06-06 10:38:46 -07002965 fp = inc_fopen(p, &xsl, &xst, true);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002966 if (fp) {
2967 p = xsl->str;
2968 fclose(fp); /* Don't actually care about the file */
2969 }
2970 macro_start = nasm_malloc(sizeof(*macro_start));
2971 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07002972 macro_start->text = nasm_quote(p, strlen(p));
H. Peter Anvin418ca702008-05-30 10:42:30 -07002973 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002974 macro_start->a.mac = NULL;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002975 if (xsl)
2976 nasm_free(xsl);
2977
2978 /*
2979 * We now have a macro name, an implicit parameter count of
2980 * zero, and a string token to use as an expansion. Create
2981 * and store an SMacro.
2982 */
2983 define_smacro(ctx, mname, casesense, 0, macro_start);
2984 free_tlist(tline);
2985 free_tlist(origline);
2986 return DIRECTIVE_FOUND;
2987 }
2988
H. Peter Anvine2c80182005-01-15 22:15:51 +00002989 case PP_STRLEN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002990 casesense = true;
H. Peter Anvin70653092007-10-19 14:42:29 -07002991
H. Peter Anvine2c80182005-01-15 22:15:51 +00002992 tline = tline->next;
2993 skip_white_(tline);
2994 tline = expand_id(tline);
2995 if (!tline || (tline->type != TOK_ID &&
2996 (tline->type != TOK_PREPROC_ID ||
2997 tline->text[1] != '$'))) {
2998 error(ERR_NONFATAL,
2999 "`%%strlen' expects a macro identifier as first parameter");
3000 free_tlist(origline);
3001 return DIRECTIVE_FOUND;
3002 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003003 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003004 last = tline;
3005 tline = expand_smacro(tline->next);
3006 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003007
H. Peter Anvine2c80182005-01-15 22:15:51 +00003008 t = tline;
3009 while (tok_type_(t, TOK_WHITESPACE))
3010 t = t->next;
3011 /* t should now point to the string */
3012 if (t->type != TOK_STRING) {
3013 error(ERR_NONFATAL,
3014 "`%%strlen` requires string as second parameter");
3015 free_tlist(tline);
3016 free_tlist(origline);
3017 return DIRECTIVE_FOUND;
3018 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003019
H. Peter Anvine2c80182005-01-15 22:15:51 +00003020 macro_start = nasm_malloc(sizeof(*macro_start));
3021 macro_start->next = NULL;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003022 make_tok_num(macro_start, nasm_unquote(t->text, NULL));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003023 macro_start->a.mac = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003024
H. Peter Anvine2c80182005-01-15 22:15:51 +00003025 /*
3026 * We now have a macro name, an implicit parameter count of
3027 * zero, and a numeric token to use as an expansion. Create
3028 * and store an SMacro.
3029 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003030 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003031 free_tlist(tline);
3032 free_tlist(origline);
3033 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003034
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003035 case PP_STRCAT:
3036 casesense = true;
3037
3038 tline = tline->next;
3039 skip_white_(tline);
3040 tline = expand_id(tline);
3041 if (!tline || (tline->type != TOK_ID &&
3042 (tline->type != TOK_PREPROC_ID ||
3043 tline->text[1] != '$'))) {
3044 error(ERR_NONFATAL,
3045 "`%%strcat' expects a macro identifier as first parameter");
3046 free_tlist(origline);
3047 return DIRECTIVE_FOUND;
3048 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003049 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003050 last = tline;
3051 tline = expand_smacro(tline->next);
3052 last->next = NULL;
3053
3054 len = 0;
3055 for (t = tline; t; t = t->next) {
3056 switch (t->type) {
3057 case TOK_WHITESPACE:
3058 break;
3059 case TOK_STRING:
3060 len += t->a.len = nasm_unquote(t->text, NULL);
3061 break;
H. Peter Anvinaccf4332008-07-01 21:42:08 -07003062 case TOK_OTHER:
3063 if (!strcmp(t->text, ",")) /* permit comma separators */
3064 break;
3065 /* else fall through */
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003066 default:
3067 error(ERR_NONFATAL,
3068 "non-string passed to `%%strcat' (%d)", t->type);
3069 free_tlist(tline);
3070 free_tlist(origline);
3071 return DIRECTIVE_FOUND;
3072 }
3073 }
3074
3075 p = pp = nasm_malloc(len);
3076 t = tline;
3077 for (t = tline; t; t = t->next) {
3078 if (t->type == TOK_STRING) {
3079 memcpy(p, t->text, t->a.len);
3080 p += t->a.len;
3081 }
3082 }
3083
3084 /*
3085 * We now have a macro name, an implicit parameter count of
3086 * zero, and a numeric token to use as an expansion. Create
3087 * and store an SMacro.
3088 */
3089 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
3090 macro_start->text = nasm_quote(pp, len);
3091 nasm_free(pp);
3092 define_smacro(ctx, mname, casesense, 0, macro_start);
3093 free_tlist(tline);
3094 free_tlist(origline);
3095 return DIRECTIVE_FOUND;
3096
H. Peter Anvine2c80182005-01-15 22:15:51 +00003097 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003098 {
3099 int64_t a1, a2;
3100 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003101
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003102 casesense = true;
3103
H. Peter Anvine2c80182005-01-15 22:15:51 +00003104 tline = tline->next;
3105 skip_white_(tline);
3106 tline = expand_id(tline);
3107 if (!tline || (tline->type != TOK_ID &&
3108 (tline->type != TOK_PREPROC_ID ||
3109 tline->text[1] != '$'))) {
3110 error(ERR_NONFATAL,
3111 "`%%substr' expects a macro identifier as first parameter");
3112 free_tlist(origline);
3113 return DIRECTIVE_FOUND;
3114 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003115 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003116 last = tline;
3117 tline = expand_smacro(tline->next);
3118 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003119
H. Peter Anvine2c80182005-01-15 22:15:51 +00003120 t = tline->next;
3121 while (tok_type_(t, TOK_WHITESPACE))
3122 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003123
H. Peter Anvine2c80182005-01-15 22:15:51 +00003124 /* t should now point to the string */
3125 if (t->type != TOK_STRING) {
3126 error(ERR_NONFATAL,
3127 "`%%substr` requires string as second parameter");
3128 free_tlist(tline);
3129 free_tlist(origline);
3130 return DIRECTIVE_FOUND;
3131 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003132
H. Peter Anvine2c80182005-01-15 22:15:51 +00003133 tt = t->next;
3134 tptr = &tt;
3135 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003136 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3137 pass, error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003138 if (!evalresult) {
3139 free_tlist(tline);
3140 free_tlist(origline);
3141 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003142 } else if (!is_simple(evalresult)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003143 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3144 free_tlist(tline);
3145 free_tlist(origline);
3146 return DIRECTIVE_FOUND;
3147 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003148 a1 = evalresult->value-1;
3149
3150 while (tok_type_(tt, TOK_WHITESPACE))
3151 tt = tt->next;
3152 if (!tt) {
3153 a2 = 1; /* Backwards compatibility: one character */
3154 } else {
3155 tokval.t_type = TOKEN_INVALID;
3156 evalresult = evaluate(ppscan, tptr, &tokval, NULL,
3157 pass, error, NULL);
3158 if (!evalresult) {
3159 free_tlist(tline);
3160 free_tlist(origline);
3161 return DIRECTIVE_FOUND;
3162 } else if (!is_simple(evalresult)) {
3163 error(ERR_NONFATAL, "non-constant value given to `%%substr`");
3164 free_tlist(tline);
3165 free_tlist(origline);
3166 return DIRECTIVE_FOUND;
3167 }
3168 a2 = evalresult->value;
3169 }
3170
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003171 len = nasm_unquote(t->text, NULL);
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003172 if (a2 < 0)
3173 a2 = a2+1+len-a1;
3174 if (a1+a2 > (int64_t)len)
3175 a2 = len-a1;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003176
H. Peter Anvine2c80182005-01-15 22:15:51 +00003177 macro_start = nasm_malloc(sizeof(*macro_start));
3178 macro_start->next = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003179 macro_start->text = nasm_quote((a1 < 0) ? "" : t->text+a1, a2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003180 macro_start->type = TOK_STRING;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003181 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003182
H. Peter Anvine2c80182005-01-15 22:15:51 +00003183 /*
3184 * We now have a macro name, an implicit parameter count of
3185 * zero, and a numeric token to use as an expansion. Create
3186 * and store an SMacro.
3187 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003188 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003189 free_tlist(tline);
3190 free_tlist(origline);
3191 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003192 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003193
H. Peter Anvine2c80182005-01-15 22:15:51 +00003194 case PP_ASSIGN:
3195 case PP_IASSIGN:
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003196 casesense = (i == PP_ASSIGN);
3197
H. Peter Anvine2c80182005-01-15 22:15:51 +00003198 tline = tline->next;
3199 skip_white_(tline);
3200 tline = expand_id(tline);
3201 if (!tline || (tline->type != TOK_ID &&
3202 (tline->type != TOK_PREPROC_ID ||
3203 tline->text[1] != '$'))) {
3204 error(ERR_NONFATAL,
3205 "`%%%sassign' expects a macro identifier",
3206 (i == PP_IASSIGN ? "i" : ""));
3207 free_tlist(origline);
3208 return DIRECTIVE_FOUND;
3209 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003210 ctx = get_ctx(tline->text, &mname, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003211 last = tline;
3212 tline = expand_smacro(tline->next);
3213 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003214
H. Peter Anvine2c80182005-01-15 22:15:51 +00003215 t = tline;
3216 tptr = &t;
3217 tokval.t_type = TOKEN_INVALID;
3218 evalresult =
3219 evaluate(ppscan, tptr, &tokval, NULL, pass, error, NULL);
3220 free_tlist(tline);
3221 if (!evalresult) {
3222 free_tlist(origline);
3223 return DIRECTIVE_FOUND;
3224 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003225
H. Peter Anvine2c80182005-01-15 22:15:51 +00003226 if (tokval.t_type)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003227 error(ERR_WARNING|ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003228 "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003229
H. Peter Anvine2c80182005-01-15 22:15:51 +00003230 if (!is_simple(evalresult)) {
3231 error(ERR_NONFATAL,
3232 "non-constant value given to `%%%sassign'",
3233 (i == PP_IASSIGN ? "i" : ""));
3234 free_tlist(origline);
3235 return DIRECTIVE_FOUND;
3236 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003237
H. Peter Anvine2c80182005-01-15 22:15:51 +00003238 macro_start = nasm_malloc(sizeof(*macro_start));
3239 macro_start->next = NULL;
3240 make_tok_num(macro_start, reloc_value(evalresult));
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003241 macro_start->a.mac = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003242
H. Peter Anvine2c80182005-01-15 22:15:51 +00003243 /*
3244 * We now have a macro name, an implicit parameter count of
3245 * zero, and a numeric token to use as an expansion. Create
3246 * and store an SMacro.
3247 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003248 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003249 free_tlist(origline);
3250 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003251
H. Peter Anvine2c80182005-01-15 22:15:51 +00003252 case PP_LINE:
3253 /*
3254 * Syntax is `%line nnn[+mmm] [filename]'
3255 */
3256 tline = tline->next;
3257 skip_white_(tline);
3258 if (!tok_type_(tline, TOK_NUMBER)) {
3259 error(ERR_NONFATAL, "`%%line' expects line number");
3260 free_tlist(origline);
3261 return DIRECTIVE_FOUND;
3262 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003263 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003264 m = 1;
3265 tline = tline->next;
3266 if (tok_is_(tline, "+")) {
3267 tline = tline->next;
3268 if (!tok_type_(tline, TOK_NUMBER)) {
3269 error(ERR_NONFATAL, "`%%line' expects line increment");
3270 free_tlist(origline);
3271 return DIRECTIVE_FOUND;
3272 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003273 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003274 tline = tline->next;
3275 }
3276 skip_white_(tline);
3277 src_set_linnum(k);
3278 istk->lineinc = m;
3279 if (tline) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003280 nasm_free(src_set_fname(detoken(tline, false)));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003281 }
3282 free_tlist(origline);
3283 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003284
H. Peter Anvine2c80182005-01-15 22:15:51 +00003285 default:
3286 error(ERR_FATAL,
3287 "preprocessor directive `%s' not yet implemented",
H. Peter Anvin4169a472007-09-12 01:29:43 +00003288 pp_directives[i]);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003289 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003290 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003291}
3292
3293/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003294 * Ensure that a macro parameter contains a condition code and
3295 * nothing else. Return the condition code index if so, or -1
3296 * otherwise.
3297 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003298static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003299{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003300 Token *tt;
3301 int i, j, k, m;
3302
H. Peter Anvin25a99342007-09-22 17:45:45 -07003303 if (!t)
3304 return -1; /* Probably a %+ without a space */
3305
H. Peter Anvineba20a72002-04-30 20:53:55 +00003306 skip_white_(t);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003307 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003308 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003309 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003310 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003311 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003312 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003313
3314 i = -1;
Ed Beroset3ab3f412002-06-11 03:31:49 +00003315 j = elements(conditions);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003316 while (j - i > 1) {
3317 k = (j + i) / 2;
3318 m = nasm_stricmp(t->text, conditions[k]);
3319 if (m == 0) {
3320 i = k;
3321 j = -2;
3322 break;
3323 } else if (m < 0) {
3324 j = k;
3325 } else
3326 i = k;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003327 }
3328 if (j != -2)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003329 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003330 return i;
3331}
3332
H. Peter Anvind784a082009-04-20 14:01:18 -07003333static bool paste_tokens(Token **head, bool handle_paste_tokens)
3334{
3335 Token **tail, *t, *tt;
3336 Token **paste_head;
3337 bool did_paste = false;
3338 char *tmp;
3339
3340 /* Now handle token pasting... */
3341 paste_head = NULL;
3342 tail = head;
3343 while ((t = *tail) && (tt = t->next)) {
3344 switch (t->type) {
3345 case TOK_WHITESPACE:
3346 if (tt->type == TOK_WHITESPACE) {
3347 /* Zap adjacent whitespace tokens */
3348 t->next = delete_Token(tt);
3349 } else {
3350 /* Do not advance paste_head here */
3351 tail = &t->next;
3352 }
3353 break;
3354 case TOK_ID:
3355 case TOK_PREPROC_ID:
3356 case TOK_NUMBER:
3357 case TOK_FLOAT:
3358 {
3359 size_t len = 0;
3360 char *tmp, *p;
3361
3362 while (tt && (tt->type == TOK_ID || tt->type == TOK_PREPROC_ID ||
3363 tt->type == TOK_NUMBER || tt->type == TOK_FLOAT ||
3364 tt->type == TOK_OTHER)) {
3365 len += strlen(tt->text);
3366 tt = tt->next;
3367 }
3368
3369 /* Now tt points to the first token after the potential
3370 paste area... */
3371 if (tt != t->next) {
3372 /* We have at least two tokens... */
3373 len += strlen(t->text);
3374 p = tmp = nasm_malloc(len+1);
3375
3376 while (t != tt) {
3377 strcpy(p, t->text);
3378 p = strchr(p, '\0');
3379 t = delete_Token(t);
3380 }
3381
3382 t = *tail = tokenize(tmp);
3383 nasm_free(tmp);
3384
3385 while (t->next) {
3386 tail = &t->next;
3387 t = t->next;
3388 }
3389 t->next = tt; /* Attach the remaining token chain */
3390
3391 did_paste = true;
3392 }
3393 paste_head = tail;
3394 tail = &t->next;
3395 break;
3396 }
3397 case TOK_PASTE: /* %+ */
3398 if (handle_paste_tokens) {
3399 /* Zap %+ and whitespace tokens to the right */
3400 while (t && (t->type == TOK_WHITESPACE ||
3401 t->type == TOK_PASTE))
3402 t = *tail = delete_Token(t);
3403 if (!paste_head || !t)
3404 break; /* Nothing to paste with */
3405 tail = paste_head;
3406 t = *tail;
3407 tt = t->next;
3408 while (tok_type_(tt, TOK_WHITESPACE))
3409 tt = t->next = delete_Token(tt);
3410
3411 if (tt) {
3412 tmp = nasm_strcat(t->text, tt->text);
3413 delete_Token(t);
3414 tt = delete_Token(tt);
3415 t = *tail = tokenize(tmp);
3416 nasm_free(tmp);
3417 while (t->next) {
3418 tail = &t->next;
3419 t = t->next;
3420 }
3421 t->next = tt; /* Attach the remaining token chain */
3422 did_paste = true;
3423 }
3424 paste_head = tail;
3425 tail = &t->next;
3426 break;
3427 }
3428 /* else fall through */
3429 default:
3430 tail = paste_head = &t->next;
3431 break;
3432 }
3433 }
3434 return did_paste;
3435}
H. Peter Anvin76690a12002-04-30 20:52:49 +00003436/*
3437 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003438 * %-n) and MMacro-local identifiers (%%foo) as well as
3439 * macro indirection (%[...]).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003440 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003441static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003442{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003443 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003444 bool changed = false;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003445
3446 tail = &thead;
3447 thead = NULL;
3448
H. Peter Anvine2c80182005-01-15 22:15:51 +00003449 while (tline) {
3450 if (tline->type == TOK_PREPROC_ID &&
3451 (((tline->text[1] == '+' || tline->text[1] == '-')
3452 && tline->text[2]) || tline->text[1] == '%'
3453 || (tline->text[1] >= '0' && tline->text[1] <= '9'))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003454 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003455 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003456 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003457 unsigned int n;
3458 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003459 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003460
H. Peter Anvine2c80182005-01-15 22:15:51 +00003461 t = tline;
3462 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003463
H. Peter Anvine2c80182005-01-15 22:15:51 +00003464 mac = istk->mstk;
3465 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3466 mac = mac->next_active;
3467 if (!mac)
3468 error(ERR_NONFATAL, "`%s': not in a macro call", t->text);
3469 else
3470 switch (t->text[1]) {
3471 /*
3472 * We have to make a substitution of one of the
3473 * forms %1, %-1, %+1, %%foo, %0.
3474 */
3475 case '0':
3476 type = TOK_NUMBER;
3477 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3478 text = nasm_strdup(tmpbuf);
3479 break;
3480 case '%':
3481 type = TOK_ID;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003482 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvine2c80182005-01-15 22:15:51 +00003483 mac->unique);
3484 text = nasm_strcat(tmpbuf, t->text + 2);
3485 break;
3486 case '-':
3487 n = atoi(t->text + 2) - 1;
3488 if (n >= mac->nparam)
3489 tt = NULL;
3490 else {
3491 if (mac->nparam > 1)
3492 n = (n + mac->rotate) % mac->nparam;
3493 tt = mac->params[n];
3494 }
3495 cc = find_cc(tt);
3496 if (cc == -1) {
3497 error(ERR_NONFATAL,
3498 "macro parameter %d is not a condition code",
3499 n + 1);
3500 text = NULL;
3501 } else {
3502 type = TOK_ID;
3503 if (inverse_ccs[cc] == -1) {
3504 error(ERR_NONFATAL,
3505 "condition code `%s' is not invertible",
3506 conditions[cc]);
3507 text = NULL;
3508 } else
H. Peter Anvin67c63722008-10-26 23:49:00 -07003509 text = nasm_strdup(conditions[inverse_ccs[cc]]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003510 }
3511 break;
3512 case '+':
3513 n = atoi(t->text + 2) - 1;
3514 if (n >= mac->nparam)
3515 tt = NULL;
3516 else {
3517 if (mac->nparam > 1)
3518 n = (n + mac->rotate) % mac->nparam;
3519 tt = mac->params[n];
3520 }
3521 cc = find_cc(tt);
3522 if (cc == -1) {
3523 error(ERR_NONFATAL,
3524 "macro parameter %d is not a condition code",
3525 n + 1);
3526 text = NULL;
3527 } else {
3528 type = TOK_ID;
3529 text = nasm_strdup(conditions[cc]);
3530 }
3531 break;
3532 default:
3533 n = atoi(t->text + 1) - 1;
3534 if (n >= mac->nparam)
3535 tt = NULL;
3536 else {
3537 if (mac->nparam > 1)
3538 n = (n + mac->rotate) % mac->nparam;
3539 tt = mac->params[n];
3540 }
3541 if (tt) {
3542 for (i = 0; i < mac->paramlen[n]; i++) {
3543 *tail = new_Token(NULL, tt->type, tt->text, 0);
3544 tail = &(*tail)->next;
3545 tt = tt->next;
3546 }
3547 }
3548 text = NULL; /* we've done it here */
3549 break;
3550 }
3551 if (!text) {
3552 delete_Token(t);
3553 } else {
3554 *tail = t;
3555 tail = &t->next;
3556 t->type = type;
3557 nasm_free(t->text);
3558 t->text = text;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003559 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003560 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07003561 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003562 continue;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003563 } else if (tline->type == TOK_INDIRECT) {
3564 t = tline;
3565 tline = tline->next;
3566 tt = tokenize(t->text);
3567 tt = expand_mmac_params(tt);
3568 tt = expand_smacro(tt);
3569 *tail = tt;
3570 while (tt) {
3571 tt->a.mac = NULL; /* Necessary? */
3572 tail = &tt->next;
3573 tt = tt->next;
3574 }
3575 delete_Token(t);
H. Peter Anvin6125b622009-04-08 14:02:25 -07003576 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003577 } else {
3578 t = *tail = tline;
3579 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003580 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003581 tail = &t->next;
3582 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003583 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00003584 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07003585
H. Peter Anvind784a082009-04-20 14:01:18 -07003586 if (changed)
H. Peter Anvinba7a0d02009-05-04 10:11:22 -07003587 paste_tokens(&thead, true);
H. Peter Anvin6125b622009-04-08 14:02:25 -07003588
H. Peter Anvin76690a12002-04-30 20:52:49 +00003589 return thead;
3590}
3591
3592/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003593 * Expand all single-line macro calls made in the given line.
3594 * Return the expanded version of the line. The original is deemed
3595 * to be destroyed in the process. (In reality we'll just move
3596 * Tokens from input to output a lot of the time, rather than
3597 * actually bothering to destroy and replicate.)
3598 */
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003599#define DEADMAN_LIMIT (1 << 20)
3600
H. Peter Anvine2c80182005-01-15 22:15:51 +00003601static Token *expand_smacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003602{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003603 Token *t, *tt, *mstart, **tail, *thead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003604 struct hash_table *smtbl;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003605 SMacro *head = NULL, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003606 Token **params;
3607 int *paramsize;
H. Peter Anvin25a99342007-09-22 17:45:45 -07003608 unsigned int nparam, sparam;
H. Peter Anvind784a082009-04-20 14:01:18 -07003609 int brackets;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003610 Token *org_tline = tline;
3611 Context *ctx;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003612 const char *mname;
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003613 int deadman = DEADMAN_LIMIT;
H. Peter Anvind784a082009-04-20 14:01:18 -07003614 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003615
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003616 /*
3617 * Trick: we should avoid changing the start token pointer since it can
3618 * be contained in "next" field of other token. Because of this
3619 * we allocate a copy of first token and work with it; at the end of
3620 * routine we copy it back
3621 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003622 if (org_tline) {
3623 tline =
3624 new_Token(org_tline->next, org_tline->type, org_tline->text,
3625 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003626 tline->a.mac = org_tline->a.mac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003627 nasm_free(org_tline->text);
3628 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003629 }
3630
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003631again:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003632 tail = &thead;
3633 thead = NULL;
H. Peter Anvind784a082009-04-20 14:01:18 -07003634 expanded = false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003635
H. Peter Anvine2c80182005-01-15 22:15:51 +00003636 while (tline) { /* main token loop */
H. Peter Anvin2a15e692007-11-19 13:14:59 -08003637 if (!--deadman) {
H. Peter Anvincb1cf592007-11-19 12:26:50 -08003638 error(ERR_NONFATAL, "interminable macro recursion");
3639 break;
3640 }
3641
H. Peter Anvine2c80182005-01-15 22:15:51 +00003642 if ((mname = tline->text)) {
3643 /* if this token is a local macro, look in local context */
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003644 if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID)
3645 ctx = get_ctx(mname, &mname, true);
3646 else
3647 ctx = NULL;
3648 smtbl = ctx ? &ctx->localmac : &smacros;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003649 head = (SMacro *) hash_findix(smtbl, mname);
H. Peter Anvin072771e2008-05-22 13:17:51 -07003650
H. Peter Anvine2c80182005-01-15 22:15:51 +00003651 /*
3652 * We've hit an identifier. As in is_mmacro below, we first
3653 * check whether the identifier is a single-line macro at
3654 * all, then think about checking for parameters if
3655 * necessary.
3656 */
H. Peter Anvin97a23472007-09-16 17:57:25 -07003657 for (m = head; m; m = m->next)
3658 if (!mstrcmp(m->name, mname, m->casesense))
3659 break;
3660 if (m) {
3661 mstart = tline;
3662 params = NULL;
3663 paramsize = NULL;
3664 if (m->nparam == 0) {
3665 /*
3666 * Simple case: the macro is parameterless. Discard the
3667 * one token that the macro call took, and push the
3668 * expansion back on the to-do stack.
3669 */
3670 if (!m->expansion) {
3671 if (!strcmp("__FILE__", m->name)) {
3672 int32_t num = 0;
H. Peter Anvin932de6c2008-07-31 18:46:11 -07003673 char *file = NULL;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003674 src_get(&num, &file);
3675 tline->text = nasm_quote(file, strlen(file));
H. Peter Anvin97a23472007-09-16 17:57:25 -07003676 tline->type = TOK_STRING;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003677 nasm_free(file);
H. Peter Anvin97a23472007-09-16 17:57:25 -07003678 continue;
3679 }
3680 if (!strcmp("__LINE__", m->name)) {
3681 nasm_free(tline->text);
3682 make_tok_num(tline, src_get_linnum());
3683 continue;
3684 }
3685 if (!strcmp("__BITS__", m->name)) {
3686 nasm_free(tline->text);
3687 make_tok_num(tline, globalbits);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003688 continue;
H. Peter Anvin97a23472007-09-16 17:57:25 -07003689 }
3690 tline = delete_Token(tline);
3691 continue;
3692 }
3693 } else {
3694 /*
3695 * Complicated case: at least one macro with this name
H. Peter Anvine2c80182005-01-15 22:15:51 +00003696 * exists and takes parameters. We must find the
3697 * parameters in the call, count them, find the SMacro
3698 * that corresponds to that form of the macro call, and
3699 * substitute for the parameters when we expand. What a
3700 * pain.
3701 */
3702 /*tline = tline->next;
3703 skip_white_(tline); */
3704 do {
3705 t = tline->next;
3706 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003707 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003708 t->text = NULL;
3709 t = tline->next = delete_Token(t);
3710 }
3711 tline = t;
3712 } while (tok_type_(tline, TOK_WHITESPACE));
3713 if (!tok_is_(tline, "(")) {
3714 /*
3715 * This macro wasn't called with parameters: ignore
3716 * the call. (Behaviour borrowed from gnu cpp.)
3717 */
3718 tline = mstart;
3719 m = NULL;
3720 } else {
3721 int paren = 0;
3722 int white = 0;
3723 brackets = 0;
3724 nparam = 0;
3725 sparam = PARAM_DELTA;
3726 params = nasm_malloc(sparam * sizeof(Token *));
3727 params[0] = tline->next;
3728 paramsize = nasm_malloc(sparam * sizeof(int));
3729 paramsize[0] = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003730 while (true) { /* parameter loop */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003731 /*
3732 * For some unusual expansions
3733 * which concatenates function call
3734 */
3735 t = tline->next;
3736 while (tok_type_(t, TOK_SMAC_END)) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003737 t->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003738 t->text = NULL;
3739 t = tline->next = delete_Token(t);
3740 }
3741 tline = t;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003742
H. Peter Anvine2c80182005-01-15 22:15:51 +00003743 if (!tline) {
3744 error(ERR_NONFATAL,
3745 "macro call expects terminating `)'");
3746 break;
3747 }
3748 if (tline->type == TOK_WHITESPACE
3749 && brackets <= 0) {
3750 if (paramsize[nparam])
3751 white++;
3752 else
3753 params[nparam] = tline->next;
3754 continue; /* parameter loop */
3755 }
3756 if (tline->type == TOK_OTHER
3757 && tline->text[1] == 0) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003758 char ch = tline->text[0];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003759 if (ch == ',' && !paren && brackets <= 0) {
3760 if (++nparam >= sparam) {
3761 sparam += PARAM_DELTA;
3762 params = nasm_realloc(params,
3763 sparam *
3764 sizeof(Token
3765 *));
3766 paramsize =
3767 nasm_realloc(paramsize,
3768 sparam *
3769 sizeof(int));
3770 }
3771 params[nparam] = tline->next;
3772 paramsize[nparam] = 0;
3773 white = 0;
3774 continue; /* parameter loop */
3775 }
3776 if (ch == '{' &&
3777 (brackets > 0 || (brackets == 0 &&
3778 !paramsize[nparam])))
3779 {
3780 if (!(brackets++)) {
3781 params[nparam] = tline->next;
3782 continue; /* parameter loop */
3783 }
3784 }
3785 if (ch == '}' && brackets > 0)
3786 if (--brackets == 0) {
3787 brackets = -1;
3788 continue; /* parameter loop */
3789 }
3790 if (ch == '(' && !brackets)
3791 paren++;
3792 if (ch == ')' && brackets <= 0)
3793 if (--paren < 0)
3794 break;
3795 }
3796 if (brackets < 0) {
3797 brackets = 0;
3798 error(ERR_NONFATAL, "braces do not "
3799 "enclose all of macro parameter");
3800 }
3801 paramsize[nparam] += white + 1;
3802 white = 0;
3803 } /* parameter loop */
3804 nparam++;
3805 while (m && (m->nparam != nparam ||
3806 mstrcmp(m->name, mname,
3807 m->casesense)))
3808 m = m->next;
3809 if (!m)
H. Peter Anvin917a3492008-09-24 09:14:49 -07003810 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00003811 "macro `%s' exists, "
3812 "but not taking %d parameters",
3813 mstart->text, nparam);
3814 }
3815 }
3816 if (m && m->in_progress)
3817 m = NULL;
3818 if (!m) { /* in progess or didn't find '(' or wrong nparam */
H. Peter Anvin70653092007-10-19 14:42:29 -07003819 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00003820 * Design question: should we handle !tline, which
3821 * indicates missing ')' here, or expand those
3822 * macros anyway, which requires the (t) test a few
H. Peter Anvin70653092007-10-19 14:42:29 -07003823 * lines down?
H. Peter Anvine2c80182005-01-15 22:15:51 +00003824 */
3825 nasm_free(params);
3826 nasm_free(paramsize);
3827 tline = mstart;
3828 } else {
3829 /*
3830 * Expand the macro: we are placed on the last token of the
3831 * call, so that we can easily split the call from the
3832 * following tokens. We also start by pushing an SMAC_END
3833 * token for the cycle removal.
3834 */
3835 t = tline;
3836 if (t) {
3837 tline = t->next;
3838 t->next = NULL;
3839 }
3840 tt = new_Token(tline, TOK_SMAC_END, NULL, 0);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003841 tt->a.mac = m;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003842 m->in_progress = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003843 tline = tt;
3844 for (t = m->expansion; t; t = t->next) {
3845 if (t->type >= TOK_SMAC_PARAM) {
3846 Token *pcopy = tline, **ptail = &pcopy;
3847 Token *ttt, *pt;
3848 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003849
H. Peter Anvine2c80182005-01-15 22:15:51 +00003850 ttt = params[t->type - TOK_SMAC_PARAM];
3851 for (i = paramsize[t->type - TOK_SMAC_PARAM];
3852 --i >= 0;) {
3853 pt = *ptail =
3854 new_Token(tline, ttt->type, ttt->text,
3855 0);
3856 ptail = &pt->next;
3857 ttt = ttt->next;
3858 }
3859 tline = pcopy;
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -07003860 } else if (t->type == TOK_PREPROC_Q) {
3861 tt = new_Token(tline, TOK_ID, mname, 0);
3862 tline = tt;
3863 } else if (t->type == TOK_PREPROC_QQ) {
3864 tt = new_Token(tline, TOK_ID, m->name, 0);
3865 tline = tt;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003866 } else {
3867 tt = new_Token(tline, t->type, t->text, 0);
3868 tline = tt;
3869 }
3870 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003871
H. Peter Anvine2c80182005-01-15 22:15:51 +00003872 /*
3873 * Having done that, get rid of the macro call, and clean
3874 * up the parameters.
3875 */
3876 nasm_free(params);
3877 nasm_free(paramsize);
3878 free_tlist(mstart);
H. Peter Anvind784a082009-04-20 14:01:18 -07003879 expanded = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003880 continue; /* main token loop */
3881 }
3882 }
3883 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003884
H. Peter Anvine2c80182005-01-15 22:15:51 +00003885 if (tline->type == TOK_SMAC_END) {
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003886 tline->a.mac->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003887 tline = delete_Token(tline);
3888 } else {
3889 t = *tail = tline;
3890 tline = tline->next;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003891 t->a.mac = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003892 t->next = NULL;
3893 tail = &t->next;
3894 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003895 }
3896
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003897 /*
3898 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00003899 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003900 * TOK_IDs should be concatenated.
3901 * Also we look for %+ tokens and concatenate the tokens before and after
3902 * them (without white spaces in between).
3903 */
H. Peter Anvind784a082009-04-20 14:01:18 -07003904 if (expanded && paste_tokens(&thead, true)) {
3905 /* If we concatenated something, re-scan the line for macros */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003906 tline = thead;
3907 goto again;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003908 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003909
H. Peter Anvine2c80182005-01-15 22:15:51 +00003910 if (org_tline) {
3911 if (thead) {
3912 *org_tline = *thead;
3913 /* since we just gave text to org_line, don't free it */
3914 thead->text = NULL;
3915 delete_Token(thead);
3916 } else {
3917 /* the expression expanded to empty line;
3918 we can't return NULL for some reasons
3919 we just set the line to a single WHITESPACE token. */
3920 memset(org_tline, 0, sizeof(*org_tline));
3921 org_tline->text = NULL;
3922 org_tline->type = TOK_WHITESPACE;
3923 }
3924 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003925 }
3926
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003927 return thead;
3928}
3929
3930/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003931 * Similar to expand_smacro but used exclusively with macro identifiers
3932 * right before they are fetched in. The reason is that there can be
3933 * identifiers consisting of several subparts. We consider that if there
3934 * are more than one element forming the name, user wants a expansion,
3935 * otherwise it will be left as-is. Example:
3936 *
3937 * %define %$abc cde
3938 *
3939 * the identifier %$abc will be left as-is so that the handler for %define
3940 * will suck it and define the corresponding value. Other case:
3941 *
3942 * %define _%$abc cde
3943 *
3944 * In this case user wants name to be expanded *before* %define starts
3945 * working, so we'll expand %$abc into something (if it has a value;
3946 * otherwise it will be left as-is) then concatenate all successive
3947 * PP_IDs into one.
3948 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003949static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003950{
3951 Token *cur, *oldnext = NULL;
3952
H. Peter Anvin734b1882002-04-30 21:01:08 +00003953 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003954 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003955
3956 cur = tline;
3957 while (cur->next &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003958 (cur->next->type == TOK_ID ||
3959 cur->next->type == TOK_PREPROC_ID
3960 || cur->next->type == TOK_NUMBER))
3961 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003962
3963 /* If identifier consists of just one token, don't expand */
3964 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003965 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003966
H. Peter Anvine2c80182005-01-15 22:15:51 +00003967 if (cur) {
3968 oldnext = cur->next; /* Detach the tail past identifier */
3969 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003970 }
3971
H. Peter Anvin734b1882002-04-30 21:01:08 +00003972 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003973
H. Peter Anvine2c80182005-01-15 22:15:51 +00003974 if (cur) {
3975 /* expand_smacro possibly changhed tline; re-scan for EOL */
3976 cur = tline;
3977 while (cur && cur->next)
3978 cur = cur->next;
3979 if (cur)
3980 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00003981 }
3982
3983 return tline;
3984}
3985
3986/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003987 * Determine whether the given line constitutes a multi-line macro
3988 * call, and return the MMacro structure called if so. Doesn't have
3989 * to check for an initial label - that's taken care of in
3990 * expand_mmacro - but must check numbers of parameters. Guaranteed
3991 * to be called with tline->type == TOK_ID, so the putative macro
3992 * name is easy to find.
3993 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003994static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003995{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003996 MMacro *head, *m;
3997 Token **params;
3998 int nparam;
3999
H. Peter Anvin166c2472008-05-28 12:28:58 -07004000 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004001
4002 /*
4003 * Efficiency: first we see if any macro exists with the given
4004 * name. If not, we can return NULL immediately. _Then_ we
4005 * count the parameters, and then we look further along the
4006 * list if necessary to find the proper MMacro.
4007 */
4008 for (m = head; m; m = m->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004009 if (!mstrcmp(m->name, tline->text, m->casesense))
4010 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004011 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004012 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004013
4014 /*
4015 * OK, we have a potential macro. Count and demarcate the
4016 * parameters.
4017 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004018 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004019
4020 /*
4021 * So we know how many parameters we've got. Find the MMacro
4022 * structure that handles this number.
4023 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004024 while (m) {
4025 if (m->nparam_min <= nparam
4026 && (m->plus || nparam <= m->nparam_max)) {
4027 /*
4028 * This one is right. Just check if cycle removal
4029 * prohibits us using it before we actually celebrate...
4030 */
4031 if (m->in_progress) {
H. Peter Anvineba20a72002-04-30 20:53:55 +00004032#if 0
H. Peter Anvine2c80182005-01-15 22:15:51 +00004033 error(ERR_NONFATAL,
4034 "self-reference in multi-line macro `%s'", m->name);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004035#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00004036 nasm_free(params);
4037 return NULL;
4038 }
4039 /*
4040 * It's right, and we can use it. Add its default
4041 * parameters to the end of our list if necessary.
4042 */
4043 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
4044 params =
4045 nasm_realloc(params,
4046 ((m->nparam_min + m->ndefs +
4047 1) * sizeof(*params)));
4048 while (nparam < m->nparam_min + m->ndefs) {
4049 params[nparam] = m->defaults[nparam - m->nparam_min];
4050 nparam++;
4051 }
4052 }
4053 /*
4054 * If we've gone over the maximum parameter count (and
4055 * we're in Plus mode), ignore parameters beyond
4056 * nparam_max.
4057 */
4058 if (m->plus && nparam > m->nparam_max)
4059 nparam = m->nparam_max;
4060 /*
4061 * Then terminate the parameter list, and leave.
4062 */
4063 if (!params) { /* need this special case */
4064 params = nasm_malloc(sizeof(*params));
4065 nparam = 0;
4066 }
4067 params[nparam] = NULL;
4068 *params_array = params;
4069 return m;
4070 }
4071 /*
4072 * This one wasn't right: look for the next one with the
4073 * same name.
4074 */
4075 for (m = m->next; m; m = m->next)
4076 if (!mstrcmp(m->name, tline->text, m->casesense))
4077 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004078 }
4079
4080 /*
4081 * After all that, we didn't find one with the right number of
4082 * parameters. Issue a warning, and fail to expand the macro.
4083 */
H. Peter Anvin917a3492008-09-24 09:14:49 -07004084 error(ERR_WARNING|ERR_PASS1|ERR_WARN_MNP,
H. Peter Anvine2c80182005-01-15 22:15:51 +00004085 "macro `%s' exists, but not taking %d parameters",
4086 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004087 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004088 return NULL;
4089}
4090
4091/*
4092 * Expand the multi-line macro call made by the given line, if
4093 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvineba20a72002-04-30 20:53:55 +00004094 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004095 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004096static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004097{
4098 Token *startline = tline;
4099 Token *label = NULL;
4100 int dont_prepend = 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004101 Token **params, *t, *mtok, *tt;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004102 MMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004103 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004104 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004105 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004106
4107 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004108 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004109 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004110 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004111 return 0;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004112 mtok = t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004113 m = is_mmacro(t, &params);
H. Peter Anvinc751e862008-06-09 10:18:45 -07004114 if (m) {
4115 mname = t->text;
4116 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004117 Token *last;
4118 /*
4119 * We have an id which isn't a macro call. We'll assume
4120 * it might be a label; we'll also check to see if a
4121 * colon follows it. Then, if there's another id after
4122 * that lot, we'll check it again for macro-hood.
4123 */
4124 label = last = t;
4125 t = t->next;
4126 if (tok_type_(t, TOK_WHITESPACE))
4127 last = t, t = t->next;
4128 if (tok_is_(t, ":")) {
4129 dont_prepend = 1;
4130 last = t, t = t->next;
4131 if (tok_type_(t, TOK_WHITESPACE))
4132 last = t, t = t->next;
4133 }
4134 if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, &params)) == NULL)
4135 return 0;
4136 last->next = NULL;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004137 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004138 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004139 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004140
4141 /*
4142 * Fix up the parameters: this involves stripping leading and
4143 * trailing whitespace, then stripping braces if they are
4144 * present.
4145 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004146 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004147 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004148
H. Peter Anvine2c80182005-01-15 22:15:51 +00004149 for (i = 0; params[i]; i++) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004150 int brace = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004151 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004152
H. Peter Anvine2c80182005-01-15 22:15:51 +00004153 t = params[i];
4154 skip_white_(t);
4155 if (tok_is_(t, "{"))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004156 t = t->next, brace = true, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004157 params[i] = t;
4158 paramlen[i] = 0;
4159 while (t) {
4160 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4161 break; /* ... because we have hit a comma */
4162 if (comma && t->type == TOK_WHITESPACE
4163 && tok_is_(t->next, ","))
4164 break; /* ... or a space then a comma */
4165 if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}"))
4166 break; /* ... or a brace */
4167 t = t->next;
4168 paramlen[i]++;
4169 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004170 }
4171
4172 /*
4173 * OK, we have a MMacro structure together with a set of
4174 * parameters. We must now go through the expansion and push
H. Peter Anvin76690a12002-04-30 20:52:49 +00004175 * copies of each Line on to istk->expansion. Substitution of
4176 * parameter tokens and macro-local tokens doesn't get done
4177 * until the single-line macro substitution process; this is
4178 * because delaying them allows us to change the semantics
4179 * later through %rotate.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004180 *
H. Peter Anvin76690a12002-04-30 20:52:49 +00004181 * First, push an end marker on to istk->expansion, mark this
4182 * macro as in progress, and set up its invocation-specific
4183 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004184 */
4185 ll = nasm_malloc(sizeof(Line));
4186 ll->next = istk->expansion;
4187 ll->finishes = m;
4188 ll->first = NULL;
4189 istk->expansion = ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004190
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004191 m->in_progress = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004192 m->params = params;
4193 m->iline = tline;
4194 m->nparam = nparam;
4195 m->rotate = 0;
4196 m->paramlen = paramlen;
4197 m->unique = unique++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004198 m->lineno = 0;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004199
4200 m->next_active = istk->mstk;
4201 istk->mstk = m;
4202
H. Peter Anvine2c80182005-01-15 22:15:51 +00004203 for (l = m->expansion; l; l = l->next) {
4204 Token **tail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004205
H. Peter Anvine2c80182005-01-15 22:15:51 +00004206 ll = nasm_malloc(sizeof(Line));
4207 ll->finishes = NULL;
4208 ll->next = istk->expansion;
4209 istk->expansion = ll;
4210 tail = &ll->first;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004211
H. Peter Anvine2c80182005-01-15 22:15:51 +00004212 for (t = l->first; t; t = t->next) {
4213 Token *x = t;
H. Peter Anvince2233b2008-05-25 21:57:00 -07004214 switch (t->type) {
4215 case TOK_PREPROC_Q:
H. Peter Anvinc751e862008-06-09 10:18:45 -07004216 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004217 break;
4218 case TOK_PREPROC_QQ:
4219 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4220 break;
4221 case TOK_PREPROC_ID:
4222 if (t->text[1] == '0' && t->text[2] == '0') {
4223 dont_prepend = -1;
4224 x = label;
4225 if (!x)
4226 continue;
4227 }
4228 /* fall through */
4229 default:
4230 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4231 break;
H. Peter Anvin166c2472008-05-28 12:28:58 -07004232 }
H. Peter Anvince2233b2008-05-25 21:57:00 -07004233 tail = &tt->next;
4234 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004235 *tail = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004236 }
4237
4238 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004239 * If we had a label, push it on as the first line of
4240 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004241 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004242 if (label) {
4243 if (dont_prepend < 0)
4244 free_tlist(startline);
4245 else {
4246 ll = nasm_malloc(sizeof(Line));
4247 ll->finishes = NULL;
4248 ll->next = istk->expansion;
4249 istk->expansion = ll;
4250 ll->first = startline;
4251 if (!dont_prepend) {
4252 while (label->next)
4253 label = label->next;
4254 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
4255 }
4256 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004257 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004258
H. Peter Anvin734b1882002-04-30 21:01:08 +00004259 list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004260
H. Peter Anvineba20a72002-04-30 20:53:55 +00004261 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004262}
4263
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004264/* The function that actually does the error reporting */
4265static void verror(int severity, const char *fmt, va_list arg)
4266{
4267 char buff[1024];
4268
4269 vsnprintf(buff, sizeof(buff), fmt, arg);
4270
4271 if (istk && istk->mstk && istk->mstk->name)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004272 _error(severity, "(%s:%d) %s", istk->mstk->name,
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004273 istk->mstk->lineno, buff);
4274 else
H. Peter Anvin917a3492008-09-24 09:14:49 -07004275 _error(severity, "%s", buff);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004276}
4277
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004278/*
4279 * Since preprocessor always operate only on the line that didn't
H. Peter Anvin917a3492008-09-24 09:14:49 -07004280 * arrived yet, we should always use ERR_OFFBY1.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004281 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004282static void error(int severity, const char *fmt, ...)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004283{
4284 va_list arg;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004285
4286 /* If we're in a dead branch of IF or something like it, ignore the error */
H. Peter Anvin77ba0c62002-05-14 03:18:53 +00004287 if (istk && istk->conds && !emitting(istk->conds->state))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004288 return;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004289
H. Peter Anvin734b1882002-04-30 21:01:08 +00004290 va_start(arg, fmt);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004291 verror(severity, fmt, arg);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004292 va_end(arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004293}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004294
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004295/*
4296 * Because %else etc are evaluated in the state context
4297 * of the previous branch, errors might get lost with error():
4298 * %if 0 ... %else trailing garbage ... %endif
4299 * So %else etc should report errors with this function.
4300 */
4301static void error_precond(int severity, const char *fmt, ...)
4302{
4303 va_list arg;
4304
4305 /* Only ignore the error if it's really in a dead branch */
4306 if (istk && istk->conds && istk->conds->state == COND_NEVER)
4307 return;
4308
4309 va_start(arg, fmt);
4310 verror(severity, fmt, arg);
4311 va_end(arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004312}
4313
H. Peter Anvin734b1882002-04-30 21:01:08 +00004314static void
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004315pp_reset(char *file, int apass, efunc errfunc, evalfunc eval,
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004316 ListGen * listgen, StrList **deplist)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004317{
H. Peter Anvin7383b402008-09-24 10:20:40 -07004318 Token *t;
4319
H. Peter Anvin99941bf2002-05-14 17:44:03 +00004320 _error = errfunc;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004321 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004322 istk = nasm_malloc(sizeof(Include));
4323 istk->next = NULL;
4324 istk->conds = NULL;
4325 istk->expansion = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004326 istk->mstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004327 istk->fp = fopen(file, "r");
H. Peter Anvineba20a72002-04-30 20:53:55 +00004328 istk->fname = NULL;
4329 src_set_fname(nasm_strdup(file));
4330 src_set_linnum(0);
4331 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004332 if (!istk->fp)
H. Peter Anvin917a3492008-09-24 09:14:49 -07004333 error(ERR_FATAL|ERR_NOFILE, "unable to open input file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +00004334 file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004335 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07004336 nested_mac_count = 0;
4337 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07004338 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004339 unique = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004340 if (tasm_compatible_mode) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004341 stdmacpos = nasm_stdmac;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004342 } else {
H. Peter Anvina4835d42008-05-20 14:21:29 -07004343 stdmacpos = nasm_stdmac_after_tasm;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004344 }
H. Peter Anvind2456592008-06-19 15:04:18 -07004345 any_extrastdmac = extrastdmac && *extrastdmac;
4346 do_predef = true;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004347 list = listgen;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004348 evaluate = eval;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004349
4350 /*
4351 * 0 for dependencies, 1 for preparatory passes, 2 for final pass.
4352 * The caller, however, will also pass in 3 for preprocess-only so
4353 * we can set __PASS__ accordingly.
4354 */
4355 pass = apass > 2 ? 2 : apass;
4356
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07004357 dephead = deptail = deplist;
4358 if (deplist) {
4359 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
4360 sl->next = NULL;
4361 strcpy(sl->str, file);
4362 *deptail = sl;
4363 deptail = &sl->next;
4364 }
H. Peter Anvin7383b402008-09-24 10:20:40 -07004365
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004366 /*
4367 * Define the __PASS__ macro. This is defined here unlike
4368 * all the other builtins, because it is special -- it varies between
4369 * passes.
4370 */
H. Peter Anvin7383b402008-09-24 10:20:40 -07004371 t = nasm_malloc(sizeof(*t));
4372 t->next = NULL;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07004373 make_tok_num(t, apass);
H. Peter Anvin7383b402008-09-24 10:20:40 -07004374 t->a.mac = NULL;
4375 define_smacro(NULL, "__PASS__", true, 0, t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004376}
4377
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004378static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004379{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004380 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004381 Token *tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004382
H. Peter Anvine2c80182005-01-15 22:15:51 +00004383 while (1) {
4384 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004385 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00004386 * buffer or from the input file.
4387 */
4388 tline = NULL;
4389 while (istk->expansion && istk->expansion->finishes) {
4390 Line *l = istk->expansion;
4391 if (!l->finishes->name && l->finishes->in_progress > 1) {
4392 Line *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004393
H. Peter Anvine2c80182005-01-15 22:15:51 +00004394 /*
4395 * This is a macro-end marker for a macro with no
4396 * name, which means it's not really a macro at all
4397 * but a %rep block, and the `in_progress' field is
4398 * more than 1, meaning that we still need to
4399 * repeat. (1 means the natural last repetition; 0
4400 * means termination by %exitrep.) We have
4401 * therefore expanded up to the %endrep, and must
4402 * push the whole block on to the expansion buffer
4403 * again. We don't bother to remove the macro-end
4404 * marker: we'd only have to generate another one
4405 * if we did.
4406 */
4407 l->finishes->in_progress--;
4408 for (l = l->finishes->expansion; l; l = l->next) {
4409 Token *t, *tt, **tail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004410
H. Peter Anvine2c80182005-01-15 22:15:51 +00004411 ll = nasm_malloc(sizeof(Line));
4412 ll->next = istk->expansion;
4413 ll->finishes = NULL;
4414 ll->first = NULL;
4415 tail = &ll->first;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004416
H. Peter Anvine2c80182005-01-15 22:15:51 +00004417 for (t = l->first; t; t = t->next) {
4418 if (t->text || t->type == TOK_WHITESPACE) {
4419 tt = *tail =
4420 new_Token(NULL, t->type, t->text, 0);
4421 tail = &tt->next;
4422 }
4423 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004424
H. Peter Anvine2c80182005-01-15 22:15:51 +00004425 istk->expansion = ll;
4426 }
4427 } else {
4428 /*
4429 * Check whether a `%rep' was started and not ended
4430 * within this macro expansion. This can happen and
4431 * should be detected. It's a fatal error because
4432 * I'm too confused to work out how to recover
4433 * sensibly from it.
4434 */
4435 if (defining) {
4436 if (defining->name)
4437 error(ERR_PANIC,
4438 "defining with name in expansion");
4439 else if (istk->mstk->name)
4440 error(ERR_FATAL,
4441 "`%%rep' without `%%endrep' within"
4442 " expansion of macro `%s'",
4443 istk->mstk->name);
4444 }
H. Peter Anvin87bc6192002-04-30 20:53:16 +00004445
H. Peter Anvine2c80182005-01-15 22:15:51 +00004446 /*
4447 * FIXME: investigate the relationship at this point between
4448 * istk->mstk and l->finishes
4449 */
4450 {
4451 MMacro *m = istk->mstk;
4452 istk->mstk = m->next_active;
4453 if (m->name) {
4454 /*
4455 * This was a real macro call, not a %rep, and
4456 * therefore the parameter information needs to
4457 * be freed.
4458 */
4459 nasm_free(m->params);
4460 free_tlist(m->iline);
4461 nasm_free(m->paramlen);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004462 l->finishes->in_progress = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004463 } else
4464 free_mmacro(m);
4465 }
4466 istk->expansion = l->next;
4467 nasm_free(l);
4468 list->downlevel(LIST_MACRO);
4469 }
4470 }
4471 while (1) { /* until we get a line we can use */
H. Peter Anvineba20a72002-04-30 20:53:55 +00004472
H. Peter Anvine2c80182005-01-15 22:15:51 +00004473 if (istk->expansion) { /* from a macro expansion */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004474 char *p;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004475 Line *l = istk->expansion;
4476 if (istk->mstk)
4477 istk->mstk->lineno++;
4478 tline = l->first;
4479 istk->expansion = l->next;
4480 nasm_free(l);
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004481 p = detoken(tline, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004482 list->line(LIST_MACRO, p);
4483 nasm_free(p);
4484 break;
4485 }
4486 line = read_line();
4487 if (line) { /* from the current input file */
4488 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004489 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004490 nasm_free(line);
4491 break;
4492 }
4493 /*
4494 * The current file has ended; work down the istk
4495 */
4496 {
4497 Include *i = istk;
4498 fclose(i->fp);
4499 if (i->conds)
4500 error(ERR_FATAL,
4501 "expected `%%endif' before end of file");
4502 /* only set line and file name if there's a next node */
4503 if (i->next) {
4504 src_set_linnum(i->lineno);
4505 nasm_free(src_set_fname(i->fname));
H. Peter Anvin86877b22008-06-20 15:55:45 -07004506 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004507 istk = i->next;
4508 list->downlevel(LIST_INCLUDE);
4509 nasm_free(i);
4510 if (!istk)
4511 return NULL;
Victor van den Elzen4c9d6222008-10-01 13:08:50 +02004512 if (istk->expansion && istk->expansion->finishes)
4513 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004514 }
4515 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004516
H. Peter Anvine2c80182005-01-15 22:15:51 +00004517 /*
4518 * We must expand MMacro parameters and MMacro-local labels
4519 * _before_ we plunge into directive processing, to cope
4520 * with things like `%define something %1' such as STRUC
4521 * uses. Unless we're _defining_ a MMacro, in which case
4522 * those tokens should be left alone to go into the
4523 * definition; and unless we're in a non-emitting
4524 * condition, in which case we don't want to meddle with
4525 * anything.
4526 */
Charles Crayned4200be2008-07-12 16:42:33 -07004527 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin992fe752008-10-19 15:45:05 -07004528 && !(istk->mstk && !istk->mstk->in_progress)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004529 tline = expand_mmac_params(tline);
H. Peter Anvin992fe752008-10-19 15:45:05 -07004530 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004531
H. Peter Anvine2c80182005-01-15 22:15:51 +00004532 /*
4533 * Check the line to see if it's a preprocessor directive.
4534 */
4535 if (do_directive(tline) == DIRECTIVE_FOUND) {
4536 continue;
4537 } else if (defining) {
4538 /*
4539 * We're defining a multi-line macro. We emit nothing
4540 * at all, and just
Keith Kaniosb7a89542007-04-12 02:40:54 +00004541 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004542 */
4543 Line *l = nasm_malloc(sizeof(Line));
4544 l->next = defining->expansion;
4545 l->first = tline;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004546 l->finishes = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004547 defining->expansion = l;
4548 continue;
4549 } else if (istk->conds && !emitting(istk->conds->state)) {
4550 /*
4551 * We're in a non-emitting branch of a condition block.
4552 * Emit nothing at all, not even a blank line: when we
4553 * emerge from the condition we'll give a line-number
4554 * directive so we keep our place correctly.
4555 */
4556 free_tlist(tline);
4557 continue;
4558 } else if (istk->mstk && !istk->mstk->in_progress) {
4559 /*
4560 * We're in a %rep block which has been terminated, so
4561 * we're walking through to the %endrep without
4562 * emitting anything. Emit nothing at all, not even a
4563 * blank line: when we emerge from the %rep block we'll
4564 * give a line-number directive so we keep our place
4565 * correctly.
4566 */
4567 free_tlist(tline);
4568 continue;
4569 } else {
4570 tline = expand_smacro(tline);
4571 if (!expand_mmacro(tline)) {
4572 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00004573 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00004574 */
H. Peter Anvin6867acc2007-10-10 14:58:45 -07004575 line = detoken(tline, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004576 free_tlist(tline);
4577 break;
4578 } else {
4579 continue; /* expand_mmacro calls free_tlist */
4580 }
4581 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004582 }
4583
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004584 return line;
4585}
4586
H. Peter Anvine2c80182005-01-15 22:15:51 +00004587static void pp_cleanup(int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004588{
H. Peter Anvine2c80182005-01-15 22:15:51 +00004589 if (defining) {
Victor van den Elzen8f1120f2008-07-16 13:41:37 +02004590 if(defining->name) {
4591 error(ERR_NONFATAL,
4592 "end of file while still defining macro `%s'",
4593 defining->name);
4594 } else {
4595 error(ERR_NONFATAL, "end of file while still in %%rep");
4596 }
4597
H. Peter Anvine2c80182005-01-15 22:15:51 +00004598 free_mmacro(defining);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004599 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004600 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004601 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07004602 free_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00004603 while (istk) {
4604 Include *i = istk;
4605 istk = istk->next;
4606 fclose(i->fp);
4607 nasm_free(i->fname);
4608 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004609 }
4610 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004611 ctx_pop();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004612 nasm_free(src_set_fname(NULL));
H. Peter Anvine2c80182005-01-15 22:15:51 +00004613 if (pass == 0) {
H. Peter Anvin86877b22008-06-20 15:55:45 -07004614 IncPath *i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004615 free_llist(predef);
4616 delete_Blocks();
H. Peter Anvin86877b22008-06-20 15:55:45 -07004617 while ((i = ipath)) {
4618 ipath = i->next;
4619 if (i->path)
4620 nasm_free(i->path);
4621 nasm_free(i);
4622 }
H. Peter Anvin11dfa1a2008-07-02 18:11:04 -07004623 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004624}
4625
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004626void pp_include_path(char *path)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004627{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004628 IncPath *i;
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004629
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004630 i = nasm_malloc(sizeof(IncPath));
H. Peter Anvin37a321f2007-09-24 13:41:58 -07004631 i->path = path ? nasm_strdup(path) : NULL;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004632 i->next = NULL;
4633
H. Peter Anvine2c80182005-01-15 22:15:51 +00004634 if (ipath != NULL) {
4635 IncPath *j = ipath;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004636 while (j->next != NULL)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004637 j = j->next;
4638 j->next = i;
4639 } else {
4640 ipath = i;
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004641 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004642}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00004643
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004644void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004645{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004646 Token *inc, *space, *name;
4647 Line *l;
4648
H. Peter Anvin734b1882002-04-30 21:01:08 +00004649 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
4650 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
4651 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004652
4653 l = nasm_malloc(sizeof(Line));
4654 l->next = predef;
4655 l->first = inc;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004656 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004657 predef = l;
4658}
4659
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004660void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004661{
4662 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004663 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004664 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004665
4666 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00004667 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4668 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004669 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004670 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00004671 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004672 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004673 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004674
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004675 l = nasm_malloc(sizeof(Line));
4676 l->next = predef;
4677 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004678 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004679 predef = l;
4680}
4681
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004682void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00004683{
4684 Token *def, *space;
4685 Line *l;
4686
H. Peter Anvin734b1882002-04-30 21:01:08 +00004687 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4688 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00004689 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00004690
4691 l = nasm_malloc(sizeof(Line));
4692 l->next = predef;
4693 l->first = def;
H. Peter Anvin538002d2008-06-28 18:30:27 -07004694 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00004695 predef = l;
4696}
4697
Keith Kaniosb7a89542007-04-12 02:40:54 +00004698/*
4699 * Added by Keith Kanios:
4700 *
4701 * This function is used to assist with "runtime" preprocessor
4702 * directives. (e.g. pp_runtime("%define __BITS__ 64");)
4703 *
4704 * ERRORS ARE IGNORED HERE, SO MAKE COMPLETELY SURE THAT YOU
4705 * PASS A VALID STRING TO THIS FUNCTION!!!!!
4706 */
4707
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004708void pp_runtime(char *definition)
Keith Kaniosb7a89542007-04-12 02:40:54 +00004709{
4710 Token *def;
H. Peter Anvin70653092007-10-19 14:42:29 -07004711
Keith Kaniosb7a89542007-04-12 02:40:54 +00004712 def = tokenize(definition);
4713 if(do_directive(def) == NO_DIRECTIVE_FOUND)
4714 free_tlist(def);
H. Peter Anvin70653092007-10-19 14:42:29 -07004715
Keith Kaniosb7a89542007-04-12 02:40:54 +00004716}
4717
H. Peter Anvina70547f2008-07-19 21:44:26 -07004718void pp_extra_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004719{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004720 extrastdmac = macros;
4721}
4722
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004723static void make_tok_num(Token * tok, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004724{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004725 char numbuf[20];
Keith Kaniosa5fc6462007-10-13 07:09:22 -07004726 snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvineba20a72002-04-30 20:53:55 +00004727 tok->text = nasm_strdup(numbuf);
4728 tok->type = TOK_NUMBER;
4729}
4730
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004731Preproc nasmpp = {
4732 pp_reset,
4733 pp_getline,
4734 pp_cleanup
4735};